[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.
This commit is contained in:
Darius Arnold 2018-08-13 22:42:19 +02:00
parent 96adbddeba
commit 6936cfcfa6
2 changed files with 25 additions and 4 deletions

View File

@ -483,7 +483,11 @@ class AutopickStation(object):
AttributeError when no metadata or source origins is given AttributeError when no metadata or source origins is given
""" """
id = get_seed_id() 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] source_origin = origin[0]
model = TauPyModel(taup_model) model = TauPyModel(taup_model)
arrivals = model.get_travel_times_geo(source_depth_in_km=source_origin.depth, 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)) ' origin time using TauPy'.format(estFirstP, estFirstS))
return 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: 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 # correct user mistake where a relative cuttime is selected (pstart < 0) but use of taupy is disabled/ has
# not the required parameters # not the required parameters
if self.p_params.pstart < 0: exit_taupy()
self.p_params.pstart = 0
return return
print('autopickstation: use_taup flag active.') print('autopickstation: use_taup flag active.')

View File

@ -49,7 +49,7 @@ class MockMetadata:
self.coordinates = [gra1, gra2, ech, fiesa, a106] 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 Mocks the method get_coordinates from obspy.io.xseed.parser.Parser object
to avoid building a parser for the unit tests 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.ech = self.wfstream.select(station='ECH')
self.fiesa = self.wfstream.select(station='FIESA') self.fiesa = self.wfstream.select(station='FIESA')
self.a106 = self.wfstream.select(station='A106A') self.a106 = self.wfstream.select(station='A106A')
self.a005a = self.wfstream.select(station='A005A')
# Create input parameter container # Create input parameter container
self.inputfile_taupy_enabled = os.path.join(os.path.dirname(__file__), 'autoPyLoT_global_taupy_true.in') 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') 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) result, station = autopickstation(wfstream=self.a106, pickparam=self.pickparam_taupy_enabled, metadata=self.metadata, origin=self.origin)
self.assertEqual(expected, result) 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__': if __name__ == '__main__':
unittest.main() unittest.main()