Skip to content

Repository files navigation

topozarr

Python library to create multiscale Zarr stores for usage with zarr-layer.

Tries to follow the GeoZarr spec, which is composed of these zarr-conventions:

  • multiscales — pyramid structure and resolution levels
  • proj: — coordinate reference system (CRS)
  • spatial: — affine transform, bounding box, and dimension names

Warning: experimental

Usage

Installation

uv add topozarr
# or
pip install topozarr

Multiscales are computed by topozarr-core, a small Rust kernel installed automatically as a wheel. The tutorial extra includes everything needed to run the examples below:

uv add 'topozarr[tutorial]'
# or
pip install 'topozarr[tutorial]'

Example

import xarray as xr
import xproj # for crs assignment
from topozarr import create_pyramid

# Load the air_temperature Xarray tutorial dataset
ds = xr.tutorial.open_dataset('air_temperature').drop_encoding()

# Assign a CRS
ds = ds.proj.assign_crs(spatial_ref="EPSG:4326")
print(ds)
pyramid = create_pyramid(
    ds,
    levels=2,
    x_dim="lon",
    y_dim="lat",
    method="mean",  # "mean" (default) | "max" | "min" | "sum" | "nearest"
)
print(pyramid.encoding)

# write
pyramid.write("pyramid.zarr")

levels is the total number of resolution levels, including the original. Level 0 is the original (highest) resolution; by default each subsequent level is coarsened by a factor of 2 per spatial dimension, so the last level is the coarsest.

create_pyramid returns a write plan; pyramid.write(store) does the work.

Not every dataset needs overviews. For a single-resolution (flat) geozarr group, attach_geozarr_metadata adds the convention attrs and recommend_encoding gives you the same chunk/shard heuristic to hand to to_zarr:

from topozarr import attach_geozarr_metadata, recommend_encoding

ds = attach_geozarr_metadata(ds, x_dim="lon", y_dim="lat")
ds.to_zarr(
    "flat.zarr",
    zarr_format=3,
    consolidated=False,
    encoding=recommend_encoding(ds, x_dim="lon", y_dim="lat"),
)

Documentation

Full docs at carbonplan.github.io/topozarr:

  • Usage — sparse pyramids (factors), visualization hints (layer_hints), chunking and sharding (and why sharding rules), and object-storage / Icechunk backends.
  • Design — the plan/execute split, chunk and shard heuristics, streaming memory model, and Rust kernel semantics.

Contributing

Clone the repo, install with the test dependency group, and run the tests:

git clone https://github.com/carbonplan/topozarr
cd topozarr
uv sync --group test
uv run pytest -n auto

Run conformance tests against the GeoZarr spec (requires the conformance group):

uv sync --group conformance
uv run pytest -n auto -m conformance

Lint and format:

uv run pre-commit run --all-files

To regenerate the demo datasets in S3 (requires AWS credentials), install the tutorial extra and run the build script:

uv sync --group test --extra tutorial
uv run python scripts/build_demo_data.py --help

Building from source requires a Rust toolchain for the topozarr-core kernel. See the contributing docs for conformance tests, linting, and demo-data scripts.

License

MIT — see the LICENSE file for details.

About Us

CarbonPlan is a nonprofit organization that uses data and science for climate action. We aim to improve the transparency and scientific integrity of climate solutions through open data and tools. Find out more at carbonplan.org or get in touch by opening an issue or sending us an email

About

Create multiscale Zarr stores for visualization

Resources

Code of conduct

Contributing

Security policy

Stars

27 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages