import argparse
import fsspec
import xarray as xr
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 (local storage, NetCDF File, H5NetCDF driver)
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 = f'earthaccess_data/{args["filename"]}'
src # Define source and target projection
= "EPSG:3857"
dstSRS = "EPSG:4326"
srcSRS = height = 256
width # Specify fsspec caching since default options don't work well for raster data
= {
fsspec_caching "cache_type": "none",
}= fsspec.filesystem("file")
fs with fs.open(src, **fsspec_caching) as f:
# Define ODC geobox for target tile
= GeoBox.from_bbox(te, dstSRS, shape=(height, width))
gbox # Open dataset
= xr.open_dataset(f, engine="h5netcdf", chunks={})[args["variable"]]
da if dataset == "gpm_imerg":
# Transpose and rename dataset dims to align with GDAL expectations
= (
da "lon": "x", "lat": "y"})
da.rename({"time", "y", "x")
.transpose(
.squeeze()
)# 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).load()
if __name__ == "__main__":
if "get_ipython" in dir():
# Just call warp_resample if running as a Jupyter Notebook
= warp_resample("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",
="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