Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run AGV shortest_path and crossings_to_ribasim #178

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pixi.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14706,10 +14706,12 @@ packages:
name: peilbeheerst-model
version: 0.1.0
path: src/peilbeheerst_model
sha256: e8c270f68d683c802990a8bb905cd665fd327b33e1d17bf3e01dfe9c49ec335e
sha256: 9ec22544800123361c4449542c548c1e3739a3f8d9d5bbe2f91a8d50d304fac5
requires_dist:
- fiona
- geopandas
- matplotlib
- networkx
- numpy
- pandas
- pydantic
Expand Down
50 changes: 45 additions & 5 deletions src/peilbeheerst_model/Shortest_path/10_shortest_path_AGV.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,47 @@
from peilbeheerst_model import shortest_path_waterschap
# The AGV shortest path code is identical to the other water boards.
# Below the content of shortest_path_waterschappen is spelled out to be able to
# point to the right data from the cloud storage.

import fiona
import geopandas as gpd
import pandas as pd
from shapely.wkt import dumps

from peilbeheerst_model.shortest_path import shortest_path
from ribasim_nl import CloudStorage

waterschap = "AmstelGooienVecht"
gdf_out = shortest_path_waterschap(waterschap)
gdf_out.to_file(
f"/DATAFOLDER/projects/4750_30/Data_shortest_path/{waterschap}/{waterschap}_shortest_path.gpkg", driver="GPKG"
)

cloud = CloudStorage()
# cloud.download_verwerkt(waterschap)
# cloud.download_verwerkt("Rijkswaterstaat")
# cloud.download_basisgegevens()

# %%
verwerkt_dir = cloud.joinpath(waterschap, "verwerkt")

# Load Data
# Define crossings file path
data_path = verwerkt_dir / "crossings.gpkg"

# Load crossings file
DATA = {L: gpd.read_file(data_path, layer=L) for L in fiona.listlayers(data_path)}

# Select rhws

# Select RHWS peilgebeied & calculate representative point
gdf_rhws = DATA["peilgebied"].loc[DATA["peilgebied"]["peilgebied_cat"] == 1].copy()
gdf_rhws["representative_point"] = gdf_rhws.representative_point()

# Apply aggregation level based filter
gdf_cross = (
DATA["crossings_hydroobject_filtered"].loc[DATA["crossings_hydroobject_filtered"]["agg_links_in_use"]].copy()
) # filter aggregation level

gdf_crossings_out = shortest_path(waterschap, DATA, gdf_cross, gdf_rhws)
# Write final output
gdf_out = gpd.GeoDataFrame(pd.concat(gdf_crossings_out))
gdf_out["shortest_path"] = gdf_out["shortest_path"].apply(lambda geom: dumps(geom) if geom is not None else None)
gdf_out.to_file(verwerkt_dir / "shortest_path.gpkg", driver="GPKG")

# cloud.upload_verwerkt(waterschap)
116 changes: 116 additions & 0 deletions src/peilbeheerst_model/crossings_to_ribasim/AmstelGooienVecht.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Modified version of the AGV part of 02_crossings_to_ribasim.py.

# %%

import pandas as pd
from ribasim import Model

from peilbeheerst_model import CrossingsToRibasim, RibasimNetwork, waterschap_data
from ribasim_nl import CloudStorage

# %%
waterschap = "AmstelGooienVecht"
waterschap_struct = waterschap_data[waterschap]

cloud = CloudStorage()
verwerkt_dir = cloud.joinpath(waterschap, "verwerkt")
# cloud.download_verwerkt(waterschap)
# cloud.download_basisgegevens()

# %%

pd.set_option("display.max_columns", None)
# warnings.filterwarnings("ignore")


model_characteristics = {
# model description
"waterschap": "AmstelGooienVecht",
"modelname": "repro",
"modeltype": "boezemmodel",
# define paths
"path_postprocessed_data": verwerkt_dir / "postprocessed.gpkg",
"path_crossings": verwerkt_dir / "crossings.gpkg",
"path_boezem": verwerkt_dir / "shortest_path.gpkg",
"path_Pdrive": None,
# apply filters
"crossings_layer": "crossings_hydroobject_filtered",
"in_use": True,
"agg_links_in_use": True,
"agg_areas_in_use": True,
"aggregation": True,
# data storage settings
"write_goodcloud": False, # TODO
"write_checks": True,
# numerical settings
"solver": None,
"logging": None,
"starttime": "2024-01-01 00:00:00",
"endtime": "2024-01-02 00:00:00",
}

waterboard = CrossingsToRibasim(model_characteristics=model_characteristics)

post_processed_data, crossings = waterboard.read_files()
post_processed_data, crossings = waterboard.routing_processor(post_processed_data, crossings)
crossings = waterboard.assign_node_ids(crossings)
edges = waterboard.create_edges(crossings)
nodes, edges = waterboard.create_nodes(crossings, edges)
edges = waterboard.embed_boezems(edges, post_processed_data, crossings)

# create individual model parts of the network
network = RibasimNetwork(nodes=nodes, edges=edges, model_characteristics=model_characteristics)

edge = network.edge()
basin_node, basin_profile, basin_static, basin_state, basin_area = network.basin()
pump_node, pump_static = network.pump()
tabulated_rating_curve_node, tabulated_rating_curve_static = network.tabulated_rating_curve()
level_boundary_node, level_boundary_static = network.level_boundary()
flow_boundary_node, flow_boundary_static = network.flow_boundary()
manning_resistance_node, manning_resistance_static = network.manning_resistance()
terminal_node = network.terminal()

# linear_resistance = network.linear_resistance()
# fractional_flow = network.fractional_flow()
# outlet = network.outlet()
# discrete_control = network.discrete_control()
# pid_control = network.pid_control()

# insert the individual model modules in an actual model
model = Model(starttime=model_characteristics["starttime"], endtime=model_characteristics["endtime"], crs="EPSG:28992")

model.edge.df = edge

model.basin.node.df = basin_node
model.basin.profile = basin_profile
model.basin.static = basin_static
model.basin.state = basin_state
model.basin.area = basin_area

model.pump.node.df = pump_node
model.pump.static = pump_static

model.tabulated_rating_curve.node.df = tabulated_rating_curve_node
model.tabulated_rating_curve.static = tabulated_rating_curve_static

model.manning_resistance.node.df = manning_resistance_node
model.manning_resistance.static = manning_resistance_static

model.level_boundary.node.df = level_boundary_node
model.level_boundary.static = level_boundary_static

model.flow_boundary.node.df = flow_boundary_node
model.flow_boundary.static = flow_boundary_static

model.terminal.node.df = terminal_node

# add checks and metadata
checks = network.check(model, post_processed_data=post_processed_data, crossings=crossings)
model = network.add_meta_data(model, checks, post_processed_data, crossings)

# write the result
network.WriteResults(model=model, checks=checks)

# %%

# cloud.upload_verwerkt(waterschap)
15 changes: 11 additions & 4 deletions src/peilbeheerst_model/peilbeheerst_model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
__version__ = "0.1.0"

from peilbeheerst_model.parse_crossings import ParseCrossings
from peilbeheerst_model.shortest_path import shortest_path_waterschap
from peilbeheerst_model.waterschappen import waterschap_data
from .crossings_to_ribasim import CrossingsToRibasim, RibasimNetwork
from .parse_crossings import ParseCrossings
from .shortest_path import shortest_path_waterschap
from .waterschappen import waterschap_data

__all__ = ["ParseCrossings", "shortest_path_waterschap", "waterschap_data"]
__all__ = [
"CrossingsToRibasim",
"ParseCrossings",
"RibasimNetwork",
"shortest_path_waterschap",
"waterschap_data",
]
Loading
Loading