source: sans/USANSReduction/trunk/Put in User Procedures Folder/USANS Procedures v2.20/BT5_Loader.ipf @ 51

Last change on this file since 51 was 51, checked in by ajj, 16 years ago

Fixing prefactor bug. See RT Ticket #742

  • Property eol-style set to native
  • Property svn:executable set to *
File size: 6.7 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma Version=2.20
3
4/////////////////////////
5// 101001 Vers. 1
6//
7// functions to load and parse the ASCII ICP files generated at BT5 USANS
8// Nearly all of the important information about the data is dragged around
9// with the DetCts wave as a wave note
10//
11// - thes wave note is a string of KEY:value items
12//
13//      str = "FILE:"+filen+";"
14//      str += "TIMEPT:"+num2str(counttime)+";"
15//      str += "PEAKANG:;"              //no value yet
16//      str += "STATUS:;"               //no value yet
17//      str += "LABEL:"+fileLabel+";"
18//      str += "PEAKVAL:;"              //no value yet
19//      str += "TWIDE:0;"               //no value yet
20//
21/////////////////////////
22
23
24//given the filename, loads a single data file into a "TYPE" folder, into a
25//series of 1D waves. counting time is associated with the DetCts wave as a note
26//"blank" note entries are set up here as well, so later they can be polled/updated
27Function LoadBT5File(fname,type)
28        String fname,type
29       
30        Variable num=200,err=0,refnum
31        Make/O/D/N=(num) $("root:"+type+":Angle")
32        Make/O/D/N=(num) $("root:"+type+":DetCts")
33        Make/O/D/N=(num) $("root:"+type+":ErrDetCts")
34        Make/O/D/N=(num) $("root:"+type+":MonCts")
35        Make/O/D/N=(num) $("root:"+type+":TransCts")
36        Wave Angle = $("root:"+type+":Angle")
37        Wave DetCts = $("root:"+type+":DetCts")
38        Wave ErrDetCts = $("root:"+type+":ErrDetCts")
39        Wave MonCts = $("root:"+type+":MonCts")
40        Wave TransCts = $("root:"+type+":TransCts")
41       
42        Open/R refNum as fname          //if fname is "", a dialog will be presented
43        if(refnum==0)
44                return(1)               //user cancelled
45        endif
46        //read in the ASCII data line-by-line
47        Variable numLinesLoaded = 0,firstchar,countTime
48        Variable v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,ii,valuesRead
49        String buffer ="",s1,s2,s3,s4,s5,s6,s7,s8,s9,s10
50        String filen="",fileLabel=""
51       
52        //parse the first line
53        FReadLine refnum,buffer
54        sscanf buffer, "%s%s%s%s%s%s%g%g%s%g%s",s1,s2,s3,s4,s5,s6,v1,v2,s7,v3,s8
55        ii=strlen(s1)
56        filen=s1[1,(ii-2)]                      //remove the ' from beginning and end
57        //Print "filen = ",filen,strlen(filen)
58        //v2 is the monitor prefactor. multiply monitor time by prefactor. AJJ 5 March 07
59        countTime=v1*v2
60        //Print "time (sec) = ",countTime
61       
62        //skip line 2
63        FReadLine refnum,buffer
64        //the next line is the sample label, use it all, minus the terminator
65        FReadLine refnum,filelabel
66       
67        //skip the next 10 lines
68        For(ii=0;ii<10;ii+=1)
69                FReadLine refnum,buffer
70        EndFor
71       
72        //read the data until EOF - assuming always a pair or lines
73        do
74                FReadLine refNum, buffer
75                if (strlen(buffer) == 0)
76                        break                                                   // End of file
77                endif
78                firstChar = char2num(buffer[0])
79                if ( (firstChar==10) || (firstChar==13) )
80                        break                                                   // Hit blank line. End of data in the file.
81                endif
82                sscanf buffer,"%g%g%g",v1,v2,v3         //v2,v3 not used
83                angle[numlinesloaded] = v1              //[0] is the ANGLE
84               
85                FReadLine refNum,buffer //assume a 2nd line is there, w/16 values
86                sscanf buffer,"%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g",v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16
87                //valuesRead = V_flag
88                //print valuesRead
89                MonCts[numlinesloaded] = v1             //monitor
90                DetCts[numlinesloaded] = v2 + v3 + v5 + v6 + v7         //5 detectors
91                TransCts[numlinesloaded] = v4           //trans detector
92                ErrDetCts[numlinesloaded] = sqrt(detCts[numlinesloaded])
93                //values 8-16 are always zero
94                numlinesloaded += 1             //2 lines in file read, one "real" line of data
95        while(1)
96               
97        Close refNum            // Close the file.
98        //Print "numlines = ",numlinesloaded
99        //trim the waves to the correct number of points
100        Redimension/N=(numlinesloaded) Angle,MonCts,DetCts,TransCts,ErrDetCts
101       
102        //remove LF from end of filelabel
103        filelabel=fileLabel[0,(strlen(fileLabel)-2)]
104       
105        //set the wave note for the DetCts
106        String str=""
107        str = "FILE:"+filen+";"
108        str += "TIMEPT:"+num2str(counttime)+";"
109        str += "PEAKANG:;"              //no value yet
110        str += "STATUS:;"               //no value yet
111        str += "LABEL:"+fileLabel+";"
112        str += "PEAKVAL:;"              //no value yet
113        str += "TWIDE:0;"               //no value yet
114        Note DetCts,str
115       
116        String/G fileLbl = filelabel
117        return err                      // Zero signifies no error.     
118End
119
120
121//will search for peak in root:type:DetCts,  find and returns the angle
122// of the peak poistion (= q=0)
123//returns -9999 if error, appends value to wavenote
124// !can't return -1,0,1, since these may be the peak angle!
125//
126Function FindZeroAngle(type)
127        String type
128       
129        Variable pkNotFound,pkPt,pkAngle,pkVal,temp
130        Wave angle = $("root:"+type+":Angle")
131        Wave detCts = $("root:"+type+":DetCts")
132
133        WaveStats/Q detcts
134        temp=V_Maxloc           //in points
135        FindPeak/P/Q/M=10/R=[(temp-10),(temp+10)] DetCts                //+/- 10 pts from maximum
136       
137        //FindPeak/P/Q/M=(1000) detCts          // /M=min ht -- peak must be at least 100x higher than 1st pt
138        pkNotFound=V_Flag               //V_Flag==1 if no pk found
139        pkPt = V_PeakLoc
140        pkVal = V_PeakVal               //for calc of T_rock
141        if(pkNotFound)
142                //DoAlert 0, "Peak not found"
143                Return (-9999)          //fatal error
144        Endif
145        pkAngle = Angle[pkPt]
146        //Print "Peak Angle = ",pkAngle
147        //update the note
148        String str=""
149        str=note(DetCts)
150        str = ReplaceNumberByKey("PEAKANG",str,pkangle,":",";")
151        str = ReplaceNumberByKey("PEAKVAL",str,pkVal,":",";")
152        Note/K DetCts
153        Note detCts,str
154       
155        return(pkAngle)
156End
157
158// given the peakAngle (q=0) and the "type" folder of scattering angle,
159// convert the angle to Q-values [degrees to (1/A)]
160// makes a new Qvals wave, duplicating the Angle wave, which is assumed to exist
161// Uses a conversion constant supplied by John Barker, and is hard-wired in
162//
163Function ConvertAngle2Qvals(type,pkAngle)
164        String type
165        Variable pkAngle
166       
167        Wave angle = $("root:"+type+":Angle")
168        Variable num=numpnts(angle)
169        Variable deg2QConv=5.55e-5              //JGB -- 2/24/01
170       
171        Make/O/N=(num) $("root:"+type+":Qvals")
172        Wave qvals = $("root:"+type+":Qvals")   
173        Qvals = deg2QConv*(angle[p] - pkAngle)
174       
175        return(0)       //no error
176End
177
178//updates the wavenote with the average Trans det cts for calculation of T_Wide
179//  finds the average number of counts on the transmission detector at angles
180// greater than 2 deg. (per John, practical experience where the trans data levels off)
181//
182// error value of 1 is returned and wavenote not updated if level is not found
183//
184//
185Function FindTWideCts(type)
186        String type
187       
188        Variable levNotFound,levPt,Cts,num,ii
189        Wave angle = $("root:"+type+":Angle")
190        Wave detCts = $("root:"+type+":DetCts")
191        Wave TransCts = $("root:"+type+":TransCts")
192        FindLevel/Q/P angle,2           //use angles greater than 2 deg
193        levNotFound=V_Flag              //V_Flag==1 if no pk found
194        if(levNotFound)
195                return(1)
196        endif
197        levPt = trunc(V_LevelX)         // in points, force to integer
198       
199        //average the trans cts from levPt to the end of the dataset
200        num=numpnts(TransCts)
201        Cts=0
202        for(ii=levPt;ii<num;ii+=1)
203                Cts += transCts[ii]
204        endfor
205        Cts /= (num-levPt-1)
206       
207        //update the note
208        Wave DetCts = $("root:"+type+":DetCts")
209        String str,strVal
210        str=note(DetCts)
211        str = ReplaceNumberByKey("TWIDE",str,Cts,":",";")
212        Note/K DetCts
213        Note detCts,str
214       
215        return(0)
216End
Note: See TracBrowser for help on using the repository browser.