import numpy as np
import rasterio
import rasterio as rio
from rasterio.session import AWSSession
from rasterio.warp import calculate_default_transform, reproject
Rasterio with NetCDF, VSIS3, and earthaccess
def configure_auth():
import boto3
import earthaccess
= earthaccess.login()
auth = auth.get_s3_credentials("PODAAC")
s3_credentials = boto3.Session(
session =s3_credentials["accessKeyId"],
aws_access_key_id=s3_credentials["secretAccessKey"],
aws_secret_access_key=s3_credentials["sessionToken"],
aws_session_token="us-west-2",
region_name
)= rio.Env(
rio_env
AWSSession(session),
)__enter__()
rio_env.return rio_env
def warp_resample():
= "podaac-ops-cumulus-protected"
bucket = "MUR-JPL-L4-GLOB-v4.1/20020601090000-JPL-L4_GHRSST-SSTfnd-MUR-GLOB-v02.0-fv04.1.nc"
input_uri = f"NETCDF:/vsis3/{bucket}/{input_uri}:analysed_sst"
src = "EPSG:3857"
dstSRS = "EPSG:4326"
srcSRS = height = 256
width with rasterio.open(src) as da:
= calculate_default_transform(
dst_transform, w, h
srcSRS,
dstSRS,
da.width,
da.height,*da.bounds,
=width,
dst_width=height,
dst_height
)= np.zeros((width, height), np.uint8)
destination return reproject(
1),
rasterio.band(da,
destination,=dstSRS,
dst_crs=dst_transform,
dst_transform )
if __name__ == "__main__":
= configure_auth()
rio_env
warp_resample()__exit__() rio_env.