source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/USANS/BT5_AddFiles.ipf @ 611

Last change on this file since 611 was 611, checked in by srkline, 13 years ago

Added USANS help for adding files. Ticket #214

File size: 29.2 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma IgorVersion=6.1
3
4
5// Version 1.0
6// SRK 7 OCT 2009
7//
8
9// Adding two raw .bt5 files:
10//      - only files with the same angle range and the same number of points can be added.
11//      - NO shifting is done - these are raw data files, and the angular shift is not known
12//      - it can be repeated multiple times to add more than two files together
13//      - the two added files are listed in the title of the summed file.
14
15// Adding two data sets:
16//      - here, two entire data sets (inlcluding the main beam) are added together after
17//         loading and normalizing and coverting to Q. The data file is NOT saved out, because
18//              it is not a format that can be re-read in for reduction.
19//      - to use the summed data set, it is first summed in a separate panel, and then transferred
20//              to either the active "SAM" or "EMP" data
21//      - since the zero offset can make the point values not exactly the same, only those within a
22//              certain tolerance are actually added. Points that cannot be added are still kept in the final
23//              data set.
24//
25
26// issues
27// - are the errors calculated correctly? I think so...
28// - if there is a direct 1:1 correspondence of the data points, saving is easy
29//   -- if not, then some points will have a different counting time
30//   -- this is always the case if I do a "full" data set.
31//
32// - convert to countrate seems like a good idea, then 1+1=2 and I can propagate errors
33//  -- but how to save the data? At this point, errCR != sqrt(CR)
34// -> answer is -- don't save the full data sets that have been summed...
35//
36// - Why can't I just use the whole set of files from the main USANS panel, and add what needs
37//   to be added? Because if there is an angle shift, I have no way of knowing which files to apply
38//   the shift to...
39//
40// - do I have anything hard-wired in the code that needs to be generalized before release?
41//
42
43
44// simple stuff to do:
45// - allow user to input Qpeak (maybe of no use?) If so, do it right away with a dialog
46//   if no peak was found.
47
48
49
50Proc ShowUSANSAddPanel()
51        DoWindow/F USANS_Add_Panel
52        if(V_Flag==0)
53                Init_AddUSANS()
54                USANS_Add_Panel()
55        Endif
56End
57
58
59// initializes the folders and globals for use with the USANS_Add_Panel
60//
61// there is some overlap of the controls from the Main_USANS panel
62// separate waves are created for the lists in :AddPanel
63//
64Proc Init_AddUSANS()
65        NewDataFolder/O root:Packages
66        NewDataFolder/O root:Packages:NIST
67        NewDataFolder/O root:Packages:NIST:USANS
68        NewDataFolder/O root:Packages:NIST:USANS:Globals
69        NewDataFolder/O/S root:Packages:NIST:USANS:Globals:AddPanel
70       
71        String/G root:Packages:NIST:USANS:Globals:gUSANSFolder  = "root:Packages:NIST:USANS"
72        String USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
73       
74        //Preference value to determine if we are outputting XML
75//      Variable/G root:Packages:NIST:USANS:Globals:gUseXMLOutput = 0
76        String/G FilterStr
77        Variable/G gAddUSANSTolerance = 0.01            // add points if x is within 1% (an arbitrary choice!)
78
79        Make/O/T/N=1 fileWave,AWave,BWave
80        fileWave=""
81        AWave=""
82        BWave=""
83
84//      Make/O/T/N=5 statusWave=""
85        Make/O/B/U/N=1 selFileW
86
87
88        SetDataFolder root:
89       
90        NewDataFolder/O $(USANSFolder+":TMP_A")
91        NewDataFolder/O $(USANSFolder+":TMP_B")
92        NewDataFolder/O $(USANSFolder+":SUM_AB")
93
94End
95
96
97//draws the USANS_Add_Panel, somewhat similar to the Main panel
98//
99// but used exclusively for adding files, and is a graph/control bar
100//
101
102Proc USANS_Add_Panel()
103        PauseUpdate; Silent 1           // building window...
104        Display /W=(100,44,900,493)/K=1 as "USANS_Add_Panel"
105        DoWindow/C USANS_Add_Panel
106        DoWindow/T USANS_Add_Panel, "Add Raw USANS Files"
107        ControlBar/L 300
108       
109//      ModifyGraph cbRGB=(36929,50412,31845)
110        ModifyGraph cbRGB=(65535,60076,49151)
111       
112//      (I don't know where these end up anyhow...)
113//      SetDrawLayer UserBack
114//      SetDrawEnv fstyle= 1
115//      DrawText 12,53,"Data Files"
116//      SetDrawEnv fstyle= 1
117//      DrawText 157,192,"Empty Scans"
118//      SetDrawEnv fstyle= 1
119//      DrawText 154,54,"Sample Scans"
120//      DrawLine 6,337,398,337
121//      DrawLine 5,33,393,33
122//      SetDrawEnv fstyle= 1
123//      DrawText 140,357,"Raw Data Header"
124//      SetDrawEnv fstyle= 1
125       
126        ListBox AfileLB,pos={5,55},size={110,230}//,proc=AddFileListBoxProc
127        ListBox AfileLB,listWave=root:Packages:NIST:USANS:Globals:AddPanel:fileWave
128        ListBox AfileLB,selWave=root:Packages:NIST:USANS:Globals:AddPanel:selFileW,mode= 4
129        ListBox A_LB,pos={149,55},size={110,90},listWave=root:Packages:NIST:USANS:Globals:AddPanel:AWave
130        ListBox A_LB,mode= 1,selRow= -1
131        Button Clear_A_Button,pos={227,148},size={35,20},proc=ClearABButtonProc,title="Clr"
132        Button Clear_A_Button,help={"Clears the list of sample scans"}
133        Button Clear_B_Button,pos={227,286},size={35,20},proc=ClearABButtonProc,title="Clr"
134        Button Clear_B_Button,help={"Clears the list of empty scans"}
135        Button ARefreshButton,pos={9,310},size={104,20},proc=RefreshListButtonProc,title="Refresh"
136        Button ARefreshButton,help={"Refreshes the list of raw ICP data files"}
137        Button Del_A_Button,pos={183,148},size={35,20},proc=DelAButtonProc,title="Del"
138        Button Del_A_Button,help={"Deletes the selected file(s) from the list of SET A scans"}
139        Button Del_B_Button,pos={183,286},size={35,20},proc=DelBButtonProc,title="Del"
140        Button Del_B_Button,help={"Deletes the selected file(s) from the list of SET B scans"}
141        ListBox B_LB,pos={151,194},size={110,90}
142        ListBox B_LB,listWave=root:Packages:NIST:USANS:Globals:AddPanel:BWave,mode= 1,selRow= 0
143        Button to_A_List,pos={118,55},size={25,90},proc=to_A_ListButtonProc,title="A\r->"
144        Button to_A_List,help={"Adds the selected file(s) to the list of SET A scans"}
145        Button to_B_List,pos={120,195},size={25,90},proc=to_B_ListButtonProc,title="B\r->"
146        Button to_B_List,help={"Adds the selected file(s) to the list of SET B scans"}
147//      ListBox StatusLB,pos={11,358},size={386,77}
148//      ListBox StatusLB,listWave=root:Packages:NIST:USANS:Globals:AddPanel:statusWave
149        Button pickPathButton,pos={6,8},size={80,20},proc=PickBT5PathButton,title="DataPath..."
150        Button pickPathButton,help={"Select the data folder where the raw ICP data files are located"}
151        Button PlotSelected_A_Button,pos={140,148},size={35,20},proc=PlotSelected_AB_ButtonProc,title="Plot"
152        Button PlotSelected_A_Button,help={"Plot the selected sample scattering files in the COR_Graph"}
153        Button PlotSelected_B_Button,pos={140,286},size={35,20},proc=PlotSelected_AB_ButtonProc,title="Plot"
154        Button PlotSelected_B_Button,help={"Plot the selected empty cell scattering files in the COR_Graph"}
155        Button pickSavePathButton,pos={97,8},size={80,20},proc=PickSaveButtonProc,title="SavePath..."
156        Button pickSavePathButton,help={"Select the data folder where data is to be saved to disk"}
157        Button USANSHelpButton,pos={220,6},size={50,20},proc=USANSAddFilesHelpButton,title="Help"
158        Button USANSHelpButton,help={"Show the USANS reduction help file"}
159
160        SetVariable FilterSetVar,pos={8,289},size={106,18},title="Filter",fSize=12
161        SetVariable FilterSetVar,value= root:Packages:NIST:USANS:Globals:AddPanel:FilterStr
162       
163        Button A_AddDone,pos={231,414},size={50,20},proc=AddUSANSDone,title="Done"
164        Button A_AddDone,help={"Closes the panel"}
165        Button AddUSANSButton,pos={12,368},size={80,20},proc=AddUSANSFilesButtonProc,title="Add Files"
166        Button AddUSANSButton,help={"Adds the A and B files  together"}
167        Button AddUSANSButton,fColor=(16386,65535,16385)
168        Button ClearSumButton,pos={159,322},size={80,20},proc=ClearSumButtonProc,title="Clear Sum"
169        Button ClearSumButton,help={"Clears the summed data from the graph and clears the data in memory"}
170        Button SaveSumButton,pos={13,405},size={80,20},proc=SaveSumButtonProc,title="Move Sum"
171        Button SaveSumButton,help={"Saves the summed data as a fake bt5 file"}
172        Button SaveSumButton,fColor=(16385,28398,65535)
173       
174EndMacro
175
176// Show the help file, don't necessarily keep it with the experiment (/K=1)
177Function USANSAddFilesHelpButton(ctrlName) : ButtonControl
178        String ctrlName
179       
180        DisplayHelpTopic/Z/K=1 "Adding Two Data Sets"
181        if(V_flag !=0)
182                DoAlert 0,"The USANS Data Reduction Help file could not be found"
183        endif
184        return(0)
185End
186
187Function AddUSANSDone(ba) : ButtonControl
188        STRUCT WMButtonAction &ba
189
190        switch( ba.eventCode )
191                case 2: // mouse up
192                        // click code here
193                        DoWindow/K USANS_Add_Panel
194                        break
195        endswitch
196
197        return 0
198End
199
200Function AddUSANSFilesButtonProc(ba) : ButtonControl
201        STRUCT WMButtonAction &ba
202
203        switch( ba.eventCode )
204                case 2: // mouse up
205                        // click code here
206                       
207                        Add_AB("")
208                       
209                        break
210        endswitch
211
212        return 0
213End
214
215Function ClearSumButtonProc(ba) : ButtonControl
216        STRUCT WMButtonAction &ba
217
218        switch( ba.eventCode )
219                case 2: // mouse up
220                        // click code here
221                        RemoveFromGraph/W=USANS_Add_Panel/Z $("DetCts_"+"SUM_AB")
222                        CleanOutFolder("SUM_AB")
223                       
224                        break
225        endswitch
226
227        return 0
228End
229
230
231// really a "move" since I'm not sure about how to poroperly save...
232//
233Function SaveSumButtonProc(ba) : ButtonControl
234        STRUCT WMButtonAction &ba
235
236        switch( ba.eventCode )
237                case 2: // mouse up
238                        // click code here
239                        String type=""
240                        Prompt type,"Move the sumed data to:", popup, "EMP;SAM;"
241                        DoPrompt "Move Summed Data",type
242                        if(V_flag)
243                                return(0)
244                        endif
245                       
246                        MoveSummedData(type)
247                       
248                        break
249        endswitch
250
251        return 0
252End
253
254Function Add_AB(ctrlName) : ButtonControl
255        String ctrlName
256       
257        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
258        // copy waves over to the SUM_AB folder
259        String fromType = "TMP_A",toType="SUM_AB"
260        Duplicate/O $(USANSFolder+":"+fromType+":Angle"),$(USANSFolder+":"+toType+":Angle_A")
261        Duplicate/O $(USANSFolder+":"+fromType+":DetCts"),$(USANSFolder+":"+toType+":DetCts_A")
262        Duplicate/O $(USANSFolder+":"+fromType+":MonCts"),$(USANSFolder+":"+toType+":MonCts_A")
263        Duplicate/O $(USANSFolder+":"+fromType+":TransCts"),$(USANSFolder+":"+toType+":TransCts_A")
264        Duplicate/O $(USANSFolder+":"+fromType+":ErrDetCts"),$(USANSFolder+":"+toType+":ErrDetCts_A")
265       
266        fromType = "TMP_B"
267        Duplicate/O $(USANSFolder+":"+fromType+":Angle"),$(USANSFolder+":"+toType+":Angle_B")
268        Duplicate/O $(USANSFolder+":"+fromType+":DetCts"),$(USANSFolder+":"+toType+":DetCts_B")
269        Duplicate/O $(USANSFolder+":"+fromType+":MonCts"),$(USANSFolder+":"+toType+":MonCts_B")
270        Duplicate/O $(USANSFolder+":"+fromType+":TransCts"),$(USANSFolder+":"+toType+":TransCts_B")
271        Duplicate/O $(USANSFolder+":"+fromType+":ErrDetCts"),$(USANSFolder+":"+toType+":ErrDetCts_B")
272
273        SetDataFolder $(USANSFolder+":"+toType)
274        Wave Angle_A = Angle_A
275        Wave DetCts_A = DetCts_A
276        Wave MonCts_A = MonCts_A
277        Wave TransCts_A = TransCts_A
278        Wave ErrDetCts_A = ErrDetCts_A
279       
280        Wave Angle_B = Angle_B
281        Wave DetCts_B = DetCts_B
282        Wave MonCts_B = MonCts_B
283        Wave TransCts_B = TransCts_B
284        Wave ErrDetCts_B = ErrDetCts_B
285
286        Make/O/D/N=0 Angle,DetCts,MonCts,TransCts,ErrDetCts
287       
288        //do something with the wave note on each DetCts wave so it's not lost
289//      String/G note_A = note(DetCts_A)
290//      String/G note_B = note(DetCts_B)
291       
292       
293        Variable minPt,minDelta
294        Variable ii,jj,nA,nB,sumPt=0
295        nA = numpnts(Angle_A)
296        nB = numpnts(Angle_B)
297        Make/O/D/N=(nB) tmp_delta
298       
299        NVAR tol = root:Packages:NIST:USANS:Globals:AddPanel:gAddUSANSTolerance //1% error allowed as default
300               
301        for(ii=0;ii<nA;ii+=1)
302                Redimension/N=(nB) tmp_delta
303                tmp_delta = NaN         //initialize every pass
304                tmp_delta = abs( (Angle_A[ii] - Angle_B)/Angle_B )
305               
306                WaveStats/Q tmp_delta
307                minPt = V_minLoc
308                minDelta = V_min
309               
310                if(minDelta < tol)
311//                      Printf "Angle_A %g matches with Angle_B %g with error %g\r",Angle_A[ii],Angle_B[minPt],minDelta
312                        // add the points to the sum waves
313                        InsertPoints sumPt,1,Angle,DetCts,MonCts,TransCts,ErrDetCts
314                        Angle[sumPt] = (Angle_A[ii]+Angle_B[minPt])/2
315                        DetCts[sumPt] = (DetCts_A[ii] + DetCts_B[minPt])/2
316                        MonCts[sumPt] = (MonCts_A[ii] + MonCts_B[minPt])/2
317                        TransCts[sumPt] = (TransCts_A[ii] + TransCts_B[minPt])/2
318                        ErrDetCts[sumPt] = sqrt(ErrDetCts_A[ii]^2 + ErrDetCts_B[minPt]^2)/2
319                       
320                        sumPt += 1
321                        // remove the point from B (all of them)
322                        DeletePoints minPt, 1, Angle_B,DetCts_B,MonCts_B,TransCts_B,ErrDetCts_B
323                        nB -= 1
324                        //
325                else
326                        //Printf "NoMatch for Angle_A %g  with error %g\r",Angle_A[ii],minDelta
327                        // just copy it over
328                        InsertPoints sumPt,1,Angle,DetCts,MonCts,TransCts,ErrDetCts
329                        Angle[sumPt] = Angle_A[ii]
330                        DetCts[sumPt] = DetCts_A[ii]
331                        MonCts[sumPt] = MonCts_A[ii]
332                        TransCts[sumPt] = TransCts_A[ii]
333                        ErrDetCts[sumPt] = ErrDetCts_A[ii]
334                       
335                        sumPt += 1
336                endif
337               
338        endfor
339       
340        // bring in all of the unmatched "B" values, including the error
341        Concatenate {Angle_B},Angle
342        Concatenate {DetCts_B},DetCts
343        Concatenate {MonCts_B},MonCts
344        Concatenate {TransCts_B},TransCts
345        Concatenate {ErrDetCts_B},ErrDetCts
346
347        // then sort everything by angle
348        Sort Angle,Angle,DetCts,MonCts,TransCts,ErrDetCts
349       
350        // update the wave note on detCts? it is zeroed now, for one
351        String aNote="",bNote="",afile="",bFile="",str=""
352        aNote=note(DetCts_A)
353        aNote = ReplaceNumberByKey("PEAKANG",aNote,0,":",";")           //set the angle to zero in the summed set
354        //adjust the A wavenote, to account for the new dataset
355        bNote = note(DetCts_B)
356        bFile = StringByKey("FILE",bNote,":",";")
357
358        aFile = StringByKey("FILE",aNote,":",";")
359        aFile += "," + bfile
360        aNote = ReplaceStringByKey("FILE",aNote,aFile,":",";")
361
362        Note/K DetCts
363        Note DetCts,aNote
364               
365        SetDataFolder root:
366       
367        //copy the data to plot to the root:Graph directory, and give clear names
368        String type="SUM_AB"
369        if(WaveExists($(USANSFolder+":"+type+":Qvals")))
370                Duplicate/O $(USANSFolder+":"+type+":Qvals"),$(USANSFolder+":Graph:Qvals_"+type)
371        Endif
372        Duplicate/O $(USANSFolder+":"+type+":Angle"),$(USANSFolder+":Graph:Angle_"+type)
373        Duplicate/O $(USANSFolder+":"+type+":DetCts"),$(USANSFolder+":Graph:DetCts_"+type)
374        Duplicate/O $(USANSFolder+":"+type+":ErrDetCts"),$(USANSFolder+":Graph:ErrDetCts_"+type)
375       
376       
377        Do_AB_Graph("SUM_AB")
378        return(0)
379End
380
381
382// plots the selected A or B files onto the USANS_Add_Panel
383// Does the following:
384// - loads raw data
385// ?? normalizes counts to time and 1E6 monitor counts
386// - sorts by angle
387// - finds zero angle (and peak height)
388// X converts to q-values
389// X finds T wide
390// - updates the graph
391//
392Function PlotSelected_AB_ButtonProc(ctrlName) : ButtonControl
393        String ctrlName
394
395        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
396
397        String type=""
398        if(cmpstr(ctrlName,"PlotSelected_A_Button")==0)
399                type = "TMP_A"
400                Wave/T listW=$(USANSFolder+":Globals:AddPanel:AWave")
401        else
402                type = "TMP_B"
403                Wave/T listW=$(USANSFolder+":Globals:AddPanel:BWave")
404        endif
405       
406        //get selected files from listbox (everything)
407        //use the listBox wave directly
408
409        //Wave for indication of current data set AJJ Sept 2006
410//      Wave isCurrent = $(USANSFolder+":Globals:MainPanel:SAMisCurrent")
411        Variable ii,num=numpnts(listW)
412        String fname="",fpath="",curPathStr=""
413        PathInfo bt5PathName
414        fpath = S_Path
415        PathInfo bt5CurPathName
416        curPathStr = S_Path
417       
418//      print fpath
419       
420        if(cmpstr("",listW[0])==0)
421                return(0)               //null item in 1st position, exit
422        Endif
423       
424        //load, normalize, and append
425        //loop over the number of items in the list
426        for(ii=0;ii<num;ii+=1)
427                fname = fpath + listw[ii]
428                               
429                LoadBT5File(fname,"SWAP")       //overwrite what's in the SWAP folder
430//              Convert2Countrate("SWAP",0)             //convert to cts/s, don't normalize to default Mon
431                Convert2Countrate("SWAP",1)             //convert to cts/s, yes, normalize to default Mon
432                if(ii==0)       //first time, overwrite
433                        NewDataWaves("SWAP",type)
434                else            //append to waves in "SAM"
435                        AppendDataWaves("SWAP",type)
436                endif
437        endfor
438        //sort after all loaded
439        DoAngleSort(type)
440       
441        //find the peak and convert to Q-values
442        Variable zeroAngle = FindZeroAngle(type)
443        if(zeroAngle == -9999)
444                DoAlert 0,"Couldn't find a peak - using zero as zero angle"
445                zeroAngle = 0
446        Endif
447       
448//      ConvertAngle2Qvals(type,zeroAngle)
449        Wave angle = $(USANSFolder+":"+type+":Angle")
450        Angle = angle[p] - zeroAngle
451
452        //
453        //copy the data to plot to the root:Graph directory, and give clear names
454        if(WaveExists($(USANSFolder+":"+type+":Qvals")))
455                Duplicate/O $(USANSFolder+":"+type+":Qvals"),$(USANSFolder+":Graph:Qvals_"+type)
456        Endif
457        Duplicate/O $(USANSFolder+":"+type+":Angle"),$(USANSFolder+":Graph:Angle_"+type)
458        Duplicate/O $(USANSFolder+":"+type+":DetCts"),$(USANSFolder+":Graph:DetCts_"+type)
459        Duplicate/O $(USANSFolder+":"+type+":ErrDetCts"),$(USANSFolder+":Graph:ErrDetCts_"+type)
460       
461        //now plot the data (or just bring the graph to the front)
462        Do_AB_Graph(type)
463End
464
465// add SAM data to the graph if it exists and is not already on the graph
466//
467Function Do_AB_Graph(type)
468        String type
469       
470        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
471
472        //is it already on the graph?
473        SetDataFolder $(USANSFolder+":Graph")
474        String list=""
475        list = Wavelist("DetCts_"+type+"*",";","WIN:USANS_Add_Panel")
476        if(strlen(list)!=0)
477                //Print "SAM already on graph"
478                SetDataFolder root:
479                return(0)
480        endif
481       
482        //check for the three possibilities
483        if(cmpstr(type,"TMP_A")==0)
484                //append the data if it exists
485                If(waveExists($"DetCts_TMP_A")==1)
486//                      DoWindow/F USANS_Add_Panel
487                        AppendToGraph DetCts_TMP_A vs Angle_TMP_A
488                        ModifyGraph rgb(DetCts_TMP_A)=(1,12815,52428)
489                        ModifyGraph mode(DetCts_TMP_A)=3,marker(DetCts_TMP_A)=19,msize(DetCts_TMP_A)=2
490                        ModifyGraph tickUnit=1,log=1,mirror=2,grid=1
491                        ErrorBars/T=0 DetCts_TMP_A Y,wave=(ErrDetCts_TMP_A,ErrDetCts_TMP_A)
492                endif
493        endif
494       
495        if(cmpstr(type,"TMP_B")==0)
496                //append the data if it exists
497                If(waveExists($"DetCts_TMP_B")==1)
498//                      DoWindow/F USANS_Add_Panel
499                        AppendToGraph DetCts_TMP_B vs Angle_TMP_B
500                        ModifyGraph rgb(DetCts_TMP_B)=(1,39321,19939)
501                        ModifyGraph mode(DetCts_TMP_B)=3,marker(DetCts_TMP_B)=19,msize(DetCts_TMP_B)=2
502                        ModifyGraph tickUnit=1,log=1,mirror=2,grid=1
503                        ErrorBars/T=0 DetCts_TMP_B Y,wave=(ErrDetCts_TMP_B,ErrDetCts_TMP_B)
504                endif
505        endif
506       
507                if(cmpstr(type,"SUM_AB")==0)
508                //append the data if it exists
509                If(waveExists($"DetCts_SUM_AB")==1)
510//                      DoWindow/F USANS_Add_Panel
511                        AppendToGraph DetCts_SUM_AB vs Angle_SUM_AB
512                        ModifyGraph rgb(DetCts_SUM_AB)=(65535,0,0)
513                        ModifyGraph mode(DetCts_SUM_AB)=3,marker(DetCts_SUM_AB)=19,msize(DetCts_SUM_AB)=2
514                        ModifyGraph tickUnit=1,log=1,mirror=2,grid=1
515                        ErrorBars/T=0 DetCts_SUM_AB Y,wave=(ErrDetCts_SUM_AB,ErrDetCts_SUM_AB)
516                endif
517        endif
518       
519        if(strlen(list)==0)
520                //drawing a new graph
521                Legend
522                Label left "(Counts/sec)/(MON)*10\\S6\\M"
523                Label bottom "Angle (deg)"
524        endif
525       
526        SetDataFolder root:
527End
528
529
530// copies the selected files from the raw file list box to the sam file listbox
531//
532// makes sure that any null items are removed from the wave attached to the listbox
533//
534Function to_A_ListButtonProc(ctrlName) : ButtonControl
535        String ctrlName
536
537        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
538
539        //Print "toSamList button"
540        Wave/T fileWave=$(USANSFolder+":Globals:AddPanel:fileWave")
541        Wave/T AWave=$(USANSFolder+":Globals:AddPanel:AWave")
542        Wave sel=$(USANSFolder+":Globals:AddPanel:selFileW")
543        //Wave to indicate Current status
544//      Wave isCurrent = $(USANSFolder+":Globals:MainPanel:SAMisCurrent")
545
546       
547        Variable num=numpnts(sel),ii=0
548        variable lastPt=numpnts(AWave)
549        do
550                if(sel[ii] == 1)
551                        InsertPoints lastPt,1, AWave
552                        AWave[lastPt]=filewave[ii]
553//                      InsertPoints lastPt, 1, isCurrent
554//                      isCurrent[lastPt] = 0
555                        lastPt +=1
556                endif
557                ii+=1
558        while(ii<num)
559       
560        //clean out any (null) elements
561        num=numpnts(AWave)
562        for(ii=0;ii<num;ii+=1)
563                if(cmpstr(AWave[ii],"") ==0)
564                        DeletePoints ii,1,AWave
565//                      DeletePoints ii,1,isCurrent
566                        num -= 1
567                Endif
568        Endfor
569       
570        return(0)
571End
572
573// copies the selected files from the raw file list box to the sam file listbox
574//
575// makes sure that any null items are removed from the wave attached to the listbox
576//
577Function to_B_ListButtonProc(ctrlName) : ButtonControl
578        String ctrlName
579
580        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
581
582        //Print "toEmptyList button"
583        Wave/T fileWave=$(USANSFolder+":Globals:AddPanel:fileWave")
584        Wave/T BWave=$(USANSFolder+":Globals:AddPanel:BWave")
585        Wave sel=$(USANSFolder+":Globals:AddPanel:selFileW")
586        //Wave to indicate Current status
587//      Wave isCurrent = $(USANSFolder+":Globals:MainPanel:EMPisCurrent")
588
589
590       
591        Variable num=numpnts(sel),ii=0
592        variable lastPt=numpnts(BWave)
593        do
594                if(sel[ii] == 1)
595                        InsertPoints lastPt,1, BWave
596                        BWave[lastPt]=filewave[ii]
597//                      InsertPoints lastPt, 1, isCurrent
598//                      isCurrent[lastPt] = 0
599                        lastPt +=1
600                endif
601                ii+=1
602        while(ii<num)
603       
604        //clean out any (null) elements
605        num=numpnts(BWave)
606        for(ii=0;ii<num;ii+=1)
607                if(cmpstr(BWave[ii],"") ==0)
608                        DeletePoints ii,1,BWave
609//                      DeletePoints ii,1,isCurrent
610                        num -= 1
611                Endif
612        Endfor
613       
614        return(0)
615End
616
617//deletes the selected file from the A list
618// multiple selections are not allowed
619// the cor_graph is not updated
620//
621Function DelAButtonProc(ctrlName) : ButtonControl
622        String ctrlName
623
624        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
625
626        ControlInfo A_LB
627        Variable selRow=V_Value
628        Wave lw=$(S_DataFolder + S_Value)
629        DeletePoints selRow,1,lw       
630        //Clear out current flag AJJ Sept O6
631//      Wave isCurrent = $(USANSFolder+":Globals:AddPanel:SAMisCurrent")
632//      DeletePoints selRow, 1, isCurrent       
633End
634
635//deletes the selected file from the B list
636// multiple selections are not allowed
637// the cor_graph is not updated
638//
639Function DelBButtonProc(ctrlName) : ButtonControl
640        String ctrlName
641
642        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
643
644        ControlInfo B_LB
645        Variable selRow=V_Value
646        Wave lw=$(S_DataFolder + S_Value)
647        DeletePoints selRow,1,lw
648        //Clear out current flag AJJ Sept O6
649//      Wave isCurrent = $(USANSFolder+":Globals:MainPanel:EMPisCurrent")
650//      DeletePoints selRow, 1, isCurrent       
651End
652
653//clears either the A or B scan Lists...
654//Also clears the data folders and the COR_Graph
655//
656Function ClearABButtonProc(ctrlName) : ButtonControl
657        String ctrlName
658
659        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
660               
661        SetDataFolder $(USANSFolder+":Globals:AddPanel")
662        strswitch(ctrlName)
663                case "Clear_A_Button":
664                        Make/O/T/N=1 AWave
665                        AWave=""
666
667                        //clear the graph, then the data folders as well
668                        RemoveFromGraph/W=USANS_Add_Panel/Z $("DetCts_"+"TMP_A")
669                        CleanOutFolder("TMP_A")
670                        RemoveFromGraph/W=USANS_Add_Panel/Z $("DetCts_"+"SUM_AB")
671                        CleanOutFolder("SUM_AB")
672                        break
673                case "Clear_B_Button":
674                        Make/O/T/N=1 BWave
675                        BWave=""
676
677                        //clear the graph, then the data folders as well
678//                      CleanOutGraph("TMP_B")
679                        RemoveFromGraph/W=USANS_Add_Panel/Z $("DetCts_"+"TMP_B")
680                        CleanOutFolder("TMP_B")
681//                      CleanOutGraph("SUM_AB")
682                        RemoveFromGraph/W=USANS_Add_Panel/Z $("DetCts_"+"SUM_AB")
683                        CleanOutFolder("SUM_AB")
684                        break
685        endswitch
686       
687        SetDataFolder root:
688End
689
690
691
692
693//move the summed data to one of the other data types for reduction
694//
695// type is either SAM or EMP
696//
697// -- still need to move stuff to the EMP folder??
698//
699Function MoveSummedData(type)
700        String type
701       
702        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
703
704        Variable zeroAngle=0            //summed data is already converted to zero angle
705        ConvertAngle2Qvals("SUM_AB",zeroAngle)
706       
707        //find the Trans Cts for T_Wide
708        FindTWideCts("SUM_AB")
709       
710        if(cmpstr(type,"EMP")==0)
711                //copy the data to plot to the root:Graph directory, and give clear names
712                if(WaveExists($(USANSFolder+":SUM_AB:Qvals")))
713                        Duplicate/O $(USANSFolder+":SUM_AB:Qvals"),$(USANSFolder+":Graph:Qvals_EMP")
714                Endif
715                Duplicate/O $(USANSFolder+":SUM_AB:Angle"),$(USANSFolder+":Graph:Angle_EMP")
716                Duplicate/O $(USANSFolder+":SUM_AB:DetCts"),$(USANSFolder+":Graph:DetCts_EMP")
717                Duplicate/O $(USANSFolder+":SUM_AB:ErrDetCts"),$(USANSFolder+":Graph:ErrDetCts_EMP")
718               
719                // then copy everything to the EMP folder, for later processing steps
720                NewDataWaves("SUM_AB","EMP")
721               
722        endif
723       
724        if(cmpstr(type,"SAM")==0)       
725                //copy the data to plot to the root:Graph directory, and give clear names
726                if(WaveExists($(USANSFolder+":SUM_AB:Qvals")))
727                        Duplicate/O $(USANSFolder+":SUM_AB:Qvals"),$(USANSFolder+":Graph:Qvals_SAM")
728                Endif
729                Duplicate/O $(USANSFolder+":SUM_AB:Angle"),$(USANSFolder+":Graph:Angle_SAM")
730                Duplicate/O $(USANSFolder+":SUM_AB:DetCts"),$(USANSFolder+":Graph:DetCts_SAM")
731                Duplicate/O $(USANSFolder+":SUM_AB:ErrDetCts"),$(USANSFolder+":Graph:ErrDetCts_SAM")
732               
733                // then copy everything to the SAM folder, for later processing steps
734                NewDataWaves("SUM_AB","SAM")
735               
736        endif
737               
738        //now plot the data (or just bring the graph to the front)
739        DoCORGraph()
740        return(0)
741End
742
743
744/////////////////////////////////
745//
746//
747// simple add procedures, if raw files are the same, and there's nothing to shift
748//
749//
750
751Proc SelectFilesToAdd(file1,file2)
752        String file1,file2
753        Prompt file1, "First File", popup, BT5FileList("*.bt5*")
754        Prompt file2, "Second File", popup, BT5FileList("*.bt5*")
755       
756//      Print file1,file2
757       
758        LoadAndAddUSANS(file1,file2)
759       
760End
761
762Function/S BT5FileList(filter)
763        String filter
764       
765       
766        //get all the files, then trim the list
767        String list=IndexedFile(bt5PathName,-1,"????")
768        String newList="",item=""
769        Variable num=ItemsInList(list,";"),ii
770        for(ii=0;ii<num;ii+=1)
771                item = StringFromList(ii, list  ,";")
772               
773                if( stringmatch(item,filter) )          //ONLY keep files that match the filter + *.bt5 AJJ Sept 06
774                        newlist += item + ";"
775                endif
776                //print "ii=",ii
777        endfor
778        newList = SortList(newList,";",0)       //get them in order
779        num=ItemsInList(newlist,";")
780       
781       
782        return(newList)
783End
784
785
786// main add procedure, farms out as needed
787//
788Function LoadAndAddUSANS(file1,file2)
789        String file1,file2
790       
791        //load each file into a textWave
792        Make/O/T/N=200 tw1,tw2
793       
794        String fname="",fpath="",fullPath=""
795        Variable ctTime1,ang11,ang21
796        Variable ctTime2,ang12,ang22
797        Variable dialog=1
798       
799        PathInfo/S savePathName
800        fpath = S_Path
801       
802        if(strlen(S_Path)==0)
803                DoAlert 0,"You must select a Save Path... from the main USANS_Panel"
804                return(0)
805        endif
806       
807        fname = fpath + file1
808        LoadBT5_toWave(fname,tw1,ctTime1,ang11,ang21)           //redimensions tw1
809        Print "File 1: time, angle1, angle2",ctTime1,ang11,ang21
810       
811        fname = fpath + file2
812        LoadBT5_toWave(fname,tw2,ctTime2,ang12,ang22)           //redimensions tw2
813        Print "File 2: time, angle1, angle2",ctTime2,ang12,ang22
814
815        // check if OK to add
816        // # lines loaded
817        // #pts1 = #pts2
818        // angle range 1 = angle range 2
819        if(numpnts(tw1) != numpnts(tw2))
820                DoAlert 0,"Files are not the same length and can't be directly added"
821                //Killwaves/Z tw1,tw2,tw3
822                return(0)
823        endif
824        if(ang11 != ang12)
825                DoAlert 0,"Files don't start at the same angle and can't be directly added"
826                //Killwaves/Z tw1,tw2,tw3
827                return(0)
828        endif
829        if(ang21 != ang22)
830                DoAlert 0,"Files don't end at the same angle and can't be directly added"
831                //Killwaves/Z tw1,tw2,tw3
832                return(0)
833        endif
834       
835        //if OK, parse line-by-line into third textWave
836        // add / update as necessary
837        Print "OK to add"
838       
839
840
841        Variable v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16
842        Variable a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16
843        Variable ii,valuesRead,countTime,num,refnum
844        String s1,s2,s3,s4,s5,s6,s7,s8,s9,s10
845        String filen="",fileLabel="",buffer="",str="",term="\r\n"
846       
847        Duplicate/O/T tw2,tw3
848        tw3=""
849       
850        //line 0, info to update
851        buffer = tw1[0]
852        sscanf buffer, "%s%s%s%s%s%s%g%g%s%g%s",s1,s2,s3,s4,s5,s6,v1,v2,s7,v3,s8
853        sprintf str,"%s %s 'I'        %d    1  'TIME'   %d  'SUM'",s1,s2+" "+s3+" "+s4+" "+s5,ctTime1+ctTime2,v3
854//      num=v3
855        tw3[0] = str+term
856        tw3[1] = tw1[1]         //labels, direct copy
857       
858        //line 2, sample label
859        buffer = tw1[2]
860        sscanf buffer,"%s",s1
861        tw3[2] = s1 + " = "+file1+" + "+file2+term
862        tw3[3,12] = tw1[p]              //unused values, direct copy
863
864        num = numpnts(tw1)              //
865        //parse two lines at a time per data point,starting at 13
866        for(ii=13;ii<num-1;ii+=2)
867                buffer = tw1[ii]
868                sscanf buffer,"%g%g%g%g%g",v1,v2,v3,v4,v5               // 5 values here now
869               
870                buffer = tw2[ii]
871                sscanf buffer,"%g%g%g%g%g",a1,a2,a3,a4,a5               // 5 values here now
872               
873                if(a1 != v1)
874                        DoAlert 0,"Angles don't match and can't be directly added"
875                        //Killwaves/Z tw1,tw2,tw3
876                        return(0)
877                endif
878               
879                sprintf str,"     %g   %g   %g   %g   %g",v1,v2+a2,v3+a3,v4+a4,v5+a5
880                tw3[ii] = str+term
881               
882               
883                buffer = tw1[ii+1]
884                sscanf buffer,"%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g",v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16
885               
886                buffer = tw2[ii+1]
887                sscanf buffer,"%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g",a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16
888       
889                sprintf str,"%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g",a1+v1,a2+v2,a3+v3,a4+v4,a5+v5,a6+v6,a7+v7,a8+v8,a9+v9,a10+v10,a11+v11,a12+v12,a13+v13,a14+v14,a15+v15,a16+v16
890                tw3[ii+1] = str+term
891       
892        endfor
893       
894        // write out the final file (=tw3)
895        filen = file1[0,strlen(file1)-5]+"_SUM.bt5"
896       
897        if(dialog)
898                PathInfo/S savePathName
899                fullPath = DoSaveFileDialog("Save data as",fname=filen)
900                If(cmpstr(fullPath,"")==0)
901                        //user cancel, don't write out a file
902                        Close/A
903                        Abort "no data file was written"
904                Endif
905                //Print "dialog fullpath = ",fullpath
906        Endif
907       
908        Open refNum as fullPath
909        wfprintf refnum, "%s",tw3
910        Close refnum
911               
912        //killwaves/Z tw1,tw2,tw3
913       
914        return(0)
915end
916
917//returns count time and start/stop angles as written in header
918// number of lines in the file is a separate check
919//
920Function LoadBT5_toWave(fname,tw,ctTime,a1,a2)
921        String fname
922        WAVE/T tw
923        Variable &ctTime,&a1,&a2
924       
925        Variable numLinesLoaded = 0,firstchar,refnum
926        String buffer =""
927        Variable v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,ii,valuesRead
928        String s1,s2,s3,s4,s5,s6,s7,s8,s9,s10
929       
930        Open/R refNum as fname
931       
932        //read the data until EOF - assuming always a pair or lines
933        do
934                FReadLine refNum, buffer
935                if (strlen(buffer) == 0)
936                        break                                                   // End of file
937                endif
938                firstChar = char2num(buffer[0])
939                if ( (firstChar==10) || (firstChar==13) )
940                        break                                                   // Hit blank line. End of data in the file.
941                endif
942               
943                tw[numLinesLoaded] = buffer
944               
945                numlinesloaded += 1
946        while(1)
947               
948        Close refNum            // Close the file.
949
950        //trim the waves to the correct number of points
951        Redimension/N=(numlinesloaded) tw       
952       
953        buffer = tw[0]
954        sscanf buffer, "%s%s%s%s%s%s%g%g%s%g%s",s1,s2,s3,s4,s5,s6,v1,v2,s7,v3,s8
955        //v2 is the monitor prefactor. multiply monitor time by prefactor. AJJ 5 March 07
956        ctTime=v1*v2
957       
958        buffer = tw[6]
959        sscanf buffer, "%g%g%g%g",v1,v2,v3,v4
960        a1=v2
961        a2=v4
962       
963        return(0)
964End
965///////////////////////////////////
Note: See TracBrowser for help on using the repository browser.