Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/squirrel/io/backends/stationxml.py: 96%
71 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-04 09:52 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-04 09:52 +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.stationxml`.
8'''
10import logging
11import time
12import copy
13from pyrocko.io.io_common import get_stats, touch # noqa
14from ... import model
17logger = logging.getLogger('psq.io.stationxml')
20Y = 60*60*24*365
23def provided_formats():
24 return ['stationxml']
27def detect(first512):
28 if first512.find(b'<FDSNStationXML') != -1:
29 return 'stationxml'
31 return None
34def iload(format, file_path, segment, content):
35 assert format == 'stationxml'
37 far_future = time.time() + 20*Y
39 from pyrocko.io import stationxml
40 value_or_none = stationxml.value_or_none
42 sx = stationxml.load_xml(filename=file_path)
44 inut = 0
46 for network in sx.network_list:
47 for station in network.station_list:
48 net = network.code
49 sta = station.code
51 tmin = station.start_date
52 tmax = station.end_date
53 if tmax is not None and tmax > far_future:
54 tmax = None
56 station_nut = model.make_station_nut(
57 file_segment=0,
58 file_element=inut,
59 codes=model.CodesNSL(net, sta, '*'),
60 tmin=tmin,
61 tmax=tmax)
63 if 'station' in content:
64 station_nut.content = model.Station(
65 lat=station.latitude.value,
66 lon=station.longitude.value,
67 elevation=value_or_none(station.elevation),
68 **station_nut.station_kwargs)
70 station_copy = copy.copy(station)
71 station_copy.channel_list = []
73 station_nut.raw_content['stationxml'] = station_copy
75 yield station_nut
76 inut += 1
78 for channel in station.channel_list:
79 cha = channel.code
80 loc = channel.location_code.strip()
82 tmin = channel.start_date
83 tmax = channel.end_date
84 if tmax is not None and tmax > far_future:
85 tmax = None
87 deltat = None
88 if channel.sample_rate is not None \
89 and channel.sample_rate.value != 0.0:
91 deltat = 1.0 / channel.sample_rate.value
93 if deltat is None and channel.response:
94 out_rate_resp = channel.response.output_sample_rate
95 if out_rate_resp:
96 deltat = 1.0 / out_rate_resp
98 nut = model.make_channel_nut(
99 file_segment=0,
100 file_element=inut,
101 codes=model.CodesNSLCE(net, sta, loc, cha, ''),
102 tmin=tmin,
103 tmax=tmax,
104 deltat=deltat)
106 if 'channel' in content:
107 nut.content = model.Channel(
108 lat=channel.latitude.value,
109 lon=channel.longitude.value,
110 elevation=value_or_none(channel.elevation),
111 depth=value_or_none(channel.depth),
112 azimuth=value_or_none(channel.azimuth),
113 dip=value_or_none(channel.dip),
114 **nut.channel_kwargs)
116 channel_copy = copy.copy(channel)
117 channel_copy.response = None
118 nut.raw_content['stationxml'] = channel_copy
120 yield nut
121 inut += 1
123 context = '%s.%s.%s.%s' % (net, sta, loc, cha)
125 if channel.response:
127 nut = model.make_response_nut(
128 file_segment=0,
129 file_element=inut,
130 codes=model.CodesNSLCE(net, sta, loc, cha, ''),
131 tmin=tmin,
132 tmax=tmax,
133 deltat=deltat)
135 try:
136 resp = channel.response.get_squirrel_response(
137 context, **nut.response_kwargs)
139 if 'response' in content:
140 nut.content = resp
141 nut.raw_content['stationxml'] = channel.response
143 yield nut
144 inut += 1
146 except stationxml.StationXMLError as e:
147 logger.debug('Bad instrument response: %s' % str(e))