diff --git a/geomagio/residual/Calculation.py b/geomagio/residual/Calculation.py index 9bfb31a5a71e4ae55a757fcfe339223bec8c5cf3..a8f38cdafdc70e85117a1c2d026314761164f2aa 100644 --- a/geomagio/residual/Calculation.py +++ b/geomagio/residual/Calculation.py @@ -39,8 +39,8 @@ def calculate(reading: Reading, adjust_reference: bool = True) -> Reading: absoluteH, absoluteZ = calculate_HZ_absolutes( corrected_f=corrected_f, inclination=inclination, - reference=adjust_reference and reference or None, mean=mean, + reference=adjust_reference and reference or None, ) absoluteD = calculate_D_absolute( azimuth=reading.azimuth, @@ -49,14 +49,14 @@ def calculate(reading: Reading, adjust_reference: bool = True) -> Reading: reference=adjust_reference and reference or mean, ) # calculate scale - if reading[mt.NORTH_DOWN_SCALE]: + scale_value = None + scale_measurements = reading[mt.NORTH_DOWN_SCALE] + if scale_measurements: scale_value = calculate_scale_value( corrected_f=corrected_f, inclination=inclination, - measurements=reading[mt.NORTH_DOWN_SCALE], + measurements=scale_measurements, ) - else: - scale_value = None # create new reading object calculated = Reading( absolutes=[absoluteD, absoluteH, absoluteZ], @@ -86,6 +86,7 @@ def calculate_D_absolute( ------- D Absolute """ + mean = average_measurement(measurements, DECLINATION_TYPES) # average mark average_mark = average_measurement(measurements, MARK_TYPES).angle # adjust based on which is larger @@ -111,16 +112,22 @@ def calculate_D_absolute( for m in declination_measurements ] ) - shift = 0.0 + shift = 0 if azimuth > 180: - azimuth -= 180 shift = -180 # add subtract average mark angle from average meridian angle and add # azimuth to get the declination baseline - d_b = (meridian - average_mark) + azimuth + d_b = (meridian - average_mark) + azimuth + shift # calculate absolute d_abs = d_b + np.degrees(np.arctan(reference.e / (reference.h + h_baseline))) - return Absolute(element="D", absolute=d_abs, baseline=d_b, shift=shift) + return Absolute( + element="D", + absolute=d_abs, + baseline=d_b, + shift=shift, + starttime=mean.time, + endtime=mean.endtime, + ) def calculate_HZ_absolutes( diff --git a/test/residual_test/residual_test.py b/test/residual_test/residual_test.py index d5be09d8b0489f0484e1aece962f315f76069ce9..5679f2ba990f2b79e262a576e2cc85449be8c43c 100644 --- a/test/residual_test/residual_test.py +++ b/test/residual_test/residual_test.py @@ -10,56 +10,58 @@ from geomagio.residual import ( ) -def assert_readings_equal(expected: Reading, actual: Reading, decimal: float): +def assert_readings_equal(expected: Reading, actual: Reading, decimal: int): """ Compares calculation actuals to expected absolutes from spreadsheet """ + print(expected.json(exclude={"measurements", "metadata"}, indent=2)) + print(actual.json(exclude={"measurements", "metadata"}, indent=2)) expected_absolutes = {a.element: a for a in expected.absolutes} actual_absolutes = {a.element: a for a in actual.absolutes} assert_almost_equal( - [expected_absolutes["H"].absolute, expected_absolutes["H"].baseline], [actual_absolutes["H"].absolute, actual_absolutes["H"].baseline], + [expected_absolutes["H"].absolute, expected_absolutes["H"].baseline], decimal=decimal, verbose=True, ) assert_almost_equal( - [expected_absolutes["D"].absolute, expected_absolutes["D"].baseline], [actual_absolutes["D"].absolute, actual_absolutes["D"].baseline], + [expected_absolutes["D"].absolute, expected_absolutes["D"].baseline], decimal=decimal, verbose=True, ) assert_almost_equal( - [expected_absolutes["Z"].absolute, expected_absolutes["Z"].baseline], [actual_absolutes["Z"].absolute, actual_absolutes["Z"].baseline], + [expected_absolutes["Z"].absolute, expected_absolutes["Z"].baseline], decimal=decimal, verbose=True, ) - if expected.scale_value: + if expected.scale_value is not None: assert_almost_equal( - expected.scale_value, actual.scale_value, decimal=1, verbose=True + actual.scale_value, expected.scale_value, decimal=1, verbose=True ) -def compare_spreadsheet_absolutes(path): +def get_null_absolutes(observatory, starttime, endtime): """ - Tests functionality of SpreadsheetAbsolutesFactory and recalculation of absolutes + Tests functionality of WebAbsolutesFactory and recalculation of absolutes """ # establish SpreadsheetAbsolutesFactory for reading test data from Excel - saf = SpreadsheetAbsolutesFactory() + waf = WebAbsolutesFactory() # Read spreadsheet containing test data - reading = saf.parse_spreadsheet(path=path) - return reading + readings = waf.get_readings(observatory, starttime, endtime) + return readings -def compare_null_absolutes(observatory, starttime, endtime): +def get_spreadsheet_absolutes(path): """ - Tests functionality of WebAbsolutesFactory and recalculation of absolutes + Tests functionality of SpreadsheetAbsolutesFactory and recalculation of absolutes """ # establish SpreadsheetAbsolutesFactory for reading test data from Excel - waf = WebAbsolutesFactory() + saf = SpreadsheetAbsolutesFactory() # Read spreadsheet containing test data - reading = waf.get_readings(observatory, starttime, endtime)[0] + reading = saf.parse_spreadsheet(path=path) return reading @@ -70,9 +72,11 @@ def test_DED_20140952332(): Tests calculations for measurements in units of DMS. """ # gather absolute from DED test data and recalculate - reading = compare_spreadsheet_absolutes(path="etc/residual/DED-20140952332.xlsm") + reading = get_spreadsheet_absolutes(path="etc/residual/DED-20140952332.xlsm") # test results with original spreadsheet values - assert_readings_equal(reading, calculate(reading), 1) + assert_readings_equal( + expected=reading, actual=calculate(reading=reading), decimal=2 + ) def test_BRW_20133650000(): @@ -82,36 +86,48 @@ def test_BRW_20133650000(): Tests calculations for measurements in units of DM. """ # gather absolute from DED test data and recalculate - reading = compare_spreadsheet_absolutes(path="etc/residual/BRW-20133650000.xlsm") + reading = get_spreadsheet_absolutes(path="etc/residual/BRW-20133650000.xlsm") # test results with original spreadsheet values - assert_readings_equal(reading, calculate(reading), 1) + assert_readings_equal( + expected=reading, + actual=calculate(reading=reading), + decimal=1, # change due to no longer rounding + ) -def test_BOU_20200422(): +def test_BOU_20190702(): """ Compare calulations to original absolutes obejct from web absolutes. Tests gathering of BOU's metadata for use by calculations. - Tests calculations for null method measurements in units of DMS. + Tests calculations for null method measurements in units of DM. """ - reading = compare_null_absolutes( + readings = get_null_absolutes( observatory="BOU", - starttime=UTCDateTime("2020-04-22T00:00:00Z"), - endtime=UTCDateTime("2020-04-23T00:00:00Z"), + starttime=UTCDateTime("2019-07-02T00:00:00Z"), + endtime=UTCDateTime("2019-07-03T00:00:00Z"), ) - - assert_readings_equal(reading, calculate(reading, False), 0.0) + for reading in readings: + assert_readings_equal( + expected=reading, + actual=calculate(reading=reading, adjust_reference=False), + decimal=1, + ) -def test_BOU_20190702(): +def test_BOU_20200422(): """ Compare calulations to original absolutes obejct from web absolutes. Tests gathering of BOU's metadata for use by calculations. - Tests calculations for null method measurements in units of DM. + Tests calculations for null method measurements in units of DMS. """ - reading = compare_null_absolutes( + readings = get_null_absolutes( observatory="BOU", - starttime=UTCDateTime("2019-07-02T00:00:00Z"), - endtime=UTCDateTime("2019-07-03T00:00:00Z"), + starttime=UTCDateTime("2020-04-22T00:00:00Z"), + endtime=UTCDateTime("2020-04-23T00:00:00Z"), ) - - assert_readings_equal(reading, calculate(reading, False), 0.0) + for reading in readings: + assert_readings_equal( + expected=reading, + actual=calculate(reading=reading, adjust_reference=False), + decimal=0, + )