Command Palette

Search for a command to run...

UnylyUnyly
Назад к скиллам

data-migration

БесплатноЗапускает вложенные скриптыНе проверен

Use when converting HDF5 (.h5) or NetCDF (.nc) files to Zarr, migrating scientific datasets or a Zarr v2 store to Zarr v3, or building a virtual Zarr store…

uw-ssecот uw-ssec★ 33 звезды на GitHub

Об этом скилле

Data Migration to Zarr

Copies HDF5 and NetCDF data into Zarr, upgrades Zarr v2 stores to v3, or builds virtual Zarr references over files that stay where they are.

Core principle: a migration is done when the target's decoded values, attributes and time coordinate match the source, not when to_zarr returns. Most silent damage comes from encoding=, which replaces the source variable's dtype, packing and fill value instead of adding to them.

When to Use

  • Converting .h5 or .nc files into a Zarr store, or a Zarr v2 store into v3
  • Serving an archive as Zarr without copying it (VirtualiZarr, kerchunk, Icechunk)
  • A migrated store has phony_dim_* names, lost packing or fill values, wrong dates, the source's tiny chunks, or failed comparisons

When NOT to use:

  • Rechunking a store that is already Zarr: use zarr-chunk-optimization:rechunking.
  • Everyday xarray reads, appends and region writes: zarr-xarray-integration.
  • Codecs: compression-codecs. Cloud stores and credentials: cloud-storage-backends.

Quick Reference

Source Route Code
One file with named dimensions xr.open_dataset(path, chunks=...), to_zarr Below
Many NetCDF files xr.open_mfdataset, .chunk(...) the result, to_zarr patterns.md Pattern 2
HDF5 groups xarray can't model h5py walk, zarr.from_array per dataset Pattern 4
Zarr v2 store Replace the v2 codec encoding, to_zarr(zarr_format=3) Pattern 7
Too large to copy VirtualiZarr to kerchunk JSON or Icechunk Pattern 3
Thousands of files, restartable Append per file with a checkpoint Pattern 5

Core Pattern

import numpy as np
import xarray as xr

KEEP = ("dtype", "scale_factor", "add_offset", "_FillValue", "missing_value")


def cf_encoding(var):
    """The source packing and fill keys, which encoding= would otherwise drop."""
    enc = {k: v for k, v in var.encoding.items() if k in KEEP}
    if var.encoding.get("_Unsigned") == "true":  # zarr rejects _Unsigned: store the unsigned dtype
        signed = np.dtype(enc["dtype"])
        enc["dtype"] = np.dtype(f"u{signed.itemsize}")
        for k in ("_FillValue", "missing_value"):
            if k in enc:
                enc[k] = np.asarray(enc[k], signed).astype(enc["dtype"])
    return enc


target = {"time": 30, "lat": 90, "lon": 180}
ds = xr.open_dataset("climate_model_output.nc", chunks=target)  # name every dimension
encoding = {
    name: cf_encoding(var) | {"chunks": tuple(target[d] for d in var.dims)}
    for name, var in ds.data_vars.items() if set(var.dims) <= set(target)
}
ds.to_zarr("climate_output.zarr", mode="w-", encoding=encoding)

A dimension left out of chunks= keeps the file's HDF5 chunks, and to_zarr can then raise would overlap multiple Dask chunks. encoding chunks are a tuple in dimension order. Coordinates stay out of encoding=, so time keeps its units and calendar. Without missing_value, masked values came back as 0; without the unsigned dtype, 200 came back as -56.

Validate Before Deleting the Source

def lost_or_changed(a, b):
    return {k for k in a if k not in b or not np.array_equal(a[k], b[k])}


src = xr.open_dataset("climate_model_output.nc", chunks=target)
dst = xr.open_zarr("climate_output.zarr")
xr.testing.assert_allclose(src, dst)  # names, sizes, coords, values; NaN == NaN; chunk by chunk
assert not (bad := lost_or_changed(src.attrs, dst.attrs)), f"global attributes {bad}"
for name in src.variables:  # assert_allclose ignores attributes
    assert not (bad := lost_or_changed(src[name].attrs, dst[name].attrs)), f"{name} attributes {bad}"

Use xr.testing.assert_equal when no dtype was narrowed. Per-file checks: examples.md Example 4.

Bundled Script

python assets/hdf5-to-zarr-migration.py builds a 47 MB HDF5 file with dimension scales in a temp directory, converts it, checks it, and exits 1 on any failed check.

Common Mistakes

Mistake Fix
encoding chunks as a dict TypeError: ... must be an int or a tuple of ints. Use a tuple.
encoding={"v": {"chunks": ...}} alone Replaces the source encoding: packed int16 became float64. Merge the KEEP keys. All of var.encoding raises unexpected encoding parameters.
HDF5 without dimension scales Dimensions are phony_dim_N and chunks={"time": 30} is ignored. Rename them or attach scales.
HDF5 fillvalue without _FillValue Not masked, so sentinels stay numbers. Apply .where(...) first.
open_mfdataset(..., chunks={"time": 30}) Chunks are per file, so to_zarr raises would overlap multiple Dask chunks. .chunk(...) the combined dataset.
.chunk(...) on data read from Zarr The source encoding["chunks"] wins silently. Pop chunks, shards and preferred_chunks.
rechunker 0.5.4 pins zarr<3,>=2.11. See zarr-chunk-optimization:rechunking.
`z
Полный SKILL.md на GitHub

Установить data-migration в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Без ограничений — скилл может использовать любой инструмент.

Вложенные файлы

assets/hdf5-to-zarr-migration.pyreferences/common-issues.mdreferences/examples.mdreferences/patterns.md

FAQ

Что делает скилл data-migration?

Use when converting HDF5 (.h5) or NetCDF (.nc) files to Zarr, migrating scientific datasets or a Zarr v2 store to Zarr v3, or building a virtual Zarr store over existing files with VirtualiZarr or kerchunk. Also use when a converted store lost scale_factor, _FillValue or time units, kept the old chunk layout, shows phony_dim names, or fails validation against its source.

Как установить скилл data-migration?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл data-migration запускает скрипты?

Да, скилл несёт исполняемые скрипты. Проверь исходник перед установкой.

Похожие скиллы

Сравнить data-migration с