Makefile: new MONITOR_PORT for jadzia
[toast/airingbutler.git] / plot_mhz19.py
1 #!/usr/bin/python3
2
3 import csv
4 import argparse
5 from datetime import datetime, date, timedelta
6 from matplotlib import pyplot as plt
7
8 filename = 'log.csv'
9 '''
10 Time,TS,CO2,Temp
11 21:02:02,0,661,24
12 21:02:08,5021,657,24
13
14 '''
15
16 def parsedatetime(timevalue, currentdate):
17     dt = datetime.strptime(timevalue, '%H:%M:%S')
18     return datetime.combine(currentdate, dt.time())
19
20 def main():
21     parser = argparse.ArgumentParser('Plot log.csv.')
22     parser.add_argument('--temperature', '-t', action='store_true', help='Include temperature plot')
23     args = parser.parse_args()
24
25     with open(filename) as f:
26         reader = csv.reader(f)
27         headers = next(reader)
28
29         time = []
30         co2 = []
31         temp = []
32         startdate = date(2020, 10, 22)
33         for row in reader:
34             dt = parsedatetime(row[0], startdate)
35             if len(time) > 0 and dt - time[-1] < timedelta(hours=-2): # DST switch!
36                 startdate += timedelta(days=1)
37                 dt += timedelta(days=1)
38             time.append(dt)
39             co2.append(int(row[2]))
40             temp.append(int(row[3]))
41
42         plt.plot(time, co2, c = 'red')
43         plt.title('CO₂')
44         plt.xlabel('time')
45         plt.ylabel('ppm')
46         plt.tick_params(axis = 'both', which = 'major')
47         if args.temperature is True:
48             plt.gca().twinx()
49             plt.plot(time, temp, c = 'blue')
50         plt.show()
51
52 if __name__ == '__main__':
53     main()