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 IndirectCylinderAbsorption(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 radius', defaultValue='', doc = 'Sample radius') |
---|
32 | self.declareProperty(name='Sample number density', defaultValue='', doc = 'Sample number density') |
---|
33 | self.declareProperty(name='Verbose', defaultValue=False, doc = 'Switch Verbose Off/On') |
---|
34 | self.declareProperty(name='Plot', defaultValue=False, doc = 'Plot options') |
---|
35 | self.declareProperty(name='Save', defaultValue=False, doc = 'Switch Save result to nxs file Off/On') |
---|
36 | |
---|
37 | def PyExec(self): |
---|
38 | |
---|
39 | from IndirectCommon import StartTime, EndTime, getEfixed, addSampleLogs |
---|
40 | from IndirectImport import import_mantidplot |
---|
41 | mp = import_mantidplot() |
---|
42 | |
---|
43 | StartTime('Cylinder Absorption') |
---|
44 | workdir = config['defaultsave.directory'] |
---|
45 | self._setup() |
---|
46 | efixed = getEfixed(self._sam) # Get efixed |
---|
47 | swaveWS = '__sam_wave' |
---|
48 | ConvertUnits(InputWorkspace=self._sam, OutputWorkspace=swaveWS, Target='Wavelength', |
---|
49 | EMode='Indirect', EFixed=efixed) |
---|
50 | |
---|
51 | name = self._sam[:-4] |
---|
52 | assWS = name + '_cyl_ass' |
---|
53 | corrWS = name + '_corrected' |
---|
54 | SetSampleMaterial(swaveWS, ChemicalFormula=self._chem, SampleNumberDensity=self._density) |
---|
55 | CylinderAbsorption(InputWorkspace=swaveWS, OutputWorkspace = assWS, |
---|
56 | SampleNumberDensity=self._density, |
---|
57 | NumberOfWavelengthPoints=10, CylinderSampleHeight=3.0, |
---|
58 | CylinderSampleRadius=self._radius, NumberOfSlices=1, NumberOfAnnuli=10) |
---|
59 | plot_list = [corrWS, self._sam] |
---|
60 | |
---|
61 | if self._usecan: |
---|
62 | cwaveWS = '__can_wave' |
---|
63 | ConvertUnits(InputWorkspace=self._can, OutputWorkspace=cwaveWS, Target='Wavelength', |
---|
64 | EMode='Indirect', EFixed=efixed) |
---|
65 | if self._can_scale != 1.0: |
---|
66 | if self._verbose: |
---|
67 | logger.notice('Scaling can by : '+str(self._can_scale)) |
---|
68 | Scale(InputWorkspace=cwaveWS, OutputWorkspace=cwaveWS, Factor=self._can_scale, Operation='Multiply') |
---|
69 | Minus(LHSWorkspace=swaveWS, RHSWorkspace=cwaveWS, OutputWorkspace=swaveWS) |
---|
70 | plot_list.append(self._can) |
---|
71 | |
---|
72 | Divide(LHSWorkspace=swaveWS, RHSWorkspace=assWS, OutputWorkspace=swaveWS) |
---|
73 | ConvertUnits(InputWorkspace=swaveWS, OutputWorkspace=corrWS, Target='DeltaE', |
---|
74 | EMode='Indirect', EFixed=efixed) |
---|
75 | sample_logs = {'sample_shape': 'cylinder', |
---|
76 | 'sample_filename': self._sam, 'sample_radius': self._radius} |
---|
77 | addSampleLogs(assWS, sample_logs) |
---|
78 | addSampleLogs(corrWS, sample_logs) |
---|
79 | if self._usecan: |
---|
80 | AddSampleLog(Workspace=assWS, LogName='can_filename', LogType='String', LogText=str(self._can)) |
---|
81 | AddSampleLog(Workspace=corrWS, LogName='can_filename', LogType='String', LogText=str(self._can)) |
---|
82 | AddSampleLog(Workspace=assWS, LogName='can_scale', LogType='String', LogText=str(self._can_scale)) |
---|
83 | AddSampleLog(Workspace=corrWS, LogName='can_scale', LogType='String', LogText=str(self._can_scale)) |
---|
84 | |
---|
85 | if self._plot: |
---|
86 | mp.plotSpectrum(plot_list, 0) |
---|
87 | |
---|
88 | if self._save: |
---|
89 | path = os.path.join(workdir,corrWS + '.nxs') |
---|
90 | SaveNexusProcessed(InputWorkspace=corrWS, Filename=path) |
---|
91 | if self._verbose: |
---|
92 | logger.notice('Output file created : '+path) |
---|
93 | |
---|
94 | EndTime('Cylinder Absorption') |
---|
95 | |
---|
96 | def _setup(self): |
---|
97 | self._verbose = self.getProperty('Verbose').value |
---|
98 | sInput = self.getPropertyValue('Sample Input') |
---|
99 | if sInput == 'Workspace': |
---|
100 | s_ws = self.getPropertyValue('Sample Workspace') |
---|
101 | else: |
---|
102 | s_ws = '' |
---|
103 | if sInput == 'File': |
---|
104 | s_file = self.getPropertyValue('Sample File') |
---|
105 | else: |
---|
106 | s_file = '' |
---|
107 | self._input = sInput |
---|
108 | self._path = s_file |
---|
109 | self._ws = s_ws |
---|
110 | self._getData() |
---|
111 | self._sam = self._name |
---|
112 | |
---|
113 | self._usecan = self.getProperty('Use can').value |
---|
114 | if self._usecan: |
---|
115 | cInput = self.getPropertyValue('Can Input') |
---|
116 | if cInput == 'Workspace': |
---|
117 | c_ws = self.getPropertyValue('Can Workspace') |
---|
118 | else: |
---|
119 | c_ws = '' |
---|
120 | if cInput == 'File': |
---|
121 | c_file = self.getPropertyValue('Can File') |
---|
122 | else: |
---|
123 | c_file = '' |
---|
124 | self._input = cInput |
---|
125 | self._path = c_file |
---|
126 | self._ws = c_ws |
---|
127 | self._getData() |
---|
128 | self._can = self._name |
---|
129 | self._can_scale = self.getPropertyValue('Can scale factor') |
---|
130 | |
---|
131 | self._chem = self.getPropertyValue('Chemical formula') |
---|
132 | self._density = self.getPropertyValue('Sample number density') |
---|
133 | self._radius = self.getPropertyValue('Sample radius') |
---|
134 | self._plot = self.getProperty('Plot').value |
---|
135 | self._save = self.getProperty('Save').value |
---|
136 | |
---|
137 | def _getData(self): #get data |
---|
138 | if self._input == 'Workspace': |
---|
139 | inWS = self._ws |
---|
140 | self._name = inWS |
---|
141 | if self._verbose: |
---|
142 | logger.notice('Input from Workspace : '+inWS) |
---|
143 | elif self._input == 'File': |
---|
144 | self._getFileName() |
---|
145 | inWS = self._name |
---|
146 | LoadNexus(Filename=self._path, OutputWorkspace=inWS) |
---|
147 | else: |
---|
148 | raise ValueError('Input type not defined') |
---|
149 | |
---|
150 | def _getFileName(self): |
---|
151 | import os.path |
---|
152 | path = self._path |
---|
153 | if(os.path.isfile(path)): |
---|
154 | base = os.path.basename(path) |
---|
155 | self._name = os.path.splitext(base)[0] |
---|
156 | ext = os.path.splitext(base)[1] |
---|
157 | if self._verbose: |
---|
158 | logger.notice('Input file : '+path) |
---|
159 | else: |
---|
160 | raise ValueError('Could not find file: ' + path) |
---|
161 | |
---|
162 | |
---|
163 | # Register algorithm with Mantid |
---|
164 | AlgorithmFactory.subscribe(IndirectCylinderAbsorption) |
---|
165 | # |
---|