Tendaysforecast / app.py
Meteorama's picture
Update app.py
0c650c6 verified
import streamlit as st
import plotly.express as px
import openmeteo_requests
import requests_cache
import pandas as pd
from retry_requests import retry
from datetime import datetime, timedelta
from support_functions import *
import numpy as np
from scipy import interpolate
#Page Configuration
st.set_page_config(initial_sidebar_state="collapsed", page_title='10 Days Forecast', menu_items={
'Get Help': None,
'Report a bug': None,
'About': "Designed by Meteorama"
})
st.header("Meteorama")
st.header("10 Days Weather Forecast")
#Check for Lat Lon info and Get ECMWF Data
#Get Initial Configuration
url_params = st.query_params
url_params_keys = dict(url_params).keys()
if('lat' not in url_params_keys or 'lon' not in url_params_keys):
latvals = 22.47
lonvals = 70.05
st.info("Latitude and Longitude values not defined. Defaulting to Jamnagar...")
else:
latvals = float(url_params['lat'])
lonvals = float(url_params['lon'])
wxdata = get_ecmwf_data(latvals, lonvals)
#Get Dates list
start_date = datetime(wxdata['Year'].values[0], wxdata['Month'].values[0], wxdata['Day'].values[0])
end_date = start_date + timedelta(days=10)
st.caption(f"Time Period: {parseday(start_date)} to {parseday(end_date)}")
#Draw Tabs
tab1, tab2 = st.tabs(['Overall', 'Daily'])
#Min and Max temperature
min_max_date_list = []
min_max_min_list = []
min_max_max_list = []
for i in range(11):
cur_date = start_date + timedelta(days=i)
minidf = wxdata[(wxdata['Day'] == cur_date.day)&(wxdata['Month'] == cur_date.month)&(wxdata['Year'] == cur_date.year)]
min_max_date_list.append(cur_date)
min_max_min_list.append(minidf['temperature_2m'].min())
min_max_max_list.append(minidf['temperature_2m'].max())
min_max_df = pd.DataFrame()
min_max_df['Date'] = min_max_date_list
min_max_df['Minimum Temperature'] = min_max_min_list
min_max_df['Maximum Temperature'] = min_max_max_list
mintempfig = px.line(min_max_df, x = 'Date', y = 'Minimum Temperature')
maxtempfig = px.line(min_max_df, x = 'Date', y = 'Maximum Temperature')
with tab1.expander("Minimum and Maximum Temperature"):
st.plotly_chart(mintempfig, use_container_width = True)
st.plotly_chart(maxtempfig, use_container_width = True)
#temperature and inversion
#Temperature plot
tempfig = px.line(wxdata, x = 'Date_IST', y = 'temperature_2m', hover_data= 'temperature_925hPa',
labels = {'Date_IST': 'Date and Time', 'temperature_2m': 'Dry Bulb Temp', 'temperature_925hPa': 'Temp at F/L025'})
#Inversion
invlist = []
temp2mlist = wxdata['temperature_2m'].values
temp025list = wxdata['temperature_925hPa'].values
for t in range(len(temp2mlist)):
if((temp025list[t] - temp2mlist[t])>0):
invlist.append((temp025list[t] - temp2mlist[t]))
else:
invlist.append(0)
wxdata['Inversion'] = invlist
invfig = px.bar(wxdata, x='Date_IST', y='Inversion')
with tab1.expander("Temperature and Inversion"):
st.plotly_chart(tempfig, use_container_width = True)
st.plotly_chart(invfig, use_container_width = True)
#RH plot
rhfig = px.line(wxdata, x = 'Date_IST', y = 'relative_humidity_2m', labels = {'newdate': 'Date and Time', 'relative_humidity_2m': 'RH(%age)'})
with tab1.expander("Relative Humidity"):
st.plotly_chart(rhfig, use_container_width = True)
#Cloud Plots
cloudsfig = px.line(wxdata, x = 'Date_IST', y = 'cloud_cover', hover_data = ['cloud_cover_low', 'cloud_cover_mid', 'cloud_cover_high'],
labels = {'Date_IST': 'Date and Time', 'cloud_cover': 'Total Cloud Cover (%age)',
'cloud_cover_low': 'Low Clouds',
'cloud_cover_mid': 'Medium Clouds',
'cloud_cover_high': 'High Clouds'})
cloudsfig.update_layout(yaxis_range=[0,100])
lowcloudsfig = px.line(wxdata, x = 'Date_IST', y = 'cloud_cover_low', hover_data = ['cloud_cover', 'cloud_cover_mid', 'cloud_cover_high'],
labels = {'Date_IST': 'Date and Time', 'cloud_cover': 'Total Cloud Cover (%age)',
'cloud_cover_low': 'Low Clouds',
'cloud_cover_mid': 'Medium Clouds',
'cloud_cover_high': 'High Clouds'})
midcloudsfig = px.line(wxdata, x = 'Date_IST', y = 'cloud_cover_mid', hover_data = ['cloud_cover', 'cloud_cover_low', 'cloud_cover_high'],
labels = {'Date_IST': 'Date and Time', 'cloud_cover': 'Total Cloud Cover (%age)',
'cloud_cover_low': 'Low Clouds',
'cloud_cover_mid': 'Medium Clouds',
'cloud_cover_high': 'High Clouds'})
highcloudsfig = px.line(wxdata, x = 'Date_IST', y = 'cloud_cover_high', hover_data = ['cloud_cover', 'cloud_cover_low', 'cloud_cover_mid'],
labels = {'Date_IST': 'Date and Time', 'cloud_cover': 'Total Cloud Cover (%age)',
'cloud_cover_low': 'Low Clouds',
'cloud_cover_mid': 'Medium Clouds',
'cloud_cover_high': 'High Clouds'})
lowcloudsfig.update_layout(yaxis_range=[0,100])
midcloudsfig.update_layout(yaxis_range=[0,100])
highcloudsfig.update_layout(yaxis_range=[0,100])
with tab1.expander("Cloudiness"):
st.plotly_chart(cloudsfig, use_container_width = True)
st.plotly_chart(lowcloudsfig, use_container_width = True)
st.plotly_chart(midcloudsfig, use_container_width = True)
st.plotly_chart(highcloudsfig, use_container_width = True)
#precipitaion plot
pptfig = px.bar(wxdata, x = 'Date_IST', y = 'precipitation', labels = {'Date_IST': 'Date and Time', 'precipitation': 'Precipitation'})
with tab1.expander("Precipitation"):
st.plotly_chart(pptfig, use_container_width = True)
#pressure plot
qnhfig = px.line(wxdata, x = 'Date_IST', y = 'surface_pressure', labels = {'Date_IST': 'Date and Time', 'surface_pressure': 'QNH(hPa)'})
qnhfig.update_layout(yaxis_range=[995,1018])
with tab1.expander("Surface Pressure"):
st.plotly_chart(qnhfig, use_container_width = True)
#Daily Data
ds = tab2.slider(
"Forecast for ", start_date, end_date,
value=start_date,
format="DD MMM YY")
wxdata2 = wxdata[(wxdata['Day'] == ds.day)&(wxdata['Month'] == ds.month)&(wxdata['Year'] == ds.year)]
with tab2.expander("Current Weather Register"):
st.table(makecwr(wxdata2))
#temperature and inversion
#Temperature plot
tempfig = px.line(wxdata2, x = 'Date_IST', y = 'temperature_2m', hover_data= 'temperature_925hPa',
labels = {'Date_IST': 'Date and Time', 'temperature_2m': 'Dry Bulb Temp', 'temperature_925hPa': 'Temp at F/L025'})
#Inversion
invlist = []
temp2mlist = wxdata2['temperature_2m'].values
temp025list = wxdata2['temperature_925hPa'].values
for t in range(len(temp2mlist)):
if((temp025list[t] - temp2mlist[t])>0):
invlist.append((temp025list[t] - temp2mlist[t]))
else:
invlist.append(0)
wxdata2['Inversion'] = invlist
invfig = px.bar(wxdata2, x='Date_IST', y='Inversion')
invfig.update_layout(yaxis_range=[0,5])
with tab2.expander("Temperature and Inversion"):
st.plotly_chart(tempfig, use_container_width = True)
st.plotly_chart(invfig, use_container_width = True)
#RH plot
rhfig = px.line(wxdata2, x = 'Date_IST', y = 'relative_humidity_2m', labels = {'newdate': 'Date and Time', 'relative_humidity_2m': 'RH(%age)'})
with tab2.expander("Relative Humidity"):
st.plotly_chart(rhfig, use_container_width = True)
#Cloud Plots
cloudsfig = px.line(wxdata2, x = 'Date_IST', y = 'cloud_cover', hover_data = ['cloud_cover_low', 'cloud_cover_mid', 'cloud_cover_high'],
labels = {'Date_IST': 'Date and Time', 'cloud_cover': 'Total Cloud Cover (%age)',
'cloud_cover_low': 'Low Clouds',
'cloud_cover_mid': 'Medium Clouds',
'cloud_cover_high': 'High Clouds'})
cloudsfig.update_layout(yaxis_range=[0,100])
lowcloudsfig = px.line(wxdata2, x = 'Date_IST', y = 'cloud_cover_low', hover_data = ['cloud_cover', 'cloud_cover_mid', 'cloud_cover_high'],
labels = {'Date_IST': 'Date and Time', 'cloud_cover': 'Total Cloud Cover (%age)',
'cloud_cover_low': 'Low Clouds',
'cloud_cover_mid': 'Medium Clouds',
'cloud_cover_high': 'High Clouds'})
midcloudsfig = px.line(wxdata2, x = 'Date_IST', y = 'cloud_cover_mid', hover_data = ['cloud_cover', 'cloud_cover_low', 'cloud_cover_high'],
labels = {'Date_IST': 'Date and Time', 'cloud_cover': 'Total Cloud Cover (%age)',
'cloud_cover_low': 'Low Clouds',
'cloud_cover_mid': 'Medium Clouds',
'cloud_cover_high': 'High Clouds'})
highcloudsfig = px.line(wxdata2, x = 'Date_IST', y = 'cloud_cover_high', hover_data = ['cloud_cover', 'cloud_cover_low', 'cloud_cover_mid'],
labels = {'Date_IST': 'Date and Time', 'cloud_cover': 'Total Cloud Cover (%age)',
'cloud_cover_low': 'Low Clouds',
'cloud_cover_mid': 'Medium Clouds',
'cloud_cover_high': 'High Clouds'})
lowcloudsfig.update_layout(yaxis_range=[0,100])
midcloudsfig.update_layout(yaxis_range=[0,100])
highcloudsfig.update_layout(yaxis_range=[0,100])
with tab2.expander("Cloudiness"):
st.plotly_chart(cloudsfig, use_container_width = True)
st.plotly_chart(lowcloudsfig, use_container_width = True)
st.plotly_chart(midcloudsfig, use_container_width = True)
st.plotly_chart(highcloudsfig, use_container_width = True)
#pressure plot
qnhfig = px.line(wxdata2, x = 'Date_IST', y = 'surface_pressure', labels = {'Date_IST': 'Date and Time', 'surface_pressure': 'QNH(hPa)'})
qnhfig.update_layout(yaxis_range=[995,1018])
with tab2.expander("Surface Pressure"):
st.plotly_chart(qnhfig, use_container_width = True)
old_ht = [500,2500,5000,10000,18000,30000,35000,40000]
new_ht = [1000, 2000, 3000, 5000, 7000, 9000, 15000, 18000, 25000, 30000]
upper_air_df = pd.DataFrame()
upper_air_df['Height(KM)'] = [0.3,0.6,0.9,1.5,2.1,3.0,4.5,6.0,7.5,9.0][::-1]
#Upper Air Data
sel_times = tab2.multiselect("Select Time for Upper Air Data (IST)", wxdata2['Hour'].values, [5,11,17,23])
for sel_time in sel_times:
filter_df = wxdata2[wxdata2['Hour'] == sel_time]
wind_dir_list = []
wind_speed_list = []
temp_list = []
for level in ['200hPa', '250hPa', '300hPa', '500hPa', '700hPa', '850hPa', '925hPa', '1000hPa'][::-1]:
wind_dir_list.append(filter_df[f'winddirection_{level}'].values[0])
wind_speed_list.append(filter_df[f'windspeed_{level}'].values[0])
temp_list.append(int(filter_df[f'temperature_{level}'].values[0]))
new_wind_dir = interp_for_levels(old_ht, wind_dir_list, new_ht, is_wind_dir = True)[::-1]
new_wind_speed = interp_for_levels(old_ht, wind_speed_list, new_ht)[::-1]
new_temp = interp_for_levels(old_ht, temp_list, new_ht, round_to = 1)[::-1]
text_to_be_shown = []
for y in range(len(new_wind_dir)):
while(new_wind_dir[y]>360):
new_wind_dir[y] = new_wind_dir[y]-360
wwww = f"{new_wind_dir[y]:03d}/{new_wind_speed[y]:02d}({new_temp[y]:02d})"
text_to_be_shown.append(wwww)
upper_air_df[f"{sel_time:02d}:30Hr"] = text_to_be_shown
tab2.dataframe(upper_air_df, use_container_width=True)
st.success("Made by Meteo Rama..!!")