-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
325 lines (268 loc) · 10.5 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
from textwrap import dedent
import pytz
from datetime import date, timedelta, datetime
from logging import getLogger
import streamlit as st
import pandas as pd
import pvlib
from pvlib import clearsky
import numpy as np
from scipy import integrate
logging = getLogger()
def direction_vec(elevation, azimuth):
se = np.sin(np.radians(elevation))
ce = np.cos(np.radians(elevation))
sa = np.sin(np.radians(azimuth))
ca = np.cos(np.radians(azimuth))
return np.array([sa * ce, ca * ce, se])
def irradiation_factor(sun_vec, window_vec):
return np.max([sun_vec @ window_vec, 0])
def calculate_pressure(altitude):
return pvlib.atmosphere.alt2pres(altitude)
@st.cache_data(persist=True, max_entries=30)
def calculate_data(
min_date, max_date, freq, latitude, longitude, tz, pressure
) -> pd.DataFrame:
times = pd.date_range(start=min_date, end=max_date, freq=freq, tz=tz)
solpos = pvlib.solarposition.get_solarposition(times, latitude, longitude)
apparent_zenith = solpos["apparent_zenith"]
airmass = pvlib.atmosphere.get_relative_airmass(apparent_zenith)
airmass = pvlib.atmosphere.get_absolute_airmass(airmass, pressure)
linke_turbidity = pvlib.clearsky.lookup_linke_turbidity(times, latitude, longitude)
dni_extra = pvlib.irradiance.get_extra_radiation(times)
ineichen = clearsky.ineichen(
apparent_zenith, airmass, linke_turbidity, altitude, dni_extra
)
ineichen[["apparent_elevation", "azimuth"]] = solpos[
["apparent_elevation", "azimuth"]
]
return ineichen
@st.cache_data(
hash_funcs={
"areas": lambda areas: [
f"{a['azimuth']}{a['elevation']}{a['size']}" for a in areas
]
}
)
def join_areas(_base_data: pd.DataFrame, areas: list[dict]) -> pd.DataFrame:
# areas = st.session_state.get("areas", [])
for i, area in enumerate(areas):
area_direction = direction_vec(area.get("elevation", 0), area.get("azimuth", 0))
area_direction_factors = _base_data[["apparent_elevation", "azimuth"]].apply(
lambda row: irradiation_factor(direction_vec(*row), area_direction), axis=1
)
_base_data[f"area_{i}_direction_factor"] = area_direction_factors
_base_data[f"area_{i}_irradiation"] = area.get("size", 0) * (
area_direction_factors * _base_data["dni"] + _base_data["dhi"]
)
if areas:
_base_data["irradiation_sum"] = _base_data[
[f"area_{ii}_irradiation" for ii in range(len(areas))]
].sum(axis=1)
return _base_data
def integrate_joined_data(joined_data: pd.DataFrame) -> pd.DataFrame | None:
irradiation_cols = [
f"area_{i}_irradiation" for i in range(len(st.session_state.get("areas", [])))
]
all_cols = []
grouper = pd.Grouper(freq="D")
dx = joined_data.index[:2].diff()[-1]
assert type(dx) == pd.Timedelta
data = {}
for i, col in enumerate(irradiation_cols):
cname = f"area_{i}_integrated"
data[cname] = joined_data.groupby(grouper)[col].apply(
lambda g: integrate.trapezoid(g, dx=dx.seconds)
)
all_cols.append(cname)
pdf = pd.DataFrame(data)
if len(irradiation_cols) > 1:
pdf["integrated_sum"] = pdf.sum(axis=1)
all_cols.append("integrated_sum")
return pdf[all_cols].iloc[:-1] / 3_600_000
# if "irradiation_sum" in joined_data.columns:
# integrated_data = joined_data.groupby(grouper)[
# "irradiation_sum"
# ].apply(lambda g: integrate.trapezoid(g, dx=dx.seconds))
# return integrated_data.iloc[:-1] / 3600 / 1000
def daily_data(data: pd.DataFrame, plot_date: date):
return data.loc[plot_date.isoformat() : plot_date.isoformat()]
def add_area():
st.session_state.areas.append({})
def remove_area(idx: int):
st.session_state.areas.pop(idx)
def sync_i(idx):
label = st.session_state[f"area_text_{idx}"]
if label:
st.session_state.areas[idx]["label"] = label
else:
if "label" in st.session_state.areas[idx]:
st.session_state.areas[idx].pop("label")
st.session_state.areas[idx]["azimuth"] = st.session_state[f"area_azimuth_{idx}"]
st.session_state.areas[idx]["elevation"] = st.session_state[f"area_elevation_{idx}"]
st.session_state.areas[idx]["size"] = st.session_state[f"area_size_{idx}"]
@st.cache_data(persist=True, max_entries=30)
def get_solar_vector(plot_date, time_of_day, long, lat):
solar_position = pvlib.solarposition.get_solarposition(
datetime.combine(plot_date, time_of_day), long, lat
)
plot_sun_vec = direction_vec(
*solar_position.loc[
datetime.combine(plot_date, time_of_day) :,
["apparent_elevation", "azimuth"],
]
.iloc[0]
.values
)
return plot_sun_vec
if "areas" not in st.session_state:
st.session_state.areas = []
else:
logging.info(f"Loaded existing areas:\n{st.session_state.areas}")
input_columns = st.columns(4)
with input_columns[0]:
long = st.number_input(label="long", value=7.6547815)
with input_columns[1]:
lat = st.number_input(label="lat", value=51.5751116)
with input_columns[2]:
altitude = st.number_input(label="altitude", value=60)
with input_columns[3]:
timezone = st.selectbox(
label="timezone",
options=pytz.common_timezones,
index=pytz.common_timezones.index("Europe/Berlin"),
)
time_columns = st.columns(5)
with time_columns[0]:
min_date = st.date_input(
label="from",
value=st.session_state.get("min_date", date.today()),
key="min_date",
)
with time_columns[1]:
max_date = st.date_input(
label="to",
value=st.session_state.get("max_date", date.today() + timedelta(days=1)),
key="max_date",
)
with time_columns[2]:
plot_date = st.date_input(
label="plot_date",
value=st.session_state.get("plot_date", date.today()),
min_value=st.session_state.get("min_date", date.today()),
max_value=st.session_state.get("max_date", date.today() + timedelta(days=1)),
)
with time_columns[3]:
time_of_day = st.time_input(
label="time_of_day",
value=st.session_state.get("time_of_day", datetime.now()),
key="time_of_day",
)
with time_columns[4]:
freq = st.selectbox(label="freq", options=["10min", "30min", "1h"], index=1)
pressure = calculate_pressure(altitude)
data = calculate_data(min_date, max_date, freq, lat, long, timezone, pressure)
ineichen_plot_data = daily_data(data, plot_date=plot_date)
joined_area_data = join_areas(data, st.session_state.get("areas", []))
area_plot_data = daily_data(joined_area_data, plot_date=plot_date)
integrated_plot_data = integrate_joined_data(joined_area_data)
if st.session_state.get("debug"):
st.dataframe(integrated_plot_data)
sun_vec = get_solar_vector(plot_date, time_of_day, long, lat)
st.markdown(
body=dedent(
f"""
Calculation for {long=:.2f} {lat=:.2f} at {altitude=:.0f}m in {timezone=}
Values between {min_date=:%Y-%m-%d} and {max_date=:%Y-%m-%d} with {freq=}
Base dataset contains {data.shape[0]} rows.
Base data is using {data.memory_usage().sum() / 1e6:.1f}MB of RAM.
Full data is using {area_plot_data.memory_usage().sum() / 1e6:.1f}MB of RAM.
Pressure at given coordinates is {pressure / 1_000:.2f}kPa
Solar vector at {plot_date} {time_of_day} is {sun_vec}.
"""
).strip()
)
st.header(f"Ineichen Irradiation on {plot_date:%Y-%m-%d}")
st.line_chart(ineichen_plot_data, y=["dhi", "dni", "ghi"])
if num_areas := len(st.session_state.get("areas", [])):
area_factor_cols = [f"area_{i}_direction_factor" for i in range(num_areas)]
area_irrad_cols = {
f"area_{i}_irradiation": st.session_state.areas[i].get(
"label", f"area_{i}_irradiation"
)
for i in range(num_areas)
}
area_integrated_cols = {
f"area_{i}_integrated": st.session_state.areas[i].get(
"label", f"area_{i}_integrated"
)
for i in range(num_areas)
}
if len(area_irrad_cols) > 1:
area_irrad_cols["irradiation_sum"] = "Sum"
area_integrated_cols["integrated_sum"] = "Sum"
st.header("Daily Area Irradiation")
st.line_chart(
area_plot_data.rename(columns=area_irrad_cols),
y=list(area_irrad_cols.values()),
y_label="Irradiation / Watt",
)
st.header("Daily Energy Potential")
st.line_chart(
integrated_plot_data.rename(columns=area_integrated_cols),
y_label="Energy / kWh",
)
st.line_chart(
integrated_plot_data.rename(columns=area_integrated_cols).cumsum(),
y_label="Energy / kWh",
)
else:
st.text("Define areas to evaluate irradiation over time.")
area_main_header_col, area_add_col = st.columns(2)
with area_main_header_col:
st.markdown("## Areas")
with area_add_col:
st.button("Add Area", on_click=add_area)
st.text(f"{st.session_state.areas=}")
for i, area in enumerate(st.session_state.areas):
header_col, delete_col = st.columns(2)
with header_col:
st.markdown(f"### Area {area.get('label', i)}")
with delete_col:
st.button("Delete", on_click=remove_area, args=(i,), key=f"area_delete_{i}")
with st.form(f"area_{i}"):
label_col, azimuth_col, elevation_col, area_col = st.columns(4)
with label_col:
label = st.text_input(
"label",
key=f"area_text_{i}",
value=st.session_state.get("areas")[i].get("label"),
)
with azimuth_col:
azimuth = st.number_input(
"azimuth",
key=f"area_azimuth_{i}",
step=1,
value=st.session_state.get("areas")[i].get("azimuth", 0),
)
with elevation_col:
elevation = st.number_input(
"elevation",
key=f"area_elevation_{i}",
step=1,
value=st.session_state.get("areas")[i].get("elevation", 0),
)
with area_col:
area = st.number_input(
"area",
key=f"area_size_{i}",
step=0.1,
value=st.session_state.get("areas")[i].get("size", 1.0),
)
area_direction = direction_vec(elevation, azimuth)
factor = irradiation_factor(sun_vec, area_direction)
if st.session_state.get("debug"):
st.text(f"Area vector {area_direction=}")
st.text(f"Irradiation {factor=}")
st.form_submit_button("Save", on_click=sync_i, args=(i,))
st.toggle(label="Debug mode", key="debug")