Skip to content
Snippets Groups Projects

Metadata based instrument calibrations (restoring changes from MR #306 with improvements)

2 files
+ 13
17
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -3,7 +3,7 @@ from obspy import UTCDateTime
@@ -3,7 +3,7 @@ from obspy import UTCDateTime
from requests.exceptions import JSONDecodeError, ConnectTimeout
from requests.exceptions import JSONDecodeError, ConnectTimeout
from geomagio.metadata import Metadata, MetadataFactory, MetadataCategory
from geomagio.metadata import Metadata, MetadataFactory, MetadataCategory
from geomagio.Util import write_cache_file, read_cache_file
from geomagio.Util import write_state_file, read_state_file
class InstrumentCalibrations:
class InstrumentCalibrations:
@@ -315,7 +315,7 @@ def get_instrument_calibrations(
@@ -315,7 +315,7 @@ def get_instrument_calibrations(
"""
"""
if not calibrations:
if not calibrations:
cache_filename = f"{observatory}_instrument_cals.json"
state_filename = f"{observatory}_instrument_cals.json"
metadata = []
metadata = []
factory = MetadataFactory(
factory = MetadataFactory(
@@ -326,20 +326,16 @@ def get_instrument_calibrations(
@@ -326,20 +326,16 @@ def get_instrument_calibrations(
starttime=start_time,
starttime=start_time,
endtime=end_time,
endtime=end_time,
station=observatory,
station=observatory,
 
data_valid=True,
)
)
try:
try:
metadata = factory.get_metadata(query=query)
metadata = factory.get_metadata(query=query)
# remove invalid metadata entries
for i, m in enumerate(metadata):
if not m.data_valid or "instrument_category" not in m.metadata:
metadata.remove(m)
except:
except:
print(
print(
"Warning: An error occurred while trying to pull metadata from the metadata server!"
"Warning: An error occurred while trying to pull metadata from the metadata server!"
)
)
if not metadata or metadata is None:
if not metadata:
print(
print(
f"Warning: No valid metadata returned for {observatory} for time interval: {start_time} - {end_time}"
f"Warning: No valid metadata returned for {observatory} for time interval: {start_time} - {end_time}"
)
)
@@ -350,15 +346,15 @@ def get_instrument_calibrations(
@@ -350,15 +346,15 @@ def get_instrument_calibrations(
except ValueError as e:
except ValueError as e:
print(e)
print(e)
if not calibrations or calibrations is None:
if not calibrations:
print(
print(
f"Warning: No valid calibrations generated for {observatory} for time interval: {start_time} - {end_time}"
f"Warning: No valid calibrations generated for {observatory} for time interval: {start_time} - {end_time}"
)
)
print(f"Pulling calibrations for {observatory} from cache file.")
print(f"Pulling calibrations for {observatory} from state file.")
calibrations = read_cache_file(filename=cache_filename)
calibrations = read_state_file(filename=state_filename)
else:
else:
write_cache_file(cache_filename, calibrations)
write_state_file(state_filename, calibrations)
return [
return [
c
c
Loading