pydata / pydata/xarray

Unable to write into existing Zarr archive due to mismatching dask chunks

Open
#10,034 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug needs triage
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

What happened?

I am having a Zarr archive (v3) that I open this way: xarray.open_zarr(self.parsed_data_path, zarr_format=3, consolidated=False)

<xarray.Dataset> Size: 4GB
Dimensions:                       (dt_calc: 1, dt_fore: 49, lat: 746, lon: 1215)
Coordinates:
    lon                           (lat, lon) float64 7MB dask.array<chunksize=(187, 304), meta=np.ndarray>
    lat                           (lat, lon) float64 7MB dask.array<chunksize=(187, 304), meta=np.ndarray>
  * dt_fore                       (dt_fore) float64 392B 0.0 1.0 ... 47.0 48.0
  * dt_calc                       (dt_calc) datetime64[ns] 8B 2025-01-22
Data variables: (12/41)
    total_cloud_cover             (dt_calc, dt_fore, lat, lon) int16 89MB dask.array<chunksize=(1, 49, 10, 10), meta=np.ndarray>
    wind_speed_341                (dt_calc, dt_fore, lat, lon) int16 89MB dask.array<chunksize=(1, 49, 10, 10), meta=np.ndarray>
    wind_direction_149            (dt_calc, dt_fore, lat, lon) int16 89MB dask.array<chunksize=(1, 49, 10, 10), meta=np.ndarray>
    wind_speed_271                (dt_calc, dt_fore, lat, lon) int16 89MB dask.array<chunksize=(1, 49, 10, 10), meta=np.ndarray>
    wind_direction_97             (dt_calc, dt_fore, lat, lon) int16 89MB dask.array<chunksize=(1, 49, 10, 10), meta=np.ndarray>
    specific_humidity_3000        (dt_calc, dt_fore, lat, lon) float16 89MB dask.array<chunksize=(1, 49, 10, 10), meta=np.ndarray>
    ...                            ...
    geopotential_height_500       (dt_calc, dt_fore, lat, lon) int16 89MB dask.array<chunksize=(1, 49, 10, 10), meta=np.ndarray>
    wind_direction_271            (dt_calc, dt_fore, lat, lon) int16 89MB dask.array<chunksize=(1, 49, 10, 10), meta=np.ndarray>
    air_temperature_850           (dt_calc, dt_fore, lat, lon) float16 89MB dask.array<chunksize=(1, 49, 10, 10), meta=np.ndarray>
    wind_direction_10             (dt_calc, dt_fore, lat, lon) int16 89MB dask.array<chunksize=(1, 49, 10, 10), meta=np.ndarray>
    medium_level_clouds           (dt_calc, dt_fore, lat, lon) int16 89MB dask.array<chunksize=(1, 49, 10, 10), meta=np.ndarray>
    geopotential_height_850       (dt_calc, dt_fore, lat, lon) int16 89MB dask.array<chunksize=(1, 49, 10, 10), meta=np.ndarray>

Now I want to write this into another Zarr archive with different chunksizes = (31, 49, 50, 50). When I do this the same way I did before I receive the following error message:

ValueError: Specified zarr chunks encoding['chunks']=(31, 49, 50, 50) for variable named 'wind_direction_97' would overlap multiple dask chunks ((31,), (49,), (10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 6), (10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 5)) on the region (slice(None, None, None), slice(None, None, None), slice(None, None, None), slice(None, None, None)). Writing this array in parallel with dask could lead to corrupted data. Consider either rechunking using `chunk()`, deleting or modifying `encoding['chunks']`, or specify `safe_chunks=False`.

So I chunk the data as suggested:

self.parsed_data = self.parsed_data.chunk({COLUMN_DT_CALC: 31, COLUMN_DT_FORE: 49, "lat": 50, "lon": 50})

Afterwards I receive another error:

ValueError: Specified zarr chunks encoding['chunks']=(31, 49, 50, 50) for variable named 'wind_direction_97' would overlap multiple dask chunks ((1,), (49,), (50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 46), (50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 15)) on the region (slice(21, 22, None), slice(None, 49, None), slice(None, 746, None), slice(None, 1215, None)). Writing this array in parallel with dask could lead to corrupted data. Consider either rechunking using `chunk()`, deleting or modifying `encoding['chunks']`, or specify `safe_chunks=False`

I am trying to debug this for several hours now and I can't find any helpful notes, so I hope anyone of you can help me to fix this. I applied drop_encoding() before.

The Zarr archive will be initialized as followed:

def _initialize_zarr_store(
    store: Union[zarr.storage.FsspecStore, Path],
    dataset: xarray.Dataset,
    encoding: Dict[str, Any],
    full_time_dimension: str,
    write_dim: str,
    zarr_format: int,
):
    """Initialize the Zarr store with an empty template if it does not already exist."""
    template = xarray.full_like(dataset, np.nan)
    try:
        template = (
            template.rename({write_dim: "drop_dim"})
            .isel(drop_dim=0, drop=True)
            .expand_dims(dim={write_dim: full_time_dimension}, axis=0)
        )
        template.to_zarr(
            store,
            mode="w-",
            encoding=encoding,
            compute=False,
            consolidated=(zarr_format < 3),
            zarr_format=zarr_format,
        )

    except (zarr.errors.ContainsGroupError, FileExistsError):
        # Store already exists, no need to create a new template
        pass

Please note that I had to add this line: template = xarray.full_like(dataset, np.nan) after the update to the latest version, as compute=False does not work correctly.

And this is how I write the data with to_zarr:

    dataset.to_zarr(
        store,
        region=region,
        mode="r+",
        compute=True,
        consolidated=True if zarr_format < 3 else False,
        zarr_format=zarr_format,
    )

Note that there was no Issue with versions: zarr==2.17.2, xarray==2025.1.1

What did you expect to happen?

That I am able to write a dataset (loaded from a Zarrv3 archive) into a region of another Zarr archive that is in version 2.

Minimal Complete Verifiable Example

MVCE confirmation
  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS

commit: None
python: 3.12.8 (main, Feb 4 2025, 04:56:13) [GCC 12.2.0]
python-bits: 64
OS: Linux
OS-release: 6.8.0-110052-tuxedo
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: C.UTF-8
LOCALE: ('C', 'UTF-8')
libhdf5: 1.14.2
libnetcdf: 4.9.4-development

xarray: 2025.1.2
pandas: 2.2.3
numpy: 2.2.0
scipy: 1.14.1
netCDF4: 1.7.2
pydap: None
h5netcdf: 1.4.1
h5py: 3.12.1
zarr: 3.0.1
cftime: 1.6.4.post1
nc_time_axis: None
iris: None
bottleneck: None
dask: 2024.11.2
distributed: 2024.11.2
matplotlib: 3.9.3
cartopy: None
seaborn: None
numbagg: None
fsspec: 2024.12.0
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 75.6.0
pip: 24.3.1
conda: None
pytest: 7.3.1
mypy: None
IPython: 8.11.0
sphinx: None

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the region write using the shown _initialize_zarr_store function and dataset.to_zarr calls, then inspect how chunk() and drop_encoding() affect the encoding. Compare the source Dask chunks with the requested Zarr chunks and the region slices. Done means the reported Zarr v3-to-v2 regional write completes without unsafe-overlap errors while preserving the intended chunks.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.