Skip to content

Commit

Permalink
abstract duplicate lines in the parameter and initial condition conve…
Browse files Browse the repository at this point in the history
…rters
  • Loading branch information
MAfarrag committed Dec 9, 2024
1 parent c4e5892 commit 540f4e4
Showing 1 changed file with 38 additions and 61 deletions.
99 changes: 38 additions & 61 deletions hydrolib/tools/ext_old_to_new/converters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from typing import Any
from typing import Any, Dict

from hydrolib.core.basemodel import DiskOnlyFileModel
from hydrolib.core.dflowfm.bc.models import ForcingModel
Expand Down Expand Up @@ -160,6 +160,39 @@ def convert(self, forcing: ExtOldForcing) -> Boundary:
return new_block


def create_convert_inputs(forcing: ExtOldForcing) -> Dict[str, str]:
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

return block_data


class InitialConditionConverter(BaseConverter):

def __init__(self):
Expand Down Expand Up @@ -193,36 +226,8 @@ def convert(self, forcing: ExtOldForcing) -> InitialField:
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)
data = create_convert_inputs(forcing)
new_block = InitialField(**data)

return new_block

Expand Down Expand Up @@ -260,36 +265,8 @@ def convert(self, forcing: ExtOldForcing) -> ParameterField:
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 = ParameterField(**block_data)
data = create_convert_inputs(forcing)
new_block = ParameterField(**data)

return new_block

Expand Down

0 comments on commit 540f4e4

Please sign in to comment.