Skip to content

Commit 9dc3bcb

Browse files
committed
Copy strategy from conservative routines
1 parent 1de9330 commit 9dc3bcb

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/xarray_regrid/methods/flox_reduce.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,13 @@ def statistic_reduce(
7272
msg = f"Invalid method. Please choose from '{valid_methods}'."
7373
raise ValueError(msg)
7474

75-
coords = utils.common_coords(data, target_ds, remove_coord=time_dim)
76-
target_coords = xr.Dataset(target_ds.coords) # coords target coords for reindexing
77-
sorted_target_coords = target_coords.sortby(coords)
75+
# Make sure the regridding coordinates are sorted
76+
coord_names = utils.common_coords(data, target_ds, remove_coord=time_dim)
77+
sorted_target_coords = xr.Dataset(coords=target_ds.coords)
78+
for coord_name in coord_names:
79+
sorted_target_coords = utils.ensure_monotonic(sorted_target_coords, coord_name)
80+
data = utils.ensure_monotonic(data, coord_name)
81+
coords = {name: sorted_target_coords[name] for name in coord_names}
7882

7983
bounds = tuple(
8084
construct_intervals(sorted_target_coords[coord].to_numpy()) for coord in coords
@@ -92,7 +96,7 @@ def statistic_reduce(
9296
)
9397

9498
result = restore_properties(result, data, target_ds, coords, fill_value)
95-
result = result.reindex_like(target_coords, copy=False)
99+
result = result.reindex_like(sorted_target_coords, copy=False)
96100
return result
97101

98102

0 commit comments

Comments
 (0)