Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PI-2472: _regrid_area_weighted_array: Axes creation before loop #3519

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/iris/experimental/regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,14 @@ def _regrid_area_weighted_array(src_data, x_dim, y_dim,
cached_x_bounds.append(x_bounds)
cached_x_indices.append(x_indices)

# Axes of data over which the weighted mean is calculated.
axes = []
if y_dim is not None:
axes.append(y_dim)
if x_dim is not None:
axes.append(x_dim)
axis = tuple(axes)

# Simple for loop approach.
for j, (y_0, y_1) in enumerate(grid_y_bounds):
# Reverse lower and upper if dest grid is decreasing.
Expand Down Expand Up @@ -581,19 +589,14 @@ def _regrid_area_weighted_array(src_data, x_dim, y_dim,
# Broadcast the weights array to allow numpy's ma.average
# to be called.
weights_padded_shape = [1] * data.ndim
axes = []
if y_dim is not None:
weights_padded_shape[y_dim] = weights_shape_y
axes.append(y_dim)
if x_dim is not None:
weights_padded_shape[x_dim] = weights_shape_x
axes.append(x_dim)
# Assign new shape to raise error on copy.
weights.shape = weights_padded_shape
# Broadcast weights to match shape of data.
_, weights = np.broadcast_arrays(data, weights)
# Axes of data over which the weighted mean is calculated.
axis = tuple(axes)

# Calculate weighted mean taking into account missing data.
new_data_pt = _weighted_mean_with_mdtol(
Expand Down