Skip to content

Commit

Permalink
_regrid_area_weighted_array: Tweak variable order to near other use i…
Browse files Browse the repository at this point in the history
…n code (SciTools#3571)
  • Loading branch information
abooton authored and pp-mo committed Jan 14, 2020
1 parent d793977 commit d2c0955
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions lib/iris/experimental/regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,24 @@ def _regrid_area_weighted_array(src_data, x_dim, y_dim,
grid.
"""
# Determine which grid bounds are within src extent.
y_within_bounds = _within_bounds(
src_y_bounds, grid_y_bounds, grid_y_decreasing
)
x_within_bounds = _within_bounds(
src_x_bounds, grid_x_bounds, grid_x_decreasing
)

# Cache which src_bounds are within grid bounds
cached_x_bounds = []
cached_x_indices = []
for (x_0, x_1) in grid_x_bounds:
if grid_x_decreasing:
x_0, x_1 = x_1, x_0
x_bounds, x_indices = _cropped_bounds(src_x_bounds, x_0, x_1)
cached_x_bounds.append(x_bounds)
cached_x_indices.append(x_indices)

# Create empty data array to match the new grid.
# Note that dtype is not preserved and that the array is
# masked to allow for regions that do not overlap.
Expand All @@ -484,22 +502,6 @@ def _regrid_area_weighted_array(src_data, x_dim, y_dim,
# Assign to mask to explode it, allowing indexed assignment.
new_data.mask = False

# Determine which grid bounds are within src extent.
y_within_bounds = _within_bounds(src_y_bounds, grid_y_bounds,
grid_y_decreasing)
x_within_bounds = _within_bounds(src_x_bounds, grid_x_bounds,
grid_x_decreasing)

# Cache which src_bounds are within grid bounds
cached_x_bounds = []
cached_x_indices = []
for (x_0, x_1) in grid_x_bounds:
if grid_x_decreasing:
x_0, x_1 = x_1, x_0
x_bounds, x_indices = _cropped_bounds(src_x_bounds, x_0, x_1)
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:
Expand Down Expand Up @@ -547,15 +549,15 @@ def _regrid_area_weighted_array(src_data, x_dim, y_dim,
raise RuntimeError(
"Cannot handle split bounds " "in both x and y."
)
# Calculate weights based on areas of cropped bounds.
weights = area_func(y_bounds, x_bounds)

if x_dim is not None:
indices[x_dim] = x_indices
if y_dim is not None:
indices[y_dim] = y_indices
data = src_data[tuple(indices)]

# Calculate weights based on areas of cropped bounds.
weights = area_func(y_bounds, x_bounds)

# Transpose weights to match dim ordering in data.
weights_shape_y = weights.shape[0]
weights_shape_x = weights.shape[1]
Expand Down

0 comments on commit d2c0955

Please sign in to comment.