source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/SANS/HFIR_Utils.ipf @ 641

Last change on this file since 641 was 641, checked in by srkline, 13 years ago

A variety of changes to get some of the basic reduction functions working with HFIR raw data files.

Patch now works correctly without duplicating file names in the popup list.

Transmissions can be assigned and calculated now that I've changed the critera for deciding if a file is a transmission file. Hopefully this will always work - it's based on the beamstop y-position being less than 30 mm for all four beam stops.

When checking for a DIV file, it returns an OK if the extension is .xml, since the raw data and div files can't be distinguished like the binary VAX data.

The file catalog is correct, but still excruciatingly slow.

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