Skip to content

Commit

Permalink
create class method for the ExternalForcingConverter from the old e…
Browse files Browse the repository at this point in the history
…xternal forcing `ExtOldModel`
  • Loading branch information
MAfarrag committed Nov 26, 2024
1 parent a523f58 commit 0ada611
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
21 changes: 12 additions & 9 deletions hydrolib/tools/ext_old_to_new/main_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@

class ExternalForcingConverter:

def __init__(self):
def __init__(self, extold_model: ExtOldModel = None):
"""Initialize the converter."""
pass
self._extold_model = extold_model

@staticmethod
def read_old_file(extoldfile: PathOrStr) -> ExtOldModel:
@property
def extold_model(self):
return self._extold_model

@classmethod
def read_old_file(cls, extoldfile: PathOrStr) -> "ExternalForcingConverter":
"""Read a legacy D-Flow FM external forcings file (.ext) into an
ExtOldModel object.
Expand All @@ -51,7 +55,7 @@ def read_old_file(extoldfile: PathOrStr) -> ExtOldModel:
if _verbose:
print(f"Read {len(extold_model.forcing)} forcing blocks from {extoldfile}.")

return extold_model
return cls(extold_model)


def ext_old_to_new(
Expand Down Expand Up @@ -80,7 +84,6 @@ def ext_old_to_new(
The updated models (already written to disk). Maybe used
at call site to inspect the updated models.
"""

if _verbose:
workdir = os.getcwd() + "\\"
print(f"Work dir: {workdir}")
Expand All @@ -93,7 +96,7 @@ def ext_old_to_new(
print(f"* {structurefile}")

try:
extold_model = ExternalForcingConverter().read_old_file(extoldfile)
converter = ExternalForcingConverter.read_old_file(extoldfile)
except Exception as error:
print("The old external forcing file could not be read:", error)
return
Expand All @@ -102,7 +105,7 @@ def ext_old_to_new(
inifield_model = construct_filemodel_new_or_existing(IniFieldModel, inifieldfile)
structure_model = construct_filemodel_new_or_existing(StructureModel, structurefile)

for forcing in extold_model.forcing:
for forcing in converter.extold_model.forcing:
try:
converter_class = ConverterFactory.create_converter(forcing.quantity)
new_quantity_block = converter_class.convert(forcing)
Expand Down Expand Up @@ -136,7 +139,7 @@ def ext_old_to_new(
backup_file(structure_model.filepath, backup)
structure_model.save()

return extold_model, ext_model, inifield_model, structure_model
return ext_model, inifield_model, structure_model


def ext_old_to_new_from_mdu(
Expand Down
11 changes: 5 additions & 6 deletions tests/tools/test_main_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_ext_with_only_initial_contitions(
new_structure_file = Path("tests/data/input/new-structure.ext")

path = old_forcing_file_initial_condition["path"]
extold_model, ext_model, inifield_model, structure_model = ext_old_to_new(
ext_model, inifield_model, structure_model = ext_old_to_new(
path, new_ext_file, new_initial_file, new_structure_file
)

Expand Down Expand Up @@ -96,11 +96,10 @@ def test_read_ext_old_data(
old_forcing_file_quantities: List[str],
old_forcing_comment_len: int,
):
converter = ExternalForcingConverter()
model = converter.read_old_file(old_forcing_file)
assert len(model.forcing) == len(old_forcing_file_quantities)
assert len(model.comment) == old_forcing_comment_len
quantities = [forcing.quantity for forcing in model.forcing]
converter = ExternalForcingConverter.read_old_file(old_forcing_file)
assert len(converter.extold_model.forcing) == len(old_forcing_file_quantities)
assert len(converter.extold_model.comment) == old_forcing_comment_len
quantities = [forcing.quantity for forcing in converter.extold_model.forcing]
assert all([quantity in old_forcing_file_quantities for quantity in quantities])
# test verbose
main_converter._verbose = True
Expand Down

0 comments on commit 0ada611

Please sign in to comment.