From 1b4a05ce294d94f16b64e00879cdb6124d2b05b4 Mon Sep 17 00:00:00 2001
From: Martyn Gigg <martyn.gigg@stfc.ac.uk>
Date: Thu, 5 Jan 2012 21:09:41 +0000
Subject: [PATCH] Refs #4200. Diag should now match libisis.

Additional parameters were required to better match the granularity
of the libisis testsx
---
 Code/Mantid/instrument/ARCS_Parameters.xml    |  75 +++-
 Code/Mantid/instrument/CNCS_Parameters.xml    |  73 +++-
 Code/Mantid/instrument/HYSPEC_Parameters.xml  |  73 +++-
 Code/Mantid/instrument/LET_Parameters.xml     | 110 ++++-
 Code/Mantid/instrument/MAPS_Parameters.xml    | 105 ++++-
 Code/Mantid/instrument/MARI_Parameters.xml    |  98 ++++-
 Code/Mantid/instrument/MERLIN_Parameters.xml  | 111 ++++-
 Code/Mantid/instrument/SEQUOIA_Parameters.xml |  73 +++-
 .../Inelastic/DirectEnergyConversion.py       | 197 ++++++---
 Code/Mantid/scripts/Inelastic/diagnostics.py  | 398 +++++++-----------
 10 files changed, 895 insertions(+), 418 deletions(-)

diff --git a/Code/Mantid/instrument/ARCS_Parameters.xml b/Code/Mantid/instrument/ARCS_Parameters.xml
index 0f868a5cf40..5697452ec58 100644
--- a/Code/Mantid/instrument/ARCS_Parameters.xml
+++ b/Code/Mantid/instrument/ARCS_Parameters.xml
@@ -50,6 +50,74 @@
 
 		<!-- Diagnostic test defaults -->
 
+		<!-- Absolute lo threshold for vanadium diag (tiny) -->
+		<parameter name="diag_tiny">
+		  <value val="1e-10"/>
+		</parameter>
+
+		<!-- Absolute hi threshold for vanadium diag (huge) -->
+		<parameter name="diag_huge">
+		  <value val="1e10"/>
+		</parameter>
+
+		<!-- Remove zeroes in background (s_zero)-->
+		<parameter name="diag_samp_zero">
+		  <value val="1.0"/>
+		</parameter>
+
+		<!-- Fraction of median to consider counting low for the white beam diag (sv_lo)-->
+		<parameter name="diag_samp_lo">
+		  <value val="0.0"/>
+		</parameter>
+
+		<!-- Fraction of median to consider counting high for the white beam diag (sv_hi)-->
+		<parameter name="diag_samp_hi">
+		  <value val="5.0"/>
+		</parameter>
+
+		<!-- Error criterion as a multiple of error bar for background (sv_sig) -->
+		<parameter name="diag_samp_sig">
+		  <value val="3.3"/>
+		</parameter>
+
+		<!-- Lower bound defining outliers as fraction of median value (v_out_lo)-->
+		<parameter name="diag_van_out_lo">
+		  <value val="0.01"/>
+		</parameter>
+
+		<!-- Upper bound defining outliers as fraction of median value (v_out_hi)-->
+		<parameter name="diag_van_out_hi">
+		  <value val="100."/>
+		</parameter>
+
+		<!-- Fraction of median to consider counting low for the white beam diag (vv_lo)-->
+		<parameter name="diag_van_lo">
+		  <value val="0.1"/>
+		</parameter>
+
+		<!-- Fraction of median to consider counting high for the white beam diag (vv_hi)-->
+		<parameter name="diag_van_hi">
+		  <value val="3.0"/>
+		</parameter>
+
+		<!-- Error criterion as a multiple of error bar for background (vv_sig) -->
+		<parameter name="diag_van_sig">
+		  <value val="3.3"/>
+		</parameter>
+
+		<!-- True if background is to be checked -->
+		<parameter name="check_background">
+		  <value val="1.0"/>
+		</parameter>
+
+		<!-- True if the bleed tests should be run -->
+		<parameter name="diag_bleed_test">
+		  <value val="0.0"/>
+		</parameter>
+
+
+		<!-- TO BE REMOVED Diagnostic test defaults -->
+
 		<parameter name="remove_zero">
 			<value val="1.0" />
 		</parameter>
@@ -87,6 +155,7 @@
 		</parameter>
 
 		<!-- -->
+
 		<!-- Absolute units conversion average -->
 
 		<parameter name="monovan_lo_bound">
@@ -106,12 +175,6 @@
 		</parameter>
 
 		<!-- -->
-		<!-- PSD bleed test -->
-
-		<parameter name="bleed_test">
-			<value val="0.0" />
-		</parameter>
-
 		<!-- All the following parameters are taken directly from the MARI definition 
 			and are WRONG! They are only here for now to get things working -->
 
diff --git a/Code/Mantid/instrument/CNCS_Parameters.xml b/Code/Mantid/instrument/CNCS_Parameters.xml
index 4c3054e6479..af3a868ea1c 100644
--- a/Code/Mantid/instrument/CNCS_Parameters.xml
+++ b/Code/Mantid/instrument/CNCS_Parameters.xml
@@ -50,6 +50,73 @@
 
 		<!-- Diagnostic test defaults -->
 
+		<!-- Absolute lo threshold for vanadium diag (tiny) -->
+		<parameter name="diag_tiny">
+		  <value val="1e-10"/>
+		</parameter>
+
+		<!-- Absolute hi threshold for vanadium diag (huge) -->
+		<parameter name="diag_huge">
+		  <value val="1e10"/>
+		</parameter>
+
+		<!-- Remove zeroes in background (s_zero)-->
+		<parameter name="diag_samp_zero">
+		  <value val="1.0"/>
+		</parameter>
+
+		<!-- Fraction of median to consider counting low for the white beam diag (sv_lo)-->
+		<parameter name="diag_samp_lo">
+		  <value val="0.0"/>
+		</parameter>
+
+		<!-- Fraction of median to consider counting high for the white beam diag (sv_hi)-->
+		<parameter name="diag_samp_hi">
+		  <value val="5.0"/>
+		</parameter>
+
+		<!-- Error criterion as a multiple of error bar for background (sv_sig) -->
+		<parameter name="diag_samp_sig">
+		  <value val="3.3"/>
+		</parameter>
+
+		<!-- Lower bound defining outliers as fraction of median value (v_out_lo)-->
+		<parameter name="diag_van_out_lo">
+		  <value val="0.01"/>
+		</parameter>
+
+		<!-- Upper bound defining outliers as fraction of median value (v_out_hi)-->
+		<parameter name="diag_van_out_hi">
+		  <value val="100."/>
+		</parameter>
+
+		<!-- Fraction of median to consider counting low for the white beam diag (vv_lo)-->
+		<parameter name="diag_van_lo">
+		  <value val="0.1"/>
+		</parameter>
+
+		<!-- Fraction of median to consider counting high for the white beam diag (vv_hi)-->
+		<parameter name="diag_van_hi">
+		  <value val="3.0"/>
+		</parameter>
+
+		<!-- Error criterion as a multiple of error bar for background (vv_sig) -->
+		<parameter name="diag_van_sig">
+		  <value val="3.3"/>
+		</parameter>
+
+		<!-- True if background is to be checked -->
+		<parameter name="check_background">
+		  <value val="1.0"/>
+		</parameter>
+
+		<!-- True if the bleed tests should be run -->
+		<parameter name="diag_bleed_test">
+		  <value val="0.0"/>
+		</parameter>
+
+		<!-- TO BE REMOVED Diagnostic test defaults -->
+
 		<parameter name="remove_zero">
 			<value val="1.0" />
 		</parameter>
@@ -104,12 +171,6 @@
 			<value val="1.2" />
 		</parameter>
 
-		<!-- PSD bleed test -->
-
-		<parameter name="bleed_test">
-			<value val="0.0" />
-		</parameter>
-
 		<!-- All the following parameters are taken directly from the MARI definition 
 			and are WRONG! They are only here for now to get things working -->
 
diff --git a/Code/Mantid/instrument/HYSPEC_Parameters.xml b/Code/Mantid/instrument/HYSPEC_Parameters.xml
index f58e45f309f..5bf54fe1c1c 100644
--- a/Code/Mantid/instrument/HYSPEC_Parameters.xml
+++ b/Code/Mantid/instrument/HYSPEC_Parameters.xml
@@ -50,6 +50,73 @@
 
 		<!-- Diagnostic test defaults -->
 
+		<!-- Absolute lo threshold for vanadium diag (tiny) -->
+		<parameter name="diag_tiny">
+		  <value val="1e-10"/>
+		</parameter>
+
+		<!-- Absolute hi threshold for vanadium diag (huge) -->
+		<parameter name="diag_huge">
+		  <value val="1e10"/>
+		</parameter>
+
+		<!-- Remove zeroes in background (s_zero)-->
+		<parameter name="diag_samp_zero">
+		  <value val="1.0"/>
+		</parameter>
+
+		<!-- Fraction of median to consider counting low for the white beam diag (sv_lo)-->
+		<parameter name="diag_samp_lo">
+		  <value val="0.0"/>
+		</parameter>
+
+		<!-- Fraction of median to consider counting high for the white beam diag (sv_hi)-->
+		<parameter name="diag_samp_hi">
+		  <value val="5.0"/>
+		</parameter>
+
+		<!-- Error criterion as a multiple of error bar for background (sv_sig) -->
+		<parameter name="diag_samp_sig">
+		  <value val="3.3"/>
+		</parameter>
+
+		<!-- Lower bound defining outliers as fraction of median value (v_out_lo)-->
+		<parameter name="diag_van_out_lo">
+		  <value val="0.01"/>
+		</parameter>
+
+		<!-- Upper bound defining outliers as fraction of median value (v_out_hi)-->
+		<parameter name="diag_van_out_hi">
+		  <value val="100."/>
+		</parameter>
+
+		<!-- Fraction of median to consider counting low for the white beam diag (vv_lo)-->
+		<parameter name="diag_van_lo">
+		  <value val="0.1"/>
+		</parameter>
+
+		<!-- Fraction of median to consider counting high for the white beam diag (vv_hi)-->
+		<parameter name="diag_van_hi">
+		  <value val="3.0"/>
+		</parameter>
+
+		<!-- Error criterion as a multiple of error bar for background (vv_sig) -->
+		<parameter name="diag_van_sig">
+		  <value val="3.3"/>
+		</parameter>
+
+		<!-- True if background is to be checked -->
+		<parameter name="check_background">
+		  <value val="1.0"/>
+		</parameter>
+
+		<!-- True if the bleed tests should be run -->
+		<parameter name="diag_bleed_test">
+		  <value val="0.0"/>
+		</parameter>
+
+		<!-- TO BE REMOVED Diagnostic test defaults -->
+
 		<parameter name="remove_zero">
 			<value val="1.0" />
 		</parameter>
@@ -104,12 +171,6 @@
 			<value val="1.2" />
 		</parameter>
 
-		<!-- PSD bleed test -->
-
-		<parameter name="bleed_test">
-			<value val="0.0" />
-		</parameter>
-
 		<!-- All the following parameters are taken directly from the MARI definition 
 			and are WRONG! They are only here for now to get things working -->
 
diff --git a/Code/Mantid/instrument/LET_Parameters.xml b/Code/Mantid/instrument/LET_Parameters.xml
index af6cd303cc4..bbc34e406a0 100644
--- a/Code/Mantid/instrument/LET_Parameters.xml
+++ b/Code/Mantid/instrument/LET_Parameters.xml
@@ -72,42 +72,81 @@
   <value val="50.9415"/>
 </parameter>
 
-<!-- Diagnostic test defaults -->
-
-<parameter name="remove_zero">
-  <value val="1.0"/>
-</parameter>
+<!-- Diagnostic defaults -->
 
-<parameter name="tiny">
+<!-- Absolute lo threshold for vanadium diag (tiny) -->
+<parameter name="diag_tiny">
   <value val="1e-10"/>
 </parameter>
 
-<parameter name="large">
+<!-- Absolute hi threshold for vanadium diag (huge) -->
+<parameter name="diag_huge">
   <value val="1e10"/>
 </parameter>
 
-<parameter name="median_lo">
-  <value val="0.1"/>
+<!-- Remove zeroes in background (s_zero)-->
+<parameter name="diag_samp_zero">
+  <value val="0.0"/>
 </parameter>
 
-<parameter name="median_hi">
-  <value val="3.0"/>
+<!-- Fraction of median to consider counting low for the white beam diag (sv_lo)-->
+<parameter name="diag_samp_lo">
+  <value val="0.0"/>
 </parameter>
 
-<parameter name="signif">
+<!-- Fraction of median to consider counting high for the white beam diag (sv_hi)-->
+<parameter name="diag_samp_hi">
+  <value val="1.5"/>
+</parameter>
+
+<!-- Error criterion as a multiple of error bar for background (sv_sig) -->
+<parameter name="diag_samp_sig">
   <value val="3.3"/>
 </parameter>
 
+<!-- Lower bound defining outliers as fraction of median value (v_out_lo)-->
+<parameter name="diag_van_out_lo">
+  <value val="0.01"/>
+</parameter>
+
+<!-- Upper bound defining outliers as fraction of median value (v_out_hi)-->
+<parameter name="diag_van_out_hi">
+  <value val="100."/>
+</parameter>
+
+<!-- Fraction of median to consider counting low for the white beam diag (vv_lo)-->
+<parameter name="diag_van_lo">
+  <value val="0.1"/>
+</parameter>
+
+<!-- Fraction of median to consider counting high for the white beam diag (vv_hi)-->
+<parameter name="diag_van_hi">
+  <value val="2.0"/>
+</parameter>
+
+<!-- Error criterion as a multiple of error bar for background (vv_sig) -->
+<parameter name="diag_van_sig">
+  <value val="0.0"/>
+</parameter>
+
+<!-- True if background is to be checked -->
 <parameter name="check_background">
   <value val="1.0"/>
 </parameter>
 
-<parameter name="bkgd_threshold">
-  <value val="5.0"/>
+<!-- True if the bleed tests should be run -->
+<parameter name="diag_bleed_test">
+  <value val="1.0"/>
 </parameter>
 
-<parameter name="variation">
-  <value val="1.1"/>
+<!-- The maximum framerate to allow in a tube -->
+<parameter name="diag_bleed_maxrate">
+  <value val="0.01"/>
+</parameter>
+
+<!-- The number of pixels to ignore around the centre of the tube -->
+<parameter name="diag_bleed_pixels">
+  <value val="80"/>
 </parameter>
 
 <!--  -->
@@ -130,19 +169,44 @@
 </parameter>
 
 <!--  -->
-<!-- PSD bleed test -->
-<!-- PSD bleed test -->
 
-<parameter name="bleed_test">
+<!-- To be removed -->
+<!-- Diagnostic test defaults -->
+
+<parameter name="remove_zero">
   <value val="1.0"/>
 </parameter>
 
-<parameter name="bleed_max_framerate">
-  <value val="0.01"/>
+<parameter name="tiny">
+  <value val="1e-10"/>
 </parameter>
 
-<parameter name="bleed_ignored_pixels">
-  <value val="80"/>
+<parameter name="large">
+  <value val="1e10"/>
+</parameter>
+
+<parameter name="median_lo">
+  <value val="0.1"/>
+</parameter>
+
+<parameter name="median_hi">
+  <value val="3.0"/>
+</parameter>
+
+<parameter name="signif">
+  <value val="3.3"/>
+</parameter>
+
+<parameter name="check_background">
+  <value val="1.0"/>
+</parameter>
+
+<parameter name="bkgd_threshold">
+  <value val="5.0"/>
+</parameter>
+
+<parameter name="variation">
+  <value val="1.1"/>
 </parameter>
 
 </component-link>
diff --git a/Code/Mantid/instrument/MAPS_Parameters.xml b/Code/Mantid/instrument/MAPS_Parameters.xml
index 7c032442ec3..513843b00fe 100644
--- a/Code/Mantid/instrument/MAPS_Parameters.xml
+++ b/Code/Mantid/instrument/MAPS_Parameters.xml
@@ -73,45 +73,80 @@
   <value val="50.9415"/>
 </parameter>
 
-<!-- Diagnostic test defaults -->
+<!-- Diagnostic defaults -->
 
-<parameter name="remove_zero">
-  <value val="1.0"/>
+<!-- Perform diag by bank. These are the spectrum numbers -->
+<parameter name="diag_spectra" type="string">
+  <value val="(1,17280);(17281,18432);(18433,32256);(32257,41472)"/>
 </parameter>
 
-<parameter name="tiny">
+<!-- Absolute lo threshold for vanadium diag (tiny) -->
+<parameter name="diag_tiny">
   <value val="1e-10"/>
 </parameter>
 
-<parameter name="large">
+<!-- Absolute hi threshold for vanadium diag (huge) -->
+<parameter name="diag_huge">
   <value val="1e10"/>
 </parameter>
 
-<parameter name="median_lo">
-  <value val="0.1"/>
+<!-- Remove zeroes in background (s_zero)-->
+<parameter name="diag_samp_zero">
+  <value val="0.0"/>
 </parameter>
 
-<parameter name="median_hi">
-  <value val="3.0"/>
+<!-- Fraction of median to consider counting low for the white beam diag (sv_lo)-->
+<parameter name="diag_samp_lo">
+  <value val="0.0"/>
 </parameter>
 
-<parameter name="signif">
+<!-- Fraction of median to consider counting high for the white beam diag (sv_hi)-->
+<parameter name="diag_samp_hi">
+  <value val="1.5"/>
+</parameter>
+
+<!-- Error criterion as a multiple of error bar for background (sv_sig) -->
+<parameter name="diag_samp_sig">
   <value val="3.3"/>
 </parameter>
 
-<parameter name="check_background">
-  <value val="1.0"/>
+<!-- Lower bound defining outliers as fraction of median value (v_out_lo)-->
+<parameter name="diag_van_out_lo">
+  <value val="0.01"/>
 </parameter>
 
-<parameter name="bkgd_threshold">
-  <value val="5.0"/>
+<!-- Upper bound defining outliers as fraction of median value (v_out_hi)-->
+<parameter name="diag_van_out_hi">
+  <value val="100."/>
 </parameter>
 
-<parameter name="variation">
-  <value val="1.1"/>
+<!-- Fraction of median to consider counting low for the white beam diag (vv_lo)-->
+<parameter name="diag_van_lo">
+  <value val="0.1"/>
+</parameter>
+
+<!-- Fraction of median to consider counting high for the white beam diag (vv_hi)-->
+<parameter name="diag_van_hi">
+  <value val="2.0"/>
+</parameter>
+
+<!-- Error criterion as a multiple of error bar for background (vv_sig) -->
+<parameter name="diag_van_sig">
+  <value val="0.0"/>
+</parameter>
+
+<!-- True if background is to be checked -->
+<parameter name="check_background">
+  <value val="1.0"/>
+</parameter>
+
+<!-- True if the bleed tests should be run -->
+<parameter name="diag_bleed_test">
+  <value val="0.0"/>
 </parameter>
 
 <!--  -->
+
 <!-- Absolute units conversion average -->
 
 <parameter name="monovan_lo_bound">
@@ -130,12 +165,40 @@
   <value val="1.2"/>
 </parameter>
 
-<!--  -->
-<!-- PSD bleed test -->
-<!-- PSD bleed test -->
+<!-- -->
 
-<parameter name="bleed_test">
-  <value val="0.0"/>
+<!-- To be removed -->
+
+<parameter name="remove_zero">
+  <value val="1.0"/>
+</parameter>
+
+<parameter name="tiny">
+  <value val="1e-10"/>
+</parameter>
+
+<parameter name="large">
+  <value val="1e10"/>
+</parameter>
+
+<parameter name="median_lo">
+  <value val="0.1"/>
+</parameter>
+
+<parameter name="median_hi">
+  <value val="3.0"/>
+</parameter>
+
+<parameter name="signif">
+  <value val="3.3"/>
+</parameter>
+
+<parameter name="bkgd_threshold">
+  <value val="5.0"/>
+</parameter>
+
+<parameter name="variation">
+  <value val="1.1"/>
 </parameter>
 
 </component-link>
diff --git a/Code/Mantid/instrument/MARI_Parameters.xml b/Code/Mantid/instrument/MARI_Parameters.xml
index 3b5ff47f5f4..c325342fc20 100644
--- a/Code/Mantid/instrument/MARI_Parameters.xml
+++ b/Code/Mantid/instrument/MARI_Parameters.xml
@@ -72,42 +72,71 @@
   <value val="50.9415"/>
 </parameter>
 
-<!-- Diagnostic test defaults -->
+<!-- Diagnostic defaults -->
 
-<parameter name="remove_zero">
-  <value val="1.0"/>
-</parameter>
-
-<parameter name="tiny">
+<!-- Absolute lo threshold for vanadium diag (tiny) -->
+<parameter name="diag_tiny">
   <value val="1e-10"/>
 </parameter>
 
-<parameter name="large">
+<!-- Absolute hi threshold for vanadium diag (huge) -->
+<parameter name="diag_huge">
   <value val="1e10"/>
 </parameter>
 
-<parameter name="median_lo">
+<!-- Remove zeroes in background (s_zero)-->
+<parameter name="diag_samp_zero">
+  <value val="0.0"/>
+</parameter>
+
+<!-- Fraction of median to consider counting low for the white beam diag (sv_lo)-->
+<parameter name="diag_samp_lo">
+  <value val="0.0"/>
+</parameter>
+
+<!-- Fraction of median to consider counting high for the white beam diag (sv_hi)-->
+<parameter name="diag_samp_hi">
+  <value val="2.0"/>
+</parameter>
+
+<!-- Error criterion as a multiple of error bar for background (sv_sig) -->
+<parameter name="diag_samp_sig">
+  <value val="3.3"/>
+</parameter>
+
+<!-- Lower bound defining outliers as fraction of median value (v_out_lo)-->
+<parameter name="diag_van_out_lo">
+  <value val="0.01"/>
+</parameter>
+
+<!-- Upper bound defining outliers as fraction of median value (v_out_hi)-->
+<parameter name="diag_van_out_hi">
+  <value val="100."/>
+</parameter>
+
+<!-- Fraction of median to consider counting low for the white beam diag (vv_lo)-->
+<parameter name="diag_van_lo">
   <value val="0.1"/>
 </parameter>
 
-<parameter name="median_hi">
+<!-- Fraction of median to consider counting high for the white beam diag (vv_hi)-->
+<parameter name="diag_van_hi">
   <value val="3.0"/>
 </parameter>
 
-<parameter name="signif">
-  <value val="3.3"/>
+<!-- Error criterion as a multiple of error bar for background (vv_sig) -->
+<parameter name="diag_van_sig">
+  <value val="0.0"/>
 </parameter>
 
+<!-- True if background is to be checked -->
 <parameter name="check_background">
   <value val="1.0"/>
 </parameter>
 
-<parameter name="bkgd_threshold">
-  <value val="5.0"/>
-</parameter>
-
-<parameter name="variation">
-  <value val="1.1"/>
+<!-- True if the bleed tests should be run -->
+<parameter name="diag_bleed_test">
+  <value val="0.0"/>
 </parameter>
 
 <!--  -->
@@ -130,10 +159,39 @@
 </parameter>
 
 <!--  -->
-<!-- PSD bleed test -->
 
-<parameter name="bleed_test">
-  <value val="0.0"/>
+<!-- To be removed -->
+
+<parameter name="remove_zero">
+  <value val="1.0"/>
+</parameter>
+
+<parameter name="large">
+  <value val="1e10"/>
+</parameter>
+
+<parameter name="median_lo">
+  <value val="0.1"/>
+</parameter>
+
+<parameter name="median_hi">
+  <value val="3.0"/>
+</parameter>
+
+<parameter name="signif">
+  <value val="3.3"/>
+</parameter>
+
+<parameter name="check_background">
+  <value val="1.0"/>
+</parameter>
+
+<parameter name="bkgd_threshold">
+  <value val="5.0"/>
+</parameter>
+
+<parameter name="variation">
+  <value val="1.1"/>
 </parameter>
 
 </component-link>
diff --git a/Code/Mantid/instrument/MERLIN_Parameters.xml b/Code/Mantid/instrument/MERLIN_Parameters.xml
index 31b46cfb72f..4cbb1d8bee3 100644
--- a/Code/Mantid/instrument/MERLIN_Parameters.xml
+++ b/Code/Mantid/instrument/MERLIN_Parameters.xml
@@ -73,42 +73,81 @@
   <value val="50.9415"/>
 </parameter>
 
-<!-- Diagnostic test defaults -->
-
-<parameter name="remove_zero">
-  <value val="1.0"/>
-</parameter>
+<!-- Diagnostic defaults -->
 
-<parameter name="tiny">
+<!-- Absolute lo threshold for vanadium diag (tiny) -->
+<parameter name="diag_tiny">
   <value val="1e-10"/>
 </parameter>
 
-<parameter name="large">
+<!-- Absolute hi threshold for vanadium diag (huge) -->
+<parameter name="diag_huge">
   <value val="1e10"/>
 </parameter>
 
-<parameter name="median_lo">
-  <value val="0.1"/>
+<!-- Remove zeroes in background (s_zero)-->
+<parameter name="diag_samp_zero">
+  <value val="0.0"/>
 </parameter>
 
-<parameter name="median_hi">
-  <value val="3.0"/>
+<!-- Fraction of median to consider counting low for the white beam diag (sv_lo)-->
+<parameter name="diag_samp_lo">
+  <value val="0.0"/>
 </parameter>
 
-<parameter name="signif">
+<!-- Fraction of median to consider counting high for the white beam diag (sv_hi)-->
+<parameter name="diag_samp_hi">
+  <value val="1.5"/>
+</parameter>
+
+<!-- Error criterion as a multiple of error bar for background (sv_sig) -->
+<parameter name="diag_samp_sig">
   <value val="3.3"/>
 </parameter>
 
+<!-- Lower bound defining outliers as fraction of median value (v_out_lo)-->
+<parameter name="diag_van_out_lo">
+  <value val="0.01"/>
+</parameter>
+
+<!-- Upper bound defining outliers as fraction of median value (v_out_hi)-->
+<parameter name="diag_van_out_hi">
+  <value val="100."/>
+</parameter>
+
+<!-- Fraction of median to consider counting low for the white beam diag (vv_lo)-->
+<parameter name="diag_van_lo">
+  <value val="0.1"/>
+</parameter>
+
+<!-- Fraction of median to consider counting high for the white beam diag (vv_hi)-->
+<parameter name="diag_van_hi">
+  <value val="2.0"/>
+</parameter>
+
+<!-- Error criterion as a multiple of error bar for background (vv_sig) -->
+<parameter name="diag_van_sig">
+  <value val="0.0"/>
+</parameter>
+
+<!-- True if background is to be checked -->
 <parameter name="check_background">
   <value val="1.0"/>
 </parameter>
 
-<parameter name="bkgd_threshold">
-  <value val="5.0"/>
+<!-- True if the bleed tests should be run -->
+<parameter name="diag_bleed_test">
+  <value val="1.0"/>
 </parameter>
 
-<parameter name="variation">
-  <value val="1.1"/>
+<!-- The maximum framerate to allow in a tube -->
+<parameter name="diag_bleed_maxrate">
+  <value val="0.01"/>
+</parameter>
+
+<!-- The number of pixels to ignore around the centre of the tube -->
+<parameter name="diag_bleed_pixels">
+  <value val="80"/>
 </parameter>
 
 <!--  -->
@@ -131,20 +170,48 @@
 </parameter>
 
 <!--  -->
-<!-- PSD bleed test -->
 
-<parameter name="bleed_test">
+<!-- To be removed -->
+<!-- Diagnostic test defaults -->
+
+<parameter name="remove_zero">
   <value val="1.0"/>
 </parameter>
 
-<parameter name="bleed_max_framerate">
-  <value val="0.01"/>
+<parameter name="tiny">
+  <value val="1e-10"/>
 </parameter>
 
-<parameter name="bleed_ignored_pixels">
-  <value val="80"/>
+<parameter name="large">
+  <value val="1e10"/>
 </parameter>
 
+<parameter name="median_lo">
+  <value val="0.1"/>
+</parameter>
+
+<parameter name="median_hi">
+  <value val="3.0"/>
+</parameter>
+
+<parameter name="signif">
+  <value val="3.3"/>
+</parameter>
+
+<parameter name="check_background">
+  <value val="1.0"/>
+</parameter>
+
+<parameter name="bkgd_threshold">
+  <value val="5.0"/>
+</parameter>
+
+<parameter name="variation">
+  <value val="1.1"/>
+</parameter>
+
+
+
 </component-link>
 
 </parameter-file>
diff --git a/Code/Mantid/instrument/SEQUOIA_Parameters.xml b/Code/Mantid/instrument/SEQUOIA_Parameters.xml
index def201e0981..1dc31bdde0d 100644
--- a/Code/Mantid/instrument/SEQUOIA_Parameters.xml
+++ b/Code/Mantid/instrument/SEQUOIA_Parameters.xml
@@ -39,6 +39,73 @@
 
 		<!-- Diagnostic test defaults -->
 
+		<!-- Absolute lo threshold for vanadium diag (tiny) -->
+		<parameter name="diag_tiny">
+		  <value val="1e-10"/>
+		</parameter>
+
+		<!-- Absolute hi threshold for vanadium diag (huge) -->
+		<parameter name="diag_huge">
+		  <value val="1e10"/>
+		</parameter>
+
+		<!-- Remove zeroes in background (s_zero)-->
+		<parameter name="diag_samp_zero">
+		  <value val="1.0"/>
+		</parameter>
+
+		<!-- Fraction of median to consider counting low for the white beam diag (sv_lo)-->
+		<parameter name="diag_samp_lo">
+		  <value val="0.0"/>
+		</parameter>
+
+		<!-- Fraction of median to consider counting high for the white beam diag (sv_hi)-->
+		<parameter name="diag_samp_hi">
+		  <value val="5.0"/>
+		</parameter>
+
+		<!-- Error criterion as a multiple of error bar for background (sv_sig) -->
+		<parameter name="diag_samp_sig">
+		  <value val="3.3"/>
+		</parameter>
+
+		<!-- Lower bound defining outliers as fraction of median value (v_out_lo)-->
+		<parameter name="diag_van_out_lo">
+		  <value val="0.01"/>
+		</parameter>
+
+		<!-- Upper bound defining outliers as fraction of median value (v_out_hi)-->
+		<parameter name="diag_van_out_hi">
+		  <value val="100."/>
+		</parameter>
+
+		<!-- Fraction of median to consider counting low for the white beam diag (vv_lo)-->
+		<parameter name="diag_van_lo">
+		  <value val="0.1"/>
+		</parameter>
+
+		<!-- Fraction of median to consider counting high for the white beam diag (vv_hi)-->
+		<parameter name="diag_van_hi">
+		  <value val="3.0"/>
+		</parameter>
+
+		<!-- Error criterion as a multiple of error bar for background (vv_sig) -->
+		<parameter name="diag_van_sig">
+		  <value val="3.3"/>
+		</parameter>
+
+		<!-- True if background is to be checked -->
+		<parameter name="check_background">
+		  <value val="1.0"/>
+		</parameter>
+
+		<!-- True if the bleed tests should be run -->
+		<parameter name="diag_bleed_test">
+		  <value val="0.0"/>
+		</parameter>
+
+		<!-- TO BE REMOVED Diagnostic test defaults -->
+
 		<parameter name="remove_zero">
 			<value val="1.0" />
 		</parameter>
@@ -95,12 +162,6 @@
 		</parameter>
 
 		<!-- -->
-		<!-- PSD bleed test -->
-
-		<parameter name="bleed_test">
-			<value val="0.0" />
-		</parameter>
-
 
 		<!-- All the following parameters are taken directly from the MARI definition 
 			and are WRONG! They are only here for now to get things working -->
diff --git a/Code/Mantid/scripts/Inelastic/DirectEnergyConversion.py b/Code/Mantid/scripts/Inelastic/DirectEnergyConversion.py
index 815f0ffd1ec..f8ccd459ff8 100755
--- a/Code/Mantid/scripts/Inelastic/DirectEnergyConversion.py
+++ b/Code/Mantid/scripts/Inelastic/DirectEnergyConversion.py
@@ -44,7 +44,7 @@ def setup_reducer(inst_name):
     """
     try:
         return DirectEnergyConversion(inst_name)
-    except RuntimeError:
+    except RuntimeError, exc:
         raise RuntimeError('Unknown instrument "%s", cannot continue' % inst_name)
     
 
@@ -53,65 +53,123 @@ class DirectEnergyConversion(object):
     Performs a convert to energy assuming the provided instrument is an elastic instrument
     """
 
-    def diagnose(self, white_run, sample_run=None, other_white=None, remove_zero=None, 
-                 tiny=None, large=None, median_lo=None, median_hi=None, signif=None, 
-                 bkgd_threshold=None, bkgd_range=None, variation=None,
-                 bleed_test=False, bleed_maxrate=None, bleed_pixels=None,
-                 hard_mask=None, print_results=False):
+    def diagnose(self, white, **kwargs):
         """
-        A pass through method to the 'real' one in diagnostics.py
-
-        Run diagnostics on the provided run and white beam files.
-
-        There are 4 possible tests, depending on the input given:
-          White beam diagnosis
-          Background tests
-          Second white beam
-          PSD bleed test
+            Run diagnostics on the provided workspaces.
             
-        Required inputs:
-        
-          white_run  - The run number or filepath of the white beam run
-        
-        Optional inputs:
-          sample_run - The run number or filepath of the sample run for the background test (default = None)
-          other_white   - If provided an addional set of tests is performed on this file. (default = None)
-          remove_zero - If true then zeroes in the data will count as failed (default = False)
-          tiny          - Minimum threshold for acceptance (default = 1e-10)
-          large         - Maximum threshold for acceptance (default = 1e10)
-          median_lo     - Fraction of median to consider counting low (default = 0.1)
-          median_hi     - Fraction of median to consider counting high (default = 3.0)
-          signif        - Counts within this number of multiples of the 
-                          standard dev will be kept (default = 3.3)
-          bkgd_threshold - High threshold for background removal in multiples of median (default = 5.0)
-          bkgd_range - The background range as a list of 2 numbers: [min,max]. 
-                       If not present then they are taken from the parameter file. (default = None)
-          variation  - The number of medians the ratio of the first/second white beam can deviate from
-                       the average by (default=1.1)
-          bleed_test - If true then the CreatePSDBleedMask algorithm is run
-          bleed_maxrate - If the bleed test is on then this is the maximum framerate allowed in a tube
-          bleed_pixels - If the bleed test is on then this is the number of pixels ignored within the
-                         bleed test diagnostic
-          hard_mask  - A file specifying those spectra that should be masked without testing
-          print_results - If True then the results are printed to std out
-          inst_name  - The name of the instrument to perform the diagnosis.
-                       If it is not provided then the default instrument is used (default = None)
-        """
+            This method does some additional processing before moving on to the diagnostics:
+              1) Computes the white beam integrals, converting to energy
+              2) Computes the background integral using the instrument defined range
+              3) Computes a total count from the sample
+              
+            These inputs are passed to the diagnostics functions
+    
+            Required inputs:
+            
+              white  - A workspace, run number or filepath of a white beam run. A workspace is assumed to
+                       have simple been loaded and nothing else.
+            
+            Optional inputs:
+              sample - A workspace, run number or filepath of a sample run. A workspace is assumed to
+                       have simple been loaded and nothing else. (default = None)
+              second_white - If provided an additional set of tests is performed on this. (default = None)
+              hard_mask  - A file specifying those spectra that should be masked without testing (default=None)
+              tiny        - Minimum threshold for acceptance (default = 1e-10)
+              huge        - Maximum threshold for acceptance (default = 1e10)
+              bkgd_range - A list of two numbers indicating the background range (default=instrument defaults)
+              van_out_lo  - Lower bound defining outliers as fraction of median value (default = 0.01)
+              van_out_hi  - Upper bound defining outliers as fraction of median value (default = 100.)
+              van_lo      - Fraction of median to consider counting low for the white beam diag (default = 0.1)
+              van_hi      - Fraction of median to consider counting high for the white beam diag (default = 1.5)
+              van_sig  - Error criterion as a multiple of error bar i.e. to fail the test, the magnitude of the\n"
+                          "difference with respect to the median value must also exceed this number of error bars (default=0.0)
+              samp_zero    - If true then zeroes in the vanadium data will count as failed (default = True)
+              samp_lo      - Fraction of median to consider counting low for the white beam diag (default = 0)
+              samp_hi      - Fraction of median to consider counting high for the white beam diag (default = 2.0)
+              samp_sig  - Error criterion as a multiple of error bar i.e. to fail the test, the magnitude of the\n"
+                          "difference with respect to the median value must also exceed this number of error bars (default=3.3)
+              variation  - The number of medians the ratio of the first/second white beam can deviate from
+                           the average by (default=1.1)
+              bleed_test - If true then the CreatePSDBleedMask algorithm is run
+              bleed_maxrate - If the bleed test is on then this is the maximum framerate allowed in a tube
+              bleed_pixels - If the bleed test is on then this is the number of pixels ignored within the
+                             bleed test diagnostic
+              print_results - If True then the results are printed to the screen
+        """
+        if kwargs.get('second_white', None) is not None:
+            raise RuntimeError("Diagnostic does not support second white beam run yet")
+
         lhs_names = lhs_info('names')
         if len(lhs_names) > 0:
             var_name = lhs_names[0]
         else:
             var_name = None
-          
-        __diagnostic_mask = diagnostics.diagnose(white_run, sample_run, other_white, remove_zero,
-                                        tiny, large, median_lo, median_hi, signif,
-                                        bkgd_threshold, bkgd_range, variation,
-                                        bleed_test, bleed_maxrate, bleed_pixels,                                              
-                                        hard_mask, print_results, self.instr_name)
-        if var_name is not None:
-            result = RenameWorkspace(str(__diagnostic_mask), var_name).workspace()
+
+        # Check for any keywords that have not been supplied and put in the defaults
+        for par in self.diag_params:
+            arg = par.lstrip('diag_')
+            if arg not in kwargs:
+                kwargs[arg] = getattr(self, par)
+        
+        # Get the white beam vanadium integrals
+        whiteintegrals = self.do_white(white, None, None) # No grouping yet
+        # Get the background/total counts from the sample if present
+        if 'sample' in kwargs:
+            sample = kwargs['sample']
+            del kwargs['sample']
+            # If the bleed test is requested then we need to pass in the sample_run as well
+            if kwargs.get('bleed_test', False):
+                kwargs['sample_run'] = sample
+            
+            # Set up the background integrals
+            result_ws = common.load_runs(sample)
+            self.normalise(result_ws, result_ws, self.normalise_method)
+            if 'bkgd_range' in kwargs:
+                bkgd_range = kwargs['bkgd_range']
+                del kwargs['bkgd_range']
+            else:
+                bkgd_range = self.background_range
+            background_int = Integration(result_ws, OutputWorkspace='background_int',\
+                                         RangeLower=bkgd_range[0],RangeUpper=bkgd_range[1], \
+                                         IncludePartialBins=True).workspace()
+            total_counts = Integration(result_ws, OutputWorkspace='total_counts', IncludePartialBins=True).workspace()
+            background_int = ConvertUnits(background_int, background_int, "Energy", AlignBins=0).workspace()
+            background_int *= 1.7016e8
+            background_int /= whiteintegrals
+            kwargs['background_int'] = background_int
+            kwargs['sample_counts'] = total_counts
+            
+        # Check how we should run diag
+        if self.diag_spectra is None:
+            # Do the whole lot at once
+            diagnostics.diagnose(whiteintegrals, **kwargs)
+        else:
+            banks = self.diag_spectra.split(";")
+            bank_spectra = []
+            for b in banks:
+                token = b.split(",")  # b = "(,)"
+                if len(token) != 2: 
+                    raise ValueError("Invalid bank spectra specification in diag %s" % self.diag_spectra)
+                start = int(token[0].lstrip('('))
+                end = int(token[1].rstrip(')'))
+                bank_spectra.append((start,end))
+            
+            for index, bank in enumerate(bank_spectra):
+                kwargs['start_index'] = bank[0] - 1
+                kwargs['end_index'] = bank[1] - 1
+                diagnostics.diagnose(whiteintegrals, **kwargs)
+                
+        if 'sample' in kwargs:
+            DeleteWorkspace('background_int')
+            DeleteWorkspace('total_counts')
+        # Return a mask workspace
+        diag_mask = ExtractMasking(whiteintegrals, OutputWorkspace='diag_mask').workspace()
+        DeleteWorkspace(whiteintegrals)
+
+        if var_name is not None and var_name != str(diag_mask):
+            result = RenameWorkspace(str(diag_mask), var_name).workspace()
         else:
-            result = __diagnostic_mask
+            result = diag_mask
         self.spectra_masks = result
         return result
     
@@ -495,13 +553,13 @@ class DirectEnergyConversion(object):
         
         ConvertToMatrixWorkspace(data_ws, data_ws)
         
-        min_value = self.tiny
-        max_value = self.large
+        min_value = self.diag_tiny
+        max_value = self.diag_huge
         median_lbound = self.monovan_lo_bound
         median_ubound = self.monovan_hi_bound
         median_frac_low = self.monovan_lo_frac
         median_frac_hi = self.monovan_hi_frac
-        median_sig = self.signif
+        median_sig = self.diag_samp_sig
 
         self.mask_detectors_outside_range(data_ws, min_value, max_value, median_lbound,
                                           median_ubound, median_frac_low, median_frac_hi, median_sig)
@@ -728,13 +786,34 @@ class DirectEnergyConversion(object):
         self.van_mass = self.get_default_parameter("vanadium-mass")
         self.van_rmm = self.get_default_parameter("vanadium-rmm")
 
-        self.tiny = self.get_default_parameter('tiny')
-        self.large = self.get_default_parameter('large')
+        # Diag 
+        self.diag_params = ['diag_tiny', 'diag_huge', 'diag_samp_zero', 'diag_samp_lo', 'diag_samp_hi','diag_samp_sig',\
+                            'diag_van_out_lo', 'diag_van_out_hi', 'diag_van_lo', 'diag_van_hi', 'diag_van_sig']
+        # Add an attribute for each of them
+        for par in self.diag_params:
+            setattr(self, par, self.get_default_parameter(par))
+            
+        # Bleed options
+        try:
+            self.diag_bleed_test = self.get_default_parameter('diag_bleed_test')
+            self.diag_bleed_maxrate = self.get_default_parameter('diag_bleed_maxrate')
+            self.diag_bleed_pixels = self.get_default_parameter('diag_bleed_pixels')
+            self.diag_params.extend(['diag_bleed_maxrate','diag_bleed_pixels'])
+        except ValueError:
+            self.diag_bleed_test = False
+        self.diag_params.append('diag_bleed_test')
+
+        # Do we have specified spectra to diag over
+        try:
+            self.diag_spectra = self.instrument.getStringParameter("diag_spectra")[0]
+        except Exception:
+            self.diag_spectra = None
+        
+        # Absolute units
         self.monovan_lo_bound = self.get_default_parameter('monovan_lo_bound')
         self.monovan_hi_bound = self.get_default_parameter('monovan_hi_bound')
         self.monovan_lo_frac = self.get_default_parameter('monovan_lo_frac')
         self.monovan_hi_frac = self.get_default_parameter('monovan_hi_frac')
-        self.signif = self.get_default_parameter('signif')
 
         # Mark IDF files as read
         self._idf_values_read = True
@@ -755,4 +834,4 @@ class DirectEnergyConversion(object):
         if self._log_to_mantid:
             mtd.sendLogMessage(msg)
 
-#-----------------------------------------------------------------
\ No newline at end of file
+#-----------------------------------------------------------------
diff --git a/Code/Mantid/scripts/Inelastic/diagnostics.py b/Code/Mantid/scripts/Inelastic/diagnostics.py
index 13e82bade91..b1c1bb03176 100644
--- a/Code/Mantid/scripts/Inelastic/diagnostics.py
+++ b/Code/Mantid/scripts/Inelastic/diagnostics.py
@@ -11,200 +11,173 @@ masking and also passed to MaskDetectors to match masking there.
 from mantidsimple import *
 import CommonFunctions as common
 
-def diagnose(white_run, sample_run=None, other_white=None, remove_zero=None, 
-             tiny=None, large=None, median_lo=None, median_hi=None, signif=None, 
-             bkgd_threshold=None, bkgd_range=None, variation=None,
-             bleed_test=False, bleed_maxrate=None, bleed_pixels=None,
-             hard_mask=None, print_results=False, inst_name=None):
-    """
-    Run diagnostics on the provided run and white beam files.
-
-    There are 4 possible tests, depending on the input given:
-      White beam diagnosis
-      Background tests
-      Second white beam
-      Bleed test
-    
-    Required inputs:
+class ArgumentParser(object):
     
-      white_run  - The run number or filepath of the white beam run
-    
-    Optional inputs:
-      sample_run - The run number or filepath of the sample run for the background test (default = None)
-      other_white   - If provided an addional set of tests is performed on this file. (default = None)
-      remove_zero - If true then zeroes in the data will count as failed (default = False)
-      tiny          - Minimum threshold for acceptance (default = 1e-10)
-      large         - Maximum threshold for acceptance (default = 1e10)
-      median_lo     - Fraction of median to consider counting low (default = 0.1)
-      median_hi     - Fraction of median to consider counting high (default = 3.0)
-      signif           - Counts within this number of multiples of the 
-                      standard dev will be kept (default = 3.3)
-      bkgd_threshold - High threshold for background removal in multiples of median (default = 5.0)
-      bkgd_range - The background range as a list of 2 numbers: [min,max]. 
-                   If not present then they are taken from the parameter file. (default = None)
-      variation  - The number of medians the ratio of the first/second white beam can deviate from
-                   the average by (default=1.1)
-      bleed_test - If true then the CreatePSDBleedMask algorithm is run
-      bleed_maxrate - If the bleed test is on then this is the maximum framerate allowed in a tube
-      bleed_pixels - If the bleed test is on then this is the number of pixels ignored within the
-                     bleed test diagnostic
-      hard_mask  - A file specifying those spectra that should be masked without testing
-      print_results - If True then the results are printed to std out
-      inst_name  - The name of the instrument to perform the diagnosis.
-                   If it is not provided then the default instrument is used (default = None)
+    def __init__(self, keywords):
+        self.start_index = None # Make this more general for anything that is missing!
+        self.end_index = None
+        for key, value in keywords.iteritems():
+            setattr(self, key, value)
+            
+        
+
+def diagnose(white_int, **kwargs):
     """
-    # Set the default instrument so that Mantid can search for the runs correctly, 
-    # but switch it back at the end
-    def_inst = mtd.settings["default.instrument"]
-    if inst_name == None:
-        inst_name = def_inst
-    elif inst_name != def_inst:
-        mtd.settings["default.instrument"] = inst_name
-    else: pass
+        Run diagnostics on the provided workspaces.
 
+        Required inputs:
+        
+          white_int  - A workspace, run number or filepath of a white beam run. If a run/file is given it
+                       simply loaded and integrated. A workspace is assumed to be prepared in it's integrated form
+        
+        Optional inputs:
+          start_index    - The index to start the diag
+          end_index    - The index to finish the diag
+          background_int - A workspace, run number or filepath of a sample run that has been integrated over the background region.
+                          If a run/file is given it simply loaded and integrated across the whole range
+          sample_counts - A workspace containing the total integrated counts from a sample run
+          second_white - If provided an additional set of tests is performed on this.
+          hard_mask  - A file specifying those spectra that should be masked without testing 
+          tiny        - Minimum threshold for acceptance 
+          huge       - Maximum threshold for acceptance
+          van_out_lo  - Lower bound defining outliers as fraction of median value 
+          van_out_hi  - Upper bound defining outliers as fraction of median value 
+          van_lo      - Fraction of median to consider counting low for the white beam diag 
+          van_hi      - Fraction of median to consider counting high for the white beam diag 
+          van_sig     - Error criterion as a multiple of error bar i.e. to fail the test, the magnitude of the
+                        difference with respect to the median value must also exceed this number of error bars 
+          samp_zero    - If true then zeroes in background are masked also
+          samp_lo      - Fraction of median to consider counting low for the background  diag 
+          samp_hi      - Fraction of median to consider counting high for the background diag
+          samp_sig     - Error criterion as a multiple of error bar i.e. to fail the test, the magnitude of the\n"
+                      "difference with respect to the median value must also exceed this number of error bars
+          variation  - The number of medians the ratio of the first/second white beam can deviate from
+                       the average by 
+          bleed_test - If true then the CreatePSDBleedMask algorithm is run
+          bleed_maxrate - If the bleed test is on then this is the maximum framerate allowed in a tube
+          bleed_pixels - If the bleed test is on then this is the number of pixels ignored within the
+                         bleed test diagnostic
+          print_results - If True then the results are printed to the screen
+    """
+    # Grab the arguments
+    parser = ArgumentParser(kwargs)
+    start_index = parser.start_index
+    end_index = parser.end_index
+    
     # Load the hard mask file if necessary
     hard_mask_spectra = ''
-    if hard_mask is not None:
+    if kwargs.get('hard_mask', None) is not None:
         hard_mask_spectra = common.load_mask(hard_mask)
 
     # Map the test number to the results
     # Each element is the mask workspace name then the number of failures
     test_results = [ [None, None], [None, None], [None, None], [None, None]]
-
-    ##
-    ## Accumulate the masking on this workspace
-    ##
-    # What shall we call the output
-    lhs_names = lhs_info('names')
-    if len(lhs_names) > 0:
-        diag_total_mask = lhs_names[0]
-    else:
-        diag_total_mask = 'diag_total_mask'
+    
 
     ##
     ## White beam Test
     ##
     white_counts = None
-    if white_run is not None and str(white_run) != '':
-        # Load and integrate
-        data_ws = common.load_run(white_run)
-        # Make sure we have a matrix workspace otherwise Integration() returns all zeros.
-        ConvertToMatrixWorkspace(data_ws, data_ws)
-        white_counts = Integration(data_ws, "__counts_white-beam").workspace()
-        MaskDetectors(white_counts, SpectraList=hard_mask_spectra)
+    if white_int is not None and str(white_int) != '':
+        # Hard mask
+        MaskDetectors(white_int, SpectraList=hard_mask_spectra)
         # Run first white beam tests
-        __white_masks, num_failed = \
-                      _do_white_test(white_counts, tiny, large, median_lo, median_hi, signif)
+        __white_masks, num_failed = _do_white_test(white_int, parser.tiny, parser.huge, 
+                                                   parser.van_out_lo, parser.van_out_hi,
+                                                   parser.van_lo, parser.van_hi, 
+                                                   parser.van_sig, start_index, end_index)
         test_results[0] = [str(__white_masks), num_failed]
-        diag_total_mask = CloneWorkspace(__white_masks, diag_total_mask).workspace()
+        _add_masking(white_int, __white_masks, start_index, end_index)
+        DeleteWorkspace(__white_masks)
     else:
         raise RuntimeError('Invalid input for white run "%s"' % str(white_run))
 
-    ##
-    ## Second white beam Test
-    ##
-    second_white_counts = None
-    if other_white is not None and str(other_white) != '':
-        # Load and integrate
-        data_ws = common.load_run(other_white)
-        second_white_counts = Integration(data_ws, "__counts_white-beam2").workspace()
-        MaskDetectors(white_counts, SpectraList=hard_mask_spectra)
-        # Run tests
-        __second_white_masks, num_failed = \
-                             _do_second_white_test(white_counts, second_white_counts,
-                                                   tiny, large, median_lo, median_hi,
-                                                   signif,variation)
-        test_results[1] = [str(__second_white_masks), num_failed]
-        # Accumulate
-        diag_total_mask += __second_white_masks
-
-    ##
-    ## Tests on the sample(s)
-    ##
-    if sample_run is not None and sample_run != '':
-        if type(sample_run) != list:
-            sample_run = [sample_run]
-        __bkgd_masks = None
-        __bleed_masks = None
-        bkgd_failures = 0
-        bleed_failures = 0
-        for sample in sample_run:
-            ## Background test
-            __bkgd_masks_i, failures = _do_background_test(sample, white_counts, second_white_counts,
-                                                           bkgd_range, bkgd_threshold, remove_zero, signif,
-                                                           hard_mask_spectra)
-            bkgd_failures += failures
-            __bkgd_masks = _accumulate(__bkgd_masks,__bkgd_masks_i)
-                
-            ## PSD Bleed Test
-            if type(bleed_test) == bool and bleed_test == True:
-                __bleed_masks_i, failures = _do_bleed_test(sample, bleed_maxrate, bleed_pixels)
-                bleed_failures += failures
-                __bleed_masks = _accumulate(__bleed_masks, __bleed_masks_i)
-
-        ## End of sample loop
-        test_results[2] = [str(__bkgd_masks), bkgd_failures]
-        diag_total_mask += __bkgd_masks
-        if __bleed_masks is not None:
-            test_results[3] = [str(__bleed_masks), bleed_failures]
-            diag_total_mask += __bleed_masks
-
-    ## End of background test
-
-    # Remove temporary workspaces
-    mtd.deleteWorkspace(str(white_counts))
-    if second_white_counts is not None:
-        mtd.deleteWorkspace(str(second_white_counts))
+    #
+    # Zero total count check for sample counts
+    #
+    if hasattr(parser, 'sample_counts'):
+        _add_masking(parser.sample_counts, white_int)
+        maskZero = FindDetectorsOutsideLimits(InputWorkspace=parser.sample_counts, OutputWorkspace='maskZero',
+                                              StartWorkspaceIndex=start_index, EndWorkspaceIndex=end_index,
+                                              LowThreshold=1e-10, HighThreshold=1e100).workspace()
+        _add_masking(white_int, maskZero, start_index, end_index)
+        DeleteWorkspace(maskZero)
+        
+    #
+    # Background check
+    #
+    if hasattr(parser, 'background_int'):
+        _add_masking(parser.background_int, white_int)
+        __bkgd_mask, failures = _do_background_test(parser.background_int, parser.samp_lo, 
+                                                    parser.samp_hi, parser.samp_sig, parser.samp_zero, start_index, end_index)
+        _add_masking(white_int, __bkgd_mask, start_index, end_index)
+        DeleteWorkspace(__bkgd_mask)
     
-    # Revert our default instrument changes if necessary
-    if inst_name != def_inst:
-        mtd.settings["default.instrument"] = def_inst
-
-    if print_results:
-        print_test_summary(test_results)
+    #
+    # Bleed test
+    #
+    if hasattr(parser, 'bleed_test') and parser.bleed_test:
+        if not hasattr(parser, 'sample_run'):
+            raise RuntimeError("Bleed test requested but the sample_run keyword has not been provided")
+        _bleed_masks, failures = _do_bleed_test(parser.sample_run, parser.bleed_maxrate, parser.bleed_pixels)
+        _add_masking(white_int, __bleed_masks)
+        DeleteWorkspace(_bleed_masks)
     
-    # This will be a MaskWorkspace which contains the accumulation of all of the masks
-    return diag_total_mask
+    if hasattr(parser, 'print_results') and parser.print_results:
+        print_test_summary(test_results)
 
 #-------------------------------------------------------------------------------
 
-def _do_white_test(white_counts, tiny, large, median_lo, median_hi, signif):
+def _add_masking(input_ws, mask_ws, start_index=None, end_index=None):
+    """
+    Mask the Detectors on the input workspace that are masked 
+    on the mask_ws. Avoids a current bug in using MaskDetectors with a MaskedWorkspace in a loop 
+    """
+    #MaskDetectors(input_ws, MaskedWorkspace=mask_ws, StartWorkspaceIndex=start_index, EndWorkspaceIndex=end_index)
+    masked = ExtractMasking(mask_ws, '__tmp')
+    MaskDetectors(input_ws, DetectorList=masked.getPropertyValue("DetectorList"))
+    DeleteWorkspace(masked.workspace())
+
+#-------------------------------------------------------------------------------
+
+def _do_white_test(white_int, tiny, large, out_lo, out_hi, median_lo, median_hi, sigma, 
+                   start_index=None, end_index=None):
     """
     Run the diagnostic tests on the integrated white beam run
 
     Required inputs:
     
-      white_counts  - A workspace containing the integrated counts from a
-                      white beam vanadium run
-      tiny          - Minimum threshold for acceptance
-      large         - Maximum threshold for acceptance
-      median_lo     - Fraction of median to consider counting low
-      median_hi     - Fraction of median to consider counting high
-      signif        - Counts within this number of multiples of the 
-                      standard dev will be kept (default = 3.3)
+      white_int - An integrated workspace
+      tiny      - Minimum threshold for acceptance
+      large     - Maximum threshold for acceptance
+      out_lo    - Lower bound defining outliers as fraction of median value (default = 0.01)
+      out_hi    - Upper bound defining outliers as fraction of median value (default = 100.)
+      median_lo - Fraction of median to consider counting low
+      median_hi - Fraction of median to consider counting high
+      sigma     - Error criterion as a multiple of error bar
+                      
     """
     mtd.sendLogMessage('Running first white beam test')
 
-    # What shall we call the output
-    lhs_names = lhs_info('names')
-    if len(lhs_names) > 0:
-        ws_name = lhs_names[0]
-    else:
-        ws_name = '__do_white_test'
-
     # Make sure we are a MatrixWorkspace
-    ConvertToMatrixWorkspace(white_counts, white_counts)
+    ConvertToMatrixWorkspace(white_int, white_int)
     
     # The output workspace will have the failed detectors masked
-    range_check = FindDetectorsOutsideLimits(white_counts, ws_name, HighThreshold=large, LowThreshold=tiny)
-    median_test = MedianDetectorTest(white_counts, ws_name, SignificanceTest=signif,
-                                     LowThreshold=median_lo, HighThreshold=median_hi)
-
-    num_failed = range_check['NumberOfFailures'].value + median_test['NumberOfFailures'].value
-
-    maskWS = median_test.workspace()
-    MaskDetectors(white_counts, MaskedWorkspace=maskWS)
-    return maskWS, num_failed
+    white_masks = FindDetectorsOutsideLimits(white_int, OutputWorkspace='white_masks', StartWorkspaceIndex=start_index,
+                                             EndWorkspaceIndex=end_index,
+                                             HighThreshold=large, LowThreshold=tiny)
+    MaskDetectors(white_int, MaskedWorkspace=white_masks.workspace(),StartWorkspaceIndex=start_index, EndWorkspaceIndex=end_index)
+    num_failed = white_masks['NumberOfFailures'].value
+    DeleteWorkspace(white_masks.workspace())
+
+    white_masks = MedianDetectorTest(white_int, OutputWorkspace='white_masks', StartWorkspaceIndex=start_index,
+                                     EndWorkspaceIndex=end_index, SignificanceTest=sigma,
+                                     LowThreshold=median_lo, HighThreshold=median_hi, 
+                                     LowOutlier=out_lo, HighOutlier=out_hi, ExcludeZeroesFromMedian=False)
+    MaskDetectors(white_int, MaskedWorkspace=white_masks.workspace(), StartWorkspaceIndex=start_index, EndWorkspaceIndex=end_index)
+    num_failed += white_masks['NumberOfFailures'].value
+
+    return white_masks.workspace(), num_failed
 
 #-------------------------------------------------------------------------------
 
@@ -259,75 +232,24 @@ def _do_second_white_test(white_counts, comp_white_counts, tiny, large, median_l
 
 #------------------------------------------------------------------------------
 
-def _do_background_test(sample_run, white_counts, comp_white_counts, bkgd_range,
-                        bkgd_threshold, remove_zero, signif, hard_mask_spectra):
+def _do_background_test(background_int, median_lo, median_hi, sigma, mask_zero, 
+                        start_index=None, end_index=None):
     """
-    Run the background tests on the integrated sample run normalised by an
-    integrated white beam run
+    Run the background tests
 
     Required inputs:
-    
-      sample_run          - The run number or filepath of the sample run
-      white_counts        - A workspace containing the integrated counts from a
-                            white beam vanadium run
-      comp_white_counts - A second workspace containing the integrated counts from a
-                            different white beam vanadium run
-      bkgd_range          - The background range as a list of 2 numbers: [min,max]. 
-                            If not present then they are taken from the parameter file.
-      bkgd_threshold      - High threshold for background removal in multiples of median
-      remove_zero         - If true then zeroes in the data will count as failed (default = False)
-      signif              - Counts within this number of multiples of the 
-                            standard dev will be kept (default = 3.3)
-      prev_ma sks         - If present then this masking is applied to the sample run before the
-                            test is applied. It is expected as a MaskWorkspace
+      background_int - An integrated workspace
+      median_lo - Fraction of median to consider counting low
+      median_hi - Fraction of median to consider counting high
+      sigma     - Error criterion as a multiple of error bar
+      mask_zero - If True, zero background counts will be considered a fail
+
     """
     mtd.sendLogMessage('Running background count test')
-    # Load and integrate the sample using the defined background range. If none is given use the
-    # instrument defaults
-    data_ws = common.load_runs(sample_run)
-    instrument = data_ws.getInstrument()
-    if bkgd_range is None:
-        min_value = float(instrument.getNumberParameter('bkgd-range-min')[0])
-        max_value = float(instrument.getNumberParameter('bkgd-range-max')[0])
-    elif len(bkgd_range) != 2:
-        raise ValueError("The provided background range is incorrect. A list of 2 numbers is required.")
-    else:
-        min_value = bkgd_range[0]
-        if min_value is None:
-            float(instrument.getNumberParameter('bkgd-range-min')[0])
-        max_value = bkgd_range[1]
-        if max_value is None:
-            float(instrument.getNumberParameter('bkgd-range-max')[0])
-
-    # Get the total counts
-    sample_counts = Integration(data_ws, '__counts_mono-sample', RangeLower=min_value, \
-                                RangeUpper=max_value).workspace()
-
-    # Apply hard mask spectra and previous masking first
-    MaskDetectors(sample_counts, SpectraList=hard_mask_spectra, MaskedWorkspace=white_counts)
-    
-    # If we have another white beam then compute the harmonic mean of the counts
-    # The harmonic mean: 1/av = (1/Iwbv1 + 1/Iwbv2)/2
-    # We'll resuse the comp_white_counts workspace as we don't need it anymore
-    white_count_mean = white_counts
-    if comp_white_counts is not None:
-        MaskDetectors(sample_counts, MaskedWorkspace=comp_white_counts)
-        white_count_mean = (comp_white_counts * white_counts)/(comp_white_counts + white_counts)
-
-    # Make sure we have matrix workspaces before this division
-    ConvertToMatrixWorkspace(white_count_mean, white_count_mean)
-    ConvertToMatrixWorkspace(sample_counts, sample_counts)
-        
-    # Normalise the sample run
-    sample_counts = Divide(sample_counts, white_count_mean, sample_counts).workspace()
-    if comp_white_counts is not None:
-        mtd.deleteWorkspace(str(white_count_mean))
 
     # If we need to remove zeroes as well then set the the low threshold to a tiny positive number
-    if remove_zero:
-        low_threshold = 1e-40
-    else:
-        low_threshold = -1.0
+    if mask_zero:
+        median_lo = 1e-40
 
     # What shall we call the output
     lhs_names = lhs_info('names')
@@ -336,13 +258,13 @@ def _do_background_test(sample_run, white_counts, comp_white_counts, bkgd_range,
     else:
         ws_name = '__do_background_test'
 
-    median_test = MedianDetectorTest(sample_counts, ws_name, SignificanceTest=signif,\
-                                     LowThreshold=low_threshold, HighThreshold=bkgd_threshold)
-    # Remove temporary
-    mtd.deleteWorkspace(str(sample_counts))
+    mask_bkgd = MedianDetectorTest(InputWorkspace=background_int,  OutputWorkspace='mask_bkgd', 
+                                   StartWorkspaceIndex=start_index, EndWorkspaceIndex=end_index,
+                                   SignificanceTest=sigma, 
+                                   LowThreshold=median_lo, HighThreshold=median_hi, 
+                                   LowOutlier=0.0, HighOutlier=1e100, ExcludeZeroesFromMedian=True)
 
-    num_failed = median_test['NumberOfFailures'].value
-    return median_test.workspace(), num_failed
+    return mask_bkgd.workspace(), mask_bkgd['NumberOfFailures'].value
 
 #-------------------------------------------------------------------------------
 
@@ -378,23 +300,6 @@ def _do_bleed_test(sample_run, max_framerate, ignored_pixels):
 
 #-------------------------------------------------------------------------------
 
-def _accumulate(lhs, rhs):
-    """Essentially performs lhs += rhs for workspaces
-    but checks if the lhs is None first and just assigns the workspace over
-    """
-    if lhs is None:
-        lhs_names = lhs_info('names')
-        if len(lhs_names) == 0: 
-            raise RuntimeError('diagnostics._accumulate called without assignment of return value')
-        lhs = RenameWorkspace(rhs, lhs_names[0]).workspace()
-    else:
-        lhs += rhs
-        mtd.deleteWorkspace(str(rhs))                
-
-    return lhs
-    
-#-------------------------------------------------------------------------------
-
 def print_test_summary(test_results):
     """Print a summary of the failures per test run.
 
@@ -460,11 +365,6 @@ def get_failed_spectra_list(diag_workspace):
     if type(diag_workspace) == str:
         diag_workspace = mtd[diag_workspace]
 
-    if hasattr(diag_workspace, "getAxis") == False:
-        raise ValueError("Invalid input to get_failed_spectra_list. "
-                         "A workspace handle or name is expected")
-        
-    spectra_axis = diag_workspace.getAxis(1)
     failed_spectra = []
     for i in range(diag_workspace.getNumberHistograms()):
 	try:
@@ -472,6 +372,6 @@ def get_failed_spectra_list(diag_workspace):
 	except RuntimeError:
             continue
 	if det.isMasked():
-            failed_spectra.append(spectra_axis.spectraNumber(i))
+            failed_spectra.append(diag_workspace.getSpectrum(i).getSpectrumNo())
 
     return failed_spectra
-- 
GitLab