Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
visr committed Mar 25, 2024
1 parent 0acbbed commit 3400abe
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions python/ribasim/ribasim/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path
from typing import Any

import numpy as np
import pandas as pd
import tomli
import tomli_w
Expand All @@ -15,6 +16,11 @@
model_validator,
)

try:
import xugrid
except ImportError:
xugrid = None

Check warning on line 22 in python/ribasim/ribasim/model.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/model.py#L21-L22

Added lines #L21 - L22 were not covered by tests

import ribasim
from ribasim.config import (
Allocation,
Expand Down Expand Up @@ -330,3 +336,58 @@ def plot(self, ax=None, indicate_subnetworks: bool = True) -> Any:
ax.legend(handles, labels, loc="lower left", bbox_to_anchor=(1, 0.5))

return ax

def to_xugrid(self) -> xugrid.UgridDataset:
"""Convert the network to a xugrid.UgridDataset."""
node_df = self.node_table().df

Check warning on line 342 in python/ribasim/ribasim/model.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/model.py#L342

Added line #L342 was not covered by tests

# This will need to be adopted for locally unique node IDs,
# otherwise the `node_lookup` with `argsort` is not correct.
assert node_df.node_id.is_unique
node_df.sort_values("node_id", inplace=True)

Check warning on line 347 in python/ribasim/ribasim/model.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/model.py#L346-L347

Added lines #L346 - L347 were not covered by tests

edge_df = self.edge.df.copy()

Check warning on line 349 in python/ribasim/ribasim/model.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/model.py#L349

Added line #L349 was not covered by tests
# We assume only the flow network is of interest.
edge_df = edge_df[edge_df.edge_type == "flow"]

Check warning on line 351 in python/ribasim/ribasim/model.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/model.py#L351

Added line #L351 was not covered by tests

node_id = node_df.node_id.to_numpy(dtype="int32")
from_node_id = edge_df.from_node_id.to_numpy(dtype="int32")
to_node_id = edge_df.to_node_id.to_numpy(dtype="int32")

Check warning on line 355 in python/ribasim/ribasim/model.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/model.py#L353-L355

Added lines #L353 - L355 were not covered by tests

# from node_id to the node_dim index
node_lookup = pd.Series(

Check warning on line 358 in python/ribasim/ribasim/model.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/model.py#L358

Added line #L358 was not covered by tests
index=node_id,
data=node_id.argsort().astype("int32"),
name="node_index",
)

if node_df.crs is None:

Check warning on line 364 in python/ribasim/ribasim/model.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/model.py#L364

Added line #L364 was not covered by tests
# can be removed when CRS is required, #1254
projected = False

Check warning on line 366 in python/ribasim/ribasim/model.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/model.py#L366

Added line #L366 was not covered by tests
else:
projected = node_df.crs.is_projected

Check warning on line 368 in python/ribasim/ribasim/model.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/model.py#L368

Added line #L368 was not covered by tests

grid = xugrid.Ugrid1d(

Check warning on line 370 in python/ribasim/ribasim/model.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/model.py#L370

Added line #L370 was not covered by tests
node_x=node_df.geometry.x,
node_y=node_df.geometry.y,
fill_value=-1,
edge_node_connectivity=np.column_stack(
(
node_lookup[from_node_id],
node_lookup[to_node_id],
)
),
name="ribasim",
projected=projected,
crs=node_df.crs,
)

edge_dim = grid.edge_dimension
node_dim = grid.node_dimension

Check warning on line 386 in python/ribasim/ribasim/model.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/model.py#L385-L386

Added lines #L385 - L386 were not covered by tests

uds = xugrid.UgridDataset(None, grid)
uds = uds.assign_coords(node_id=(node_dim, node_id))
uds = uds.assign_coords(from_node_id=(edge_dim, from_node_id))
uds = uds.assign_coords(to_node_id=(edge_dim, to_node_id))

Check warning on line 391 in python/ribasim/ribasim/model.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/model.py#L388-L391

Added lines #L388 - L391 were not covered by tests

return uds

Check warning on line 393 in python/ribasim/ribasim/model.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/model.py#L393

Added line #L393 was not covered by tests

0 comments on commit 3400abe

Please sign in to comment.