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

Last change on this file since 819 was 819, checked in by ajj, 12 years ago
  • Temporary fix to PolyCore_and_NShells to have large enough number of integration steps. This required not using the XOP versions of the function. Fix of XOP will follow once build environments are setup again.
  • Added function to DataSetHandling?.ipf to resort a loaded dataset by Q. GRASP doesn't sort the data when joining data from different distances and this makes fits look odd. Note that function does not resort resolution wave at the moment. This was a quick hack for Hanna Wacklin and Lionel Porcar and a proper fix will be forthcoming.
File size: 57.1 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
1712Function ReSortDataSet(set1name)
1713        String set1name
1714       
1715        String set1Path = "root:"+set1Name+":"
1716        String curPath = GetDataFolder(1)
1717
1718        SetDataFolder set1Path
1719       
1720        sort $(set1name+"_q"),$(set1name+"_q"),$(set1name+"_i"),$(set1name+"_s")
1721       
1722        SetDataFolder curPath
1723
1724        return 0
1725End
1726
1727
1728///////////////////////////Other Utility functions ////////////////////////////
1729
1730Function SaveDataSetToFile(folderName)
1731        String folderName
1732
1733        String protoStr = ""
1734        //Check for history string in folder
1735        //If it doesn't exist then
1736
1737        //Do saving of data file.
1738       
1739        NVAR gXML_Write = root:Packages:NIST:gXML_Write
1740
1741        if (gXML_Write == 1)
1742                ReWrite1DXMLData(folderName)
1743        else
1744                fReWrite1DData(folderName,"tab","CRLF")
1745        endif
1746
1747        //Include history string to record what was done?
1748
1749End
1750
1751
1752
1753// still need to get the header information, and possibly the SASprocessnote from the XML load into the 6-column header
1754//
1755// start by looking in:
1756//      String xmlReaderFolder = "root:Packages:CS_XMLreader:"
1757// for Title and Title_folder strings -> then the metadata (but the processnote is still not there
1758//
1759// may need to get it directly using the filename
1760Function  convertNISTXMLtoNIST6Col(fname)
1761        String fname
1762
1763        String list, item,path
1764        Variable num,ii
1765       
1766        //load the XML
1767       
1768        LoadNISTXMLData(fname,"",0,0)           //no plot, no force overwrite
1769//      Execute "A_LoadOneDDataWithName(\""+fname+"\",0)"               //won't plot
1770
1771        // then rewrite what is in the data folder that was just loaded
1772        String basestr = ParseFilePath(0, fname, ":", 1, 0)
1773        baseStr = CleanupName(baseStr,0)
1774        print fname
1775        print basestr
1776
1777        fReWrite1DData_noPrompt(baseStr,"tab","CR")
1778
1779        return(0)
1780End
1781
1782
1783///////// SRK - VERY SIMPLE batch converter
1784// no header information is preserved
1785// file names are partially preserved
1786//
1787
1788/// to use this:
1789// -open the Plot Manager and set the path
1790// -run this function
1791//
1792// it doesn't matter if the XML ouput flag is set - this overrides.
1793Function batchXML26ColConvert()
1794
1795        String list, item,path,fname
1796        Variable num,ii
1797       
1798        PathInfo CatPathName
1799        path = S_Path
1800
1801        list = A_ReducedDataFileList("")
1802        num = itemsInList(list)
1803        Print num
1804        for(ii=0;ii<num;ii+=1)
1805                item = StringFromList(ii, list ,";")
1806                fname=path + item
1807                Execute "A_LoadOneDDataWithName(\""+fname+"\",0)"               //won't plot
1808//              easier to load all, then write out, since the name will be changed
1809        endfor
1810       
1811       
1812        list = DM_DataSetPopupList()
1813
1814        num = itemsInList(list)
1815        Print num
1816        for(ii=0;ii<num;ii+=1)
1817                item = StringFromList(ii, list ,";")
1818                fReWrite1DData_noPrompt(item,"tab","CR")
1819        endfor
1820       
1821End
1822
1823///////// SRK - VERY SIMPLE batch converter
1824// NO header information is preserved
1825// file names are partially preserved
1826//
1827
1828/// to use this:
1829// -open the Plot Manager and set the path
1830// -run this function
1831//
1832// it doesn't matter if the XML ouput flag is set - this overrides.
1833//
1834// The GRASP output data is 5-column Q-I-errI-sigQ-nCells
1835// which gets read in as q-i-s-ism-fit_ism (as if it was some wierd USANS data format)
1836// -- so fake the output...
1837//
1838Function batchGrasp26ColConvert()
1839
1840        String list, item,path,fname
1841        Variable num,ii,npt
1842       
1843        PathInfo CatPathName
1844        path = S_Path
1845
1846        list = A_ReducedDataFileList("")
1847        num = itemsInList(list)
1848        Print num
1849        for(ii=0;ii<num;ii+=1)
1850                item = StringFromList(ii, list ,";")
1851                fname=path + item
1852                Execute "A_LoadOneDDataWithName(\""+fname+"\",0)"               //won't plot
1853//              easier to load all, then write out, since the name will be changed
1854        endfor
1855       
1856       
1857        list = DM_DataSetPopupList()
1858
1859        num = itemsInList(list)
1860       
1861        Print num
1862        for(ii=0;ii<num;ii+=1)
1863                item = StringFromList(ii, list ,";")
1864               
1865                // fake the 6-column NIST data structure
1866                WAVE qw = $("root:"+item+":"+item+"_q")
1867                npt = numpnts(qw)
1868                Make/O/D/N=(npt,4) $("root:"+item+":"+item+"_res")
1869                WAVE res = $("root:"+item+":"+item+"_res")
1870                WAVE sigQ = $("root:"+item+":"+item+"_ism")
1871                res[][0] = sigQ[p]      // sigQ
1872                res[][1] = qw[p]                // qBar ~ q
1873                res[][2] = 1            //shadow
1874                res[][3] = qw[p]                // q
1875               
1876               
1877                fReWrite1DData_noPrompt(item,"tab","CR")
1878        endfor
1879       
1880End
1881
1882// quick version (copied from fReWrite1DData() that NEVER asks for a new fileName
1883// - and right now, always expect 6-column data, either SANS or USANS (re-writes -dQv)
1884// - AJJ Nov 2009 : better make sure we always fake 6 columns on reading then....
1885Function fReWrite1DData_noPrompt(folderStr,delim,term)
1886        String folderStr,delim,term
1887       
1888        String formatStr="",fullpath=""
1889        Variable refnum,dialog=1
1890       
1891        String dataSetFolderParent,basestr
1892       
1893        //setup delimeter and terminator choices
1894        If(cmpstr(delim,"tab")==0)
1895                //tab-delimeted
1896                formatStr="%15.8g\t%15.8g\t%15.8g\t%15.8g\t%15.8g\t%15.8g"
1897        else
1898                //use 3 spaces
1899                formatStr="%15.8g   %15.8g   %15.8g   %15.8g   %15.8g   %15.8g"
1900        Endif
1901        If(cmpstr(term,"CR")==0)
1902                formatStr += "\r"
1903        Endif
1904        If(cmpstr(term,"LF")==0)
1905                formatStr += "\n"
1906        Endif
1907        If(cmpstr(term,"CRLF")==0)
1908                formatStr += "\r\n"
1909        Endif
1910       
1911        //Abuse ParseFilePath to get path without folder name
1912        dataSetFolderParent = ParseFilePath(1,folderStr,":",1,0)
1913        //Abuse ParseFilePath to get basestr
1914        basestr = ParseFilePath(0,folderStr,":",1,0)
1915       
1916        //make sure the waves exist
1917        SetDataFolder $(dataSetFolderParent+basestr)
1918        WAVE/Z qw = $(baseStr+"_q")
1919        WAVE/Z iw = $(baseStr+"_i")
1920        WAVE/Z sw = $(baseStr+"_s")
1921        WAVE/Z resw = $(baseStr+"_res")
1922       
1923        if(WaveExists(qw) == 0)
1924                Abort "q is missing"
1925        endif
1926        if(WaveExists(iw) == 0)
1927                Abort "i is missing"
1928        endif
1929        if(WaveExists(sw) == 0)
1930                Abort "s is missing"
1931        endif
1932        if(WaveExists(resw) == 0)
1933                Abort "Resolution information is missing."
1934        endif
1935       
1936        Duplicate/O qw qbar,sigQ,fs
1937        if(dimsize(resW,1) > 4)
1938                //it's USANS put -dQv back in the last 3 columns
1939                NVAR/Z dQv = USANS_dQv
1940                if(NVAR_Exists(dQv) == 0)
1941                        Abort "It's USANS data, and I don't know what the slit height is."
1942                endif
1943                sigQ = -dQv
1944                qbar = -dQv
1945                fs = -dQv
1946        else
1947                //it's SANS
1948                sigQ = resw[p][0]
1949                qbar = resw[p][1]
1950                fs = resw[p][2]
1951        endif
1952       
1953        dialog=0
1954        if(dialog)
1955                PathInfo/S catPathName
1956//              fullPath = DoSaveFileDialog("Save data as",fname=baseStr+".txt")
1957                fullPath = DoSaveFileDialog("Save data as",fname=baseStr[0,strlen(BaseStr)-1])
1958                Print fullPath
1959                If(cmpstr(fullPath,"")==0)
1960                        //user cancel, don't write out a file
1961                        Close/A
1962                        Abort "no data file was written"
1963                Endif
1964                //Print "dialog fullpath = ",fullpath
1965        Endif
1966        PathInfo catPathName
1967        fullPath = S_Path + baseStr[0,strlen(BaseStr)-1]
1968
1969        Open refnum as fullpath
1970       
1971        fprintf refnum,"Modified data written from folder %s on %s\r\n",baseStr,(date()+" "+time())
1972        wfprintf refnum,formatStr,qw,iw,sw,sigQ,qbar,fs
1973        Close refnum
1974       
1975        KillWaves/Z sigQ,qbar,fs
1976       
1977        SetDataFolder root:
1978        return(0)
1979End
1980
1981///////////end SRK
1982
1983
1984
1985///// SRK
1986///// Rebinning
1987
1988
1989// main entry procedure for subtraction panel
1990// re-initializes necessary folders and waves
1991Proc OpenRebin()
1992        DoWindow/F Rebin_Panel
1993        if(V_Flag==0)
1994                InitRebin()
1995                Rebin()         //the panel
1996//              Plot_Sub1D()            //the graph
1997        endif
1998       
1999End
2000
2001Function InitRebin()
2002
2003        NewDataFolder/O/S root:Packages:NIST:Rebin
2004//      Variable/G gPtsBeg1=0
2005//      Variable/G gPtsEnd1=0
2006        Variable/G binning=2
2007
2008        SetDataFolder root:
2009End
2010
2011Proc Rebin()
2012
2013        PauseUpdate; Silent 1           // building window...
2014        NewPanel /W=(658,347,920,562)/K=1
2015        DoWindow/C Rebin_Panel
2016        ModifyPanel cbRGB=(65535,48662,45086),fixedSize=1
2017        Button button0,pos={161,178},size={50,20},proc=Rebin_Done,title="Done"
2018        PopupMenu popup0,pos={11,50},size={211,20},title="Data in Memory"
2019        PopupMenu popup0,mode=1,value= #"A_OneDDataInMemory()"
2020        Button button1,pos={60,14},size={150,20},proc=Rebin_Load,title="Load Data From File"
2021        Button button2,pos={118,84},size={100,20},proc=Rebin_Append,title="Append Data"
2022        Button button3,pos={11,84},size={80,20},proc=Rebin_newGraph,title="New Graph"
2023        Button button6,pos={11,138},size={70,20},proc=Rebin_by,title="Rebin by"
2024        Button button_8,pos={160,138},size={90,20},proc=SaveResultBin,title="Save Result"
2025        Button button_9,pos={20,178},size={30,20},proc=Rebin_Help,title="?"
2026
2027        SetVariable end_3,pos={97,140},size={40,14},title=" ",Font="Arial",fsize=10
2028        SetVariable end_3,limits={1,10,1},value= root:Packages:NIST:Rebin:binning
2029
2030EndMacro
2031
2032Function Rebin_Help(ba) : ButtonControl
2033        STRUCT WMButtonAction &ba
2034       
2035        String win = ba.win
2036
2037        switch (ba.eventCode)
2038                case 2:
2039                        // click code here
2040                        DisplayHelpTopic/Z/K=1 "Re-Bin Data"
2041                        if(V_flag !=0)
2042                                DoAlert 0,"The ReBin Help file could not be found"
2043                        endif
2044                        break
2045        endswitch
2046
2047        return 0
2048End
2049
2050Function Rebin_Done(ctrlName) : ButtonControl
2051        String ctrlName
2052        DoWindow/K Rebin_Panel
2053End
2054
2055Proc Rebin_Load(ctrlName) : ButtonControl
2056        String ctrlName
2057       
2058        A_LoadOneDData()
2059        ControlUpdate/W=Rebin_Panel popup0
2060End
2061
2062Function Rebin_Append(ctrlName) : ButtonControl
2063        String ctrlName
2064        //appends data set from memory   /// joindre
2065        String iStr,qStr,eStr
2066        Variable rr,gg,bb
2067       
2068        //get the current selection
2069        ControlInfo popup0
2070        if(strlen(S_Value)==0)
2071                Abort "You must load data from a file into memory before appending the data"
2072        Endif
2073       
2074        A_PM_doAppend(S_Value)
2075       
2076        DoWindow/F Rebin_Panel
2077End
2078
2079
2080Function Rebin_newGraph(ctrlName) : ButtonControl
2081        String ctrlName
2082
2083        //get the current selection
2084        ControlInfo popup0
2085        if(strlen(S_Value)==0 || cmpstr(S_Value,"No data loaded")==0)
2086                Abort "You must load data from a file into memory before plotting the data"
2087        Endif
2088       
2089        A_PM_doNewGraph(S_Value)
2090       
2091        DoWindow/F Rebin_Panel
2092End
2093
2094
2095
2096Function Rebin_by (ctrlName) : ButtonControl
2097        String ctrlName
2098       
2099       
2100        Variable len
2101        Variable ii,jj, helplp,kk
2102        String iStr,qStr,eStr,sqStr,qbStr,fsStr,wtStr,folderStr
2103        Variable rr,gg,bb
2104        NVAR binpts = root:Packages:NIST:Rebin:binning
2105       
2106       
2107       
2108        ControlInfo popup0
2109        if(strlen(S_Value)==0)
2110                Abort "You must load data from a file into memory before plotting the data"
2111        Endif
2112        folderStr = S_Value     
2113       
2114        SetDataFolder $("root:"+folderStr)
2115       
2116        Wave w0 = $(folderStr+"_q")
2117        Wave w1 = $(folderStr+"_i")
2118        Wave w2 = $(folderStr+"_s")
2119        Wave resW = $(folderStr+"_res")
2120       
2121        len = numpnts(w0)
2122        Make/O/D/N=(len) w3,w4,w5
2123        w3 = resW[p][0]         //std dev of resolution fn
2124        w4 = resW[p][1]         //mean q-value
2125        w5 = resW[p][2]         //beamstop shadow factor
2126       
2127        Make/O/D/N=(round((len-1)/binpts)) qbin,Ibin,sigbin,sqbin,qbbin,fsbin,helpTemp
2128
2129        qbin = 0
2130        Ibin = 0
2131        sigbin = 0
2132        sqbin = 0
2133        qbbin = 0
2134        fsbin = 0
2135        helptemp = 0
2136       
2137        ii=0
2138        do
2139                qBin = qBin + w0[binpts*p+ii]/binpts
2140                iBin = iBin + w1[binpts*p+ii]/binpts
2141                helptemp = helptemp + w2[binpts*p+ii]^2
2142                sqBin = sqBin + w3[binpts*p+ii]/binpts
2143                qbBin = qbBin + w4[binpts*p+ii]/binpts
2144                fsBin = fsBin + w5[binpts*p+ii]/binpts
2145               
2146
2147      ii+=1
2148        while (ii<binpts)
2149     
2150        sigBin  = sqrt(helptemp)/binpts
2151
2152        CheckDisplayed Ibin             //check top graph only
2153        if(V_flag==0)
2154                AppendToGraph Ibin vs qbin
2155                ModifyGraph log=1,mode(Ibin)=4,marker(Ibin)=29,msize(Ibin)=3,rgb(Ibin)=(65535,0,0)
2156                ErrorBars/T=0 ibin Y,wave=(sigbin,sigbin)
2157        endif
2158
2159        KillWaves/Z helpTemp,w3,w4,w5
2160       
2161        SetDataFolder root:
2162End
2163
2164
2165// duplicates the binned data to its own data folder, fixing up the names
2166// as needed, then write out the file from the folder using the standard procedures
2167// then kill the data folder to clean up.
2168Function SaveResultBin(ctrlName) : ButtonControl
2169        String ctrlName
2170       
2171        String finalname,folderStr
2172       
2173        ControlInfo popup0
2174        folderStr=S_Value       
2175       
2176        SetDataFolder $("root:"+folderStr)
2177        WAVE qbin = qBin
2178        WAVE Ibin = iBin
2179        WAVE sigbin = sigBin
2180        WAVE sqbin = sqBin
2181        WAVE qbbin = qbBin
2182        WAVE fsbin = fsBin
2183       
2184        NVAR/Z dQv = USANS_dQv
2185        Variable isSANS = (NVAR_Exists(dQv) == 0)
2186//      Print "isSANS = ",isSANS
2187        if(isSANS)
2188                // SANS data - make a smaller resWave
2189                Variable np=numpnts(qBin)
2190                Make/D/O/N=(np,4) $(folderStr+"_resBin")
2191                Wave res = $(folderStr+"_resBin")
2192               
2193                res[][0] = sqBin[p]             //sigQ
2194                res[][1] = qbBin[p]             //qBar
2195                res[][2] = fsBin[p]             //fShad
2196                res[][3] = qBin[p]              //Qvalues
2197       
2198        endif
2199        finalname = folderStr+"bin"
2200       
2201        SetDataFolder root:
2202        DuplicateDataSet(folderStr,finalname,1)
2203       
2204        // rename the waves so that the correct ones are written out
2205        SetDataFolder $("root:"+finalname)
2206        Killwaves/Z $(finalname+"_q")
2207        Killwaves/Z $(finalname+"_i")
2208        Killwaves/Z $(finalname+"_s")
2209        if(isSANS)              //if USANS, keep the resolution waves, so that the writer will recognize the set as USANS
2210                Killwaves/Z $(finalname+"_res")
2211        endif
2212               
2213        WAVE qbin = qBin                //reset the wave references to the new folder (bin)
2214        WAVE Ibin = iBin
2215        WAVE sigbin = sigBin
2216        if(isSANS)
2217                Wave res = $(finalName+"_resBin")
2218        endif
2219       
2220        Rename qbin $(finalname+"_q")
2221        Rename ibin $(finalname+"_i")
2222        Rename sigbin $(finalname+"_s")
2223        if(isSANS)
2224                Rename res $(finalname+"_res")
2225        endif
2226        SetDataFolder root:
2227       
2228        // save out the data in the folder
2229        SaveDataSetToFile(finalName)
2230
2231        KillDataFolder/Z $("root:"+finalname)
2232
2233        SetDataFolder root:
2234        return(0)
2235End
2236
2237
2238
2239
2240/// end rebinning procedures
Note: See TracBrowser for help on using the repository browser.