我正在尝试编辑NetCDF数据集并将其保存回同一文件名(例如,更改数据集中的属性或更改变量名称)。例如:
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')
这给出了“许可拒绝”错误”常规词:[errno 13]允许拒绝“
读取” ,看来这与已打开的文件有关,我也尝试过:
with xr.open_dataset('test.nc') as test_open:
test_open=test_open.rename({'__xarray_dataarray_variable__':'var'})
test_open.to_netcdf('test.nc')
或者
with xr.open_dataset('test.nc') as test_open:
test_open=test_open.rename({'__xarray_dataarray_variable__':'var'})
test_open.to_netcdf('test.nc')
我经常需要使用大型数据集来执行此操作不要将数据集加载到内存中,尽管即使我有时会出现“ HDF5”错误。
我不完全了解这里引起问题的原因,有人可以解释吗?在不必加载或重命名数据集的情况下,更改数据集上的元数据似乎一定是一个普遍的问题。我正在使用最新版本的Xarray,0.16.1。
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.
发布评论
评论(1)
来自: https://xarray.pydata.org/en/stable/ user-guide/io.html
“可以使用 mode='a' 参数附加或覆盖 netCDF 变量。使用此选项时,数据集中的所有变量都将写入原始 netCDF 文件,无论如果它们存在于原作中数据集。”
在 python NetCDF 库中我使用:
就地编辑属性和变量。也许可以将
clobber
传递给 xarray 的**kwargs
,然后再传递给底层 NetCDF4 引擎。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:
to edit attributes and variables in place. Perhaps
clobber
could be passed to xarray's**kwargs
which get passed to the underlying NetCDF4 engine.