问题描述
我正在打开一个 zarr 文件,然后将其重新分块,然后将其写回另一个 zarr 存储。然而,当我重新打开它时,它不尊重我之前编写的块大小。这是 jupyter 的代码和输出。知道我在这里做错了什么吗?
<!DOCTYPE html>
<html>
<head><title>TextAhnaf</title></head>
<link rel="stylesheet" href="Styles.css">
<body>
<header>
<nav>
<ul>
<li><a href="Login.html">Login</a></li>
<h4>Contact</h4>
</ul>
</nav>
<h1>Text</h1>
</header>
</body>
</html>
bathy_ds = xr.open_zarr('data/bathy_store')
bathy_ds.elevation
bathy_ds.chunk(5000).elevation
它正在恢复到原始分块,好像我没有完全覆盖它或更改其他一些需要更改的设置。
解决方法
这似乎是一个已知的 issue,并且在该问题的线程和 recently merged PR 中进行了相当多的讨论。
基本上,数据集在 .encoding
属性中携带原始分块。因此,当您调用第二个写操作时,ds[var].encoding['chunks']
中定义的块(如果存在)将用于将 var
写入 zarr。
根据GH issue中的对话,目前最好的解决方案是手动删除相关变量的块编码:
for var in ds:
del ds[var].encoding['chunks']
但是,应该注意的是,这似乎是一个不断发展的情况,最好检查进度以调整最终解决方案。
这是一个展示问题和解决方案的小例子:
import xarray as xr
# load data and write to initial chunking
x = xr.tutorial.load_dataset("air_temperature")
x.chunk({"time":500,"lat":-1,"lon":-1}).to_zarr("zarr1.zarr")
# display initial chunking
xr.open_zarr("zarr1.zarr/").air
# rechunk
y = xr.open_zarr("zarr1.zarr/").chunk({"time": -1})
# display
y.air
#write w/o modifying .encoding
y.to_zarr("zarr2.zarr")
# display
xr.open_zarr("zarr2.zarr/").air
# delete encoding and store
del y.air.encoding['chunks']
y.to_zarr("zarr3.zarr")
# display
xr.open_zarr("zarr3.zarr/").air