1 | # Correction - applies absoption correction |
---|
2 | |
---|
3 | from MantidFramework import * |
---|
4 | from mantidsimple import * |
---|
5 | from mantidplot import * |
---|
6 | import math, os.path as op |
---|
7 | |
---|
8 | def ReadNxsFile(name,ana,ext): |
---|
9 | workdir = mantid.getConfigProperty('defaultsave.directory') |
---|
10 | file = name+'_'+ana+'_'+ext+'.nxs' |
---|
11 | path = op.join(workdir, file) # path name for nxs file |
---|
12 | LoadNexusProcessed(file,name) |
---|
13 | |
---|
14 | def getEfixed(workspace, detIndex=0): |
---|
15 | det = mtd[workspace].getDetector(detIndex) |
---|
16 | try: |
---|
17 | efixed = det.getNumberParameter('Efixed')[0] |
---|
18 | except AttributeError: |
---|
19 | ids = det.getDetectorIDs() |
---|
20 | det = mtd[workspace].getInstrument().getDetector(ids[0]) |
---|
21 | efixed = det.getNumberParameter('Efixed')[0] |
---|
22 | return efixed |
---|
23 | |
---|
24 | def conjoincreated(input, name, unit): |
---|
25 | dataX = [] |
---|
26 | dataY = [] |
---|
27 | dataE = [] |
---|
28 | NoSpectra = 0 |
---|
29 | specDet = [] |
---|
30 | for ws in input: |
---|
31 | curWS = mtd[ws] |
---|
32 | nSpec = curWS.getNumberHistograms() |
---|
33 | NoSpectra += nSpec |
---|
34 | axis1 = curWS.getAxis(1) |
---|
35 | for s in range(0, nSpec): |
---|
36 | det = curWS.getDetector(s) |
---|
37 | specDet.append([axis1.spectraNumber(s), det.getID()]) |
---|
38 | readX = curWS.readX(s) |
---|
39 | readY = curWS.readY(s) |
---|
40 | readE = curWS.readE(s) |
---|
41 | for i in range(0, len(readX)): |
---|
42 | dataX.append(readX[i]) |
---|
43 | for i in range(0, len(readY)): |
---|
44 | dataY.append(readY[i]) |
---|
45 | for i in range(0, len(readE)): |
---|
46 | dataE.append(readE[i]) |
---|
47 | conjoined = CreateWorkspace(name, dataX, dataY, dataE, NoSpectra, UnitX=unit).workspace() #Comes with a spectra axis from 1->NoSpectra |
---|
48 | newSpectraAxis = createSpectraAxis(len(specDet)) # We one that has the correct spectrum numbers as defined by the input workspace |
---|
49 | for i in range(len(specDet)): |
---|
50 | newSpectraAxis.setValue(i,specDet[i][0]) |
---|
51 | spectra = conjoined.getSpectrum(i) |
---|
52 | spectra.setDetectorID(specDet[i][1]) |
---|
53 | conjoined.replaceAxis(1, newSpectraAxis) |
---|
54 | # We need to do this each time because a band new workspace has been created with no instrument and hence no detector IDs |
---|
55 | # The first time it will take a bit of time but subsequent runs it will be very quick |
---|
56 | LoadInstrument(Workspace=conjoined,InstrumentName='OSIRIS',RewriteSpectraMap=False) |
---|
57 | |
---|
58 | def CorrRun(ncan,sname,cname,geom): |
---|
59 | workdir = mantid.getConfigProperty('defaultsave.directory') |
---|
60 | samWS=sname |
---|
61 | ext = 'ass' |
---|
62 | assWS = ext |
---|
63 | tmp = mantid.getMatrixWorkspace(samWS) |
---|
64 | s_hist = tmp.getNumberHistograms() |
---|
65 | efixed = getEfixed(samWS) |
---|
66 | ConvertUnits(samWS,samWS,'Wavelength','Indirect',efixed) |
---|
67 | if ncan > 1: |
---|
68 | canWS = cname |
---|
69 | efix = getEfixed(canWS) |
---|
70 | ediff = efix-efixed |
---|
71 | if ediff > 0.001: |
---|
72 | error = 'Can efixed (' +str(efix) + ') not = Sample (' +str(efixed) +')' |
---|
73 | exit(error) |
---|
74 | ConvertUnits(canWS,canWS,'Wavelength','Indirect',efixed) |
---|
75 | corrWS = sname +'_Correct_'+ cname[3:8] |
---|
76 | csamWS = 'SamCor' |
---|
77 | ccanWS = 'CanCor' |
---|
78 | for n in range(0,s_hist): |
---|
79 | ExtractSingleSpectrum(samWS,csamWS,n) |
---|
80 | if ncan == 1: |
---|
81 | if n == 0: |
---|
82 | CloneWorkspace(csamWS,corrWS) # for first time, rename tempWS as output WS |
---|
83 | else: |
---|
84 | list = [corrWS,csamWS] |
---|
85 | conjoincreated(list, corrWS, 'Wavelength') |
---|
86 | # ConjoinWorkspaces(corrWS,csamWS) # subsequent times, add tempWS to output WS |
---|
87 | else: |
---|
88 | ExtractSingleSpectrum(canWS,ccanWS,n) |
---|
89 | smcWS = 'SminusC' |
---|
90 | Minus(csamWS,ccanWS,smcWS) |
---|
91 | |
---|
92 | if n == 0: |
---|
93 | CloneWorkspace(smcWS,corrWS) # for first time, rename tempWS as output WS |
---|
94 | else: |
---|
95 | list = [corrWS,smcWS] |
---|
96 | conjoincreated(list, corrWS, 'Wavelength') |
---|
97 | # ConjoinWorkspaces(corrWS,smcWS) # subsequent times, add tempWS to output WS |
---|
98 | if ncan > 1: |
---|
99 | ConvertUnits(canWS,canWS,'DeltaE','Indirect',efixed) |
---|
100 | ConvertUnits(samWS,samWS,'DeltaE','Indirect',efixed) |
---|
101 | CloneWorkspace(corrWS,'corr') |
---|
102 | ConvertUnits('corr','corrE','DeltaE','Indirect',efixed) |
---|
103 | ConvertUnits(corrWS,corrWS,'DeltaE','Indirect',efixed) |
---|
104 | # mantid.deleteWorkspace(csamWS) |
---|
105 | # mantid.deleteWorkspace(ccanWS) |
---|
106 | mantid.deleteWorkspace(smcWS) |
---|
107 | SaveNexusProcessed(corrWS,op.join(workdir,corrWS+'.nxs')) |
---|
108 | |
---|
109 | def CorrStart(prog,ncan,ana,sname,cname,geom): |
---|
110 | ReadNxsFile(sname,ana,'red') |
---|
111 | tWS = mantid.getMatrixWorkspace(sname) |
---|
112 | s_hist = tWS.getNumberHistograms() # no. of hist/groups in sam |
---|
113 | if ncan == 2: |
---|
114 | ReadNxsFile(cname,ana,'red') |
---|
115 | tWS = mantid.getMatrixWorkspace(cname) |
---|
116 | c_hist = tWS.getNumberHistograms() # no. of hist/groups in sam |
---|
117 | if s_hist != c_hist: # check that no. groups are the same |
---|
118 | error = 'Can histograms (' +str(c_hist) + ') not = Sample (' +str(s_hist) +')' |
---|
119 | exit(error) |
---|
120 | if prog == 'Correct': |
---|
121 | CorrRun(ncan,sname,cname,geom) |
---|
122 | |
---|
123 | |
---|