Skip to content

almost-equal grids #784

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

Closed
mathause opened this issue Mar 4, 2016 · 6 comments
Closed

almost-equal grids #784

mathause opened this issue Mar 4, 2016 · 6 comments

Comments

@mathause
Copy link
Collaborator

mathause commented Mar 4, 2016

Sometimes it happens that you have two grids that are equal up to precision or "for practical purposes". xarray does not align them. Is there a possibility to still get them aligned (and not having to copy the coordinates from one to the other)?

Examples:

  • CESM output from the land model and the atmosphere model.
  • I have a file with a region definition on a fine grid. If I regrid that to the model resolution with cdo the grids are equal up to ~1*10**-4.
@darothen
Copy link

darothen commented Mar 4, 2016

Hi @mathause, I actually just ran into a very similar problem to your second bullet point. I had some limited success by manually re-building the re-gridded dataset onto the CESM coordinate system, swapping out the not-exactly-but-actually-close-enough coordinates for the CESM reference data's coordinates. In my case, I was re-gridding with CDO, but even when I explicitly pull out the CESM grid definition it wouldn't match precisely.

Since there was a lot of boilerplate code to do this in xarray (although I had a lot of success defining a callback to pass in with open_dataset), it was far easier just to use NCO to copy the correct coordinate variables into the re-gridded data.

@shoyer
Copy link
Member

shoyer commented Mar 4, 2016

This doesn't work automatically, but you can use the method='nearest' and the tolerance argument with .reindex or .reindex_like to align two datasets with different grids, i.e.,

second = first.reindex_like(second, method='nearest', tolerance=0.001)

It would be nice for this to be able to work automatically, but the challenge is picking a heuristic that does not violate the principle of least surprise. A proposed a few ideas in a related issue in pandas (pandas-dev/pandas#9817 (comment)).

@ocefpaf
Copy link
Contributor

ocefpaf commented Mar 4, 2016

second = first.reindex_like(second, method='nearest', tolerance=0.001)

I really like this. Explicit and self-documenting code. I would avoid making this automatic.

@mathause
Copy link
Collaborator Author

mathause commented Mar 4, 2016

That sounds good. Do you need both, methodand tolerance?

What I do at the moment is probably not as elegant:

    ds.coords['lat'] = np.round(ds['lat'], 4)
    ds.coords['lon'] = np.round(ds['lon'], 4)

@darothen
Copy link

darothen commented Mar 4, 2016

The reindex_like() approach works super well in my case. Since only my latitudes are screwed up (and they're spaced by a tad more than a degree), a low tolerance 1e-2-1e-3 worked perfectly.

@jhamman
Copy link
Member

jhamman commented Mar 26, 2016

@mathause - any reason to keep this open or did we sort this out?

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

No branches or pull requests

5 participants