From 52e648776e1ae0039119c088e49c64b54da20dcf Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Tue, 17 Dec 2019 12:20:46 +0000 Subject: [PATCH 1/4] Add tests for AreaWeightedRegridder --- .../test_AreaWeightedRegridder.py | 61 ++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py b/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py index a335044133..b42e691701 100644 --- a/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py +++ b/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py @@ -15,7 +15,6 @@ from unittest import mock import numpy as np - from iris.analysis._area_weighted import AreaWeightedRegridder from iris.coord_systems import GeogCS from iris.coords import DimCoord @@ -102,6 +101,66 @@ def test_mismatched_src_coord_systems(self): with self.assertRaises(ValueError): AreaWeightedRegridder(src, target) + def test_src_and_target_are_the_same(self): + src = self.cube(np.linspace(20, 30, 3), np.linspace(10, 25, 4)) + target = self.cube(np.linspace(20, 30, 3), np.linspace(10, 25, 4)) + for name in ["latitude", "longitude"]: + src.coord(name).guess_bounds() + target.coord(name).guess_bounds() + regridder = AreaWeightedRegridder(src, target) + result = regridder(src) + self.assertArrayAllClose(result.data, target.data) + + def test_multiple_src_on_same_grid(self): + src1 = self.cube(np.linspace(20, 32, 4), np.linspace(10, 22, 4)) + src2 = self.cube(np.linspace(20, 32, 4), np.linspace(10, 22, 4)) + src2.data *= 4 + self.assertArrayEqual(src1.data * 4, src2.data) + for name in ["latitude", "longitude"]: + src1.coord(name).guess_bounds() + src2.coord(name).guess_bounds() + + # Ensure the bounds of the target cover the same range as the source. + target = self.cube(np.linspace(20, 32, 2), np.linspace(10, 22, 2)) + target.coord("latitude").bounds = np.column_stack( + ( + src1.coord("latitude").bounds[[0, 1], [0, 1]], + src1.coord("latitude").bounds[[2, 3], [0, 1]], + ) + ) + target.coord("longitude").bounds = np.column_stack( + ( + src1.coord("longitude").bounds[[0, 1], [0, 1]], + src1.coord("longitude").bounds[[2, 3], [0, 1]], + ) + ) + + regridder = AreaWeightedRegridder(src1, target) + result1 = regridder(src1) + result2 = regridder(src2) + + reference1 = self.cube(np.linspace(20, 32, 2), np.linspace(10, 22, 2)) + reference1.data = np.array( + [ + [np.mean(src1.data[0:2, 0:2]), np.mean(src1.data[0:2, 2:4])], + [np.mean(src1.data[2:4, 0:2]), np.mean(src1.data[2:4, 2:4])], + ] + ) + reference2 = self.cube(np.linspace(20, 32, 2), np.linspace(10, 22, 2)) + reference2.data = np.array( + [ + [np.mean(src2.data[0:2, 0:2]), np.mean(src2.data[0:2, 2:4])], + [np.mean(src2.data[2:4, 0:2]), np.mean(src2.data[2:4, 2:4])], + ] + ) + + self.assertArrayAllClose( + result1.data, reference1.data, atol=2e-2, rtol=2e-3 + ) + self.assertArrayAllClose( + result2.data, reference2.data, atol=1e-1, rtol=2e-3 + ) + if __name__ == "__main__": tests.main() From ca6791c29e801b05597dd792e831993b054ef432 Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Tue, 17 Dec 2019 12:23:41 +0000 Subject: [PATCH 2/4] Restore new line --- .../unit/analysis/area_weighted/test_AreaWeightedRegridder.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py b/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py index b42e691701..45d95b7fb6 100644 --- a/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py +++ b/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py @@ -15,6 +15,7 @@ from unittest import mock import numpy as np + from iris.analysis._area_weighted import AreaWeightedRegridder from iris.coord_systems import GeogCS from iris.coords import DimCoord From 8d9c04e2628627a0665bbb795604aba234ac41a8 Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Tue, 17 Dec 2019 14:34:42 +0000 Subject: [PATCH 3/4] Remove coord system and units so it is no longer spherical --- .../test_AreaWeightedRegridder.py | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py b/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py index 45d95b7fb6..5e6d6a4e35 100644 --- a/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py +++ b/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py @@ -113,28 +113,34 @@ def test_src_and_target_are_the_same(self): self.assertArrayAllClose(result.data, target.data) def test_multiple_src_on_same_grid(self): + coord_names = ["latitude", "longitude"] src1 = self.cube(np.linspace(20, 32, 4), np.linspace(10, 22, 4)) src2 = self.cube(np.linspace(20, 32, 4), np.linspace(10, 22, 4)) src2.data *= 4 self.assertArrayEqual(src1.data * 4, src2.data) - for name in ["latitude", "longitude"]: + for name in coord_names: + # Remove coords system and units so it is no longer spherical. + src1.coord(name).coord_system = None + src1.coord(name).units = None + src2.coord(name).coord_system = None + src2.coord(name).units = None + # Ensure contiguous bounds exists. src1.coord(name).guess_bounds() src2.coord(name).guess_bounds() - # Ensure the bounds of the target cover the same range as the source. - target = self.cube(np.linspace(20, 32, 2), np.linspace(10, 22, 2)) - target.coord("latitude").bounds = np.column_stack( - ( - src1.coord("latitude").bounds[[0, 1], [0, 1]], - src1.coord("latitude").bounds[[2, 3], [0, 1]], - ) - ) - target.coord("longitude").bounds = np.column_stack( - ( - src1.coord("longitude").bounds[[0, 1], [0, 1]], - src1.coord("longitude").bounds[[2, 3], [0, 1]], + target = self.cube(np.linspace(20, 32, 2), np.linspace(-6, 6, 2)) + for name in coord_names: + # Remove coords system and units so it is no longer spherical. + target.coord(name).coord_system = None + target.coord(name).units = None + # Ensure the bounds of the target cover the same range as the + # source. + target.coord(name).bounds = np.column_stack( + ( + src1.coord(name).bounds[[0, 1], [0, 1]], + src1.coord(name).bounds[[2, 3], [0, 1]], + ) ) - ) regridder = AreaWeightedRegridder(src1, target) result1 = regridder(src1) @@ -155,12 +161,8 @@ def test_multiple_src_on_same_grid(self): ] ) - self.assertArrayAllClose( - result1.data, reference1.data, atol=2e-2, rtol=2e-3 - ) - self.assertArrayAllClose( - result2.data, reference2.data, atol=1e-1, rtol=2e-3 - ) + self.assertArrayEqual(result1.data, reference1.data) + self.assertArrayEqual(result2.data, reference2.data) if __name__ == "__main__": From 16e364d7ee43ed0f61c0347ca3e8931e4e04e98b Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Tue, 17 Dec 2019 14:45:04 +0000 Subject: [PATCH 4/4] Restore original latitude values (the tests didn't break because only the data are checked) --- .../unit/analysis/area_weighted/test_AreaWeightedRegridder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py b/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py index 5e6d6a4e35..5ab24ee89d 100644 --- a/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py +++ b/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py @@ -128,7 +128,7 @@ def test_multiple_src_on_same_grid(self): src1.coord(name).guess_bounds() src2.coord(name).guess_bounds() - target = self.cube(np.linspace(20, 32, 2), np.linspace(-6, 6, 2)) + target = self.cube(np.linspace(20, 32, 2), np.linspace(10, 22, 2)) for name in coord_names: # Remove coords system and units so it is no longer spherical. target.coord(name).coord_system = None