Skip to content

Commit

Permalink
Re #6151 Allow API2 to use HFIR data proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
mdoucet committed Dec 19, 2012
1 parent eaa81f7 commit 6d0ee69
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
import sys

# Check whether Mantid is available
try:
from MantidFramework import *
mtd.initialise(False)
import mantidsimple
from reduction.instruments.sans.hfir_load import LoadRun
from reduction.instruments.sans.sans_reducer import SANSReducer
import reduction.instruments.sans.hfir_instrument as hfir_instrument

HAS_MANTID = True
except:
HAS_MANTID = False

# Check whether we have numpy
try:
import numpy
HAS_NUMPY = True
except:
HAS_NUMPY = False


class DataProxy(object):
"""
Class used to load a data file temporarily to extract header information
Expand All @@ -31,40 +11,66 @@ class DataProxy(object):
data_ws = ''
sample_thickness = None
beam_diameter = None
python_api = 1

## Error log
errors = []

def __init__(self, data_file, workspace_name=None):
def __init__(self, data_file, workspace_name=None, python_api=1):
self.errors = []
if HAS_MANTID:
try:
if workspace_name is None:
self.data_ws = "__raw_data_file"
else:
self.data_ws = str(workspace_name)
reducer = SANSReducer()
reducer.set_instrument(hfir_instrument.HFIRSANS())
loader = LoadRun(str(data_file))
loader.execute(reducer, self.data_ws)
x = mtd[self.data_ws].dataX(0)
self.wavelength = (x[0]+x[1])/2.0
self.wavelength_spread = x[1]-x[0]
self.sample_detector_distance = mtd[self.data_ws].getRun().getProperty("sample_detector_distance").value
self.sample_thickness = mtd[self.data_ws].getRun().getProperty("sample-thickness").value
self.beam_diameter = mtd[self.data_ws].getRun().getProperty("beam-diameter").value

if False and HAS_NUMPY:
nx_pixels = int(mtd[self.data_ws].getInstrument().getNumberParameter("number-of-x-pixels")[0])
ny_pixels = int(mtd[self.data_ws].getInstrument().getNumberParameter("number-of-y-pixels")[0])
nMonitors = int(mtd[self.data_ws].getInstrument().getNumberParameter("number-of-monitors")[0])
raw_data = numpy.zeros(nx_pixels*ny_pixels)
for i in range(nMonitors-1, nx_pixels*ny_pixels+nMonitors ):
raw_data[i-nMonitors] = mtd[self.data_ws].readY(i)[0]

self.data = numpy.reshape(raw_data, (nx_pixels, ny_pixels), order='F')
mtd.sendLogMessage("Loaded data file: %s" % data_file)
except:
mtd.sendLogMessage("DataProxy: Error loading data file:\n%s" % sys.exc_value)
self.errors.append("Error loading data file:\n%s" % sys.exc_value)

self.python_api = python_api
if python_api==1:
return self._load_python_api_1(data_file, workspace_name)
else:
return self._load_python_api_2(data_file, workspace_name)

def _load_python_api_1(self, data_file, workspace_name):
from MantidFramework import mtd
import mantidsimple
from reduction.instruments.sans.hfir_load import LoadRun
from reduction.instruments.sans.sans_reducer import SANSReducer
import reduction.instruments.sans.hfir_instrument as hfir_instrument
try:
if workspace_name is None:
self.data_ws = "__raw_data_file"
else:
self.data_ws = str(workspace_name)
reducer = SANSReducer()
reducer.set_instrument(hfir_instrument.HFIRSANS())
loader = LoadRun(str(data_file))
loader.execute(reducer, self.data_ws)
x = mtd[self.data_ws].dataX(0)
self.wavelength = (x[0]+x[1])/2.0
self.wavelength_spread = x[1]-x[0]
self.sample_detector_distance = mtd[self.data_ws].getRun().getProperty("sample_detector_distance").value
self.sample_thickness = mtd[self.data_ws].getRun().getProperty("sample-thickness").value
self.beam_diameter = mtd[self.data_ws].getRun().getProperty("beam-diameter").value

mtd.sendLogMessage("Loaded data file: %s" % data_file)
except:
mtd.sendLogMessage("DataProxy: Error loading data file:\n%s" % sys.exc_value)
self.errors.append("Error loading data file:\n%s" % sys.exc_value)

def _load_python_api_2(self, data_file, workspace_name):
from mantid.api import AnalysisDataService
from mantid.kernel import Logger
import mantid.simpleapi as api
try:
if workspace_name is None:
self.data_ws = "__raw_data_file"
else:
self.data_ws = str(workspace_name)
api.HFIRLoad(Filename=str(data_file), OutputWorkspace=self.data_ws)
ws = AnalysisDataService.retrieve(self.data_ws)
x = ws.dataX(0)
self.wavelength = (x[0]+x[1])/2.0
self.wavelength_spread = x[1]-x[0]
self.sample_detector_distance = ws.getRun().getProperty("sample_detector_distance").value
self.sample_thickness = ws.getRun().getProperty("sample-thickness").value
self.beam_diameter = ws.getRun().getProperty("beam-diameter").value

Logger.get("hfir_data_proxy").information("Loaded data file: %s" % data_file)
except:
Logger.get("hfir_data_proxy").error("Error loading data file:\n%s" % sys.exc_value)
self.errors.append("Error loading data file:\n%s" % sys.exc_value)

Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ def get_data_info(self):

fname = str(self._content.background_edit.text())
if len(str(fname).strip())>0:
dataproxy = self._data_proxy(fname, "__background_raw")
api = 2 if self._settings.api2 else 1
dataproxy = self._data_proxy(fname, "__background_raw",
python_api=api)
if len(dataproxy.errors)>0:
#QtGui.QMessageBox.warning(self, "Error", dataproxy.errors[0])
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ def get_data_info(self):
return
fname = data_files[0]
if len(str(fname).strip())>0:
dataproxy = self._data_proxy(fname)
api = 2 if self._settings.api2 else 1
dataproxy = self._data_proxy(fname, python_api=api)
if len(dataproxy.errors)>0:
#QtGui.QMessageBox.warning(self, "Error", dataproxy.errors[0])
return
Expand Down

0 comments on commit 6d0ee69

Please sign in to comment.