Repository: spacetelescope/jwql

Issue:
Fix most_recent_search() in dark monitor
I've just found that most_recent_search() in the bad pixel monitor is not functioning the way I anticipated. It seems to be not filtering correctly, and is not finding results when it should. This is leading to problems in the database tables, and later on, the Bokeh plots. I have a fix that I think should work for the bad pixel monitor. But since the original function in there was based on most_recent_search() in the dark current monitor, I should go fix the dark current monitor as well.

Somehow the subquery wasn't working, so this is the new code I used:

```
query = session.query(self.query_table).filter(self.query_table.aperture==self.aperture). \
                                                filter(run_field==True)

        dates = np.zeros(0)
        if file_type.lower() == 'dark':
            for instance in query:
                dates = np.append(dates, instance.dark_end_time_mjd)
        elif file_type.lower() == 'flat':
            for instance in query:
                dates = np.append(dates, instance.flat_end_time_mjd)
```


Discussion:

Resolving patch:
diff --git a/jwql/instrument_monitors/common_monitors/dark_monitor.py b/jwql/instrument_monitors/common_monitors/dark_monitor.py
--- a/jwql/instrument_monitors/common_monitors/dark_monitor.py
+++ b/jwql/instrument_monitors/common_monitors/dark_monitor.py
@@ -441,31 +441,20 @@ def most_recent_search(self):
             Date (in MJD) of the ending range of the previous MAST query
             where the dark monitor was run.
         """
+        query = session.query(self.query_table).filter(self.query_table.aperture==self.aperture). \
+                                                filter(self.query_table.run_monitor==True)
 
-        sub_query = session.query(self.query_table.aperture,
-                                  func.max(self.query_table.end_time_mjd).label('maxdate')
-                                  ).group_by(self.query_table.aperture).subquery('t2')
+        dates = np.zeros(0)
+        for instance in query:
+            dates = np.append(dates, instance.end_time_mjd)
 
-        # Note that "self.query_table.run_monitor == True" below is
-        # intentional. Switching = to "is" results in an error in the query.
-        query = session.query(self.query_table).join(
-            sub_query,
-            and_(
-                self.query_table.aperture == self.aperture,
-                self.query_table.end_time_mjd == sub_query.c.maxdate,
-                self.query_table.run_monitor == True
-            )
-        ).all()
-
-        query_count = len(query)
+        query_count = len(dates)
         if query_count == 0:
             query_result = 57357.0  # a.k.a. Dec 1, 2015 == CV3
             logging.info(('\tNo query history for {}. Beginning search date will be set to {}.'
                          .format(self.aperture, query_result)))
-        elif query_count > 1:
-            raise ValueError('More than one "most recent" query?')
         else:
-            query_result = query[0].end_time_mjd
+            query_result = np.max(dates)
 
         return query_result
 


Test patch: