source: sans/SANSReduction/branches/kline_29MAR07/Put in User Procedures/SANS_Reduction_v5.00/NCNR_Utils.ipf @ 72

Last change on this file since 72 was 70, checked in by srkline, 16 years ago

more file adjustments to clean out NCNR bits

File size: 31.3 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma version=5.0
3#pragma IgorVersion=5.0
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
10
11// initializes globals that are specific to a particular facility
12// - number of XY pixels
13// - pixexl resolution [cm]
14// - detector deadtime constant [s]
15Function InitFacilityGlobals()
16
17        //Detector -specific globals
18        Variable/G root:myGlobals:gNPixelsX=128
19        Variable/G root:myGlobals:gNPixelsY=128
20       
21        Variable/G root:myGlobals:PixelResNG3_ILL = 1.0         //pixel resolution in cm
22        Variable/G root:myGlobals:PixelResNG5_ILL = 1.0
23        Variable/G root:myGlobals:PixelResNG7_ILL = 1.0
24        Variable/G root:myGlobals:PixelResNG3_ORNL = 0.5
25        Variable/G root:myGlobals:PixelResNG5_ORNL = 0.5
26        Variable/G root:myGlobals:PixelResNG7_ORNL = 0.5
27        Variable/G root:myGlobals:PixelResDefault = 0.5
28       
29        Variable/G root:myGlobals:DeadtimeNG3_ILL = 3.0e-6              //deadtime in seconds
30        Variable/G root:myGlobals:DeadtimeNG5_ILL = 3.0e-6
31        Variable/G root:myGlobals:DeadtimeNG7_ILL = 3.0e-6
32        Variable/G root:myGlobals:DeadtimeNG3_ORNL = 3.4e-6
33        Variable/G root:myGlobals:DeadtimeNG5_ORNL = 0.6e-6             //as of 9 MAY 2002
34        Variable/G root:myGlobals:DeadtimeNG7_ORNL = 3.4e-6
35        Variable/G root:myGlobals:DeadtimeDefault = 3.4e-6
36End
37
38
39//**********************
40// Resolution calculation - used by the averaging routines
41// to calculate the resolution function at each q-value
42// - the return value is not used
43//
44// equivalent to John's routine on the VAX Q_SIGMA_AVE.FOR
45// Incorporates eqn. 3-15 from J. Appl. Cryst. (1995) v. 28 p105-114
46//
47// - 21 MAR 07 uses projected BS diameter on the detector
48//
49Function/S getResolution(inQ,lambda,lambdaWidth,DDet,apOff,S1,S2,L1,L2,BS,del_r,SigmaQ,QBar,fSubS)
50        Variable inQ, lambda, lambdaWidth, DDet, apOff, S1, S2, L1, L2, BS, del_r
51        Variable &fSubS, &QBar, &SigmaQ         //these are the output quantities at the input Q value
52       
53        //lots of calculation variables
54        Variable a2, q_small, lp, v_lambda, v_b, v_d, vz, yg, v_g
55        Variable r0, delta, inc_gamma, fr, fv, rmd, v_r1, rm, v_r
56
57        //Constants
58        //Variable del_r = .1
59        Variable vz_1 = 3.956e5         //velocity [cm/s] of 1 A neutron
60        Variable g = 981.0                              //gravity acceleration [cm/s^2]
61
62        String results
63        results ="Failure"
64
65        //rename for working variables,  these must be gotten from global
66        //variables
67
68//      Variable wLam, wLW, wL1, wL2, wS1, wS2
69//      Variable wBS, wDDet, wApOff
70//      wLam = lambda
71//      wLW = lambdaWidth
72//      wDDet = DDet
73//      wApOff = apOff
74        S1 *= 0.5*0.1                   //convert to radius and [cm]
75        S2 *= 0.5*0.1
76
77        L1 *= 100.0                     // [cm]
78        L1 -= apOff                             //correct the distance
79
80        L2 *= 100.0
81        L2 += apOff
82        del_r *= 0.1                            //width of annulus, convert mm to [cm]
83       
84        BS *= 0.5*0.1                   //nominal BS diameter passed in, convert to radius and [cm]
85        // 21 MAR 07 SRK - use the projected BS diameter, based on a point sample aperture
86        Variable LB
87        LB = 20.1 + 1.61*BS                     //distance in cm from beamstop to anode plane (empirical)
88        BS = bs + bs*lb/(l2-lb)         //adjusted diameter of shadow from parallax
89       
90        //Start resolution calculation
91        a2 = S1*L2/L1 + S2*(L1+L2)/L1
92        q_small = 2.0*Pi*(BS-a2)*(1.0-lambdaWidth)/(lambda*L2)
93        lp = 1.0/( 1.0/L1 + 1.0/L2)
94
95        v_lambda = lambdaWidth^2/6.0
96        v_b = 0.25*(S1*L2/L1)^2 +0.25*(S2*L2/lp)^2
97        v_d = (DDet/2.3548)^2 + del_r^2/12.0
98        vz = vz_1 / lambda
99        yg = 0.5*g*L2*(L1+L2)/vz^2
100        v_g = 2.0*yg^2*v_lambda
101
102        r0 = L2*tan(2.0*asin(lambda*inQ/(4.0*Pi) ))
103        delta = 0.5*(BS - r0)^2/v_d
104
105        if (r0 < BS)
106                inc_gamma=exp(gammln(1.5))*(1-gammp(1.5,delta))
107        else
108                inc_gamma=exp(gammln(1.5))*(1+gammp(1.5,delta))
109        endif
110
111        fSubS = 0.5*(1.0+erf( (r0-BS)/sqrt(2.0*v_d) ) )
112        if (fSubS <= 0.0)
113                fSubS = 1.e-10
114        endif
115        fr = 1.0 + sqrt(v_d)*exp(-1.0*delta) /(r0*fSubS*sqrt(2.0*Pi))
116        fv = inc_gamma/(fSubS*sqrt(Pi)) - r0^2*(fr-1.0)^2/v_d
117
118        rmd = fr*r0
119        v_r1 = v_b + fv*v_d +v_g
120
121        rm = rmd + 0.5*v_r1/rmd
122        v_r = v_r1 - 0.5*(v_r1/rmd)^2
123        if (v_r < 0.0)
124                v_r = 0.0
125        endif
126        QBar = (4.0*Pi/lambda)*sin(0.5*atan(rm/L2))
127        SigmaQ = QBar*sqrt(v_r/rmd^2 +v_lambda)
128
129        results = "success"
130        Return results
131End
132
133
134//Utility function that returns the detector resolution (in cm) given information
135//from the file header
136//Global values are set in the Initialize procedure
137Function DetectorPixelResolution(fileStr,detStr)
138        String fileStr,detStr
139       
140        Variable DDet
141        String instr=fileStr[1,3]       //filestr is "[NGnSANSn] " or "[NGnSANSnn]" (11 characters total)
142       
143        NVAR PixelResNG3_ILL = root:myGlobals:PixelResNG3_ILL           //pixel resolution in cm
144        NVAR PixelResNG5_ILL = root:myGlobals:PixelResNG5_ILL
145        NVAR PixelResNG7_ILL = root:myGlobals:PixelResNG7_ILL
146        NVAR PixelResNG3_ORNL = root:myGlobals:PixelResNG3_ORNL
147        NVAR PixelResNG5_ORNL = root:myGlobals:PixelResNG5_ORNL
148        NVAR PixelResNG7_ORNL = root:myGlobals:PixelResNG7_ORNL
149        NVAR PixelResDefault = root:myGlobals:PixelResDefault
150       
151        strswitch(instr)
152                case "NG3":
153                        if(cmpstr(detStr, "ILL   ") == 0 )
154                                DDet= PixelResNG3_ILL
155                        else
156                                DDet = PixelResNG3_ORNL //detector is ordella-type
157                        endif
158                        break
159                case "NG5":
160                        if(cmpstr(detStr, "ILL   ") == 0 )
161                                DDet= PixelResNG5_ILL
162                        else
163                                DDet = PixelResNG5_ORNL //detector is ordella-type
164                        endif
165                        break
166                case "NG7":
167                        if(cmpstr(detStr, "ILL   ") == 0 )
168                                DDet= PixelResNG7_ILL
169                        else
170                                DDet = PixelResNG7_ORNL //detector is ordella-type
171                        endif
172                        break
173                default:                                                       
174                        //return error?
175                        DDet = PixelResDefault  //5mm, typical for new ORNL detectors
176        endswitch
177       
178        return(DDet)
179End
180
181//Utility function that returns the detector deadtime (in seconds) given information
182//from the file header
183//Global values are set in the Initialize procedure
184Function DetectorDeadtime(fileStr,detStr)
185        String fileStr,detStr
186       
187        Variable deadtime
188        String instr=fileStr[1,3]       //filestr is "[NGnSANSn] " or "[NGnSANSnn]" (11 characters total)
189       
190        NVAR DeadtimeNG3_ILL = root:myGlobals:DeadtimeNG3_ILL           //pixel resolution in cm
191        NVAR DeadtimeNG5_ILL = root:myGlobals:DeadtimeNG5_ILL
192        NVAR DeadtimeNG7_ILL = root:myGlobals:DeadtimeNG7_ILL
193        NVAR DeadtimeNG3_ORNL = root:myGlobals:DeadtimeNG3_ORNL
194        NVAR DeadtimeNG5_ORNL = root:myGlobals:DeadtimeNG5_ORNL
195        NVAR DeadtimeNG7_ORNL = root:myGlobals:DeadtimeNG7_ORNL
196        NVAR DeadtimeDefault = root:myGlobals:DeadtimeDefault
197       
198        strswitch(instr)
199                case "NG3":
200                        if(cmpstr(detStr, "ILL   ") == 0 )
201                                deadtime= DeadtimeNG3_ILL
202                        else
203                                deadtime = DeadtimeNG3_ORNL     //detector is ordella-type
204                        endif
205                        break
206                case "NG5":
207                        if(cmpstr(detStr, "ILL   ") == 0 )
208                                deadtime= DeadtimeNG5_ILL
209                        else
210                                deadtime = DeadtimeNG5_ORNL     //detector is ordella-type
211                        endif
212                        break
213                case "NG7":
214                        if(cmpstr(detStr, "ILL   ") == 0 )
215                                deadtime= DeadtimeNG7_ILL
216                        else
217                                deadtime = DeadtimeNG7_ORNL     //detector is ordella-type
218                        endif
219                        break
220                default:                                                       
221                        //return error?
222                        deadtime = DeadtimeDefault      //1e-6 seconds, typical for new ORNL detectors
223        endswitch
224       
225        return(deadtime)
226End
227
228
229/////VAX filename/Run number parsing utilities
230//**************************
231// Vers. 1.2 092101
232//
233//this file is a collection of uilities for processing vax filenames
234//and processing lists (especially for display in popup menus)
235//
236//required to correctly account for VAX supplied version numbers, which
237//may or may not be removed by the ftp utility
238//
239// - parses lists of run numbers into real filenames
240// - selects proper detector constants
241//
242//**************************
243
244
245//given a filename of a SANS data filename of the form
246//TTTTTnnn.SAn_TTT_Txxx
247//returns the run number "nnn" as a number
248//returns -1 as an invalid file number
249Function GetRunNumFromFile(item)
250        String item
251        Variable invalid = -1   //negative numbers are invalid
252        Variable num=-1
253       
254        //find the "dot"
255        String runStr=""
256        Variable pos = strsearch(item,".",0)
257        if(pos == -1)
258                //"dot" not found
259                return (invalid)
260        else
261                //found, get the three characters preceeding it
262                if (pos <=2)
263                        //not enough characters
264                        return (invalid)
265                else
266                        runStr = item[pos-3,pos-1]
267                        //convert to a number
268                        num = str2num(runStr)
269                        //if valid, return it
270                        if (num == NaN)
271                                //3 characters were not a number
272                                return (invalid)
273                        else
274                                //run was OK
275                                return (num)
276                        Endif
277                Endif
278        Endif
279End
280
281//given a filename of a SANS data filename of the form
282//TTTTTnnn.SAn_TTT_Txxx
283//returns the run number "nnn" as a STRING of THREE characters
284//returns "ABC" as an invalid file number
285Function/S GetRunNumStrFromFile(item)
286        String item
287        String invalid = "ABC"  //"ABC" is not a valid run number, since it's text
288        Variable num=-1
289       
290        //find the "dot"
291        String runStr=""
292        Variable pos = strsearch(item,".",0)
293        if(pos == -1)
294                //"dot" not found
295                return (invalid)
296        else
297                //found, get the three characters preceeding it
298                if (pos <=2)
299                        //not enough characters
300                        return (invalid)
301                else
302                        runStr = item[pos-3,pos-1]
303                        return (runStr)
304                Endif
305        Endif
306End
307
308//returns a string containing the full path and file to the file containing the
309//run number "num". The null string is returned if no valid file can be found
310//the path "catPathName" used and is hard-wired, will abort if this path does not exist
311//the file returned will be a RAW SANS data file, other types of files are
312//filtered out.
313Function/S FindFileFromRunNumber(num)
314        Variable num
315       
316        String fullName="",partialName="",item=""
317        //get list of raw data files in folder that match "num" (add leading zeros)
318        if( (num>999) || (num<=0) )
319                //Print "error in  FindFileFromRunNumber(num), file number too large or too small"
320                Return ("")
321        Endif
322        //make a three character string of the run number
323        String numStr=""
324        if(num<10)
325                numStr = "00"+num2str(num)
326        else
327                if(num<100)
328                        numStr = "0"+num2str(num)
329                else
330                        numStr = num2str(num)
331                Endif
332        Endif
333        //Print "numstr = ",numstr
334       
335        //make sure that path exists
336        PathInfo catPathName
337        String path = S_path
338        if (V_flag == 0)
339                Abort "folder path does not exist - use Pick Path button"
340        Endif
341        String list="",newList="",testStr=""
342       
343        list = IndexedFile(catPathName,-1,"????")       //get all files in folder
344        //find (the) one with the number in the run # location in the name
345        Variable numItems,ii,runFound,isRAW
346        numItems = ItemsInList(list,";")                //get the new number of items in the list
347        ii=0
348        do
349                //parse through the list in this order:
350                // 1 - does item contain run number (as a string) "TTTTTnnn.SAn_XXX_Tyyy"
351                // 2 - exclude by isRaw? (to minimize disk access)
352                item = StringFromList(ii, list  ,";" )
353                if(strlen(item) != 0)
354                        //find the run number, if it exists as a three character string
355                        testStr = GetRunNumStrFromFile(item)
356                        runFound= cmpstr(numStr,testStr)        //compare the three character strings, 0 if equal
357                        if(runFound == 0)
358                                //the run Number was found
359                                //build valid filename
360                                partialName = FindValidFileName(item)
361                                if(strlen(partialName) != 0)            //non-null return from FindValidFileName()
362                                        fullName = path + partialName
363                                        //check if RAW, if so,this must be the file!
364                                        isRAW = CheckIfRawData(fullName)
365                                        if(isRaw)
366                                                //stop here
367                                                return(fullname)
368                                        Endif
369                                Endif
370                        Endif
371                Endif
372                ii+=1
373        while(ii<numItems)              //process all items in list
374        Return ("")     //null return if file not found in list
375End
376
377//function to test a binary file to see if it is a RAW binary SANS file
378//first checks the total bytes in the file (which for raw data is 33316 bytes)
379//**note that the "DIV" file will also show up as a raw file by the run field
380//should be listed in CAT/SHORT and in patch windows
381//
382//Function then checks the file fname (full path:file) for "RAW" run.type field
383//if not found, the data is not raw data and zero is returned
384Function CheckIfRawData(fname)
385        String fname
386       
387        Variable refnum,totalBytes
388        String testStr=""
389       
390        Open/R/T="????TEXT" refNum as fname
391        //get the total number of bytes in the file, to avoid moving past EOF
392        FStatus refNum
393        totalBytes = V_logEOF
394        //Print totalBytes
395        if(totalBytes!=33316)
396                //can't possibly be a raw data file
397                Close refnum
398                return(0)               //not a raw SANS file
399        Endif
400        FSetPos refNum,75
401        FReadLine/N=3 refNum,testStr
402        Close refNum
403       
404        if(cmpstr(testStr,"RAW")==0)
405                //true, is raw data file
406                Return(1)
407        else
408                //some other file
409                Return(0)
410        Endif
411End
412
413//function to remove all spaces from names when searching for filenames
414//the filename (as saved) will never have interior spaces (TTTTTnnn_AB _Bnnn)
415//but the text field in the header WILL, if less than 3 characters were used for the
416//user's initials, and can have leading spaces if prefix was less than 5 characters
417//
418//returns a string identical to the original string, except with the interior spaces removed
419//
420Function/S RemoveAllSpaces(str)
421        String str
422       
423        String tempstr = str
424        Variable ii,spc,len             //should never be more than 2 or 3 trailing spaces in a filename
425        ii=0
426        do
427                len = strlen(tempStr)
428                spc = strsearch(tempStr," ",0)          //is the last character a space?
429                if (spc == -1)
430                        break           //no more spaces found, get out
431                endif
432                str = tempstr
433                tempStr = str[0,(spc-1)] + str[(spc+1),(len-1)] //remove the space from the string
434        While(1)        //should never be more than 2 or 3
435       
436        If(strlen(tempStr) < 1)
437                tempStr = ""            //be sure to return a null string if problem found
438        Endif
439       
440        //Print strlen(tempstr)
441       
442        Return(tempStr)
443               
444End
445
446
447//Function attempts to find valid filename from partial name that has been stripped of
448//the VAX version number. The partial name is tried first
449//*** the PATH is hard-wired to catPathName (which is assumed to exist)
450//version numers up to ;10 are tried
451//only the "name;vers" is returned. the path is not prepended, hence the return string
452//is not a complete specification of the file
453//
454// added 11/99 - uppercase and lowercase versions of the file are tried, if necessary
455// since from marquee, the filename field (textread[0]) must be used, and can be a mix of
456// upper/lowercase letters, while the filename on the server (should) be all caps
457// now makes repeated calls to ValidFileString()
458//
459Function/S FindValidFilename(partialName)
460        String PartialName
461       
462        String retStr=""
463       
464        //try name with no changes - to allow for ABS files that have spaces in the names 12APR04
465        retStr = ValidFileString(partialName)
466        if(cmpstr(retStr,"") !=0)
467                //non-null return
468                return(retStr)
469        Endif
470       
471        //if the partial name is derived from the file header, there can be spaces at the beginning
472        //or in the middle of the filename - depending on the prefix and initials used
473        //
474        //remove any leading spaces from the name before starting
475        partialName = RemoveAllSpaces(partialName)
476       
477        //try name with no spaces
478        retStr = ValidFileString(partialName)
479        if(cmpstr(retStr,"") !=0)
480                //non-null return
481                return(retStr)
482        Endif
483       
484        //try all UPPERCASE
485        partialName = UpperStr(partialName)
486        retStr = ValidFileString(partialName)
487        if(cmpstr(retStr,"") !=0)
488                //non-null return
489                return(retStr)
490        Endif
491       
492        //try all lowercase (ret null if failure)
493        partialName = LowerStr(partialName)
494        retStr = ValidFileString(partialName)
495        if(cmpstr(retStr,"") !=0)
496                //non-null return
497                return(retStr)
498        else
499                return(retStr)
500        Endif
501End
502
503//Function attempts to find valid filename from partial name that has been stripped of
504//the VAX version number. The partial name is tried first
505//*** the PATH is hard-wired to catPathName (which is assumed to exist)
506//version numers up to ;10 are tried
507//only the "name;vers" is returned. the path is not prepended, hence the return string
508//is not a complete specification of the file
509//
510Function/S ValidFileString(partialName)
511        String partialName
512       
513        String tempName = "",msg=""
514        Variable ii,refnum
515       
516        ii=0
517        do
518                if(ii==0)
519                        //first pass, try the partialName
520                        tempName = partialName
521                        Open/Z/R/T="????TEXT"/P=catPathName refnum tempName     //Does open file (/Z flag)
522                        if(V_flag == 0)
523                                //file exists
524                                Close refnum            //YES needed,
525                                break
526                        endif
527                else
528                        tempName = partialName + ";" + num2str(ii)
529                        Open/Z/R/T="????TEXT"/P=catPathName refnum tempName
530                        if(V_flag == 0)
531                                //file exists
532                                Close refnum
533                                break
534                        endif
535                Endif
536                ii+=1
537                //print "ii=",ii
538        while(ii<11)
539        //go get the selected bits of information, using tempName, which exists
540        if(ii>=11)
541                //msg = partialName + " not found. is version number > 11?"
542                //DoAlert 0, msg
543                //PathInfo catPathName
544                //Print S_Path
545                Return ("")             //use null string as error condition
546        Endif
547       
548        Return (tempName)
549End
550
551
552
553
554//returns a string containing filename (WITHOUT the ;vers)
555//the input string is a full path to the file (Mac-style, still works on Win in IGOR)
556//with the folders separated by colons
557Function/S GetFileNameFromPathNoSemi(fullPath)
558        String fullPath
559       
560        Variable offset1,offset2
561        String filename=""
562        //String PartialPath
563        offset1 = 0
564        do
565                offset2 = StrSearch(fullPath, ":", offset1)
566                if (offset2 == -1)                              // no more colons ?
567                        fileName = FullPath[offset1,strlen(FullPath) ]
568                        //PartialPath = FullPath[0, offset1-1]
569                        break
570                endif
571                offset1 = offset2+1
572        while (1)
573       
574        //remove version number from name, if it's there - format should be: filename;N
575        filename =  StringFromList(0,filename,";")              //returns null if error
576       
577        Return filename
578End
579
580//returns a string containing filename (INCLUDING the ;vers)
581//the input string is a full path to the file (Mac-style, still works on Win in IGOR)
582//with the folders separated by colons
583Function/S GetFileNameFromPathKeepSemi(fullPath)
584        String fullPath
585       
586        Variable offset1,offset2
587        String filename
588        //String PartialPath
589        offset1 = 0
590        do
591                offset2 = StrSearch(fullPath, ":", offset1)
592                if (offset2 == -1)                              // no more colons ?
593                        fileName = FullPath[offset1,strlen(FullPath) ]
594                        //PartialPath = FullPath[0, offset1-1]
595                        break
596                endif
597                offset1 = offset2+1
598        while (1)
599       
600        //keep version number from name, if it's there - format should be: filename;N
601       
602        Return filename
603End
604
605//given the full path and filename (fullPath), strips the data path
606//(Mac-style, separated by colons) and returns this path
607//this partial path is the same string that would be returned from PathInfo, for example
608Function/S GetPathStrFromfullName(fullPath)
609        String fullPath
610       
611        Variable offset1,offset2
612        //String filename
613        String PartialPath
614        offset1 = 0
615        do
616                offset2 = StrSearch(fullPath, ":", offset1)
617                if (offset2 == -1)                              // no more colons ?
618                        //fileName = FullPath[offset1,strlen(FullPath) ]
619                        PartialPath = FullPath[0, offset1-1]
620                        break
621                endif
622                offset1 = offset2+1
623        while (1)
624       
625        Return PartialPath
626End
627
628//given the VAX filename, pull off the first 8 characters to make a valid
629//file string that can be used for naming averaged 1-d files
630Function/S GetNameFromHeader(fullName)
631        String fullName
632        String temp, newName = ""
633        Variable spc,ii=0
634       
635        //filename is 20 characters NNNNNxxx.SAn_NNN_NNN
636        //want the first 8 characters, NNNNNxxx, then strip off any spaces at the beginning
637        //NNNNN was entered as less than 5 characters
638        //returns a null string if no name can be found
639        do
640                temp = fullname[ii,7]           //characters ii,7 of the name
641                spc = strsearch(temp," ",0)
642                if (spc == -1)
643                        break           //no more spaces found
644                endif
645                ii+=1
646        While(ii<8)
647       
648        If(strlen(temp) < 1)
649                newName = ""            //be sure to return a null string if problem found
650        else
651                newName = temp
652        Endif
653       
654        Return(newName)
655End
656
657//list (input) is a list, typically returned from IndexedFile()
658//which is semicolon-delimited, and may contain filesnames from the VAX
659//that contain version numbers, where the version number appears as a separate list item
660//(and also as a non-existent file)
661//these numbers must be purged from the list, especially for display in a popup
662//or list processing of filenames
663//the function returns the list, cleaned of version numbers (up to 11)
664//raw data files will typically never have a version number other than 1.
665Function/S RemoveVersNumsFromList(list)
666        String list
667       
668        //get rid of version numbers first (up to 11)
669        Variable ii,num
670        String item
671        num = ItemsInList(list,";")
672        ii=1
673        do
674                item = num2str(ii)
675                list = RemoveFromList(item, list ,";" )
676                ii+=1
677        while(ii<12)
678       
679        return (list)
680End
681
682//input is a list of run numbers, and output is a list of filenames (not the full path)
683//*** input list must be COMMA delimited***
684//output is equivalent to selecting from the CAT table
685//if some or all of the list items are valid filenames, keep them...
686//if an error is encountered, notify of the offending element and return the null list
687//
688//output is COMMA delimited
689//
690// this routine is expecting that the "ask", "none" special cases are handled elsewhere
691//and not passed here
692Function/S ParseRunNumberList(list)
693        String list
694       
695        String newList="",item="",tempStr=""
696        Variable num,ii,runNum
697       
698        //expand number ranges, if any
699        list = ExpandNumRanges(list)
700       
701        num=itemsinlist(list,",")
702       
703        for(ii=0;ii<num;ii+=1)
704                //get the item
705                item = StringFromList(ii,list,",")
706                //is it already a valid filename?
707                tempStr=FindValidFilename(item) //returns filename if good, null if error
708                if(strlen(tempstr)!=0)
709                        //valid name, add to list
710                        //Print "it's a file"
711                        newList += tempStr + ","
712                else
713                        //not a valid name
714                        //is it a number?
715                        runNum=str2num(item)
716                        //print runnum
717                        if(numtype(runNum) != 0)
718                                //not a number -  maybe an error                       
719                                DoAlert 0,"List item "+item+" is not a valid run number or filename. Please enter a valid number or filename."
720                                return("")
721                        else
722                                //a run number or an error
723                                tempStr = GetFileNameFromPathNoSemi( FindFileFromRunNumber(runNum) )
724                                if(strlen(tempstr)==0)
725                                        //file not found, error
726                                        DoAlert 0,"List item "+item+" is not a valid run number. Please enter a valid number."
727                                        return("")
728                                else
729                                        newList += tempStr + ","
730                                endif
731                        endif
732                endif
733        endfor          //loop over all items in list
734       
735        return(newList)
736End
737
738//takes a comma delimited list that MAY contain number range, and
739//expands any range of run numbers into a comma-delimited list...
740//and returns the new list - if not a range, return unchanged
741Function/S ExpandNumRanges(list)
742        String list
743       
744        String newList="",dash="-",item,str
745        Variable num,ii,hasDash
746       
747        num=itemsinlist(list,",")
748//      print num
749        for(ii=0;ii<num;ii+=1)
750                //get the item
751                item = StringFromList(ii,list,",")
752                //does it contain a dash?
753                hasDash = strsearch(item,dash,0)                //-1 if no dash found
754                if(hasDash == -1)
755                        //not a range, keep it in the list
756                        newList += item + ","
757                else
758                        //has a dash (so it's a range), expand (or add null)
759                        newList += ListFromDash(item)           
760                endif
761        endfor
762       
763        return newList
764End
765
766//be sure to add a trailing comma to the return string...
767Function/S ListFromDash(item)
768        String item
769       
770        String numList="",loStr="",hiStr=""
771        Variable lo,hi,ii
772       
773        loStr=StringFromList(0,item,"-")        //treat the range as a list
774        hiStr=StringFromList(1,item,"-")
775        lo=str2num(loStr)
776        hi=str2num(hiStr)
777        if( (numtype(lo) != 0) || (numtype(hi) !=0 ) || (lo > hi) )
778                numList=""
779                return numList
780        endif
781        for(ii=lo;ii<=hi;ii+=1)
782                numList += num2str(ii) + ","
783        endfor
784       
785        Return numList
786End
787
788
789////////Transmission
790//******************
791//lookup tables for attenuator transmissions
792//NG3 and NG7 attenuators are physically different, so the transmissions are slightly different
793//NG1 - (8m SANS) is not supported
794
795Proc MakeNG3AttenTable()
796
797        NewDataFolder/O root:myGlobals:Attenuators
798        //do explicitly to avoid data folder problems, redundant, but it must work without fail
799        Make/O/N=9 root:myGlobals:Attenuators:ng3att0
800        Make/O/N=9 root:myGlobals:Attenuators:ng3att1
801        Make/O/N=9 root:myGlobals:Attenuators:ng3att2
802        Make/O/N=9 root:myGlobals:Attenuators:ng3att3
803        Make/O/N=9 root:myGlobals:Attenuators:ng3att4
804        Make/O/N=9 root:myGlobals:Attenuators:ng3att5
805        Make/O/N=9 root:myGlobals:Attenuators:ng3att6
806        Make/O/N=9 root:myGlobals:Attenuators:ng3att7
807        Make/O/N=9 root:myGlobals:Attenuators:ng3att8
808        Make/O/N=9 root:myGlobals:Attenuators:ng3att9
809        Make/O/N=9 root:myGlobals:Attenuators:ng3att10
810       
811        //each wave has 8 elements, the transmission of att# at the wavelengths
812        //lambda = 5,6,7,8,10,12,14,17
813        Make/O/N=9 root:myGlobals:Attenuators:ng3lambda={5,6,7,8,10,12,14,17,20}
814        root:myGlobals:Attenuators:ng3att0 = {1, 1, 1, 1, 1, 1, 1, 1,1 }
815        root:myGlobals:Attenuators:ng3att1 = {0.421, 0.394, 0.371, 0.349, 0.316, 0.293, 0.274, 0.245,0.220}
816        root:myGlobals:Attenuators:ng3att2 = {0.187, 0.164, 0.145, 0.130, 0.106, 0.0916, 0.0808, 0.0651,0.0531}
817        root:myGlobals:Attenuators:ng3att3 = {0.0777, 0.0636, 0.0534, 0.0446, 0.0330, 0.0262, 0.0217, 0.0157 ,0.0116}
818        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}
819        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}
820        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}
821        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}
822        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}
823        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}
824        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}
825
826End
827
828Proc MakeNG7AttenTable()
829
830        NewDataFolder/O root:myGlobals:Attenuators
831       
832        Make/O/N=9 root:myGlobals:Attenuators:ng7att0
833        Make/O/N=9 root:myGlobals:Attenuators:ng7att1
834        Make/O/N=9 root:myGlobals:Attenuators:ng7att2
835        Make/O/N=9 root:myGlobals:Attenuators:ng7att3
836        Make/O/N=9 root:myGlobals:Attenuators:ng7att4
837        Make/O/N=9 root:myGlobals:Attenuators:ng7att5
838        Make/O/N=9 root:myGlobals:Attenuators:ng7att6
839        Make/O/N=9 root:myGlobals:Attenuators:ng7att7
840        Make/O/N=9 root:myGlobals:Attenuators:ng7att8
841        Make/O/N=9 root:myGlobals:Attenuators:ng7att9
842        Make/O/N=9 root:myGlobals:Attenuators:ng7att10
843       
844        //each wave has 8 elements, the transmission of att# at the wavelengths
845        //lambda = 5,6,7,8,10,12,14,17
846        Make/O/N=9 root:myGlobals:Attenuators:ng7lambda={5,6,7,8,10,12,14,17,20}
847        root:myGlobals:Attenuators:ng7att0 = {1, 1, 1, 1, 1, 1, 1, 1 ,1}
848        root:myGlobals:Attenuators:ng7att1 = {0.418, 0.393, 0.369, 0.347, 0.313, 0.291, 0.271, 0.244, 0.219 }
849        root:myGlobals:Attenuators:ng7att2 = {0.189, 0.167, 0.148, 0.132, 0.109, 0.0945, 0.0830, 0.0681, 0.0560}
850        root:myGlobals:Attenuators:ng7att3 = {0.0784, 0.0651, 0.0541, 0.0456, 0.0340, 0.0273, 0.0223, 0.0164 , 0.0121}
851        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}
852        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}
853        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}
854        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}
855        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}
856        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}
857        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}
858       
859End
860
861//returns the transmission of the attenuator (at NG3) given the attenuator number
862//which must be an integer(to select the wave) and given the wavelength.
863//the wavelength may be any value between 5 and 20 (A), and is interpolated
864//between calibrated wavelengths for a given attenuator
865Function LookupAttenNG3(lambda,attenNo)
866        Variable lambda, attenNo
867       
868        Variable trans
869        String attStr="root:myGlobals:Attenuators:ng3att"+num2str(trunc(attenNo))
870        String lamStr = "root:myGlobals:Attenuators:ng3lambda"
871       
872        if(attenNo == 0)
873                return (1)              //no attenuation, return trans == 1
874        endif
875       
876        if( (lambda < 5) || (lambda > 20 ) )
877                Abort "Wavelength out of calibration range (5,20). You must manually enter the absolute parameters"
878        Endif
879       
880        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) )
881                Execute "MakeNG3AttenTable()"
882        Endif
883        //just in case creating the tables fails....
884        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) )
885                Abort "Attenuator lookup waves could not be found. You must manually enter the absolute parameters"
886        Endif
887       
888        //lookup the value by interpolating the wavelength
889        //the attenuator must always be an integer
890        Wave att = $attStr
891        Wave lam = $lamstr
892        trans = interp(lambda,lam,att)
893       
894//      Print "trans = ",trans
895       
896        return trans
897End
898
899//returns the transmission of the attenuator (at NG7) given the attenuator number
900//which must be an integer(to select the wave) and given the wavelength.
901//the wavelength may be any value between 5 and 20 (A), and is interpolated
902//between calibrated wavelengths for a given attenuator
903//
904// this set of tables is also used for NG5 (NG1) SANS instrument - as the attenuator has yet to be calibrated
905Function LookupAttenNG7(lambda,attenNo)
906        Variable lambda, attenNo
907       
908        Variable trans
909        String attStr="root:myGlobals:Attenuators:ng7att"+num2str(trunc(attenNo))
910        String lamStr = "root:myGlobals:Attenuators:ng7lambda"
911       
912        if(attenNo == 0)
913                return (1)              //no attenuation, return trans == 1
914        endif
915       
916        if( (lambda < 5) || (lambda > 20 ) )
917                Abort "Wavelength out of calibration range (5,20). You must manually enter the absolute parameters"
918        Endif
919       
920        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) )
921                Execute "MakeNG7AttenTable()"
922        Endif
923        //just in case creating the tables fails....
924        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) )
925                Abort "Attenuator lookup waves could not be found. You must manually enter the absolute parameters"
926        Endif
927       
928        //lookup the value by interpolating the wavelength
929        //the attenuator must always be an integer
930        Wave att = $attStr
931        Wave lam = $lamstr
932        trans = interp(lambda,lam,att)
933       
934        //Print "trans = ",trans
935       
936        return trans
937
938End
939
940//returns the proper attenuation factor based on the instrument (NG3, NG5, or NG7)
941//NG5 values are taken from the NG7 tables (there is very little difference in the
942//values, and NG5 attenuators have not been calibrated (as of 8/01)
943//filestr as passed is textread[3], the default directory
944Function AttenuationFactor(fileStr,lam,attenNo)
945        String fileStr
946        Variable lam,attenNo
947       
948        Variable attenFactor=1,loc
949        String instr=fileStr[1,3]       //filestr is "[NGnSANSn] " or "[NGnSANSnn]" (11 characters total)
950       
951        strswitch(instr)
952                case "NG3":
953                        attenFactor = LookupAttenNG3(lam,attenNo)
954                        break
955                case "NG5":
956                        //using NG7 lookup Table
957                        attenFactor = LookupAttenNG7(lam,attenNo)
958                        break
959                case "NG7":
960                        attenFactor = LookupAttenNG7(lam,attenNo)
961                        break
962                default:                                                       
963                        //return error?
964                        attenFactor=1
965        endswitch
966//      print "instr, lambda, attenNo,attenFactor = ",instr,lam,attenNo,attenFactor
967        return(attenFactor)
968End
969
970//function called byt the popups to get a file list of data that can be sorted
971// this procedure simply removes the raw data files from the string - there
972//can be lots of other junk present, but this is very fast...
973//
974// could also use the alternate procedure of keeping only file with the proper extension
975//
976// another possibility is to get a listing of the text files, but is unreliable on
977// Windows, where the data file must be .txt (and possibly OSX)
978//
979Function/S ReducedDataFileList(ctrlName)
980        String ctrlName
981
982        String list="",newList="",item=""
983        Variable num,ii
984       
985        //check for the path
986        PathInfo catPathName
987        if(V_Flag==0)
988                DoAlert 0, "Data path does not exist - pick the data path from the button on the main panel"
989                Return("")
990        Endif
991       
992        list = IndexedFile(catpathName,-1,"????")
993        num=ItemsInList(list,";")
994        //print "num = ",num
995        for(ii=(num-1);ii>=0;ii-=1)
996                item = StringFromList(ii, list  ,";")
997                //simply remove all that are not raw data files (SA1 SA2 SA3)
998                if( !stringmatch(item,"*.SA1*") && !stringmatch(item,"*.SA2*") && !stringmatch(item,"*.SA3*") )
999                        if( !stringmatch(item,".*") && !stringmatch(item,"*.pxp") && !stringmatch(item,"*.DIV"))                //eliminate mac "hidden" files, pxp, and div files
1000                                newlist += item + ";"
1001                        endif
1002                endif
1003        endfor
1004        //remove VAX version numbers
1005        newList = RemoveVersNumsFromList(newList)
1006        //sort
1007        newList = SortList(newList,";",0)
1008
1009        return newlist
1010End
Note: See TracBrowser for help on using the repository browser.