-
Notifications
You must be signed in to change notification settings - Fork 0
/
tempgraph.py
191 lines (157 loc) · 6.12 KB
/
tempgraph.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import dateutil.parser as parser
import os
import sys
import json
import datetime
import re
from tweet import *
from datagetter import TempGetter
class tempgraph:
def Do(self, home_path, year, month, day, tweetmsg):
seprate = os.sep
filename = home_path + "%s%04d%s%02d%s%02d.json" % (os.sep,year,os.sep,month,os.sep,day)
graphfilename = home_path + "%s%04d%s%02d%s%02d.png" % (os.sep,year,os.sep,month,os.sep,day)
try:
file = open(filename,"r")
except:
sys.exit(1)
jsonObjects = json.load(file)
file.close()
x = []
xgrid = ["00:00", "00:30", "01:00", "01:30", "02:00", "02:30",
"03:00", "03:30", "04:00", "04:30", "05:00", "05:30",
"06:00", "06:30", "07:00", "07:30", "08:00", "08:30",
"09:00", "09:30", "10:00", "10:30", "11:00", "11:30",
"12:00", "12:30", "13:00", "13:30", "14:00", "14:30",
"15:00", "15:30", "16:00", "16:30", "17:00", "17:30",
"18:00", "18:30", "19:00", "19:30", "20:00", "20:30",
"21:00", "21:30", "22:00", "22:30", "23:00", "23:30", "23:59"]
xgrid.sort()
# keylist = jsonObjects.keys()
# for timedata in keylist:
# x.append(timedata)
# y_temp1.append(jsonObjects[timedata]["temp1"])
# y_temp2.append(jsonObjects[timedata]["temp2"])
# 取得した時刻のラベルを設定する
keylist = jsonObjects.keys()
plot_temptures_min = {}
plot_temptures_max = {}
xgrid_temptures = {}
for timedata in keylist:
hourmin = timedata.split(":")
hour = int(hourmin[0])
minuts = int(hourmin[1])
for curi in range(len(xgrid)):
nexti = curi + 1
if nexti >= len(xgrid):
nexti = curi
xgrid_hourmin = xgrid[curi].split(":")
xgrid_hour = int(xgrid_hourmin[0])
xgrid_minuts = int(xgrid_hourmin[1])
xgrid_next_hourmin = xgrid[nexti].split(":")
xgrid_next_hour = int(xgrid_next_hourmin[0])
xgrid_next_minuts = int(xgrid_next_hourmin[1])
if xgrid[curi] not in xgrid_temptures:
xgrid_temptures.update({xgrid[curi]:{"arrays":[], "min":{}, "max":{}}})
now_minuts = hour * 60 + minuts
if not ((now_minuts >= xgrid_hour * 60 + xgrid_minuts) and (now_minuts < xgrid_next_hour * 60 + xgrid_next_minuts)):
continue
# print (timedata + ":" + str(jsonObjects[timedata]))
# print ("data %d:%d" % (hour, minuts))
# print ("graph %d %d" % (xgrid_hour, xgrid_minuts))
# print ("graph next %d %d" % (xgrid_next_hour, xgrid_next_minuts))
# print (xgrid[curi] + ":" + str(jsonObjects[timedata]))
xgrid_temptures[xgrid[curi]]["arrays"].append(jsonObjects[timedata])
# 最小値を更新する
for tempkey in jsonObjects[timedata]:
if tempkey not in plot_temptures_min:
plot_temptures_min[tempkey] = []
if tempkey not in xgrid_temptures[xgrid[curi]]["min"]:
xgrid_temptures[xgrid[curi]]["min"][tempkey] = jsonObjects[timedata][tempkey]
continue
if xgrid_temptures[xgrid[curi]]["min"][tempkey] > jsonObjects[timedata][tempkey]:
xgrid_temptures[xgrid[curi]]["min"][tempkey] = jsonObjects[timedata][tempkey]
# 最大値を更新する
for tempkey in jsonObjects[timedata]:
if tempkey not in plot_temptures_max:
plot_temptures_max[tempkey] = []
if tempkey not in xgrid_temptures[xgrid[curi]]["max"]:
xgrid_temptures[xgrid[curi]]["max"][tempkey] = jsonObjects[timedata][tempkey]
continue
if xgrid_temptures[xgrid[curi]]["max"][tempkey] < jsonObjects[timedata][tempkey]:
xgrid_temptures[xgrid[curi]]["max"][tempkey] = jsonObjects[timedata][tempkey]
for xgridtime in xgrid:
tempKeys = plot_temptures_min.keys()
for tempKey in tempKeys:
plot_temptures_min[tempKey].append(0)
plot_temptures_max[tempKey].append(0)
for temptures in xgrid_temptures[xgridtime]["arrays"]:
tempKeys = temptures.keys()
for tempKey in tempKeys:
plot_temptures_min[tempKey][-1] = xgrid_temptures[xgridtime]["min"][tempKey]
plot_temptures_max[tempKey][-1] = xgrid_temptures[xgridtime]["max"][tempKey]
tempKeys = plot_temptures_min.keys()
max_length = 0
for tempKey in tempKeys:
max_length = max(len(plot_temptures_min[tempKey]), max_length)
for tempKey in tempKeys:
for counter in range(len(plot_temptures_min[tempKey]), max_length):
plot_temptures_min[tempKey].append(0)
tempKeys = plot_temptures_max.keys()
max_length = 0
for tempKey in tempKeys:
max_length = max(len(plot_temptures_max[tempKey]), max_length)
for tempKey in tempKeys:
for counter in range(len(plot_temptures_max[tempKey]), max_length):
plot_temptures_max[tempKey].append(0)
# データをセット
fig = plt.figure()
tempKeys = plot_temptures_min.keys()
tg = TempGetter()
tg.Create(home_path)
gridx = 1
for tempKey in tempKeys:
ax = fig.add_subplot(2, 1, gridx)
gridx = gridx + 1
xval = [parser.parse(xv) for xv in xgrid]
devicename = tg.GetDeviceNickName(tempKey)
if devicename is None:
devicename = tempKey
with plt.style.context('fivethirtyeight'):
ax.plot(xval,plot_temptures_max[tempKey],linestyle="-",label="max", marker='o', linewidth = 2.0, color='red')
ax.plot(xval,plot_temptures_min[tempKey],linestyle="-",label="min", marker='o', linewidth = 2.0, color='green')
ax.set_ylim(0, 60)
ax.set_xlabel("Time")
ax.set_ylabel("Tempture")
ax.legend(loc="upper right")
ax.set_title("%04d/%02d/%02d(%s)" % (year,month,day, tg.GetDeviceNickNameEN(tempKey)))
ax.grid()
# グラフのフォーマットの設定
days = mdates.HourLocator() # every day
daysFmt = mdates.DateFormatter('%H:%M')
ax.xaxis.set_major_locator(days)
ax.xaxis.set_major_formatter(daysFmt)
fig.autofmt_xdate()
plt.savefig(graphfilename)
if tweetmsg is not "":
tw = tweet()
tw.Create(home_path)
tw.DoImage(graphfilename, tweetmsg)
if __name__ == "__main__":
param = sys.argv
home_path = param[1] + os.sep
year = int(param[2])
month = int(param[3])
day = int(param[4])
tweetmsg = ""
for arg in param:
match = re.search("-tw=\S*", arg)
if match is not None:
tweetmsg = match.group().split("-tw=")[1]
tg = tempgraph()
tg.Do(home_path, year, month, day, tweetmsg)
sys.exit(0)