Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sortby to target grid #29

Merged
merged 6 commits into from
Feb 15, 2024
Merged

Conversation

kjdoore
Copy link
Collaborator

@kjdoore kjdoore commented Feb 13, 2024

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
Copy link
Contributor

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
Copy link
Collaborator Author

kjdoore commented Feb 14, 2024

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

@kjdoore
Copy link
Collaborator Author

kjdoore commented Feb 14, 2024

@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.

Copy link
Contributor

@BSchilperoort BSchilperoort left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making the changes! I looked in xarray's documentation and code, and found the reindex_like method which seems to work very well and is just a single line.

I also reordered the tests a bit with more parameterization to make them more compact :)

The test suite has become a bit slow though, which is because we use a full ERA5 file for the tests. This is good for some of the bigger tests, but for things like the attributes and coordinates it is probably better to use a smaller file.

@BSchilperoort BSchilperoort merged commit 432b847 into xarray-contrib:main Feb 15, 2024
7 checks passed
@kjdoore
Copy link
Collaborator Author

kjdoore commented Feb 15, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants