From 7ad62ec2cca207ab6f45e7adbc84912542cd1e43 Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Tue, 17 Dec 2019 15:49:49 +0000 Subject: [PATCH 01/11] Refactor AreaWeightedRegridder --- lib/iris/analysis/_area_weighted.py | 20 +++++++++-- .../test_AreaWeightedRegridder.py | 35 +++++++++++-------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/lib/iris/analysis/_area_weighted.py b/lib/iris/analysis/_area_weighted.py index a7c0e41d4b..f32e2b7f28 100644 --- a/lib/iris/analysis/_area_weighted.py +++ b/lib/iris/analysis/_area_weighted.py @@ -59,8 +59,24 @@ def __init__(self, src_grid_cube, target_grid_cube, mdtol=1): # The need for an actual Cube is an implementation quirk caused by the # current usage of the experimental regrid function. + self._src_grid_cube_cache = None self._target_grid_cube_cache = None + self._regrid_info = eregrid._regrid_area_weighted_rectilinear_src_and_grid__prepare( + self._src_grid_cube, self._target_grid_cube + ) + + @property + def _src_grid_cube(self): + if self._src_grid_cube_cache is None: + x, y = self._src_grid + data = np.empty((y.points.size, x.points.size)) + cube = iris.cube.Cube(data) + cube.add_dim_coord(y, 0) + cube.add_dim_coord(x, 1) + self._src_grid_cube_cache = cube + return self._src_grid_cube_cache + @property def _target_grid_cube(self): if self._target_grid_cube_cache is None: @@ -97,6 +113,6 @@ def __call__(self, cube): "The given cube is not defined on the same " "source grid as this regridder." ) - return eregrid.regrid_area_weighted_rectilinear_src_and_grid( - cube, self._target_grid_cube, mdtol=self._mdtol + return eregrid._regrid_area_weighted_rectilinear_src_and_grid__perform( + cube, self._regrid_info, mdtol=self._mdtol ) 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 5ab24ee89d..3bfd1ff9b9 100644 --- a/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py +++ b/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py @@ -42,13 +42,20 @@ def extract_grid(self, cube): def check_mdtol(self, mdtol=None): src_grid, target_grid = self.grids() - if mdtol is None: - regridder = AreaWeightedRegridder(src_grid, target_grid) - mdtol = 1 - else: - regridder = AreaWeightedRegridder( - src_grid, target_grid, mdtol=mdtol - ) + + with mock.patch( + "iris.experimental.regrid." + "_regrid_area_weighted_rectilinear_src_and_grid__prepare", + return_value=mock.sentinel.result, + ) as prepare: + if mdtol is None: + regridder = AreaWeightedRegridder(src_grid, target_grid) + mdtol = 1 + else: + regridder = AreaWeightedRegridder( + src_grid, target_grid, mdtol=mdtol + ) + self.assertEqual(prepare.call_count, 1) # Make a new cube to regrid with different data so we can # distinguish between regridding the original src grid @@ -58,18 +65,18 @@ def check_mdtol(self, mdtol=None): with mock.patch( "iris.experimental.regrid." - "regrid_area_weighted_rectilinear_src_and_grid", + "_regrid_area_weighted_rectilinear_src_and_grid__perform", return_value=mock.sentinel.result, - ) as regrid: + ) as perform: result = regridder(src) - self.assertEqual(regrid.call_count, 1) - _, args, kwargs = regrid.mock_calls[0] + self.assertEqual(perform.call_count, 1) + _, args, kwargs = perform.mock_calls[0] self.assertEqual(args[0], src) - self.assertEqual( - self.extract_grid(args[1]), self.extract_grid(target_grid) - ) + # self.assertEqual( + # self.extract_grid(args[1]), self.extract_grid(target_grid) + # ) self.assertEqual(kwargs, {"mdtol": mdtol}) self.assertIs(result, mock.sentinel.result) From 7da87aef9e0f8febe5a7f7f571f4beb46624aa3f Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Tue, 17 Dec 2019 17:43:23 +0000 Subject: [PATCH 02/11] Cache the regrid info --- lib/iris/analysis/_area_weighted.py | 41 +++++-------------- .../test_AreaWeightedRegridder.py | 34 ++++++++------- 2 files changed, 27 insertions(+), 48 deletions(-) diff --git a/lib/iris/analysis/_area_weighted.py b/lib/iris/analysis/_area_weighted.py index f32e2b7f28..897414363e 100644 --- a/lib/iris/analysis/_area_weighted.py +++ b/lib/iris/analysis/_area_weighted.py @@ -51,42 +51,23 @@ def __init__(self, src_grid_cube, target_grid_cube, mdtol=1): # impervious to external changes to the original source cubes. self._src_grid = snapshot_grid(src_grid_cube) self._target_grid = snapshot_grid(target_grid_cube) + self._src_grid_cube = src_grid_cube.copy() + self._target_grid_cube = target_grid_cube.copy() # Missing data tolerance. if not (0 <= mdtol <= 1): msg = "Value for mdtol must be in range 0 - 1, got {}." raise ValueError(msg.format(mdtol)) self._mdtol = mdtol - # The need for an actual Cube is an implementation quirk caused by the - # current usage of the experimental regrid function. - self._src_grid_cube_cache = None - self._target_grid_cube_cache = None + # Cache the regrid info. + self._regrid_info_cache = None - self._regrid_info = eregrid._regrid_area_weighted_rectilinear_src_and_grid__prepare( - self._src_grid_cube, self._target_grid_cube - ) - - @property - def _src_grid_cube(self): - if self._src_grid_cube_cache is None: - x, y = self._src_grid - data = np.empty((y.points.size, x.points.size)) - cube = iris.cube.Cube(data) - cube.add_dim_coord(y, 0) - cube.add_dim_coord(x, 1) - self._src_grid_cube_cache = cube - return self._src_grid_cube_cache - - @property - def _target_grid_cube(self): - if self._target_grid_cube_cache is None: - x, y = self._target_grid - data = np.empty((y.points.size, x.points.size)) - cube = iris.cube.Cube(data) - cube.add_dim_coord(y, 0) - cube.add_dim_coord(x, 1) - self._target_grid_cube_cache = cube - return self._target_grid_cube_cache + def _regrid_info(self): + if self._regrid_info_cache is None: + self._regrid_info_cache = eregrid._regrid_area_weighted_rectilinear_src_and_grid__prepare( + self._src_grid_cube, self._target_grid_cube + ) + return self._regrid_info_cache def __call__(self, cube): """ @@ -114,5 +95,5 @@ def __call__(self, cube): "source grid as this regridder." ) return eregrid._regrid_area_weighted_rectilinear_src_and_grid__perform( - cube, self._regrid_info, mdtol=self._mdtol + cube, self._regrid_info(), mdtol=self._mdtol ) 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 3bfd1ff9b9..89311c9bc9 100644 --- a/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py +++ b/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py @@ -42,20 +42,13 @@ def extract_grid(self, cube): def check_mdtol(self, mdtol=None): src_grid, target_grid = self.grids() - - with mock.patch( - "iris.experimental.regrid." - "_regrid_area_weighted_rectilinear_src_and_grid__prepare", - return_value=mock.sentinel.result, - ) as prepare: - if mdtol is None: - regridder = AreaWeightedRegridder(src_grid, target_grid) - mdtol = 1 - else: - regridder = AreaWeightedRegridder( - src_grid, target_grid, mdtol=mdtol - ) - self.assertEqual(prepare.call_count, 1) + if mdtol is None: + regridder = AreaWeightedRegridder(src_grid, target_grid) + mdtol = 1 + else: + regridder = AreaWeightedRegridder( + src_grid, target_grid, mdtol=mdtol + ) # Make a new cube to regrid with different data so we can # distinguish between regridding the original src grid @@ -65,11 +58,16 @@ def check_mdtol(self, mdtol=None): with mock.patch( "iris.experimental.regrid." - "_regrid_area_weighted_rectilinear_src_and_grid__perform", - return_value=mock.sentinel.result, - ) as perform: - result = regridder(src) + "_regrid_area_weighted_rectilinear_src_and_grid__prepare" + ) as prepare: + with mock.patch( + "iris.experimental.regrid." + "_regrid_area_weighted_rectilinear_src_and_grid__perform", + return_value=mock.sentinel.result, + ) as perform: + result = regridder(src) + self.assertEqual(prepare.call_count, 1) self.assertEqual(perform.call_count, 1) _, args, kwargs = perform.mock_calls[0] From a9487e2da8caa12a7836bd387f08ac93a465cd1f Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Wed, 18 Dec 2019 09:33:54 +0000 Subject: [PATCH 03/11] Cache the src and target cubes --- lib/iris/analysis/_area_weighted.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/iris/analysis/_area_weighted.py b/lib/iris/analysis/_area_weighted.py index 897414363e..92bea061aa 100644 --- a/lib/iris/analysis/_area_weighted.py +++ b/lib/iris/analysis/_area_weighted.py @@ -51,8 +51,6 @@ def __init__(self, src_grid_cube, target_grid_cube, mdtol=1): # impervious to external changes to the original source cubes. self._src_grid = snapshot_grid(src_grid_cube) self._target_grid = snapshot_grid(target_grid_cube) - self._src_grid_cube = src_grid_cube.copy() - self._target_grid_cube = target_grid_cube.copy() # Missing data tolerance. if not (0 <= mdtol <= 1): msg = "Value for mdtol must be in range 0 - 1, got {}." @@ -62,6 +60,33 @@ def __init__(self, src_grid_cube, target_grid_cube, mdtol=1): # Cache the regrid info. self._regrid_info_cache = None + # The need for an actual Cube is an implementation quirk caused by the + # current usage of the experimental regrid function. + self._src_grid_cube_cache = None + self._target_grid_cube_cache = None + + @property + def _src_grid_cube(self): + if self._src_grid_cube_cache is None: + x, y = self._src_grid + data = np.empty((y.points.size, x.points.size)) + cube = iris.cube.Cube(data) + cube.add_dim_coord(y, 0) + cube.add_dim_coord(x, 1) + self._src_grid_cube_cache = cube + return self._src_grid_cube_cache + + @property + def _target_grid_cube(self): + if self._target_grid_cube_cache is None: + x, y = self._target_grid + data = np.empty((y.points.size, x.points.size)) + cube = iris.cube.Cube(data) + cube.add_dim_coord(y, 0) + cube.add_dim_coord(x, 1) + self._target_grid_cube_cache = cube + return self._target_grid_cube_cache + def _regrid_info(self): if self._regrid_info_cache is None: self._regrid_info_cache = eregrid._regrid_area_weighted_rectilinear_src_and_grid__prepare( From 797ff8de20bdeb682561931bdac5265f1d112d50 Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Thu, 19 Dec 2019 11:07:55 +0000 Subject: [PATCH 04/11] Use == rather than is when checking for equality --- lib/iris/analysis/_regrid.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/iris/analysis/_regrid.py b/lib/iris/analysis/_regrid.py index 19ed909fdc..0670c073ae 100644 --- a/lib/iris/analysis/_regrid.py +++ b/lib/iris/analysis/_regrid.py @@ -881,9 +881,9 @@ def _create_cube( def copy_coords(src_coords, add_method): for coord in src_coords: dims = src.coord_dims(coord) - if coord is src_x_coord: + if coord == src_x_coord: coord = grid_x_coord - elif coord is src_y_coord: + elif coord == src_y_coord: coord = grid_y_coord elif x_dim in dims or y_dim in dims: continue From df3c864a348452d647307261dbe4103c8fa9cf8a Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Thu, 19 Dec 2019 11:42:58 +0000 Subject: [PATCH 05/11] Resolve mock.sentinel unit test issue --- .../area_weighted/test_AreaWeightedRegridder.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 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 89311c9bc9..34074ea822 100644 --- a/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py +++ b/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py @@ -67,14 +67,17 @@ def check_mdtol(self, mdtol=None): ) as perform: result = regridder(src) + # Prepare: self.assertEqual(prepare.call_count, 1) + _, args, kwargs = prepare.mock_calls[0] + self.assertEqual( + self.extract_grid(args[1]), self.extract_grid(target_grid) + ) + + # Perform: self.assertEqual(perform.call_count, 1) _, args, kwargs = perform.mock_calls[0] - self.assertEqual(args[0], src) - # self.assertEqual( - # self.extract_grid(args[1]), self.extract_grid(target_grid) - # ) self.assertEqual(kwargs, {"mdtol": mdtol}) self.assertIs(result, mock.sentinel.result) From a7e5b889b692c6dc63d1c3131ea39df3f0eb3ea3 Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Thu, 19 Dec 2019 13:06:38 +0000 Subject: [PATCH 06/11] Don't cache the source cube --- lib/iris/analysis/_area_weighted.py | 22 ++------------ lib/iris/analysis/_regrid.py | 4 +-- .../test_AreaWeightedRegridder.py | 30 +++++++++---------- 3 files changed, 20 insertions(+), 36 deletions(-) diff --git a/lib/iris/analysis/_area_weighted.py b/lib/iris/analysis/_area_weighted.py index 92bea061aa..458fb544da 100644 --- a/lib/iris/analysis/_area_weighted.py +++ b/lib/iris/analysis/_area_weighted.py @@ -62,19 +62,10 @@ def __init__(self, src_grid_cube, target_grid_cube, mdtol=1): # The need for an actual Cube is an implementation quirk caused by the # current usage of the experimental regrid function. - self._src_grid_cube_cache = None self._target_grid_cube_cache = None - @property - def _src_grid_cube(self): - if self._src_grid_cube_cache is None: - x, y = self._src_grid - data = np.empty((y.points.size, x.points.size)) - cube = iris.cube.Cube(data) - cube.add_dim_coord(y, 0) - cube.add_dim_coord(x, 1) - self._src_grid_cube_cache = cube - return self._src_grid_cube_cache + self._regrid_info = eregrid._regrid_area_weighted_rectilinear_src_and_grid__prepare( + src_grid_cube, self._target_grid_cube) @property def _target_grid_cube(self): @@ -87,13 +78,6 @@ def _target_grid_cube(self): self._target_grid_cube_cache = cube return self._target_grid_cube_cache - def _regrid_info(self): - if self._regrid_info_cache is None: - self._regrid_info_cache = eregrid._regrid_area_weighted_rectilinear_src_and_grid__prepare( - self._src_grid_cube, self._target_grid_cube - ) - return self._regrid_info_cache - def __call__(self, cube): """ Regrid this :class:`~iris.cube.Cube` onto the target grid of @@ -120,5 +104,5 @@ def __call__(self, cube): "source grid as this regridder." ) return eregrid._regrid_area_weighted_rectilinear_src_and_grid__perform( - cube, self._regrid_info(), mdtol=self._mdtol + cube, self._regrid_info, mdtol=self._mdtol ) diff --git a/lib/iris/analysis/_regrid.py b/lib/iris/analysis/_regrid.py index 0670c073ae..19ed909fdc 100644 --- a/lib/iris/analysis/_regrid.py +++ b/lib/iris/analysis/_regrid.py @@ -881,9 +881,9 @@ def _create_cube( def copy_coords(src_coords, add_method): for coord in src_coords: dims = src.coord_dims(coord) - if coord == src_x_coord: + if coord is src_x_coord: coord = grid_x_coord - elif coord == src_y_coord: + elif coord is src_y_coord: coord = grid_y_coord elif x_dim in dims or y_dim in dims: continue 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 34074ea822..f7a3a66b21 100644 --- a/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py +++ b/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py @@ -42,13 +42,17 @@ def extract_grid(self, cube): def check_mdtol(self, mdtol=None): src_grid, target_grid = self.grids() - if mdtol is None: - regridder = AreaWeightedRegridder(src_grid, target_grid) - mdtol = 1 - else: - regridder = AreaWeightedRegridder( - src_grid, target_grid, mdtol=mdtol - ) + with mock.patch( + "iris.experimental.regrid." + "_regrid_area_weighted_rectilinear_src_and_grid__prepare" + ) as prepare: + if mdtol is None: + regridder = AreaWeightedRegridder(src_grid, target_grid) + mdtol = 1 + else: + regridder = AreaWeightedRegridder( + src_grid, target_grid, mdtol=mdtol + ) # Make a new cube to regrid with different data so we can # distinguish between regridding the original src grid @@ -58,14 +62,10 @@ def check_mdtol(self, mdtol=None): with mock.patch( "iris.experimental.regrid." - "_regrid_area_weighted_rectilinear_src_and_grid__prepare" - ) as prepare: - with mock.patch( - "iris.experimental.regrid." - "_regrid_area_weighted_rectilinear_src_and_grid__perform", - return_value=mock.sentinel.result, - ) as perform: - result = regridder(src) + "_regrid_area_weighted_rectilinear_src_and_grid__perform", + return_value=mock.sentinel.result, + ) as perform: + result = regridder(src) # Prepare: self.assertEqual(prepare.call_count, 1) From ea40fad8f6c5bd8bf0cfdff34d7bd7ca876ffbf9 Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Thu, 19 Dec 2019 13:08:13 +0000 Subject: [PATCH 07/11] Fix stickler issue --- lib/iris/analysis/_area_weighted.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/iris/analysis/_area_weighted.py b/lib/iris/analysis/_area_weighted.py index 458fb544da..6ac64413fc 100644 --- a/lib/iris/analysis/_area_weighted.py +++ b/lib/iris/analysis/_area_weighted.py @@ -65,7 +65,8 @@ def __init__(self, src_grid_cube, target_grid_cube, mdtol=1): self._target_grid_cube_cache = None self._regrid_info = eregrid._regrid_area_weighted_rectilinear_src_and_grid__prepare( - src_grid_cube, self._target_grid_cube) + src_grid_cube, self._target_grid_cube + ) @property def _target_grid_cube(self): From f2f3503dbf02068d56dbdfe0ee1212c77a63e205 Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Thu, 19 Dec 2019 13:12:09 +0000 Subject: [PATCH 08/11] Remove unnecessary code --- lib/iris/analysis/_area_weighted.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/iris/analysis/_area_weighted.py b/lib/iris/analysis/_area_weighted.py index 6ac64413fc..9cdf2e8bdc 100644 --- a/lib/iris/analysis/_area_weighted.py +++ b/lib/iris/analysis/_area_weighted.py @@ -57,9 +57,6 @@ def __init__(self, src_grid_cube, target_grid_cube, mdtol=1): raise ValueError(msg.format(mdtol)) self._mdtol = mdtol - # Cache the regrid info. - self._regrid_info_cache = None - # The need for an actual Cube is an implementation quirk caused by the # current usage of the experimental regrid function. self._target_grid_cube_cache = None From 27a4addcdde174fa74285bccb957843847cb5982 Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Thu, 19 Dec 2019 16:20:48 +0000 Subject: [PATCH 09/11] Ensure the data dims of the x and y coordinates of the source cube to be regridded are equal to the data dims of the x and y coordinates of the source cube used to set up the regridder --- lib/iris/analysis/_area_weighted.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/iris/analysis/_area_weighted.py b/lib/iris/analysis/_area_weighted.py index 9cdf2e8bdc..a6ab8586cb 100644 --- a/lib/iris/analysis/_area_weighted.py +++ b/lib/iris/analysis/_area_weighted.py @@ -41,16 +41,22 @@ def __init__(self, src_grid_cube, target_grid_cube, mdtol=1): .. Note:: - Both sourge and target cubes must have an XY grid defined by + Both source and target cubes must have an XY grid defined by separate X and Y dimensions with dimension coordinates. All of the XY dimension coordinates must also be bounded, and have the same cooordinate system. """ # Snapshot the state of the cubes to ensure that the regridder is - # impervious to external changes to the original source cubes. + # impervious to external changes to the original cubes. self._src_grid = snapshot_grid(src_grid_cube) self._target_grid = snapshot_grid(target_grid_cube) + + # Store the x_dim and y_dim of the source cube. + x, y = get_xy_dim_coords(src_grid_cube) + self._src_x_dim = src_grid_cube.coord_dims(x) + self._src_y_dim = src_grid_cube.coord_dims(y) + # Missing data tolerance. if not (0 <= mdtol <= 1): msg = "Value for mdtol must be in range 0 - 1, got {}." @@ -96,7 +102,9 @@ def __call__(self, cube): area-weighted regridding. """ - if get_xy_dim_coords(cube) != self._src_grid: + if get_xy_dim_coords(cube) != self._src_grid or not ( + _xy_data_dims_are_equal(cube, self._src_x_dim, self._src_y_dim) + ): raise ValueError( "The given cube is not defined on the same " "source grid as this regridder." @@ -104,3 +112,12 @@ def __call__(self, cube): return eregrid._regrid_area_weighted_rectilinear_src_and_grid__perform( cube, self._regrid_info, mdtol=self._mdtol ) + + +def _xy_data_dims_are_equal(cube, x_dim, y_dim): + """ + Return whether the data dimensions of the x and y coordinates on the + the cube are equal to the values ``x_dim`` and ``y_dim``, respectively. + """ + x1, y1 = get_xy_dim_coords(cube) + return cube.coord_dims(x1) == x_dim and cube.coord_dims(y1) == y_dim From ec9045f2c131a4f2248c2c8f0dbd5610faa86976 Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Thu, 19 Dec 2019 16:31:06 +0000 Subject: [PATCH 10/11] Added unit test for previous commit --- .../test_AreaWeightedRegridder.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) 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 f7a3a66b21..31c0148976 100644 --- a/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py +++ b/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py @@ -172,6 +172,29 @@ def test_multiple_src_on_same_grid(self): self.assertArrayEqual(result1.data, reference1.data) self.assertArrayEqual(result2.data, reference2.data) + def test_mismatched_data_dims(self): + coord_names = ["latitude", "longitude"] + src1 = self.cube(np.linspace(20, 32, 4), np.linspace(10, 22, 4)) + src2 = self.cube(np.linspace(10, 22, 4), np.linspace(20, 32, 4)) + for name in coord_names: + # 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: + # 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) + self.assertRaises(ValueError, regridder, src2) + if __name__ == "__main__": tests.main() From 3c7a0931b877bf3980865f6a9ba6dce1e763c945 Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Thu, 19 Dec 2019 17:30:55 +0000 Subject: [PATCH 11/11] Actually test the correct thing :/ --- .../area_weighted/test_AreaWeightedRegridder.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 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 31c0148976..b4dc426c6d 100644 --- a/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py +++ b/lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py @@ -174,8 +174,17 @@ def test_multiple_src_on_same_grid(self): def test_mismatched_data_dims(self): coord_names = ["latitude", "longitude"] - src1 = self.cube(np.linspace(20, 32, 4), np.linspace(10, 22, 4)) - src2 = self.cube(np.linspace(10, 22, 4), np.linspace(20, 32, 4)) + x = np.linspace(20, 32, 4) + y = np.linspace(10, 22, 4) + src1 = self.cube(x, y) + + data = np.arange(len(y) * len(x)).reshape(len(x), len(y)) + src2 = Cube(data) + lat = DimCoord(y, "latitude", units="degrees") + lon = DimCoord(x, "longitude", units="degrees") + # Add dim coords in opposite order to self.cube. + src2.add_dim_coord(lat, 1) + src2.add_dim_coord(lon, 0) for name in coord_names: # Ensure contiguous bounds exists. src1.coord(name).guess_bounds()