admin管理员组

文章数量:1122846

I have a 3D xarray DataArray that I've saved to to zarr using DataArray.to_zarr(). However, the resulting format is not readable by my tools, in this case napari. The structure of the zarr has one folder per dimension. However, if I make it into a dask array and save it with dask.array.to_zarr(), the result is readable in napari, and the result has a single folder.

I would prefer not to have to convert my array to a dask array. Is there a way that I can make xarray write the zarr to the same format as dask.array.to_zarr()?

Minimum reproducible example:

import dask.array as darr
import xarray as xa
import numpy as np
import os
import shutil

data = np.random.randint(0, (2**16)-1, (7000, 512, 512))
array_darr = darr.array(data)
array_xa = xa.DataArray(data, dims=['z', 'y', 'x'], coords={'z': np.arange(data.shape[0]),
    'y': np.arange(data.shape[1]), 'x': np.arange(data.shape[2])})

dir_sup = os.getcwd()

path_darr = os.path.join(dir_sup, 'dask_example.zarr')
if os.path.exists(path_darr):
    shutil.rmtree(path_darr)
array_darr.to_zarr(path_darr)

print(array_xa)

path_xa = os.path.join(dir_sup, 'xarray_example.zarr')
if os.path.exists(path_xa):
    shutil.rmtree(path_xa)
array_xa.to_zarr(path_xa)

本文标签: How to make xarraytozarr write the same way as daskarraytozarrStack Overflow