Skip to main content

Copy a zarr dataset while changing chunking - variable by variable

Project description

zarr-copy

zarr-copy logo

Copy a zarr dataset while optionally rechunking variables — variable by variable.

Installation

pip install -e .

Or with uv:

uv sync

Usage

zarr-copy [-h] [-v VAR [VAR ...]] [--rechunk DIM=SIZE [DIM=SIZE ...]]
          [--double-remap DIM] [--tmp-path DIR] [--transform FILE]
          PATH [PATH ...] DST

Positional arguments

Argument Description
PATH [PATH …] One or more source paths or S3 URIs. Multiple sources require --transform.
DST Path or S3 URI of the destination zarr group (always the last positional argument).

Optional arguments

Flag Description
-v VAR … Variable names to copy. Copies all variables when omitted.
--rechunk DIM=SIZE … Rechunk one or more dimensions, e.g. --rechunk time=1 lat=256.
--double-remap DIM Use the two-pass copy strategy for the given dimension to reduce peak memory usage.
--tmp-path DIR Directory for temporary files used by --double-remap. Defaults to the system temp dir.
--transform FILE Python config file defining output variable names and how to compute them (see below).

Examples

Copy all variables without rechunking:

zarr-copy /data/input.zarr /data/output.zarr

Copy selected variables and rechunk the time dimension:

zarr-copy /data/input.zarr /data/output.zarr -v temperature salinity --rechunk time=1

Copy from S3, rechunk multiple dimensions, and use the double-remap strategy:

zarr-copy s3://my-bucket/input.zarr /data/output.zarr \
  --rechunk time=1 lat=256 lon=256 \
  --double-remap time \
  --tmp-path /scratch

Write to S3 from a local source:

zarr-copy /data/input.zarr s3://my-bucket/output.zarr \
  --rechunk lat=256 lon=256

Increase spatial chunk sizes for better read performance along lat/lon:

zarr-copy /data/input.zarr /data/output.zarr --rechunk lat=256 lon=256

Optimise for time-series access by making the time dimension unchunked (one chunk per step):

zarr-copy /data/input.zarr /data/output.zarr --rechunk time=1

Rechunk a large dataset where the time axis is being heavily reordered, using /scratch for intermediate storage to cap memory use:

zarr-copy /data/input.zarr /data/output.zarr \
  -v u v w temperature \
  --rechunk time=1 level=1 lat=180 lon=360 \
  --double-remap time \
  --tmp-path /scratch

Notes

  • Rechunking requires _ARRAY_DIMENSIONS metadata on each array (standard for xarray/CF-convention zarr stores).
  • S3 access is handled transparently via s3fs.
  • The --double-remap strategy writes data to a temporary intermediate zarr (lz4-compressed) to avoid holding large in-memory buffers during rechunking.

Transform: rename and compute output variables

The --transform flag accepts a Python file that defines a module-level transform dict. Each key is the output variable name; each value is either:

  • a string — rename from the named source variable (simple copy), or
  • a callable — compute the output from one or more source variables whose names are taken from the callable's parameter names.

When --transform is provided the -v flag is ignored; all output variables are determined by the spec.

Transform config file example

# transform.py
transform = {
    "time": "time",                                   # rename only
    "lon":  "lon",
    "lat":  "lat",
    "rlut": lambda ttr: ttr / -3600,                  # single source
    "rsut": lambda tisr, tsr: (tisr - tsr) / 3600,   # two sources
    "pr":   lambda tp: tp * (1000 / 3600),
    "psl":  "msl",                                    # rename
    "ts":   "skt",
    "uas":  "10u",
}

Usage

zarr-copy /data/era5.zarr /data/cmip.zarr --transform transform.py

Combine with --rechunk to rename, compute, and rechunk in one pass:

zarr-copy /data/era5.zarr /data/cmip.zarr \
  --transform transform.py \
  --rechunk time=1 lat=256 lon=256

Merge variables from two source datasets into one output using a transform. The last positional argument is always the destination; sources are searched in order and the first match wins:

zarr-copy /data/era5_atm.zarr /data/era5_sfc.zarr /data/cmip.zarr \
  --transform transform.py \
  --rechunk time=1 lat=256 lon=256

SLURM: parallel rechunking on HPC

For large datasets, zarr-copy-slurm submits one SLURM job per variable so all variables are rechunked concurrently. It accepts the same rechunking flags as zarr-copy and forwards them to each job.

zarr-copy-slurm [-h] [-v VAR [VAR ...]] [--rechunk DIM=SIZE [DIM=SIZE ...]]
                [--double-remap DIM] [--tmp-path DIR] [--transform FILE]
                [--cpus N] [--mem MEM] [--walltime HH:MM:SS] [--log-dir DIR]
                [--dry-run]
                PATH [PATH ...] DST

Examples

Preview the generated job scripts without submitting (useful for checking before committing to a long run):

zarr-copy-slurm /data/input.zarr /data/output.zarr \
  --rechunk time=1 lat=256 lon=256 \
  --dry-run

Submit one job per variable, rechunking the time dimension into single steps:

zarr-copy-slurm /data/input.zarr /data/output.zarr --rechunk time=1

Submit jobs for selected variables only, with custom resource requests:

zarr-copy-slurm /data/input.zarr /data/output.zarr \
  -v temperature salinity u v \
  --rechunk time=1 lat=256 lon=256 \
  --cpus 8 --mem 64G --walltime 08:00:00

Large dataset with the double-remap strategy, temporaries on scratch:

zarr-copy-slurm /data/input.zarr /data/output.zarr \
  --rechunk time=1 level=1 lat=180 lon=360 \
  --double-remap time \
  --tmp-path /scratch/$USER \
  --log-dir /data/logs

Submit one job per output variable defined by a transform spec:

zarr-copy-slurm /data/era5.zarr /data/cmip.zarr \
  --transform transform.py \
  --rechunk time=1 lat=256 lon=256

Without installing the package, run directly with uvx:

uvx --from zarr-copy zarr-copy-slurm /data/input.zarr /data/output.zarr \
  --rechunk time=1 --dry-run

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zarr_copy-0.7.0.tar.gz (189.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zarr_copy-0.7.0-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file zarr_copy-0.7.0.tar.gz.

File metadata

  • Download URL: zarr_copy-0.7.0.tar.gz
  • Upload date:
  • Size: 189.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for zarr_copy-0.7.0.tar.gz
Algorithm Hash digest
SHA256 6c5fb2287de9cf422c4da5194402b2796cbfd9f988bfe85a66bedad1e0105d4d
MD5 1c44a91c9784c9ba6e1c2558cc5fa944
BLAKE2b-256 78df072de81e4e858e10bc6789a3f59e2068f20e22c57a23026a2fa6c36b15a1

See more details on using hashes here.

File details

Details for the file zarr_copy-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: zarr_copy-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for zarr_copy-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 abb8b8f2c302b268a4684cadcf503f4d14a7162052068bd65291d20b02a4d9bb
MD5 4d00bc2ee626a7b96d564c26eee5c950
BLAKE2b-256 11af02f4238086ac4e243c79c66182eae6632697dde2253b6ef4c091cfd10fc5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page