comparison agl.py @ 1:6e3ca5bfda04

Improve plotting/timezone stuff. Still not very generic - MPL wants a name like 'Australia/Adelaide' but there sees to be no way to get that from dateutil or pytz.
author Daniel O'Connor <darius@dons.net.au>
date Sun, 10 Sep 2017 11:25:39 +0930
parents 8d6ba11c1b76
children 2b7fb26f9114
comparison
equal deleted inserted replaced
0:8d6ba11c1b76 1:6e3ca5bfda04
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 import ConfigParser 3 import ConfigParser
4 import datetime 4 import datetime
5 import dateutil
5 import exceptions 6 import exceptions
6 import json 7 import json
7 import os 8 import os
8 import pytz
9 import requests 9 import requests
10 import sqlite3 10 import sqlite3
11 import sys 11 import sys
12 12
13 loginurl = 'https://command.aglsolar.com.au/api/v2/Account/LoginUser' 13 loginurl = 'https://command.aglsolar.com.au/api/v2/Account/LoginUser'
99 import numpy 99 import numpy
100 import matplotlib 100 import matplotlib
101 import matplotlib.dates 101 import matplotlib.dates
102 import matplotlib.pylab 102 import matplotlib.pylab
103 103
104 #matplotlib.rcParams['timezone'] = pytz.timezone('Australia/Adelaide')
105
106 colourlist = ['b','g','r','c','m','y','k'] 104 colourlist = ['b','g','r','c','m','y','k']
105
106 # Work out what axes we are using
107 yaxisunits1 = None 107 yaxisunits1 = None
108 yaxisunits2 = None 108 yaxisunits2 = None
109 ax1lines = [] 109 ax1lines = []
110 ax2lines = [] 110 ax2lines = []
111 colouridx = 0 111 colouridx = 0
118 yaxisunits2 = unit 118 yaxisunits2 = unit
119 else: 119 else:
120 if unit != yaxisunits1 and unit != yaxisunits2: 120 if unit != yaxisunits1 and unit != yaxisunits2:
121 raise exceptions.Exception('Asked to graph >2 different units') 121 raise exceptions.Exception('Asked to graph >2 different units')
122 122
123 cur.execute('SELECT t_stamp, ' + reduce(lambda a, b: a + ', ' + b, cols) + ' FROM agl WHERE t_stamp > ? AND t_stamp < ? ORDER BY t_stamp', 123 ltname = 'Australia/Adelaide'
124 lt = dateutil.tz.gettz(ltname)
125 utc = dateutil.tz.gettz('UTC')
126 matplotlib.rcParams['timezone'] = ltname
127
128 if start.tzinfo == None:
129 start = start.replace(tzinfo = lt)
130 if end.tzinfo == None:
131 end = end.replace(tzinfo = lt)
132
133 start = start.astimezone(utc)
134 end = end.astimezone(utc)
135
136 # Actually get the data
137 colstr = reduce(lambda a, b: a + ', ' + b, cols)
138 # Data is stored as naive datetime's which are in UTC so convert the requested time here
139 cur.execute('SELECT t_stamp, ' + colstr + ' FROM agl WHERE t_stamp > ? AND t_stamp < ? ORDER BY t_stamp',
124 (start, end)) 140 (start, end))
125 ary = numpy.array(cur.fetchall()) 141 ary = numpy.array(cur.fetchall())
142 # Convert naive UTC to proper UTC then adjust to local time
143 xdata = map(lambda f: f.replace(tzinfo = utc).astimezone(lt), ary[:,0])
126 for idx in range(len(cols)): 144 for idx in range(len(cols)):
127 if units[cols[idx]] == yaxisunits1: 145 if units[cols[idx]] == yaxisunits1:
128 ax1lines.append([ary[:,0], ary[:,idx + 1], cols[idx], colourlist[colouridx]]) 146 ax1lines.append([xdata, ary[:,idx + 1], cols[idx], colourlist[colouridx]])
129 else: 147 else:
130 ax2lines.append([ary[:,0], ary[:,idx + 1], cols[idx], colourlist[colouridx]]) 148 ax2lines.append([xdata, ary[:,idx + 1], cols[idx], colourlist[colouridx]])
131 colouridx += 1 149 colouridx += 1
132 150
133 fig = matplotlib.pylab.figure() 151 fig = matplotlib.pylab.figure()
134 ax1 = fig.add_subplot(111) 152 ax1 = fig.add_subplot(111)
135 ax1.set_ylabel(yaxisunits1) 153 ax1.set_ylabel(yaxisunits1)
147 ax2.plot(line[0], line[1], label = line[2], color = line[3]) 165 ax2.plot(line[0], line[1], label = line[2], color = line[3])
148 ax2.legend(loc = 'upper right') 166 ax2.legend(loc = 'upper right')
149 167
150 # Rotate X axis labels 168 # Rotate X axis labels
151 for ax in fig.get_axes(): 169 for ax in fig.get_axes():
152 ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%H:%M')) 170 ax.set_xlim([start, end])
171 ax.xaxis.grid(True)
172 ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%d %b\n%H:%M'))
153 ax.xaxis.set_major_locator(matplotlib.dates.HourLocator(interval = 2)) 173 ax.xaxis.set_major_locator(matplotlib.dates.HourLocator(interval = 2))
154 ax.xaxis.set_minor_locator(matplotlib.dates.MinuteLocator(interval = 5)) 174 ax.xaxis.set_minor_locator(matplotlib.dates.MinuteLocator(interval = 5))
155 for label in ax.get_xticklabels(): 175 for label in ax.get_xticklabels():
156 label.set_ha('right') 176 label.set_ha('center')
157 label.set_rotation(30) 177 label.set_rotation(90)
158 178
159 # Fudge margins to give more graph and less space 179 # Fudge margins to give more graph and less space
160 fig.subplots_adjust(left = 0.10, right = 0.88, top = 0.95, bottom = 0.15) 180 fig.subplots_adjust(left = 0.10, right = 0.88, top = 0.95, bottom = 0.15)
161 matplotlib.pyplot.show() 181 matplotlib.pyplot.show()
162 182