source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/VSANS/VC_DetectorBinning_Utils.ipf @ 1128

Last change on this file since 1128 was 1128, checked in by srkline, 4 years ago

multiple changes to improve the functionality of VCALC

more values are reported, and the IQ plot now accounts for a beam stop shadowing the low q region. Qmin and qmax values are reported for each panel. the beam intensity value is more realistic, with correct SSD values.

File size: 46.7 KB
Line 
1#pragma rtGlobals=3             // Use modern global access method and strict wave access.
2
3/////////////////////////
4//
5// Utility functions to:
6//              calculate Q, Qx, Qy, Qz
7//              fill the detector panels with simulated data (the model functions are here)
8//              bin the 2D detector to 1D I(Q) based on Q and deltaQ (bin width)
9//
10/////////////////////////
11
12
13
14
15// x- hard wired for a sphere - change this to allow minimal selections and altering of coefficients
16// x- add the "fake" 2D simulation to fill the panels which are then later averaged as I(Q)
17//
18// NOTE - this is a VCALC only routine, so it's not been made completely generic
19//
20Function FillPanel_wModelData(det,qTot,type)
21        Wave det,qTot
22        String type
23
24//      SetDataFolder root:Packages:NIST:VSANS:VCALC:Front
25
26        // q-values and detector arrays already allocated and calculated
27        Duplicate/O det tmpInten,tmpSig,prob_i
28               
29        Variable imon,trans,thick,sdd,pixSizeX,pixSizeY,sdd_offset
30
31        //imon = VC_BeamIntensity()*CountTime
32        imon = VCALC_getImon()          //TODO: currently from the panel, not calculated
33        trans = 0.8
34        thick = 0.1
35       
36        // need SDD
37        // need pixel dimensions
38        // nominal sdd in cm, offset in cm, want result in cm !
39
40        sdd = VC_getSDD(type)   // setback is already included  VCALC_getTopBottomSDDSetback(type)              // result is sdd in [cm]
41
42        pixSizeX = VCALC_getPixSizeX(type)              // cm
43        pixSizeY = VCALC_getPixSizeY(type)
44       
45       
46        //?? pick the function from a popup on the panel? (bypass the analysis panel, or maybe it's better to
47        //  keep the panel to keep people used to using it.)
48        // peak @ 0.1 ~ AgBeh
49        //      Make/O/D coef_BroadPeak = {1e-9, 3, 20, 100.0, 0.1,3,0.1}               
50        //
51        // peak @ 0.015 in middle of middle detector, maybe not "real" vycor, but that is to be resolved
52        //      Make/O/D coef_BroadPeak = {1e-9, 3, 20, 500.0, 0.015,3,0.1}     
53        //
54        //
55        Variable addEmpBgd=0
56       
57// TODOHIGHRES
58// this is a slow step - try to figure out how to multithread this efficiently. simply adding the
59//  keyword has little effect. maybe only do this for "B", maybe rewrite the calculation to not use pq indexing
60//             
61        String funcStr = VCALC_getModelFunctionStr()
62        strswitch(funcStr)
63                case "Big Debye":
64                        tmpInten = VC_Debye(100,3000,0.0001,qTot[p][q])
65                        break
66                case "Big Sphere":
67                        tmpInten = VC_SphereForm(1,2000,1e-6,0.01,qTot[p][q])   
68                        break
69                case "Debye":
70                        MultiThread tmpInten = VC_Debye(10,300,0.0001,qTot[p][q])
71                        break
72                case "Sphere":
73                        tmpInten = VC_SphereForm(1,60,1e-6,0.001,qTot[p][q])   
74                        break
75                case "AgBeh":
76                        tmpInten = VC_BroadPeak(1e-11,3,20,100.0,0.1,3,0.1,qTot[p][q])
77                        break
78                case "Vycor":
79                        tmpInten = VC_BroadPeak(1e-9,3,20,500.0,0.015,3,0.1,qTot[p][q])
80                        break   
81                case "Empty Cell":
82                        tmpInten = VC_EC_Empirical(2.2e-12,3.346,0.0065,9.0,0.016,qTot[p][q])
83                        break
84                case "Blocked Beam":
85                        tmpInten = VC_BlockedBeam(0.01,qTot[p][q])
86                        break
87                case "Debye +":
88                        tmpInten = VC_Debye(10,300,0.0001,qTot[p][q])
89                        addEmpBgd = 1
90                        break
91                case "AgBeh +":
92                        tmpInten = VC_BroadPeak(1e-11,3,20,100.0,0.1,3,0.1,qTot[p][q])
93                        addEmpBgd = 1
94                        break
95                case "Empty Cell +":
96                        tmpInten = VC_EC_Empirical(2.2e-12,3.346,0.0065,9.0,0.016,qTot[p][q])
97                        tmpInten += VC_BlockedBeam(0.01,qTot[p][q])
98                        break
99                default:
100                        tmpInten = VC_Debye(10,300,0.1,qTot[p][q])
101        endswitch
102
103
104        if(addEmpBgd == 1)
105                tmpInten += VC_EC_Empirical(2.2e-12,3.346,0.0065,9.0,0.016,qTot[p][q])
106                tmpInten += VC_BlockedBeam(0.01,qTot[p][q])
107        endif
108
109       
110// x- this is faked to get around the singularity at the center of the back detector
111//
112//
113        if(cmpstr(type,"B") == 0)
114                Variable nx,ny,px,py
115                nx = VCALC_get_nPix_X(type)
116                ny = VCALC_get_nPix_Y(type)
117                px = trunc(nx/2)
118                py = trunc(ny/2)
119               
120                tmpInten[px][py] = (tmpInten[px][py+1] + tmpInten[px][py-1])/2
121        endif
122
123
124
125///////////////
126//      // calculate the scattering cross section simply to be able to estimate the transmission
127//      Variable sig_sas=0
128//     
129//      // remember that the random deviate is the coherent portion ONLY - the incoherent background is
130//      // subtracted before the calculation.
131//      CalculateRandomDeviate(funcUnsmeared,$coefStr,wavelength,"root:Packages:NIST:SAS:ran_dev",sig_sas)
132//
133//      if(sig_sas > 100)
134//              DoAlert 0,"SAS cross section > 100. Estimates of multiple scattering are unreliable. Choosing a model with a well-defined Rg may help"
135//      endif           
136//
137//      // calculate the multiple scattering fraction for display (10/2009)
138//      Variable ii,nMax=10,tau
139//      mScat=0
140//      tau = thick*sig_sas
141//      // this sums the normalized scattering P', so the result is the fraction of multiply coherently scattered
142//      // neutrons out of those that were scattered
143//      for(ii=2;ii<nMax;ii+=1)
144//              mScat += tau^(ii)/factorial(ii)
145////            print tau^(ii)/factorial(ii)
146//      endfor
147//      estTrans = exp(-1*thick*sig_sas)                //thickness and sigma both in units of cm
148//      mscat *= (estTrans)/(1-estTrans)
149//
150////    if(mScat > 0.1)         //  Display warning
151//
152//      Print "Sig_sas = ",sig_sas
153////////////////////
154       
155        prob_i = trans*thick*pixSizeX*pixSizeY/(sdd)^2*tmpInten                 //probability of a neutron in q-bin(i)
156               
157        tmpInten = (imon)*prob_i                //tmpInten is not the model calculation anymore!!
158
159
160/// **** can I safely assume a Gaussian error in the count rate??
161        tmpSig = sqrt(tmpInten)         // corrected based on John's memo, from 8/9/99
162
163        tmpInten += gnoise(tmpSig)
164        tmpInten = (tmpInten[p][q] < 0) ? 0 : tmpInten[p][q]                    // MAR 2013 -- is this the right thing to do
165        tmpInten = trunc(tmpInten)
166               
167       
168        det = tmpInten
169
170// if I want "absolute" scale -- then I lose the integer nature of the detector (but keep the random)
171//      det /= trans*thick*pixSizeX*pixSizeY/(sdd)^2*imon
172
173       
174        KillWaves/Z tmpInten,tmpSig,prob_i     
175        SetDataFolder root:
176
177        return(0)
178End
179
180
181// For a given detector panel, calculate the q-values
182// -work with everything as arrays
183// Input needed:
184// detector data
185// detector type (LRTB?)
186// beam center (may be off the detector)
187// SDD
188// lambda
189//
190// pixel dimensions for detector type (global constants)
191// - data dimensions read directly from array
192//
193// --What is calculated:
194// array of Q
195// array of qx,qy,qz
196// array of error already exists
197//
198//
199// -- sdd in cm
200// -- lambda in Angstroms
201Function VC_Detector_2Q(data,qTot,qx,qy,qz,xCtr,yCtr,sdd,lam,pixSizeX,pixSizeY)
202        Wave data,qTot,qx,qy,qz
203        Variable xCtr,yCtr,sdd,lam,pixSizeX,pixSizeY
204               
205        // loop over the array and calculate the values - this is done as a wave assignment
206// TODO -- be sure that it's p,q -- or maybe p+1,q+1 as used in WriteQIS.ipf   
207        qTot = VC_CalcQval(p,q,xctr,yctr,sdd,lam,pixSizeX,pixSizeY)
208        qx = VC_CalcQX(p,q,xctr,yctr,sdd,lam,pixSizeX,pixSizeY)
209        qy = VC_CalcQY(p,q,xctr,yctr,sdd,lam,pixSizeX,pixSizeY)
210        qz = VC_CalcQZ(p,q,xctr,yctr,sdd,lam,pixSizeX,pixSizeY)
211       
212        return(0)
213End
214
215
216// for testing, a version that will calculate the q-arrays for VCALC based on whatever nonlinear coefficients
217// exist in the RAW data folder
218//
219// reverts to the "regular" linear detector if waves not found or a flag is set
220//
221// need to call the VSANS V_CalcQval routines (these use the real-space distance, not pixel dims)
222//
223// ***** everything passed in is [cm], except for wavelength [A]
224//
225// ****  TODO :: calibration constants are still in [mm]
226//
227//
228// TODO:
229// -- tube width is hard-wired in
230//
231//
232Function VC_Detector_2Q_NonLin(data,qTot,qx,qy,qz,xCtr,yCtr,sdd,lam,pixSizeX,pixSizeY,detStr)
233        Wave data,qTot,qx,qy,qz
234        Variable xCtr,yCtr,sdd,lam,pixSizeX,pixSizeY
235        String detStr
236       
237        String destPath = "root:Packages:NIST:VSANS:VCALC"
238       
239        // be sure that the real distance waves exist
240        // TODO -- this may not be the best location?
241
242// calibration waves do not exist yet, so make some fake ones   '
243        // do I count on the orientation as an input, or do I just figure it out on my own?
244        String orientation
245        Variable dimX,dimY
246        dimX = DimSize(data,0)
247        dimY = DimSize(data,1)
248        if(dimX > dimY)
249                orientation = "horizontal"
250        else
251                orientation = "vertical"
252        endif
253       
254        if(cmpstr(orientation,"vertical")==0)
255                Make/O/D/N=(3,48) tmpCalib
256                // for the "tall" L/R banks
257                tmpCalib[0][] = -512
258                tmpCalib[1][] = 8
259                tmpCalib[2][] = 0
260        else
261                Make/O/D/N=(3,48) tmpCalib
262                // for the "short" T/B banks
263                tmpCalib[0][] = -256
264                tmpCalib[1][] = 4
265                tmpCalib[2][] = 0
266        endif
267        // override if back panel
268        if(cmpstr(detStr,"B") == 0)
269                // and for the back detector "B"
270                Make/O/D/N=3 tmpCalibX,tmpCalibY
271                tmpCalibX[0] = VCALC_getPixSizeX(detStr)                        // pixel size in [cm]  VCALC_getPixSizeX(detStr) is [cm]
272                tmpCalibX[1] = 1
273                tmpcalibX[2] = 10000
274                tmpCalibY[0] = VCALC_getPixSizeY(detStr)                        // pixel size in [cm]  VCALC_getPixSizeX(detStr) is [cm]
275                tmpCalibY[1] = 1
276                tmpcalibY[2] = 10000
277        endif
278       
279//      Wave w_calib = V_getDetTube_spatialCalib("VCALC",detStr)
280        Variable tube_width = 8.4                       // TODO: UNITS!!! Hard-wired value in [mm]
281        if(cmpstr(detStr,"B") == 0)
282                V_NonLinearCorrection_B("VCALC",data,tmpCalibX,tmpCalibY,detStr,destPath)
283                // beam center is in pixels, so use the old routine
284                V_ConvertBeamCtrPix_to_mmB("VCALC","B",destPath)
285        else
286                V_NonLinearCorrection("VCALC",data,tmpCalib,tube_width,detStr,destPath)
287        endif
288                               
289        Wave/Z data_realDistX = $(destPath + ":entry:instrument:detector_"+detStr+":data_realDistX")
290        Wave/Z data_realDistY = $(destPath + ":entry:instrument:detector_"+detStr+":data_realDistY")
291        NVAR gUseNonLinearDet = root:Packages:NIST:VSANS:VCALC:gUseNonLinearDet
292
293        if(kBCTR_CM && cmpstr(detStr,"B") != 0)
294                if(gUseNonLinearDet && WaveExists(data_realDistX) && WaveExists(data_realDistY))
295                        // beam ctr is in cm already
296
297                        // calculate all of the q-values
298                        qTot = V_CalcQval(p,q,xCtr,yCtr,sdd,lam,data_realDistX,data_realDistY)
299                        qx = V_CalcQX(p,q,xCtr,yCtr,sdd,lam,data_realDistX,data_realDistY)
300                        qy = V_CalcQY(p,q,xCtr,yCtr,sdd,lam,data_realDistX,data_realDistY)
301                        qz = V_CalcQZ(p,q,xCtr,yCtr,sdd,lam,data_realDistX,data_realDistY)
302               
303        //              Print "det, x_mm, y_mm ",detStr,num2str(newX),num2str(newY)
304        //              Print "det, x_pix, y_pix ",detStr,num2str(xCtr),num2str(yCtr)
305                else
306                        // do the q-calculation using linear detector
307                        //VC_Detector_2Q(data,qTot,qx,qy,qz,xCtr,yCtr,sdd,lam,pixSizeX,pixSizeY)
308                        qTot = V_CalcQval(p,q,xCtr,yCtr,sdd,lam,data_realDistX,data_realDistY)
309                        qx = V_CalcQX(p,q,xCtr,yCtr,sdd,lam,data_realDistX,data_realDistY)
310                        qy = V_CalcQY(p,q,xCtr,yCtr,sdd,lam,data_realDistX,data_realDistY)
311                        qz = V_CalcQZ(p,q,xCtr,yCtr,sdd,lam,data_realDistX,data_realDistY)
312                endif   
313       
314       
315        else
316        // using the old calculation with beam center in pixels
317                if(gUseNonLinearDet && WaveExists(data_realDistX) && WaveExists(data_realDistY))
318                        // convert the beam centers to mm
319//                      String orientation
320                        Variable newX,newY
321                        dimX = DimSize(data_realDistX,0)
322                        dimY = DimSize(data_realDistX,1)
323                        if(dimX > dimY)
324                                orientation = "horizontal"
325                        else
326                                orientation = "vertical"
327                        endif
328                       
329               
330                //
331                        if(cmpstr(orientation,"vertical")==0)
332                                //      this is data dimensioned as (Ntubes,Npix)
333                                newX = tube_width*xCtr
334                                newY = data_realDistY[0][yCtr]
335                        else
336                                //      this is data (horizontal) dimensioned as (Npix,Ntubes)
337                                newX = data_realDistX[xCtr][0]
338                                newY = tube_width*yCtr
339                        endif   
340       
341                        //if detector "B", different calculation for the centers (not tubes)
342                        if(cmpstr(detStr,"B")==0)
343                                newX = data_realDistX[xCtr][0]
344                                newY = data_realDistY[0][yCtr]
345                                //newX = xCtr
346                                //newY = yCtr
347                        endif           
348                                       
349                        // calculate all of the q-values
350                        qTot = V_CalcQval(p,q,newX,newY,sdd,lam,data_realDistX,data_realDistY)
351                        qx = V_CalcQX(p,q,newX,newY,sdd,lam,data_realDistX,data_realDistY)
352                        qy = V_CalcQY(p,q,newX,newY,sdd,lam,data_realDistX,data_realDistY)
353                        qz = V_CalcQZ(p,q,newX,newY,sdd,lam,data_realDistX,data_realDistY)
354               
355        //              Print "det, x_mm, y_mm ",detStr,num2str(newX),num2str(newY)
356        //              Print "det, x_pix, y_pix ",detStr,num2str(xCtr),num2str(yCtr)
357                else
358                        // do the q-calculation using linear detector
359                        VC_Detector_2Q(data,qTot,qx,qy,qz,xCtr,yCtr,sdd,lam,pixSizeX,pixSizeY)
360                endif   
361       
362        endif
363       
364        KillWaves/Z tmpCalib,tmpCalibX,tmpCalibY
365       
366        return(0)
367End
368
369
370//////////////////////
371// NOTE: The Q calculations are different than what is in GaussUtils in that they take into
372// accout the different x/y pixel sizes and the beam center not being on the detector -
373// off a different edge for each LRTB type
374/////////////////////
375
376//function to calculate the overall q-value, given all of the necesary trig inputs
377//and are in detector coordinates (1,128) rather than axis values
378//the pixel locations need not be integers, reals are ok inputs
379//sdd is in [cm]
380//wavelength is in Angstroms
381//
382//returned magnitude of Q is in 1/Angstroms
383//
384//
385Function VC_CalcQval(xaxval,yaxval,xctr,yctr,sdd,lam,pixSizeX,pixSizeY)
386        Variable xaxval,yaxval,xctr,yctr,sdd,lam,pixSizeX,pixSizeY
387       
388        Variable dx,dy,qval,two_theta,dist
389               
390
391        dx = (xaxval - xctr)*pixSizeX           //delta x in cm
392        dy = (yaxval - yctr)*pixSizeY           //delta y in cm
393        dist = sqrt(dx^2 + dy^2)
394       
395        two_theta = atan(dist/sdd)
396
397        qval = 4*Pi/lam*sin(two_theta/2)
398       
399        return qval
400End
401
402//calculates just the q-value in the x-direction on the detector
403//input/output is the same as CalcQval()
404//ALL inputs are in detector coordinates
405//
406//sdd is in [cm]
407//wavelength is in Angstroms
408//
409// repaired incorrect qx and qy calculation 3 dec 08 SRK (Lionel and C. Dewhurst)
410// now properly accounts for qz
411//
412Function VC_CalcQX(xaxval,yaxval,xctr,yctr,sdd,lam,pixSizeX,pixSizeY)
413        Variable xaxval,yaxval,xctr,yctr,sdd,lam,pixSizeX,pixSizeY
414
415        Variable qx,qval,phi,dx,dy,dist,two_theta
416       
417        qval = VC_CalcQval(xaxval,yaxval,xctr,yctr,sdd,lam,pixSizeX,pixSizeY)
418       
419//      sdd *=100               //convert to cm
420        dx = (xaxval - xctr)*pixSizeX           //delta x in cm
421        dy = (yaxval - yctr)*pixSizeY           //delta y in cm
422        phi = V_FindPhi(dx,dy)
423       
424        //get scattering angle to project onto flat detector => Qr = qval*cos(theta)
425        dist = sqrt(dx^2 + dy^2)
426        two_theta = atan(dist/sdd)
427
428        qx = qval*cos(two_theta/2)*cos(phi)
429       
430        return qx
431End
432
433//calculates just the q-value in the y-direction on the detector
434//input/output is the same as CalcQval()
435//ALL inputs are in detector coordinates
436//sdd is in [cm]
437//wavelength is in Angstroms
438//
439// repaired incorrect qx and qy calculation 3 dec 08 SRK (Lionel and C. Dewhurst)
440// now properly accounts for qz
441//
442Function VC_CalcQY(xaxval,yaxval,xctr,yctr,sdd,lam,pixSizeX,pixSizeY)
443        Variable xaxval,yaxval,xctr,yctr,sdd,lam,pixSizeX,pixSizeY
444       
445        Variable dy,qval,dx,phi,qy,dist,two_theta
446       
447        qval = VC_CalcQval(xaxval,yaxval,xctr,yctr,sdd,lam,pixSizeX,pixSizeY)
448       
449//      sdd *=100               //convert to cm
450        dx = (xaxval - xctr)*pixSizeX           //delta x in cm
451        dy = (yaxval - yctr)*pixSizeY           //delta y in cm
452        phi = V_FindPhi(dx,dy)
453       
454        //get scattering angle to project onto flat detector => Qr = qval*cos(theta)
455        dist = sqrt(dx^2 + dy^2)
456        two_theta = atan(dist/sdd)
457       
458        qy = qval*cos(two_theta/2)*sin(phi)
459       
460        return qy
461End
462
463//calculates just the z-component of the q-vector, not measured on the detector
464//input/output is the same as CalcQval()
465//ALL inputs are in detector coordinates
466//sdd is in [cm]
467//wavelength is in Angstroms
468//
469// not actually used, but here for completeness if anyone asks
470//
471Function VC_CalcQZ(xaxval,yaxval,xctr,yctr,sdd,lam,pixSizeX,pixSizeY)
472        Variable xaxval,yaxval,xctr,yctr,sdd,lam,pixSizeX,pixSizeY
473       
474        Variable dy,qval,dx,phi,qz,dist,two_theta
475       
476        qval = VC_CalcQval(xaxval,yaxval,xctr,yctr,sdd,lam,pixSizeX,pixSizeY)
477       
478//      sdd *=100               //convert to cm
479       
480        //get scattering angle to project onto flat detector => Qr = qval*cos(theta)
481        dx = (xaxval - xctr)*pixSizeX           //delta x in cm
482        dy = (yaxval - yctr)*pixSizeY           //delta y in cm
483        dist = sqrt(dx^2 + dy^2)
484        two_theta = atan(dist/sdd)
485       
486        qz = qval*sin(two_theta/2)
487       
488        return qz
489End
490
491//phi is defined from +x axis, proceeding CCW around [0,2Pi]
492Threadsafe Function V_FindPhi(vx,vy)
493        variable vx,vy
494       
495        variable phi
496       
497        phi = atan(vy/vx)               //returns a value from -pi/2 to pi/2
498       
499        // special cases
500        if(vx==0 && vy > 0)
501                return(pi/2)
502        endif
503        if(vx==0 && vy < 0)
504                return(3*pi/2)
505        endif
506        if(vx >= 0 && vy == 0)
507                return(0)
508        endif
509        if(vx < 0 && vy == 0)
510                return(pi)
511        endif
512       
513       
514        if(vx > 0 && vy > 0)
515                return(phi)
516        endif
517        if(vx < 0 && vy > 0)
518                return(phi + pi)
519        endif
520        if(vx < 0 && vy < 0)
521                return(phi + pi)
522        endif
523        if( vx > 0 && vy < 0)
524                return(phi + 2*pi)
525        endif
526       
527        return(phi)
528end
529
530Function VC_SphereForm(scale,radius,delrho,bkg,x)                               
531        Variable scale,radius,delrho,bkg
532        Variable x
533       
534        // variables are:                                                       
535        //[0] scale
536        //[1] radius (A)
537        //[2] delrho (A-2)
538        //[3] background (cm-1)
539       
540//      Variable scale,radius,delrho,bkg                               
541//      scale = w[0]
542//      radius = w[1]
543//      delrho = w[2]
544//      bkg = w[3]
545       
546       
547        // calculates scale * f^2/Vol where f=Vol*3*delrho*((sin(qr)-qrcos(qr))/qr^3
548        // and is rescaled to give [=] cm^-1
549       
550        Variable bes,f,vol,f2
551        //
552        //handle q==0 separately
553        If(x==0)
554                f = 4/3*pi*radius^3*delrho*delrho*scale*1e8 + bkg
555                return(f)
556        Endif
557       
558//      bes = 3*(sin(x*radius)-x*radius*cos(x*radius))/x^3/radius^3
559       
560        bes = 3*sqrt(pi/(2*x*radius))*BesselJ(1.5,x*radius)/(x*radius)
561       
562        vol = 4*pi/3*radius^3
563        f = vol*bes*delrho              // [=] A
564        // normalize to single particle volume, convert to 1/cm
565        f2 = f * f / vol * 1.0e8                // [=] 1/cm
566       
567        return (scale*f2+bkg)   // Scale, then add in the background
568       
569End
570
571ThreadSafe Function VC_Debye(scale,rg,bkg,x)
572        Variable scale,rg,bkg
573        Variable x
574       
575        // variables are:
576        //[0] scale factor
577        //[1] radius of gyration [A]
578        //[2] background        [cm-1]
579       
580        // calculates (scale*debye)+bkg
581        Variable Pq,qr2
582       
583        qr2=(x*rg)^2
584        Pq = 2*(exp(-(qr2))-1+qr2)/qr2^2
585       
586        //scale
587        Pq *= scale
588        // then add in the background
589        return (Pq+bkg)
590End
591
592// a sum of a power law and debye to approximate the scattering from a real empty cell
593//
594//      make/O/D coef_ECEmp = {2.2e-8,3.346,0.0065,9.0,0.016}
595//
596Function VC_EC_Empirical(aa,mm,scale,rg,bkg,x)
597        Variable aa,mm,scale,rg,bkg
598        Variable x
599       
600        // variables are:
601        //[0] = A
602        //[1] = power m
603        //[2] scale factor
604        //[3] radius of gyration [A]
605        //[4] background        [cm-1]
606       
607        Variable Iq
608       
609        // calculates (scale*debye)+bkg
610        Variable Pq,qr2
611       
612//      if(x*Rg < 1e-3)         //added Oct 2008 to avoid numerical errors at low arg values
613//              return(scale+bkg)
614//      endif
615       
616        Iq = aa*x^-mm
617       
618        qr2=(x*rg)^2
619        Pq = 2*(exp(-(qr2))-1+qr2)/qr2^2
620       
621        //scale
622        Pq *= scale
623        // then add the terms up
624        return (Iq + Pq + bkg)
625End
626
627// blocked beam
628//
629Function VC_BlockedBeam(bkg,x)
630        Variable bkg
631        Variable x
632       
633        return (bkg)
634End
635
636
637//
638// a broad peak to simulate silver behenate or vycor
639//
640// peak @ 0.1 ~ AgBeh
641//      Make/O/D coef_BroadPeak = {1e-9, 3, 20, 100.0, 0.1,3,0.1}               
642//
643//
644// peak @ 0.015 in middle of middle detector, maybe not "real" vycor, but that is to be resolved
645//      Make/O/D coef_BroadPeak = {1e-9, 3, 20, 500.0, 0.015,3,0.1}             
646//
647//
648Function VC_BroadPeak(aa,nn,cc,LL,Qzero,mm,bgd,x)
649        Variable aa,nn,cc,LL,Qzero,mm,bgd
650        Variable x
651       
652        // variables are:                                                       
653        //[0] Porod term scaling
654        //[1] Porod exponent
655        //[2] Lorentzian term scaling
656        //[3] Lorentzian screening length [A]
657        //[4] peak location [1/A]
658        //[5] Lorentzian exponent
659        //[6] background
660       
661//      local variables
662        Variable inten, qval
663//      x is the q-value for the calculation
664        qval = x
665//      do the calculation and return the function value
666       
667        inten = aa/(qval)^nn + cc/(1 + (abs(qval-Qzero)*LL)^mm) + bgd
668
669        Return (inten)
670       
671End
672
673//
674// updated to new folder structure Feb 2016
675// folderStr = RAW,SAM, VCALC or other
676// detStr is the panel identifer "ML", etc.
677//
678Function V_SetDeltaQ(folderStr,detStr)
679        String folderStr,detStr
680
681        Variable isVCALC
682        if(cmpstr(folderStr,"VCALC") == 0)
683                isVCALC = 1
684        endif
685       
686        String folderPath = "root:Packages:NIST:VSANS:"+folderStr
687        String instPath = ":entry:instrument:detector_"
688               
689        if(isVCALC)
690                WAVE inten = $(folderPath+instPath+detStr+":det_"+detStr)               // 2D detector data
691        else
692                Wave inten = V_getDetectorDataW(folderStr,detStr)
693        endif
694
695        Wave qx = $(folderPath+instPath+detStr+":qx_"+detStr)
696        Wave qy = $(folderPath+instPath+detStr+":qy_"+detStr)
697       
698        Variable xDim,yDim,delQ
699       
700        xDim=DimSize(inten,0)
701        yDim=DimSize(inten,1)
702       
703        if(xDim<yDim)
704                delQ = abs(qx[0][0] - qx[1][0])/2
705        else
706                delQ = abs(qy[0][1] - qy[0][0])/2
707        endif
708
709// TODOHIGHRES
710// -- is this how I want to handle the too-fine resolution of 1x1 binning?
711        NVAR gHighResBinning = root:Packages:NIST:VSANS:Globals:gHighResBinning
712
713        if(cmpstr(detStr,"B") == 0 && gHighResBinning == 1)
714                delQ = 4*delQ
715                Print "Reset delta Q for binning the back detector to 4x pix = ",delQ
716        endif
717       
718        // set the global
719        Variable/G $(folderPath+instPath+detStr+":gDelQ_"+detStr) = delQ
720//      Print "SET delQ = ",delQ," for ",type
721       
722        return(delQ)
723end
724
725
726//TODO -- need a switch here to dispatch to the averaging type
727Proc VC_BinQxQy_to_1D(folderStr,type)
728        String folderStr
729        String type
730//      Prompt folderStr,"Pick the data folder containing 2D data",popup,getAList(4)
731//      Prompt type,"detector identifier"
732
733
734        VC_fDoBinning_QxQy2D(folderStr, type)
735
736
737/// this is for a tall, narrow slit mode       
738//      VC_fBinDetector_byRows(folderStr,type)
739       
740End
741
742
743// folderStr is RAW, VCALC, SAM, etc.
744// type is "B", "FL" for single binning, "FLR", or "MLRTB" or similar if multiple panels are combined
745//
746Proc VC_Graph_1D_detType(folderStr,type)
747        String folderStr,type
748       
749        SetDataFolder $("root:Packages:NIST:VSANS:"+folderStr)
750       
751        Display $("iBin_qxqy"+"_"+type) vs $("qBin_qxqy"+"_"+type)
752        ModifyGraph mirror=2,grid=1,log=1
753        ModifyGraph mode=4,marker=19,msize=2
754//      ErrorBars/T=0 iBin_qxqy Y,wave=(eBin2D_qxqy,eBin2D_qxqy)                // for simulations, I don't have 2D uncertainty
755        ErrorBars/T=0 $("iBin_qxqy"+"_"+type) Y,wave=($("eBin_qxqy"+"_"+type),$("eBin_qxqy"+"_"+type))
756        legend
757       
758        SetDataFolder root:
759
760End
761
762
763
764//////////
765//
766//              Function that bins a 2D detctor panel into I(q) based on the q-value of the pixel
767//              - each pixel QxQyQz has been calculated beforehand
768//              - if multiple panels are selected to be combined, it is done here during the binning
769//              - the setting of deltaQ step is still a little suspect (TODO)
770//
771//
772// see the equivalent function in PlotUtils2D_v40.ipf
773//
774//Function fDoBinning_QxQy2D(inten,qx,qy,qz)
775//
776// this has been modified to accept different detector panels and to take arrays
777// -- type = FL or FR or...other panel identifiers
778//
779// TODO "iErr" is not always defined correctly since it doesn't really apply here for data that is not 2D simulation
780//
781//
782// updated Feb2016 to take new folder structure
783// TODO
784// -- VERIFY
785// -- figure out what the best location is to put the averaged data? currently @ top level of WORK folder
786//    but this is a lousy choice.
787// x- binning is now Mask-aware. If mask is not present, all data is used. If data is from VCALC, all data is used
788// x- Where do I put the solid angle correction? In here as a weight for each point, or later on as
789//    a blanket correction (matrix multiply) for an entire panel? (Solid Angle correction is done in the
790//    step where data is added to a WORK file (see Raw_to_Work())
791//
792//
793// TODO:
794// -- some of the input parameters for the resolution calcuation are either assumed (apOff) or are currently
795//    hard-wired. these need to be corrected before even the pinhole resolution is correct
796// x- resolution calculation is in the correct place. The calculation is done per-panel (specified by TYPE),
797//    and then the unwanted points can be discarded (all 6 columns) as the data is trimmed and concatenated
798//    is separate functions that are resolution-aware.
799//
800//
801// folderStr = WORK folder, type = the binning type (may include multiple detectors)
802Function VC_fDoBinning_QxQy2D(folderStr,type,collimationStr)
803        String folderStr,type,collimationStr
804       
805        Variable nSets = 0
806        Variable xDim,yDim
807        Variable ii,jj
808        Variable qVal,nq,var,avesq,aveisq
809        Variable binIndex,val,isVCALC=0,maskMissing
810
811        String folderPath = "root:Packages:NIST:VSANS:"+folderStr
812        String instPath = ":entry:instrument:detector_"
813        String detStr
814               
815        if(cmpstr(folderStr,"VCALC") == 0)
816                isVCALC = 1
817        endif
818       
819        detStr = type
820
821// now switch on the type to determine which waves to declare and create
822// since there may be more than one panel to step through. There may be two, there may be four
823//
824
825// TODO:
826// -- Solid_Angle -- waves will be present for WORK data other than RAW, but not for RAW
827//
828// assume that the mask files are missing unless we can find them. If VCALC data,
829//  then the Mask is missing by definition
830        maskMissing = 1
831
832        strswitch(type) // string switch
833//              case "FL":              // execute if case matches expression
834//              case "FR":
835//                      detStr = type
836//                      if(isVCALC)
837//                              WAVE inten = $(folderPath+instPath+detStr+":det_"+detStr)
838//                              WAVE/Z iErr = $("iErr_"+detStr)                 // 2D errors -- may not exist, especially for simulation
839//                      else
840//                              Wave inten = V_getDetectorDataW(folderStr,detStr)
841//                              Wave iErr = V_getDetectorDataErrW(folderStr,detStr)
842//                              Wave/Z mask = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+detStr+":data")
843//                              if(WaveExists(mask) == 1)
844//                                      maskMissing = 0
845//                              endif
846//                             
847//                      endif
848//                      NVAR delQ = $(folderPath+instPath+detStr+":gDelQ_"+detStr)
849//                      Wave qTotal = $(folderPath+instPath+detStr+":qTot_"+detStr)                     // 2D q-values
850//                      nSets = 1
851//                      break   
852                                                               
853//              case "FT":             
854//              case "FB":
855//                      detStr = type
856//                      if(isVCALC)
857//                              WAVE inten = $(folderPath+instPath+detStr+":det_"+detStr)
858//                              WAVE/Z iErr = $("iErr_"+detStr)                 // 2D errors -- may not exist, especially for simulation               
859//                      else
860//                              Wave inten = V_getDetectorDataW(folderStr,detStr)
861//                              Wave iErr = V_getDetectorDataErrW(folderStr,detStr)
862//                              Wave/Z mask = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+detStr+":data")
863//                              if(WaveExists(mask) == 1)
864//                                      maskMissing = 0
865//                              endif
866//                      endif
867//                      NVAR delQ = $(folderPath+instPath+detStr+":gDelQ_"+detStr)
868//                      Wave qTotal = $(folderPath+instPath+detStr+":qTot_"+detStr)                     // 2D q-values
869//                      nSets = 1
870//                      break
871                       
872//              case "ML":             
873//              case "MR":
874//                      detStr = type
875//                      if(isVCALC)
876//                              WAVE inten = $(folderPath+instPath+detStr+":det_"+detStr)
877//                              WAVE/Z iErr = $("iErr_"+detStr)                 // 2D errors -- may not exist, especially for simulation               
878//                      else
879//                              Wave inten = V_getDetectorDataW(folderStr,detStr)
880//                              Wave iErr = V_getDetectorDataErrW(folderStr,detStr)
881//                              Wave/Z mask = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+detStr+":data")
882//                              if(WaveExists(mask) == 1)
883//                                      maskMissing = 0
884//                              endif
885//                      endif   
886//                      //TODO:
887//                      // -- decide on the proper deltaQ for binning. either nominal value for LR, or one
888//                      //    determined specifically for that panel (currently using one tube width as deltaQ)
889//                      // -- this is repeated multiple times in this switch
890//                      NVAR delQ = $(folderPath+instPath+detStr+":gDelQ_"+detStr)
891//                      Wave qTotal = $(folderPath+instPath+detStr+":qTot_"+detStr)                     // 2D q-values
892//                      nSets = 1
893//                      break   
894                                       
895//              case "MT":             
896//              case "MB":
897//                      detStr = type
898//                      if(isVCALC)
899//                              WAVE inten = $(folderPath+instPath+detStr+":det_"+detStr)
900//                              WAVE/Z iErr = $("iErr_"+detStr)                 // 2D errors -- may not exist, especially for simulation               
901//                      else
902//                              Wave inten = V_getDetectorDataW(folderStr,detStr)
903//                              Wave iErr = V_getDetectorDataErrW(folderStr,detStr)
904//                              Wave/Z mask = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+detStr+":data")
905//                              if(WaveExists(mask) == 1)
906//                                      maskMissing = 0
907//                              endif
908//                      endif   
909//                      NVAR delQ = $(folderPath+instPath+detStr+":gDelQ_"+detStr)
910//                      Wave qTotal = $(folderPath+instPath+detStr+":qTot_"+detStr)                     // 2D q-values
911//                      nSets = 1
912//                      break   
913
914// only one panel, simply pick that panel and move on out of the switch
915                case "FL":
916                case "FR":
917                case "FT":
918                case "FB":
919                case "ML":
920                case "MR":
921                case "MT":
922                case "MB":                     
923                case "B":       
924                        if(isVCALC)
925                                WAVE inten = $(folderPath+instPath+detStr+":det_"+detStr)
926                                WAVE/Z iErr = $("iErr_"+detStr)                 // 2D errors -- may not exist, especially for simulation               
927                        else
928                                Wave inten = V_getDetectorDataW(folderStr,detStr)
929                                Wave iErr = V_getDetectorDataErrW(folderStr,detStr)
930                                Wave/Z mask = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+detStr+":data")
931                                if(WaveExists(mask) == 1)
932                                        maskMissing = 0
933                                endif
934                        endif   
935                        NVAR delQ = $(folderPath+instPath+detStr+":gDelQ_"+detStr)
936                        Wave qTotal = $(folderPath+instPath+detStr+":qTot_"+detStr)                     // 2D q-values 
937                        nSets = 1
938                        break   
939                       
940                case "FLR":
941                // detStr has multiple values now, so unfortuntely, I'm hard-wiring things...
942                // TODO
943                // -- see if I can un-hard-wire some of this below when more than one panel is combined
944                        if(isVCALC)
945                                WAVE inten = $(folderPath+instPath+"FL"+":det_"+"FL")
946                                WAVE/Z iErr = $("iErr_"+"FL")                   // 2D errors -- may not exist, especially for simulation               
947                                WAVE inten2 = $(folderPath+instPath+"FR"+":det_"+"FR")
948                                WAVE/Z iErr2 = $("iErr_"+"FR")                  // 2D errors -- may not exist, especially for simulation       
949                        else
950                                Wave inten = V_getDetectorDataW(folderStr,"FL")
951                                Wave iErr = V_getDetectorDataErrW(folderStr,"FL")
952                                Wave inten2 = V_getDetectorDataW(folderStr,"FR")
953                                Wave iErr2 = V_getDetectorDataErrW(folderStr,"FR")
954                                Wave/Z mask = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+"FL"+":data")
955                                Wave/Z mask2 = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+"FR"+":data")
956                                if(WaveExists(mask) == 1 && WaveExists(mask2) == 1)
957                                        maskMissing = 0
958                                endif
959                        endif   
960                        NVAR delQ = $(folderPath+instPath+"FL"+":gDelQ_FL")
961                       
962                        Wave qTotal = $(folderPath+instPath+"FL"+":qTot_"+"FL")                 // 2D q-values 
963                        Wave qTotal2 = $(folderPath+instPath+"FR"+":qTot_"+"FR")                        // 2D q-values 
964               
965                        nSets = 2
966                        break                   
967               
968                case "FTB":
969                        if(isVCALC)
970                                WAVE inten = $(folderPath+instPath+"FT"+":det_"+"FT")
971                                WAVE/Z iErr = $("iErr_"+"FT")                   // 2D errors -- may not exist, especially for simulation               
972                                WAVE inten2 = $(folderPath+instPath+"FB"+":det_"+"FB")
973                                WAVE/Z iErr2 = $("iErr_"+"FB")                  // 2D errors -- may not exist, especially for simulation       
974                        else
975                                Wave inten = V_getDetectorDataW(folderStr,"FT")
976                                Wave iErr = V_getDetectorDataErrW(folderStr,"FT")
977                                Wave inten2 = V_getDetectorDataW(folderStr,"FB")
978                                Wave iErr2 = V_getDetectorDataErrW(folderStr,"FB")
979                                Wave/Z mask = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+"FT"+":data")
980                                Wave/Z mask2 = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+"FB"+":data")
981                                if(WaveExists(mask) == 1 && WaveExists(mask2) == 1)
982                                        maskMissing = 0
983                                endif
984                        endif   
985                        NVAR delQ = $(folderPath+instPath+"FT"+":gDelQ_FT")
986                       
987                        Wave qTotal = $(folderPath+instPath+"FT"+":qTot_"+"FT")                 // 2D q-values 
988                        Wave qTotal2 = $(folderPath+instPath+"FB"+":qTot_"+"FB")                        // 2D q-values 
989       
990                        nSets = 2
991                        break           
992               
993                case "FLRTB":
994                        if(isVCALC)
995                                WAVE inten = $(folderPath+instPath+"FL"+":det_"+"FL")
996                                WAVE/Z iErr = $("iErr_"+"FL")                   // 2D errors -- may not exist, especially for simulation               
997                                WAVE inten2 = $(folderPath+instPath+"FR"+":det_"+"FR")
998                                WAVE/Z iErr2 = $("iErr_"+"FR")                  // 2D errors -- may not exist, especially for simulation       
999                                WAVE inten3 = $(folderPath+instPath+"FT"+":det_"+"FT")
1000                                WAVE/Z iErr3 = $("iErr_"+"FT")                  // 2D errors -- may not exist, especially for simulation               
1001                                WAVE inten4 = $(folderPath+instPath+"FB"+":det_"+"FB")
1002                                WAVE/Z iErr4 = $("iErr_"+"FB")                  // 2D errors -- may not exist, especially for simulation       
1003                        else
1004                                Wave inten = V_getDetectorDataW(folderStr,"FL")
1005                                Wave iErr = V_getDetectorDataErrW(folderStr,"FL")
1006                                Wave inten2 = V_getDetectorDataW(folderStr,"FR")
1007                                Wave iErr2 = V_getDetectorDataErrW(folderStr,"FR")
1008                                Wave inten3 = V_getDetectorDataW(folderStr,"FT")
1009                                Wave iErr3 = V_getDetectorDataErrW(folderStr,"FT")
1010                                Wave inten4 = V_getDetectorDataW(folderStr,"FB")
1011                                Wave iErr4 = V_getDetectorDataErrW(folderStr,"FB")
1012                                Wave/Z mask = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+"FL"+":data")
1013                                Wave/Z mask2 = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+"FR"+":data")
1014                                Wave/Z mask3 = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+"FT"+":data")
1015                                Wave/Z mask4 = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+"FB"+":data")
1016                                if(WaveExists(mask) == 1 && WaveExists(mask2) == 1 && WaveExists(mask3) == 1 && WaveExists(mask4) == 1)
1017                                        maskMissing = 0
1018                                endif
1019                        endif   
1020                        NVAR delQ = $(folderPath+instPath+"FL"+":gDelQ_FL")
1021                       
1022                        Wave qTotal = $(folderPath+instPath+"FL"+":qTot_"+"FL")                 // 2D q-values 
1023                        Wave qTotal2 = $(folderPath+instPath+"FR"+":qTot_"+"FR")                        // 2D q-values 
1024                        Wave qTotal3 = $(folderPath+instPath+"FT"+":qTot_"+"FT")                        // 2D q-values 
1025                        Wave qTotal4 = $(folderPath+instPath+"FB"+":qTot_"+"FB")                        // 2D q-values 
1026               
1027                        nSets = 4
1028                        break           
1029                       
1030                case "MLR":
1031                        if(isVCALC)
1032                                WAVE inten = $(folderPath+instPath+"ML"+":det_"+"ML")
1033                                WAVE/Z iErr = $("iErr_"+"ML")                   // 2D errors -- may not exist, especially for simulation               
1034                                WAVE inten2 = $(folderPath+instPath+"MR"+":det_"+"MR")
1035                                WAVE/Z iErr2 = $("iErr_"+"MR")                  // 2D errors -- may not exist, especially for simulation       
1036                        else
1037                                Wave inten = V_getDetectorDataW(folderStr,"ML")
1038                                Wave iErr = V_getDetectorDataErrW(folderStr,"ML")
1039                                Wave inten2 = V_getDetectorDataW(folderStr,"MR")
1040                                Wave iErr2 = V_getDetectorDataErrW(folderStr,"MR")
1041                                Wave/Z mask = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+"ML"+":data")
1042                                Wave/Z mask2 = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+"MR"+":data")
1043                                if(WaveExists(mask) == 1 && WaveExists(mask2) == 1)
1044                                        maskMissing = 0
1045                                endif
1046                        endif   
1047                        NVAR delQ = $(folderPath+instPath+"ML"+":gDelQ_ML")
1048                       
1049                        Wave qTotal = $(folderPath+instPath+"ML"+":qTot_"+"ML")                 // 2D q-values 
1050                        Wave qTotal2 = $(folderPath+instPath+"MR"+":qTot_"+"MR")                        // 2D q-values 
1051               
1052                        nSets = 2
1053                        break                   
1054               
1055                case "MTB":
1056                        if(isVCALC)
1057                                WAVE inten = $(folderPath+instPath+"MT"+":det_"+"MT")
1058                                WAVE/Z iErr = $("iErr_"+"MT")                   // 2D errors -- may not exist, especially for simulation               
1059                                WAVE inten2 = $(folderPath+instPath+"MB"+":det_"+"MB")
1060                                WAVE/Z iErr2 = $("iErr_"+"MB")                  // 2D errors -- may not exist, especially for simulation       
1061                        else
1062                                Wave inten = V_getDetectorDataW(folderStr,"MT")
1063                                Wave iErr = V_getDetectorDataErrW(folderStr,"MT")
1064                                Wave inten2 = V_getDetectorDataW(folderStr,"MB")
1065                                Wave iErr2 = V_getDetectorDataErrW(folderStr,"MB")
1066                                Wave/Z mask = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+"MT"+":data")
1067                                Wave/Z mask2 = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+"MB"+":data")
1068                                if(WaveExists(mask) == 1 && WaveExists(mask2) == 1)
1069                                        maskMissing = 0
1070                                endif
1071                        endif   
1072                        NVAR delQ = $(folderPath+instPath+"MT"+":gDelQ_MT")
1073                       
1074                        Wave qTotal = $(folderPath+instPath+"MT"+":qTot_"+"MT")                 // 2D q-values 
1075                        Wave qTotal2 = $(folderPath+instPath+"MB"+":qTot_"+"MB")                        // 2D q-values 
1076               
1077                        nSets = 2
1078                        break                           
1079               
1080                case "MLRTB":
1081                        if(isVCALC)
1082                                WAVE inten = $(folderPath+instPath+"ML"+":det_"+"ML")
1083                                WAVE/Z iErr = $("iErr_"+"ML")                   // 2D errors -- may not exist, especially for simulation               
1084                                WAVE inten2 = $(folderPath+instPath+"MR"+":det_"+"MR")
1085                                WAVE/Z iErr2 = $("iErr_"+"MR")                  // 2D errors -- may not exist, especially for simulation       
1086                                WAVE inten3 = $(folderPath+instPath+"MT"+":det_"+"MT")
1087                                WAVE/Z iErr3 = $("iErr_"+"MT")                  // 2D errors -- may not exist, especially for simulation               
1088                                WAVE inten4 = $(folderPath+instPath+"MB"+":det_"+"MB")
1089                                WAVE/Z iErr4 = $("iErr_"+"MB")                  // 2D errors -- may not exist, especially for simulation       
1090                        else
1091                                Wave inten = V_getDetectorDataW(folderStr,"ML")
1092                                Wave iErr = V_getDetectorDataErrW(folderStr,"ML")
1093                                Wave inten2 = V_getDetectorDataW(folderStr,"MR")
1094                                Wave iErr2 = V_getDetectorDataErrW(folderStr,"MR")
1095                                Wave inten3 = V_getDetectorDataW(folderStr,"MT")
1096                                Wave iErr3 = V_getDetectorDataErrW(folderStr,"MT")
1097                                Wave inten4 = V_getDetectorDataW(folderStr,"MB")
1098                                Wave iErr4 = V_getDetectorDataErrW(folderStr,"MB")
1099                                Wave/Z mask = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+"ML"+":data")
1100                                Wave/Z mask2 = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+"MR"+":data")
1101                                Wave/Z mask3 = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+"MT"+":data")
1102                                Wave/Z mask4 = $("root:Packages:NIST:VSANS:MSK:entry:instrument:detector_"+"MB"+":data")
1103                                if(WaveExists(mask) == 1 && WaveExists(mask2) == 1 && WaveExists(mask3) == 1 && WaveExists(mask4) == 1)
1104                                        maskMissing = 0
1105                                endif
1106                        endif   
1107                        NVAR delQ = $(folderPath+instPath+"ML"+":gDelQ_ML")
1108                       
1109                        Wave qTotal = $(folderPath+instPath+"ML"+":qTot_"+"ML")                 // 2D q-values 
1110                        Wave qTotal2 = $(folderPath+instPath+"MR"+":qTot_"+"MR")                        // 2D q-values 
1111                        Wave qTotal3 = $(folderPath+instPath+"MT"+":qTot_"+"MT")                        // 2D q-values 
1112                        Wave qTotal4 = $(folderPath+instPath+"MB"+":qTot_"+"MB")                        // 2D q-values 
1113               
1114                        nSets = 4
1115                        break                                                                   
1116                                       
1117                default:
1118                        nSets = 0                                                       
1119                        Print "ERROR   ---- type is not recognized "
1120        endswitch
1121
1122//      Print "delQ = ",delQ," for ",type
1123
1124        if(nSets == 0)
1125                SetDataFolder root:
1126                return(0)
1127        endif
1128
1129
1130// RAW data is currently read in and the 2D error wave is correctly generated
1131// 2D error is propagated through all reduction steps, but I have not
1132// verified that it is an exact duplication of the 1D error
1133//
1134//
1135//
1136// IF ther is no 2D error wave present for some reason, make a fake one
1137        if(WaveExists(iErr)==0  && WaveExists(inten) != 0)
1138                Duplicate/O inten,iErr
1139                Wave iErr=iErr
1140//              iErr = 1+sqrt(inten+0.75)                       // can't use this -- it applies to counts, not intensity (already a count rate...)
1141                iErr = sqrt(inten+0.75)                 // TODO -- here I'm just using some fictional value
1142        endif
1143        if(WaveExists(iErr2)==0 && WaveExists(inten2) != 0)
1144                Duplicate/O inten2,iErr2
1145                Wave iErr2=iErr2
1146//              iErr2 = 1+sqrt(inten2+0.75)                     // can't use this -- it applies to counts, not intensity (already a count rate...)
1147                iErr2 = sqrt(inten2+0.75)                       // TODO -- here I'm just using some fictional value
1148        endif
1149        if(WaveExists(iErr3)==0  && WaveExists(inten3) != 0)
1150                Duplicate/O inten3,iErr3
1151                Wave iErr3=iErr3
1152//              iErr3 = 1+sqrt(inten3+0.75)                     // can't use this -- it applies to counts, not intensity (already a count rate...)
1153                iErr3 = sqrt(inten3+0.75)                       // TODO -- here I'm just using some fictional value
1154        endif
1155        if(WaveExists(iErr4)==0  && WaveExists(inten4) != 0)
1156                Duplicate/O inten4,iErr4
1157                Wave iErr4=iErr4
1158//              iErr4 = 1+sqrt(inten4+0.75)                     // can't use this -- it applies to counts, not intensity (already a count rate...)
1159                iErr4 = sqrt(inten4+0.75)                       // TODO -- here I'm just using some fictional value
1160        endif
1161
1162        // TODO -- nq will need to be larger, once the back detector is installed
1163        //
1164        // note that the back panel of 320x320 (1mm res) results in 447 data points!
1165        // - so I upped nq to 600
1166
1167        if(cmpstr(type,"B") == 0)
1168                nq = 8000
1169        else
1170                nq=600
1171        endif
1172
1173//******TODO****** -- where to put the averaged data -- right now, folderStr is forced to ""   
1174//      SetDataFolder $("root:"+folderStr)              //should already be here, but make sure...     
1175        Make/O/D/N=(nq)  $(folderPath+":"+"iBin_qxqy"+"_"+type)
1176        Make/O/D/N=(nq)  $(folderPath+":"+"qBin_qxqy"+"_"+type)
1177        Make/O/D/N=(nq)  $(folderPath+":"+"nBin_qxqy"+"_"+type)
1178        Make/O/D/N=(nq)  $(folderPath+":"+"iBin2_qxqy"+"_"+type)
1179        Make/O/D/N=(nq)  $(folderPath+":"+"eBin_qxqy"+"_"+type)
1180        Make/O/D/N=(nq)  $(folderPath+":"+"eBin2D_qxqy"+"_"+type)
1181       
1182        Wave iBin_qxqy = $(folderPath+":"+"iBin_qxqy_"+type)
1183        Wave qBin_qxqy = $(folderPath+":"+"qBin_qxqy"+"_"+type)
1184        Wave nBin_qxqy = $(folderPath+":"+"nBin_qxqy"+"_"+type)
1185        Wave iBin2_qxqy = $(folderPath+":"+"iBin2_qxqy"+"_"+type)
1186        Wave eBin_qxqy = $(folderPath+":"+"eBin_qxqy"+"_"+type)
1187        Wave eBin2D_qxqy = $(folderPath+":"+"eBin2D_qxqy"+"_"+type)
1188       
1189       
1190//      delQ = abs(sqrt(qx[2]^2+qy[2]^2+qz[2]^2) - sqrt(qx[1]^2+qy[1]^2+qz[1]^2))               //use bins of 1 pixel width
1191// TODO: not sure if I want to set dQ in x or y direction...
1192        // the short dimension is the 8mm tubes, use this direction as dQ?
1193        // but don't use the corner of the detector, since dQ will be very different on T/B or L/R due to the location of [0,0]
1194        // WRT the beam center. use qx or qy directly. Still not happy with this way...
1195
1196
1197        qBin_qxqy[] =  p*delQ   
1198        SetScale/P x,0,delQ,"",qBin_qxqy                //allows easy binning
1199
1200        iBin_qxqy = 0
1201        iBin2_qxqy = 0
1202        eBin_qxqy = 0
1203        eBin2D_qxqy = 0
1204        nBin_qxqy = 0   //number of intensities added to each bin
1205
1206// now there are situations of:
1207// 1 panel
1208// 2 panels
1209// 4 panels
1210//
1211// this needs to be a double loop now...
1212// TODO:
1213// -- the iErr (=2D) wave and accumulation of error is NOT CALCULATED CORRECTLY YET
1214// -- verify the 2D error propagation by reducing it to 1D error
1215//
1216//
1217// The 1D error does not use iErr, and IS CALCULATED CORRECTLY
1218//
1219// x- the solid angle per pixel will be present for WORK data other than RAW, but not for RAW
1220
1221//
1222// if any of the masks don't exist, display the error, and proceed with the averaging, using all data
1223        if(maskMissing == 1)
1224                Print "Mask file not found for at least one detector - so all data is used"
1225        endif
1226       
1227        NVAR gIgnoreDetB = root:Packages:NIST:VSANS:Globals:gIgnoreDetB
1228        if(gIgnoreDetB && cmpstr(type,"B") == 0)
1229                maskMissing = 1
1230                Print "Mask skipped for B due to possible mismatch (Panel B ignored in preferences)"
1231        endif
1232
1233        Variable mask_val
1234// use set 1 (no number) only
1235        if(nSets >= 1)
1236                xDim=DimSize(inten,0)
1237                yDim=DimSize(inten,1)
1238       
1239                for(ii=0;ii<xDim;ii+=1)
1240                        for(jj=0;jj<yDim;jj+=1)
1241                                //qTot = sqrt(qx[ii]^2 + qy[ii]^2+ qz[ii]^2)
1242                                qVal = qTotal[ii][jj]
1243                                binIndex = trunc(x2pnt(qBin_qxqy, qVal))
1244                                val = inten[ii][jj]
1245                               
1246                                if(isVCALC || maskMissing)              // mask_val == 0 == keep, mask_val == 1 = YES, mask out the point
1247                                        mask_val = 0
1248                                else
1249                                        mask_val = mask[ii][jj]
1250                                endif
1251                                if (numType(val)==0 && mask_val == 0)           //count only the good points, ignore Nan or Inf
1252                                        iBin_qxqy[binIndex] += val
1253                                        iBin2_qxqy[binIndex] += val*val
1254                                        eBin2D_qxqy[binIndex] += iErr[ii][jj]*iErr[ii][jj]
1255                                        nBin_qxqy[binIndex] += 1
1256                                endif
1257                        endfor
1258                endfor
1259               
1260        endif
1261
1262// add in set 2 (set 1 already done)
1263        if(nSets >= 2)
1264                xDim=DimSize(inten2,0)
1265                yDim=DimSize(inten2,1)
1266       
1267                for(ii=0;ii<xDim;ii+=1)
1268                        for(jj=0;jj<yDim;jj+=1)
1269                                //qTot = sqrt(qx[ii]^2 + qy[ii]^2+ qz[ii]^2)
1270                                qVal = qTotal2[ii][jj]
1271                                binIndex = trunc(x2pnt(qBin_qxqy, qVal))
1272                                val = inten2[ii][jj]
1273                               
1274                                if(isVCALC || maskMissing)
1275                                        mask_val = 0
1276                                else
1277                                        mask_val = mask2[ii][jj]
1278                                endif
1279                                if (numType(val)==0 && mask_val == 0)           //count only the good points, ignore Nan or Inf
1280                                        iBin_qxqy[binIndex] += val
1281                                        iBin2_qxqy[binIndex] += val*val
1282                                        eBin2D_qxqy[binIndex] += iErr2[ii][jj]*iErr2[ii][jj]
1283                                        nBin_qxqy[binIndex] += 1
1284                                endif
1285                        endfor
1286                endfor
1287               
1288        endif
1289
1290// add in set 3 and 4 (set 1 and 2 already done)
1291        if(nSets == 4)
1292                xDim=DimSize(inten3,0)
1293                yDim=DimSize(inten3,1)
1294       
1295                for(ii=0;ii<xDim;ii+=1)
1296                        for(jj=0;jj<yDim;jj+=1)
1297                                //qTot = sqrt(qx[ii]^2 + qy[ii]^2+ qz[ii]^2)
1298                                qVal = qTotal3[ii][jj]
1299                                binIndex = trunc(x2pnt(qBin_qxqy, qVal))
1300                                val = inten3[ii][jj]
1301                               
1302                                if(isVCALC || maskMissing)
1303                                        mask_val = 0
1304                                else
1305                                        mask_val = mask3[ii][jj]
1306                                endif
1307                                if (numType(val)==0 && mask_val == 0)           //count only the good points, ignore Nan or Inf
1308                                        iBin_qxqy[binIndex] += val
1309                                        iBin2_qxqy[binIndex] += val*val
1310                                        eBin2D_qxqy[binIndex] += iErr3[ii][jj]*iErr3[ii][jj]
1311                                        nBin_qxqy[binIndex] += 1
1312                                endif
1313                        endfor
1314                endfor
1315               
1316               
1317                xDim=DimSize(inten4,0)
1318                yDim=DimSize(inten4,1)
1319       
1320                for(ii=0;ii<xDim;ii+=1)
1321                        for(jj=0;jj<yDim;jj+=1)
1322                                //qTot = sqrt(qx[ii]^2 + qy[ii]^2+ qz[ii]^2)
1323                                qVal = qTotal4[ii][jj]
1324                                binIndex = trunc(x2pnt(qBin_qxqy, qVal))
1325                                val = inten4[ii][jj]
1326                               
1327                                if(isVCALC || maskMissing)
1328                                        mask_val = 0
1329                                else
1330                                        mask_val = mask4[ii][jj]
1331                                endif
1332                                if (numType(val)==0 && mask_val == 0)           //count only the good points, ignore Nan or Inf
1333                                        iBin_qxqy[binIndex] += val
1334                                        iBin2_qxqy[binIndex] += val*val
1335                                        eBin2D_qxqy[binIndex] += iErr4[ii][jj]*iErr4[ii][jj]
1336                                        nBin_qxqy[binIndex] += 1
1337                                endif
1338                        endfor
1339                endfor
1340               
1341        endif
1342
1343
1344// after looping through all of the data on the panels, calculate errors on I(q),
1345// just like in CircSectAve.ipf
1346// TODO:
1347// -- 2D Errors were (maybe) properly acculumated through reduction, so this loop of calculations is NOT VERIFIED (yet)
1348// x- the error on the 1D intensity, is correctly calculated as the standard error of the mean.
1349        for(ii=0;ii<nq;ii+=1)
1350                if(nBin_qxqy[ii] == 0)
1351                        //no pixels in annuli, data unknown
1352                        iBin_qxqy[ii] = 0
1353                        eBin_qxqy[ii] = 1
1354                        eBin2D_qxqy[ii] = NaN
1355                else
1356                        if(nBin_qxqy[ii] <= 1)
1357                                //need more than one pixel to determine error
1358                                iBin_qxqy[ii] /= nBin_qxqy[ii]
1359                                eBin_qxqy[ii] = 1
1360                                eBin2D_qxqy[ii] /= (nBin_qxqy[ii])^2
1361                        else
1362                                //assume that the intensity in each pixel in annuli is normally distributed about mean...
1363                                //  -- this is correctly calculating the error as the standard error of the mean, as
1364                                //    was always done for SANS as well.
1365                                iBin_qxqy[ii] /= nBin_qxqy[ii]
1366                                avesq = iBin_qxqy[ii]^2
1367                                aveisq = iBin2_qxqy[ii]/nBin_qxqy[ii]
1368                                var = aveisq-avesq
1369                                if(var<=0)
1370                                        eBin_qxqy[ii] = 1e-6
1371                                else
1372                                        eBin_qxqy[ii] = sqrt(var/(nBin_qxqy[ii] - 1))
1373                                endif
1374                                // and calculate as it is propagated pixel-by-pixel
1375                                eBin2D_qxqy[ii] /= (nBin_qxqy[ii])^2
1376                        endif
1377                endif
1378        endfor
1379       
1380        eBin2D_qxqy = sqrt(eBin2D_qxqy)         // as equation (3) of John's memo
1381       
1382        // find the last non-zero point, working backwards
1383        val=nq
1384        do
1385                val -= 1
1386        while((nBin_qxqy[val] == 0) && val > 0)
1387       
1388//      print val, nBin_qxqy[val]
1389        DeletePoints val, nq-val, iBin_qxqy,qBin_qxqy,nBin_qxqy,iBin2_qxqy,eBin_qxqy,eBin2D_qxqy
1390
1391        if(val == 0)
1392                // all the points were deleted
1393                return(0)
1394        endif
1395       
1396       
1397        // since the beam center is not always on the detector, many of the low Q bins will have zero pixels
1398        // find the first non-zero point, working forwards
1399        val = -1
1400        do
1401                val += 1
1402        while(nBin_qxqy[val] == 0)     
1403        DeletePoints 0, val, iBin_qxqy,qBin_qxqy,nBin_qxqy,iBin2_qxqy,eBin_qxqy,eBin2D_qxqy
1404
1405        // ?? there still may be a point in the q-range that gets zero pixel contribution - so search this out and get rid of it
1406        val = numpnts(nBin_qxqy)-1
1407        do
1408                if(nBin_qxqy[val] == 0)
1409                        DeletePoints val, 1, iBin_qxqy,qBin_qxqy,nBin_qxqy,iBin2_qxqy,eBin_qxqy,eBin2D_qxqy
1410                endif
1411                val -= 1
1412        while(val>0)
1413
1414// utility function to remove NaN values from the waves
1415        V_RemoveNaNsQIS(qBin_qxqy, iBin_qxqy, eBin_qxqy)
1416
1417       
1418        // TODO:
1419        // -- This is where I calculate the resolution in SANS (see CircSectAve)
1420        // -- use the isVCALC flag to exclude VCALC from the resolution calculation if necessary
1421        // -- from the top of the function, folderStr = work folder, type = "FLRTB" or other type of averaging
1422        //
1423        nq = numpnts(qBin_qxqy)
1424        Make/O/D/N=(nq)  $(folderPath+":"+"sigmaQ_qxqy"+"_"+type)
1425        Make/O/D/N=(nq)  $(folderPath+":"+"qBar_qxqy"+"_"+type)
1426        Make/O/D/N=(nq)  $(folderPath+":"+"fSubS_qxqy"+"_"+type)
1427        Wave sigmaq = $(folderPath+":"+"sigmaQ_qxqy_"+type)
1428        Wave qbar = $(folderPath+":"+"qBar_qxqy_"+type)
1429        Wave fsubs = $(folderPath+":"+"fSubS_qxqy_"+type)
1430        Variable ret1,ret2,ret3                 
1431
1432// all of the different collimation conditions are handled within the V_getResolution function
1433// which is responsible for switching based on the different collimation types (white beam, slit, Xtal, etc)
1434// to calculate the correct resolution, or fill the waves with the correct "flags"
1435//
1436
1437// For white beam data, the wavelength distribution can't be represented as a gaussian, but all of the other
1438//  geometric corrections still apply. Passing zero for the lambdaWidth will return the geometry contribution,
1439//  as long as the wavelength can be handled separately. It appears to be correct to do as a double integral,
1440//  with the inner(lambda) calculated first, then the outer(geometry).
1441//
1442
1443// possible values are:
1444//
1445// pinhole
1446// pinhole_whiteBeam
1447// convergingPinholes
1448//
1449// *slit data should be reduced using the slit routine, not here, proceed but warn
1450// narrowSlit
1451// narrowSlit_whiteBeam
1452//
1453        ii=0
1454        do
1455                V_getResolution(qBin_qxqy[ii],folderStr,type,collimationStr,ret1,ret2,ret3)
1456                sigmaq[ii] = ret1       
1457                qbar[ii] = ret2
1458                fsubs[ii] = ret3       
1459                ii+=1
1460        while(ii<nq)
1461
1462
1463        SetDataFolder root:
1464       
1465        return(0)
1466End
1467
1468
Note: See TracBrowser for help on using the repository browser.