-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DataArray attributes not present in DataSet. Coherency problem between DataSet and NetCDF file #5208
Comments
This syntax recreates the DataArray Instead try
|
dcherian, thank you for your help. But a problem (feature ?) remains : when a Dataset is created, the command Olivier |
You can see variable attributes if you write |
import numpy as np
import xarray as xr
# Creates DataArrays
nt = 4
time = np.arange (nt) * 86400.0
time = xr.DataArray (time, coords=[time,], dims=["time",])
aa = time * 2.0
# Adding attributes to DataArrays
time.attrs['units'] = "second"
aa.attrs['units'] = "whatever"
# Attributes are visible in the DataArrays
print ('----------> time DataArray: ')
print (time)
print ('----------> aa DataArray : ' )
print (aa)
print ('----------> aa attributes : ')
print (aa.attrs )
# Creating a Dataset
ds = xr.Dataset(
{ "aa": (["time",], aa), },
coords={"time": (["time",], time), }, )
# Attributes are not visible in the Dataset
print ('----------> DataSet before setting attributes')
print (ds)
# My request #1 : attributes of the DataArrays should be added to the DataSet (may be optional)
print ('----------> Attributes of aa in DataSet : none')
print ( ds['aa'].attrs )
print ('----------> Attributes of aa outside DataSet : still here')
print ( aa.attrs )
print ('----------> Attributes are not written to the NetCDF file')
ds.to_netcdf ('sample1.nc')
# Adding attributes directly to the Dataset
# Attributes are still not visible in the Dataset
print ('----------> DataSet after setting attributes : attributes not shown' )
ds=ds.assign_attrs({'Visible':'NotInvisibleMan'})
ds['time'].attrs['units']="second"
ds['aa'].attrs['units']="whatever"
ds.to_netcdf('safeReturn.nc')
print(xr.open_dataset('safeReturn.nc').attrs)
print(xr.open_dataset('safeReturn.nc')['aa'].attrs)
print(xr.open_dataset('safeReturn.nc')['time'].attrs) |
When I create a DataSet from DataArrays, attributes are lost.
When are create attributes in a DataSet, they are know shown by
print (DataSet)
, but are written in the NetCDF file.Below is python code showing the xarray behaviour in details.
My requests :
print (DataSet)
. Like for DataArrays.Thanks,
Olivier
The text was updated successfully, but these errors were encountered: