Skip to content

Commit

Permalink
Slim mesh api
Browse files Browse the repository at this point in the history
  • Loading branch information
ecomodeller committed Mar 12, 2024
1 parent 6a701f0 commit 99cedf7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build: typecheck test
python -m build

lint:
ruff .
ruff check .

pylint:
pylint --disable=all --enable=attribute-defined-outside-init mikeio/
Expand Down
52 changes: 7 additions & 45 deletions mikeio/dfsu/_mesh.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
from __future__ import annotations
from pathlib import Path
from typing import TYPE_CHECKING
from collections.abc import Collection
import warnings


import numpy as np

from mikecore.eum import eumQuantity
from mikecore.MeshBuilder import MeshBuilder

from mikecore.MeshFile import MeshFile

from ..eum import EUMType, EUMUnit

from ..spatial import GeometryFM2D
from ._common import (
get_elements_from_source,
get_nodes_from_source,
element_table_to_mikecore,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -48,20 +45,8 @@ class Mesh:
"""

def __init__(self, filename: str | Path) -> None:
ext = Path(filename).suffix.lower()

if ext == ".mesh":
self.geometry: GeometryFM2D = self._read_header(filename)
elif ext == ".dfsu":
# TODO remove in v 1.8
import mikeio

warnings.warn(
f'Reading dfsu with `Mesh` is deprecated. Read a .dfsu geometry with `geom = mikeio.open("{filename}").geometry`',
FutureWarning,
)
self.geometry = mikeio.open(str(filename)).geometry

self.geometry: GeometryFM2D = self._read_header(filename)
self.plot = self.geometry.plot

def _read_header(self, filename: str | Path) -> GeometryFM2D:
Expand Down Expand Up @@ -128,51 +113,28 @@ def zn(self) -> np.ndarray:
return self.geometry.node_coordinates[:, 2]

@zn.setter
def zn(self, v: np.ndarray):
def zn(self, v: np.ndarray) -> None:
if len(v) != self.n_nodes:
raise ValueError(f"zn must have length of nodes ({self.n_nodes})")
self.geometry._nc[:, 2] = v

def write(
self,
outfilename: str | Path,
elements: Collection[int] | None = None,
unit: EUMUnit = EUMUnit.meter,
) -> None:
"""write mesh to file
Parameters
----------
outfilename : str
path to file
elements : list(int)
list of element ids (subset) to be saved to new mesh
"""
# TODO reconsider if selection of elements should be done here, it is possible to do with geometry.isel and then write the mesh
builder = MeshBuilder()

if elements is not None:
geometry = self.geometry.isel(elements)
else:
geometry = self.geometry
geometry = self.geometry

assert isinstance(geometry, GeometryFM2D) # i.e. not a GeometryPoint2d

quantity = eumQuantity.Create(EUMType.Bathymetry, unit)
elem_table = element_table_to_mikecore(geometry.element_table)

nc = geometry.node_coordinates
builder.SetNodes(nc[:, 0], nc[:, 1], nc[:, 2], geometry.codes)

builder.SetElements(elem_table)
builder.SetProjection(geometry.projection_string)
builder.SetEumQuantity(quantity)

newMesh = builder.CreateMesh()
newMesh.Write(outfilename)

def plot_boundary_nodes(self, boundary_names=None, figsize=None, ax=None) -> None:
return self.geometry.plot.boundary_nodes(boundary_names, figsize, ax)
self.geometry.to_mesh(outfilename=outfilename)

def to_shapely(self) -> MultiPolygon:
"""Convert Mesh geometry to shapely MultiPolygon
Expand Down
4 changes: 2 additions & 2 deletions tests/test_dfsu_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def test_plot_mesh_ax():

def test_plot_mesh_boundary_nodes():
msh = Mesh("tests/testdata/odense_rough.mesh")
msh.plot_boundary_nodes()
msh.plot_boundary_nodes(["Land", "Sea"])
msh.plot.boundary_nodes()
msh.plot.boundary_nodes(["Land", "Sea"])
assert True


Expand Down
18 changes: 0 additions & 18 deletions tests/test_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,6 @@ def test_write(tri_mesh, tmp_path):
assert outfilename.exists()


def test_write_part(tri_mesh, tmp_path):
outfilename = tmp_path / "simple_sub.mesh"

msh = tri_mesh

msh.write(outfilename, elements=list(range(0, 100)))

assert outfilename.exists()


def test_write_part_isel(tri_mesh, tmp_path):
outfilename = tmp_path / "simple_sub.mesh"

Expand Down Expand Up @@ -160,14 +150,6 @@ def test_write_mesh_from_dfsu(tmp_path):
assert np.all(np.hstack(msh2.element_table) == np.hstack(geometry.element_table))


def test_open_dfsu_as_mesh_is_removed_give_hint():
dfsufilename = "tests/testdata/FakeLake.dfsu"
with pytest.warns(FutureWarning):
msh = Mesh(dfsufilename)

msh.plot()


def test_to_shapely(tri_mesh) -> None:
msh = tri_mesh
shp = msh.to_shapely()
Expand Down

0 comments on commit 99cedf7

Please sign in to comment.