From 6936cfcfa62670fe0e9299a03dd33872e80d7d8e Mon Sep 17 00:00:00 2001 From: Darius Arnold Date: Mon, 13 Aug 2018 22:42:19 +0200 Subject: [PATCH] [bugfix] Taupy didn't check return of get_coordinates For a station not in the metadata, get_coordinates would return None which wasn't checked for. This includes a test for a station which is not in metadata. --- pylot/core/pick/autopick.py | 16 +++++++++++++--- .../test_autopickstation/test_autopickstation.py | 13 ++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/pylot/core/pick/autopick.py b/pylot/core/pick/autopick.py index 6800f0a8..a798fa6f 100644 --- a/pylot/core/pick/autopick.py +++ b/pylot/core/pick/autopick.py @@ -483,7 +483,11 @@ class AutopickStation(object): AttributeError when no metadata or source origins is given """ id = get_seed_id() - station_coords = metadata.get_coordinates(id) + station_coords = metadata.get_coordinates(id, self.ztrace.stats.starttime) + if station_coords is None: + exit_taupy() + raise AttributeError('Warning: Could not find station in metadata') + # TODO raise when metadata.get_coordinates returns None source_origin = origin[0] model = TauPyModel(taup_model) arrivals = model.get_travel_times_geo(source_depth_in_km=source_origin.depth, @@ -512,11 +516,17 @@ class AutopickStation(object): ' origin time using TauPy'.format(estFirstP, estFirstS)) return estFirstP, estFirstS + def exit_taupy(): + """If taupy failed to calculate theoretical starttimes, picking continues. + For this a clean exit is required, since the P starttime is no longer relative to the theoretic onset but + to the vertical trace starttime, eg. it can't be < 0.""" + if self.p_params.pstart < 0: + self.p_params.pstart = 0 + if self.p_params.use_taup is False or not self.origin or not self.metadata: # correct user mistake where a relative cuttime is selected (pstart < 0) but use of taupy is disabled/ has # not the required parameters - if self.p_params.pstart < 0: - self.p_params.pstart = 0 + exit_taupy() return print('autopickstation: use_taup flag active.') diff --git a/tests/test_autopickstation/test_autopickstation.py b/tests/test_autopickstation/test_autopickstation.py index 6c9b4f2c..08002377 100644 --- a/tests/test_autopickstation/test_autopickstation.py +++ b/tests/test_autopickstation/test_autopickstation.py @@ -49,7 +49,7 @@ class MockMetadata: self.coordinates = [gra1, gra2, ech, fiesa, a106] - def get_coordinates(self, station_id): + def get_coordinates(self, station_id, time=None): """ Mocks the method get_coordinates from obspy.io.xseed.parser.Parser object to avoid building a parser for the unit tests @@ -87,6 +87,7 @@ class TestAutopickStation(unittest.TestCase): self.ech = self.wfstream.select(station='ECH') self.fiesa = self.wfstream.select(station='FIESA') self.a106 = self.wfstream.select(station='A106A') + self.a005a = self.wfstream.select(station='A005A') # Create input parameter container self.inputfile_taupy_enabled = os.path.join(os.path.dirname(__file__), 'autoPyLoT_global_taupy_true.in') self.inputfile_taupy_disabled = os.path.join(os.path.dirname(__file__), 'autoPyLoT_global_taupy_false.in') @@ -196,5 +197,15 @@ class TestAutopickStation(unittest.TestCase): result, station = autopickstation(wfstream=self.a106, pickparam=self.pickparam_taupy_enabled, metadata=self.metadata, origin=self.origin) self.assertEqual(expected, result) + def test_autopickstation_station_missing_in_metadata(self): + """This station is not in the metadata, but Taupy is enabled. Taupy should exit cleanly and modify the starttime + relative to the theoretical onset to one relative to the traces starttime, eg never negative. + """ + self.pickparam_taupy_enabled.setParamKV('pstart', -100) # modify starttime to be relative to theoretical onset + expected = {'P': {'picker': 'auto', 'snrdb': 14.464757855513506, 'network': u'Z3', 'weight': 0, 'Mo': None, 'Ao': None, 'lpp': UTCDateTime(2016, 1, 24, 10, 41, 39, 605000), 'Mw': None, 'fc': None, 'snr': 27.956048519707181, 'marked': [], 'mpp': UTCDateTime(2016, 1, 24, 10, 41, 38, 605000), 'w0': None, 'spe': 1.6666666666666667, 'epp': UTCDateTime(2016, 1, 24, 10, 41, 35, 605000), 'fm': None, 'channel': u'LHZ'}, 'S': {'picker': 'auto', 'snrdb': 10.112844176301248, 'network': u'Z3', 'weight': 1, 'Mo': None, 'Ao': None, 'lpp': UTCDateTime(2016, 1, 24, 10, 50, 51, 605000), 'Mw': None, 'fc': None, 'snr': 10.263238413785425, 'marked': [], 'mpp': UTCDateTime(2016, 1, 24, 10, 50, 48, 605000), 'w0': None, 'spe': 4.666666666666667, 'epp': UTCDateTime(2016, 1, 24, 10, 50, 40, 605000), 'fm': None, 'channel': u'LHE'}} + with HidePrints(): + result, station = autopickstation(wfstream = self.a005a, pickparam=self.pickparam_taupy_enabled, metadata=self.metadata, origin=self.origin) + self.assertEqual(expected, result) + if __name__ == '__main__': unittest.main()