source: sans/Analysis/branches/ajj_23APR07/IGOR_Package_Files/Put in User Procedures/SANS_Models_v3.00/USANS_SlitSmearing.ipf @ 238

Last change on this file since 238 was 238, checked in by ajj, 15 years ago

Update to USANS slit-smearing on data load.

File size: 6.5 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma version=3.00
3#pragma IgorVersion=6.0
4
5//Functions for doing USANS Slit smearing by method of weight matrix
6//Routines originally from J Barker fortran code
7//Translated to IGOR by M-H Kim
8//Updated to use IGOR features and integrated into SANS Macros by A J Jackson
9//
10// AJJ
11// July 26 2007 - Modified functions to work with new SANS Analysis Macros
12//     - pass basestr to functions to determine wave names and avoid globals
13//     - pass dQv to functions to  avoid globals
14//     - pass N to CalcR to avoid globals
15
16//
17
18Function USANS_CalcWeights(basestr, dQv)
19        String basestr
20        Variable dQv
21
22        Variable/G USANS_N=numpnts($(basestr+"_q"))
23        Variable/G USANS_dQv = dQv
24        String/G root:myGlobals:USANS_basestr = basestr
25
26        Make/D/O/N=(USANS_N,USANS_N) $(basestr+"_res")
27        Make/D/O/N=(USANS_N,USANS_N) W1mat
28        Make/D/O/N=(USANS_N,USANS_N) W2mat
29        Make/D/O/N=(USANS_N,USANS_N) Rmat
30        Wave weights = $(basestr+"_res")
31
32        //Variable/G USANS_m = EnterSlope(baseStr)
33        Variable/G USANS_m = -4
34        Variable/G USANS_slope_numpnts = 10
35       
36        Print "---- Calculating Weighting Matrix for USANS Data ----"
37       
38        EnterSlope(basestr)
39       
40        //Deal with broken Pauseforuser
41        SetDataFolder $("root:"+basestr)
42       
43        print USANS_m
44
45        if (USANS_m == 999)
46                KillWaves/Z  $(basestr+"_res"),W1Mat,W2mat,Rmat
47                return(1)
48        endif
49       
50        Variable tref = startMSTimer
51        print "Calculating W1..."
52        W1mat = (p <= q ) && (q <= USANS_N-2) ? CalcW1(p,q)  : 0
53        print "Calculating W2..."
54        W2mat = (p+1 <= q ) && (q <= USANS_N-1) ?  CalcW2(p,q) : 0
55        print "Calculating Remainders..."
56        Rmat = (q == USANS_N-1) ? CalcR(p) : 0
57//      print "Summing weights..."
58        weights = W1mat + W2mat + Rmat
59        print "Done"
60        Variable ms = stopMSTimer(tref)
61        print "Time elapsed = ", ms/1e6, "s"
62        //return Weights
63End
64
65Function EnterSlope(baseStr)
66        String baseStr
67       
68        NVAR USANS_m,USANS_slope_numpnts
69       
70//      Variable slope=-4
71
72//      Prompt slope "Enter a slope for the file \""+ baseStr + "\""
73//      DoPrompt "Enter Slope", slope
74//              If (V_Flag)
75//                      return (999)                    //return a bogus slope if the user canceled
76//              Endif
77//      print "slope=", slope
78//      return slope
79
80        NewPanel /W=(600,300,1000,700)/N=USANS_Slope as "USANS Slope Extrapolation"
81        SetDrawLayer UserBack
82        Button button_OK,pos={294,317},size={100,20},title="Accept Slope",font="Geneva"
83        Button button_OK,proc=USANS_Slope_ButtonProc
84        //Button button_Cancel,pos={233,317},size={55,20},title="Cancel",font="Geneva"
85        //Button button_Cancel,proc=USANS_Slope_ButtonProc
86        SetVariable setvar_numpnts, pos={43,350}, size={150,19}, title="# Points",fSize=13
87        SetVariable setvar_numpnts, value= USANS_slope_numpnts
88        SetVariable setvar_Slope,pos={43,321},size={100,19},title="Slope",fSize=13
89        SetVariable setvar_Slope,limits={-inf,inf,0},value= USANS_m
90        Display/W=(9,6,402,305)/HOST=USANS_Slope $(basestr+"_i") vs $(basestr+"_q")
91        RenameWindow #,SlopePlot
92        ModifyGraph log=1
93        SetActiveSubwindow ##
94       
95        //Print TraceNameList("USANS_Slope#SlopePlot",";",1)
96       
97        USANS_CalculateSlope(basestr,USANS_slope_numpnts)
98       
99        PauseForUser  USANS_Slope       
100//      return slope
101       
102End
103
104Function USANS_CalculateSlope(basestr, calcpoints)
105        String basestr
106        Variable calcpoints
107       
108        Wave iw = $(basestr+"_i")
109        Wave qw = $(basestr+"_i")
110        Wave sw = $(basestr+"_i")
111
112        Variable num_extr=25
113        NVAR nend = USANS_slope_numpnts
114        // Make extra waves for extrapolation
115        // Taken from DSM_SetExtrWaves
116               
117        Make/O/D/N=(num_extr) extr_hqq,extr_hqi
118        extr_hqi=1              //default values
119
120        //set the q-range
121        Variable qmax,num
122
123        num=numpnts(qw)
124        qmax=6*qw[num-1]
125       
126        extr_hqq = qw[num-1] + x * (qmax-qw[num-1])/num_extr
127               
128       
129        // Modifed from DSM_DoExtraploate in LakeDesmearing_JB.ipf
130        // which is part of the USANS Reduction macros
131       
132               
133        //      Wave extr_lqi=extr_lqi
134        //      Wave extr_lqq=extr_lqq
135        //      Wave extr_hqi=extr_hqi
136        //      Wave extr_hqq=extr_hqq
137                Variable/G V_FitMaxIters=300
138                Variable/G V_fitOptions=4               //suppress the iteration window
139                Variable retval
140                num=numpnts(iw)
141       
142                Make/O/D P_coef={0,1,-4}                        //input
143        //      Make/O/T Constr={"K2<0","K2 > -20"}
144                //(set background to zero and hold fixed)
145                CurveFit/H="100" Power kwCWave=P_coef  iw[(num-1-nend),(num-1)] /X=qw /W=sw /D
146                extr_hqi=P_coef[0]+P_coef[1]*extr_hqq^P_coef[2]
147       
148                Printf "Smeared Power law exponent = %g\r",P_coef[2]
149                Printf "**** For Desmearing, use a Power law exponent of %5.1f\r",P_coef[2]-1
150       
151                retVal = P_coef[2]-1                   
152                return(retVal)
153               
154End
155
156
157Function USANS_Slope_ButtonProc(ctrlName) : ButtonControl
158        String ctrlName
159       
160        NVAR USANS_m
161       
162        strswitch (ctrlName)
163                case "button_OK":
164                        ControlUpdate/W=USANS_Slope setvar_Slope
165                        DoWindow/K USANS_Slope
166                        break
167                case "button_Cancel":
168                        DoWindow/K USANS_Slope
169                        USANS_m = 999
170                        break
171        endswitch
172         
173
174End
175
176Function CalcW1(i,j)
177
178        Variable i,j
179        SVAR USANS_basestr=root:myGlobals:USANS_basestr
180        SetDataFolder $("root:"+USANS_basestr)
181
182       
183       
184        NVAR dQv = USANS_dQv
185       
186        Variable UU,UL,dqj,rU,rL,wU,wL,dqw
187        Wave Qval = $(USANS_basestr+"_q")
188       
189        UU =sqrt(Qval[j+1]^2-Qval[i]^2)
190        UL = sqrt(Qval[j]^2-Qval[i]^2)
191        dqw = Qval[j+1]-Qval[j]
192        rU = sqrt(UU^2+Qval[i]^2)
193        rL = sqrt(UL^2+Qval[i]^2)
194       
195        wU = (1.0/dQv)*(Qval[j+1]*UU/dqw - 0.5*UU*rU/dqw - 0.5*Qval[i]^2*ln(UU+rU)/dqw )
196        wL = (1.0/dQv)*(Qval[j+1]*UL/dqw - 0.5*UL*rL/dqw - 0.5*Qval[i]^2*ln(UL+rL)/dqw )
197       
198        Return wU-wL
199
200End
201
202Function CalcW2(i,j)
203
204        Variable i,j
205       
206        SVAR USANS_basestr=root:myGlobals:USANS_basestr
207        SetDataFolder $("root:"+USANS_basestr)
208
209
210
211        NVAR dQv = USANS_dQv
212       
213        variable UU,UL,dqw,rU,rL,wU,wL
214       
215        Wave Qval = $(USANS_basestr+"_q")
216
217        UU = sqrt(Qval[j]^2-Qval[i]^2)                 
218        UL = sqrt(Qval[j-1]^2-Qval[i]^2)               
219        dqw = Qval[j]-Qval[j-1]                 
220        rU = sqrt(UU^2+Qval[i]^2)
221        rL = sqrt(UL^2+Qval[i]^2)
222        wU = (1.0/dQv)*( -Qval[j-1]*UU/dqw + 0.5*UU*rU/dqw + 0.5*Qval[i]^2*ln(UU+rU)/dqw )
223        wL = (1.0/dQv)*( -Qval[j-1]*UL/dqw + 0.5*UL*rL/dqw + 0.5*Qval[i]^2*ln(UL+rL)/dqw )
224
225        Return wU-wL
226
227End
228
229Function CalcR(i)
230
231        Variable i
232
233        SVAR USANS_basestr=root:myGlobals:USANS_basestr
234        SetDataFolder $("root:"+USANS_basestr)
235
236
237        NVAR m = USANS_m
238        NVAR N = USANS_N
239        NVAR dQv = USANS_dQv
240       
241        Variable retval
242        Wave Qval = $(USANS_basestr+"_q")
243        Wave Ival = $(USANS_basestr+"_i")
244        Variable/G USANS_intQpt = Qval[i]
245       
246        Variable lower = sqrt(qval[N-1]^2-qval[i]^2)
247        Variable upper = dQv
248
249        if (i == N)
250                lower = 0
251        endif
252       
253        retval = Integrate1D(Remainder,lower,upper)
254       
255        retval *= 1/dQv
256       
257        Return retval
258
259End
260
261Function Remainder(i)
262       
263        Variable i
264       
265        SVAR USANS_basestr=root:myGlobals:USANS_basestr
266        SetDataFolder $("root:"+USANS_basestr)
267
268        NVAR m = USANS_m
269        NVAR qi = USANS_intQpt
270        NVAR N = USANS_N
271        WAVE Qval = $(USANS_basestr+"_q")       
272        Variable retVal
273       
274        retVal=Qval[N-1]^(-m)*(i^2+qi^2)^(m/2)
275
276        return retval
277
278End
Note: See TracBrowser for help on using the repository browser.