-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
392 lines (341 loc) · 19.9 KB
/
app.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# standard library
# pip install streamlit streamlit-option-menu netCDF4 plotly matplotlib shapely geopandas
# pip install folium==0.14.0 streamlit-folium==0.15.1
# pip install leafmap==0.29.6 xarray pyproj
# pip install keplergl==0.3.2
import os
import time
# third-party library
import pandas as pd
import numpy as np
import streamlit as st
from streamlit_option_menu import option_menu
# local library
from utils import display_leafmap, display_map, display_matplots, display_plotly, read_netcdf, useful_functions
# Start clock to test out site load time
start_time = time.time()
# Define Page layout
st.set_page_config(page_title="Mikroklima-Visualisierung",
layout="centered",
initial_sidebar_state="collapsed") #collapsed/expanded
# Inside the container, add the title and content
with st.container():
columns_main = st.columns((3,3,1))
with columns_main[2]:
selected_language = st.selectbox(label="Language", options=["DE", "EN"], label_visibility="hidden") #🌐
st.markdown("<div class='fixed-header'/>", unsafe_allow_html=True)
def load_language_bundle(locale):
df = pd.read_csv(r"./i18n/text_bundle.csv")
df = df.query(f"locale == '{locale}'")
lang_dict = {df.key.to_list()[i]:df.value.to_list()[i] for i in range(len(df.key.to_list()))}
return lang_dict
lang_dict = load_language_bundle(selected_language)
# Import CSS style
with open('./css/style.css') as f:
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
# Define band and time sequence based on the file to be used
simulation_domain = "N03_reduced"
def band_time_sequence():
if simulation_domain == "N03":
# Define band sequence and time sequence
x_tick_interval = 1 # 1 hour
band_sequence = [0]
band_sequence.extend(np.arange(5, 144, 6*x_tick_interval))
time_sequence = []
for i, band_index in enumerate(band_sequence):
time = useful_functions.band_index_to_time_hr_min(band_index)
time_sequence.append(time)
# Update time by few time steps to get better result in graph (temporary solution)
band_sequence[15] = 90
band_sequence[13] = 78
band_sequence_backup = band_sequence.copy()
band_sequence_backup[13] = 77
return band_sequence, band_sequence_backup, time_sequence
elif simulation_domain == "N03_reduced":
band_sequence = list(range(0,25))
band_sequence_backup = band_sequence.copy()
time_sequence = ['0:10', '1:00', '2:00', '3:00', '4:00', '5:00', '6:00', '7:00', '8:00', '9:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00', '24:00']
return band_sequence, band_sequence_backup, time_sequence
band_sequence, band_sequence_backup, time_sequence = band_time_sequence()
# Website Introduction
with st.container():
st.header(body=f"{lang_dict['website_intro_header']}", anchor=False)
st.subheader(body=f"{lang_dict['website_intro_subheader']}", anchor=False)
st.write(f"{lang_dict['website_intro_subheader_details']}")
st.divider()
# Options Menu
option_menu_styles = {
"container": {
"width": "100%", # Set the width of the entire menu container
"max-width": "initial",
"background": "white",
"border-radius": "0rem",
# "border": "0.5px solid #DD0065",
},
"nav-link": {
"width": "95%",
"color": "black",
# "background": "#d4d4d4",
"font-size": "1.0rem",
"border": "1px solid #DD0065",
"border-radius": "1rem",
# "font-family": "Franklin Gothic Demi",
},
"nav-link-selected": {
"background-color": "#DD0065",
"color": "white",
"font-size": "1.1rem",
"border-radius": "1rem",
# "font-family": "Franklin Gothic Demi",
},
}
selected_menu = option_menu(
menu_title = None,
options=[f"{lang_dict['option_menu_0']}", f"{lang_dict['option_menu_1']}", f"{lang_dict['option_menu_2']}", f"{lang_dict['option_menu_3']}", f"{lang_dict['option_menu_4']}", f"{lang_dict['option_menu_5']}"],
icons=["house", "globe2", "map", "palette", "wind", "info-circle"],
default_index=0,
orientation="horizontal",
styles= option_menu_styles,
)
# Scenerio 0
if selected_menu == f"{lang_dict['option_menu_0']}":
with st.expander(f"{lang_dict['menu_0_title']}", expanded=True):
# User Input
columns_input = st.columns(4)
with columns_input[0]:
location = st.selectbox(label=f"{lang_dict['location']}", options=["Augustinerplatz", "Marktstätte"])
if location == "Augustinerplatz":
folder_path = r"./data/landing_page/Augustinerplatz/"
elif location == "Marktstätte":
folder_path = r"./data/landing_page/Marktstätte/"
# Read images
image_names = ["before.png", "after.png", f"before_heatmap_{selected_language}.png", f"after_heatmap_{selected_language}.png"]
image_addresses = [os.path.join(folder_path, image_name) for image_name in image_names]
# Display Images
columns_main = st.columns((3,3))
with columns_main[0]:
st.markdown(f"<p class='centered-text'>{lang_dict['current_state']}</p>", unsafe_allow_html=True,)
st.image(image = image_addresses[0])
st.image(image = image_addresses[2])
with columns_main[1]:
st.markdown(f"<p class='centered-text'>{lang_dict['after_change']}</p>", unsafe_allow_html=True,)
st.image(image = image_addresses[1])
st.image(image = image_addresses[3])
# 3D Visualization 1
elif selected_menu == f"{lang_dict['option_menu_1']}":
with st.expander("3D Map Viewer", expanded=True):
# User Input
columns_input = st.columns(4)
with columns_input[0]:
# Select time of the day visualize the image overlay
time_index_3d = st.select_slider(label= f"{lang_dict['time_of_day']}", options=["09:00", "12:00", "15:00", "18:00", "21:00"], value="12:00")
with columns_input[1]:
st.markdown(f"<p class='title-text'><strong>{lang_dict['layer_options']}</strong></p>", unsafe_allow_html=True,)
# Toggle image overlay
display_image = st.checkbox(label=f"{lang_dict['var_air_temp']}", value=True)
# Toggle added trees
display_added_trees = st.checkbox(label=f"{lang_dict['after_change']}", value=True)
# Assign Default Values
opacity_3d = 0.75
lat = 47.661129
lon = 9.175209
zoom = 16
pitch = 50
bearing = -40
# Display 3D Map
columns_main = st.columns(1)
with columns_main[0]:
display_map.pydeck_3d_geojson(time_index_3d, opacity_3d, display_image, display_added_trees, lat, lon, zoom, pitch, bearing)
# OSM 2
elif selected_menu == f"{lang_dict['option_menu_2']}":
with st.expander(f"{lang_dict['menu_2_title']}", expanded=True):
# User Input
columns_input = st.columns(4)
with columns_input[0]:
# Select time of the day visualize the image overlay
time_index = st.select_slider(label=f"{lang_dict['time_of_day']}", options=["09:00", "12:00", "15:00", "18:00", "21:00"], value="12:00")
with columns_input[1]:
st.markdown(f"<p class='title-text'><strong>{lang_dict['layer_options']}</strong></p>", unsafe_allow_html=True,)
# Toggle marker display
display_markers = st.checkbox(label=f"{lang_dict['scenario_positions']}", value=True)
# Assign Default Values
opacity_2d = 0.75
display_shapefile = False
# Columns for header
columns_header = st.columns((3,3,0.5))
with columns_header[0]:
st.markdown(f"<p class='centered-text'>{lang_dict['current_state']}</p>", unsafe_allow_html=True,)
with columns_header[1]:
st.markdown(f"<p class='centered-text'>{lang_dict['after_change']}</p>", unsafe_allow_html=True,)
# Display dual Map
columns_main = st.columns(1)
# Display folium map with raster overlay
with columns_main[0]:
display_map.dual_raster_overlay(time_index, opacity_2d, display_shapefile, display_markers)
# # Add image as scale
columns_legend = st.columns((2,4,0.5))
with columns_legend[0]:
image_url = r"./images/scale_hz.png"
st.image(image_url, use_column_width='auto', width=120)
# User Input
columns_input = st.columns(4)
with columns_input[0]:
# Option to select which domain to visualize
options=[f"{lang_dict['area_parent']}", f"{lang_dict['area_child1']}", f"{lang_dict['area_child2']}"]
domain = st.selectbox(label=f"{lang_dict['analysis_area']}", options=options, index=2)
domain_index = options.index(domain) + 1
# Display single Map
columns_main = st.columns(1)
with columns_main[0]:
display_map.single_raster_overlay(time_index, opacity_2d, display_shapefile, display_markers, domain_index)
if domain == "Gesamtes Stadtgebiet":
st.markdown(f'<p class="note-text">Grade: Auswertungsbereich= 4096 x 4096 m², Auflösung=16m</p>', unsafe_allow_html=True,)
elif domain == "Innenstadtbereich":
st.markdown(f'<p class="note-text">Grade: Auswertungsbereich= 2048 x 2048 m², Auflösung=8m</p>', unsafe_allow_html=True,)
elif domain == "Zielregion":
st.markdown(f'<p class="note-text">Grade: Auswertungsbereich= 512 x 512 m², Auflösung=2m</p>', unsafe_allow_html=True,)
# Color Map 3
elif selected_menu == f"{lang_dict['option_menu_3']}":
with st.expander(label=f"{lang_dict['menu_3_title']}", expanded=True):
# User Input
columns_input = st.columns(4)
with columns_input[0]:
# Fetch locations
location_list = ["Altstadt", "Augstinerplatz", "Markstätte"]
location = st.selectbox(label=f"{lang_dict['location']}", options=location_list, index=location_list.index(location_list[1]))
with columns_input[1]:
if selected_language == "DE":
# Fetch masked data from the selected variable
variable_description = st.selectbox(label=f"{lang_dict['displayed_variable']}", options=read_netcdf.variable_list()[3])
variable_index = read_netcdf.variable_list()[3].index(variable_description)
elif selected_language == "EN":
variable_description = st.selectbox(label=f"{lang_dict['displayed_variable']}", options=read_netcdf.variable_list()[1])
variable_index = read_netcdf.variable_list()[1].index(variable_description)
with columns_input[2]:
# Select time of day and equivanlent band_index for plot
time_index = st.select_slider(label=f"{lang_dict['time_of_day']}", options=time_sequence, value="15:00")
band_index = band_sequence[time_sequence.index(time_index)]
# Read variable name and variable unit from variable dictionary
variable_name = read_netcdf.variable_list()[0][variable_index]
variable_unit = read_netcdf.variable_list()[2][variable_index]
if selected_language == "DE":
variable_description_selected = read_netcdf.variable_list()[3][variable_index]
elif selected_language == "EN":
variable_description_selected = read_netcdf.variable_list()[1][variable_index]
# Read data from masked variable data based on variable name
variable_data_1_masked, variable_data_2_masked, building_id_mask = read_netcdf.variable_data_masked(variable_name)[0:3]
variable_data_masked_run_1_aoi_1, variable_data_masked_run_1_aoi_2= read_netcdf.variable_data_masked(variable_name)[3:5]
variable_data_masked_run_2_aoi_1, variable_data_masked_run_2_aoi_2 = read_netcdf.variable_data_masked(variable_name)[5:7]
data_run_1_stn_1, data_run_1_stn_2, data_run_1_stn_3, data_run_2_stn_1, data_run_2_stn_2, data_run_2_stn_3 = read_netcdf.variable_data_masked(variable_name)[7:13]
# Assign Default Values
cmap = "turbo"
mask_color = "#474747"
vmin = min(np.nanmin(variable_data_1_masked), np.nanmin(variable_data_2_masked)) // 2.5 * 2.5
vmax = max(np.nanmax(variable_data_1_masked), np.nanmax(variable_data_2_masked) + 2.5 - 1) // 2.5 * 2.5
shapefile_color = "#FFFFFF"
shapefile_url = r"./data/area_of_interest/aoi_sim.shp"
shapefile_url_2 = r"./data/area_of_interest/aoi_stations.shp"
hatch = "//"
columns_main = st.columns(2)
# Plot color maps as per the variables
with columns_main[0]:
st.markdown(f"<p class='centered-text'>{lang_dict['current_state']}</p>", unsafe_allow_html=True,)
# Plot color map for base simulation
display_matplots.colormesh(variable_description_selected, variable_unit,
variable_data_1_masked, location,
building_id_mask, band_index, cmap, mask_color,
vmin, vmax, shapefile_color, shapefile_url, hatch, shapefile_url_2)
with columns_main[1]:
st.markdown(f"<p class='centered-text'>{lang_dict['after_change']}</p>", unsafe_allow_html=True,)
# Plot color map for test simulation
display_matplots.colormesh(variable_description_selected, variable_unit,
variable_data_2_masked, location,
building_id_mask, band_index, cmap, mask_color,
vmin, vmax, shapefile_color, shapefile_url, hatch, shapefile_url_2)
# Use expander and tabs to display graphs
expander_name = f"{lang_dict['menu_3_title_2']}: {variable_description_selected}"
with st.expander(expander_name, expanded=True):
# User Input
columns_input = st.columns(4)
with columns_input[0]:
# Fetch locations
location_list = ["Altstadt", "Augstinerplatz", "Markstätte", "Position 1", "Position 2", "Position 3"]
location = st.selectbox(label=f"{lang_dict['location']}", options=location_list, index=location_list.index(location_list[1]))
columns_main = st.columns(1)
with columns_main[0]:
if location == "Altstadt":
dataframe_run_1 = read_netcdf.compute_statistics_2d(variable_data_1_masked)
dataframe_run_2 = read_netcdf.compute_statistics_2d(variable_data_2_masked)
display_plotly.bar_graph(dataframe_run_1, dataframe_run_2, band_sequence, time_sequence, variable_description_selected, variable_unit, lang_dict)
elif location == "Augstinerplatz":
dataframe_run_1_aoi_1 = read_netcdf.compute_statistics_2d(variable_data_masked_run_1_aoi_1)
dataframe_run_2_aoi_1 = read_netcdf.compute_statistics_2d(variable_data_masked_run_2_aoi_1)
display_plotly.bar_graph(dataframe_run_1_aoi_1, dataframe_run_2_aoi_1, band_sequence, time_sequence, variable_description_selected, variable_unit, lang_dict)
elif location == "Markstätte":
dataframe_run_1_aoi_2 = read_netcdf.compute_statistics_2d(variable_data_masked_run_1_aoi_2)
dataframe_run_2_aoi_2 = read_netcdf.compute_statistics_2d(variable_data_masked_run_2_aoi_2)
display_plotly.bar_graph(dataframe_run_1_aoi_2, dataframe_run_2_aoi_2, band_sequence_backup, time_sequence, variable_description_selected, variable_unit, lang_dict)
elif location == "Position 1":
dataframe_run_1_stn_1 = read_netcdf.compute_statistics_2d(data_run_1_stn_1)
dataframe_run_2_stn_1 = read_netcdf.compute_statistics_2d(data_run_2_stn_1)
display_plotly.bar_graph(dataframe_run_1_stn_1, dataframe_run_2_stn_1, band_sequence, time_sequence, variable_description_selected, variable_unit, lang_dict)
elif location == "Position 2":
dataframe_run_1_stn_2 = read_netcdf.compute_statistics_2d(data_run_1_stn_2)
dataframe_run_2_stn_2 = read_netcdf.compute_statistics_2d(data_run_2_stn_2)
display_plotly.bar_graph(dataframe_run_1_stn_2, dataframe_run_2_stn_2, band_sequence, time_sequence, variable_description_selected, variable_unit, lang_dict)
elif location == "Position 3":
dataframe_run_1_stn_3 = read_netcdf.compute_statistics_2d(data_run_1_stn_3)
dataframe_run_2_stn_3 = read_netcdf.compute_statistics_2d(data_run_2_stn_3)
display_plotly.bar_graph(dataframe_run_1_stn_3, dataframe_run_2_stn_3, band_sequence_backup, time_sequence, variable_description_selected, variable_unit, lang_dict)
# Wind Visualization 4
elif selected_menu == f"{lang_dict['option_menu_4']}":
with st.expander(f"{lang_dict['menu_4_title']}", expanded=True):
# User Input
columns_input = st.columns(4)
with columns_input[0]:
# Select time of the day visualize the image overlay
time_sequence = ["00:10", "03:00", "06:00", "09:00", "12:00", "15:00", "18:00", "21:00", "24:00",]
time = st.select_slider(label= f"{lang_dict['time_of_day']}", options=time_sequence, value="12:00")
time_index_wind = time_sequence.index(time)
with columns_input[1]:
# Select time of the day visualize the image overlay
level_sequence = range(4,21,2)
level = st.select_slider(label= f"Gitterebene ab der niedrigsten Höhe" if selected_language == "DE" else f"Grid level from the lowest elevation",
options=level_sequence, value=6)
level_index_wind = level_sequence.index(level)
with columns_input[2]:
st.markdown(f"<p class='title-text'><strong>{lang_dict['layer_options']}</strong></p>", unsafe_allow_html=True,)
# Toggle image overlay
display_image = st.checkbox(label=f"{lang_dict['var_air_temp']}", value=True)
# Assign Default Values (not passed)
opacity_3d = 0.75
# Display Wind Streamline
columns_main = st.columns(1)
with columns_main[0]:
display_leafmap.wind_streamline2(time_index_wind, time, level_index_wind, level, display_image)
# Info 5
elif selected_menu == f"{lang_dict['option_menu_5']}":
# Website Introduction
with st.expander(f"{lang_dict['menu_5_title']}", expanded=True):
st.header(f"{lang_dict['about_title_1']}")
st.write(f"{lang_dict['about_project_content']}")
st.write(f"{lang_dict['about_project_sections']}")
with st.expander(f"{lang_dict['about_title_2']}", expanded=True):
st.header(f"{lang_dict['about_us_title']}")
st.write(f"{lang_dict['about_us_content']}")
st.write(f"{lang_dict['about_us_header']}")
st.write(f"{lang_dict['about_us_sections']}")
st.markdown(f"<p class='custom-text'><strong>{lang_dict['contact_person']}</strong></p>", unsafe_allow_html=True,)
st.markdown(f'<p class="custom-text">Dr.-Ing. Sami Bidier</p>', unsafe_allow_html=True,)
st.markdown(f'<p class="custom-text">📞 <a href="tel:+4971128693713">+49 (0)711 286 937-13</a></p>', unsafe_allow_html=True,)
st.markdown(f'<p class="custom-text">✉️ <a href="mailto:[email protected]">[email protected]</a></p>', unsafe_allow_html=True,)
st.markdown(f'<p class="custom-text"> </p>', unsafe_allow_html=True,)
else:
st.write("Test successful")
footer_container = st.container()
with footer_container:
columns_footer = st.columns(4)
with columns_footer[0]:
st.image(image=r"./data/images/structure_logo_RGB.png", width=250)
# end_time = time.time()
# st.write(f"Time taken to load: {end_time - start_time:.2f} seconds")