import argparse
import numpy as np
import rasterio
from rasterio.vrt import WarpedVRT
Resampling with rasterio (Local storage, COG file, COG driver)
def warp_resample(dataset, zoom=0):
from common import target_extent
= target_extent[zoom]
te
# Define source and target projection
= "EPSG:4326"
srcSRS = "EPSG:3857"
dstSRS = height = 256
width
= f"s3://nasa-veda-scratch/resampling/{dataset}.tif"
src
with rasterio.open(src) as da:
with WarpedVRT(da, src_crs=srcSRS, crs=dstSRS) as vrt:
= vrt.window(*te)
dst_window
= vrt.read(window=dst_window, out_shape=(height, width), masked=True)
data # Mask and fill array
= data.astype("float32", casting="unsafe")
data 0], out=data, casting="unsafe")
np.multiply(data, da.scales[0], out=data, casting="unsafe")
np.add(data, da.offsets[return data
%%time
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.",
=["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