Skip to content

Commit

Permalink
Test latitude weighting function with specific values
Browse files Browse the repository at this point in the history
  • Loading branch information
tennlee committed Aug 30, 2023
1 parent 9b42f4f commit 23776b5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/scores/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 22 additions & 1 deletion tests/scores/test_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down

0 comments on commit 23776b5

Please sign in to comment.