Pyramids via custom resampling methods

Pyramids via custom resampling methods#

In this example, we’ll show how to use pyramid_create to generate multi-scale pyramids using a custom resampling method.

[2]:
import dask  # noqa
import xarray as xr

from ndpyramid import pyramid_create

Setup output paths#

You can set the output path to object storage or a memory store, after changing the S3 URI to a location that you have write access to.

[3]:
S3 = False
if S3:
    output = "s3://carbonplan-scratch/pyramid_comparison/custom.zarr"
else:
    import zarr

    output = zarr.storage.MemoryStore()

Open input dataset#

In this example, we’ll use the Xarray tutorial dataset air_temperature.

[4]:
ds = xr.tutorial.load_dataset("air_temperature")

Generate pyramids#

We’ll use the pyramid_create function to generate a pyramid with two levels. This function can be used to apply a provided function to the dataset to create the individual pyramid levels.

[5]:
def sel_coarsen(ds, factor, dims, **kwargs):
    return ds.sel(**{dim: slice(None, None, factor) for dim in dims})


factors = [4, 2, 1]
pyramid = pyramid_create(
    ds,
    dims=("lat", "lon"),
    factors=factors,
    boundary="trim",
    func=sel_coarsen,
    method_label="slice_coarsen",
    type_label="pick",
)
pyramid.to_zarr(output, consolidated=True, mode="w")
/Users/nrhagen/miniforge3/envs/ndpyramid/lib/python3.12/site-packages/xarray/core/datatree_io.py:159: SerializationWarning: saving variable None with floating point data as an integer dtype without any _FillValue to use for NaNs
  ds.to_zarr(
/Users/nrhagen/miniforge3/envs/ndpyramid/lib/python3.12/site-packages/xarray/core/datatree_io.py:159: SerializationWarning: saving variable None with floating point data as an integer dtype without any _FillValue to use for NaNs
  ds.to_zarr(
/Users/nrhagen/miniforge3/envs/ndpyramid/lib/python3.12/site-packages/xarray/core/datatree_io.py:159: SerializationWarning: saving variable None with floating point data as an integer dtype without any _FillValue to use for NaNs
  ds.to_zarr(

Open and plot the result#

[8]:
dt = xr.open_datatree(output, engine="zarr")
dt["2"].ds.isel(time=0).air.plot()
[8]:
<matplotlib.collections.QuadMesh at 0x313c739e0>
../_images/examples_pyramid-create_9_1.png