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..5ab24ee89d 100644 --- a/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py +++ b/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py @@ -102,6 +102,68 @@ 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): + 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 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() + + 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 + 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) + 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.assertArrayEqual(result1.data, reference1.data) + self.assertArrayEqual(result2.data, reference2.data) + if __name__ == "__main__": tests.main()