import argparse
import xarray as xr
from icechunk import IcechunkStore, StorageConfig
from rasterio.warp import calculate_default_transform
Resampling with Rioxarray (S3 storage, NetCDF file, Zarr reader, icechunk virtualization)
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}-reference",
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 if dataset == "gpm_imerg":
# Transpose and rename dims to align with rioxarray expectations
= da.rename({"lon": "x", "lat": "y"}).transpose("time", "y", "x")
da # Set input dataset projection
= da.rio.write_crs(srcSRS)
da = da.rio.clip_box(
da *te,
=dstSRS,
crs
)# Define affine transformation from input to output dataset
= calculate_default_transform(
dst_transform, w, h
srcSRS,
dstSRS,
da.rio.width,
da.rio.height,*da.rio.bounds(),
=width,
dst_width=height,
dst_height
)# Reproject dataset
return da.rio.reproject(dstSRS, shape=(h, w), transform=dst_transform)
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