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

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

Many changes to USANS to allow adding of raw bt5 files. Two sets can be added and used immediately (no save), or multiple data sets of the same range can be added and saved in bt5 format.

Still a first pass, needs checking and interface tweaks.

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