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