Skip to content

Commit

Permalink
Re #6151 add background to API2 workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
mdoucet committed Nov 29, 2012
1 parent 36f9e84 commit 775184f
Show file tree
Hide file tree
Showing 6 changed files with 644 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""*WIKI*
*WIKI*"""
import mantid.simpleapi as api
from mantid.api import *
from mantid.kernel import *
from reduction_workflow.find_data import find_data

class HFIRSANSReduction(PythonAlgorithm):

Expand All @@ -20,6 +22,67 @@ def PyInit(self):
self.declareProperty("OutputMessage", "",
direction=Direction.Output, doc = "Output message")

def _multiple_load(self, data_file, workspace,
property_manager, property_manager_name):
# Check whether we have a list of files that need merging
# Make sure we process a list of files written as a string
def _load_data(filename, output_ws):
if not property_manager.existsProperty("LoadAlgorithm"):
raise RuntimeError, "SANS reduction not set up properly: missing load algorithm"
p=property_manager.getProperty("LoadAlgorithm")
alg=Algorithm.fromString(p.valueAsStr)
alg.setProperty("Filename", filename)
alg.setProperty("OutputWorkspace", output_ws)
if alg.existsProperty("ReductionProperties"):
alg.setProperty("ReductionProperties", property_manager_name)
alg.execute()
msg = "Loaded %s\n" % filename
if alg.existsProperty("OutputMessage"):
msg = alg.getProperty("OutputMessage").value
return msg

# Get instrument to use with FileFinder
instrument = ''
if property_manager.existsProperty("InstrumentName"):
instrument = property_manager.getProperty("InstrumentName")

output_str = ''
if type(data_file)==str:
data_file = find_data(data_file, instrument=instrument, allow_multiple=True)
if type(data_file)==list:
monitor = 0.0
timer = 0.0
for i in range(len(data_file)):
output_str += "Loaded %s\n" % data_file[i]
if i==0:
output_str += _load_data(data_file[i], workspace)
else:
output_str += _load_data(data_file[i], '__tmp_wksp')
api.Plus(LHSWorkspace=workspace,
RHSWorkspace='__tmp_wksp',
OutputWorkspace=workspace)
# Get the monitor and timer values
ws = AnalysisDataService.retrieve('__tmp_wksp')
monitor += ws.getRun().getProperty("monitor").value
timer += ws.getRun().getProperty("timer").value

# Get the monitor and timer of the first file, which haven't yet
# been added to the total
ws = AnalysisDataService.retrieve(workspace)
monitor += ws.getRun().getProperty("monitor").value
timer += ws.getRun().getProperty("timer").value

# Update the timer and monitor
ws.getRun().addProperty("monitor", monitor, True)
ws.getRun().addProperty("timer", timer, True)

if AnalysisDataService.doesExist('__tmp_wksp'):
AnalysisDataService.remove('__tmp_wksp')
else:
output_str += "Loaded %s\n" % data_file
output_str += _load_data(data_file, workspace)
return output_str

def PyExec(self):
filename = self.getProperty("Filename").value
output_ws = self.getPropertyValue("OutputWorkspace")
Expand Down Expand Up @@ -60,36 +123,64 @@ def PyExec(self):
p=property_manager.getProperty("TransmissionAlgorithm")
alg=Algorithm.fromString(p.valueAsStr)
alg.setProperty("InputWorkspace", output_ws)
alg.setProperty("OutputWorkspace", output_ws)
alg.setProperty("OutputWorkspace", '__'+output_ws+"_reduced")
if alg.existsProperty("ReductionProperties"):
alg.setProperty("ReductionProperties", property_manager_name)
alg.execute()
if alg.existsProperty("OutputMessage"):
output_msg += alg.getProperty("OutputMessage").value+'\n'
output_ws = '__'+output_ws+'_reduced'

# Process background data
if "BackgroundFiles" in property_list:
background = property_manager.getProperty("LoadAlgorithm").valueAsStr
background = property_manager.getProperty("BackgroundFiles").value
background_ws = "__background_%s" % output_ws
p=property_manager.getProperty("LoadAlgorithm")
alg=Algorithm.fromString(p.valueAsStr)
alg.setProperty("Filename", background)
alg.setProperty("OutputWorkspace", background_ws)
alg.setProperty("ReductionProperties", property_manager_name)
alg.execute()
output_msg += "Loaded background %s\n" % background
if alg.existsProperty("OutputMessage"):
output_msg += alg.getProperty("OutputMessage").value
msg = self._multiple_load(background, background_ws,
property_manager, property_manager_name)
bck_msg = "Loaded background %s\n" % background
bck_msg += msg

# Background transmission correction
# Process background like we processed the sample data
bck_msg += self.process_data_file(background_ws)

# Background transmission correction
if "BckTransmissionAlgorithm" in property_list:
p=property_manager.getProperty("BckTransmissionAlgorithm")
alg=Algorithm.fromString(p.valueAsStr)
alg.setProperty("InputWorkspace", background_ws)
alg.setProperty("OutputWorkspace", background_ws)
if alg.existsProperty("ReductionProperties"):
alg.setProperty("ReductionProperties", property_manager_name)
alg.execute()
if alg.existsProperty("OutputMessage"):
output_msg += alg.getProperty("OutputMessage").value+'\n'

# Subtract background
# Subtract background
api.Minus(LHSWorkspace=output_ws,
RHSWorkspace=background_ws,
OutputWorkspace=output_ws)

bck_msg = bck_msg.replace('\n','\n |')
output_msg += "Background subtracted [%s]%s\n" % (background_ws, bck_msg)

# Absolute scale correction

# Geometry correction
if "GeometryAlgorithm" in property_list:
p=property_manager.getProperty("GeometryAlgorithm")
alg=Algorithm.fromString(p.valueAsStr)
alg.setProperty("InputWorkspace", background_ws)
alg.setProperty("OutputWorkspace", background_ws)
if alg.existsProperty("ReductionProperties"):
alg.setProperty("ReductionProperties", property_manager_name)
alg.execute()
if alg.existsProperty("OutputMessage"):
output_msg += alg.getProperty("OutputMessage").value+'\n'

# Compute I(q)
if "IQAlgorithm" in property_list:
iq_output = output_ws+'_Iq'
iq_output = self.getPropertyValue("OutputWorkspace")
iq_output = iq_output+'_Iq'
p=property_manager.getProperty("IQAlgorithm")
alg=Algorithm.fromString(p.valueAsStr)
alg.setProperty("InputWorkspace", output_ws)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*WIKI*"""
from mantid.api import *
from mantid.kernel import *
import math

class SANSAzimuthalAverage1D(PythonAlgorithm):

Expand Down Expand Up @@ -45,8 +46,6 @@ def PyExec(self):
Logger.get("SANSAzimuthalAverage").warning(msg)

# Q binning options
n_bins = self.getProperty("NumberOfBins").value
log_binning = self.getProperty("LogBinning").value
binning = self.getProperty("Binning").value

input_ws_name = self.getPropertyValue("InputWorkspace")
Expand All @@ -61,7 +60,7 @@ def PyExec(self):
pixel_size_x = workspace.getInstrument().getNumberParameter("x-pixel-size")[0]
pixel_size_y = workspace.getInstrument().getNumberParameter("y-pixel-size")[0]

if binning[0]==0 and binning[1]==0 and binning[2]==0:
if len(binning)==0 or (binning[0]==0 and binning[1]==0 and binning[2]==0):
# Wavelength. Read in the wavelength bins. Skip the first one which is not set up properly for EQ-SANS
x = workspace.dataX(1)
x_length = len(x)
Expand All @@ -71,7 +70,7 @@ def PyExec(self):
wavelength_min = (x[0]+x[1])/2.0
if wavelength_min==0 or wavelength_max==0:
raise RuntimeError, "Azimuthal averaging needs positive wavelengths"
qmin, qstep, qmax = self._get_binning(reducer, workspace, wavelength_min, wavelength_max)
qmin, qstep, qmax = self._get_binning(workspace, wavelength_min, wavelength_max)
binning = [qmin, qstep, qmax]
else:
qmin = binning[0]
Expand Down Expand Up @@ -123,8 +122,9 @@ def PyExec(self):
self.setPropertyValue("OutputWorkspace", output_ws_name)


def _get_binning(self, reducer, workspace, wavelength_min, wavelength_max):

def _get_binning(self, workspace, wavelength_min, wavelength_max):
log_binning = self.getProperty("LogBinning").value
nbins = self.getProperty("NumberOfBins").value
beam_ctr_x = workspace.getRun().getProperty("beam_center_x").value
beam_ctr_y = workspace.getRun().getProperty("beam_center_y").value
sample_detector_distance = workspace.getRun().getProperty("sample_detector_distance").value
Expand All @@ -145,16 +145,16 @@ def _get_binning(self, reducer, workspace, wavelength_min, wavelength_max):
maxdist = math.sqrt(dxmax*dxmax+dymax*dymax)
qmax = 4*math.pi/wavelength_min*math.sin(0.5*math.atan(maxdist/sample_detector_distance))

if not self._log_binning:
qstep = (qmax-qmin)/self._nbins
if not log_binning:
qstep = (qmax-qmin)/nbins
f_step = (qmax-qmin)/qstep
n_step = math.floor(f_step)
if f_step-n_step>10e-10:
qmax = qmin+qstep*n_step
return qmin, qstep, qmax
else:
# Note: the log binning in Mantid is x_i+1 = x_i * ( 1 + dx )
qstep = (math.log10(qmax)-math.log10(qmin))/self._nbins
qstep = (math.log10(qmax)-math.log10(qmin))/nbins
f_step = (math.log10(qmax)-math.log10(qmin))/qstep
n_step = math.floor(f_step)
if f_step-n_step>10e-10:
Expand Down

0 comments on commit 775184f

Please sign in to comment.