diff --git a/hydrolib/tools/ext_old_to_new/main_converter.py b/hydrolib/tools/ext_old_to_new/main_converter.py index 2e03c4e1e..971638b08 100644 --- a/hydrolib/tools/ext_old_to_new/main_converter.py +++ b/hydrolib/tools/ext_old_to_new/main_converter.py @@ -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. @@ -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( @@ -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}") @@ -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 @@ -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) @@ -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( diff --git a/tests/tools/test_main_converter.py b/tests/tools/test_main_converter.py index 8ea7ee140..1b87fb100 100644 --- a/tests/tools/test_main_converter.py +++ b/tests/tools/test_main_converter.py @@ -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 ) @@ -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