-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccPlot.py
executable file
·91 lines (73 loc) · 2.12 KB
/
ccPlot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/env python
import matplotlib.pyplot as plt
import os
import sqlite
import sys
import time
values = {
'temperature': [],
'phase1': [],
'phase2': [],
'phase3': [],
}
points = {
'temperature': '',
'phase1': '',
'phase2': '',
'phase3': '',
}
pid = str(os.getpid())
pidfile = "/tmp/ccPlot.pid"
if os.path.isfile(pidfile):
print "%s already exists, exiting" % pidfile
sys.exit()
else:
file(pidfile, 'w').write(pid)
try:
database = sqlite.connect('/opt/cc2p.data/power.sqlite')
cursor = database.cursor()
for value in values:
cursor.execute(
'''SELECT * FROM %s ORDER BY timestamp DESC LIMIT 2592000''' % value
)
values[value] = cursor.fetchall()
cursor.close()
database.close()
except Exception, e:
print "Failed to access database"
print "Exception:\n%s" % str(e)
os.unlink(pidfile)
sys.exit(e)
try:
for value in values:
for row in reversed(values[value]):
utcTime = time.gmtime(row[0])
isoTime = time.strftime('%Y-%m-%dT%H:%M:%SZ', utcTime)
if points[value] == '':
points[value] = [[row[0]], [row[1]], [isoTime], ]
else:
points[value][0].append(row[0])
points[value][1].append(row[1])
points[value][2].append(isoTime)
try:
figure = plt.figure()
axis = figure.add_subplot(111)
axis.set_xlabel('Date / Time')
axis.set_ylabel('Power (W)')
axis.plot(points[value][0], points[value][1])
# axis.set_xticklabels(points[value][2])
# plt.setp(axis.get_xticklabels(), rotation='60')
# figure.subplots_adjust(bottom=0.34)
figure.savefig('%s.png' % value)
print "Written %s.png" % value
except Exception, e:
print "Failed to create graph"
print "Exception:\n%s" % str(e)
os.unlink(pidfile)
sys.exit(e)
except Exception, e:
print "Failed to create points for graph"
print "Exception:\n%s" % str(e)
os.unlink(pidfile)
sys.exit(e)
os.unlink(pidfile)