Newer
Older
Cain, Payton David
committed
import json
Cain, Payton David
committed
from numpy.testing import assert_almost_equal, assert_equal
Cain, Payton David
committed
from pydantic import parse_obj_as
Cain, Payton David
committed
from typing import List
from obspy.core import UTCDateTime
from geomagio.residual import (
calculate,
Reading,
SpreadsheetAbsolutesFactory,
Cain, Payton David
committed
SpreadsheetSummaryFactory,
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}
[actual_absolutes["H"].absolute, actual_absolutes["H"].baseline],
[expected_absolutes["H"].absolute, expected_absolutes["H"].baseline],
verbose=True,
)
assert_almost_equal(
[actual_absolutes["D"].absolute, actual_absolutes["D"].baseline],
[expected_absolutes["D"].absolute, expected_absolutes["D"].baseline],
verbose=True,
)
assert_almost_equal(
[actual_absolutes["Z"].absolute, actual_absolutes["Z"].baseline],
[expected_absolutes["Z"].absolute, expected_absolutes["Z"].baseline],
if expected.scale_value is not None:
actual.scale_value, expected.scale_value, decimal=1, verbose=True
Cain, Payton David
committed
def get_json_readings(filename: str):
with open(filename, "r") as file:
readings = json.load(file)
readings = parse_obj_as(List[Reading], readings)
def get_spreadsheet_absolutes(path):
Tests functionality of SpreadsheetAbsolutesFactory and recalculation of absolutes
"""
# establish SpreadsheetAbsolutesFactory for reading test data from Excel
saf = SpreadsheetAbsolutesFactory()
# Read spreadsheet containing test data
reading = saf.parse_spreadsheet(path=path)
Cain, Payton David
committed
def get_spreadsheet_directory_readings(path, observatory, starttime, endtime):
ssf = SpreadsheetSummaryFactory(base_directory=path)
readings = ssf.get_readings(
observatory=observatory, starttime=starttime, endtime=endtime
)
return readings
def test_CMO_summaries():
starttime = UTCDateTime("2015-04-01")
endtime = UTCDateTime("2015-06-15")
readings = get_spreadsheet_directory_readings(
Cain, Payton David
committed
observatory="CMO",
starttime=starttime,
endtime=endtime,
)
for reading in readings:
assert_equal(reading.metadata["station"], "CMO")
Cain, Payton David
committed
assert_equal(reading.metadata["instrument"], 200803)
assert_equal(reading.pier_correction, 10.5)
Wernle, Alexandra Nicole
committed
assert_equal(len(readings), 28)
Cain, Payton David
committed
assert readings[0].time > starttime
assert readings[-1].time < endtime
"""
Compare calulations to original absolutes obejct from Spreadsheet.
Tests gathering of Dedhorse's metadata for use by calculations.
Tests calculations for measurements in units of DMS.
"""
# gather absolute from DED test data and recalculate
reading = get_spreadsheet_absolutes(path="etc/residual/DED-20140952332.xlsm")
assert_readings_equal(
expected=reading, actual=calculate(reading=reading), decimal=2
)
"""
Compare calulations to original absolutes obejct from Spreadsheet.
Tests gathering of BRW's metadata for use by calculations.
Tests calculations for measurements in units of DM.
"""
# gather absolute from DED test data and recalculate
reading = get_spreadsheet_absolutes(path="etc/residual/BRW-20133650000.xlsm")
assert_readings_equal(
expected=reading,
actual=calculate(reading=reading),
decimal=1, # change due to no longer rounding
)
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 DM.
Cain, Payton David
committed
readings = get_json_readings("etc/residual/BOU20190702.json")
for reading in readings:
assert_readings_equal(
expected=reading,
actual=calculate(reading=reading, adjust_reference=False),
decimal=1,
)
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 DMS.
Cain, Payton David
committed
readings = get_json_readings("etc/residual/BOU20200422.json")
for reading in readings:
assert_readings_equal(
expected=reading,
actual=calculate(reading=reading, adjust_reference=False),
decimal=0,
)