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

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

Added 1D data processing routines to the USANS package.

Added the Data Management panel to all menus - "ReWrite? Experimental Data". This mainly adds the functionality to convert data back and forth between XML and 6-column. Still needs debugging.

Added menu item for Rebinning of 1D data. Brings up a separate panel, since I couldn't figure out where it really belonged. It's small enough that it could be added to most any panel. Routines are at the end of the DataSetHandling?.ipf file.

File size: 56.5 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// Function batchGrasp26ColConvert()
9//
10// these both need a real interface, and a way to better define the name of the
11// converted output file. And some retention of the header would be nice too...
12//
13
14
15// Functions and interfaces to manage datasets now that they are in data folders
16// Planned interface
17// - Panel to select/load data and then select operation
18// Planned functions:
19// - Rename a data set - AJJ Done Nov 2009
20// - Duplicate a data set - AJJ Done Nov 2009
21// - Subtract one data set from another
22// - Add one data set to another
23// - Divide data sets
24// - Multiply data sets
25// - Save a data folder to file
26
27/////////////////// Data Management Panel ///////////////////////////////////////////////////////
28
29//
30Proc MakeDMPanel()
31        DoWindow/F DataManagementPanel
32        if(V_flag==0)
33                fMakeDMPanel()
34        endif
35End
36
37Proc fMakeDMPanel()
38        PauseUpdate; Silent 1           // building window...
39        NewPanel /W=(459,44,959,410)/N=DataManagementPanel/K=2 as "Data Set Management"
40        ModifyPanel fixedSize=1,cbRGB=(30000,60000,60000)
41
42        //Main bit of panel
43        GroupBox grpBox_0,pos={20,10},size={460,100}
44        GroupBox grpBox_1,pos={20,130},size={460,70}
45        GroupBox grpBox_2,pos={20,220},size={460,40}
46
47        GroupBox grpBox_3,pos={20,280},size={460,40}
48
49        Button DS_button,title="Load 1D Data Set",pos={300,20},size={150,20}
50        Button DS_button,proc=DM_LoadDataSetProc
51        Button Unload_button,title="Unload 1D Data Set",pos={300,50},size={150,20}
52        Button Unload_button,proc=DM_UnloadProc
53        Button Save_button,title="Save 1D Data Set",pos={300,80},size={150,20}
54        Button Save_button,proc=DM_SaveProc
55        PopupMenu DS_popup,pos={30,40},size={318,20},title="Data Set ",proc=DM_PopupProc
56        PopupMenu DS_popup,mode=1,value= #"DM_DataSetPopupList()"
57
58        CheckBox XMLStateCtrl,pos={30,82},size={124,14},title="XML Output Enabled (change in Preferences)"
59        CheckBox XMLStateCtrl,help={"Default output format is canSAS XML rather than NIST 6 column"}
60        CheckBox XMLStateCtrl,value= root:Packages:NIST:gXML_Write,disable=2
61
62        Button Rename_button,title="Rename",pos={75,170},size={150,20}
63        Button Rename_button,proc=DM_RenameProc
64        Button Duplicate_button,title="Duplicate",pos={275,170},size={150,20}
65        Button Duplicate_button,proc=DM_DuplicateProc
66
67        SetVariable NewName_setvar,title="New Name (max 25 characters)",pos={50,140},size={400,20}
68        SetVariable NewName_setvar,fsize=12,value=_STR:"",proc=DMNameSetvarproc,live=1
69               
70        Button SaveAsXML_button,title="Save as canSAS XML",pos={75,230},size={150,20}
71        Button SaveAsXML_button,proc=DMSaveAsXMLproc   
72
73        Button SaveAs6col_button,title="Save as NIST 6 column",pos={275,230},size={160,20}
74        Button SaveAs6col_button,proc=DMSaveAs6colproc 
75       
76        Button BatchConvertData_button,title="Batch Convert Format of 1D Data Files",pos={75,290},size={350,20}
77        Button BatchConvertData_button,proc=DMBatchConvertProc
78                       
79        Button DMDone_button,title="Done",pos={360,330},size={60,20}
80        Button DMDone_button,proc=DMDoneButtonProc
81        Button DMHelp_button,title="?",pos={440,330},size={30,20}
82        Button DMHelp_button,proc=DMHelpButtonProc
83       
84       
85        ControlInfo/W=DataManagementPanel DS_popup
86        if (cmpstr(S_Value,"No data loaded") == 0)
87                SetVariable NewName_setvar,value=_STR:"dataset_copy"
88        else
89                //fake call to popup
90                //// -- can't use STRUCT in a Proc, only a function
91//              STRUCT WMPopupAction pa
92//              pa.win = "DataManagementPanel"
93//              pa.ctrlName = "DS_popup"
94//              pa.eventCode = 2
95//              DM_PopupProc(pa)
96        endif
97
98End
99
100Function DMSaveAs6colproc(ba) : ButtonControl
101        STRUCT WMButtonAction &ba
102       
103        switch( ba.eventCode)
104                case 2:
105                        ControlInfo/W=DataManagementPanel DS_popup
106                        String folderName = S_Value
107                        fReWrite1DData(folderName,"tab","CRLF")
108                        break
109        endswitch
110       
111        return 0
112End
113
114Function DMSaveAsXMLproc(ba) : ButtonControl
115        STRUCT WMButtonAction &ba
116       
117        switch( ba.eventCode)
118                case 2:
119                        ControlInfo/W=DataManagementPanel DS_popup
120                        String folderName = S_Value
121                        ReWrite1DXMLData(folderName)
122                        break
123        endswitch
124       
125        return 0
126End
127
128
129//Function SaveDataSetToFile(folderName)
130//      String folderName
131//
132//      String protoStr = ""
133//      //Check for history string in folder
134//      //If it doesn't exist then
135//
136//      //Do saving of data file.
137//     
138//      NVAR gXML_Write = root:Packages:NIST:gXML_Write
139//
140//      if (gXML_Write == 1)
141//              ReWrite1DXMLData(folderName)
142//      else
143//              fReWrite1DData(folderName,"tab","CRLF")
144//      endif
145//
146//      //Include history string to record what was done?
147//
148//End
149
150
151
152
153
154
155Function DMBatchConvertProc(ba) : ButtonControl
156        STRUCT WMButtonAction &ba
157       
158        switch( ba.eventCode)
159                case 2:
160                        Execute "MakeNCNRBatchConvertPanel()"
161                        break
162        endswitch
163       
164        return 0
165End
166
167
168Function DMNameSetvarproc(sva) : SetVariableControl
169        STRUCT WMSetVariableAction &sva
170               
171        switch( sva.eventCode )
172                case 1: // mouse up
173                case 2: // Enter key
174                case 3: // Live update
175                                String sv = sva.sval
176                                if( strlen(sv) > 25 )
177                                        sv= sv[0,24]
178                                        SetVariable  $(sva.ctrlName),win=$(sva.win),value=_STR:sv
179                                        ControlUpdate /W=$(sva.win) $(sva.ctrlName)
180                                        Beep
181                                endif
182                                break
183                endswitch
184        return 0
185End
186
187Function DM_RenameProc(ba) : ButtonControl
188        STRUCT WMButtonAction &ba
189       
190        String DS,NewName
191       
192        ControlInfo/W=$(ba.win) DS_popup
193        DS = S_Value
194       
195        ControlInfo/W=$(ba.win) NewName_setvar
196        NewName = CleanupName(S_Value, 0 )              //clean up any bad characters, and put the cleaned string back
197        SetVariable NewName_setvar,value=_STR:NewName
198       
199        switch (ba.eventcode)
200                case 2: // mouse up
201                        RenameDataSet(DS,NewName)
202                        ControlUpdate /W=$(ba.win) DS_Popup
203                        break
204        endswitch
205
206
207       
208        return 0
209end
210               
211Function DM_DuplicateProc(ba) : ButtonControl
212        STRUCT WMButtonAction &ba
213       
214        String DS,NewName
215       
216        ControlInfo/W=$(ba.win) DS_popup
217        DS = S_Value
218       
219        ControlInfo/W=$(ba.win) NewName_setvar
220        NewName = CleanupName(S_Value, 0 )              //clean up any bad characters, and put the cleaned string back
221        SetVariable NewName_setvar,value=_STR:NewName
222       
223        switch (ba.eventcode)
224                case 2: // mouse up
225                        DuplicateDataSet(DS,NewName,0)
226                        ControlUpdate /W=$(ba.win) DS_Popup
227                        break
228        endswitch
229       
230        return 0
231end
232
233Function DM_SaveProc(ba) : ButtonControl
234        STRUCT WMButtonAction &ba
235       
236        switch(ba.eventCode)
237                case 2:
238                        ControlInfo/W=$(ba.win) DS_popup
239                        SaveDataSetToFile(S_Value)
240                        break
241        endswitch
242       
243        return 0
244end
245
246Function DM_UnloadProc(ba) : ButtonControl
247        STRUCT WMButtonAction &ba
248
249        switch (ba.eventcode)
250                case 2: // mouse up
251                       
252                        String savDF=GetDataFolder(1)
253                        String DF
254                        ControlInfo /W=$(ba.win) DS_Popup
255                        DF = S_Value
256                       
257                        print DF
258                        //check for horrific null output from control
259                        if (cmpstr(DF,"") != 0)
260                       
261                                SetDataFolder DF
262                                KillVariables/A
263                                SetDataFolder savDF
264                       
265                                KillDataFolder/Z $DF
266                                ControlUpdate /W=$(ba.win) DS_Popup
267                       
268                                ControlInfo/W=DataManagementPanel DS_popup
269                                if (cmpstr(S_Value,"No data loaded") == 0)
270                                        SetVariable NewName_setvar,value=_STR:"dataset_copy"
271                                else
272                                        //fake call to popup
273                                        STRUCT WMPopupAction pa
274                                        pa.win = "DataManagementPanel"
275                                        pa.ctrlName = "DS_popup"
276                                        pa.eventCode = 2
277                                        DM_PopupProc(pa)
278                                endif
279                        endif
280                        break
281        endswitch
282       
283        return 0
284end
285               
286
287Function DM_PopupProc(pa) : PopupMenuControl
288        STRUCT WMPopupAction &pa
289       
290        String resultName
291       
292        switch( pa.eventCode)
293                case 2:
294                        //print "Called by "+pa.ctrlname+" with value "+pa.popStr
295                        ControlInfo/W=$(pa.win) $(pa.ctrlName)
296                        String popStr = S_Value
297                        if (stringmatch(pa.ctrlname,"*DS*") == 1)
298                                resultName = stringfromlist(0,popStr,"_")+"_copy"
299                               
300                                SetVariable NewName_setvar win=$(pa.win), value=_STR:resultName
301                        endif
302                break
303        endswitch
304       
305
306End
307
308
309//Must follow naming scheme to match buttons to popups
310//"Name_button" goes with "Name_popup"
311Function DM_LoadDataSetProc(ba) : ButtonControl
312        STRUCT WMButtonAction &ba
313
314
315        switch( ba.eventCode )
316                case 2: // mouse up
317                        // click code here
318                        String cmd = "A_LoadOneDDataWithName(\"\","+num2str(0)+")"
319                        Execute cmd
320               
321                        SVAR gLastFileName = root:packages:NIST:gLastFileName
322
323                        String windowName = ba.win
324                        String popupName = StringFromList(0,ba.ctrlName,"_")+"_popup"
325                       
326                        ControlUpdate/W=$(windowName) $(popupName)
327                        //instead of a simple controlUpdate, better to pop the menu to make sure that the other menus follow
328                        // convoluted method to find the right item and pop the menu.
329
330                        String list,folderStr
331                        Variable num
332                        folderStr = CleanupName(gLastFileName,0)
333                        list = DM_DataSetPopupList()
334                        num=WhichListItem(folderStr,list,";",0,0)
335                        if(num != -1)
336                                PopupMenu $(popupName),mode=num+1,win=$(windowName)
337                                ControlUpdate/W=$(windowName) $(popupName)
338                               
339                                if (cmpstr(popupName,"DS_popup") ==  0)
340                                        //send fake mouse action to popup to update old name if
341                                        Struct WMPopupAction pa
342                                        pa.eventCode = 2                //fake mouse up
343                                        pa.win = windowName
344                                        pa.ctrlName = "DS_popup"
345                                        DM_PopupProc(pa)
346                                endif                   
347                        endif
348                        break
349        endswitch
350       
351        return 0
352End
353
354
355Function/S DMGetDSName(dsNum)
356        Variable dsNum
357       
358        String ctrlName
359        if (dsNum == 1)
360                ctrlName = "DS1_popup"
361        elseif (dsNum == 2)
362                ctrlName = "DS2_popup"
363        endif
364       
365        ControlInfo/W=DataManagementPanel $(ctrlName)
366
367        Return S_Value
368
369End
370
371
372Function DMDoneButtonProc(ba) : ButtonControl
373        STRUCT WMButtonAction &ba
374       
375        String win = ba.win
376
377        switch (ba.eventCode)
378                case 2:
379                        DoWindow/K DataManagementPanel
380                        break
381        endswitch
382
383        return 0
384End
385
386Function DMHelpButtonProc(ba) : ButtonControl
387        STRUCT WMButtonAction &ba
388       
389        String win = ba.win
390
391        switch (ba.eventCode)
392                case 2:
393                        // click code here
394                        DisplayHelpTopic/Z/K=1 "Data Set Management"
395                        if(V_flag !=0)
396                                DoAlert 0,"The Data Set Management Help file could not be found"
397                        endif
398                        break
399        endswitch
400
401        return 0
402End
403
404/////////////////////// Batch Data Conversion Panel ////////////////////////////////////
405//
406//
407
408Proc MakeNCNRBatchConvertPanel()
409        NCNRInitBatchConvert()
410        DoWindow/F NCNRBatchConvertPanel
411        if(V_flag==0)
412                fMakeNCNRBatchConvertPanel()
413        endif
414End
415
416
417Function NCNRInitBatchConvert()
418        NewDataFolder/O/S root:Packages:NIST:BatchConvert
419        Make/O/T/N=1 filewave=""
420        Make/O/N=1 selWave=0
421        Variable/G ind=0,gRadioVal=1
422        SetDataFolder root:
423End
424
425Proc fMakeNCNRBatchConvertPanel()
426        PauseUpdate; Silent 1           // building window...
427        NewPanel /W=(658,347,1018,737)/N=NCNRBatchConvertPanel/K=2 as "Batch Convert 1D Data"
428//      NewPanel /W=(658,347,1018,737)/N=NCNRBatchConvertPanel as "Batch Convert 1D Data"
429        ModifyPanel cbRGB=(40000,50000,32896)
430        ModifyPanel fixedSize=1
431       
432        ListBox fileList,pos={13,11},size={206,179}
433        ListBox fileList,listWave=root:Packages:NIST:BatchConvert:fileWave
434        ListBox fileList,selWave=root:Packages:NIST:BatchConvert:selWave,mode= 4
435
436        Button button7,pos={238,20},size={100,20},proc=NCNRBatchConvertNewFolder,title="New Folder"
437        Button button7,help={"Select a new data folder"}
438        TitleBox msg0,pos={238,160},size={100,30},title="\JCShift-click to\rselect multiple files"
439        TitleBox msg0,frame=0,fixedSize=1
440       
441        GroupBox filterGroup,pos={13,200},size={206,60},title="Filter list by input file type"
442        CheckBox filterCheck_1,pos={24,220},size={36,14},title="XML",value= 1,mode=1, proc=BC_filterCheckProc
443        CheckBox filterCheck_2,pos={24,239},size={69,14},title="ABS or AVE",value= 0,mode=1, proc=BC_filterCheckProc
444        CheckBox filterCheck_3,pos={100,220},size={69,14},title="none",value= 0,mode=1, proc=BC_filterCheckProc
445       
446        Button button8,pos={238,75},size={100,20},proc=NCNRBatchConvertHelpProc,title="Help"
447        Button button9,pos={238,47},size={100,20},proc=NCNRBatchConvertRefresh,title="Refresh List"
448        Button button10,pos={238,102},size={100,20},proc=NCNRBatchConvertSelectAll,title="Select All"
449        Button button0,pos={238,130},size={100,20},proc=NCNRBatchConvertDone,title="Done"
450
451        GroupBox outputGroup,pos={13,270},size={206,60},title="Output File Type"
452        CheckBox outputCheck_1,pos={24,289},size={36,14},title="XML",value= 0,mode=1, proc=BC_outputCheckProc
453        CheckBox outputCheck_2,pos={24,309},size={69,14},title="ABS or AVE",value= 1,mode=1, proc=BC_outputCheckProc
454       
455        Button button6,pos={13,350},size={206,20},proc=NCNRBatchConvertFiles,title="Convert File(s)"
456        Button button6,help={"Converts the files to the format selected"}
457       
458
459       
460End
461
462Function BC_filterCheckProc(ctrlName,checked) : CheckBoxControl
463        String ctrlName
464        Variable checked
465
466        NVAR gRadioVal= root:Packages:NIST:BatchConvert:gRadioVal
467       
468        strswitch (ctrlName)
469                case "filterCheck_1":
470                        gRadioVal= 1
471                        break
472                case "filterCheck_2":
473                        gRadioVal= 2
474                        break
475                case "filterCheck_3":
476                        gRadioVal= 3
477                        break
478        endswitch
479        CheckBox filterCheck_1,value= gRadioVal==1
480        CheckBox filterCheck_2,value= gRadioVal==2
481        CheckBox filterCheck_3,value= gRadioVal==3
482       
483        NCNRBatchConvertGetList()
484         
485        return(0)
486End
487
488Function BC_outputCheckProc(ctrlName,checked) : CheckBoxControl
489        String ctrlName
490        Variable checked
491
492        if(cmpstr("outputCheck_1",ctrlName)==0)
493                CheckBox outputCheck_1,value=checked
494                CheckBox outputCheck_2,value=!checked
495        else
496                CheckBox outputCheck_1,value=!checked
497                CheckBox outputCheck_2,value=checked
498        endif
499       
500        return(0)
501End
502
503Function NCNRBatchConvertFiles(ba) : ButtonControl
504                STRUCT WMButtonAction &ba
505               
506               
507                switch (ba.eventCode)
508                        case 2:
509                       
510                                //check the input/output as best I can (none may be the input filter)
511                                Variable inputType,outputType=1
512                                NVAR gRadioVal= root:Packages:NIST:BatchConvert:gRadioVal
513                                inputType = gRadioVal
514                                ControlInfo outputCheck_1
515                                if(V_value==1)
516                                        outputType = 1          //xml
517                                else
518                                        outputType = 2          //6-col
519                                endif
520                               
521                                if(inputType==outputType)
522                                        DoAlert 0,"Input and output types are the same. Nothing will be converted"
523                                        return(0)
524                                endif
525                       
526                       
527                                        // input and output are different, proceed
528
529                                Wave/T fileWave=$"root:Packages:NIST:BatchConvert:fileWave"
530                                Wave sel=$"root:Packages:NIST:BatchConvert:selWave"
531                               
532                                String fname="",pathStr="",newFileName=""
533                                Variable ii,num
534                                PathInfo catPathName                    //this is where the files are
535                                pathStr=S_path
536                                                       
537                                // process the selected items
538                                num=numpnts(sel)
539                                ii=0
540                                do
541                                        if(sel[ii] == 1)
542                                                fname=pathStr + fileWave[ii]
543                                               
544                                                if(outputType == 1)
545                                                        convertNISTtoNISTXML(fname)
546                                                endif
547                                               
548                                                if(outputType == 2)
549                                                        convertNISTXMLtoNIST6Col(fname)
550                                                endif
551                                        endif
552                                        ii+=1
553                                while(ii<num)
554                               
555                                break
556                endswitch
557
558        return 0
559End
560
561Function NCNRBatchConvertSelectAll(ba) : ButtonControl
562        STRUCT WMButtonAction &ba
563       
564        switch (ba.eventcode)
565                case 2:
566                        Wave sel=$"root:Packages:NIST:BatchConvert:selWave"
567                        sel = 1
568                        break
569        endswitch
570
571End
572
573Function NCNRBatchConvertNewFolder(ba) : ButtonControl
574        STRUCT WMButtonAction &ba
575       
576        switch (ba.eventcode)
577                case 2:
578                        A_PickPath()
579                        NCNRBatchConvertGetList()
580                        break
581        endswitch
582
583End
584
585
586// filter is a bit harsh - will need to soften this by presenting an option to enter the suffix
587//
588Function NCNRBatchConvertGetList()
589
590        //make sure that path exists
591        PathInfo catPathName
592        if (V_flag == 0)
593                Abort "Folder path does not exist - use \"New Folder\" button"
594        Endif
595       
596        String newList = A_ReducedDataFileList(""),tmpList=""
597        Variable num
598       
599        NVAR gRadioVal= root:Packages:NIST:BatchConvert:gRadioVal
600        ControlInfo filterCheck_1
601        if(gRadioVal == 1)
602                //keep XML data
603                tmpList = ListMatch(newList, "*.ABSx" ,";")             //note that ListMatch is not case-sensitive
604                tmpList += ListMatch(newList, "*.AVEx" ,";")
605                tmpList += ListMatch(newList, "*.CORx" ,";")
606                tmpList += ListMatch(newList, "*.xml" ,";")
607        else
608                if(gRadioVal ==2)
609                        //keep ave, abs data
610                        tmpList = ListMatch(newList, "*.ABS" ,";")
611                        tmpList += ListMatch(newList, "*.AVE" ,";")
612                else
613                        //return everything
614                        tmpList = newList
615                endif
616        endif
617        newList = tmpList
618       
619        num=ItemsInList(newlist,";")
620        WAVE/T fileWave=$"root:Packages:NIST:BatchConvert:fileWave"
621        WAVE selWave=$"root:Packages:NIST:BatchConvert:selWave"
622        Redimension/N=(num) fileWave
623        Redimension/N=(num) selWave
624        fileWave = StringFromList(p,newlist,";")
625        Sort filewave,filewave
626       
627        return 0
628End
629
630Function NCNRBatchConvertRefresh(ba) : ButtonControl
631                STRUCT WMButtonAction &ba
632               
633                switch (ba.eventCode)
634                        case 2:
635                                NCNRBatchConvertGetList()
636                                break
637                endswitch
638               
639                return 0
640End
641       
642
643Function NCNRBatchConvertDone(ba) : ButtonControl
644                STRUCT WMButtonAction &ba
645
646                switch (ba.eventCode)
647                        case 2:
648                                DoWindow/K NCNRBatchConvertPanel
649                                break
650                endswitch
651               
652                return 0
653End
654
655Function NCNRBatchConvertHelpProc(ba) : ButtonControl
656        STRUCT WMButtonAction &ba
657       
658        String win = ba.win
659
660        switch (ba.eventCode)
661                case 2:
662                        // click code here
663                        DisplayHelpTopic/Z/K=1 "Batch Data Conversion"
664                        if(V_flag !=0)
665                                DoAlert 0,"The Batch Data Conversion Help file could not be found"
666                        endif
667                        break
668        endswitch
669
670        return 0
671End
672
673/////////////////////// Data Arithmetic Panel /////////////////////////////////////////
674
675//
676Function MakeDAPanel()
677        DoWindow/F DataArithmeticPanel
678        if(V_flag==0)
679                fMakeDAPanel()
680        else
681                DoWindow/F DAPlotPanel
682        endif
683       
684        return(0)
685End
686
687Function fMakeDAPanel()
688        PauseUpdate; Silent 1           // building window...
689        DoWindow/K DataArithmeticPanel
690        NewPanel /W=(459,44,959,404)/N=DataArithmeticPanel/K=2 as "Data Set Arithmetic"
691        ModifyPanel fixedSize=1
692
693        //Main bit of panel
694        GroupBox grpBox_0,pos={20,10},size={460,105}
695
696        Button DS1_button,pos={300,20},size={150,20},proc=DA_LoadDataSetProc,title="Load 1D Data Set 1"
697        Button DS1_button,valueColor=(65535,0,0),userdata="DS1"
698        PopupMenu DS1_popup,pos={30,21},size={318,20},title="Data Set 1"
699        PopupMenu DS1_popup,mode=1,value= #"DM_DataSetPopupList()"
700        PopupMenu DS1_popup,proc=DA_PopupProc
701        PopupMenu DS1_popup,fsize=12,fcolor=(65535,0,0),valueColor=(65535,0,0)
702
703        Button DS2_button,pos={300,50},size={150,20},proc=DA_LoadDataSetProc,title="Load 1D Data Set 2"
704        Button DS2_button,valueColor=(0,0,65535),userdata="DS2"
705        PopupMenu DS2_popup,pos={30,51},size={318,20},title="Data Set 2"
706        PopupMenu DS2_popup,mode=1,value= #"DM_DataSetPopupList()"
707        PopupMenu DS2_popup,proc=DA_PopupProc
708        PopupMenu DS2_popup,fsize=12,fcolor=(0,0,65535),valueColor=(0,0,65535)
709
710        Button DAPlot_button,title="Plot",pos={100,85},size={150,20}
711        Button DAPlot_button,proc=DAPlotButtonProc
712        Button DADone_button,title="Done",pos={360,85},size={60,20}
713        Button DADone_button,proc=DADoneButtonProc
714        Button DAHelp_button,title="?",pos={440,85},size={30,20}
715        Button DAHelp_button,proc=DAHelpButtonProc
716
717
718        //Tabs
719        TabControl DATabs,pos={20,120},size={460,220},tabLabel(0)="Subtract", proc=DATabsProc
720        TabControl DATabs,tablabel(1)="Add",tablabel(2)="Multiply",tablabel(3)="Divide"
721        TabControl DATabs,value=0
722
723        Button DACalculate_button,title="Calculate",pos={50,310},size={150,20}
724        Button DACalculate_button,proc=DACalculateProc
725        Button DASave_button,title="Save Result",pos={300,310},size={150,20}
726        Button DASave_button,proc=DASaveProc,disable=2
727        Button DACursors_button,title="Get Matching Range",pos={175,250},size={150,20}
728        Button DACursors_button,proc=DACursorButtonProc
729       
730        SetVariable DAResultName_sv,title="Result Name (max 25 characters)",pos={50,280},size={400,20}
731        SetVariable DAResultName_Sv,fsize=12,proc=DANameSetvarproc,live=1
732        //Update the result name
733        ControlInfo/W=DataArithmeticPanel DS1_popup
734        if (cmpstr(S_Value,"No data loaded") == 0)
735                SetVariable DAResultName_sv,value=_STR:"SubtractionResult"
736        else
737                //fake call to popup
738                STRUCT WMPopupAction pa
739                pa.win = "DataArithmeticPanel"
740                pa.ctrlName = "DS1_popup"
741                pa.eventCode = 2
742                DA_PopupProc(pa)
743        endif
744       
745        CheckBox DANoDS2_cb,title="Data Set 2 = 1?",pos={300,180}
746        CheckBox DANoDS2_cb,proc=DANoDS2Proc
747       
748        ValDisplay DARangeStar_vd,title="Start",pos={40,220},size={100,20},fsize=12,value=_NUM:0
749        ValDisplay DARangeEnd_vd,title="End  ",pos={160,220},size={100,20},fsize=12,value=_NUM:0
750       
751        SetVariable DAScale_sv,title="Scale Factor (f)",pos={280,220},size={180,20},fsize=12,value=_NUM:1
752
753        GroupBox grpBox_1,pos={30,210},size={440,70}
754       
755        NewPanel/HOST=DataArithmeticPanel/N=arithDisplay/W=(50,150,170,190)
756       
757        //Update the result name
758        ControlInfo/W=DataArithmeticPanel DS1_popup
759
760       
761       
762        arithDisplayProc(0)
763       
764End
765
766
767Function MakeDAPlotPanel()
768        PauseUpdate; Silent 1           // building window...
769        DoWindow/K DAPlotPanel
770        NewPanel /W=(14,44,454,484)/N=DAPlotPanel/K=1 as "Data Set Arithmetic"
771        ModifyPanel fixedSize=1
772
773        Display/HOST=DAPlotPanel/N=DAPlot/W=(0,0,440,400)
774        Legend
775        ShowInfo
776        SetActiveSubWindow DAPlotPanel
777        Checkbox DAPlot_log_cb, title="Log I(q)", pos={20,410},value=0
778        Checkbox DAPlot_log_cb, proc=DALogLinIProc
779        Checkbox DAPlot_lin_cb, title="High Q Linear", pos={100,410},value=0
780        Checkbox DAPlot_lin_cb, proc=DAHighQLinProc
781       
782End
783
784Function AddDAPlot(dataset)
785        Variable dataset
786       
787        String win = "DataArithmeticPanel"
788        String DS1name,DS2name,ResultName
789
790        switch(dataset)
791                case 1:
792                        ControlInfo/W=$(win) DS1_popup
793                        DS1name = S_Value
794                        Wave qWave = $("root:"+DS1name+":"+DS1name+"_q")
795                        Wave iWave = $("root:"+DS1name+":"+DS1name+"_i")
796                        Wave errWave = $("root:"+DS1name+":"+DS1name+"_s")
797                        AppendToGraph/W=DAPlotPanel#DAPlot iWave vs Qwave
798                        ErrorBars/W=DAPlotPanel#DAPlot /T=0 $(DS1name+"_i"), Y wave=(errWave,errWave)
799                        ModifyGraph/W=DAPlotPanel#DAPlot rgb($(DS1name+"_i"))=(65535,0,0)
800                        ControlInfo/W=$(win) DANoDS2_cb
801//                      if (V_Value == 1)
802//                                      Cursor/W=DAPlotPanel#DAPlot A, $(DS1name+"_i"), leftx(iWave)
803//                                      Cursor/W=DAPlotPanel#DAPlot/A=0 B, $(DS1name+"_i"),  rightx(iWave)
804//                      endif
805                        break
806                case 2:
807                        ControlInfo/W=$(win) DANoDS2_cb
808                        if (V_Value == 0)
809                                ControlInfo/W=$(win) DS2_popup
810                                DS2name = S_Value
811                                if(cmpstr(DS2name,"No data loaded")==0)
812                                        break                   //in case someone loads set 1, but not set two, then plots
813                                endif
814                                Wave qWave = $("root:"+DS2name+":"+DS2name+"_q")
815                                Wave iWave = $("root:"+DS2name+":"+DS2name+"_i")
816                                Wave errWave = $("root:"+DS2name+":"+DS2name+"_s")
817                                AppendToGraph/W=DAPlotPanel#DAPlot iWave vs Qwave
818                                ErrorBars/W=DAPlotPanel#DAPlot /T=0 $(DS2name+"_i"), Y wave=(errWave,errWave)                   
819                                ModifyGraph/W=DAPlotPanel#DAPlot rgb($(DS2name+"_i"))=(0,0,65535)
820                                Cursor/W=DAPlotPanel#DAPlot A, $(DS2name+"_i"), leftx(iWave)
821                                Cursor/W=DAPlotPanel#DAPlot/A=0 B, $(DS2name+"_i"),  rightx(iWave)
822                        else
823                                ControlInfo/W=$(win) DS1_popup
824                                DS1name = S_Value
825                                DuplicateDataSet("root:"+DS1name,"NullSolvent",1)
826                                Wave qWave =root:NullSolvent:NullSolvent_q
827                                Wave iWave = root:NullSolvent:NullSolvent_i
828                                Wave errWave = root:NullSolvent:NullSolvent_s
829                                Wave iWaveDS1 = $("root:"+DS1name+":"+DS1name+"_i")
830                                iWave = 1
831                                errWave = 0
832                                AppendToGraph/W=DAPlotPanel#DAPlot iWave vs Qwave
833                                ErrorBars/W=DAPlotPanel#DAPlot /T=0 NullSolvent_i, Y wave=(errWave,errWave)                     
834                                ModifyGraph/W=DAPlotPanel#DAPlot rgb(NullSolvent_i)=(0,0,65535)
835                                //Cursor/W=DAPlotPanel#DAPlot A, NullSolvent_i, leftx(iWave)
836                                //Cursor/W=DAPlotPanel#DAPlot/A=0 B, NullSolvent_i,  rightx(iWave)
837                                if(strlen(CsrInfo(A,"DAPlotPanel#DAPlot")) == 0)                //cursors not already on the graph             
838                                        Cursor/W=DAPlotPanel#DAPlot A, $(DS1Name+"_i"), leftx(iWaveDS1)
839                                        Cursor/W=DAPlotPanel#DAPlot/A=0 B, $(DS1Name+"_i"),  rightx(iWaveDS1)                   
840                                endif
841                        endif
842                        break
843                case 3:
844                        ControlInfo/W=$(win) DAResultName_sv
845                        ResultName = S_Value
846                        Wave qWave = $("root:"+ResultName+":"+ResultName+"_q")
847                        Wave iWave = $("root:"+ResultName+":"+ResultName+"_i")
848                        Wave errWave = $("root:"+ResultName+":"+ResultName+"_s")
849                        AppendToGraph/W=DAPlotPanel#DAPlot iWave vs Qwave
850                        ErrorBars/W=DAPlotPanel#DAPlot /T=0 $(ResultName+"_i"), Y wave=(errWave,errWave)
851                        ModifyGraph/W=DAPlotPanel#DAPlot rgb($(ResultName+"_i"))=(0,65535,0)
852                        break
853        endswitch
854
855        ControlInfo/W=DAPlotPanel DAPlot_log_cb
856        ModifyGraph/W=DAPlotPanel#DAPlot mode=3, msize=2, marker=19, mirror=1, tick=2, log(bottom)=1,log(left)=V_Value,tickUnit=1
857End
858
859Function arithDisplayProc(s)
860        Variable s
861
862        SetActiveSubWindow DataArithmeticPanel#arithDisplay
863
864        switch (s)
865                case 0:
866                        //Subtract
867                        DrawAction/L=progFront delete
868                        DrawRect 0,0,120,40
869                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,65535,0)
870                        DrawText 10,32,"I"
871                        SetDrawEnv fname="Times", fsize=22, fstyle= 1,textrgb= (0,0,0)
872                        DrawText 20,30,"="
873                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (65535,0,0)
874                        DrawText 35,32,"I"
875                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,65535)
876                        DrawText 95,32,"I"
877                        SetDrawEnv fname="Symbol", fsize=22, fstyle= 1,textrgb= (0,0,0)
878                        DrawText 52,32,"-"
879                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,0)
880                        DrawText 75,30,"f"
881                        SetDrawEnv fname="Times", fsize=22, fstyle= 1,textrgb= (0,0,0)
882                        DrawText 85,32,"*"
883                        break
884                case 1:
885                        //Add
886                        DrawAction/L=progFront delete
887                        DrawRect 0,0,120,40
888                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,65535,0)
889                        DrawText 10,32,"I"
890                        SetDrawEnv fname="Times", fsize=22, fstyle= 1,textrgb= (0,0,0)
891                        DrawText 20,30,"="
892                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (65535,0,0)
893                        DrawText 35,32,"I"
894                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,65535)
895                        DrawText 95,32,"I"
896                        SetDrawEnv fname="Symbol", fsize=22, fstyle= 1,textrgb= (0,0,0)
897                        DrawText 52,32,"+"
898                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,0)
899                        DrawText 75,30,"f"
900                        SetDrawEnv fname="Times", fsize=22, fstyle= 1,textrgb= (0,0,0)
901                        DrawText 85,32,"*"
902                        break
903                case 2:
904                        //Multiply
905                        DrawAction/L=progFront delete
906                        DrawRect 0,0,120,40
907                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,65535,0)
908                        DrawText 10,32,"I"
909                        SetDrawEnv fname="Times", fsize=22, fstyle= 1,textrgb= (0,0,0)
910                        DrawText 20,30,"="
911                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (65535,0,0)
912                        DrawText 35,32,"I"
913                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,65535)
914                        DrawText 95,32,"I"
915                        SetDrawEnv fname="Symbol", fsize=22, fstyle= 1,textrgb= (0,0,0)
916                        DrawText 52,32,"*"
917                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,0)
918                        DrawText 66,30,"("
919                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,0)
920                        DrawText 75,30,"f"
921                        SetDrawEnv fname="Times", fsize=22, fstyle= 1,textrgb= (0,0,0)
922                        DrawText 85,32,"*"
923                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,0)                                 
924                        DrawText 105,30,")"
925                        break
926                case 3:
927                        //Divide
928                        DrawAction/L=progFront delete
929                        DrawRect 0,0,120,40
930                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,65535,0)
931                        DrawText 10,32,"I"
932                        SetDrawEnv fname="Times", fsize=22, fstyle= 1,textrgb= (0,0,0)
933                        DrawText 20,30,"="
934                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (65535,0,0)
935                        DrawText 35,32,"I"
936                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,65535)
937                        DrawText 95,32,"I"
938                        SetDrawEnv fname="Symbol", fsize=22, fstyle= 1,textrgb= (0,0,0)
939                        DrawText 52,32,"/"
940                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,0)
941                        DrawText 66,30,"("
942                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,0)
943                        DrawText 75,30,"f"
944                        SetDrawEnv fname="Times", fsize=22, fstyle= 1,textrgb= (0,0,0)
945                        DrawText 85,32,"*"
946                        SetDrawEnv fname="Times", fsize=22, fstyle= 2,textrgb= (0,0,0)                                 
947                        DrawText 105,30,")"
948                        break   
949        endswitch
950
951        SetActiveSubWindow DataArithmeticPanel
952       
953        return 0
954End
955
956//Must follow naming scheme to match buttons to popups
957//"Name_button" goes with "Name_popup"
958Function DA_LoadDataSetProc(ba) : ButtonControl
959        STRUCT WMButtonAction &ba
960       
961        switch( ba.eventCode )
962                case 2: // mouse up
963                        // click code here
964                        String cmd = "A_LoadOneDDataWithName(\"\","+num2str(0)+")"
965                        Execute cmd
966               
967                        SVAR gLastFileName = root:packages:NIST:gLastFileName
968
969                        String windowName = ba.win
970                        String popupName = StringFromList(0,ba.ctrlName,"_")+"_popup"
971                       
972                        ControlUpdate/W=$(windowName) $(popupName)
973                        //instead of a simple controlUpdate, better to pop the menu to make sure that the other menus follow
974                        // convoluted method to find the right item and pop the menu.
975
976                        String list,folderStr
977                        Variable num
978                        folderStr = CleanupName(gLastFileName,0)
979                        list = DM_DataSetPopupList()
980                        num=WhichListItem(folderStr,list,";",0,0)
981                        if(num != -1)
982                                PopupMenu $(popupName),mode=num+1,win=$(windowName)
983                                ControlUpdate/W=$(windowName) $(popupName)
984               
985                        endif
986                        //fake call to popup
987                        STRUCT WMPopupAction pa
988                        pa.win = ba.win
989                        pa.ctrlName = "DS1_popup"
990                        pa.eventCode = 2
991                        DA_PopupProc(pa)
992                        break
993        endswitch
994       
995        return 0
996End
997
998Function DA_PopupProc(pa) : PopupMenuControl
999        STRUCT WMPopupAction &pa
1000       
1001        String resultName
1002       
1003        switch( pa.eventCode)
1004                case 2:
1005                        //print "Called by "+pa.ctrlname+" with value "+pa.popStr
1006                        ControlInfo/W=$(pa.win) $(pa.ctrlName)
1007                        String popStr = S_Value
1008                        if (stringmatch(pa.ctrlname,"*DS1*") == 1)
1009                                resultName = stringfromlist(0,popStr,"_")+"_mod"
1010                               
1011                                SetVariable DAResultName_sv win=$(pa.win), value=_STR:resultName
1012                        endif
1013                break
1014        endswitch
1015       
1016
1017End
1018
1019// function to control the drawing of buttons in the TabControl on the main panel
1020// Naming scheme for the buttons MUST be strictly adhered to... else buttons will
1021// appear in odd places...
1022// all buttons are named MainButton_NA where N is the tab number and A is the letter denoting
1023// the button's position on that particular tab.
1024// in this way, buttons will always be drawn correctly..
1025//
1026Function DATabsProc(tca) : TabControl
1027        STRUCT WMTabControlAction &tca
1028               
1029        switch (tca.eventCode)
1030                case 2:
1031                //      Print "name,number",name,tab
1032                        String ctrlList = ControlNameList("",";"),item="",nameStr=""
1033                        Variable num = ItemsinList(ctrlList,";"),ii,onTab
1034                        for(ii=0;ii<num;ii+=1)
1035                                //items all start w/"DSTabItem_"
1036                                item=StringFromList(ii, ctrlList ,";")
1037                                nameStr=item[0,9]
1038                                if(cmpstr(nameStr,"DATabItem_")==0)
1039                                        onTab = str2num(item[10])
1040                                        ControlInfo $item
1041                                        switch (V_flag)
1042                                                case 1:
1043                                                        Button $item,disable=(tca.tab!=onTab)
1044                                                        break
1045                                                case 2:
1046                                                        CheckBox $item,disable=(tca.tab!=onTab)
1047                                                        break
1048                                                case 3:
1049                                                        PopUpMenu       $item,disable=(tca.tab!=onTab)
1050                                                        break
1051                                                case 5:
1052                                                        SetVariable     $item,disable=(tca.tab!=onTab)
1053                                                        break
1054                                        endswitch
1055                                endif
1056                        endfor
1057                       
1058                        arithDisplayProc(tca.tab)
1059                        break
1060        endswitch       
1061End
1062
1063
1064Function DACalculateProc(ba) : ButtonControl
1065        STRUCT WMButtonAction &ba
1066       
1067        String DS1,DS2,Resultname
1068       
1069        switch(ba.eventCode)
1070        case 2:
1071                //Which tab?
1072                ControlInfo/W=$(ba.win) DATabs
1073                Variable tabNum = V_value
1074                //Print "Tab number "+num2str(tabNum)
1075
1076                ControlInfo/W=$(ba.win) DS1_popup
1077                DS1 = S_Value
1078                ControlInfo/W=$(ba.win) DANoDS2_cb
1079                if (V_Value == 0)
1080                        ControlInfo/W=$(ba.win) DS2_popup
1081                        DS2 = S_Value
1082                else
1083                        DS2 = "NullSolvent"
1084                endif
1085                ControlInfo/W=$(ba.win) DAResultName_sv
1086                Resultname = CleanupName(S_Value, 0 )           //clean up any bad characters, and put the cleaned string back
1087                SetVariable DAResultName_sv,value=_STR:ResultName
1088               
1089                ControlInfo/W=$(ba.win) DAScale_sv
1090                Variable Scalefactor = V_Value
1091
1092                switch(tabNum)
1093                        case 0:
1094                                //do subtraction
1095                                //print "Subtraction of "+DS2+" from "+DS1+" with sf "+num2str(scalefactor)+ " into "+Resultname
1096                                SubtractDataSets(DS1,DS2,Scalefactor,Resultname)
1097                                break
1098                        case 1:
1099                                //do addition
1100                                AddDataSets(DS1,DS2,Scalefactor,Resultname)
1101                                break
1102                        case 2:
1103                                //do multiplication
1104                                MultiplyDataSets(DS1,DS2,Scalefactor,Resultname)
1105                                break
1106                        case 3:
1107                                //do division
1108                                DivideDataSets(DS1,DS2,Scalefactor,Resultname)
1109                                break
1110                endswitch
1111               
1112                //Sort out plot
1113                //Fake button press to DAPlotButtonProc
1114                STRUCT WMButtonAction ba2
1115                ba2.win = ba.win
1116                ba2.ctrlName = "DAPlot_button"
1117                ba2.eventCode = 2
1118               
1119                // I've commented this out - the cursors get reset to the ends since this removes all sets from the graph, and
1120                // then replots them. What is the real purpose of this call? To clear the old result off before adding the
1121                // new one?
1122//              DAPlotButtonProc(ba2)
1123                ba2.userData = ResultName
1124                DAPlotRemoveResult(ba2)
1125               
1126               
1127                AddDAPlot(3)
1128                DoWindow/F DataArithmeticPanel
1129               
1130                //Enable save button now that we have a result to save
1131                Button DASave_Button win=$(ba.win),disable=0
1132               
1133//              SetActiveSubWindow DAPlotPanel
1134        endswitch
1135       
1136End
1137
1138// remove what is not the
1139//
1140Function DAPlotRemoveResult(ba) : ButtonControl
1141        STRUCT WMButtonAction &ba
1142       
1143        String win = ba.win
1144        String ResultName = ba.userData
1145        String item="",traceList=""
1146        Variable ii=0,num
1147
1148        switch (ba.eventCode)
1149                case 2:         //mouse up
1150                        //data set 1
1151                        ControlInfo/W=$(win) DS1_popup
1152                        String DS1 = S_Value
1153                       
1154                        //Get folder for DS2
1155                        ControlInfo/W=$(win) DS2_popup
1156                        String DS2 = S_Value
1157                       
1158                        // state of the checkbox
1159                        ControlInfo/W=$(win) DANoDS2_cb
1160                        if(V_Value)
1161                                DS2 = "NullSolvent"
1162                        endif
1163                       
1164                        DoWindow DAPlotPanel
1165                        if (V_Flag == 0)
1166                                MakeDAPlotPanel()
1167                        else
1168                                DoWindow/HIDE=0/F DAPlotPanel
1169                                traceList = TraceNameList("DAPlotPanel#DAPlot",";",1)
1170                                num=ItemsInList(traceList)
1171                                ii=0
1172                                do
1173                                        item = StringFromList(ii,traceList,";")
1174                                        if (stringmatch(item,ResultName+"*")==1)                //it it's the specific trace I've asked to remove
1175                                                RemoveFromGraph/W=DAPlotPanel#DAPlot $item
1176                                        elseif (stringmatch(item,DS1+"*")==0 && stringmatch(item,DS2+"*")==0)           //if it's not set1 & not set2
1177                                                RemoveFromGraph/W=DAPlotPanel#DAPlot $item
1178                                        endif
1179                                       
1180                                        ii+=1
1181                                while(ii<num)                           
1182                        endif
1183                       
1184                        break
1185        endswitch
1186
1187        return 0
1188End
1189
1190
1191Function DAPlotButtonProc(ba) : ButtonControl
1192        STRUCT WMButtonAction &ba
1193       
1194        String win = ba.win
1195
1196        switch (ba.eventCode)
1197                case 2:
1198                        //mouse up
1199                        //Get folder for DS1
1200                        DoWindow DAPlotPanel
1201                        if (V_Flag == 0)
1202                                MakeDAPlotPanel()
1203                        else
1204                                DoWindow/HIDE=0/F DAPlotPanel
1205                                do
1206                                        String tracename = StringFromList(0,TraceNameList("DAPlotPanel#DAPlot",";",1),";")
1207                                        if (cmpstr(tracename,"")==0)
1208                                                break
1209                                        else
1210                                                RemoveFromGraph/W=DAPlotPanel#DAPlot $tracename
1211                                        endif                   
1212                                while(1)                               
1213                        endif
1214                       
1215                        ControlInfo/W=$(win) DS1_popup
1216                        String DS1 = S_Value
1217                        if (cmpstr(DS1,"") != 0 )
1218                                AddDAPlot(1)
1219                        endif
1220                        //Get folder for DS2
1221                        ControlInfo/W=$(win) DS2_popup
1222                        String DS2 = S_Value
1223                        if (cmpstr(DS2,"") != 0)
1224                                AddDAPlot(2)
1225                        endif
1226                        break
1227        endswitch
1228
1229        return 0
1230End
1231
1232Function DADoneButtonProc(ba) : ButtonControl
1233        STRUCT WMButtonAction &ba
1234       
1235        String win = ba.win
1236
1237        switch (ba.eventCode)
1238                case 2:
1239                        DoWindow/K DAPlotPanel
1240                        DoWindow/K DataArithmeticPanel
1241                        break
1242        endswitch
1243
1244        return 0
1245End
1246
1247Function DAHelpButtonProc(ba) : ButtonControl
1248        STRUCT WMButtonAction &ba
1249       
1250        String win = ba.win
1251
1252        switch (ba.eventCode)
1253                case 2:
1254                        // click code here
1255                        DisplayHelpTopic/Z/K=1 "Data Set Arithmetic"
1256                        if(V_flag !=0)
1257                                DoAlert 0,"The Data Set Arithmetic Help file could not be found"
1258                        endif
1259                        break
1260        endswitch
1261
1262        return 0
1263End
1264
1265Function DALogLinIProc(cba) : CheckBoxControl
1266        STRUCT WMCheckBoxAction &cba
1267
1268        switch(cba.eventcode)
1269                case 2:
1270                       
1271                        ModifyGraph/W=DAPlotPanel#DAPlot log(left)=cba.checked
1272                        ModifyGraph/W=DAPlotPanel#DAPlot log(bottom)=cba.checked
1273                        ModifyGraph/W=DAPlotPanel#DAPlot zero(left)=0
1274                        SetAxis/A/W=DAPlotPanel#DAPlot
1275                       
1276                        if(cba.checked)
1277                                Checkbox DAPlot_lin_cb,value=0          //uncheck lin
1278                        endif
1279        endswitch
1280
1281
1282End
1283
1284Function DAHighQLinProc(cba) : CheckBoxControl
1285        STRUCT WMCheckBoxAction &cba
1286
1287        switch(cba.eventcode)
1288                case 2:
1289                        if(cba.checked)
1290                                ModifyGraph/W=DAPlotPanel#DAPlot log=0,zero(left)=1
1291                                SetAxis/W=DAPlotPanel#DAPlot left -0.1,0.1
1292                                SetAxis/W=DAPlotPanel#DAPlot bottom 0.1,*
1293                                SetAxis/W=DAPlotPanel#DAPlot left -0.02,0.02
1294                               
1295                                Checkbox DAPlot_log_cb,value=0          //uncheck the log
1296                        endif
1297        endswitch
1298
1299
1300End
1301
1302
1303Function DACursorButtonProc(ba) : ButtonControl
1304        STRUCT WMButtonAction &ba
1305
1306        String DS1,DS2
1307       
1308        switch(ba.eventCode)
1309                case 2:
1310               
1311                        ControlInfo/W=$(ba.win) DS1_popup
1312                        DS1 = S_Value
1313                        ControlInfo/W=$(ba.win) DANoDS2_cb
1314                        Variable NoDS2 = V_Value
1315                        if (NoDS2 == 0)
1316                                ControlInfo/W=$(ba.win) DS2_popup
1317                                DS2 = S_Value
1318                        else
1319                                DS2 = "NullSolvent"
1320                        endif
1321               
1322                        //AJJ Nov 2009 - UGLY - Will have to revisit this when we deal with hierarchical folders
1323                        Wave set1_i = $("root:"+DS1+":"+DS1+"_i")
1324                        Wave set1_q = $("root:"+DS1+":"+DS1+"_q")
1325                        Wave set2_i = $("root:"+DS2+":"+DS2+"_i")
1326                        Wave set2_q = $("root:"+DS2+":"+DS2+"_q")
1327                        Duplicate/O set1_i tmp_i
1328                        Duplicate/O set1_q tmp_q
1329                        tmp_i = set1_i / interp(set1_q[p],set2_q,set2_i)       
1330                       
1331                        //Get cursors
1332                        Variable q1,q2
1333                       
1334                        DoWindow/F DAPlotPanel
1335                        SetActiveSubWindow DAPlotPanel#DAPlot
1336                       
1337                        q1 = CsrXWaveRef(A)[pcsr(A)]
1338                        q2 = CsrXWaveRef(B)[pcsr(B)]
1339
1340                        //Update value display
1341                        ValDisplay DARangeStar_vd,value=_NUM:q1, win=$(ba.win)
1342                        ValDisplay DARangeEnd_vd,value=_NUM:q2, win=$(ba.win)
1343                       
1344                        //Calculate scalefactor
1345                       
1346                        if (NoDS2 == 1)
1347                                Wave avgWave = set1_i
1348                        else
1349                                Wave avgWave = tmp_i
1350                        endif
1351
1352                        Variable p1 = BinarySearch(tmp_q,q1)                   
1353                        Variable p2 = BinarySearch(tmp_q,q2)                   
1354
1355                        //print avgWave
1356
1357                        WaveStats/Q/R=[p1,p2] avgWave
1358                        //print V_avg
1359                        //Update sv control
1360                        SetVariable DAScale_sv, value=_NUM:V_avg, win=$(ba.win)
1361                       
1362                        KillWaves/Z tmp_i,tmp_q
1363                        DoWindow/F DataArithmeticPanel
1364        endswitch
1365
1366End
1367
1368
1369Function DANoDS2Proc(cba) : CheckBoxControl
1370        STRUCT WMCheckBoxAction &cba
1371       
1372       
1373        switch(cba.eventCode)
1374                case 2:
1375                        if (cba.checked == 1)
1376                                //Disable DS2 popup etc
1377                                PopupMenu DS2_popup win=$(cba.win), disable=2
1378                                Button DS2_button win=$(cba.win), disable=2
1379                        else
1380                                //Enable DS2 popup etc
1381                                PopupMenu DS2_popup win=$(cba.win), disable=0
1382                                Button DS2_button win=$(cba.win), disable=0
1383                        endif
1384                        //Sort out plot
1385                        //Fake button press to DAPlotButtonProc
1386                        STRUCT WMButtonAction ba2
1387                        ba2.win = cba.win
1388                        ba2.ctrlName = "DAPlot_button"
1389                        ba2.eventCode = 2
1390                        DAPlotButtonProc(ba2)
1391                        SetActiveSubWindow DAPlotPanel
1392                        DoWindow/F DataArithmeticPanel
1393        endswitch
1394
1395End
1396
1397Function DASaveProc(ba) : ButtonControl
1398        STRUCT WMButtonAction &ba
1399       
1400        switch(ba.eventCode)
1401                case 2:
1402                        ControlInfo/W=$(ba.win) DAResultName_sv
1403                        SaveDataSetToFile(S_Value)
1404                        break
1405        endswitch
1406
1407
1408End
1409
1410/////////////////////// Common Panel Functions ///////////////////////////////////////
1411
1412
1413// is there a simpler way to do this? I don't think so.
1414Function/S DM_DataSetPopupList()
1415
1416        String str=GetAList(4)
1417
1418        if(strlen(str)==0)
1419                str = "No data loaded"
1420        endif
1421        str = SortList(str)
1422       
1423        return(str)
1424End
1425
1426
1427Function DANameSetvarproc(sva) : SetVariableControl
1428        STRUCT WMSetVariableAction &sva
1429               
1430        switch( sva.eventCode )
1431                case 1: // mouse up
1432                case 2: // Enter key
1433                case 3: // Live update
1434                                String sv = sva.sval
1435                                if( strlen(sv) > 25 )
1436                                        sv= sv[0,24]
1437                                        SetVariable  $(sva.ctrlName),win=$(sva.win),value=_STR:sv
1438                                        ControlUpdate /W=$(sva.win) $(sva.ctrlName)
1439                                        Beep
1440                                endif
1441                                Button DASave_Button win=$(sva.win), disable=2
1442                                break
1443                endswitch
1444        return 0
1445End
1446
1447
1448////////////////////// Functions to do manipulations ///////////////////////////////////
1449
1450Function RenameDataSet(dataSetFolder, newName)
1451        String dataSetFolder
1452        String newName
1453       
1454        String dataSetFolderParent,basestr,objName
1455        Variable index = 0
1456       
1457        //Abuse ParseFilePath to get path without folder name
1458        dataSetFolderParent = ParseFilePath(1,dataSetFolder,":",1,0)
1459        //Abuse ParseFilePath to get basestr
1460        basestr = ParseFilePath(0,dataSetFolder,":",1,0)
1461
1462//      try
1463                RenameDataFolder $(dataSetFolder) $(newName)//; AbortOnRTE
1464       
1465
1466                SetDataFolder $(dataSetFolderParent+newName)//; AbortOnRTE
1467                do
1468                        objName = GetIndexedObjName("",1,index)
1469                        if (strlen(objName) == 0)
1470                                break
1471                        endif
1472                        Rename $(objName) $(ReplaceString(basestr,objName,newName))
1473                        index+=1
1474                while(1)
1475                SetDataFolder root:
1476//      catch
1477//              Print "Aborted: " + num2str(V_AbortCode)
1478//              SetDataFolder root:
1479//      endtry
1480End
1481
1482
1483Function DuplicateDataSet(dataSetFolder, newName, forceoverwrite)
1484        String dataSetFolder
1485        String newName
1486        Variable forceoverwrite
1487
1488        String dataSetFolderParent,basestr,objName
1489        Variable index = 0
1490
1491        //Abuse ParseFilePath to get path without folder name
1492        dataSetFolderParent = ParseFilePath(1,dataSetFolder,":",1,0)
1493        //Abuse ParseFilePath to get basestr
1494        basestr = ParseFilePath(0,dataSetFolder,":",1,0)
1495       
1496        print "Duplicating "+dataSetFolder+" as "+newName
1497       
1498        SetDataFolder $(dataSetFolderParent)
1499       
1500        if (!DataFolderExists(newName))
1501                NewDataFolder $(newName)
1502        else
1503                if (!forceoverwrite)
1504                        DoAlert 1, "A dataset with the name "+newName+" already exists. Overwrite?"
1505                        if (V_flag == 2)
1506                                return 1
1507                        endif
1508                endif
1509        endif   
1510
1511        //If we are here, the folder (now) exists and the user has agreed to overwrite
1512        //either in the function call or from the alert.
1513       
1514        // here, GetIndexedObjectName copies all of the waves
1515        index = 0
1516        do
1517                objName = GetIndexedObjName(basestr,1,index)
1518                if (strlen(objName) == 0)
1519                        break
1520                endif
1521                objname = ":"+basestr+":"+objname
1522                        Duplicate/O $(objName) $(ReplaceString(basestr,objName,newName))
1523                index+=1
1524        while(1)
1525
1526// -- for USANS data, we need the slit height. copy all of the "USANS_*" variables
1527// may need to augment this for other situations
1528        index = 0
1529        do
1530                objName = GetIndexedObjName(basestr,2,index)
1531                if (strlen(objName) == 0)
1532                        break
1533                endif
1534                if(stringmatch(objName,"USANS*") == 1)
1535                        objname = ":"+basestr+":"+objname
1536                        NVAR tmp = $objName
1537                        Variable/G $(ReplaceString(basestr,objName,newName))= tmp
1538                endif
1539                index+=1
1540        while(1)
1541       
1542
1543        SetDataFolder root:
1544        return 0
1545End
1546
1547
1548// Subtract Set2 From Set1
1549// Use Result_I = Set1_I - f*Set2_I
1550Function SubtractDataSets(set1Name,set2Name,set2Scale,resultName)
1551        String set1Name
1552        String set2Name
1553        Variable set2Scale
1554        String resultName
1555
1556        String set1Path = "root:"+set1Name+":"
1557        String set2Path = "root:"+set2Name+":"
1558        String resultPath = "root:"+resultName+":"
1559       
1560        SetDataFolder root:
1561        //Create folder for result
1562        //UnloadDataSet(resultName)
1563        //Make the folder
1564        if (DuplicateDataSet(set1Path,resultName,0))
1565                return 1
1566        else
1567        //Do subtraction of I waves - including interpolation if necessary.
1568        Wave result_i = $(resultPath+resultName+"_i")
1569        Wave result_s = $(resultPath+resultName+"_s")
1570        Wave set1_i = $(set1Path+set1Name+"_i")
1571        Wave set1_q = $(set1Path+set1Name+"_q")
1572        Wave set1_s = $(set1Path+set1Name+"_s")
1573        Wave set2_i = $(set2Path+set2Name+"_i")
1574        Wave set2_q = $(set2Path+set2Name+"_q")
1575        Wave set2_s = $(set2Path+set2Name+"_s")
1576       
1577        result_i = set1_i - (set2Scale*interp(set1_q[p],set2_q,set2_i))
1578        result_s = sqrt(set1_s^2 + (set2Scale*interp(set1_q[p],set2_q,set2_s))^2 )
1579        //Calculate result error wave - can we produce corrected Q error?
1580       
1581        //Generate history string to record what was done?
1582        return 0
1583        endif
1584End
1585
1586// Add Set2 to Set1
1587// Use Result_I = Set1_I + f*Set2_I
1588Function AddDataSets(set1Name,set2Name,set2Scale,resultName)
1589        String set1Name
1590        String set2Name
1591        Variable set2Scale
1592        String resultName
1593
1594        String set1Path = "root:"+set1Name+":"
1595        String set2Path = "root:"+set2Name+":"
1596        String resultPath = "root:"+resultName+":"
1597       
1598        SetDataFolder root:
1599        //Create folder for result
1600        if(DuplicateDataSet(set1Path,resultName,0))
1601                //User said no overwrite
1602                return 1
1603        else
1604        //Do addition of I waves - including interpolation if necessary.
1605        Wave result_i = $(resultPath+resultName+"_i")
1606        Wave result_s = $(resultPath+resultName+"_s")
1607        Wave set1_i = $(set1Path+set1Name+"_i")
1608        Wave set1_q = $(set1Path+set1Name+"_q")
1609        Wave set1_s = $(set1Path+set1Name+"_s")
1610        Wave set2_i = $(set2Path+set2Name+"_i")
1611        Wave set2_q = $(set2Path+set2Name+"_q")
1612        Wave set2_s = $(set2Path+set2Name+"_s")
1613       
1614        result_i =  set1_i + set2Scale*interp(set1_q[p],set2_q,set2_i) 
1615        //Calculate result error wave (note that this is identical to subtraction)
1616        result_s = sqrt(set1_s^2 + (set2Scale*interp(set1_q[p],set2_q,set2_s))^2 )
1617
1618        //  - can we produce corrected Q error?
1619        //Generate history string to record what was done?
1620        return 0
1621        endif
1622End
1623
1624// Multiply Set1 by Set2
1625// Use Result_I  = Set1_I * (f*Set2_I)
1626Function MultiplyDataSets(set1Name, set2Name, set2Scale, resultName)
1627        String set1Name
1628        String set2Name
1629        Variable set2Scale
1630        String resultName
1631
1632        String set1Path = "root:"+set1Name+":"
1633        String set2Path = "root:"+set2Name+":"
1634        String resultPath = "root:"+resultName+":"
1635       
1636        SetDataFolder root:
1637        //Create folder for result
1638        //Make the folder
1639        if(DuplicateDataSet(set1Path,resultName,0))
1640                //User said no overwrite
1641                return 1
1642        else
1643        //Do multiplcation of I waves - including interpolation if necessary.
1644        Wave result_i = $(resultPath+resultName+"_i")
1645        Wave result_s = $(resultPath+resultName+"_s")
1646        Wave set1_i = $(set1Path+set1Name+"_i")
1647        Wave set1_q = $(set1Path+set1Name+"_q")
1648        Wave set1_s = $(set1Path+set1Name+"_s")
1649        Wave set2_i = $(set2Path+set2Name+"_i")
1650        Wave set2_q = $(set2Path+set2Name+"_q")
1651        Wave set2_s = $(set2Path+set2Name+"_s")
1652       
1653        result_i =  set1_i*set2Scale*interp(set1_q[p],set2_q,set2_i)
1654        //Calculate result error wave
1655        // sum each of the relative errors, interpolating set 2 intensity and error as needed
1656        // then sqrt
1657        result_s = (set2Scale*interp(set1_q[p],set2_q,set2_i)*set1_s)^2
1658        result_s += (set2Scale*set1_i*interp(set1_q[p],set2_q,set2_s))^2
1659        result_s = sqrt(result_s)
1660
1661        // - can we produce corrected Q error?
1662
1663        //Generate history string to record what was done?
1664        return 0
1665        endif
1666End
1667
1668// Divide Set1 by Set2
1669// Use Result_I  = Set1_I / (f*Set2_I)
1670Function DivideDataSets(set1Name, set2Name, set2Scale, resultName)
1671        String set1Name
1672        String set2Name
1673        Variable set2Scale
1674        String resultName
1675
1676        String set1Path = "root:"+set1Name+":"
1677        String set2Path = "root:"+set2Name+":"
1678        String resultPath = "root:"+resultName+":"
1679       
1680        SetDataFolder root:
1681        //Create folder for result
1682        //Make the folder
1683        if(DuplicateDataSet(set1Path,resultName,0))
1684                //User said no overwrite
1685                return 1
1686        else
1687        //Do division of I waves - including interpolation if necessary.
1688        Wave result_i = $(resultPath+resultName+"_i")
1689        Wave result_s = $(resultPath+resultName+"_s")
1690        Wave set1_i = $(set1Path+set1Name+"_i")
1691        Wave set1_q = $(set1Path+set1Name+"_q")
1692        Wave set1_s = $(set1Path+set1Name+"_s")
1693        Wave set2_i = $(set2Path+set2Name+"_i")
1694        Wave set2_q = $(set2Path+set2Name+"_q")
1695        Wave set2_s = $(set2Path+set2Name+"_s")
1696       
1697        result_i =  set1_i/(set2Scale*interp(set1_q[p],set2_q,set2_i)   )
1698        //Calculate result error wave
1699        // sum each of the relative errors, interpolating set 2 intensity and error as needed
1700        // then sqrt
1701        result_s = (set1_s/set2Scale/interp(set1_q[p],set2_q,set2_i))^2
1702        result_s += (interp(set1_q[p],set2_q,set2_s)*set1_i/set2Scale/interp(set1_q[p],set2_q,set2_i)^2)^2
1703        result_s = sqrt(result_s)
1704
1705        // - can we produce corrected Q error?
1706       
1707        //Generate history string to record what was done?
1708        return 0
1709        endif
1710End
1711
1712
1713///////////////////////////Other Utility functions ////////////////////////////
1714
1715Function SaveDataSetToFile(folderName)
1716        String folderName
1717
1718        String protoStr = ""
1719        //Check for history string in folder
1720        //If it doesn't exist then
1721
1722        //Do saving of data file.
1723       
1724        NVAR gXML_Write = root:Packages:NIST:gXML_Write
1725
1726        if (gXML_Write == 1)
1727                ReWrite1DXMLData(folderName)
1728        else
1729                fReWrite1DData(folderName,"tab","CRLF")
1730        endif
1731
1732        //Include history string to record what was done?
1733
1734End
1735
1736
1737
1738// still need to get the header information, and possibly the SASprocessnote from the XML load into the 6-column header
1739//
1740// start by looking in:
1741//      String xmlReaderFolder = "root:Packages:CS_XMLreader:"
1742// for Title and Title_folder strings -> then the metadata (but the processnote is still not there
1743//
1744// may need to get it directly using the filename
1745Function  convertNISTXMLtoNIST6Col(fname)
1746        String fname
1747
1748        String list, item,path
1749        Variable num,ii
1750       
1751        //load the XML
1752       
1753        LoadNISTXMLData(fname,"",0,0)           //no plot, no force overwrite
1754//      Execute "A_LoadOneDDataWithName(\""+fname+"\",0)"               //won't plot
1755
1756        // then rewrite what is in the data folder that was just loaded
1757        String basestr = ParseFilePath(0, fname, ":", 1, 0)
1758        baseStr = CleanupName(baseStr,0)
1759        print fname
1760        print basestr
1761
1762        fReWrite1DData_noPrompt(baseStr,"tab","CR")
1763
1764        return(0)
1765End
1766
1767
1768///////// SRK - VERY SIMPLE batch converter
1769// no header information is preserved
1770// file names are partially preserved
1771//
1772
1773/// to use this:
1774// -open the Plot Manager and set the path
1775// -run this function
1776//
1777// it doesn't matter if the XML ouput flag is set - this overrides.
1778Function batchXML26ColConvert()
1779
1780        String list, item,path,fname
1781        Variable num,ii
1782       
1783        PathInfo CatPathName
1784        path = S_Path
1785
1786        list = A_ReducedDataFileList("")
1787        num = itemsInList(list)
1788        Print num
1789        for(ii=0;ii<num;ii+=1)
1790                item = StringFromList(ii, list ,";")
1791                fname=path + item
1792                Execute "A_LoadOneDDataWithName(\""+fname+"\",0)"               //won't plot
1793//              easier to load all, then write out, since the name will be changed
1794        endfor
1795       
1796       
1797        list = DM_DataSetPopupList()
1798
1799        num = itemsInList(list)
1800        Print num
1801        for(ii=0;ii<num;ii+=1)
1802                item = StringFromList(ii, list ,";")
1803                fReWrite1DData_noPrompt(item,"tab","CR")
1804        endfor
1805       
1806End
1807
1808///////// SRK - VERY SIMPLE batch converter
1809// NO header information is preserved
1810// file names are partially preserved
1811//
1812
1813/// to use this:
1814// -open the Plot Manager and set the path
1815// -run this function
1816//
1817// it doesn't matter if the XML ouput flag is set - this overrides.
1818//
1819// The GRASP output data is 5-column Q-I-errI-sigQ-nCells
1820// which gets read in as q-i-s-ism-fit_ism (as if it was some wierd USANS data format)
1821// -- so fake the output...
1822//
1823Function batchGrasp26ColConvert()
1824
1825        String list, item,path,fname
1826        Variable num,ii,npt
1827       
1828        PathInfo CatPathName
1829        path = S_Path
1830
1831        list = A_ReducedDataFileList("")
1832        num = itemsInList(list)
1833        Print num
1834        for(ii=0;ii<num;ii+=1)
1835                item = StringFromList(ii, list ,";")
1836                fname=path + item
1837                Execute "A_LoadOneDDataWithName(\""+fname+"\",0)"               //won't plot
1838//              easier to load all, then write out, since the name will be changed
1839        endfor
1840       
1841       
1842        list = DM_DataSetPopupList()
1843
1844        num = itemsInList(list)
1845       
1846        Print num
1847        for(ii=0;ii<num;ii+=1)
1848                item = StringFromList(ii, list ,";")
1849               
1850                // fake the 6-column NIST data structure
1851                WAVE qw = $("root:"+item+":"+item+"_q")
1852                npt = numpnts(qw)
1853                Make/O/D/N=(npt,4) $("root:"+item+":"+item+"_res")
1854                WAVE res = $("root:"+item+":"+item+"_res")
1855                WAVE sigQ = $("root:"+item+":"+item+"_ism")
1856                res[][0] = sigQ[p]      // sigQ
1857                res[][1] = qw[p]                // qBar ~ q
1858                res[][2] = 1            //shadow
1859                res[][3] = qw[p]                // q
1860               
1861               
1862                fReWrite1DData_noPrompt(item,"tab","CR")
1863        endfor
1864       
1865End
1866
1867// quick version (copied from fReWrite1DData() that NEVER asks for a new fileName
1868// - and right now, always expect 6-column data, either SANS or USANS (re-writes -dQv)
1869// - AJJ Nov 2009 : better make sure we always fake 6 columns on reading then....
1870Function fReWrite1DData_noPrompt(folderStr,delim,term)
1871        String folderStr,delim,term
1872       
1873        String formatStr="",fullpath=""
1874        Variable refnum,dialog=1
1875       
1876        String dataSetFolderParent,basestr
1877       
1878        //setup delimeter and terminator choices
1879        If(cmpstr(delim,"tab")==0)
1880                //tab-delimeted
1881                formatStr="%15.8g\t%15.8g\t%15.8g\t%15.8g\t%15.8g\t%15.8g"
1882        else
1883                //use 3 spaces
1884                formatStr="%15.8g   %15.8g   %15.8g   %15.8g   %15.8g   %15.8g"
1885        Endif
1886        If(cmpstr(term,"CR")==0)
1887                formatStr += "\r"
1888        Endif
1889        If(cmpstr(term,"LF")==0)
1890                formatStr += "\n"
1891        Endif
1892        If(cmpstr(term,"CRLF")==0)
1893                formatStr += "\r\n"
1894        Endif
1895       
1896        //Abuse ParseFilePath to get path without folder name
1897        dataSetFolderParent = ParseFilePath(1,folderStr,":",1,0)
1898        //Abuse ParseFilePath to get basestr
1899        basestr = ParseFilePath(0,folderStr,":",1,0)
1900       
1901        //make sure the waves exist
1902        SetDataFolder $(dataSetFolderParent+basestr)
1903        WAVE/Z qw = $(baseStr+"_q")
1904        WAVE/Z iw = $(baseStr+"_i")
1905        WAVE/Z sw = $(baseStr+"_s")
1906        WAVE/Z resw = $(baseStr+"_res")
1907       
1908        if(WaveExists(qw) == 0)
1909                Abort "q is missing"
1910        endif
1911        if(WaveExists(iw) == 0)
1912                Abort "i is missing"
1913        endif
1914        if(WaveExists(sw) == 0)
1915                Abort "s is missing"
1916        endif
1917        if(WaveExists(resw) == 0)
1918                Abort "Resolution information is missing."
1919        endif
1920       
1921        Duplicate/O qw qbar,sigQ,fs
1922        if(dimsize(resW,1) > 4)
1923                //it's USANS put -dQv back in the last 3 columns
1924                NVAR/Z dQv = USANS_dQv
1925                if(NVAR_Exists(dQv) == 0)
1926                        Abort "It's USANS data, and I don't know what the slit height is."
1927                endif
1928                sigQ = -dQv
1929                qbar = -dQv
1930                fs = -dQv
1931        else
1932                //it's SANS
1933                sigQ = resw[p][0]
1934                qbar = resw[p][1]
1935                fs = resw[p][2]
1936        endif
1937       
1938        dialog=0
1939        if(dialog)
1940                PathInfo/S catPathName
1941//              fullPath = DoSaveFileDialog("Save data as",fname=baseStr+".txt")
1942                fullPath = DoSaveFileDialog("Save data as",fname=baseStr[0,strlen(BaseStr)-1])
1943                Print fullPath
1944                If(cmpstr(fullPath,"")==0)
1945                        //user cancel, don't write out a file
1946                        Close/A
1947                        Abort "no data file was written"
1948                Endif
1949                //Print "dialog fullpath = ",fullpath
1950        Endif
1951        PathInfo catPathName
1952        fullPath = S_Path + baseStr[0,strlen(BaseStr)-1]
1953
1954        Open refnum as fullpath
1955       
1956        fprintf refnum,"Modified data written from folder %s on %s\r\n",baseStr,(date()+" "+time())
1957        wfprintf refnum,formatStr,qw,iw,sw,sigQ,qbar,fs
1958        Close refnum
1959       
1960        KillWaves/Z sigQ,qbar,fs
1961       
1962        SetDataFolder root:
1963        return(0)
1964End
1965
1966///////////end SRK
1967
1968
1969
1970///// SRK
1971///// Rebinning
1972
1973
1974// main entry procedure for subtraction panel
1975// re-initializes necessary folders and waves
1976Proc OpenRebin()
1977        DoWindow/F Rebin_Panel
1978        if(V_Flag==0)
1979                InitRebin()
1980                Rebin()         //the panel
1981//              Plot_Sub1D()            //the graph
1982        endif
1983       
1984End
1985
1986Function InitRebin()
1987
1988        NewDataFolder/O/S root:Packages:NIST:Rebin
1989//      Variable/G gPtsBeg1=0
1990//      Variable/G gPtsEnd1=0
1991        Variable/G binning=2
1992
1993        SetDataFolder root:
1994End
1995
1996Proc Rebin()
1997
1998        PauseUpdate; Silent 1           // building window...
1999        NewPanel /W=(658,347,920,562)/K=1
2000        DoWindow/C Rebin_Panel
2001        ModifyPanel cbRGB=(65535,48662,45086),fixedSize=1
2002        Button button0,pos={161,178},size={50,20},proc=Rebin_Done,title="Done"
2003        PopupMenu popup0,pos={11,50},size={211,20},title="Data in Memory"
2004        PopupMenu popup0,mode=1,value= #"A_OneDDataInMemory()"
2005        Button button1,pos={60,14},size={150,20},proc=Rebin_Load,title="Load Data From File"
2006        Button button2,pos={118,84},size={100,20},proc=Rebin_Append,title="Append Data"
2007        Button button3,pos={11,84},size={80,20},proc=Rebin_newGraph,title="New Graph"
2008        Button button6,pos={11,138},size={70,20},proc=Rebin_by,title="Rebin by"
2009        Button button_8,pos={160,138},size={90,20},proc=SaveResultBin,title="Save Result"
2010       
2011        SetVariable end_3,pos={97,140},size={40,14},title=" ",Font="Arial",fsize=10
2012        SetVariable end_3,limits={1,10,1},value= root:Packages:NIST:Rebin:binning
2013
2014EndMacro
2015
2016
2017Function Rebin_Done(ctrlName) : ButtonControl
2018        String ctrlName
2019        DoWindow/K Rebin_Panel
2020End
2021
2022Proc Rebin_Load(ctrlName) : ButtonControl
2023        String ctrlName
2024       
2025        A_LoadOneDData()
2026        ControlUpdate/W=Rebin_Panel popup0
2027End
2028
2029Function Rebin_Append(ctrlName) : ButtonControl
2030        String ctrlName
2031        //appends data set from memory   /// joindre
2032        String iStr,qStr,eStr
2033        Variable rr,gg,bb
2034       
2035        //get the current selection
2036        ControlInfo popup0
2037        if(strlen(S_Value)==0)
2038                Abort "You must load data from a file into memory before appending the data"
2039        Endif
2040       
2041        A_PM_doAppend(S_Value)
2042       
2043        DoWindow/F Rebin_Panel
2044End
2045
2046
2047Function Rebin_newGraph(ctrlName) : ButtonControl
2048        String ctrlName
2049
2050        //get the current selection
2051        ControlInfo popup0
2052        if(strlen(S_Value)==0 || cmpstr(S_Value,"No data loaded")==0)
2053                Abort "You must load data from a file into memory before plotting the data"
2054        Endif
2055       
2056        A_PM_doNewGraph(S_Value)
2057       
2058        DoWindow/F Rebin_Panel
2059End
2060
2061
2062
2063Function Rebin_by (ctrlName) : ButtonControl
2064        String ctrlName
2065       
2066       
2067        Variable len
2068        Variable ii,jj, helplp,kk
2069        String iStr,qStr,eStr,sqStr,qbStr,fsStr,wtStr,folderStr
2070        Variable rr,gg,bb
2071        NVAR binpts = root:Packages:NIST:Rebin:binning
2072       
2073       
2074       
2075        ControlInfo popup0
2076        if(strlen(S_Value)==0)
2077                Abort "You must load data from a file into memory before plotting the data"
2078        Endif
2079        folderStr = S_Value     
2080       
2081        SetDataFolder $("root:"+folderStr)
2082       
2083        Wave w0 = $(folderStr+"_q")
2084        Wave w1 = $(folderStr+"_i")
2085        Wave w2 = $(folderStr+"_s")
2086        Wave resW = $(folderStr+"_res")
2087       
2088        len = numpnts(w0)
2089        Make/O/D/N=(len) w3,w4,w5
2090        w3 = resW[p][0]         //std dev of resolution fn
2091        w4 = resW[p][1]         //mean q-value
2092        w5 = resW[p][2]         //beamstop shadow factor
2093       
2094        Make/O/D/N=(round((len-1)/binpts)) qbin,Ibin,sigbin,sqbin,qbbin,fsbin,helpTemp
2095
2096        qbin = 0
2097        Ibin = 0
2098        sigbin = 0
2099        sqbin = 0
2100        qbbin = 0
2101        fsbin = 0
2102        helptemp = 0
2103       
2104        ii=0
2105        do
2106                qBin = qBin + w0[binpts*p+ii]/binpts
2107                iBin = iBin + w1[binpts*p+ii]/binpts
2108                helptemp = helptemp + w2[binpts*p+ii]^2
2109                sqBin = sqBin + w3[binpts*p+ii]/binpts
2110                qbBin = qbBin + w4[binpts*p+ii]/binpts
2111                fsBin = fsBin + w5[binpts*p+ii]/binpts
2112               
2113
2114      ii+=1
2115        while (ii<binpts)
2116     
2117        sigBin  = sqrt(helptemp)/binpts
2118
2119        CheckDisplayed Ibin             //check top graph only
2120        if(V_flag==0)
2121                AppendToGraph Ibin vs qbin
2122                ModifyGraph log=1,mode(Ibin)=4,marker(Ibin)=29,msize(Ibin)=3,rgb(Ibin)=(65535,0,0)
2123                ErrorBars/T=0 ibin Y,wave=(sigbin,sigbin)
2124        endif
2125
2126        KillWaves/Z helpTemp,w3,w4,w5
2127       
2128        SetDataFolder root:
2129End
2130
2131
2132// duplicates the binned data to its own data folder, fixing up the names
2133// as needed, then write out the file from the folder using the standard procedures
2134// then kill the data folder to clean up.
2135Function SaveResultBin(ctrlName) : ButtonControl
2136        String ctrlName
2137       
2138        String finalname,folderStr
2139       
2140        ControlInfo popup0
2141        folderStr=S_Value       
2142       
2143        SetDataFolder $("root:"+folderStr)
2144        WAVE qbin = qBin
2145        WAVE Ibin = iBin
2146        WAVE sigbin = sigBin
2147        WAVE sqbin = sqBin
2148        WAVE qbbin = qbBin
2149        WAVE fsbin = fsBin
2150       
2151        NVAR/Z dQv = USANS_dQv
2152        Variable isSANS = (NVAR_Exists(dQv) == 0)
2153//      Print "isSANS = ",isSANS
2154        if(isSANS)
2155                // SANS data - make a smaller resWave
2156                Variable np=numpnts(qBin)
2157                Make/D/O/N=(np,4) $(folderStr+"_resBin")
2158                Wave res = $(folderStr+"_resBin")
2159               
2160                res[][0] = sqBin[p]             //sigQ
2161                res[][1] = qbBin[p]             //qBar
2162                res[][2] = fsBin[p]             //fShad
2163                res[][3] = qBin[p]              //Qvalues
2164       
2165        endif
2166        finalname = folderStr+"bin"
2167       
2168        SetDataFolder root:
2169        DuplicateDataSet(folderStr,finalname,1)
2170       
2171        // rename the waves so that the correct ones are written out
2172        SetDataFolder $("root:"+finalname)
2173        Killwaves/Z $(finalname+"_q")
2174        Killwaves/Z $(finalname+"_i")
2175        Killwaves/Z $(finalname+"_s")
2176        if(isSANS)              //if USANS, keep the resolution waves, so that the writer will recognize the set as USANS
2177                Killwaves/Z $(finalname+"_res")
2178        endif
2179               
2180        WAVE qbin = qBin                //reset the wave references to the new folder (bin)
2181        WAVE Ibin = iBin
2182        WAVE sigbin = sigBin
2183        if(isSANS)
2184                Wave res = $(finalName+"_resBin")
2185        endif
2186       
2187        Rename qbin $(finalname+"_q")
2188        Rename ibin $(finalname+"_i")
2189        Rename sigbin $(finalname+"_s")
2190        if(isSANS)
2191                Rename res $(finalname+"_res")
2192        endif
2193        SetDataFolder root:
2194       
2195        // save out the data in the folder
2196        SaveDataSetToFile(finalName)
2197
2198        KillDataFolder/Z $("root:"+finalname)
2199
2200        SetDataFolder root:
2201        return(0)
2202End
2203
2204
2205
2206
2207/// end rebinning procedures
Note: See TracBrowser for help on using the repository browser.