-
Notifications
You must be signed in to change notification settings - Fork 3
/
report.py
executable file
·131 lines (102 loc) · 4.73 KB
/
report.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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2021-2023 Mathieu Schopfer
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse
from datetime import datetime
import os
import os.path
from matplotlib import pyplot as plt
import numpy as np
import airco2ntrol_mini as aco2m
_co2_line = None
_last_point = None
_warning_threshold = 600
_danger_threshold = 800
parser = argparse.ArgumentParser(description='Measure and log a CO2')
parser.add_argument('--prefix', type=str, help='file name prefix, default `airco2ntrol`', default='airco2ntrol')
parser.add_argument('--dir', type=str, help='log file output directory', default='logs')
parser.add_argument('--range', type=float, help='plot time range in hours', default=8)
parser.add_argument('--interval', type=int, help='sampling time interval in seconds', default=300)
args = parser.parse_args()
def _format_axis_time(t, pos=None):
return datetime.fromtimestamp(t).time().isoformat(timespec='minutes')
def update_plot(t, co2, _):
timestamps = np.append(_co2_line.get_xdata(), t)
co2s = np.append(_co2_line.get_ydata(), co2)
plot_range_s = args.range*3600 # Plot range in seconds
# Remove data out of plot time range
k = np.flatnonzero(timestamps[-1]-timestamps < plot_range_s)
timestamps = timestamps[k]
co2s = co2s[k]
xsup = timestamps[0]+plot_range_s if timestamps[-1]-timestamps[0] < plot_range_s else timestamps[-1]
plt.xlim(timestamps[0], xsup)
ymax_default = 1000
co2max = np.max(co2s)
ysup = ymax_default if co2max < ymax_default-150 else co2max+150
plt.ylim(300, ysup)
_co2_line.set_xdata(timestamps)
_co2_line.set_ydata(co2s)
_last_point.set_xdata([t])
_last_point.set_ydata([co2])
fig = plt.gcf()
fig.canvas.draw()
fig.canvas.flush_events()
if __name__ == '__main__':
try:
aco2m.open_device()
except OSError as e:
print('Could not open the device, check that it is correctly plugged:', e)
else:
# Create output directory
odir = os.path.expanduser(args.dir)
if not os.path.exists(odir):
os.mkdir(odir)
# Create log file
timestamp = datetime.now().isoformat(timespec='seconds')
filePath = os.path.join(odir, f'{args.prefix}_{timestamp}.csv')
with open(filePath, 'at', encoding='UTF-8', errors='replace', buffering = 1) as logFile:
# CSV logging
def logger(t, co2, temperature):
_t = datetime.fromtimestamp(t)
# Log to file
timestamp = _t.isoformat(timespec='seconds')
logFile.write(f'{timestamp:s},{co2:.0f},{temperature:.1f}\n')
# Console output
timestamp = _t.time().isoformat(timespec='seconds')
print(f'{timestamp:s}\t{co2:.0f} ppm\t\t{temperature:.1f} °C', end='\r')
aco2m.register_watcher(logger)
logFile.write('Time,CO2[ppm],Temperature[°C]\n')
print('Time\t\tCO2\t\tTemperature')
# Plotting
aco2m.register_watcher(update_plot)
plt.ion() # Activate interactive plotting
_co2_line, = plt.plot([], [], linewidth=2, color='black') # Init line
_last_point, = plt.plot([], [], marker='o', color='black') # Init line
# Add background colours
plt.axhspan(0, _warning_threshold, color='tab:green', alpha=0.5)
plt.axhspan(_warning_threshold, _danger_threshold, color='tab:orange', alpha=0.5)
plt.axhspan(_danger_threshold, 3000, color='tab:red', alpha=0.5) # 3000 ppm is the device measurement limit
# Customize look
ax = plt.gca()
ax.get_xaxis().set_major_formatter(_format_axis_time)
plt.grid(color='whitesmoke', linestyle=':', linewidth=1)
plt.xlabel('Time')
plt.ylabel('CO2 [ppm]')
# Title specifies time range in hours or minutes
range = f'{args.range:.2f}' if args.range >= 1 else f'{args.range*60:.0f}'
units = 'hours' if args.range >= 1 else 'minutes'
plt.title(f'CO2 concentration over the last {range} {units}')
aco2m.watch(args.interval)