Add table to report.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Mon, 28 Jan 2019 22:32:04 +0000 (23:32 +0100)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Mon, 28 Jan 2019 22:32:04 +0000 (23:32 +0100)
web/seepark_web.py

index 131489bd1dbc5a4dfd4b9372de8ccc41307bf783..fb522baa80ebe2d10bf5b545b466381309e0961f 100644 (file)
@@ -30,6 +30,15 @@ MONTH_DE = [
     'November',
     'Dezember']
 
     'November',
     'Dezember']
 
+DAY_OF_WEEK_DE = [
+    'Montag',
+    'Dienstag',
+    'Mittwoch',
+    'Donnerstag',
+    'Freitag',
+    'Samstag',
+    'Sonntag']
+
 
 # https://stackoverflow.com/a/37350445
 def sqlalchemy_model_to_dict(model):
 
 # https://stackoverflow.com/a/37350445
 def sqlalchemy_model_to_dict(model):
@@ -305,9 +314,9 @@ def report(year, month):
 
     begin = datetime.datetime(year, month, 1)
     end = add_month(begin)
 
     begin = datetime.datetime(year, month, 1)
     end = add_month(begin)
-    data = list(select_sensordata_grouped(mainsensor, 'Wassertemperatur', begin, end))
-    x = [d.timestamp for d in data]
-    y = [d.value for d in data]
+    data = list(select_sensordata(mainsensor, 'Wassertemperatur', begin, end))
+    x = np.array([d.timestamp for d in data])
+    y = np.array([d.value for d in data])
 
     days_datetime = []
     d = begin
 
     days_datetime = []
     d = begin
@@ -319,6 +328,36 @@ def report(year, month):
     binary_pdf = io.BytesIO()
     with PdfPages(binary_pdf) as pdf:
         a4 = (21./2.54, 29.7/2.54)
     binary_pdf = io.BytesIO()
     with PdfPages(binary_pdf) as pdf:
         a4 = (21./2.54, 29.7/2.54)
+        title = 'Seepark Wassertemperatur {} {}'.format(MONTH_DE[begin.month-1], begin.year)
+        report_times = [datetime.time(10), datetime.time(15)]
+
+        # table
+        plt.figure(figsize=a4)
+        columns = ['Datum']
+        for t in report_times:
+            columns.append('Wassertemperatur {} Uhr'.format(t.hour))
+        cells = []
+        for d in days_datetime:
+            cell = ['{}, {}. {}'.format(DAY_OF_WEEK_DE[d.weekday()], d.day, MONTH_DE[d.month-1])]
+            for t in report_times:
+                report_datetime = datetime.datetime.combine(d.date(), t)
+                closed_index = np.argmin(np.abs(x - report_datetime))
+                if abs(x[closed_index] - report_datetime) > datetime.timedelta(hours=1):
+                    cell.append('N/A')
+                else:
+                    value = y[closed_index]
+                    cell.append('{:.1f}° C'.format(value))
+            cells.append(cell)
+
+        ax = plt.gca()
+        ax.table(cellText=cells, colLabels=columns,
+                 loc='upper left')
+        ax.axis('off')
+        plt.title(title)
+        plt.subplots_adjust(left=0.1, right=0.9)  # do not cut row labels
+        pdf.savefig()
+
+        # graphic
         plt.figure(figsize=a4)
         plt.plot(x, y)
         plt.xticks(days_datetime, days_str, rotation='vertical')
         plt.figure(figsize=a4)
         plt.plot(x, y)
         plt.xticks(days_datetime, days_str, rotation='vertical')
@@ -326,7 +365,6 @@ def report(year, month):
         plt.ylabel('Temparatur in °C')
         plt.axis(xmin=begin, xmax=end)
         plt.grid()
         plt.ylabel('Temparatur in °C')
         plt.axis(xmin=begin, xmax=end)
         plt.grid()
-        title = 'Seepark Wassertemperatur {} {}'.format(MONTH_DE[begin.month-1], begin.year)
         plt.title(title)
         pdf.savefig()
 
         plt.title(title)
         pdf.savefig()