import argparse
import xarray as xr
from icechunk import IcechunkStore, StorageConfig
from odc.geo.geobox import GeoBox
from odc.geo.geom import Geometry
from odc.geo.xr import crop, xr_reproject
from shapely.geometry import box
Resampling with ODC-geo (S3 storage, Zarr V3 store, Zarr reader with icechunk)
def warp_resample(dataset, zoom=0):
from common import earthaccess_args, target_extent
= target_extent[zoom]
te
# Define filepath, driver, and variable information
= earthaccess_args[dataset]
args # 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 # Define source and target projection
= "EPSG:3857"
dstSRS = "EPSG:4326"
srcSRS = height = 256
width # Define ODC geobox for target tile
= GeoBox.from_bbox(te, dstSRS, shape=(height, width))
gbox if dataset == "gpm_imerg":
# Transpose and rename dataset dims to align with GDAL expectations
= da.rename({"lon": "x", "lat": "y"}).squeeze()
da # Assign input projection
= da.odc.assign_crs(srcSRS)
da # Crop dataset to tile bounds
= box(*te)
bbox = Geometry(bbox, "EPSG:3857")
geom = crop(da, geom)
da # Load into memory to avoid topology error
da.load()# Reproject dataset
return xr_reproject(da, gbox, tight=True).load()
if __name__ == "__main__":
if "get_ipython" in dir():
# Just call warp_resample if running as a Jupyter Notebook
= warp_resample("mursst")
da else:
# Configure dataset via argpase if running via CLI
= argparse.ArgumentParser(description="Set environment for the script.")
parser
parser.add_argument("--dataset",
="mursst",
defaulthelp="Dataset to resample.",
=["gpm_imerg", "mursst"],
choices
)
parser.add_argument("--zoom",
=0,
defaulthelp="Zoom level for tile extent.",
)= parser.parse_args()
user_args = warp_resample(user_args.dataset, int(user_args.zoom)) da