Skip to content

Commit

Permalink
fixed path names and consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinGebhart committed Sep 26, 2024
1 parent aae38e8 commit c9e3ea8
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 137 deletions.
42 changes: 17 additions & 25 deletions climada_petals/hazard/copernicus_forecast/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def _process_data(self, data_out, year_list, month_list, bounds, overwrite, tf_i

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

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
# check if data already exists including all relevant data variables
data_already_exists = self._is_data_present(
download_file, file_extension,
daily_file, 'nc',
indicator.get_index_params(tf_index)['variables']
)

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

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
Expand Down Expand Up @@ -403,7 +403,9 @@ def download_and_process_data(
"""

bounds = self._get_bounds_for_area_selection(area_selection)
self._download_data(data_out, year_list, month_list, bounds, overwrite, tf_index, format, originating_centre, system, max_lead_month)
self._download_data(
data_out, year_list, month_list, bounds, overwrite, tf_index,
format, originating_centre, system, max_lead_month)
self._process_data(data_out, year_list, month_list, bounds, overwrite, tf_index, format)

def calculate_index(

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

View check run for this annotation

Jenkins - WCR / Pylint

too-many-locals

LOW: Too many local variables (21/15)
Raw output
Used when a function or method has too many local variables.
Expand Down Expand Up @@ -445,22 +447,16 @@ def calculate_index(
f'Index file {tf_index}_{area_str}_{year}{month:02d}.nc already exists.'
)

# calculate indeces
else:
if tf_index in ["HIS", "HIA", "Tmean", "Tmax", "Tmin"]:
# Calculate heat indices like HIS, HIA, Tmean, Tmax, Tmin
ds_daily, ds_monthly, ds_stats = indicator.calculate_heat_indices(input_file_name, tf_index)

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

View check run for this annotation

Jenkins - WCR / Pylint

line-too-long

LOW: Line too long (116/100)
Raw output
Used when a line is longer than a given number of characters.

elif tf_index == "TR":
# Handle Tropical Nights (TR)
ds_daily, ds_monthly, ds_stats = indicator.calculate_and_save_tropical_nights_per_lag(grib_file_name, tf_index)

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

View check run for this annotation

Jenkins - WCR / Pylint

line-too-long

LOW: Line too long (135/100)
Raw output
Used when a line is longer than a given number of characters.

elif tf_index == "TX30":
# Handle Hot Days (Tmax > 30°C)
ds_daily, ds_monthly, ds_stats = indicator.calculate_and_save_tx30_per_lag(grib_file_name, tf_index)

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

View check run for this annotation

Jenkins - WCR / Pylint

line-too-long

LOW: Line too long (124/100)
Raw output
Used when a line is longer than a given number of characters.

# TODO: add functionality

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

View check run for this annotation

Jenkins - WCR / Pylint

fixme

NORMAL: TODO: add functionality
Raw output
no description found
# elif tf_index == "HW":
# Handle Heat Wave Days (3 consecutive days Tmax > threshold)
# calculate_and_save_heat_wave_days_per_lag(data_out, year_list, month_list, tf_index, area_selection)

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

View check run for this annotation

Jenkins - WCR / Pylint

line-too-long

LOW: Line too long (126/100)
Raw output
Used when a line is longer than a given number of characters.

else:
Expand All @@ -483,9 +479,6 @@ def save_index_to_hazard(self, year_list, month_list, area_selection, data_out,

bounds = self._get_bounds_for_area_selection(area_selection)
area_str = f"{int(bounds[1])}_{int(bounds[0])}_{int(bounds[2])}_{int(bounds[3])}"

base_input_dir = os.path.join(data_out, tf_index)
base_output_dir = os.path.join(data_out, f"{tf_index}/hazard")
hazard_type = tf_index
intensity_variable = f"{tf_index}"

Expand All @@ -496,28 +489,26 @@ def save_index_to_hazard(self, year_list, month_list, area_selection, data_out,

for year in year_list:
for month in month_list:
# define directories
input_file_name = f'{data_out}/{tf_index}/{year}/{month:02d}/' \
f'{hazard_type}_{area_str}_{year}{month:02d}.nc'
output_dir = f'{data_out}/{tf_index}/hazard/{year}/{month:02d}'
os.makedirs(output_dir, exist_ok=True)

month_str = f"{month:02d}"
current_input_dir = f'{base_input_dir}/{year}/{month:02d}'
nc_file_pattern = f"{hazard_type}_{area_str}_{year}{month_str}.nc"
nc_file_path = os.path.join(current_input_dir, nc_file_pattern)
current_output_dir = os.path.join(base_output_dir, f"{year}{month_str}")
os.makedirs(current_output_dir, exist_ok=True)
try:
ds = xr.open_dataset(nc_file_path)

ds = xr.open_dataset(input_file_name)

Check warning on line 499 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"])

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

View check run for this annotation

Jenkins - WCR / Pylint

line-too-long

LOW: Line too long (106/100)
Raw output
Used when a line is longer than a given number of characters.
ds["step"] = pd.to_datetime(ds["step"].values)

ensemble_members = ds["number"].values

for member in ensemble_members:
# check if data already exists
filename = f"hazard_{hazard_type}_member_{member}_{area_str}_{year}{month_str}.hdf5"
file_path = os.path.join(current_output_dir, filename)
filename = f"hazard_{hazard_type}_member_{member}_{area_str}_{year}{month:02d}.hdf5"

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

View check run for this annotation

Jenkins - WCR / Pylint

line-too-long

LOW: Line too long (108/100)
Raw output
Used when a line is longer than a given number of characters.
file_path = f'{output_dir}/{filename}'
if os.path.exists(file_path) and not overwrite:
self.logger.info(f'Index file ' \

Check warning on line 509 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
f'{tf_index}_{area_str}_{year}{month:02d}.nc already exists.')
# write hazard
else:
ds_subset = ds.sel(number=member)
hazard = Hazard.from_xarray_raster(
Expand All @@ -531,7 +522,7 @@ def save_index_to_hazard(self, year_list, month_list, area_selection, data_out,
hazard.check()
hazard.write_hdf5(file_path)

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

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

View check run for this annotation

Jenkins - WCR / Pylint

line-too-long

LOW: Line too long (102/100)
Raw output
Used when a line is longer than a given number of characters.

except FileNotFoundError as e:

Check warning on line 527 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 All @@ -542,7 +533,8 @@ def save_index_to_hazard(self, year_list, month_list, area_selection, data_out,
last_hazard_file = file_path
hazard_obj = Hazard.from_hdf5(last_hazard_file)
hazard_obj.plot_intensity(1, smooth=False)



@staticmethod
def _is_data_present(file, format, vars):

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

View check run for this annotation

Jenkins - WCR / Pylint

redefined-builtin

NORMAL: Redefining built-in 'format'
Raw output
Used when a variable or function override a built-in.

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

View check run for this annotation

Jenkins - WCR / Pylint

redefined-builtin

NORMAL: Redefining built-in 'vars'
Raw output
Used when a variable or function override a built-in.
data_already_exists = os.path.isfile(file)
Expand Down
Loading

0 comments on commit c9e3ea8

Please sign in to comment.