source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/USANS/COR_Graph.ipf @ 629

Last change on this file since 629 was 624, checked in by ajj, 13 years ago

Implement XML writing in USANS reduction

  • Property eol-style set to native
  • Property svn:executable set to *
File size: 17.8 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma Version=2.20
3#pragma IgorVersion=6.1
4
5////////////////////////////
6// 101001 Vers. 1
7//
8// procedures to plot the sma, emp, and cor data
9// - user interaction is through control bar on COR_Graph
10// - provides user with feedback about peak angle, transmissions
11// - asks for information about BKG and EMP levels
12// - asks for sample thickness
13// - interactively selects range of data to save (and type)
14// - dispatches to routines to determine transmissions and correct the data
15// - dispatches to save routines
16//
17/////////////////////////////
18
19
20//plot all that is available in the root:Graph folder
21// no distinction as to what the status of the data really is
22// "Clr" buttons on the USANS_Panel will clear the graph..
23//
24Function DoCORGraph()
25
26        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
27       
28        DoWindow/F COR_Graph
29        if(V_flag==0)
30                //draw the blank window and the control bar
31                Display /W=(5,42,450,550) /K=1
32                DoWindow/C COR_Graph
33                ControlBar 150
34                SetVariable gTransWide,pos={210,12},size={135,15},title="Trans - Wide",format="%5.4f"
35                SetVariable gTransWide,help={"Average counts on transmssion detector at wide angles"}
36                SetVariable gTransWide,limits={0,1,0.001},value= $(USANSFolder+":Globals:MainPanel:gTransWide")
37                SetVariable gTransRock,pos={210,27},size={135,15},title="Trans - Rock",format="%5.4f"
38                SetVariable gTransRock,help={"Transmission counts at the zero-angle peak"}
39                SetVariable gTransRock,limits={0,1,0.001},value= $(USANSFolder+":Globals:MainPanel:gTransRock")
40                SetVariable gEmpCts,pos={210,42},size={135,15},title="EMP Level",format="%7.4f"
41                SetVariable gEmpCts,limits={-Inf,Inf,0.1},value= $(USANSFolder+":Globals:MainPanel:gEmpCts")
42                SetVariable gEmpCts,help={"High q limit of empty cell scattering normalized to 1.0e6 monitor counts"}
43                SetVariable gBkgCts,pos={210,57},size={135,15},title="BKG Level",format="%7.4f"
44                SetVariable gBkgCts,limits={-Inf,Inf,0.1},value= $(USANSFolder+":Globals:MainPanel:gBkgCts")
45                SetVariable gBkgCts,help={"Background scattering level normalized to 1.0e6 monitor counts"}
46                SetVariable gThick,pos={210,72},size={135,15},title="SAM Thick(cm)",format="%5.4f"
47                SetVariable gThick,help={"Thickness of the sample in centimeters"}
48                SetVariable gThick,limits={0,5,0.01},value= $(USANSFolder+":Globals:MainPanel:gThick")
49                Button UpdateButton,pos={125,15},size={70,40},proc=UpdateButtonProc,title="Update\rTrans"
50                Button UpdateButton,help={"Updates both the wide and rocking transmission values based on the raw data files"}
51                Button CorrectButton,pos={125,60},size={70,40},proc=CorrectButtonProc,title="Correct\rData"
52                Button CorrectButton,help={"Corrects the sample data by subtracting empty cell and backgrond scattering"}
53                Button SaveDataButton,pos={355,3},size={85,20},proc=SaveButtonProc,title="Save Data..."
54                Button SaveDataButton,help={"Saves the selected data type to disk in ASCII format"}
55                CheckBox useCrsrCheck,pos={360,27},size={119,14},proc=UseCrsrCheckProc,title="Use Cursors?"
56                CheckBox useCrsrCheck,value= 0
57                CheckBox useCrsrCheck,help={"Adds cursors to the datset to select the range of data points to save"}
58                CheckBox CORCheck,pos={380,44},size={40,14},title="COR",value=1,proc=TypeToggleCheckProc,mode=1
59                CheckBox CORCheck,help={"Selects COR data as the saved type"}
60                CheckBox SAMCheck,pos={380,60},size={40,14},title="SAM",value=0,proc=TypeToggleCheckProc,mode=1
61                CheckBox SAMCheck,help={"Selects SAM data s the saved type"}
62                CheckBox EMPCheck,pos={380,76},size={40,14},title="EMP",value= 0,proc=TypeToggleCheckProc,mode=1
63                CheckBox EMPCheck,help={"Selects EMP data as the saved type"}
64                Button qpkButton,pos={12,61},size={90,20},proc=QpkButtonProc,title="Change Qpk"
65                Button qpkButton,help={"Use this to override the automatically determined peak locations in SAM or EMP datasets"}
66                ValDisplay valdispSAM,pos={10,15},size={100,14},title="Qpk SAM",format="%1.3f"
67                ValDisplay valdispSAM,limits={0,0,0},barmisc={0,1000},value=QpkFromNote("SAM")
68                ValDisplay valdispSAM,help={"Displays the peak angle the raw SAM data, determined automatically"}
69                ValDisplay valdispEMP,pos={10,36},size={100,14},title="Qpk EMP",format="%1.3f"
70                ValDisplay valdispEMP,limits={0,0,0},barmisc={0,1000},value=QpkFromNote("EMP")
71                ValDisplay valdispEMP,help={"Displays the peak angle the raw EMP data, determined automatically"}
72       
73                CheckBox check0 title="Log X-axis",proc=LogLinToggleCheckProc
74                CheckBox check0 pos={12,100},value=0,mode=0
75                SetVariable setvar0,pos={210,100},size={120,20},title="Trock/Twide",format="%5.4f"
76                SetVariable setVar0,help={"fraction of unscattered neutrons"}
77                SetVariable setVar0,limits={0,2,0},value= $(USANSFolder+":Globals:MainPanel:gTransRatio")
78               
79                Legend
80        Endif
81        // add each data type to the graph, if possible (each checks on its own)
82        //SAM
83        GraphSAM()
84        //EMP
85        GraphEMP()
86        //COR
87        GraphCOR()
88        //EMP and BKG levels
89        GraphEMPBKGLevels()
90       
91        ControlUpdate/A/W=COR_Graph
92       
93        ModifyGraph log(left)=1,mirror=2,grid=1,standoff=0
94        ModifyGraph tickUnit=1
95        Label left "(Counts/sec)/(MON)*10\\S6\\M"
96        Label bottom "q (A\\S-1\\M)"
97       
98        return(0)
99End
100
101// add SAM data to the graph if it exists and is not already on the graph
102//
103Function GraphSAM()
104
105        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
106
107        //is it already on the graph?
108        SetDataFolder $(USANSFolder+":Graph")
109        String list=""
110        list = Wavelist("DetCts_SAM*",";","WIN:COR_Graph")
111        if(strlen(list)!=0)
112                //Print "SAM already on graph"
113                return(0)
114        Endif
115        //append the data if it exists
116        If(waveExists($"DetCts_SAM")==1)
117                DoWindow/F COR_Graph
118                AppendToGraph DetCts_SAM vs Qvals_SAM
119                ModifyGraph rgb(DetCts_SAM)=(1,12815,52428)
120                ModifyGraph mode(DetCts_SAM)=3,marker(DetCts_SAM)=19,msize(DetCts_SAM)=2
121                ModifyGraph tickUnit=1
122                ErrorBars/T=0 DetCts_SAM Y,wave=(ErrDetCts_SAM,ErrDetCts_SAM)
123        endif
124        SetDataFolder root:
125End
126
127// add EMP data to the graph if it exists and is not already on the graph
128//
129Function GraphEMP()
130
131        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
132
133        SetDataFolder $(USANSFolder+":Graph")
134        String list=""
135        list = Wavelist("DetCts_EMP*",";","WIN:COR_Graph")
136        if(strlen(list)!=0)
137        //      Print "EMP already on graph"
138                return(0)
139        Endif
140        //append the data if it exists
141        If(waveExists($"DetCts_EMP")==1)
142                DoWindow/F COR_Graph
143                AppendToGraph DetCts_EMP vs Qvals_EMP
144                ModifyGraph msize(DetCts_EMP)=2,rgb(DetCts_EMP)=(1,39321,19939)
145                ModifyGraph mode(DetCts_EMP)=3,marker(DetCts_EMP)=19
146                ModifyGraph tickUnit=1
147                ErrorBars/T=0 DetCts_EMP Y,wave=(ErrDetCts_EMP,ErrDetCts_EMP)
148        endif
149        SetDataFolder root:
150        return(0)
151End
152
153// add COR data to the graph if it exists and is not already on the graph
154//
155Function GraphCOR()
156        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
157
158        SetDataFolder $(USANSFolder+":Graph")
159       
160        String list=""
161        list = Wavelist("DetCts_COR*",";","WIN:COR_Graph")
162        if(strlen(list)!=0)
163        //      Print "COR already on graph"
164                return(0)
165        Endif
166        //append the data if it exists
167        If(waveExists($"DetCts_COR")==1)
168                DoWindow/F COR_Graph
169                AppendToGraph DetCts_COR vs Qvals_COR
170                ModifyGraph msize(DetCts_COR)=2,rgb(DetCts_COR)=(52428,34958,1)
171                ModifyGraph mode(DetCts_COR)=3,marker(DetCts_COR)=19
172                ModifyGraph tickUnit=1
173                ErrorBars/T=0 DetCts_COR Y,wave=(ErrDetCts_COR,ErrDetCts_COR)
174        endif
175
176        SetDataFolder root:
177        return(0)
178End
179
180// add horizoontal lines for the background and empty cell levels
181Function GraphEMPBKGLevels()
182
183        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
184
185        //if the data is on the graph, remove them and replot, to properly reset the scale
186        DoUpdate
187        String list = TraceNameList("COR_Graph",";",1)
188        //Print list
189        DoWindow/F COR_Graph
190        if(stringmatch(list,"*empLevel*")==1)
191                //remove
192                RemoveFromGraph empLevel,bkgLevel
193        Endif
194        DoUpdate
195        AppendToGraph $(USANSFolder+":EMP:EMPLevel"),$(USANSFolder+":BKG:BKGLevel")
196        ModifyGraph rgb(empLevel)=(0,0,0),lsize(bkgLevel)=2,rgb(bkgLevel)=(52428,1,1)
197        ModifyGraph lsize(empLevel)=2,offset={0,0}
198        ModifyGraph tickUnit=1
199        GetAxis/W=COR_Graph/Q bottom
200        SetScale/I x V_min,V_max,"",$(USANSFolder+":EMP:EMPLevel"),$(USANSFolder+":BKG:BKGLevel")
201        return(0)
202End
203
204// polls the control bar for proper selections of SavePath
205// checks for selected dataset from radio buttons
206// obtains the poin numbers from the cursors, if selected
207// dispatches to save routine
208//
209Function SaveButtonProc(ctrlName) : ButtonControl
210        String ctrlName
211
212        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
213        NVAR useXMLOutput = root:Packages:NIST:gXML_Write
214
215       
216        PathInfo/S savePathName
217        if(V_Flag==0)
218                DoAlert 0,"Pick a \"SavePath...\"\rNo data file has been written"
219                return(1)
220        Endif
221       
222        String type=""
223        Variable useCrsrs=0,ptA=0,ptB=0
224       
225        //check for data save type (controlled by radio buttons)
226        NVAR gRadioVal=$(USANSFolder+":Globals:MainPanel:gTypeCheck")           //1=COR,2=SAM,3=EMP
227        switch(gRadioVal)
228                case 1: //COR
229                        type="COR"
230                        break                           
231                case 2: //SAM
232                        type="SAM"
233                        break
234                case 3: //EMP
235                        type="EMP"
236                        break   
237                default:
238                        DoAlert 0,"No Radio button selected\rNo data file written"
239                        return(1)
240        endswitch
241       
242        //check for data save between cursors
243        ControlInfo UseCrsrCheck
244        useCrsrs = V_Value              //1 if checked
245        //if so, read off the point range (cursors should be on the same wave as the save type)
246        if(useCrsrs)
247                Wave xwave=$(USANSFolder+":Graph:Qvals_"+type)
248                ptA=x2pnt(xwave,xcsr(A))
249                ptB=x2pnt(xwave,xcsr(B))
250                if(ptA>ptB)
251                        ptA=x2pnt(xwave,xcsr(B))
252                        ptB=x2pnt(xwave,xcsr(A))
253                endif
254                ptA=trunc(ptA)  //make sure it's integer
255                ptB=trunc(ptB)
256                //Print ptA,ptB
257        endif
258       
259        //fill in the blanks and dispatch to save routine
260        if (useXMLOutput == 1)
261                WriteXMLUSANSWaves(type,"",ptA,ptB,1)
262        else
263                WriteUSANSWaves(type,"",ptA,ptB,1)
264        endif
265       
266        return(0)
267End
268
269//show/hide cursors depending on the checkbox selection
270//put cursors on the selected save data type
271Function UseCrsrCheckProc(ctrlName,checked) : CheckBoxControl
272        String ctrlName
273        Variable checked
274       
275        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
276       
277        NVAR gRadioVal=$(USANSFolder+":Globals:MainPanel:gTypeCheck")           //1=COR,2=SAM,3=EMP
278        String type=""
279        switch(gRadioVal)
280                case 1: //COR
281                        type="COR"
282                        break                           
283                case 2: //SAM
284                        type="SAM"
285                        break
286                case 3: //EMP
287                        type="EMP"
288                        break   
289                default:
290                        DoAlert 0,"No Radio button selected\rCan't place cursors"
291                        return(1)
292        endswitch
293        if(checked)
294                //show info and cursors, if the wave in on the graph
295                String str,yname
296                str=TraceNameList("", ";", 1)
297                yname="DetCts_"+type
298                Variable ok=WhichListItem(yname, str,";",0)
299                if(ok != -1)
300                        Wave ywave=$(USANSFolder+":Graph:DetCts_"+type)
301                        Showinfo/W=COR_Graph
302                        Cursor/A=1/P/S=1 A,$yname,0
303                        Cursor/A=0/P/S=1 B,$yname,numpnts(ywave)-1
304                else
305                        //trace is not on graph
306                        CheckBox $ctrlName, value=0             //not checked
307                        HideInfo/W=COR_Graph
308                        Cursor/K/W=COR_Graph A
309                        Cursor/K/W=COR_Graph B
310                        DoAlert 0,type+" data is not on the graph"
311                endif
312        else
313                //hide info and cursors, if there are any displayed
314                HideInfo/W=COR_Graph
315                Cursor/K/W=COR_Graph A
316                Cursor/K/W=COR_Graph B
317                CheckBox useCrsrCheck,win=COR_Graph,value= 0
318        endif
319        DoUpdate
320End
321
322//radio button control of save type
323//sets global, so buttons don't need to be polled
324Function TypeToggleCheckProc(ctrlName,checked) : CheckBoxControl
325        String ctrlName
326        Variable checked
327
328        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
329
330        NVAR gRadioVal=$(USANSFolder+":Globals:MainPanel:gTypeCheck")
331       
332        strswitch(ctrlName)
333                case "CORCheck":
334                        gRadioVal=1
335                        break
336                case "SAMCheck":
337                        gRadioVal=2
338                        break
339                case "EMPCheck":
340                        gRadioVal=3
341                        break
342        endswitch
343        CheckBox CORCheck,win=COR_Graph,value= (gRadioVal==1)
344        CheckBox SAMCheck,win=COR_Graph,value= (gRadioVal==2)
345        CheckBox EMPCheck,win=COR_Graph,value= (gRadioVal==3)
346       
347        //move the cursors to the correct trace on the graph
348        ControlInfo useCrsrCheck
349        checked=V_Value
350        UseCrsrCheckProc("useCrsrCheck",V_Value)
351End
352
353//updates the trans values and the bkg/empty values on the graph, if necessary
354//calculate the T_Wide and T_Rock from the wave notes
355// if there is an error in the wave notes, "NaN" will typically be returned
356//
357Function UpdateButtonProc(ctrlName) : ButtonControl
358        String ctrlName
359
360        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
361       
362        Wave samCts=$(USANSFolder+":SAM:DetCts")
363        Wave empCts=$(USANSFolder+":EMP:DetCts")
364        if((WaveExists(samCts)==0) || (WaveExists(empCts)==0))
365                Variable/G $(USANSFolder+":Globals:MainPanel:gTransWide")=NaN           //error
366                Variable/G $(USANSFolder+":Globals:MainPanel:gTransRock")=NaN
367                return(1)
368        Endif
369        //get the wave notes, and the transCt values
370        String samNote=note(samCts),empNote=note(empCts)
371        Variable samWide,empWide,samRock,empRock
372        samWide = NumberByKey("TWIDE",samNote,":",";")
373        empWide = NumberByKey("TWIDE",empNote,":",";")
374        samRock = NumberByKey("PEAKVAL",samNote,":",";")
375        empRock = NumberByKey("PEAKVAL",empNote,":",";")
376        Variable/G $(USANSFolder+":Globals:MainPanel:gTransWide")=samWide/empWide
377        Variable/G $(USANSFolder+":Globals:MainPanel:gTransRock")=samRock/empRock
378       
379        TransRatio()            //calculate the ratio and update
380       
381        return(0)
382End
383
384// dispatches to the function to perform the data correction
385//
386Function CorrectButtonProc(ctrlName) : ButtonControl
387        String ctrlName
388       
389        DoCorrectData()
390        return(0)
391End
392
393//button to present a simple input dialog to ask the user for the data type
394// (either SAM or EMP) and the new value of the peak angle to use.
395//
396// rarely needed, but sometimes the data can fool IGOR, and no peak can be found
397// in some cases, data may not cover the primary beam. In both of these cases it
398// is necessary to manually override the peak angle
399//
400// calls RePlotWithUserAngle to "re-do" everything
401//
402Function QpkButtonProc(ctrlName) : ButtonControl
403        String ctrlName
404       
405        Variable newPkAngle=0
406        String dataSet="SAM;EMP;",type=""
407       
408        Prompt type,"Select Data Set",popup,dataSet
409        Prompt newPkAngle, "Enter new peak ANGLE, in Degrees"
410        DoPrompt "Override the peak angle",type,newPkAngle
411        //Print newPkAngle,type
412        if(V_Flag==1)           //user cancel, exit
413                return(1)
414        endif
415        //with the new information (type and angle) re-do the whole mess
416        //...as if the "plot" button was hit, except that the angle is known...
417       
418        RePlotWithUserAngle(type,newPkAngle)
419End
420
421Function TransRatio()
422        NVAR tr = root:Packages:NIST:USANS:Globals:MainPanel:gTransRock
423        NVAR tw = root:Packages:NIST:USANS:Globals:MainPanel:gTransWide
424        NVAR rat = root:Packages:NIST:USANS:Globals:MainPanel:gTransRatio
425       
426        rat = tr/tw
427        if(rat < 0.9)
428                SetVariable setVar0 labelBack=(65535,32768,32768)
429        else
430                SetVariable setVar0 labelBack=0
431        endif
432        return(0)
433End
434
435Function LogLinToggleCheckProc(cba) : CheckBoxControl
436        STRUCT WMCheckboxAction &cba
437
438        switch( cba.eventCode )
439                case 2: // mouse up
440                        Variable checked = cba.checked                 
441                        if(checked)
442                                ModifyGraph log(bottom)=1
443                        else
444                                ModifyGraph log(bottom)=0
445                        endif
446                        break
447        endswitch
448
449        return 0
450End
451
452
453//returns the peak location found (and used) for zero angle
454//displayed on the COR_Graph
455Function QpkFromNote(type)
456        String type
457
458        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
459               
460        Wave/Z detCts=$(USANSFolder+":Graph:DetCts_"+type)
461        if(!WaveExists(detCts))
462                return(NaN)
463        Endif
464        String str=note(detcts)
465        Variable val
466       
467        val=NumberByKey("PEAKANG", str,":",";")
468        return(val)
469End
470
471//nearly identical to PlotSelectedSAMButtonProc
472// - re-loads the data and goes through all the steps as it they were new datsets
473// - DOES NOT try to find the peak angle, instead uses the input zeroAngle
474// - replaces the PEAKANG:value with the input zeroAngle
475// updates the plot with the corrected angle
476//
477Function RePlotWithUserAngle(type,zeroAngle)
478        String type
479        Variable zeroAngle
480
481        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
482       
483        //SETS the wave note with the PEAKANG value
484       
485        //loads each of the data files
486        //normalizes each file to countrate immediately
487        //appends them to the individual waves in "SAM" folder
488        //sorts by angle
489        //converts to Q-values USING SUPPLIED ANGLE
490        //
491        //get selected files from listbox (everything)
492        //use the listBox wave directly
493       
494        Wave/T listW=$(USANSFolder+":Globals:MainPanel:"+type+"Wave")
495        Variable ii,num=numpnts(listW)
496        String fname="",fpath="",str=""
497        PathInfo bt5PathName
498        fpath = S_Path
499       
500        //load, normalize, and append
501        //loop over the number of items in the list
502        for(ii=0;ii<num;ii+=1)
503                fname = fpath + listw[ii]
504                LoadBT5File(fname,"SWAP")       //overwrite what's in the SWAP folder
505                Convert2Countrate("SWAP",1)
506                if(ii==0)       //first time, overwrite
507                        NewDataWaves("SWAP",type)
508                else            //append to waves in TYPE folder
509                        AppendDataWaves("SWAP",type)
510                endif
511        endfor
512        //sort after all loaded
513        DoAngleSort(type)
514       
515        //find the peak and convert to Q-values
516        //Variable zeroAngle = FindZeroAngle("SAM")
517        //if(zeroAngle == -9999)
518                //DoAlert 0,"Couldn't find a peak - using zero as zero angle"
519        //      zeroAngle = 0
520        //Endif
521       
522        //find the peak value at the supplied angle, rather than automatic...
523        Wave tmpangle = $(USANSFolder+":"+type+":Angle")
524        Wave tmpdetCts = $(USANSFolder+":"+type+":DetCts")
525        Variable pkHt=0
526        pkHt = interp(zeroAngle,tmpangle,tmpdetcts)
527       
528        if(numtype(pkHt) != 0)          // bad but it warns, useful if only positive angles are available, use the smallest
529                str = "Can't find peak at angle = "+num2str(zeroAngle)+" Using the value at angle = "+num2str(tmpAngle[0])
530                pkHt = tmpDetCts[0]
531        endif
532
533        str=note(tmpDetCts)
534        str = ReplaceNumberByKey("PEAKANG",str,zeroAngle,":",";")
535        str = ReplaceNumberByKey("PEAKVAL",str,pkHt,":",";")
536        Note/K tmpDetCts
537        Note tmpdetCts,str
538       
539        ConvertAngle2Qvals(type,zeroAngle)
540        //find the Trans Cts for T_Wide
541        FindTWideCts(type)
542        //
543        //copy the data to plot to the root:Graph directory, and give clear names
544        if(WaveExists($(USANSFolder+":"+type+":Qvals")))
545                Duplicate/O $(USANSFolder+":"+type+":Qvals"),$(USANSFolder+":Graph:Qvals_"+type)
546        Endif
547        Duplicate/O $(USANSFolder+":"+type+":Angle"),$(USANSFolder+":Graph:Angle_"+type)
548        Duplicate/O $(USANSFolder+":"+type+":DetCts"),$(USANSFolder+":Graph:DetCts_"+type)
549        Duplicate/O $(USANSFolder+":"+type+":ErrDetCts"),$(USANSFolder+":Graph:ErrDetCts_"+type)
550       
551        //now plot the data (or just bring the graph to the front)
552        DoCORGraph()
553       
554        //update the valDisplays
555        ControlUpdate/A/W=COR_Graph             //overkill, does them all
556End
Note: See TracBrowser for help on using the repository browser.