-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwadi_weather.py
executable file
·203 lines (168 loc) · 6.05 KB
/
wadi_weather.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
"""
Wadi ad-Dawasir weather information
https://docs.streamlit.io/knowledge-base/tutorials/databases/private-gsheet
Dev/local run:
streamlit run --server.fileWatcherType=poll --server.port 8503 wadi_weather.py
"""
import streamlit as st
from streamlit_gsheets import GSheetsConnection
import pandas as pd
import plotly.graph_objects as go
import warnings
import datetime
warnings.filterwarnings('ignore')
# Page configuration
st.set_page_config(
page_title="Wadi ad-Dawasir weather data",
page_icon="⛅",
layout="wide",
initial_sidebar_state="expanded")
# Read data
conn = st.connection("gsheets", type=GSheetsConnection)
wdata = conn.read(parse_dates=["date"]).dropna()
# Determine total date range in sheet, and set
# initial date range to the first month of data.
start_date = wdata.date.min()
end_date = wdata.date.max()
start_date_end_month = start_date + pd.offsets.MonthEnd()
d1, d2 = start_date.to_pydatetime(), start_date_end_month.to_pydatetime()
d3 = end_date.to_pydatetime()
# Sidebar input (year and colormap)
st.sidebar.title('⛅ Wadi ad-Dawasir weather data')
# Initial date range
if 'cdate_range' not in st.session_state:
st.session_state['cdate_range'] = [d1, d2]
cdate_range = st.session_state['cdate_range']
# Plots -- here with go instead of px
# because we want a simpler view
plot_temperature = go.Figure().update_layout(
title=dict(text="🌡️ Temperature (°C)")
)
plot_temperature.add_trace(
go.Bar(x=wdata.date, y=wdata.max_T, name="Hi",
marker = {'color' : '#e65618'},
hovertemplate="%{x}<br>%{y}")
)
plot_temperature.add_trace(
go.Bar(x=wdata.date, y=wdata.min_T, name="Low",
marker = {'color' : '#08b0c6'},
hovertemplate="%{x}<br>%{y}")
)
plot_humidity = go.Figure().update_layout(
title=dict(text="💦 Humidity (%)")
)
plot_humidity.add_trace(
go.Bar(x=wdata.date, y=wdata.max_H, name="Hi",
marker = {'color' : '#e65618'},
hovertemplate="%{x}<br>%{y}")
)
plot_humidity.add_trace(
go.Bar(x=wdata.date, y=wdata.min_H, name="Low",
marker = {'color' : '#08b0c6'},
hovertemplate="%{x}<br>%{y}")
)
plot_wind = go.Figure().update_layout(
title=dict(text=("💨 Wind speed (km/h)"))
)
plot_wind.add_trace(
go.Bar(x=wdata.date, y=wdata.max_U, name="Hi",
marker = {'color' : '#e65618'},
hovertemplate="%{x}<br>%{y}")
)
plot_wind.add_trace(
go.Bar(x=wdata.date, y=wdata.mean_U, name="Avg",
marker = {'color' : '#04570c'},
hovertemplate="%{x}<br>%{y}")
)
plot_solar = go.Figure().update_layout(
title=dict(text=("☀️ Daily accumulated solar radiation (W/m²)"))
)
plot_solar.add_trace(
go.Bar(x=wdata.date, y=wdata.solar, name="Solar radiation",
marker = {'color' : '#e65618'},
hovertemplate="%{x}<br>%{y}")
)
plot_et = go.Figure().update_layout(
title=dict(text=("💧 Evapotranspiration (mm/day)"))
)
plot_et.add_trace(
go.Bar(x=wdata.date, y=wdata.ET, name="ET",
marker = {'color' : '#189ee6'},
hovertemplate="%{x}<br>%{y}")
)
selected_range_date = st.sidebar.date_input(
"Plot view date range",
(d1, d2),
d1, d3,
key="date_range",
)
# update selection date range only when the date_input returns
# TWO values, otherwise keep the copy from the session state.
selected_range_date = st.session_state.date_range
if len(selected_range_date)==2:
cdate_range = selected_range_date
st.session_state.cdate_range = cdate_range
# Selected data
date_query = f"date>='{cdate_range[0]}' and date<='{cdate_range[1]}'"
selected_data = (wdata
.query(date_query)
.assign(
mean_T=lambda x: (x.max_T+x.min_T)/2,
mean_H=lambda x: (x.max_H+x.min_H)/2,
)
)
# Summary data on selected data
s = selected_data.agg({
"mean_T": "mean",
"mean_H": "mean",
"max_T": "max",
"min_T": "min",
"max_H": "max",
"min_H": "min",
"max_U": "max",
"mean_U": "mean",
"solar": "mean",
"ET": "mean"
})
T = pd.DataFrame()
# +-1 day, otherwise 1 of the two bars is missing.
pdate_range = [cdate_range[0]
+datetime.timedelta(days=-1)
,
cdate_range[1]
+datetime.timedelta(days=1)
]
plot_temperature.update_layout(xaxis=dict(range=pdate_range, uirevision=True), uirevision=True)
plot_humidity.update_layout(xaxis=dict(range=pdate_range, uirevision=True), uirevision=True)
plot_wind.update_layout(xaxis=dict(range=pdate_range, uirevision=True), uirevision=True)
plot_solar.update_layout(xaxis=dict(range=pdate_range, uirevision=True), uirevision=True)
plot_et.update_layout(xaxis=dict(range=pdate_range, uirevision=True), uirevision=True)
# Main panel
st.markdown("## Wadi ad-Dawasir weather data")
st.plotly_chart(plot_temperature, use_container_width=True)
st.plotly_chart(plot_humidity, use_container_width=True)
st.plotly_chart(plot_wind, use_container_width=True)
st.plotly_chart(plot_et, use_container_width=True)
st.plotly_chart(plot_solar, use_container_width=True)
with st.sidebar:
st.markdown("## Summary")
st.markdown("### 🌡️ Temperature")
t1, t2, t3 = st.columns(3)
t1.metric("High", f"{s.max_T:.1f} °C")
t2.metric("Low", f"{s.min_T:.1f} °C")
t3.metric("Average", f"{s.mean_T:.1f} °C")
st.markdown("### 💦 Humidity")
h1, h2, h3 = st.columns(3)
h1.metric("High", f"{s.max_H:.0f} %")
h2.metric("Low", f"{s.min_H:.0f} %")
h3.metric("Average", f"{s.mean_H:.0f} %")
st.markdown("### 💨 Wind speed")
u1, u2 = st.columns(2)
u1.metric("High", f"{s.max_U:.1f} km/h")
u2.metric("Average", f"{s.mean_U:.1f} km/h")
st.markdown("### 💧 ET")
st.metric("Average", f"{s.ET:.1f} mm/day")
st.expander('About', expanded=True).write('''
- Weather data by [INMA agricultural company](https://inmanet.com.sa/en/home)
- Select a view date range to update the plots view and summary.
''')