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+
import streamlit as st
|
| 2 |
+
import plotly.express as px
|
| 3 |
+
import openmeteo_requests
|
| 4 |
+
import requests_cache
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from retry_requests import retry
|
| 7 |
+
from datetime import datetime, timedelta
|
| 8 |
+
from support_functions import *
|
| 9 |
+
import numpy as np
|
| 10 |
+
from scipy import interpolate
|
| 11 |
+
|
| 12 |
+
#Page Configuration
|
| 13 |
+
st.set_page_config(initial_sidebar_state="collapsed", page_title='local_fcst', menu_items={
|
| 14 |
+
'Get Help': None,
|
| 15 |
+
'Report a bug': None,
|
| 16 |
+
'About': "Designed by Meteorama"
|
| 17 |
+
})
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
st.header("Meteorama")
|
| 21 |
+
|
| 22 |
+
st.header("for local forecast")
|
| 23 |
+
|
| 24 |
+
#Check for Lat Lon info and Get ECMWF Data
|
| 25 |
+
#Get Initial Configuration
|
| 26 |
+
url_params = st.query_params
|
| 27 |
+
url_params_keys = dict(url_params).keys()
|
| 28 |
+
if('lat' not in url_params_keys or 'lon' not in url_params_keys):
|
| 29 |
+
latvals = 22.47
|
| 30 |
+
lonvals = 70.05
|
| 31 |
+
st.info("Latitude and Longitude values not defined. Defaulting to Jamnagar...")
|
| 32 |
+
else:
|
| 33 |
+
latvals = float(url_params['lat'])
|
| 34 |
+
lonvals = float(url_params['lon'])
|
| 35 |
+
|
| 36 |
+
wxdata = get_ecmwf_data(latvals, lonvals)
|
| 37 |
+
|
| 38 |
+
#Get Dates list
|
| 39 |
+
start_date = datetime(wxdata['Year'].values[0], wxdata['Month'].values[0], wxdata['Day'].values[0])
|
| 40 |
+
end_date = start_date + timedelta(days=10)
|
| 41 |
+
st.caption(f"Time Period: {parseday(start_date)} to {parseday(end_date)}")
|
| 42 |
+
|
| 43 |
+
#Draw Tabs
|
| 44 |
+
tab2 = st.tabs(['Daily'])
|
| 45 |
+
|
| 46 |
+
#Daily Data
|
| 47 |
+
ds = tab2.slider(
|
| 48 |
+
"Forecast for ", start_date, end_date,
|
| 49 |
+
value=start_date,
|
| 50 |
+
format="DD MMM YY")
|
| 51 |
+
|
| 52 |
+
wxdata2 = wxdata[(wxdata['Day'] == ds.day)&(wxdata['Month'] == ds.month)&(wxdata['Year'] == ds.year)]
|
| 53 |
+
|
| 54 |
+
with tab2.expander("Current Weather Register"):
|
| 55 |
+
st.table(makecwr(wxdata2))
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
#temperature and inversion
|
| 59 |
+
#Temperature plot
|
| 60 |
+
tempfig = px.line(wxdata2, x = 'Date_IST', y = 'temperature_2m', hover_data= 'temperature_925hPa',
|
| 61 |
+
labels = {'Date_IST': 'Date and Time', 'temperature_2m': 'Dry Bulb Temp', 'temperature_925hPa': 'Temp at F/L025'})
|
| 62 |
+
|
| 63 |
+
#Inversion
|
| 64 |
+
invlist = []
|
| 65 |
+
temp2mlist = wxdata2['temperature_2m'].values
|
| 66 |
+
temp025list = wxdata2['temperature_925hPa'].values
|
| 67 |
+
|
| 68 |
+
for t in range(len(temp2mlist)):
|
| 69 |
+
if((temp025list[t] - temp2mlist[t])>0):
|
| 70 |
+
invlist.append((temp025list[t] - temp2mlist[t]))
|
| 71 |
+
else:
|
| 72 |
+
invlist.append(0)
|
| 73 |
+
|
| 74 |
+
wxdata2['Inversion'] = invlist
|
| 75 |
+
invfig = px.bar(wxdata2, x='Date_IST', y='Inversion')
|
| 76 |
+
invfig.update_layout(yaxis_range=[0,5])
|
| 77 |
+
|
| 78 |
+
with tab2.expander("Temperature and Inversion"):
|
| 79 |
+
st.plotly_chart(tempfig, use_container_width = True)
|
| 80 |
+
st.plotly_chart(invfig, use_container_width = True)
|
| 81 |
+
|
| 82 |
+
#RH plot
|
| 83 |
+
rhfig = px.line(wxdata2, x = 'Date_IST', y = 'relative_humidity_2m', labels = {'newdate': 'Date and Time', 'relative_humidity_2m': 'RH(%age)'})
|
| 84 |
+
|
| 85 |
+
with tab2.expander("Relative Humidity"):
|
| 86 |
+
st.plotly_chart(rhfig, use_container_width = True)
|
| 87 |
+
|
| 88 |
+
#Cloud Plots
|
| 89 |
+
cloudsfig = px.line(wxdata2, x = 'Date_IST', y = 'cloud_cover', hover_data = ['cloud_cover_low', 'cloud_cover_mid', 'cloud_cover_high'],
|
| 90 |
+
labels = {'Date_IST': 'Date and Time', 'cloud_cover': 'Total Cloud Cover (%age)',
|
| 91 |
+
'cloud_cover_low': 'Low Clouds',
|
| 92 |
+
'cloud_cover_mid': 'Medium Clouds',
|
| 93 |
+
'cloud_cover_high': 'High Clouds'})
|
| 94 |
+
cloudsfig.update_layout(yaxis_range=[0,100])
|
| 95 |
+
lowcloudsfig = px.line(wxdata2, x = 'Date_IST', y = 'cloud_cover_low', hover_data = ['cloud_cover', 'cloud_cover_mid', 'cloud_cover_high'],
|
| 96 |
+
labels = {'Date_IST': 'Date and Time', 'cloud_cover': 'Total Cloud Cover (%age)',
|
| 97 |
+
'cloud_cover_low': 'Low Clouds',
|
| 98 |
+
'cloud_cover_mid': 'Medium Clouds',
|
| 99 |
+
'cloud_cover_high': 'High Clouds'})
|
| 100 |
+
midcloudsfig = px.line(wxdata2, x = 'Date_IST', y = 'cloud_cover_mid', hover_data = ['cloud_cover', 'cloud_cover_low', 'cloud_cover_high'],
|
| 101 |
+
labels = {'Date_IST': 'Date and Time', 'cloud_cover': 'Total Cloud Cover (%age)',
|
| 102 |
+
'cloud_cover_low': 'Low Clouds',
|
| 103 |
+
'cloud_cover_mid': 'Medium Clouds',
|
| 104 |
+
'cloud_cover_high': 'High Clouds'})
|
| 105 |
+
highcloudsfig = px.line(wxdata2, x = 'Date_IST', y = 'cloud_cover_high', hover_data = ['cloud_cover', 'cloud_cover_low', 'cloud_cover_mid'],
|
| 106 |
+
labels = {'Date_IST': 'Date and Time', 'cloud_cover': 'Total Cloud Cover (%age)',
|
| 107 |
+
'cloud_cover_low': 'Low Clouds',
|
| 108 |
+
'cloud_cover_mid': 'Medium Clouds',
|
| 109 |
+
'cloud_cover_high': 'High Clouds'})
|
| 110 |
+
lowcloudsfig.update_layout(yaxis_range=[0,100])
|
| 111 |
+
midcloudsfig.update_layout(yaxis_range=[0,100])
|
| 112 |
+
highcloudsfig.update_layout(yaxis_range=[0,100])
|
| 113 |
+
|
| 114 |
+
with tab2.expander("Cloudiness"):
|
| 115 |
+
st.plotly_chart(cloudsfig, use_container_width = True)
|
| 116 |
+
st.plotly_chart(lowcloudsfig, use_container_width = True)
|
| 117 |
+
st.plotly_chart(midcloudsfig, use_container_width = True)
|
| 118 |
+
st.plotly_chart(highcloudsfig, use_container_width = True)
|
| 119 |
+
|
| 120 |
+
#pressure plot
|
| 121 |
+
qnhfig = px.line(wxdata2, x = 'Date_IST', y = 'surface_pressure', labels = {'Date_IST': 'Date and Time', 'surface_pressure': 'QNH(hPa)'})
|
| 122 |
+
qnhfig.update_layout(yaxis_range=[995,1018])
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
with tab2.expander("Surface Pressure"):
|
| 126 |
+
st.plotly_chart(qnhfig, use_container_width = True)
|
| 127 |
+
|
| 128 |
+
old_ht = [500,2500,5000,10000,18000,30000,35000,40000]
|
| 129 |
+
new_ht = [1000, 2000, 3000, 5000, 7000, 9000, 15000, 18000, 25000, 30000]
|
| 130 |
+
upper_air_df = pd.DataFrame()
|
| 131 |
+
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]
|
| 132 |
+
#Upper Air Data
|
| 133 |
+
sel_times = tab2.multiselect("Select Time for Upper Air Data (IST)", wxdata2['Hour'].values, [5,11,17,23])
|
| 134 |
+
for sel_time in sel_times:
|
| 135 |
+
filter_df = wxdata2[wxdata2['Hour'] == sel_time]
|
| 136 |
+
wind_dir_list = []
|
| 137 |
+
wind_speed_list = []
|
| 138 |
+
temp_list = []
|
| 139 |
+
for level in ['200hPa', '250hPa', '300hPa', '500hPa', '700hPa', '850hPa', '925hPa', '1000hPa'][::-1]:
|
| 140 |
+
wind_dir_list.append(filter_df[f'winddirection_{level}'].values[0])
|
| 141 |
+
wind_speed_list.append(filter_df[f'windspeed_{level}'].values[0])
|
| 142 |
+
temp_list.append(int(filter_df[f'temperature_{level}'].values[0]))
|
| 143 |
+
|
| 144 |
+
new_wind_dir = interp_for_levels(old_ht, wind_dir_list, new_ht, is_wind_dir = True)[::-1]
|
| 145 |
+
new_wind_speed = interp_for_levels(old_ht, wind_speed_list, new_ht)[::-1]
|
| 146 |
+
new_temp = interp_for_levels(old_ht, temp_list, new_ht, round_to = 1)[::-1]
|
| 147 |
+
|
| 148 |
+
text_to_be_shown = []
|
| 149 |
+
for y in range(len(new_wind_dir)):
|
| 150 |
+
while(new_wind_dir[y]>360):
|
| 151 |
+
new_wind_dir[y] = new_wind_dir[y]-360
|
| 152 |
+
wwww = f"{new_wind_dir[y]:03d}/{new_wind_speed[y]:02d}({new_temp[y]:02d})"
|
| 153 |
+
text_to_be_shown.append(wwww)
|
| 154 |
+
|
| 155 |
+
upper_air_df[f"{sel_time:02d}:30Hr"] = text_to_be_shown
|
| 156 |
+
|
| 157 |
+
tab2.dataframe(upper_air_df, use_container_width=True)
|
| 158 |
+
|
| 159 |
+
st.success("Made by Meteo Rama..!!")
|