Mercurial > ~darius > hgwebdir.cgi > agl
comparison agl.py @ 10:70cc1e874157
Print import/export tarrifs
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Mon, 11 Sep 2017 17:57:38 +0930 |
parents | e2807c99e107 |
children | 9fac3371e9ad |
comparison
equal
deleted
inserted
replaced
9:e2807c99e107 | 10:70cc1e874157 |
---|---|
138 | 138 |
139 scale_limits = { | 139 scale_limits = { |
140 'battery_charge' : (0, 100), | 140 'battery_charge' : (0, 100), |
141 } | 141 } |
142 | 142 |
143 tarrifs = { | |
144 'power_exported' : 0.163, | |
145 'power_imported' : 0.380, | |
146 } | |
147 | |
143 names = { | 148 names = { |
144 'battery_charge' : 'Battery Charge', | 149 'battery_charge' : 'Battery Charge', |
145 'battery_power' : 'Battery Power', | 150 'battery_power' : 'Battery Power', |
146 'power_consumed' : 'Power Consumed', | 151 'power_consumed' : 'Power Consumed', |
147 'power_expected' : 'Power Expected', | 152 'power_expected' : 'Power Expected', |
204 if ary.shape[0] == 0: | 209 if ary.shape[0] == 0: |
205 print('No data') | 210 print('No data') |
206 return | 211 return |
207 # Convert TZ naive UTC to TZ aware UTC then adjust to local time | 212 # Convert TZ naive UTC to TZ aware UTC then adjust to local time |
208 xdata = map(lambda f: f.replace(tzinfo = utc).astimezone(lt), ary[:,0]) | 213 xdata = map(lambda f: f.replace(tzinfo = utc).astimezone(lt), ary[:,0]) |
214 xhours = matplotlib.dates.date2num(xdata) * 24 | |
215 xdeltas = xhours[1:] - xhours[0:-1] | |
209 for idx in range(len(cols)): | 216 for idx in range(len(cols)): |
217 col = cols[idx] | |
210 ydata = ary[:,idx + 1] | 218 ydata = ary[:,idx + 1] |
211 if cols[idx] in convs: | 219 if col in convs: |
212 ydata = convs[cols[idx]](ydata) | 220 ydata = convs[col](ydata) |
213 | 221 if col in scale_limits: |
214 if cols[idx] in scale_limits: | 222 scale_min = scale_limits[col][0] |
215 scale_min = scale_limits[cols[idx]][0] | 223 scale_max = scale_limits[col][1] |
216 scale_max = scale_limits[cols[idx]][1] | |
217 else: | 224 else: |
218 scale_min = None | 225 scale_min = None |
219 scale_max = None | 226 scale_max = None |
220 | 227 |
221 if units[cols[idx]] == yaxisunits1: | 228 if col in tarrifs: |
229 annotation = '%s: $%.2f' % (names[col], (ydata[1:] * xdeltas).sum() / 1000.0 * tarrifs[col]) | |
230 print(annotation) | |
231 else: | |
232 annotation = None | |
233 | |
234 if units[col] == yaxisunits1: | |
222 ax = ax1lines | 235 ax = ax1lines |
223 else: | 236 else: |
224 ax = ax2lines | 237 ax = ax2lines |
225 ax.append([xdata, ydata, names[cols[idx]], colourlist[colouridx], scale_min, scale_max]) | 238 ax.append([xdata, ydata, names[col], colourlist[colouridx], scale_min, scale_max, annotation]) |
226 colouridx += 1 | 239 colouridx += 1 |
227 | 240 |
228 if fname == None: | 241 if fname == None: |
229 import matplotlib.pylab | 242 import matplotlib.pylab |
230 fig = matplotlib.pylab.figure() | 243 fig = matplotlib.pylab.figure() |
233 fig = matplotlib.figure.Figure(figsize = (8, 6), dpi = 75) | 246 fig = matplotlib.figure.Figure(figsize = (8, 6), dpi = 75) |
234 | 247 |
235 ax1 = fig.add_subplot(111) | 248 ax1 = fig.add_subplot(111) |
236 ax1.set_ylabel(yaxisunits1) | 249 ax1.set_ylabel(yaxisunits1) |
237 | 250 |
251 annotations = [] | |
238 for line in ax1lines: | 252 for line in ax1lines: |
239 ax1.plot(line[0], line[1], label = line[2], color = line[3]) | 253 ax1.plot(line[0], line[1], label = line[2], color = line[3]) |
240 if line[4] != None and line[5] != None: | 254 if line[4] != None and line[5] != None: |
241 ax1.set_ylim((line[4], line[5])) | 255 ax1.set_ylim((line[4], line[5])) |
242 | 256 if line[6] != None: |
257 annotations.append(line[6]) | |
243 ax1.legend(loc = 'upper left') | 258 ax1.legend(loc = 'upper left') |
244 | 259 |
245 if yaxisunits2 != None: | 260 if yaxisunits2 != None: |
246 ax2 = ax1.twinx() | 261 ax2 = ax1.twinx() |
247 ax2.set_ylabel(yaxisunits2) | 262 ax2.set_ylabel(yaxisunits2) |
248 | 263 |
249 for line in ax2lines: | 264 for line in ax2lines: |
250 ax2.plot(line[0], line[1], label = line[2], color = line[3]) | 265 ax2.plot(line[0], line[1], label = line[2], color = line[3]) |
251 if line[4] != None and line[5] != None: | 266 if line[4] != None and line[5] != None: |
252 ax2.set_ylim(bottom = line[4], top = line[5]) | 267 ax2.set_ylim(bottom = line[4], top = line[5]) |
268 if line[6] != None: | |
269 annotations.append(line[6]) | |
270 | |
253 ax2.legend(loc = 'upper right') | 271 ax2.legend(loc = 'upper right') |
254 | 272 ax1.text(0.02, 0.8, reduce(lambda a, b: a + '\n' + b, annotations), |
273 transform = ax1.transAxes, bbox=dict(facecolor='red', alpha=0.5)) | |
255 ndays = int(max(1, round(((end - start).total_seconds()) / 86400))) | 274 ndays = int(max(1, round(((end - start).total_seconds()) / 86400))) |
256 for ax in fig.get_axes(): | 275 for ax in fig.get_axes(): |
257 ax.set_title('%s to %s' % (start.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d'))) | 276 ax.set_title('%s to %s' % (start.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d'))) |
258 ax.set_xlim([start, end]) | 277 ax.set_xlim([start, end]) |
259 ax.format_xdata = lambda d: matplotlib.dates.num2date(d).strftime('%d %b %H:%M') | 278 ax.format_xdata = lambda d: matplotlib.dates.num2date(d).strftime('%d %b %H:%M') |