source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/VSANS/V_Marquee_Operations.ipf @ 1041

Last change on this file since 1041 was 1041, checked in by srkline, 6 years ago

Added the angle dependent transmission correction to the data correction in the raw_to_work step, in 2D

added a testing file that can generate fake event data, read, write, and decode it. Read is based on GBLoadWave. Hoepfully I'll not need to write an XOP. manipulation of the 64 bit words are done with simple bit shifts and logic.

also added are a number of error checking routines to improve behavior when wave, folders, etc. are missing.

File size: 8.5 KB
Line 
1#pragma TextEncoding = "MacRoman"
2#pragma rtGlobals=3             // Use modern global access method and strict wave access.
3
4
5// TODO:
6// x- get BoxSum Operational, for rudimentary Transmission calculations and ABS scale
7// x- need to be able to identify the detector panel from the Marquee selection
8// x- then do the count in (unscaled) coordinates of the actual data array
9//
10//
11// -- still many operations in (SANS)Marquee.ipf that will be useful to implement:
12//
13// -- writing the box coordinates and the counts (and error) to the data file
14// -- determining the beam center (centroid) of the selection + writing this to the file
15//  -- a box sum over a range of files (with a plot)
16// -- box sum over annular regions
17// -- box sum over arcs
18// --  saving Graphics image
19// -- histogram of the counts
20//
21//
22//
23
24
25
26
27//
28//function will print marquee coordinates in axis terms, not detector terms
29//
30// will also calculate the sum in the box, and the error, and print it out
31//
32Function V_PrintMarqueeCoords() :  GraphMarquee
33        GetMarquee left,bottom
34        if(V_flag == 0)
35                Print "There is no Marquee"
36        else
37                Variable count,x1,x2,y1,y2,ct_err
38                x1 = V_left
39                x2 = V_right
40                y1 = V_bottom
41                y2 = V_top
42                printf "marquee left in bottom axis terms: %g\r",round(V_left)
43                printf "marquee right in bottom axis terms: %g\r",round(V_right)
44                printf "marquee bottom in left axis terms: %g\r",round(V_bottom)
45                printf "marquee top in left axis terms: %g\r",round(V_top)
46//              printf "**note that you must add 1 to each axis coordinate to get detector coordinates\r"
47               
48                // NOTE:
49                // this function MODIFIES x and y values on return, converting them to panel coordinates
50                // detector panel is identified from the (left,top) coordinate (x1,y2)
51                String detStr = V_FindDetStrFromLoc(x1,x2,y1,y2)               
52//              Printf "Detector = %s\r",detStr
53
54//
55                SVAR gCurDispType = root:Packages:NIST:VSANS:Globals:gCurDispType       
56                // this function will modify the x and y values (passed by reference) as needed to keep on the panel
57                V_KeepSelectionInBounds(x1,x2,y1,y2,detStr,gCurDispType)
58                Printf "%d;%d;%d;%d;\r",x1,x2,y1,y2
59
60
61                count = V_SumCountsInBox(x1,x2,y1,y2,ct_err,gCurDispType,detStr)
62               
63                Print "counts = ",count
64                Print "err/counts = ",ct_err/count
65
66        endif
67End
68
69
70// NOTE:
71// this function MODIFIES x and y values on return, converting them to panel coordinates
72// detector panel is identified from the (left,top) coordinate (x1,y2)
73Function/S V_FindDetStrFromLoc(x1,x2,y1,y2)
74        Variable &x1,&x2,&y1,&y2
75       
76        // which images are here?
77        String detStr="",imStr,carriageStr
78        String currentImageRef,activeSubwindow,imageList
79        Variable ii,nIm,testX,testY,tab
80       
81        // which tab is selected? -this is the main graph panel (subwindow may not be the active one!)
82        ControlInfo/W=VSANS_Data tab0
83        tab = V_Value
84        if(tab == 0)
85                activeSubwindow = "VSANS_Data#det_panelsF"
86        elseif (tab == 1)
87                activeSubwindow = "VSANS_Data#det_panelsM"
88        else
89                activeSubwindow = "VSANS_Data#det_panelsB"
90        endif
91       
92        imageList = ImageNameList(activeSubwindow,";") 
93
94        nIm = ItemsInList(imageList,";")
95        if(nIm==0)
96                return("")              //problem, get out
97        endif
98
99        // images were added in the order TBLR, so look back through in the order RLBT, checking each to see if
100        // the xy value is found on that (scaled) array
101                               
102        // loop backwards through the list of panels (may only be one if on the back)
103        for(ii=nIm-1;ii>=0;ii-=1)
104                Wave w = ImageNameToWaveRef(activeSubwindow,StringFromList(ii, imageList,";"))
105               
106                // which, if any image is the mouse xy location on?
107                // use a multidemensional equivalent to x2pnt: (ScaledDimPos - DimOffset(waveName, dim))/DimDelta(waveName,dim)
108
109               
110                testX = ScaleToIndex(w,x1,0)
111                testY = ScaleToIndex(w,y2,1)
112               
113                if( (testX >= 0 && testX < DimSize(w,0)) && (testY >= 0 && testY < DimSize(w,1)) )
114                        // we're in-bounds on this wave
115                       
116                        // deduce the detector panel
117                        currentImageRef = StringFromList(ii, imageList,";")     //the image instance ##
118                        // string is "data", or "data#2" etc. - so this returns "", "1", "2", or "3"
119                        imStr = StringFromList(1, currentImageRef,"#")         
120                        carriageStr = activeSubWindow[strlen(activeSubWindow)-1]
121                       
122                        if(cmpstr(carriageStr,"B")==0)
123                                detStr = carriageStr
124                        else
125                                if(strlen(imStr)==0)
126                                        imStr = "9"                     // a dummy value so I can replace it later
127                                endif
128                                detStr = carriageStr+imStr              // "F2" or something similar
129                                detStr = ReplaceString("9", detStr, "T")        // ASSUMPTION :::: instances 0123 correspond to TBLR
130                                detStr = ReplaceString("1", detStr, "B")        // ASSUMPTION :::: this is the order that the panels
131                                detStr = ReplaceString("2", detStr, "L")        // ASSUMPTION :::: are ALWAYS added to the graph
132                                detStr = ReplaceString("3", detStr, "R")        // ASSUMPTION ::::
133                        endif
134                       
135                        Printf "Detector panel %s=(%d,%d)\r",detStr,testX,testY
136                       
137                        x1 = ScaleToIndex(w,x1,0)               // get all four marquee values to pass back to the calling function
138                        x2 = ScaleToIndex(w,x2,0)               // converted into detector coordinates
139                        y1 = ScaleToIndex(w,y1,1)
140                        y2 = ScaleToIndex(w,y2,1)
141                       
142                       
143                        ii = -1         //look no further, set ii to bad value to exit the for loop
144
145                endif
146               
147        endfor
148       
149        return(detStr)
150       
151End
152
153//testing function only, not called anywhere
154Function V_testKeepInBounds(x1,x2,y1,y2,detStr,folderStr)
155        Variable x1,x2,y1,y2
156        String detStr,folderStr
157       
158        V_KeepSelectionInBounds(x1,x2,y1,y2,detStr,folderStr)
159        Print x1,x2,y1,y2
160        return(0)
161End
162
163// for the given detector
164Function V_KeepSelectionInBounds(x1,x2,y1,y2,detStr,folderStr)
165        Variable &x1,&x2,&y1,&y2
166        String detStr,folderStr
167       
168        Variable pixelsX = V_getDet_pixel_num_x(folderStr,detStr)
169        Variable pixelsY = V_getDet_pixel_num_y(folderStr,detStr)
170
171        //keep selection in-bounds
172        x1 = (round(x1) >= 0) ? round(x1) : 0
173        x2 = (round(x2) <= (pixelsX-1)) ? round(x2) : (pixelsX-1)
174        y1 = (round(y1) >= 0) ? round(y1) : 0
175        y2 = (round(y2) <= (pixelsY-1)) ? round(y2) : (pixelsY-1)
176       
177        return(0)
178End
179
180
181
182//sums the data counts in the box specified by (x1,y1) to (x2,y2)
183//assuming that x1<x2, and y1<y2
184//the x,y values must also be in array coordinates[0] NOT scaled detector coords.
185//
186// accepts arbitrary detector coordinates. calling function is responsible for
187// keeping selection in bounds
188//
189Function V_SumCountsInBox(x1,x2,y1,y2,ct_err,type,detStr)
190        Variable x1,x2,y1,y2,&ct_err
191        String type,detStr
192       
193        Variable counts = 0,ii,jj,err2_sum
194       
195// get the waves of the data and the data_err
196        Wave w = V_getDetectorDataW(type,detStr)
197        Wave data_err = V_getDetectorDataErrW(type,detStr)
198
199                       
200        err2_sum = 0            // running total of the squared error
201        ii=x1
202        jj=y1
203        do
204                do
205                        counts += w[ii][jj]
206                        err2_sum += data_err[ii][jj]*data_err[ii][jj]
207                        jj+=1
208                while(jj<=y2)
209                jj=y1
210                ii+=1
211        while(ii<=x2)
212       
213        err2_sum = sqrt(err2_sum)
214        ct_err = err2_sum
215       
216//      Print "error = ",ct_err
217//      Print "error/counts = ",ct_err/counts
218       
219        Return (counts)
220End
221
222
223Function V_FindCentroid() :  GraphMarquee
224
225//      //get the current displayed data (so the correct folder is used)
226//      SVAR cur_folder=root:myGlobals:gDataDisplayType
227//      String dest = "root:Packages:NIST:" + cur_folder
228       
229        Variable xzsum,yzsum,zsum,xctr,yctr
230        Variable left,right,bottom,top,ii,jj,counts
231       
232
233       
234        GetMarquee left,bottom
235        if(V_flag == 0)
236                Print "There is no Marquee"
237        else
238                left = round(V_left)            //get integer values for selection limits
239                right = round(V_right)
240                top = round(V_top)
241                bottom = round(V_bottom)
242
243                // NOTE:
244                // this function MODIFIES x and y values on return, converting them to panel coordinates
245                // detector panel is identified from the (left,top) coordinate (x1,y2)
246                String detStr = V_FindDetStrFromLoc(left,right,bottom,top)             
247        //      Printf "Detector = %s\r",detStr
248       
249        //
250                SVAR gCurDispType = root:Packages:NIST:VSANS:Globals:gCurDispType       
251                // this function will modify the x and y values (passed by reference) as needed to keep on the panel
252                V_KeepSelectionInBounds(left,right,bottom,top,detStr,gCurDispType)
253                Print left,right,bottom,top
254                       
255               
256                // selection valid now, calculate beamcenter
257                // get the waves of the data and the data_err
258                Wave data = V_getDetectorDataW(gCurDispType,detStr)
259                Wave data_err = V_getDetectorDataErrW(gCurDispType,detStr)
260       
261                xzsum = 0
262                yzsum = 0
263                zsum = 0
264                // count over rectangular selection, doing each row, L-R, bottom to top
265                ii = bottom -1
266                do
267                        ii +=1
268                        jj = left-1
269                        do
270                                jj += 1
271                                counts = data[jj][ii]
272                                xzsum += jj*counts
273                                yzsum += ii*counts
274                                zsum += counts
275                        while(jj<right)
276                while(ii<top)
277               
278                xctr = xzsum/zsum
279                yctr = yzsum/zsum
280               
281                // add 1 to each to get to detector coordinates (1,128)
282                // rather than the data array which is [0,127]
283//              xctr+=1
284//              yctr+=1
285               
286                Print "X-center (in array coordinates 0->n-1 ) = ",xctr
287                Print "Y-center (in array coordinates 0->n-1 ) = ",yctr
288        endif
289       
290        //back to root folder (redundant)
291        SetDataFolder root:
292       
293End
Note: See TracBrowser for help on using the repository browser.