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
1b839cf0
Commit
1b839cf0
authored
2 years ago
by
Jeremy M Fee
Browse files
Options
Downloads
Plain Diff
Merge branch 'add_absolutes' into 'master'
Add absolutes See merge request
!189
parents
dc14f3c5
a9d3ea16
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!189
Add absolutes
Pipeline
#184116
passed
2 years ago
Stage: init
Stage: test
Stage: integration
Stage: deploy
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
geomagio/processing/copy_absolutes.py
+149
-0
149 additions, 0 deletions
geomagio/processing/copy_absolutes.py
pyproject.toml
+1
-0
1 addition, 0 deletions
pyproject.toml
with
150 additions
and
0 deletions
geomagio/processing/copy_absolutes.py
0 → 100644
+
149
−
0
View file @
1b839cf0
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 7 11:05:19 2022
@author: awernle
"""
import
json
import
os
from
datetime
import
date
,
datetime
,
time
,
timedelta
from
typing
import
List
import
typer
from
obspy
import
UTCDateTime
from
..metadata
import
Metadata
,
MetadataCategory
,
MetadataFactory
from
..residual
import
Reading
,
WebAbsolutesFactory
TODAY
=
datetime
.
combine
(
date
.
today
(),
time
(
0
,
0
,
0
))
def
copy_absolutes
(
observatory
:
str
=
typer
.
Option
(...,
help
=
"
Observatory code
"
),
starttime
:
datetime
=
typer
.
Option
(
default
=
TODAY
-
timedelta
(
days
=
1
),
help
=
"
Search start time, default
"
),
endtime
:
datetime
=
typer
.
Option
(
default
=
TODAY
),
metadata_token
:
str
=
typer
.
Option
(
default
=
os
.
getenv
(
"
GITLAB_API_TOKEN
"
),
help
=
"
Token for metadata web service.
"
,
metavar
=
"
TOKEN
"
,
show_default
=
"
environment variable GITLAB_API_TOKEN
"
,
),
metadata_url
:
str
=
typer
.
Option
(
default
=
"
https://geomag.usgs.gov/ws/secure
"
,
help
=
"
URL to metadata web service
"
,
metavar
=
"
URL
"
,
),
web_absolutes_url
:
str
=
typer
.
Option
(
default
=
"
https://geomag.usgs.gov/baselines/observation.json.php
"
,
help
=
"
URL to web absolutes service
"
,
metavar
=
"
URL
"
,
),
):
"""
Copy absolutes from the web absolutes service into the metadata service.
"""
# read readings from web absolutes
web_absolutes_factory
=
WebAbsolutesFactory
(
url
=
web_absolutes_url
)
readings
=
get_readings
(
factory
=
web_absolutes_factory
,
observatory
=
observatory
,
starttime
=
UTCDateTime
(
starttime
),
endtime
=
UTCDateTime
(
endtime
),
)
print
(
f
"
Found
{
len
(
readings
)
}
absolutes
"
)
# write readings to metadata service
metadata_factory
=
MetadataFactory
(
token
=
metadata_token
,
url
=
metadata_url
)
with
typer
.
progressbar
(
iterable
=
readings
,
label
=
"
Uploading to metadata service
"
)
as
progressbar
:
for
reading
in
progressbar
:
upload_reading
(
factory
=
metadata_factory
,
reading
=
reading
)
def
create_reading_metadata
(
reading
:
Reading
)
->
Metadata
:
"""
Create reading metadata object.
Parameters
----------
reading:
reading to convert to metadata.
Returns
-------
metadata object for reading
"""
measurement_times
=
[
m
.
time
for
m
in
reading
.
measurements
if
m
.
time
]
metadata
=
Metadata
(
category
=
MetadataCategory
.
READING
,
created_by
=
reading
.
metadata
.
get
(
"
observer
"
,
"
copy_absolutes
"
),
endtime
=
max
(
measurement_times
),
metadata
=
json
.
loads
(
reading
.
json
()),
network
=
"
NT
"
,
starttime
=
min
(
measurement_times
),
station
=
reading
.
metadata
[
"
station
"
],
status
=
"
reviewed
"
if
reading
.
metadata
.
get
(
"
reviewed
"
)
else
"
new
"
,
updated_by
=
reading
.
metadata
.
get
(
"
reviewer
"
),
)
return
metadata
def
get_readings
(
factory
:
WebAbsolutesFactory
,
observatory
:
str
,
starttime
:
UTCDateTime
,
endtime
:
UTCDateTime
,
)
->
List
[
Reading
]:
"""
Get readings from web absolutes.
Parameters
----------
factory
configured WebAbsolutesFactory
observatory
search observatory
starttime
search start time
endtime
search end time
Returns
-------
list of found readings
"""
readings
=
factory
.
get_readings
(
observatory
=
observatory
,
starttime
=
UTCDateTime
(
starttime
),
endtime
=
UTCDateTime
(
endtime
),
include_measurements
=
True
,
)
return
readings
def
main
()
->
None
:
"""
Entrypoint for copy absolutes method.
"""
# for one command, can use typer.run
typer
.
run
(
copy_absolutes
)
def
upload_reading
(
factory
:
MetadataFactory
,
reading
:
Reading
)
->
Metadata
:
"""
Upload reading to metadata service
Parameters
----------
factory
factory configured for metadata service
reading
reading to upload
Returns
-------
created metadata object
"""
metadata
=
create_reading_metadata
(
reading
=
reading
)
# TODO: should this check if metadata was already uploaded?
# TODO: should that check occur before calling this method?
return
factory
.
create_metadata
(
metadata
=
metadata
)
if
__name__
==
"
__main__
"
:
main
()
This diff is collapsed.
Click to expand it.
pyproject.toml
+
1
−
0
View file @
1b839cf0
...
@@ -71,3 +71,4 @@ geomag-py = "geomagio.Controller:main"
...
@@ -71,3 +71,4 @@ geomag-py = "geomagio.Controller:main"
magproc-prepfiles
=
"geomagio.processing.magproc:main"
magproc-prepfiles
=
"geomagio.processing.magproc:main"
make-cal
=
"geomagio.processing.make_cal:main"
make-cal
=
"geomagio.processing.make_cal:main"
geomag-filter
=
"geomagio.processing.filters:main"
geomag-filter
=
"geomagio.processing.filters:main"
copy-absolutes
=
"geomagio.processing.copy_absolutes:main"
\ No newline at end of file
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