Spaces:
Runtime error
Runtime error
File size: 21,548 Bytes
00ac9b2 5872239 00ac9b2 |
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 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
#standard imports
import pandas as pd
import numpy as np
import streamlit as st
import swifter
swifter.register_modin()
from typing import Dict, List, Optional
from temperature_functions import annual_avg, df_date_split, lat_long_process,lat_long_list_creation
from temperature_functions import group_df, daily_avg, annual_avg_plot, avg_temp_plot, annual_min_plot, annual_max_plot
from temperature_functions import daily_avg_calc, daily_avg_plot, monthly_mean_calc, selecting_mean, plot_mean_data, max_temp_plot
from temperature_functions import min_temp_plot, convert_df, map_creation, search_func
import folium
from streamlit_folium import st_folium
from branca.element import Figure
import warnings
warnings.filterwarnings("ignore")
#For parallel processing
from pandarallel import pandarallel
pandarallel.initialize(progress_bar=True)
def run():
#nearest neighbor
nn = 0
# bucket_name = 'timeseries_data_storage'
# file_path1 = 'temperature1.zip'
# file_path2 = 'temperature2.zip'
tempe1 = pd.read_csv('historicalData/temperature1.zip',compression = 'zip')
tempe2 = pd.read_csv('historicalData/temperature2.zip',compression = 'zip')
temperatureDF = pd.concat([tempe1,tempe2],axis =0 )
#creating copy of our dataframe
temperatureDataFrame = temperatureDF.copy()
#applying the function on the dataframe
temperatureDataFrame = lat_long_process(temperatureDataFrame)
#applying the lat long creation function on the dataframe
lat_long_list = lat_long_list_creation(temperatureDataFrame)
#applying the function on the dataframe
result = group_df(temperatureDataFrame,lat_long_list)
#Dataframe containing daily average
result['daily_avg'] = result.swifter.apply(lambda x: daily_avg(x.tmin,x.tmax),axis=1)
### Dashboard Creation
#creating the Sidebar Menu
with st.sidebar.empty():
with st.container():
data_type = st.radio("Select Data Type to View",
('Daily Average','Monthly Mean Temperature','Annual Maximum Temperature','Annual Minimum Temperature','Annual Average Temperature','Annual Max, Min, & Average Temperature'))
#dividing the screen column into 2 sections for daily_average
if data_type == 'Daily Average':
ab = st.empty()
a = st.empty()
b = st.empty()
start = pd.to_datetime('2001/01/01')
end = pd.to_datetime('2019/12/31')
with ab:
col1,col2 = st.columns(2)
#creating dropdown menu for entering start and end date
with col1:
st.markdown('**Enter Start Date**')
start = st.date_input("",value = start,key = 1)
if start < pd.to_datetime('2001/01/01'):
st.write('Start date should not be less than 2001/01/01')
st.markdown('**Enter End Date**')
end = st.date_input("",value = end, key = 2)
if end > pd.to_datetime('2019/12/31'):
st.write('End date should not be greater than 2019/12/31')
#dropdown menu for selecting lat long values
with col2:
st.markdown('**Select the Latitude and Longitude**')
option = st.selectbox("",pd.DataFrame(lat_long_list),key = 3)
lat = float(option.split(',')[0])
long = float(option.split(',')[1])
m = map_creation(lat,long,0,0)
last_click = m['last_clicked']
with a:
dataframe_s_e = daily_avg_calc(result,option,start,end)
fig = daily_avg_plot(dataframe_s_e,option)
st.plotly_chart(fig,use_container_width = True)
with b:
col11,col22,col33 = st.columns(3)
with col22:
st.download_button("Download Data",data = convert_df(dataframe_s_e),
file_name='daily_average_temperature.csv',
mime='text/csv',)
#finding the clicked lat long and the nearest lat long
with col2:
if last_click is not None:
clicked_lat = last_click['lat']
clicked_long = last_click['lng']
st.markdown("**Last Clicked Latitude Longitude point is:**")
st.write("{:0.2f},{:0.2f}".format(clicked_lat,clicked_long))
nn = search_func(clicked_lat,clicked_long,lat_long_list,result)
st.write("**Nearest Latitude and Longitude is:**",nn)
else:
st.markdown("**Click on the map to fetch the Latitude and Longitude**")
st.markdown(" ")
st.markdown(" ")
if last_click is not None:
clicked_lat = last_click['lat']
clicked_long = last_click['lng']
map_creation(lat,long,clicked_lat,clicked_long)
#plotting the nearest neighbors graph
if nn != 0 :
df = daily_avg_calc(result,nn,start,end)
fig_nn = daily_avg_plot(df,nn)
with a:
st.plotly_chart(fig_nn,use_container_width = True)
with b:
col11,col22,col33 = st.columns(3)
with col22:
st.download_button("Download Data",data = convert_df(df),
file_name='daily_average_temperature_nn.csv',
mime='text/csv',)
elif data_type == 'Monthly Mean Temperature':
#creating the UI for selecting the year,lat long
c = st.empty()
d = st.empty()
e = st.empty()
with c:
col1,col2 = st.columns(2)
with col1:
st.markdown('**Select the Start Year**')
start_year = st.selectbox('',
('2001','2002','2003','2004','2005','2006','2007','2008','2009',
'2010','2011','2012','2013','2014','2015','2016','2017','2018','2019'),key = 4)
st.markdown('**Select the End Year**')
end_year = st.selectbox('',
('2001','2002','2003','2004','2005','2006','2007','2008','2009',
'2010','2011','2012','2013','2014','2015','2016','2017','2018','2019'),key = 5)
with col2:
st.markdown('**Select the Latitude and Longitude**')
option_mean = st.selectbox("",pd.DataFrame(lat_long_list),key = 6)
#Finding the monthly mean/avg temperature
temperature_monthly_df = result.copy()
#function for calculating mean
df_mean = monthly_mean_calc(temperature_monthly_df,lat_long_list)
#function for selecting the specified group of lat long along with the start and end date
df = selecting_mean(df_mean,option_mean,start_year,end_year)
#function for plotting the mean data
mean_chart = plot_mean_data(df,option_mean)
with d:
st.altair_chart(mean_chart)
#for map
lat_mean = float(option_mean.split(',')[0])
long_mean = float(option_mean.split(',')[1])
with col2:
m_mean = map_creation(lat_mean,long_mean,0,0)
last_click = m_mean['last_clicked']
if last_click is not None:
clicked_lat = last_click['lat']
clicked_long = last_click['lng']
st.markdown("**Last Clicked Latitude Longitude point is:**")
st.write("{:0.2f},{:0.2f}".format(clicked_lat,clicked_long))
nn = search_func(clicked_lat,clicked_long,lat_long_list,result)
st.write("**Nearest Latitude and Longitude is:**",nn)
else:
st.markdown("**Click on the map to fetch the Latitude and Longitude**")
st.markdown(" ")
st.markdown(" ")
if last_click is not None:
clicked_lat = last_click['lat']
clicked_long = last_click['lng']
map_creation(lat_mean,long_mean,clicked_lat,clicked_long)
with e:
col11,col22,col33 = st.columns(3)
with col22:
st.download_button("Download Data",data = convert_df(df),
file_name='Monthly_mean_temperature.csv',
mime='text/csv',)
st.markdown(" ")
#code for plotting the nearest neighbors data
if nn != 0 :
df_nn_mean = selecting_mean(df_mean,nn,start_year,end_year)
fig_nn_mean = plot_mean_data(df_nn_mean,nn)
with d:
st.altair_chart(fig_nn_mean)
with e:
col11,col22,col33 = st.columns(3)
with col22:
st.download_button("Download Data",data = convert_df(df_nn_mean),
file_name='Monthly_mean_temperature_nn.csv',
mime='text/csv',)
#code for annual maximum temperature
elif data_type == 'Annual Maximum Temperature':
g = st.empty()
h = st.empty()
i = st.empty()
Annual_temp = result.copy()
with g:
col1,col2 = st.columns(2)
with col1:
st.markdown('**Select the Latitude and Longitude**.')
option_annual_temp = st.selectbox("",pd.DataFrame(lat_long_list),key = 7)
df_max = annual_max_plot(Annual_temp,option_annual_temp,lat_long_list)
fig_max = max_temp_plot(df_max,option_annual_temp)
with h:
st.plotly_chart(fig_max, use_container_width=True)
#For maps
lat_annual_max = float(option_annual_temp.split(',')[0])
long_annual_max = float(option_annual_temp.split(',')[1])
with g:
with col2:
m_max = map_creation(lat_annual_max,long_annual_max,0,0)
last_click = m_max['last_clicked']
if last_click is not None:
clicked_lat = last_click['lat']
clicked_long = last_click['lng']
st.markdown("**Last Clicked Latitude Longitude point is:**")
st.write("{:0.2f},{:0.2f}".format(clicked_lat,clicked_long))
nn = search_func(clicked_lat,clicked_long,lat_long_list,result)
st.write("**Nearest Latitude and Longitude is:**",nn)
else:
st.markdown("**Click on the map to fetch the Latitude and Longitude**")
st.markdown(" ")
st.markdown(" ")
if last_click is not None:
clicked_lat = last_click['lat']
clicked_long = last_click['lng']
map_creation(lat_annual_max,long_annual_max,clicked_lat,clicked_long)
with i:
col11,col22,col33 = st.columns(3)
with col22:
st.download_button("Download Data",data = convert_df(df_max),
file_name='Annual_maximum_temperature.csv',
mime='text/csv',)
with st.container():
if nn!=0:
df_max_temp_nn = annual_max_plot(Annual_temp,nn,lat_long_list)
fig_max_nn = max_temp_plot(df_max_temp_nn,nn)
with h:
st.plotly_chart(fig_max_nn, use_container_width = True)
with i:
col11,col22,col33 = st.columns(3)
with col22:
st.download_button("Download Data",data = convert_df(df_max_temp_nn),
file_name='Annual_maximum_temperature_nn.csv',
mime='text/csv',)
elif data_type == 'Annual Minimum Temperature':
j = st.empty()
k = st.empty()
l = st.empty()
with j:
col1,col2 = st.columns(2)
with col1:
st.markdown('**Select the Latitude and Longitude**.')
option_annual_min_temp = st.selectbox("", pd.DataFrame(lat_long_list),key = 8)
Annual_temp_min = result.copy()
df_min = annual_min_plot(Annual_temp_min,option_annual_min_temp,lat_long_list)
fig_min = min_temp_plot(df_min,option_annual_min_temp)
with k:
st.plotly_chart(fig_min,use_container_width = True)
#For maps
lat_min = float(option_annual_min_temp.split(',')[0])
long_min = float(option_annual_min_temp.split(',')[1])
with j:
with col2:
m_min = map_creation(lat_min,long_min,0,0)
last_click = m_min['last_clicked']
if last_click is not None:
clicked_lat = last_click['lat']
clicked_long = last_click['lng']
st.markdown("**Last Clicked Latitude Longitude point is:**")
st.write("{:0.2f},{:0.2f}".format(clicked_lat,clicked_long))
nn = search_func(clicked_lat,clicked_long,lat_long_list,result)
st.write("**Nearest Latitude and Longitude is:**",nn)
else:
st.markdown("**Click on the map to fetch the Latitude and Longitude**")
st.markdown(" ")
st.markdown(" ")
with l:
col100,col200,col300 = st.columns(3)
with col200:
st.download_button("Download Data",data = convert_df(df_min),
file_name='Annual_minimum_temperature.csv',
mime='text/csv',)
with st.container():
if nn!=0:
nn_min_temp_df = annual_min_plot(Annual_temp_min,nn,lat_long_list)
fig_min_nn = min_temp_plot(nn_min_temp_df,nn)
with k:
st.plotly_chart(fig_min_nn, use_container_width = True)
with l:
col11,col22,col33 = st.columns(3)
with col22:
st.download_button("Download Data",data = convert_df(nn_min_temp_df),
file_name='Annual_minimum_temperature_nn.csv',
mime='text/csv',)
elif data_type == 'Annual Average Temperature':
m = st.empty()
n = st.empty()
o = st.empty()
annual_avg_df = result.copy()
with m:
col1,col2 = st.columns(2)
with col1:
st.markdown('**Select the Latitude and Longitude**')
annual_avg_option = st.selectbox("",pd.DataFrame(lat_long_list),key = 9)
annual_temp = annual_avg_plot(annual_avg_df,annual_avg_option,lat_long_list)
with n:
fig_avg = avg_temp_plot(annual_temp,annual_avg_option)
st.plotly_chart(fig_avg,use_container_width = True)
with o:
col11,col22,col33 = st.columns(3)
with col22:
st.download_button("Download Data",
data = convert_df(annual_temp),
file_name='Annual_average_temperature.csv',
mime='text/csv',)
#For maps
lat_avg = float(annual_avg_option.split(',')[0])
long_avg = float(annual_avg_option.split(',')[1])
with m:
with col2:
m_avg = map_creation(lat_avg,long_avg,0,0)
last_click = m_avg['last_clicked']
if last_click is not None:
clicked_lat = last_click['lat']
clicked_long = last_click['lng']
st.markdown("**Last Clicked Latitude Longitude point is:**")
st.write("{:0.2f},{:0.2f}".format(clicked_lat,clicked_long))
nn = search_func(clicked_lat,clicked_long,lat_long_list,result)
st.write("**Nearest Latitude and Longitude is:**",nn)
else:
st.markdown("**Click on the map to fetch the Latitude and Longitude**")
with st.container():
if nn!=0:
nn_avg_temp_df = annual_avg_plot(annual_avg_df,nn,lat_long_list)
fig_avg_nn = avg_temp_plot(nn_avg_temp_df,nn)
with n:
st.plotly_chart(fig_avg_nn, use_container_width = True)
with o:
col11,col22,col33 = st.columns(3)
with col22:
st.download_button("Download Data",
data = convert_df(nn_avg_temp_df),
file_name='Annual_average_temperature_nn.csv',
mime='text/csv',)
elif data_type == 'Annual Max, Min, & Average Temperature':
df = result.copy()
col1,col2 = st.columns(2)
with col1:
st.markdown('**Select the Latitude and Longitude**.')
option_lat_long = st.selectbox("",pd.DataFrame(lat_long_list),key = 10)
with col2:
#For maps
lat_avg_3 = float(option_lat_long.split(',')[0])
long_avg_3 = float(option_lat_long.split(',')[1])
m_max_min_avg = map_creation(lat_avg_3,long_avg_3,0,0)
last_click = m_max_min_avg['last_clicked']
if last_click is not None:
clicked_lat = last_click['lat']
clicked_long = last_click['lng']
st.markdown("**Last Clicked Latitude Longitude point is:**")
st.markdown(" ")
st.markdown(" ")
st.write("{:0.2f},{:0.2f}".format(clicked_lat,clicked_long))
nn = search_func(clicked_lat,clicked_long,lat_long_list,result)
st.write("**Nearest Latitude and Longitude is:**",nn)
else:
st.markdown("**Click on the map to fetch the Latitude and Longitude**")
col1,col2,col3 = st.columns(3)
with col1:
df1 = annual_max_plot(df,option_lat_long,lat_long_list)
fig_2 = max_temp_plot(df1,option_lat_long)
st.plotly_chart(fig_2, use_container_width=True)
st.download_button("Download Data",
data = convert_df(df1),
file_name='Annual_maximum_temperature.csv',
mime='text/csv',)
with st.container():
if nn!=0:
df_annual_max_2 = annual_max_plot(df,nn,lat_long_list)
fig_annual_max_nn_2 = max_temp_plot(df_annual_max_2,nn)
st.plotly_chart(fig_annual_max_nn_2, use_container_width=True)
st.download_button("Download Data",
data = convert_df(df_annual_max_2),
file_name='Annual_maximum_temperature_nn.csv',
mime='text/csv',)
with col2:
with st.container():
df2 = annual_min_plot(df,option_lat_long,lat_long_list)
fig_3 = min_temp_plot(df2,option_lat_long)
st.plotly_chart(fig_3,use_container_width = True)
st.download_button("Download Data",
data = convert_df(df2),
file_name='Annual_minimum_temperature.csv',
mime='text/csv',)
with st.container():
if nn!=0:
df_annual_min_2 = annual_min_plot(df,nn,lat_long_list)
fig_annual_min_nn_2 = min_temp_plot(df_annual_min_2,nn)
st.plotly_chart(fig_annual_min_nn_2, use_container_width=True)
st.download_button("Download Data",
data = convert_df(df_annual_min_2),
file_name='Annual_minimum_temperature_nn.csv',
mime='text/csv',)
with col3:
with st.container():
annual_average = annual_avg_plot(df,option_lat_long,lat_long_list)
fig_4 = avg_temp_plot(annual_average,option_lat_long)
st.plotly_chart(fig_4,use_container_width = True)
st.download_button("Download Data",
data = convert_df(annual_average),
file_name='Annual_average_temperature.csv',
mime='text/csv',)
with st.container():
if nn!=0:
df_annual_avg_nn_2 = annual_avg_plot(df,nn,lat_long_list)
fig_nn_4 = avg_temp_plot(df_annual_avg_nn_2,nn)
st.plotly_chart(fig_nn_4,use_container_width=True)
st.download_button("Download Data",
data = convert_df(df_annual_avg_nn_2),
file_name='Annual_average_temperature_nn.csv',
mime='text/csv',)
if __name__ == 'main':
run()
|