annotate logplot.py @ 8:cb03cea09bda

Logfile plotter
author darius
date Sat, 29 Sep 2007 14:40:33 +0000
parents
children 87d9d862ecc4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
1 #!/usr/bin/env python
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
2
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
3 import re, datetime, time, pylab, matplotlib
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
4 daterestr = '([0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}): '
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
5 targtre = re.compile(daterestr + 'target temperature - (-?[0-9]+\.[0-9]+)')
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
6 linere = re.compile(daterestr + '(-?[0-9]+\.[0-9]+)\s+(-?[0-9]+\.[0-9]+)\s+(-?[0-9]+\.[0-9]+)\s+([a-z-]+)\s+([a-z-]+)')
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
7 datefmt = '%Y/%m/%d %H:%M:%S'
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
8 tz = matplotlib.pytz.timezone('Australia/Adelaide')
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
9
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
10 targetTemp = 18.0
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
11 logfile = '/tmp/beermon.log'
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
12 start = time.mktime(time.strptime("2007/09/24 00:00:00", datefmt))
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
13 #end = time.mktime(time.strptime("2007/09/28 00:00:00", datefmt))
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
14 end = None
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
15
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
16 f = open(logfile)
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
17
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
18 times = []
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
19 fermTemps = []
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
20 fridgeTemps = []
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
21 ambTemps = []
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
22 states = []
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
23 targetTemps = []
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
24
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
25 for line in f:
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
26 m = linere.match(line)
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
27 if (m == None):
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
28 m = targtre.match(line)
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
29 if (m != None):
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
30 t = time.mktime(time.strptime(m.group(1), datefmt))
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
31 if (t < start or t > end):
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
32 continue
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
33
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
34 targetTemp = float(m.group(2))
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
35 continue
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
36 else:
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
37 continue
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
38
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
39 t = time.mktime(time.strptime(m.group(1), datefmt))
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
40 if (start != None and t < start):
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
41 continue
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
42
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
43 if (end != None and t > end):
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
44 continue
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
45
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
46 fermTemp = float(m.group(2))
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
47 fridgeTemp = float(m.group(3))
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
48 ambTemp = float(m.group(4))
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
49 state = m.group(5)
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
50
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
51 times.append(t / (24 * 60 * 60)) # Is in float days
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
52 fermTemps.append(fermTemp)
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
53 fridgeTemps.append(fridgeTemp)
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
54 ambTemps.append(ambTemp)
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
55 states.append(state)
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
56 targetTemps.append(targetTemp)
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
57
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
58 print "From %s to %s" % (time.asctime(time.localtime(times[0] * (24 * 60 * 60))),
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
59 time.asctime(time.localtime(times[-1] * (24 * 60 * 60))))
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
60 fig = pylab.figure()
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
61 ax = fig.add_subplot(111)
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
62 p = ax.plot(times, fermTemps, times, fridgeTemps, times, ambTemps, times, targetTemps)
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
63 ax.set_autoscale_on(True)
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
64 hoursFmt = pylab.DateFormatter('%d %b %H:%M', tz = tz)
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
65 ax.xaxis.set_major_formatter(hoursFmt)
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
66 labels = ax.get_xticklabels()
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
67 pylab.setp(labels, 'rotation', 5.0)
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
68 ax.grid(True)
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
69 ax.set_xlabel('time(UTC)')
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
70 ax.set_ylabel('Temperature(C)')
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
71 pylab.legend((p[0], p[1], p[2], p[3]), ('Fermenter', 'Fridge', 'Ambient', 'Target'), 'best')
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
72 #pylab.legend((p[0], p[1], p[2]), ('Fermenter', 'Fridge', 'Ambient'), 'best')
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
73 #pylab.axhline(y = targetTemp)
cb03cea09bda Logfile plotter
darius
parents:
diff changeset
74 pylab.show()