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

Last change on this file since 779 was 750, checked in by srkline, 12 years ago

Some utility procedures were moved from NSORT to NCNR_Utilities. These changes were previously propogated to ANSTO_Utils, and now are propagated to HFIR and ILL_Util files.

File size: 45.9 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 = 3.4e-6
40        Variable/G root:myGlobals:DeadtimeNG5_ORNL = 0.6e-6             //as of 9 MAY 2002
41        Variable/G root:myGlobals:DeadtimeNG7_ORNL = 3.4e-6
42        Variable/G root:myGlobals:DeadtimeDefault = 3.4e-6
43       
44        //new 11APR07
45        Variable/G root:myGlobals:BeamstopXTol = -8                     // (cm) is BS Xpos is -5 cm or less, it's a trans measurement
46        // sample aperture offset is NOT stored in the VAX header, but it should be
47        // - when it is, remove the global and write an accessor AND make a place for
48        // it in the RealsRead
49        Variable/G root:myGlobals:apOff = 5.0                           // (cm) distance from sample aperture to sample position
50
51End
52
53
54//**********************
55// Resolution calculation - used by the averaging routines
56// to calculate the resolution function at each q-value
57// - the return value is not used
58//
59// equivalent to John's routine on the VAX Q_SIGMA_AVE.FOR
60// Incorporates eqn. 3-15 from J. Appl. Cryst. (1995) v. 28 p105-114
61//
62// - 21 MAR 07 uses projected BS diameter on the detector
63// - APR 07 still need to add resolution with lenses. currently there is no flag in the
64//          raw data header to indicate the presence of lenses.
65//
66// - Aug 07 - added input to switch calculation based on lenses (==1 if in)
67//
68// - called by CircSectAvg.ipf and RectAnnulAvg.ipf
69//
70// passed values are read from RealsRead
71// except DDet and apOff, which are set from globals before passing
72//
73//
74Function/S getResolution(inQ,lambda,lambdaWidth,DDet,apOff,S1,S2,L1,L2,BS,del_r,usingLenses,SigmaQ,QBar,fSubS)
75        Variable inQ, lambda, lambdaWidth, DDet, apOff, S1, S2, L1, L2, BS, del_r,usingLenses
76        Variable &fSubS, &QBar, &SigmaQ         //these are the output quantities at the input Q value
77       
78        //lots of calculation variables
79        Variable a2, q_small, lp, v_lambda, v_b, v_d, vz, yg, v_g
80        Variable r0, delta, inc_gamma, fr, fv, rmd, v_r1, rm, v_r
81
82        //Constants
83        Variable vz_1 = 3.956e5         //velocity [cm/s] of 1 A neutron
84        Variable g = 981.0                              //gravity acceleration [cm/s^2]
85
86        String results
87        results ="Failure"
88
89        S1 *= 0.5*0.1                   //convert to radius and [cm]
90        S2 *= 0.5*0.1
91
92        L1 *= 100.0                     // [cm]
93        L1 -= apOff                             //correct the distance
94
95        L2 *= 100.0
96        L2 += apOff
97        del_r *= 0.1                            //width of annulus, convert mm to [cm]
98       
99        BS *= 0.5*0.1                   //nominal BS diameter passed in, convert to radius and [cm]
100        // 21 MAR 07 SRK - use the projected BS diameter, based on a point sample aperture
101        Variable LB
102        LB = 20.1 + 1.61*BS                     //distance in cm from beamstop to anode plane (empirical)
103        BS = bs + bs*lb/(l2-lb)         //adjusted diameter of shadow from parallax
104       
105        //Start resolution calculation
106        a2 = S1*L2/L1 + S2*(L1+L2)/L1
107        q_small = 2.0*Pi*(BS-a2)*(1.0-lambdaWidth)/(lambda*L2)
108        lp = 1.0/( 1.0/L1 + 1.0/L2)
109
110        v_lambda = lambdaWidth^2/6.0
111       
112//      if(usingLenses==1)                      //SRK 2007
113        if(usingLenses != 0)                    //SRK 2008 allows for the possibility of different numbers of lenses in header
114                v_b = 0.25*(S1*L2/L1)^2 +0.25*(2/3)*(lambdaWidth/lambda)^2*(S2*L2/lp)^2         //correction to 2nd term
115        else
116                v_b = 0.25*(S1*L2/L1)^2 +0.25*(S2*L2/lp)^2              //original form
117        endif
118       
119        v_d = (DDet/2.3548)^2 + del_r^2/12.0
120        vz = vz_1 / lambda
121        yg = 0.5*g*L2*(L1+L2)/vz^2
122        v_g = 2.0*(2.0*yg^2*v_lambda)                                   //factor of 2 correction, B. Hammouda, 2007
123
124        r0 = L2*tan(2.0*asin(lambda*inQ/(4.0*Pi) ))
125        delta = 0.5*(BS - r0)^2/v_d
126
127        if (r0 < BS)
128                inc_gamma=exp(gammln(1.5))*(1-gammp(1.5,delta))
129        else
130                inc_gamma=exp(gammln(1.5))*(1+gammp(1.5,delta))
131        endif
132
133        fSubS = 0.5*(1.0+erf( (r0-BS)/sqrt(2.0*v_d) ) )
134        if (fSubS <= 0.0)
135                fSubS = 1.e-10
136        endif
137        fr = 1.0 + sqrt(v_d)*exp(-1.0*delta) /(r0*fSubS*sqrt(2.0*Pi))
138        fv = inc_gamma/(fSubS*sqrt(Pi)) - r0^2*(fr-1.0)^2/v_d
139
140        rmd = fr*r0
141        v_r1 = v_b + fv*v_d +v_g
142
143        rm = rmd + 0.5*v_r1/rmd
144        v_r = v_r1 - 0.5*(v_r1/rmd)^2
145        if (v_r < 0.0)
146                v_r = 0.0
147        endif
148        QBar = (4.0*Pi/lambda)*sin(0.5*atan(rm/L2))
149        SigmaQ = QBar*sqrt(v_r/rmd^2 +v_lambda)
150
151        results = "success"
152        Return results
153End
154
155
156//
157//**********************
158// 2D resolution function calculation - ***NOT*** in terms of X and Y
159// but written in terms of Parallel and perpendicular to the Q vector at each point
160//
161// -- it must be written this way since the 2D function is an ellipse with its major
162// axis pointing in the direction of Q_parallel. Hence there is no way to properly define the
163// elliptical gaussian in terms of sigmaX and sigmaY
164//
165// based on notes from David Mildner, 2008
166//
167//
168// - 21 MAR 07 uses projected BS diameter on the detector
169// - APR 07 still need to add resolution with lenses. currently there is no flag in the
170//          raw data header to indicate the presence of lenses.
171//
172// - Aug 07 - added input to switch calculation based on lenses (==1 if in)
173//
174// passed values are read from RealsRead
175// except DDet and apOff, which are set from globals before passing
176//
177// phi is the azimuthal angle, CCW from +x axis
178// r_dist is the real-space distance from ctr of detector to QxQy pixel location
179//
180Function/S get2DResolution(inQ,phi,lambda,lambdaWidth,DDet,apOff,S1,S2,L1,L2,BS,del_r,usingLenses,r_dist,SigmaQX,SigmaQY,fSubS)
181        Variable inQ, phi,lambda, lambdaWidth, DDet, apOff, S1, S2, L1, L2, BS, del_r,usingLenses,r_dist
182        Variable &SigmaQX,&SigmaQY,&fSubS               //these are the output quantities at the input Q value
183       
184        //lots of calculation variables
185        Variable a2, lp, v_lambda, v_b, v_d, vz, yg, v_g
186        Variable r0, delta, inc_gamma, fr, fv, rmd, v_r1, rm, v_r
187
188        //Constants
189        Variable vz_1 = 3.956e5         //velocity [cm/s] of 1 A neutron
190        Variable g = 981.0                              //gravity acceleration [cm/s^2]
191        Variable m_h    = 252.8                 // m/h [=] s/cm^2
192
193        String results
194        results ="Failure"
195
196        S1 *= 0.5*0.1                   //convert to radius and [cm]
197        S2 *= 0.5*0.1
198
199        L1 *= 100.0                     // [cm]
200        L1 -= apOff                             //correct the distance
201
202        L2 *= 100.0
203        L2 += apOff
204        del_r *= 0.1                            //width of annulus, convert mm to [cm]
205       
206        BS *= 0.5*0.1                   //nominal BS diameter passed in, convert to radius and [cm]
207        // 21 MAR 07 SRK - use the projected BS diameter, based on a point sample aperture
208        Variable LB
209        LB = 20.1 + 1.61*BS                     //distance in cm from beamstop to anode plane (empirical)
210        BS = bs + bs*lb/(l2-lb)         //adjusted diameter of shadow from parallax
211       
212        //Start resolution calculation
213        a2 = S1*L2/L1 + S2*(L1+L2)/L1
214        lp = 1.0/( 1.0/L1 + 1.0/L2)
215
216        v_lambda = lambdaWidth^2/6.0
217       
218//      if(usingLenses==1)                      //SRK 2007
219        if(usingLenses != 0)                    //SRK 2008 allows for the possibility of different numbers of lenses in header
220                v_b = 0.25*(S1*L2/L1)^2 +0.25*(2/3)*(lambdaWidth/lambda)^2*(S2*L2/lp)^2         //correction to 2nd term
221        else
222                v_b = 0.25*(S1*L2/L1)^2 +0.25*(S2*L2/lp)^2              //original form
223        endif
224       
225        v_d = (DDet/2.3548)^2 + del_r^2/12.0
226        vz = vz_1 / lambda
227        yg = 0.5*g*L2*(L1+L2)/vz^2
228        v_g = 2.0*(2.0*yg^2*v_lambda)                                   //factor of 2 correction, B. Hammouda, 2007
229
230        r0 = L2*tan(2.0*asin(lambda*inQ/(4.0*Pi) ))
231        delta = 0.5*(BS - r0)^2/v_d
232
233        if (r0 < BS)
234                inc_gamma=exp(gammln(1.5))*(1-gammp(1.5,delta))
235        else
236                inc_gamma=exp(gammln(1.5))*(1+gammp(1.5,delta))
237        endif
238
239        fSubS = 0.5*(1.0+erf( (r0-BS)/sqrt(2.0*v_d) ) )
240        if (fSubS <= 0.0)
241                fSubS = 1.e-10
242        endif
243//      fr = 1.0 + sqrt(v_d)*exp(-1.0*delta) /(r0*fSubS*sqrt(2.0*Pi))
244//      fv = inc_gamma/(fSubS*sqrt(Pi)) - r0^2*(fr-1.0)^2/v_d
245//
246//      rmd = fr*r0
247//      v_r1 = v_b + fv*v_d +v_g
248//
249//      rm = rmd + 0.5*v_r1/rmd
250//      v_r = v_r1 - 0.5*(v_r1/rmd)^2
251//      if (v_r < 0.0)
252//              v_r = 0.0
253//      endif
254
255        Variable kap,a_val,a_val_L2
256       
257        kap = 2*pi/lambda
258        a_val = L2*(L1+L2)*g/2*(m_h)^2
259        a_val_L2 = a_val/L2*1e-16               //convert 1/cm^2 to 1/A^2
260
261
262///////// in terms of Q_parallel ("x") and Q_perp ("y") - this works, since parallel is in the direction of Q and I
263// can calculate that from the QxQy (I just need the projection)
264
265
266// for test case with no gravity, set a_val = 0
267// note that gravity has no wavelength dependence. the lambda^4 cancels out.
268//
269//      a_val = 0
270
271        // the detector pixel is square, so correct for phi
272        DDet = DDet*cos(phi) + DDet*sin(phi)
273       
274        // this is really sigma_Q_parallel
275        SigmaQX = kap*kap/12* (3*(S1/L1)^2 + 3*(S2/LP)^2 + (DDet/L2)^2 + (del_r/L2)^2 + (sin(phi))^2*8*(a_val_L2)^2*lambda^4*lambdaWidth^2)
276        SigmaQX += inQ*inQ*v_lambda
277
278        //this is really sigma_Q_perpendicular
279        SigmaQY = 3*(S1/L1)^2 + 3*(S2/LP)^2 + (DDet/L2)^2 + (del_r/L2)^2 + (cos(phi))^2*8*(a_val_L2)^2*lambda^4*lambdaWidth^2
280        SigmaQY *= kap*kap/12
281       
282        SigmaQX = sqrt(SigmaQX)
283        SigmaQy = sqrt(SigmaQY)
284       
285       
286        results = "success"
287        Return results
288End
289
290
291
292
293//Utility function that returns the detector resolution (in cm)
294//Global values are set in the Initialize procedure
295//
296//
297// - called by CircSectAvg.ipf, RectAnnulAvg.ipf, and ProtocolAsPanel.ipf
298//
299// fileStr is passed as TextRead[3]
300// detStr is passed as TextRead[9]
301//
302// *** as of Jan 2008, depricated. Now detector pixel sizes are read from the file header
303// rw[10] = x size (mm); rw[13] = y size (mm)
304//
305Function xDetectorPixelResolution(fileStr,detStr)
306        String fileStr,detStr
307       
308        Variable DDet
309        String instr=fileStr[1,3]       //filestr is "[NGnSANSn] " or "[NGnSANSnn]" (11 characters total)
310       
311        NVAR PixelResNG3_ILL = root:myGlobals:PixelResNG3_ILL           //pixel resolution in cm
312        NVAR PixelResNG5_ILL = root:myGlobals:PixelResNG5_ILL
313        NVAR PixelResNG7_ILL = root:myGlobals:PixelResNG7_ILL
314        NVAR PixelResNG3_ORNL = root:myGlobals:PixelResNG3_ORNL
315        NVAR PixelResNG5_ORNL = root:myGlobals:PixelResNG5_ORNL
316        NVAR PixelResNG7_ORNL = root:myGlobals:PixelResNG7_ORNL
317        NVAR PixelResDefault = root:myGlobals:PixelResDefault
318       
319        strswitch(instr)
320                case "NG3":
321                        if(cmpstr(detStr, "ILL   ") == 0 )
322                                DDet= PixelResNG3_ILL
323                        else
324                                DDet = PixelResNG3_ORNL //detector is ordella-type
325                        endif
326                        break
327                case "NG5":
328                        if(cmpstr(detStr, "ILL   ") == 0 )
329                                DDet= PixelResNG5_ILL
330                        else
331                                DDet = PixelResNG5_ORNL //detector is ordella-type
332                        endif
333                        break
334                case "NG7":
335                        if(cmpstr(detStr, "ILL   ") == 0 )
336                                DDet= PixelResNG7_ILL
337                        else
338                                DDet = PixelResNG7_ORNL //detector is ordella-type
339                        endif
340                        break
341                default:                                                       
342                        //return error?
343                        DDet = PixelResDefault  //0.5 cm, typical for new ORNL detectors
344        endswitch
345       
346        return(DDet)
347End
348
349//Utility function that returns the detector deadtime (in seconds)
350//Global values are set in the Initialize procedure
351//
352// - called by WorkFileUtils.ipf
353//
354// fileStr is passed as TextRead[3]
355// detStr is passed as TextRead[9]
356//
357Function DetectorDeadtime(fileStr,detStr)
358        String fileStr,detStr
359       
360        Variable deadtime
361        String instr=fileStr[1,3]       //filestr is "[NGnSANSn] " or "[NGnSANSnn]" (11 characters total)
362       
363        NVAR DeadtimeNG3_ILL = root:myGlobals:DeadtimeNG3_ILL           //pixel resolution in cm
364        NVAR DeadtimeNG5_ILL = root:myGlobals:DeadtimeNG5_ILL
365        NVAR DeadtimeNG7_ILL = root:myGlobals:DeadtimeNG7_ILL
366        NVAR DeadtimeNG3_ORNL = root:myGlobals:DeadtimeNG3_ORNL
367        NVAR DeadtimeNG5_ORNL = root:myGlobals:DeadtimeNG5_ORNL
368        NVAR DeadtimeNG7_ORNL = root:myGlobals:DeadtimeNG7_ORNL
369        NVAR DeadtimeDefault = root:myGlobals:DeadtimeDefault
370       
371        strswitch(instr)
372                case "NG3":
373                        if(cmpstr(detStr, "ILL   ") == 0 )
374                                deadtime= DeadtimeNG3_ILL
375                        else
376                                deadtime = DeadtimeNG3_ORNL     //detector is ordella-type
377                        endif
378                        break
379                case "NG5":
380                        if(cmpstr(detStr, "ILL   ") == 0 )
381                                deadtime= DeadtimeNG5_ILL
382                        else
383                                deadtime = DeadtimeNG5_ORNL     //detector is ordella-type
384                        endif
385                        break
386                case "NG7":
387                        if(cmpstr(detStr, "ILL   ") == 0 )
388                                deadtime= DeadtimeNG7_ILL
389                        else
390                                deadtime = DeadtimeNG7_ORNL     //detector is ordella-type
391                        endif
392                        break
393                default:                                                       
394                        //return error?
395                        deadtime = DeadtimeDefault      //1e-6 seconds, typical for new ORNL detectors
396        endswitch
397       
398        return(deadtime)
399End
400
401//make a three character string of the run number
402//Moved to facility utils
403Function/S RunDigitString(num)
404        Variable num
405       
406        String numStr=""
407        if(num<10)
408                numStr = "00"+num2str(num)
409        else
410                if(num<100)
411                        numStr = "0"+num2str(num)
412                else
413                        numStr = num2str(num)
414                Endif
415        Endif
416        //Print "numstr = ",numstr
417        return(numstr)
418End
419
420//given a filename of a SANS data filename of the form
421//TTTTTnnn.SAn_TTT_Txxx
422//returns the prefix "TTTTT" as some number of characters
423//returns "" as an invalid file prefix
424//
425// NCNR-specifc, does not really belong here - but it's a beta procedure used for the
426// Combine Files Panel
427//
428Function/S GetPrefixStrFromFile(item)
429        String item
430        String invalid = ""     //"" is not a valid run prefix, since it's text
431        Variable num=-1
432       
433        //find the "dot"
434        String runStr=""
435        Variable pos = strsearch(item,".",0)
436        if(pos == -1)
437                //"dot" not found
438                return (invalid)
439        else
440                //found, skip the three characters preceeding it
441                if (pos <=3)
442                        //not enough characters
443                        return (invalid)
444                else
445                        runStr = item[0,pos-4]
446                        return (runStr)
447                Endif
448        Endif
449End
450
451
452
453
454/////VAX filename/Run number parsing utilities
455//
456// a collection of uilities for processing vax filenames
457//and processing lists (especially for display in popup menus)
458//
459//required to correctly account for VAX supplied version numbers, which
460//may or may not be removed by the ftp utility
461//
462// - parses lists of run numbers into real filenames
463// - selects proper detector constants
464//
465//**************************
466//
467//given a filename of a SANS data filename of the form
468//TTTTTnnn.SAn_TTT_Txxx
469//returns the run number "nnn" as a number
470//returns -1 as an invalid file number
471//
472// called by several ipfs
473//
474//
475Function GetRunNumFromFile(item)
476        String item
477        Variable invalid = -1   //negative numbers are invalid
478        Variable num=-1
479       
480        //find the "dot"
481        String runStr=""
482        Variable pos = strsearch(item,".",0)
483        if(pos == -1)
484                //"dot" not found
485                return (invalid)
486        else
487                //found, get the three characters preceeding it
488                if (pos <=2)
489                        //not enough characters
490                        return (invalid)
491                else
492                        runStr = item[pos-3,pos-1]
493                        //convert to a number
494                        num = str2num(runStr)
495                        //if valid, return it
496                        if (num == NaN)
497                                //3 characters were not a number
498                                return (invalid)
499                        else
500                                //run was OK
501                                return (num)
502                        Endif
503                Endif
504        Endif
505End
506
507//given a filename of a SANS data filename of the form
508//TTTTTnnn.SAn_TTT_Txxx
509//returns the run number "nnn" as a STRING of THREE characters
510//returns "ABC" as an invalid file number
511//
512// local function to aid in locating files by run number
513//
514Function/S GetRunNumStrFromFile(item)
515        String item
516        String invalid = "ABC"  //"ABC" is not a valid run number, since it's text
517        Variable num=-1
518       
519        //find the "dot"
520        String runStr=""
521        Variable pos = strsearch(item,".",0)
522        if(pos == -1)
523                //"dot" not found
524                return (invalid)
525        else
526                //found, get the three characters preceeding it
527                if (pos <=2)
528                        //not enough characters
529                        return (invalid)
530                else
531                        runStr = item[pos-3,pos-1]
532                        return (runStr)
533                Endif
534        Endif
535End
536
537//returns a string containing the full path to the file containing the
538//run number "num". The null string is returned if no valid file can be found
539//the path "catPathName" used and is hard-wired, will abort if this path does not exist
540//the file returned will be a RAW SANS data file, other types of files are
541//filtered out.
542//
543// called by Buttons.ipf and Transmission.ipf, and locally by parsing routines
544//
545Function/S FindFileFromRunNumber(num)
546        Variable num
547       
548        String fullName="",partialName="",item=""
549        //get list of raw data files in folder that match "num" (add leading zeros)
550        if( (num>999) || (num<=0) )
551                //Print "error in  FindFileFromRunNumber(num), file number too large or too small"
552                Return ("")
553        Endif
554        //make a three character string of the run number
555        String numStr=""
556        if(num<10)
557                numStr = "00"+num2str(num)
558        else
559                if(num<100)
560                        numStr = "0"+num2str(num)
561                else
562                        numStr = num2str(num)
563                Endif
564        Endif
565        //Print "numstr = ",numstr
566       
567        //make sure that path exists
568        PathInfo catPathName
569        String path = S_path
570        if (V_flag == 0)
571                Abort "folder path does not exist - use Pick Path button"
572        Endif
573        String list="",newList="",testStr=""
574       
575        list = IndexedFile(catPathName,-1,"????")       //get all files in folder
576        //find (the) one with the number in the run # location in the name
577        Variable numItems,ii,runFound,isRAW
578        numItems = ItemsInList(list,";")                //get the new number of items in the list
579        ii=0
580        do
581                //parse through the list in this order:
582                // 1 - does item contain run number (as a string) "TTTTTnnn.SAn_XXX_Tyyy"
583                // 2 - exclude by isRaw? (to minimize disk access)
584                item = StringFromList(ii, list  ,";" )
585                if(strlen(item) != 0)
586                        //find the run number, if it exists as a three character string
587                        testStr = GetRunNumStrFromFile(item)
588                        runFound= cmpstr(numStr,testStr)        //compare the three character strings, 0 if equal
589                        if(runFound == 0)
590                                //the run Number was found
591                                //build valid filename
592                                partialName = FindValidFileName(item)
593                                if(strlen(partialName) != 0)            //non-null return from FindValidFileName()
594                                        fullName = path + partialName
595                                        //check if RAW, if so,this must be the file!
596                                        isRAW = CheckIfRawData(fullName)
597                                        if(isRaw)
598                                                //stop here
599                                                return(fullname)
600                                        Endif
601                                Endif
602                        Endif
603                Endif
604                ii+=1
605        while(ii<numItems)              //process all items in list
606        Return ("")     //null return if file not found in list
607End
608
609//function to test a binary file to see if it is a RAW binary SANS file
610//first checks the total bytes in the file (which for raw data is 33316 bytes)
611//**note that the "DIV" file will also show up as a raw file by the run field
612//should be listed in CAT/SHORT and in patch windows
613//
614//Function then checks the file fname (full path:file) for "RAW" run.type field
615//if not found, the data is not raw data and zero is returned
616//
617// called by many procedures (both external and local)
618//
619Function CheckIfRawData(fname)
620        String fname
621       
622        Variable refnum,totalBytes
623        String testStr=""
624       
625        Open/R/T="????TEXT" refNum as fname
626        if(strlen(s_filename) == 0)     //user cancel (/Z not used, so V_flag not set)
627                return(0)
628        endif
629       
630        //get the total number of bytes in the file
631        FStatus refNum
632        totalBytes = V_logEOF
633        //Print totalBytes
634        if(totalBytes < 100)
635                Close refNum
636                return(0)               //not a raw file
637        endif
638        FSetPos refNum,75
639        FReadLine/N=3 refNum,testStr
640        Close refNum
641       
642        if(totalBytes == 33316 && ( cmpstr(testStr,"RAW")==0 ||  cmpstr(testStr,"SIM")==0))
643                //true, is raw data file
644                Return(1)
645        else
646                //some other file
647                Return(0)
648        Endif
649End
650
651//function to check the header of a raw data file (full path specified by fname)
652//checks the field of the x-position of the beamstop during data collection
653//if the x-position is more negative (farther to the left) than xTol(input)
654//the the beamstop is "out" and the file is a transmission run and not a scattering run
655//xtol typically set at -5 (cm) - trans runs have bs(x) at -10 to -15 cm
656// function returns 1 if beamstop is out, 0 if beamstop is in
657//
658// tolerance is set as a global value "root:myGlobals:BeamstopXTol"
659//
660// called by Transmission.ipf, CatVSTable.ipf, NSORT.ipf
661//
662Function isTransFile(fName)
663        String fname
664       
665        Variable refnum,xpos
666        NVAR xTol = root:myGlobals:BeamstopXTol
667       
668        //pos = 369, read one real value
669       
670        SetDataFolder root:
671        String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
672        String strToExecute=""
673        // 1 R*4 value
674        strToExecute = GBLoadStr + "/S=368/U=1" + "\"" + fname + "\""
675        Execute strToExecute
676        Wave w=$"root:tempGBWave0"
677        xPos = w[0]
678        KillWaves/Z w
679        //Print "xPos = ",xpos
680       
681        if(xpos<=xTol)
682                //xpos is farther left (more negative) than xtol (currently -5 cm)
683                Return(1)
684        else
685                //some other file
686                Return(0)
687        Endif
688End
689
690
691//function to remove all spaces from names when searching for filenames
692//the filename (as saved) will never have interior spaces (TTTTTnnn_AB _Bnnn)
693//but the text field in the header WILL, if less than 3 characters were used for the
694//user's initials, and can have leading spaces if prefix was less than 5 characters
695//
696//returns a string identical to the original string, except with the interior spaces removed
697//
698// local function for file name manipulation
699//
700Function/S RemoveAllSpaces(str)
701        String str
702       
703        String tempstr = str
704        Variable ii,spc,len             //should never be more than 2 or 3 trailing spaces in a filename
705        ii=0
706        do
707                len = strlen(tempStr)
708                spc = strsearch(tempStr," ",0)          //is the last character a space?
709                if (spc == -1)
710                        break           //no more spaces found, get out
711                endif
712                str = tempstr
713                tempStr = str[0,(spc-1)] + str[(spc+1),(len-1)] //remove the space from the string
714        While(1)        //should never be more than 2 or 3
715       
716        If(strlen(tempStr) < 1)
717                tempStr = ""            //be sure to return a null string if problem found
718        Endif
719       
720        //Print strlen(tempstr)
721       
722        Return(tempStr)
723               
724End
725
726
727//Function attempts to find valid filename from partial name by checking for
728// the existence of the file on disk.
729// - checks as is
730// - adds ";vers" for possible VAX files
731// - strips spaces
732// - permutations of upper/lowercase
733//
734// added 11/99 - uppercase and lowercase versions of the file are tried, if necessary
735// since from marquee, the filename field (textread[0]) must be used, and can be a mix of
736// upper/lowercase letters, while the filename on the server (should) be all caps
737// now makes repeated calls to ValidFileString()
738//
739// returns a valid filename (No path prepended) or a null string
740//
741// called by any functions, both external and local
742//
743Function/S FindValidFilename(partialName)
744        String PartialName
745       
746        String retStr=""
747       
748        //try name with no changes - to allow for ABS files that have spaces in the names 12APR04
749        retStr = ValidFileString(partialName)
750        if(cmpstr(retStr,"") !=0)
751                //non-null return
752                return(retStr)
753        Endif
754       
755        //if the partial name is derived from the file header, there can be spaces at the beginning
756        //or in the middle of the filename - depending on the prefix and initials used
757        //
758        //remove any leading spaces from the name before starting
759        partialName = RemoveAllSpaces(partialName)
760       
761        //try name with no spaces
762        retStr = ValidFileString(partialName)
763        if(cmpstr(retStr,"") !=0)
764                //non-null return
765                return(retStr)
766        Endif
767       
768        //try all UPPERCASE
769        partialName = UpperStr(partialName)
770        retStr = ValidFileString(partialName)
771        if(cmpstr(retStr,"") !=0)
772                //non-null return
773                return(retStr)
774        Endif
775       
776        //try all lowercase (ret null if failure)
777        partialName = LowerStr(partialName)
778        retStr = ValidFileString(partialName)
779        if(cmpstr(retStr,"") !=0)
780                //non-null return
781                return(retStr)
782        else
783                return(retStr)
784        Endif
785End
786
787// Function checks for the existence of a file
788// partialName;vers (to account for VAX filenaming conventions)
789// The partial name is tried first with no version number
790//
791// *** the PATH is hard-wired to catPathName (which is assumed to exist)
792// version numers up to ;10 are tried
793// only the "name;vers" is returned if successful. The path is not prepended
794//
795// local function
796//
797Function/S ValidFileString(partialName)
798        String partialName
799       
800        String tempName = "",msg=""
801        Variable ii,refnum
802       
803        ii=0
804        do
805                if(ii==0)
806                        //first pass, try the partialName
807                        tempName = partialName
808                        Open/Z/R/T="????TEXT"/P=catPathName refnum tempName     //Does open file (/Z flag)
809                        if(V_flag == 0)
810                                //file exists
811                                Close refnum            //YES needed,
812                                break
813                        endif
814                else
815                        tempName = partialName + ";" + num2str(ii)
816                        Open/Z/R/T="????TEXT"/P=catPathName refnum tempName
817                        if(V_flag == 0)
818                                //file exists
819                                Close refnum
820                                break
821                        endif
822                Endif
823                ii+=1
824                //print "ii=",ii
825        while(ii<11)
826        //go get the selected bits of information, using tempName, which exists
827        if(ii>=11)
828                //msg = partialName + " not found. is version number > 11?"
829                //DoAlert 0, msg
830                //PathInfo catPathName
831                //Print S_Path
832                Return ("")             //use null string as error condition
833        Endif
834       
835        Return (tempName)
836End
837
838//returns a string containing filename (WITHOUT the ;vers)
839//the input string is a full path to the file (Mac-style, still works on Win in IGOR)
840//with the folders separated by colons
841//
842// called by MaskUtils.ipf, ProtocolAsPanel.ipf, WriteQIS.ipf
843//
844Function/S GetFileNameFromPathNoSemi(fullPath)
845        String fullPath
846       
847        Variable offset1,offset2
848        String filename=""
849        //String PartialPath
850        offset1 = 0
851        do
852                offset2 = StrSearch(fullPath, ":", offset1)
853                if (offset2 == -1)                              // no more colons ?
854                        fileName = FullPath[offset1,strlen(FullPath) ]
855                        //PartialPath = FullPath[0, offset1-1]
856                        break
857                endif
858                offset1 = offset2+1
859        while (1)
860       
861        //remove version number from name, if it's there - format should be: filename;N
862        filename =  StringFromList(0,filename,";")              //returns null if error
863       
864        Return filename
865End
866
867//returns a string containing filename (INCLUDING the ;vers)
868//the input string is a full path to the file (Mac-style, still works on Win in IGOR)
869//with the folders separated by colons
870//
871// local, currently unused
872//
873Function/S GetFileNameFromPathKeepSemi(fullPath)
874        String fullPath
875       
876        Variable offset1,offset2
877        String filename
878        //String PartialPath
879        offset1 = 0
880        do
881                offset2 = StrSearch(fullPath, ":", offset1)
882                if (offset2 == -1)                              // no more colons ?
883                        fileName = FullPath[offset1,strlen(FullPath) ]
884                        //PartialPath = FullPath[0, offset1-1]
885                        break
886                endif
887                offset1 = offset2+1
888        while (1)
889       
890        //keep version number from name, if it's there - format should be: filename;N
891       
892        Return filename
893End
894
895//given the full path and filename (fullPath), strips the data path
896//(Mac-style, separated by colons) and returns this path
897//this partial path is the same string that would be returned from PathInfo, for example
898//
899// - allows the user to save to a different path than catPathName
900//
901// called by WriteQIS.ipf
902//
903Function/S GetPathStrFromfullName(fullPath)
904        String fullPath
905       
906        Variable offset1,offset2
907        //String filename
908        String PartialPath
909        offset1 = 0
910        do
911                offset2 = StrSearch(fullPath, ":", offset1)
912                if (offset2 == -1)                              // no more colons ?
913                        //fileName = FullPath[offset1,strlen(FullPath) ]
914                        PartialPath = FullPath[0, offset1-1]
915                        break
916                endif
917                offset1 = offset2+1
918        while (1)
919       
920        Return PartialPath
921End
922
923//given the VAX filename, pull off the first 8 characters to make a valid
924//file string that can be used for naming averaged 1-d files
925//
926// called by ProtocolAsPanel.ipf and Tile_2D.ipf
927//
928Function/S GetNameFromHeader(fullName)
929        String fullName
930        String temp, newName = ""
931        Variable spc,ii=0
932       
933        //filename is 20 characters NNNNNxxx.SAn_NNN_NNN
934        //want the first 8 characters, NNNNNxxx, then strip off any spaces at the beginning
935        //NNNNN was entered as less than 5 characters
936        //returns a null string if no name can be found
937        do
938                temp = fullname[ii,7]           //characters ii,7 of the name
939                spc = strsearch(temp," ",0)
940                if (spc == -1)
941                        break           //no more spaces found
942                endif
943                ii+=1
944        While(ii<8)
945       
946        If(strlen(temp) < 1)
947                newName = ""            //be sure to return a null string if problem found
948        else
949                newName = temp
950        Endif
951       
952        Return(newName)
953End
954
955//list (input) is a list, typically returned from IndexedFile()
956//which is semicolon-delimited, and may contain filenames from the VAX
957//that contain version numbers, where the version number appears as a separate list item
958//(and also as a non-existent file)
959//these numbers must be purged from the list, especially for display in a popup
960//or list processing of filenames
961//the function returns the list, cleaned of version numbers (up to 11)
962//raw data files will typically never have a version number other than 1.
963//
964// if there are no version numbers in the list, the input list is returned
965//
966// called by CatVSTable.ipf, NSORT.ipf, Transmission.ipf, WorkFileUtils.ipf
967//
968Function/S RemoveVersNumsFromList(list)
969        String list
970       
971        //get rid of version numbers first (up to 11)
972        Variable ii,num
973        String item
974        num = ItemsInList(list,";")
975        ii=1
976        do
977                item = num2str(ii)
978                list = RemoveFromList(item, list ,";" )
979                ii+=1
980        while(ii<12)
981       
982        return (list)
983End
984
985//input is a list of run numbers, and output is a list of filenames (not the full path)
986//*** input list must be COMMA delimited***
987//output is equivalent to selecting from the CAT table
988//if some or all of the list items are valid filenames, keep them...
989//if an error is encountered, notify of the offending element and return a null list
990//
991//output is COMMA delimited
992//
993// this routine is expecting that the "ask", "none" special cases are handled elsewhere
994//and not passed here
995//
996// called by Marquee.ipf, MultipleReduce.ipf, ProtocolAsPanel.ipf
997//
998Function/S ParseRunNumberList(list)
999        String list
1000       
1001        String newList="",item="",tempStr=""
1002        Variable num,ii,runNum
1003       
1004        //expand number ranges, if any
1005        list = ExpandNumRanges(list)
1006       
1007        num=itemsinlist(list,",")
1008       
1009        for(ii=0;ii<num;ii+=1)
1010                //get the item
1011                item = StringFromList(ii,list,",")
1012                //is it already a valid filename?
1013                tempStr=FindValidFilename(item) //returns filename if good, null if error
1014                if(strlen(tempstr)!=0)
1015                        //valid name, add to list
1016                        //Print "it's a file"
1017                        newList += tempStr + ","
1018                else
1019                        //not a valid name
1020                        //is it a number?
1021                        runNum=str2num(item)
1022                        //print runnum
1023                        if(numtype(runNum) != 0)
1024                                //not a number -  maybe an error                       
1025                                DoAlert 0,"List item "+item+" is not a valid run number or filename. Please enter a valid number or filename."
1026                                return("")
1027                        else
1028                                //a run number or an error
1029                                tempStr = GetFileNameFromPathNoSemi( FindFileFromRunNumber(runNum) )
1030                                if(strlen(tempstr)==0)
1031                                        //file not found, error
1032                                        DoAlert 0,"List item "+item+" is not a valid run number. Please enter a valid number."
1033                                        return("")
1034                                else
1035                                        newList += tempStr + ","
1036                                endif
1037                        endif
1038                endif
1039        endfor          //loop over all items in list
1040       
1041        return(newList)
1042End
1043
1044//takes a comma delimited list that MAY contain number range, and
1045//expands any range of run numbers into a comma-delimited list...
1046//and returns the new list - if not a range, return unchanged
1047//
1048// local function
1049//
1050Function/S ExpandNumRanges(list)
1051        String list
1052       
1053        String newList="",dash="-",item,str
1054        Variable num,ii,hasDash
1055       
1056        num=itemsinlist(list,",")
1057//      print num
1058        for(ii=0;ii<num;ii+=1)
1059                //get the item
1060                item = StringFromList(ii,list,",")
1061                //does it contain a dash?
1062                hasDash = strsearch(item,dash,0)                //-1 if no dash found
1063                if(hasDash == -1)
1064                        //not a range, keep it in the list
1065                        newList += item + ","
1066                else
1067                        //has a dash (so it's a range), expand (or add null)
1068                        newList += ListFromDash(item)           
1069                endif
1070        endfor
1071       
1072        return newList
1073End
1074
1075//be sure to add a trailing comma to the return string...
1076//
1077// local function
1078//
1079Function/S ListFromDash(item)
1080        String item
1081       
1082        String numList="",loStr="",hiStr=""
1083        Variable lo,hi,ii
1084       
1085        loStr=StringFromList(0,item,"-")        //treat the range as a list
1086        hiStr=StringFromList(1,item,"-")
1087        lo=str2num(loStr)
1088        hi=str2num(hiStr)
1089        if( (numtype(lo) != 0) || (numtype(hi) !=0 ) || (lo > hi) )
1090                numList=""
1091                return numList
1092        endif
1093        for(ii=lo;ii<=hi;ii+=1)
1094                numList += num2str(ii) + ","
1095        endfor
1096       
1097        Return numList
1098End
1099
1100
1101////////Transmission
1102//******************
1103//lookup tables for attenuator transmissions
1104//NG3 and NG7 attenuators are physically different, so the transmissions are slightly different
1105//NG1 - (8m SANS) is not supported
1106//
1107// new calibration done June 2007, John Barker
1108//
1109Proc MakeNG3AttenTable()
1110
1111        NewDataFolder/O root:myGlobals:Attenuators
1112        //do explicitly to avoid data folder problems, redundant, but it must work without fail
1113        Variable num=10         //10 needed for tables after June 2007
1114
1115        Make/O/N=(num) root:myGlobals:Attenuators:ng3att0
1116        Make/O/N=(num) root:myGlobals:Attenuators:ng3att1
1117        Make/O/N=(num) root:myGlobals:Attenuators:ng3att2
1118        Make/O/N=(num) root:myGlobals:Attenuators:ng3att3
1119        Make/O/N=(num) root:myGlobals:Attenuators:ng3att4
1120        Make/O/N=(num) root:myGlobals:Attenuators:ng3att5
1121        Make/O/N=(num) root:myGlobals:Attenuators:ng3att6
1122        Make/O/N=(num) root:myGlobals:Attenuators:ng3att7
1123        Make/O/N=(num) root:myGlobals:Attenuators:ng3att8
1124        Make/O/N=(num) root:myGlobals:Attenuators:ng3att9
1125        Make/O/N=(num) root:myGlobals:Attenuators:ng3att10
1126       
1127        //each wave has 10 elements, the transmission of att# at the wavelengths
1128        //lambda = 4,5,6,7,8,10,12,14,17,20 (4 A and 20 A are extrapolated values)
1129        Make/O/N=(num) root:myGlobals:Attenuators:ng3lambda={4,5,6,7,8,10,12,14,17,20}
1130       
1131        // new calibration done June 2007, John Barker
1132        root:myGlobals:Attenuators:ng3att0 = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
1133        root:myGlobals:Attenuators:ng3att1 = {0.444784,0.419,0.3935,0.3682,0.3492,0.3132,0.2936,0.2767,0.2477,0.22404}
1134        root:myGlobals:Attenuators:ng3att2 = {0.207506,0.1848,0.1629,0.1447,0.1292,0.1056,0.09263,0.08171,0.06656,0.0546552}
1135        root:myGlobals:Attenuators:ng3att3 = {0.092412,0.07746,0.06422,0.05379,0.04512,0.03321,0.02707,0.02237,0.01643,0.0121969}
1136        root:myGlobals:Attenuators:ng3att4 = {0.0417722,0.03302,0.02567,0.02036,0.01604,0.01067,0.00812,0.006316,0.00419,0.00282411}
1137        root:myGlobals:Attenuators:ng3att5 = {0.0187129,0.01397,0.01017,0.007591,0.005668,0.003377,0.002423,0.001771,0.001064,0.000651257}
1138        root:myGlobals:Attenuators:ng3att6 = {0.00851048,0.005984,0.004104,0.002888,0.002029,0.001098,0.0007419,0.0005141,0.000272833,0.000150624}
1139        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}
1140        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}
1141        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}
1142        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}
1143 
1144  //old tables, pre-June 2007
1145//      Make/O/N=9 root:myGlobals:Attenuators:ng3lambda={5,6,7,8,10,12,14,17,20}
1146//      root:myGlobals:Attenuators:ng3att0 = {1, 1, 1, 1, 1, 1, 1, 1,1 }
1147//      root:myGlobals:Attenuators:ng3att1 = {0.421, 0.394, 0.371, 0.349, 0.316, 0.293, 0.274, 0.245,0.220}
1148//      root:myGlobals:Attenuators:ng3att2 = {0.187, 0.164, 0.145, 0.130, 0.106, 0.0916, 0.0808, 0.0651,0.0531}
1149//      root:myGlobals:Attenuators:ng3att3 = {0.0777, 0.0636, 0.0534, 0.0446, 0.0330, 0.0262, 0.0217, 0.0157 ,0.0116}
1150//      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}
1151//      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}
1152//      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}
1153//      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}
1154//      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}
1155//      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}
1156//      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}
1157
1158End
1159
1160// new calibration done June 2007, John Barker
1161Proc MakeNG7AttenTable()
1162
1163        NewDataFolder/O root:myGlobals:Attenuators
1164       
1165        Variable num=10         //10 needed for tables after June 2007
1166       
1167        Make/O/N=(num) root:myGlobals:Attenuators:ng7att0
1168        Make/O/N=(num) root:myGlobals:Attenuators:ng7att1
1169        Make/O/N=(num) root:myGlobals:Attenuators:ng7att2
1170        Make/O/N=(num) root:myGlobals:Attenuators:ng7att3
1171        Make/O/N=(num) root:myGlobals:Attenuators:ng7att4
1172        Make/O/N=(num) root:myGlobals:Attenuators:ng7att5
1173        Make/O/N=(num) root:myGlobals:Attenuators:ng7att6
1174        Make/O/N=(num) root:myGlobals:Attenuators:ng7att7
1175        Make/O/N=(num) root:myGlobals:Attenuators:ng7att8
1176        Make/O/N=(num) root:myGlobals:Attenuators:ng7att9
1177        Make/O/N=(num) root:myGlobals:Attenuators:ng7att10
1178       
1179        //NG7 wave has 10 elements, the transmission of att# at the wavelengths
1180        //lambda =4, 5,6,7,8,10,12,14,17,20
1181        // note that some of the higher attenuations and ALL of the 4 A and 20A data is interpolated
1182        // none of these values are expected to be used in reality since the flux would be too low in practice
1183        Make/O/N=(num) root:myGlobals:Attenuators:ng7lambda={4,5,6,7,8,10,12,14,17,20}
1184
1185// New calibration, June 2007, John Barker
1186        root:myGlobals:Attenuators:ng7att0 = {1, 1, 1, 1, 1, 1, 1, 1 ,1,1}     
1187        root:myGlobals:Attenuators:ng7att1 = {0.448656,0.4192,0.3925,0.3661,0.3458,0.3098,0.2922,0.2738,0.2544,0.251352}
1188        root:myGlobals:Attenuators:ng7att2 = {0.217193,0.1898,0.1682,0.148,0.1321,0.1076,0.0957,0.08485,0.07479,0.0735965}
1189        root:myGlobals:Attenuators:ng7att3 = {0.098019,0.07877,0.06611,0.05429,0.04548,0.03318,0.02798,0.0234,0.02004,0.0202492}
1190        root:myGlobals:Attenuators:ng7att4 = {0.0426904,0.03302,0.02617,0.02026,0.0158,0.01052,0.008327,0.006665,0.005745,0.00524807}
1191        root:myGlobals:Attenuators:ng7att5 = {0.0194353,0.01398,0.01037,0.0075496,0.005542,0.003339,0.002505,0.001936,0.001765,0.00165959}
1192        root:myGlobals:Attenuators:ng7att6 = {0.00971666,0.005979,0.004136,0.002848,0.001946,0.001079,0.0007717,0.000588,0.000487337,0.000447713}
1193        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}
1194        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}
1195        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}
1196        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}
1197
1198// Pre-June 2007 calibration values - do not use these anymore 
1199////    root:myGlobals:Attenuators:ng7att0 = {1, 1, 1, 1, 1, 1, 1, 1 ,1}
1200////    root:myGlobals:Attenuators:ng7att1 = {0.418, 0.393, 0.369, 0.347, 0.313, 0.291, 0.271, 0.244, 0.219 }
1201////    root:myGlobals:Attenuators:ng7att2 = {0.189, 0.167, 0.148, 0.132, 0.109, 0.0945, 0.0830, 0.0681, 0.0560}
1202////    root:myGlobals:Attenuators:ng7att3 = {0.0784, 0.0651, 0.0541, 0.0456, 0.0340, 0.0273, 0.0223, 0.0164 , 0.0121}
1203////    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}
1204////    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}
1205////    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}
1206////    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}
1207////    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}
1208////    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}
1209////    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}
1210End
1211
1212//returns the transmission of the attenuator (at NG3) given the attenuator number
1213//which must be an integer(to select the wave) and given the wavelength.
1214//the wavelength may be any value between 4 and 20 (A), and is interpolated
1215//between calibrated wavelengths for a given attenuator
1216//
1217// Mar 2010 - abs() added to attStr to account for ICE reporting -0.0001 as an attenuator position, which truncates to "-0"
1218Function LookupAttenNG3(lambda,attenNo)
1219        Variable lambda, attenNo
1220       
1221        Variable trans
1222        String attStr="root:myGlobals:Attenuators:ng3att"+num2str(trunc(abs(attenNo)))
1223        String lamStr = "root:myGlobals:Attenuators:ng3lambda"
1224       
1225        if(attenNo == 0)
1226                return (1)              //no attenuation, return trans == 1
1227        endif
1228       
1229        if( (lambda < 4) || (lambda > 20 ) )
1230                Abort "Wavelength out of calibration range (4,20). You must manually enter the absolute parameters"
1231        Endif
1232       
1233        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) )
1234                Execute "MakeNG3AttenTable()"
1235        Endif
1236        //just in case creating the tables fails....
1237        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) )
1238                Abort "Attenuator lookup waves could not be found. You must manually enter the absolute parameters"
1239        Endif
1240       
1241        //lookup the value by interpolating the wavelength
1242        //the attenuator must always be an integer
1243        Wave att = $attStr
1244        Wave lam = $lamstr
1245        trans = interp(lambda,lam,att)
1246       
1247//      Print "trans = ",trans
1248       
1249        return trans
1250End
1251
1252//returns the transmission of the attenuator (at NG7) given the attenuator number
1253//which must be an integer(to select the wave) and given the wavelength.
1254//the wavelength may be any value between 4 and 20 (A), and is interpolated
1255//between calibrated wavelengths for a given attenuator
1256//
1257// this set of tables is also used for NG5 (NG1) SANS instrument - as the attenuator has never been calibrated
1258//
1259// local function
1260//
1261// Mar 2010 - abs() added to attStr to account for ICE reporting -0.0001 as an attenuator position, which truncates to "-0"
1262Function LookupAttenNG7(lambda,attenNo)
1263        Variable lambda, attenNo
1264       
1265        Variable trans
1266        String attStr="root:myGlobals:Attenuators:ng7att"+num2str(trunc(abs(attenNo)))
1267        String lamStr = "root:myGlobals:Attenuators:ng7lambda"
1268       
1269        if(attenNo == 0)
1270                return (1)              //no attenuation, return trans == 1
1271        endif
1272       
1273        if( (lambda < 4) || (lambda > 20 ) )
1274                Abort "Wavelength out of calibration range (4,20). You must manually enter the absolute parameters"
1275        Endif
1276       
1277        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) )
1278                Execute "MakeNG7AttenTable()"
1279        Endif
1280        //just in case creating the tables fails....
1281        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) )
1282                Abort "Attenuator lookup waves could not be found. You must manually enter the absolute parameters"
1283        Endif
1284       
1285        //lookup the value by interpolating the wavelength
1286        //the attenuator must always be an integer
1287        Wave att = $attStr
1288        Wave lam = $lamstr
1289        trans = interp(lambda,lam,att)
1290       
1291        //Print "trans = ",trans
1292       
1293        return trans
1294
1295End
1296
1297//returns the proper attenuation factor based on the instrument (NG3, NG5, or NG7)
1298//NG5 values are taken from the NG7 tables (there is very little difference in the
1299//values, and NG5 attenuators have not been calibrated (as of 8/01)
1300//
1301// filestr is passed from TextRead[3] = the default directory
1302// lam is passed from RealsRead[26]
1303// AttenNo is passed from ReaslRead[3]
1304//
1305// Attenuation factor as defined here is <= 1
1306//
1307// ORNL can pass ("",1,attenuationFactor) and have this function simply
1308// spit back the attenuationFactor (that was read into rw[3])
1309//
1310// called by Correct.ipf, ProtocolAsPanel.ipf, Transmission.ipf
1311//
1312Function AttenuationFactor(fileStr,lam,attenNo)
1313        String fileStr
1314        Variable lam,attenNo
1315       
1316        Variable attenFactor=1,loc
1317        String instr=fileStr[1,3]       //filestr is "[NGnSANSn] " or "[NGnSANSnn]" (11 characters total)
1318       
1319        strswitch(instr)
1320                case "NG3":
1321                        attenFactor = LookupAttenNG3(lam,attenNo)
1322                        break
1323                case "NG5":
1324                        //using NG7 lookup Table
1325                        attenFactor = LookupAttenNG7(lam,attenNo)
1326                        break
1327                case "NG7":
1328                        attenFactor = LookupAttenNG7(lam,attenNo)
1329                        break
1330                default:                                                       
1331                        //return error?
1332                        attenFactor=1
1333        endswitch
1334//      print "instr, lambda, attenNo,attenFactor = ",instr,lam,attenNo,attenFactor
1335        return(attenFactor)
1336End
1337
1338//function called by the popups to get a file list of data that can be sorted
1339// this procedure simply removes the raw data files from the string - there
1340//can be lots of other junk present, but this is very fast...
1341//
1342// could also use the alternate procedure of keeping only file with the proper extension
1343//
1344// another possibility is to get a listing of the text files, but is unreliable on
1345// Windows, where the data file must be .txt (and possibly OSX)
1346//
1347// called by FIT_Ops.ipf, NSORT.ipf, PlotUtils.ipf
1348//
1349Function/S ReducedDataFileList(ctrlName)
1350        String ctrlName
1351
1352        String list="",newList="",item=""
1353        Variable num,ii
1354       
1355        //check for the path
1356        PathInfo catPathName
1357        if(V_Flag==0)
1358                DoAlert 0, "Data path does not exist - pick the data path from the button on the main panel"
1359                Return("")
1360        Endif
1361       
1362        list = IndexedFile(catpathName,-1,"????")
1363       
1364        list = RemoveFromList(ListMatch(list,"*.SA1*",";"), list, ";", 0)
1365        list = RemoveFromList(ListMatch(list,"*.SA2*",";"), list, ";", 0)
1366        list = RemoveFromList(ListMatch(list,"*.SA3*",";"), list, ";", 0)
1367        list = RemoveFromList(ListMatch(list,".*",";"), list, ";", 0)
1368        list = RemoveFromList(ListMatch(list,"*.pxp",";"), list, ";", 0)
1369        list = RemoveFromList(ListMatch(list,"*.DIV",";"), list, ";", 0)
1370        list = RemoveFromList(ListMatch(list,"*.GSP",";"), list, ";", 0)
1371        list = RemoveFromList(ListMatch(list,"*.MASK",";"), list, ";", 0)
1372
1373        //remove VAX version numbers
1374        list = RemoveVersNumsFromList(List)
1375        //sort
1376        newList = SortList(List,";",0)
1377
1378        return newlist
1379End
1380
1381// returns a list of raw data files in the catPathName directory on disk
1382// - list is SEMICOLON-delimited
1383//
1384// does it the "cheap" way, simply finding the ".SAn" in the file name
1385// = does not check for proper byte length.
1386//
1387// called by PatchFiles.ipf, Tile_2D.ipf
1388//
1389Function/S GetRawDataFileList()
1390       
1391        //make sure that path exists
1392        PathInfo catPathName
1393        if (V_flag == 0)
1394                Abort "Folder path does not exist - use Pick Path button on Main Panel"
1395        Endif
1396       
1397        String list=IndexedFile(catPathName,-1,"????")
1398        String newList="",item=""
1399        Variable num=ItemsInList(list,";"),ii
1400        for(ii=0;ii<num;ii+=1)
1401                item = StringFromList(ii, list  ,";")
1402                if( stringmatch(item,"*.sa1*") )
1403                        newlist += item + ";"
1404                endif
1405                if( stringmatch(item,"*.sa2*") )
1406                        newlist += item + ";"
1407                endif
1408                if( stringmatch(item,"*.sa3*") )
1409                        newlist += item + ";"
1410                endif
1411                //print "ii=",ii
1412        endfor
1413        newList = SortList(newList,";",0)
1414        return(newList)
1415End
1416
1417// Return the filename that represents the previous or next file.
1418// Input is current filename and increment.
1419// Increment should be -1 or 1
1420// -1 => previous file
1421// 1 => next file
1422Function/S GetPrevNextRawFile(curfilename, prevnext)
1423        String curfilename
1424        Variable prevnext
1425
1426        String filename
1427       
1428        //get the run number
1429        Variable num = GetRunNumFromFile(curfilename)
1430               
1431        //find the next specified file by number
1432        fileName = FindFileFromRunNumber(num+prevnext)
1433
1434        if(cmpstr(fileName,"")==0)
1435                //null return, do nothing
1436                fileName = FindFileFromRunNumber(num)
1437        Endif
1438
1439//      print "in FU "+filename
1440
1441        Return filename
1442End
1443
1444
Note: See TracBrowser for help on using the repository browser.