From b4ce9b7afbb519d55738ff513fb0bbdc884c3fbc Mon Sep 17 00:00:00 2001
From: bgeels <bgeels@usgs.gov>
Date: Fri, 29 Mar 2024 11:53:12 -0600
Subject: [PATCH] Add some error handling for get_instrument_calibrations

---
 .../instrument/InstrumentCalibrations.py      | 44 +++++++++++++------
 1 file changed, 31 insertions(+), 13 deletions(-)

diff --git a/geomagio/metadata/instrument/InstrumentCalibrations.py b/geomagio/metadata/instrument/InstrumentCalibrations.py
index 330ab8e8..40052b9c 100644
--- a/geomagio/metadata/instrument/InstrumentCalibrations.py
+++ b/geomagio/metadata/instrument/InstrumentCalibrations.py
@@ -327,16 +327,34 @@ def get_instrument_calibrations(
 
         # remove invalid metadata entries
         for i, m in enumerate(metadata):
-            if not m.data_valid:
-                metadata.pop(i)
-
-        instrumentCalibrations = InstrumentCalibrations(metadata)
-        calibrations = instrumentCalibrations.get_calibrations()
-
-    return [
-        c
-        for c in calibrations
-        if c["station"] == observatory
-        and (end_time is None or c["start_time"] is None or c["start_time"] < end_time)
-        and (start_time is None or c["end_time"] is None or c["end_time"] > start_time)
-    ]
+            if not m.data_valid or "instrument_category" not in m.metadata:
+                metadata.remove(m)
+
+        if not metadata or metadata is None:
+            raise ValueError(
+                f"No valid metadata returned for {observatory} for time interval: {start_time} - {end_time}"
+            )
+        else:
+            instrumentCalibrations = InstrumentCalibrations(metadata)
+            calibrations = instrumentCalibrations.get_calibrations()
+
+    if not calibrations or calibrations is None:
+        raise ValueError(
+            f"No valid calibrations returned for {observatory} for time interval: {start_time} - {end_time}"
+        )
+    else:
+        return [
+            c
+            for c in calibrations
+            if c["station"] == observatory
+            and (
+                end_time is None
+                or c["start_time"] is None
+                or c["start_time"] < end_time
+            )
+            and (
+                start_time is None
+                or c["end_time"] is None
+                or c["end_time"] > start_time
+            )
+        ]
-- 
GitLab