Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
geomag-algorithms
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ghsc
National Geomagnetism Program
geomag-algorithms
Commits
d040e85c
Commit
d040e85c
authored
9 years ago
by
Hal Simpson
Browse files
Options
Downloads
Patches
Plain Diff
Add Url code to TimeSeriesFactory
parent
fe209273
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
geomagio/TimeseriesFactory.py
+199
-1
199 additions, 1 deletion
geomagio/TimeseriesFactory.py
with
199 additions
and
1 deletion
geomagio/TimeseriesFactory.py
+
199
−
1
View file @
d040e85c
"""
Abstract Timeseries Factory Interface.
"""
"""
Abstract Timeseries Factory Interface.
"""
import
os
from
TimeseriesFactoryException
import
TimeseriesFactoryException
class
TimeseriesFactory
(
object
):
class
TimeseriesFactory
(
object
):
...
@@ -17,13 +19,25 @@ class TimeseriesFactory(object):
...
@@ -17,13 +19,25 @@ class TimeseriesFactory(object):
interval : {
'
daily
'
,
'
hourly
'
,
'
minute
'
,
'
monthly
'
,
'
second
'
}
interval : {
'
daily
'
,
'
hourly
'
,
'
minute
'
,
'
monthly
'
,
'
second
'
}
data interval, optional.
data interval, optional.
default
'
minute
'
.
default
'
minute
'
.
urlTemplate : str
A string that contains any of the following replacement patterns:
-
'
%(i)s
'
: interval abbreviation
-
'
%(interval)s
'
interval name
-
'
%(julian)s
'
julian date
-
'
%(obs)s
'
lowercase observatory code
-
'
%(OBS)s
'
uppercase observatory code
-
'
%(t)s
'
type abbreviation
-
'
%(type)s
'
type name
-
'
%(year)s
'
year formatted as YYYY
-
'
%(ymd)s
'
time formatted as YYYYMMDD
"""
"""
def
__init__
(
self
,
observatory
=
None
,
channels
=
(
'
H
'
,
'
D
'
,
'
Z
'
,
'
F
'
),
def
__init__
(
self
,
observatory
=
None
,
channels
=
(
'
H
'
,
'
D
'
,
'
Z
'
,
'
F
'
),
type
=
'
variation
'
,
interval
=
'
minute
'
):
type
=
'
variation
'
,
interval
=
'
minute
'
,
urlTemplate
=
''
):
self
.
observatory
=
observatory
self
.
observatory
=
observatory
self
.
channels
=
channels
self
.
channels
=
channels
self
.
type
=
type
self
.
type
=
type
self
.
interval
=
interval
self
.
interval
=
interval
self
.
urlTemplate
=
urlTemplate
def
get_timeseries
(
self
,
starttime
,
endtime
,
observatory
=
None
,
def
get_timeseries
(
self
,
starttime
,
endtime
,
observatory
=
None
,
channels
=
None
,
type
=
None
,
interval
=
None
):
channels
=
None
,
type
=
None
,
interval
=
None
):
...
@@ -94,3 +108,187 @@ class TimeseriesFactory(object):
...
@@ -94,3 +108,187 @@ class TimeseriesFactory(object):
if any errors occur.
if any errors occur.
"""
"""
raise
NotImplementedError
(
'"
put_timeseries
"
not implemented
'
)
raise
NotImplementedError
(
'"
put_timeseries
"
not implemented
'
)
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
'
):
"""
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.
Raises
------
TimeseriesFactoryException
if type or interval are not supported.
"""
return
self
.
urlTemplate
%
{
'
i
'
:
self
.
_get_interval_abbreviation
(
interval
),
'
interval
'
:
self
.
_get_interval_name
(
interval
),
'
julian
'
:
date
.
strftime
(
"
%j
"
),
'
obs
'
:
observatory
.
lower
(),
'
OBS
'
:
observatory
.
upper
(),
'
t
'
:
self
.
_get_type_abbreviation
(
type
),
'
type
'
:
self
.
_get_type_name
(
type
),
'
year
'
:
date
.
strftime
(
"
%Y
"
),
'
ymd
'
:
date
.
strftime
(
'
%Y%m%d
'
)}
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment