From 0266b40715cb98f42f1e767b59905b22de8b1202 Mon Sep 17 00:00:00 2001 From: pcain-usgs <pcain@usgs.gov> Date: Fri, 30 Apr 2021 15:00:14 -0600 Subject: [PATCH] Use missing measurements method in calculation --- geomagio/api/ws/algorithms.py | 17 +---------------- geomagio/residual/Calculation.py | 22 ++++++++++++++++++---- geomagio/residual/Measurement.py | 4 ---- geomagio/residual/__init__.py | 2 ++ 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/geomagio/api/ws/algorithms.py b/geomagio/api/ws/algorithms.py index f904dd9a0..c67c463fe 100644 --- a/geomagio/api/ws/algorithms.py +++ b/geomagio/api/ws/algorithms.py @@ -8,9 +8,7 @@ from ...algorithm import DbDtAlgorithm from ...residual import ( calculate, Reading, - DECLINATION_TYPES, - INCLINATION_TYPES, - MARK_TYPES, + get_missing_measurement_types, ) from .DataApiQuery import DataApiQuery from .data import format_timeseries, get_data_factory, get_data_query, get_timeseries @@ -46,16 +44,3 @@ def calculate_residual(reading: Reading, adjust_reference: bool = True): detail=f"Missing {missing_types} measurements in input reading", ) return calculate(reading=reading, adjust_reference=adjust_reference) - - -def get_missing_measurement_types(reading: Reading) -> List[str]: - measurement_types = [m.measurement_type for m in reading.measurements] - missing_types = [] - missing_types.extend( - [type for type in DECLINATION_TYPES if type not in measurement_types] - ) - missing_types.extend( - [type for type in INCLINATION_TYPES if type not in measurement_types] - ) - missing_types.extend([type for type in MARK_TYPES if type not in measurement_types]) - return missing_types diff --git a/geomagio/residual/Calculation.py b/geomagio/residual/Calculation.py index c18fe228e..6c44d4af3 100644 --- a/geomagio/residual/Calculation.py +++ b/geomagio/residual/Calculation.py @@ -28,10 +28,11 @@ def calculate(reading: Reading, adjust_reference: bool = True) -> Reading: NOTE: rest of reading object is shallow copy. """ # reference measurement, used to adjust absolutes - try: - reference = adjust_reference and reading[mt.WEST_DOWN][0] or None - except: - raise ValueError(f"Missing {mt.WEST_DOWN.value} measurement") + missing_types = get_missing_measurement_types(reading=reading) + if len(missing_types) != 0: + missing_types = ", ".join(t.value for t in missing_types) + raise ValueError(f"Missing {missing_types} measurements in input reading") + reference = adjust_reference and reading[mt.WEST_DOWN][0] or None # calculate inclination inclination, f, i_mean = calculate_I( hemisphere=reading.hemisphere, measurements=reading.measurements @@ -281,3 +282,16 @@ def calculate_scale_value( residual_change = m2.residual - m1.residual scale_value = corrected_f * field_change / np.abs(residual_change) return scale_value + + +def get_missing_measurement_types(reading: Reading) -> List[str]: + measurement_types = [m.measurement_type for m in reading.measurements] + missing_types = [] + missing_types.extend( + [type for type in DECLINATION_TYPES if type not in measurement_types] + ) + missing_types.extend( + [type for type in INCLINATION_TYPES if type not in measurement_types] + ) + missing_types.extend([type for type in MARK_TYPES if type not in measurement_types]) + return missing_types diff --git a/geomagio/residual/Measurement.py b/geomagio/residual/Measurement.py index 8b0fe6b3c..e052f53bc 100644 --- a/geomagio/residual/Measurement.py +++ b/geomagio/residual/Measurement.py @@ -52,10 +52,6 @@ def average_measurement( """ if types: measurements = [m for m in measurements if m.measurement_type in types] - if len(measurements) == 0: - # no measurements to average - missing_types = ", ".join(t.value for t in types) - raise ValueError(f"Missing {missing_types} measurements") starttime = safe_min([m.time.timestamp for m in measurements if m.time]) endtime = safe_max([m.time.timestamp for m in measurements if m.time]) measurement = AverageMeasurement( diff --git a/geomagio/residual/__init__.py b/geomagio/residual/__init__.py index ec2de5e8a..352c66b32 100644 --- a/geomagio/residual/__init__.py +++ b/geomagio/residual/__init__.py @@ -9,6 +9,7 @@ from .Calculation import ( calculate_HZ_absolutes, calculate_I, calculate_scale_value, + get_missing_measurement_types, ) from .CalFileFactory import CalFileFactory from .Measurement import Measurement, AverageMeasurement, average_measurement @@ -35,6 +36,7 @@ __all__ = [ "calculate_I", "calculate_scale_value", "DECLINATION_TYPES", + "get_missing_measurement_types", "INCLINATION_TYPES", "MARK_TYPES", "Measurement", -- GitLab