support xarray.Dataset as input to get_cartopy and getvar
it would be great if the get_cartopy and getvar functions would accept a xarray.Dataset as var input. Obviously, this can only work properly if the Dataset was loaded with a standard xarray.open_dataset call and the metadata were not modified, but still, it would be very convenient.
Thank you for the feedback. I plan on improving xarray support in the future 1.1 release, so this is definitely on my to-do list. For now, a workaround for get_cartopy is to use the 'getproj' function by supplying the MAP_PROJ, TRUELAT1, TRUELAT2, MOAD_CEN_LAT, STAND_LON, POLE_LAT, POLE_LON global attributes from the WRF file as keyword arguments to getproj (note: it's case insensitive). This will return one of the Projection subclasses. You can then call the Projection.cartopy() method to get the cartopy object. As long as the projection is not a RotatedPole projection, you should have all you need.
For the RotatedPole projection, you need to use the Projection.cartopy_xlim() and Projection.cartopy_ylim() methods to set the map boundaries, since the builtin stuff with cartopy doesn't work correctly with RotatedPole (however, it might be fixed now). This requires supplying a GeoBounds argument, which is just the corner points from XLAT and XLONG.
Thanks, that's great to hear!
Thanks to the xarray developer(s), it's even easier:
ds = xr.open_dataset(FILENAME)
proj = get_cartopy(ds._file_obj.ds)
Cheers!
Nice, didn't know about that. I might be able to sneak this change in now, since this requires no change to the underlying netcdf reading code. It might not get documented until version 1.1.
ds._file_obj.ds doesn't work for xarray new version (>=0.17.0) anymore :
lib/python3.8/site-packages/xarray/core/common.py", line 239, in __getattr__
raise AttributeError(
AttributeError: 'Dataset' object has no attribute '_file_obj'
I guess it's related to https://github.com/pydata/xarray/pull/4809.
Hi,
So is there a way to work with the get_cartopy using xarray.open_mfdataset()