33from typing import Any , Dict , List , Literal , Optional
44
55import numpy as np
6+ import rasterio
67from fastapi import HTTPException
78from pydantic import BaseModel
9+ from rasterio .session import AWSSession
810from rio_tiler .constants import WEB_MERCATOR_TMS
911from rio_tiler .io .rasterio import Reader
1012from rio_tiler .models import Info
1113from starlette .requests import Request
14+ from titiler .xarray .io import Reader as XarrayReader
1215
1316from titiler .cmr .backend import CMRBackend
1417from titiler .cmr .dependencies import ConceptID
1518from titiler .cmr .logger import logger
1619from titiler .cmr .reader import xarray_open_dataset
1720from titiler .cmr .settings import AuthSettings
1821from titiler .cmr .utils import get_concept_id_umm
19- from titiler .xarray .io import Reader as XarrayReader
2022
2123
2224class VariableInfo (BaseModel ):
@@ -201,9 +203,28 @@ def evaluate_xarray_compatibility(
201203 if not assets :
202204 raise ValueError ("No assets found for XarrayReader" )
203205
204- with xarray_open_dataset (assets [0 ]["url" ], auth = auth ) as ds :
206+ asset = assets [0 ]
207+
208+ s3_credentials = (
209+ src_dst .get_s3_credentials (endpoint )
210+ if src_dst .get_s3_credentials
211+ and (endpoint := asset .get ("s3_credentials_url" , None ))
212+ else None
213+ )
214+
215+ with xarray_open_dataset (
216+ asset ["url" ],
217+ auth = auth ,
218+ s3_credentials = {
219+ "key" : s3_credentials ["accessKeyId" ],
220+ "secret" : s3_credentials ["secretAccessKey" ],
221+ "token" : s3_credentials ["sessionToken" ],
222+ }
223+ if s3_credentials
224+ else None ,
225+ ) as ds :
205226 result = extract_xarray_metadata (ds )
206- result ["example_assets" ] = assets [ 0 ] ["url" ]
227+ result ["example_assets" ] = asset ["url" ]
207228 return result
208229
209230
@@ -250,11 +271,33 @@ def evaluate_rasterio_compatibility(
250271 if not assets :
251272 raise ValueError ("No assets found for MultiFilesBandsReader" )
252273
274+ s3_credentials = (
275+ src_dst .get_s3_credentials (endpoint )
276+ if src_dst .get_s3_credentials
277+ and (endpoint := assets [0 ].get ("s3_credentials_url" , None ))
278+ else None
279+ )
280+
253281 example_assets : Dict [str , str ] = assets [0 ]["url" ]
254282
255- with src_dst .reader (
256- input = list (example_assets .values ())[0 ], tms = src_dst .tms
257- ) as _src_dst :
283+ session = (
284+ AWSSession (
285+ aws_access_key_id = s3_credentials ["accessKeyId" ],
286+ aws_secret_access_key = s3_credentials ["secretAccessKey" ],
287+ aws_session_token = s3_credentials ["sessionToken" ],
288+ )
289+ if s3_credentials
290+ else None
291+ )
292+ with (
293+ rasterio .Env (
294+ session ,
295+ ),
296+ src_dst .reader (
297+ input = list (example_assets .values ())[0 ],
298+ tms = src_dst .tms ,
299+ ) as _src_dst ,
300+ ):
258301 info = _src_dst .info ()
259302
260303 return {
@@ -298,6 +341,7 @@ def evaluate_concept_compatibility(
298341 ** result ,
299342 )
300343 except (ValueError , HTTPException , OSError , KeyError ) as e :
344+ xarray_error = e
301345 logger .warning (f"XarrayReader failed: { e } " )
302346
303347 # Fall back to rasterio
@@ -309,7 +353,13 @@ def evaluate_concept_compatibility(
309353 ** result ,
310354 )
311355 except (ValueError , HTTPException , OSError , KeyError ) as e :
356+ rasterio_error = e
312357 logger .warning (f"MultiFilesBandsReader failed: { e } " )
313358
314359 # Both failed
315- raise HTTPException (400 , f"cannot parse concept_id { concept_id } " )
360+ raise HTTPException (
361+ 400 ,
362+ f"Could not open a sample granule for concept_id { concept_id } "
363+ "with either the rasterio or xarray backends.\n \n "
364+ f"xarray error: { xarray_error } \n \n rasterio_error: { rasterio_error } " ,
365+ )
0 commit comments