From ece8abe1e1a6031c93329846eec469529414bbea Mon Sep 17 00:00:00 2001
From: Mees Fix <mfix@stsci.edu>
Date: Fri, 12 Jul 2024 14:24:59 -0700
Subject: [PATCH 1/3] Changing custom filter kwarg value

---
 .../nirspec_monitors/ta_monitors/msata_monitor.py             | 4 ++--
 .../nirspec_monitors/ta_monitors/wata_monitor.py              | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/jwql/instrument_monitors/nirspec_monitors/ta_monitors/msata_monitor.py b/jwql/instrument_monitors/nirspec_monitors/ta_monitors/msata_monitor.py
index c0ea6201c..dfec82c77 100755
--- a/jwql/instrument_monitors/nirspec_monitors/ta_monitors/msata_monitor.py
+++ b/jwql/instrument_monitors/nirspec_monitors/ta_monitors/msata_monitor.py
@@ -1304,7 +1304,7 @@ def plt_mags_time(self):
         """,
         )
         self.date_range.js_on_change("value", callback)
-        mini_view = CDSView(filters=[self.date_filter])
+        mini_view = CDSView(filter=self.date_filter)
 
         # create the bokeh plot
         plot = figure(
@@ -1408,7 +1408,7 @@ def setup_date_range(self):
                 return indices;
                 """,
         )
-        self.date_view = CDSView(filters=[self.date_filter])
+        self.date_view = CDSView(filter=self.date_filter)
 
     def mk_plt_layout(self):
         """Create the bokeh plot layout"""
diff --git a/jwql/instrument_monitors/nirspec_monitors/ta_monitors/wata_monitor.py b/jwql/instrument_monitors/nirspec_monitors/ta_monitors/wata_monitor.py
index e11955420..64e33e956 100755
--- a/jwql/instrument_monitors/nirspec_monitors/ta_monitors/wata_monitor.py
+++ b/jwql/instrument_monitors/nirspec_monitors/ta_monitors/wata_monitor.py
@@ -797,7 +797,7 @@ def setup_date_range(self):
                 return indices;
                 """,
         )
-        self.date_view = CDSView(filters=[filt])
+        self.date_view = CDSView(filter=filt)
 
     def mk_plt_layout(self):
         """Create the bokeh plot layout"""

From d9468ac35bf6ee20681c14f0fdfffb71e268951a Mon Sep 17 00:00:00 2001
From: Mees Fix <mfix@stsci.edu>
Date: Fri, 12 Jul 2024 19:33:22 -0700
Subject: [PATCH 2/3] Decoupled data query and plotting, added tests with faked
 data to make sure plots build

---
 .../ta_monitors/msata_monitor.py              |  26 ++-
 .../ta_monitors/wata_monitor.py               |  19 +-
 jwql/tests/test_nrs_ta_plotting.py            | 167 ++++++++++++++++++
 3 files changed, 198 insertions(+), 14 deletions(-)
 create mode 100644 jwql/tests/test_nrs_ta_plotting.py

diff --git a/jwql/instrument_monitors/nirspec_monitors/ta_monitors/msata_monitor.py b/jwql/instrument_monitors/nirspec_monitors/ta_monitors/msata_monitor.py
index dfec82c77..3f0fade13 100755
--- a/jwql/instrument_monitors/nirspec_monitors/ta_monitors/msata_monitor.py
+++ b/jwql/instrument_monitors/nirspec_monitors/ta_monitors/msata_monitor.py
@@ -1410,12 +1410,16 @@ def setup_date_range(self):
         )
         self.date_view = CDSView(filter=self.date_filter)
 
-    def mk_plt_layout(self):
-        """Create the bokeh plot layout"""
-        self.query_results = pd.DataFrame(
-            list(NIRSpecMsataStats.objects.all().values())
-        )
-        self.source = ColumnDataSource(data=self.query_results)
+    def mk_plt_layout(self, plot_data):
+        """Create the bokeh plot layout
+
+        Parameters
+        ----------
+        plot_data : pandas.DateFrame
+            Pandas data frame of data to plot.
+        """
+
+        self.source = ColumnDataSource(data=plot_data)
 
         # add a time array to the data source
         self.add_time_column()
@@ -1815,8 +1819,14 @@ def run(self):
             # Add MSATA data to stats table.
             self.add_msata_data()
 
-            # Generate plot -- the database is queried in mk_plt_layout().
-            self.mk_plt_layout()
+            # Query results and convert into pandas df.
+            self.query_results = pd.DataFrame(
+                list(NIRSpecMsataStats.objects.all().values())
+            )
+
+            # Generate plot
+            self.mk_plt_layout(self.query_results)
+
             logging.info(
                 "\tNew output plot file will be written as: {}".format(
                     self.output_file_name
diff --git a/jwql/instrument_monitors/nirspec_monitors/ta_monitors/wata_monitor.py b/jwql/instrument_monitors/nirspec_monitors/ta_monitors/wata_monitor.py
index 64e33e956..57256e02f 100755
--- a/jwql/instrument_monitors/nirspec_monitors/ta_monitors/wata_monitor.py
+++ b/jwql/instrument_monitors/nirspec_monitors/ta_monitors/wata_monitor.py
@@ -799,11 +799,16 @@ def setup_date_range(self):
         )
         self.date_view = CDSView(filter=filt)
 
-    def mk_plt_layout(self):
-        """Create the bokeh plot layout"""
-        self.query_results = pd.DataFrame(list(NIRSpecWataStats.objects.all().values()))
+    def mk_plt_layout(self, plot_data):
+        """Create the bokeh plot layout
+        
+        Parameters
+        ----------
+        plot_data : pandas.DataFrame
+            Dataframe of data to plot in bokeh
+        """
 
-        self.source = ColumnDataSource(data=self.query_results)
+        self.source = ColumnDataSource(data=plot_data)
 
         # add a time array to the data source
         self.add_time_column()
@@ -1144,8 +1149,10 @@ def run(self):
             # Add WATA data to stats table.
             self.add_wata_data()
 
-            # Generate plot -- the database is queried in mk_plt_layout().
-            self.mk_plt_layout()
+            # Get Results from database table
+            self.query_results = pd.DataFrame(list(NIRSpecWataStats.objects.all().values()))
+            # Generate plot.
+            self.mk_plt_layout(self.query_results)
             logging.info(
                 "\tNew output plot file will be written as: {}".format(
                     self.output_file_name
diff --git a/jwql/tests/test_nrs_ta_plotting.py b/jwql/tests/test_nrs_ta_plotting.py
new file mode 100644
index 000000000..5ee04a6ff
--- /dev/null
+++ b/jwql/tests/test_nrs_ta_plotting.py
@@ -0,0 +1,167 @@
+"""Test NRS TA (WATA & MSATA) plotting bokeh routines.
+
+Author
+______
+    - Mees Fix
+"""
+
+import datetime
+import pandas as pd
+
+
+from jwql.instrument_monitors.nirspec_monitors.ta_monitors.msata_monitor import MSATA
+from jwql.instrument_monitors.nirspec_monitors.ta_monitors.wata_monitor import WATA
+
+
+def test_nrs_msata_bokeh():
+    test_row = {
+        "id": 1,
+        "filename": "filename",
+        "date_obs": datetime.datetime(
+            1990, 11, 15, 20, 28, 59, 8000, tzinfo=datetime.timezone.utc
+        ),
+        "visit_id": "visit_id",
+        "tafilter": "tafilter",
+        "detector": "detector",
+        "readout": "readout",
+        "subarray": "subarray",
+        "num_refstars": 1,
+        "ta_status": "ta_status",
+        "v2halffacet": 1.0,
+        "v3halffacet": 1.0,
+        "v2msactr": 1.0,
+        "v3msactr": 1.0,
+        "lsv2offset": 1.0,
+        "lsv3offset": 1.0,
+        "lsoffsetmag": 1.0,
+        "lsrolloffset": 1.0,
+        "lsv2sigma": 1.0,
+        "lsv3sigma": 1.0,
+        "lsiterations": 1,
+        "guidestarid": 1,
+        "guidestarx": 1.0,
+        "guidestary": 1.0,
+        "guidestarroll": 1.0,
+        "samx": 1.0,
+        "samy": 1.0,
+        "samroll": 1.0,
+        "box_peak_value": [
+            1.0,
+            1.0,
+        ],
+        "reference_star_mag": [
+            1.0,
+            1.0,
+        ],
+        "convergence_status": [
+            "convergence_status",
+            "convergence_status",
+        ],
+        "reference_star_number": [
+            1,
+            1,
+        ],
+        "lsf_removed_status": [
+            "lsf_removed_status",
+            "lsf_removed_status",
+        ],
+        "lsf_removed_reason": [
+            "lsf_removed_reason",
+            "lsf_removed_reason",
+        ],
+        "lsf_removed_x": [
+            1.0,
+            1.0,
+        ],
+        "lsf_removed_y": [
+            1.0,
+            1.0,
+        ],
+        "planned_v2": [
+            1.0,
+            1.0,
+        ],
+        "planned_v3": [
+            1.0,
+            1.0,
+        ],
+        "stars_in_fit": 1,
+        "entry_date": datetime.datetime(
+            1990, 11, 15, 20, 28, 59, 8000, tzinfo=datetime.timezone.utc
+        ),
+    }
+
+    df = pd.DataFrame([test_row])
+    monitor = MSATA()
+    monitor.output_file_name = "msata_output.html"
+    monitor.mk_plt_layout(df)
+
+
+def test_nrs_wata_bokeh():
+    test_row = {
+        "id": 1,
+        "filename": "filename",
+        "date_obs": datetime.datetime(
+            1990, 11, 15, 20, 28, 59, 8000, tzinfo=datetime.timezone.utc
+        ),
+        "visit_id": "visit_id",
+        "tafilter": "tafilter",
+        "readout": "readout",
+        "ta_status": "ta_status",
+        "star_name": 1,
+        "star_ra": 1.0,
+        "star_dec": 1.0,
+        "star_mag": 1.0,
+        "star_catalog": 1,
+        "planned_v2": 1.0,
+        "planned_v3": 1.0,
+        "stamp_start_col": 1,
+        "stamp_start_row": 1,
+        "star_detector": "star_detector",
+        "max_val_box": 1.0,
+        "max_val_box_col": 1,
+        "max_val_box_row": 1,
+        "iterations": 1,
+        "corr_col": 1,
+        "corr_row": 1,
+        "stamp_final_col": 1.0,
+        "stamp_final_row": 1.0,
+        "detector_final_col": 1.0,
+        "detector_final_row": 1.0,
+        "final_sci_x": 1.0,
+        "final_sci_y": 1.0,
+        "measured_v2": 1.0,
+        "measured_v3": 1.0,
+        "ref_v2": 1.0,
+        "ref_v3": 1.0,
+        "v2_offset": 1.0,
+        "v3_offset": 1.0,
+        "sam_x": 1.0,
+        "sam_y": 1.0,
+        "entry_date": datetime.datetime(
+            1990, 11, 15, 20, 28, 59, 8000, tzinfo=datetime.timezone.utc
+        ),
+        "nrsrapid_f140x": [
+            1.0
+        ],  # Not in DB but added to column source data in algorithm, adding here
+        "nrsrapid_f110w": [
+            1.0
+        ],  # Not in DB but added to column source data in algorithm, adding here
+        "nrsrapid_clear": [
+            1.0
+        ],  # Not in DB but added to column source data in algorithm, adding here
+        "nrsrapidd6_f140x": [
+            1.0
+        ],  # Not in DB but added to column source data in algorithm, adding here
+        "nrsrapidd6_f110w": [
+            1.0
+        ],  # Not in DB but added to column source data in algorithm, adding here
+        "nrsrapidd6_clear": [
+            1.0
+        ],  # Not in DB but added to column source data in algorithm, adding here
+    }
+
+    df = pd.DataFrame([test_row])
+    monitor = WATA()
+    monitor.output_file_name = "wata_output.html"
+    monitor.mk_plt_layout(df)

From 5c055fc8f9a7b371c6ac5287f02e6c40fce04e6d Mon Sep 17 00:00:00 2001
From: Mees Fix <mfix@stsci.edu>
Date: Fri, 12 Jul 2024 19:34:21 -0700
Subject: [PATCH 3/3] pep8

---
 .../nirspec_monitors/ta_monitors/wata_monitor.py                | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/jwql/instrument_monitors/nirspec_monitors/ta_monitors/wata_monitor.py b/jwql/instrument_monitors/nirspec_monitors/ta_monitors/wata_monitor.py
index 57256e02f..a2f3301e5 100755
--- a/jwql/instrument_monitors/nirspec_monitors/ta_monitors/wata_monitor.py
+++ b/jwql/instrument_monitors/nirspec_monitors/ta_monitors/wata_monitor.py
@@ -801,7 +801,7 @@ def setup_date_range(self):
 
     def mk_plt_layout(self, plot_data):
         """Create the bokeh plot layout
-        
+
         Parameters
         ----------
         plot_data : pandas.DataFrame