From 23776b51f7dec4ab96115392d37ee7b047a31fe8 Mon Sep 17 00:00:00 2001 From: Tennessee Leeuwenburg Date: Wed, 30 Aug 2023 12:31:55 +1000 Subject: [PATCH] Test latitude weighting function with specific values --- src/scores/functions.py | 8 ++++++++ tests/scores/test_weights.py | 23 ++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/scores/functions.py b/src/scores/functions.py index 4cf5e006..f9f8f3ac 100644 --- a/src/scores/functions.py +++ b/src/scores/functions.py @@ -30,6 +30,14 @@ def create_latitude_weights(latitudes): which is contained in a particular region. This is approximated by the cosine of the latitude on an LLXY grid. Nuances not accounted for include the variation in latitude across the region, or the irregularity of the surface of the earth. + + Returns: + An xarray containing the weight values to be used for area approximation + + Args: + An xarray (or castable type) containing latitudes between +90 and -90 degrees + + Note - floating point behaviour can vary between systems, precisions and other factors """ weights = np.cos(np.deg2rad(latitudes)) return weights diff --git a/tests/scores/test_weights.py b/tests/scores/test_weights.py index 7642f730..b43ec2f5 100644 --- a/tests/scores/test_weights.py +++ b/tests/scores/test_weights.py @@ -61,7 +61,6 @@ def test_weights_latitude(): """ Tests the use of latitude weightings, not the correctness """ - # TODO: Write a correctness test for latitude weighting conversions to be specified by hand lat_weightings_values = scores.functions.create_latitude_weights(OBS_2D.latitude) @@ -70,6 +69,28 @@ def test_weights_latitude(): weighted = score(FCST_2D, OBS_2D, weights=lat_weightings_values) assert unweighted != weighted + # Latitudes in degrees, tested to 8 decimal places + latitude_tests = [ + (90, 0), + (89, 0.017452), + (45, 0.707107), + (22.5, 0.92388), + (0, 1), + (-22.5, 0.92388), + (-45, 0.707107), + (-89, 0.017452), + (-90, 0) + ] + latitudes, expected = zip(*latitude_tests) + latitudes = xr.DataArray(list(latitudes)) # Will not work from a tuple + expected = xr.DataArray(list(expected)) # Will not work from a tuple + + found = scores.functions.create_latitude_weights(latitudes) + decimal_places = 6 + found = found.round(decimal_places) + expected = expected.round(decimal_places) + assert found.equals(expected) + def test_weights_NaN_matching(): da = xr.DataArray