Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/multitrace.py: 78%
40 statements
« prev ^ index » next coverage.py v6.5.0, created at 2024-03-07 11:54 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2024-03-07 11:54 +0000
1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6'''
7Multi-component waveform data model.
8'''
11import numpy as num
13from . import trace
14from .guts import Object, Float, Timestamp, List
15from .guts_array import Array
16from .squirrel.model import CodesNSLCE
19class MultiTrace(Object):
20 '''
21 Container for multi-component waveforms with common time span and sampling.
23 Instances of this class can be used to efficiently represent
24 multi-component waveforms of a single sensor or of a sensor array. The data
25 samples are stored in a single 2D array where the first index runs over
26 components and the second index over time. Metadata contains sampling rate,
27 start-time and :py:class:`~pyrocko.squirrel.model.CodesNSLCE` identifiers
28 for the contained traces.
30 :param traces:
31 If given, construct multi-trace from given single-component waveforms
32 (see :py:func:`~pyrocko.trace.get_traces_data_as_array`) and ignore
33 any other arguments.
34 :type traces:
35 :py:class:`list` of :py:class:`~pyrocko.trace.Trace`
36 '''
38 codes = List.T(
39 CodesNSLCE.T(),
40 help='List of codes identifying the components.')
41 data = Array.T(
42 shape=(None, None),
43 help='Array containing the data samples indexed as '
44 '``(icomponent, isample)``.')
45 tmin = Timestamp.T(
46 default=Timestamp.D('1970-01-01 00:00:00'),
47 help='Start time.')
48 deltat = Float.T(
49 default=1.0,
50 help='Sampling interval [s]')
52 def __init__(
53 self,
54 traces=None,
55 codes=None,
56 data=None,
57 tmin=None,
58 deltat=None):
60 if traces is not None:
61 if len(traces) == 0:
62 data = num.zeros((0, 0))
63 else:
64 data = trace.get_traces_data_as_array(traces)
65 deltat = traces[0].deltat
66 tmin = traces[0].tmin
67 codes = [tr.codes for tr in traces]
69 self.ntraces, self.nsamples = data.shape
71 if codes is None:
72 codes = [CodesNSLCE()] * self.ntraces
74 if len(codes) != self.ntraces:
75 raise ValueError(
76 'MultiTrace construction: mismatch between number of traces '
77 'and number of codes given.')
79 if deltat is None:
80 deltat = self.T.deltat.default()
82 if tmin is None:
83 tmin = self.T.tmin.default()
85 Object.__init__(self, codes=codes, data=data, tmin=tmin, deltat=deltat)
87 def __len__(self):
88 '''
89 Get number of components.
90 '''
91 return self.ntraces
93 def __getitem__(self, i):
94 '''
95 Get single component waveform (shared data).
97 :param i:
98 Component index.
99 :type i:
100 int
101 '''
102 return self.get_trace(i)
104 @property
105 def tmax(self):
106 '''
107 End time (time of last sample, read-only).
108 '''
109 return self.tmin + (self.nsamples - 1) * self.deltat
111 def get_trace(self, i):
112 '''
113 Get single component waveform (shared data).
115 :param i:
116 Component index.
117 :type i:
118 int
119 '''
121 network, station, location, channel, extra = self.codes[i]
122 return trace.Trace(
123 network=network,
124 station=station,
125 location=location,
126 channel=channel,
127 extra=extra,
128 tmin=self.tmin,
129 deltat=self.deltat,
130 ydata=self.data[i, :])
132 def snuffle(self):
133 '''
134 Show in Snuffler.
135 '''
136 trace.snuffle(list(self))