Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
Merge pull request #60 from mhearne-usgs/event_params
Browse files Browse the repository at this point in the history
Added getEventParams method and added timeouts to url requests.
  • Loading branch information
emthompson-usgs authored Sep 20, 2017
2 parents 8d392a3 + dcb0272 commit 6ba951b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
59 changes: 50 additions & 9 deletions impactutils/comcat/query.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
#stdlib imports
from urllib import request
import json
from datetime import datetime

URL_TEMPLATE = 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/[EVENTID].geojson'
GEOSERVE_URL = 'http://earthquake.usgs.gov/ws/geoserve/regions.json?latitude=[LAT]&longitude=[LON]'

TIMEOUT = 15
WAITSECS = 3

def _get_url_data(url,timeout=TIMEOUT):
try:
fh = request.urlopen(url,timeout=timeout)
data = fh.read().decode('utf-8')
fh.close()
return data
except HTTPError as htpe:
if htpe.code == 503:
try:
time.sleep(WAITSECS)
fh = request.urlopen(url,timeout=timeout)
data = fh.read().decode('utf-8')
fh.close()
return data
except Exception as msg:
raise Exception('Error downloading data from url %s. "%s".' % (url,msg))


class GeoServe(object):
"""Class to wrap around the NEIC GeoServe web service.
Expand All @@ -16,6 +38,9 @@ def __init__(self,lat,lon):
Desired latitude.
:param lon:
Desired longitude.
:raises:
Exception if the GeoServe URL cannot be reached after two attempts,
and a suitable timeout period.
"""
url = GEOSERVE_URL.replace('[LAT]',str(lat))
url = url.replace('[LON]',str(lon))
Expand Down Expand Up @@ -50,19 +75,15 @@ def getAuthoritative(self):
return (auth_source,auth_type)

def _getJSONContent(self,url):
fh = request.urlopen(url)
data = fh.read().decode('utf-8')
fh.close()
data = _get_url_data(url,timeout=TIMEOUT)
content = json.loads(data)
return content

class ComCatInfo(object):
def __init__(self,eventid):
url = URL_TEMPLATE.replace('[EVENTID]',eventid)
try:
fh = request.urlopen(url)
data = fh.read().decode('utf-8')
fh.close()
data = _get_url_data(url)
self._jdict = json.loads(data)
except Exception as e:
raise Exception('Could not connect to ComCat server.').with_traceback(e.__traceback__)
Expand Down Expand Up @@ -109,6 +130,28 @@ def getAssociatedSources(self):
newsources.remove(authsource)
return (authsource,newsources)

def getEventParams(self):
"""Query ComCat for the time,lat,lon,depth, and magnitude associated with input ID.
:returns:
Dictionary containing:
- time Datetime object representing earthquake origin time in UTC.
- lat Origin latitude.
- lon Origin longitude.
- depth Origin depth.
- magnitude Origin magnitude.
"""
lon,lat,depth = self._jdict['geometry']['coordinates']
itime = self._jdict['properties']['time']
etime = datetime.utcfromtimestamp(int(itime/1000))
mag = self._jdict['properties']['mag']
edict = {'time':etime,
'lat':lat,
'lon':lon,
'depth':depth,
'magnitude':mag}
return edict

def getLocation(self):
"""Query ComCat for the location string associated with input ID.
Expand Down Expand Up @@ -140,12 +183,10 @@ def getShakeGrid(self,local_file=None):
try:
shake_url = self._jdict['properties']['products']['shakemap'][0]['contents']['download/grid.xml']['url']
if local_file is not None:
fh = request.urlopen(shake_url)
data = fh.read().decode('utf-8')
data = _get_url_data(shake_url)
f = open(local_file,'w')
f.write(data)
f.close()
fh.close()
return local_file
return shake_url
except:
Expand Down
14 changes: 13 additions & 1 deletion test/comcat/query_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import tempfile
import os.path
import sys
from datetime import datetime

#hack the path so that I can debug these functions if I need to
homedir = os.path.dirname(os.path.abspath(__file__)) #where is this script?
Expand All @@ -21,7 +22,7 @@ def geoserve():
positions = [{'name':'california','coords':(37.28935,-119.53125),'source':'NC','type':'anss'},
{'name':'alaska','coords':(63.379217,-151.699219),'source':'AK','type':'anss'},
{'name':'aleutians','coords':(53.209322,-167.34375),'source':'US','type':'NA'},
{'name':'japan','coords':(36.700907,138.999023),'source':'JMA','type':'contributor'}]
{'name':'japan','coords':(36.700907,138.999023),'source':'US','type':'NA'}]

gs = GeoServe(0,0)
for pdict in positions:
Expand Down Expand Up @@ -80,6 +81,17 @@ def test_cc():
url = ccinfo.getURL()
assert cmpurl == url

#test the getEventParams method
eventid = 'us1000778i'
ccinfo = ComCatInfo(eventid)
cmpdict = {'lat':-42.7373,
'lon':173.054,
'depth':15.11,
'magnitude':7.8,
'time':datetime(2016,11,13,11,2,56)}
edict = ccinfo.getEventParams()
assert edict == cmpdict

#test getAssociatedSources method
sources = {'ci37374687':('ci',['us','nc','at']),
'nc72672610':('nc',['at','us']),
Expand Down

0 comments on commit 6ba951b

Please sign in to comment.