Skip to content

Commit

Permalink
move the initial_condition_converter to the converters module
Browse files Browse the repository at this point in the history
  • Loading branch information
MAfarrag committed Dec 2, 2024
1 parent 215c05e commit 6c5590e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 80 deletions.
5 changes: 3 additions & 2 deletions hydrolib/tools/ext_old_to_new/converter_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
ExtOldMeteoQuantity,
)
from hydrolib.tools.ext_old_to_new.base_converter import BaseConverter
from hydrolib.tools.ext_old_to_new.initial_condition_converter import (
from hydrolib.tools.ext_old_to_new.converters import (
InitialConditionConverter,
MeteoConverter,
)
from hydrolib.tools.ext_old_to_new.meteo_converter import MeteoConverter
# BoundaryConditionConverter


def __contains__(cls, item):
Expand Down
71 changes: 68 additions & 3 deletions hydrolib/tools/ext_old_to_new/converters.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from abc import abstractmethod

from hydrolib.core.basemodel import DiskOnlyFileModel
from hydrolib.core.dflowfm.ext.models import Meteo
from hydrolib.core.dflowfm.extold.models import ExtOldForcing
from hydrolib.core.dflowfm.inifield.models import InterpolationMethod
from hydrolib.core.dflowfm.inifield.models import InitialField, InterpolationMethod
from hydrolib.tools.ext_old_to_new.enum_converters import (
oldfiletype_to_forcing_file_type,
oldmethod_to_averaging_type,
Expand Down Expand Up @@ -138,3 +136,70 @@ def convert(self, forcing: ExtOldForcing) -> Meteo:
meteo_block = Meteo(**meteo_data)

return meteo_block


class InitialConditionConverter(BaseConverter):

def __init__(self):
super().__init__()

def convert(self, forcing: ExtOldForcing) -> InitialField:
"""Convert an old external forcing block with meteo data to a Meteo
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 Meteo object is suitable for use in new
external forcings files, adhering to the updated format and
specifications.
Args:
forcing (ExtOldForcing): The contents of a single forcing block
in an old external forcings file. This object contains all the
necessary information, such as quantity, values, and timestamps,
required for the conversion process.
Returns:
Meteo: A Meteo object that represents the converted forcing
block, ready to be included in a new external forcings file. The
Meteo 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.
"""
block_data = {
"quantity": forcing.quantity,
"datafile": forcing.filename,
"datafiletype": oldfiletype_to_forcing_file_type(forcing.filetype),
}
if block_data["datafiletype"] == "polygon":
block_data["value"] = forcing.value

if forcing.sourcemask != DiskOnlyFileModel(None):
raise ValueError(
f"Attribute 'SOURCEMASK' is no longer supported, cannot "
f"convert this input. Encountered for QUANTITY="
f"{forcing.quantity} and FILENAME={forcing.filename}."
)
block_data["interpolationmethod"] = oldmethod_to_interpolation_method(
forcing.method
)
if block_data["interpolationmethod"] == InterpolationMethod.averaging:
block_data["averagingtype"] = oldmethod_to_averaging_type(forcing.method)
block_data["averagingrelsize"] = forcing.relativesearchcellsize
block_data["averagingnummin"] = forcing.nummin
block_data["averagingpercentile"] = forcing.percentileminmax
block_data["operand"] = forcing.operand

if hasattr(forcing, "extrapolation"):
block_data["extrapolationmethod"] = forcing.extrapolation
if hasattr(forcing, "locationtype"):
block_data["locationtype"] = forcing.locationtype

new_block = InitialField(**block_data)

return new_block
75 changes: 0 additions & 75 deletions hydrolib/tools/ext_old_to_new/initial_condition_converter.py

This file was deleted.

0 comments on commit 6c5590e

Please sign in to comment.