Zarr fundamentals

From uw-ssec/rse-plugins · View interactive version

Zarr fundamentals is an AI agent skill from the GitHub repository uw-ssec/rse-plugins. Use when creating, opening, indexing or organizing Zarr arrays and groups with zarr-python (.zarr stores, zarr.json or .zarray metadata), setting attributes, fill values, dimension names or shards, choosing between Zarr format v2 and v3, porting zarr 2 code that breaks on zarr 3 (FSStore, ProcessSyn

How to use this skill

Skill file

Zarr Fundamentals

Creates, opens, indexes and organizes chunked N-dimensional Zarr arrays and groups with the zarr-python 3 API, in Zarr format v3 (the default) or v2.

Core principle: zarr reads, decodes and writes whole chunks. In a sharded array the inner chunk is the read unit and the shard is the stored object, so every write rewrites its whole shard. Match chunks to reads, and give each parallel writer whole chunks (whole shards when sharded).

Docs: https://zarr.readthedocs.io/ | Spec: https://zarr-specs.readthedocs.io/

When to Use

When NOT to use:

Quick Reference

Install zarr (pip or pixi); numcodecs comes with it. There is no zarr[extra].

import numpy as np
import zarr

z = zarr.create_array("data.zarr", shape=(2000, 2000), chunks=(500, 500), dtype="float32",
                      fill_value=np.nan, dimension_names=("y", "x"), attributes={"units": "K"})
z[:500, :] = 1.0                       # covers 4 whole chunks
z = zarr.open_array("data.zarr", mode="r")

root = zarr.open_group("experiment.zarr", mode="w")
temp = root.create_group("observations").create_array(
    "temperature", shape=(365, 180, 360), chunks=(30, 90, 180), dtype="float32")
root.attrs["project"] = "Climate Study"
print(root.tree())                     # needs the optional `rich` package

Format v2 vs v3. zarr-python 3 reads and writes both, with async I/O for both:

| | v2 | v3 |

|---|---|---|

| Metadata | .zarray, .zgroup, .zattrs | One zarr.json per node |

| Default chunk keys | 0.0 | c/0/0 |

| Default compressor (zarr-python 3.1.5) | zarr.create_array: Zstd; Group.create_array / xarray: Blosc lz4 | Zstd |

| shards=, dimension_names= | ValueError | Supported |

| Consolidated metadata | .zmetadata | Inside root zarr.json (zarr-python extension, warns) |

Default to v3. Fixed-length U/S and structured dtypes raise UnstableSpecificationWarning in v3: other readers may fail.

Parallel writes: concurrent reads are safe, and so are writers that each cover whole, distinct chunks (shards when sharded), in threads or processes. Two writers in one chunk or shard each rewrite the whole object, so the later one erases the other's values. zarr-python 3 has no synchronizers.

Common Mistakes

| Mistake | Fix |

|---------|-----|

| FSStore, DirectoryStore, LRUStoreCache: AttributeError | FsspecStore.from_url(...), LocalStore or a path string |

| zarr.ProcessSynchronizer: AttributeError; synchronizer= does nothing but warn | Partition writes on chunk or shard boundaries |

| create_array(compressor=...), or z.compressor on v3: TypeError | compressors=, z.compressors |

| len(z); z.resize(200, 10): TypeError | z.shape[0]; z.resize((200, 10)) |

| chunks= larger than shards=: ValueError | shards is a multiple of chunks |

| z["field"] on a structured array: TypeError | Read records first: z[0:1000]["field"] |

| mode="w" to edit an existing store | mode="r+" or "a" |

Resources

| Resource | Load when |

|----------|-----------|

| references/patterns.md | Hierarchies, appending, indexing, sharding, parallel writes |

| references/examples.md | CF-style dataset, multi-instrument store, structured dtypes |

| references/common-issues.md | Format confusion, lost attributes, OOM, lost writes |

| python assets/zarr-quickstart.py | Runnable demo in a temporary directory |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.