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

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