-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_changeEvents.py
More file actions
149 lines (120 loc) · 4.48 KB
/
Copy pathgenerate_changeEvents.py
File metadata and controls
149 lines (120 loc) · 4.48 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# This script generates a networkChangeEvents xml file
# Inputs - flood_network CSV outputs
import re
import pandas as pd
from datetime import datetime, timedelta
from pathlib import Path
import argparse
def write_headers(file):
file.write(f'<?xml version="1.0" encoding="UTF-8"?>\n')
file.write(
f'<networkChangeEvents xmlns="http://www.matsim.org/files/dtd" '
f'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
f'xsi:schemaLocation="http://www.matsim.org/files/dtd '
f'http://www.matsim.org/files/dtd/networkChangeEvents.xsd">\n\n'
)
def load_df(filepath, id):
df = pd.read_csv(filepath)
df = df.drop_duplicates(id)
return df[df[id].notna()]
def calculate_time(time1, time2, time_format="%H:%M:%S"):
t1 = datetime.strptime(time1, time_format)
t2 = datetime.strptime(time2, time_format)
delta1 = timedelta(hours=t1.hour, minutes=t1.minute, seconds=t1.second)
delta2 = timedelta(hours=t2.hour, minutes=t2.minute, seconds=t2.second)
total_delta = delta1 + delta2
total_seconds = int(total_delta.total_seconds())
hours, remainder = divmod(total_seconds, 3600)
minutes, seconds = divmod(remainder, 60)
return f"{hours:02}:{minutes:02}:{seconds:02}"
# Sorts based on T value, e.g. R1_C1_T0_0min, R1_C1_T1_5min, ...
def sort_filenames(files):
return sorted(files, key=lambda x: int(re.search(r'_T(\d+)_', x).group(1)))
def main(
flood_network_csv_filepath: str,
output_dir: str,
event_start_time: str,
time_interval: str,
flood_network_id_name: str = "ID",
velocity_keyword: str = "velocity"
):
flood_network_csv_filepath = Path(flood_network_csv_filepath)
output_dir = Path(output_dir)
df = load_df(flood_network_csv_filepath, flood_network_id_name)
velocity_cols = [
col for col in df.columns if col.endswith("_" + velocity_keyword)
]
if len(velocity_cols) == 0:
raise Exception(f"No fields found containing velocity_keyword: {velocity_keyword}")
if len(velocity_cols) > 1:
velocity_cols = sort_filenames(velocity_cols)
with open(output_dir / "networkChangeEvents.xml", "w") as writefile:
write_headers(writefile)
current_time = event_start_time
for column in velocity_cols:
print(column)
grouped = df.groupby(column)
dfs = {value: df for value, df in grouped}
for value, data in dfs.items():
writefile.write(f'<networkChangeEvent startTime="{current_time}">\n')
links = data["ID"].to_list()
for link in links:
writefile.write(f'<link refId="{link}"/>\n')
# MATSim doesn't accept 0 speed - use very small alternative
if value == 0:
value = 0.001
writefile.write(f'<freespeed type="absolute" value="{value}"/>\n')
writefile.write("</networkChangeEvent>\n")
# TODO: Calculate time interval automatically from column names
current_time = calculate_time(current_time, time_interval)
writefile.write(f'</networkChangeEvents>\n')
print('Done')
if __name__ == "__main__":
p = argparse.ArgumentParser()
p.add_argument(
'--flood_network_csv_filepath',
required=True,
type=str
)
p.add_argument(
'--output_dir',
required=False,
default=".",
type=str,
help='(optional) Default=.'
)
p.add_argument(
'--event_start_time',
required=True,
type=str,
help="Event start time as format %%H:%%M:%%S (e.g. 12:00:00 for 12pm)"
)
p.add_argument(
'--time_interval',
required=True,
type=str,
help="Interval time as format %%H:%%M:%%S (e.g. 00:10:00 for a 10 min interval)"
)
p.add_argument(
'--flood_network_id_name',
required=False,
default="ID",
help='(optional) Default=ID Name of the network ID field',
type=str,
)
p.add_argument(
'--velocity_keyword',
required=False,
default="velocity",
type=str,
help='(optional) Default=velocity'
)
args = p.parse_args()
main(
flood_network_csv_filepath=args.flood_network_csv_filepath,
output_dir=args.output_dir,
event_start_time=args.event_start_time,
time_interval=args.time_interval,
flood_network_id_name=args.flood_network_id_name,
velocity_keyword=args.velocity_keyword
)