From 80145c98781f11aa3735059b1de85d227c30387b Mon Sep 17 00:00:00 2001
From: Coda Phillips <cphillips@sean.ssec.wisc.edu>
Date: Wed, 6 Jul 2016 11:07:01 -0500
Subject: [PATCH] Finish up the interpreter

---
 interpret_qc.py      | 56 ++++++++++++++++++++++++++++++++++++++++----
 templates/index.html | 12 ++++++++++
 templates/qc.html    | 11 +++++++++
 3 files changed, 75 insertions(+), 4 deletions(-)
 create mode 100644 templates/index.html
 create mode 100644 templates/qc.html

diff --git a/interpret_qc.py b/interpret_qc.py
index 11f6f2c..a181e3c 100644
--- a/interpret_qc.py
+++ b/interpret_qc.py
@@ -2,21 +2,64 @@ import netCDF4
 import pandas as pd
 import numpy as np
 import matplotlib.pyplot as plt
+import mpld3
 from aeri_tools.io.dmv.housekeeping import get_all_housekeeping
 from main import files_to_update
 from glob import glob
 import os
+import flask
+from flask import render_template
+from datetime import datetime
+
+app = flask.Flask(__name__)
+
+global_ftp_dir = None
+
+@app.route('/')
+def index():
+    ae_dirs = glob(os.path.join(global_ftp_dir, 'AE*/*.qc'))
+    ae_dirs = [a.lstrip('/') for a in ae_dirs]
+    return flask.render_template('index.html', ae_dirs=ae_dirs)
+
+@app.route('/qc/<path:qc_path>')
+def qc_day(qc_path):
+    qc_path = '/'+qc_path
+    cxs_path = qc_path.replace('.qc','B1.CXS')
+    sum_path = qc_path.replace('.qc','.SUM')
+
+    cxs = get_all_housekeeping(cxs_path)
+    frame = cxs.combine_first(get_all_housekeeping(sum_path))
+    frame = frame.combine_first(get_qc_frame(qc_path))
+
+    plots = []
+
+    for qc_variable in ([
+        'ABBapexTemp',
+        'ABBtopTemp',
+        'ABBbottomTemp',
+        'HBBapexTemp',
+        'HBBtopTemp',
+        'HBBbottomTemp',
+        'calibrationAmbientTemp']):
+        plot = plot_variable_qc(frame, qc_variable)
+        if plot is not None:
+            plots.append(plot)
+
+    return flask.render_template('qc.html', qc_path=qc_path, plots=plots)
 
 def save_plot(filename):
     print('saving {}'.format(filename))
     plt.savefig(filename, transparent=True)
 
-def plot_variable_qc(frame, loc, filename):
+def plot_variable_qc(frame, loc, filename=None):
     qc_loc = 'qc_' + loc
     if sum(frame[qc_loc]) > 0:
-        plt.figure()
+        fig = plt.figure(figsize=(50,5))
         plot_outliers(frame, frame[qc_loc] == 0, loc)
-        save_plot(filename)
+        if filename is not None:
+            save_plot(filename)
+        else:
+            return mpld3.fig_to_html(fig)
 
 def plot_outliers(frame, qc_mask, loc):
     if (~qc_mask).any():
@@ -60,8 +103,13 @@ if __name__ == '__main__':
     import argparse
     parser = argparse.ArgumentParser()
     parser.add_argument('ftp')
+    parser.add_argument('--serve', action='store_true')
 
     args = parser.parse_args()
 
-    generate_all_plots(args.ftp)
+    if not args.serve:
+        generate_all_plots(args.ftp)
+    else:
+        global_ftp_dir = args.ftp
+        app.run(debug=True)
     
diff --git a/templates/index.html b/templates/index.html
new file mode 100644
index 0000000..161aa81
--- /dev/null
+++ b/templates/index.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html>
+    <head>
+    </head>
+    <body>
+        <ul>
+        {% for d in ae_dirs %}
+            <a href="/qc/{{d}}"><li>/{{d}}</li></a>
+        {% endfor %}
+        </ul>
+    </body
+</html>
diff --git a/templates/qc.html b/templates/qc.html
new file mode 100644
index 0000000..78bd4e2
--- /dev/null
+++ b/templates/qc.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+    <head>
+    </head>
+    <body>
+    <h1>{{qc_path}}</h1>
+    {% for plot in plots %}
+        {{plot|safe}}
+    {% endfor %}
+    </body
+</html>
-- 
GitLab