Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 42 additions & 53 deletions bluemath_tk/wrappers/swash/swash_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ class SwashModelWrapper(BaseModelWrapper):
"Msetup": "calculate_setup",
"Hrms": "calculate_statistical_analysis",
"Hfreqs": "calculate_spectral_analysis",
"Watlev": "get_waterlevel",
}

def __init__(
Expand Down Expand Up @@ -412,7 +411,8 @@ def postprocess_case(
case_num: int,
case_dir: str,
output_vars: List[str] = None,
force: bool = False,
overwrite_output: bool = True,
overwrite_output_postprocessed: bool = True,
remove_tab: bool = False,
remove_nc: bool = False,
) -> xr.Dataset:
Expand All @@ -425,14 +425,17 @@ def postprocess_case(
The case number.
case_dir : str
The case directory.
force : bool, optional
Force the postprocessing, re-creating the output.nc file. Default is False.
force_output : bool, optional
Force the creation of the output.nc file. Default is False.
force_output_postprocessed : bool, optional
Force the postprocessing of the output.nc file into output_postprocessed. Default is False.
output_vars : list, optional
The output variables to postprocess. Default is None.
remove_tab : bool, optional
Remove the tab files. Default is False.
remove_nc : bool, optional
Remove the netCDF file. Default is False.


Returns
-------
Expand All @@ -444,48 +447,57 @@ def postprocess_case(

warnings.filterwarnings("ignore")

self.logger.info(f"Postprocessing case {case_num} in {case_dir}.")
self.logger.info(f"[{case_num}]: Postprocessing case {case_num} in {case_dir}.")

if output_vars is None:
self.logger.info("Postprocessing all available variables.")
self.logger.debug(f"[{case_num}]: Postprocessing all available variables.")
output_vars = list(self.postprocess_functions.keys())

output_nc_path = os.path.join(case_dir, "output.nc")
if not os.path.exists(output_nc_path) or force:
if not os.path.exists(output_nc_path) or overwrite_output:
# Convert tab files to netCDF file
output_path = os.path.join(case_dir, "output.tab")
run_path = os.path.join(case_dir, "run.tab")
output_nc = self._convert_case_output_files_to_nc(
case_num=case_num, output_path=output_path, run_path=run_path
)
if os.path.exists(output_nc_path):
os.remove(output_nc_path)
output_nc.to_netcdf(output_nc_path)
else:
self.logger.info("Reading existing output.nc file.")
self.logger.info(f"[{case_num}]: Reading existing output.nc file.")
output_nc = xr.open_dataset(output_nc_path)

# Postprocess variables from output.nc
var_ds_list = []
for var in output_vars:
if var in self.postprocess_functions:
var_ds = getattr(self, self.postprocess_functions[var])(
case_num=case_num, case_dir=case_dir, output_nc=output_nc
)
var_ds_list.append(var_ds)
else:
self.logger.warning(
f"Variable {var} is not available for postprocessing."
)

# Merge all variables in one Dataset
ds = xr.merge(var_ds_list, compat="no_conflicts")

# Save Dataset to netCDF file
processed_nc_path = os.path.join(case_dir, "output_postprocessed.nc")
if os.path.exists(processed_nc_path):
os.remove(processed_nc_path)
ds.to_netcdf(processed_nc_path)
if not os.path.exists(processed_nc_path) or overwrite_output_postprocessed:
# Postprocess variables from output.nc
var_ds_list = []
for var in output_vars:
if var in self.postprocess_functions:
self.logger.debug(f"[{case_num}]: Postprocessing variable {var}.")
var_ds = getattr(self, self.postprocess_functions[var])(
case_num=case_num, case_dir=case_dir, output_nc=output_nc
)
var_ds_list.append(var_ds)
else:
# If the variable is present in output_nc, extract and squeeze it
if var in output_nc:
self.logger.debug(f"[{case_num}]: Extracting variable {var}.")
var_ds = output_nc[var].squeeze()
var_ds_list.append(var_ds)
else:
self.logger.warning(
f"[{case_num}]: Variable {var} is not available for postprocessing."
)

# Merge all variables in one Dataset
ds = xr.merge(var_ds_list, compat="no_conflicts")
self.logger.info(
f"[{case_num}]: Creating new output_postprocessed.nc file from output.nc."
)
ds.to_netcdf(processed_nc_path)
else:
self.logger.info(f"[{case_num}]: Reading existing output_postprocessed.nc file.")
ds = xr.open_dataset(processed_nc_path)


# Remove raw files to save space
if remove_tab:
Expand Down Expand Up @@ -534,29 +546,6 @@ def find_maximas(self, x: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:

return peaks, x[peaks]

def get_waterlevel(
self, case_num: int, case_dir: str, output_nc: xr.Dataset
) -> xr.Dataset:
"""
Get water level from the output netCDF file.

Parameters
----------
case_num : int
The case number.
case_dir : str
The case directory.
output_nc : xr.Dataset
The output netCDF file.

Returns
-------
xr.Dataset
The water level.
"""

# get water level
return output_nc[["Watlev"]].squeeze()

def calculate_runup2(
self, case_num: int, case_dir: str, output_nc: xr.Dataset
Expand Down