import argparse
import numpy as np
import xarray as xr
import xesmf as xe
from icechunk import IcechunkStore, StorageConfig
Resampling with XESMF (S3 storage, Zarr V3 store, Zarr reader with icechunk)¶
def _reconstruct_xesmf_weights(ds_w):
"""
Reconstruct weights into format that xESMF understands
Notes
-----
From ndpyramid - https://github.com/carbonplan/ndpyramid
"""
import sparse
import xarray as xr
= ds_w["col"].values - 1
col = ds_w["row"].values - 1
row = ds_w["S"].values
s = ds_w.attrs["n_out"], ds_w.attrs["n_in"]
n_out, n_in = np.stack([row, col])
crds return xr.DataArray(
=("out_dim", "in_dim"), name="weights"
sparse.COO(crds, s, (n_out, n_in)), dims )
def regrid(dataset, zoom=0):
from common import earthaccess_args # noqa: 402
= earthaccess_args[dataset]
args # Load pre-generated weights and target dataset
= StorageConfig.s3_from_env(
weights_storage ="nasa-veda-scratch",
bucket=f"resampling/test-weight-caching/{dataset}-weights-{zoom}",
prefix="us-west-2",
region
)= StorageConfig.s3_from_env(
target_storage ="nasa-veda-scratch",
bucket=f"resampling/test-weight-caching/{dataset}-target-{zoom}",
prefix="us-west-2",
region
)= IcechunkStore.open_existing(storage=weights_storage, mode="r")
weights_store = IcechunkStore.open_existing(storage=target_storage, mode="r")
target_store = _reconstruct_xesmf_weights(
weights =3, consolidated=False)
xr.open_zarr(weights_store, zarr_format
)= xr.open_zarr(target_store, zarr_format=3, consolidated=False)
grid # Open dataset
= StorageConfig.s3_from_env(
storage ="nasa-veda-scratch",
bucket=f"resampling/icechunk/{dataset}",
prefix="us-west-2",
region
)= IcechunkStore.open_existing(storage=storage, mode="r")
store = xr.open_zarr(store, zarr_format=3, consolidated=False)[args["variable"]]
da # Create XESMF regridder
= xe.Regridder(
regridder
da,
grid,"nearest_s2d",
=True,
periodic="nearest_s2d",
extrap_method=True,
ignore_degenerate=True,
reuse_weights=weights,
weights
)# Regrid dataset
return regridder(da)
if __name__ == "__main__":
if "get_ipython" in dir():
# Just call warp_resample if running as a Jupyter Notebook
= regrid("gpm_imerg")
da else:
# Configure dataset via argpase if running via CLI
= argparse.ArgumentParser(description="Set environment for the script.")
parser
parser.add_argument("--dataset",
="gpm_imerg",
defaulthelp="Dataset to resample.",
=["gpm_imerg", "mursst"],
choices
)
parser.add_argument("--zoom",
=0,
defaulthelp="Zoom level for tile extent.",
)= parser.parse_args()
user_args = regrid(user_args.dataset, int(user_args.zoom)) da