Skip to content

Commit

Permalink
Allow specifying time intervals for start_time and end_time
Browse files Browse the repository at this point in the history
Change defaults in sample configspec.conf to have a woring example.
  • Loading branch information
claudiodsf committed Dec 19, 2023
1 parent e723352 commit a72521f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
10 changes: 6 additions & 4 deletions seiscat/conf/configspec.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ extra_field_defaults = force_list(default=None)
## FDSN event webservice URL or shortcut
# It can be an ObsPy supported shortcut (ex., ISC, USGS, RESIF)
# or a full URL (ex., http://www.isc.ac.uk/fdsnws/event/1/)
fdsn_event_url = string(default=ISC)
fdsn_event_url = string(default=USGS)


## Event selection criteria
## Use "None" to signify no limit
# Start time for event selection (UTC date time or None)
# Start time for event selection (UTC date time, time interval or None)
# ex.: start_time = 2021-08-23T00:00:00
start_time = string(default=None)
# End time for event selection (UTC date time or None)
# ex.: start_time = -10 days
start_time = string(default="-1 day")
# End time for event selection (UTC date time, time interval or None)
# ex.: end_time = 2021-08-24T00:00:00
# ex.: end_time = -1 hour
end_time = string(default=None)
# Recheck period (string or None).
# Use a string with the format "X unit" where X is an integer and unit is
Expand Down
29 changes: 21 additions & 8 deletions seiscat/fdsnws.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,21 @@ def _to_utc_datetime(time):
:param time: time in string format
:returns: UTCDateTime object or None
"""
return None if time is None else UTCDateTime(time)
if time is None:
return None
try:
return UTCDateTime(time)
except TypeError:
try:
time_interval = _parse_time_interval(time)
return UTCDateTime() + time_interval
except ValueError as e:
raise ValueError(
f'Invalid time format: {time}.\n'
'Please use YYYY-MM-DDTHH:MM:SS or '
'a time interval (typically in the past),\n'
'e.g., -1 day, -2 hours, -5 minutes, -10 seconds.'
) from e


def _parse_time_interval(time_interval):
Expand All @@ -50,25 +64,24 @@ def _parse_time_interval(time_interval):
value = int(parts[0])
unit = parts[1]
if unit.endswith('s'):
# remvove plural form
unit = unit[:-1]
if unit == 'day':
return timedelta(days=value)
elif unit == 'hour':
if unit == 'hour':
return timedelta(hours=value)
elif unit == 'minute':
if unit == 'minute':
return timedelta(minutes=value)
elif unit == 'second':
if unit == 'second':
return timedelta(seconds=value)
else:
raise ValueError(f'Invalid time unit: {unit}')
raise ValueError(f'Invalid time unit: {unit}')


class InvalidQuery(Exception):
"""Invalid query exception."""
pass


class QueryArgs(object):
class QueryArgs():
"""Build query arguments for FDSN client."""
def __init__(self, config, suffix, first_query):
"""
Expand Down

0 comments on commit a72521f

Please sign in to comment.