Skip to content

Commit

Permalink
Dommel basin profile (#142)
Browse files Browse the repository at this point in the history
With this branch we can create de dommel model version 2024.8.4 from
2024.6.3

All code used is updated to function with current ribasim-version (main)

---------

Co-authored-by: Martijn Visser <[email protected]>
  • Loading branch information
DanielTollenaar and visr authored Sep 5, 2024
1 parent dc1d72d commit e754aca
Show file tree
Hide file tree
Showing 9 changed files with 630 additions and 96 deletions.
82 changes: 28 additions & 54 deletions notebooks/de_dommel/01_fix_model_network.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# %%
import sqlite3

import geopandas as gpd
import pandas as pd
from ribasim import Node
from ribasim.nodes import basin, level_boundary, manning_resistance, outlet
from ribasim_nl import CloudStorage, Model, NetworkValidator
from shapely.geometry import Point

cloud = CloudStorage()

Expand All @@ -24,26 +23,6 @@
basin.State(level=[0]),
]

# %% remove urban_runoff
# Connect to the SQLite database

conn = sqlite3.connect(database_gpkg)

# get table into DataFrame
table = "Basin / static"
df = pd.read_sql_query(f"SELECT * FROM '{table}'", conn)

# drop urban runoff column if exists
if "urban_runoff" in df.columns:
df.drop(columns="urban_runoff", inplace=True)

# Write the DataFrame back to the SQLite table
df.to_sql(table, conn, if_exists="replace", index=False)

# # Close the connection
conn.close()


# %% read model
model = Model.read(ribasim_toml)

Expand All @@ -52,10 +31,7 @@
# %% verwijder duplicated edges

# see: https://github.com/Deltares/Ribasim-NL/issues/102#issuecomment-2288780504
model.edge.df = model.edge.df[~((model.edge.df.from_node_type == "Basin") & (model.edge.df.to_node_type == "Basin"))]

# see: https://github.com/Deltares/Ribasim-NL/issues/102#issuecomment-2291081244
duplicated_fids = network_validator.edge_duplicated().index.to_list()
model.edge.df = model.edge.df.drop_duplicates(subset=["from_node_id", "to_node_id"])

if not network_validator.edge_duplicated().empty:
Expand All @@ -64,20 +40,16 @@
# %% toevoegen bovenstroomse knopen

# see: https://github.com/Deltares/Ribasim-NL/issues/102#issuecomment-2291091067
edge_mask = model.edge.df.index.isin(duplicated_fids)
node_id = model.next_node_id
edge_fid = next(i for i in duplicated_fids if i in model.edge.df.index)
model.edge.df.loc[edge_mask, ["from_node_type"]] = "Basin"
model.edge.df.loc[edge_mask, ["from_node_id"]] = node_id
edge_id = model.edge.df.loc[model.edge.df.to_node_id == 251].index[0]
model.edge.df.loc[edge_id, ["from_node_id"]] = node_id

node = Node(node_id, model.edge.df.at[edge_fid, "geometry"].boundary.geoms[0])
node = Node(node_id, model.edge.df.at[edge_id, "geometry"].boundary.geoms[0])
model.basin.area.df.loc[model.basin.area.df.node_id == 1009, ["node_id"]] = node_id
area = basin.Area(geometry=model.basin.area[node_id].geometry.to_list())
model.basin.add(node, basin_data + [area])

# see: https://github.com/Deltares/Ribasim-NL/issues/102#issuecomment-2291111647


for row in network_validator.edge_incorrect_connectivity().itertuples():
# drop edge from model
model.remove_edge(row.from_node_id, row.to_node_id, remove_disconnected_nodes=False)
Expand All @@ -91,28 +63,31 @@
if row.to_node_id == 2:
geometry = row.geometry.interpolate(0.99, normalized=True)
name = ""
meta_object_type = "openwater"
if row.to_node_id == 14:
gdf = gpd.read_file(
cloud.joinpath("DeDommel", "verwerkt", "1_ontvangen_data", "Geodata", "data_Q42018.gpkg"),
layer="DuikerSifonHevel",
cloud.joinpath("DeDommel", "verwerkt", "4_ribasim", "hydamo.gpkg"),
layer="duikersifonhevel",
engine="pyogrio",
fid_as_index=True,
)
kdu = gdf.loc[5818]
kdu = gdf.loc[250]
geometry = kdu.geometry.interpolate(0.5, normalized=True)
name = kdu.objectId
geometry = Point(geometry.x, geometry.y)
name = kdu.CODE
meta_object_type = "duikersifonhevel"

# add manning-node
manning_node_id = model.next_node_id
manning_data = manning_resistance.Static(length=[100], manning_n=[0.04], profile_width=[10], profile_slope=[1])
model.manning_resistance.add(
Node(node_id=manning_node_id, geometry=geometry, name=name),
[manning_data],
outlet_node_id = model.next_node_id
outlet_data = outlet.Static(flow_rate=[100])
model.outlet.add(
Node(node_id=outlet_node_id, geometry=geometry, name=name, meta_object_type=meta_object_type),
[outlet_data],
)

# add edges
model.edge.add(model.basin[row.from_node_id], model.manning_resistance[manning_node_id])
model.edge.add(model.manning_resistance[manning_node_id], model.level_boundary[row.to_node_id])
model.edge.add(model.basin[row.from_node_id], model.outlet[outlet_node_id])
model.edge.add(model.outlet[outlet_node_id], model.level_boundary[row.to_node_id])


if not network_validator.edge_incorrect_connectivity().empty:
Expand All @@ -122,11 +97,11 @@

# see: https://github.com/Deltares/Ribasim-NL/issues/102#issuecomment-2291271525
for row in network_validator.node_internal_basin().itertuples():
if row.node_id not in model.basin.area.df.node_id.to_numpy(): # remove or change to level-boundary
edge_select_df = model.edge.df[model.edge.df.to_node_id == row.node_id]
if row.Index not in model.basin.area.df.node_id.to_numpy(): # remove or change to level-boundary
edge_select_df = model.edge.df[model.edge.df.to_node_id == row.Index]
if len(edge_select_df) == 1:
if edge_select_df.iloc[0]["from_node_type"] == "FlowBoundary":
model.remove_node(row.node_id)
if model.node_table().df.at[edge_select_df.iloc[0]["from_node_id"], "node_type"] == "FlowBoundary":
model.remove_node(row.Index)
model.remove_node(edge_select_df.iloc[0]["from_node_id"])
model.edge.df.drop(index=edge_select_df.index[0], inplace=True)

Expand Down Expand Up @@ -166,7 +141,7 @@

geometry = gdf.loc[2751].geometry.interpolate(0.5, normalized=True)
node_id = model.next_node_id
data = manning_resistance.Static(length=[100], manning_n=[0.04], profile_width=[10], profile_slope=[1])
manning_data = manning_resistance.Static(length=[100], manning_n=[0.04], profile_width=[10], profile_slope=[1])

model.manning_resistance.add(Node(node_id=node_id, geometry=geometry), [manning_data])

Expand All @@ -192,17 +167,16 @@
# # see: https://github.com/Deltares/Ribasim-NL/issues/132
model.basin.area.df.loc[model.basin.area.df.duplicated("node_id"), ["node_id"]] = -1
model.basin.area.df.reset_index(drop=True, inplace=True)
model.basin.area.df.index.name = "fid"
model.fix_unassigned_basin_area()
model.fix_unassigned_basin_area(method="closest", distance=100)
model.fix_unassigned_basin_area()

model.basin.area.df = model.basin.area.df[~model.basin.area.df.node_id.isin(model.unassigned_basin_area.node_id)]

# # %% write model
ribasim_toml = ribasim_toml.parents[1].joinpath("DeDommel", ribasim_toml.name)
model.write(ribasim_toml)
model.edge.df.reset_index(drop=True, inplace=True)
model.edge.df.index.name = "edge_id"
ribasim_toml = ribasim_toml = cloud.joinpath("DeDommel", "modellen", "DeDommel_fix_model_network", "model.toml")

# %% upload model

# cloud.upload_model("DeDommel", "DeDommel")
# %%
model.write(ribasim_toml)
9 changes: 5 additions & 4 deletions notebooks/de_dommel/02_fix_edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


# %% load model
ribasim_toml = cloud.joinpath("DeDommel", "modellen", "DeDommel", "model.toml")
ribasim_toml = cloud.joinpath("DeDommel", "modellen", "DeDommel_fix_model_network", "model.toml")
model = Model.read(ribasim_toml)

# %% network from HydroObjects
Expand All @@ -15,13 +15,13 @@
network = Network.from_network_gpkg(network_gpkg)
else:
network = Network.from_lines_gpkg(
cloud.joinpath("DeDommel", "verwerkt", "2_voorbewerking", "hydamo.gpkg"), layer="hydroobject"
cloud.joinpath("DeDommel", "verwerkt", "4_ribasim", "hydamo.gpkg"), layer="hydroobject"
)

network.to_file(network_gpkg)
# %% edges follow HydroObjects
model.reset_edge_geometry()
node_df = model.node_table().df.set_index("node_id")
node_df = model.node_table().df
data = []
for row in model.edge.df.itertuples():
try:
Expand Down Expand Up @@ -58,4 +58,5 @@
gpd.GeoDataFrame(data, crs=28992).to_file("rare_edges.gpkg")

# %% write model
# model.write(ribasim_toml)
ribasim_toml = cloud.joinpath("DeDommel", "modellen", "DeDommel_fix_edges", "model.toml")
model.write(ribasim_toml)
9 changes: 5 additions & 4 deletions notebooks/de_dommel/03_fix_basin_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


# %% load model
ribasim_toml = cloud.joinpath("DeDommel", "modellen", "DeDommel", "model.toml")
ribasim_toml = cloud.joinpath("DeDommel", "modellen", "DeDommel_fix_edges", "model.toml")
model = Model.read(ribasim_toml)


Expand All @@ -23,7 +23,7 @@
dissolved_area_gdf.to_file(cloud.joinpath("DeDommel", "verwerkt", "water_area.gpkg"))

# %%
basin_df = model.basin.node.df.set_index("node_id")
basin_df = model.basin.node.df
basin_area_df = gpd.read_file(
cloud.joinpath("DeDommel", "verwerkt", "basin_area.gpkg"), engine="pyogrio", fid_as_index=True
)
Expand Down Expand Up @@ -51,7 +51,7 @@
area_df = selected_areas[selected_areas.contains(row.geometry)]
if not area_df.empty:
name = area_df.iloc[0].NAAM
model.basin.node.df.loc[model.basin.node[row.Index].index[0], ["name"]] = name
model.basin.node.df.loc[row.Index, ["name"]] = name
# assign name to edges if defined
model.edge.df.loc[edges_mask, ["name"]] = name

Expand All @@ -75,7 +75,8 @@

area_df = gpd.GeoDataFrame(data, crs=model.basin.node.df.crs)
area_df = area_df[~area_df.is_empty]
area_df.index.name = "fid"
model.basin.area.df = area_df
# %%

ribasim_toml = cloud.joinpath("DeDommel", "modellen", "DeDommel_fix_areas", "model.toml")
model.write(ribasim_toml)
Loading

0 comments on commit e754aca

Please sign in to comment.