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?
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
Thanks. I guess this inevitably requires identification and manipulation of DataArrays in the catalog that are not "cf-xarray-compliant".
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: May 16 2025 at 17:14 UTC