obsplus.utils.waveforms module

Stream utilities

obsplus.utils.waveforms.archive_to_sds(bank, sds_path, starttime=None, endtime=None, overlap=30, type_code='D', stream_processor=None)[source]

Create a seiscomp data structure archive from a waveform source.

Parameters:
  • bank (Union[Path, str, WaveBank]) – A wavebank or path to such.

  • sds_path (Union[Path, str]) – The path for the new sds archive to be created.

  • starttime (Optional[UTCDateTime]) – If not None, the starttime to convert data from bank.

  • endtime (Optional[UTCDateTime]) – If not None, the endtime to convert data from bank.

  • overlap (float) – The overlap to use for each file.

  • type_code (str) – The str indicating the datatype.

  • stream_processor (Optional[callable]) – A callable that will take a single stream as input and return a a single stream. May return and empty stream to skip a stream.

Notes

see: https://www.seiscomp3.org/doc/applications/slarchive/SDS.html

obsplus.utils.waveforms.get_waveform_bulk_df(bulk)[source]

Create a bulk request dataframe from a variety of inputs.

Parameters:

bulk

A sequence of

[network, station, location, channel, starttime, endtime]

or a dataframe with columns named the same.

Return type:

A dataframe with waveform bulk columns.

obsplus.utils.waveforms.get_waveform_client(waveforms)[source]

Extract a waveform client from various inputs.

Parameters:

waveforms (Union[WaveformClient, str, Path, Trace, Stream]) –

Any of the following:
  • A path to an obspy-readable waveform file

  • A path to a directory of obspy-readable waveform files

  • A obspy.Stream instance

  • An instance of WaveBank

  • Any other object that has a get_waveforms method

Raises:

TypeError – If a waveform client cannot be determined from the input.

Return type:

WaveformClient

Notes

If the output does not define a get_waveform_bulk method one will be added.

obsplus.utils.waveforms.merge_traces(st, inplace=False)[source]

An efficient function to merge overlapping data for a stream.

This function is equivalent to calling merge(1) and split() then returning the resulting trace. This means only traces that have overlaps or adjacent times will be merged, otherwise they will remain separate traces.

Parameters:
  • st (TypeVar(trace_sequence, Stream, MutableSequence[Trace])) – The input stream to merge.

  • inplace – If True st is modified in place.

Return type:

A stream with merged traces.

obsplus.utils.waveforms.stream2contiguous(stream)[source]

Yields trimmed streams for times which all traces have data.

Parameters:

stream (Stream) – The input stream

Return type:

Stream

Examples

>>> import obspy
>>> st = obspy.read()
>>> t1, t2 = st[0].stats.starttime, st[0].stats.endtime
>>> _ = st[0].trim(endtime=t2 - 2)  # remove data at end of one trace
>>> out = stream2contiguous(st)
>>> # stream2contiguous should now have trimmed all traces to match
>>> assert all(len(tr.data) for tr in st)
obsplus.utils.waveforms.stream_bulk_split(st, bulk, fill_value=None)[source]

Split a stream into a list of streams that meet requirements in bulk.

This is similar to the get_waveforms_bulk methods of waveform_client, but rather than merging any overlapping data it is returned in a list of traces.

Parameters:
  • st (Stream) – A stream object

  • bulk (List[Sequence[Tuple[str, str, str, str, Union[str, UTCDateTime, float, datetime64, Timestamp], Union[str, UTCDateTime, float, datetime64, Timestamp]]]]) – A bulk request. Wildcards not currently supported on str params.

  • fill_value (Optional[Any]) – If not None fill any missing data in time range with this value.

Return type:

List of traces, each meeting the corresponding request in bulk.

obsplus.utils.waveforms.trim_event_stream(stream, merge=1, copy=True, trim_tolerance=None, required_len=0.95)[source]

Trim the waveforms to a common start time and end time.

Uses latest start and earliest end for each trace, unless an abnormally short trace is found.

Parameters:
  • stream (obspy.Stream) – The waveforms to trim

  • merge (int, optional) – If not None, merge the waveforms with this method before trimming. See obspy waveforms docs for merge options

  • copy (bool) – Copy the waveforms before altering it.

  • trim_tolerance – The number of seconds the trimmed starttime or endtime can vary from the starttime/endtime in the current traces.

  • required_len (Optional[float]) – The fraction of the longest len a trace must have to be considered good. If any trace is shorter remove it.

Returns:

The merged waveforms

Return type:

stream