wrf-python icon indicating copy to clipboard operation
wrf-python copied to clipboard

support xarray.Dataset as input to get_cartopy and getvar

Open andreas-h opened this issue 8 years ago • 5 comments

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.

andreas-h avatar Mar 09 '17 13:03 andreas-h

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.

bladwig1 avatar Mar 09 '17 17:03 bladwig1

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!

andreas-h avatar Mar 10 '17 11:03 andreas-h

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.

bladwig1 avatar Mar 10 '17 16:03 bladwig1

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.

zxdawn avatar Mar 09 '21 03:03 zxdawn

Hi,

So is there a way to work with the get_cartopy using xarray.open_mfdataset()

Murk89 avatar Sep 13 '21 15:09 Murk89