source: sans/SANSReduction/branches/kline_29MAR07/Put in User Procedures/SANS_Reduction_v5.00/RawWindowHook.ipf @ 72

Last change on this file since 72 was 41, checked in by srkline, 16 years ago

change to UNIX line endings

File size: 14.6 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma version=5.0
3#pragma IgorVersion=4.0
4
5//*****************************
6// Vers. 1.2 092101
7//
8// hook function and associated procedures that interact with the user
9// and the SANS_Data window
10// -displays pixel counts
11// - displays Q, qx, qy values
12// - displays q axes and pixel axes
13//
14// - of course, displays the detector image, w/ nice colors, legend, sliders to adjust color mapping
15// and a control bar to let the user adjust scaling, do averaging...
16//
17// allows the display default to be set to log scaling
18//*****************************
19
20
21//main procedure for display of SANS data files
22//uses "hook" functions to interact witht the user to get live cursor readout
23//based on the IGOR Pro Demo experiment- "SimpleDemoHook" - see the original
24//demo for more details of implementation
25//
26Function fRawWindowHook()
27        //globals for the main data display window are kept in root:myGlobals
28        Variable/G root:myGlobals:gXPos=0
29        Variable/G root:myGlobals:gYPos=0
30        Variable/G root:myGlobals:gQX=0
31        Variable/G root:myGlobals:gQY=0
32        Variable/G root:myGlobals:gQQ=0
33        Variable/G root:myGlobals:gNCounts=0
34        String/G root:myGlobals:gCurDispFile = "default string"
35        String/G root:myGlobals:gCurTitle = ""
36//      Variable/G root:myGlobals:gMapLow=0.
37//      Variable/G root:myGlobals:gMapHigh=1
38        Make/O/N=2 root:myGlobals:q_x_axis,root:myGlobals:q_y_axis
39       
40        NVAR pixelsX = root:myGlobals:gNPixelsX
41        NVAR pixelsY = root:myGlobals:gNPixelsY
42       
43        //get the current displayed data (so the correct folder is used)
44        SVAR cur_folder=root:myGlobals:gDataDisplayType
45        SVAR cur_title = root:myGlobals:gCurTitle
46        String curPath = "root:"+cur_folder
47        Wave/T tw=$(curPath+":TextRead")
48        cur_title = tw[6]                       //always update the title string
49       
50        DoWindow/F SANS_Data
51        if( V_Flag==0 )
52                //no SANS_Data window - make one
53
54                //Create NIH colors if needed
55                if(!WaveExists($"root:myGlobals:NIHColors"))
56                        NIHColorIndex()
57                Endif
58               
59                //window creation stuff
60                Display /W=(10,50,400,490) /K=1 //K=1 flag suppresses saveMacro dialog
61                DoWindow/C SANS_Data
62                DoWindow/T SANS_Data,cur_folder
63                SetWindow SANS_Data,hook=RawWindowHook,hookevents=2
64                ControlBar 100
65                Button doAve,pos={290,23},size={50,20},proc=ShowAvgPanel_SANSData,title="I vs. Q"
66                Button doAve,help={"This will circularly average the data to I vs. Q"}
67                SetVariable xpos,pos={7,5},size={50,17},title="X"
68                SetVariable xpos,limits={-Inf,Inf,0},value= root:myGlobals:gXPos
69                SetVariable xpos,help={"x-position on the detector"},frame=0,noedit=1
70                SetVariable ypos,pos={7,29},size={50,17},title="Y"
71                SetVariable ypos,limits={-Inf,Inf,0},value= root:myGlobals:gYPos
72                SetVariable ypos,help={"y-position on the detector"},frame=0,noedit=1
73                SetVariable counts,pos={7,59},size={150,17},title="Counts"
74                SetVariable counts,limits={-Inf,Inf,0},value= root:myGlobals:gNCounts
75                SetVariable counts,help={"Neutron counts"},frame=0,noedit=1
76                Button bisLin,pos={230,23},size={50,20},proc=Log_Lin,title="isLin"
77                Button bisLin,help={"\"isLin\" means the counts displayed are on linear scale. \"isLog\" means the counts displayed are on log(base10) scale."}
78                SetVariable qxval,pos={68,2},size={85,17},title="qX"
79                SetVariable qxval,help={"q value in the x-direction on the detector"},frame=0,noedit=1
80                SetVariable qxval,format="%+7.5f",limits={-Inf,Inf,0},value= root:myGlobals:gQX
81                SetVariable qyval,pos={68,21},size={85,17},title="qY"
82                SetVariable qyval,help={"q value in the y-direction on the detector"},frame=0,noedit=1
83                SetVariable qyval,format="%+7.5f",limits={-Inf,Inf,0},value= root:myGlobals:gQY
84                SetVariable q_pos,pos={68,40},size={85,17},title="q "
85                SetVariable q_pos,help={"q-value on the detector at (x,y)"},format="%+7.5f"
86                SetVariable q_pos,limits={-Inf,Inf,0},value= root:myGlobals:gQQ,frame=0,noedit=1
87                SetVariable CurFile,pos={170,4},size={210,17},title="File"
88                SetVariable CurFile,help={"Currently displayed file"},frame=0,noedit=1
89                SetVariable CurFile,limits={-Inf,Inf,0},value= root:myGlobals:gCurDispFile
90               
91                SetVariable CurTitle,pos={4,81},size={260,17},title=" "//,title="LABEL:"
92                SetVariable CurTitle,help={"Title string for currently displayed file"},frame=0,noedit=1
93                SetVariable CurTitle,fstyle=1,limits={-Inf,Inf,0},value= root:myGlobals:gCurTitle
94                Button Print_status,pos={170,23},size={50,20},proc=StatusButton,title="Status"
95                Button Print_status,help={"Print out information about the currently displayed file into the history window"}
96//              SetVariable lowThresh,pos={170,55},size={110,17},proc=LowThreshSetVarProc,title="Map low"
97//              SetVariable lowThresh,limits={0,1,0.01},value= root:myGlobals:gMapLow
98//              SetVariable lowThresh,help={"Low limit of threshold as a fraction of maximum data value"}
99//              SetVariable highThresh,pos={285,55},size={120,17},proc=HighThreshSetVarProc,title="Map high"
100//              SetVariable highThresh,limits={0,1,0.01},value= root:myGlobals:gMapHigh
101//              SetVariable highThresh,help={"High limit of threshold as a fraction of maximum data value"}
102                Button maskButton size={84,20}, pos={170,53}, proc=maskButtonProc,title="Show Mask"
103                Button maskButton help={"If a mask has been loaded this will overlay it on the current plot"}
104                Slider loSlide,pos={280,77},size={100,16},proc=MapSliderProc
105                Slider loSlide,limits={0,1,0.01},value= 0,vert= 0,ticks= 0
106                Slider loSlide,help={"Adjust the low threshold of the color map"}
107                Slider hiSlide,pos={280,60},size={100,16},proc=MapSliderProc
108                Slider hiSlide,limits={0,1,0.01},value= 1,vert= 0,ticks= 0
109                Slider hiSlide,help={"Adjust the high threshold of the color map"}
110                GroupBox slideGrp,pos={268,46},size={124,51},title="Color Map"
111        endif
112       
113        //window was already open, or has just been created, add (or remove)
114        //controls specific for RAW data (specified by the current folder)
115        if(cmpstr(cur_folder,"RAW")==0)
116                //show the "next" buttons
117                //these buttons should only be available in RAW data type
118                Button backOne size={20,20},pos={350,23},proc=BackOneFileButtonProc,title="<"
119                Button backOne help={"Display the previous RAW data file run number"}
120                Button forwardOne size={20,20},pos={375,23},proc=ForwardOneFileButtonProc,title=">"
121                Button forwardOne help={"Display the next RAW data file run number"}
122                //
123        else
124                //kill them
125                KillControl backOne
126                KillControl forwardOne
127        Endif
128       
129        //reset the slider values to 0,1
130        Slider loSlide,value=0
131        Slider hiSlide,value=1
132       
133        //remove old data and add new data to it
134        //data display/modification stuff
135        RemoveImage/Z data
136        WAVE data = $(curPath + ":data")
137        WAVE NIHColors = $"root:myGlobals:NIHColors"
138        AppendImage data
139        WaveStats/Q $(curPath + ":data")
140        if(cmpstr(cur_folder,"MSK")==0)
141                ModifyImage data ctab={0,1,BlueRedGreen,0}
142   else
143                //Call the procedure that would normally be called if the sliders were moved
144//              MapSliderProc("both", 0, 1)
145                MapSliderProc("reset", 0, 1)
146    //  ScaleColorsToData(V_min, V_max, NIHColors)
147         //  ModifyImage data cindex=NIHColors
148        endif
149        //make the pixels square, color the backgrounds
150        ModifyGraph width={plan,1,bottom,left},mirror=0
151        ModifyGraph axisenab(bottom)={0,0.7}
152        ModifyGraph axOffset(left)=-3
153        ModifyGraph standoff=0
154        ModifyGraph wbRGB=(65535,54611,49151),gbRGB=(65535,54611,49151),cbRGB=(1,52428,52428)
155       
156        //add the qx and qy axes
157        Wave q_x_axis=$"root:myGlobals:q_x_axis"
158        Wave q_y_axis=$"root:myGlobals:q_y_axis"
159        Set_Q_Axes(q_x_axis,q_y_axis,curPath)
160        RemoveFromGraph/Z q_x_axis,q_y_axis
161        AppendToGraph/T q_x_axis
162        AppendToGraph/R=Right_Q q_y_axis                                //plot on a free axis, crossing at x=127 (pixelsX)
163        ModifyGraph freePos(Right_q)={pixelsX-1,bottom}
164        ModifyGraph minor(top)=1,minor(Right_Q)=1,lowTrip(top)=1e-05,lowTrip(Right_Q)=1e-05
165        ModifyGraph mode(q_x_axis)=2,mode(q_y_axis)=2           //dots
166        ModifyGraph axisEnab(top)={0,0.7}
167
168        //add the color bar
169        ColorScale/N=colBar/A=RT/X=-3/Y=-1.5/Z=1 image=data, heightPct=100, widthPct=4,notation=1
170        ColorScale/C/N=colBar/B=(65535,60076,49151)
171       
172        //update the displayed filename, using FileList in the current data folder
173        SVAR FileList = $(curPath + ":FileList")
174        String/G root:myGlobals:gCurDispFile = FileList
175       
176        //update the window title
177        DoWindow/T SANS_Data,cur_folder
178       
179        // reset the initial state of the "isLin" button if it is reading "isLog", since the initial data state is
180        //always set to linear
181        //re-draw the data on the graph to make sure "data" from the current folder is being used
182        ControlInfo bisLog
183        if(V_flag ==1)  //if bisLog exists, this will return true
184                Button bisLog,title="isLin",rename=bisLin
185        endif
186        //now that button state and data are sure to match (both are linear)
187        // set the display to log scale, if the global has been set
188        NVAR gLogScalingAsDefault=root:myGlobals:gLogScalingAsDefault
189        if(gLogScalingAsDefault)
190                Log_lin("bisLin")
191        endif
192       
193        //force an update of everything in the window
194        DoUpdate
195       
196        //return data folder to root before exiting (redundant)
197        SetDataFolder root:     
198End
199
200//this is the hook function that is associated with the SANS_Data graph window
201//only mouse moved events are processed, although much more could be done
202//for more elaborate interaction with the user.
203//- sets globals (that are displayed in the control bar of the graph) for the
204//x and y positions (in Detector coordinates (1,128))
205//qx, qy, and q (in Angstroms)
206//and the actual neutron counts (if raw)
207//or the data array value
208//
209Function RawWindowHook(s)
210        String s
211       
212        //get the current displayed data (so the correct folder is used)
213        SVAR cur_folder=root:myGlobals:gDataDisplayType
214        SetDataFolder "root:"+cur_folder                //use the full path, so it will always work
215        String curPath = "root:" + cur_folder
216        NVAR dataIsLog=$(curPath + ":gIsLogScale")              //now a global variable in the current folder, not the globals folder
217        if (dataIsLog)
218                wave w=$(curPath + ":linear_data")
219        else
220                wave w=$(curPath + ":data")
221      endif
222        wave reals=$(curPath + ":realsread")
223       
224        String/G root:myGlobals:gHookStr= s
225        Variable xpix,ypix,xaxval,yaxval,xint,yint,rawval
226        String msg
227       
228        NVAR pixelsX = root:myGlobals:gNPixelsX
229        NVAR pixelsY = root:myGlobals:gNPixelsY
230       
231        //only do something for mousemoved events
232        if( StrSearch(s,"EVENT:mousemoved;",0) > 0 )
233                xpix= NumberByKey("MOUSEX",s)
234                ypix= NumberByKey("MOUSEY",s)
235                xaxval= AxisValFromPixel("","bottom",xpix)
236                yaxval= AxisValFromPixel("","left",ypix)
237                //xint = trunc(xaxval)
238                //yint = trunc(yaxval)
239                xint = round(xaxval)
240                yint = round(yaxval)
241               
242                if((xint<0) || (xint>pixelsX-1) || (yint<0) || (yint>pixelsY-1))        //make sure cursor is on the image
243                        rawval = 0
244                else
245                        rawval = w[xint][yint] 
246                        //update q, qX, and qY
247                        if(cmpstr(cur_folder,"MSK")!=0)
248                                Variable xctr=reals[16],yctr=reals[17],sdd=reals[18],lam=reals[26]
249                                Variable/G root:myGlobals:gQQ = CalcQval(xaxval+1,yaxval+1,xctr,yctr,sdd,lam)
250                                Variable/G root:myGlobals:gQX = CalcQX(xaxval+1,xctr,sdd,lam)
251                                Variable/G root:myGlobals:gQY = CalcQY(yaxval+1,yctr,sdd,lam)
252                        else
253                                Variable/G root:myGlobals:gQQ = 0
254                                Variable/G root:myGlobals:gQX = 0
255                                Variable/G root:myGlobals:gQY = 0
256                        endif
257                        //add one to the x and y values to get from IGOR array indexing 0->127 to 1->128 detector
258                        Variable/G root:myGlobals:gXPos=xint+1
259                        Variable/G root:myGlobals:gYPos=yint+1
260                        Variable/G root:myGlobals:gNCounts=rawval
261                endif
262        endif
263       
264        //set data folder back to root
265        SetDataFolder root:
266       
267        return 0
268end
269       
270//function to calculate the overall q-value, given all of the necesary trig inputs
271//NOTE: detector locations passed in are pixels = 0.5cm real space on the detector
272//and are in detector coordinates (1,128) rather than axis values
273//the pixel locations need not be integers, reals are ok inputs
274//sdd is in meters
275//wavelength is in Angstroms
276//returned q-value is in 1/Angstroms
277//
278// generalized to read the detector pixel dimension from the file header...
279//
280Function CalcQval(xaxval,yaxval,xctr,yctr,sdd,lam)
281        Variable xaxval,yaxval,xctr,yctr,sdd,lam
282       
283        Variable dx,dy,thetax,thetay,qval,qx,qy
284       
285        Wave realW=$"root:raw:realsRead"
286        Variable pixSizeX = realW[10]/10                //header is in mm, want cm
287        Variable pixSizeY = realW[13]/10                //header is in mm, want cm
288       
289        sdd *=100               //convert to cm
290        dx = (xaxval - xctr)*pixSizeX           //delta x in cm
291        dy = (yaxval - yctr)*pixSizeY           //delta y in cm
292        thetax = atan(dx/sdd)
293        thetay = atan(dy/sdd)
294        qx = 4*Pi/lam*sin(thetax/2)
295        qy = 4*Pi/lam*sin(thetay/2)
296        qval = sqrt(qx^2 + qy^2)
297       
298        return qval
299End
300
301//calculates just the q-value in the x-direction on the detector
302//input/output is the same as CalcQval()
303//ALL inputs are in detector coordinates
304//
305// generalized to read the detector pixel dimension from the file header...
306//
307Function CalcQX(xaxval,xctr,sdd,lam)
308        Variable xaxval,xctr,sdd,lam
309        //NOTE: detector locations passed in are pixel = 0.5cm real space on the Ordela detector
310        //sdd is in meters
311        //wavelength is in Angstroms
312       
313        Wave realW=$"root:raw:realsRead"
314        Variable pixSize = realW[10]/10         //header is in mm, want cm
315       
316        Variable dx,thetax,qx
317       
318        sdd *=100               //convert to cm
319        dx = (xaxval - xctr)*pixSize    //delta x in cm
320        thetax = atan(dx/sdd)
321        qx = 4*Pi/lam*sin(thetax/2)
322       
323        return qx
324End
325
326//calculates just the q-value in the y-direction on the detector
327//input/output is the same as CalcQval()
328//ALL inputs are in detector coordinates
329//
330// generalized to read the detector pixel dimension from the file header...
331//
332Function CalcQY(yaxval,yctr,sdd,lam)
333        Variable yaxval,yctr,sdd,lam
334        //NOTE: detector locations passed in are pixel = 0.5cm real space on the Ordela detector
335        //sdd is in meters
336        //wavelength is in Angstroms
337       
338        Wave realW=$"root:raw:realsRead"
339        Variable pixSize = realW[13]/10         //header is in mm, want cm
340               
341        Variable dy,thetay,qy
342       
343        sdd *=100               //convert to cm
344        dy = (yaxval - yctr)*pixSize            //delta y in cm
345        thetay = atan(dy/sdd)
346        qy = 4*Pi/lam*sin(thetay/2)
347       
348        return qy
349End
350
351//function to set the q-axis scaling after the data has been read in
352// - needs the location of the currently displayed data to get the header information
353// to be able to calculate q-values at the edges of the detector
354//** assumes a linear correcpondence between pixel->q-values (which should bea a really
355// safe bet, since we're using the small -angle approximation...)
356//
357// actually re-scales the qy wave that is on the SANS_Data image
358// the qy dataset is 2 values, plotted as "dots", nearly invisible...
359// but does an adequate job of getting ticks on the right and top axes
360//
361Function Set_Q_Axes(qx,qy,curPath)
362        Wave qx,qy
363        String curPath
364
365        NVAR pixelsX = root:myGlobals:gNPixelsX
366        NVAR pixelsY = root:myGlobals:gNPixelsY
367       
368        WAVE reals=$(curPath + ":realsread")
369        Variable xctr=reals[16],yctr=reals[17],sdd=reals[18],lam=reals[26]
370        Variable maxX,minX,maxY,minY
371       
372        minX = CalcQX(1,xctr,sdd,lam)
373        maxX = CalcQX(pixelsX,xctr,sdd,lam)
374        SetScale/I x minX,maxX,"",qx
375       
376        minY = CalcQY(1,yctr,sdd,lam)
377        maxY = CalcQY(pixelsY,yctr,sdd,lam)
378        qy[0] = minY
379        qy[1] = maxY
380       
381        return(0)
382End
383
384Function ToggleDefaultMapping()
385        NVAR value = root:myGlobals:gLogScalingAsDefault
386        value = !(value)
387End
388
Note: See TracBrowser for help on using the repository browser.