-
I have a question, which I don't know if this is a bug, a feature, or something I'm doing wrong (I hope it was correct to open a discussion and not an issue). Use caseWe are developing a small wrapper around the gsw (Gibbs Sea Water) package that is used to compute thermodynamic properties of the ocean (e.g. density, potential temperature, etc). The goal of this wrapper is to add a xarray layer around the gsw package, adding when possible standard_name attribute, units, etc. For some variables, the CF-convention tells that a scalar coordinate should be added, counting for the reference pressure used for the calculation, e.g. So the main question is: how to attach a scalar coordinate to only one dataarray and not the whole dataset? CodeIf the coordinate is added to a dataarray in a dataset, it seems to be only attach to this specific dataarray: import xarray as xr
ds = xr.Dataset()
temp = xr.DataArray([[0,1,2], [3,4,5]], dims=('lat', 'lon'))
sigma1 = xr.DataArray([25.0], dims=('depth'))
ds['temp'] = temp
ds['sigma1'] = sigma1
print(ds, '\n')
ds.sigma1.coords['pref'] = 1000
print(ds) Output:
However, if the scalar coordinate is added to a dataarray that is merge into a dataset, it becomes a coordinate of the dataset: import xarray as xr
ds = xr.Dataset()
temp = xr.DataArray([[0,1,2], [3,4,5]], dims=('lat', 'lon'))
sigma1 = xr.DataArray([25.0], dims=('depth'))
sigma1.coords['pref'] = 1000
ds['temp'] = temp
ds['sigma1'] = sigma1
print(ds) Output
What we triedThe CF convention describes scalar coordinate through the attribute Anyone would have any idea if this is possible within the xarray model? Other links |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
As you've found, xarray does not support this. import cf_xarray
import xarray as xr
ds = xr.Dataset()
temp = xr.DataArray([[0, 1, 2], [3, 4, 5]], dims=("lat", "lon"))
sigma1 = xr.DataArray(
[25.0], dims=("depth"), coords={"pref": 1000}, attrs={"coordinates": "pref"}
)
ds["temp"] = temp
ds["sigma1"] = sigma1
print(ds.cf["temp"].coords)
print(ds.cf["sigma1"].coords)
|
Beta Was this translation helpful? Give feedback.
As you've found, xarray does not support this.
cf_xarray
, on the other hand, does parse thecoordinates
attribute