Skip to content

Dimr

DIMR xml files

Model

Component (BaseModel, ABC) pydantic-model

Specification of a BMI-compliant model component instance that will be executed by DIMR.

Attributes:

Name Type Description
library str

The library name of the compoment.

name str

The component name.

workingDir Path

The working directory.

inputFile Path

The name of the input file.

process Optional[int]

Number of subprocesses in the component.

setting Optional[List[hydrolib.core.io.dimr.models.KeyValuePair]]

A list of variables that are provided to the BMI model before initialization.

parameter Optional[List[hydrolib.core.io.dimr.models.KeyValuePair]]

A list of variables that are provided to the BMI model after initialization.

mpiCommunicator Optional[str]

The MPI communicator value.

model Optional[hydrolib.core.basemodel.FileModel]

The model represented by this component.

Generic attribute for models that have children fields that could contain files.

Source code in hydrolib/core/io/dimr/models.py
def is_intermediate_link(self) -> bool:
    return True

ComponentOrCouplerRef (BaseModel) pydantic-model

Reference to a BMI-compliant model component instance.

Attributes:

Name Type Description
name str

Name of the reference to a BMI-compliant model component instance.

Control (BaseModel) pydantic-model

Control flow specification for the DIMR-execution.

Attributes:

Name Type Description
parallel Optional[List[hydrolib.core.io.dimr.models.Parallel]]

Specification of a control flow that has to be executed in parallel.

start Optional[List[hydrolib.core.io.dimr.models.ComponentOrCouplerRef]]

Reference to the component instance to be started.

Generic attribute for models that have children fields that could contain files.

Source code in hydrolib/core/io/dimr/models.py
def is_intermediate_link(self) -> bool:
    # TODO set to True once we replace Paths with FileModels
    return False

CoupledItem (BaseModel) pydantic-model

Specification of an item that has to be exchanged.

Attributes:

Name Type Description
sourceName str

Name of the item at the source component.

targetName str

Name of the item at the target component.

Generic attribute for models that have children fields that could contain files.

Source code in hydrolib/core/io/dimr/models.py
def is_intermediate_link(self) -> bool:
    # TODO set to True once we replace Paths with FileModels
    return False

Coupler (BaseModel) pydantic-model

Specification of the coupling actions to be performed between two BMI-compliant model components.

Attributes:

Name Type Description
name str

The name of the coupler.

sourceComponent str

The component that provides the data to has to be exchanged.

targetComponent str

The component that consumes the data to has to be exchanged.

item List[hydrolib.core.io.dimr.models.CoupledItem]

A list of items that have to be exchanged.

logger Optional[hydrolib.core.io.dimr.models.Logger]

Logger for logging the values that get exchanged.

Generic attribute for models that have children fields that could contain files.

Source code in hydrolib/core/io/dimr/models.py
def is_intermediate_link(self) -> bool:
    # TODO set to True once we replace Paths with FileModels
    return False

DIMR (FileModel) pydantic-model

DIMR model representation.

dict(self, *args, **kwargs)

Converts this object recursively to a dictionary.

Returns:

Type Description
dict

The created dictionary for this object.

Source code in hydrolib/core/io/dimr/models.py
def dict(self, *args, **kwargs):
    """Converts this object recursively to a dictionary.

    Returns:
        dict: The created dictionary for this object.
    """
    return self._to_serializable_dict(self)

Documentation (BaseModel) pydantic-model

Information on the present DIMR configuration file.

Attributes:

Name Type Description
fileVersion str

The DIMR file version.

createdBy str

Creators of the DIMR file.

creationDate datetime

The creation date of the DIMR file.

GlobalSettings (BaseModel) pydantic-model

Global settings for the DIMR configuration.

Attributes:

Name Type Description
logger_ncFormat int

NetCDF format type for logging.

KeyValuePair (BaseModel) pydantic-model

Key value pair to specify settings and parameters.

Attributes:

Name Type Description
key str

The key.

value str

The value.

Logger (BaseModel) pydantic-model

Used to log values to the specified file in workingdir for each timestep

Attributes:

Name Type Description
workingDir Path

Directory where the log file is written.

outputFile Path

Name of the log file.

Parallel (BaseModel) pydantic-model

Specification of a parallel control flow: one main component and a group of related components and couplers. Step wise execution order according to order in parallel control flow.

Attributes:

Name Type Description
startGroup StartGroup

Group of components and couplers to be executed.

start ComponentOrCouplerRef

Main component to be executed step wise (provides start time, end time and time step).

StartGroup (BaseModel) pydantic-model

Specification of model components and couplers to be executed with a certain frequency.

Attributes:

Name Type Description
time str

Time frame specification for the present group: start time, stop time and frequency. Expressed in terms of the time frame of the main component.

start List[hydrolib.core.io.dimr.models.ComponentOrCouplerRef]

Ordered list of components to be executed.

coupler List[hydrolib.core.io.dimr.models.ComponentOrCouplerRef]

Oredered list of couplers to be executed.

Parser

DIMRParser

A parser for DIMR xml files.

parse(path: Path) -> dict staticmethod

Parses a DIMR file to a dictionary.

Parameters:

Name Type Description Default
path Path

Path to the DIMR configuration file.

required
Source code in hydrolib/core/io/dimr/parser.py
@staticmethod
def parse(path: Path) -> dict:
    """Parses a DIMR file to a dictionary.

    Args:
        path (Path): Path to the DIMR configuration file.
    """
    if not path.is_file():
        warn(f"File: `{path}` not found, skipped parsing.")
        return {}

    parser = etree.XMLParser(
        remove_comments=True, resolve_entities=False, no_network=True
    )
    root = etree.parse(str(path), parser=parser).getroot()

    return DIMRParser._node_to_dictionary(root, True)

Serializer

DIMRSerializer

A serializer for DIMR files.

serialize(path: Path, data: dict) staticmethod

Serializes the DIMR data to the file at the specified path.

Attributes:

Name Type Description
path Path

The path to the destination file.

data Dict

The data to be serialized.

Source code in hydrolib/core/io/dimr/serializer.py
@staticmethod
def serialize(path: Path, data: dict):
    """
    Serializes the DIMR data to the file at the specified path.

    Attributes:
        path (Path): The path to the destination file.
        data (Dict): The data to be serialized.
    """

    path.parent.mkdir(parents=True, exist_ok=True)

    xmlns = "http://schemas.deltares.nl/dimr"
    xsi = "http://www.w3.org/2001/XMLSchema-instance"
    schema_location = "http://content.oss.deltares.nl/schemas/dimr-1.3.xsd"

    attrib = {e.QName(xsi, "schemaLocation"): f"{xmlns} {schema_location}"}
    namespaces = {None: xmlns, "xsi": xsi}

    root = e.Element(
        "dimrConfig",
        attrib=attrib,
        nsmap=namespaces,
    )
    DIMRSerializer._build_tree(root, data)

    to_string = minidom.parseString(e.tostring(root))
    xml = to_string.toprettyxml(indent="  ", encoding="utf-8")

    with path.open("wb") as f:
        f.write(xml)
Back to top