Skip to content

Commit 37d207e

Browse files
ehoganpp-mo
authored andcommitted
PI-2472: Area weighted regridding (SciTools#3623)
* The Area-weights routine is refactored into the "__prepare" and "__perform" structure, in-line with our other regridding methods. * The area-weights are now computed in the "__prepare", so are calculated once. * The information required for checking the regridding weights and target grid info are now cached on the "regridder" object. * This is inline with the general use already described in the documentation.
1 parent d98c824 commit 37d207e

File tree

5 files changed

+418
-198
lines changed

5 files changed

+418
-198
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* The area weights used when performing area weighted regridding are now
2+
cached.

lib/iris/analysis/_area_weighted.py

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,36 +55,37 @@ def __init__(self, src_grid_cube, target_grid_cube, mdtol=1):
5555
5656
.. Note::
5757
58-
Both sourge and target cubes must have an XY grid defined by
58+
Both source and target cubes must have an XY grid defined by
5959
separate X and Y dimensions with dimension coordinates.
6060
All of the XY dimension coordinates must also be bounded, and have
6161
the same cooordinate system.
6262
6363
"""
64-
# Snapshot the state of the cubes to ensure that the regridder is
65-
# impervious to external changes to the original source cubes.
64+
# Snapshot the state of the source cube to ensure that the regridder is
65+
# impervious to external changes to the original cubes.
6666
self._src_grid = snapshot_grid(src_grid_cube)
67-
self._target_grid = snapshot_grid(target_grid_cube)
67+
6868
# Missing data tolerance.
6969
if not (0 <= mdtol <= 1):
7070
msg = 'Value for mdtol must be in range 0 - 1, got {}.'
7171
raise ValueError(msg.format(mdtol))
7272
self._mdtol = mdtol
7373

74-
# The need for an actual Cube is an implementation quirk caused by the
75-
# current usage of the experimental regrid function.
76-
self._target_grid_cube_cache = None
77-
78-
@property
79-
def _target_grid_cube(self):
80-
if self._target_grid_cube_cache is None:
81-
x, y = self._target_grid
82-
data = np.empty((y.points.size, x.points.size))
83-
cube = iris.cube.Cube(data)
84-
cube.add_dim_coord(y, 0)
85-
cube.add_dim_coord(x, 1)
86-
self._target_grid_cube_cache = cube
87-
return self._target_grid_cube_cache
74+
# Store regridding information
75+
_regrid_info = eregrid._regrid_area_weighted_rectilinear_src_and_grid__prepare(
76+
src_grid_cube, target_grid_cube
77+
)
78+
(
79+
src_x,
80+
src_y,
81+
src_x_dim,
82+
src_y_dim,
83+
self.grid_x,
84+
self.grid_y,
85+
self.meshgrid_x,
86+
self.meshgrid_y,
87+
self.weights_info,
88+
) = _regrid_info
8889

8990
def __call__(self, cube):
9091
"""
@@ -106,8 +107,25 @@ def __call__(self, cube):
106107
area-weighted regridding.
107108
108109
"""
109-
if get_xy_dim_coords(cube) != self._src_grid:
110-
raise ValueError('The given cube is not defined on the same '
111-
'source grid as this regridder.')
112-
return eregrid.regrid_area_weighted_rectilinear_src_and_grid(
113-
cube, self._target_grid_cube, mdtol=self._mdtol)
110+
src_x, src_y = get_xy_dim_coords(cube)
111+
if (src_x, src_y) != self._src_grid:
112+
raise ValueError(
113+
"The given cube is not defined on the same "
114+
"source grid as this regridder."
115+
)
116+
src_x_dim = cube.coord_dims(src_x)[0]
117+
src_y_dim = cube.coord_dims(src_y)[0]
118+
_regrid_info = (
119+
src_x,
120+
src_y,
121+
src_x_dim,
122+
src_y_dim,
123+
self.grid_x,
124+
self.grid_y,
125+
self.meshgrid_x,
126+
self.meshgrid_y,
127+
self.weights_info,
128+
)
129+
return eregrid._regrid_area_weighted_rectilinear_src_and_grid__perform(
130+
cube, _regrid_info, mdtol=self._mdtol
131+
)

lib/iris/analysis/_regrid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -829,9 +829,9 @@ def _create_cube(data, src, x_dim, y_dim, src_x_coord, src_y_coord,
829829
def copy_coords(src_coords, add_method):
830830
for coord in src_coords:
831831
dims = src.coord_dims(coord)
832-
if coord is src_x_coord:
832+
if coord == src_x_coord:
833833
coord = grid_x_coord
834-
elif coord is src_y_coord:
834+
elif coord == src_y_coord:
835835
coord = grid_y_coord
836836
elif x_dim in dims or y_dim in dims:
837837
continue

0 commit comments

Comments
 (0)