-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
How to efficiently use DataArrays with Cartopy's add_cyclic_point utility? #1005
Comments
Yes, that would be my suggested approach. This would be a good use case for |
Note that |
Any update on this issue? It would be great if Just for other peoples reference, I now have this workaround, creating dd, ll = add_cyclic_point(erai_jja.values, erai_jja.lon)
erai_jja_cy = xr.DataArray(dd, coords={'lat':erai_jja.lat, 'lon':ll}, dims=('lat','lon')) |
Something like this might work for you: import xarray
import numpy as np
def add_cyclic_point(xarray_obj, dim, period=None):
if period is None:
period = xarray_obj.sizes[dim] / xarray_obj.coords[dim][:2].diff(dim).item()
first_point = xarray_obj.isel({dim: slice(1)})
first_point.coords[dim] = first_point.coords[dim]+period
return xarray.concat([xarray_obj, first_point], dim=dim) Example usage:
It would also make sense to add a more generic |
Stephan, thanks a lot for your code snippet from December, this is an elegant solution to the problem. One minor correction though, because I found that it fails to infer the period if none is given. The divide should be a multiplication I believe, i.e. import xarray
import numpy as np
def add_cyclic_point(xarray_obj, dim, period=None):
if period is None:
period = xarray_obj.sizes[dim] * xarray_obj.coords[dim][:2].diff(dim).item()
first_point = xarray_obj.isel({dim: slice(1)})
first_point.coords[dim] = first_point.coords[dim]+period
return xarray.concat([xarray_obj, first_point], dim=dim) |
Hi, I'm not sure as to the best way to take a global NetCDF dataset (e.g. 2.5 x 2.5 deg GFS) whose longitude range does not "cycle" back to 0/360 and pass the variable of interest and the longitude array to Cartopy's add_cyclic_point() utility. I am first selecting the variable of interest and the longitude and converting them to arrays, and then passing them along to add_cyclic_point, but not sure then how to take the tuple that is returned by that utility and "reintegrating" it into xarray's plot.pcolormesh utility. Do I need to create a new Xarray DataArray from the output of add_cyclic_point?
The text was updated successfully, but these errors were encountered: