1 | # Algorithm to start Decon |
---|
2 | from mantid.simpleapi import * |
---|
3 | from mantid.api import PythonAlgorithm, AlgorithmFactory, MatrixWorkspaceProperty, FileProperty, FileAction, PropertyMode |
---|
4 | from mantid.kernel import StringListValidator, StringMandatoryValidator, Direction, logger |
---|
5 | from mantid import config |
---|
6 | import os.path |
---|
7 | |
---|
8 | class IndirectFlatPlateAbsorption(PythonAlgorithm): |
---|
9 | |
---|
10 | def category(self): |
---|
11 | return "Workflow\\MIDAS;PythonAlgorithms" |
---|
12 | |
---|
13 | def PyInit(self): |
---|
14 | self.declareProperty(name='Sample Input', defaultValue='Workspace', validator=StringListValidator(['Workspace','File']), |
---|
15 | doc='Sample input type') |
---|
16 | self.declareProperty(MatrixWorkspaceProperty('Sample Workspace', '', optional=PropertyMode.Optional, |
---|
17 | direction=Direction.Input), doc="Name for the input Sample workspace.") |
---|
18 | self.declareProperty(FileProperty('Sample File', '', action=FileAction.OptionalLoad, extensions=["_red.nxs"]), |
---|
19 | doc='File path for Sample file') |
---|
20 | |
---|
21 | self.declareProperty(name='Use Can', defaultValue=False, doc = 'Use Can') |
---|
22 | self.declareProperty(name='Can Input', defaultValue='Workspace', validator=StringListValidator(['Workspace','File']), |
---|
23 | doc='Can input type') |
---|
24 | self.declareProperty(MatrixWorkspaceProperty('Can Workspace', '', optional=PropertyMode.Optional, |
---|
25 | direction=Direction.Input), doc="Name for the input Can workspace.") |
---|
26 | self.declareProperty(FileProperty('Can File', '', action=FileAction.OptionalLoad, extensions=["_red.nxs"]), |
---|
27 | doc='File path for Can file') |
---|
28 | self.declareProperty(name='Can scale factor', defaultValue='1.0', doc = 'Scale factor to multiply can data') |
---|
29 | |
---|
30 | self.declareProperty(name='Chemical formula', defaultValue='', doc = 'Chemical formula') |
---|
31 | self.declareProperty(name='Sample height', defaultValue='', doc = 'Sample height') |
---|
32 | self.declareProperty(name='Sample width', defaultValue='', doc = 'Sample width') |
---|
33 | self.declareProperty(name='Sample thickness', defaultValue='', doc = 'Sample thickness') |
---|
34 | self.declareProperty(name='Element size', defaultValue=0.1, doc = 'Element size in mm') |
---|
35 | self.declareProperty(name='Sample number density', defaultValue='', doc = 'Sample number density') |
---|
36 | self.declareProperty(name='Verbose', defaultValue=False, doc = 'Switch Verbose Off/On') |
---|
37 | self.declareProperty(name='Plot', defaultValue=False, doc = 'Plot options') |
---|
38 | self.declareProperty(name='Save', defaultValue=False, doc = 'Switch Save result to nxs file Off/On') |
---|
39 | |
---|
40 | def PyExec(self): |
---|
41 | |
---|
42 | from IndirectCommon import StartTime, EndTime, getEfixed, addSampleLogs |
---|
43 | from IndirectImport import import_mantidplot |
---|
44 | mp = import_mantidplot() |
---|
45 | |
---|
46 | StartTime('FlatPlate Absorption') |
---|
47 | workdir = config['defaultsave.directory'] |
---|
48 | self._setup() |
---|
49 | efixed = getEfixed(self._sam) # Get efixed |
---|
50 | swaveWS = '__sam_wave' |
---|
51 | ConvertUnits(InputWorkspace=self._sam, OutputWorkspace=swaveWS, Target='Wavelength', |
---|
52 | EMode='Indirect', EFixed=efixed) |
---|
53 | |
---|
54 | name = self._sam[:-4] |
---|
55 | assWS = name + '_flt_ass' |
---|
56 | corrWS = name + '_corrected' |
---|
57 | SetSampleMaterial(swaveWS, ChemicalFormula=self._chem, SampleNumberDensity=self._density) |
---|
58 | FlatPlateAbsorption(InputWorkspace=swaveWS, OutputWorkspace=assWS, |
---|
59 | SampleHeight=self._height, SampleWidth=self._width, SampleThickness=self._thickness, |
---|
60 | ElementSize=self._element, EMode='Indirect', EFixed=efixed, NumberOfWavelengthPoints=10) |
---|
61 | plot_list = [corrWS, self._sam] |
---|
62 | |
---|
63 | if self._usecan: |
---|
64 | cwaveWS = '__can_wave' |
---|
65 | ConvertUnits(InputWorkspace=self._can, OutputWorkspace=cwaveWS, Target='Wavelength', |
---|
66 | EMode='Indirect', EFixed=efixed) |
---|
67 | if self._can_scale != 1.0: |
---|
68 | if self._verbose: |
---|
69 | logger.notice('Scaling can by : '+str(self._can_scale)) |
---|
70 | Scale(InputWorkspace=cwaveWS, OutputWorkspace=cwaveWS, Factor=self._can_scale, Operation='Multiply') |
---|
71 | Minus(LHSWorkspace=swaveWS, RHSWorkspace=cwaveWS, OutputWorkspace=swaveWS) |
---|
72 | plot_list.append(self._can) |
---|
73 | |
---|
74 | Divide(LHSWorkspace=swaveWS, RHSWorkspace=assWS, OutputWorkspace=swaveWS) |
---|
75 | ConvertUnits(InputWorkspace=swaveWS, OutputWorkspace=corrWS, Target='DeltaE', |
---|
76 | EMode='Indirect', EFixed=efixed) |
---|
77 | sample_logs = {'sample_shape': 'flatplate', 'sample_filename': self._sam, |
---|
78 | 'sample_height': self._height, 'sample_width': self._width, |
---|
79 | 'sample_thickness': self._thickness, 'element_size': self._element} |
---|
80 | addSampleLogs(assWS, sample_logs) |
---|
81 | addSampleLogs(corrWS, sample_logs) |
---|
82 | if self._usecan: |
---|
83 | AddSampleLog(Workspace=assWS, LogName='can_filename', LogType='String', LogText=str(self._can)) |
---|
84 | AddSampleLog(Workspace=corrWS, LogName='can_filename', LogType='String', LogText=str(self._can)) |
---|
85 | AddSampleLog(Workspace=assWS, LogName='can_scale', LogType='String', LogText=str(self._can_scale)) |
---|
86 | AddSampleLog(Workspace=corrWS, LogName='can_scale', LogType='String', LogText=str(self._can_scale)) |
---|
87 | |
---|
88 | if self._plot: |
---|
89 | mp.plotSpectrum(plot_list, 0) |
---|
90 | |
---|
91 | if self._save: |
---|
92 | path = os.path.join(workdir,corrWS + '.nxs') |
---|
93 | SaveNexusProcessed(InputWorkspace=corrWS, Filename=path) |
---|
94 | if self._verbose: |
---|
95 | logger.notice('Output file created : '+path) |
---|
96 | |
---|
97 | EndTime('FlatPlate Absorption') |
---|
98 | |
---|
99 | def _setup(self): |
---|
100 | self._verbose = self.getProperty('Verbose').value |
---|
101 | sInput = self.getPropertyValue('Sample Input') |
---|
102 | if sInput == 'Workspace': |
---|
103 | s_ws = self.getPropertyValue('Sample Workspace') |
---|
104 | else: |
---|
105 | s_ws = '' |
---|
106 | if sInput == 'File': |
---|
107 | s_file = self.getPropertyValue('Sample File') |
---|
108 | else: |
---|
109 | s_file = '' |
---|
110 | self._input = sInput |
---|
111 | self._path = s_file |
---|
112 | self._ws = s_ws |
---|
113 | self._getData() |
---|
114 | self._sam = self._name |
---|
115 | |
---|
116 | self._usecan = self.getProperty('Use can').value |
---|
117 | if self._usecan: |
---|
118 | cInput = self.getPropertyValue('Can Input') |
---|
119 | if cInput == 'Workspace': |
---|
120 | c_ws = self.getPropertyValue('Can Workspace') |
---|
121 | else: |
---|
122 | c_ws = '' |
---|
123 | if cInput == 'File': |
---|
124 | c_file = self.getPropertyValue('Can File') |
---|
125 | else: |
---|
126 | c_file = '' |
---|
127 | self._input = cInput |
---|
128 | self._path = c_file |
---|
129 | self._ws = c_ws |
---|
130 | self._getData() |
---|
131 | self._can = self._name |
---|
132 | self._can_scale = self.getPropertyValue('Can scale factor') |
---|
133 | |
---|
134 | self._chem = self.getPropertyValue('Chemical formula') |
---|
135 | self._density = self.getPropertyValue('Sample number density') |
---|
136 | self._height = float(self.getPropertyValue('Sample height')) |
---|
137 | self._width = float(self.getPropertyValue('Sample width')) |
---|
138 | self._thickness = float(self.getPropertyValue('Sample thickness')) |
---|
139 | self._element = self.getPropertyValue('Element size') |
---|
140 | self._plot = self.getProperty('Plot').value |
---|
141 | self._save = self.getProperty('Save').value |
---|
142 | |
---|
143 | def _getData(self): #get data |
---|
144 | if self._input == 'Workspace': |
---|
145 | inWS = self._ws |
---|
146 | self._name = inWS |
---|
147 | if self._verbose: |
---|
148 | logger.notice('Input from Workspace : '+inWS) |
---|
149 | elif self._input == 'File': |
---|
150 | self._getFileName() |
---|
151 | inWS = self._name |
---|
152 | LoadNexus(Filename=self._path, OutputWorkspace=inWS) |
---|
153 | else: |
---|
154 | raise ValueError('Input type not defined') |
---|
155 | |
---|
156 | def _getFileName(self): |
---|
157 | import os.path |
---|
158 | path = self._path |
---|
159 | if(os.path.isfile(path)): |
---|
160 | base = os.path.basename(path) |
---|
161 | self._name = os.path.splitext(base)[0] |
---|
162 | ext = os.path.splitext(base)[1] |
---|
163 | if self._verbose: |
---|
164 | logger.notice('Input file : '+path) |
---|
165 | else: |
---|
166 | raise ValueError('Could not find file: ' + path) |
---|
167 | |
---|
168 | |
---|
169 | # Register algorithm with Mantid |
---|
170 | AlgorithmFactory.subscribe(IndirectFlatPlateAbsorption) |
---|
171 | # |
---|