Closed
Description
Migrated from Stack Overflow.
NetCDF supports the NC_STRING type, which can stores arrays of strings in attributes. Xarray already supports reading arrays of strings from attributes in netCDF files, and It would be great if it also supported writing the same.
Reading already works
import xarray as xr
import netCDF4 as nc
rg = nc.Dataset('test_string.nc', 'w', format='NETCDF4')
rg.setncattr_string('testing', ['a', 'b'])
rg.close()
ds = xr.open_dataset('test_string.nc')
print(ds)
gives
<xarray.Dataset>
Dimensions: ()
Data variables:
*empty*
Attributes:
testing: ['a', 'b']
This works because I used the setncattr_string
method. Setting the attributes like rg.testing = ['a', 'b']
does not work and results in a concatenated list (just like the xarray example below).
Writing doesn't work
import xarray as xr
ds = xr.Dataset()
ds.attrs['testing'] = ['a', 'b']
ds.to_netcdf('asdf.nc')
ds = xr.open_dataset('asdf.nc', autoclose=True)
print(ds)
gives
<xarray.Dataset>
Dimensions: ()
Data variables:
*empty*
Attributes:
testing: ab
Note the list elements have been concatenated. So this is a request for xarray to implement something like netCDF4's setncattr_string
. I would be happy to help do this if someone pointed me in the right direction; I looked through the code but got lost pretty quickly. Thanks.
Metadata
Metadata
Assignees
Labels
No labels