Skip to content

Commit

Permalink
updated reset-index
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielTollenaar committed Sep 10, 2024
1 parent e754aca commit 5613858
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 15 deletions.
66 changes: 66 additions & 0 deletions notebooks/koppelen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# %%

import sqlite3

import pandas as pd
from ribasim_nl import CloudStorage, Model, reset_index

cloud = CloudStorage()


def update_database(toml_file):
database_gpkg = toml_file.with_name("database.gpkg")
conn = sqlite3.connect(database_gpkg)

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

# drop urban runoff column if exists
df.rename(columns={"min_crest_level": "min_upstream_level"}, 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()


model_url = cloud.joinurl("AmstelGooienVecht", "modellen", "AmstelGooienVecht_parametrized_2024_8_47")
model_path = cloud.joinpath("AmstelGooienVecht", "modellen", "AmstelGooienVecht_parametrized_2024_8_47")
cloud.download_content(model_url)

# %% update RWS-HWS
model_path = cloud.joinpath("Rijkswaterstaat", "modellen", "hws")
toml_file = model_path / "hws.toml"
update_database(toml_file)
rws_model = Model.read(toml_file)

# some fixes
node_id = 8413
level = rws_model.upstream_profile(node_id).level.min() + 0.1

mask = (rws_model.tabulated_rating_curve.static.df.node_id == node_id) & (
rws_model.tabulated_rating_curve.static.df.level < level
)
rws_model.tabulated_rating_curve.static.df.loc[mask, ["level"]] = level

# %% reset index
rws_model = reset_index(rws_model)

# write model
rws_model.write(model_path.with_name("hws_temp") / "hws.toml")

# %% update AGV
toml_file = model_path / "ribasim.toml"
update_database(toml_file)
model_to_couple = Model.read(toml_file)

model_to_couple.write(model_path.with_name("AmstelGooienVecht_temp") / "agv.toml")


# %%

node_ids = model_to_couple.level_boundary.static.df[
model_to_couple.level_boundary.static.df.meta_to_authority == "Rijkswaterstaat"
].node_id.to_list()
26 changes: 11 additions & 15 deletions src/ribasim_nl/ribasim_nl/reset_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,33 @@

def reset_index(model: Model, node_start=1):
# only reset if we have to
node_id_min = model.node_table().df.node_id.min()
node_id_max = model.node_table().df.node_id.max()
node_id_min = model.node_table().df.index.min()
node_id_max = model.node_table().df.index.max()
expected_length = node_id_max - node_id_min + 1
if not ((node_start == node_id_min) and (expected_length == len(model.node_table().df))):
# make sure column node_id == index
node_ids = model.node_table().df.node_id
node_ids = model.node_table().df.index

# create a new index for re-indexing all tables
index = pd.Series(data=[i + node_start for i in range(len(node_ids))], index=node_ids).astype("int32")

# # re-index node_id and fid
# model.network.node.df.index = model.network.node.df["fid"].apply(
# lambda x: index.loc[x]
# )
# model.network.node.df.index.name = "fid"
# model.network.node.df.drop(columns=["fid"], inplace=True)
# model.network.node.df.loc[:, "node_id"] = model.network.node.df.index

# renumber edges
model.edge.df.loc[:, ["from_node_id"]] = model.edge.df["from_node_id"].apply(lambda x: index.loc[x])
model.edge.df.loc[:, ["from_node_id"]] = model.edge.df["from_node_id"].apply(lambda x: index[x])

model.edge.df.loc[:, ["to_node_id"]] = model.edge.df["to_node_id"].apply(lambda x: index.loc[x])
model.edge.df.loc[:, ["to_node_id"]] = model.edge.df["to_node_id"].apply(lambda x: index[x])

# renumber tables
for node_type in model.node_table().df.node_type.unique():
ribasim_node = getattr(model, pascal_to_snake_case(node_type))
for attr in ribasim_node.model_fields.keys():
table = getattr(ribasim_node, attr)
if table.df is not None:
table.df.loc[:, "node_id"] = table.df["node_id"].apply(lambda x: index.loc[x])

if "node_id" in table.df.columns:
table.df.loc[:, "node_id"] = table.df["node_id"].apply(lambda x: index[x])
table.df.index += 1
elif table.df.index.name == "node_id":
table.df.index = table.df.reset_index("node_id")["node_id"].apply(lambda x: index.loc[x])
# table.df.index.name = "node_id"
return model


Expand Down

0 comments on commit 5613858

Please sign in to comment.