generated from CambridgeEngineering/PartIA-Flood-Warning-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task2G.py
77 lines (61 loc) · 2.22 KB
/
Task2G.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
# Import modules
import datetime
from floodsystem.stationdata import build_station_list, update_water_levels
from floodsystem.flood import stations_level_over_threshold
from floodsystem.datafetcher import fetch_measure_levels
def run():
# Build list of stations
stations = build_station_list()
# Update latest level data for all stations
update_water_levels(stations)
# Highest risk of flooding when water is above high typical range
stations_tol = stations_level_over_threshold(stations, 1)
stations_tol.sort(key=lambda x: x[1])
# Consider risk based on data for past 5 days
dt = 5
dic_risk = {}
for station in stations_tol:
# Risk is 0 for low, 1 for moderate, 2 for high, 3 for sevre
risk = 0
dates, levels = fetch_measure_levels(station[0].measure_id, dt=datetime.timedelta(days=dt))
# We count how many times the level increases compared to the previous level
n = 0
for i in range(len(levels) - 1):
if type(levels[i + 1]) == float and type(levels[i]) == float:
if levels[i] < levels[i + 1]:
n += 1
# Severe risk more than 60% increase, high more than 45% and moderate more than 30%
if n > 0.6 * len(levels):
risk = 3
elif n > 0.45 * len(levels):
risk = 2
elif n > 0.3 * len(levels):
risk = 1
dic_risk[station] = risk
# Severe risk stations
print('Severe:')
for station in stations_tol:
if dic_risk[station] == 3:
print(station[0].town)
print('\n' * 1)
# Print high stations
print('High:')
for station in stations_tol:
if dic_risk[station] == 2:
print(station[0].town)
print('\n' * 1)
# Print moderate stations
print('Moderate:')
for station in stations_tol:
if dic_risk[station] == 1:
print(station[0].town)
print('\n' * 1)
# Print low stations
print('Low:')
for station in stations_tol:
if dic_risk[station] == 0:
print(station[0].town)
print('\n' * 1)
if __name__ == "__main__":
print("*** Task 2G: CUED Part IA Flood Warning System ***")
run()