Stream: python-questions
Topic: cf-xarray fails to identify X,Y axes
Stephen Yeager (Nov 17 2021 at 20:31):
I'm looking at CMIP6 ocean data using cf-xarray. One of my functions needs the dimensions of the 'X' and 'Y' axes of a DataArray, defined as follows:
ny = da.cf.sizes['Y']
nx = da.cf.sizes['X']
This works for many models, like this one: Screen-Shot-2021-11-17-at-1.22.19-PM.png
but fails for others, like this one: Screen-Shot-2021-11-17-at-1.21.01-PM.png
Using cf.guess_coord_axis() doesn't help. It seems that cf-xarray can identify X,Y axes only if they are included as coordinate arrays. Is there an easy workaround for robust identification of grid 'X' and 'Y' dimensions?
Deepak Cherian (Nov 17 2021 at 21:11):
cf_xarray fundamentally works by looking at attributes. the x
and y
in that dataset don't have attributes. If yo udo
ds["x"] = np.arange(ds.sizes["x"])
ds["y"] = np.arange(ds.sizes["y"])
ds = ds.cf.guess_coord_axis()
This should now work and set the right attributes on x
and y
Stephen Yeager (Nov 17 2021 at 21:29):
Thanks. I guess this inevitably requires identification and manipulation of DataArrays in the catalog that are not "cf-xarray-compliant".
Deepak Cherian (Nov 18 2021 at 17:06):
Well... x
and y
have absolutely no data associated with them in the file. They only exist as dimension names, which isn't great.
For convenience, we could teach cf-xarray to optionally do that assignment before guessing: https://github.com/xarray-contrib/cf-xarray/issues/266
Last updated: Jan 30 2022 at 12:01 UTC