Add air temperature.
[chrisu/seepark.git] / web / seepark_web.py
index b899a1730ebdfaea506fd652c7993f0d3b2d2779..0ba8f57e5132d8ddf368538e0a37afe4dbc8297b 100644 (file)
@@ -125,6 +125,13 @@ def select_sensordata(sensor_id, sensor_type, begin, end):
     return query.all()
 
 
+def sensordata_to_xy(sensordata):
+    sensordata = list(sensordata)
+    x = np.array([d.timestamp for d in sensordata])
+    y = np.array([d.value for d in sensordata])
+    return x, y
+
+
 def select_sensordata_grouped(sensor_id, sensor_type, begin, end):
     # determine resolution (interval in seconds for data points)
     resolution = calc_grouping_resolution(begin, end)
@@ -154,6 +161,13 @@ def select_openweatherdata(cityid, begin, end):
     return query.all()
 
 
+def openweatherdata_to_xy(openweatherdata):
+    openweatherdata = list(openweatherdata)
+    x = np.array([d.datetime for d in openweatherdata])
+    y = np.array([d.temp for d in openweatherdata])
+    return x, y
+
+
 def select_openweatherdata_grouped(cityid, begin, end):
     # determine resolution (interval in seconds for data points)
     resolution = calc_grouping_resolution(begin, end)
@@ -311,12 +325,16 @@ def currentwater(version):
 
 @app.route('/report/<int:year>-<int:month>')
 def report(year, month):
+    paper_size = (29.7 / 2.54, 21. / 2.54)  # A4
 
     begin = datetime.datetime(year, month, 1)
     end = add_month(begin)
-    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])
+
+    water_data = sensordata_to_xy(select_sensordata(mainsensor, 'Wassertemperatur', begin, end))
+    air_data = openweatherdata_to_xy(select_openweatherdata(cityid, begin, end))
+
+    report_times = [datetime.time(10), datetime.time(15)]
+    report_data = {'Wasser': water_data, 'Luft': air_data}
 
     days_datetime = []
     d = begin
@@ -326,16 +344,17 @@ def report(year, month):
 
     binary_pdf = io.BytesIO()
     with PdfPages(binary_pdf) as pdf:
-        a4 = (29.7/2.54, 21./2.54)
-        title = 'Seepark Wassertemperatur {} {}'.format(MONTH_DE[begin.month-1], begin.year)
-        report_times = [datetime.time(10), datetime.time(15)]
+        title = 'Seepark Obsteig {} {}'.format(MONTH_DE[begin.month-1], begin.year)
 
         # graphic
-        plt.figure(figsize=a4)
-        plt.plot(x, y)
+        plt.figure(figsize=paper_size)
+        for label, data in sorted(report_data.items(), reverse=True):
+            x, y = data
+            plt.plot(x, y, label=label)
         plt.xticks(days_datetime, [''] * len(days_datetime))
         plt.ylabel('Temperatur in °C')
         plt.axis(xmin=begin, xmax=end)
+        plt.legend()
         plt.grid()
         plt.title(title)
 
@@ -344,22 +363,24 @@ def report(year, month):
         for d in days_datetime:
             columns.append('{}.'.format(d.day))
         rows = []
-        for t in report_times:
-            rows.append('Wasser {:02d}:{:02d} °C'.format(t.hour, t.minute))
+        for label in sorted(report_data.keys(), reverse=True):
+            for t in report_times:
+                rows.append('{:02d}:{:02d} {} °C'.format(t.hour, t.minute, label))
         cells = []
-        for t in report_times:
-            columns.append('{}.'.format(d.day))
-            row_cells = []
-            for d in days_datetime:
-                report_datetime = datetime.datetime.combine(d.date(), t)
-                closest_index = np.argmin(np.abs(x - report_datetime))
-                if abs(x[closest_index] - report_datetime) > datetime.timedelta(hours=1):
-                    cell = 'N/A'
-                else:
-                    value = y[closest_index]
-                    cell = '{:.1f}'.format(value)
-                row_cells.append(cell)
-            cells.append(row_cells)
+        for label, data in sorted(report_data.items(), reverse=True):
+            for t in report_times:
+                row_cells = []
+                x, y = data
+                for d in days_datetime:
+                    report_datetime = datetime.datetime.combine(d.date(), t)
+                    closest_index = np.argmin(np.abs(x - report_datetime))
+                    if abs(x[closest_index] - report_datetime) > datetime.timedelta(hours=1):
+                        cell = 'N/A'
+                    else:
+                        value = y[closest_index]
+                        cell = '{:.1f}'.format(value)
+                    row_cells.append(cell)
+                cells.append(row_cells)
         table = plt.table(cellText=cells, colLabels=columns, rowLabels=rows, loc='bottom')
         table.scale(xscale=1, yscale=2)
         plt.title(title)
@@ -369,8 +390,8 @@ def report(year, month):
         pdf_info = pdf.infodict()
         pdf_info['Title'] = title
         pdf_info['Author'] = 'Chrisu Jähnl'
-        pdf_info['Subject'] = 'Wassertemperatur'
-        pdf_info['Keywords'] = 'Seepark Wassertemperatur'
+        pdf_info['Subject'] = 'Temperaturen'
+        pdf_info['Keywords'] = 'Seepark Obsteig'
         pdf_info['CreationDate'] = datetime.datetime.now()
         pdf_info['ModDate'] = datetime.datetime.today()