From 23373a53e04da0811fce4b84649ebab3427a9c47 Mon Sep 17 00:00:00 2001
From: spencer <swilbur@usgs.gov>
Date: Tue, 3 Oct 2023 14:25:30 -0600
Subject: [PATCH] Updated the IRISFactory to support the new VariometerMetadata
 class. Also removed unnecessary lines of code in VariometerMetadata.py and
 ObservatoryMetadata.py.

---
 geomagio/ObservatoryMetadata.py | 54 ++++++++++++++++++++++++++++++
 geomagio/VariometerMetaData.py  | 58 ++++++++++++++++++++++++++++++---
 geomagio/api/ws/Observatory.py  |  5 ++-
 geomagio/edge/IRISFactory.py    |  3 +-
 4 files changed, 114 insertions(+), 6 deletions(-)

diff --git a/geomagio/ObservatoryMetadata.py b/geomagio/ObservatoryMetadata.py
index 2aa6adbf..cdab2586 100644
--- a/geomagio/ObservatoryMetadata.py
+++ b/geomagio/ObservatoryMetadata.py
@@ -282,6 +282,24 @@ DEFAULT_METADATA = {
         },
         "interval_specific": DEFAULT_INTERVAL_SPECIFIC,
     },
+    "GUT": {
+        "metadata": {
+            "station_name": "Guatamala Test",
+            "agency_name": "United States Geological Survey (USGS)",
+            "geodetic_latitude": "13.588",
+            "geodetic_longitude": "144.867",
+            "elevation": "140",
+            "sensor_orientation": "HDZF",
+            "sensor_sampling_rate": 100.0,
+            "declination_base": 764,
+            "is_gin": False,
+            "is_intermagnet": False,
+            "conditions_of_use": "The Conditions of Use for data provided"
+            + " through INTERMAGNET and acknowledgement templates"
+            + " can be found at www.intermagnet.org",
+        },
+        "interval_specific": DEFAULT_INTERVAL_SPECIFIC,
+    },
     "HON": {
         "metadata": {
             "station_name": "Honolulu",
@@ -300,6 +318,24 @@ DEFAULT_METADATA = {
         },
         "interval_specific": DEFAULT_INTERVAL_SPECIFIC,
     },
+    "HOT": {
+        "metadata": {
+            "station_name": "Hot Test",
+            "agency_name": "United States Geological Survey (USGS)",
+            "geodetic_latitude": "21.316",
+            "geodetic_longitude": "202.000",
+            "elevation": "4",
+            "sensor_orientation": "HDZF",
+            "sensor_sampling_rate": 100.0,
+            "declination_base": 5982,
+            "is_gin": False,
+            "is_intermagnet": False,
+            "conditions_of_use": "The Conditions of Use for data provided"
+            + " through INTERMAGNET and acknowledgement templates"
+            + " can be found at www.intermagnet.org",
+        },
+        "interval_specific": DEFAULT_INTERVAL_SPECIFIC,
+    },
     "KAK": {
         "metadata": {
             "station_name": "Kakioka",
@@ -391,6 +427,24 @@ DEFAULT_METADATA = {
         },
         "interval_specific": DEFAULT_INTERVAL_SPECIFIC,
     },
+    "SJT": {
+        "metadata": {
+            "station_name": "SJT Test",
+            "agency_name": "United States Geological Survey (USGS)",
+            "geodetic_latitude": "18.113",
+            "geodetic_longitude": "293.849",
+            "elevation": "424",
+            "sensor_orientation": "HDZF",
+            "sensor_sampling_rate": 100.0,
+            "declination_base": 208439,
+            "is_gin": False,
+            "is_intermagnet": False,
+            "conditions_of_use": "The Conditions of Use for data provided"
+            + " through INTERMAGNET and acknowledgement templates"
+            + " can be found at www.intermagnet.org",
+        },
+        "interval_specific": DEFAULT_INTERVAL_SPECIFIC,
+    },
     "TUC": {
         "metadata": {
             "station_name": "Tucson",
diff --git a/geomagio/VariometerMetaData.py b/geomagio/VariometerMetaData.py
index deb2f6d7..990a65c6 100644
--- a/geomagio/VariometerMetaData.py
+++ b/geomagio/VariometerMetaData.py
@@ -1,7 +1,3 @@
-from pydantic import BaseModel
-from typing import Dict
-
-
 DEFAULT_INTERVAL_SPECIFIC = {
     "day": {
         "data_interval_type": "1-day (00:00-23:59)",
@@ -131,3 +127,57 @@ DEFAULT_ASL_METADATA = {
         "interval_specific": DEFAULT_INTERVAL_SPECIFIC,
     },
 }
+
+
+class VariometerMetadata(object):
+    """Helper class for providing all the metadata needed for a geomag
+          timeseries.
+    Notes
+    -----
+    Currently the only method is set_metadata.  Eventually this will probably
+    pull from a database, or maybe a config file.
+    """
+
+    def __init__(self, metadata=None, interval_specific=None):
+        self.metadata = metadata or DEFAULT_ASL_METADATA
+        self.interval_specific = interval_specific or DEFAULT_INTERVAL_SPECIFIC
+
+    def set_metadata(self, stats, observatory, channel, type, interval):
+        """Set timeseries metadata (aka a traces stats)
+
+        Parameters
+        ----------
+        stats : obspy.core.trace.stats
+            the class associated with a given obspy trace, which contains
+            it's metadata
+        observatory : string
+            the observatory code to look up.
+        channel : str
+            single character channel {H, E, D, Z, F}
+        type : {'variation', 'quasi-definitive'}
+            data type.
+        interval : {'minute', 'second'}
+            data interval.
+
+        Returns
+        -------
+        obspy.core.trace.stats
+          the combined stats and the default metadata.
+        """
+        stats["channel"] = channel
+        stats["data_interval"] = interval
+        stats["data_type"] = type
+        if observatory not in self.metadata:
+            return
+        # copy in standard metadata
+        metadata = self.metadata[observatory]["metadata"]
+        for key in metadata:
+            stats[key] = metadata[key]
+        # copy in interval specific metadata
+        interval_specific = self.interval_specific
+        if "interval_specific" in self.metadata[observatory]:
+            interval_specific = self.metadata[observatory]["interval_specific"]
+        # stats['data_interval_type'] = data_interval_type[interval]
+        if interval in interval_specific:
+            for key in interval_specific[interval]:
+                stats[key] = interval_specific[interval][key]
diff --git a/geomagio/api/ws/Observatory.py b/geomagio/api/ws/Observatory.py
index e3aa9696..73a8a4e6 100644
--- a/geomagio/api/ws/Observatory.py
+++ b/geomagio/api/ws/Observatory.py
@@ -1,5 +1,8 @@
 from pydantic import BaseModel, validator
-from geomagio.ObservatoryMetadata import DEFAULT_METADATA, DEFAULT_INTERVAL_SPECIFIC
+from geomagio.ObservatoryMetadata import (
+    DEFAULT_METADATA,
+    DEFAULT_INTERVAL_SPECIFIC,
+)  # remove data_int here as well
 from geomagio.VariometerMetaData import DEFAULT_ASL_METADATA
 from typing import Dict
 
diff --git a/geomagio/edge/IRISFactory.py b/geomagio/edge/IRISFactory.py
index 0d0601af..5499bdf8 100644
--- a/geomagio/edge/IRISFactory.py
+++ b/geomagio/edge/IRISFactory.py
@@ -5,6 +5,7 @@ from obspy.clients.iris.client import Client
 
 from ..geomag_types import DataInterval, DataType
 from ..ObservatoryMetadata import ObservatoryMetadata
+from geomagio.VariometerMetaData import VariometerMetadata
 from .. import TimeseriesUtility
 from .IRISSNCL import IRISSNCL
 from .MiniSeedFactory import MiniSeedFactory
@@ -72,7 +73,7 @@ class IRISFactory(MiniSeedFactory):
             channels=channels,
             type=type,
             interval=interval,
-            observatoryMetadata=observatoryMetadata or ObservatoryMetadata(),
+            observatoryMetadata=observatoryMetadata or VariometerMetadata(),
             locationCode=locationCode,
             convert_channels=convert_channels,
         )
-- 
GitLab