Skip to content

Commit fda08f6

Browse files
committed
[JTH] cleaned wrappers and docu
1 parent aec5ddc commit fda08f6

File tree

10 files changed

+87
-177
lines changed

10 files changed

+87
-177
lines changed

bluemath_tk/wrappers/_base_wrappers.py

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,41 @@ class BaseModelWrapper(BlueMathModel):
3434
3535
Methods
3636
-------
37-
_check_parameters_type(default_parameters, model_parameters)
37+
_check_parameters_type -> None
3838
Check if the parameters have the correct type.
39-
_exec_bash_commands(str_cmd, out_file=None, err_file=None)
39+
_exec_bash_commands -> None
4040
Execute bash commands.
41-
list_available_launchers()
41+
list_available_launchers -> dict
4242
List the available launchers.
43-
set_cases_dirs_from_output_dir()
43+
set_cases_dirs_from_output_dir -> None
4444
Set the cases directories from the output directory.
45-
write_array_in_file(array, filename)
45+
write_array_in_file -> None
4646
Write an array in a file.
47-
copy_files(src, dst)
47+
copy_files -> None
4848
Copy file(s) from source to destination.
49-
render_file_from_template(template_name, context, output_filename=None)
49+
render_file_from_template -> None
5050
Render a file from a template.
51-
create_cases_context_one_by_one()
51+
create_cases_context_one_by_one -> List[dict]
5252
Create an array of dictionaries with the combinations of values from the
5353
input dictionary, one by one.
54-
create_cases_context_all_combinations()
54+
create_cases_context_all_combinations -> List[dict]
5555
Create an array of dictionaries with each possible combination of values
5656
from the input dictionary.
57-
build_cases(mode="one_by_one")
57+
build_cases -> None
5858
Create the cases folders and render the input files.
59-
run_cases(launcher=None, cases_to_run=None, parallel=False)
59+
run_case -> None
60+
Run the case based on the launcher specified.
61+
run_cases -> None
6062
Run the cases based on the launcher specified.
6163
Parallel execution is optional.
62-
postprocess_case(case_num, case_dir)
64+
Cases to run can be specified.
65+
run_cases_bulk -> None
66+
Run the cases based on the launcher specified.
67+
postprocess_case -> None
6368
Postprocess the model output for a specific case.
64-
join_postprocessed_files(postprocessed_files)
69+
join_postprocessed_files -> xr.Dataset
6570
Join the postprocessed files.
66-
postprocess_cases(cases_to_postprocess=None)
71+
postprocess_cases -> Union[xr.Dataset, List[xr.Dataset]]
6772
Postprocess the model output.
6873
"""
6974

@@ -348,7 +353,13 @@ def build_cases(self, mode: str = "one_by_one") -> None:
348353
f"{len(self.cases_dirs)} cases created in {mode} mode and saved in {self.output_dir}"
349354
)
350355

351-
def run_case(self, case_dir: str, launcher: str) -> None:
356+
def run_case(
357+
self,
358+
case_dir: str,
359+
launcher: str,
360+
ouput_log_file: str = "wrapper_out.log",
361+
error_log_file: str = "wrapper_error.log",
362+
) -> None:
352363
"""
353364
Run the case based on the launcher specified.
354365
@@ -358,14 +369,23 @@ def run_case(self, case_dir: str, launcher: str) -> None:
358369
The case directory.
359370
launcher : str
360371
The launcher to run the case.
372+
ouput_log_file : str, optional
373+
The name of the output log file. Default is "wrapper_out.log".
374+
error_log_file : str, optional
375+
The name of the error log file. Default is "wrapper_error.log".
361376
"""
362377

363378
# Get launcher command from the available launchers
364379
launcher = self.list_available_launchers().get(launcher, launcher)
365380

381+
# Run the case in the case directory
366382
self.logger.info(f"Running case in {case_dir} with launcher={launcher}.")
367383
os.chdir(case_dir)
368-
self._exec_bash_commands(str_cmd=launcher)
384+
self._exec_bash_commands(
385+
str_cmd=launcher,
386+
out_file=ouput_log_file,
387+
err_file=error_log_file,
388+
)
369389

370390
def run_cases(
371391
self,
File renamed without changes.
Lines changed: 16 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
from .._base_wrappers import BaseModelWrapper
32

43

@@ -9,17 +8,10 @@ class SwanModelWrapper(BaseModelWrapper):
98
109
Attributes
1110
----------
12-
swan_exec : str
13-
The SWAN executable path.
1411
default_parameters : dict
15-
The default parameters type for the model.
16-
17-
Methods
18-
-------
19-
set_swan_exec(swan_exec: str) -> None
20-
Set the SWAN executable path.
21-
run_model(case_dir: str, log_file: str = "swan_exec.log") -> None
22-
Run the SWAN model for the specified case.
12+
The default parameters type for the wrapper.
13+
available_launchers : dict
14+
The available launchers for the wrapper.
2315
"""
2416

2517
default_parameters = {
@@ -29,71 +21,30 @@ class SwanModelWrapper(BaseModelWrapper):
2921
"spr": float,
3022
}
3123

24+
available_launchers = {
25+
"bash": "swanrun -input input.sws",
26+
"docker": "docker run --rm -v .:/case_dir -w /case_dir tausiaj/swan-geoocean:41.51 swanrun -input input.sws",
27+
}
28+
3229
def __init__(
3330
self,
3431
templates_dir: str,
35-
templates_name: dict,
3632
model_parameters: dict,
3733
output_dir: str,
38-
):
34+
templates_name: dict = "all",
35+
debug: bool = True,
36+
) -> None:
3937
"""
40-
Initialize the SWAN model wrapper.
41-
42-
Parameters
43-
----------
44-
templates_dir : str
45-
The directory where the templates are stored.
46-
templates_name : list
47-
The names of the templates.
48-
model_parameters : dict
49-
The parameters to be used in the templates.
50-
output_dir : str
51-
The directory where the output files will be saved.
38+
Initialize the SWASH model wrapper.
5239
"""
5340

5441
super().__init__(
5542
templates_dir=templates_dir,
56-
templates_name=templates_name,
5743
model_parameters=model_parameters,
5844
output_dir=output_dir,
45+
templates_name=templates_name,
5946
default_parameters=self.default_parameters,
6047
)
61-
self.set_logger_name(self.__class__.__name__)
62-
self._swan_exec: str = None
63-
64-
@property
65-
def swan_exec(self) -> str:
66-
return self._swan_exec
67-
68-
def set_swan_exec(self, swan_exec: str) -> None:
69-
self._swan_exec = swan_exec
70-
71-
def run_model(self, case_dir: str, log_file: str = "swan_exec.log") -> None:
72-
"""
73-
Run the SWAN model for the specified case.
74-
75-
Parameters
76-
----------
77-
case_dir : str
78-
The case directory.
79-
log_file : str, optional
80-
The log file name. Default is "swan_exec.log".
81-
82-
Raises
83-
------
84-
ValueError
85-
If the SWAN executable was not set.
86-
"""
87-
88-
if not self.swan_exec:
89-
raise ValueError("The SWAN executable was not set.")
90-
# check if windows OS
91-
is_win = sys.platform.startswith("win")
92-
if is_win:
93-
cmd = "cd {0} && {1} input".format(case_dir, self.swan_exec)
94-
else:
95-
cmd = "cd {0} && {1} -input input.sws".format(case_dir, self.swan_exec)
96-
# redirect output
97-
cmd += f" 2>&1 > {log_file}"
98-
# execute command
99-
self._exec_bash_commands(str_cmd=cmd)
48+
self.set_logger_name(
49+
name=self.__class__.__name__, level="DEBUG" if debug else "INFO"
50+
)

bluemath_tk/wrappers/swash/swash_example.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,14 @@ def build_cases(
9292
model_parameters = mda.centroids.to_dict(orient="list")
9393
output_dir = "/home/tausiaj/GitHub-GeoOcean/BlueMath/test_cases/swash/"
9494
# Create an instance of the SWASH model wrapper
95-
swash_model = VeggySwashModelWrapper(
95+
swash_wrapper = VeggySwashModelWrapper(
9696
templates_dir=templates_dir,
9797
model_parameters=model_parameters,
9898
output_dir=output_dir,
9999
)
100100
# Build the input files
101-
swash_model.build_cases(mode="one_by_one")
101+
swash_wrapper.build_cases(mode="one_by_one")
102102
# List available launchers
103-
print(swash_model.list_available_launchers())
104-
# Set the SWASH executable (not used if docker is used)
105-
swash_model.set_swash_exec(
106-
"/home/tausiaj/GeoOcean-Execs/SWASH-10.05-Linux/bin/swashrun"
107-
)
103+
print(swash_wrapper.list_available_launchers())
108104
# Run the model
109-
swash_model.run_cases(launcher="bash", parallel=True)
105+
swash_wrapper.run_cases(launcher="bash", parallel=True)

bluemath_tk/wrappers/swash/swash_wrapper.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,9 @@ class SwashModelWrapper(BaseModelWrapper):
2222
The available launchers for the wrapper.
2323
postprocess_functions : dict
2424
The postprocess functions for the wrapper.
25-
swash_exec : str
26-
The SWASH executable path.
2725
2826
Methods
2927
-------
30-
set_swash_exec -> None
31-
Sets the SWASH executable path.
3228
list_available_postprocess_vars -> List[str]
3329
List available postprocess variables.
3430
_read_tabfile -> pd.DataFrame
@@ -94,14 +90,6 @@ def __init__(
9490
self.set_logger_name(
9591
name=self.__class__.__name__, level="DEBUG" if debug else "INFO"
9692
)
97-
self._swash_exec: str = None
98-
99-
@property
100-
def swash_exec(self) -> str:
101-
return self._swash_exec
102-
103-
def set_swash_exec(self, swash_exec: str) -> None:
104-
self._swash_exec = swash_exec
10593

10694
def list_available_postprocess_vars(self) -> List[str]:
10795
"""

bluemath_tk/wrappers/xbeach/xbeach_wrapper.py

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,39 @@ class XBeachModelWrapper(BaseModelWrapper):
88
99
Attributes
1010
----------
11-
xbeach_exec : str
12-
The XBeach executable path.
1311
default_parameters : dict
14-
The default parameters type for the model.
15-
16-
Methods
17-
-------
18-
set_xbeach_exec(xbeach_exec: str) -> None
19-
Set the XBeach executable path.
20-
run_model() -> None
21-
Run the XBeach model for the specified case.
12+
The default parameters type for the wrapper.
13+
available_launchers : dict
14+
The available launchers for the wrapper.
2215
"""
2316

2417
default_parameters = {
2518
"spectra": str,
2619
}
2720

21+
available_launchers = {
22+
"geoocean-cluster": "launchXbeach.sh",
23+
}
24+
2825
def __init__(
2926
self,
3027
templates_dir: str,
31-
templates_name: dict,
3228
model_parameters: dict,
3329
output_dir: str,
30+
templates_name: dict = "all",
31+
debug: bool = True,
3432
) -> None:
3533
"""
3634
Initialize the SWASH model wrapper.
37-
38-
Parameters
39-
----------
40-
templates_dir : str
41-
The directory where the templates are stored.
42-
templates_name : list
43-
The names of the templates.
44-
model_parameters : dict
45-
The parameters to be used in the templates.
46-
output_dir : str
47-
The directory where the output files will be saved.
4835
"""
4936

5037
super().__init__(
5138
templates_dir=templates_dir,
52-
templates_name=templates_name,
5339
model_parameters=model_parameters,
5440
output_dir=output_dir,
41+
templates_name=templates_name,
5542
default_parameters=self.default_parameters,
5643
)
57-
self.set_logger_name(self.__class__.__name__)
58-
self._xbeach_exec = None
59-
60-
@property
61-
def xbeach_exec(self) -> str:
62-
return self._xbeach_exec
63-
64-
def set_xbeach_exec(self, xbeach_exec: str) -> None:
65-
self._xbeach_exec = xbeach_exec
66-
67-
def run_model(self) -> None:
68-
pass
44+
self.set_logger_name(
45+
name=self.__class__.__name__, level="DEBUG" if debug else "INFO"
46+
)

docs/wrappers/delft3d_wrapper.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
::: bluemath_tk.wrappers.delft3d.delft3d_wrapper

0 commit comments

Comments
 (0)