source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/VSANS/V_Attenuation.ipf @ 1073

Last change on this file since 1073 was 1073, checked in by srkline, 5 years ago

lots of changes here:
many little fixes to clean up TODO items and marke them DONE

changed the handling of the panel "gap" to split the gap evenly. Q-calculations have been re-verified with this change.

re-named the list of "bin Type" values, and added a few more choices. Streamlined how the averaging and plotting works with this list so that it can be more easily modified as different combinations of binning are envisioned. This resulted in a lot of excess code being cut out and replaced with cleaner logic. This change has also been verified to work as intended.

Attenuation is now always calculated from the table. The table also by (NEW) definition has values for the white beam (one waelength) and graphite (multiple possible wavelengths) where the wavelengths are artificially scaled (*1000) or *1e6) so that the interpolations can be done internally without the need for multiple attenuator tables.

File size: 7.3 KB
Line 
1#pragma TextEncoding = "MacRoman"
2#pragma rtGlobals=3             // Use modern global access method and strict wave access.
3
4
5//
6// 7-NOV-2017
7//
8// The attenuator tables are more complex now - since there are more ways to select wavelength
9// - there is the standard velocity selector (no tilt)
10// - a white beam mode (with a non-symmetric distribution)
11// - a graphite monochromator, allowing wavelength selection
12//
13// To accomodate this, I have chosen to alter the table to allow all of the wavelength "modes"
14// to be in the same table for interpolation, by use of the "correct" input wavelength
15// velocity_selector lambda is input as is
16// white beam lambda = (lambda * 1e3)
17// graphite crystal lambda = (lambda * 1e6)
18//
19// in this way, the interpolation will work just fine. Otherwise, the only solution I could see was to
20// have separate tables stored in the header for each of the "modes".
21//
22//
23
24//
25// functions to calculate attenuator values from the tables
26//
27// V_CalculateAttenuationFactor(fname)
28// V_CalculateAttenuationError(fname)
29//
30// interpolate if necessary
31//
32
33//
34// patch entire tables if necessary
35//
36//
37// attenuator tables are currently /N=(n,17)
38
39
40Proc V_LoadCSVAttenTable()
41
42        // this load command will:
43        // load CSV data into a matrix
44        // skip a one-line header
45        // name it "atten0"
46        // prompt for file
47        LoadWave/J/M/D/A=atten/E=1/K=1/L={0,1,0,0,0}                            //will prompt for the file, auto name
48       
49        Rename atten0, atten_values
50
51End
52
53Proc V_LoadCSVAttenErrTable()
54
55        // this load command will:
56        // load CSV data into a matrix
57        // skip a one-line header
58        // name it "atten0"
59        // prompt for file
60        LoadWave/J/M/D/A=atten/E=1/K=1/L={0,1,0,0,0}                            //will prompt for the file, auto name
61       
62        Rename atten0, atten_err
63
64End
65
66// V_writeAttenIndex_table(fname,inW)
67//
68// V_writeAttenIndex_table_err(fname,inW)
69//
70
71Proc V_WriteCSVAttenTable(lo,hi,atten_values)
72        Variable lo,hi
73        String atten_values="atten_values"
74
75        V_fPatchAttenValueTable(lo,hi,$atten_values)
76End
77
78Proc V_WriteCSVAttenErrTable(lo,hi,atten_err)
79        Variable lo,hi
80        String atten_err="atten_err"
81
82        V_fPatchAttenErrTable(lo,hi,$atten_err)
83End
84
85
86// simple utility to patch the attenuator table wave in the file headers
87// lo is the first file number
88// hi is the last file number (inclusive)
89//
90Function V_fPatchAttenValueTable(lo,hi,attenW)
91        Variable lo,hi
92        Wave attenW
93       
94        Variable ii
95        String fname
96       
97//      // check the dimensions of the attenW (8,17)
98//      if (DimSize(attenW, 0) != 8 || DimSize(attenW, 1) != 17 )
99//              Abort "attenuator wave is not of proper dimension (8,17)"
100//      endif
101       
102        //loop over all files
103        for(ii=lo;ii<=hi;ii+=1)
104                fname = V_FindFileFromRunNumber(ii)
105                if(strlen(fname) != 0)
106                        V_writeAttenIndex_table(fname,attenW)                   
107                else
108                        printf "run number %d not found\r",ii
109                endif
110        endfor
111       
112        return(0)
113End
114
115// simple utility to patch the attenuator error (std dev) wave in the file headers
116// lo is the first file number
117// hi is the last file number (inclusive)
118//
119Function V_fPatchAttenErrTable(lo,hi,attenW)
120        Variable lo,hi
121        Wave attenW
122       
123        Variable ii
124        String fname
125       
126//      // check the dimensions of the attenW (8,17)
127//      if (DimSize(attenW, 0) != 8 || DimSize(attenW, 1) != 17 )
128//              Abort "attenuator wave is not of proper dimension (8,17)"
129//      endif
130       
131        //loop over all files
132        for(ii=lo;ii<=hi;ii+=1)
133                fname = V_FindFileFromRunNumber(ii)
134                if(strlen(fname) != 0)
135                        V_writeAttenIndex_table_err(fname,attenW)                       
136                else
137                        printf "run number %d not found\r",ii
138                endif
139        endfor
140       
141        return(0)
142End
143
144
145
146//////////////////////
147//
148// function to calculate the attenuation factor from the table in the file
149//
150// fill in a "dummy" wavelength for White Beam and graphite
151// *= 1e3 for White Beam
152// *= 1e6 for graphite
153// use these dummy values just for the lookup table
154//
155// TODO -- need the enumerated values for the monochromator type
156// TODO -- V_getMonochromatorType(fname) is NOT written correctly by NICE
157// TODO -- determine the dimensions of the wave, don't hard-wire
158// TODO -- update to use separate tables for each monochromator mode
159// TODO -- (same updates for the error table)
160//
161Function V_CalculateAttenuationFactor(fname)
162        String fname
163       
164        Variable val,lambda,numAtt
165        String monoType
166       
167        numAtt = V_getAtten_number(fname)
168        lambda = V_getWavelength(fname)
169       
170        // TODO -- need to switch on "type"
171        //  == velocity_selector || ?? for white beam || graphite
172//      monoType = V_getMonochromatorType(fname)
173
174        monoType = V_DeduceMonochromatorType(fname)
175        print monoType
176
177        // set a fake wavelength for the interpolation or get out
178        strswitch(monoType)     // string switch
179                case "velocity_selector":       // execute if case matches expression
180                        // use lambda as-is
181                        break           // exit from switch
182                case "white_beam":      // execute if case matches expression
183                        lambda *= 1e3
184                        break
185                case "crystal":
186                        lambda *= 1e6
187                        break
188                default:                        // optional default expression executed
189                        Abort "Monochromator type could not be determined in V_CalculateAttenuationFactor"              // when no case matches
190        endswitch
191       
192       
193        Wave w = V_getAttenIndex_table(fname)           // N=(x,17)
194        Variable num = DimSize(w,0)
195        Make/O/D/N=(num) tmpVal,tmpLam                                         
196       
197        tmpVal = w[p][numAtt+1]         // offset by one, 1st column is wavelength
198        tmpLam = w[p][0]
199        val = interp(lambda, tmpLam, tmpVal )
200        Print "Calculated Atten = ",val
201       
202        //killwaves/Z tmpVal,tmpLam
203        return(val)
204       
205End
206
207//////////////////////
208//
209// function to calculate the attenuation error from the table in the file
210//
211// fill in a "dummy" wavelength for White Beam and graphite
212// *= 1e3 for White Beam
213// *= 1e6 for graphite
214// use these dummy values just for the lookup table
215//
216// TODO -- need the enumerated values for the monochromator type
217// TODO -- V_getMonochromatorType(fname) is NOT written correctly by NICE
218//
219//
220Function V_CalculateAttenuationError(fname)
221        String fname
222       
223        Variable val,lambda,numAtt
224        String monoType
225       
226        numAtt = V_getAtten_number(fname)
227        lambda = V_getWavelength(fname)
228       
229        // TODO -- need to switch on "type"
230        //  == velocity_selector || ?? for white beam || crystal
231//      monoType = V_getMonochromatorType(fname)
232       
233        monoType = V_DeduceMonochromatorType(fname)
234        print monoType
235        // set a fake wavelength for the interpolation or get out
236        strswitch(monoType)     // string switch
237                case "velocity_selector":       // execute if case matches expression
238                        // use lambda as-is
239                        break           // exit from switch
240                case "white_beam":      // execute if case matches expression
241                        lambda *= 1e3
242                        break
243                case "crystal":
244                        lambda *= 1e6
245                        break
246                default:                        // optional default expression executed
247                        Abort "Monochromator type could not be determined in V_CalculateAttenuationError"               // when no case matches
248        endswitch       
249       
250        Wave w = V_getAttenIndex_error_table(fname)             // N=(x,17)
251        Variable num = DimSize(w,0)
252        Make/O/D/N=(num) tmpVal,tmpLam
253       
254        tmpVal = w[p][numAtt+1]         // offset by one, 1st column is wavelength
255        tmpLam = w[p][0]
256        val = interp(lambda, tmpLam, tmpVal )
257       
258        //killwaves/Z tmpVal,tmpLam
259        return(val)
260       
261End
262
263// TODO -- this may not correctly mimic the enumerated type of the file
264//  but I need to fudge this somehow
265//
266// returns null string if the type cannot be deduced, calling procedure is responsible
267//  for properly handling this error condition
268//
269Function/S V_DeduceMonochromatorType(fname)
270        String fname
271       
272        String typeStr=""
273
274        if(cmpstr(V_getVelSelStatus(fname),"IN") == 0)
275                typeStr = "velocity_selector"
276        endif
277       
278        if(cmpstr(V_getWhiteBeamStatus(fname),"IN") == 0)
279                typeStr = "white_beam"
280        endif
281       
282        if(cmpstr(V_getCrystalStatus(fname),"IN") == 0)
283                typeStr = "crystal"
284        endif   
285       
286        return(typeStr)
287End
Note: See TracBrowser for help on using the repository browser.