Skip to content

Commit 64cedf1

Browse files
ehoganabooton
authored andcommitted
PI-2472: Area weighted regridding (#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 3b4a00d commit 64cedf1

File tree

5 files changed

+401
-205
lines changed

5 files changed

+401
-205
lines changed
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

+36-21
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,37 @@ def __init__(self, src_grid_cube, target_grid_cube, mdtol=1):
4141
4242
.. Note::
4343
44-
Both sourge and target cubes must have an XY grid defined by
44+
Both source and target cubes must have an XY grid defined by
4545
separate X and Y dimensions with dimension coordinates.
4646
All of the XY dimension coordinates must also be bounded, and have
4747
the same cooordinate system.
4848
4949
"""
50-
# Snapshot the state of the cubes to ensure that the regridder is
51-
# impervious to external changes to the original source cubes.
50+
# Snapshot the state of the source cube to ensure that the regridder is
51+
# impervious to external changes to the original cubes.
5252
self._src_grid = snapshot_grid(src_grid_cube)
53-
self._target_grid = snapshot_grid(target_grid_cube)
53+
5454
# Missing data tolerance.
5555
if not (0 <= mdtol <= 1):
5656
msg = "Value for mdtol must be in range 0 - 1, got {}."
5757
raise ValueError(msg.format(mdtol))
5858
self._mdtol = mdtol
5959

60-
# The need for an actual Cube is an implementation quirk caused by the
61-
# current usage of the experimental regrid function.
62-
self._target_grid_cube_cache = None
63-
64-
@property
65-
def _target_grid_cube(self):
66-
if self._target_grid_cube_cache is None:
67-
x, y = self._target_grid
68-
data = np.empty((y.points.size, x.points.size))
69-
cube = iris.cube.Cube(data)
70-
cube.add_dim_coord(y, 0)
71-
cube.add_dim_coord(x, 1)
72-
self._target_grid_cube_cache = cube
73-
return self._target_grid_cube_cache
60+
# Store regridding information
61+
_regrid_info = eregrid._regrid_area_weighted_rectilinear_src_and_grid__prepare(
62+
src_grid_cube, target_grid_cube
63+
)
64+
(
65+
src_x,
66+
src_y,
67+
src_x_dim,
68+
src_y_dim,
69+
self.grid_x,
70+
self.grid_y,
71+
self.meshgrid_x,
72+
self.meshgrid_y,
73+
self.weights_info,
74+
) = _regrid_info
7475

7576
def __call__(self, cube):
7677
"""
@@ -92,11 +93,25 @@ def __call__(self, cube):
9293
area-weighted regridding.
9394
9495
"""
95-
if get_xy_dim_coords(cube) != self._src_grid:
96+
src_x, src_y = get_xy_dim_coords(cube)
97+
if (src_x, src_y) != self._src_grid:
9698
raise ValueError(
9799
"The given cube is not defined on the same "
98100
"source grid as this regridder."
99101
)
100-
return eregrid.regrid_area_weighted_rectilinear_src_and_grid(
101-
cube, self._target_grid_cube, mdtol=self._mdtol
102+
src_x_dim = cube.coord_dims(src_x)[0]
103+
src_y_dim = cube.coord_dims(src_y)[0]
104+
_regrid_info = (
105+
src_x,
106+
src_y,
107+
src_x_dim,
108+
src_y_dim,
109+
self.grid_x,
110+
self.grid_y,
111+
self.meshgrid_x,
112+
self.meshgrid_y,
113+
self.weights_info,
114+
)
115+
return eregrid._regrid_area_weighted_rectilinear_src_and_grid__perform(
116+
cube, _regrid_info, mdtol=self._mdtol
102117
)

lib/iris/analysis/_regrid.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -881,9 +881,9 @@ def _create_cube(
881881
def copy_coords(src_coords, add_method):
882882
for coord in src_coords:
883883
dims = src.coord_dims(coord)
884-
if coord is src_x_coord:
884+
if coord == src_x_coord:
885885
coord = grid_x_coord
886-
elif coord is src_y_coord:
886+
elif coord == src_y_coord:
887887
coord = grid_y_coord
888888
elif x_dim in dims or y_dim in dims:
889889
continue

0 commit comments

Comments
 (0)