From 201f401c243907cdafeb7332e5cfb82862ba1e5a Mon Sep 17 00:00:00 2001
From: spencer <swilbur@usgs.gov>
Date: Mon, 23 Sep 2024 16:09:13 -0600
Subject: [PATCH] Added an exception to deal with the 204 error so that a
 stream of empty values would be returned.

---
 geomagio/edge/FDSNFactory.py | 67 ++++++++++++++++++++----------------
 1 file changed, 37 insertions(+), 30 deletions(-)

diff --git a/geomagio/edge/FDSNFactory.py b/geomagio/edge/FDSNFactory.py
index 97e742b8..0d2a07ef 100644
--- a/geomagio/edge/FDSNFactory.py
+++ b/geomagio/edge/FDSNFactory.py
@@ -256,14 +256,11 @@ class FDSNFactory(TimeseriesFactory):
                 endtime=endtime + half_delta,
                 attach_response=True,
             )
+
         except FDSNNoDataException:
             data = Stream()
         data.merge()
 
-        if channel in channel_rotations:
-            data = self._rotate_and_return_requested_channel(
-                data, sncl, starttime, endtime, channel
-            )
         if data.count() == 0 and add_empty_channels:
             data += self._get_empty_trace(
                 starttime=starttime,
@@ -275,39 +272,49 @@ class FDSNFactory(TimeseriesFactory):
                 network=sncl.network,
                 location=sncl.location,
             )
+            self._set_metadata(data, observatory, channel, type, interval)
+        # Skip further processing if the data is still empty
+        if data.count() == 0:
+            return data
+
         if data.count() != 0:
             TimeseriesUtility.pad_and_trim_trace(
                 trace=data[0], starttime=starttime, endtime=endtime
             )
+        if channel in channel_rotations:
+            data = self._rotate_and_return_requested_channel(
+                data, sncl, starttime, endtime, channel
+            )
 
         for trace in data:
-            response = trace.stats.response
-            if self.remove_sensitivity is not None:
-                if self.remove_sensitivity_flag == "remove_sensitivity":
-                    trace.remove_sensitivity()
-
-                elif self.remove_sensitivity_flag == "remove_response":
-                    trace.remove_response()
-                else:
-                    raise ValueError(
-                        f"Warning: Unrecognized remove_sensitivity value '{self.remove_sensitivity}'. No sensitivity or response removal applied."
-                    )
-            else:
-                if response.instrument_sensitivity:
-                    if response.instrument_sensitivity.input_units == "T":
-                        # some ASL stations produce data in Teslas, not nanoteslas
-                        response.instrument_sensitivity.value /= 1e9
-                        response.instrument_sensitivity.input_units = "nT"
-                    # apply total gain/sensitivity, ignore any frequency response
-                    trace.remove_sensitivity()
+            if hasattr(trace.stats, "response") and trace.stats.response is not None:
+                response = trace.stats.response
+                if self.remove_sensitivity is not None:
+                    if self.remove_sensitivity_flag == "remove_sensitivity":
+                        trace.remove_sensitivity()
+
+                    elif self.remove_sensitivity_flag == "remove_response":
+                        trace.remove_response()
+                    else:
+                        raise ValueError(
+                            f"Warning: Unrecognized remove_sensitivity value '{self.remove_sensitivity}'. No sensitivity or response removal applied."
+                        )
                 else:
-                    # according to fdsn docs, if no instrument_sensitivity,
-                    # the response must be an instrument_polynomial
-                    # https://docs.fdsn.org/projects/stationxml/en/latest/reference.html#instrumentsensitivity
-
-                    # Geomag Program observatories use 2nd order instrument_polynomial
-                    # to capture both offset and scale, but no frequency response
-                    data.remove_response()
+                    if response.instrument_sensitivity:
+                        if response.instrument_sensitivity.input_units == "T":
+                            # some ASL stations produce data in Teslas, not nanoteslas
+                            response.instrument_sensitivity.value /= 1e9
+                            response.instrument_sensitivity.input_units = "nT"
+                        # apply total gain/sensitivity, ignore any frequency response
+                        trace.remove_sensitivity()
+                    else:
+                        # according to fdsn docs, if no instrument_sensitivity,
+                        # the response must be an instrument_polynomial
+                        # https://docs.fdsn.org/projects/stationxml/en/latest/reference.html#instrumentsensitivity
+
+                        # Geomag Program observatories use 2nd order instrument_polynomial
+                        # to capture both offset and scale, but no frequency response
+                        data.remove_response()
 
         else:
             self._set_metadata(data, observatory, channel, type, interval)
-- 
GitLab