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