diff 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
line wrap: on
line diff
--- a/agl.py	Fri Sep 08 17:51:41 2017 +0930
+++ b/agl.py	Sun Sep 10 11:25:39 2017 +0930
@@ -2,10 +2,10 @@
 
 import ConfigParser
 import datetime
+import dateutil
 import exceptions
 import json
 import os
-import pytz
 import requests
 import sqlite3
 import sys
@@ -101,9 +101,9 @@
     import matplotlib.dates
     import matplotlib.pylab
 
-    #matplotlib.rcParams['timezone'] = pytz.timezone('Australia/Adelaide')
+    colourlist = ['b','g','r','c','m','y','k']
 
-    colourlist = ['b','g','r','c','m','y','k']
+    # Work out what axes we are using
     yaxisunits1 = None
     yaxisunits2 = None
     ax1lines = []
@@ -120,14 +120,32 @@
             if unit != yaxisunits1 and unit != yaxisunits2:
                 raise exceptions.Exception('Asked to graph >2 different units')
 
-    cur.execute('SELECT t_stamp, ' + reduce(lambda a, b: a + ', ' + b, cols) + ' FROM agl WHERE t_stamp > ? AND t_stamp < ? ORDER BY t_stamp',
+    ltname = 'Australia/Adelaide'
+    lt = dateutil.tz.gettz(ltname)
+    utc = dateutil.tz.gettz('UTC')
+    matplotlib.rcParams['timezone'] = ltname
+
+    if start.tzinfo == None:
+        start = start.replace(tzinfo = lt)
+    if end.tzinfo == None:
+        end = end.replace(tzinfo = lt)
+
+    start = start.astimezone(utc)
+    end = end.astimezone(utc)
+
+    # Actually get the data
+    colstr = reduce(lambda a, b: a + ', ' + b, cols)
+    # Data is stored as naive datetime's which are in UTC so convert the requested time here
+    cur.execute('SELECT t_stamp, ' + colstr + ' FROM agl WHERE t_stamp > ? AND t_stamp < ? ORDER BY t_stamp',
                     (start, end))
     ary = numpy.array(cur.fetchall())
+    # Convert naive UTC to proper UTC then adjust to local time
+    xdata = map(lambda f: f.replace(tzinfo = utc).astimezone(lt), ary[:,0])
     for idx in range(len(cols)):
         if units[cols[idx]] == yaxisunits1:
-            ax1lines.append([ary[:,0], ary[:,idx + 1], cols[idx], colourlist[colouridx]])
+            ax1lines.append([xdata, ary[:,idx + 1], cols[idx], colourlist[colouridx]])
         else:
-            ax2lines.append([ary[:,0], ary[:,idx + 1], cols[idx], colourlist[colouridx]])
+            ax2lines.append([xdata, ary[:,idx + 1], cols[idx], colourlist[colouridx]])
         colouridx += 1
 
     fig = matplotlib.pylab.figure()
@@ -149,12 +167,14 @@
 
     # Rotate X axis labels
     for ax in fig.get_axes():
-        ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%H:%M'))
+        ax.set_xlim([start, end])
+        ax.xaxis.grid(True)
+        ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%d %b\n%H:%M'))
         ax.xaxis.set_major_locator(matplotlib.dates.HourLocator(interval = 2))
         ax.xaxis.set_minor_locator(matplotlib.dates.MinuteLocator(interval = 5))
         for label in ax.get_xticklabels():
-            label.set_ha('right')
-            label.set_rotation(30)
+            label.set_ha('center')
+            label.set_rotation(90)
 
     # Fudge margins to give more graph and less space
     fig.subplots_adjust(left = 0.10, right = 0.88, top = 0.95, bottom = 0.15)