source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/SANS/KIST_Utils.ipf @ 857

Last change on this file since 857 was 857, checked in by srkline, 10 years ago

Added support for reading 4-column data into the 2D reader

Calculation of kappa for ABS now does a specific byte check for ensuring a "good" DIV file. hopefully this will eliminate errors where incorrect DIV files are selected and bad kappa values are generated - and the program thinks (incorrectly) that there is a valid DVI file present, when there really isn't one (and then there is no easy recovery from this)

Added CheckIfDIVData() functions to NCNR_Utils and to FACILITY_Utils, and to other (facility)_Utils

Changed the calls to writing BT5 files after adding so that it could be more easily scripted in the future.

File size: 19.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 = 3.4e-6              //deadtime in seconds
30
31        Variable/G root:myGlobals:apOff = 5.0           // (cm) distance from sample aperture to sample position
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//Utility function that returns the detector resolution (in cm)
139//Global values are set in the Initialize procedure
140//
141//
142// - called by CircSectAvg.ipf, RectAnnulAvg.ipf, and ProtocolAsPanel.ipf
143//
144// fileStr is passed as TextRead[3]
145// detStr is passed as TextRead[9]
146//
147// *** as of Jan 2008, depricated. Now detector pixel sizes are read from the file header
148// rw[10] = x size (mm); rw[13] = y size (mm)
149//
150// depricated - pixel dimensions are read directly from the file header
151Function xDetectorPixelResolution(fileStr,detStr)
152        String fileStr,detStr
153       
154        Variable DDet
155
156        //your code here
157       
158        return(DDet)
159End
160
161//Utility function that returns the detector deadtime (in seconds)
162//Global values are set in the Initialize procedure
163//
164// - called by WorkFileUtils.ipf
165//
166// fileStr is passed as TextRead[3] and is the filename
167// detStr is passed as TextRead[9] and is an identifier for the detector
168//
169Function DetectorDeadtime(fileStr,detStr)
170        String fileStr,detStr
171       
172        Variable deadtime
173       
174// your code here
175
176        return(deadtime)
177End
178
179
180// item is a filename
181//
182// this function extracts some sort of number from the file
183// presumably some sort of automatically incrementing run number set by the
184// acquisition system
185//
186// this run number should be a unique identifier for the file
187//
188Function GetRunNumFromFile(item)
189        String item
190
191        Variable num=-1         // an invalid return value
192       
193        //your code here
194       
195        return (num)
196End
197
198// item is a filename
199//
200// this function extracts some sort of number from the file
201// presumably some sort of automatically incrementing run number set by the
202// acquisition system
203//
204// this run number should be a unique identifier for the file
205//
206// same as GetRunNumFromFile(0), just with a string return
207//
208// "ABC" returned as an invalid result
209Function/S GetRunNumStrFromFile(item)
210        String item
211       
212        String invalid = "ABC"  //"ABC" is not a valid run number, since it's text
213        String retStr
214        retStr=invalid
215       
216        //your code here
217       
218        return(retStr)
219End
220
221//returns a string containing the full path to the file containing the
222//run number "num". The null string is returned if no valid file can be found.
223//
224//
225// search in the path "catPathName" (hard-wired), will abort if this path does not exist
226//the file returned will be a RAW SANS data file, other types of files are
227//filtered out.
228//
229// called by Buttons.ipf and Transmission.ipf, and locally by parsing routines
230//
231Function/S FindFileFromRunNumber(num)
232        Variable num
233       
234        String fullName="",partialName="",item=""
235       
236        //make sure that path exists
237        PathInfo catPathName
238        String path = S_path
239        if (V_flag == 0)
240                Abort "folder path does not exist - use Pick Path button"
241        Endif
242
243        //your code here       
244
245        return(fullname)
246       
247End
248
249//function to test a file to see if it is a RAW SANS file
250//
251// returns truth 0/1
252//
253// called by many procedures (both external and local)
254//
255Function CheckIfRawData(fname)
256        String fname
257       
258
259//      if(your test here)
260//              //true, is raw data file
261//              Return(1)
262//      else
263//              //some other file
264//              Return(0)
265//      Endif
266
267End
268
269//function to test a file to see if it is a DIV file
270//
271// returns truth 0/1
272//
273// called by many procedures (both external and local)
274//
275Function CheckIfDIVData(fname)
276        String fname
277       
278
279//      if(your test here)
280//              //true, is DIV data file
281//              Return(1)
282//      else
283//              //some other file
284//              Return(0)
285//      Endif
286
287End
288
289// function returns 1 if file is a transmission file, 0 if not
290//
291// called by Transmission.ipf, CatVSTable.ipf, NSORT.ipf
292//
293Function isTransFile(fName)
294        String fname
295       
296//      if(your test here)
297//              //yes, its a transmisison file
298//              Return(1)
299//      else
300//              //some other file
301//              Return(0)
302//      Endif
303End
304
305
306//function to remove all spaces from names when searching for filenames
307//the filename (as saved) will never have interior spaces (TTTTTnnn_AB _Bnnn)
308//but the text field in the header may
309//
310//returns a string identical to the original string, except with the interior spaces removed
311//
312// local function for file name manipulation
313//
314// no change needed here
315Function/S RemoveAllSpaces(str)
316        String str
317       
318        String tempstr = str
319        Variable ii,spc,len             //should never be more than 2 or 3 trailing spaces in a filename
320        ii=0
321        do
322                len = strlen(tempStr)
323                spc = strsearch(tempStr," ",0)          //is the last character a space?
324                if (spc == -1)
325                        break           //no more spaces found, get out
326                endif
327                str = tempstr
328                tempStr = str[0,(spc-1)] + str[(spc+1),(len-1)] //remove the space from the string
329        While(1)        //should never be more than 2 or 3
330       
331        If(strlen(tempStr) < 1)
332                tempStr = ""            //be sure to return a null string if problem found
333        Endif
334       
335        //Print strlen(tempstr)
336       
337        Return(tempStr)
338               
339End
340
341
342//Function attempts to find valid filename from partial name by checking for
343// the existence of the file on disk
344//
345// returns a valid filename (No path prepended) or a null string
346//
347// called by any functions, both external and local
348//
349Function/S FindValidFilename(partialName)
350        String PartialName
351       
352        String retStr=""
353       
354        //your code here
355       
356        return(retStr)
357End
358
359
360//returns a string containing filename (WITHOUT the ;vers)
361//the input string is a full path to the file (Mac-style, still works on Win in IGOR)
362//with the folders separated by colons
363//
364// called by MaskUtils.ipf, ProtocolAsPanel.ipf, WriteQIS.ipf
365//
366// NEEDS NO CHANGES
367//
368Function/S GetFileNameFromPathNoSemi(fullPath)
369        String fullPath
370       
371        Variable offset1,offset2
372        String filename=""
373        //String PartialPath
374        offset1 = 0
375        do
376                offset2 = StrSearch(fullPath, ":", offset1)
377                if (offset2 == -1)                              // no more colons ?
378                        fileName = FullPath[offset1,strlen(FullPath) ]
379                        //PartialPath = FullPath[0, offset1-1]
380                        break
381                endif
382                offset1 = offset2+1
383        while (1)
384       
385        //remove version number from name, if it's there - format should be: filename;N
386        filename =  StringFromList(0,filename,";")              //returns null if error
387       
388        Return filename
389End
390
391//returns a string containing filename (INCLUDING the ;vers)
392//the input string is a full path to the file (Mac-style, still works on Win in IGOR)
393//with the folders separated by colons
394//
395// local, currently unused
396//
397// NEEDS NO CHANGES
398//
399Function/S GetFileNameFromPathKeepSemi(fullPath)
400        String fullPath
401       
402        Variable offset1,offset2
403        String filename
404        //String PartialPath
405        offset1 = 0
406        do
407                offset2 = StrSearch(fullPath, ":", offset1)
408                if (offset2 == -1)                              // no more colons ?
409                        fileName = FullPath[offset1,strlen(FullPath) ]
410                        //PartialPath = FullPath[0, offset1-1]
411                        break
412                endif
413                offset1 = offset2+1
414        while (1)
415       
416        //keep version number from name, if it's there - format should be: filename;N
417       
418        Return filename
419End
420
421//given the full path and filename (fullPath), strips the data path
422//(Mac-style, separated by colons) and returns this path
423//this partial path is the same string that would be returned from PathInfo, for example
424//
425// - allows the user to save to a different path than catPathName
426//
427// called by WriteQIS.ipf
428//
429// NEEDS NO CHANGES
430//
431Function/S GetPathStrFromfullName(fullPath)
432        String fullPath
433       
434        Variable offset1,offset2
435        //String filename
436        String PartialPath
437        offset1 = 0
438        do
439                offset2 = StrSearch(fullPath, ":", offset1)
440                if (offset2 == -1)                              // no more colons ?
441                        //fileName = FullPath[offset1,strlen(FullPath) ]
442                        PartialPath = FullPath[0, offset1-1]
443                        break
444                endif
445                offset1 = offset2+1
446        while (1)
447       
448        Return PartialPath
449End
450
451//given the filename trim or modify the filename to get a new
452//file string that can be used for naming averaged 1-d files
453//
454// called by ProtocolAsPanel.ipf and Tile_2D.ipf
455//
456Function/S GetNameFromHeader(fullName)
457        String fullName
458        String newName = ""
459
460        //your code here
461       
462        Return(newName)
463End
464
465//list (input) is a list, typically returned from IndexedFile()
466//which is semicolon-delimited, and may contain filenames from the VAX
467//that contain version numbers, where the version number appears as a separate list item
468//(and also as a non-existent file)
469//these numbers must be purged from the list, especially for display in a popup
470//or list processing of filenames
471//the function returns the list, cleaned of version numbers (up to 11)
472//raw data files will typically never have a version number other than 1.
473//
474// if there are no version numbers in the list, the input list is returned
475//
476// called by CatVSTable.ipf, NSORT.ipf, Transmission.ipf, WorkFileUtils.ipf
477//
478//
479// NO CHANGE NEEDED
480//
481Function/S RemoveVersNumsFromList(list)
482        String list
483       
484        //get rid of version numbers first (up to 11)
485        Variable ii,num
486        String item
487        num = ItemsInList(list,";")
488        ii=1
489        do
490                item = num2str(ii)
491                list = RemoveFromList(item, list ,";" )
492                ii+=1
493        while(ii<12)
494       
495        return (list)
496End
497
498//input is a list of run numbers, and output is a list of filenames (not the full path)
499//*** input list must be COMMA delimited***
500//output is equivalent to selecting from the CAT table
501//if some or all of the list items are valid filenames, keep them...
502//if an error is encountered, notify of the offending element and return a null list
503//
504//output is COMMA delimited
505//
506// this routine is expecting that the "ask", "none" special cases are handled elsewhere
507//and not passed here
508//
509// called by Marquee.ipf, MultipleReduce.ipf, ProtocolAsPanel.ipf
510//
511// NO CHANGE NEEDED
512//
513Function/S ParseRunNumberList(list)
514        String list
515       
516        String newList="",item="",tempStr=""
517        Variable num,ii,runNum
518       
519        //expand number ranges, if any
520        list = ExpandNumRanges(list)
521       
522        num=itemsinlist(list,",")
523       
524        for(ii=0;ii<num;ii+=1)
525                //get the item
526                item = StringFromList(ii,list,",")
527                //is it already a valid filename?
528                tempStr=FindValidFilename(item) //returns filename if good, null if error
529                if(strlen(tempstr)!=0)
530                        //valid name, add to list
531                        //Print "it's a file"
532                        newList += tempStr + ","
533                else
534                        //not a valid name
535                        //is it a number?
536                        runNum=str2num(item)
537                        //print runnum
538                        if(numtype(runNum) != 0)
539                                //not a number -  maybe an error                       
540                                DoAlert 0,"List item "+item+" is not a valid run number or filename. Please enter a valid number or filename."
541                                return("")
542                        else
543                                //a run number or an error
544                                tempStr = GetFileNameFromPathNoSemi( FindFileFromRunNumber(runNum) )
545                                if(strlen(tempstr)==0)
546                                        //file not found, error
547                                        DoAlert 0,"List item "+item+" is not a valid run number. Please enter a valid number."
548                                        return("")
549                                else
550                                        newList += tempStr + ","
551                                endif
552                        endif
553                endif
554        endfor          //loop over all items in list
555       
556        return(newList)
557End
558
559//takes a comma delimited list that MAY contain number range, and
560//expands any range of run numbers into a comma-delimited list...
561//and returns the new list - if not a range, return unchanged
562//
563// local function
564//
565// NO CHANGE NEEDED
566//
567Function/S ExpandNumRanges(list)
568        String list
569       
570        String newList="",dash="-",item,str
571        Variable num,ii,hasDash
572       
573        num=itemsinlist(list,",")
574//      print num
575        for(ii=0;ii<num;ii+=1)
576                //get the item
577                item = StringFromList(ii,list,",")
578                //does it contain a dash?
579                hasDash = strsearch(item,dash,0)                //-1 if no dash found
580                if(hasDash == -1)
581                        //not a range, keep it in the list
582                        newList += item + ","
583                else
584                        //has a dash (so it's a range), expand (or add null)
585                        newList += ListFromDash(item)           
586                endif
587        endfor
588       
589        return newList
590End
591
592//be sure to add a trailing comma to the return string...
593//
594// local function
595//
596// NO CHANGE NEEDED
597//
598Function/S ListFromDash(item)
599        String item
600       
601        String numList="",loStr="",hiStr=""
602        Variable lo,hi,ii
603       
604        loStr=StringFromList(0,item,"-")        //treat the range as a list
605        hiStr=StringFromList(1,item,"-")
606        lo=str2num(loStr)
607        hi=str2num(hiStr)
608        if( (numtype(lo) != 0) || (numtype(hi) !=0 ) || (lo > hi) )
609                numList=""
610                return numList
611        endif
612        for(ii=lo;ii<=hi;ii+=1)
613                numList += num2str(ii) + ","
614        endfor
615       
616        Return numList
617End
618
619
620//returns the proper attenuation factor based on the instrument
621//
622// filestr is passed from TextRead[3] = the default directory, used to identify the instrument
623// lam is passed from RealsRead[26]
624// AttenNo is passed from ReaslRead[3]
625//
626// Attenuation factor as defined here is <= 1
627//
628// Facilities can pass ("",1,attenuationFactor) and have this function simply
629// spit back the attenuationFactor (that was read into rw[3])
630//
631// called by Correct.ipf, ProtocolAsPanel.ipf, Transmission.ipf
632// atten_err is one std. deviation, passed back by reference
633Function AttenuationFactor(fileStr,lam,attenNo,atten_err)
634        String fileStr
635        Variable lam,attenNo, &atten_err
636       
637        Variable attenFactor=1
638       
639        // your code here
640
641        return(attenFactor)
642End
643
644//function called by the popups to get a file list of data that can be sorted
645// this procedure simply removes the raw data files from the string - there
646//can be lots of other junk present, but this is very fast...
647//
648// could also use the alternate procedure of keeping only file with the proper extension
649//
650// another possibility is to get a listing of the text files, but is unreliable on
651// Windows, where the data file must be .txt (and possibly OSX)
652//
653// called by FIT_Ops.ipf, NSORT.ipf, PlotUtils.ipf
654//
655// modify for specific facilities by changing the "*.SA1*","*.SA2*","*.SA3*" stringmatch
656// items which are specific to NCNR
657//
658Function/S ReducedDataFileList(ctrlName)
659        String ctrlName
660
661        String list="",newList="",item=""
662        Variable num,ii
663       
664        //check for the path
665        PathInfo catPathName
666        if(V_Flag==0)
667                DoAlert 0, "Data path does not exist - pick the data path from the button on the main panel"
668                Return("")
669        Endif
670       
671        list = IndexedFile(catpathName,-1,"????")
672        num=ItemsInList(list,";")
673        //print "num = ",num
674        for(ii=(num-1);ii>=0;ii-=1)
675                item = StringFromList(ii, list  ,";")
676                //simply remove all that are not raw data files (SA1 SA2 SA3)
677                if( !stringmatch(item,"*.SA1*") && !stringmatch(item,"*.SA2*") && !stringmatch(item,"*.SA3*") )
678                        if( !stringmatch(item,".*") && !stringmatch(item,"*.pxp") && !stringmatch(item,"*.DIV"))                //eliminate mac "hidden" files, pxp, and div files
679                                newlist += item + ";"
680                        endif
681                endif
682        endfor
683        //remove VAX version numbers
684        newList = RemoveVersNumsFromList(newList)
685        //sort
686        newList = SortList(newList,";",0)
687
688        return newlist
689End
690
691// returns a list of raw data files in the catPathName directory on disk
692// - list is SEMICOLON-delimited
693//
694// called by PatchFiles.ipf, Tile_2D.ipf
695//
696Function/S GetRawDataFileList()
697       
698        //make sure that path exists
699        PathInfo catPathName
700        if (V_flag == 0)
701                Abort "Folder path does not exist - use Pick Path button on Main Panel"
702        Endif
703       
704        String list=""
705       
706        // your code here
707       
708        return(list)
709End
710
711//**********************
712// 2D resolution function calculation - in terms of X and Y
713//
714// based on notes from David Mildner, 2008
715//
716// the final NCNR version is located in NCNR_Utils.ipf
717//
718Function/S get2DResolution(inQ,phi,lambda,lambdaWidth,DDet,apOff,S1,S2,L1,L2,BS,del_r,usingLenses,r_dist,SigmaQX,SigmaQY,fSubS)
719        Variable inQ, phi,lambda, lambdaWidth, DDet, apOff, S1, S2, L1, L2, BS, del_r,usingLenses,r_dist
720        Variable &SigmaQX,&SigmaQY,&fSubS               //these are the output quantities at the input Q value
721       
722        return("Function Empty")
723End
724
725
726// Return the filename that represents the previous or next file.
727// Input is current filename and increment.
728// Increment should be -1 or 1
729// -1 => previous file
730// 1 => next file
731Function/S GetPrevNextRawFile(curfilename, prevnext)
732        String curfilename
733        Variable prevnext
734
735        String filename
736       
737        //get the run number
738        Variable num = GetRunNumFromFile(curfilename)
739               
740        //find the next specified file by number
741        fileName = FindFileFromRunNumber(num+prevnext)
742
743        if(cmpstr(fileName,"")==0)
744                //null return, do nothing
745                fileName = FindFileFromRunNumber(num)
746        Endif
747
748//      print "in FU "+filename
749
750        Return filename
751End
Note: See TracBrowser for help on using the repository browser.