def test_PowerSpectrum(): ts = TimeSeries(100) ts.data[0] = 1.0 # delta function - spectrum will be flat ts.live = True engine = MTPowerSpectrumEngine(100, 5, 10) spec = engine.apply(ts) assert spec.Nyquist() == spec.f0 + spec.df * spec.nf() spec_copy = pickle.loads(pickle.dumps(spec)) assert spec.df == spec_copy.df assert spec.f0 == spec_copy.f0 assert spec.spectrum_type == spec_copy.spectrum_type assert np.allclose(spec.spectrum, spec_copy.spectrum)
def make_wavelet_noise_data(nscale=0.1, ns=2048, padlength=512, dt=0.05, npoles=3, corners=[0.08, 0.8]): wn = TimeSeries(ns) wn.t0 = 0.0 wn.dt = dt wn.tref = TimeReferenceType.Relative wn.live = True nd = ns + 2 * padlength y = nscale * randn(nd) sos = signal.butter(npoles, corners, btype='bandpass', output='sos', fs=1.0 / dt) y = signal.sosfilt(sos, y) for i in range(ns): wn.data[i] = y[i + padlength] return (wn)
def test_TimeSeries(): ts = TimeSeries() ts.npts = 100 ts.t0 = 0.0 ts.dt = 0.001 ts.live = 1 ts.tref = TimeReferenceType.Relative ts.data.append(1.0) ts.data.append(2.0) ts.data.append(3.0) ts.data.append(4.0) ts.sync_npts() assert ts.npts == 104 assert ts.npts == ts["npts"] ts += ts for i in range(4): ts.data[i] = i * 0.5 ts_copy = pickle.loads(pickle.dumps(ts)) assert ts.data == ts_copy.data assert ts.data[3] == 1.5 assert ts.data[103] == 8 assert ts.time(100) == 0.1 assert ts.sample_number(0.0998) == 100
def Trace2TimeSeries(trace, history=None): """ Convert an obspy Trace object to a TimeSeries object. An obspy Trace object mostly maps directly into the mspass TimeSeries object with the stats of Trace mapping (almost) directly to the TimeSeries Metadata object that is a base class to TimeSeries. A deep copy of the data vector in the original Trace is made to the result. That copy is done in C++ for speed (we found a 100+ fold speedup using that mechanism instead of a simple python loop) There is one important type collision in copying obspy starttime and endtime stats fields. obspy uses their UTCDateTime object to hold time but TimeSeries only supports an epoch time (UTCDateTime.timestamp) so the code here has to convert from the UTCDateTime to epoch time in the TimeSeries. Note in a TimeSeries starttime is the t0 attribute. The biggest mismatch in Trace and TimeSeries is that Trace has no concept of object level history as used in mspass. That history must be maintained outside obspy. To maintain full history the user must pass the history maintained externally through the optional history parameter. The contents of history will be loaded directly into the result with no sanity checks. :param trace: obspy trace object to convert :type trace: :class:`~obspy.core.trace.Trace` :param history: mspass ProcessingHistory object to post to result. :return: TimeSeries object derived from obpsy input Trace object :rtype: :class:`~mspasspy.ccore.TimeSeries` """ # The obspy trace object stats attribute only acts like a dictionary # we can't use it directly but this trick simplifies the copy to # mesh with py::dict for pybind11 - needed in TimeSeries constructor below h = dict(trace.stats) # These tests are excessively paranoid since starttime and endtime # are required attributes in Trace, but better save in case # someone creates one outside obspy if Keywords.starttime in trace.stats: t = h[Keywords.starttime] h[Keywords.starttime] = t.timestamp else: # We have to set this to something if it isn't set or # the TimeSeries constructor may abort h[Keywords.starttime] = 0.0 # we don't require endtime in TimeSeries so ignore if it is not set if "endtime" in trace.stats: t = h["endtime"] h["endtime"] = t.timestamp # # these define a map of aliases to apply when we convert to mspass # metadata from trace - we redefined these names but others could # surface as obspy evolves independently from mspass mspass_aliases = dict() mspass_aliases["station"] = Keywords.sta mspass_aliases["network"] = Keywords.net mspass_aliases["location"] = Keywords.loc mspass_aliases["channel"] = Keywords.chan for k in mspass_aliases: if k in h: x = h.pop(k) alias_key = mspass_aliases[k] h[alias_key] = x dout = TimeSeries(h, trace.data) if history != None: dout.load_history(history) dout.set_live() # The following dead_mspass attribute is used by our decorator API # to determine whether an object was dead before the conversion. try: if trace.dead_mspass: dout.live = False except AttributeError: pass return dout