From 348396368f9716768a659a49340a26ba3d841db3 Mon Sep 17 00:00:00 2001 From: "Simon D.A. Thomas" Date: Wed, 23 Apr 2025 11:42:09 +0100 Subject: [PATCH] hopefully adding fort.22.nc regeneration capabilities --- adforce/ani.py | 23 +++++++++++++++++++++++ adforce/config.py | 27 ++++++++++++++++++++++++++- adforce/wrap.py | 11 +---------- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/adforce/ani.py b/adforce/ani.py index 269d3b2..789c970 100644 --- a/adforce/ani.py +++ b/adforce/ani.py @@ -20,6 +20,8 @@ from .mesh import bbox_mesh, xr_loader from .fort22datatree import read_fort22 from .constants import NO_BBOX, NEW_ORLEANS +from .config import load_config +from .fort22 import create_fort22 # from sithom.xr import plot_units @@ -145,6 +147,7 @@ def ani_heights_and_winds( x_pos (float, optional): relative x position of quiver label. y_pos (float, optional): relative y position of quiver label. """ + regenerate_fort22_if_does_not_exist(path_in) plot_defaults() img_folder = os.path.join(path_in, "img", "height_and_wind") gif_folder = os.path.join(path_in, "gif", "height_and_wind") @@ -421,6 +424,8 @@ def single_wind_and_height_step( """ plot_defaults() + regenerate_fort22_if_does_not_exist(path_in) + img_folder = os.path.join(path_in, "img") os.makedirs(img_folder, exist_ok=True) if bbox is not None: @@ -564,6 +569,24 @@ def single_wind_and_height_step( plt.clf() +def regenerate_fort22_if_does_not_exist(path: str) -> None: + """ + Regenerate fort.22 if it does not exist. + + Args: + path (str): path to fort.22.nc. + """ + if not os.path.exists(os.path.join(path, "fort.22.nc")) and os.path.exists( + os.path.join(path, "config.yaml") + ): + print("Regenerating fort.22.nc") + cfg = load_config(os.path.join(path, "config.yaml")) + create_fort22(path, cfg.grid, cfg.tc) + print("fort.22.nc regenerated") + else: + print("fort.22.nc already exists or config.yaml not found") + + if __name__ == "__main__": # python -m adforce.ani run_animation() diff --git a/adforce/config.py b/adforce/config.py index d660848..88f9f7c 100644 --- a/adforce/config.py +++ b/adforce/config.py @@ -8,12 +8,37 @@ ChavasCollision: A dataclass to store the impact location of the Chavas Lin and Emanuel (2015) profile collision on a straight track. """ +import os from dataclasses import dataclass -import hydra from omegaconf import DictConfig +from omegaconf import OmegaConf @dataclass class ChavasCollision: impact_lon: float impact_lat: float + + +def save_config(cfg: DictConfig) -> None: + """Save the configuration file for a wrapped ADCIRC run. + + Args: + cfg (DictConfig): configuration. + """ + with open(os.path.join(cfg.files.run_folder, "config.yaml"), "w") as fp: + OmegaConf.save(config=cfg, f=fp.name) + + +def load_config(path: str) -> DictConfig: + """Load the configuration file for a wrapped ADCIRC run. + + Args: + path (str): path to the config file. + + Returns: + DictConfig: loaded configuration. + """ + with open(path, "r") as fp: + cfg = OmegaConf.load(fp.name) + return cfg diff --git a/adforce/wrap.py b/adforce/wrap.py index c8ec98d..a7b7a33 100644 --- a/adforce/wrap.py +++ b/adforce/wrap.py @@ -11,6 +11,7 @@ from .slurm import setoff_slurm_job_and_wait from .subprocess import setoff_subprocess_job_and_wait from .mesh import xr_loader +from .config import save_config def observe_max_point(cfg: DictConfig) -> float: @@ -39,16 +40,6 @@ def observe_max_point(cfg: DictConfig) -> float: return maxele -def save_config(cfg: DictConfig) -> None: - """Save the configuration file. - - Args: - cfg (DictConfig): configuration. - """ - with open(os.path.join(cfg.files.run_folder, "config.yaml"), "w") as fp: - OmegaConf.save(config=cfg, f=fp.name) - - @timeit def idealized_tc_observe(cfg: DictConfig) -> float: """Wrap the adcirc call.