Skip to content

Commit

Permalink
Update to_xugrid and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
visr committed Mar 29, 2024
1 parent 0186a86 commit 521e1c5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
8 changes: 1 addition & 7 deletions python/ribasim/ribasim/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,12 +401,6 @@ def to_xugrid(self):
name="node_index",
)

if node_df.crs is None:
# TODO: can be removed when CRS is required, #1254
projected = False
else:
projected = node_df.crs.is_projected

grid = xugrid.Ugrid1d(
node_x=node_df.geometry.x,
node_y=node_df.geometry.y,
Expand All @@ -418,7 +412,7 @@ def to_xugrid(self):
)
),
name="ribasim",
projected=projected,
projected=node_df.crs.is_projected,
crs=node_df.crs,
)

Expand Down
13 changes: 13 additions & 0 deletions python/ribasim/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest
import xugrid
from pydantic import ValidationError
from pyproj import CRS
from ribasim.config import Solver
from ribasim.geometry.edge import NodeData
from ribasim.input_base import esc_id
Expand Down Expand Up @@ -139,6 +140,7 @@ def test_tabulated_rating_curve_model(tabulated_rating_curve, tmp_path):
basin_area = tabulated_rating_curve.basin.area.df
assert basin_area is not None
assert basin_area.geometry.geom_type.iloc[0] == "Polygon"
assert basin_area.crs == CRS.from_epsg(28992)
model_orig.write(tmp_path / "tabulated_rating_curve/ribasim.toml")
model_new = Model.read(tmp_path / "tabulated_rating_curve/ribasim.toml")
pd.testing.assert_series_equal(
Expand Down Expand Up @@ -186,6 +188,16 @@ def test_node_table(basic):
assert df.subnetwork_id.dtype == pd.Int32Dtype()
assert df.node_type.iloc[0] == "Basin"
assert df.node_type.iloc[-1] == "Terminal"
assert df.crs == CRS.from_epsg(28992)


def test_edge_table(basic):
model = basic
df = model.edge.df
assert df.geometry.is_unique
assert df.from_node_id.dtype == np.int32
assert df.subnetwork_id.dtype == pd.Int32Dtype()
assert df.crs == CRS.from_epsg(28992)


def test_indexing(basic):
Expand Down Expand Up @@ -224,6 +236,7 @@ def test_xugrid(basic, tmp_path):
assert isinstance(uds, xugrid.UgridDataset)
assert uds.grid.edge_dimension == "ribasim_nEdges"
assert uds.grid.node_dimension == "ribasim_nNodes"
assert uds.grid.crs == CRS.from_epsg(28992)
assert uds.node_id.dtype == np.int32
uds.ugrid.to_netcdf(tmp_path / "ribasim.nc")
uds = xugrid.open_dataset(tmp_path / "ribasim.nc")
Expand Down

0 comments on commit 521e1c5

Please sign in to comment.