Newer
Older
"""Abstract Timeseries Factory Interface."""
import obspy.core
from TimeseriesFactoryException import TimeseriesFactoryException
class TimeseriesFactory(object):
"""Base class for timeseries factories.
Add input support by:
- implementing `parse_string`
- or, overriding `get_timeseries`
Add output support by:
- implementing `write_file`
- or, overriding `put_timeseries`
Attributes
----------
observatory : str
default observatory code, usually 3 characters.
channels : array_like
default list of channels to load, optional.
default ('H', 'D', 'Z', 'F')
type : {'definitive', 'provisional', 'quasi-definitive', 'variation'}
default data type, optional.
default 'variation'.
interval : {'daily', 'hourly', 'minute', 'monthly', 'second'}
data interval, optional.
default 'minute'.

Jeremy M Fee
committed
A string that contains replacement patterns.
See https://github.com/usgs/geomag-algorithms/blob/master/docs/io.md
and/or TimeseriesFactory._get_url()
urlInterval : int
Interval in seconds between URLs.
Intervals begin at the unix epoch (1970-01-01T00:00:00Z)
"""
def __init__(self, observatory=None, channels=('H', 'D', 'Z', 'F'),
type='variation', interval='minute',
urlTemplate='', urlInterval=-1):
self.observatory = observatory
self.channels = channels
self.type = type
self.interval = interval
self.urlInterval = urlInterval
def get_timeseries(self, starttime, endtime, observatory=None,
channels=None, type=None, interval=None):
"""Get timeseries data.
Support for specific channels, types, and intervals varies
between factory and observatory. Subclasses should raise
TimeseriesFactoryException if the data is not available, or
if an error occurs accessing data.
Parameters
----------
starttime : UTCDateTime
time of first sample in timeseries.
endtime : UTCDateTime
time of last sample in timeseries.
observatory : str
observatory code, usually 3 characters, optional.
uses default if unspecified.
channels : array_like
list of channels to load, optional.
uses default if unspecified.
type : {'definitive', 'provisional', 'quasi-definitive', 'variation'}
data type, optional.
uses default if unspecified.
interval : {'daily', 'hourly', 'minute', 'monthly', 'second'}
data interval, optional.
uses default if unspecified.
Returns
-------
obspy.core.Stream
stream containing traces for requested timeseries.
Raises
------
TimeseriesFactoryException
if any parameters are unsupported, or errors occur loading data.

Jeremy M Fee
committed
observatory = observatory or self.observatory
channels = channels or self.channels
type = type or self.type
interval = interval or self.interval
timeseries = obspy.core.Stream()
urlIntervals = Util.get_intervals(
starttime=starttime,
endtime=endtime,
size=self.urlInterval)
for urlInterval in urlIntervals:
url = self._get_url(
observatory=observatory,
date=urlInterval['start'],
type=type,
interval=interval,
channels=channels)
data = Util.read_url(url)
try:

Jeremy M Fee
committed
timeseries += self.parse_string(data,
observatory=observatory,
type=type,
interval=interval,
channels=channels)

Jeremy M Fee
committed
except NotImplementedError:
raise NotImplementedError('"get_timeseries" not implemented')
except Exception as e:
print >> sys.stderr, "Error parsing data: " + str(e)
print >> sys.stderr, data
timeseries.merge()
timeseries.trim(starttime, endtime)
return timeseries

Jeremy M Fee
committed
def parse_string(self, data, **kwargs):
"""Parse the contents of a string in the format of an IAGA2002 file.
Parameters
----------

Jeremy M Fee
committed
data : str
string containing parsable content.
Returns
-------
obspy.core.Stream
parsed data.
"""
raise NotImplementedError('"parse_string" not implemented')
def put_timeseries(self, timeseries, starttime=None, endtime=None,
channels=None, type=None, interval=None):
"""Store timeseries data.
Parameters
----------
timeseries : obspy.core.Stream
stream containing traces to store.
starttime : UTCDateTime
time of first sample in timeseries to store.
uses first sample if unspecified.
endtime : UTCDateTime
time of last sample in timeseries to store.
uses last sample if unspecified.
channels : array_like
list of channels to store, optional.
uses default if unspecified.
type : {'definitive', 'provisional', 'quasi-definitive', 'variation'}
data type, optional.
uses default if unspecified.
interval : {'daily', 'hourly', 'minute', 'monthly', 'second'}
data interval, optional.
uses default if unspecified.
Raises
------
TimeseriesFactoryException
if any errors occur.
"""

Jeremy M Fee
committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
if not self.urlTemplate.startswith('file://'):
raise TimeseriesFactoryException('Only file urls are supported')
channels = channels or self.channels
type = type or self.type
interval = interval or self.interval
stats = timeseries[0].stats
delta = stats.delta
observatory = stats.station
starttime = starttime or stats.starttime
endtime = endtime or stats.endtime
urlIntervals = Util.get_intervals(
starttime=starttime,
endtime=endtime,
size=self.urlInterval)
for urlInterval in urlIntervals:
url = self._get_url(
observatory=observatory,
date=urlInterval['start'],
type=type,
interval=interval,
channels=channels)
url_data = timeseries.slice(
starttime=urlInterval['start'],
# subtract delta to omit the sample at end: `[start, end)`
endtime=(urlInterval['end'] - delta))
url_file = Util.get_file_from_url(url, createParentDirectory=True)
with open(url_file, 'wb') as fh:
try:
self.write_file(fh, url_data, channels)
except NotImplementedError:
raise NotImplementedError(
'"put_timeseries" not implemented')

Jeremy M Fee
committed
def write_file(self, fh, timeseries, channels):
"""Write timeseries data to the given file object.
Parameters
----------
fh : writable
file handle where data is written.
timeseries : obspy.core.Stream
stream containing traces to store.
channels : list
list of channels to store.
"""
raise NotImplementedError('"write_file" not implemented')
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
def _get_file_from_url(self, url):
"""Get a file for writing.
Ensures parent directory exists.
Parameters
----------
url : str
path to file
Returns
-------
str
path to file without file:// prefix
Raises
------
TimeseriesFactoryException
if url does not start with file://
"""
if not url.startswith('file://'):
raise TimeseriesFactoryException(
'Only file urls are supported for writing')
filename = url.replace('file://', '')
parent = os.path.dirname(filename)
if not os.path.exists(parent):
os.makedirs(parent)
return filename
def _get_url(self, observatory, date, type='variation', interval='minute',
channels=None):
"""Get the url for a specified file.
Replaces patterns (described in class docstring) with values based on
parameter values.
Parameters
----------
observatory : str
observatory code.
date : obspy.core.UTCDateTime
day to fetch (only year, month, day are used)
type : {'variation', 'quasi-definitive', 'definitive'}
data type.
interval : {'minute', 'second', 'hourly', 'daily'}
data interval.
channels : list
list of data channels being requested
Raises
------
TimeseriesFactoryException
if type or interval are not supported.
"""

Jeremy M Fee
committed
params = {

Jeremy M Fee
committed
'i': self._get_interval_abbreviation(interval),
'interval': self._get_interval_name(interval),
# used by Hermanus
'minute': date.hour * 60 + date.minute,
# end Hermanus
# used by Kakioka
'month': date.strftime('%b').lower(),
'MONTH': date.strftime('%b').upper(),
# end Kakioka

Jeremy M Fee
committed
'obs': observatory.lower(),
'OBS': observatory.upper(),
't': self._get_type_abbreviation(type),
'type': self._get_type_name(type),
# LEGACY
# old date properties, string.format supports any strftime format
# i.e. '{date:%j}'
'julian': date.strftime('%j'),
'year': date.strftime('%Y'),

Jeremy M Fee
committed
'ymd': date.strftime('%Y%m%d')
}
if '{' in self.urlTemplate:
# use new style string formatting
return self.urlTemplate.format(**params)
# use old style string interpolation
return self.urlTemplate % params
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
def _get_interval_abbreviation(self, interval):
"""Get abbreviation for a data interval.
Used by ``_get_url`` to replace ``%(i)s`` in urlTemplate.
Parameters
----------
interval : {'daily', 'hourly', 'minute', 'monthly', 'second'}
Returns
-------
abbreviation for ``interval``.
Raises
------
TimeseriesFactoryException
if ``interval`` is not supported.
"""
interval_abbr = None
if interval == 'daily':
interval_abbr = 'day'
elif interval == 'hourly':
interval_abbr = 'hor'
elif interval == 'minute':
interval_abbr = 'min'
elif interval == 'monthly':
interval_abbr = 'mon'
elif interval == 'second':
interval_abbr = 'sec'
else:
raise TimeseriesFactoryException(
'Unexpected interval "%s"' % interval)
return interval_abbr
def _get_interval_name(self, interval):
"""Get name for a data interval.
Used by ``_get_url`` to replace ``%(interval)s`` in urlTemplate.
Parameters
----------
interval : {'minute', 'second'}
Returns
-------
name for ``interval``.
Raises
------
TimeseriesFactoryException
if ``interval`` is not supported.
"""
interval_name = None
if interval == 'minute':
interval_name = 'OneMinute'
elif interval == 'second':
interval_name = 'OneSecond'
else:
raise TimeseriesFactoryException(
'Unsupported interval "%s"' % interval)
return interval_name
def _get_type_abbreviation(self, type):
"""Get abbreviation for a data type.
Used by ``_get_url`` to replace ``%(t)s`` in urlTemplate.
Parameters
----------
type : {'definitive', 'provisional', 'quasi-definitive', 'variation'}
Returns
-------
name for ``type``.
Raises
------
TimeseriesFactoryException
if ``type`` is not supported.
"""
type_abbr = None
if type == 'definitive':
type_abbr = 'd'
elif type == 'provisional':
type_abbr = 'p'
elif type == 'quasi-definitive':
type_abbr = 'q'
elif type == 'variation':
type_abbr = 'v'
else:
raise TimeseriesFactoryException(
'Unexpected type "%s"' % type)
return type_abbr
def _get_type_name(self, type):
"""Get name for a data type.
Used by ``_get_url`` to replace ``%(type)s`` in urlTemplate.
Parameters
----------
type : {'variation', 'quasi-definitive'}
Returns
-------
name for ``type``.
Raises
------
TimeseriesFactoryException
if ``type`` is not supported.
"""
type_name = None
if type == 'variation':
type_name = ''
elif type == 'quasi-definitive':
type_name = 'QuasiDefinitive'
else:
raise TimeseriesFactoryException(
'Unsupported type "%s"' % type)
return type_name