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

Revision 570, 27.9 KB checked in by srkline, 5 years ago (diff)

Change (1):
In preparation for release, updated pragma IgorVersion?=6.1 in all procedures

Change (2):
As a side benefit of requiring 6.1, we can use the MultiThread? keyword to thread any model function we like. The speed benefit is only noticeable on functions that require at least one integration and at least 100 points (resolution smearing is NOT threaded, too many threadSafe issues, too little benefit). I have chosen to use the MultiThread? only on the XOP assignment. In the Igor code there are too many functions that are not explicitly declared threadsafe, making for a mess.

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