Skip to content

Add Zarr v3 output and per-dimension sharding to rechunk2zarr - #35

Draft
tackandr with Copilot wants to merge 3 commits into
developmentfrom
copilot/rechunk-into-zarr-v3
Draft

Add Zarr v3 output and per-dimension sharding to rechunk2zarr#35
tackandr with Copilot wants to merge 3 commits into
developmentfrom
copilot/rechunk-into-zarr-v3

Conversation

Copilot AI commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

rechunk2zarr only produced Zarr v2 stores. This adds a --zarr-format switch and per-dimension shard sizes so users can emit v3 sharded arrays.

Changes

  • rechunk.py
    • New rechunk_zarr kwargs: zarr_format (2 or 3, default 2) and t_shard / c_shard / spatial_shard.
    • New _validate_shards helper: rejects shards when zarr_format != 3, requires each shard to be a positive-integer multiple of its chunk, and requires an explicit c_chunk when c_shard is set.
    • Passes shards= to create_array; axes without an explicit shard collapse to one chunk per shard (i.e. no sharding on that axis).
    • Single-pass write loop now iterates by shard size when sharding is enabled so each shard is written exactly once (buffer grows with shard, not chunk, size).
    • When writing to a v3 destination, compressors="auto" is used — v2 numcodecs compressors from the source aren't valid v3 BytesBytesCodecs and can't be passed through.
  • CLI: --zarr-format {2,3}, --t-shard, --c-shard, --spatial-shard.
  • test_rechunk.py (new): validation edge cases + end-to-end tests for v2, v3-no-shards, v3 full sharding, and v3 partial sharding.
  • README.md: documents the new flags and library usage.

Example

from rechunk import rechunk_zarr

rechunk_zarr(
    src_path="myfile.zarr",
    dst_path="myfile_v3.zarr",
    t_chunk=24, c_chunk=8, spatial_chunk=100,
    zarr_format=3,
    t_shard=48,          # multiple of t_chunk
    spatial_shard=500,   # multiple of spatial_chunk; c axis stays unsharded
)
rechunk2zarr src.zarr dst.zarr --zarr-format 3 \
    --t-chunk 24 --t-shard 48 --spatial-chunk 100 --spatial-shard 500

Copilot AI linked an issue Aug 17, 2026 that may be closed by this pull request
Co-authored-by: tackandr <26540124+tackandr@users.noreply.github.com>
Copilot AI changed the title [WIP] Add option to rechunk code for Zarr v3 output Add Zarr v3 output and per-dimension sharding to rechunk2zarr Aug 17, 2026
Copilot AI requested a review from tackandr August 17, 2026 13:01
@tackandr

Copy link
Copy Markdown
Owner

@copilot When I open a resulting dataset it says: KeyError: 'Zarr object is missing the dimension_names metadata which is required for xarray to determine variable dimensions.'

Co-authored-by: tackandr <26540124+tackandr@users.noreply.github.com>

Copilot AI commented Aug 17, 2026

Copy link
Copy Markdown
Contributor Author

@copilot When I open a resulting dataset it says: KeyError: 'Zarr object is missing the dimension_names metadata which is required for xarray to determine variable dimensions.'

Fixed in f03d3b4. The v3 destination arrays weren't getting a dimension_names metadata entry — v2 stores dims in the _ARRAY_DIMENSIONS attribute, which xarray doesn't consult for v3 stores. rechunk_zarr now reads the source's dimension names (from v3 metadata or the v2 _ARRAY_DIMENSIONS attr) and passes them to create_array(..., dimension_names=...) for every v3 array (data + coords), so xr.open_zarr(dst) works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rechunk into Zarr v3

2 participants