Skip to content

Commit 2d13339

Browse files
committed
_regrid_area_weighted_array: Only move x and y dims if necessary. Add extra comments
1 parent 852c34d commit 2d13339

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

lib/iris/experimental/regrid.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,10 @@ def _calculate_regrid_area_weighted_weights(
484484
area_func,
485485
circular=False,
486486
):
487-
""" """
487+
"""
488+
Compute the area weights used for area-weighted regridding.
489+
490+
"""
488491
# Determine which grid bounds are within src extent.
489492
y_within_bounds = _within_bounds(
490493
src_y_bounds, grid_y_bounds, grid_y_decreasing
@@ -594,20 +597,24 @@ def _calculate_regrid_area_weighted_weights(
594597
del area_func, circular
595598

596599
# Ensure we have x_dim and y_dim.
597-
x_dim_orig = copy.copy(x_dim)
598-
y_dim_orig = copy.copy(y_dim)
600+
x_dim_orig = x_dim
601+
y_dim_orig = y_dim
599602
if y_dim is None:
600603
src_data = np.expand_dims(src_data, axis=src_data.ndim)
601604
y_dim = src_data.ndim - 1
602605
if x_dim is None:
603606
src_data = np.expand_dims(src_data, axis=src_data.ndim)
604607
x_dim = src_data.ndim - 1
605608
# Move y_dim and x_dim to last dimensions
606-
src_data = np.moveaxis(src_data, x_dim, -1)
607-
if x_dim < y_dim:
608-
src_data = np.moveaxis(src_data, y_dim - 1, -2)
609-
elif x_dim > y_dim:
610-
src_data = np.moveaxis(src_data, y_dim, -2)
609+
if not x_dim == src_data.ndim - 1:
610+
src_data = np.moveaxis(src_data, x_dim, -1)
611+
if not y_dim == src_data.ndim - 2:
612+
if x_dim < y_dim:
613+
# note: y_dim was shifted along by one position when
614+
# x_dim was moved to the last dimension
615+
src_data = np.moveaxis(src_data, y_dim - 1, -2)
616+
elif x_dim > y_dim:
617+
src_data = np.moveaxis(src_data, y_dim, -2)
611618
x_dim = src_data.ndim - 1
612619
y_dim = src_data.ndim - 2
613620

@@ -663,12 +670,14 @@ def _calculate_regrid_area_weighted_weights(
663670
# Slice out relevant data (this may or may not be a view()
664671
# depending on x_indices being a slice or not).
665672
data = src_data[..., y_indices, x_indices]
666-
Nx = data.shape[-1]
667-
Ny = data.shape[-2]
668-
src_area_datas[..., 0:Ny, 0:Nx, target_pt_ji] = data
669-
src_area_weights[0:Ny, 0:Nx, target_pt_ji] = weights
673+
len_x = data.shape[-1]
674+
len_y = data.shape[-2]
675+
src_area_datas[..., 0:len_y, 0:len_x, target_pt_ji] = data
676+
src_area_weights[0:len_y, 0:len_x, target_pt_ji] = weights
670677
if src_masked:
671-
src_area_masks[..., 0:Ny, 0:Nx, target_pt_ji] = data.mask
678+
src_area_masks[
679+
..., 0:len_y, 0:len_x, target_pt_ji
680+
] = data.mask
672681

673682
# Broadcast the weights array to allow numpy's ma.average
674683
# to be called.
@@ -713,9 +722,13 @@ def _calculate_regrid_area_weighted_weights(
713722
new_data = np.squeeze(new_data, axis=x_dim)
714723
new_data = np.moveaxis(new_data, -1, y_dim_orig)
715724
elif x_dim_orig < y_dim_orig:
725+
# move the x_dim back first, so that the y_dim will
726+
# then be moved to its original position
716727
new_data = np.moveaxis(new_data, -1, x_dim_orig)
717728
new_data = np.moveaxis(new_data, -1, y_dim_orig)
718729
else:
730+
# move the y_dim back first, so that the x_dim will
731+
# then be moved to its original position
719732
new_data = np.moveaxis(new_data, -2, y_dim_orig)
720733
new_data = np.moveaxis(new_data, -1, x_dim_orig)
721734

lib/iris/tests/experimental/regrid/test_regrid_area_weighted_rectilinear_src_and_grid.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,13 +382,22 @@ def test_hybrid_height(self):
382382

383383
def test_missing_data(self):
384384
src = self.simple_cube.copy()
385-
src.data = ma.masked_array(src.data)
385+
src.data = ma.masked_array(src.data, fill_value=999)
386386
src.data[1, 2] = ma.masked
387387
dest = _resampled_grid(self.simple_cube, 2.3, 2.4)
388388
res = regrid_area_weighted(src, dest)
389389
mask = np.zeros((7, 9), bool)
390390
mask[slice(2, 5), slice(4, 7)] = True
391391
self.assertArrayEqual(res.data.mask, mask)
392+
self.assertArrayEqual(res.data.fill_value, 999)
393+
394+
def test_masked_data_all_false(self):
395+
src = self.simple_cube.copy()
396+
src.data = ma.masked_array(src.data, mask=False, fill_value=999)
397+
dest = _resampled_grid(self.simple_cube, 2.3, 2.4)
398+
res = regrid_area_weighted(src, dest)
399+
self.assertArrayEqual(res.data.mask, False)
400+
self.assertArrayEqual(res.data.fill_value, 999)
392401

393402
def test_no_x_overlap(self):
394403
src = self.simple_cube

0 commit comments

Comments
 (0)