Skip to content

Commit

Permalink
merge base branch
Browse files Browse the repository at this point in the history
  • Loading branch information
MAfarrag committed Jan 8, 2025
2 parents f086597 + d9387e0 commit 3367e61
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 42 deletions.
5 changes: 5 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ Check relevant points.
- [ ] Test B

# Checklist:
Prepare items below using:
\[ :x: \] (markdown: `[ :x: ]`) for TODO items
\[ :white_check_mark: \] (markdown: `[ :white_check_mark: ]`) for DONE items
\[ N/A \] for items that are not applicable for this PR.


- [ ] updated version number in setup.py/pyproject.toml/environment.yml.
- [ ] updated the lock file.
Expand Down
4 changes: 2 additions & 2 deletions hydrolib/core/dflowfm/extold/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class ExtOldParametersQuantity(StrEnum):
FrictionCoefficient = "frictioncoefficient"
HorizontalEddyViscosityCoefficient = "horizontaleddyviscositycoefficient"
HorizontalEddyDiffusivityCoefficient = "horizontaleddydiffusivitycoefficient"
Advectiontype = "advectiontype"
AdvectionType = "advectiontype"
InfiltrationCapacity = "infiltrationcapacity"


Expand Down Expand Up @@ -307,7 +307,7 @@ class ExtOldSourcesSinks(StrEnum):


class ExtOldQuantity(StrEnum):
"""Enum class containing th e valid values for the boundary conditions category
"""Enum class containing the valid values for the boundary conditions category
of the external forcings.
"""

Expand Down
7 changes: 2 additions & 5 deletions hydrolib/core/dflowfm/inifield/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,8 @@ class InitialField(AbstractSpatialField):
an inifield file.
Typically inside the definition list of a [FMModel][hydrolib.core.dflowfm.mdu.models.FMModel]`.geometry.inifieldfile.initial[..]`
A `[Initial Condition]` block for use inside an external forcings file,
i.e., a [ExtModel][hydrolib.core.dflowfm.ext.models.ExtModel].
All lowercased attributes match with the meteo input as described in
[UM Sec.C.5.2.3](https://content.oss.deltares.nl/delft3dfm1d2d/D-Flow_FM_User_Manual_1D2D.pdf#subsection.C.5.2.3).
All lowercased attributes match with the initial field input as described in
[UM Sec.D.2](https://content.oss.deltares.nl/delft3dfm1d2d/D-Flow_FM_User_Manual_1D2D.pdf#subsection.D.2).
"""

_header: Literal["Initial"] = "Initial"
Expand Down
33 changes: 17 additions & 16 deletions hydrolib/tools/ext_old_to_new/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from hydrolib.core.dflowfm.tim.models import TimModel
from hydrolib.core.dflowfm.tim.parser import TimParser
from hydrolib.tools.ext_old_to_new.utils import (
convert_initial_cond_param_dict,
convert_interpolation_data,
create_initial_cond_and_parameter_input_dict,
find_temperature_salinity_in_quantities,
oldfiletype_to_forcing_file_type,
)
Expand Down Expand Up @@ -120,12 +120,12 @@ def __init__(self):
super().__init__()

def convert(self, forcing: ExtOldForcing) -> Boundary:
"""Convert an old external forcing block with meteo data to a boundary
forcing block suitable for inclusion in a new external forcings file.
"""Convert an old external forcing block to a boundary forcing block
suitable for inclusion in a new external forcings file.
This function takes a forcing block from an old external forcings
file, represented by an instance of ExtOldForcing, and converts it
into a Meteo object. The Boundary object is suitable for use in new
into a boundary object. The Boundary object is suitable for use in new
external forcings files, adhering to the updated format and
specifications.
Expand All @@ -136,7 +136,7 @@ def convert(self, forcing: ExtOldForcing) -> Boundary:
required for the conversion process.
Returns:
Boundary: A Boindary object that represents the converted forcing
Boundary: A Boundary object that represents the converted forcing
block, ready to be included in a new external forcings file. The
Boundary object conforms to the new format specifications, ensuring
compatibility with updated systems and models.
Expand Down Expand Up @@ -191,7 +191,7 @@ def convert(self, forcing: ExtOldForcing) -> InitialField:
References:
[Sec.D](https://content.oss.deltares.nl/delft3dfm1d2d/D-Flow_FM_User_Manual_1D2D.pdf#subsection.D)
"""
data = convert_initial_cond_param_dict(forcing)
data = create_initial_cond_and_parameter_input_dict(forcing)
new_block = InitialField(**data)

return new_block
Expand All @@ -203,14 +203,14 @@ def __init__(self):
super().__init__()

def convert(self, forcing: ExtOldForcing) -> ParameterField:
"""Convert an old external forcing block with meteo data to a boundary
forcing block suitable for inclusion in a new external forcings file.
"""Convert an old external forcing block to a parameter forcing block
suitable for inclusion in an initial field and parameter file.
This function takes a forcing block from an old external forcings
file, represented by an instance of ExtOldForcing, and converts it
into a Meteo object. The Boundary object is suitable for use in new
external forcings files, adhering to the updated format and
specifications.
into a ParameterField object. The ParameterField object is suitable for use in
an IniFieldModel, representing an initial field and parameter file, adhering
to the updated format and specifications.
Args:
forcing (ExtOldForcing): The contents of a single forcing block
Expand All @@ -219,18 +219,19 @@ def convert(self, forcing: ExtOldForcing) -> ParameterField:
required for the conversion process.
Returns:
Boundary: A Boindary object that represents the converted forcing
block, ready to be included in a new external forcings file. The
Boundary object conforms to the new format specifications, ensuring
compatibility with updated systems and models.
ParameterField:
A ParameterField object that represents the converted forcing
block, ready to be included in an initial field and parameter file. The
ParameterField object conforms to the new format specifications, ensuring
compatibility with updated systems and models.
Raises:
ValueError: If the forcing block contains a quantity that is not
supported by the converter, a ValueError is raised. This ensures
that only compatible forcing blocks are processed, maintaining
data integrity and preventing errors in the conversion process.
"""
data = convert_initial_cond_param_dict(forcing)
data = create_initial_cond_and_parameter_input_dict(forcing)
new_block = ParameterField(**data)

return new_block
Expand Down
10 changes: 5 additions & 5 deletions hydrolib/tools/ext_old_to_new/main_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,16 @@ def structure_model(self, path: PathOrStr):
@staticmethod
def _read_old_file(extoldfile: PathOrStr) -> ExtOldModel:
"""Read a legacy D-Flow FM external forcings file (.ext) into an
ExtOldModel object.
ExtOldModel object.
Args:
extoldfile (PathOrStr): path to the external forcings file (.ext)
Returns:
ExtOldModel: object with all forcing blocks.
Returns:
ExtOldModel: object with all forcing blocks.
Raises:
FileNotFoundError: If the old external forcing file does not exist.
Raises:
FileNotFoundError: If the old external forcing file does not exist.
"""
if not isinstance(extoldfile, Path):
extoldfile = Path(extoldfile)
Expand Down
14 changes: 11 additions & 3 deletions hydrolib/tools/ext_old_to_new/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,18 @@ def convert_interpolation_data(
return data


def convert_initial_cond_param_dict(forcing: ExtOldForcing) -> Dict[str, str]:
"""Initial condition and Parameters data dictionary.
def create_initial_cond_and_parameter_input_dict(
forcing: ExtOldForcing,
) -> Dict[str, str]:
"""Create the input dictionary for the `InitialField` or `ParameterField`
Args:
forcing: [ExtOldForcing]
External forcing block from the old external forcings file.
Initial condition and Parameters have the same structure for the conversion.
Returns:
Dict[str, str]:
the input dictionary to the `InitialField` or `ParameterField` constructor
"""
block_data = {
"quantity": forcing.quantity,
Expand Down
14 changes: 7 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions tests/tools/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

from hydrolib.core.basemodel import DiskOnlyFileModel
from hydrolib.core.dflowfm.bc.models import ForcingModel
from hydrolib.core.dflowfm.ext.models import Boundary, Meteo
from hydrolib.core.dflowfm.common.models import Operand
from hydrolib.core.dflowfm.ext.models import (
Boundary,
Meteo,
MeteoForcingFileType,
MeteoInterpolationMethod,
)
from hydrolib.core.dflowfm.extold.models import ExtOldForcing, ExtOldQuantity
from hydrolib.core.dflowfm.inifield.models import InitialField, ParameterField
from hydrolib.tools.ext_old_to_new.converters import (
Expand Down Expand Up @@ -72,10 +78,13 @@ def test_default(self):
new_quantity_block = MeteoConverter().convert(forcing)
assert isinstance(new_quantity_block, Meteo)
assert new_quantity_block.quantity == "windx"
assert new_quantity_block.operand == "O"
assert new_quantity_block.operand == Operand.override
assert new_quantity_block.forcingfile == DiskOnlyFileModel("windtest.amu")
assert new_quantity_block.forcingfiletype == "meteoGridEqui"
assert new_quantity_block.interpolationmethod == "linearSpaceTime"
assert new_quantity_block.forcingfiletype == MeteoForcingFileType.meteogridequi
assert (
new_quantity_block.interpolationmethod
== MeteoInterpolationMethod.linearSpaceTime
)


class TestBoundaryConverter:
Expand Down

0 comments on commit 3367e61

Please sign in to comment.