|
| 1 | +# Zarr |
| 2 | + |
| 3 | +[Zarr-Python](https://zarr.readthedocs.io/en/stable/index.html) is a Python library for reading and writing the [Zarr file format](https://zarr.dev/) for N-dimensional arrays. Zarr-Python is often used in conjunction with [Xarray](https://xarray.dev/). |
| 4 | + |
| 5 | +Zarr datasets are often very large and thus stored in object storage for cost effectiveness. As of Zarr-Python version 3.0.7 and later, you can [use Obstore as a backend](https://zarr.readthedocs.io/en/stable/user-guide/storage.html#object-store) for Zarr-Python. For large queries this [can be significantly faster](https://github.com/maxrjones/zarr-obstore-performance) than the default fsspec-based backend. |
| 6 | + |
| 7 | +## Example |
| 8 | + |
| 9 | +!!! note |
| 10 | + |
| 11 | + This example is also [available on Github](https://github.com/developmentseed/obstore/blob/main/examples/zarr/README.md) if you'd like to test it out locally. |
| 12 | + |
| 13 | +```py |
| 14 | +import matplotlib.pyplot as plt |
| 15 | +import pystac_client |
| 16 | +import xarray as xr |
| 17 | +from zarr.storage import ObjectStore |
| 18 | + |
| 19 | +from obstore.auth.planetary_computer import PlanetaryComputerCredentialProvider |
| 20 | +from obstore.store import AzureStore |
| 21 | + |
| 22 | +# These first lines are specific to Zarr stored in the Microsoft Planetary |
| 23 | +# Computer. We use pystac-client to find the metadata for this specific Zarr |
| 24 | +# store. |
| 25 | +catalog = pystac_client.Client.open( |
| 26 | + "https://planetarycomputer.microsoft.com/api/stac/v1/", |
| 27 | +) |
| 28 | +collection = catalog.get_collection("daymet-daily-hi") |
| 29 | +asset = collection.assets["zarr-abfs"] |
| 30 | + |
| 31 | +# We construct an AzureStore because this Zarr dataset is stored in Azure |
| 32 | +# storage |
| 33 | +azure_store = AzureStore( |
| 34 | + credential_provider=PlanetaryComputerCredentialProvider.from_asset(asset), |
| 35 | +) |
| 36 | + |
| 37 | +# Next we use the Zarr ObjectStorage adapter and pass it to xarray. |
| 38 | +zarr_store = ObjectStore(azure_store, read_only=True) |
| 39 | +ds = xr.open_dataset(zarr_store, consolidated=True, engine="zarr") |
| 40 | + |
| 41 | +# And plot with matplotlib |
| 42 | +fig, ax = plt.subplots(figsize=(12, 12)) |
| 43 | +ds.sel(time="2009")["tmax"].mean(dim="time").plot.imshow(ax=ax, cmap="inferno") |
| 44 | +fig.savefig("zarr-example.png") |
| 45 | +``` |
| 46 | + |
| 47 | +This plots: |
| 48 | + |
| 49 | + |
0 commit comments