creating bar charts for pm2.5 and pm10 daily average compared against thresh from WHO
This commit is contained in:
parent
02df0b4026
commit
e78976b576
|
@ -0,0 +1,95 @@
|
|||
""" creates the PM 2.5 and pm 10 graphs """
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from matplotlib import pyplot as plt
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
from app.db_connect import db_connect, db_close
|
||||
|
||||
|
||||
def get_pm_data(config):
|
||||
""" gets last 10 days worth of data"""
|
||||
now = datetime.now()
|
||||
day_until = int(now.date().strftime('%s'))
|
||||
day_from = day_until - 10 * 24 * 60 * 60
|
||||
conn, cur = db_connect(config)
|
||||
cur.execute(
|
||||
f'SELECT epoch_time, pm25, pm10 FROM aqi \
|
||||
WHERE epoch_time > {day_from} \
|
||||
AND epoch_time < {day_until} \
|
||||
ORDER BY epoch_time DESC;'
|
||||
)
|
||||
rows = cur.fetchall()
|
||||
db_close(conn, cur)
|
||||
return rows
|
||||
|
||||
|
||||
def get_pm_axis(rows):
|
||||
""" build axis """
|
||||
# build dataframe
|
||||
x_timeline = [datetime.fromtimestamp(i[0]) for i in rows]
|
||||
y_pm25_values = [int(i[1]) for i in rows]
|
||||
y_pm10_values = [int(i[2]) for i in rows]
|
||||
data = {
|
||||
'timestamp': x_timeline,
|
||||
'pm25': y_pm25_values,
|
||||
'pm10': y_pm10_values
|
||||
}
|
||||
df = pd.DataFrame(data)
|
||||
indexed = df.set_index('timestamp')
|
||||
indexed.sort_values(by=['timestamp'], inplace=True, ascending=True)
|
||||
mean = indexed.resample('1d').mean()
|
||||
mean.reset_index(level=0, inplace=True)
|
||||
# axis
|
||||
mean['pm25'] = mean['pm25'].round()
|
||||
mean['pm10'] = mean['pm10'].round()
|
||||
x = mean['timestamp']
|
||||
y_1 = mean['pm25']
|
||||
y_2 = mean['pm10']
|
||||
return x, y_1, y_2
|
||||
|
||||
|
||||
def build_pm_plot(x, y, y_max, thresh, title):
|
||||
""" write plots to file """
|
||||
file_name = title.replace('.', '')
|
||||
# make ticks
|
||||
x_range = np.arange(10).tolist()
|
||||
x_date_time = pd.to_datetime(x).dt.date.unique()
|
||||
x_dates = [i.strftime('%d %b') for i in x_date_time]
|
||||
# color
|
||||
col = []
|
||||
for val in y:
|
||||
if val < thresh:
|
||||
col.append('#00cc00')
|
||||
else:
|
||||
col.append('#cc0000')
|
||||
# title
|
||||
plt_title = f'PM {title} values for last 10 days'
|
||||
# plot
|
||||
plt.style.use('seaborn')
|
||||
plt.bar(x_dates, y, color=col, width=0.5)
|
||||
plt.axhline(y=thresh, color='#00cc00', linestyle=':')
|
||||
plt.xticks(ticks=x_range, labels=x_dates)
|
||||
plt.yticks(np.arange(0, y_max, step=25))
|
||||
plt.title(plt_title)
|
||||
plt.tight_layout()
|
||||
plt.savefig(f'dyn/pm{file_name}.png', dpi=300)
|
||||
plt.close('all')
|
||||
plt.figure()
|
||||
|
||||
|
||||
|
||||
def rebuild_pm_bar(config):
|
||||
""" rebuild pm2.5 and pm10 values """
|
||||
rows = get_pm_data(config)
|
||||
x, y_1, y_2 = get_pm_axis(rows)
|
||||
# max
|
||||
y_max = np.ceil(max(y_1.append(y_2))/25)*25 + 25
|
||||
# pm 2.5
|
||||
build_pm_plot(x, y_1, y_max, thresh=25, title='2.5')
|
||||
# pm 10
|
||||
build_pm_plot(x, y_2, y_max, thresh=50, title='10')
|
||||
# done
|
||||
print('recreated PM 2.5 and PM 10 graphs')
|
|
@ -10,6 +10,7 @@ from app import app
|
|||
from app import aqi_parser
|
||||
from app import weather
|
||||
from app import graph
|
||||
from app import graph_pm
|
||||
from app.db_connect import db_insert
|
||||
|
||||
|
||||
|
@ -38,6 +39,7 @@ auth = HTTPBasicAuth()
|
|||
config = get_config()
|
||||
weather.handle_weather(config)
|
||||
graph.create_current(config)
|
||||
graph_pm.rebuild_pm_bar(config)
|
||||
graph.rebuild_3days(config)
|
||||
graph.rebuild_7days(config)
|
||||
# build username / pw dict for basic auth
|
||||
|
@ -54,10 +56,13 @@ scheduler.add_job(
|
|||
graph.create_current, args=[config], trigger="cron", minute='*/5', name='current_graph'
|
||||
)
|
||||
scheduler.add_job(
|
||||
graph.rebuild_3days, args=[config], trigger="cron", day='*', hour='1', minute='3', name='3_days'
|
||||
graph.rebuild_3days, args=[config], trigger="cron", day='*', hour='1', minute='1', name='3_days'
|
||||
)
|
||||
scheduler.add_job(
|
||||
graph.rebuild_7days, args=[config], trigger="cron", day='*', hour='1', minute='4', name='7_days'
|
||||
graph.rebuild_7days, args=[config], trigger="cron", day='*', hour='1', minute='2', name='7_days'
|
||||
)
|
||||
scheduler.add_job(
|
||||
graph_pm.rebuild_pm_bar, args=[config], trigger="cron", day='*', hour='1', minute='3', name='pm_bar'
|
||||
)
|
||||
scheduler.start()
|
||||
|
||||
|
|
Loading…
Reference in New Issue