Skip to content
Snippets Groups Projects
EnginXFocus.py 3.93 KiB
Newer Older
#pylint: disable=no-init,invalid-name
from mantid.kernel import *
from mantid.api import *

class EnginXFocus(PythonAlgorithm):
        return "Diffraction\Engineering;PythonAlgorithms"
        return "EnginXFocus"
        return "Focuses a run."
        self.declareProperty(FileProperty("Filename", "", FileAction.Load),\
        self.declareProperty(WorkspaceProperty("OutputWorkspace", "", Direction.Output),\
        self.declareProperty(ITableWorkspaceProperty("DetectorPositions", "", Direction.Input, PropertyMode.Optional),\
    		"Calibrated detector positions. If not specified, default ones are used.")

        self.declareProperty("Bank", 1, "Which bank to focus")
        ws = self._loadRun()

    	# Leave the data for the bank we are interested in only
        ws = self._cropData(ws)
        self._applyCalibration(ws)
        ws = self._convertToDSpacing(ws)
        ws = self._sumSpectra(ws)
        ws = self._convertToTOF(ws)

    	# OpenGenie displays distributions instead of pure counts (this is done implicitly when
    	# converting units), so I guess that's what users will expect
        self._convertToDistr(ws)
        self.setProperty("OutputWorkspace", ws)
        """ Loads the specified run
        alg = self.createChildAlgorithm('Load')
        alg.setProperty('Filename', self.getProperty("Filename").value)
        alg.execute()
        return alg.getProperty('OutputWorkspace').value
        """ Refines the detector positions using the result of calibration (if one is specified)
        detPos = self.getProperty("DetectorPositions").value
        if detPos:
            alg = self.createChildAlgorithm('ApplyCalibration')
            alg.setProperty('Workspace', ws)
            alg.setProperty('PositionTable', detPos)
            alg.execute()
        """ Converts workspace to dSpacing
        alg = self.createChildAlgorithm('ConvertUnits')
        alg.setProperty('InputWorkspace', ws)
        alg.setProperty('Target', 'dSpacing')
        alg.setProperty('AlignBins', True)
        alg.execute()
        return alg.getProperty('OutputWorkspace').value
        """ Converts workspace to TOF
        alg = self.createChildAlgorithm('ConvertUnits')
        alg.setProperty('InputWorkspace', ws)
        alg.setProperty('Target', 'TOF')
        alg.execute()
        return alg.getProperty('OutputWorkspace').value
        """ Convert workspace to distribution
        alg = self.createChildAlgorithm('ConvertToDistribution')
        alg.setProperty('Workspace', ws)
        alg.execute()
        """ Crops the workspace so that only data for the specified bank is left.

    	    NB: This assumes spectra for a bank are consequent.
    	"""

        import EnginXUtils
        indices = EnginXUtils.getWsIndicesForBank(self.getProperty('Bank').value, ws)

    	# Leave only spectra between min and max
        alg = self.createChildAlgorithm('CropWorkspace')
        alg.setProperty('InputWorkspace', ws)
        alg.setProperty('StartWorkspaceIndex', min(indices))
        alg.setProperty('EndWorkspaceIndex', max(indices))
        alg.execute()
        return alg.getProperty('OutputWorkspace').value
        """ Calls the SumSpectra algorithm
        alg = self.createChildAlgorithm('SumSpectra')
        alg.setProperty('InputWorkspace', ws)
        alg.execute()
        return alg.getProperty('OutputWorkspace').value
AlgorithmFactory.subscribe(EnginXFocus)