Chunking strategy

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

Chunking strategy is an AI agent skill from the GitHub repository uw-ssec/rse-plugins. Use when choosing or tuning Zarr chunk shapes for a multi-dimensional scientific array on S3, GCS or local disk, when map reads or time-series reads from a Zarr store are slow or run out of memory, or when comparing candidate chunk configurations by wall-clock time, peak memory or performance bias (

How to use this skill

Skill file

Chunking Strategy Benchmarking

Benchmarks candidate Zarr chunk shapes against the reads a dataset actually serves, measuring wall-clock time and peak memory before any rechunk.

Core principle: a read fetches and decompresses every chunk it touches, whole. Map reads want chunks short in time and wide in space, time-series reads the opposite, so measure candidates instead of guessing. Method: Nguyen et al. (2023), DOI: 10.1002/essoar.10511054.2.

When to Use

When NOT to use:

Quick Reference

| Read pattern (dims time, lat, lon) | xarray (positional) | Script flag | Shape that favors it (float32) |

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

| Map at one time step | da.isel(time=t) | --slice-dims 0 | (1, 721, 1440), 4.2 MB |

| Time series at one point | da.isel(lat=i, lon=j) | --traverse-dims 0 | (1000, 32, 32), 4.1 MB |

| Spectrum (dims time, freq, baseline) | da.isel(time=t, baseline=b) | --traverse-dims 1 | (8, 2048, 64), 4.2 MB |

Running the Benchmark

scripts/benchmark_runner.py times reads of the store's existing layout. --configs is only a label copied into the output; it does not rechunk. Each candidate needs its own store (a sample rechunked with rechunking, or generated with synthetic-data) and its own run:

python scripts/benchmark_runner.py \
  --dataset /scratch/era5_sample_c1000x32x32.zarr \
  --configs "1000,32,32" \
  --slice-dims 0 --traverse-dims 0 \
  --runs 5 --output results_c1000x32x32.json

Workflow

  1. Map each weighted read pattern to slice or traverse plus a dim index.
  2. Pick 3–5 candidates (current layout, one per dominant pattern, one balanced), varying one dim at a time. Drop any over the memory budget.
  3. Build one store per candidate on a sample where every pattern touches 10+ chunks.
  4. Benchmark each store. If std/mean > 0.3, rerun with --runs 10 or more before comparing.
  5. Compute PB and weighted score; fill assets/report-template.md or hand off to performance-reporting.

Common Mistakes

| Mistake | Fix |

|---|---|

| Passing several --configs against one store and reading the differences as chunk effects | Every run reads the same layout. One store per candidate |

| --traverse-dims 1 2 on (time, lat, lon) to get time series | That reads lines along lat and lon. A time series is --traverse-dims 0 |

| ds.sel(time=42) for "time step 42" | sel matches labels: KeyError on datetime coords. Use isel(time=42) |

| sel(lat=slice(30, 60), lon=slice(-120, -60)) on ERA5-style grids | Latitude descends and longitude is 0–360, so both return empty. Use lat=slice(60, 30), lon=slice(240, 300) |

| ds.chunk({...}).to_zarr(...) to build a candidate store | The source's encoding["chunks"] wins and the old layout is written. Delete encoding["chunks"] and encoding["preferred_chunks"] per variable first |

| Treating a Zarr v3 shard as the read unit | The inner chunk is the read unit; the shard is the storage object. Size and benchmark inner chunks for reads |

| Expecting Kerchunk or VirtualiZarr to provide a second layout | They reference existing chunks only. A new layout needs a rechunk |

References

| Reference | Load when |

|---|---|

| access-patterns.md | Mapping workflows to spatial, time-series and spectral reads |

| memory-constraints.md | Memory budgets, OOM, all-or-nothing chunk reads |

| benchmarking-methodology.md | Run counts, cache clearing, building candidate stores, pitfalls |

| cloud-storage-patterns.md | S3/GCS latency, request cost, sharding, caching |

| performance-interpretation.md | Reading results, PB, weighted scores, recommendations |

| nguyen-2023.md | Research basis for the method |

Limitations

Read performance only (no write, rechunk or codec benchmarking), one variable per store.

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.