summaryrefslogtreecommitdiffstats
path: root/plot.py
diff options
context:
space:
mode:
Diffstat (limited to 'plot.py')
-rwxr-xr-xplot.py126
1 files changed, 126 insertions, 0 deletions
diff --git a/plot.py b/plot.py
new file mode 100755
index 0000000..6d9d304
--- /dev/null
+++ b/plot.py
@@ -0,0 +1,126 @@
+#!/usr/bin/python3
+
+# based on https://github.com/cnvogelg/ardu/blob/master/datalog/tools/plot_log.py
+
+# This script plots a log file of the format
+# "unix_timestamp key=value key2=value key3=value ..."
+# using rrdtool. Keys are arbitrary values of the format "type:number"
+# (e.g. t:1, t:2, h:1 for temperature and humidity) used to identify each sample.
+# Note that the keys currently ignored.
+#
+# The whole log is plotted into one graph.
+
+import sys
+import subprocess
+import os
+
+if len(sys.argv) != 3:
+ print("Usage: plot.py <log> <png>", file=sys.stderr)
+ sys.exit(1)
+
+log_file = sys.argv[1]
+png_file = sys.argv[2]
+rrd_file = "tmp.rrd"
+
+#### start of config
+
+# If you change this you also need to change the create and graph lists below.
+# Also note that samples have to be in the same order every time (the key is currently ignored).
+samples_per_line=3
+
+# The 4th field of the DS lines is the heartbeat. If you sample at an
+# interval < that value you will not see a graph. Simply adjust if necessary.
+create = [
+ # Index of the 0 after --start is used below. Adjust code if you
+ # change the amount of values before it.
+ 'rrdtool','create',rrd_file,'--step','30','--start',0,
+
+ 'DS:temp1:GAUGE:50:U:U',
+ 'DS:temp2:GAUGE:50:U:U',
+ 'DS:hum1:GAUGE:50:U:U',
+
+ 'RRA:AVERAGE:0.5:1:20000',
+ 'RRA:MIN:0.5:1:20000',
+ 'RRA:MAX:0.5:1:20000',
+]
+
+graph = [
+ # Indices of the two Nones are used below. Adjust the code if you
+ # change the amount of values before them..
+ 'rrdtool','graph',png_file,
+ '-s',None,'-e',None,
+ '--height=500','--width=1000',
+ '--color=BACK#FFFFFF',
+ #'--title','Temperature',
+ '--vertical-label', 'Temperature °C',
+ '--right-axis-label', 'Humidity %',
+ '--right-axis', '2:0',
+ '--alt-autoscale',
+ #'-l','25','-u','30',
+ # --left-axis-format is only supported in rrdtool-git
+ #'--left-axis-format', '%4.2lf',
+ '--right-axis-format', '%4.2lf',
+
+ 'DEF:temp1='+rrd_file+':temp1:AVERAGE',
+ 'LINE1:temp1#339966:temp 1\l',
+ 'DEF:temp2='+rrd_file+':temp2:AVERAGE',
+ 'LINE1:temp2#663399:temp 2\l',
+ 'DEF:hum1='+rrd_file+':hum1:AVERAGE',
+ 'CDEF:hum11=hum1,2,/',
+ 'LINE1:hum11#996633:hum 1\l',
+]
+
+#### end of config
+
+
+# load samples
+samples = []
+f = open(log_file,"r")
+for line in f:
+ elem = line.split(' ')
+ values = []
+ # timestamp
+ values.append(int(elem[0]))
+ # values
+ for i in range(1, samples_per_line + 1):
+ values.append(float(elem[i].split("=")[1]))
+ samples.append(values)
+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
+# TODO: multiple calls in case of too many arguments
+n = len(samples)
+cmd = ['rrdupdate', rrd_file, '--']
+for i in range(n):
+ entry = ":".join(map(str, samples[i]))
+ cmd.append(entry);
+
+ret = subprocess.call(cmd)
+if ret != 0:
+ #print("ERROR calling: {}".format(" ".join(cmd)))
+ sys.exit(1)
+
+
+# graph
+graph[4] = str(start_ts)
+graph[6] = str(end_ts)
+ret = subprocess.call(graph)
+if ret != 0:
+ #print("ERROR calling: {}".format(" ".join(graph)))
+ sys.exit(1)
+
+os.unlink(rrd_file)
+
+# vim: set noet: