Skip to content

Commit

Permalink
Merge pull request #10253 from gem/usgsid_notfound
Browse files Browse the repository at this point in the history
ARISTOTLE: notify to the user when the given rupture identifier is not found
  • Loading branch information
ptormene authored Jan 9, 2025
2 parents fb39ac8 + 97b4899 commit e03d625
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
22 changes: 14 additions & 8 deletions openquake/hazardlib/shakemap/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ def _contents_properties_shakemap(usgs_id, user, use_shakemap, monitor):
# with open(f'/tmp/{usgs_id}.json', 'wb') as f:
# url = SHAKEMAP_URL.format(usgs_id)
# f.write(urlopen(url).read())
err = {}
if user.testdir: # in parsers_test
fname = os.path.join(user.testdir, usgs_id + '.json')
text = open(fname).read()
Expand All @@ -629,7 +630,9 @@ def _contents_properties_shakemap(usgs_id, user, use_shakemap, monitor):
text = urlopen(url).read()
except URLError as exc:
# in parsers_test
raise URLError(f'Unable to download from {url}: {exc}')
err_msg = f'Unable to download from {url}: {exc}'
err = {"status": "failed", "error_msg": err_msg}
return None, None, None, err

js = json.loads(text)
properties = js['properties']
Expand All @@ -651,7 +654,7 @@ def _contents_properties_shakemap(usgs_id, user, use_shakemap, monitor):
shakemap_array = get_shakemap_array(grid_fname)
else:
shakemap_array = None
return contents, properties, shakemap_array
return contents, properties, shakemap_array, err


def get_rup_dic(usgs_id, user, use_shakemap, rupture_file=None, station_data_file=None,
Expand All @@ -665,10 +668,11 @@ def get_rup_dic(usgs_id, user, use_shakemap, rupture_file=None, station_data_fil
:param use_shakemap: download the ShakeMap only if True
:param rupture_file: None
:param station_data_file: None
:returns: (rupture object or None, rupture dictionary)
:returns: (rupture object or None, rupture dictionary, error dictionary or {})
"""
rupdic = {}
rup_data = {}
err = {}
if rupture_file and rupture_file.endswith('.xml'):
[rup_node] = nrml.read(os.path.join(user.testdir, rupture_file)
if user.testdir else rupture_file)
Expand All @@ -684,19 +688,21 @@ def get_rup_dic(usgs_id, user, use_shakemap, rupture_file=None, station_data_fil
rupture_file=rupture_file,
station_data_file=station_data_file)
if usgs_id == 'FromFile':
return rup, rupdic
return rup, rupdic, err
elif rupture_file and rupture_file.endswith('.json'):
with open(rupture_file) as f:
rup_data = json.load(f)
if usgs_id == 'FromFile':
rupdic = convert_rup_data(rup_data, usgs_id, rupture_file)
rupdic['station_data_file'] = station_data_file
rup = convert_to_oq_rupture(rup_data)
return rup, rupdic
return rup, rupdic, err

assert usgs_id
contents, properties, shakemap = _contents_properties_shakemap(
contents, properties, shakemap, err = _contents_properties_shakemap(
usgs_id, user, use_shakemap, monitor)
if err:
return None, None, err

if 'download/rupture.json' not in contents:
# happens for us6000f65h in parsers_test
Expand All @@ -720,15 +726,15 @@ def get_rup_dic(usgs_id, user, use_shakemap, rupture_file=None, station_data_fil
rupdic['station_data_file_from_usgs'] = False
if not rup_data or rupdic['require_dip_strike']:
# in parsers_test
return None, rupdic
return None, rupdic, err

rup = convert_to_oq_rupture(rup_data)
if rup is None:
# in parsers_test for us6000jllz
rupdic['rupture_issue'] = 'Unable to convert the rupture from the USGS format'
rupdic['require_dip_strike'] = True
# in parsers_test for usp0001ccb
return rup, rupdic
return rup, rupdic, err


def get_array_usgs_id(kind, usgs_id):
Expand Down
4 changes: 3 additions & 1 deletion openquake/hazardlib/shakemap/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,10 @@ def aristotle_validate(POST, user, rupture_file=None, station_data_file=None,
if 'use_shakemap' in POST:
use_shakemap = POST['use_shakemap'] == 'true'

rup, rupdic = get_rup_dic(
rup, rupdic, err = get_rup_dic(
dic['usgs_id'], user, use_shakemap, rupture_file, station_data_file, monitor)
if err:
return None, None, None, err
# round floats
for k, v in rupdic.items():
if isinstance(v, float): # lon, lat, dep, strike, dip
Expand Down
20 changes: 10 additions & 10 deletions openquake/hazardlib/tests/shakemap/parsers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import os
import unittest
from urllib.error import URLError
from openquake.hazardlib.shakemap.parsers import get_rup_dic, User

user = User(level=2, testdir=os.path.join(os.path.dirname(__file__), 'data'))
Expand All @@ -36,19 +35,19 @@ def setUp(cls):

def test_1(self):
# wrong usgs_id
with self.assertRaises(URLError) as ctx:
get_rup_dic('usp0001cc', User(level=2, testdir=''), use_shakemap=True)
_rup, _rupdic, err = get_rup_dic(
'usp0001cc', User(level=2, testdir=''), use_shakemap=True)
self.assertIn('Unable to download from https://earthquake.usgs.gov/fdsnws/'
'event/1/query?eventid=usp0001cc&', str(ctx.exception))
'event/1/query?eventid=usp0001cc&', err['error_msg'])

def test_2(self):
_rup, dic = get_rup_dic('usp0001ccb', user=user, use_shakemap=True)
_rup, dic, _err = get_rup_dic('usp0001ccb', user=user, use_shakemap=True)
self.assertIsNotNone(dic['shakemap_array'])
_rup, dic = get_rup_dic('usp0001ccb', user=user, use_shakemap=False)
_rup, dic, _err = get_rup_dic('usp0001ccb', user=user, use_shakemap=False)
self.assertIsNone(dic['shakemap_array'])

def test_3(self):
_rup, dic = get_rup_dic('us6000f65h', user=user, use_shakemap=True)
_rup, dic, _err = get_rup_dic('us6000f65h', user=user, use_shakemap=True)
self.assertEqual(dic['lon'], -73.475)
self.assertEqual(dic['lat'], 18.408)
self.assertEqual(dic['dep'], 10.0)
Expand All @@ -66,27 +65,28 @@ def test_3(self):

def test_4(self):
# point_rup
_rup, dic = get_rup_dic('us6000jllz', user=user, use_shakemap=True)
_rup, dic, _err = get_rup_dic('us6000jllz', user=user, use_shakemap=True)
self.assertEqual(dic['lon'], 37.0143)
self.assertEqual(dic['lat'], 37.2256)
self.assertEqual(dic['dep'], 10.)
self.assertEqual(dic['require_dip_strike'], True)

def test_5(self):
# 12 vertices instead of 4 in rupture.json
rup, dic = get_rup_dic('us20002926', user=user, use_shakemap=True)
rup, dic, _err = get_rup_dic('us20002926', user=user, use_shakemap=True)
self.assertIsNone(rup)
self.assertEqual(dic['require_dip_strike'], True)
self.assertEqual(dic['rupture_issue'],
'Unable to convert the rupture from the USGS format')

def test_6(self):
rup, dic = get_rup_dic('usp0001ccb', user=user, use_shakemap=True)
rup, dic, _err = get_rup_dic('usp0001ccb', user=user, use_shakemap=True)
self.assertEqual(rup.mag, 6.7)
self.assertEqual(dic['require_dip_strike'], False)
self.assertEqual(dic['station_data_issue'],
'3 stations were found, but none of them are seismic')


"""
NB: to profile a test you can use
Expand Down

0 comments on commit e03d625

Please sign in to comment.