Skip to content

Commit 085937d

Browse files
committed
add graphs of aqi and similar to app
1 parent 36c47dc commit 085937d

File tree

1 file changed

+160
-7
lines changed

1 file changed

+160
-7
lines changed

app.py

+160-7
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
mc = MongoClient(host=HOST, port=PORT)
3434
cw = mc[NAME].weather_current
3535
cf = mc[NAME].weather_forecasted
36-
36+
ca = mc[NAME].aqi_current
37+
caf = mc[NAME].aqi_forecasted
3738

3839
N_FORECASTS_SHOWN = 10
3940
N_FORECAST_WINDOW = 7
@@ -126,6 +127,11 @@ def fetch_history():
126127
def fetch_forecasts():
127128
return [i for i in cf.find({})]
128129

130+
def fetch_aqi_history():
131+
return [i for i in ca.find({})]
132+
133+
def fetch_aqi_forecasts():
134+
return [i for i in caf.find({})]
129135

130136
def weathers2df(weathers):
131137
"""
@@ -182,6 +188,23 @@ def create_df():
182188
return df
183189

184190

191+
def create_aqi_df():
192+
f = fetch_aqi_forecasts()
193+
afdf = weathers2df(f)
194+
afdf["origin"] = "forecast"
195+
196+
h = fetch_aqi_history()
197+
ahdf = weathers2df(h)
198+
ahdf["origin"] = "history"
199+
200+
adf = pd.concat((ahdf, afdf))
201+
202+
dt_cols = ["datetime", "fetched_at"]
203+
dt_cols_str = [d + "_str" for d in dt_cols]
204+
adf[dt_cols_str] = adf[dt_cols].apply(datetime2string)
205+
return adf
206+
207+
185208
def get_error_graphs(df_graph, x_column, main_line_column, lower_column, upper_column, rgb_str, name):
186209
line = go.Scatter(
187210
x=df_graph[x_column],
@@ -417,6 +440,8 @@ def generate_page(n_intervals):
417440
logging.info(f"Regenerating info div: times regenerated = {n_intervals}")
418441
df = create_df()
419442

443+
adf = create_aqi_df()
444+
420445
fig_temp = create_time_figure(
421446
df,
422447
"temperature.temp",
@@ -515,7 +540,7 @@ def generate_page(n_intervals):
515540
show_forecast=False,
516541
history_rgb="rgb(255,255,51)",
517542
as_type="graphs",
518-
line_only_graph_type="markers",
543+
line_only_graph_type="lines",
519544
custom_main_trace_label="Sunrise"
520545
)
521546

@@ -527,7 +552,7 @@ def generate_page(n_intervals):
527552
show_forecast=False,
528553
history_rgb="rgb(255,128,0)",
529554
as_type="graphs",
530-
line_only_graph_type="markers",
555+
line_only_graph_type="lines",
531556
custom_main_trace_label="Sunset"
532557
)
533558

@@ -547,6 +572,125 @@ def generate_page(n_intervals):
547572
forecast_rgb=FORECAST_RGB
548573
)
549574

575+
fig_aqi = create_time_figure(
576+
adf,
577+
"aqi",
578+
"Adjusted AQI (1 [good] - 5 [hazardous])",
579+
show_history=True,
580+
show_forecast=True,
581+
history_rgb="rgb(7,130,255)",
582+
forecast_rgb=FORECAST_RGB
583+
)
584+
585+
586+
trace_pollutants_forecasts = False
587+
trace_pollutants_markers = "lines"
588+
589+
graphs_co = create_time_figure(
590+
adf,
591+
"co",
592+
None,
593+
show_history=True,
594+
show_forecast=trace_pollutants_forecasts,
595+
history_rgb="rgb(122,120,227)",
596+
as_type="graphs",
597+
line_only_graph_type=trace_pollutants_markers,
598+
custom_main_trace_label="CO"
599+
)
600+
601+
graphs_no = create_time_figure(
602+
adf,
603+
"no",
604+
None,
605+
show_history=True,
606+
show_forecast=trace_pollutants_forecasts,
607+
history_rgb="rgb(112,224,208)",
608+
as_type="graphs",
609+
line_only_graph_type=trace_pollutants_markers,
610+
custom_main_trace_label="NO"
611+
)
612+
613+
graphs_no2 = create_time_figure(
614+
adf,
615+
"no2",
616+
None,
617+
show_history=True,
618+
show_forecast=trace_pollutants_forecasts,
619+
history_rgb="rgb(224,112,118)",
620+
as_type="graphs",
621+
line_only_graph_type=trace_pollutants_markers,
622+
custom_main_trace_label="NO2"
623+
)
624+
625+
graphs_o3 = create_time_figure(
626+
adf,
627+
"o3",
628+
None,
629+
show_history=True,
630+
show_forecast=trace_pollutants_forecasts,
631+
history_rgb="rgb(76,153,60)",
632+
as_type="graphs",
633+
line_only_graph_type=trace_pollutants_markers,
634+
custom_main_trace_label="O3"
635+
)
636+
637+
graphs_nh3 = create_time_figure(
638+
adf,
639+
"nh3",
640+
None,
641+
show_history=True,
642+
show_forecast=trace_pollutants_forecasts,
643+
history_rgb="rgb(136,163,60)",
644+
as_type="graphs",
645+
line_only_graph_type=trace_pollutants_markers,
646+
custom_main_trace_label="NH3"
647+
)
648+
649+
graphs_so2 = create_time_figure(
650+
adf,
651+
"so2",
652+
None,
653+
show_history=True,
654+
show_forecast=trace_pollutants_forecasts,
655+
history_rgb="rgb(145,145,145)",
656+
as_type="graphs",
657+
line_only_graph_type=trace_pollutants_markers,
658+
custom_main_trace_label="SO2"
659+
)
660+
661+
662+
fig_pollutants = go.Figure(
663+
graphs_co + graphs_no + graphs_no2 + graphs_o3 + graphs_nh3 + graphs_so2
664+
)
665+
fig_pollutants.update_layout(
666+
template="plotly_dark",
667+
title="Trace Air Pollutants (ug/m^3)"
668+
)
669+
fig_pollutants.update_yaxes(type="log", title="Pollutant concentration")
670+
671+
fig_pm25 = create_time_figure(
672+
adf,
673+
"pm2_5",
674+
"Fine Particulate Matter PM2.5 (ug/m^3)",
675+
show_history=True,
676+
show_forecast=True,
677+
history_rgb="rgb(255,0,0)",
678+
forecast_rgb=FORECAST_RGB
679+
)
680+
681+
fig_pm10 = create_time_figure(
682+
adf,
683+
"pm10",
684+
"Coarse Particulate Matter PM10 (ug/m^3)",
685+
show_history=True,
686+
show_forecast=True,
687+
history_rgb="rgb(43,0,237)",
688+
forecast_rgb=FORECAST_RGB
689+
)
690+
691+
692+
693+
550694
graphs = [
551695
dcc.Graph(id='graph_temperature',figure=fig_temp),
552696
dcc.Graph(id="graph_feels_like", figure=fig_feels_like),
@@ -557,8 +701,12 @@ def generate_page(n_intervals):
557701
dcc.Graph(id="graph_pressure", figure=fig_pressure),
558702
dcc.Graph(id="graph_humidty", figure=fig_humidity),
559703
dcc.Graph(id="graph_visibility", figure=fig_visibility),
560-
dcc.Graph(id="graph_cloud_coverage", figure=fig_cloud_cover),
561-
dcc.Graph(id="graph_suntimes", figure=fig_suntimes)
704+
# dcc.Graph(id="graph_cloud_coverage", figure=fig_cloud_cover),
705+
dcc.Graph(id="graph_suntimes", figure=fig_suntimes),
706+
dcc.Graph(id="graph_aqi", figure=fig_aqi),
707+
dcc.Graph(id="graph_pollutants", figure=fig_pollutants),
708+
dcc.Graph(id="graph_pm25", figure=fig_pm25),
709+
dcc.Graph(id="graph_pm10", figure=fig_pm10)
562710
]
563711
divs = []
564712

@@ -674,6 +822,7 @@ def generate_page(n_intervals):
674822
html.Tr([
675823
html.Td("Precipitation prob./accum (3h)", style=white_text),
676824
html.Td(f'{most_recent_weather["precipitation_probability"]}/{most_recent_weather["rain.3h"]}mm', style=white_text)
825+
# html.Td("K", style=white_text)
677826
]),
678827
html.Tr([
679828
html.Td("Wind speed/direction", style=white_text),
@@ -715,5 +864,9 @@ def generate_page(n_intervals):
715864
pd.set_option('display.max_rows', 500)
716865
pd.set_option('display.max_columns', 500)
717866
pd.set_option('display.width', 1000)
718-
# app.run_server(debug=True, port=SERVER_PORT)
719-
app.run_server(debug=False, host="0.0.0.0", port=SERVER_PORT)
867+
app.run_server(debug=True, port=SERVER_PORT)
868+
# app.run_server(debug=False, host="0.0.0.0", port=SERVER_PORT)
869+
870+
871+
# df = create_aqi_df()
872+
# print(df)

0 commit comments

Comments
 (0)