Mercurial > ~darius > hgwebdir.cgi > vanlogger
annotate graph.py @ 20:d4356465dce1
Set limits for battery voltages to make graph comparison easier
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Tue, 27 Feb 2018 16:12:36 +1030 |
parents | 4d97dfb07169 |
children |
rev | line source |
---|---|
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
2 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
3 import argparse |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
4 import datetime |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
5 import dateutil |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
6 import exceptions |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
7 import matplotlib |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
8 import matplotlib.dates |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
9 import numpy |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
10 import os |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
11 import requests |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
12 import sqlite3 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
13 import tzlocal |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
14 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
15 class Column(object): |
8
30e7adf283ca
Add depth of discharge annotation when plotting state_of_charge
Daniel O'Connor <darius@dons.net.au>
parents:
4
diff
changeset
|
16 def __init__(self, rowname, title, table, units, limits = (None, None), conv = None, annofn = None): |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
17 self.rowname = rowname |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
18 self.title = title |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
19 self.table = table |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
20 self.units = units |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
21 self.limits = limits |
8
30e7adf283ca
Add depth of discharge annotation when plotting state_of_charge
Daniel O'Connor <darius@dons.net.au>
parents:
4
diff
changeset
|
22 self.conv = conv |
30e7adf283ca
Add depth of discharge annotation when plotting state_of_charge
Daniel O'Connor <darius@dons.net.au>
parents:
4
diff
changeset
|
23 self.annofn = annofn |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
24 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
25 columns = [ |
20
d4356465dce1
Set limits for battery voltages to make graph comparison easier
Daniel O'Connor <darius@dons.net.au>
parents:
19
diff
changeset
|
26 Column('main_voltage', 'Battery Voltage', 'eprolog', 'Vdc', (10, 30)), |
d4356465dce1
Set limits for battery voltages to make graph comparison easier
Daniel O'Connor <darius@dons.net.au>
parents:
19
diff
changeset
|
27 Column('aux_voltage', 'Aux Voltage', 'eprolog', 'Vdc', (10, 30)), |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
28 Column('battery_curr', 'Battery Current', 'eprolog', 'A'), |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
29 Column('amp_hours', 'Battery Amp Hours', 'eprolog', 'Ah'), |
8
30e7adf283ca
Add depth of discharge annotation when plotting state_of_charge
Daniel O'Connor <darius@dons.net.au>
parents:
4
diff
changeset
|
30 Column('state_of_charge', 'State of Charge', 'eprolog', '%', (0, 100), annofn = lambda xdata, ydata: 'DoD: %.1f' % (100 - ydata.min())), |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
31 Column('time_remaining', 'Time Remaining', 'eprolog', 'min'), |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
32 Column('battery_temp', 'Battery Temperature', 'eprolog', 'C'), |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
33 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
34 Column('ac_act_power', 'Active Power', 'giantlog', 'W'), |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
35 Column('ac_app_power', 'Apparent Power', 'giantlog', 'W'), |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
36 Column('ac_frequency', 'AC Frequency', 'giantlog', 'Hz'), |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
37 Column('ac_volts', 'AC Voltage', 'giantlog', 'Vac'), |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
38 Column('batt_chr_curr', 'Discharge Current', 'giantlog', 'A'), |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
39 Column('batt_dis_curr', 'Charge Current', 'giantlog', 'A'), |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
40 Column('battery_cap', 'Battery Capacity', 'giantlog', '%', (0, 100)), |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
41 Column('battery_volts', 'Battery Voltage', 'giantlog', 'Vdc'), |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
42 Column('grid_frequency', 'Grid Frequency', 'giantlog', 'Hz'), |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
43 Column('grid_volts', 'Grid Voltage', 'giantlog', 'Vac'), |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
44 Column('hs_temperature', 'HS Temperature', 'giantlog', 'C'), |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
45 Column('load_pct', 'Load', 'giantlog', '%', (0, 100)), |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
46 ] |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
47 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
48 def valid_date(s): |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
49 try: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
50 return datetime.datetime.strptime(s, "%Y-%m-%d") |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
51 except ValueError: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
52 raise argparse.ArgumentTypeError("Not a valid date: '{0}'.".format(s)) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
53 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
54 def main(): |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
55 parser = argparse.ArgumentParser() |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
56 parser.add_argument('-f', '--filename', help = 'Path to database', type = str, required = True) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
57 parser.add_argument('-g', '--graphfn', help = 'File to write graph to', type = str) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
58 parser.add_argument('-d', '--days', help = 'Days ago to graph', type = int) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
59 parser.add_argument('-s', '--start', help = 'Start date for graph (YYYY-MM-DD)', type = valid_date) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
60 parser.add_argument('-e', '--end', help = 'End date for graph (YYYY-MM-DD)', type = valid_date) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
61 parser.add_argument('-c', '--column', help = 'Column to plot (can be specified multiple times)', type = str, action = 'append') |
13
3f22fa1f32d5
Twiddle with start/end passing, add option for overriding local time zone.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
62 parser.add_argument('-z', '--timezone', help = 'Timezone to operate in in (Oslon format) default: localtime)', type = str) |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
63 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
64 args = parser.parse_args() |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
65 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
66 if args.days is not None and args.days < 0: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
67 parser.error('days must be non-negative') |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
68 |
17
57dece5674e3
Be more clever about what 'today' is
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
69 # If it's before 6am then plot yesterday |
57dece5674e3
Be more clever about what 'today' is
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
70 today = datetime.date.today() |
57dece5674e3
Be more clever about what 'today' is
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
71 if datetime.datetime.now().hour < 6: |
57dece5674e3
Be more clever about what 'today' is
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
72 today -= datetime.timedelta(days = 1) |
57dece5674e3
Be more clever about what 'today' is
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
73 |
11
e1bec6dfd524
Improve start/end/days handling
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
74 selector = [args.start is not None, args.end is not None, args.days is not None] |
14
aa18210c2703
Improve start/end/days handling
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
75 if selector == [True, True, False]: # Start and end |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
76 pass |
14
aa18210c2703
Improve start/end/days handling
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
77 elif selector == [True, False, True] or selector == [True, False, False]: # Start and days or start |
11
e1bec6dfd524
Improve start/end/days handling
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
78 if args.days == None: |
e1bec6dfd524
Improve start/end/days handling
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
79 args.days = 1 |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
80 args.end = args.start + datetime.timedelta(days = args.days) |
14
aa18210c2703
Improve start/end/days handling
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
81 elif selector == [False, True, True] or selector == [False, True, False]: # End and days or end |
11
e1bec6dfd524
Improve start/end/days handling
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
82 if args.days == None: |
e1bec6dfd524
Improve start/end/days handling
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
83 args.days = 1 |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
84 args.start = args.end - datetime.timedelta(days = args.days) |
14
aa18210c2703
Improve start/end/days handling
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
85 elif selector == [False, False, True]: # Days |
17
57dece5674e3
Be more clever about what 'today' is
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
86 args.end = today + datetime.timedelta(days = 1) |
14
aa18210c2703
Improve start/end/days handling
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
87 args.end = datetime.datetime(args.end.year, args.end.month, args.end.day) |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
88 args.start = args.end - datetime.timedelta(days = args.days) |
14
aa18210c2703
Improve start/end/days handling
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
89 elif selector == [False, False, False]: # Nothing |
17
57dece5674e3
Be more clever about what 'today' is
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
90 args.start = today |
14
aa18210c2703
Improve start/end/days handling
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
91 args.start = datetime.datetime(args.start.year, args.start.month, args.start.day) |
aa18210c2703
Improve start/end/days handling
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
92 args.end = args.start + datetime.timedelta(days = 1) |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
93 else: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
94 parser.error('can\'t specify days, start and end simultaneously') |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
95 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
96 if args.start >= args.end: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
97 parser.error('Start must be before end') |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
98 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
99 cols = args.column |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
100 if cols == None: |
10
3f5b617b4715
Change default columns to be more useful
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
101 cols = ['state_of_charge', 'load_pct', 'main_voltage', 'aux_voltage'] |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
102 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
103 dbh = sqlite3.connect(args.filename, detect_types = sqlite3.PARSE_DECLTYPES) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
104 cur = dbh.cursor() |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
105 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
106 # Get local timezone name and convert start/end to it |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
107 # Why is this so hard... |
13
3f22fa1f32d5
Twiddle with start/end passing, add option for overriding local time zone.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
108 if args.timezone is None: |
3f22fa1f32d5
Twiddle with start/end passing, add option for overriding local time zone.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
109 args.timezone = tzlocal.get_localzone().zone |
3f22fa1f32d5
Twiddle with start/end passing, add option for overriding local time zone.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
110 |
3f22fa1f32d5
Twiddle with start/end passing, add option for overriding local time zone.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
111 lt = dateutil.tz.gettz(args.timezone) |
3f22fa1f32d5
Twiddle with start/end passing, add option for overriding local time zone.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
112 if lt == None: |
3f22fa1f32d5
Twiddle with start/end passing, add option for overriding local time zone.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
113 parser.error('Unknown timezone') |
3f22fa1f32d5
Twiddle with start/end passing, add option for overriding local time zone.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
114 |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
115 utc = dateutil.tz.gettz('UTC') |
13
3f22fa1f32d5
Twiddle with start/end passing, add option for overriding local time zone.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
116 matplotlib.rcParams['timezone'] = args.timezone |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
117 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
118 if args.start.tzinfo == None: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
119 args.start = args.start.replace(tzinfo = lt) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
120 if args.end.tzinfo == None: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
121 args.end = args.end.replace(tzinfo = lt) |
13
3f22fa1f32d5
Twiddle with start/end passing, add option for overriding local time zone.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
122 graph(args.graphfn, cur, cols, args.start, args.end, lt, utc) |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
123 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
124 def graph(fname, cur, _cols, start, end, lt, utc): |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
125 import numpy |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
126 import matplotlib |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
127 import matplotlib.dates |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
128 |
13
3f22fa1f32d5
Twiddle with start/end passing, add option for overriding local time zone.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
129 startepoch = int(start.strftime('%s')) |
3f22fa1f32d5
Twiddle with start/end passing, add option for overriding local time zone.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
130 endepoch = int(end.strftime('%s')) |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
131 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
132 colourlist = ['b','g','r','c','m','y','k'] |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
133 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
134 cols = [] |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
135 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
136 yaxisunits1 = None |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
137 yaxisunits2 = None |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
138 ax1lines = [] |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
139 ax2lines = [] |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
140 colouridx = 0 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
141 for col in _cols: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
142 # Check the column exists |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
143 for c in columns: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
144 if col == c.rowname: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
145 cols.append(c) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
146 break |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
147 else: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
148 raise exceptions.Exception('Unknown column name ' + c) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
149 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
150 # Work out what axes we are using |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
151 if yaxisunits1 == None: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
152 yaxisunits1 = c.units |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
153 if yaxisunits2 == None: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
154 if c.units != yaxisunits1: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
155 yaxisunits2 = c.units |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
156 else: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
157 if c.units != yaxisunits1 and c.units != yaxisunits2: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
158 raise exceptions.Exception('Asked to graph >2 different units') |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
159 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
160 for c in cols: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
161 # Get the data |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
162 cur.execute('SELECT tstamp, ' + c.rowname + ' FROM ' + c.table + ' WHERE tstamp > ? AND tstamp < ? ORDER BY tstamp', |
13
3f22fa1f32d5
Twiddle with start/end passing, add option for overriding local time zone.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
163 (startepoch, endepoch)) |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
164 ary = numpy.array(cur.fetchall()) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
165 if ary.shape[0] == 0: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
166 print('No data for ' + c.rowname) |
15 | 167 continue |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
168 |
15 | 169 # Convert epoch stamp to datetime with UTC timezone |
170 c.xdata = map(lambda f: datetime.datetime.utcfromtimestamp(f).replace(tzinfo = utc), ary[:,0]) | |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
171 c.ydata = ary[:,1] |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
172 if c.conv != None: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
173 c.ydata = map(c.conv, c.ydata) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
174 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
175 scale_min, scale_max = c.limits |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
176 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
177 # DoD? |
8
30e7adf283ca
Add depth of discharge annotation when plotting state_of_charge
Daniel O'Connor <darius@dons.net.au>
parents:
4
diff
changeset
|
178 if c.annofn != None: |
30e7adf283ca
Add depth of discharge annotation when plotting state_of_charge
Daniel O'Connor <darius@dons.net.au>
parents:
4
diff
changeset
|
179 c.annotation = c.annofn(c.xdata, c.ydata) |
30e7adf283ca
Add depth of discharge annotation when plotting state_of_charge
Daniel O'Connor <darius@dons.net.au>
parents:
4
diff
changeset
|
180 else: |
30e7adf283ca
Add depth of discharge annotation when plotting state_of_charge
Daniel O'Connor <darius@dons.net.au>
parents:
4
diff
changeset
|
181 c.annotation = None |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
182 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
183 # Work out which axis to plot on |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
184 if c.units == yaxisunits1: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
185 ax = ax1lines |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
186 else: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
187 ax = ax2lines |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
188 c.colour = colourlist[colouridx] |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
189 colouridx += 1 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
190 ax.append(c) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
191 |
15 | 192 if len(ax1lines) == 0 and len(ax2lines) == 0: |
193 print('Nothing to plot') | |
194 return | |
195 | |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
196 # Load the right backend for display or save |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
197 if fname == None: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
198 import matplotlib.pylab |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
199 fig = matplotlib.pylab.figure() |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
200 else: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
201 import matplotlib.backends.backend_agg |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
202 fig = matplotlib.figure.Figure(figsize = (12, 6), dpi = 75) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
203 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
204 # Do the plot |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
205 ax1 = fig.add_subplot(111) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
206 ax1.set_ylabel(yaxisunits1) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
207 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
208 annotations = [] |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
209 for line in ax1lines: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
210 ax1.plot(line.xdata, line.ydata, label = line.title, color = line.colour) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
211 if line.limits[0] != None or line.limits[1] != None: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
212 ax1.set_ylim(line.limits[0], line.limits[1]) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
213 if line.annotation != None: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
214 annotations.append(line.annotation) |
15 | 215 ax1.legend(loc = 'center left') |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
216 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
217 if len(ax2lines) > 0: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
218 ax2 = ax1.twinx() |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
219 ax2.set_ylabel(yaxisunits2) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
220 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
221 for line in ax2lines: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
222 ax2.plot(line.xdata, line.ydata, label = line.title, color = line.colour) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
223 if line.limits[0] != None or line.limits[1] != None: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
224 ax2.set_ylim(line.limits[0], line.limits[1]) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
225 if line.annotation != None: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
226 annotations.append(line.annotation) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
227 |
15 | 228 ax2.legend(loc = 'center right') |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
229 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
230 if len(annotations) > 0: |
15 | 231 ax1.text(0.02, 0.3, reduce(lambda a, b: a + '\n' + b, annotations), |
9
899bc9c22787
Tweak annotation to be out of the way
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
232 transform = ax1.transAxes, bbox = dict(facecolor = 'blue', alpha = 0.5), |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
233 ha = 'left', va = 'top') |
13
3f22fa1f32d5
Twiddle with start/end passing, add option for overriding local time zone.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
234 ndays = int(max(1, round(((end - start).total_seconds()) / 86400))) |
15 | 235 once = False |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
236 for ax in fig.get_axes(): |
15 | 237 if not once: |
238 once = True | |
239 if ndays > 1: | |
240 ax.set_title('%s to %s' % (start.astimezone(lt).strftime('%Y-%m-%d'), end.astimezone(lt).strftime('%Y-%m-%d'))) | |
241 else: | |
242 ax.set_title('%s' % (start.astimezone(lt).strftime('%Y-%m-%d'))) | |
243 ax.set_xlim([start.astimezone(utc), end.astimezone(utc)]) | |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
244 ax.xaxis.grid(True) |
15 | 245 ax.xaxis.axis_date(lt) |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
246 ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%d %b\n%H:%M')) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
247 ax.xaxis.set_major_locator(matplotlib.dates.HourLocator(interval = 2 * ndays)) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
248 ax.xaxis.set_minor_locator(matplotlib.dates.MinuteLocator(interval = 5 * ndays)) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
249 for label in ax.get_xticklabels(): |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
250 label.set_ha('center') |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
251 label.set_rotation(90) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
252 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
253 # Fudge margins to give more graph and less space |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
254 fig.subplots_adjust(left = 0.10, right = 0.88, top = 0.95, bottom = 0.15) |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
255 if fname == None: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
256 matplotlib.pyplot.show() |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
257 else: |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
258 canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(fig) # Sets canvas in fig too |
19
4d97dfb07169
Use the right date for graph names
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
259 fig.savefig(start.astimezone(lt).strftime(fname)) |
4
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
260 |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
261 if __name__ == '__main__': |
a7e9775b33f6
Add graph script based on AGL code (but better)
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
262 main() |