Ejemplo n.º 1
0
def make_simulation_wavelet(n=100,
                            dt=0.05,
                            t0=-1.0,
                            imp=(20.0, -15.0, 4.0, -1.0),
                            lag=(20, 24, 35, 45),
                            npoles=3,
                            corners=[2.0, 6.0]):
    dvec = make_impulse_vector(lag, imp, n)
    fsampling = int(1.0 / dt)
    sos = signal.butter(npoles,
                        corners,
                        btype='bandpass',
                        output='sos',
                        fs=fsampling)
    f = signal.sosfilt(sos, dvec)
    wavelet = TimeSeries(n)
    wavelet.set_t0(t0)
    wavelet.set_dt(dt)
    # This isn't necessary at the moment because relative is the default
    #wavelet.set_tref(TimeReferenceType.Relative)
    wavelet.set_npts(n)
    wavelet.set_live()
    for i in range(n):
        wavelet.data[i] = f[i]
    return wavelet
Ejemplo n.º 2
0
def get_live_timeseries():
    ts = TimeSeries()
    ts.set_live()
    ts.dt = 1 / sampling_rate
    ts.npts = ts_size
    # ts.put('net', 'IU')
    ts.put('npts', ts_size)
    ts.put('sampling_rate', sampling_rate)
    ts.tref = TimeReferenceType.UTC
    ts.t0 = datetime.utcnow().timestamp()
    ts['delta'] = 0.1
    ts['calib'] = 0.1
    ts['site_id'] = bson.objectid.ObjectId()
    ts['channel_id'] = bson.objectid.ObjectId()
    ts['source_id'] = bson.objectid.ObjectId()
    ts.set_as_origin('test', '0', '0', AtomicType.TIMESERIES)
    ts.data = DoubleVector(np.random.rand(ts_size))
    return ts
Ejemplo n.º 3
0
def get_live_timeseries():
    ts = TimeSeries()
    ts.set_live()
    ts.dt = 1 / sampling_rate
    ts.npts = ts_size
    # ts.put('net', 'IU')
    ts.put("npts", ts_size)
    ts.put("sampling_rate", sampling_rate)
    ts.tref = TimeReferenceType.UTC
    ts.t0 = datetime.utcnow().timestamp()
    ts["delta"] = 0.1
    ts["calib"] = 0.1
    ts["site_id"] = bson.objectid.ObjectId()
    ts["channel_id"] = bson.objectid.ObjectId()
    ts["source_id"] = bson.objectid.ObjectId()
    ts.set_as_origin("test", "0", "0", AtomicType.TIMESERIES)
    ts.data = DoubleVector(np.random.rand(ts_size))
    return ts
Ejemplo n.º 4
0
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