@@ -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
0 commit comments