import numpy as np
import rasterio
import rasterio as rio
from rasterio.session import AWSSession
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 load_data():
= "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 with rasterio.open(src) as src_da:
= src_da.read(1, masked=True)
ma = ma.astype("float32", casting="unsafe")
ma 0], out=ma, casting="unsafe")
np.multiply(ma, src_da.scales[0], out=ma, casting="unsafe")
np.add(ma, src_da.offsets[return ma.filled(fill_value=np.nan)
if __name__ == "__main__":
= configure_auth()
rio_env = load_data()
da __exit__() rio_env.