From f98b761774241887c6f0c26a0eb37eb9d43004f9 Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Wed, 31 Jan 2024 15:18:34 +0100 Subject: [PATCH 1/2] use new contacts_set from meshkernel 4.0.2 --- hydrolib/core/dflowfm/net/reader.py | 16 ++++++---------- tests/dflowfm/test_net.py | 6 ++---- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/hydrolib/core/dflowfm/net/reader.py b/hydrolib/core/dflowfm/net/reader.py index 56ce913ee..d3ad8cd98 100644 --- a/hydrolib/core/dflowfm/net/reader.py +++ b/hydrolib/core/dflowfm/net/reader.py @@ -10,6 +10,7 @@ import numpy as np from hydrolib.core.basemodel import BaseModel +from meshkernel import Contacts if TYPE_CHECKING: from hydrolib.core.dflowfm.net.models import Link1d2d, Mesh1d, Mesh2d @@ -123,16 +124,11 @@ def read_link1d2d(self, link1d2d: Link1d2d) -> None: for meshkey, nckey in self._explorer.link1d2d_var_name_mapping.items(): setattr(link1d2d, meshkey, self._read_nc_attribute(ds[nckey])) - # TODO: setting contacts is not possible yet in meshkernel - # https://github.com/Deltares/MeshKernelPy/issues/107 - # https://github.com/Deltares/HYDROLIB-core/issues/575 - # so misalignment between link1d2d.link1d2d and - # empty _link1d2d.meshkernel.contacts_get().mesh2d_indices - # mesh1d_indices = link1d2d.link1d2d[:,0] - # mesh2d_indices = link1d2d.link1d2d[:,1] - # import meshkernel as mk - # contacts = mk.Contacts(mesh1d_indices=mesh1d_indices, mesh2d_indices=mesh2d_indices) - # link1d2d.meshkernel.contacts_set(contacts) + # set contacts on meshkernel, use .copy() to avoid strided arrays + mesh1d_indices = link1d2d.link1d2d[:,0].copy() + mesh2d_indices = link1d2d.link1d2d[:,1].copy() + contacts = Contacts(mesh1d_indices=mesh1d_indices, mesh2d_indices=mesh2d_indices) + link1d2d.meshkernel.contacts_set(contacts) ds.close() diff --git a/tests/dflowfm/test_net.py b/tests/dflowfm/test_net.py index b7cfacb5b..4a0a1e691 100644 --- a/tests/dflowfm/test_net.py +++ b/tests/dflowfm/test_net.py @@ -160,10 +160,8 @@ def test_create_1d_2d_1d2d(): network2_con_m1d = network2._link1d2d.meshkernel.contacts_get().mesh1d_indices network2_con_m2d = network2._link1d2d.meshkernel.contacts_get().mesh2d_indices assert network2_link1d2d.shape == (21, 2) - # TODO: below asserts fail, since the meshkernel contacts are not set upon reading - # https://github.com/Deltares/HYDROLIB-core/issues/575 - # assert network2_con_m1d.size == 21 - # assert network2_con_m2d.size == 21 + assert network2_con_m1d.size == 21 + assert network2_con_m2d.size == 21 # plot both networks import matplotlib.pyplot as plt From 6dd556f9a8745c88e551e4d6d5d488810985529a Mon Sep 17 00:00:00 2001 From: priscavdsluis Date: Wed, 31 Jan 2024 14:46:00 +0000 Subject: [PATCH 2/2] autoformat: isort & black --- hydrolib/core/dflowfm/net/reader.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/hydrolib/core/dflowfm/net/reader.py b/hydrolib/core/dflowfm/net/reader.py index d3ad8cd98..96cb3100f 100644 --- a/hydrolib/core/dflowfm/net/reader.py +++ b/hydrolib/core/dflowfm/net/reader.py @@ -8,9 +8,9 @@ import netCDF4 as nc import numpy as np +from meshkernel import Contacts from hydrolib.core.basemodel import BaseModel -from meshkernel import Contacts if TYPE_CHECKING: from hydrolib.core.dflowfm.net.models import Link1d2d, Mesh1d, Mesh2d @@ -125,9 +125,11 @@ def read_link1d2d(self, link1d2d: Link1d2d) -> None: setattr(link1d2d, meshkey, self._read_nc_attribute(ds[nckey])) # set contacts on meshkernel, use .copy() to avoid strided arrays - mesh1d_indices = link1d2d.link1d2d[:,0].copy() - mesh2d_indices = link1d2d.link1d2d[:,1].copy() - contacts = Contacts(mesh1d_indices=mesh1d_indices, mesh2d_indices=mesh2d_indices) + mesh1d_indices = link1d2d.link1d2d[:, 0].copy() + mesh2d_indices = link1d2d.link1d2d[:, 1].copy() + contacts = Contacts( + mesh1d_indices=mesh1d_indices, mesh2d_indices=mesh2d_indices + ) link1d2d.meshkernel.contacts_set(contacts) ds.close()