zarr不考虑来自xarray的块大小并恢复到原始块大小

2024-05-15 21:34:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在打开一个zarr文件,然后重新缓存它,然后将它写回另一个zarr存储。然而,当我打开它的时候,它并不尊重我之前写的块大小。以下是jupyter的代码和输出。知道我做错了什么吗

bathy_ds = xr.open_zarr('data/bathy_store')
bathy_ds.elevation

enter image description here

bathy_ds.chunk(5000).elevation

enter image description here

bathy_ds.chunk(5000).to_zarr('data/elevation_store')
new_ds = xr.open_zarr('data/elevation_store')
new_ds.elevation

enter image description here

它正在恢复到原始的分块,好像我没有完全覆盖它或更改需要更改的其他设置


Tags: 文件tostore代码newdatadsjupyter
1条回答
网友
1楼 · 发布于 2024-05-15 21:34:37

这似乎是一个已知的issue,并且在该问题的线程和recently merged PR中正在进行相当多的讨论

基本上,数据集在.encoding属性中携带原始分块。因此,当您调用第二个写入操作时,在ds[var].encoding['chunks'](如果存在)中定义的块将用于将var写入zarr

根据GH问题中的对话,目前最好的解决方案是手动删除相关变量的区块编码:

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

enter image description here

# rechunk
y = xr.open_zarr("zarr1.zarr/").chunk({"time": -1})

# display
y.air

enter image description here

#write w/o modifying .encoding
y.to_zarr("zarr2.zarr")

# display
xr.open_zarr("zarr2.zarr/").air

enter image description here

# delete encoding and store
del y.air.encoding['chunks']
y.to_zarr("zarr3.zarr")

# display
xr.open_zarr("zarr3.zarr/").air

enter image description here

相关问题 更多 >