Skip to content

Commit

Permalink
concat hazard objects and update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinGebhart committed Sep 27, 2024
1 parent 94732e6 commit 9be7845
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1,813 deletions.
51 changes: 28 additions & 23 deletions climada_petals/hazard/copernicus_forecast/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,39 +462,44 @@ def save_index_to_hazard(
os.makedirs(output_dir, exist_ok=True)

try:
# open input file
ds = xr.open_dataset(input_file_name)
ds["step"] = xr.DataArray(
[f"{date}-01" for date in ds["step"].values], dims=["step"]
)
ds["step"] = pd.to_datetime(ds["step"].values)
ensemble_members = ds["number"].values
# check if file already exists
file_path = f"{output_dir}/hazard_{hazard_type}_" \
f"{area_str}_{year}{month:02d}.hdf5"
if os.path.exists(file_path) and not overwrite:
self.logger.info(f'hazard file {file_path} already exists.')

Check warning on line 469 in climada_petals/hazard/copernicus_forecast/handler.py

View check run for this annotation

Jenkins - WCR / Pylint

logging-fstring-interpolation

NORMAL: Use lazy % formatting in logging functions
Raw output
no description found

for member in ensemble_members:
# check if data already exists
file_path = f"{output_dir}/hazard_{hazard_type}_member_{member}_" \
f"{area_str}_{year}{month:02d}.hdf5"
if os.path.exists(file_path) and not overwrite:
self.logger.info(f'hazard file {file_path} already exists.')
else:
# open input file
ds = xr.open_dataset(input_file_name)

Check warning on line 473 in climada_petals/hazard/copernicus_forecast/handler.py

View check run for this annotation

Jenkins - WCR / Pylint

invalid-name

LOW: Variable name "ds" doesn't conform to '(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$' pattern
Raw output
Used when the name doesn't match the regular expression associated to its type(constant, variable, class...).
ds["step"] = xr.DataArray(
[f"{date}-01" for date in ds["step"].values], dims=["step"]
)
ds["step"] = pd.to_datetime(ds["step"].values)
ensemble_members = ds["number"].values
hazard = []

# create and write hazard object
else:
for i, member in enumerate(ensemble_members):
ds_subset = ds.sel(number=member)
hazard = Hazard.from_xarray_raster(
hazard.append(Hazard.from_xarray_raster(
data=ds_subset,
hazard_type=hazard_type,
intensity_unit=intensity_unit,
intensity=intensity_variable,
coordinate_vars={
"event": "step", "longitude": "longitude",
"latitude": "latitude"}
)

hazard.check()
hazard.write_hdf5(file_path)

print(f"Completed processing for {year}-{month:02d}. "\
f"Data saved in {output_dir}.")
))
if i==0:
number_lead_times = len(hazard[0].event_name)
hazard[i].event_name = [f'member{member}'] * number_lead_times

# concatenate and write hazards
hazard = Hazard.concat(hazard)
hazard.check()
hazard.write_hdf5(file_path)

print(f"Completed processing for {year}-{month:02d}. "\
f"Data saved in {output_dir}.")

except FileNotFoundError as e:

Check warning on line 504 in climada_petals/hazard/copernicus_forecast/handler.py

View check run for this annotation

Jenkins - WCR / Pylint

invalid-name

LOW: Variable name "e" doesn't conform to '(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$' pattern
Raw output
Used when the name doesn't match the regular expression associated to its type(constant, variable, class...).
print(f"File not found: {e.filename}")
Expand Down
Loading

0 comments on commit 9be7845

Please sign in to comment.