Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/multitrace.py: 77%

39 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2024-01-10 08:51 +0000

1import numpy as num 

2 

3from . import trace 

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

5from .guts_array import Array 

6from .squirrel.model import CodesNSLCE 

7 

8 

9class MultiTrace(Object): 

10 codes = List.T(CodesNSLCE.T()) 

11 data = Array.T(shape=(None, None)) 

12 tmin = Timestamp.T(default=Timestamp.D('1970-01-01 00:00:00')) 

13 deltat = Float.T(default=1.0) 

14 

15 def __init__( 

16 self, 

17 traces=None, 

18 codes=None, 

19 data=None, 

20 tmin=None, 

21 deltat=None): 

22 

23 if traces is not None: 

24 

25 if len(traces) == 0: 

26 data = num.zeros((0, 0)) 

27 else: 

28 data = trace.get_traces_data_as_array(traces) 

29 deltat = traces[0].deltat 

30 tmin = traces[0].tmin 

31 codes = [tr.codes for tr in traces] 

32 

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

34 

35 if codes is None: 

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

37 

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

39 raise ValueError( 

40 'MultiTrace construction: mismatch between number of traces ' 

41 'and number of codes given.') 

42 

43 if deltat is None: 

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

45 

46 if tmin is None: 

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

48 

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

50 

51 def __len__(self): 

52 return self.ntraces 

53 

54 def __getitem__(self, i): 

55 return self.get_trace(i) 

56 

57 @property 

58 def tmax(self): 

59 return self.tmin + (self.nsamples - 1) * self.deltat 

60 

61 def get_trace(self, i): 

62 network, station, location, channel, extra = self.codes[i] 

63 return trace.Trace( 

64 network=network, 

65 station=station, 

66 location=location, 

67 channel=channel, 

68 extra=extra, 

69 tmin=self.tmin, 

70 deltat=self.deltat, 

71 ydata=self.data[i, :]) 

72 

73 def snuffle(self): 

74 trace.snuffle(list(self))