@@ -461,6 +461,24 @@ def _regrid_area_weighted_array(src_data, x_dim, y_dim,
461461 grid.
462462
463463 """
464+ # Determine which grid bounds are within src extent.
465+ y_within_bounds = _within_bounds (
466+ src_y_bounds , grid_y_bounds , grid_y_decreasing
467+ )
468+ x_within_bounds = _within_bounds (
469+ src_x_bounds , grid_x_bounds , grid_x_decreasing
470+ )
471+
472+ # Cache which src_bounds are within grid bounds
473+ cached_x_bounds = []
474+ cached_x_indices = []
475+ for (x_0 , x_1 ) in grid_x_bounds :
476+ if grid_x_decreasing :
477+ x_0 , x_1 = x_1 , x_0
478+ x_bounds , x_indices = _cropped_bounds (src_x_bounds , x_0 , x_1 )
479+ cached_x_bounds .append (x_bounds )
480+ cached_x_indices .append (x_indices )
481+
464482 # Create empty data array to match the new grid.
465483 # Note that dtype is not preserved and that the array is
466484 # masked to allow for regions that do not overlap.
@@ -484,22 +502,6 @@ def _regrid_area_weighted_array(src_data, x_dim, y_dim,
484502 # Assign to mask to explode it, allowing indexed assignment.
485503 new_data .mask = False
486504
487- # Determine which grid bounds are within src extent.
488- y_within_bounds = _within_bounds (src_y_bounds , grid_y_bounds ,
489- grid_y_decreasing )
490- x_within_bounds = _within_bounds (src_x_bounds , grid_x_bounds ,
491- grid_x_decreasing )
492-
493- # Cache which src_bounds are within grid bounds
494- cached_x_bounds = []
495- cached_x_indices = []
496- for (x_0 , x_1 ) in grid_x_bounds :
497- if grid_x_decreasing :
498- x_0 , x_1 = x_1 , x_0
499- x_bounds , x_indices = _cropped_bounds (src_x_bounds , x_0 , x_1 )
500- cached_x_bounds .append (x_bounds )
501- cached_x_indices .append (x_indices )
502-
503505 # Axes of data over which the weighted mean is calculated.
504506 axes = []
505507 if y_dim is not None :
@@ -547,15 +549,15 @@ def _regrid_area_weighted_array(src_data, x_dim, y_dim,
547549 raise RuntimeError (
548550 "Cannot handle split bounds " "in both x and y."
549551 )
552+ # Calculate weights based on areas of cropped bounds.
553+ weights = area_func (y_bounds , x_bounds )
554+
550555 if x_dim is not None :
551556 indices [x_dim ] = x_indices
552557 if y_dim is not None :
553558 indices [y_dim ] = y_indices
554559 data = src_data [tuple (indices )]
555560
556- # Calculate weights based on areas of cropped bounds.
557- weights = area_func (y_bounds , x_bounds )
558-
559561 # Transpose weights to match dim ordering in data.
560562 weights_shape_y = weights .shape [0 ]
561563 weights_shape_x = weights .shape [1 ]
0 commit comments