]> ToastFreeware Gitweb - toast/airingbutler.git/blobdiff - plot_mhz19.py
plot_mhz19.py: (optionally) pass filename and startdate
[toast/airingbutler.git] / plot_mhz19.py
index fc3976ad005ef7b1b440d6a6557a3c57cbf04f9a..defb0da640ee66956c9bfb5b3b0ec764703c5c11 100755 (executable)
@@ -1,29 +1,55 @@
 #!/usr/bin/python3
 
 import csv
 #!/usr/bin/python3
 
 import csv
+import argparse
+from datetime import datetime, date, timedelta
 from matplotlib import pyplot as plt
 
 from matplotlib import pyplot as plt
 
-filename = 'log.csv'
 '''
 '''
+filename = 'log.csv'
 Time,TS,CO2,Temp
 21:02:02,0,661,24
 21:02:08,5021,657,24
 
 '''
 Time,TS,CO2,Temp
 21:02:02,0,661,24
 21:02:08,5021,657,24
 
 '''
-with open(filename) as f:
-    reader = csv.reader(f)
-    headers = next(reader)
-
-    time = []
-    co2 = []
-    for row in reader:
-        time.append(row[0])
-        co2.append(int(row[2]))
-
-    fig = plt.figure(dpi = 128, figsize = (10,6))
-    plt.plot(co2, c = 'red')
-    plt.title('CO₂', fontsize = 24)
-    plt.xlabel('',fontsize = 16)
-    plt.ylabel('ppm', fontsize = 16)
-    plt.tick_params(axis = 'both', which = 'major', labelsize = 16)
-    plt.show()
+
+def parsedatetime(timevalue, currentdate):
+    dt = datetime.strptime(timevalue, '%H:%M:%S')
+    return datetime.combine(currentdate, dt.time())
+
+def main():
+    parser = argparse.ArgumentParser('Plot log.csv.')
+    parser.add_argument('filename', nargs='?', default='log.csv', help='Name of csv file')
+    parser.add_argument('-s', '--startdate', nargs='?', default='2020-10-22', help='First date in csv file')
+    parser.add_argument('-t', '--temperature', action='store_true', help='Include temperature plot')
+    args = parser.parse_args()
+    startdate = datetime.strptime(args.startdate, '%Y-%m-%d').date()
+
+    with open(args.filename) as f:
+        reader = csv.reader(f)
+        headers = next(reader)
+
+        time = []
+        co2 = []
+        temp = []
+        for row in reader:
+            dt = parsedatetime(row[0], startdate)
+            if len(time) > 0 and dt - time[-1] < timedelta(hours=-2): # DST switch!
+                startdate += timedelta(days=1)
+                dt += timedelta(days=1)
+            time.append(dt)
+            co2.append(int(row[2]))
+            temp.append(int(row[3]))
+
+        plt.plot(time, co2, c = 'red')
+        plt.title('CO₂')
+        plt.xlabel('time')
+        plt.ylabel('ppm')
+        plt.tick_params(axis = 'both', which = 'major')
+        if args.temperature is True:
+            plt.gca().twinx()
+            plt.plot(time, temp, c = 'blue')
+        plt.show()
+
+if __name__ == '__main__':
+    main()