Reputation: 1175
I wish to have a list of strings as an attribute in an xarray Dataset
that survives serialization to netCDF. I believe this is possible with the NC_STRING type in netCDF-4. xarray supports this but I can't get it to persist after writing to and reading from a netCDF file. After the roundtrip, the attr comes back with the list elements concatenated. I'm thinking I might have to set an encoding parameter, and although I know how to do this for variables, I don't know how for attributes.
import xarray as xr
ds = xr.Dataset()
ds.attrs['testing'] = ['a', 'b']
print(ds)
Gives:
<xarray.Dataset>
Dimensions: ()
Data variables:
*empty*
Attributes:
testing: ['a', 'b']
Now,
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
In response to @jhamman's comment, this behavior is supported using netCDF4's netncattr_string
method:
import netCDF4 as nc
rg = nc.Dataset('test_string.nc', 'w', format='NETCDF4')
rg.setncattr_string('testing', ['a', 'b'])
rg.close()
Running ncdump on test_string.nc produces:
$ ncdump test_string.nc
netcdf test_string {
// global attributes:
string :testing = "a", "b" ;
}
as opposed to setting an attribute in the usual way, which results in a concatenated attribute, as when the nc is created by xarray above:
rg = nc.Dataset('test_normal.nc', 'w', format='NETCDF4')
rg.testing = ['a', 'b']
rg.close()
ncdump results:
$ ncdump test_normal.nc
netcdf test_normal {
// global attributes:
:testing = "ab" ;
}
Reading the .nc file created using setncattr_string
properly imports a list of strings as the attributes in an xarray Dataset:
ds = xr.open_dataset('test_string.nc')
print(ds)
gives:
<xarray.Dataset>
Dimensions: ()
Data variables:
*empty*
Attributes:
testing: ['a', 'b']
So I guess something like setncattr_string
could be implemented in xarray to achieve this?
Upvotes: 2
Views: 2118
Reputation: 1175
Encoding lists of strings as attributes has been implemented in xarray and should be in the next release (0.10.4).
Upvotes: 1