Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenworsley committed Oct 2, 2023
1 parent 085d1c6 commit 8c47217
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 4 deletions.
46 changes: 46 additions & 0 deletions esmf_regrid/tests/unit/schemes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Unit tests for `esmf_regrid.schemes`."""

from iris.coord_systems import TransverseMercator, GeogCS
import numpy as np
from numpy import ma
import pytest
Expand Down Expand Up @@ -166,3 +167,48 @@ def _test_mask_from_regridder(scheme, mask_keyword):
np.testing.assert_allclose(
getattr(rg_from_different, regridder_attr), mask_different
)


def _test_non_degree_crs(scheme, expected_sum, expected_unmasked):
"""Test regridding scheme is compatible with coordinates with non-degree units"""
coord_system = TransverseMercator(
49,
-2,
false_easting=400000,
false_northing=-100000,
scale_factor_at_central_meridian=0.9996012717,
ellipsoid=GeogCS(semi_major_axis=6377563.396, semi_minor_axis=6356256.91),
)

n_lons_src = 2
n_lats_src = 3
lon_bounds = (-197500, -192500)
lat_bounds = (1247500, 1237500)
tm_cube = _grid_cube(
n_lons_src,
n_lats_src,
lon_bounds,
lat_bounds,
circular=False,
coord_system=coord_system,
standard_names=["projection_x_coordinate", "projection_y_coordinate"],
units="m",
)
data = np.arange(n_lats_src * n_lons_src).reshape([n_lats_src, n_lons_src])
tm_cube.data = data

n_lons_tgt = 12
n_lats_tgt = 14
lon_bounds_tgt = (-13, -12.8)
lat_bounds_tgt = (60.5, 60.7)
cube_tgt = _grid_cube(
n_lons_tgt, n_lats_tgt, lon_bounds_tgt, lat_bounds_tgt, circular=True
)

result = tm_cube.regrid(cube_tgt, scheme())

# Check that the data is as expected.
assert result.data.sum() == expected_sum

# Check that the number of masked points is as expected.
assert (1 - result.data.mask).sum() == expected_unmasked
8 changes: 8 additions & 0 deletions esmf_regrid/tests/unit/schemes/test_ESMFAreaWeighted.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
_test_invalid_mdtol,
_test_mask_from_init,
_test_mask_from_regridder,
_test_non_degree_crs,
)


Expand Down Expand Up @@ -62,3 +63,10 @@ def test_invalid_tgt_location():
match = "For area weighted regridding, target location must be 'face'."
with pytest.raises(ValueError, match=match):
_ = ESMFAreaWeighted(tgt_location="node")


def test_non_degree_crs():
"""Test for coordinates with non-degree units"""
expected_sum = 50.86147272655136
expected_unmasked = 21
_test_non_degree_crs(ESMFAreaWeighted, expected_sum, expected_unmasked)
8 changes: 8 additions & 0 deletions esmf_regrid/tests/unit/schemes/test_ESMFBilinear.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
_test_invalid_mdtol,
_test_mask_from_init,
_test_mask_from_regridder,
_test_non_degree_crs,
)


Expand Down Expand Up @@ -51,3 +52,10 @@ def test_mask_from_regridder(mask_keyword):
Checks that use_src_mask and use_tgt_mask are passed down correctly.
"""
_test_mask_from_regridder(ESMFBilinear, mask_keyword)


def test_non_degree_crs():
"""Test for coordinates with non-degree units"""
expected_sum = 35.90837983047451
expected_unmasked = 13
_test_non_degree_crs(ESMFBilinear, expected_sum, expected_unmasked)
8 changes: 8 additions & 0 deletions esmf_regrid/tests/unit/schemes/test_ESMFNearest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from esmf_regrid.tests.unit.schemes.__init__ import (
_test_mask_from_init,
_test_mask_from_regridder,
_test_non_degree_crs,
)
from esmf_regrid.tests.unit.schemes.test__cube_to_GridInfo import _grid_cube
from esmf_regrid.tests.unit.schemes.test__mesh_to_MeshInfo import (
Expand Down Expand Up @@ -94,3 +95,10 @@ def test_mask_from_regridder(mask_keyword):
Checks that use_src_mask and use_tgt_mask are passed down correctly.
"""
_test_mask_from_regridder(ESMFNearest, mask_keyword)


def test_non_degree_crs():
"""Test for coordinates with non-degree units"""
expected_sum = 490
expected_unmasked = 168
_test_non_degree_crs(ESMFNearest, expected_sum, expected_unmasked)
10 changes: 6 additions & 4 deletions esmf_regrid/tests/unit/schemes/test__cube_to_GridInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,23 @@ def _grid_cube(
lat_outer_bounds,
circular=False,
coord_system=None,
standard_names=["longitude", "latitude"],
units="degrees",
):
lon_points, lon_bounds = _generate_points_and_bounds(n_lons, lon_outer_bounds)
lon = DimCoord(
lon_points,
"longitude",
units="degrees",
standard_names[0],
units=units,
bounds=lon_bounds,
circular=circular,
coord_system=coord_system,
)
lat_points, lat_bounds = _generate_points_and_bounds(n_lats, lat_outer_bounds)
lat = DimCoord(
lat_points,
"latitude",
units="degrees",
standard_names[1],
units=units,
bounds=lat_bounds,
coord_system=coord_system,
)
Expand Down

0 comments on commit 8c47217

Please sign in to comment.