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

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

Updated version of HFIR_DataReadWrite.ipf from Jae Hie.

Updated the initialization of the templates from other facilities to remove the NCNR_Package_Loader.ipf since they are by definition incompatible.

Forward/Back? incrementing of the run number (as for the 2D display) has been moved to the Facility_Utils.ipf for all facilities and for the template.

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