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

Last change on this file since 1109 was 1109, checked in by srkline, 4 years ago

Many changes:

Made the VCALC panel aware of all of the binning options
Corrected the behavior of the VCALC preset conditions
Adjusted how the Slit data is binned so that there are not duplicated q-values in the output

Made Absolute scaling aware of the back detector. Now the ABS String in the protocol has a second
set of scaling constants tagged with "_B" for the back detector. There is an added button
on the protocol panel to set the second set of constants. For the back detector, the read noise
is subtracted by reading it from the empty beam file (shifting over to the right by one box width)
All of the associated abs procedures are now aware of this.
More error checking needs to be added.

Back detector image is now shifted upon loading of the data. the default mask takes this into account
and masks out the padded (zero) regions.

in the protocol, DIV and MSK do not use grep any longer. it was just way too slow. Now it depends on

the file name having DIV or MASK respectively.



Raw data files can now be added together, in the usual way from the protocol panel.



File size: 12.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// 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//
33Function 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
68End
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)
74Function/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       
152End
153
154//testing function only, not called anywhere
155Function 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)
162End
163
164// for the given detector
165Function 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)
179End
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//
190Function 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)
221End
222
223
224//sums the data counts in the box specified by (x1,y1) to (x2,y2)
225//assuming that x1<x2, and y1<y2
226//the x,y values must also be in array coordinates[0] NOT scaled detector coords.
227//
228// accepts arbitrary detector coordinates. calling function is responsible for
229// keeping selection in bounds
230//
231// basically the same as V_SumCountsInBox, except the PBR value has been removed so that the
232// function can be used from the command line
233//
234Function V_SumCountsInBox_Cmd(x1,x2,y1,y2,type,detStr)
235        Variable x1,x2,y1,y2
236        String type,detStr
237       
238        Variable counts = 0,ii,jj,err2_sum,ct_err
239       
240// get the waves of the data and the data_err
241        Wave w = V_getDetectorDataW(type,detStr)
242        Wave data_err = V_getDetectorDataErrW(type,detStr)
243
244                       
245        err2_sum = 0            // running total of the squared error
246        ii=x1
247        jj=y1
248        do
249                do
250                        counts += w[ii][jj]
251                        err2_sum += data_err[ii][jj]*data_err[ii][jj]
252                        jj+=1
253                while(jj<=y2)
254                jj=y1
255                ii+=1
256        while(ii<=x2)
257       
258        err2_sum = sqrt(err2_sum)
259        ct_err = err2_sum
260
261        Print "sum of counts = ",counts
262        Print "error = ",ct_err
263        Print "error/counts = ",ct_err/counts
264       
265        Return (counts)
266End
267
268Proc pV_SumCountsInBox_Cmd(x1,x2,y1,y2,type,detStr) : GraphMarquee
269        Variable x1=280,x2=430,y1=350,y2=1020
270        String type="RAW",detStr="B"
271       
272        V_SumCountsInBox_Cmd(x1,x2,y1,y2,type,detStr)
273End
274
275Function V_FindCentroid() :  GraphMarquee
276
277//      //get the current displayed data (so the correct folder is used)
278//      SVAR cur_folder=root:myGlobals:gDataDisplayType
279//      String dest = "root:Packages:NIST:" + cur_folder
280       
281        Variable xzsum,yzsum,zsum,xctr,yctr
282        Variable left,right,bottom,top,ii,jj,counts
283        Variable x_mm_sum,y_mm_sum,x_mm,y_mm
284
285       
286        GetMarquee left,bottom
287        if(V_flag == 0)
288                Print "There is no Marquee"
289        else
290                left = round(V_left)            //get integer values for selection limits
291                right = round(V_right)
292                top = round(V_top)
293                bottom = round(V_bottom)
294
295                // detector panel is identified from the (left,top) coordinate (x1,y2)
296                String detStr = V_FindDetStrFromLoc(left,right,bottom,top)             
297        //      Printf "Detector = %s\r",detStr
298       
299        //
300                SVAR gCurDispType = root:Packages:NIST:VSANS:Globals:gCurDispType       
301                // this function will modify the x and y values (passed by reference) as needed to keep on the panel
302                V_KeepSelectionInBounds(left,right,bottom,top,detStr,gCurDispType)
303                Print left,right,bottom,top
304                       
305               
306                // selection valid now, calculate beamcenter
307                // get the waves of the data and the data_err
308                Wave data = V_getDetectorDataW(gCurDispType,detStr)
309                Wave data_err = V_getDetectorDataErrW(gCurDispType,detStr)
310               
311                // get the real-space information
312                String destPath = "root:Packages:NIST:VSANS:"+gCurDispType
313                Wave data_realDistX = $(destPath + ":entry:instrument:detector_"+detStr+":data_realDistX")
314                Wave data_realDistY = $(destPath + ":entry:instrument:detector_"+detStr+":data_realDistY")
315       
316                xzsum = 0
317                yzsum = 0
318                zsum = 0
319                x_mm_sum = 0
320                y_mm_sum = 0
321               
322                // count over rectangular selection, doing each row, L-R, bottom to top
323                ii = bottom -1
324                do
325                        ii +=1
326                        jj = left-1
327                        do
328                                jj += 1
329                                counts = data[jj][ii]
330                                xzsum += jj*counts
331                                yzsum += ii*counts
332                                zsum += counts
333                               
334                                x_mm_sum += data_realDistX[jj][ii]*counts
335                                y_mm_sum += data_realDistY[jj][ii]*counts
336                        while(jj<right)
337                while(ii<top)
338               
339                xctr = xzsum/zsum
340                yctr = yzsum/zsum
341               
342                x_mm = x_mm_sum/zsum
343                y_mm = y_mm_sum/zsum
344                // add 1 to each to get to detector coordinates (1,128)
345                // rather than the data array which is [0,127]
346//              xctr+=1
347//              yctr+=1
348               
349                Print "X-center (in array coordinates 0->n-1 ) = ",xctr
350                Print "Y-center (in array coordinates 0->n-1 ) = ",yctr
351               
352                Print "X-center (cm) = ",x_mm/10
353                Print "Y-center (cm) = ",y_mm/10
354               
355                if(cmpstr(detStr,"FR") == 0)
356                        Print "FRONT Reference X-center (cm) = ",x_mm/10
357                        Print "FRONT Reference Y-center (cm) = ",y_mm/10
358                endif
359
360                if(cmpstr(detStr,"MR") == 0)
361                        Print "MIDDLE Reference X-center (cm) = ",x_mm/10
362                        Print "MIDDLE Reference Y-center (cm) = ",y_mm/10
363                endif
364               
365// if measured on the LEFT panel, convert to the RIGHT coordinates for the reference value     
366// these corrections are exactly the opposite of what is done in V_fDeriveBeamCenters(xFR,yFR,xMR,yMR)
367                if(cmpstr(detStr,"FL") == 0)
368                        Print "FRONT Reference X-center (cm) = ",x_mm/10 + (0.03 + 0.03)/2
369                        Print "FRONT Reference Y-center (cm) = ",y_mm/10 - (0.34 + 0.32)/2
370                endif
371               
372                if(cmpstr(detStr,"ML") == 0)
373                        Print "MIDDLE Reference X-center (cm) = ",x_mm/10 + (0.06 + 0.05)/2
374                        Print "MIDDLE Reference Y-center (cm) = ",y_mm/10 - (0.14 + 0.01)/2
375                endif
376        endif
377       
378        //back to root folder (redundant)
379        SetDataFolder root:
380       
381End
382
383
384
385//
386//function writes new box coordinates to the data file
387//
388// also writes the panel where the coordinates were set (non-nice field in /reduction)
389//
390Function V_UpdateBoxCoords() :  GraphMarquee
391        GetMarquee left,bottom
392        if(V_flag == 0)
393                Print "There is no Marquee"
394        else
395                Variable count,x1,x2,y1,y2,ct_err
396                x1 = V_left
397                x2 = V_right
398                y1 = V_bottom
399                y2 = V_top
400
401//              Print x1,x2,y1,y2
402
403                // NOTE:
404                // this function MODIFIES x and y values on return, converting them to panel coordinates
405                // detector panel is identified from the (left,top) coordinate (x1,y2)
406                String detStr = V_FindDetStrFromLoc(x1,x2,y1,y2)               
407//
408                SVAR gCurDispType = root:Packages:NIST:VSANS:Globals:gCurDispType
409                string boxStr
410                // this function will modify the x and y values (passed by reference) as needed to keep on the panel
411                V_KeepSelectionInBounds(x1,x2,y1,y2,detStr,gCurDispType)
412                sprintf boxStr,"%d;%d;%d;%d;",x1,x2,y1,y2
413
414//              Print x1,x2,y1,y2
415               
416                SVAR gCurrentFile = root:Packages:NIST:VSANS:Globals:gLastLoadedFile            //for the status of the display
417
418                V_writeBoxCoordinates(gCurrentFile,V_List2NumWave(boxStr,";","inW"))
419       
420                V_writeReduction_BoxPanel(gCurrentFile,detStr)
421
422//              count = V_SumCountsInBox(x1,x2,y1,y2,ct_err,gCurDispType,detStr)               
423//              Print "counts = ",count
424//              Print "err/counts = ",ct_err/count
425
426                // kill the file from RawVSANS so that the updated box coordinates will be re-read in
427                //
428                V_KillNamedDataFolder(gCurrentFile)
429        endif
430End
Note: See TracBrowser for help on using the repository browser.