From 709b7b553a3dae685f646ecd344bc112d5251622 Mon Sep 17 00:00:00 2001
From: Mathieu Doucet <doucetm@ornl.gov>
Date: Mon, 9 Aug 2010 15:59:44 +0000
Subject: [PATCH] Modify LoadSpice2D loader to store sample-det distance as
 instr parameter so that Load() can be used. Re #1442

---
 Code/Mantid/DataHandling/src/LoadSpice2D.cpp  | 10 +++++++++-
 .../scripts/SANS/SANSReductionSteps.py        | 20 +++++++++----------
 .../scripts/SANS/utest_CommandInterface.py    | 10 ++++------
 3 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/Code/Mantid/DataHandling/src/LoadSpice2D.cpp b/Code/Mantid/DataHandling/src/LoadSpice2D.cpp
index 85ffafa1faa..855dfbfc93a 100644
--- a/Code/Mantid/DataHandling/src/LoadSpice2D.cpp
+++ b/Code/Mantid/DataHandling/src/LoadSpice2D.cpp
@@ -202,7 +202,7 @@ namespace Mantid
       int pixelcount = pixels.count();
       if( pixelcount != numberXPixels*numberYPixels )
       {
-        throw Kernel::Exception::FileError("Inconsitent data set: "
+        throw Kernel::Exception::FileError("Inconsistent data set: "
             "There were more data pixels found than declared in the Spice XML meta-data.", fileName);
       }
       if( numSpectra == 0 )
@@ -247,6 +247,14 @@ namespace Mantid
       // run load instrument
       runLoadInstrument(instrument, ws);
       runLoadMappingTable(ws, numberXPixels, numberYPixels);
+
+      // Set the sample-detector distance
+      boost::shared_ptr<Mantid::API::IInstrument> i = ws->getInstrument();
+      boost::shared_ptr<Mantid::Geometry::IComponent> sample = i->getSample();
+
+      Geometry::ParameterMap &pmap = ws->instrumentParameters();
+      pmap.add("double", sample.get(), "sample-detector-distance", distance);
+
     }
 
     /** Run the sub-algorithm LoadInstrument (as for LoadRaw)
diff --git a/Code/Mantid/PythonAPI/scripts/SANS/SANSReductionSteps.py b/Code/Mantid/PythonAPI/scripts/SANS/SANSReductionSteps.py
index 994bac93d39..eb55d2a0d13 100644
--- a/Code/Mantid/PythonAPI/scripts/SANS/SANSReductionSteps.py
+++ b/Code/Mantid/PythonAPI/scripts/SANS/SANSReductionSteps.py
@@ -32,7 +32,7 @@ class BaseBeamFinder(ReductionStep):
         """
         # Load the file to extract the beam center from, and process it.
         filepath = reducer._full_file_path(self._datafile)
-        LoadSpice2D(filepath, "beam_center")
+        Load(filepath, "beam_center")
         
         beam_center = FindCenterOfMassPosition("beam_center",
                                                Output = None,
@@ -130,19 +130,19 @@ class BeamSpreaderTransmission(BaseTransmission):
             
             sample_spreader_ws = "_trans_sample_spreader"
             filepath = reducer._full_file_path(self._sample_spreader)
-            LoadSpice2D(filepath, sample_spreader_ws)
+            Load(filepath, sample_spreader_ws)
             
             direct_spreader_ws = "_trans_direct_spreader"
             filepath = reducer._full_file_path(self._direct_spreader)
-            LoadSpice2D(filepath, direct_spreader_ws)
+            Load(filepath, direct_spreader_ws)
             
             sample_scatt_ws = "_trans_sample_scatt"
             filepath = reducer._full_file_path(self._sample_scattering)
-            LoadSpice2D(filepath, sample_scatt_ws)
+            Load(filepath, sample_scatt_ws)
             
             direct_scatt_ws = "_trans_direct_scatt"
             filepath = reducer._full_file_path(self._direct_scattering)
-            LoadSpice2D(filepath, direct_scatt_ws)
+            Load(filepath, direct_scatt_ws)
             
             # Subtract dark current
             if reducer._dark_current_subtracter is not None:
@@ -199,11 +199,11 @@ class DirectBeamTransmission(BaseTransmission):
 
             sample_ws = "_transmission_sample"
             filepath = reducer._full_file_path(self._sample_file)
-            LoadSpice2D(filepath, sample_ws)
+            Load(filepath, sample_ws)
             
             empty_ws = "_transmission_empty"
             filepath = reducer._full_file_path(self._empty_file)
-            LoadSpice2D(filepath, empty_ws)
+            Load(filepath, empty_ws)
             
             # Subtract dark current
             if reducer._dark_current_subtracter is not None:
@@ -281,7 +281,7 @@ class SubtractDarkCurrent(ReductionStep):
         if self._dark_current_ws is None:
             filepath = reducer._full_file_path(self._dark_current_file)
             self._dark_current_ws = extract_workspace_name(filepath)
-            LoadSpice2D(filepath, self._dark_current_ws)
+            Load(filepath, self._dark_current_ws)
             
             # Normalize the dark current data to counting time
             darktimer_ws = self._dark_current_ws+"_timer"
@@ -333,8 +333,8 @@ class LoadRun(ReductionStep):
         
         # Load data
         filepath = reducer._full_file_path(self._data_file)
-        loader = LoadSpice2D(filepath, workspace)
-        reducer.instrument.sample_detector_distance = float(loader.getPropertyValue("SampleDetectorDistance"))
+        loader = Load(filepath, workspace)
+        reducer.instrument.sample_detector_distance = mtd[workspace].getInstrument().getSample().getNumberParameter("sample-detector-distance")[0]        
         mantid.sendLogMessage("Loaded %s: sample-detector distance = %g" %(workspace, reducer.instrument.sample_detector_distance))
         
         # Move detector array to correct position
diff --git a/Code/Mantid/PythonAPI/scripts/SANS/utest_CommandInterface.py b/Code/Mantid/PythonAPI/scripts/SANS/utest_CommandInterface.py
index 596d3518e8a..a078f52c14a 100644
--- a/Code/Mantid/PythonAPI/scripts/SANS/utest_CommandInterface.py
+++ b/Code/Mantid/PythonAPI/scripts/SANS/utest_CommandInterface.py
@@ -56,30 +56,28 @@ def _check_result(ws, test_file, tolerance=1e-6):
     
     # Utility methods for manipulating the lists
     def _diff_chi2(x,y): return (x[1]-y[1])*(x[1]-y[1])/(x[2]*x[2])
-    def _diff_iq(x,y): 
-        #print x[1], y[1]
-        return x[1]-y[1]
+    def _diff_iq(x,y): return x[1]-y[1]
     def _diff_err(x,y): return x[2]-y[2]
     def _add(x,y): return x+y
     
     # Check that I(q) is the same for both data sets
     deltas = map(_diff_iq, data_mantid, data_igor)
     delta  = reduce(_add, deltas)/len(deltas)
-    if math.fabs(delta)>tolerance:
+    if math.fabs(delta)>tolerance or math.isnan(delta):
         passed = False
         print "Sum of I(q) deltas is outside tolerance: %g > %g" % (math.fabs(delta), tolerance)
     
     # Then compare the errors
     deltas = map(_diff_err, data_mantid, data_igor)
     delta_err  = reduce(_add, deltas)/len(deltas)
-    if math.fabs(delta_err)>tolerance:
+    if math.fabs(delta_err)>tolerance or math.isnan(delta):
         passed = False
         print "Sum of dI(q) deltas is outside tolerance: %g > %g" % (math.fabs(delta_err), tolerance)
     
     # Compute chi2 of our result relative to IGOR 
     deltas = map(_diff_chi2, data_mantid, data_igor)
     chi2  = reduce(_add, deltas)/len(data_igor)
-    if chi2>10.0*tolerance:
+    if chi2>10.0*tolerance or math.isnan(delta):
         passed= False
         print "Chi2 is outside tolerance: %g > %g" % (chi2, 10.0*tolerance)
  
-- 
GitLab