From 281972260d0302dddd23c0012bb025e5bfb04d63 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 28 Jul 2013 14:25:44 +0200 Subject: add plot.py Signed-off-by: Florian Pritz --- plot.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100755 plot.py diff --git a/plot.py b/plot.py new file mode 100755 index 0000000..f4a7dc6 --- /dev/null +++ b/plot.py @@ -0,0 +1,79 @@ +#!/usr/bin/python3 + +# based on https://github.com/cnvogelg/ardu/blob/master/datalog/tools/plot_log.py + +import sys +import subprocess +import os + +if len(sys.argv) != 3: + print("Usage: plot.py ", file=sys.stderr) + sys.exit(1) + +log_file = sys.argv[1] +png_file = sys.argv[2] +rrd_file = "tmp.rrd" + +create = [ + 'rrdtool','create',rrd_file,'--step','30','--start',0, + 'DS:temp:GAUGE:50:U:U', + 'RRA:AVERAGE:0.5:1:10000', + 'RRA:MIN:0.5:1:10000', + 'RRA:MAX:0.5:1:10000' +] + +graph = [ + 'rrdtool','graph',png_file, + '-s',None,'-e',None, + '--height=300','--width=750', + '--color=BACK#FFFFFF', + '--vertical-label', '°C', + '--title','Temperature', + '-l','25','-u','30', + 'DEF:temp='+rrd_file+':temp:AVERAGE', + 'LINE1:temp#3366CC' +] + +samples = [] +f = open(log_file,"r") +for line in f: + elem = line.split(' ') + temp = float(elem[1].split("=")[1]) + ts = int(elem[0]) + samples.append((ts,temp)) +f.close() + +# create rrd +start_ts = samples[0][0] +end_ts = samples[-1][0] +create[6] = str(start_ts - 1) +print("creating rrdb:",create) +ret = subprocess.call(create) +if ret != 0: + print("ERROR calling: {}".format(" ".join(create))) + sys.exit(1) + +# update rrd +n = len(samples) +for i in range(n): + print(i,n,end="\r") + sys.stdout.flush() + s = samples[i] + entry = "{}:{}".format(s[0],s[1]) + cmd = ['rrdupdate',rrd_file,'--',entry] + ret = subprocess.call(cmd) + if ret != 0: + print("ERROR calling: {}".format(" ".join(cmd))) + sys.exit(1) + +# graph +print("plotting rrdb:",graph) +graph[4] = str(start_ts) +graph[6] = str(end_ts) +ret = subprocess.call(graph) +if ret != 0: + print("ERROR calling: {}".format(" ".join(cmd))) + sys.exit(1) + +os.unlink(rrd_file) +print("done") -- cgit v1.2.3-24-g4f1b