From 82ffe411bb12858202f464a275dc802b838605b0 Mon Sep 17 00:00:00 2001 From: Daley Adrichem Date: Thu, 4 Jul 2024 17:09:16 +0200 Subject: [PATCH 1/3] Simplify logging --- flood_adapt/integrator/fiat_adapter.py | 3 ++- flood_adapt/integrator/sfincs_adapter.py | 3 ++- flood_adapt/log.py | 27 ++++++++++++++++++++--- flood_adapt/object_model/hazard/hazard.py | 5 +++-- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/flood_adapt/integrator/fiat_adapter.py b/flood_adapt/integrator/fiat_adapter.py index acccfe668..18a1c52ad 100644 --- a/flood_adapt/integrator/fiat_adapter.py +++ b/flood_adapt/integrator/fiat_adapter.py @@ -1,4 +1,5 @@ import gc +import logging from pathlib import Path from typing import List, Optional, Union @@ -25,7 +26,7 @@ class FiatAdapter: def __init__(self, model_root: str, database_path: str) -> None: """Load FIAT model based on a root directory.""" # Load FIAT template - self._logger = FloodAdaptLogging.getLogger(__name__) + self._logger = FloodAdaptLogging.getLogger(__name__, level=logging.ERROR) self.fiat_model = FiatModel(root=model_root, mode="r", logger=self._logger) self.fiat_model.read() diff --git a/flood_adapt/integrator/sfincs_adapter.py b/flood_adapt/integrator/sfincs_adapter.py index 09f654195..e5bc13230 100644 --- a/flood_adapt/integrator/sfincs_adapter.py +++ b/flood_adapt/integrator/sfincs_adapter.py @@ -1,4 +1,5 @@ import gc +import logging import os from pathlib import Path from typing import Optional, Union @@ -41,7 +42,7 @@ def __init__(self, site: Site, model_root: Optional[str] = None): Args: model_root (str, optional): Root directory of overland sfincs model. Defaults to None. """ - self._logger = FloodAdaptLogging.getLogger(__name__) + self._logger = FloodAdaptLogging.getLogger(__name__, level=logging.ERROR) self.sf_model = SfincsModel(root=model_root, mode="r+", logger=self._logger) self.sf_model.read() self.site = site diff --git a/flood_adapt/log.py b/flood_adapt/log.py index 026ce0431..bfe813325 100644 --- a/flood_adapt/log.py +++ b/flood_adapt/log.py @@ -65,11 +65,32 @@ def remove_file_handler(cls, file_path: str) -> None: cls.getLogger().removeHandler(handler) @classmethod - def getLogger(cls, name: str = None) -> logging.Logger: + def getLogger(cls, name: str = None, level: int = None) -> logging.Logger: + """Get a logger with the specified name. If no name is provided, return the root logger. + + If the logger does not exist, it is created with the specified level. If no level is provided, the logger inherits the level of the root logger. + + Parameters + ---------- + name : str, optional + The name of the logger. If not provided, the root logger is returned. + level : int, optional + The level of the logger. If not provided, the logger inherits the level of the root logger. + + Returns + ------- + logging.Logger + The logger with the specified name. + """ if name is None: - return cls._root_logger + logger = cls._root_logger + else: + logger = logging.getLogger(f"FloodAdapt.{name}") + + if level is not None: + logger.setLevel(level) - return logging.getLogger(f"FloodAdapt.{name}") + return logger @classmethod def shutdown(cls): diff --git a/flood_adapt/object_model/hazard/hazard.py b/flood_adapt/object_model/hazard/hazard.py index 5d0e06b3f..07cd7281e 100644 --- a/flood_adapt/object_model/hazard/hazard.py +++ b/flood_adapt/object_model/hazard/hazard.py @@ -55,7 +55,6 @@ class Hazard: def __init__(self, scenario: ScenarioModel, database, results_dir: Path) -> None: self._logger = FloodAdaptLogging.getLogger(__name__) - self._mode: Mode self.simulation_paths: List[Path] self.simulation_paths_offshore: List[Path] @@ -273,6 +272,7 @@ def run_sfincs(self): run_success = True for simulation_path in self.simulation_paths: + self._logger.info(f"Running SFINCS model for {'-'.join(simulation_path.parts[-2:])}.") with cd(simulation_path): sfincs_log = "sfincs.log" # with open(results_dir.joinpath(f"{self.name}.log"), "a") as log_handler: @@ -977,7 +977,7 @@ def calculate_rp_floodmaps(self): np.copy(zb), len(floodmap_rp), 1 ) # if not flooded (i.e. not in valid_cells) revert to bed_level, read from SFINCS results so it is the minimum bed level in a grid cell - self._logger.info("Calculating flood risk maps, this may take some time...") + self._logger.info("Calculating flood risk map data, this may take some time...") for jj in valid_cells: # looping over all non-masked cells. # linear interpolation for all return periods to evaluate h[:, jj] = np.interp( @@ -998,6 +998,7 @@ def calculate_rp_floodmaps(self): h[dry] = np.nan for ii, rp in enumerate(floodmap_rp): + self._logger.info(f"Creating flood risk map for return period {rp} years.") # #create single nc zs_rp_single = xr.DataArray( data=h[ii, :], coords={"z": zs["z"]}, attrs={"units": "meters"} From 028b8fb505c5bb8332b03fb59e7573f057092ff2 Mon Sep 17 00:00:00 2001 From: Daley Adrichem Date: Thu, 4 Jul 2024 17:09:57 +0200 Subject: [PATCH 2/3] Fix black --- flood_adapt/log.py | 8 ++++---- flood_adapt/object_model/hazard/hazard.py | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/flood_adapt/log.py b/flood_adapt/log.py index bfe813325..328f6d7cd 100644 --- a/flood_adapt/log.py +++ b/flood_adapt/log.py @@ -67,20 +67,20 @@ def remove_file_handler(cls, file_path: str) -> None: @classmethod def getLogger(cls, name: str = None, level: int = None) -> logging.Logger: """Get a logger with the specified name. If no name is provided, return the root logger. - + If the logger does not exist, it is created with the specified level. If no level is provided, the logger inherits the level of the root logger. - + Parameters ---------- name : str, optional The name of the logger. If not provided, the root logger is returned. level : int, optional The level of the logger. If not provided, the logger inherits the level of the root logger. - + Returns ------- logging.Logger - The logger with the specified name. + The logger with the specified name. """ if name is None: logger = cls._root_logger diff --git a/flood_adapt/object_model/hazard/hazard.py b/flood_adapt/object_model/hazard/hazard.py index 07cd7281e..4a0e0a2c4 100644 --- a/flood_adapt/object_model/hazard/hazard.py +++ b/flood_adapt/object_model/hazard/hazard.py @@ -272,7 +272,9 @@ def run_sfincs(self): run_success = True for simulation_path in self.simulation_paths: - self._logger.info(f"Running SFINCS model for {'-'.join(simulation_path.parts[-2:])}.") + self._logger.info( + f"Running SFINCS model for {'-'.join(simulation_path.parts[-2:])}." + ) with cd(simulation_path): sfincs_log = "sfincs.log" # with open(results_dir.joinpath(f"{self.name}.log"), "a") as log_handler: From e2051ba01d3aca1971e65b0d7b6f5af5062cc752 Mon Sep 17 00:00:00 2001 From: Daley Adrichem Date: Tue, 9 Jul 2024 12:30:14 +0200 Subject: [PATCH 3/3] Fix deleting files in hydromts --- flood_adapt/integrator/fiat_adapter.py | 4 ++-- flood_adapt/integrator/sfincs_adapter.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/flood_adapt/integrator/fiat_adapter.py b/flood_adapt/integrator/fiat_adapter.py index 18a1c52ad..dd2b1fd4e 100644 --- a/flood_adapt/integrator/fiat_adapter.py +++ b/flood_adapt/integrator/fiat_adapter.py @@ -26,8 +26,8 @@ class FiatAdapter: def __init__(self, model_root: str, database_path: str) -> None: """Load FIAT model based on a root directory.""" # Load FIAT template - self._logger = FloodAdaptLogging.getLogger(__name__, level=logging.ERROR) - self.fiat_model = FiatModel(root=model_root, mode="r", logger=self._logger) + self._logger = FloodAdaptLogging.getLogger(__name__, level=logging.INFO) + self.fiat_model = FiatModel(root=model_root, mode="r") self.fiat_model.read() # Get site information diff --git a/flood_adapt/integrator/sfincs_adapter.py b/flood_adapt/integrator/sfincs_adapter.py index e5bc13230..c67bdd86d 100644 --- a/flood_adapt/integrator/sfincs_adapter.py +++ b/flood_adapt/integrator/sfincs_adapter.py @@ -42,8 +42,8 @@ def __init__(self, site: Site, model_root: Optional[str] = None): Args: model_root (str, optional): Root directory of overland sfincs model. Defaults to None. """ - self._logger = FloodAdaptLogging.getLogger(__name__, level=logging.ERROR) - self.sf_model = SfincsModel(root=model_root, mode="r+", logger=self._logger) + self._logger = FloodAdaptLogging.getLogger(__name__, level=logging.INFO) + self.sf_model = SfincsModel(root=model_root, mode="r+") self.sf_model.read() self.site = site