def test_parse_obspy_objects(): filename = os.path.join(DATA, "TA.Q56A..BH.xml") inv = obspy.read_inventory(filename) receivers = Receiver.parse(inv) assert len(receivers) == 1 rec = receivers[0] assert (rec.latitude, rec.longitude, rec.network, rec.station) == ( elliptic_to_geocentric_latitude(39.041), -79.1871, "TA", "Q56A", ) receivers = Receiver.parse(inv[0]) assert len(receivers) == 1 rec = receivers[0] assert (rec.latitude, rec.longitude, rec.network, rec.station) == ( elliptic_to_geocentric_latitude(39.041), -79.1871, "TA", "Q56A", ) receivers = Receiver.parse(inv[0][0], network_code="TA") assert len(receivers) == 1 rec = receivers[0] assert (rec.latitude, rec.longitude, rec.network, rec.station) == ( elliptic_to_geocentric_latitude(39.041), -79.1871, "TA", "Q56A", )
def test_equality_methods(): """ Tests the (in)equality methods of source and receiver objects. They both inherit from the same base class thus they are tested here - but it would also be suitable for the receiver tests. """ src1 = Source(latitude=1, longitude=2) src2 = Source(latitude=3, longitude=3) rec1 = Receiver(latitude=1, longitude=2) rec2 = Receiver(latitude=3, longitude=3) assert src1 == src1 assert src2 == src2 assert rec1 == rec1 assert rec2 == rec2 assert src1 != src2 assert src1 != rec1 assert src1 != rec2 assert src2 != src1 assert src2 != rec1 assert src2 != rec2 assert rec1 != rec2 assert rec1 != src1 assert rec1 != src2 assert rec2 != src1 assert rec2 != src1 assert rec2 != src2
def test_parse_sac_file_without_coordinates(): filename = os.path.join(DATA, "example_without_coordinates.sac") with pytest.raises(ReceiverParseError) as e: Receiver.parse(filename) assert "SAC file does not contain coordinates for channel".lower() in \ str(e).lower()
def test_station_x_y_z(): station = Receiver(latitude=42.6390, longitude=74.4940, depth_in_m=0.0) assert abs(station.x() - 1252949.21995) < 1E-5 assert abs(station.y() - 4516152.38916) < 1E-5 assert abs(station.z() - 4315567.96379) < 1E-5 assert abs(station.colatitude - 47.3609999) < 1E-5 assert station.depth_in_m == 0.0 assert station.radius_in_m() == 6371000.0
def test_parse_stations_file(tmpdir): """ Tests parsing from a STATIONS file. tmpdir is a pytest fixture. """ filename = os.path.join(tmpdir.dirname, "STATIONS") lines = ( "AAK II 10. 20. 1645.0 30.0", "BBK AA 20. 30. 1645.0 30.0", ) with open(filename, "wt") as fh: fh.write("\n".join(lines)) receivers = Receiver.parse(filename) assert len(receivers) == 2 rec = receivers[0] assert (rec.latitude, rec.longitude, rec.network, rec.station) == ( elliptic_to_geocentric_latitude(10.0), 20.0, "II", "AAK", ) rec = receivers[1] assert (rec.latitude, rec.longitude, rec.network, rec.station) == ( elliptic_to_geocentric_latitude(20.0), 30.0, "AA", "BBK", )
def test_duplicate_receivers(): """ Many waveform files contain multiple channels of the same stations. Of course these duplicates need to be purged. """ filename = os.path.join(DATA, "example.sac") st = obspy.read(filename) st += st.copy() st[1].stats.channel = "LHZ" receivers = Receiver.parse(st) assert len(receivers) == 1 rec = receivers[0] # Coordinates are assumed to be WGS84 and will be converted to geocentric. assert ( round(rec.latitude, 3), round(rec.longitude, 3), rec.network, rec.station, ) == ( round(elliptic_to_geocentric_latitude(34.94598), 3), round(-106.45713, 3), "IU", "ANMO", )
def test_invalid_lat_lng_values(): """ Tests invalid latitude/longitude values """ Receiver(latitude=10, longitude=10) with pytest.raises(ValueError): Receiver(latitude=100, longitude=10) with pytest.raises(ValueError): Receiver(latitude=-100, longitude=10) with pytest.raises(ValueError): Receiver(latitude=10, longitude=200) with pytest.raises(ValueError): Receiver(latitude=10, longitude=-200)
def test_dataless_seed_files(): filename = os.path.join(DATA, "dataless.seed.BW_FURT") receivers = Receiver.parse(filename) assert len(receivers) == 1 rec = receivers[0] assert (round(rec.latitude, 3), round(rec.longitude, 3), rec.network, rec.station) == \ (round(48.162899, 3), round(11.2752, 3), 'BW', 'FURT')
def iterate(self): rec = Receiver(latitude=20, longitude=20) lat = random.random() * 0.5 lat += 44 lng = random.random() * 0.5 lng += 44 depth_in_m = random.random() * 50000 src = Source(latitude=lat, longitude=lng, depth_in_m=depth_in_m) self.db.get_seismograms(source=src, receiver=rec)
def test_parse_StationXML(): filename = os.path.join(DATA, "TA.Q56A..BH.xml") receivers = Receiver.parse(filename) assert len(receivers) == 1 rec = receivers[0] assert (rec.latitude, rec.longitude, rec.network, rec.station) == \ (elliptic_to_geocentric_latitude(39.041), -79.1871, "TA", "Q56A")
def test_parse_stationxml(): filename = os.path.join(DATA, "TA.Q56A..BH.xml") receivers = Receiver.parse(filename) assert len(receivers) == 1 rec = receivers[0] assert (rec.latitude, rec.longitude, rec.network, rec.station) == \ (elliptic_to_geocentric_latitude(39.041), -79.1871, "TA", "Q56A")
def test_parse_sac_files(): filename = os.path.join(DATA, "example.sac") receivers = Receiver.parse(filename) assert len(receivers) == 1 rec = receivers[0] assert (round(rec.latitude, 3), round(rec.longitude, 3), rec.network, rec.station) == \ (round(34.94598, 3), round(-106.45713, 3), 'IU', 'ANMO')
def test_parse_StationXML(): filename = os.path.join(DATA, "TA.Q56A..BH.xml") receivers = Receiver.parse(filename) assert len(receivers) == 1 rec = receivers[0] assert (rec.latitude, rec.longitude, rec.network, rec.station) == \ (39.041, -79.1871, "TA", "Q56A")
def test_parse_obspy_waveform_objects(): filename = os.path.join(DATA, "example.sac") st = obspy.read(filename) # From stream. receivers = Receiver.parse(st) assert len(receivers) == 1 rec = receivers[0] assert (round(rec.latitude, 3), round(rec.longitude, 3), rec.network, rec.station) == \ (round(34.94598, 3), round(-106.45713, 3), 'IU', 'ANMO') # From trace. receivers = Receiver.parse(st[0]) assert len(receivers) == 1 rec = receivers[0] assert (round(rec.latitude, 3), round(rec.longitude, 3), rec.network, rec.station) == \ (round(34.94598, 3), round(-106.45713, 3), 'IU', 'ANMO')
def test_dataless_seed_files(): filename = os.path.join(DATA, "dataless.seed.BW_FURT") receivers = Receiver.parse(filename) assert len(receivers) == 1 rec = receivers[0] # Coordinates are assumed to be WGS84 and will be converted to geocentric. assert (round(rec.latitude, 3), round(rec.longitude, 3), rec.network, rec.station) == \ (round(elliptic_to_geocentric_latitude(48.162899), 3), round(11.2752, 3), 'BW', 'FURT')
def test_parse_sac_files(): filename = os.path.join(DATA, "example.sac") receivers = Receiver.parse(filename) assert len(receivers) == 1 rec = receivers[0] assert (round(rec.latitude, 3), round(rec.longitude, 3), rec.network, rec.station) == \ (round(elliptic_to_geocentric_latitude(34.94598), 3), round(-106.45713, 3), 'IU', 'ANMO')
def test_str_method_of_receiver(): """ Tests the string method of the receiver class. """ rec = Receiver(latitude=1.0, longitude=2.0, network="BW", station="ALTM") assert str(rec) == ("Instaseis Receiver:\n" "\tLongitude : 2.0 deg\n" "\tLatitude : 1.0 deg\n" "\tNetwork : BW\n" "\tStation : ALTM\n" "\tLocation : \n")
def test_parse_obspy_objects(): filename = os.path.join(DATA, "TA.Q56A..BH.xml") inv = obspy.read_inventory(filename) receivers = Receiver.parse(inv) assert len(receivers) == 1 rec = receivers[0] assert (rec.latitude, rec.longitude, rec.network, rec.station) == \ (elliptic_to_geocentric_latitude(39.041), -79.1871, "TA", "Q56A") receivers = Receiver.parse(inv[0]) assert len(receivers) == 1 rec = receivers[0] assert (rec.latitude, rec.longitude, rec.network, rec.station) == \ (elliptic_to_geocentric_latitude(39.041), -79.1871, "TA", "Q56A") receivers = Receiver.parse(inv[0][0], network_code="TA") assert len(receivers) == 1 rec = receivers[0] assert (rec.latitude, rec.longitude, rec.network, rec.station) == \ (elliptic_to_geocentric_latitude(39.041), -79.1871, "TA", "Q56A")
def setup(self): self.db = open_db(self.path, read_on_demand=False, buffer_size_in_mb=250) self.counter = 0 self.current_depth_counter = 0 # Depth increases in 1 km steps up to a depth of 25 km. self.depths = range(0, 25001, 1000) self.depth_count = len(self.depths) # Fix Receiver self.rec = Receiver(latitude=45.0, longitude=45.0)
def iterate(self): # Random points on a sphere. lat = np.rad2deg(np.arcsin(2 * random.random() - 1)) lng = random.random() * 360.0 - 180.0 rec = Receiver(latitude=lat, longitude=lng) lat = np.rad2deg(np.arcsin(2 * random.random() - 1)) lng = random.random() * 360.0 - 180.0 depth_in_m = random.random() * self.max_depth src = Source(latitude=lat, longitude=lng, depth_in_m=depth_in_m) self.db.get_seismograms(source=src, receiver=rec)
def test_parse_obspy_waveform_objects(): filename = os.path.join(DATA, "example.sac") st = obspy.read(filename) # From stream. receivers = Receiver.parse(st) assert len(receivers) == 1 rec = receivers[0] # Coordinates are assumed to be WGS84 and will be converted to geocentric. assert (round(rec.latitude, 3), round(rec.longitude, 3), rec.network, rec.station) == \ (round(elliptic_to_geocentric_latitude(34.94598), 3), round(-106.45713, 3), 'IU', 'ANMO') # From trace. receivers = Receiver.parse(st[0]) assert len(receivers) == 1 rec = receivers[0] assert (round(rec.latitude, 3), round(rec.longitude, 3), rec.network, rec.station) == \ (round(elliptic_to_geocentric_latitude(34.94598), 3), round(-106.45713, 3), 'IU', 'ANMO')
def on_load_stations_button_released(self): pwd = os.getcwd() self.stations_file = str(QtGui.QFileDialog.getOpenFileName(self, "Choose Stations File", pwd)) if not self.stations_file: return self.receivers = Receiver.parse(self.stations_file) recnames = [] for _r in self.receivers: recnames.append("%s.%s" % (_r.network, _r.station)) self.ui.stations_combo.clear() self.ui.stations_combo.addItems(recnames) self._plot_bg_receivers()
def test_duplicate_receivers(): """ Many waveform files contain multiple channels of the same stations. Of course these duplicates need to be purged. """ filename = os.path.join(DATA, "example.sac") st = obspy.read(filename) st += st.copy() st[1].stats.channel = "LHZ" receivers = Receiver.parse(st) assert len(receivers) == 1 rec = receivers[0] assert (round(rec.latitude, 3), round(rec.longitude, 3), rec.network, rec.station) == \ (round(34.94598, 3), round(-106.45713, 3), 'IU', 'ANMO')
def on_load_stations_button_released(self): pwd = os.getcwd() self.stations_file = str( QtGui.QFileDialog.getOpenFileName(self, "Choose Stations File", pwd)) if not self.stations_file: return self.receivers = Receiver.parse(self.stations_file) recnames = [] for _r in self.receivers: recnames.append("%s.%s" % (_r.network, _r.station)) self.ui.stations_combo.clear() self.ui.stations_combo.addItems(recnames) self._plot_bg_receivers()
def test_parse_stations_file(tmpdir): """ Tests parsing from a STATIONS file. tmpdir is a pytest fixture. """ filename = os.path.join(tmpdir.dirname, "STATIONS") lines = ( "AAK II 10. 20. 1645.0 30.0", "BBK AA 20. 30. 1645.0 30.0" ) with open(filename, "wt") as fh: fh.write("\n".join(lines)) receivers = Receiver.parse(filename) assert len(receivers) == 2 rec = receivers[0] assert (rec.latitude, rec.longitude, rec.network, rec.station) == \ (elliptic_to_geocentric_latitude(10.0), 20.0, "II", "AAK") rec = receivers[1] assert (rec.latitude, rec.longitude, rec.network, rec.station) == \ (elliptic_to_geocentric_latitude(20.0), 30.0, "AA", "BBK")
def receiver(self): return Receiver( latitude=float(self.ui.receiver_latitude.value()), longitude=float(self.ui.receiver_longitude.value()), )
def test_error_handling_when_parsing_station_files(tmpdir): """ Tests error handling when parsing station files. """ # Differing coordinates for channels of the same station. inv = obspy.read_inventory() with pytest.raises(ReceiverParseError) as err: inv[0][0][0].latitude -= 10 Receiver.parse(inv) assert err.value.args[0] == ("The coordinates of the channels of station " "'GR.FUR' are not identical.") # Once again, with a file. with io.BytesIO() as buf: inv.write(buf, format="stationxml") buf.seek(0) with pytest.raises(ReceiverParseError) as err: Receiver.parse(buf) assert err.value.args[0] == ("The coordinates of the channels of station " "'GR.FUR' are not identical.") # ObsPy Trace without a sac attribute. with pytest.raises(ReceiverParseError) as err: Receiver.parse(obspy.read()) assert err.value.args[0] == ("ObsPy Trace must have an sac attribute.") # Trigger error when a SEED files has differing origins. filename = os.path.join(DATA, "dataless.seed.BW_FURT") p = obspy.io.xseed.parser.Parser(filename) p.blockettes[52][1].latitude += 1 with pytest.raises(ReceiverParseError) as err: Receiver.parse(p) assert err.value.args[0] == ("The coordinates of the channels of station " "'BW.FURT' are not identical.") # Same thing but this time with a file. tmpfile = os.path.join(tmpdir.strpath, "temp.seed") p.write_seed(tmpfile) with pytest.raises(ReceiverParseError) as err: Receiver.parse(tmpfile) assert err.value.args[0] == ("The coordinates of the channels of station " "'BW.FURT' are not identical.") # Parsing random string. with pytest.raises(ValueError) as err: Receiver.parse("random_string") assert err.value.args[0] == "'random_string' could not be parsed."
def test_error_handling_when_parsing_station_files(tmpdir): """ Tests error handling when parsing station files. """ # Differing coordinates for channels of the same station. inv = obspy.read_inventory() with pytest.raises(ReceiverParseError) as err: inv[0][0][0].latitude -= 10 Receiver.parse(inv) assert err.value.args[0] == ("The coordinates of the channels of station " "'GR.FUR' are not identical.") # Once again, with a file. with io.BytesIO() as buf: inv.write(buf, format="stationxml") buf.seek(0) with pytest.raises(ReceiverParseError) as err: Receiver.parse(buf) assert err.value.args[0] == ("The coordinates of the channels of station " "'GR.FUR' are not identical.") # ObsPy Trace without a sac attribute. with pytest.raises(ReceiverParseError) as err: Receiver.parse(obspy.read()) assert err.value.args[0] == ("ObsPy Trace must have an sac attribute.") # Trigger error when a SEED files has differing origins. filename = os.path.join(DATA, "dataless.seed.BW_FURT") p = obspy.io.xseed.parser.Parser(filename) p.blockettes[52][1].latitude += 1 with pytest.raises(ReceiverParseError) as err: Receiver.parse(p) assert err.value.args[0] == ("The coordinates of the channels of station " "'BW.FURT' are not identical") # Same thing but this time with a file. tmpfile = os.path.join(tmpdir.strpath, "temp.seed") p.write_seed(tmpfile) with pytest.raises(ReceiverParseError) as err: Receiver.parse(tmpfile) assert err.value.args[0] == ("The coordinates of the channels of station " "'BW.FURT' are not identical") # Parsing random string. with pytest.raises(ValueError) as err: Receiver.parse("random_string") assert err.value.args[0] == "'random_string' could not be parsed."
def iterate(self): src = Source(latitude=10, longitude=10) rec = Receiver(latitude=20, longitude=20) self.db.get_seismograms(source=src, receiver=rec, return_obspy_stream=False)
def iterate(self): src = Source(latitude=10, longitude=10) rec = Receiver(latitude=20, longitude=20) self.db.get_seismograms(source=src, receiver=rec)