Skip to content

Commit

Permalink
fix: reworked catalog accessor. Retrieving from IRIS ("https://servic…
Browse files Browse the repository at this point in the history
…e.iris.edu/fdsnws/event/1/") in text file, which is faster and safer.

tests: fixed event list location
  • Loading branch information
pabloitu committed May 16, 2023
1 parent def0bc8 commit 533e2a2
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 178 deletions.
51 changes: 20 additions & 31 deletions csep/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,11 @@ def query_bsi(start_time, end_time, min_magnitude=2.50,

return bsi


def query_gns(start_time, end_time, min_magnitude=2.950,
min_latitude=-47, max_latitude=-34,
min_longitude=164, max_longitude=180,
max_depth=45.5,
min_latitude=-47, max_latitude=-34,
min_longitude=164, max_longitude=180,
max_depth=45.5,
verbose=True,
apply_filters=False, **kwargs):
"""
Expand Down Expand Up @@ -360,38 +361,26 @@ def query_gns(start_time, end_time, min_magnitude=2.950,

return gns

def query_gcmt(start_time, end_time, min_magnitude=5.0, min_depth=None,

def query_gcmt(start_time, end_time, min_magnitude=5.0,
max_depth=None,
catalog_id=None, verbose=True,
catalog_id=None,
min_latitude=None, max_latitude=None,
min_longitude=None, max_longitude=None):
if min_latitude:
searchshape = 'RECT'
else:
searchshape = 'GLOBAL'
events, creation_time = readers._query_gcmt(
start_year=start_time.year,
start_month=start_time.month,
start_day=start_time.day,
searchshape=searchshape,
start_time=start_time.time().isoformat(),
end_year=end_time.year,
end_month=end_time.month,
end_day=end_time.day,
end_time=end_time.time().isoformat(),
min_mag=min_magnitude,
min_dep=min_depth,
max_dep=max_depth,
left_lon=min_longitude,
right_lon=max_longitude,
bot_lat=min_latitude,
top_lat=max_latitude,
verbose=verbose
)
catalog = catalogs.CSEPCatalog(data=events, name='ISC Bulletin - gCMT',

eventlist = readers._query_gcmt(start_time=start_time,
end_time=end_time,
min_magnitude=min_magnitude,
min_latitude=min_latitude,
max_latitude=max_latitude,
min_longitude=min_longitude,
max_longitude=max_longitude,
max_depth=max_depth)

catalog = catalogs.CSEPCatalog(data=eventlist,
name='gCMT',
catalog_id=catalog_id,
date_accessed=creation_time)
catalog.filter([f'magnitude >= {min_magnitude}'], in_place=True)
date_accessed=utc_now_datetime())
return catalog


Expand Down
132 changes: 132 additions & 0 deletions csep/utils/iris.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# python imports
from datetime import datetime
from urllib import request
from urllib.parse import urlencode

# PyCSEP imports
from csep.utils.time_utils import datetime_to_utc_epoch

HOST_CATALOG = "https://service.iris.edu/fdsnws/event/1/query?"
TIMEOUT = 180


def gcmt_search(format='text',
starttime=None,
endtime=None,
updatedafter=None,
minlatitude=None,
maxlatitude=None,
minlongitude=None,
maxlongitude=None,
latitude=None,
longitude=None,
maxradius=None,
catalog='GCMT',
contributor=None,
maxdepth=1000,
maxmagnitude=10.0,
mindepth=-100,
minmagnitude=0,
offset=1,
orderby='time-asc',
host=None,
verbose=False):
"""Search the IRIS database for events matching input criteria.
This search function is a wrapper around the ComCat Web API described here:
https://service.iris.edu/fdsnws/event/1/
This function returns a list of SummaryEvent objects, described elsewhere in this package.
Args:
starttime (datetime):
Python datetime - Limit to events on or after the specified start time.
endtime (datetime):
Python datetime - Limit to events on or before the specified end time.
updatedafter (datetime):
Python datetime - Limit to events updated after the specified time.
minlatitude (float):
Limit to events with a latitude larger than the specified minimum.
maxlatitude (float):
Limit to events with a latitude smaller than the specified maximum.
minlongitude (float):
Limit to events with a longitude larger than the specified minimum.
maxlongitude (float):
Limit to events with a longitude smaller than the specified maximum.
latitude (float):
Specify the latitude to be used for a radius search.
longitude (float):
Specify the longitude to be used for a radius search.
maxradius (float):
Limit to events within the specified maximum number of degrees
from the geographic point defined by the latitude and longitude parameters.
catalog (str):
Limit to events from a specified catalog.
contributor (str):
Limit to events contributed by a specified contributor.
maxdepth (float):
Limit to events with depth less than the specified maximum.
maxmagnitude (float):
Limit to events with a magnitude smaller than the specified maximum.
mindepth (float):
Limit to events with depth more than the specified minimum.
minmagnitude (float):
Limit to events with a magnitude larger than the specified minimum.
offset (int):
Return results starting at the event count specified, starting at 1.
orderby (str):
Order the results. The allowed values are:
- time order by origin descending time
- time-asc order by origin ascending time
- magnitude order by descending magnitude
- magnitude-asc order by ascending magnitude
host (str):
Replace default ComCat host (earthquake.usgs.gov) with a custom host.
Returns:
list: List of SummaryEvent() objects.
"""

# getting the inputargs must be the first line of the method!
inputargs = locals().copy()
newargs = {}

for key, value in inputargs.items():
if value is True:
newargs[key] = 'true'
continue
if value is False:
newargs[key] = 'false'
continue
if value is None:
continue
newargs[key] = value

del newargs['verbose']

events = _search_gcmt(**newargs)

return events


def _search_gcmt(**_newargs):
"""
Performs de-query at ISC API and returns event list and access date
"""
paramstr = urlencode(_newargs)
url = HOST_CATALOG + paramstr
fh = request.urlopen(url, timeout=TIMEOUT)
data = fh.read().decode('utf8').split('\n')
fh.close()
eventlist = []
for line in data[1:]:
line_ = line.split('|')
if len(line_) != 1:
id_ = line_[0]
time_ = datetime.fromisoformat(line_[1])
dt = datetime_to_utc_epoch(time_)
lat = float(line_[2])
lon = float(line_[3])
depth = float(line_[4])
mag = float(line_[10])
eventlist.append((id_, dt, lat, lon, depth, mag))

return eventlist
Loading

0 comments on commit 533e2a2

Please sign in to comment.