]> ToastFreeware Gitweb - toast/airingbutler.git/blobdiff - plot_mhz19.py
annotate-output now accepts a datetime format, so let's use it
[toast/airingbutler.git] / plot_mhz19.py
index 526c975d8f75fe91ec9db698768ee60151c2917c..7d53481d34b141b33198380d6b5d86ab249b89d0 100755 (executable)
@@ -1,43 +1,48 @@
 #!/usr/bin/python3
 
 import csv
+import argparse
 from datetime import datetime, date, timedelta
 from matplotlib import pyplot as plt
 
-filename = 'log.csv'
 '''
+# annotate-output +"%F %T" ./log_mhz19.py -d /dev/arduino | tee -a log
+filename = 'log.csv'
 Time,TS,CO2,Temp
-21:02:02,0,661,24
-21:02:08,5021,657,24
-
+2024-09-22 01:08:15,279472,1578,24
+2024-09-22 01:08:20,284518,1577,24
 '''
 
-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('-t', '--temperature', action='store_true', help='Include temperature plot')
+    args = parser.parse_args()
+
+    with open(args.filename) as f:
+        reader = csv.reader(f)
+        headers = next(reader)
 
-with open(filename) as f:
-    reader = csv.reader(f)
-    headers = next(reader)
+        time = []
+        co2 = []
+        temp = []
+        for row in reader:
+            dt = datetime.strptime(row[0], '%Y-%m-%d %H:%M:%S')
+            if len(time) > 0 and dt - time[-1] < timedelta(hours=-2): # DST switch!
+                dt += timedelta(days=1)
+            time.append(dt)
+            co2.append(int(row[2]))
+            temp.append(int(row[3]))
 
-    time = []
-    co2 = []
-    temp = []
-    startdate = date(2020, 10, 22)
-    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()
 
-    plt.plot(time, co2, c = 'red')
-    plt.title('CO₂')
-    plt.xlabel('time')
-    plt.ylabel('ppm')
-    plt.tick_params(axis = 'both', which = 'major')
-    plt.gca().twinx()
-    plt.plot(time, temp, c = 'blue')
-    plt.show()
+if __name__ == '__main__':
+    main()