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-20 14:04 +0000

1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

4# ---|P------/S----------~Lg---------- 

5 

6''' 

7Multi-component waveform data model. 

8''' 

9 

10 

11import numpy as num 

12 

13from . import trace 

14from .guts import Object, Float, Timestamp, List 

15from .guts_array import Array 

16from .squirrel.model import CodesNSLCE 

17 

18 

19class MultiTrace(Object): 

20 ''' 

21 Container for multi-component waveforms with common time span and sampling. 

22 

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. 

29 

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 ''' 

37 

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]') 

51 

52 def __init__( 

53 self, 

54 traces=None, 

55 codes=None, 

56 data=None, 

57 tmin=None, 

58 deltat=None): 

59 

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] 

68 

69 self.ntraces, self.nsamples = data.shape 

70 

71 if codes is None: 

72 codes = [CodesNSLCE()] * self.ntraces 

73 

74 if len(codes) != self.ntraces: 

75 raise ValueError( 

76 'MultiTrace construction: mismatch between number of traces ' 

77 'and number of codes given.') 

78 

79 if deltat is None: 

80 deltat = self.T.deltat.default() 

81 

82 if tmin is None: 

83 tmin = self.T.tmin.default() 

84 

85 Object.__init__(self, codes=codes, data=data, tmin=tmin, deltat=deltat) 

86 

87 def __len__(self): 

88 ''' 

89 Get number of components. 

90 ''' 

91 return self.ntraces 

92 

93 def __getitem__(self, i): 

94 ''' 

95 Get single component waveform (shared data). 

96 

97 :param i: 

98 Component index. 

99 :type i: 

100 int 

101 ''' 

102 return self.get_trace(i) 

103 

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 

110 

111 def get_trace(self, i): 

112 ''' 

113 Get single component waveform (shared data). 

114 

115 :param i: 

116 Component index. 

117 :type i: 

118 int 

119 ''' 

120 

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, :]) 

131 

132 def snuffle(self): 

133 ''' 

134 Show in Snuffler. 

135 ''' 

136 trace.snuffle(list(self))