source: sans/Dev/trunk/NCNR_User_Procedures/Common/DataSetHandling.ipf @ 709

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

SA_Includes_v410 : now include Smear_2D

PeakGauss_2D, Sphere_2D : included threaded resolution smearing calculations for testing

DataSetHandling? : Included a quick and dirty batch converter for XML->6-col. See the top
of the file for the command to run

GaussUtils? : re-define the ResSemear_2D_AAOStruct. Relocated q-value and phi calculations from
RawWindowHook? to this file so they would be available for reduction and analysis

Smear_2D : now has a generic (non-threaded) smearing routine. Threading must be done in
individual functions since FUNCREF can't be passed to threads (plus a few other issues)

PlotUtils_2D : updated loader for new QxQy? columns. Fixes to Wrapper_2D to enable smeared fits

RawWindowHook? : removed Q-value calculation functions and moved these to GaussUtils?

WriteQIS : now writes out 8-columns for QxQy? data, defining the resolution
function in terms of directions parallel and perpendicular to Q. TEMPORARILY in the data
file an error in intensity is generated that is SQRT(I), being careful to
replace any NaN, inf, or zero with an average error value

MultiScatter_MonteCarlo_2D : 4-processor aware

NCNR_Utils : 2D resolution calculation is now in terms of parallel and perpendicular
rather than x and y. Gravity is included in the y-component

File size: 43.3 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2
3
4///////// SRK - VERY SIMPLE batch converter has been added: see
5//
6//      Function batchXML26ColConvert()
7//
8
9
10
11// Functions and interfaces to manage datasets now that they are in data folders
12// Planned interface
13// - Panel to select/load data and then select operation
14// Planned functions:
15// - Rename a data set - AJJ Done Nov 2009
16// - Duplicate a data set - AJJ Done Nov 2009
17// - Subtract one data set from another
18// - Add one data set to another
19// - Divide data sets
20// - Multiply data sets
21// - Save a data folder to file
22
23/////////////////// Data Management Panel ///////////////////////////////////////////////////////
24
25//
26Proc MakeDMPanel()
27        DoWindow/F DataManagementPanel
28        if(V_flag==0)
29                fMakeDMPanel()
30        endif
31End
32
33Proc fMakeDMPanel()
34        PauseUpdate; Silent 1           // building window...
35        NewPanel /W=(459,44,959,410)/N=DataManagementPanel/K=2 as "Data Set Management"
36        ModifyPanel fixedSize=1,cbRGB=(30000,60000,60000)
37
38        //Main bit of panel
39        GroupBox grpBox_0,pos={20,10},size={460,100}
40        GroupBox grpBox_1,pos={20,130},size={460,70}
41        GroupBox grpBox_2,pos={20,220},size={460,40}
42
43        GroupBox grpBox_3,pos={20,280},size={460,40}
44
45        Button DS_button,title="Load 1D Data Set",pos={300,20},size={150,20}
46        Button DS_button,proc=DM_LoadDataSetProc
47        Button Unload_button,title="Unload 1D Data Set",pos={300,50},size={150,20}
48        Button Unload_button,proc=DM_UnloadProc
49        Button Save_button,title="Save 1D Data Set",pos={300,80},size={150,20}
50        Button Save_button,proc=DM_SaveProc
51        PopupMenu DS_popup,pos={30,40},size={318,20},title="Data Set ",proc=DM_PopupProc
52        PopupMenu DS_popup,mode=1,value= #"DM_DataSetPopupList()"
53
54        CheckBox XMLStateCtrl,pos={30,82},size={124,14},title="XML Output Enabled (change in Preferences)"
55        CheckBox XMLStateCtrl,help={"Default output format is canSAS XML rather than NIST 6 column"}
56        CheckBox XMLStateCtrl,value= root:Packages:NIST:gXML_Write,disable=2
57
58        Button Rename_button,title="Rename",pos={75,170},size={150,20}
59        Button Rename_button,proc=DM_RenameProc
60        Button  Duplicate_button,title="Duplicate",pos={275,170},size={150,20}
61        Button Duplicate_button,proc=DM_DuplicateProc
62
63        SetVariable NewName_setvar,title="New Name (max 25 characters)",pos={50,140},size={400,20}
64        SetVariable NewName_setvar,fsize=12,value=_STR:"",proc=DMNameSetvarproc,live=1
65               
66        Button SaveAsXML_button,title="Save as canSAS XML",pos={75,230},size={150,20}
67        Button SaveAsXML_button,proc=DMSaveAsXMLproc   
68
69        Button SaveAs6col_button,title="Save as NIST 6 column",pos={275,230},size={150,20}
70        Button SaveAs6col_button,proc=DMSaveAs6colproc 
71       
72        Button BatchConvertData_button,title="Convert Format of 1D Data Files",pos={75,290},size={350,20}
73        Button BatchConvertData_button,proc=DMBatchConvertProc
74                       
75        Button DMDone_button,title="Done",pos={360,330},size={60,20}
76        Button DMDone_button,proc=DMDoneButtonProc
77        Button DMHelp_button,title="?",pos={440,330},size={30,20}
78        Button DMHelp_button,proc=DMHelpButtonProc
79       
80       
81        ControlInfo/W=DataManagementPanel DS_popup
82        if (cmpstr(S_Value,"No data loaded") == 0)
83                SetVariable NewName_setvar,value=_STR:"dataset_copy"
84        else
85                //fake call to popup
86                STRUCT WMPopupAction pa
87                pa.win = "DataManagementPanel"
88                pa.ctrlName = "DS_popup"
89                pa.eventCode = 2
90                DM_PopupProc(pa)
91        endif
92
93End
94
95Function DMBatchConvertProc(ba) : ButtonControl
96        STRUCT WMButtonAction &ba
97       
98        switch( ba.eventCode)
99                case 2:
100                        Execute "MakeBatchConvertPanel()"
101                        break
102        endswitch
103       
104        return 0
105End
106
107
108Function DMNameSetvarproc(sva) : SetVariableControl
109        STRUCT WMSetVariableAction &sva
110               
111        switch( sva.eventCode )
112                case 1: // mouse up
113                case 2: // Enter key
114                case 3: // Live update
115                                String sv = sva.sval
116                                if( strlen(sv) > 25 )
117                                        sv= sv[0,24]
118                                        SetVariable  $(sva.ctrlName),win=$(sva.win),value=_STR:sv
119                                        ControlUpdate /W=$(sva.win) $(sva.ctrlName)
120                                        Beep
121                                endif
122                                break
123                endswitch
124        return 0
125End
126
127Function DM_RenameProc(ba) : ButtonControl
128        STRUCT WMButtonAction &ba
129       
130        String DS,NewName
131       
132        ControlInfo/W=$(ba.win) DS_popup
133        DS = S_Value
134       
135        ControlInfo/W=$(ba.win) NewName_setvar
136        NewName = CleanupName(S_Value, 0 )              //clean up any bad characters, and put the cleaned string back
137        SetVariable NewName_setvar,value=_STR:NewName
138       
139        switch (ba.eventcode)
140                case 2: // mouse up
141                        RenameDataSet(DS,NewName)
142                        ControlUpdate /W=$(ba.win) DS_Popup
143                        break
144        endswitch
145
146
147       
148        return 0
149end
150               
151Function DM_DuplicateProc(ba) : ButtonControl
152        STRUCT WMButtonAction &ba
153       
154        String DS,NewName
155       
156        ControlInfo/W=$(ba.win) DS_popup
157        DS = S_Value
158       
159        ControlInfo/W=$(ba.win) NewName_setvar
160        NewName = CleanupName(S_Value, 0 )              //clean up any bad characters, and put the cleaned string back
161        SetVariable NewName_setvar,value=_STR:NewName
162       
163        switch (ba.eventcode)
164                case 2: // mouse up
165                        DuplicateDataSet(DS,NewName,0)
166                        ControlUpdate /W=$(ba.win) DS_Popup
167                        break
168        endswitch
169       
170        return 0
171end
172
173Function DM_SaveProc(ba) : ButtonControl
174        STRUCT WMButtonAction &ba
175       
176        switch(ba.eventCode)
177                case 2:
178                        ControlInfo/W=$(ba.win) DS_popup
179                        SaveDataSetToFile(S_Value)
180                        break
181        endswitch
182       
183        return 0
184end
185
186Function DM_UnloadProc(ba) : ButtonControl
187        STRUCT WMButtonAction &ba
188
189        switch (ba.eventcode)
190                case 2: // mouse up
191                       
192                        String savDF=GetDataFolder(1)
193                        String DF
194                        ControlInfo /W=$(ba.win) DS_Popup
195                        DF = S_Value
196                       
197                        print DF
198                        //check for horrific null output from control
199                        if (cmpstr(DF,"") != 0)
200                       
201                                SetDataFolder DF
202                                KillVariables/A
203                                SetDataFolder savDF
204                       
205                                KillDataFolder/Z $DF
206                                ControlUpdate /W=$(ba.win) DS_Popup
207                       
208                                ControlInfo/W=DataManagementPanel DS_popup
209                                if (cmpstr(S_Value,"No data loaded") == 0)
210                                        SetVariable NewName_setvar,value=_STR:"dataset_copy"
211                                else
212                                        //fake call to popup
213                                        STRUCT WMPopupAction pa
214                                        pa.win = "DataManagementPanel"
215                                        pa.ctrlName = "DS_popup"
216                                        pa.eventCode = 2
217                                        DM_PopupProc(pa)
218                                endif
219                        endif
220                        break
221        endswitch
222       
223        return 0
224end
225               
226
227Function DM_PopupProc(pa) : PopupMenuControl
228        STRUCT WMPopupAction &pa
229       
230        String resultName
231       
232        switch( pa.eventCode)
233                case 2:
234                        //print "Called by "+pa.ctrlname+" with value "+pa.popStr
235                        ControlInfo/W=$(pa.win) $(pa.ctrlName)
236                        String popStr = S_Value
237                        if (stringmatch(pa.ctrlname,"*DS*") == 1)
238                                resultName = stringfromlist(0,popStr,"_")+"_copy"
239                               
240                                SetVariable NewName_setvar win=$(pa.win), value=_STR:resultName
241                        endif
242                break
243        endswitch
244       
245
246End
247
248
249//Must follow naming scheme to match buttons to popups
250//"Name_button" goes with "Name_popup"
251Function DM_LoadDataSetProc(ba) : ButtonControl
252        STRUCT WMButtonAction &ba
253
254
255        switch( ba.eventCode )
256                case 2: // mouse up
257                        // click code here
258                        String cmd = "A_LoadOneDDataWithName(\"\","+num2str(0)+")"
259                        Execute cmd
260               
261                        SVAR gLastFileName = root:packages:NIST:gLastFileName
262
263                        String windowName = ba.win
264                        String popupName = StringFromList(0,ba.ctrlName,"_")+"_popup"
265                       
266                        ControlUpdate/W=$(windowName) $(popupName)
267                        //instead of a simple controlUpdate, better to pop the menu to make sure that the other menus follow
268                        // convoluted method to find the right item and pop the menu.
269
270                        String list,folderStr
271                        Variable num
272                        folderStr = CleanupName(gLastFileName,0)
273                        list = DM_DataSetPopupList()
274                        num=WhichListItem(folderStr,list,";",0,0)
275                        if(num != -1)
276                                PopupMenu $(popupName),mode=num+1,win=$(windowName)
277                                ControlUpdate/W=$(windowName) $(popupName)
278                               
279                                if (cmpstr(popupName,"DS_popup") ==  0)
280                                        //send fake mouse action to popup to update old name if
281                                        Struct WMPopupAction pa
282                                        pa.eventCode = 2                //fake mouse up
283                                        pa.win = windowName
284                                        pa.ctrlName = "DS_popup"
285                                        DM_PopupProc(pa)
286                                endif                   
287                        endif
288                        break
289        endswitch
290       
291        return 0
292End
293
294
295Function/S DMGetDSName(dsNum)
296        Variable dsNum
297       
298        String ctrlName
299        if (dsNum == 1)
300                ctrlName = "DS1_popup"
301        elseif (dsNum == 2)
302                ctrlName = "DS2_popup"
303        endif
304       
305        ControlInfo/W=DataManagementPanel $(ctrlName)
306
307        Return S_Value
308
309End
310
311
312Function DMDoneButtonProc(ba) : ButtonControl
313        STRUCT WMButtonAction &ba
314       
315        String win = ba.win
316
317        switch (ba.eventCode)
318                case 2:
319                        DoWindow/K DataManagementPanel
320                        break
321        endswitch
322
323        return 0
324End
325
326Function DMHelpButtonProc(ba) : ButtonControl
327        STRUCT WMButtonAction &ba
328       
329        String win = ba.win
330
331        switch (ba.eventCode)
332                case 2:
333                        // click code here
334                        DisplayHelpTopic/Z/K=1 "Data Set Management"
335                        if(V_flag !=0)
336                                DoAlert 0,"The Data Set Management Help file could not be found"
337                        endif
338                        break
339        endswitch
340
341        return 0
342End
343
344/////////////////////// Batch Data Conversion Panel ////////////////////////////////////
345//
346//
347
348Proc MakeNCNRBatchConvertPanel()
349        NCNRInitBatchConvert()
350        DoWindow/F NCNRBatchConvertPanel
351        if(V_flag==0)
352                fMakeNCNRBatchConvertPanel()
353        endif
354End
355
356
357Function NCNRInitBatchConvert()
358        NewDataFolder/O/S root:Packages:NIST:BatchConvert
359        Make/O/T/N=1 filewave=""
360        Make/O/N=1 selWave=0
361        Variable/G ind=0
362        SetDataFolder root:
363End
364
365Proc fMakeNCNRBatchConvertPanel()
366        PauseUpdate; Silent 1           // building window...
367        NewPanel /W=(658,347,1018,737)/N=NCNRBatchConvertPanel/K=2 as "Batch Convert 1D Data"
368        ModifyPanel cbRGB=(40000,50000,32896)
369        ModifyPanel fixedSize=1
370       
371        ListBox fileList,pos={13,11},size={206,179}
372        ListBox fileList,listWave=root:Packages:NIST:BatchConvert:fileWave
373        ListBox fileList,selWave=root:Packages:NIST:BatchConvert:selWave,mode= 4
374
375        Button button7,pos={238,20},size={100,20},proc=NCNRBatchConvertNewFolder,title="New Folder"
376        Button button7,help={"Select a new data folder"}
377        TitleBox msg0,pos={238,140},size={100,30},title="\JCShift-click to\rselect multiple files"
378        TitleBox msg0,frame=0,fixedSize=1
379       
380        GroupBox filterGroup,pos={13,200},size={206,60},title="Filter list by input file type"
381       
382        Button button8,pos={238,76},size={100,20},proc=NCNRBatchConvertHelpProc,title="Help"
383        Button button9,pos={238,48},size={100,20},proc=NCNRBatchConvertRefresh,title="Refresh List"
384        Button button0,pos={238,106},size={100,20},proc=NCNRBatchConvertDone,title="Done"
385
386        GroupBox outputGroup,pos={13,270},size={206,60},title="Output File Type"
387
388       
389        Button button6,pos={13,350},size={206,20},proc=NCNRBatchConvertFiles,title="Convert File(s)"
390        Button button6,help={"Converts the files to the format selected"}
391       
392
393       
394End
395
396
397Function NCNRBatchConvertFiles(ba) : ButtonControl
398                STRUCT WMButtonAction &ba
399               
400                switch (ba.eventCode)
401                        case 2:
402                                Wave/T fileWave=$"root:Packages:NIST:BatchConvert:fileWave"
403                                Wave sel=$"root:BatchConvert:NIST:BatchConvert:selWave"
404                               
405                                break
406                endswitch
407
408        return 0
409End
410
411
412Function NCNRBatchConvertNewFolder(ba) : ButtonControl
413        STRUCT WMButtonAction &ba
414       
415        switch (ba.eventcode)
416                case 2:
417                        A_PickPath()
418                        NCNRBatchConvertGetList()
419                        break
420        endswitch
421
422End
423
424Function NCNRBatchConvertGetList()
425
426        //make sure that path exists
427        PathInfo catPathName
428        if (V_flag == 0)
429                Abort "Folder path does not exist - use \"New Folder\" button"
430        Endif
431       
432        String newList = A_ReducedDataFileList("")
433        Variable num
434       
435        num=ItemsInList(newlist,";")
436        WAVE/T fileWave=$"root:Packages:NIST:BatchConvert:fileWave"
437        WAVE selWave=$"root:Packages:NIST:BatchConvert:selWave"
438        Redimension/N=(num) fileWave
439        Redimension/N=(num) selWave
440        fileWave = StringFromList(p,newlist,";")
441        Sort filewave,filewave
442End
443
444Function NCNRBatchConvertRefresh(ba) : ButtonControl
445                STRUCT WMButtonAction &ba
446               
447                switch (ba.eventCode)
448                        case 2:
449                                NCNRBatchConvertGetList()
450                                break
451                endswitch
452               
453                return 0
454End
455       
456
457Function NCNRBatchConvertDone(ba) : ButtonControl
458                STRUCT WMButtonAction &ba
459
460                switch (ba.eventCode)
461                        case 2:
462                                DoWindow/K NCNRBatchConvertPanel
463                                break
464                endswitch
465               
466                return 0
467End
468
469Function NCNRBatchConvertHelpProc(ba) : ButtonControl
470        STRUCT WMButtonAction &ba
471       
472        String win = ba.win
473
474        switch (ba.eventCode)
475                case 2:
476                        // click code here
477                        DisplayHelpTopic/Z/K=1 "Batch Data Conversion"
478                        if(V_flag !=0)
479                                DoAlert 0,"The Batch Data Conversion Help file could not be found"
480                        endif
481                        break
482        endswitch
483
484        return 0
485End
486
487/////////////////////// Data Arithmetic Panel /////////////////////////////////////////
488
489//
490Function MakeDAPanel()
491        DoWindow/F DataArithmeticPanel
492        if(V_flag==0)
493                fMakeDAPanel()
494        else
495                DoWindow/F DAPlotPanel
496        endif
497       
498        return(0)
499End
500
501Function fMakeDAPanel()
502        PauseUpdate; Silent 1           // building window...
503        DoWindow/K DataArithmeticPanel
504        NewPanel /W=(459,44,959,404)/N=DataArithmeticPanel/K=2 as "Data Set Arithmetic"
505        ModifyPanel fixedSize=1
506
507        //Main bit of panel
508        GroupBox grpBox_0,pos={20,10},size={460,105}
509
510        Button DS1_button,pos={300,20},size={150,20},proc=DA_LoadDataSetProc,title="Load 1D Data Set 1"
511        Button DS1_button,valueColor=(65535,0,0),userdata="DS1"
512        PopupMenu DS1_popup,pos={30,21},size={318,20},title="Data Set 1"
513        PopupMenu DS1_popup,mode=1,value= #"DM_DataSetPopupList()"
514        PopupMenu DS1_popup,proc=DA_PopupProc
515        PopupMenu DS1_popup,fsize=12,fcolor=(65535,0,0),valueColor=(65535,0,0)
516
517        Button DS2_button,pos={300,50},size={150,20},proc=DA_LoadDataSetProc,title="Load 1D Data Set 2"
518        Button DS2_button,valueColor=(0,0,65535),userdata="DS2"
519        PopupMenu DS2_popup,pos={30,51},size={318,20},title="Data Set 2"
520        PopupMenu DS2_popup,mode=1,value= #"DM_DataSetPopupList()"
521        PopupMenu DS2_popup,proc=DA_PopupProc
522        PopupMenu DS2_popup,fsize=12,fcolor=(0,0,65535),valueColor=(0,0,65535)
523
524        Button DAPlot_button,title="Plot",pos={100,85},size={150,20}
525        Button DAPlot_button,proc=DAPlotButtonProc
526        Button DADone_button,title="Done",pos={360,85},size={60,20}
527        Button DADone_button,proc=DADoneButtonProc
528        Button DAHelp_button,title="?",pos={440,85},size={30,20}
529        Button DAHelp_button,proc=DAHelpButtonProc
530
531
532        //Tabs
533        TabControl DATabs,pos={20,120},size={460,220},tabLabel(0)="Subtract", proc=DATabsProc
534        TabControl DATabs,tablabel(1)="Add",tablabel(2)="Multiply",tablabel(3)="Divide"
535        TabControl DATabs,value=0
536
537        Button DACalculate_button,title="Calculate",pos={50,310},size={150,20}
538        Button DACalculate_button,proc=DACalculateProc
539        Button DASave_button,title="Save Result",pos={300,310},size={150,20}
540        Button DASave_button,proc=DASaveProc,disable=2
541        Button DACursors_button,title="Get Matching Range",pos={175,250},size={150,20}
542        Button DACursors_button,proc=DACursorButtonProc
543       
544        SetVariable DAResultName_sv,title="Result Name (max 25 characters)",pos={50,280},size={400,20}
545        SetVariable DAResultName_Sv,fsize=12,proc=DANameSetvarproc,live=1
546        //Update the result name
547        ControlInfo/W=DataArithmeticPanel DS1_popup
548        if (cmpstr(S_Value,"No data loaded") == 0)
549                SetVariable DAResultName_sv,value=_STR:"SubtractionResult"
550        else
551                //fake call to popup
552                STRUCT WMPopupAction pa
553                pa.win = "DataArithmeticPanel"
554                pa.ctrlName = "DS1_popup"
555                pa.eventCode = 2
556                DA_PopupProc(pa)
557        endif
558       
559        CheckBox DANoDS2_cb,title="Data Set 2 = 1?",pos={300,180}
560        CheckBox DANoDS2_cb,proc=DANoDS2Proc
561       
562        ValDisplay DARangeStar_vd,title="Start",pos={40,220},size={100,20},fsize=12,value=_NUM:0
563        ValDisplay DARangeEnd_vd,title="End  ",pos={160,220},size={100,20},fsize=12,value=_NUM:0
564       
565        SetVariable DAScale_sv,title="Scale Factor (f)",pos={280,220},size={180,20},fsize=12,value=_NUM:1
566
567        GroupBox grpBox_1,pos={30,210},size={440,70}
568       
569        NewPanel/HOST=DataArithmeticPanel/N=arithDisplay/W=(50,150,170,190)
570       
571        //Update the result name
572        ControlInfo/W=DataArithmeticPanel DS1_popup
573
574       
575       
576        arithDisplayProc(0)
577       
578End
579
580
581Function MakeDAPlotPanel()
582        PauseUpdate; Silent 1           // building window...
583        DoWindow/K DAPlotPanel
584        NewPanel /W=(14,44,454,484)/N=DAPlotPanel/K=1 as "Data Set Arithmetic"
585        ModifyPanel fixedSize=1
586
587        Display/HOST=DAPlotPanel/N=DAPlot/W=(0,0,440,400)
588        Legend
589        ShowInfo
590        SetActiveSubWindow DAPlotPanel
591        Checkbox DAPlot_log_cb, title="Log I(q)", pos={20,410},value=0
592        Checkbox DAPlot_log_cb, proc=DALogLinIProc
593       
594End
595
596Function AddDAPlot(dataset)
597        Variable dataset
598       
599        String win = "DataArithmeticPanel"
600        String DS1name,DS2name,ResultName
601
602        switch(dataset)
603                case 1:
604                        ControlInfo/W=$(win) DS1_popup
605                        DS1name = S_Value
606                        Wave qWave = $("root:"+DS1name+":"+DS1name+"_q")
607                        Wave iWave = $("root:"+DS1name+":"+DS1name+"_i")
608                        Wave errWave = $("root:"+DS1name+":"+DS1name+"_s")
609                        AppendToGraph/W=DAPlotPanel#DAPlot iWave vs Qwave
610                        ErrorBars/W=DAPlotPanel#DAPlot /T=0 $(DS1name+"_i"), Y wave=(errWave,errWave)
611                        ModifyGraph/W=DAPlotPanel#DAPlot rgb($(DS1name+"_i"))=(65535,0,0)
612                        ControlInfo/W=$(win) DANoDS2_cb
613//                      if (V_Value == 1)
614//                                      Cursor/W=DAPlotPanel#DAPlot A, $(DS1name+"_i"), leftx(iWave)
615//                                      Cursor/W=DAPlotPanel#DAPlot/A=0 B, $(DS1name+"_i"),  rightx(iWave)
616//                      endif
617                        break
618                case 2:
619                        ControlInfo/W=$(win) DANoDS2_cb
620                        if (V_Value == 0)
621                                ControlInfo/W=$(win) DS2_popup
622                                DS2name = S_Value
623                                if(cmpstr(DS2name,"No data loaded")==0)
624                                        break                   //in case someone loads set 1, but not set two, then plots
625                                endif
626                                Wave qWave = $("root:"+DS2name+":"+DS2name+"_q")
627                                Wave iWave = $("root:"+DS2name+":"+DS2name+"_i")
628                                Wave errWave = $("root:"+DS2name+":"+DS2name+"_s")
629                                AppendToGraph/W=DAPlotPanel#DAPlot iWave vs Qwave
630                                ErrorBars/W=DAPlotPanel#DAPlot /T=0 $(DS2name+"_i"), Y wave=(errWave,errWave)                   
631                                ModifyGraph/W=DAPlotPanel#DAPlot rgb($(DS2name+"_i"))=(0,0,65535)
632                                Cursor/W=DAPlotPanel#DAPlot A, $(DS2name+"_i"), leftx(iWave)
633                                Cursor/W=DAPlotPanel#DAPlot/A=0 B, $(DS2name+"_i"),  rightx(iWave)
634                        else
635                                ControlInfo/W=$(win) DS1_popup
636                                DS1name = S_Value
637                                DuplicateDataSet("root:"+DS1name,"NullSolvent",1)
638                                Wave qWave =root:NullSolvent:NullSolvent_q
639                                Wave iWave = root:NullSolvent:NullSolvent_i
640                                Wave errWave = root:NullSolvent:NullSolvent_s
641                                Wave iWaveDS1 = $("root:"+DS1name+":"+DS1name+"_i")
642                                iWave = 1
643                                errWave = 0
644                                AppendToGraph/W=DAPlotPanel#DAPlot iWave vs Qwave
645                                ErrorBars/W=DAPlotPanel#DAPlot /T=0 NullSolvent_i, Y wave=(errWave,errWave)                     
646                                ModifyGraph/W=DAPlotPanel#DAPlot rgb(NullSolvent_i)=(0,0,65535)
647                                //Cursor/W=DAPlotPanel#DAPlot A, NullSolvent_i, leftx(iWave)
648                                //Cursor/W=DAPlotPanel#DAPlot/A=0 B, NullSolvent_i,  rightx(iWave)
649                                if(strlen(CsrInfo(A,"DAPlotPanel#DAPlot")) == 0)                //cursors not already on the graph             
650                                        Cursor/W=DAPlotPanel#DAPlot A, $(DS1Name+"_i"), leftx(iWaveDS1)
651                                        Cursor/W=DAPlotPanel#DAPlot/A=0 B, $(DS1Name+"_i"),  rightx(iWaveDS1)                   
652                                endif
653                        endif
654                        break
655                case 3:
656                        ControlInfo/W=$(win) DAResultName_sv
657                        ResultName = S_Value
658                        Wave qWave = $("root:"+ResultName+":"+ResultName+"_q")
659                        Wave iWave = $("root:"+ResultName+":"+ResultName+"_i")
660                        Wave errWave = $("root:"+ResultName+":"+ResultName+"_s")
661                        AppendToGraph/W=DAPlotPanel#DAPlot iWave vs Qwave
662                        ErrorBars/W=DAPlotPanel#DAPlot /T=0 $(ResultName+"_i"), Y wave=(errWave,errWave)
663                        ModifyGraph/W=DAPlotPanel#DAPlot rgb($(ResultName+"_i"))=(0,65535,0)
664                        break
665        endswitch
666
667        ControlInfo/W=DAPlotPanel DAPlot_log_cb
668        ModifyGraph/W=DAPlotPanel#DAPlot mode=3, msize=2, marker=19, mirror=1, tick=2, log(bottom)=1,log(left)=V_Value,tickUnit=1
669End
670
671Function arithDisplayProc(s)
672        Variable s
673
674        SetActiveSubWindow DataArithmeticPanel#arithDisplay
675
676        switch (s)
677                case 0:
678                        //Subtract
679                        DrawAction/L=progFront delete
680                        DrawRect 0,0,120,40
681                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,65535,0)
682                        DrawText 10,32,"I"
683                        SetDrawEnv fname="Times", fsize=22, fstyle= 1,textrgb= (0,0,0)
684                        DrawText 20,30,"="
685                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (65535,0,0)
686                        DrawText 35,32,"I"
687                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,65535)
688                        DrawText 95,32,"I"
689                        SetDrawEnv fname="Symbol", fsize=22, fstyle= 1,textrgb= (0,0,0)
690                        DrawText 52,32,"-"
691                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,0)
692                        DrawText 75,30,"f"
693                        SetDrawEnv fname="Times", fsize=22, fstyle= 1,textrgb= (0,0,0)
694                        DrawText 85,32,"*"
695                        break
696                case 1:
697                        //Add
698                        DrawAction/L=progFront delete
699                        DrawRect 0,0,120,40
700                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,65535,0)
701                        DrawText 10,32,"I"
702                        SetDrawEnv fname="Times", fsize=22, fstyle= 1,textrgb= (0,0,0)
703                        DrawText 20,30,"="
704                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (65535,0,0)
705                        DrawText 35,32,"I"
706                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,65535)
707                        DrawText 95,32,"I"
708                        SetDrawEnv fname="Symbol", fsize=22, fstyle= 1,textrgb= (0,0,0)
709                        DrawText 52,32,"+"
710                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,0)
711                        DrawText 75,30,"f"
712                        SetDrawEnv fname="Times", fsize=22, fstyle= 1,textrgb= (0,0,0)
713                        DrawText 85,32,"*"
714                        break
715                case 2:
716                        //Multiply
717                        DrawAction/L=progFront delete
718                        DrawRect 0,0,120,40
719                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,65535,0)
720                        DrawText 10,32,"I"
721                        SetDrawEnv fname="Times", fsize=22, fstyle= 1,textrgb= (0,0,0)
722                        DrawText 20,30,"="
723                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (65535,0,0)
724                        DrawText 35,32,"I"
725                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,65535)
726                        DrawText 95,32,"I"
727                        SetDrawEnv fname="Symbol", fsize=22, fstyle= 1,textrgb= (0,0,0)
728                        DrawText 52,32,"*"
729                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,0)
730                        DrawText 66,30,"("
731                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,0)
732                        DrawText 75,30,"f"
733                        SetDrawEnv fname="Times", fsize=22, fstyle= 1,textrgb= (0,0,0)
734                        DrawText 85,32,"*"
735                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,0)                                 
736                        DrawText 105,30,")"
737                        break
738                case 3:
739                        //Divide
740                        DrawAction/L=progFront delete
741                        DrawRect 0,0,120,40
742                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,65535,0)
743                        DrawText 10,32,"I"
744                        SetDrawEnv fname="Times", fsize=22, fstyle= 1,textrgb= (0,0,0)
745                        DrawText 20,30,"="
746                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (65535,0,0)
747                        DrawText 35,32,"I"
748                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,65535)
749                        DrawText 95,32,"I"
750                        SetDrawEnv fname="Symbol", fsize=22, fstyle= 1,textrgb= (0,0,0)
751                        DrawText 52,32,"/"
752                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,0)
753                        DrawText 66,30,"("
754                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,0)
755                        DrawText 75,30,"f"
756                        SetDrawEnv fname="Times", fsize=22, fstyle= 1,textrgb= (0,0,0)
757                        DrawText 85,32,"*"
758                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,0)                                 
759                        DrawText 105,30,")"
760                        break           
761        endswitch
762
763        SetActiveSubWindow DataArithmeticPanel
764       
765        return 0
766End
767
768//Must follow naming scheme to match buttons to popups
769//"Name_button" goes with "Name_popup"
770Function DA_LoadDataSetProc(ba) : ButtonControl
771        STRUCT WMButtonAction &ba
772       
773        switch( ba.eventCode )
774                case 2: // mouse up
775                        // click code here
776                        String cmd = "A_LoadOneDDataWithName(\"\","+num2str(0)+")"
777                        Execute cmd
778               
779                        SVAR gLastFileName = root:packages:NIST:gLastFileName
780
781                        String windowName = ba.win
782                        String popupName = StringFromList(0,ba.ctrlName,"_")+"_popup"
783                       
784                        ControlUpdate/W=$(windowName) $(popupName)
785                        //instead of a simple controlUpdate, better to pop the menu to make sure that the other menus follow
786                        // convoluted method to find the right item and pop the menu.
787
788                        String list,folderStr
789                        Variable num
790                        folderStr = CleanupName(gLastFileName,0)
791                        list = DM_DataSetPopupList()
792                        num=WhichListItem(folderStr,list,";",0,0)
793                        if(num != -1)
794                                PopupMenu $(popupName),mode=num+1,win=$(windowName)
795                                ControlUpdate/W=$(windowName) $(popupName)
796               
797                        endif
798                        //fake call to popup
799                        STRUCT WMPopupAction pa
800                        pa.win = ba.win
801                        pa.ctrlName = "DS1_popup"
802                        pa.eventCode = 2
803                        DA_PopupProc(pa)
804                        break
805        endswitch
806       
807        return 0
808End
809
810Function DA_PopupProc(pa) : PopupMenuControl
811        STRUCT WMPopupAction &pa
812       
813        String resultName
814       
815        switch( pa.eventCode)
816                case 2:
817                        //print "Called by "+pa.ctrlname+" with value "+pa.popStr
818                        ControlInfo/W=$(pa.win) $(pa.ctrlName)
819                        String popStr = S_Value
820                        if (stringmatch(pa.ctrlname,"*DS1*") == 1)
821                                resultName = stringfromlist(0,popStr,"_")+"_mod"
822                               
823                                SetVariable DAResultName_sv win=$(pa.win), value=_STR:resultName
824                        endif
825                break
826        endswitch
827       
828
829End
830
831// function to control the drawing of buttons in the TabControl on the main panel
832// Naming scheme for the buttons MUST be strictly adhered to... else buttons will
833// appear in odd places...
834// all buttons are named MainButton_NA where N is the tab number and A is the letter denoting
835// the button's position on that particular tab.
836// in this way, buttons will always be drawn correctly..
837//
838Function DATabsProc(tca) : TabControl
839        STRUCT WMTabControlAction &tca
840               
841        switch (tca.eventCode)
842                case 2:
843                //      Print "name,number",name,tab
844                        String ctrlList = ControlNameList("",";"),item="",nameStr=""
845                        Variable num = ItemsinList(ctrlList,";"),ii,onTab
846                        for(ii=0;ii<num;ii+=1)
847                                //items all start w/"DSTabItem_"
848                                item=StringFromList(ii, ctrlList ,";")
849                                nameStr=item[0,9]
850                                if(cmpstr(nameStr,"DATabItem_")==0)
851                                        onTab = str2num(item[10])
852                                        ControlInfo $item
853                                        switch (V_flag)
854                                                case 1:
855                                                        Button $item,disable=(tca.tab!=onTab)
856                                                        break
857                                                case 2:
858                                                        CheckBox $item,disable=(tca.tab!=onTab)
859                                                        break
860                                                case 3:
861                                                        PopUpMenu       $item,disable=(tca.tab!=onTab)
862                                                        break
863                                                case 5:
864                                                        SetVariable     $item,disable=(tca.tab!=onTab)
865                                                        break
866                                        endswitch
867                                endif
868                        endfor
869                       
870                        arithDisplayProc(tca.tab)
871                        break
872        endswitch       
873End
874
875
876Function DACalculateProc(ba) : ButtonControl
877        STRUCT WMButtonAction &ba
878       
879        String DS1,DS2,Resultname
880       
881        switch(ba.eventCode)
882        case 2:
883                //Which tab?
884                ControlInfo/W=$(ba.win) DATabs
885                Variable tabNum = V_value
886                //Print "Tab number "+num2str(tabNum)
887
888                ControlInfo/W=$(ba.win) DS1_popup
889                DS1 = S_Value
890                ControlInfo/W=$(ba.win) DANoDS2_cb
891                if (V_Value == 0)
892                        ControlInfo/W=$(ba.win) DS2_popup
893                        DS2 = S_Value
894                else
895                        DS2 = "NullSolvent"
896                endif
897                ControlInfo/W=$(ba.win) DAResultName_sv
898                Resultname = CleanupName(S_Value, 0 )           //clean up any bad characters, and put the cleaned string back
899                SetVariable DAResultName_sv,value=_STR:ResultName
900               
901                ControlInfo/W=$(ba.win) DAScale_sv
902                Variable Scalefactor = V_Value
903
904                switch(tabNum)
905                        case 0:
906                                //do subtraction
907                                //print "Subtraction of "+DS2+" from "+DS1+" with sf "+num2str(scalefactor)+ " into "+Resultname
908                                SubtractDataSets(DS1,DS2,Scalefactor,Resultname)
909                                break
910                        case 1:
911                                //do addition
912                                AddDataSets(DS1,DS2,Scalefactor,Resultname)
913                                break
914                        case 2:
915                                //do multiplication
916                                MultiplyDataSets(DS1,DS2,Scalefactor,Resultname)
917                                break
918                        case 3:
919                                //do division
920                                DivideDataSets(DS1,DS2,Scalefactor,Resultname)
921                                break
922                endswitch
923               
924                //Sort out plot
925                //Fake button press to DAPlotButtonProc
926                STRUCT WMButtonAction ba2
927                ba2.win = ba.win
928                ba2.ctrlName = "DAPlot_button"
929                ba2.eventCode = 2
930               
931                // I've commented this out - the cursors get reset to the ends since this removes all sets from the graph, and
932                // then replots them. What is the real purpose of this call? To clear the old result off before adding the
933                // new one?
934//              DAPlotButtonProc(ba2)
935                ba2.userData = ResultName
936                DAPlotRemoveResult(ba2)
937               
938               
939                AddDAPlot(3)
940                DoWindow/F DataArithmeticPanel
941               
942                //Enable save button now that we have a result to save
943                Button DASave_Button win=$(ba.win),disable=0
944               
945//              SetActiveSubWindow DAPlotPanel
946        endswitch
947       
948End
949
950// remove what is not the
951//
952Function DAPlotRemoveResult(ba) : ButtonControl
953        STRUCT WMButtonAction &ba
954       
955        String win = ba.win
956        String ResultName = ba.userData
957        String item="",traceList=""
958        Variable ii=0,num
959
960        switch (ba.eventCode)
961                case 2:         //mouse up
962                        //data set 1
963                        ControlInfo/W=$(win) DS1_popup
964                        String DS1 = S_Value
965                       
966                        //Get folder for DS2
967                        ControlInfo/W=$(win) DS2_popup
968                        String DS2 = S_Value
969                       
970                        // state of the checkbox
971                        ControlInfo/W=$(win) DANoDS2_cb
972                        if(V_Value)
973                                DS2 = "NullSolvent"
974                        endif
975                       
976                        DoWindow DAPlotPanel
977                        if (V_Flag == 0)
978                                MakeDAPlotPanel()
979                        else
980                                DoWindow/HIDE=0/F DAPlotPanel
981                                traceList = TraceNameList("DAPlotPanel#DAPlot",";",1)
982                                num=ItemsInList(traceList)
983                                ii=0
984                                do
985                                        item = StringFromList(ii,traceList,";")
986                                        if (stringmatch(item,ResultName+"*")==1)                //it it's the specific trace I've asked to remove
987                                                RemoveFromGraph/W=DAPlotPanel#DAPlot $item
988                                        elseif (stringmatch(item,DS1+"*")==0 && stringmatch(item,DS2+"*")==0)           //if it's not set1 & not set2
989                                                RemoveFromGraph/W=DAPlotPanel#DAPlot $item
990                                        endif
991                                       
992                                        ii+=1
993                                while(ii<num)                           
994                        endif
995                       
996                        break
997        endswitch
998
999        return 0
1000End
1001
1002
1003Function DAPlotButtonProc(ba) : ButtonControl
1004        STRUCT WMButtonAction &ba
1005       
1006        String win = ba.win
1007
1008        switch (ba.eventCode)
1009                case 2:
1010                        //mouse up
1011                        //Get folder for DS1
1012                        DoWindow DAPlotPanel
1013                        if (V_Flag == 0)
1014                                MakeDAPlotPanel()
1015                        else
1016                                DoWindow/HIDE=0/F DAPlotPanel
1017                                do
1018                                        String tracename = StringFromList(0,TraceNameList("DAPlotPanel#DAPlot",";",1),";")
1019                                        if (cmpstr(tracename,"")==0)
1020                                                break
1021                                        else
1022                                                RemoveFromGraph/W=DAPlotPanel#DAPlot $tracename
1023                                        endif                   
1024                                while(1)                               
1025                        endif
1026                       
1027                        ControlInfo/W=$(win) DS1_popup
1028                        String DS1 = S_Value
1029                        if (cmpstr(DS1,"") != 0 )
1030                                AddDAPlot(1)
1031                        endif
1032                        //Get folder for DS2
1033                        ControlInfo/W=$(win) DS2_popup
1034                        String DS2 = S_Value
1035                        if (cmpstr(DS2,"") != 0)
1036                                AddDAPlot(2)
1037                        endif
1038                        break
1039        endswitch
1040
1041        return 0
1042End
1043
1044Function DADoneButtonProc(ba) : ButtonControl
1045        STRUCT WMButtonAction &ba
1046       
1047        String win = ba.win
1048
1049        switch (ba.eventCode)
1050                case 2:
1051                        DoWindow/K DAPlotPanel
1052                        DoWindow/K DataArithmeticPanel
1053                        break
1054        endswitch
1055
1056        return 0
1057End
1058
1059Function DAHelpButtonProc(ba) : ButtonControl
1060        STRUCT WMButtonAction &ba
1061       
1062        String win = ba.win
1063
1064        switch (ba.eventCode)
1065                case 2:
1066                        // click code here
1067                        DisplayHelpTopic/Z/K=1 "Data Set Arithmetic"
1068                        if(V_flag !=0)
1069                                DoAlert 0,"The Data Set Arithmetic Help file could not be found"
1070                        endif
1071                        break
1072        endswitch
1073
1074        return 0
1075End
1076
1077Function DALogLinIProc(cba) : CheckBoxControl
1078        STRUCT WMCheckBoxAction &cba
1079
1080        switch(cba.eventcode)
1081                case 2:
1082                       
1083                        ModifyGraph/W=DAPlotPanel#DAPlot log(left)=cba.checked
1084               
1085        endswitch
1086
1087
1088End
1089
1090Function DACursorButtonProc(ba) : ButtonControl
1091        STRUCT WMButtonAction &ba
1092
1093        String DS1,DS2
1094       
1095        switch(ba.eventCode)
1096                case 2:
1097               
1098                        ControlInfo/W=$(ba.win) DS1_popup
1099                        DS1 = S_Value
1100                        ControlInfo/W=$(ba.win) DANoDS2_cb
1101                        Variable NoDS2 = V_Value
1102                        if (NoDS2 == 0)
1103                                ControlInfo/W=$(ba.win) DS2_popup
1104                                DS2 = S_Value
1105                        else
1106                                DS2 = "NullSolvent"
1107                        endif
1108               
1109                        //AJJ Nov 2009 - UGLY - Will have to revisit this when we deal with hierarchical folders
1110                        Wave set1_i = $("root:"+DS1+":"+DS1+"_i")
1111                        Wave set1_q = $("root:"+DS1+":"+DS1+"_q")
1112                        Wave set2_i = $("root:"+DS2+":"+DS2+"_i")
1113                        Wave set2_q = $("root:"+DS2+":"+DS2+"_q")
1114                        Duplicate/O set1_i tmp_i
1115                        Duplicate/O set1_q tmp_q
1116                        tmp_i = set1_i / interp(set1_q[p],set2_q,set2_i)       
1117                       
1118                        //Get cursors
1119                        Variable q1,q2
1120                       
1121                        DoWindow/F DAPlotPanel
1122                        SetActiveSubWindow DAPlotPanel#DAPlot
1123                       
1124                        q1 = CsrXWaveRef(A)[pcsr(A)]
1125                        q2 = CsrXWaveRef(B)[pcsr(B)]
1126
1127                        //Update value display
1128                        ValDisplay DARangeStar_vd,value=_NUM:q1, win=$(ba.win)
1129                        ValDisplay DARangeEnd_vd,value=_NUM:q2, win=$(ba.win)
1130                       
1131                        //Calculate scalefactor
1132                       
1133                        if (NoDS2 == 1)
1134                                Wave avgWave = set1_i
1135                        else
1136                                Wave avgWave = tmp_i
1137                        endif
1138
1139                        Variable p1 = BinarySearch(tmp_q,q1)                   
1140                        Variable p2 = BinarySearch(tmp_q,q2)                   
1141
1142                        //print avgWave
1143
1144                        WaveStats/Q/R=[p1,p2] avgWave
1145                        //print V_avg
1146                        //Update sv control
1147                        SetVariable DAScale_sv, value=_NUM:V_avg, win=$(ba.win)
1148                       
1149                        KillWaves/Z tmp_i,tmp_q
1150                        DoWindow/F DataArithmeticPanel
1151        endswitch
1152
1153End
1154
1155
1156Function DANoDS2Proc(cba) : CheckBoxControl
1157        STRUCT WMCheckBoxAction &cba
1158       
1159       
1160        switch(cba.eventCode)
1161                case 2:
1162                        if (cba.checked == 1)
1163                                //Disable DS2 popup etc
1164                                PopupMenu DS2_popup win=$(cba.win), disable=2
1165                                Button DS2_button win=$(cba.win), disable=2
1166                        else
1167                                //Enable DS2 popup etc
1168                                PopupMenu DS2_popup win=$(cba.win), disable=0
1169                                Button DS2_button win=$(cba.win), disable=0
1170                        endif
1171                        //Sort out plot
1172                        //Fake button press to DAPlotButtonProc
1173                        STRUCT WMButtonAction ba2
1174                        ba2.win = cba.win
1175                        ba2.ctrlName = "DAPlot_button"
1176                        ba2.eventCode = 2
1177                        DAPlotButtonProc(ba2)
1178                        SetActiveSubWindow DAPlotPanel
1179                        DoWindow/F DataArithmeticPanel
1180        endswitch
1181
1182End
1183
1184Function DASaveProc(ba) : ButtonControl
1185        STRUCT WMButtonAction &ba
1186       
1187        switch(ba.eventCode)
1188                case 2:
1189                        ControlInfo/W=$(ba.win) DAResultName_sv
1190                        SaveDataSetToFile(S_Value)
1191                        break
1192        endswitch
1193
1194
1195End
1196
1197/////////////////////// Common Panel Functions ///////////////////////////////////////
1198
1199
1200// is there a simpler way to do this? I don't think so.
1201Function/S DM_DataSetPopupList()
1202
1203        String str=GetAList(4)
1204
1205        if(strlen(str)==0)
1206                str = "No data loaded"
1207        endif
1208        str = SortList(str)
1209       
1210        return(str)
1211End
1212
1213
1214Function DANameSetvarproc(sva) : SetVariableControl
1215        STRUCT WMSetVariableAction &sva
1216               
1217        switch( sva.eventCode )
1218                case 1: // mouse up
1219                case 2: // Enter key
1220                case 3: // Live update
1221                                String sv = sva.sval
1222                                if( strlen(sv) > 25 )
1223                                        sv= sv[0,24]
1224                                        SetVariable  $(sva.ctrlName),win=$(sva.win),value=_STR:sv
1225                                        ControlUpdate /W=$(sva.win) $(sva.ctrlName)
1226                                        Beep
1227                                endif
1228                                Button DASave_Button win=$(sva.win), disable=2
1229                                break
1230                endswitch
1231        return 0
1232End
1233
1234
1235////////////////////// Functions to do manipulations ///////////////////////////////////
1236
1237Function RenameDataSet(dataSetFolder, newName)
1238        String dataSetFolder
1239        String newName
1240       
1241        String dataSetFolderParent,basestr,objName
1242        Variable index = 0
1243       
1244        //Abuse ParseFilePath to get path without folder name
1245        dataSetFolderParent = ParseFilePath(1,dataSetFolder,":",1,0)
1246        //Abuse ParseFilePath to get basestr
1247        basestr = ParseFilePath(0,dataSetFolder,":",1,0)
1248
1249//      try
1250                RenameDataFolder $(dataSetFolder) $(newName)//; AbortOnRTE
1251       
1252
1253                SetDataFolder $(dataSetFolderParent+newName)//; AbortOnRTE
1254                do
1255                        objName = GetIndexedObjName("",1,index)
1256                        if (strlen(objName) == 0)
1257                                break
1258                        endif
1259                        Rename $(objName) $(ReplaceString(basestr,objName,newName))
1260                        index+=1
1261                while(1)
1262                SetDataFolder root:
1263//      catch
1264//              Print "Aborted: " + num2str(V_AbortCode)
1265//              SetDataFolder root:
1266//      endtry
1267End
1268
1269
1270Function DuplicateDataSet(dataSetFolder, newName, forceoverwrite)
1271        String dataSetFolder
1272        String newName
1273        Variable forceoverwrite
1274
1275        String dataSetFolderParent,basestr,objName
1276        Variable index = 0
1277
1278        //Abuse ParseFilePath to get path without folder name
1279        dataSetFolderParent = ParseFilePath(1,dataSetFolder,":",1,0)
1280        //Abuse ParseFilePath to get basestr
1281        basestr = ParseFilePath(0,dataSetFolder,":",1,0)
1282       
1283        print "Duplicating "+dataSetFolder+" as "+newName
1284       
1285        SetDataFolder $(dataSetFolderParent)
1286       
1287        if (!DataFolderExists(newName))
1288                NewDataFolder $(newName)
1289        else
1290                if (!forceoverwrite)
1291                        DoAlert 1, "A dataset with the name "+newName+" already exists. Overwrite?"
1292                        if (V_flag == 2)
1293                                return 1
1294                        endif
1295                endif
1296        endif   
1297
1298        //If we are here, the folder (now) exists and the user has agreed to overwrite
1299        //either in the function call or from the alert.
1300        do
1301                objName = GetIndexedObjName(basestr,1,index)
1302                if (strlen(objName) == 0)
1303                        break
1304                endif
1305                objname = ":"+basestr+":"+objname
1306                        Duplicate/O $(objName) $(ReplaceString(basestr,objName,newName))
1307                index+=1
1308        while(1)
1309
1310        SetDataFolder root:
1311        return 0
1312End
1313
1314
1315// Subtract Set2 From Set1
1316// Use Result_I = Set1_I - f*Set2_I
1317Function SubtractDataSets(set1Name,set2Name,set2Scale,resultName)
1318        String set1Name
1319        String set2Name
1320        Variable set2Scale
1321        String resultName
1322
1323        String set1Path = "root:"+set1Name+":"
1324        String set2Path = "root:"+set2Name+":"
1325        String resultPath = "root:"+resultName+":"
1326       
1327        SetDataFolder root:
1328        //Create folder for result
1329        //UnloadDataSet(resultName)
1330        //Make the folder
1331        if (DuplicateDataSet(set1Path,resultName,0))
1332                return 1
1333        else
1334        //Do subtraction of I waves - including interpolation if necessary.
1335        Wave result_i = $(resultPath+resultName+"_i")
1336        Wave result_s = $(resultPath+resultName+"_s")
1337        Wave set1_i = $(set1Path+set1Name+"_i")
1338        Wave set1_q = $(set1Path+set1Name+"_q")
1339        Wave set1_s = $(set1Path+set1Name+"_s")
1340        Wave set2_i = $(set2Path+set2Name+"_i")
1341        Wave set2_q = $(set2Path+set2Name+"_q")
1342        Wave set2_s = $(set2Path+set2Name+"_s")
1343       
1344        result_i = set1_i - (set2Scale*interp(set1_q[p],set2_q,set2_i))
1345        result_s = sqrt(set1_s^2 + (set2Scale*interp(set1_q[p],set2_q,set2_s))^2 )
1346        //Calculate result error wave - can we produce corrected Q error?
1347       
1348        //Generate history string to record what was done?
1349        return 0
1350        endif
1351End
1352
1353// Add Set2 to Set1
1354// Use Result_I = Set1_I + f*Set2_I
1355Function AddDataSets(set1Name,set2Name,set2Scale,resultName)
1356        String set1Name
1357        String set2Name
1358        Variable set2Scale
1359        String resultName
1360
1361        String set1Path = "root:"+set1Name+":"
1362        String set2Path = "root:"+set2Name+":"
1363        String resultPath = "root:"+resultName+":"
1364       
1365        SetDataFolder root:
1366        //Create folder for result
1367        if(DuplicateDataSet(set1Path,resultName,0))
1368                //User said no overwrite
1369                return 1
1370        else
1371        //Do addition of I waves - including interpolation if necessary.
1372        Wave result_i = $(resultPath+resultName+"_i")
1373        Wave result_s = $(resultPath+resultName+"_s")
1374        Wave set1_i = $(set1Path+set1Name+"_i")
1375        Wave set1_q = $(set1Path+set1Name+"_q")
1376        Wave set1_s = $(set1Path+set1Name+"_s")
1377        Wave set2_i = $(set2Path+set2Name+"_i")
1378        Wave set2_q = $(set2Path+set2Name+"_q")
1379        Wave set2_s = $(set2Path+set2Name+"_s")
1380       
1381        result_i =  set1_i + set2Scale*interp(set1_q[p],set2_q,set2_i) 
1382        //Calculate result error wave (note that this is identical to subtraction)
1383        result_s = sqrt(set1_s^2 + (set2Scale*interp(set1_q[p],set2_q,set2_s))^2 )
1384
1385        //  - can we produce corrected Q error?
1386        //Generate history string to record what was done?
1387        return 0
1388        endif
1389End
1390
1391// Multiply Set1 by Set2
1392// Use Result_I  = Set1_I * (f*Set2_I)
1393Function MultiplyDataSets(set1Name, set2Name, set2Scale, resultName)
1394        String set1Name
1395        String set2Name
1396        Variable set2Scale
1397        String resultName
1398
1399        String set1Path = "root:"+set1Name+":"
1400        String set2Path = "root:"+set2Name+":"
1401        String resultPath = "root:"+resultName+":"
1402       
1403        SetDataFolder root:
1404        //Create folder for result
1405        //Make the folder
1406        if(DuplicateDataSet(set1Path,resultName,0))
1407                //User said no overwrite
1408                return 1
1409        else
1410        //Do multiplcation of I waves - including interpolation if necessary.
1411        Wave result_i = $(resultPath+resultName+"_i")
1412        Wave result_s = $(resultPath+resultName+"_s")
1413        Wave set1_i = $(set1Path+set1Name+"_i")
1414        Wave set1_q = $(set1Path+set1Name+"_q")
1415        Wave set1_s = $(set1Path+set1Name+"_s")
1416        Wave set2_i = $(set2Path+set2Name+"_i")
1417        Wave set2_q = $(set2Path+set2Name+"_q")
1418        Wave set2_s = $(set2Path+set2Name+"_s")
1419       
1420        result_i =  set1_i*set2Scale*interp(set1_q[p],set2_q,set2_i)
1421        //Calculate result error wave
1422        // sum each of the relative errors, interpolating set 2 intensity and error as needed
1423        // then sqrt
1424        result_s = (set2Scale*interp(set1_q[p],set2_q,set2_i)*set1_s)^2
1425        result_s += (set2Scale*set1_i*interp(set1_q[p],set2_q,set2_s))^2
1426        result_s = sqrt(result_s)
1427
1428        // - can we produce corrected Q error?
1429
1430        //Generate history string to record what was done?
1431        return 0
1432        endif
1433End
1434
1435// Divide Set1 by Set2
1436// Use Result_I  = Set1_I / (f*Set2_I)
1437Function DivideDataSets(set1Name, set2Name, set2Scale, resultName)
1438        String set1Name
1439        String set2Name
1440        Variable set2Scale
1441        String resultName
1442
1443        String set1Path = "root:"+set1Name+":"
1444        String set2Path = "root:"+set2Name+":"
1445        String resultPath = "root:"+resultName+":"
1446       
1447        SetDataFolder root:
1448        //Create folder for result
1449        //Make the folder
1450        if(DuplicateDataSet(set1Path,resultName,0))
1451                //User said no overwrite
1452                return 1
1453        else
1454        //Do division of I waves - including interpolation if necessary.
1455        Wave result_i = $(resultPath+resultName+"_i")
1456        Wave result_s = $(resultPath+resultName+"_s")
1457        Wave set1_i = $(set1Path+set1Name+"_i")
1458        Wave set1_q = $(set1Path+set1Name+"_q")
1459        Wave set1_s = $(set1Path+set1Name+"_s")
1460        Wave set2_i = $(set2Path+set2Name+"_i")
1461        Wave set2_q = $(set2Path+set2Name+"_q")
1462        Wave set2_s = $(set2Path+set2Name+"_s")
1463       
1464        result_i =  set1_i/(set2Scale*interp(set1_q[p],set2_q,set2_i)   )
1465        //Calculate result error wave
1466        // sum each of the relative errors, interpolating set 2 intensity and error as needed
1467        // then sqrt
1468        result_s = (set1_s/set2Scale/interp(set1_q[p],set2_q,set2_i))^2
1469        result_s += (interp(set1_q[p],set2_q,set2_s)*set1_i/set2Scale/interp(set1_q[p],set2_q,set2_i)^2)^2
1470        result_s = sqrt(result_s)
1471
1472        // - can we produce corrected Q error?
1473       
1474        //Generate history string to record what was done?
1475        return 0
1476        endif
1477End
1478
1479
1480///////////////////////////Other Utility functions ////////////////////////////
1481
1482Function SaveDataSetToFile(folderName)
1483        String folderName
1484
1485        String protoStr = ""
1486        //Check for history string in folder
1487        //If it doesn't exist then
1488
1489        //Do saving of data file.
1490       
1491        NVAR gXML_Write = root:Packages:NIST:gXML_Write
1492
1493        if (gXML_Write == 1)
1494                ReWrite1DXMLData(folderName)
1495        else
1496                fReWrite1DData(folderName,"tab","CRLF")
1497        endif
1498
1499//      NISTSave1DData(folderName,protoStr)
1500
1501        //Include history string to record what was done?
1502
1503End
1504
1505
1506
1507//////////////////// Write data functions ////////////////////////////////////
1508// AJJ Nov 2009 - should move towards unified writer
1509//
1510//////////////////////////////////////////////////////////////////////////
1511
1512//Function NISTSave1DData(folderPath,protoStr)
1513//      String folderPath, protoStr
1514//     
1515//      //protoStr is string that will be written to either
1516//     
1517//
1518//End
1519
1520
1521
1522///////// SRK - VERY SIMPLE batch converter
1523// no header information is preserved
1524// file names are partially preserved
1525//
1526
1527/// to use this:
1528// -open the Plot Manager and set the path
1529// -run this function
1530//
1531// it doesn't matter if the XML ouput flag is set - this overrides.
1532Function batchXML26ColConvert()
1533
1534        String list, item,path,fname
1535        Variable num,ii
1536       
1537        PathInfo CatPathName
1538        path = S_Path
1539
1540        list = A_ReducedDataFileList("")
1541        num = itemsInList(list)
1542        Print num
1543        for(ii=0;ii<num;ii+=1)
1544                item = StringFromList(ii, list ,";")
1545                fname=path + item
1546                Execute "A_LoadOneDDataWithName(\""+fname+"\",0)"               //won't plot
1547//              easier to load all, then write out, since the name will be changed
1548        endfor
1549       
1550       
1551        list = DM_DataSetPopupList()
1552
1553        num = itemsInList(list)
1554        Print num
1555        for(ii=0;ii<num;ii+=1)
1556                item = StringFromList(ii, list ,";")
1557                fReWrite1DData_noPrompt(item,"tab","CR")
1558        endfor
1559       
1560End
1561
1562// quick version (copied from fReWrite1DData() that NEVER asks for a new fileName
1563// - and right now, always expect 6-column data, either SANS or USANS (re-writes -dQv)
1564// - AJJ Nov 2009 : better make sure we always fake 6 columns on reading then....
1565Function fReWrite1DData_noPrompt(folderStr,delim,term)
1566        String folderStr,delim,term
1567       
1568        String formatStr="",fullpath=""
1569        Variable refnum,dialog=1
1570       
1571        String dataSetFolderParent,basestr
1572       
1573        //setup delimeter and terminator choices
1574        If(cmpstr(delim,"tab")==0)
1575                //tab-delimeted
1576                formatStr="%15.8g\t%15.8g\t%15.8g\t%15.8g\t%15.8g\t%15.8g"
1577        else
1578                //use 3 spaces
1579                formatStr="%15.8g   %15.8g   %15.8g   %15.8g   %15.8g   %15.8g"
1580        Endif
1581        If(cmpstr(term,"CR")==0)
1582                formatStr += "\r"
1583        Endif
1584        If(cmpstr(term,"LF")==0)
1585                formatStr += "\n"
1586        Endif
1587        If(cmpstr(term,"CRLF")==0)
1588                formatStr += "\r\n"
1589        Endif
1590       
1591        //Abuse ParseFilePath to get path without folder name
1592        dataSetFolderParent = ParseFilePath(1,folderStr,":",1,0)
1593        //Abuse ParseFilePath to get basestr
1594        basestr = ParseFilePath(0,folderStr,":",1,0)
1595       
1596        //make sure the waves exist
1597        SetDataFolder $(dataSetFolderParent+basestr)
1598        WAVE/Z qw = $(baseStr+"_q")
1599        WAVE/Z iw = $(baseStr+"_i")
1600        WAVE/Z sw = $(baseStr+"_s")
1601        WAVE/Z resw = $(baseStr+"_res")
1602       
1603        if(WaveExists(qw) == 0)
1604                Abort "q is missing"
1605        endif
1606        if(WaveExists(iw) == 0)
1607                Abort "i is missing"
1608        endif
1609        if(WaveExists(sw) == 0)
1610                Abort "s is missing"
1611        endif
1612        if(WaveExists(resw) == 0)
1613                Abort "Resolution information is missing."
1614        endif
1615       
1616        Duplicate/O qw qbar,sigQ,fs
1617        if(dimsize(resW,1) > 4)
1618                //it's USANS put -dQv back in the last 3 columns
1619                NVAR/Z dQv = USANS_dQv
1620                if(NVAR_Exists(dQv) == 0)
1621                        Abort "It's USANS data, and I don't know what the slit height is."
1622                endif
1623                sigQ = -dQv
1624                qbar = -dQv
1625                fs = -dQv
1626        else
1627                //it's SANS
1628                sigQ = resw[p][0]
1629                qbar = resw[p][1]
1630                fs = resw[p][2]
1631        endif
1632       
1633        dialog=0
1634        if(dialog)
1635                PathInfo/S catPathName
1636//              fullPath = DoSaveFileDialog("Save data as",fname=baseStr+".txt")
1637                fullPath = DoSaveFileDialog("Save data as",fname=baseStr[0,strlen(BaseStr)-2])
1638                Print fullPath
1639                If(cmpstr(fullPath,"")==0)
1640                        //user cancel, don't write out a file
1641                        Close/A
1642                        Abort "no data file was written"
1643                Endif
1644                //Print "dialog fullpath = ",fullpath
1645        Endif
1646        PathInfo catPathName
1647        fullPath = S_Path + baseStr[0,strlen(BaseStr)-2]
1648
1649        Open refnum as fullpath
1650       
1651        fprintf refnum,"Modified data written from folder %s on %s\r\n",baseStr,(date()+" "+time())
1652        wfprintf refnum,formatStr,qw,iw,sw,sigQ,qbar,fs
1653        Close refnum
1654       
1655        KillWaves/Z sigQ,qbar,fs
1656       
1657        SetDataFolder root:
1658        return(0)
1659End
1660
1661///////////end SRK
Note: See TracBrowser for help on using the repository browser.