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

Last change on this file was 947, checked in by srkline, 8 years ago

Changes to VCALC files, moving detector information into sub-folders in anticipation of the possible folder structure that I will use for the actual data reduction. But purely speculative at this point.

Fixing some typos in other procedures.

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