Skip to content

Instantly share code, notes, and snippets.

@dominikl
Created February 23, 2024 14:09
Show Gist options
  • Save dominikl/33bc2224e0013381808bf43477a92f0e to your computer and use it in GitHub Desktop.
Save dominikl/33bc2224e0013381808bf43477a92f0e to your computer and use it in GitHub Desktop.
import os
os.environ["ZARR_V3_EXPERIMENTAL_API"] = "1"
import zarr
import argparse
parser = argparse.ArgumentParser(description="Convert ome.zarr from zarr spec 2 to 3")
parser.add_argument("input", help="The input zarr")
parser.add_argument("output", nargs="?", help="Optional output zarr")
args = parser.parse_args()
if not args.output:
args.output = f"{args.input}3"
def copy_visitor(k, v):
if isinstance(v, zarr.hierarchy.Group):
print(f"Copy Group {k} from {args.input} to {args.output}")
out_group = out_store.create_group(k)
out_group.attrs.update(v.attrs)
elif isinstance(v, zarr.core.Array):
print(f"Copy Array {k} from {args.input} to {args.output}")
out_store.array(k, v)
else:
print(f"Can't handle {v}")
in_store = zarr.open(args.input, mode='r', zarr_version=2)
out_store = zarr.open(args.output, mode='w', zarr_version=3)
in_store.visititems(copy_visitor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment