From a5fbbd4513ab0fa0c8f13b70875bc7dc6dadddf4 Mon Sep 17 00:00:00 2001
From: pcain-usgs <pcain@usgs.gov>
Date: Thu, 30 Apr 2020 11:05:20 -0600
Subject: [PATCH] Account for azimuth shift, create default residual

---
 geomagio/residual/Calculation.py | 17 +++++++++++------
 geomagio/residual/Measurement.py |  2 +-
 2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/geomagio/residual/Calculation.py b/geomagio/residual/Calculation.py
index 37776f54d..5439186ca 100644
--- a/geomagio/residual/Calculation.py
+++ b/geomagio/residual/Calculation.py
@@ -34,7 +34,7 @@ def calculate(reading: Reading) -> Reading:
     inclination, f, mean = calculate_I(
         hemisphere=reading.hemisphere, measurements=reading.measurements
     )
-    corrected_f = f + reading.pier_correction  # TODO: should this be returned?
+    corrected_f = f + reading.pier_correction
     # calculate absolutes
     absoluteH, absoluteZ = calculate_HZ_absolutes(
         corrected_f=corrected_f, inclination=inclination, mean=mean, reference=reference
@@ -46,11 +46,14 @@ def calculate(reading: Reading) -> Reading:
         reference=reference,
     )
     # calculate scale
-    scale_value = calculate_scale_value(
-        corrected_f=corrected_f,
-        inclination=inclination,
-        measurements=reading[mt.NORTH_DOWN_SCALE],
-    )
+    if len(reading[mt.NORTH_DOWN_SCALE]) > 0:
+        scale_value = calculate_scale_value(
+            corrected_f=corrected_f,
+            inclination=inclination,
+            measurements=reading[mt.NORTH_DOWN_SCALE],
+        )
+    else:
+        scale_value = None
     # create new reading object
     calculated = Reading(
         absolutes=[absoluteD, absoluteH, absoluteZ],
@@ -105,6 +108,8 @@ def calculate_D_absolute(
             for m in declination_measurements
         ]
     )
+    if azimuth > 180:
+        azimuth -= 180
     # add subtract average mark angle from average meridian angle and add
     # azimuth to get the declination baseline
     d_b = (meridian - average_mark) + azimuth
diff --git a/geomagio/residual/Measurement.py b/geomagio/residual/Measurement.py
index 27d98ed5c..f31be4554 100644
--- a/geomagio/residual/Measurement.py
+++ b/geomagio/residual/Measurement.py
@@ -87,7 +87,7 @@ def measurement_index(
 
 def safe_average(l: List[Optional[float]]):
     values = l and [f for f in l if f] or None
-    return values and numpy.nanmean(values) or None
+    return values and numpy.nanmean(values) or 0.0
 
 
 def safe_max(l: List[Optional[float]]):
-- 
GitLab