From 92774c0126eb26037664971f23775a2b91880fa0 Mon Sep 17 00:00:00 2001
From: Jameson Graef Rollins <jameson.rollins@ligo.org>
Date: Thu, 19 Dec 2019 13:25:52 -0800
Subject: [PATCH] brs plugin cleanup

* move config to config module (so it's more easily available elsewhere)
* properly map station and axis
---
 locklost/config.py               | 56 ++++++++++++++++++++++++++
 locklost/plugins/brs.py          | 68 +++-----------------------------
 locklost/web/templates/event.tpl | 48 +++++++---------------
 3 files changed, 76 insertions(+), 96 deletions(-)

diff --git a/locklost/config.py b/locklost/config.py
index 592b8e7d..e402b91e 100644
--- a/locklost/config.py
+++ b/locklost/config.py
@@ -226,6 +226,62 @@ LPY_CHANNELS = ifochans(LPY_CHANNELS)
 
 ##################################################
 
+BRS_SEARCH_WINDOW = [-30, 5]
+BRS_THRESHOLD = 15
+
+# H1 checks single status channel that says what combination of BRSX/BRSY is
+# being used
+if IFO == 'H1':
+    BRS_CONFIG = {
+        'ETMX': {
+            'axis': 'RY',
+            'skip_states': {65},
+        },
+        'ETMY': {
+            'axis': 'RX',
+            'skip_states': {60},
+        },
+    }
+    for station in BRS_CONFIG:
+        BRS_CONFIG[station]['state_chan'] = 'H1:GRD-SEI_CONF_REQUEST_N'
+        BRS_CONFIG[station]['skip_states'] |= {10, 17, 45}
+
+# L1 checks status channels for each individual BRS (ETMs and ITMs) that says
+# whether it's being used
+elif IFO == 'L1':
+    BRS_CONFIG = {
+        'ITMX': {
+            'axis': 'RY',
+            'state_chan': 'L1:ISI-ITMX_ST1_SENSCOR_X_FADE_CUR_CHAN_MON',
+        },
+        'ETMX': {
+            'axis': 'RY',
+            'state_chan': 'L1:ISI-ETMX_ST1_SENSCOR_Y_FADE_CUR_CHAN_MON',
+        },
+        'ITMY': {
+            'axis': 'RX',
+            'state_chan': 'L1:ISI-ITMY_ST1_SENSCOR_Y_FADE_CUR_CHAN_MON',
+        },
+        'ETMY': {
+            'axis': 'RX',
+            'state_chan': 'L1:ISI-ETMY_ST1_SENSCOR_Y_FADE_CUR_CHAN_MON',
+        },
+    }
+    for station in BRS_CONFIG:
+        BRS_CONFIG[station]['skip_states'] = {8, 9}
+
+for station, params in BRS_CONFIG.items():
+    BRS_CONFIG[station]['channels'] = [
+        '{}:ISI-GND_BRS_{}_{}_BLRMS_{}'.format(
+            IFO, station, params['axis'], ending) \
+        for ending in [
+                '100M_300M',
+                '300M_1',
+                '1_3',
+        ]]
+
+##################################################
+
 BOARD_SAT_CM = [
     'mediumseagreen',
     'cornflowerblue',
diff --git a/locklost/plugins/brs.py b/locklost/plugins/brs.py
index 8e6dfeb1..dee6045e 100644
--- a/locklost/plugins/brs.py
+++ b/locklost/plugins/brs.py
@@ -8,63 +8,6 @@ from .. import config
 from .. import data
 from .. import plotutils
 
-#################################################
-
-CHANNEL_ENDINGS = [
-    '100M_300M',
-    '300M_1',
-    '1_3',
-]
-
-# H1 checks single status channel that says what combination of BRSX/BRSY is
-# being used
-if config.IFO == 'H1':
-    CONFIG = {
-        'ETMX': {
-            'skip_states': {65},
-        },
-        'ETMY': {
-            'skip_states': {60},
-        },
-    }
-    for station in CONFIG:
-        CONFIG[station]['state_chan'] = 'H1:GRD-SEI_CONF_REQUEST_N'
-        CONFIG[station]['skip_states'] |= {10, 17, 45}
-    AXES = ['RY', 'RX']
-
-# L1 checks status channels for each individual BRS (ETMs and ITMs) that says
-# whether it's being used
-elif config.IFO == 'L1':
-    CONFIG = {
-        'ITMX': {
-            'state_chan': 'L1:ISI-ITMX_ST1_SENSCOR_X_FADE_CUR_CHAN_MON',
-        },
-        'ETMX': {
-            'state_chan': 'L1:ISI-ETMX_ST1_SENSCOR_Y_FADE_CUR_CHAN_MON',
-        },
-        'ITMY': {
-            'state_chan': 'L1:ISI-ITMY_ST1_SENSCOR_Y_FADE_CUR_CHAN_MON',
-        },
-        'ETMY': {
-            'state_chan': 'L1:ISI-ETMY_ST1_SENSCOR_Y_FADE_CUR_CHAN_MON',
-        },
-    }
-    for station in CONFIG:
-        CONFIG[station]['skip_states'] = {8, 9}
-    AXES = ['RY', 'RX', 'RY', 'RX']
-
-for station in CONFIG:
-    channels = []
-    for axis in AXES:
-        for ending in CHANNEL_ENDINGS:
-            channels.append('{}:ISI-GND_BRS_{}_{}_BLRMS_{}'.format(config.IFO, station, axis, ending))
-    CONFIG[station]['channels'] = channels
-
-THRESHOLD = 15
-
-SEARCH_WINDOW = [-30, 5]
-
-#################################################
 
 def check_brs(event):
     """Checks for BRS glitches at both end stations.
@@ -76,9 +19,9 @@ def check_brs(event):
 
     plotutils.set_rcparams()
 
-    segment = Segment(SEARCH_WINDOW).shift(int(event.gps))
+    segment = Segment(config.BRS_SEARCH_WINDOW).shift(int(event.gps))
 
-    for station, params in CONFIG.items():
+    for station, params in config.BRS_CONFIG.items():
         # fetch all data (state and data)
         channels = [params['state_chan']] + params['channels']
         try:
@@ -94,16 +37,17 @@ def check_brs(event):
         if state in params['skip_states']:
             logging.info('{} not using sensor correction during lockloss'.format(station))
             continue
+        del buf_dict[params['state_chan']]
 
         # look for glitches
         max_brs = 0
         thresh_crossing = segment[1]
         for channel, buf in buf_dict.items():
             max_brs = max([max_brs, max(buf.data)])
-            if any(buf.data > THRESHOLD):
+            if any(buf.data > config.BRS_THRESHOLD):
                 logging.info('BRS GLITCH DETECTED in {}'.format(channel))
                 event.add_tag('BRS_GLITCH')
-                glitch_idx = np.where(buf.data > THRESHOLD)[0][0]
+                glitch_idx = np.where(buf.data > config.BRS_THRESHOLD)[0][0]
                 glitch_time = buf.tarray[glitch_idx]
                 thresh_crossing = min(glitch_time, thresh_crossing)
 
@@ -118,7 +62,7 @@ def check_brs(event):
                 lw=2,
             )
         ax.axhline(
-            THRESHOLD,
+            config.BRS_THRESHOLD,
             linestyle='--',
             color='black',
             label='BRS glitch threshold',
diff --git a/locklost/web/templates/event.tpl b/locklost/web/templates/event.tpl
index 3aa84e09..a25a6869 100644
--- a/locklost/web/templates/event.tpl
+++ b/locklost/web/templates/event.tpl
@@ -1,7 +1,6 @@
 % rebase('base.tpl', IFO=IFO, web_script=web_script, date=date, online_status=online_status)
 
 % import os
-% import glob
 % from datetime import timedelta
 % from locklost import config
 % from locklost.web.utils import analysis_status_button, event_plot_urls
@@ -98,43 +97,24 @@
 <hr />
 <div class="container">
 <br />
-% has_tag = False
-% if event.has_tag('BRS_GLITCH'):
-%       has_tag = True
-% end
-% if config.IFO == 'H1':
-%     brs_plots, endstation_used = [], {'ETMX':True, 'ETMY':True}
-% elif config.IFO == 'L1':
-%     brs_plots, endstation_used = [], {'ETMX':True, 'ETMY':True, 'ITMX':True, 'ITMY':True}
-% end
-
-% for endstation in endstation_used:
-%     brs_url = event.url('brs_{}.png'.format(endstation))
-%     if os.path.exists(event.path('brs_{}.png'.format(endstation))):
-%         brs_plots.append(brs_url)
+% brs_plots = []
+% brs_unused = []
+% for station in config.BRS_CONFIG:
+%     if os.path.exists(event.path('brs_{}.png'.format(station))):
+%         brs_plots.append(event.url('brs_{}.png'.format(station)))
 %     else:
-%         endstation_used[endstation] = False
+%         brs_unused.append(station)
 %     end
 % end
-<p>{{endstation_used}}</p>
-% if any(status is True for status in endstation_used.values()):
-      <div class="row">
-%     include('collapsed_plots.tpl', title ='BRS plots', id='brs', plots=brs_plots, size=5, section='main', expand=has_tag)
-      </div>
-% end
-% for endstation, used in endstation_used.items():
-%     if used is False:
-          <br/>
-          <div class="row">
-          <p>{{endstation}} not using sensor correction during lockloss</p>
-          </div>
-%     end
+<div class="row">
+% include('collapsed_plots.tpl', title ='BRS plots', id='brs', plots=brs_plots, size=5, section='main', expand=event.has_tag('BRS_GLITCH'))
+</div>
+% for station in brs_unused:
+<br/>
+<div class="row">
+<p>{{station}} not using sensor correction during lockloss</p>
+</div>
 % end
-% for endstation in ['ETMX', 'ETMY']:
-%       brs_url = event.url('brs_{}.png'.format(endstation))
-%       brs_plots.append(brs_url)
-%       end
-% include('collapsed_plots.tpl', title ='BRS plots', id='brs', plots=brs_plots, size=5, expand=has_tag, section='main')
 </div>
 
 <!-- analog board saturation plot -->
-- 
GitLab