annotate logplot.py @ 10:a1e7bb27f6d5

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