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
1540d889
Commit
1540d889
authored
5 years ago
by
Travis Rivers
Committed by
Jeremy M Fee
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
update parse method to take advantage of defaults in webservicequery object
parent
6a976561
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/webservice/data.py
+34
-46
34 additions, 46 deletions
geomagio/webservice/data.py
with
34 additions
and
46 deletions
geomagio/webservice/data.py
+
34
−
46
View file @
1540d889
...
...
@@ -5,16 +5,10 @@ from obspy import UTCDateTime
from
collections
import
OrderedDict
from
json
import
dumps
from
geomagio.edge
import
EdgeFactory
from
geomagio.iaga2002
import
IAGA2002Writer
from
geomagio.imfjson
import
IMFJSONWriter
DEFAULT_DATA_TYPE
=
"
variation
"
DEFAULT_ELEMENTS
=
(
"
X
"
,
"
Y
"
,
"
Z
"
,
"
F
"
)
DEFAULT_OUTPUT_FORMAT
=
"
iaga2002
"
DEFAULT_SAMPLING_PERIOD
=
"
60
"
ERROR_CODE_MESSAGES
=
{
204
:
"
No Data
"
,
400
:
"
Bad Request
"
,
...
...
@@ -22,7 +16,7 @@ ERROR_CODE_MESSAGES = {
409
:
"
Conflict
"
,
500
:
"
Internal Server Error
"
,
501
:
"
Not Implemented
"
,
503
:
"
Service Unavailable
"
,
503
:
"
Service Unavailable
"
}
VALID_DATA_TYPES
=
[
"
variation
"
,
"
adjusted
"
,
"
quasi-definitive
"
,
"
definitive
"
]
VALID_OUTPUT_FORMATS
=
[
"
iaga2002
"
,
"
json
"
]
...
...
@@ -30,9 +24,10 @@ VALID_SAMPLING_PERIODS = ["1", "60"]
blueprint
=
Blueprint
(
"
data
"
,
__name__
)
factory
=
EdgeFactory
(
host
=
'
cwbpub.cr.usgs.gov
'
,
port
=
2060
,
write_port
=
7981
)
host
=
os
.
getenv
(
'
host
'
,
'
cwbpub.cr.usgs.gov
'
),
port
=
os
.
getenv
(
'
port
'
,
2060
),
write_port
=
os
.
getenv
(
'
write_port
'
,
7981
)
)
def
init_app
(
app
:
Flask
):
global
blueprint
...
...
@@ -40,7 +35,6 @@ def init_app(app: Flask):
app
.
register_blueprint
(
blueprint
)
class
WebServiceQuery
(
object
):
"""
Query parameters for a web service request.
Parameters
...
...
@@ -63,13 +57,12 @@ class WebServiceQuery(object):
output format.
default
'
iaga2002
'
.
"""
def
__init__
(
self
,
observatory_id
=
None
,
starttime
=
None
,
endtime
=
None
,
elements
=
None
,
elements
=
(
"
X
"
,
"
Y
"
,
"
Z
"
,
"
F
"
)
,
sampling_period
=
60
,
data_type
=
"
variation
"
,
output_format
=
"
iaga2002
"
,
...
...
@@ -111,7 +104,6 @@ class WebServiceQuery(object):
"
Valid values are: %s
"
%
(
self
.
output_format
,
VALID_OUTPUT_FORMATS
)
)
@blueprint.route
(
"
/data
"
,
methods
=
[
"
GET
"
])
def
get_data
():
query_params
=
request
.
args
...
...
@@ -130,7 +122,12 @@ def get_data():
error_body
=
error
(
400
,
message
,
parsed_query
,
url
)
return
error_body
timeseries
=
get_timeseries
(
parsed_query
)
try
:
timeseries
=
get_timeseries
(
parsed_query
)
except
Exception
as
e
:
message
=
str
(
e
)
error_body
=
error
(
500
,
message
,
parsed_query
,
url
)
return
error_body
return
format_timeseries
(
timeseries
,
parsed_query
)
...
...
@@ -147,14 +144,14 @@ def http_error(code, message, query, url):
http_error_body
=
json_error
(
code
,
message
,
url
)
return
http_error_body
else
:
http_error_body
=
iaga2002_error
(
code
,
message
)
http_error_body
=
iaga2002_error
(
code
,
message
,
url
)
return
http_error_body
def
json_error
(
code
,
message
,
url
):
error_dict
=
OrderedDict
()
error_dict
[
'
type
'
]
=
"
Error
"
error_dict
[
'
metadata
'
]
=
OrderedDict
()
error_dict
[
'
metadata
'
][
'
status
'
]
=
400
error_dict
[
'
metadata
'
][
'
status
'
]
=
code
date
=
datetime
.
utcnow
().
strftime
(
"
%Y-%m-%dT%H:%M:%S.%fZ
"
)
error_dict
[
'
metadata
'
][
'
generated
'
]
=
date
error_dict
[
'
metadata
'
][
'
url
'
]
=
url
...
...
@@ -168,15 +165,14 @@ def json_error(code, message, url):
def
iaga2002_error
(
code
,
message
,
url
):
status_message
=
ERROR_CODE_MESSAGES
[
code
]
error_body
=
'
Error
'
+
str
(
code
)
+
'
:
'
+
status_message
+
\
'
\n\n
'
+
message
+
'
\n\n
'
+
\
'
Usage details are available from
'
+
\
'
http://geomag.usgs.gov/ws/edge/
\n\n
'
+
\
'
Request:
\n
'
+
\
url
+
'
\n\n
'
+
\
'
Request Submitted:
\n
'
+
\
datetime
.
utcnow
().
strftime
(
"
%Y-%m-%dT%H:%M:%SZ
"
)
+
'
\n\n
'
error_body
=
'
Error
'
+
str
(
code
)
+
'
:
'
\
+
status_message
+
'
\n\n
'
+
message
+
'
\n\n
'
\
+
'
Usage details are available from
'
\
+
'
http://geomag.usgs.gov/ws/edge/
\n\n
'
\
+
'
Request:
\n
'
+
url
+
'
\n\n
'
+
'
Request Submitted:
\n
'
\
+
datetime
.
utcnow
().
strftime
(
"
%Y-%m-%dT%H:%M:%SZ
"
)
+
'
\n
'
error_body
=
Response
(
error_body
,
mimetype
=
"
text/plain
"
)
return
error_body
def
parse_query
(
query
):
...
...
@@ -197,8 +193,10 @@ def parse_query(query):
WebServiceException
if any parameters are not supported.
"""
# Create web service query object
params
=
WebServiceQuery
()
# Get values
# Get
and assign
values
if
not
query
.
get
(
"
endtime
"
):
now
=
datetime
.
now
()
today
=
UTCDateTime
(
now
.
year
,
now
.
month
,
now
.
day
,
0
)
...
...
@@ -211,38 +209,30 @@ def parse_query(query):
else
:
start_time
=
UTCDateTime
(
query
.
get
(
"
starttime
"
))
if
not
query
.
get
(
"
sampling_period
"
):
sampling_period
=
DEFAULT_SAMPLING_PERIOD
else
:
if
query
.
get
(
"
sampling_period
"
):
sampling_period
=
query
.
get
(
"
sampling_period
"
)
params
.
sampling_period
=
sampling_period
if
not
query
.
get
(
"
format
"
):
format
=
DEFAULT_OUTPUT_FORMAT
else
:
if
query
.
get
(
"
format
"
):
format
=
query
.
get
(
"
format
"
)
params
.
output_format
=
format
observatory
=
query
.
get
(
"
observatory
"
)
if
not
query
.
get
(
"
channels
"
):
channels
=
DEFAULT_ELEMENTS
else
:
if
query
.
get
(
"
channels
"
):
channels
=
query
.
get
(
"
channels
"
).
split
(
"
,
"
)
params
.
elements
=
channels
type
=
query
.
get
(
"
type
"
)
params
=
WebServiceQuery
()
if
query
.
get
(
"
type
"
)
:
type
=
query
.
get
(
"
type
"
)
params
.
data_type
=
type
params
.
observatory_id
=
observatory
params
.
starttime
=
start_time
params
.
endtime
=
end_time
params
.
elements
=
channels
params
.
sampling_period
=
sampling_period
params
.
data_type
=
type
params
.
output_format
=
format
return
params
def
get_timeseries
(
query
):
"""
Parameters
...
...
@@ -271,7 +261,6 @@ def get_timeseries(query):
return
timeseries
def
format_timeseries
(
timeseries
,
query
):
"""
Formats timeseries into JSON or IAGA data
...
...
@@ -300,7 +289,6 @@ def format_timeseries(timeseries, query):
return
iaga_output
class
WebServiceException
(
Exception
):
"""
Base class for exceptions thrown by web services.
"""
pass
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