-
Notifications
You must be signed in to change notification settings - Fork 0
/
watts_json.py
executable file
·112 lines (85 loc) · 2.78 KB
/
watts_json.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/env python
import Image
import urllib
import datetime
import json
import os
class Watts(object):
def __init__(self):
#imgurl = "http://www.tepco.co.jp/forecast/html/images/juyo-j.gif"
imgurl = "http://www.tepco.co.jp.cache.yimg.jp/forecast/html/images/juyo-j.gif"
tmpfile = '/tmp/juyo-j.gif'
urllib.urlretrieve(imgurl, tmpfile)
self.im = Image.open(tmpfile)
self.data = list(self.im.getdata())
self.x = self.im.size[0]
self.y = self.im.size[1]
self.line_color = self.data[0]
self._upper_line()
self._lower_line()
self._time_list()
self._scan_watts()
self._calc_watts()
self._json_write()
def _upper_line(self):
start = 50
end = 60
xpos = int(self.x / 2)
base = self.data[start * self.x + xpos]
for y in xrange(start, end):
if base != self.data[y * self.x + xpos]:
# print "upper_lins is %d" % y
self.upper_line = y
return
raise Error
def _lower_line(self):
start = 270
end = 300
xpos = int(self.x / 2)
base = self.data[start * self.x + xpos]
for y in xrange(start, end):
if base != self.data[y * self.x + xpos]:
# print "lower_line is %d" % y
self.lower_line = y
return
raise Error
def _time_list(self):
start = 50
end = 590
self.time_list = list()
ypos = self.lower_line - 1
for x in xrange(start, end):
cur = self.data[self.x * ypos + x]
if cur == self.line_color:
self.time_list.append(x)
if len(self.time_list) >= 24:
break
def _scan_watts(self):
start = 50
end = 590
self.raw_watts_list = list()
for time in self.time_list:
for y in xrange(self.upper_line + 1, self.lower_line):
if self.data[y * self.x + (time + 1)] == self.line_color:
self.raw_watts_list.append(self.lower_line - y)
# print self.raw_watts_list
def _calc_watts(self):
self.watts = dict()
factor = 6000.0 / (self.lower_line - self.upper_line)
# print factor
time = 0
for raw_watt in self.raw_watts_list:
self.watts[time] = raw_watt * factor
time += 1
def _json_write(self):
today = datetime.date.today()
filename = today.strftime("watts-%Y%m%d.json")
alias = "watts.json"
f = open(filename, 'w')
json.dump(self.watts, f)
f.close()
os.remove(alias)
os.symlink(filename, alias)
if __name__ == '__main__':
w = Watts()
print w.watts