Search code examples
pythonobspy

In Obspy, how can I get an amplitude value at a specific time?


Suppose I have an object Trace, say trace, already and I want to have an amplitude data at the time 30 sec.

I think I can do it like below, assuming begin time is 0 for simplicity,

delta = trace.stats.delta
count = int(30 / delta)
target_value = trace.data[count]

Do they prepare a good way to do it? something like....

trace.foo(time=30)

Solution

  • Right now we do not have such a convenience method, but I agree that it would be a good addition. You could make a feature request in our GitHub issue tracker: https://github.com/obspy/obspy/issues/new

    In the meantime, you could make use of the sample times convenience functions mixed with some numpy to achieve what you want..

    If you're looking for times relative to start of trace:

    from obspy import read
    
    tr = read()[0]
    my_time = 4  # 4 seconds after start of trace
    times = tr.times()
    index = times.searchsorted(my_time)
    print(times[index])
    print(tr.data[index])
    

    4.0
    204.817965896
    

    If you're looking for absolute times:

    from obspy import read, UTCDateTime
    
    tr = read()[0]
    my_time = UTCDateTime("2009-08-24T00:20:12.0Z")  # 4 seconds after start of trace
    times = tr.times('utcdatetime')
    index = times.searchsorted(my_time)
    print(times[index])
    print(tr.data[index])
    

    2009-08-24T00:20:12.000000Z
    156.68994731
    

    If you're not matching the exact time of one sample, you will have to manually interpolate.

    Hope this gets you started.