Skip to content

Commit

Permalink
lots of updates
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenvanormondt committed Mar 2, 2025
1 parent e80eb52 commit c07cfe5
Show file tree
Hide file tree
Showing 13 changed files with 2,140 additions and 376 deletions.
695 changes: 695 additions & 0 deletions hydromt_sfincs/boundary_conditions.py

Large diffs are not rendered by default.

35 changes: 20 additions & 15 deletions hydromt_sfincs/config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from datetime import datetime
import time
from typing import TYPE_CHECKING, List, Optional, Dict, Any
from ast import literal_eval
from os.path import abspath, isabs, join, split, exists
from pathlib import Path

from hydromt.model.components import ModelComponent

from hydromt_sfincs.config_variables import SfincsConfigVariables
# from hydromt_sfincs.config_variables import SfincsConfigVariables
from hydromt_sfincs.config_variables import sfincs_config_variables

if TYPE_CHECKING:
from hydromt_sfincs import SfincsModel
Expand All @@ -17,14 +19,14 @@ class SfincsConfig(ModelComponent):

def __init__(self, model: "SfincsModel"):
self._filename = "sfincs.inp"
self._data: SfincsConfigVariables = None
self._data: sfincs_config_variables = None
super().__init__(model=model)

@property
def data(self) -> SfincsConfigVariables:
def data(self):
"""Return the SfincsConfig object."""
if self._data is None:
self._data = SfincsConfigVariables()
self._data = sfincs_config_variables
return self._data

def read(self, filename: str = "sfincs.inp") -> None:
Expand Down Expand Up @@ -75,7 +77,7 @@ def read(self, filename: str = "sfincs.inp") -> None:
inp_dict[name] = val

# Convert dictionary to SfincsConfig instance
self._data = SfincsConfigVariables(**inp_dict)
self._data = self._data.copy(update=inp_dict)

# Update the grid properties from the configuration
# This will either drop the quadtree component or the regular component?
Expand Down Expand Up @@ -126,21 +128,23 @@ def get(self, key: str, fallback: Any = None, abs_path: bool = False) -> Any:

return value

def set(self, key: str, value: Any) -> None:
def set(self, key: str, value: Any, skip_validation=False) -> None:
"""Set a value with validation using Pydantic's model_copy."""

if not hasattr(self.data, key):
raise KeyError(f"'{key}' is not a valid attribute of SfincsConfig.")

# Validate the new data
# FIXME implement this in a better way
try:
# If key was an extra field, do NOT set it here
if key in self.data.model_fields:
value = SfincsConfigVariables(**{key: value}).__dict__[key]
except Exception as e:
raise TypeError(f"Invalid input type for '{key}'")
if not skip_validation:
# Validate the new data
# FIXME implement this in a better way
# It works, but it is quite slow when all the variables are set in a loop
# Therefore the skip_validation option is added
try:
self.data.model_validate({key: value})
except Exception as e:
raise TypeError(f"Invalid input type for '{key}'")

self._data = self._data.model_copy(update={key: value})
self.data.__setattr__(key, value)

def update(self, dict: Dict[str, Any]) -> None:
"""
Expand All @@ -167,6 +171,7 @@ def update_grid_from_config(self) -> None:
self.model.grid.update_grid_from_config()
# drop quadtree component
self.model.components.pop("quadtree", None)
# TODO also drop mask and subgrid components?
elif self.model.grid_type == "quadtree":
# drop regular component
self.model.components.pop("grid", None)
Expand Down
23 changes: 23 additions & 0 deletions hydromt_sfincs/config_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,26 @@ class Config:
le=1,
description="Option in integrated SnapWave solver to turn on wind growth process (1: yes, 0: no)",
)
snapwave_bndfile: str | None = Field(
None,
description="Name of the SnapWave boundary points file",
)
snapwave_bhsfile: str | None = Field(
None,
description="Name of the SnapWave wave height time-series file",
)
snapwave_btpfile: str | None = Field(
None,
description="Name of the SnapWave wave period time-series file",
)
snapwave_bwdfile: str | None = Field(
None,
description="Name of the SnapWave wave direction time-series file",
)
snapwave_bdsfile: str | None = Field(
None,
description="Name of the SnapWave wave spreading time-series file",
)
#
# Wind drag
#
Expand All @@ -637,3 +657,6 @@ class Config:
#
bcafile: str | None = Field(None, description="Name of the calibration file")
corfile: str | None = Field(None, description="Name of the correction file")


sfincs_config_variables = SfincsConfigVariables()
Loading

0 comments on commit c07cfe5

Please sign in to comment.