changeset 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
files agl.py
diffstat 1 files changed, 29 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/agl.py	Mon Sep 11 16:49:00 2017 +0930
+++ b/agl.py	Mon Sep 11 17:57:38 2017 +0930
@@ -140,6 +140,11 @@
     'battery_charge' : (0, 100),
 }
 
+tarrifs = {
+    'power_exported' : 0.163,
+    'power_imported' : 0.380,
+}
+
 names = {
     'battery_charge' : 'Battery Charge',
     'battery_power' : 'Battery Power',
@@ -206,23 +211,31 @@
         return
     # Convert TZ naive UTC to TZ aware UTC then adjust to local time
     xdata = map(lambda f: f.replace(tzinfo = utc).astimezone(lt), ary[:,0])
+    xhours = matplotlib.dates.date2num(xdata) * 24
+    xdeltas = xhours[1:] - xhours[0:-1]
     for idx in range(len(cols)):
+        col = cols[idx]
         ydata = ary[:,idx + 1]
-        if cols[idx] in convs:
-            ydata = convs[cols[idx]](ydata)
-
-        if cols[idx] in scale_limits:
-            scale_min = scale_limits[cols[idx]][0]
-            scale_max = scale_limits[cols[idx]][1]
+        if col in convs:
+            ydata = convs[col](ydata)
+        if col in scale_limits:
+            scale_min = scale_limits[col][0]
+            scale_max = scale_limits[col][1]
         else:
             scale_min = None
             scale_max = None
 
-        if units[cols[idx]] == yaxisunits1:
+        if col in tarrifs:
+            annotation = '%s: $%.2f' % (names[col], (ydata[1:] * xdeltas).sum() / 1000.0 * tarrifs[col])
+            print(annotation)
+        else:
+            annotation = None
+
+        if units[col] == yaxisunits1:
             ax = ax1lines
         else:
             ax = ax2lines
-        ax.append([xdata, ydata, names[cols[idx]], colourlist[colouridx], scale_min, scale_max])
+        ax.append([xdata, ydata, names[col], colourlist[colouridx], scale_min, scale_max, annotation])
         colouridx += 1
 
     if fname == None:
@@ -235,11 +248,13 @@
     ax1 = fig.add_subplot(111)
     ax1.set_ylabel(yaxisunits1)
 
+    annotations = []
     for line in ax1lines:
         ax1.plot(line[0], line[1], label = line[2], color = line[3])
         if line[4] != None and line[5] != None:
             ax1.set_ylim((line[4], line[5]))
-
+        if line[6] != None:
+            annotations.append(line[6])
     ax1.legend(loc = 'upper left')
 
     if yaxisunits2 != None:
@@ -250,8 +265,12 @@
             ax2.plot(line[0], line[1], label = line[2], color = line[3])
             if line[4] != None and line[5] != None:
                 ax2.set_ylim(bottom = line[4], top = line[5])
+            if line[6] != None:
+                annotations.append(line[6])
+
         ax2.legend(loc = 'upper right')
-
+    ax1.text(0.02, 0.8, reduce(lambda a, b: a + '\n' + b, annotations),
+                 transform = ax1.transAxes, bbox=dict(facecolor='red', alpha=0.5))
     ndays = int(max(1, round(((end - start).total_seconds()) / 86400)))
     for ax in fig.get_axes():
         ax.set_title('%s to %s' % (start.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d')))