-
Notifications
You must be signed in to change notification settings - Fork 128
/
funs_generate_network.py
95 lines (72 loc) · 3.23 KB
/
funs_generate_network.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import numpy as np
import networkx as nx
import pandas as pd
from funs_dikes import Lookuplin # @UnresolvedImport
def to_dict_dropna(data):
return {str(k): v.dropna().to_dict() for k, v in data.items()}
def get_network(plann_steps_max=10):
"""Build network uploading crucial parameters"""
# Upload dike info
df = pd.read_excel("./data/dikeIjssel.xlsx", dtype=object)
df = df.set_index("NodeName")
nodes = df.to_dict("index")
# Create network out of dike info
G = nx.MultiDiGraph()
for key, attr in nodes.items():
G.add_node(key, **attr)
# Select dike type nodes
branches = df["branch"].dropna().unique()
dike_list = df["type"][df["type"] == "dike"].index.values
dike_branches = {k: df[df["branch"] == k].index.values for k in branches}
# Upload fragility curves:
frag_curves = pd.read_excel(
"./data/fragcurves/frag_curves.xlsx", header=None, index_col=0
).transpose()
calibration_factors = pd.read_excel(
"./data/fragcurves/calfactors_pf1250.xlsx", index_col=0
)
# Upload room for the river projects:
steps = np.array(range(plann_steps_max))
projects = pd.read_excel(
"./data/rfr_strategies.xlsx", index_col=0, names=["project name", 0, 1, 2, 3, 4]
)
for n in steps:
a = to_dict_dropna(projects)
G.add_node(f"RfR_projects {n}", **a)
G.nodes[f"RfR_projects {n}"]["type"] = "measure"
G.add_node(f"discount rate {n}", **{"value": 0})
# Upload evacuation policies:
G.add_node("EWS", **pd.read_excel("./data/EWS.xlsx").to_dict())
G.nodes["EWS"]["type"] = "measure"
# Upload muskingum params:
Muskingum_params = pd.read_excel("./data/Muskingum/params.xlsx", index_col=0)
# Fill network with crucial info:
for dike in dike_list:
# Assign fragility curves, assuming it's the same shape for every
# location
dikeid = 50001010
G.nodes[dike]["f"] = np.column_stack(
(frag_curves.loc[:, "wl"].values, frag_curves.loc[:, dikeid].values)
)
# Adjust fragility curves
G.nodes[dike]["f"][:, 0] += calibration_factors.loc[dike].values
# Determine the level of the dike
G.nodes[dike]["dikelevel"] = Lookuplin(G.nodes[dike]["f"], 1, 0, 0.5)
# Assign stage-discharge relationships
filename = f"./data/rating_curves/{dike}_ratingcurve_new.txt" # Load file
rc_array = np.loadtxt(filename) # Load file into array
G.nodes[dike]["r"] = rc_array[
rc_array[:, 0].argsort()
] # Sort on first column before saving
# Assign losses per location:
name = f"./data/losses_tables/{dike}_lossestable.xlsx"
G.nodes[dike]["table"] = pd.read_excel(name, index_col=0).values
# Assign Muskingum paramters:
G.nodes[dike]["C1"] = Muskingum_params.loc[G.nodes[dike]["prec_node"], "C1"]
G.nodes[dike]["C2"] = Muskingum_params.loc[G.nodes[dike]["prec_node"], "C2"]
G.nodes[dike]["C3"] = Muskingum_params.loc[G.nodes[dike]["prec_node"], "C3"]
# The plausible 133 upstream wave-shapes:
G.nodes["A.0"]["Qevents_shape"] = pd.read_excel(
"./data/hydrology/wave_shapes.xls", index_col=0
)
return G, dike_list, dike_branches, steps