def test_parse_obspy_objects(): """ Tests parsing from ObsPy objects. """ cat = obspy.read_events(EVENT_FILE) ev = cat[0] _assert_src(Source.parse(cat)) _assert_src(Source.parse(ev))
def test_parse_CMTSOLUTIONS_file(tmpdir): """ Tests parsing from a CMTSOLUTIONS file. """ filename = os.path.join(str(tmpdir), "CMTSOLUTIONS") lines = ( "PDEW2011 8 23 17 51 4.60 37.9400 -77.9300 6.0 5.9 5.8 VIRGINIA", "event name: 201108231751A", "time shift: 1.1100", "half duration: 1.8000", "latitude: 37.9100", "longitude: -77.9300", "depth: 12.0000", "Mrr: 4.710000e+24", "Mtt: 3.810000e+22", "Mpp: -4.740000e+24", "Mrt: 3.990000e+23", "Mrp: -8.050000e+23", "Mtp: -1.230000e+24") with open(filename, "wt") as fh: fh.write("\n".join(lines)) origin_time = obspy.UTCDateTime(2011, 8, 23, 17, 51, 4.6) src = Source.parse(filename) src_params = np.array([src.latitude, src.longitude, src.depth_in_m, src.m_rr, src.m_tt, src.m_pp, src.m_rt, src.m_rp, src.m_tp], dtype="float64") # Latitude will have assumed to be WGS84 and converted to geocentric # latitude. np.testing.assert_allclose(src_params, np.array( (elliptic_to_geocentric_latitude(37.91), -77.93, 12000, 4.71E17, 3.81E15, -4.74E17, 3.99E16, -8.05E16, -1.23E17), dtype="float64")) assert src.origin_time == origin_time # Write again. Reset latitude beforehand. filename = os.path.join(str(tmpdir), "CMTSOLUTIONS2") src.latitude = 37.91 src.write_CMTSOLUTION_file(filename) # This time there is no need to convert latitudes. Writing will convert # to WGS84 and reading will convert back. src = Source.parse(filename) src_params = np.array([src.latitude, src.longitude, src.depth_in_m, src.m_rr, src.m_tt, src.m_pp, src.m_rt, src.m_rp, src.m_tp], dtype="float64") np.testing.assert_allclose(src_params, np.array( (37.91, -77.93, 12000, 4.71E17, 3.81E15, -4.74E17, 3.99E16, -8.05E16, -1.23E17), dtype="float64"), rtol=1E-5) assert src.origin_time == origin_time
def test_parse_cmtsolutions_file(tmpdir): """ Tests parsing from a CMTSOLUTIONS file. """ filename = os.path.join(str(tmpdir), "CMTSOLUTIONS") lines = (" PDE 2011 8 23 17 51 4.60 37.9400 -77.9300 6.0 5.9 5.8 " "VIRGINIA", "event name: 201108231751A", "time shift: 1.00", "half duration: 1.8000", "latitude: 37.9100", "longitude: -77.9300", "depth: 12.0000", "Mrr: 4.710000e+24", "Mtt: 3.810000e+22", "Mpp: -4.740000e+24", "Mrt: 3.990000e+23", "Mrp: -8.050000e+23", "Mtp: -1.230000e+24") with open(filename, "wt") as fh: fh.write("\n".join(lines)) # This is the hypocentral time + 1 seconds (the time shift in the # CMTSOLUTION file). origin_time = obspy.UTCDateTime(2011, 8, 23, 17, 51, 5.6) src = Source.parse(filename) src_params = np.array([ src.latitude, src.longitude, src.depth_in_m, src.m_rr, src.m_tt, src.m_pp, src.m_rt, src.m_rp, src.m_tp ], dtype="float64") # Latitude will have assumed to be WGS84 and converted to geocentric # latitude. The import machinery should do that. np.testing.assert_allclose( src_params, np.array((elliptic_to_geocentric_latitude(37.91), -77.93, 12000, 4.71E17, 3.81E15, -4.74E17, 3.99E16, -8.05E16, -1.23E17), dtype="float64")) assert src.origin_time == origin_time
def on_load_source_button_released(self): pwd = os.getcwd() self.source_file = str(QtGui.QFileDialog.getOpenFileName(self, "Choose Source File", pwd)) if not self.source_file: return s = Source.parse(self.source_file) self.ui.m_rr.setValue(s.m_rr) self.ui.m_pp.setValue(s.m_pp) self.ui.m_rp.setValue(s.m_rp) self.ui.m_tt.setValue(s.m_tt) self.ui.m_rt.setValue(s.m_rt) self.ui.m_tp.setValue(s.m_tp) self.ui.source_longitude.setValue(s.longitude) self.ui.source_latitude.setValue(s.latitude) self.ui.depth_slider.setValue(-s.depth_in_m / 1e3) self.set_info()
def on_load_source_button_released(self): pwd = os.getcwd() self.source_file = str( QtGui.QFileDialog.getOpenFileName(self, "Choose Source File", pwd)) if not self.source_file: return s = Source.parse(self.source_file) self.ui.m_rr.setValue(s.m_rr) self.ui.m_pp.setValue(s.m_pp) self.ui.m_rp.setValue(s.m_rp) self.ui.m_tt.setValue(s.m_tt) self.ui.m_rt.setValue(s.m_rt) self.ui.m_tp.setValue(s.m_tp) self.ui.source_longitude.setValue(s.longitude) self.ui.source_latitude.setValue(s.latitude) self.ui.depth_slider.setValue(-s.depth_in_m / 1e3) self.set_info()
def test_parse_cmtsolutions_file(tmpdir): """ Tests parsing from a CMTSOLUTIONS file. """ filename = os.path.join(str(tmpdir), "CMTSOLUTIONS") lines = ( " PDE 2011 8 23 17 51 4.60 37.9400 -77.9300 6.0 5.9 5.8 " "VIRGINIA", "event name: 201108231751A", "time shift: 1.00", "half duration: 1.8000", "latitude: 37.9100", "longitude: -77.9300", "depth: 12.0000", "Mrr: 4.710000e+24", "Mtt: 3.810000e+22", "Mpp: -4.740000e+24", "Mrt: 3.990000e+23", "Mrp: -8.050000e+23", "Mtp: -1.230000e+24") with open(filename, "wt") as fh: fh.write("\n".join(lines)) # This is the hypocentral time + 1 seconds (the time shift in the # CMTSOLUTION file). origin_time = obspy.UTCDateTime(2011, 8, 23, 17, 51, 5.6) src = Source.parse(filename) src_params = np.array([src.latitude, src.longitude, src.depth_in_m, src.m_rr, src.m_tt, src.m_pp, src.m_rt, src.m_rp, src.m_tp], dtype="float64") # Latitude will have assumed to be WGS84 and converted to geocentric # latitude. The import machinery should do that. np.testing.assert_allclose(src_params, np.array( (elliptic_to_geocentric_latitude(37.91), -77.93, 12000, 4.71E17, 3.81E15, -4.74E17, 3.99E16, -8.05E16, -1.23E17), dtype="float64")) assert src.origin_time == origin_time
def test_parse_quakeml(): """ Tests parsing from a QuakeML file. """ src = Source.parse(EVENT_FILE) _assert_src(src)
def test_event_parsing_failure_states(): """ Tests the failures when parsing an event. """ # Random string. with pytest.raises(SourceParseError) as err: Source.parse("random strings") assert err.value.args[0] == "Could not parse the given source." # Empty catalog. cat = obspy.read_events() cat.events = [] with pytest.raises(SourceParseError) as err: Source.parse(cat) assert err.value.args[0] == "Event catalog contains zero events." # Catalog with lots of events. cat = obspy.read_events() with pytest.raises(SourceParseError) as err: Source.parse(cat) assert err.value.args[0].startswith("Event catalog contains 3 events") event = obspy.read_events(EVENT_FILE)[0] # Event with no origin. ev = event.copy() ev.origins = [] with pytest.raises(SourceParseError) as err: Source.parse(ev) assert err.value.args[0] == "Event must contain an origin." # Event with no focmec. ev = event.copy() ev.focal_mechanisms = [] with pytest.raises(SourceParseError) as err: Source.parse(ev) assert err.value.args[0] == "Event must contain a focal mechanism." # Event with no moment tensor. ev = event.copy() ev.focal_mechanisms[0].moment_tensor = None # Force this to None to prevent the resource id mechanism to "resurrect" # the focal mechanism. ev.preferred_focal_mechanism_id = None with pytest.raises(SourceParseError) as err: Source.parse(ev) assert err.value.args[0] == "Event must contain a moment tensor."
def test_parse_QuakeML(): """ Tests parsing from a QuakeML file. """ src = Source.parse(EVENT_FILE) _assert_src(src)