From: gregor herrmann Date: Mon, 26 Oct 2020 01:43:47 +0000 (+0100) Subject: plot_mhz19.py: make temperature plot optional X-Git-Url: https://git.toastfreeware.priv.at/toast/airingbutler.git/commitdiff_plain/dd2475feeefbb1b7b6dc31722bd645b7d38843ed plot_mhz19.py: make temperature plot optional --- diff --git a/plot_mhz19.py b/plot_mhz19.py index 526c975..4d3b90a 100755 --- a/plot_mhz19.py +++ b/plot_mhz19.py @@ -1,6 +1,7 @@ #!/usr/bin/python3 import csv +import argparse from datetime import datetime, date, timedelta from matplotlib import pyplot as plt @@ -16,28 +17,37 @@ def parsedatetime(timevalue, currentdate): dt = datetime.strptime(timevalue, '%H:%M:%S') return datetime.combine(currentdate, dt.time()) -with open(filename) as f: - reader = csv.reader(f) - headers = next(reader) - - 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') - plt.gca().twinx() - plt.plot(time, temp, c = 'blue') - plt.show() +def main(): + parser = argparse.ArgumentParser('Plot log.csv.') + parser.add_argument('--temperature', '-t', action='store_true', help='Include temperature plot') + args = parser.parse_args() + + with open(filename) as f: + reader = csv.reader(f) + headers = next(reader) + + 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() + +if __name__ == '__main__': + main()