Skip to content

Net

Branch

interpolate(self, distance: Union[float, np.ndarray]) -> np.ndarray

Interpolate coordinates along branch by length

TODO: How to handle these kind of union datatypes? The array should also consist of floats

Parameters:

Name Type Description Default
distance Union[float, np.ndarray]

Length

required
Source code in hydrolib/core/io/net/models.py
def interpolate(self, distance: Union[float, np.ndarray]) -> np.ndarray:
    """Interpolate coordinates along branch by length
    #TODO: How to handle these kind of union datatypes? The array should also consist of floats
    Args:
        distance (Union[float, np.ndarray]): Length
    """
    intpcoords = np.stack(
        [
            np.interp(distance, self._distance, self._x_coordinates),
            np.interp(distance, self._distance, self._y_coordinates),
        ],
        axis=1,
    )

    return intpcoords

Link1d2d (BaseModel) pydantic-model

clear(self) -> None

Remove all saved links from the links administration

Source code in hydrolib/core/io/net/models.py
def clear(self) -> None:
    """Remove all saved links from the links administration"""
    self.link1d2d_id = np.empty(0, object)
    self.link1d2d_long_name = np.empty(0, object)
    self.link1d2d_contact_type = np.empty(0, np.int32)
    self.link1d2d = np.empty((0, 2), np.int32)

Mesh1d (BaseModel) pydantic-model

get_node_mask(self, branchids: List[str] = None)

Get node mask, give a mask with True for each node that is in the given branchid list

Source code in hydrolib/core/io/net/models.py
def get_node_mask(self, branchids: List[str] = None):
    """Get node mask, give a mask with True for each node that is in the given branchid list"""

    mask = np.full(self.mesh1d_node_id.shape, False, dtype=bool)
    if branchids is None:
        mask[:] = True
        return mask

    # Get number (index) of given branches
    idx = np.where(np.isin(self.network1d_branch_id, branchids))[0]
    if idx.size == 0:
        raise KeyError("No branches corresponding to the given keys were found.")

    mask[np.isin(self.mesh1d_node_branch_id, idx)] = True

    return mask

Mesh2d (BaseModel) pydantic-model

clip(self, polygon: mk.GeometryList, deletemeshoption: int = 1)

Clip the 2D mesh by a polygon. Both outside the exterior and inside the interiors is clipped

Parameters:

Name Type Description Default
polygon GeometryList

Polygon stored as GeometryList

required
deletemeshoption int

[description]. Defaults to 1.

1
Source code in hydrolib/core/io/net/models.py
def clip(self, polygon: mk.GeometryList, deletemeshoption: int = 1):
    """Clip the 2D mesh by a polygon. Both outside the exterior and inside the interiors is clipped

    Args:
        polygon (GeometryList): Polygon stored as GeometryList
        deletemeshoption (int, optional): [description]. Defaults to 1.
    """

    # Add current mesh to Mesh2d instance
    self._set_mesh2d()

    # Delete outside polygon
    deletemeshoption = mk.DeleteMeshOption(deletemeshoption)
    parts = split_by(polygon, -998.0)

    # Check if parts are closed
    for part in parts:
        if not (part.x_coordinates[0], part.y_coordinates[0]) == (
            part.x_coordinates[-1],
            part.y_coordinates[-1],
        ):
            raise ValueError(
                "First and last coordinate of each GeometryList part should match."
            )

    self.meshkernel.mesh2d_delete(parts[0], deletemeshoption, True)

    # Delete all holes
    for interior in parts[1:]:
        self.meshkernel.mesh2d_delete(interior, deletemeshoption, False)

    # Process
    self._process(self.meshkernel.mesh2d_get())

create_rectilinear(self, extent: tuple, dx: float, dy: float) -> None

Create a rectilinear mesh within a polygon. A rectangular grid is generated within the polygon bounds

Parameters:

Name Type Description Default
extent tuple

Bounding box of mesh (left, bottom, right, top)

required
dx float

Horizontal distance

required
dy float

Vertical distance

required

TODO: Perhaps the polygon processing part should be part of Hydrolib (not core)!

Exceptions:

Type Description
NotImplementedError

MultiPolygons

Source code in hydrolib/core/io/net/models.py
def create_rectilinear(self, extent: tuple, dx: float, dy: float) -> None:
    """Create a rectilinear mesh within a polygon. A rectangular grid is generated within the polygon bounds

    Args:
        extent (tuple): Bounding box of mesh (left, bottom, right, top)
        dx (float): Horizontal distance
        dy (float): Vertical distance

    TODO: Perhaps the polygon processing part should be part of Hydrolib (not core)!

    Raises:
        NotImplementedError: MultiPolygons
    """

    xmin, ymin, xmax, ymax = extent

    # Generate mesh
    mesh2d_input = mk.Mesh2dFactory.create_rectilinear_mesh(
        rows=int((ymax - ymin) / dy),
        columns=int((xmax - xmin) / dx),
        origin_x=xmin,
        origin_y=ymin,
        spacing_x=dx,
        spacing_y=dy,
    )

    # Process
    self._process(mesh2d_input)

get_mesh2d(self) -> mk.Mesh2d

Return mesh2d from meshkernel

Source code in hydrolib/core/io/net/models.py
def get_mesh2d(self) -> mk.Mesh2d:
    """Return mesh2d from meshkernel"""
    return self.meshkernel.mesh2d_get()

refine(self, polygon: mk.GeometryList, level: int)

Refine the mesh within a polygon, by a number of steps (level)

Parameters:

Name Type Description Default
polygon GeometryList

Polygon in which to refine

required
level int

Number of refinement steps

required
Source code in hydrolib/core/io/net/models.py
def refine(self, polygon: mk.GeometryList, level: int):
    """Refine the mesh within a polygon, by a number of steps (level)

    Args:
        polygon (GeometryList): Polygon in which to refine
        level (int): Number of refinement steps
    """
    # Add current mesh to Mesh2d instance
    mesh2d_input = mk.Mesh2d(
        node_x=self.mesh2d_node_x,
        node_y=self.mesh2d_node_y,
        edge_nodes=self.mesh2d_edge_nodes.ravel(),
    )
    self.meshkernel.mesh2d_set(mesh2d_input)

    # Check if parts are closed
    if not (polygon.x_coordinates[0], polygon.y_coordinates[0]) == (
        polygon.x_coordinates[-1],
        polygon.y_coordinates[-1],
    ):
        raise ValueError(
            "First and last coordinate of each GeometryList part should match."
        )

    parameters = mk.MeshRefinementParameters(
        refine_intersected=True,
        use_mass_center_when_refining=False,
        min_face_size=10.0,  # Does nothing?
        refinement_type=1,
        connect_hanging_nodes=True,
        account_for_samples_outside_face=False,
        max_refinement_iterations=level,
    )
    self.meshkernel.mesh2d_refine_based_on_polygon(polygon, parameters)

    # Process
    self._process(self.meshkernel.mesh2d_get())

Network

from_file(file_path: Path) -> Network classmethod

Read network from file. This classmethod checks what mesh components (mesh1d & network1d, mesh2d, link1d2d) are present, and loads them one by one.

Parameters:

Name Type Description Default
file Path

path to netcdf file with network data

required

Returns:

Type Description
Network

The instance of the class itself that is returned

Source code in hydrolib/core/io/net/models.py
@classmethod
def from_file(cls, file_path: Path) -> Network:
    """Read network from file. This classmethod checks what mesh components (mesh1d & network1d, mesh2d, link1d2d) are
    present, and loads them one by one.

    Args:
        file (Path): path to netcdf file with network data

    Returns:
        Network: The instance of the class itself that is returned
    """

    network = cls()
    ds = nc.Dataset(file_path)  # type: ignore[import]

    reader = UgridReader(file_path)

    if reader._explorer.mesh1d_key is not None:
        reader.read_mesh1d_network1d(network._mesh1d)

    if reader._explorer.mesh2d_key is not None:
        reader.read_mesh2d(network._mesh2d)

    if reader._explorer.link1d2d_key is not None:
        reader.read_link1d2d(network._link1d2d)

    ds.close()

    return network

to_file(self, file: Path) -> None

Write network to file

Parameters:

Name Type Description Default
file Path

File where _net.nc is written to.

required
Source code in hydrolib/core/io/net/models.py
def to_file(self, file: Path) -> None:
    """Write network to file

    Args:
        file (Path): File where _net.nc is written to.
    """

    writer = UgridWriter()
    writer.write(self, file)

NetworkModel (FileModel) pydantic-model

Network model representation.

split_by(gl: mk.GeometryList, by: float) -> list

Function to split mk.GeometryList by seperator.

Source code in hydrolib/core/io/net/models.py
def split_by(gl: mk.GeometryList, by: float) -> list:
    """Function to split mk.GeometryList by seperator."""
    x, y = gl.x_coordinates.copy(), gl.y_coordinates.copy()
    idx = np.where(x == by)[0]

    xparts = np.split(x, idx)
    yparts = np.split(y, idx)

    lists = [
        mk.GeometryList(xp[min(i, 1) :], yp[min(i, 1) :])
        for i, (xp, yp) in enumerate(zip(xparts, yparts))
    ]

    return lists
Back to top