source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/SANS/NCNR_Utils.ipf @ 827

Last change on this file since 827 was 827, checked in by srkline, 11 years ago

changed the line endings on Auto_fit.ipf so that diff will work properly

Changed the 2D resolution calculation to match the style and variables used in the JAC paper w/ David and John. No effect on the calculation, just for consistency.

File size: 56.3 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma version=5.0
3#pragma IgorVersion=6.1
4
5// this file contains globals and functions that are specific to a
6// particular facility or data file format
7// branched out 29MAR07 - SRK
8//
9// functions are either labeled with the procedure file that calls them,
10// or noted that they are local to this file
11
12
13// initializes globals that are specific to a particular facility
14// - number of XY pixels
15// - pixexl resolution [cm]
16// - detector deadtime constant [s]
17//
18// called by Initialize.ipf
19//
20Function InitFacilityGlobals()
21
22        //Detector -specific globals
23        Variable/G root:myGlobals:gNPixelsX=128
24        Variable/G root:myGlobals:gNPixelsY=128
25       
26        // as of Jan2008, detector pixel sizes are read directly from the file header, so they MUST
27        // be set correctly in instr.cfg
28        Variable/G root:myGlobals:PixelResNG3_ILL = 1.0         //pixel resolution in cm
29        Variable/G root:myGlobals:PixelResNG5_ILL = 1.0
30        Variable/G root:myGlobals:PixelResNG7_ILL = 1.0
31        Variable/G root:myGlobals:PixelResNG3_ORNL = 0.5
32        Variable/G root:myGlobals:PixelResNG5_ORNL = 0.5
33        Variable/G root:myGlobals:PixelResNG7_ORNL = 0.5
34        Variable/G root:myGlobals:PixelResDefault = 0.5
35       
36        Variable/G root:myGlobals:DeadtimeNG3_ILL = 3.0e-6              //deadtime in seconds
37        Variable/G root:myGlobals:DeadtimeNG5_ILL = 3.0e-6
38        Variable/G root:myGlobals:DeadtimeNG7_ILL = 3.0e-6
39        Variable/G root:myGlobals:DeadtimeNG3_ORNL_VAX = 3.4e-6         //pre - 23-JUL-2009 used VAX
40        Variable/G root:myGlobals:DeadtimeNG3_ORNL_ICE = 1.5e-6         //post - 23-JUL-2009 used ICE
41        Variable/G root:myGlobals:DeadtimeNG5_ORNL = 0.6e-6                     //as of 9 MAY 2002
42        Variable/G root:myGlobals:DeadtimeNG7_ORNL_VAX = 3.4e-6         //pre 25-FEB-2010 used VAX
43        Variable/G root:myGlobals:DeadtimeNG7_ORNL_ICE = 2.3e-6         //post 25-FEB-2010 used ICE
44        Variable/G root:myGlobals:DeadtimeDefault = 3.4e-6
45       
46        //new 11APR07
47        Variable/G root:myGlobals:BeamstopXTol = -8                     // (cm) is BS Xpos is -5 cm or less, it's a trans measurement
48        // sample aperture offset is NOT stored in the VAX header, but it should be
49        // - when it is, remove the global and write an accessor AND make a place for
50        // it in the RealsRead
51        Variable/G root:myGlobals:apOff = 5.0                           // (cm) distance from sample aperture to sample position
52
53End
54
55
56//**********************
57// Resolution calculation - used by the averaging routines
58// to calculate the resolution function at each q-value
59// - the return value is not used
60//
61// equivalent to John's routine on the VAX Q_SIGMA_AVE.FOR
62// Incorporates eqn. 3-15 from J. Appl. Cryst. (1995) v. 28 p105-114
63//
64// - 21 MAR 07 uses projected BS diameter on the detector
65// - APR 07 still need to add resolution with lenses. currently there is no flag in the
66//          raw data header to indicate the presence of lenses.
67//
68// - Aug 07 - added input to switch calculation based on lenses (==1 if in)
69//
70// - called by CircSectAvg.ipf and RectAnnulAvg.ipf
71//
72// passed values are read from RealsRead
73// except DDet and apOff, which are set from globals before passing
74//
75//
76Function/S getResolution(inQ,lambda,lambdaWidth,DDet,apOff,S1,S2,L1,L2,BS,del_r,usingLenses,SigmaQ,QBar,fSubS)
77        Variable inQ, lambda, lambdaWidth, DDet, apOff, S1, S2, L1, L2, BS, del_r,usingLenses
78        Variable &fSubS, &QBar, &SigmaQ         //these are the output quantities at the input Q value
79       
80        //lots of calculation variables
81        Variable a2, q_small, lp, v_lambda, v_b, v_d, vz, yg, v_g
82        Variable r0, delta, inc_gamma, fr, fv, rmd, v_r1, rm, v_r
83
84        //Constants
85        Variable vz_1 = 3.956e5         //velocity [cm/s] of 1 A neutron
86        Variable g = 981.0                              //gravity acceleration [cm/s^2]
87
88        String results
89        results ="Failure"
90
91        S1 *= 0.5*0.1                   //convert to radius and [cm]
92        S2 *= 0.5*0.1
93
94        L1 *= 100.0                     // [cm]
95        L1 -= apOff                             //correct the distance
96
97        L2 *= 100.0
98        L2 += apOff
99        del_r *= 0.1                            //width of annulus, convert mm to [cm]
100       
101        BS *= 0.5*0.1                   //nominal BS diameter passed in, convert to radius and [cm]
102        // 21 MAR 07 SRK - use the projected BS diameter, based on a point sample aperture
103        Variable LB
104        LB = 20.1 + 1.61*BS                     //distance in cm from beamstop to anode plane (empirical)
105        BS = bs + bs*lb/(l2-lb)         //adjusted diameter of shadow from parallax
106       
107        //Start resolution calculation
108        a2 = S1*L2/L1 + S2*(L1+L2)/L1
109        q_small = 2.0*Pi*(BS-a2)*(1.0-lambdaWidth)/(lambda*L2)
110        lp = 1.0/( 1.0/L1 + 1.0/L2)
111
112        v_lambda = lambdaWidth^2/6.0
113       
114//      if(usingLenses==1)                      //SRK 2007
115        if(usingLenses != 0)                    //SRK 2008 allows for the possibility of different numbers of lenses in header
116                v_b = 0.25*(S1*L2/L1)^2 +0.25*(2/3)*(lambdaWidth/lambda)^2*(S2*L2/lp)^2         //correction to 2nd term
117        else
118                v_b = 0.25*(S1*L2/L1)^2 +0.25*(S2*L2/lp)^2              //original form
119        endif
120       
121        v_d = (DDet/2.3548)^2 + del_r^2/12.0
122        vz = vz_1 / lambda
123        yg = 0.5*g*L2*(L1+L2)/vz^2
124        v_g = 2.0*(2.0*yg^2*v_lambda)                                   //factor of 2 correction, B. Hammouda, 2007
125
126        r0 = L2*tan(2.0*asin(lambda*inQ/(4.0*Pi) ))
127        delta = 0.5*(BS - r0)^2/v_d
128
129        if (r0 < BS)
130                inc_gamma=exp(gammln(1.5))*(1-gammp(1.5,delta))
131        else
132                inc_gamma=exp(gammln(1.5))*(1+gammp(1.5,delta))
133        endif
134
135        fSubS = 0.5*(1.0+erf( (r0-BS)/sqrt(2.0*v_d) ) )
136        if (fSubS <= 0.0)
137                fSubS = 1.e-10
138        endif
139        fr = 1.0 + sqrt(v_d)*exp(-1.0*delta) /(r0*fSubS*sqrt(2.0*Pi))
140        fv = inc_gamma/(fSubS*sqrt(Pi)) - r0^2*(fr-1.0)^2/v_d
141
142        rmd = fr*r0
143        v_r1 = v_b + fv*v_d +v_g
144
145        rm = rmd + 0.5*v_r1/rmd
146        v_r = v_r1 - 0.5*(v_r1/rmd)^2
147        if (v_r < 0.0)
148                v_r = 0.0
149        endif
150        QBar = (4.0*Pi/lambda)*sin(0.5*atan(rm/L2))
151        SigmaQ = QBar*sqrt(v_r/rmd^2 +v_lambda)
152
153
154// more readable method for calculating the variance in Q
155// EXCEPT - this is calculated for Qo, NOT qBar
156// (otherwise, they are nearly equivalent, except for close to the beam stop)
157//      Variable kap,a_val,a_val_l2,m_h
158//      g = 981.0                               //gravity acceleration [cm/s^2]
159//      m_h     = 252.8                 // m/h [=] s/cm^2
160//     
161//      kap = 2*pi/lambda
162//      a_val = L2*(L1+L2)*g/2*(m_h)^2
163//      a_val_L2 = a_val/L2*1e-16               //convert 1/cm^2 to 1/A^2
164//
165//      sigmaQ = 0
166//      sigmaQ = 3*(S1/L1)^2
167//     
168//      if(usingLenses != 0)
169//              sigmaQ += 2*(S2/lp)^2*(lambdaWidth)^2   //2nd term w/ lenses
170//      else   
171//              sigmaQ += 2*(S2/lp)^2                                           //2nd term w/ no lenses
172//      endif
173//     
174//      sigmaQ += (del_r/L2)^2
175//      sigmaQ += 2*(r0/L2)^2*(lambdaWidth)^2
176//      sigmaQ += 4*(a_val_l2)^2*lambda^4*(lambdaWidth)^2
177//     
178//      sigmaQ *= kap^2/12
179//      sigmaQ = sqrt(sigmaQ)
180
181
182        results = "success"
183        Return results
184End
185
186
187//
188//**********************
189// 2D resolution function calculation - ***NOT*** in terms of X and Y
190// but written in terms of Parallel and perpendicular to the Q vector at each point
191//
192// -- it must be written this way since the 2D function is an ellipse with its major
193// axis pointing in the direction of Q_parallel. Hence there is no way to properly define the
194// elliptical gaussian in terms of sigmaX and sigmaY
195//
196// based on notes from David Mildner, 2008
197//
198//
199// - 21 MAR 07 uses projected BS diameter on the detector
200// - APR 07 still need to add resolution with lenses. currently there is no flag in the
201//          raw data header to indicate the presence of lenses.
202//
203// - Aug 07 - added input to switch calculation based on lenses (==1 if in)
204//
205// passed values are read from RealsRead
206// except DDet and apOff, which are set from globals before passing
207//
208// phi is the azimuthal angle, CCW from +x axis
209// r_dist is the real-space distance from ctr of detector to QxQy pixel location
210//
211// MAR 2011 - removed the del_r terms, they don't apply since no bining is done to the 2D data
212//
213Function/S get2DResolution(inQ,phi,lambda,lambdaWidth,DDet,apOff,S1,S2,L1,L2,BS,del_r,usingLenses,r_dist,SigmaQX,SigmaQY,fSubS)
214        Variable inQ, phi,lambda, lambdaWidth, DDet, apOff, S1, S2, L1, L2, BS, del_r,usingLenses,r_dist
215        Variable &SigmaQX,&SigmaQY,&fSubS               //these are the output quantities at the input Q value
216       
217        //lots of calculation variables
218        Variable a2, lp, v_lambda, v_b, v_d, vz, yg, v_g
219        Variable r0, delta, inc_gamma, fr, fv, rmd, v_r1, rm, v_r
220
221        //Constants
222        Variable vz_1 = 3.956e5         //velocity [cm/s] of 1 A neutron
223        Variable g = 981.0                              //gravity acceleration [cm/s^2]
224        Variable m_h    = 252.8                 // m/h [=] s/cm^2
225
226        String results
227        results ="Failure"
228
229        S1 *= 0.5*0.1                   //convert to radius and [cm]
230        S2 *= 0.5*0.1
231
232        L1 *= 100.0                     // [cm]
233        L1 -= apOff                             //correct the distance
234
235        L2 *= 100.0
236        L2 += apOff
237        del_r *= 0.1                            //width of annulus, convert mm to [cm]
238       
239        BS *= 0.5*0.1                   //nominal BS diameter passed in, convert to radius and [cm]
240        // 21 MAR 07 SRK - use the projected BS diameter, based on a point sample aperture
241        Variable LB
242        LB = 20.1 + 1.61*BS                     //distance in cm from beamstop to anode plane (empirical)
243        BS = bs + bs*lb/(l2-lb)         //adjusted diameter of shadow from parallax
244       
245        //Start resolution calculation
246        a2 = S1*L2/L1 + S2*(L1+L2)/L1
247        lp = 1.0/( 1.0/L1 + 1.0/L2)
248
249        v_lambda = lambdaWidth^2/6.0
250       
251//      if(usingLenses==1)                      //SRK 2007
252        if(usingLenses != 0)                    //SRK 2008 allows for the possibility of different numbers of lenses in header
253                v_b = 0.25*(S1*L2/L1)^2 +0.25*(2/3)*(lambdaWidth/lambda)^2*(S2*L2/lp)^2         //correction to 2nd term
254        else
255                v_b = 0.25*(S1*L2/L1)^2 +0.25*(S2*L2/lp)^2              //original form
256        endif
257       
258        v_d = (DDet/2.3548)^2 + del_r^2/12.0
259        vz = vz_1 / lambda
260        yg = 0.5*g*L2*(L1+L2)/vz^2
261        v_g = 2.0*(2.0*yg^2*v_lambda)                                   //factor of 2 correction, B. Hammouda, 2007
262
263        r0 = L2*tan(2.0*asin(lambda*inQ/(4.0*Pi) ))
264        delta = 0.5*(BS - r0)^2/v_d
265
266        if (r0 < BS)
267                inc_gamma=exp(gammln(1.5))*(1-gammp(1.5,delta))
268        else
269                inc_gamma=exp(gammln(1.5))*(1+gammp(1.5,delta))
270        endif
271
272        fSubS = 0.5*(1.0+erf( (r0-BS)/sqrt(2.0*v_d) ) )
273        if (fSubS <= 0.0)
274                fSubS = 1.e-10
275        endif
276//      fr = 1.0 + sqrt(v_d)*exp(-1.0*delta) /(r0*fSubS*sqrt(2.0*Pi))
277//      fv = inc_gamma/(fSubS*sqrt(Pi)) - r0^2*(fr-1.0)^2/v_d
278//
279//      rmd = fr*r0
280//      v_r1 = v_b + fv*v_d +v_g
281//
282//      rm = rmd + 0.5*v_r1/rmd
283//      v_r = v_r1 - 0.5*(v_r1/rmd)^2
284//      if (v_r < 0.0)
285//              v_r = 0.0
286//      endif
287
288        Variable kap,a_val,a_val_L2,proj_DDet
289       
290        kap = 2*pi/lambda
291        a_val = L2*(L1+L2)*g/2*(m_h)^2
292        a_val_L2 = a_val/L2*1e-16               //convert 1/cm^2 to 1/A^2
293
294
295        // the detector pixel is square, so correct for phi
296        proj_DDet = DDet*cos(phi) + DDet*sin(phi)
297
298
299///////// OLD - don't use ---
300//in terms of Q_parallel ("x") and Q_perp ("y") - this works, since parallel is in the direction of Q and I
301// can calculate that from the QxQy (I just need the projection)
302//// for test case with no gravity, set a_val = 0
303//// note that gravity has no wavelength dependence. the lambda^4 cancels out.
304////
305////    a_val = 0
306////    a_val_l2 = 0
307//
308//     
309//      // this is really sigma_Q_parallel
310//      SigmaQX = kap*kap/12 * (3*(S1/L1)^2 + 3*(S2/LP)^2 + (proj_DDet/L2)^2 + (sin(phi))^2*8*(a_val_L2)^2*lambda^4*lambdaWidth^2)
311//      SigmaQX += inQ*inQ*v_lambda
312//
313//      //this is really sigma_Q_perpendicular
314//      proj_DDet = DDet*sin(phi) + DDet*cos(phi)               //not necessary, since DDet is the same in both X and Y directions
315//
316//      SigmaQY = kap*kap/12 * (3*(S1/L1)^2 + 3*(S2/LP)^2 + (proj_DDet/L2)^2 + (cos(phi))^2*8*(a_val_L2)^2*lambda^4*lambdaWidth^2)
317//     
318//      SigmaQX = sqrt(SigmaQX)
319//      SigmaQy = sqrt(SigmaQY)
320//     
321
322/////////////////////////////////////////////////
323/////   
324//      ////// this is all new, inclusion of gravity effect into the parallel component
325//       perpendicular component is purely geometric, no gravity component
326////    //
327        Variable yg_d,acc,sdd,ssd,lambda0,DL_L,sig_l
328        Variable var_qlx,var_qly,var_ql,qx,qy,sig_perp,sig_para, sig_para_new
329        G = 981.  //!   ACCELERATION OF GRAVITY, CM/SEC^2
330        acc = vz_1              //      3.956E5 //!     CONVERT WAVELENGTH TO VELOCITY CM/SEC
331        SDD = L2                //1317
332        SSD = L1                //1627          //cm
333        lambda0 = lambda                //              15
334        DL_L = lambdaWidth              //0.236
335        SIG_L = DL_L/sqrt(6)
336        YG_d = -0.5*G*SDD*(SSD+SDD)*(LAMBDA0/acc)^2
337/////   Print "DISTANCE BEAM FALLS DUE TO GRAVITY (CM) = ",YG
338       
339        sig_perp = kap*kap/12 * (3*(S1/L1)^2 + 3*(S2/LP)^2 + (proj_DDet/L2)^2)
340        sig_perp = sqrt(sig_perp)
341       
342       
343        FindQxQy(inQ,phi,qx,qy)
344
345// missing a factor of 2 here, and the form is different than the paper, so re-write   
346//      VAR_QLY = SIG_L^2 * (QY+4*PI*YG_d/(2*SDD*LAMBDA0))^2
347//      VAR_QLX = (SIG_L*QX)^2
348//      VAR_QL = VAR_QLY + VAR_QLX  //! WAVELENGTH CONTRIBUTION TO VARIANCE
349//      sig_para = (sig_perp^2 + VAR_QL)^0.5
350       
351        // r_dist is passed in, [=]cm
352        // from the paper
353        a_val = 0.5*G*SDD*(SSD+SDD)*m_h^2 * 1e-16               //units now are cm /(A^2)
354       
355        var_QL = 1/6*(kap/SDD)^2*(DL_L)^2*(r_dist^2 - 4*r_dist*a_val*lambda0^2*sin(phi) + 4*a_val^2*lambda0^4)
356        sig_para_new = (sig_perp^2 + VAR_QL)^0.5
357       
358       
359///// return values PBR
360        SigmaQX = sig_para_new
361        SigmaQy = sig_perp
362       
363////   
364       
365        results = "success"
366        Return results
367End
368
369
370
371
372//Utility function that returns the detector resolution (in cm)
373//Global values are set in the Initialize procedure
374//
375//
376// - called by CircSectAvg.ipf, RectAnnulAvg.ipf, and ProtocolAsPanel.ipf
377//
378// fileStr is passed as TextRead[3]
379// detStr is passed as TextRead[9]
380//
381// *** as of Jan 2008, depricated. Now detector pixel sizes are read from the file header
382// rw[10] = x size (mm); rw[13] = y size (mm)
383//
384Function xDetectorPixelResolution(fileStr,detStr)
385        String fileStr,detStr
386       
387        Variable DDet
388        String instr=fileStr[1,3]       //filestr is "[NGnSANSn] " or "[NGnSANSnn]" (11 characters total)
389       
390        NVAR PixelResNG3_ILL = root:myGlobals:PixelResNG3_ILL           //pixel resolution in cm
391        NVAR PixelResNG5_ILL = root:myGlobals:PixelResNG5_ILL
392        NVAR PixelResNG7_ILL = root:myGlobals:PixelResNG7_ILL
393        NVAR PixelResNG3_ORNL = root:myGlobals:PixelResNG3_ORNL
394        NVAR PixelResNG5_ORNL = root:myGlobals:PixelResNG5_ORNL
395        NVAR PixelResNG7_ORNL = root:myGlobals:PixelResNG7_ORNL
396        NVAR PixelResDefault = root:myGlobals:PixelResDefault
397       
398        strswitch(instr)
399                case "NG3":
400                        if(cmpstr(detStr, "ILL   ") == 0 )
401                                DDet= PixelResNG3_ILL
402                        else
403                                DDet = PixelResNG3_ORNL //detector is ordella-type
404                        endif
405                        break
406                case "NG5":
407                        if(cmpstr(detStr, "ILL   ") == 0 )
408                                DDet= PixelResNG5_ILL
409                        else
410                                DDet = PixelResNG5_ORNL //detector is ordella-type
411                        endif
412                        break
413                case "NG7":
414                        if(cmpstr(detStr, "ILL   ") == 0 )
415                                DDet= PixelResNG7_ILL
416                        else
417                                DDet = PixelResNG7_ORNL //detector is ordella-type
418                        endif
419                        break
420                default:                                                       
421                        //return error?
422                        DDet = PixelResDefault  //0.5 cm, typical for new ORNL detectors
423        endswitch
424       
425        return(DDet)
426End
427
428//Utility function that returns the detector deadtime (in seconds)
429//Global values are set in the Initialize procedure
430//
431// - called by WorkFileUtils.ipf
432//
433// fileStr is passed as TextRead[3]
434// detStr is passed as TextRead[9]
435// dateAndTimeStr is passed as TextRead[1]
436//      --- if no date/time string is passed, ICE values are the default
437//
438// Due to the switch from VAX -> ICE, there were some hardware changes that led to
439// a change in the effective detector dead time. The dead time was re-measured by J. Barker
440// as follows:
441//      Instrument                              Date measured                           deadtime constant
442//      NG3                                                     DECEMBER 2009                           1.5 microseconds
443//      NG7                                                     APRIL2010                                       2.3 microseconds
444//
445// The day of the switch to ICE on NG7 was 25-FEB-2010 (per J. Krzywon)
446// The day of the switch to ICE on NG3 was 23-JUL-2009 (per C. Gagnon)
447//
448// so any data after these dates is to use the new dead time constant. The switch is made on the
449// data collection date.
450//
451//
452Function DetectorDeadtime(fileStr,detStr,[dateAndTimeStr])
453        String fileStr,detStr,dateAndTimeStr
454       
455        Variable deadtime
456        String instr=fileStr[1,3]       //filestr is "[NGnSANSn] " or "[NGnSANSnn]" (11 characters total)
457       
458        NVAR DeadtimeNG3_ILL = root:myGlobals:DeadtimeNG3_ILL           //pixel resolution in cm
459        NVAR DeadtimeNG5_ILL = root:myGlobals:DeadtimeNG5_ILL
460        NVAR DeadtimeNG7_ILL = root:myGlobals:DeadtimeNG7_ILL
461        NVAR DeadtimeNG3_ORNL_VAX = root:myGlobals:DeadtimeNG3_ORNL_VAX
462        NVAR DeadtimeNG3_ORNL_ICE = root:myGlobals:DeadtimeNG3_ORNL_ICE
463        NVAR DeadtimeNG5_ORNL = root:myGlobals:DeadtimeNG5_ORNL
464        NVAR DeadtimeNG7_ORNL_VAX = root:myGlobals:DeadtimeNG7_ORNL_VAX
465        NVAR DeadtimeNG7_ORNL_ICE = root:myGlobals:DeadtimeNG7_ORNL_ICE
466        NVAR DeadtimeDefault = root:myGlobals:DeadtimeDefault
467       
468        // if no date string is passed, default to the ICE values
469        if(strlen(dateAndTimeStr)==0)
470                dateAndTimeStr = "01-JAN-2011"          //dummy date to force to ICE values
471        endif
472       
473       
474        Variable NG3_to_ICE = ConvertVAXDay2secs("23-JUL-2009")
475        Variable NG7_to_ICE = ConvertVAXDay2secs("25-FEB-2010")
476        Variable fileTime = ConvertVAXDay2secs(dateAndTimeStr)
477
478       
479        strswitch(instr)
480                case "NG3":
481                        if(cmpstr(detStr, "ILL   ") == 0 )
482                                deadtime= DeadtimeNG3_ILL
483                        else
484                                if(fileTime > NG3_to_ICE)
485                                        deadtime = DeadtimeNG3_ORNL_ICE //detector is ordella-type, using ICE hardware
486                                else
487                                        deadtime = DeadtimeNG3_ORNL_VAX //detector is ordella-type
488                                endif
489                        endif
490                        break
491                case "NG5":
492                        if(cmpstr(detStr, "ILL   ") == 0 )
493                                deadtime= DeadtimeNG5_ILL
494                        else
495                                deadtime = DeadtimeNG5_ORNL     //detector is ordella-type
496                        endif
497                        break
498                case "NG7":
499                        if(cmpstr(detStr, "ILL   ") == 0 )
500                                deadtime= DeadtimeNG7_ILL
501                        else
502                                if(fileTime > NG7_to_ICE)
503                                        deadtime = DeadtimeNG7_ORNL_ICE //detector is ordella-type, using ICE hardware
504                                else
505                                        deadtime = DeadtimeNG7_ORNL_VAX //detector is ordella-type
506                                endif
507                        endif
508                        break
509                default:                                                       
510                        //return error?
511                        deadtime = DeadtimeDefault      //1e-6 seconds, typical for new ORNL detectors
512        endswitch
513       
514        return(deadtime)
515End
516
517// converts ONLY DD-MON-YYYY portion of the data collection time
518// to a number of seconds from midnight on 1/1/1904, as Igor likes to do
519//
520// dateAndTime is the full string of "dd-mon-yyyy hh:mm:ss" as returned by the function
521// getFileCreationDate(file)
522//
523Function ConvertVAXDay2secs(dateAndTime)
524        string dateAndTime
525       
526        Variable day,yr,mon,time_secs
527        string monStr
528
529        sscanf dateandtime,"%d-%3s-%4d",day,monStr,yr
530        mon = monStr2num(monStr)
531//      print yr,mon,day
532        time_secs = date2secs(yr,mon,day)
533
534        return(time_secs)
535end
536
537// dd-mon-yyyy hh:mm:ss -> seconds
538// the VAX uses 24 hr time for hh
539//
540Function ConvertVAXDateTime2secs(dateAndTime)
541        string dateAndTime
542       
543        Variable day,yr,mon,hh,mm,ss,time_secs
544        string str,monStr
545       
546        str=dateandtime
547        sscanf str,"%d-%3s-%4d %d:%d:%d",day,monStr,yr,hh,mm,ss
548        mon = monStr2num(monStr)
549//      print yr,mon,day,hh,mm,ss
550        time_secs = date2secs(yr,mon,day)
551        time_secs += hh*3600 + mm*60 + ss
552
553
554        return(time_secs)
555end
556
557// takes a month string and returns the corresponding number
558//
559Function monStr2num(monStr)
560        String monStr
561       
562        String list=";JAN;FEB;MAR;APR;MAY;JUN;JUL;AUG;SEP;OCT;NOV;DEC;"
563        return(WhichListItem(monStr, list ,";"))
564end
565
566
567//make a three character string of the run number
568//Moved to facility utils
569Function/S RunDigitString(num)
570        Variable num
571       
572        String numStr=""
573        if(num<10)
574                numStr = "00"+num2str(num)
575        else
576                if(num<100)
577                        numStr = "0"+num2str(num)
578                else
579                        numStr = num2str(num)
580                Endif
581        Endif
582        //Print "numstr = ",numstr
583        return(numstr)
584End
585
586//given a filename of a SANS data filename of the form
587//TTTTTnnn.SAn_TTT_Txxx
588//returns the prefix "TTTTT" as some number of characters
589//returns "" as an invalid file prefix
590//
591// NCNR-specifc, does not really belong here - but it's a beta procedure used for the
592// Combine Files Panel
593//
594Function/S GetPrefixStrFromFile(item)
595        String item
596        String invalid = ""     //"" is not a valid run prefix, since it's text
597        Variable num=-1
598       
599        //find the "dot"
600        String runStr=""
601        Variable pos = strsearch(item,".",0)
602        if(pos == -1)
603                //"dot" not found
604                return (invalid)
605        else
606                //found, skip the three characters preceeding it
607                if (pos <=3)
608                        //not enough characters
609                        return (invalid)
610                else
611                        runStr = item[0,pos-4]
612                        return (runStr)
613                Endif
614        Endif
615End
616
617
618
619
620/////VAX filename/Run number parsing utilities
621//
622// a collection of uilities for processing vax filenames
623//and processing lists (especially for display in popup menus)
624//
625//required to correctly account for VAX supplied version numbers, which
626//may or may not be removed by the ftp utility
627//
628// - parses lists of run numbers into real filenames
629// - selects proper detector constants
630//
631//**************************
632//
633//given a filename of a SANS data filename of the form
634//TTTTTnnn.SAn_TTT_Txxx
635//returns the run number "nnn" as a number
636//returns -1 as an invalid file number
637//
638// called by several ipfs
639//
640//
641Function GetRunNumFromFile(item)
642        String item
643        Variable invalid = -1   //negative numbers are invalid
644        Variable num=-1
645       
646        //find the "dot"
647        String runStr=""
648        Variable pos = strsearch(item,".",0)
649        if(pos == -1)
650                //"dot" not found
651                return (invalid)
652        else
653                //found, get the three characters preceeding it
654                if (pos <=2)
655                        //not enough characters
656                        return (invalid)
657                else
658                        runStr = item[pos-3,pos-1]
659                        //convert to a number
660                        num = str2num(runStr)
661                        //if valid, return it
662                        if (num == NaN)
663                                //3 characters were not a number
664                                return (invalid)
665                        else
666                                //run was OK
667                                return (num)
668                        Endif
669                Endif
670        Endif
671End
672
673//given a filename of a SANS data filename of the form
674//TTTTTnnn.SAn_TTT_Txxx
675//returns the run number "nnn" as a STRING of THREE characters
676//returns "ABC" as an invalid file number
677//
678// local function to aid in locating files by run number
679//
680Function/S GetRunNumStrFromFile(item)
681        String item
682        String invalid = "ABC"  //"ABC" is not a valid run number, since it's text
683        Variable num=-1
684       
685        //find the "dot"
686        String runStr=""
687        Variable pos = strsearch(item,".",0)
688        if(pos == -1)
689                //"dot" not found
690                return (invalid)
691        else
692                //found, get the three characters preceeding it
693                if (pos <=2)
694                        //not enough characters
695                        return (invalid)
696                else
697                        runStr = item[pos-3,pos-1]
698                        return (runStr)
699                Endif
700        Endif
701End
702
703//returns a string containing the full path to the file containing the
704//run number "num". The null string is returned if no valid file can be found
705//the path "catPathName" used and is hard-wired, will abort if this path does not exist
706//the file returned will be a RAW SANS data file, other types of files are
707//filtered out.
708//
709// called by Buttons.ipf and Transmission.ipf, and locally by parsing routines
710//
711Function/S FindFileFromRunNumber(num)
712        Variable num
713       
714        String fullName="",partialName="",item=""
715        //get list of raw data files in folder that match "num" (add leading zeros)
716        if( (num>999) || (num<=0) )
717                //Print "error in  FindFileFromRunNumber(num), file number too large or too small"
718                Return ("")
719        Endif
720        //make a three character string of the run number
721        String numStr=""
722        if(num<10)
723                numStr = "00"+num2str(num)
724        else
725                if(num<100)
726                        numStr = "0"+num2str(num)
727                else
728                        numStr = num2str(num)
729                Endif
730        Endif
731        //Print "numstr = ",numstr
732       
733        //make sure that path exists
734        PathInfo catPathName
735        String path = S_path
736        if (V_flag == 0)
737                Abort "folder path does not exist - use Pick Path button"
738        Endif
739        String list="",newList="",testStr=""
740       
741        list = IndexedFile(catPathName,-1,"????")       //get all files in folder
742        //find (the) one with the number in the run # location in the name
743        Variable numItems,ii,runFound,isRAW
744        numItems = ItemsInList(list,";")                //get the new number of items in the list
745        ii=0
746        do
747                //parse through the list in this order:
748                // 1 - does item contain run number (as a string) "TTTTTnnn.SAn_XXX_Tyyy"
749                // 2 - exclude by isRaw? (to minimize disk access)
750                item = StringFromList(ii, list  ,";" )
751                if(strlen(item) != 0)
752                        //find the run number, if it exists as a three character string
753                        testStr = GetRunNumStrFromFile(item)
754                        runFound= cmpstr(numStr,testStr)        //compare the three character strings, 0 if equal
755                        if(runFound == 0)
756                                //the run Number was found
757                                //build valid filename
758                                partialName = FindValidFileName(item)
759                                if(strlen(partialName) != 0)            //non-null return from FindValidFileName()
760                                        fullName = path + partialName
761                                        //check if RAW, if so,this must be the file!
762                                        isRAW = CheckIfRawData(fullName)
763                                        if(isRaw)
764                                                //stop here
765                                                return(fullname)
766                                        Endif
767                                Endif
768                        Endif
769                Endif
770                ii+=1
771        while(ii<numItems)              //process all items in list
772        Return ("")     //null return if file not found in list
773End
774
775//function to test a binary file to see if it is a RAW binary SANS file
776//first checks the total bytes in the file (which for raw data is 33316 bytes)
777//**note that the "DIV" file will also show up as a raw file by the run field
778//should be listed in CAT/SHORT and in patch windows
779//
780//Function then checks the file fname (full path:file) for "RAW" run.type field
781//if not found, the data is not raw data and zero is returned
782//
783// called by many procedures (both external and local)
784//
785Function CheckIfRawData(fname)
786        String fname
787       
788        Variable refnum,totalBytes
789        String testStr=""
790       
791        Open/R/T="????TEXT" refNum as fname
792        if(strlen(s_filename) == 0)     //user cancel (/Z not used, so V_flag not set)
793                return(0)
794        endif
795       
796        //get the total number of bytes in the file
797        FStatus refNum
798        totalBytes = V_logEOF
799        //Print totalBytes
800        if(totalBytes < 100)
801                Close refNum
802                return(0)               //not a raw file
803        endif
804        FSetPos refNum,75
805        FReadLine/N=3 refNum,testStr
806        Close refNum
807       
808        if(totalBytes == 33316 && ( cmpstr(testStr,"RAW")==0 ||  cmpstr(testStr,"SIM")==0))
809                //true, is raw data file
810                Return(1)
811        else
812                //some other file
813                Return(0)
814        Endif
815End
816
817//function to check the header of a raw data file (full path specified by fname)
818//checks the field of the x-position of the beamstop during data collection
819//if the x-position is more negative (farther to the left) than xTol(input)
820//the the beamstop is "out" and the file is a transmission run and not a scattering run
821//xtol typically set at -5 (cm) - trans runs have bs(x) at -10 to -15 cm
822// function returns 1 if beamstop is out, 0 if beamstop is in
823//
824// tolerance is set as a global value "root:myGlobals:BeamstopXTol"
825//
826// called by Transmission.ipf, CatVSTable.ipf, NSORT.ipf
827//
828Function isTransFile(fName)
829        String fname
830       
831        Variable refnum,xpos
832        NVAR xTol = root:myGlobals:BeamstopXTol
833       
834        //pos = 369, read one real value
835       
836        SetDataFolder root:
837        String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
838        String strToExecute=""
839        // 1 R*4 value
840        strToExecute = GBLoadStr + "/S=368/U=1" + "\"" + fname + "\""
841        Execute strToExecute
842        Wave w=$"root:tempGBWave0"
843        xPos = w[0]
844        KillWaves/Z w
845        //Print "xPos = ",xpos
846       
847        if(xpos<=xTol)
848                //xpos is farther left (more negative) than xtol (currently -5 cm)
849                Return(1)
850        else
851                //some other file
852                Return(0)
853        Endif
854End
855
856
857//function to remove all spaces from names when searching for filenames
858//the filename (as saved) will never have interior spaces (TTTTTnnn_AB _Bnnn)
859//but the text field in the header WILL, if less than 3 characters were used for the
860//user's initials, and can have leading spaces if prefix was less than 5 characters
861//
862//returns a string identical to the original string, except with the interior spaces removed
863//
864// local function for file name manipulation
865//
866Function/S RemoveAllSpaces(str)
867        String str
868       
869        String tempstr = str
870        Variable ii,spc,len             //should never be more than 2 or 3 trailing spaces in a filename
871        ii=0
872        do
873                len = strlen(tempStr)
874                spc = strsearch(tempStr," ",0)          //is the last character a space?
875                if (spc == -1)
876                        break           //no more spaces found, get out
877                endif
878                str = tempstr
879                tempStr = str[0,(spc-1)] + str[(spc+1),(len-1)] //remove the space from the string
880        While(1)        //should never be more than 2 or 3
881       
882        If(strlen(tempStr) < 1)
883                tempStr = ""            //be sure to return a null string if problem found
884        Endif
885       
886        //Print strlen(tempstr)
887       
888        Return(tempStr)
889               
890End
891
892
893//Function attempts to find valid filename from partial name by checking for
894// the existence of the file on disk.
895// - checks as is
896// - adds ";vers" for possible VAX files
897// - strips spaces
898// - permutations of upper/lowercase
899//
900// added 11/99 - uppercase and lowercase versions of the file are tried, if necessary
901// since from marquee, the filename field (textread[0]) must be used, and can be a mix of
902// upper/lowercase letters, while the filename on the server (should) be all caps
903// now makes repeated calls to ValidFileString()
904//
905// returns a valid filename (No path prepended) or a null string
906//
907// called by any functions, both external and local
908//
909Function/S FindValidFilename(partialName)
910        String PartialName
911       
912        String retStr=""
913       
914        //try name with no changes - to allow for ABS files that have spaces in the names 12APR04
915        retStr = ValidFileString(partialName)
916        if(cmpstr(retStr,"") !=0)
917                //non-null return
918                return(retStr)
919        Endif
920       
921        //if the partial name is derived from the file header, there can be spaces at the beginning
922        //or in the middle of the filename - depending on the prefix and initials used
923        //
924        //remove any leading spaces from the name before starting
925        partialName = RemoveAllSpaces(partialName)
926       
927        //try name with no spaces
928        retStr = ValidFileString(partialName)
929        if(cmpstr(retStr,"") !=0)
930                //non-null return
931                return(retStr)
932        Endif
933       
934        //try all UPPERCASE
935        partialName = UpperStr(partialName)
936        retStr = ValidFileString(partialName)
937        if(cmpstr(retStr,"") !=0)
938                //non-null return
939                return(retStr)
940        Endif
941       
942        //try all lowercase (ret null if failure)
943        partialName = LowerStr(partialName)
944        retStr = ValidFileString(partialName)
945        if(cmpstr(retStr,"") !=0)
946                //non-null return
947                return(retStr)
948        else
949                return(retStr)
950        Endif
951End
952
953// Function checks for the existence of a file
954// partialName;vers (to account for VAX filenaming conventions)
955// The partial name is tried first with no version number
956//
957// *** the PATH is hard-wired to catPathName (which is assumed to exist)
958// version numers up to ;10 are tried
959// only the "name;vers" is returned if successful. The path is not prepended
960//
961// local function
962//
963Function/S ValidFileString(partialName)
964        String partialName
965       
966        String tempName = "",msg=""
967        Variable ii,refnum
968       
969        ii=0
970        do
971                if(ii==0)
972                        //first pass, try the partialName
973                        tempName = partialName
974                        Open/Z/R/T="????TEXT"/P=catPathName refnum tempName     //Does open file (/Z flag)
975                        if(V_flag == 0)
976                                //file exists
977                                Close refnum            //YES needed,
978                                break
979                        endif
980                else
981                        tempName = partialName + ";" + num2str(ii)
982                        Open/Z/R/T="????TEXT"/P=catPathName refnum tempName
983                        if(V_flag == 0)
984                                //file exists
985                                Close refnum
986                                break
987                        endif
988                Endif
989                ii+=1
990                //print "ii=",ii
991        while(ii<11)
992        //go get the selected bits of information, using tempName, which exists
993        if(ii>=11)
994                //msg = partialName + " not found. is version number > 11?"
995                //DoAlert 0, msg
996                //PathInfo catPathName
997                //Print S_Path
998                Return ("")             //use null string as error condition
999        Endif
1000       
1001        Return (tempName)
1002End
1003
1004//returns a string containing filename (WITHOUT the ;vers)
1005//the input string is a full path to the file (Mac-style, still works on Win in IGOR)
1006//with the folders separated by colons
1007//
1008// called by MaskUtils.ipf, ProtocolAsPanel.ipf, WriteQIS.ipf
1009//
1010Function/S GetFileNameFromPathNoSemi(fullPath)
1011        String fullPath
1012       
1013        Variable offset1,offset2
1014        String filename=""
1015        //String PartialPath
1016        offset1 = 0
1017        do
1018                offset2 = StrSearch(fullPath, ":", offset1)
1019                if (offset2 == -1)                              // no more colons ?
1020                        fileName = FullPath[offset1,strlen(FullPath) ]
1021                        //PartialPath = FullPath[0, offset1-1]
1022                        break
1023                endif
1024                offset1 = offset2+1
1025        while (1)
1026       
1027        //remove version number from name, if it's there - format should be: filename;N
1028        filename =  StringFromList(0,filename,";")              //returns null if error
1029       
1030        Return filename
1031End
1032
1033//returns a string containing filename (INCLUDING the ;vers)
1034//the input string is a full path to the file (Mac-style, still works on Win in IGOR)
1035//with the folders separated by colons
1036//
1037// local, currently unused
1038//
1039Function/S GetFileNameFromPathKeepSemi(fullPath)
1040        String fullPath
1041       
1042        Variable offset1,offset2
1043        String filename
1044        //String PartialPath
1045        offset1 = 0
1046        do
1047                offset2 = StrSearch(fullPath, ":", offset1)
1048                if (offset2 == -1)                              // no more colons ?
1049                        fileName = FullPath[offset1,strlen(FullPath) ]
1050                        //PartialPath = FullPath[0, offset1-1]
1051                        break
1052                endif
1053                offset1 = offset2+1
1054        while (1)
1055       
1056        //keep version number from name, if it's there - format should be: filename;N
1057       
1058        Return filename
1059End
1060
1061//given the full path and filename (fullPath), strips the data path
1062//(Mac-style, separated by colons) and returns this path
1063//this partial path is the same string that would be returned from PathInfo, for example
1064//
1065// - allows the user to save to a different path than catPathName
1066//
1067// called by WriteQIS.ipf
1068//
1069Function/S GetPathStrFromfullName(fullPath)
1070        String fullPath
1071       
1072        Variable offset1,offset2
1073        //String filename
1074        String PartialPath
1075        offset1 = 0
1076        do
1077                offset2 = StrSearch(fullPath, ":", offset1)
1078                if (offset2 == -1)                              // no more colons ?
1079                        //fileName = FullPath[offset1,strlen(FullPath) ]
1080                        PartialPath = FullPath[0, offset1-1]
1081                        break
1082                endif
1083                offset1 = offset2+1
1084        while (1)
1085       
1086        Return PartialPath
1087End
1088
1089//given the VAX filename, pull off the first 8 characters to make a valid
1090//file string that can be used for naming averaged 1-d files
1091//
1092// called by ProtocolAsPanel.ipf and Tile_2D.ipf
1093//
1094Function/S GetNameFromHeader(fullName)
1095        String fullName
1096        String temp, newName = ""
1097        Variable spc,ii=0
1098       
1099        //filename is 20 characters NNNNNxxx.SAn_NNN_NNN
1100        //want the first 8 characters, NNNNNxxx, then strip off any spaces at the beginning
1101        //NNNNN was entered as less than 5 characters
1102        //returns a null string if no name can be found
1103        do
1104                temp = fullname[ii,7]           //characters ii,7 of the name
1105                spc = strsearch(temp," ",0)
1106                if (spc == -1)
1107                        break           //no more spaces found
1108                endif
1109                ii+=1
1110        While(ii<8)
1111       
1112        If(strlen(temp) < 1)
1113                newName = ""            //be sure to return a null string if problem found
1114        else
1115                newName = temp
1116        Endif
1117       
1118        Return(newName)
1119End
1120
1121//list (input) is a list, typically returned from IndexedFile()
1122//which is semicolon-delimited, and may contain filenames from the VAX
1123//that contain version numbers, where the version number appears as a separate list item
1124//(and also as a non-existent file)
1125//these numbers must be purged from the list, especially for display in a popup
1126//or list processing of filenames
1127//the function returns the list, cleaned of version numbers (up to 11)
1128//raw data files will typically never have a version number other than 1.
1129//
1130// if there are no version numbers in the list, the input list is returned
1131//
1132// called by CatVSTable.ipf, NSORT.ipf, Transmission.ipf, WorkFileUtils.ipf
1133//
1134Function/S RemoveVersNumsFromList(list)
1135        String list
1136       
1137        //get rid of version numbers first (up to 11)
1138        Variable ii,num
1139        String item
1140        num = ItemsInList(list,";")
1141        ii=1
1142        do
1143                item = num2str(ii)
1144                list = RemoveFromList(item, list ,";" )
1145                ii+=1
1146        while(ii<12)
1147       
1148        return (list)
1149End
1150
1151//input is a list of run numbers, and output is a list of filenames (not the full path)
1152//*** input list must be COMMA delimited***
1153//output is equivalent to selecting from the CAT table
1154//if some or all of the list items are valid filenames, keep them...
1155//if an error is encountered, notify of the offending element and return a null list
1156//
1157//output is COMMA delimited
1158//
1159// this routine is expecting that the "ask", "none" special cases are handled elsewhere
1160//and not passed here
1161//
1162// called by Marquee.ipf, MultipleReduce.ipf, ProtocolAsPanel.ipf
1163//
1164Function/S ParseRunNumberList(list)
1165        String list
1166       
1167        String newList="",item="",tempStr=""
1168        Variable num,ii,runNum
1169       
1170        //expand number ranges, if any
1171        list = ExpandNumRanges(list)
1172       
1173        num=itemsinlist(list,",")
1174       
1175        for(ii=0;ii<num;ii+=1)
1176                //get the item
1177                item = StringFromList(ii,list,",")
1178                //is it already a valid filename?
1179                tempStr=FindValidFilename(item) //returns filename if good, null if error
1180                if(strlen(tempstr)!=0)
1181                        //valid name, add to list
1182                        //Print "it's a file"
1183                        newList += tempStr + ","
1184                else
1185                        //not a valid name
1186                        //is it a number?
1187                        runNum=str2num(item)
1188                        //print runnum
1189                        if(numtype(runNum) != 0)
1190                                //not a number -  maybe an error                       
1191                                DoAlert 0,"List item "+item+" is not a valid run number or filename. Please enter a valid number or filename."
1192                                return("")
1193                        else
1194                                //a run number or an error
1195                                tempStr = GetFileNameFromPathNoSemi( FindFileFromRunNumber(runNum) )
1196                                if(strlen(tempstr)==0)
1197                                        //file not found, error
1198                                        DoAlert 0,"List item "+item+" is not a valid run number. Please enter a valid number."
1199                                        return("")
1200                                else
1201                                        newList += tempStr + ","
1202                                endif
1203                        endif
1204                endif
1205        endfor          //loop over all items in list
1206       
1207        return(newList)
1208End
1209
1210//takes a comma delimited list that MAY contain number range, and
1211//expands any range of run numbers into a comma-delimited list...
1212//and returns the new list - if not a range, return unchanged
1213//
1214// local function
1215//
1216Function/S ExpandNumRanges(list)
1217        String list
1218       
1219        String newList="",dash="-",item,str
1220        Variable num,ii,hasDash
1221       
1222        num=itemsinlist(list,",")
1223//      print num
1224        for(ii=0;ii<num;ii+=1)
1225                //get the item
1226                item = StringFromList(ii,list,",")
1227                //does it contain a dash?
1228                hasDash = strsearch(item,dash,0)                //-1 if no dash found
1229                if(hasDash == -1)
1230                        //not a range, keep it in the list
1231                        newList += item + ","
1232                else
1233                        //has a dash (so it's a range), expand (or add null)
1234                        newList += ListFromDash(item)           
1235                endif
1236        endfor
1237       
1238        return newList
1239End
1240
1241//be sure to add a trailing comma to the return string...
1242//
1243// local function
1244//
1245Function/S ListFromDash(item)
1246        String item
1247       
1248        String numList="",loStr="",hiStr=""
1249        Variable lo,hi,ii
1250       
1251        loStr=StringFromList(0,item,"-")        //treat the range as a list
1252        hiStr=StringFromList(1,item,"-")
1253        lo=str2num(loStr)
1254        hi=str2num(hiStr)
1255        if( (numtype(lo) != 0) || (numtype(hi) !=0 ) || (lo > hi) )
1256                numList=""
1257                return numList
1258        endif
1259        for(ii=lo;ii<=hi;ii+=1)
1260                numList += num2str(ii) + ","
1261        endfor
1262       
1263        Return numList
1264End
1265
1266
1267////////Transmission
1268//******************
1269//lookup tables for attenuator transmissions
1270//NG3 and NG7 attenuators are physically different, so the transmissions are slightly different
1271//NG1 - (8m SANS) is not supported
1272//
1273// new calibration done June 2007, John Barker
1274//
1275Proc MakeNG3AttenTable()
1276
1277        NewDataFolder/O root:myGlobals:Attenuators
1278        //do explicitly to avoid data folder problems, redundant, but it must work without fail
1279        Variable num=10         //10 needed for tables after June 2007
1280
1281        Make/O/N=(num) root:myGlobals:Attenuators:ng3att0
1282        Make/O/N=(num) root:myGlobals:Attenuators:ng3att1
1283        Make/O/N=(num) root:myGlobals:Attenuators:ng3att2
1284        Make/O/N=(num) root:myGlobals:Attenuators:ng3att3
1285        Make/O/N=(num) root:myGlobals:Attenuators:ng3att4
1286        Make/O/N=(num) root:myGlobals:Attenuators:ng3att5
1287        Make/O/N=(num) root:myGlobals:Attenuators:ng3att6
1288        Make/O/N=(num) root:myGlobals:Attenuators:ng3att7
1289        Make/O/N=(num) root:myGlobals:Attenuators:ng3att8
1290        Make/O/N=(num) root:myGlobals:Attenuators:ng3att9
1291        Make/O/N=(num) root:myGlobals:Attenuators:ng3att10
1292       
1293        // and a wave for the errors at each attenuation factor
1294        Make/O/N=(num) root:myGlobals:Attenuators:ng3att0_err
1295        Make/O/N=(num) root:myGlobals:Attenuators:ng3att1_err
1296        Make/O/N=(num) root:myGlobals:Attenuators:ng3att2_err
1297        Make/O/N=(num) root:myGlobals:Attenuators:ng3att3_err
1298        Make/O/N=(num) root:myGlobals:Attenuators:ng3att4_err
1299        Make/O/N=(num) root:myGlobals:Attenuators:ng3att5_err
1300        Make/O/N=(num) root:myGlobals:Attenuators:ng3att6_err
1301        Make/O/N=(num) root:myGlobals:Attenuators:ng3att7_err
1302        Make/O/N=(num) root:myGlobals:Attenuators:ng3att8_err
1303        Make/O/N=(num) root:myGlobals:Attenuators:ng3att9_err
1304        Make/O/N=(num) root:myGlobals:Attenuators:ng3att10_err
1305       
1306       
1307        //each wave has 10 elements, the transmission of att# at the wavelengths
1308        //lambda = 4,5,6,7,8,10,12,14,17,20 (4 A and 20 A are extrapolated values)
1309        Make/O/N=(num) root:myGlobals:Attenuators:ng3lambda={4,5,6,7,8,10,12,14,17,20}
1310       
1311        // new calibration done June 2007, John Barker
1312        root:myGlobals:Attenuators:ng3att0 = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
1313        root:myGlobals:Attenuators:ng3att1 = {0.444784,0.419,0.3935,0.3682,0.3492,0.3132,0.2936,0.2767,0.2477,0.22404}
1314        root:myGlobals:Attenuators:ng3att2 = {0.207506,0.1848,0.1629,0.1447,0.1292,0.1056,0.09263,0.08171,0.06656,0.0546552}
1315        root:myGlobals:Attenuators:ng3att3 = {0.092412,0.07746,0.06422,0.05379,0.04512,0.03321,0.02707,0.02237,0.01643,0.0121969}
1316        root:myGlobals:Attenuators:ng3att4 = {0.0417722,0.03302,0.02567,0.02036,0.01604,0.01067,0.00812,0.006316,0.00419,0.00282411}
1317        root:myGlobals:Attenuators:ng3att5 = {0.0187129,0.01397,0.01017,0.007591,0.005668,0.003377,0.002423,0.001771,0.001064,0.000651257}
1318        root:myGlobals:Attenuators:ng3att6 = {0.00851048,0.005984,0.004104,0.002888,0.002029,0.001098,0.0007419,0.0005141,0.000272833,0.000150624}
1319        root:myGlobals:Attenuators:ng3att7 = {0.00170757,0.001084,0.0006469,0.0004142,0.0002607,0.0001201,7.664e-05,4.06624e-05,1.77379e-05,7.30624e-06}
1320        root:myGlobals:Attenuators:ng3att8 = {0.000320057,0.0001918,0.0001025,6.085e-05,3.681e-05,1.835e-05,6.74002e-06,3.25288e-06,1.15321e-06,3.98173e-07}
1321        root:myGlobals:Attenuators:ng3att9 = {6.27682e-05,3.69e-05,1.908e-05,1.196e-05,8.738e-06,6.996e-06,6.2901e-07,2.60221e-07,7.49748e-08,2.08029e-08}
1322        root:myGlobals:Attenuators:ng3att10 = {1.40323e-05,8.51e-06,5.161e-06,4.4e-06,4.273e-06,1.88799e-07,5.87021e-08,2.08169e-08,4.8744e-09,1.08687e-09}
1323 
1324  // percent errors as measured, May 2007 values
1325  // zero error for zero attenuators, appropriate average values put in for unknown values
1326        root:myGlobals:Attenuators:ng3att0_err = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
1327        root:myGlobals:Attenuators:ng3att1_err = {0.15,0.142,0.154,0.183,0.221,0.328,0.136,0.13,0.163,0.15}
1328        root:myGlobals:Attenuators:ng3att2_err = {0.25,0.257,0.285,0.223,0.271,0.405,0.212,0.223,0.227,0.25}
1329        root:myGlobals:Attenuators:ng3att3_err = {0.3,0.295,0.329,0.263,0.323,0.495,0.307,0.28,0.277,0.3}
1330        root:myGlobals:Attenuators:ng3att4_err = {0.35,0.331,0.374,0.303,0.379,0.598,0.367,0.322,0.33,0.35}
1331        root:myGlobals:Attenuators:ng3att5_err = {0.4,0.365,0.418,0.355,0.454,0.745,0.411,0.367,0.485,0.4}
1332        root:myGlobals:Attenuators:ng3att6_err = {0.45,0.406,0.473,0.385,0.498,0.838,0.454,0.49,0.5,0.5}
1333        root:myGlobals:Attenuators:ng3att7_err = {0.6,0.554,0.692,0.425,0.562,0.991,0.715,0.8,0.8,0.8}
1334        root:myGlobals:Attenuators:ng3att8_err = {0.7,0.705,0.927,0.503,0.691,1.27,1,1,1,1}
1335        root:myGlobals:Attenuators:ng3att9_err = {1,0.862,1.172,0.799,1.104,1.891,1.5,1.5,1.5,1.5}
1336        root:myGlobals:Attenuators:ng3att10_err = {1.5,1.054,1.435,1.354,1.742,2,2,2,2,2}
1337 
1338 
1339  //old tables, pre-June 2007
1340//      Make/O/N=9 root:myGlobals:Attenuators:ng3lambda={5,6,7,8,10,12,14,17,20}
1341//      root:myGlobals:Attenuators:ng3att0 = {1, 1, 1, 1, 1, 1, 1, 1,1 }
1342//      root:myGlobals:Attenuators:ng3att1 = {0.421, 0.394, 0.371, 0.349, 0.316, 0.293, 0.274, 0.245,0.220}
1343//      root:myGlobals:Attenuators:ng3att2 = {0.187, 0.164, 0.145, 0.130, 0.106, 0.0916, 0.0808, 0.0651,0.0531}
1344//      root:myGlobals:Attenuators:ng3att3 = {0.0777, 0.0636, 0.0534, 0.0446, 0.0330, 0.0262, 0.0217, 0.0157 ,0.0116}
1345//      root:myGlobals:Attenuators:ng3att4 = {0.0328, 0.0252, 0.0195, 0.0156, 0.0104, 7.68e-3, 5.98e-3, 3.91e-3,0.00262}
1346//      root:myGlobals:Attenuators:ng3att5 = {0.0139, 9.94e-3, 7.34e-3, 5.44e-3, 3.29e-3, 2.25e-3, 1.66e-3, 9.95e-4, 6.12e-4}
1347//      root:myGlobals:Attenuators:ng3att6 = {5.95e-3, 3.97e-3, 2.77e-3, 1.95e-3, 1.06e-3, 6.81e-4, 4.71e-4, 2.59e-4 , 1.45e-4}
1348//      root:myGlobals:Attenuators:ng3att7 = {1.07e-3, 6.24e-4, 3.90e-4, 2.44e-4, 1.14e-4, 6.55e-5, 4.10e-5, 1.64e-5 , 7.26e-6}
1349//      root:myGlobals:Attenuators:ng3att8 = {1.90e-4, 9.84e-5, 5.60e-5, 3.25e-5, 1.55e-5, 6.60e-6, 3.42e-6, 1.04e-6 , 3.48e-7}
1350//      root:myGlobals:Attenuators:ng3att9 = {3.61e-5, 1.74e-5, 9.90e-6, 6.45e-6, 2.35e-6, 6.35e-7, 2.86e-7, 6.61e-8 , 1.73e-8}
1351//      root:myGlobals:Attenuators:ng3att10 = {7.60e-6, 3.99e-6, 2.96e-6, 2.03e-6, 3.34e-7, 6.11e-8, 2.39e-8, 4.19e-9 , 8.60e-10}
1352
1353End
1354
1355// new calibration done June 2007, John Barker
1356Proc MakeNG7AttenTable()
1357
1358        NewDataFolder/O root:myGlobals:Attenuators
1359       
1360        Variable num=10         //10 needed for tables after June 2007
1361       
1362        Make/O/N=(num) root:myGlobals:Attenuators:ng7att0
1363        Make/O/N=(num) root:myGlobals:Attenuators:ng7att1
1364        Make/O/N=(num) root:myGlobals:Attenuators:ng7att2
1365        Make/O/N=(num) root:myGlobals:Attenuators:ng7att3
1366        Make/O/N=(num) root:myGlobals:Attenuators:ng7att4
1367        Make/O/N=(num) root:myGlobals:Attenuators:ng7att5
1368        Make/O/N=(num) root:myGlobals:Attenuators:ng7att6
1369        Make/O/N=(num) root:myGlobals:Attenuators:ng7att7
1370        Make/O/N=(num) root:myGlobals:Attenuators:ng7att8
1371        Make/O/N=(num) root:myGlobals:Attenuators:ng7att9
1372        Make/O/N=(num) root:myGlobals:Attenuators:ng7att10
1373       
1374        // and a wave for the errors at each attenuation factor
1375        Make/O/N=(num) root:myGlobals:Attenuators:ng7att0_err
1376        Make/O/N=(num) root:myGlobals:Attenuators:ng7att1_err
1377        Make/O/N=(num) root:myGlobals:Attenuators:ng7att2_err
1378        Make/O/N=(num) root:myGlobals:Attenuators:ng7att3_err
1379        Make/O/N=(num) root:myGlobals:Attenuators:ng7att4_err
1380        Make/O/N=(num) root:myGlobals:Attenuators:ng7att5_err
1381        Make/O/N=(num) root:myGlobals:Attenuators:ng7att6_err
1382        Make/O/N=(num) root:myGlobals:Attenuators:ng7att7_err
1383        Make/O/N=(num) root:myGlobals:Attenuators:ng7att8_err
1384        Make/O/N=(num) root:myGlobals:Attenuators:ng7att9_err
1385        Make/O/N=(num) root:myGlobals:Attenuators:ng7att10_err 
1386       
1387        //NG7 wave has 10 elements, the transmission of att# at the wavelengths
1388        //lambda =4, 5,6,7,8,10,12,14,17,20
1389        // note that some of the higher attenuations and ALL of the 4 A and 20A data is interpolated
1390        // none of these values are expected to be used in reality since the flux would be too low in practice
1391        Make/O/N=(num) root:myGlobals:Attenuators:ng7lambda={4,5,6,7,8,10,12,14,17,20}
1392
1393// New calibration, June 2007, John Barker
1394        root:myGlobals:Attenuators:ng7att0 = {1, 1, 1, 1, 1, 1, 1, 1 ,1,1}     
1395        root:myGlobals:Attenuators:ng7att1 = {0.448656,0.4192,0.3925,0.3661,0.3458,0.3098,0.2922,0.2738,0.2544,0.251352}
1396        root:myGlobals:Attenuators:ng7att2 = {0.217193,0.1898,0.1682,0.148,0.1321,0.1076,0.0957,0.08485,0.07479,0.0735965}
1397        root:myGlobals:Attenuators:ng7att3 = {0.098019,0.07877,0.06611,0.05429,0.04548,0.03318,0.02798,0.0234,0.02004,0.0202492}
1398        root:myGlobals:Attenuators:ng7att4 = {0.0426904,0.03302,0.02617,0.02026,0.0158,0.01052,0.008327,0.006665,0.005745,0.00524807}
1399        root:myGlobals:Attenuators:ng7att5 = {0.0194353,0.01398,0.01037,0.0075496,0.005542,0.003339,0.002505,0.001936,0.001765,0.00165959}
1400        root:myGlobals:Attenuators:ng7att6 = {0.00971666,0.005979,0.004136,0.002848,0.001946,0.001079,0.0007717,0.000588,0.000487337,0.000447713}
1401        root:myGlobals:Attenuators:ng7att7 = {0.00207332,0.001054,0.0006462,0.0003957,0.0002368,0.0001111,7.642e-05,4.83076e-05,3.99401e-05,3.54814e-05}
1402        root:myGlobals:Attenuators:ng7att8 = {0.000397173,0.0001911,0.0001044,5.844e-05,3.236e-05,1.471e-05,6.88523e-06,4.06541e-06,3.27333e-06,2.81838e-06}
1403        root:myGlobals:Attenuators:ng7att9 = {9.43625e-05,3.557e-05,1.833e-05,1.014e-05,6.153e-06,1.64816e-06,6.42353e-07,3.42132e-07,2.68269e-07,2.2182e-07}
1404        root:myGlobals:Attenuators:ng7att10 = {2.1607e-05,7.521e-06,2.91221e-06,1.45252e-06,7.93451e-07,1.92309e-07,5.99279e-08,2.87928e-08,2.19862e-08,1.7559e-08}
1405
1406  // percent errors as measured, May 2007 values
1407  // zero error for zero attenuators, appropriate average values put in for unknown values
1408        root:myGlobals:Attenuators:ng7att0_err = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
1409        root:myGlobals:Attenuators:ng7att1_err = {0.2,0.169,0.1932,0.253,0.298,0.4871,0.238,0.245,0.332,0.3}
1410        root:myGlobals:Attenuators:ng7att2_err = {0.3,0.305,0.3551,0.306,0.37,0.6113,0.368,0.413,0.45,0.4}
1411        root:myGlobals:Attenuators:ng7att3_err = {0.4,0.355,0.4158,0.36,0.4461,0.7643,0.532,0.514,0.535,0.5}
1412        root:myGlobals:Attenuators:ng7att4_err = {0.45,0.402,0.4767,0.415,0.5292,0.9304,0.635,0.588,0.623,0.6}
1413        root:myGlobals:Attenuators:ng7att5_err = {0.5,0.447,0.5376,0.487,0.6391,1.169,0.708,0.665,0.851,0.8}
1414        root:myGlobals:Attenuators:ng7att6_err = {0.6,0.501,0.6136,0.528,0.8796,1.708,0.782,0.874,1,1}
1415        root:myGlobals:Attenuators:ng7att7_err = {0.8,0.697,0.9149,0.583,1.173,2.427,1.242,2,2,2}
1416        root:myGlobals:Attenuators:ng7att8_err = {1,0.898,1.24,0.696,1.577,3.412,3,3,3,3}
1417        root:myGlobals:Attenuators:ng7att9_err = {1.5,1.113,1.599,1.154,2.324,4.721,5,5,5,5}
1418        root:myGlobals:Attenuators:ng7att10_err = {1.5,1.493,5,5,5,5,5,5,5,5} 
1419 
1420
1421// Pre-June 2007 calibration values - do not use these anymore 
1422////    root:myGlobals:Attenuators:ng7att0 = {1, 1, 1, 1, 1, 1, 1, 1 ,1}
1423////    root:myGlobals:Attenuators:ng7att1 = {0.418, 0.393, 0.369, 0.347, 0.313, 0.291, 0.271, 0.244, 0.219 }
1424////    root:myGlobals:Attenuators:ng7att2 = {0.189, 0.167, 0.148, 0.132, 0.109, 0.0945, 0.0830, 0.0681, 0.0560}
1425////    root:myGlobals:Attenuators:ng7att3 = {0.0784, 0.0651, 0.0541, 0.0456, 0.0340, 0.0273, 0.0223, 0.0164 , 0.0121}
1426////    root:myGlobals:Attenuators:ng7att4 = {0.0328, 0.0256, 0.0200, 0.0159, 0.0107, 7.98e-3, 6.14e-3, 4.09e-3 , 0.00274}
1427////    root:myGlobals:Attenuators:ng7att5 = {0.0139, 0.0101, 7.43e-3, 5.58e-3, 3.42e-3, 2.36e-3, 1.70e-3, 1.03e-3 , 6.27e-4}
1428////    root:myGlobals:Attenuators:ng7att6 = {5.90e-3, 4.07e-3, 2.79e-3, 1.99e-3, 1.11e-3, 7.13e-4, 4.91e-4, 2.59e-4 , 1.42e-4}
1429////    root:myGlobals:Attenuators:ng7att7 = {1.04e-3, 6.37e-4, 3.85e-4, 2.46e-4, 1.16e-4, 6.86e-5, 4.10e-5, 1.64e-5 ,7.02e-6}
1430////    root:myGlobals:Attenuators:ng7att8 = {1.90e-4, 1.03e-4, 5.71e-5, 3.44e-5, 1.65e-5, 6.60e-6, 3.42e-6, 1.04e-6 , 3.48e-7}
1431////    root:myGlobals:Attenuators:ng7att9 = {3.58e-5, 1.87e-5, 1.05e-5, 7.00e-6, 2.35e-6, 6.35e-7, 2.86e-7, 6.61e-8 , 1.73e-8}
1432////    root:myGlobals:Attenuators:ng7att10 = {7.76e-6, 4.56e-6, 3.25e-6, 2.03e-6, 3.34e-7, 6.11e-8, 2.39e-8, 4.19e-9, 8.60e-10}
1433End
1434
1435//returns the transmission of the attenuator (at NG3) given the attenuator number
1436//which must be an integer(to select the wave) and given the wavelength.
1437//the wavelength may be any value between 4 and 20 (A), and is interpolated
1438//between calibrated wavelengths for a given attenuator
1439//
1440// Mar 2010 - abs() added to attStr to account for ICE reporting -0.0001 as an attenuator position, which truncates to "-0"
1441Function LookupAttenNG3(lambda,attenNo,atten_err)
1442        Variable lambda, attenNo, &atten_err
1443       
1444        Variable trans
1445        String attStr="root:myGlobals:Attenuators:ng3att"+num2str(trunc(abs(attenNo)))
1446        String attErrWStr="root:myGlobals:Attenuators:ng3att"+num2str(trunc(abs(attenNo)))+"_err"
1447        String lamStr = "root:myGlobals:Attenuators:ng3lambda"
1448       
1449        if(attenNo == 0)
1450                return (1)              //no attenuation, return trans == 1
1451        endif
1452       
1453        if( (lambda < 4) || (lambda > 20 ) )
1454                Abort "Wavelength out of calibration range (4,20). You must manually enter the absolute parameters"
1455        Endif
1456       
1457        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) || !(WaveExists($attErrWStr)))
1458                Execute "MakeNG3AttenTable()"
1459        Endif
1460        //just in case creating the tables fails....
1461        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) )
1462                Abort "Attenuator lookup waves could not be found. You must manually enter the absolute parameters"
1463        Endif
1464       
1465        //lookup the value by interpolating the wavelength
1466        //the attenuator must always be an integer
1467        Wave att = $attStr
1468        Wave attErrW = $attErrWStr
1469        Wave lam = $lamstr
1470        trans = interp(lambda,lam,att)
1471        atten_err = interp(lambda,lam,attErrW)
1472
1473// the error in the tables is % error. return the standard deviation instead
1474        atten_err = trans*atten_err/100
1475               
1476//      Print "trans = ",trans
1477//      Print "trans err = ",atten_err
1478       
1479        return trans
1480End
1481
1482//returns the transmission of the attenuator (at NG7) given the attenuator number
1483//which must be an integer(to select the wave) and given the wavelength.
1484//the wavelength may be any value between 4 and 20 (A), and is interpolated
1485//between calibrated wavelengths for a given attenuator
1486//
1487// this set of tables is also used for NG5 (NG1) SANS instrument - as the attenuator has never been calibrated
1488//
1489// local function
1490//
1491// Mar 2010 - abs() added to attStr to account for ICE reporting -0.0001 as an attenuator position, which truncates to "-0"
1492Function LookupAttenNG7(lambda,attenNo,atten_err)
1493        Variable lambda, attenNo, &atten_err
1494       
1495        Variable trans
1496        String attStr="root:myGlobals:Attenuators:ng7att"+num2str(trunc(abs(attenNo)))
1497        String attErrWStr="root:myGlobals:Attenuators:ng7att"+num2str(trunc(abs(attenNo)))+"_err"
1498        String lamStr = "root:myGlobals:Attenuators:ng7lambda"
1499       
1500        if(attenNo == 0)
1501                return (1)              //no attenuation, return trans == 1
1502        endif
1503       
1504        if( (lambda < 4) || (lambda > 20 ) )
1505                Abort "Wavelength out of calibration range (4,20). You must manually enter the absolute parameters"
1506        Endif
1507       
1508        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) || !(WaveExists($attErrWStr)))
1509                Execute "MakeNG7AttenTable()"
1510        Endif
1511        //just in case creating the tables fails....
1512        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) )
1513                Abort "Attenuator lookup waves could not be found. You must manually enter the absolute parameters"
1514        Endif
1515       
1516        //lookup the value by interpolating the wavelength
1517        //the attenuator must always be an integer
1518        Wave att = $attStr
1519        Wave attErrW = $attErrWStr
1520        Wave lam = $lamstr
1521        trans = interp(lambda,lam,att)
1522        atten_err = interp(lambda,lam,attErrW)
1523
1524// the error in the tables is % error. return the standard deviation instead
1525        atten_err = trans*atten_err/100
1526       
1527//      Print "trans = ",trans
1528//      Print "trans err = ",atten_err
1529       
1530        return trans
1531
1532End
1533
1534// a utility function so that I can get the values from the command line
1535// since the atten_err is PBR
1536//
1537Function PrintAttenuation(instr,lam,attenNo)
1538        String instr
1539        Variable lam,attenNo
1540       
1541        Variable atten_err, attenFactor
1542       
1543        strswitch(instr)
1544                case "NG3":
1545                        attenFactor = LookupAttenNG3(lam,attenNo,atten_err)
1546                        break
1547                case "NG5":
1548                        //using NG7 lookup Table
1549                        attenFactor = LookupAttenNG7(lam,attenNo,atten_err)
1550                        break
1551                case "NG7":
1552                        attenFactor = LookupAttenNG7(lam,attenNo,atten_err)
1553                        break
1554                default:                                                       
1555                        //return error?
1556                        attenFactor=1
1557        endswitch
1558
1559        Print "atten, err = ", attenFactor, atten_err
1560       
1561        return(0)
1562End
1563
1564
1565
1566//returns the proper attenuation factor based on the instrument (NG3, NG5, or NG7)
1567//NG5 values are taken from the NG7 tables (there is very little difference in the
1568//values, and NG5 attenuators have not been calibrated (as of 8/01)
1569//
1570// filestr is passed from TextRead[3] = the default directory
1571// lam is passed from RealsRead[26]
1572// AttenNo is passed from ReaslRead[3]
1573//
1574// Attenuation factor as defined here is <= 1
1575//
1576// ORNL can pass ("",1,attenuationFactor) and have this function simply
1577// spit back the attenuationFactor (that was read into rw[3])
1578//
1579// called by Correct.ipf, ProtocolAsPanel.ipf, Transmission.ipf
1580//
1581//
1582// as of March 2011, returns the error (one standard deviation) in the attenuation factor as the last parameter, by reference
1583Function AttenuationFactor(fileStr,lam,attenNo,atten_err)
1584        String fileStr
1585        Variable lam,attenNo, &atten_err
1586       
1587        Variable attenFactor=1,loc
1588        String instr=fileStr[1,3]       //filestr is "[NGnSANSn] " or "[NGnSANSnn]" (11 characters total)
1589       
1590        strswitch(instr)
1591                case "NG3":
1592                        attenFactor = LookupAttenNG3(lam,attenNo,atten_err)
1593                        break
1594                case "NG5":
1595                        //using NG7 lookup Table
1596                        attenFactor = LookupAttenNG7(lam,attenNo,atten_err)
1597                        break
1598                case "NG7":
1599                        attenFactor = LookupAttenNG7(lam,attenNo,atten_err)
1600                        break
1601                default:                                                       
1602                        //return error?
1603                        attenFactor=1
1604        endswitch
1605//      print "instr, lambda, attenNo,attenFactor = ",instr,lam,attenNo,attenFactor
1606        return(attenFactor)
1607End
1608
1609//function called by the popups to get a file list of data that can be sorted
1610// this procedure simply removes the raw data files from the string - there
1611//can be lots of other junk present, but this is very fast...
1612//
1613// could also use the alternate procedure of keeping only file with the proper extension
1614//
1615// another possibility is to get a listing of the text files, but is unreliable on
1616// Windows, where the data file must be .txt (and possibly OSX)
1617//
1618// called by FIT_Ops.ipf, NSORT.ipf, PlotUtils.ipf
1619//
1620Function/S ReducedDataFileList(ctrlName)
1621        String ctrlName
1622
1623        String list="",newList="",item=""
1624        Variable num,ii
1625       
1626        //check for the path
1627        PathInfo catPathName
1628        if(V_Flag==0)
1629                DoAlert 0, "Data path does not exist - pick the data path from the button on the main panel"
1630                Return("")
1631        Endif
1632       
1633        list = IndexedFile(catpathName,-1,"????")
1634       
1635        list = RemoveFromList(ListMatch(list,"*.SA1*",";"), list, ";", 0)
1636        list = RemoveFromList(ListMatch(list,"*.SA2*",";"), list, ";", 0)
1637        list = RemoveFromList(ListMatch(list,"*.SA3*",";"), list, ";", 0)
1638        list = RemoveFromList(ListMatch(list,".*",";"), list, ";", 0)
1639        list = RemoveFromList(ListMatch(list,"*.pxp",";"), list, ";", 0)
1640        list = RemoveFromList(ListMatch(list,"*.DIV",";"), list, ";", 0)
1641        list = RemoveFromList(ListMatch(list,"*.GSP",";"), list, ";", 0)
1642        list = RemoveFromList(ListMatch(list,"*.MASK",";"), list, ";", 0)
1643
1644        //remove VAX version numbers
1645        list = RemoveVersNumsFromList(List)
1646        //sort
1647        newList = SortList(List,";",0)
1648
1649        return newlist
1650End
1651
1652// returns a list of raw data files in the catPathName directory on disk
1653// - list is SEMICOLON-delimited
1654//
1655// does it the "cheap" way, simply finding the ".SAn" in the file name
1656// = does not check for proper byte length.
1657//
1658// called by PatchFiles.ipf, Tile_2D.ipf
1659//
1660Function/S GetRawDataFileList()
1661       
1662        //make sure that path exists
1663        PathInfo catPathName
1664        if (V_flag == 0)
1665                Abort "Folder path does not exist - use Pick Path button on Main Panel"
1666        Endif
1667       
1668        String list=IndexedFile(catPathName,-1,"????")
1669        String newList="",item=""
1670        Variable num=ItemsInList(list,";"),ii
1671        for(ii=0;ii<num;ii+=1)
1672                item = StringFromList(ii, list  ,";")
1673                if( stringmatch(item,"*.sa1*") )
1674                        newlist += item + ";"
1675                endif
1676                if( stringmatch(item,"*.sa2*") )
1677                        newlist += item + ";"
1678                endif
1679                if( stringmatch(item,"*.sa3*") )
1680                        newlist += item + ";"
1681                endif
1682                //print "ii=",ii
1683        endfor
1684        newList = SortList(newList,";",0)
1685        return(newList)
1686End
1687
1688// Return the filename that represents the previous or next file.
1689// Input is current filename and increment.
1690// Increment should be -1 or 1
1691// -1 => previous file
1692// 1 => next file
1693Function/S GetPrevNextRawFile(curfilename, prevnext)
1694        String curfilename
1695        Variable prevnext
1696
1697        String filename
1698       
1699        //get the run number
1700        Variable num = GetRunNumFromFile(curfilename)
1701               
1702        //find the next specified file by number
1703        fileName = FindFileFromRunNumber(num+prevnext)
1704
1705        if(cmpstr(fileName,"")==0)
1706                //null return, do nothing
1707                fileName = FindFileFromRunNumber(num)
1708        Endif
1709
1710//      print "in FU "+filename
1711
1712        Return filename
1713End
1714
1715
Note: See TracBrowser for help on using the repository browser.