Mercurial > ~darius > hgwebdir.cgi > agl
comparison agl.py @ 4:deb3adc4e086
Add graph saving option.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Mon, 11 Sep 2017 12:32:05 +0930 |
parents | 525a66486282 |
children | b42baa411817 |
comparison
equal
deleted
inserted
replaced
3:525a66486282 | 4:deb3adc4e086 |
---|---|
36 | 36 |
37 def main(): | 37 def main(): |
38 parser = argparse.ArgumentParser() | 38 parser = argparse.ArgumentParser() |
39 parser.add_argument('-u', '--update', help = 'Update data', action="store_true") | 39 parser.add_argument('-u', '--update', help = 'Update data', action="store_true") |
40 parser.add_argument('-g', '--graph', help = 'Produce graph', action="store_true") | 40 parser.add_argument('-g', '--graph', help = 'Produce graph', action="store_true") |
41 parser.add_argument('-f', '--filename', help = 'Filename to save graph as', type = str) | |
41 parser.add_argument('-s', '--start', help = 'Start date for graph (YYYY-MM-DD)', type = valid_date) | 42 parser.add_argument('-s', '--start', help = 'Start date for graph (YYYY-MM-DD)', type = valid_date) |
42 parser.add_argument('-e', '--end', help = 'End date for graph (YYYY-MM-DD)', type = valid_date) | 43 parser.add_argument('-e', '--end', help = 'End date for graph (YYYY-MM-DD)', type = valid_date) |
43 | 44 |
44 args = parser.parse_args() | 45 args = parser.parse_args() |
45 | 46 |
88 updatedb(cur, data) | 89 updatedb(cur, data) |
89 dbh.commit() | 90 dbh.commit() |
90 date += datetime.timedelta(days = 1) | 91 date += datetime.timedelta(days = 1) |
91 | 92 |
92 if args.graph: | 93 if args.graph: |
93 graph(cur, ['battery_charge', 'power_imported', 'power_exported', 'power_consumed', 'power_generated'], start, end) | 94 graph(args.filename, cur, ['battery_charge', 'power_imported', 'power_exported', 'power_consumed', 'power_generated'], start, end) |
94 | 95 |
95 def mkdb(cur): | 96 def mkdb(cur): |
96 cur.execute(''' | 97 cur.execute(''' |
97 CREATE TABLE IF NOT EXISTS agl ( | 98 CREATE TABLE IF NOT EXISTS agl ( |
98 t_stamp TIMESTAMP PRIMARY KEY, | 99 t_stamp TIMESTAMP PRIMARY KEY, |
147 'site_cons_battery' : 'Site Consumption Batter', | 148 'site_cons_battery' : 'Site Consumption Batter', |
148 'site_cons_grid' : 'Site Consumption Grid', | 149 'site_cons_grid' : 'Site Consumption Grid', |
149 'site_cons_pv' : 'Site Consumption PV' | 150 'site_cons_pv' : 'Site Consumption PV' |
150 } | 151 } |
151 | 152 |
152 def graph(cur, cols, start, end): | 153 def graph(fname, cur, cols, start, end): |
153 import numpy | 154 import numpy |
154 import matplotlib | 155 import matplotlib |
155 import matplotlib.dates | 156 import matplotlib.dates |
156 import matplotlib.pylab | |
157 | 157 |
158 colourlist = ['b','g','r','c','m','y','k'] | 158 colourlist = ['b','g','r','c','m','y','k'] |
159 | 159 |
160 # Work out what axes we are using | 160 # Work out what axes we are using |
161 yaxisunits1 = None | 161 yaxisunits1 = None |
203 ax1lines.append([xdata, ary[:,idx + 1], names[cols[idx]], colourlist[colouridx]]) | 203 ax1lines.append([xdata, ary[:,idx + 1], names[cols[idx]], colourlist[colouridx]]) |
204 else: | 204 else: |
205 ax2lines.append([xdata, ary[:,idx + 1], names[cols[idx]], colourlist[colouridx]]) | 205 ax2lines.append([xdata, ary[:,idx + 1], names[cols[idx]], colourlist[colouridx]]) |
206 colouridx += 1 | 206 colouridx += 1 |
207 | 207 |
208 fig = matplotlib.pylab.figure() | 208 if fname == None: |
209 import matplotlib.pylab | |
210 fig = matplotlib.pylab.figure() | |
211 else: | |
212 import matplotlib.backends.backend_agg | |
213 fig = matplotlib.figure.Figure(figsize = (8, 6), dpi = 75) | |
214 | |
209 ax1 = fig.add_subplot(111) | 215 ax1 = fig.add_subplot(111) |
210 ax1.set_ylabel(yaxisunits1) | 216 ax1.set_ylabel(yaxisunits1) |
211 | 217 |
212 for line in ax1lines: | 218 for line in ax1lines: |
213 ax1.plot(line[0], line[1], label = line[2]) | 219 ax1.plot(line[0], line[1], label = line[2]) |
234 label.set_ha('center') | 240 label.set_ha('center') |
235 label.set_rotation(90) | 241 label.set_rotation(90) |
236 | 242 |
237 # Fudge margins to give more graph and less space | 243 # Fudge margins to give more graph and less space |
238 fig.subplots_adjust(left = 0.10, right = 0.88, top = 0.95, bottom = 0.15) | 244 fig.subplots_adjust(left = 0.10, right = 0.88, top = 0.95, bottom = 0.15) |
239 matplotlib.pyplot.show() | 245 if fname == None: |
246 matplotlib.pyplot.show() | |
247 else: | |
248 canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(fig) # Sets canvas in fig too | |
249 fig.savefig(fname) | |
240 | 250 |
241 def updatedb(cur, data): | 251 def updatedb(cur, data): |
242 mkdb(cur) | 252 mkdb(cur) |
243 for d in data['reads']['data']: | 253 for d in data['reads']['data']: |
244 ts = datetime.datetime.strptime(d['t_stamp'], '%Y-%m-%dT%H:%M:%SZ') | 254 ts = datetime.datetime.strptime(d['t_stamp'], '%Y-%m-%dT%H:%M:%SZ') |