编辑现有的NetCDF /将NETCDF写入同名名称

发布于 2025-01-19 18:56:22 字数 1065 浏览 2 评论 0 原文

我正在尝试编辑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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好久不见√ 2025-01-26 18:56:22

来自: https://xarray.pydata.org/en/stable/ user-guide/io.html

“可以使用 mode='a' 参数附加或覆盖 netCDF 变量。使用此选项时,数据集中的所有变量都将写入原始 netCDF 文件,无论如果它们存在于原作中数据集。”

在 python NetCDF 库中我使用:

netCDF4.Dataset(文件,'r+',clobber=True)。

就地编辑属性和变量。也许可以将 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文