Skip to content

Commit

Permalink
Add ability to provide AWS Session Token and Error on no files found (#7
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ciaransweet authored Jul 26, 2024
1 parent 341911f commit b4b5de8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/hazard/docs_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class DocStore:
# environment variable names:
__access_key = "OSC_S3_ACCESS_KEY_DEV"
__secret_key = "OSC_S3_SECRET_KEY_DEV"
__token = "OSC_S3_TOKEN_DEV"
__S3_bucket = "OSC_S3_BUCKET_DEV" # e.g. redhat-osc-physical-landing-647521352890

def __init__(
Expand All @@ -42,7 +43,11 @@ def __init__(
if fs is None:
access_key = os.environ.get(self.__access_key, None)
secret_key = os.environ.get(self.__secret_key, None)
fs = s3fs.S3FileSystem(key=access_key, secret=secret_key)
token = os.environ.get(self.__token, None)
if token:
fs = s3fs.S3FileSystem(key=access_key, secret=secret_key, token=token)
else:
fs = s3fs.S3FileSystem(key=access_key, secret=secret_key)

self._fs = fs
if type(self._fs) == s3fs.S3FileSystem: # noqa: E721 # use isinstance?
Expand Down
16 changes: 12 additions & 4 deletions src/hazard/sources/osc_zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@


class OscZarr(ReadWriteDataArray):
__access_key = "OSC_S3_ACCESS_KEY_DEV"
__secret_key = "OSC_S3_SECRET_KEY_DEV"
__token = "OSC_S3_TOKEN_DEV"

def __init__(
self,
bucket: str = default_dev_bucket,
Expand All @@ -37,10 +41,14 @@ def __init__(
if store is None:
if s3 is None:
# zarr_utilities.load_dotenv() # to load environment variables
s3 = s3fs.S3FileSystem(
key=os.environ.get("OSC_S3_ACCESS_KEY_DEV", None),
secret=os.environ.get("OSC_S3_SECRET_KEY_DEV", None),
)
access_key = os.environ.get(self.__access_key, None)
secret_key = os.environ.get(self.__secret_key, None)
token = os.environ.get(self.__token, None)
if token:
s3 = s3fs.S3FileSystem(key=access_key, secret=secret_key, token=token)
else:
s3 = s3fs.S3FileSystem(key=access_key, secret=secret_key)

group_path = str(PurePosixPath(bucket, prefix, "hazard.zarr"))
store = s3fs.S3Map(root=group_path, s3=s3, check=False)

Expand Down
6 changes: 6 additions & 0 deletions src/hazard/sources/ukcp18_rcp85.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def open_dataset_year(
files_available_for_quantity: List[str] = self._get_files_available_for_quantity_and_year(
gcm, scenario, quantity, year
)

if not files_available_for_quantity:
raise Exception(
f"No UKCP18 files available for: gcm:{gcm}, scenario:{scenario}, quantity:{quantity}, year:{year}"
)

all_data_from_files = self._combine_all_files_data(files_available_for_quantity)
only_data_for_year = all_data_from_files.sel(time=str(year))
reprojected = self._reproject_quantity(only_data_for_year, quantity)
Expand Down

0 comments on commit b4b5de8

Please sign in to comment.