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

Last change on this file since 740 was 715, checked in by ajj, 12 years ago

Various ANSTO related fixes.

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 anyhow...
426//
427Function/S GetPrefixStrFromFile(item)
428        String item
429        String invalid = ""     //"" is not a valid run prefix, since it's text
430        Variable num=-1
431       
432        //find the "dot"
433        String runStr=""
434        Variable pos = strsearch(item,".",0)
435        if(pos == -1)
436                //"dot" not found
437                return (invalid)
438        else
439                //found, skip the three characters preceeding it
440                if (pos <=3)
441                        //not enough characters
442                        return (invalid)
443                else
444                        runStr = item[0,pos-4]
445                        return (runStr)
446                Endif
447        Endif
448End
449
450
451
452
453/////VAX filename/Run number parsing utilities
454//
455// a collection of uilities for processing vax filenames
456//and processing lists (especially for display in popup menus)
457//
458//required to correctly account for VAX supplied version numbers, which
459//may or may not be removed by the ftp utility
460//
461// - parses lists of run numbers into real filenames
462// - selects proper detector constants
463//
464//**************************
465//
466//given a filename of a SANS data filename of the form
467//TTTTTnnn.SAn_TTT_Txxx
468//returns the run number "nnn" as a number
469//returns -1 as an invalid file number
470//
471// called by several ipfs
472//
473//
474Function GetRunNumFromFile(item)
475        String item
476        Variable invalid = -1   //negative numbers are invalid
477        Variable num=-1
478       
479        //find the "dot"
480        String runStr=""
481        Variable pos = strsearch(item,".",0)
482        if(pos == -1)
483                //"dot" not found
484                return (invalid)
485        else
486                //found, get the three characters preceeding it
487                if (pos <=2)
488                        //not enough characters
489                        return (invalid)
490                else
491                        runStr = item[pos-3,pos-1]
492                        //convert to a number
493                        num = str2num(runStr)
494                        //if valid, return it
495                        if (num == NaN)
496                                //3 characters were not a number
497                                return (invalid)
498                        else
499                                //run was OK
500                                return (num)
501                        Endif
502                Endif
503        Endif
504End
505
506//given a filename of a SANS data filename of the form
507//TTTTTnnn.SAn_TTT_Txxx
508//returns the run number "nnn" as a STRING of THREE characters
509//returns "ABC" as an invalid file number
510//
511// local function to aid in locating files by run number
512//
513Function/S GetRunNumStrFromFile(item)
514        String item
515        String invalid = "ABC"  //"ABC" is not a valid run number, since it's text
516        Variable num=-1
517       
518        //find the "dot"
519        String runStr=""
520        Variable pos = strsearch(item,".",0)
521        if(pos == -1)
522                //"dot" not found
523                return (invalid)
524        else
525                //found, get the three characters preceeding it
526                if (pos <=2)
527                        //not enough characters
528                        return (invalid)
529                else
530                        runStr = item[pos-3,pos-1]
531                        return (runStr)
532                Endif
533        Endif
534End
535
536//returns a string containing the full path to the file containing the
537//run number "num". The null string is returned if no valid file can be found
538//the path "catPathName" used and is hard-wired, will abort if this path does not exist
539//the file returned will be a RAW SANS data file, other types of files are
540//filtered out.
541//
542// called by Buttons.ipf and Transmission.ipf, and locally by parsing routines
543//
544Function/S FindFileFromRunNumber(num)
545        Variable num
546       
547        String fullName="",partialName="",item=""
548        //get list of raw data files in folder that match "num" (add leading zeros)
549        if( (num>999) || (num<=0) )
550                //Print "error in  FindFileFromRunNumber(num), file number too large or too small"
551                Return ("")
552        Endif
553        //make a three character string of the run number
554        String numStr=""
555        if(num<10)
556                numStr = "00"+num2str(num)
557        else
558                if(num<100)
559                        numStr = "0"+num2str(num)
560                else
561                        numStr = num2str(num)
562                Endif
563        Endif
564        //Print "numstr = ",numstr
565       
566        //make sure that path exists
567        PathInfo catPathName
568        String path = S_path
569        if (V_flag == 0)
570                Abort "folder path does not exist - use Pick Path button"
571        Endif
572        String list="",newList="",testStr=""
573       
574        list = IndexedFile(catPathName,-1,"????")       //get all files in folder
575        //find (the) one with the number in the run # location in the name
576        Variable numItems,ii,runFound,isRAW
577        numItems = ItemsInList(list,";")                //get the new number of items in the list
578        ii=0
579        do
580                //parse through the list in this order:
581                // 1 - does item contain run number (as a string) "TTTTTnnn.SAn_XXX_Tyyy"
582                // 2 - exclude by isRaw? (to minimize disk access)
583                item = StringFromList(ii, list  ,";" )
584                if(strlen(item) != 0)
585                        //find the run number, if it exists as a three character string
586                        testStr = GetRunNumStrFromFile(item)
587                        runFound= cmpstr(numStr,testStr)        //compare the three character strings, 0 if equal
588                        if(runFound == 0)
589                                //the run Number was found
590                                //build valid filename
591                                partialName = FindValidFileName(item)
592                                if(strlen(partialName) != 0)            //non-null return from FindValidFileName()
593                                        fullName = path + partialName
594                                        //check if RAW, if so,this must be the file!
595                                        isRAW = CheckIfRawData(fullName)
596                                        if(isRaw)
597                                                //stop here
598                                                return(fullname)
599                                        Endif
600                                Endif
601                        Endif
602                Endif
603                ii+=1
604        while(ii<numItems)              //process all items in list
605        Return ("")     //null return if file not found in list
606End
607
608//function to test a binary file to see if it is a RAW binary SANS file
609//first checks the total bytes in the file (which for raw data is 33316 bytes)
610//**note that the "DIV" file will also show up as a raw file by the run field
611//should be listed in CAT/SHORT and in patch windows
612//
613//Function then checks the file fname (full path:file) for "RAW" run.type field
614//if not found, the data is not raw data and zero is returned
615//
616// called by many procedures (both external and local)
617//
618Function CheckIfRawData(fname)
619        String fname
620       
621        Variable refnum,totalBytes
622        String testStr=""
623       
624        Open/R/T="????TEXT" refNum as fname
625        if(strlen(s_filename) == 0)     //user cancel (/Z not used, so V_flag not set)
626                return(0)
627        endif
628       
629        //get the total number of bytes in the file
630        FStatus refNum
631        totalBytes = V_logEOF
632        //Print totalBytes
633        if(totalBytes < 100)
634                Close refNum
635                return(0)               //not a raw file
636        endif
637        FSetPos refNum,75
638        FReadLine/N=3 refNum,testStr
639        Close refNum
640       
641        if(totalBytes == 33316 && ( cmpstr(testStr,"RAW")==0 ||  cmpstr(testStr,"SIM")==0))
642                //true, is raw data file
643                Return(1)
644        else
645                //some other file
646                Return(0)
647        Endif
648End
649
650//function to check the header of a raw data file (full path specified by fname)
651//checks the field of the x-position of the beamstop during data collection
652//if the x-position is more negative (farther to the left) than xTol(input)
653//the the beamstop is "out" and the file is a transmission run and not a scattering run
654//xtol typically set at -5 (cm) - trans runs have bs(x) at -10 to -15 cm
655// function returns 1 if beamstop is out, 0 if beamstop is in
656//
657// tolerance is set as a global value "root:myGlobals:BeamstopXTol"
658//
659// called by Transmission.ipf, CatVSTable.ipf, NSORT.ipf
660//
661Function isTransFile(fName)
662        String fname
663       
664        Variable refnum,xpos
665        NVAR xTol = root:myGlobals:BeamstopXTol
666       
667        //pos = 369, read one real value
668       
669        SetDataFolder root:
670        String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
671        String strToExecute=""
672        // 1 R*4 value
673        strToExecute = GBLoadStr + "/S=368/U=1" + "\"" + fname + "\""
674        Execute strToExecute
675        Wave w=$"root:tempGBWave0"
676        xPos = w[0]
677        KillWaves/Z w
678        //Print "xPos = ",xpos
679       
680        if(xpos<=xTol)
681                //xpos is farther left (more negative) than xtol (currently -5 cm)
682                Return(1)
683        else
684                //some other file
685                Return(0)
686        Endif
687End
688
689
690//function to remove all spaces from names when searching for filenames
691//the filename (as saved) will never have interior spaces (TTTTTnnn_AB _Bnnn)
692//but the text field in the header WILL, if less than 3 characters were used for the
693//user's initials, and can have leading spaces if prefix was less than 5 characters
694//
695//returns a string identical to the original string, except with the interior spaces removed
696//
697// local function for file name manipulation
698//
699Function/S RemoveAllSpaces(str)
700        String str
701       
702        String tempstr = str
703        Variable ii,spc,len             //should never be more than 2 or 3 trailing spaces in a filename
704        ii=0
705        do
706                len = strlen(tempStr)
707                spc = strsearch(tempStr," ",0)          //is the last character a space?
708                if (spc == -1)
709                        break           //no more spaces found, get out
710                endif
711                str = tempstr
712                tempStr = str[0,(spc-1)] + str[(spc+1),(len-1)] //remove the space from the string
713        While(1)        //should never be more than 2 or 3
714       
715        If(strlen(tempStr) < 1)
716                tempStr = ""            //be sure to return a null string if problem found
717        Endif
718       
719        //Print strlen(tempstr)
720       
721        Return(tempStr)
722               
723End
724
725
726//Function attempts to find valid filename from partial name by checking for
727// the existence of the file on disk.
728// - checks as is
729// - adds ";vers" for possible VAX files
730// - strips spaces
731// - permutations of upper/lowercase
732//
733// added 11/99 - uppercase and lowercase versions of the file are tried, if necessary
734// since from marquee, the filename field (textread[0]) must be used, and can be a mix of
735// upper/lowercase letters, while the filename on the server (should) be all caps
736// now makes repeated calls to ValidFileString()
737//
738// returns a valid filename (No path prepended) or a null string
739//
740// called by any functions, both external and local
741//
742Function/S FindValidFilename(partialName)
743        String PartialName
744       
745        String retStr=""
746       
747        //try name with no changes - to allow for ABS files that have spaces in the names 12APR04
748        retStr = ValidFileString(partialName)
749        if(cmpstr(retStr,"") !=0)
750                //non-null return
751                return(retStr)
752        Endif
753       
754        //if the partial name is derived from the file header, there can be spaces at the beginning
755        //or in the middle of the filename - depending on the prefix and initials used
756        //
757        //remove any leading spaces from the name before starting
758        partialName = RemoveAllSpaces(partialName)
759       
760        //try name with no spaces
761        retStr = ValidFileString(partialName)
762        if(cmpstr(retStr,"") !=0)
763                //non-null return
764                return(retStr)
765        Endif
766       
767        //try all UPPERCASE
768        partialName = UpperStr(partialName)
769        retStr = ValidFileString(partialName)
770        if(cmpstr(retStr,"") !=0)
771                //non-null return
772                return(retStr)
773        Endif
774       
775        //try all lowercase (ret null if failure)
776        partialName = LowerStr(partialName)
777        retStr = ValidFileString(partialName)
778        if(cmpstr(retStr,"") !=0)
779                //non-null return
780                return(retStr)
781        else
782                return(retStr)
783        Endif
784End
785
786// Function checks for the existence of a file
787// partialName;vers (to account for VAX filenaming conventions)
788// The partial name is tried first with no version number
789//
790// *** the PATH is hard-wired to catPathName (which is assumed to exist)
791// version numers up to ;10 are tried
792// only the "name;vers" is returned if successful. The path is not prepended
793//
794// local function
795//
796Function/S ValidFileString(partialName)
797        String partialName
798       
799        String tempName = "",msg=""
800        Variable ii,refnum
801       
802        ii=0
803        do
804                if(ii==0)
805                        //first pass, try the partialName
806                        tempName = partialName
807                        Open/Z/R/T="????TEXT"/P=catPathName refnum tempName     //Does open file (/Z flag)
808                        if(V_flag == 0)
809                                //file exists
810                                Close refnum            //YES needed,
811                                break
812                        endif
813                else
814                        tempName = partialName + ";" + num2str(ii)
815                        Open/Z/R/T="????TEXT"/P=catPathName refnum tempName
816                        if(V_flag == 0)
817                                //file exists
818                                Close refnum
819                                break
820                        endif
821                Endif
822                ii+=1
823                //print "ii=",ii
824        while(ii<11)
825        //go get the selected bits of information, using tempName, which exists
826        if(ii>=11)
827                //msg = partialName + " not found. is version number > 11?"
828                //DoAlert 0, msg
829                //PathInfo catPathName
830                //Print S_Path
831                Return ("")             //use null string as error condition
832        Endif
833       
834        Return (tempName)
835End
836
837//returns a string containing filename (WITHOUT the ;vers)
838//the input string is a full path to the file (Mac-style, still works on Win in IGOR)
839//with the folders separated by colons
840//
841// called by MaskUtils.ipf, ProtocolAsPanel.ipf, WriteQIS.ipf
842//
843Function/S GetFileNameFromPathNoSemi(fullPath)
844        String fullPath
845       
846        Variable offset1,offset2
847        String filename=""
848        //String PartialPath
849        offset1 = 0
850        do
851                offset2 = StrSearch(fullPath, ":", offset1)
852                if (offset2 == -1)                              // no more colons ?
853                        fileName = FullPath[offset1,strlen(FullPath) ]
854                        //PartialPath = FullPath[0, offset1-1]
855                        break
856                endif
857                offset1 = offset2+1
858        while (1)
859       
860        //remove version number from name, if it's there - format should be: filename;N
861        filename =  StringFromList(0,filename,";")              //returns null if error
862       
863        Return filename
864End
865
866//returns a string containing filename (INCLUDING the ;vers)
867//the input string is a full path to the file (Mac-style, still works on Win in IGOR)
868//with the folders separated by colons
869//
870// local, currently unused
871//
872Function/S GetFileNameFromPathKeepSemi(fullPath)
873        String fullPath
874       
875        Variable offset1,offset2
876        String filename
877        //String PartialPath
878        offset1 = 0
879        do
880                offset2 = StrSearch(fullPath, ":", offset1)
881                if (offset2 == -1)                              // no more colons ?
882                        fileName = FullPath[offset1,strlen(FullPath) ]
883                        //PartialPath = FullPath[0, offset1-1]
884                        break
885                endif
886                offset1 = offset2+1
887        while (1)
888       
889        //keep version number from name, if it's there - format should be: filename;N
890       
891        Return filename
892End
893
894//given the full path and filename (fullPath), strips the data path
895//(Mac-style, separated by colons) and returns this path
896//this partial path is the same string that would be returned from PathInfo, for example
897//
898// - allows the user to save to a different path than catPathName
899//
900// called by WriteQIS.ipf
901//
902Function/S GetPathStrFromfullName(fullPath)
903        String fullPath
904       
905        Variable offset1,offset2
906        //String filename
907        String PartialPath
908        offset1 = 0
909        do
910                offset2 = StrSearch(fullPath, ":", offset1)
911                if (offset2 == -1)                              // no more colons ?
912                        //fileName = FullPath[offset1,strlen(FullPath) ]
913                        PartialPath = FullPath[0, offset1-1]
914                        break
915                endif
916                offset1 = offset2+1
917        while (1)
918       
919        Return PartialPath
920End
921
922//given the VAX filename, pull off the first 8 characters to make a valid
923//file string that can be used for naming averaged 1-d files
924//
925// called by ProtocolAsPanel.ipf and Tile_2D.ipf
926//
927Function/S GetNameFromHeader(fullName)
928        String fullName
929        String temp, newName = ""
930        Variable spc,ii=0
931       
932        //filename is 20 characters NNNNNxxx.SAn_NNN_NNN
933        //want the first 8 characters, NNNNNxxx, then strip off any spaces at the beginning
934        //NNNNN was entered as less than 5 characters
935        //returns a null string if no name can be found
936        do
937                temp = fullname[ii,7]           //characters ii,7 of the name
938                spc = strsearch(temp," ",0)
939                if (spc == -1)
940                        break           //no more spaces found
941                endif
942                ii+=1
943        While(ii<8)
944       
945        If(strlen(temp) < 1)
946                newName = ""            //be sure to return a null string if problem found
947        else
948                newName = temp
949        Endif
950       
951        Return(newName)
952End
953
954//list (input) is a list, typically returned from IndexedFile()
955//which is semicolon-delimited, and may contain filenames from the VAX
956//that contain version numbers, where the version number appears as a separate list item
957//(and also as a non-existent file)
958//these numbers must be purged from the list, especially for display in a popup
959//or list processing of filenames
960//the function returns the list, cleaned of version numbers (up to 11)
961//raw data files will typically never have a version number other than 1.
962//
963// if there are no version numbers in the list, the input list is returned
964//
965// called by CatVSTable.ipf, NSORT.ipf, Transmission.ipf, WorkFileUtils.ipf
966//
967Function/S RemoveVersNumsFromList(list)
968        String list
969       
970        //get rid of version numbers first (up to 11)
971        Variable ii,num
972        String item
973        num = ItemsInList(list,";")
974        ii=1
975        do
976                item = num2str(ii)
977                list = RemoveFromList(item, list ,";" )
978                ii+=1
979        while(ii<12)
980       
981        return (list)
982End
983
984//input is a list of run numbers, and output is a list of filenames (not the full path)
985//*** input list must be COMMA delimited***
986//output is equivalent to selecting from the CAT table
987//if some or all of the list items are valid filenames, keep them...
988//if an error is encountered, notify of the offending element and return a null list
989//
990//output is COMMA delimited
991//
992// this routine is expecting that the "ask", "none" special cases are handled elsewhere
993//and not passed here
994//
995// called by Marquee.ipf, MultipleReduce.ipf, ProtocolAsPanel.ipf
996//
997Function/S ParseRunNumberList(list)
998        String list
999       
1000        String newList="",item="",tempStr=""
1001        Variable num,ii,runNum
1002       
1003        //expand number ranges, if any
1004        list = ExpandNumRanges(list)
1005       
1006        num=itemsinlist(list,",")
1007       
1008        for(ii=0;ii<num;ii+=1)
1009                //get the item
1010                item = StringFromList(ii,list,",")
1011                //is it already a valid filename?
1012                tempStr=FindValidFilename(item) //returns filename if good, null if error
1013                if(strlen(tempstr)!=0)
1014                        //valid name, add to list
1015                        //Print "it's a file"
1016                        newList += tempStr + ","
1017                else
1018                        //not a valid name
1019                        //is it a number?
1020                        runNum=str2num(item)
1021                        //print runnum
1022                        if(numtype(runNum) != 0)
1023                                //not a number -  maybe an error                       
1024                                DoAlert 0,"List item "+item+" is not a valid run number or filename. Please enter a valid number or filename."
1025                                return("")
1026                        else
1027                                //a run number or an error
1028                                tempStr = GetFileNameFromPathNoSemi( FindFileFromRunNumber(runNum) )
1029                                if(strlen(tempstr)==0)
1030                                        //file not found, error
1031                                        DoAlert 0,"List item "+item+" is not a valid run number. Please enter a valid number."
1032                                        return("")
1033                                else
1034                                        newList += tempStr + ","
1035                                endif
1036                        endif
1037                endif
1038        endfor          //loop over all items in list
1039       
1040        return(newList)
1041End
1042
1043//takes a comma delimited list that MAY contain number range, and
1044//expands any range of run numbers into a comma-delimited list...
1045//and returns the new list - if not a range, return unchanged
1046//
1047// local function
1048//
1049Function/S ExpandNumRanges(list)
1050        String list
1051       
1052        String newList="",dash="-",item,str
1053        Variable num,ii,hasDash
1054       
1055        num=itemsinlist(list,",")
1056//      print num
1057        for(ii=0;ii<num;ii+=1)
1058                //get the item
1059                item = StringFromList(ii,list,",")
1060                //does it contain a dash?
1061                hasDash = strsearch(item,dash,0)                //-1 if no dash found
1062                if(hasDash == -1)
1063                        //not a range, keep it in the list
1064                        newList += item + ","
1065                else
1066                        //has a dash (so it's a range), expand (or add null)
1067                        newList += ListFromDash(item)           
1068                endif
1069        endfor
1070       
1071        return newList
1072End
1073
1074//be sure to add a trailing comma to the return string...
1075//
1076// local function
1077//
1078Function/S ListFromDash(item)
1079        String item
1080       
1081        String numList="",loStr="",hiStr=""
1082        Variable lo,hi,ii
1083       
1084        loStr=StringFromList(0,item,"-")        //treat the range as a list
1085        hiStr=StringFromList(1,item,"-")
1086        lo=str2num(loStr)
1087        hi=str2num(hiStr)
1088        if( (numtype(lo) != 0) || (numtype(hi) !=0 ) || (lo > hi) )
1089                numList=""
1090                return numList
1091        endif
1092        for(ii=lo;ii<=hi;ii+=1)
1093                numList += num2str(ii) + ","
1094        endfor
1095       
1096        Return numList
1097End
1098
1099
1100////////Transmission
1101//******************
1102//lookup tables for attenuator transmissions
1103//NG3 and NG7 attenuators are physically different, so the transmissions are slightly different
1104//NG1 - (8m SANS) is not supported
1105//
1106// new calibration done June 2007, John Barker
1107//
1108Proc MakeNG3AttenTable()
1109
1110        NewDataFolder/O root:myGlobals:Attenuators
1111        //do explicitly to avoid data folder problems, redundant, but it must work without fail
1112        Variable num=10         //10 needed for tables after June 2007
1113
1114        Make/O/N=(num) root:myGlobals:Attenuators:ng3att0
1115        Make/O/N=(num) root:myGlobals:Attenuators:ng3att1
1116        Make/O/N=(num) root:myGlobals:Attenuators:ng3att2
1117        Make/O/N=(num) root:myGlobals:Attenuators:ng3att3
1118        Make/O/N=(num) root:myGlobals:Attenuators:ng3att4
1119        Make/O/N=(num) root:myGlobals:Attenuators:ng3att5
1120        Make/O/N=(num) root:myGlobals:Attenuators:ng3att6
1121        Make/O/N=(num) root:myGlobals:Attenuators:ng3att7
1122        Make/O/N=(num) root:myGlobals:Attenuators:ng3att8
1123        Make/O/N=(num) root:myGlobals:Attenuators:ng3att9
1124        Make/O/N=(num) root:myGlobals:Attenuators:ng3att10
1125       
1126        //each wave has 10 elements, the transmission of att# at the wavelengths
1127        //lambda = 4,5,6,7,8,10,12,14,17,20 (4 A and 20 A are extrapolated values)
1128        Make/O/N=(num) root:myGlobals:Attenuators:ng3lambda={4,5,6,7,8,10,12,14,17,20}
1129       
1130        // new calibration done June 2007, John Barker
1131        root:myGlobals:Attenuators:ng3att0 = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
1132        root:myGlobals:Attenuators:ng3att1 = {0.444784,0.419,0.3935,0.3682,0.3492,0.3132,0.2936,0.2767,0.2477,0.22404}
1133        root:myGlobals:Attenuators:ng3att2 = {0.207506,0.1848,0.1629,0.1447,0.1292,0.1056,0.09263,0.08171,0.06656,0.0546552}
1134        root:myGlobals:Attenuators:ng3att3 = {0.092412,0.07746,0.06422,0.05379,0.04512,0.03321,0.02707,0.02237,0.01643,0.0121969}
1135        root:myGlobals:Attenuators:ng3att4 = {0.0417722,0.03302,0.02567,0.02036,0.01604,0.01067,0.00812,0.006316,0.00419,0.00282411}
1136        root:myGlobals:Attenuators:ng3att5 = {0.0187129,0.01397,0.01017,0.007591,0.005668,0.003377,0.002423,0.001771,0.001064,0.000651257}
1137        root:myGlobals:Attenuators:ng3att6 = {0.00851048,0.005984,0.004104,0.002888,0.002029,0.001098,0.0007419,0.0005141,0.000272833,0.000150624}
1138        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}
1139        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}
1140        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}
1141        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}
1142 
1143  //old tables, pre-June 2007
1144//      Make/O/N=9 root:myGlobals:Attenuators:ng3lambda={5,6,7,8,10,12,14,17,20}
1145//      root:myGlobals:Attenuators:ng3att0 = {1, 1, 1, 1, 1, 1, 1, 1,1 }
1146//      root:myGlobals:Attenuators:ng3att1 = {0.421, 0.394, 0.371, 0.349, 0.316, 0.293, 0.274, 0.245,0.220}
1147//      root:myGlobals:Attenuators:ng3att2 = {0.187, 0.164, 0.145, 0.130, 0.106, 0.0916, 0.0808, 0.0651,0.0531}
1148//      root:myGlobals:Attenuators:ng3att3 = {0.0777, 0.0636, 0.0534, 0.0446, 0.0330, 0.0262, 0.0217, 0.0157 ,0.0116}
1149//      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}
1150//      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}
1151//      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}
1152//      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}
1153//      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}
1154//      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}
1155//      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}
1156
1157End
1158
1159// new calibration done June 2007, John Barker
1160Proc MakeNG7AttenTable()
1161
1162        NewDataFolder/O root:myGlobals:Attenuators
1163       
1164        Variable num=10         //10 needed for tables after June 2007
1165       
1166        Make/O/N=(num) root:myGlobals:Attenuators:ng7att0
1167        Make/O/N=(num) root:myGlobals:Attenuators:ng7att1
1168        Make/O/N=(num) root:myGlobals:Attenuators:ng7att2
1169        Make/O/N=(num) root:myGlobals:Attenuators:ng7att3
1170        Make/O/N=(num) root:myGlobals:Attenuators:ng7att4
1171        Make/O/N=(num) root:myGlobals:Attenuators:ng7att5
1172        Make/O/N=(num) root:myGlobals:Attenuators:ng7att6
1173        Make/O/N=(num) root:myGlobals:Attenuators:ng7att7
1174        Make/O/N=(num) root:myGlobals:Attenuators:ng7att8
1175        Make/O/N=(num) root:myGlobals:Attenuators:ng7att9
1176        Make/O/N=(num) root:myGlobals:Attenuators:ng7att10
1177       
1178        //NG7 wave has 10 elements, the transmission of att# at the wavelengths
1179        //lambda =4, 5,6,7,8,10,12,14,17,20
1180        // note that some of the higher attenuations and ALL of the 4 A and 20A data is interpolated
1181        // none of these values are expected to be used in reality since the flux would be too low in practice
1182        Make/O/N=(num) root:myGlobals:Attenuators:ng7lambda={4,5,6,7,8,10,12,14,17,20}
1183
1184// New calibration, June 2007, John Barker
1185        root:myGlobals:Attenuators:ng7att0 = {1, 1, 1, 1, 1, 1, 1, 1 ,1,1}     
1186        root:myGlobals:Attenuators:ng7att1 = {0.448656,0.4192,0.3925,0.3661,0.3458,0.3098,0.2922,0.2738,0.2544,0.251352}
1187        root:myGlobals:Attenuators:ng7att2 = {0.217193,0.1898,0.1682,0.148,0.1321,0.1076,0.0957,0.08485,0.07479,0.0735965}
1188        root:myGlobals:Attenuators:ng7att3 = {0.098019,0.07877,0.06611,0.05429,0.04548,0.03318,0.02798,0.0234,0.02004,0.0202492}
1189        root:myGlobals:Attenuators:ng7att4 = {0.0426904,0.03302,0.02617,0.02026,0.0158,0.01052,0.008327,0.006665,0.005745,0.00524807}
1190        root:myGlobals:Attenuators:ng7att5 = {0.0194353,0.01398,0.01037,0.0075496,0.005542,0.003339,0.002505,0.001936,0.001765,0.00165959}
1191        root:myGlobals:Attenuators:ng7att6 = {0.00971666,0.005979,0.004136,0.002848,0.001946,0.001079,0.0007717,0.000588,0.000487337,0.000447713}
1192        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}
1193        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}
1194        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}
1195        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}
1196
1197// Pre-June 2007 calibration values - do not use these anymore 
1198////    root:myGlobals:Attenuators:ng7att0 = {1, 1, 1, 1, 1, 1, 1, 1 ,1}
1199////    root:myGlobals:Attenuators:ng7att1 = {0.418, 0.393, 0.369, 0.347, 0.313, 0.291, 0.271, 0.244, 0.219 }
1200////    root:myGlobals:Attenuators:ng7att2 = {0.189, 0.167, 0.148, 0.132, 0.109, 0.0945, 0.0830, 0.0681, 0.0560}
1201////    root:myGlobals:Attenuators:ng7att3 = {0.0784, 0.0651, 0.0541, 0.0456, 0.0340, 0.0273, 0.0223, 0.0164 , 0.0121}
1202////    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}
1203////    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}
1204////    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}
1205////    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}
1206////    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}
1207////    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}
1208////    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}
1209End
1210
1211//returns the transmission of the attenuator (at NG3) given the attenuator number
1212//which must be an integer(to select the wave) and given the wavelength.
1213//the wavelength may be any value between 4 and 20 (A), and is interpolated
1214//between calibrated wavelengths for a given attenuator
1215//
1216// Mar 2010 - abs() added to attStr to account for ICE reporting -0.0001 as an attenuator position, which truncates to "-0"
1217Function LookupAttenNG3(lambda,attenNo)
1218        Variable lambda, attenNo
1219       
1220        Variable trans
1221        String attStr="root:myGlobals:Attenuators:ng3att"+num2str(trunc(abs(attenNo)))
1222        String lamStr = "root:myGlobals:Attenuators:ng3lambda"
1223       
1224        if(attenNo == 0)
1225                return (1)              //no attenuation, return trans == 1
1226        endif
1227       
1228        if( (lambda < 4) || (lambda > 20 ) )
1229                Abort "Wavelength out of calibration range (4,20). You must manually enter the absolute parameters"
1230        Endif
1231       
1232        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) )
1233                Execute "MakeNG3AttenTable()"
1234        Endif
1235        //just in case creating the tables fails....
1236        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) )
1237                Abort "Attenuator lookup waves could not be found. You must manually enter the absolute parameters"
1238        Endif
1239       
1240        //lookup the value by interpolating the wavelength
1241        //the attenuator must always be an integer
1242        Wave att = $attStr
1243        Wave lam = $lamstr
1244        trans = interp(lambda,lam,att)
1245       
1246//      Print "trans = ",trans
1247       
1248        return trans
1249End
1250
1251//returns the transmission of the attenuator (at NG7) given the attenuator number
1252//which must be an integer(to select the wave) and given the wavelength.
1253//the wavelength may be any value between 4 and 20 (A), and is interpolated
1254//between calibrated wavelengths for a given attenuator
1255//
1256// this set of tables is also used for NG5 (NG1) SANS instrument - as the attenuator has never been calibrated
1257//
1258// local function
1259//
1260// Mar 2010 - abs() added to attStr to account for ICE reporting -0.0001 as an attenuator position, which truncates to "-0"
1261Function LookupAttenNG7(lambda,attenNo)
1262        Variable lambda, attenNo
1263       
1264        Variable trans
1265        String attStr="root:myGlobals:Attenuators:ng7att"+num2str(trunc(abs(attenNo)))
1266        String lamStr = "root:myGlobals:Attenuators:ng7lambda"
1267       
1268        if(attenNo == 0)
1269                return (1)              //no attenuation, return trans == 1
1270        endif
1271       
1272        if( (lambda < 4) || (lambda > 20 ) )
1273                Abort "Wavelength out of calibration range (4,20). You must manually enter the absolute parameters"
1274        Endif
1275       
1276        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) )
1277                Execute "MakeNG7AttenTable()"
1278        Endif
1279        //just in case creating the tables fails....
1280        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) )
1281                Abort "Attenuator lookup waves could not be found. You must manually enter the absolute parameters"
1282        Endif
1283       
1284        //lookup the value by interpolating the wavelength
1285        //the attenuator must always be an integer
1286        Wave att = $attStr
1287        Wave lam = $lamstr
1288        trans = interp(lambda,lam,att)
1289       
1290        //Print "trans = ",trans
1291       
1292        return trans
1293
1294End
1295
1296//returns the proper attenuation factor based on the instrument (NG3, NG5, or NG7)
1297//NG5 values are taken from the NG7 tables (there is very little difference in the
1298//values, and NG5 attenuators have not been calibrated (as of 8/01)
1299//
1300// filestr is passed from TextRead[3] = the default directory
1301// lam is passed from RealsRead[26]
1302// AttenNo is passed from ReaslRead[3]
1303//
1304// Attenuation factor as defined here is <= 1
1305//
1306// ORNL can pass ("",1,attenuationFactor) and have this function simply
1307// spit back the attenuationFactor (that was read into rw[3])
1308//
1309// called by Correct.ipf, ProtocolAsPanel.ipf, Transmission.ipf
1310//
1311Function AttenuationFactor(fileStr,lam,attenNo)
1312        String fileStr
1313        Variable lam,attenNo
1314       
1315        Variable attenFactor=1,loc
1316        String instr=fileStr[1,3]       //filestr is "[NGnSANSn] " or "[NGnSANSnn]" (11 characters total)
1317       
1318        strswitch(instr)
1319                case "NG3":
1320                        attenFactor = LookupAttenNG3(lam,attenNo)
1321                        break
1322                case "NG5":
1323                        //using NG7 lookup Table
1324                        attenFactor = LookupAttenNG7(lam,attenNo)
1325                        break
1326                case "NG7":
1327                        attenFactor = LookupAttenNG7(lam,attenNo)
1328                        break
1329                default:                                                       
1330                        //return error?
1331                        attenFactor=1
1332        endswitch
1333//      print "instr, lambda, attenNo,attenFactor = ",instr,lam,attenNo,attenFactor
1334        return(attenFactor)
1335End
1336
1337//function called by the popups to get a file list of data that can be sorted
1338// this procedure simply removes the raw data files from the string - there
1339//can be lots of other junk present, but this is very fast...
1340//
1341// could also use the alternate procedure of keeping only file with the proper extension
1342//
1343// another possibility is to get a listing of the text files, but is unreliable on
1344// Windows, where the data file must be .txt (and possibly OSX)
1345//
1346// called by FIT_Ops.ipf, NSORT.ipf, PlotUtils.ipf
1347//
1348Function/S ReducedDataFileList(ctrlName)
1349        String ctrlName
1350
1351        String list="",newList="",item=""
1352        Variable num,ii
1353       
1354        //check for the path
1355        PathInfo catPathName
1356        if(V_Flag==0)
1357                DoAlert 0, "Data path does not exist - pick the data path from the button on the main panel"
1358                Return("")
1359        Endif
1360       
1361        list = IndexedFile(catpathName,-1,"????")
1362       
1363        list = RemoveFromList(ListMatch(list,"*.SA1*",";"), list, ";", 0)
1364        list = RemoveFromList(ListMatch(list,"*.SA2*",";"), list, ";", 0)
1365        list = RemoveFromList(ListMatch(list,"*.SA3*",";"), list, ";", 0)
1366        list = RemoveFromList(ListMatch(list,".*",";"), list, ";", 0)
1367        list = RemoveFromList(ListMatch(list,"*.pxp",";"), list, ";", 0)
1368        list = RemoveFromList(ListMatch(list,"*.DIV",";"), list, ";", 0)
1369        list = RemoveFromList(ListMatch(list,"*.GSP",";"), list, ";", 0)
1370        list = RemoveFromList(ListMatch(list,"*.MASK",";"), list, ";", 0)
1371
1372        //remove VAX version numbers
1373        list = RemoveVersNumsFromList(List)
1374        //sort
1375        newList = SortList(List,";",0)
1376
1377        return newlist
1378End
1379
1380// returns a list of raw data files in the catPathName directory on disk
1381// - list is SEMICOLON-delimited
1382//
1383// does it the "cheap" way, simply finding the ".SAn" in the file name
1384// = does not check for proper byte length.
1385//
1386// called by PatchFiles.ipf, Tile_2D.ipf
1387//
1388Function/S GetRawDataFileList()
1389       
1390        //make sure that path exists
1391        PathInfo catPathName
1392        if (V_flag == 0)
1393                Abort "Folder path does not exist - use Pick Path button on Main Panel"
1394        Endif
1395       
1396        String list=IndexedFile(catPathName,-1,"????")
1397        String newList="",item=""
1398        Variable num=ItemsInList(list,";"),ii
1399        for(ii=0;ii<num;ii+=1)
1400                item = StringFromList(ii, list  ,";")
1401                if( stringmatch(item,"*.sa1*") )
1402                        newlist += item + ";"
1403                endif
1404                if( stringmatch(item,"*.sa2*") )
1405                        newlist += item + ";"
1406                endif
1407                if( stringmatch(item,"*.sa3*") )
1408                        newlist += item + ";"
1409                endif
1410                //print "ii=",ii
1411        endfor
1412        newList = SortList(newList,";",0)
1413        return(newList)
1414End
1415
1416// Return the filename that represents the previous or next file.
1417// Input is current filename and increment.
1418// Increment should be -1 or 1
1419// -1 => previous file
1420// 1 => next file
1421Function/S GetPrevNextRawFile(curfilename, prevnext)
1422        String curfilename
1423        Variable prevnext
1424
1425        String filename
1426       
1427        //get the run number
1428        Variable num = GetRunNumFromFile(curfilename)
1429               
1430        //find the next specified file by number
1431        fileName = FindFileFromRunNumber(num+prevnext)
1432
1433        if(cmpstr(fileName,"")==0)
1434                //null return, do nothing
1435                fileName = FindFileFromRunNumber(num)
1436        Endif
1437
1438//      print "in FU "+filename
1439
1440        Return filename
1441End
1442
1443
Note: See TracBrowser for help on using the repository browser.