This page walks through the full workflow: build a pyramid from the Xarray air_temperature tutorial dataset, write it to a Zarr store, and open it back up.
Load and assign a CRS
create_pyramid needs to know the dataset's coordinate reference system (CRS). xproj adds a .proj accessor for assigning one.
create_pyramid is lazy. It returns a plan and writes nothing until you call write. levels is the total number of levels. Level 0 is the original resolution, and each level after it is 2x coarser along each spatial dimension.
[{'schema_url': 'https://raw.githubusercontent.com/zarr-conventions/proj/refs/tags/v0.1/schema.json', 'spec_url': 'https://github.com/zarr-conventions/proj/blob/v0.1/README.md', 'uuid': 'f17cb550-5864-4468-aeb7-f3180cfb622f', 'name': 'proj', 'description': 'Coordinate reference system information for geospatial data'}, {'schema_url': 'https://raw.githubusercontent.com/zarr-conventions/spatial/refs/tags/v0.1/schema.json', 'spec_url': 'https://github.com/zarr-conventions/spatial/blob/v0.1/README.md', 'uuid': '689b58e2-cf7b-45e0-9fff-9cfc0883d6b4', 'name': 'spatial', 'description': 'Spatial coordinate information'}]
proj:code :
EPSG:4326
proj:wkt2 :
GEOGCRS["WGS 84",ENSEMBLE["World Geodetic System 1984 ensemble",MEMBER["World Geodetic System 1984 (Transit)"],MEMBER["World Geodetic System 1984 (G730)"],MEMBER["World Geodetic System 1984 (G873)"],MEMBER["World Geodetic System 1984 (G1150)"],MEMBER["World Geodetic System 1984 (G1674)"],MEMBER["World Geodetic System 1984 (G1762)"],MEMBER["World Geodetic System 1984 (G2139)"],MEMBER["World Geodetic System 1984 (G2296)"],ELLIPSOID["WGS 84",6378137,298.257223563,LENGTHUNIT["metre",1]],ENSEMBLEACCURACY[2.0]],PRIMEM["Greenwich",0,ANGLEUNIT["degree",0.0174532925199433]],CS[ellipsoidal,2],AXIS["geodetic latitude (Lat)",north,ORDER[1],ANGLEUNIT["degree",0.0174532925199433]],AXIS["geodetic longitude (Lon)",east,ORDER[2],ANGLEUNIT["degree",0.0174532925199433]],USAGE[SCOPE["Horizontal component of 3D system."],AREA["World."],BBOX[-90,-180,90,180]],ID["EPSG",4326]]
[{'schema_url': 'https://raw.githubusercontent.com/zarr-conventions/proj/refs/tags/v0.1/schema.json', 'spec_url': 'https://github.com/zarr-conventions/proj/blob/v0.1/README.md', 'uuid': 'f17cb550-5864-4468-aeb7-f3180cfb622f', 'name': 'proj', 'description': 'Coordinate reference system information for geospatial data'}, {'schema_url': 'https://raw.githubusercontent.com/zarr-conventions/spatial/refs/tags/v0.1/schema.json', 'spec_url': 'https://github.com/zarr-conventions/spatial/blob/v0.1/README.md', 'uuid': '689b58e2-cf7b-45e0-9fff-9cfc0883d6b4', 'name': 'spatial', 'description': 'Spatial coordinate information'}]
proj:code :
EPSG:4326
proj:wkt2 :
GEOGCRS["WGS 84",ENSEMBLE["World Geodetic System 1984 ensemble",MEMBER["World Geodetic System 1984 (Transit)"],MEMBER["World Geodetic System 1984 (G730)"],MEMBER["World Geodetic System 1984 (G873)"],MEMBER["World Geodetic System 1984 (G1150)"],MEMBER["World Geodetic System 1984 (G1674)"],MEMBER["World Geodetic System 1984 (G1762)"],MEMBER["World Geodetic System 1984 (G2139)"],MEMBER["World Geodetic System 1984 (G2296)"],ELLIPSOID["WGS 84",6378137,298.257223563,LENGTHUNIT["metre",1]],ENSEMBLEACCURACY[2.0]],PRIMEM["Greenwich",0,ANGLEUNIT["degree",0.0174532925199433]],CS[ellipsoidal,2],AXIS["geodetic latitude (Lat)",north,ORDER[1],ANGLEUNIT["degree",0.0174532925199433]],AXIS["geodetic longitude (Lon)",east,ORDER[2],ANGLEUNIT["degree",0.0174532925199433]],USAGE[SCOPE["Horizontal component of 3D system."],AREA["World."],BBOX[-90,-180,90,180]],ID["EPSG",4326]]
[{'schema_url': 'https://raw.githubusercontent.com/zarr-conventions/proj/refs/tags/v0.1/schema.json', 'spec_url': 'https://github.com/zarr-conventions/proj/blob/v0.1/README.md', 'uuid': 'f17cb550-5864-4468-aeb7-f3180cfb622f', 'name': 'proj', 'description': 'Coordinate reference system information for geospatial data'}, {'schema_url': 'https://raw.githubusercontent.com/zarr-conventions/spatial/refs/tags/v0.1/schema.json', 'spec_url': 'https://github.com/zarr-conventions/spatial/blob/v0.1/README.md', 'uuid': '689b58e2-cf7b-45e0-9fff-9cfc0883d6b4', 'name': 'spatial', 'description': 'Spatial coordinate information'}]
proj:code :
EPSG:4326
proj:wkt2 :
GEOGCRS["WGS 84",ENSEMBLE["World Geodetic System 1984 ensemble",MEMBER["World Geodetic System 1984 (Transit)"],MEMBER["World Geodetic System 1984 (G730)"],MEMBER["World Geodetic System 1984 (G873)"],MEMBER["World Geodetic System 1984 (G1150)"],MEMBER["World Geodetic System 1984 (G1674)"],MEMBER["World Geodetic System 1984 (G1762)"],MEMBER["World Geodetic System 1984 (G2139)"],MEMBER["World Geodetic System 1984 (G2296)"],ELLIPSOID["WGS 84",6378137,298.257223563,LENGTHUNIT["metre",1]],ENSEMBLEACCURACY[2.0]],PRIMEM["Greenwich",0,ANGLEUNIT["degree",0.0174532925199433]],CS[ellipsoidal,2],AXIS["geodetic latitude (Lat)",north,ORDER[1],ANGLEUNIT["degree",0.0174532925199433]],AXIS["geodetic longitude (Lon)",east,ORDER[2],ANGLEUNIT["degree",0.0174532925199433]],USAGE[SCOPE["Horizontal component of 3D system."],AREA["World."],BBOX[-90,-180,90,180]],ID["EPSG",4326]]
GEOGCRS["WGS 84",ENSEMBLE["World Geodetic System 1984 ensemble",MEMBER["World Geodetic System 1984 (Transit)"],MEMBER["World Geodetic System 1984 (G730)"],MEMBER["World Geodetic System 1984 (G873)"],MEMBER["World Geodetic System 1984 (G1150)"],MEMBER["World Geodetic System 1984 (G1674)"],MEMBER["World Geodetic System 1984 (G1762)"],MEMBER["World Geodetic System 1984 (G2139)"],MEMBER["World Geodetic System 1984 (G2296)"],ELLIPSOID["WGS 84",6378137,298.257223563,LENGTHUNIT["metre",1]],ENSEMBLEACCURACY[2.0]],PRIMEM["Greenwich",0,ANGLEUNIT["degree",0.0174532925199433]],CS[ellipsoidal,2],AXIS["geodetic latitude (Lat)",north,ORDER[1],ANGLEUNIT["degree",0.0174532925199433]],AXIS["geodetic longitude (Lon)",east,ORDER[2],ANGLEUNIT["degree",0.0174532925199433]],USAGE[SCOPE["Horizontal component of 3D system."],AREA["World."],BBOX[-90,-180,90,180]],ID["EPSG",4326]]
importmatplotlib.pyplotaspltdefplot(mode):levels=["0",str(len(dt.children)-1)]# finest and coarsestfig,axes=plt.subplots(1,2,figsize=(10,3.5),layout="constrained")forax,levelinzip(axes,levels):im=dt[level].air.isel(time=0).plot(ax=ax,vmin=230,vmax=305,cmap=f"warm_{mode}",add_colorbar=False)ax.set_title(f"level {level}")fig.colorbar(im,ax=axes,label="air temperature [K]")returnfigshow(plot)
pyramid.write accepts a local path, an Obstore store, or an Icechunk store. Obstore and Icechunk can write to either local disk or cloud object storage.
fromobstore.storeimportfrom_urlfromzarr.storageimportObjectStorestore=ObjectStore(from_url("s3://<your_bucket>/air.zarr",region="us-west-2"))# local: store = ObjectStore(from_url("file:///absolute/path/to/air.zarr", mkdir=True))pyramid.write(store,mode="w")
Single-resolution stores
Small datasets often don't need a pyramid. zarr-layer can render a single-resolution store as long as its chunks are sized for the web. attach_geozarr_metadata adds the GeoZarr attributes, and recommend_encoding returns the same chunking that create_pyramid uses for each level: