Skip to content
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

Feature request: writing xarray list-type attributes to netCDF #2044

Closed
dnowacki-usgs opened this issue Apr 9, 2018 · 2 comments · Fixed by #2045
Closed

Feature request: writing xarray list-type attributes to netCDF #2044

dnowacki-usgs opened this issue Apr 9, 2018 · 2 comments · Fixed by #2045

Comments

@dnowacki-usgs
Copy link
Contributor

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.

@shoyer
Copy link
Member

shoyer commented Apr 9, 2018

OK, this is a relatively new feature for netCDF4-Python (Unidata/netcdf4-python#597), but I agree that it's one we should support in xarray. Interestingly, it looks like it will be the default behavior as of the next netCDF4-Python release (Unidata/netcdf4-python#770).

So at this point, I suppose we could just wait. Or if you like, we could backport this behavior in xarray to also support older versions of netCDF4-Python.

The logic for writing attributes with netCDF4-Python in xarray can be found here:
https://github.com/pydata/xarray/blob/master/xarray/backends/netCDF4_.py#L346=L350

and here:
https://github.com/pydata/xarray/blob/master/xarray/backends/netCDF4_.py#L402=L405

@dnowacki-usgs
Copy link
Contributor Author

Ah, nice, thanks. Glad to see it will be default behavior in the future. For kicks, I decided to try to implement it on my own. Pretty much stumbling around in the dark here, but I think I got it working. PR forthcoming.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants