# obspy.core.trace.Trace.slice

Hi

Using obspy to read data and cut the returned Traces into smaller segments I note that the slice method on the obspy.core.trace.Trace includes the data point at obspy.core.trace.Trace.stats.endtime, e.g:

``````>>> from obspy import Trace
>>> import numpy
>>> tr = Trace(data=numpy.arange(0, 10))
>>> tr.stats.delta = 1.0
>>> t = tr.stats.starttime
>>> print tr.data
[0 1 2 3 4 5 6 7 8 9]
>>> tr2 = tr.slice(t, t+5)
>>> print tr2.data,tr2.stats.endtime
[0 1 2 3 4 5] 1970-01-01T00:00:05.000000Z
>>> tr3 = tr.slice(t+5, t+10)
>>> print tr3.data,tr3.stats.endtime
[5 6 7 8 9] 1970-01-01T00:00:09.000000Z
``````

Now, the question here is if this is the standard way to handle slicing of a uniformly spaced datavector. My point being is that if you have time series data, a conservative way to look at the data inbetween your known measurements is that is constant from the one measurment until the next. I.e. if you at time 0 measure the value 10 of a given system, you do not know the value of the system until you make your next measurement, hence the conservative thing to do is to assume it to be the last measured value (in this case 10). Adopting this approach each data point can be considered to have a duration of validity equal to the time separation between your measurements. Thus following this logic I would expect the above to have yielded the output:

``````>>> from obspy import Trace
>>> import numpy
>>> tr = Trace(data=numpy.arange(0, 10))
>>> tr.stats.delta = 1.0
>>> t = tr.stats.starttime
>>> print tr.data
[0 1 2 3 4 5 6 7 8 9]
>>> tr2 = tr.slice(t, t+5)
>>> print tr2.data,tr2.stats.endtime
[0 1 2 3 4] 1970-01-01T00:00:05.000000Z
>>> tr3 = tr.slice(t+5, t+10)
>>> print tr3.data,tr3.stats.endtime
[5 6 7 8 9] 1970-01-01T00:00:10.000000Z
``````

From a practical point of view the current way the slice method works prevents slicing a trace into shorter (non-overlapping) traces using an algorithm on the form:

``````traces = []
t = tr.stats.starttime
stop = tr.stats.endtime
trace_lenght = 5
while t < stop:
traces.append(tr.slice(t,t+trace_length))
t += trace_length

``````

so question is how to best slice a Trace into smaller non-overlapping traces? Would it perhaps be possible to add an argument to the slice method to not include the data point at argument endtime?

regards Peter

När du har kontakt med oss på Uppsala universitet med e-post så innebär det att vi behandlar dina personuppgifter. För att läsa mer om hur vi gör det kan du läsa här: http://www.uu.se/om-uu/dataskydd-personuppgifter/ E-mailing Uppsala University means that we will process your personal data. For more information on how this is performed, please read here: http://www.uu.se/en/about-uu/data-protection-policy

Forgott to add, it would at least be nice if the documentation of the method points out that the data point at endtime will be included in the slice, similar to numpy.arange pointing out that the data point at the time given by argument stop is not included in the interval.

I don't use it much, so not sure about the endpoint with that one, though.
We're always happy to accept pull requests with improvements to docs.

cheers,
T

Mmmm, was sort of hoping that some of you guys that are already fiddling
with the code could have a go at it so I (being lazy as usual) don't
have to go thgough figuring out howto submit a pull request...
Eventually I hope to be able to contribute but for now that'll have to
wait, sorry

\p