xarray-contrib / xarray-regrid

Regridding utility for xarray
https://xarray-regrid.readthedocs.org/
Apache License 2.0
71 stars 7 forks source link

Added sortby to target grid #29

Closed kjdoore closed 9 months ago

kjdoore commented 9 months ago

When regridding using the conservative and most_common methods, the plane coordinates (i.e., latitude and longitude) needed to be in increasing order in the target grid. This adds sorting of the target grid.

Fixes the issue in #28.

BSchilperoort commented 9 months ago

Thanks for submitting this PR!

It's good to fix this bug, but I feel like it might be best to return the grid in the original order. This is how the interp based methods currently work.

The extra steps for this would be to compute the sorted grid, regrid using the sorted grid, and then sort by the original target_grid again:

for coord in target_grid.coords:
    data = data.sortby(target_grid[coord])

Could you also add tests for these cases (the monotonic increasing and monotonic decreasing coordinates)?

kjdoore commented 9 months ago

Good call! Yes, I'd be happy to make this adjustment and add some test to confirm it is working as intended.

kjdoore commented 9 months ago

@BSchilperoort, I made an update to return the regridded data in the original order of the target grid. sortby did not quite work as you proposed, but I was able to create a solution. Feel free to give it a review and let me know what you think.

kjdoore commented 9 months ago

Great thanks! reindex_like seems like the way to go. I looked for something like that, but didn't find it on my pass through the xarray docs. I'm glad you did.