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

Last change on this file since 795 was 795, checked in by srkline, 11 years ago

Changes to SANS reduction that apply to other Facilities:

These changes are related to the propagation of errors in 2D, on a
per-pixel basis. These changes only affect the errors that are reported in
the QxQy? ASCII file output. The 1D code is unaffected.

If these changes are not implemented, then errors of zero will be substitued as defaults
for these experimental errors.

Upon data loading, an error matrix, linear_data_error is generated and filled with
error values appropriate for Poisson statistics (not simply sqrt(n)).

4 functions in FACILITY_DataReadWrite.ipf have been added, and they are rather
self-explanatory:

In FACILITY_Utils.ipf, the AttenuatorTransmission?() function now returns
an additional parameter, atten_err, which is one standard deviation of the
attenuator transmission value. It returns a default error=0 (which is
correct if no attenuation is used). Facilities can fill this function in
with their own estimates for the uncertainty in the attenutator transmission.

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