%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('ggplot')
plt.rcParams['figure.figsize'] = 12, 8
Stream
object.from obspy import read
st = read("./data/waveform_PFO.mseed", format="mseed")
print(st)
automatic file format detection, no need to worry about file formats
from obspy import read
st = read("./data/waveform_*")
print(st)
from obspy import UTCDateTime
t = UTCDateTime("2011-03-11T05:46:23.015400Z")
st = read("./data/waveform_*", starttime=t + 10 * 60, endtime=t + 12 * 60)
print(st)
from obspy import read
st = read("./data/waveform_PFO.mseed")
print(type(st))
print(st)
print(st.traces)
print(st[0])
+
operator (or using the .append()
and .extend()
methods)st1 = read("./data/waveform_PFO.mseed")
st2 = read("./data/waveform_PFO_synthetics.mseed")
st = st1 + st2
print(st)
st3 = read("./data/waveform_BFO_BHE.sac")
st += st3
print(st)
for tr in st:
print(tr.id)
Trace.stats
) that fully describes the time series by specifying..st = read("./data/waveform_PFO.mseed")
tr = st[0] # get the first Trace in the Stream
print(tr)
print(tr.stats)
print(tr.stats.delta, "|", tr.stats.endtime)
tr.stats.sampling_rate = 5.0
print(tr.stats.delta, "|", tr.stats.endtime)
print(tr.stats.npts)
tr.data = tr.data[:100]
print(tr.stats.npts, "|", tr.stats.endtime)
tr = read("./data/waveform_PFO.mseed")[0]
tr.plot()
print(tr)
tr.resample(sampling_rate=100.0)
print(tr)
print(tr)
tr.trim(tr.stats.starttime + 12 * 60, tr.stats.starttime + 14 * 60)
print(tr)
tr.plot()
tr.detrend("linear")
tr.taper(max_percentage=0.05, type='cosine')
tr.filter("lowpass", freq=0.1)
tr.plot()
# try tr.<Tab> for other methods defined for Trace
tr.detrend?
numpy.ndarray
(as Trace.data
)print(tr.data[:20])
..by doing arithmetic operations (fast, handled in C by NumPy)
print(tr.data ** 2 + 0.5)
..by using numpy.ndarray
builtin methods (also done in C by NumPy)
print(tr.data.max())
print(tr.data.mean())
print(tr.data.ptp())
# try tr.data.<Tab> for a list of numpy methods defined on ndarray
..by using numpy
functions (also done in C by NumPy)
import numpy as np
print(np.abs(tr.data))
# you can try np.<Tab> but there is a lot in there
# try np.a<Tab>
..by feeding pointers to existing C/Fortran routines from inside Python!
This is done internally in several places, e.g. for cross correlations, beamforming or in third-party filetype libraries like e.g. libmseed.
numpy.ndarray
(e.g. when needing to parse waveforms from non-standard ascii files)from obspy import Trace
x = np.random.randint(-100, 100, 500)
tr = Trace(data=x)
tr.stats.station = "XYZ"
tr.stats.starttime = UTCDateTime()
tr.plot()
from obspy import Stream
tr2 = Trace(data=np.random.randint(-300, 100, 1000))
tr2.stats.starttime = UTCDateTime()
tr2.stats.sampling_rate = 10.0
st = Stream([tr, tr2])
st.plot()
Stream
/ Trace
¶st.<Tab>
).st.filter()
- Filter all attached traces.st.trim()
- Cut all traces.st.resample()
/ st.decimate()
- Change the sampling rate.st.trigger()
- Run triggering algorithms.st.plot()
/ st.spectrogram()
- Visualize the data.st.attach_response()
/st.remove_response()
, st.simulate()
- Instrument correctionst.merge()
, st.normalize()
, st.detrend()
, st.taper()
, ...Stream
object can also be exported to many formats, so ObsPy can be used to convert between different file formats.st = read("./data/waveform_*.sac")
st.write("output_file.mseed", format="MSEED")
!ls -l output_file*
numpy.ndarray
with zeros and (e.g. use numpy.zeros()
) and put an ideal pulse somewhere in itTrace
object with your data array
x = np.zeros(300)
x[100] = 1.0
tr = Trace(data=x)
tr.stats.station = "ABC"
tr.stats.sampling_rate = 20.0
tr.stats.starttime = UTCDateTime(2014, 2, 24, 15, 0, 0)
print(tr)
tr.plot()
tr.filter(...)
and apply a lowpass filter with a corner frequency of 1 Hertz.
tr.filter("lowpass", freq=1)
tr.plot()
tr.trim(...)
to remove some of the zeros at start and at the end
tr.trim(tr.stats.starttime + 3, tr.stats.endtime - 5)
tr.plot()
np.random.randn()
)
tr.data = tr.data * 500
tr.data = tr.data + np.random.randn(len(tr))
tr.plot()
st = read("./data/waveform_*")
print(st)
st.select()
to only keep traces of station BFO in the stream. Show the preview plot.
st = st.select(station="BFO")
st.plot()
wlen=50
for the spectrogram plot)
t1 = UTCDateTime(2011, 3, 11, 5, 55)
st.trim(t1, t1 + 10 * 60)
st.plot()
st.spectrogram(log=True, wlen=50);
st.detrend("linear")
st.filter("lowpass", freq=0.1)
st.plot()