Reputation: 117
I am trying to edit netcdf datasets and save them back to the same filename (e.g. to change the attributes or change variable names in a dataset). e.g.:
import xarray as xr
import numpy as np
data = xr.DataArray(np.random.randn(2, 3), coords={'x': ['a', 'b']}, dims=('x', 'y'))
data.to_netcdf('test.nc')
test_open=xr.open_dataset('test.nc')
test_open=test_open.rename({'__xarray_dataarray_variable__':'var'})
test_open.to_netcdf('test.nc')
This gives a 'permission denied' error "PermissionError: [Errno 13] Permission denied"
Reading https://github.com/pydata/xarray/issues/2887, it seems it's to do with the file being open, and I've also tried:
with xr.open_dataset('test.nc') as test_open:
test_open=test_open.rename({'__xarray_dataarray_variable__':'var'})
test_open.to_netcdf('test.nc')
or
with xr.open_dataset('test.nc') as test_open:
test_open=test_open.rename({'__xarray_dataarray_variable__':'var'})
test_open.to_netcdf('test.nc')
I often need to do this with large datasets so would rather not load the dataset into memory, although even if I do I sometimes get 'HDF5' errors.
I don't fully understand what is causing the problem here, can anyone explain? It seems like it must be a common problem to change metadata on a dataset without having to load or rename the dataset. I'm using the latest version of xarray, 0.16.1.
Upvotes: 4
Views: 2942
Reputation: 3794
From: https://xarray.pydata.org/en/stable/user-guide/io.html
"It is possible to append or overwrite netCDF variables using the mode='a' argument. When using this option, all variables in the dataset will be written to the original netCDF file, regardless if they exist in the original dataset."
In the python NetCDF library I use:
netCDF4.Dataset(file,'r+',clobber=True).
to edit attributes and variables in place. Perhaps clobber
could be passed to xarray's **kwargs
which get passed to the underlying NetCDF4 engine.
Upvotes: 1