source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/SANS/ILL_Utils.ipf @ 750

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

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

  • Property svn:executable set to *
File size: 26.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                                 // number of X and Y pixels
24        Variable/G root:myGlobals:gNPixelsY=128
25       
26        // pixel dimensions are now read directly from the file header.
27//      Variable/G root:myGlobals:PixelResDefault = 0.5                 //pixel resolution in cm
28       
29        Variable/G root:myGlobals:DeadtimeDefault =  (0.9e-6 )/128              //deadtime in seconds
30       
31        Variable/G root:myGlobals:BeamstopYTol = 100   
32
33        Variable/G root:myGlobals:apOff = 5.0           // (cm) distance from sample aperture to sample position
34
35        // changing behavior specific to ILL correction of data
36        Variable/G root:myGlobals:gDoDetectorEffCorr = 1                                //default state is ==1
37
38
39End
40
41
42//**********************
43// Resolution calculation - used by the averaging routines
44// to calculate the resolution function at each q-value
45// - the return value is not used
46//
47// equivalent to John's routine on the VAX Q_SIGMA_AVE.FOR
48// Incorporates eqn. 3-15 from J. Appl. Cryst. (1995) v. 28 p105-114
49//
50// - 21 MAR 07 uses projected BS diameter on the detector
51// - APR 07 still need to add resolution with lenses. currently there is no flag in the
52//          raw data header to indicate the presence of lenses.
53//
54// - Aug 07 - added input to switch calculation based on lenses (==1 if in)
55//
56// - called by CircSectAvg.ipf and RectAnnulAvg.ipf
57//
58// passed values are read from RealsRead
59// except DDet and apOff, which are set from globals before passing
60//
61//
62Function/S getResolution(inQ,lambda,lambdaWidth,DDet,apOff,S1,S2,L1,L2,BS,del_r,usingLenses,SigmaQ,QBar,fSubS)
63        Variable inQ, lambda, lambdaWidth, DDet, apOff, S1, S2, L1, L2, BS, del_r,usingLenses
64        Variable &fSubS, &QBar, &SigmaQ         //these are the output quantities at the input Q value
65       
66        //lots of calculation variables
67        Variable a2, q_small, lp, v_lambda, v_b, v_d, vz, yg, v_g
68        Variable r0, delta, inc_gamma, fr, fv, rmd, v_r1, rm, v_r
69
70        //Constants
71        Variable vz_1 = 3.956e5         //velocity [cm/s] of 1 A neutron
72        Variable g = 981.0                              //gravity acceleration [cm/s^2]
73
74        String results
75        results ="Failure"
76
77        S1 *= 0.5*0.1                   //convert to radius and [cm]
78        S2 *= 0.5*0.1
79
80        L1 *= 100.0                     // [cm]
81        L1 -= apOff                             //correct the distance
82
83        L2 *= 100.0
84        L2 += apOff
85        del_r *= 0.1                            //width of annulus, convert mm to [cm]
86       
87        BS *= 0.5*0.1                   //nominal BS diameter passed in, convert to radius and [cm]
88        // 21 MAR 07 SRK - use the projected BS diameter, based on a point sample aperture
89        Variable LB
90        LB = 20.1 + 1.61*BS                     //distance in cm from beamstop to anode plane (empirical)
91        BS = bs + bs*lb/(l2-lb)         //adjusted diameter of shadow from parallax
92       
93        //Start resolution calculation
94        a2 = S1*L2/L1 + S2*(L1+L2)/L1
95        q_small = 2.0*Pi*(BS-a2)*(1.0-lambdaWidth)/(lambda*L2)
96        lp = 1.0/( 1.0/L1 + 1.0/L2)
97
98        v_lambda = lambdaWidth^2/6.0
99       
100//      if(usingLenses==1)                      //SRK 2007
101        if(usingLenses != 0)                    //SRK 2008 allows for the possibility of different numbers of lenses in header
102                v_b = 0.25*(S1*L2/L1)^2 +0.25*(2/3)*(lambdaWidth/lambda)^2*(S2*L2/lp)^2         //correction to 2nd term
103        else
104                v_b = 0.25*(S1*L2/L1)^2 +0.25*(S2*L2/lp)^2              //original form
105        endif
106       
107        v_d = (DDet/2.3548)^2 + del_r^2/12.0
108        vz = vz_1 / lambda
109        yg = 0.5*g*L2*(L1+L2)/vz^2
110        v_g = 2.0*(2.0*yg^2*v_lambda)                                   //factor of 2 correction, B. Hammouda, 2007
111
112        r0 = L2*tan(2.0*asin(lambda*inQ/(4.0*Pi) ))
113        delta = 0.5*(BS - r0)^2/v_d
114
115        if (r0 < BS)
116                inc_gamma=exp(gammln(1.5))*(1-gammp(1.5,delta))
117        else
118                inc_gamma=exp(gammln(1.5))*(1+gammp(1.5,delta))
119        endif
120
121        fSubS = 0.5*(1.0+erf( (r0-BS)/sqrt(2.0*v_d) ) )
122        if (fSubS <= 0.0)
123                fSubS = 1.e-10
124        endif
125        fr = 1.0 + sqrt(v_d)*exp(-1.0*delta) /(r0*fSubS*sqrt(2.0*Pi))
126        fv = inc_gamma/(fSubS*sqrt(Pi)) - r0^2*(fr-1.0)^2/v_d
127
128        rmd = fr*r0
129        v_r1 = v_b + fv*v_d +v_g
130
131        rm = rmd + 0.5*v_r1/rmd
132        v_r = v_r1 - 0.5*(v_r1/rmd)^2
133        if (v_r < 0.0)
134                v_r = 0.0
135        endif
136        QBar = (4.0*Pi/lambda)*sin(0.5*atan(rm/L2))
137        SigmaQ = QBar*sqrt(v_r/rmd^2 +v_lambda)
138
139        results = "success"
140        Return results
141End
142
143
144//Utility function that returns the detector resolution (in cm)
145//Global values are set in the Initialize procedure
146//
147// - called by CircSectAvg.ipf, RectAnnulAvg.ipf, and ProtocolAsPanel.ipf
148//
149// fileStr is passed as TextRead[3]
150// detStr is passed as TextRead[9]
151//
152// *** as of Jan 2008, depricated. Now detector pixel sizes are read from the file header
153// rw[10] = x size (mm); rw[13] = y size (mm)
154//
155// depricated - pixel dimensions are read directly from the file header
156Function xDetectorPixelResolution(fileStr,detStr)
157        String fileStr,detStr
158       
159        Variable DDet
160
161        //your code here
162//      DDet= getRealValueFromHeader_2(fileStr,60,28,5,11,5)
163       
164        DDet = getRealValueFromHeader(filestr,55)
165       
166        return(DDet)
167End
168
169//Utility function that returns the detector deadtime (in seconds)
170//Global values are set in the Initialize procedure
171//
172// - called by WorkFileUtils.ipf
173//
174// fileStr is passed as TextRead[3] and is the filename
175// detStr is passed as TextRead[9] and is an identifier for the detector
176//
177Function DetectorDeadtime(fileStr,detStr)
178        String fileStr,detStr
179       
180        Variable deadtime
181       
182// your code here
183deadtime = (2e-6)
184
185        return(deadtime)
186End
187
188
189// item is a filename
190//
191// this function extracts some sort of number from the file
192// presumably some sort of automatically incrementing run number set by the
193// acquisition system
194//
195// this run number should be a unique identifier for the file
196//
197Function GetRunNumFromFile(item)
198        String item
199
200        Variable num=-1         // an invalid return value
201       
202        String runStr=""
203       
204        runstr = item
205        num = str2num(runstr)
206       
207       
208        //your code here
209       
210        return (num)
211End
212
213// item is a filename
214//
215// this function extracts some sort of number from the file
216// presumably some sort of automatically incrementing run number set by the
217// acquisition system
218//
219// this run number should be a unique identifier for the file
220//
221// same as GetRunNumFromFile(0), just with a string return
222//
223// "ABC" returned as an invalid result
224Function/S GetRunNumStrFromFile(item)
225        String item
226       
227        String invalid = "ABC"  //"ABC" is not a valid run number, since it's text
228        String retStr
229        retStr=invalid
230       
231        retstr = item
232        //your code here
233       
234
235       
236        return(retStr)
237End
238
239//returns a string containing the full path to the file containing the
240//run number "num". The null string is returned if no valid file can be found.
241//
242//
243// search in the path "catPathName" (hard-wired), will abort if this path does not exist
244//the file returned will be a RAW SANS data file, other types of files are
245//filtered out.
246//
247// called by Buttons.ipf and Transmission.ipf, and locally by parsing routines
248//
249Function/S FindFileFromRunNumber(num)
250        Variable num
251       
252        String fullName="",partialName="",item=""
253        String numStr=""
254       
255        numStr = num2str(num)
256        // pad to 6 characters
257        switch(strlen(numstr))  // numeric switch
258                case 6:         // execute if case matches expression
259                        break                                           // exit from switch
260                case 5:
261                        numStr = "0"+numStr
262                        break
263                case 4:
264                        numStr = "00"+numStr
265                        break
266                case 3:
267                        numStr = "000"+numStr
268                        break
269                case 2:
270                        numStr = "0000"+numStr
271                        break
272                case 1:
273                        numStr = "00000"+numStr
274                        break                                                                                           
275                default:                                                        // optional default expression executed
276        endswitch
277       
278//      Print "numstr = ",numstr
279       
280        //make sure that path exists
281        PathInfo catPathName
282        String path = S_path
283        if (V_flag == 0)
284                Abort "folder path does not exist - use Pick Path button"
285        Endif
286       
287        String list="",newList="",testStr=""
288       
289       
290       
291        list = IndexedFile(catPathName,-1,"????")       //get all files in folder
292        //find (the) one with the number in the run # location in the name
293        Variable numItems,ii,runFound,isRAW
294        numItems = ItemsInList(list,";")                //get the new number of items in the list
295        ii=0
296       
297        do
298                //parse through the list in this order:
299                // 1 - does item contain run number (as a string) "TTTTTnnn.SAn_XXX_Tyyy"
300                // 2 - exclude by isRaw? (to minimize disk access)
301                item = StringFromList(ii, list  ,";" )
302                if(strlen(item) != 0)
303                        //find the run number, if it exists as a three character string
304                        testStr = GetRunNumStrFromFile(item)
305                        runFound = cmpstr(numStr,testStr)
306                        if(runFound == 0)
307                                partialName = FindValidFileName(item)
308                                if(strlen(partialName) != 0)            //non-null return from FindValidFileName()
309                                        fullName = path + partialName
310                                        //check if RAW, if so,this must be the file!
311                                        isRAW = CheckIfRawData(fullName)
312                                        if(isRaw)
313                                                //stop here
314                                                //print fullname
315                                                return(fullname)
316                                        Endif
317                                Endif
318                        Endif
319                Endif
320                ii+=1
321        while(ii<numItems)              //process all items in list
322        Return ("")     //null return if file not found in list
323       
324        //your code here
325       
326End
327       
328       
329//make an (N) character string of the run number
330//Moved to facility utils
331//
332// same scheme to get 6-character string as in GetRunNumStrFromFile(item)
333//
334Function/S RunDigitString(num)
335        Variable num
336       
337//      String fullName="",partialName="",item=""
338//      String numStr=""
339//     
340//      numStr = num2istr(num)
341//      // pad to 6 characters
342//      switch(strlen(numstr))  // numeric switch
343//              case 6:         // execute if case matches expression
344//                      break                                           // exit from switch
345//              case 5:
346//                      numStr = "0"+numStr
347//                      break
348//              case 4:
349//                      numStr = "00"+numStr
350//                      break
351//              case 3:
352//                      numStr = "000"+numStr
353//                      break
354//              case 2:
355//                      numStr = "0000"+numStr
356//                      break
357//              case 1:
358//                      numStr = "00000"+numStr
359//                      break                                                                                           
360//              default:                                                        // optional default expression executed
361//      endswitch
362//     
363       
364        String numStr=""
365
366        //make 6 digit string from run number
367        sprintf numStr,"%06u",num
368       
369        //Print "numstr = ",numstr
370        if(strlen(numstr) > 6)
371                return("")
372        else
373                return(numstr)
374        endif
375       
376End
377       
378        return(numstr)
379End
380
381// there is no prefix on D22 data files - they're just a run number
382//
383// so return null
384//
385// NCNR-specifc, does not really belong here - it's a beta procedure used for the
386// Combine Files Panel, and I'm not sure of how I'm really going to get this to work properly
387// since the .ABS file written is not of the form that NSORT->Set3NSORTFiles(low,med,hi,pref)
388// is trying to construct
389//
390Function/S GetPrefixStrFromFile(item)
391        String item
392        return("")
393End
394
395
396
397//function to test a file to see if it is a RAW SANS file
398//
399// returns truth 0/1
400//
401// called by many procedures (both external and local)
402//
403Function CheckIfRawData(fname)
404        String fname
405       
406
407Variable refnum,totalBytes
408        String testStr=""
409       
410        Open/R/T="????" refNum as fname
411        //get the total number of bytes in the file
412        FStatus refNum
413        totalBytes = V_logEOF
414//      Print totalBytes
415        FSetPos refNum,75
416        FReadLine/N=3 refNum,testStr
417//      print teststr
418        Close refNum
419       
420        if(totalBytes == 142317 && cmpstr(testStr,"RRR")==0)
421                //true, is raw data file
422               
423//              print "yes"
424                Return(1)
425               
426               
427        else
428                //some other file
429               
430//              print "no"
431                Return(0)
432        Endif
433
434
435
436
437//      if()
438//              //true, is raw data file
439//              Return(1)
440//      else
441//              //some other file
442//              Return(0)
443//      Endif
444
445End
446
447
448// function returns 1 if file is a transmission file, 0 if not
449//
450// called by Transmission.ipf, CatVSTable.ipf, NSORT.ipf
451//
452Function isTransFile(fName)
453        String fname
454       
455        variable ypos
456        NVAR yTol = root:myGlobals:BeamstopYTol
457       
458        ypos = getRealValueFromHeader(fname,15)
459       
460//      print ypos
461
462//  print ytol
463       
464        if(abs(ypos)>=ytol)
465//              //yes, its a transmisison file
466
467//print "yes"
468                Return(1)
469        else
470//              //some other file
471
472//print "no"
473                Return(0)
474        Endif
475End
476
477
478//function to remove all spaces from names when searching for filenames
479//the filename (as saved) will never have interior spaces (TTTTTnnn_AB _Bnnn)
480//but the text field in the header may
481//
482//returns a string identical to the original string, except with the interior spaces removed
483//
484// local function for file name manipulation
485//
486// no change needed here
487Function/S RemoveAllSpaces(str)
488        String str
489       
490        String tempstr = str
491        Variable ii,spc,len             //should never be more than 2 or 3 trailing spaces in a filename
492        ii=0
493        do
494                len = strlen(tempStr)
495                spc = strsearch(tempStr," ",0)          //is the last character a space?
496                if (spc == -1)
497                        break           //no more spaces found, get out
498                endif
499                str = tempstr
500                tempStr = str[0,(spc-1)] + str[(spc+1),(len-1)] //remove the space from the string
501        While(1)        //should never be more than 2 or 3
502       
503        If(strlen(tempStr) < 1)
504                tempStr = ""            //be sure to return a null string if problem found
505        Endif
506       
507        //Print strlen(tempstr)
508       
509        Return(tempStr)
510               
511End
512
513
514//Function attempts to find valid filename from partial name by checking for
515// the existence of the file on disk
516//
517// returns a valid filename (No path prepended) or a null string
518//
519// called by any functions, both external and local
520//
521Function/S FindValidFilename(partialName)
522        String PartialName
523       
524        String retStr=""
525       
526        //your code here
527       
528       
529       
530        //try name with no changes - to allow for ABS files that have spaces in the names 12APR04
531        retStr = ValidFileString(partialName)
532        if(cmpstr(retStr,"") !=0)
533                //non-null return
534                return(retStr)
535        Endif
536       
537        //if the partial name is derived from the file header, there can be spaces at the beginning
538        //or in the middle of the filename - depending on the prefix and initials used
539        //
540        //remove any leading spaces from the name before starting
541        partialName = RemoveAllSpaces(partialName)
542       
543        //try name with no spaces
544        retStr = ValidFileString(partialName)
545        if(cmpstr(retStr,"") !=0)
546                //non-null return
547                return(retStr)
548        Endif
549       
550        //try all UPPERCASE
551        partialName = UpperStr(partialName)
552        retStr = ValidFileString(partialName)
553        if(cmpstr(retStr,"") !=0)
554                //non-null return
555                return(retStr)
556        Endif
557       
558        //try all lowercase (ret null if failure)
559        partialName = LowerStr(partialName)
560        retStr = ValidFileString(partialName)
561        if(cmpstr(retStr,"") !=0)
562                //non-null return
563                return(retStr)
564        else
565                return(retStr)
566        Endif
567End
568
569// Function checks for the existence of a file
570// partialName;vers (to account for VAX filenaming conventions)
571// The partial name is tried first with no version number
572//
573// *** the PATH is hard-wired to catPathName (which is assumed to exist)
574// version numers up to ;10 are tried
575// only the "name;vers" is returned if successful. The path is not prepended
576//
577// local function
578//
579
580
581
582Function/S ValidFileString(partialName)
583        String partialName
584       
585        String tempName = "",msg=""
586        Variable ii,refnum
587       
588        ii=0
589        do
590                if(ii==0)
591                        //first pass, try the partialName
592                        tempName = partialName
593                        Open/Z/R/T="????TEXT"/P=catPathName refnum tempName     //Does open file (/Z flag)
594                        if(V_flag == 0)
595                                //file exists
596                                Close refnum            //YES needed,
597                                break
598                        endif
599                else
600                        tempName = partialName + ";" + num2str(ii)
601                        Open/Z/R/T="????TEXT"/P=catPathName refnum tempName
602                        if(V_flag == 0)
603                                //file exists
604                                Close refnum
605                                break
606                        endif
607                Endif
608                ii+=1
609                //print "ii=",ii
610        while(ii<11)
611        //go get the selected bits of information, using tempName, which exists
612        if(ii>=11)
613                //msg = partialName + " not found. is version number > 11?"
614                //DoAlert 0, msg
615                //PathInfo catPathName
616                //Print S_Path
617                Return ("")             //use null string as error condition
618        Endif
619       
620        Return (tempName)
621End
622
623
624
625
626//returns a string containing filename (WITHOUT the ;vers)
627//the input string is a full path to the file (Mac-style, still works on Win in IGOR)
628//with the folders separated by colons
629//
630// called by MaskUtils.ipf, ProtocolAsPanel.ipf, WriteQIS.ipf
631//
632// NEEDS NO CHANGES
633//
634Function/S GetFileNameFromPathNoSemi(fullPath)
635        String fullPath
636       
637        Variable offset1,offset2
638        String filename=""
639        //String PartialPath
640        offset1 = 0
641        do
642                offset2 = StrSearch(fullPath, ":", offset1)
643                if (offset2 == -1)                              // no more colons ?
644                        fileName = FullPath[offset1,strlen(FullPath) ]
645                        //PartialPath = FullPath[0, offset1-1]
646                        break
647                endif
648                offset1 = offset2+1
649        while (1)
650       
651        //remove version number from name, if it's there - format should be: filename;N
652        filename =  StringFromList(0,filename,";")              //returns null if error
653       
654        Return filename
655End
656
657//returns a string containing filename (INCLUDING the ;vers)
658//the input string is a full path to the file (Mac-style, still works on Win in IGOR)
659//with the folders separated by colons
660//
661// local, currently unused
662//
663// NEEDS NO CHANGES
664//
665Function/S GetFileNameFromPathKeepSemi(fullPath)
666        String fullPath
667       
668        Variable offset1,offset2
669        String filename
670        //String PartialPath
671        offset1 = 0
672        do
673                offset2 = StrSearch(fullPath, ":", offset1)
674                if (offset2 == -1)                              // no more colons ?
675                        fileName = FullPath[offset1,strlen(FullPath) ]
676                        //PartialPath = FullPath[0, offset1-1]
677                        break
678                endif
679                offset1 = offset2+1
680        while (1)
681       
682        //keep version number from name, if it's there - format should be: filename;N
683       
684        Return filename
685End
686
687//given the full path and filename (fullPath), strips the data path
688//(Mac-style, separated by colons) and returns this path
689//this partial path is the same string that would be returned from PathInfo, for example
690//
691// - allows the user to save to a different path than catPathName
692//
693// called by WriteQIS.ipf
694//
695// NEEDS NO CHANGES
696//
697Function/S GetPathStrFromfullName(fullPath)
698        String fullPath
699       
700        Variable offset1,offset2
701        //String filename
702        String PartialPath
703        offset1 = 0
704        do
705                offset2 = StrSearch(fullPath, ":", offset1)
706                if (offset2 == -1)                              // no more colons ?
707                        //fileName = FullPath[offset1,strlen(FullPath) ]
708                        PartialPath = FullPath[0, offset1-1]
709                        break
710                endif
711                offset1 = offset2+1
712        while (1)
713       
714        Return PartialPath
715End
716
717//given the filename trim or modify the filename to get a new
718//file string that can be used for naming averaged 1-d files
719//
720// called by ProtocolAsPanel.ipf and Tile_2D.ipf
721//
722Function/S GetNameFromHeader(fullName)
723        String fullName
724        String  temp,newName = ""
725       
726        Variable spc,ii=0
727       
728        //filename is 20 characters NNNNNxxx.SAn_NNN_NNN
729        //want the first 8 characters, NNNNNxxx, then strip off any spaces at the beginning
730        //NNNNN was entered as less than 5 characters
731        //returns a null string if no name can be found
732        do
733                temp = fullname[ii,7]           //characters ii,7 of the name
734                spc = strsearch(temp," ",0)
735                if (spc == -1)
736                        break           //no more spaces found
737                endif
738                ii+=1
739        While(ii<8)
740       
741        If(strlen(temp) < 1)
742                newName = ""            //be sure to return a null string if problem found
743        else
744                newName = temp
745        Endif
746       
747//      print newname
748        //your code here
749       
750        Return(newName)
751End
752
753//list (input) is a list, typically returned from IndexedFile()
754//which is semicolon-delimited, and may contain filenames from the VAX
755//that contain version numbers, where the version number appears as a separate list item
756//(and also as a non-existent file)
757//these numbers must be purged from the list, especially for display in a popup
758//or list processing of filenames
759//the function returns the list, cleaned of version numbers (up to 11)
760//raw data files will typically never have a version number other than 1.
761//
762// if there are no version numbers in the list, the input list is returned
763//
764// called by CatVSTable.ipf, NSORT.ipf, Transmission.ipf, WorkFileUtils.ipf
765//
766//
767// NO CHANGE NEEDED
768//
769Function/S RemoveVersNumsFromList(list)
770        String list
771       
772        //get rid of version numbers first (up to 11)
773        Variable ii,num
774        String item
775        num = ItemsInList(list,";")
776        ii=1
777        do
778                item = num2str(ii)
779                list = RemoveFromList(item, list ,";" )
780                ii+=1
781        while(ii<12)
782       
783        return (list)
784End
785
786//input is a list of run numbers, and output is a list of filenames (not the full path)
787//*** input list must be COMMA delimited***
788//output is equivalent to selecting from the CAT table
789//if some or all of the list items are valid filenames, keep them...
790//if an error is encountered, notify of the offending element and return a null list
791//
792//output is COMMA delimited
793//
794// this routine is expecting that the "ask", "none" special cases are handled elsewhere
795//and not passed here
796//
797// called by Marquee.ipf, MultipleReduce.ipf, ProtocolAsPanel.ipf
798//
799// NO CHANGE NEEDED
800//
801Function/S ParseRunNumberList(list)
802        String list
803       
804        String newList="",item="",tempStr=""
805        Variable num,ii,runNum
806       
807        //expand number ranges, if any
808        list = ExpandNumRanges(list)
809       
810        num=itemsinlist(list,",")
811       
812        for(ii=0;ii<num;ii+=1)
813                //get the item
814                item = StringFromList(ii,list,",")
815                //is it already a valid filename?
816                tempStr=FindValidFilename(item) //returns filename if good, null if error
817                if(strlen(tempstr)!=0)
818                        //valid name, add to list
819                        //Print "it's a file"
820                        newList += tempStr + ","
821                else
822                        //not a valid name
823                        //is it a number?
824                        runNum=str2num(item)
825                        //print runnum
826                        if(numtype(runNum) != 0)
827                                //not a number -  maybe an error                       
828                                DoAlert 0,"List item "+item+" is not a valid run number or filename. Please enter a valid number or filename."
829                                return("")
830                        else
831                                //a run number or an error
832                                tempStr = GetFileNameFromPathNoSemi( FindFileFromRunNumber(runNum) )
833                                if(strlen(tempstr)==0)
834                                        //file not found, error
835                                        DoAlert 0,"List item "+item+" is not a valid run number. Please enter a valid number."
836                                        return("")
837                                else
838                                        newList += tempStr + ","
839                                endif
840                        endif
841                endif
842        endfor          //loop over all items in list
843       
844        return(newList)
845End
846
847//takes a comma delimited list that MAY contain number range, and
848//expands any range of run numbers into a comma-delimited list...
849//and returns the new list - if not a range, return unchanged
850//
851// local function
852//
853// NO CHANGE NEEDED
854//
855Function/S ExpandNumRanges(list)
856        String list
857       
858        String newList="",dash="-",item,str
859        Variable num,ii,hasDash
860       
861        num=itemsinlist(list,",")
862//      print num
863        for(ii=0;ii<num;ii+=1)
864                //get the item
865                item = StringFromList(ii,list,",")
866                //does it contain a dash?
867                hasDash = strsearch(item,dash,0)                //-1 if no dash found
868                if(hasDash == -1)
869                        //not a range, keep it in the list
870                        newList += item + ","
871                else
872                        //has a dash (so it's a range), expand (or add null)
873                        newList += ListFromDash(item)           
874                endif
875        endfor
876       
877        return newList
878End
879
880//be sure to add a trailing comma to the return string...
881//
882// local function
883//
884// NO CHANGE NEEDED
885//
886Function/S ListFromDash(item)
887        String item
888       
889        String numList="",loStr="",hiStr=""
890        Variable lo,hi,ii
891       
892        loStr=StringFromList(0,item,"-")        //treat the range as a list
893        hiStr=StringFromList(1,item,"-")
894        lo=str2num(loStr)
895        hi=str2num(hiStr)
896        if( (numtype(lo) != 0) || (numtype(hi) !=0 ) || (lo > hi) )
897                numList=""
898                return numList
899        endif
900        for(ii=lo;ii<=hi;ii+=1)
901                numList += num2str(ii) + ","
902        endfor
903       
904        Return numList
905End
906
907
908//returns the proper attenuation factor based on the instrument
909//
910// filestr is passed from TextRead[3] = the default directory, used to identify the instrument
911// lam is passed from RealsRead[26]
912// AttenNo is passed from ReaslRead[3]
913//
914// Attenuation factor as defined here is <= 1
915//
916// Facilities can pass ("",1,attenuationFactor) and have this function simply
917// spit back the attenuationFactor (that was read into rw[3])
918//
919// called by Correct.ipf, ProtocolAsPanel.ipf, Transmission.ipf
920//
921Function AttenuationFactor(fileStr,lam,attenNo)
922        String fileStr
923        Variable lam,attenNo
924       
925        Variable attenFactor
926        make/O/N=4 Attenuators_ILL={1,147,902,2874}
927       
928        attenFactor = Attenuators_ILL[attenNo]
929       
930//      print attenfactor
931       
932        // your code here
933
934        return(1/attenFactor)
935End
936
937//function called by the popups to get a file list of data that can be sorted
938// this procedure simply removes the raw data files from the string - there
939//can be lots of other junk present, but this is very fast...
940//
941// could also use the alternate procedure of keeping only file with the proper extension
942//
943// another possibility is to get a listing of the text files, but is unreliable on
944// Windows, where the data file must be .txt (and possibly OSX)
945//
946// called by FIT_Ops.ipf, NSORT.ipf, PlotUtils.ipf
947//
948// modify for specific facilities by changing the "*.SA1*","*.SA2*","*.SA3*" stringmatch
949// items which are specific to NCNR
950//
951Function/S ReducedDataFileList(ctrlName)
952        String ctrlName
953
954        String list="",newList="",item=""
955        Variable num,ii
956       
957        //check for the path
958        PathInfo catPathName
959        if(V_Flag==0)
960                DoAlert 0, "Data path does not exist - pick the data path from the button on the main panel"
961                Return("")
962        Endif
963       
964        list = IndexedFile(catpathName,-1,"????")
965        num=ItemsInList(list,";")
966        //print "num = ",num
967        for(ii=(num-1);ii>=0;ii-=1)
968                item = StringFromList(ii, list  ,";")
969                //simply remove all that are not raw data files (SA1 SA2 SA3)
970                if( !stringmatch(item,"*.SA1*") && !stringmatch(item,"*.SA2*") && !stringmatch(item,"*.SA3*") )
971                        if( !stringmatch(item,".*") && !stringmatch(item,"*.pxp") && !stringmatch(item,"*.DIV"))                //eliminate mac "hidden" files, pxp, and div files
972                                newlist += item + ";"
973                        endif
974                endif
975        endfor
976        //remove VAX version numbers
977        newList = RemoveVersNumsFromList(newList)
978        //sort
979        newList = SortList(newList,";",0)
980
981        return newlist
982End
983
984// returns a list of raw data files in the catPathName directory on disk
985// - list is SEMICOLON-delimited
986//
987// called by PatchFiles.ipf, Tile_2D.ipf
988//
989Function/S GetRawDataFileList()
990       
991        //make sure that path exists
992        PathInfo catPathName
993        if (V_flag == 0)
994                Abort "Folder path does not exist - use Pick Path button on Main Panel"
995        Endif
996       
997        String list=IndexedFile(catPathName,-1,"????")
998       
999        String newList="",item=""
1000        Variable num=ItemsInList(list,";"),ii
1001       
1002       
1003        for(ii=0;ii<num;ii+=1)
1004        item = StringFromList(ii, list  ,";")
1005//      if( stringmatch(item,"*") )
1006        //              newlist += item + ";"
1007//      endif
1008        if( stringmatch(item,"!*.*") )
1009                        newlist += item + ";"
1010                       
1011               
1012        endif
1013       
1014       
1015        endfor
1016       
1017        // your code here
1018        newList = SortList(newList,";",0)
1019       
1020//      print newlist
1021       
1022        return(newlist)
1023End
1024
1025//**********************
1026// 2D resolution function calculation - in terms of X and Y
1027//
1028// based on notes from David Mildner, 2008
1029//
1030// the final NCNR version is located in NCNR_Utils.ipf
1031//
1032Function/S get2DResolution(inQ,phi,lambda,lambdaWidth,DDet,apOff,S1,S2,L1,L2,BS,del_r,usingLenses,r_dist,SigmaQX,SigmaQY,fSubS)
1033        Variable inQ, phi,lambda, lambdaWidth, DDet, apOff, S1, S2, L1, L2, BS, del_r,usingLenses,r_dist
1034        Variable &SigmaQX,&SigmaQY,&fSubS               //these are the output quantities at the input Q value
1035       
1036        return("Function Empty")
1037End
1038
1039// a tube-by-tube correction that replaces our area detecor correction
1040Function DetEffCorrILL(lambda,dtdist,xd) 
1041        Variable lambda,dtdist,xd
1042        Variable ff=1,theta
1043 
1044        theta =360* atan( xd/dtdist )/(2*Pi)
1045 
1046        if (lambda <=5.1 && lambda>=4.9)
1047                ff= 1.000087- 7.094023e-5*abs(theta) + 8.622997e-5*abs(theta^2) + 9.262026e-6*abs(theta^3) -3.216369e-7*abs(theta^4) +2.142398e-9*abs(theta^5)
1048        elseif  (lambda <=8.1 && lambda>=7.9)
1049                ff= 0.9993575- 0.0002320264*abs(theta) + 9.751713e-5*abs(theta^2) + 1.018564e-5*abs(theta^3) -3.977445e-7*abs(theta^4) +2.960205e-9*abs(theta^5)
1050        endif
1051
1052        return(ff)
1053End
1054
1055
1056// Return the filename that represents the previous or next file.
1057// Input is current filename and increment.
1058// Increment should be -1 or 1
1059// -1 => previous file
1060// 1 => next file
1061Function/S GetPrevNextRawFile(curfilename, prevnext)
1062        String curfilename
1063        Variable prevnext
1064
1065        String filename
1066       
1067        //get the run number
1068        Variable num = GetRunNumFromFile(curfilename)
1069               
1070        //find the next specified file by number
1071        fileName = FindFileFromRunNumber(num+prevnext)
1072
1073        if(cmpstr(fileName,"")==0)
1074                //null return, do nothing
1075                fileName = FindFileFromRunNumber(num)
1076        Endif
1077
1078//      print "in FU "+filename
1079
1080        Return filename
1081End
Note: See TracBrowser for help on using the repository browser.