Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/squirrel/io/backends/sac.py: 98%
61 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-06 06:59 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-06 06:59 +0000
1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6'''
7Squirrel IO adaptor to :py:mod:`pyrocko.io.sac`.
8'''
10from pyrocko.io.io_common import get_stats, touch # noqa
11from pyrocko import trace
12from ... import model
15km = 1000.
18def provided_formats():
19 return ['sac']
22def detect(first512):
23 from pyrocko.io import sac
25 if sac.detect(first512):
26 return 'sac'
27 else:
28 return None
31def agg(*ds):
32 out = {}
33 for d in ds:
34 out.update(d)
36 return out
39def nonetoempty(x):
40 if x is None:
41 return x
42 else:
43 return x.strip()
46def iload(format, file_path, segment, content):
47 assert format == 'sac'
49 from pyrocko.io import sac
51 load_data = 'waveform' in content
53 s = sac.SacFile(file_path, load_data=load_data)
55 codes = model.CodesNSLCE(
56 nonetoempty(s.knetwk),
57 nonetoempty(s.kstnm),
58 nonetoempty(s.khole),
59 nonetoempty(s.kcmpnm))
61 tmin = s.get_ref_time() + s.b
62 tmax = tmin + s.delta * s.npts
64 tspan = dict(
65 tmin=tmin,
66 tmax=tmax,
67 deltat=s.delta)
69 inut = 0
70 nut = model.make_waveform_nut(
71 file_segment=0,
72 file_element=inut,
73 codes=codes,
74 **tspan)
76 if 'waveform' in content:
77 nut.content = trace.Trace(
78 ydata=s.data[0],
79 **nut.waveform_kwargs)
81 yield nut
82 inut += 1
84 if None not in (s.stla, s.stlo):
85 position = dict(
86 lat=s.stla,
87 lon=s.stlo,
88 elevation=s.stel,
89 depth=s.stdp)
91 nut = model.make_station_nut(
92 file_segment=0,
93 file_element=inut,
94 codes=model.CodesNSL(*codes.nsl),
95 **tspan)
97 if 'station' in content:
98 nut.content = model.Station(
99 **agg(position, nut.station_kwargs))
101 yield nut
102 inut += 1
104 dip = None
105 if s.cmpinc is not None:
106 dip = s.cmpinc - 90.
108 nut = model.make_channel_nut(
109 file_segment=0,
110 file_element=inut,
111 codes=codes,
112 **tspan)
114 if 'channel' in content:
115 nut.content = model.Channel(
116 azimuth=s.cmpaz,
117 dip=dip,
118 **agg(position, nut.channel_kwargs))
120 yield nut
121 inut += 1
123 if None not in (s.evla, s.evlo, s.o):
124 etime = s.get_ref_time() + s.o
125 depth = None
126 if s.evdp is not None:
127 depth = s.evdp # * km # unclear specs
129 nut = model.make_event_nut(
130 codes=model.CodesX(''),
131 # name=nonetoempty(s.kevnm),
132 file_segment=0,
133 file_element=inut,
134 tmin=etime,
135 tmax=etime)
137 if 'event' in content:
138 nut.content = model.Event(
139 lat=s.evla,
140 lon=s.evlo,
141 depth=depth,
142 magnitude=s.mag,
143 **nut.event_kwargs)
145 yield nut
146 inut += 1