source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/VSANS/V_Protocol_Reduction.ipf @ 1032

Last change on this file since 1032 was 1032, checked in by srkline, 6 years ago

made the file selections in the test protocol panel to be popup menus with the filtered files for each step. popping now selects the file. Still a work in progress.

File size: 84.8 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma version=5.0
3#pragma IgorVersion=6.1
4
5//************************
6//      Vers. 1.2 092101
7//
8//7/2001 converted protocols to simply use the filenames, rather than the path:filename
9//this should allow portability of the protocols between machines
10//**ALL** protocols now depend on the path "catPathName"
11//
12//procedure files for construction of protocols interactively,
13//selecting files from the CAT/VSHORT table, rather than picking blindly
14//from a dialog - the process of selecting/setting files is not as
15//transparent as it could be for new users
16//
17//*************************
18//////////////////////////////////
19//
20//              KEYWORD=<value> lists used in protocol definitions
21//
22//              KEYWORDS are ALWAYS capitalized, and yes, it does matter
23//
24//              for ABSOLUTE parameters
25//              (4) possible keywords, all with numerical values
26//              TSTAND=value            transmission of the standard
27//              DSTAND=value            thickness of the standard, in centimeters
28//              IZERO=value             I(q=0) value for the standard, in normalized neutron counts
29//              XSECT=value             calibrated cross-section of the standard sample
30//
31//              For calibration with a transmission file, set TSTAND, DSTAND, and XSECT to 1.0
32//              and set IZERO to KAPPA (defined in Tania's handout, or in documentation of MRED_KAP on VAX)
33//
34//
35//              For AVERAGE and for DRAWING
36//                      DRAWING routines only use a subset of the total list, since saving, naming, etc. don't apply
37//              (10) possible keywords, some numerical, some string values
38//              AVTYPE=string           string from set {Circular,Annular,Rectangular,Sector,2D_ASCII,QxQy_ASCII,PNG_Graphic;Sector_PlusMinus;}
39//              PHI=value                       azimuthal angle (-90,90)
40//              DPHI=value                      +/- angular range around phi for average
41//              WIDTH=value             total width of rectangular section, in pixels
42//              SIDE=string             string from set {left,right,both} **note NOT capitalized
43//              QCENTER=value           q-value (1/A) of center of annulus for annular average
44//              QDELTA=value            total width of annulus centered at QCENTER
45//              PLOT=string             string from set {Yes,No} = truth of generating plot of averaged data
46//              SAVE=string             string from set {Yes,No} = truth of saving averaged data to disk
47//              NAME=string             string from set {Auto,Manual} = Automatic name generation or Manual(dialog)
48//
49//    BINTYPE=string (VSANS binning type) "One;Two;Four;Slit Mode;"
50//
51//              For work.DRK usage:
52//              **the list is COMMA delimited, soparator is =
53//              DRK=none,DRKMODE=0,
54//              DRK=name                        is the name of the file, must be a full name, expected to be raw data
55//              DRKMODE=value           is a numeric value (0 or 10 to add to the Correct(mode) switch (unused?)
56//
57//////////////////////////////////
58
59
60//main entry procedure for initialzing and displaying the protocol panel
61// initilaizes folders and globals as needed
62//
63Proc V_ReductionProtocolPanel()
64        DoWindow/F V_ProtocolPanel
65        if(V_flag==0)
66                V_InitProtocolPanel()
67                V_ProtocolPanel()
68        Endif
69End
70
71//initialization procedure for the protocol panel
72//note that :gAbsStr is also shared (common global) to that used in
73//the questionnare form of the protcol (see protocol.ipf)
74//
75//0901, uses 8 points in protocol wave
76Proc V_InitProtocolPanel()
77
78        //set up the global variables needed for the protocol panel
79        //global strings to put in a temporary protocol textwave
80        Variable ii=0,nsteps=8
81        String waveStr="tempProtocol"
82        SetDataFolder root:Packages:NIST:VSANS:Globals:Protocols
83        Make/O/T/N=(nsteps) $"root:Packages:NIST:VSANS:Globals:Protocols:tempProtocol" = ""
84
85        String/G root:Packages:NIST:VSANS:Globals:Protocols:gSAM="ask"
86        String/G root:Packages:NIST:VSANS:Globals:Protocols:gBGD="ask"
87        String/G root:Packages:NIST:VSANS:Globals:Protocols:gEMP="ask"
88        String/G root:Packages:NIST:VSANS:Globals:Protocols:gDIV="ask"
89        String/G root:Packages:NIST:VSANS:Globals:Protocols:gMASK="ask"
90        String/G root:Packages:NIST:VSANS:Globals:Protocols:gAbsStr="ask"
91        String/G root:Packages:NIST:VSANS:Globals:Protocols:gAVE="AVTYPE=Circular;SAVE=Yes;NAME=Auto;PLOT=Yes;"
92        String/G root:Packages:NIST:VSANS:Globals:Protocols:gDRK="DRK=none,DRKMODE=0,"
93       
94        SetDataFolder root:
95       
96End
97
98
99//button procedure to reset the panel seletctions/checks...etc...
100//to reflect the choices in a previously saved protocol
101// - parses through the protocol and resets the appropriate global strings and
102//updates the panel display
103//
104Function V_RecallProtocolButton(ctrlName) : ButtonControl
105        String ctrlName
106
107        //will reset panel values based on a previously saved protocol
108        //pick a protocol wave from the Protocols folder
109        //MUST move to Protocols folder to get wavelist
110        SetDataFolder root:Packages:NIST:VSANS:Globals:Protocols
111        Execute "V_PickAProtocol()"
112       
113        //get the selected protocol wave choice through a global string variable
114        SVAR protocolName = root:Packages:NIST:VSANS:Globals:Protocols:gProtoStr
115
116        //If "CreateNew" was selected, ask user to try again
117        if(cmpstr("CreateNew",protocolName) == 0)
118                Abort "CreateNew is for making a new Protocol. Select a previously saved Protocol"
119        Endif
120       
121        //reset the panel based on the protocol textwave (currently a string)
122        V_ResetToSavedProtocol(protocolName)
123       
124        SetDataFolder root:
125        return(0)
126End
127
128//deletes the selected protocol from the list and from memory
129//
130Function V_DeleteProtocolButton(ctrlName) : ButtonControl
131        String ctrlName
132
133        //put up a list of protocols and pick one
134        SetDataFolder root:Packages:NIST:VSANS:Globals:Protocols
135//      Execute "DeleteAProtocol()"
136        String Protocol=""
137        Prompt Protocol "Delete A Protocol",popup, V_DeletableProtocols()
138        DoPrompt "Select protocol to delete",protocol
139        If(V_flag==1)
140                return(0)
141        endif
142
143        //If "CreateNew, Base, DoAll, or tempProtocol" was selected, do nothing
144        strswitch(protocol)
145                case "CreateNew":
146                        break
147                case "DoAll":
148                        break
149                case "Base":
150                        break
151                case "tempProtocol":
152                        break
153                default:
154                        //delete the protocol
155                        KillWaves/Z $protocol
156        endswitch
157       
158        SetDataFolder root:
159        return(0)
160End
161
162
163//function that actually parses the protocol specified by nameStr
164//which is just the name of the wave, without a datafolder path
165//
166Function V_ResetToSavedProtocol(nameStr)
167        String nameStr
168       
169        //allow special cases of Base and DoAll Protocols to be recalled to panel - since they "ask"
170        //and don't need paths
171       
172        String catPathStr
173        PathInfo catPathName
174        catPathStr=S_path
175       
176        //SetDataFolder root:Packages:NIST:VSANS:Globals:Protocols              //on windows, data folder seems to get reset (erratically) to root:
177        Wave/T w=$("root:Packages:NIST:VSANS:Globals:Protocols:" + nameStr)
178       
179        String fullPath="",comma=",",list="",nameList="",PathStr="",item=""
180        Variable ii=0,numItems,checked,specialProtocol
181       
182        if((cmpstr(nameStr,"Base")==0) || (cmpstr(nameStr,"DoAll")==0))
183                specialProtocol = 1
184        else
185                specialProtocol = 0
186        Endif
187       
188        //background
189        checked = 1
190        nameList = w[0]
191        If(cmpstr(nameList,"none") ==0)
192                checked = 0
193        Endif
194
195        //set the global string to display and checkbox
196        String/G root:Packages:NIST:VSANS:Globals:Protocols:gBGD = nameList
197        CheckBox prot_check win=V_ProtocolPanel,value=checked
198       
199        //empty
200        checked = 1
201        nameList = w[1]
202        If(cmpstr(nameList,"none") ==0)
203                checked = 0
204        Endif
205
206        //set the global string to display and checkbox
207        String/G root:Packages:NIST:VSANS:Globals:Protocols:gEMP = nameList
208        CheckBox prot_check_1 win=V_ProtocolPanel,value=checked
209       
210        //DIV file
211        checked = 1
212        nameList = w[2]
213        If(cmpstr(nameList,"none") ==0)
214                checked = 0
215        Endif
216
217        //set the global string to display and checkbox
218        String/G root:Packages:NIST:VSANS:Globals:Protocols:gDIV = nameList
219        CheckBox prot_check_2 win=V_ProtocolPanel,value=checked
220       
221        //Mask file
222        checked = 1
223        nameList = w[3]
224        If(cmpstr(nameList,"none") ==0)
225                checked = 0
226        Endif
227
228        //set the global string to display and checkbox
229        String/G root:Packages:NIST:VSANS:Globals:Protocols:gMASK = nameList
230        CheckBox prot_check_3 win=V_ProtocolPanel,value=checked
231       
232        //4 = abs parameters
233        list = w[4]
234        numItems = ItemsInList(list,";")
235        checked = 1
236        if(numitems == 4 || numitems == 5)              //allow for protocols with no SDEV list item
237                //correct number of parameters, assume ok
238                String/G root:Packages:NIST:VSANS:Globals:Protocols:gAbsStr = list
239                CheckBox prot_check_9 win=V_ProtocolPanel,value=checked
240        else
241                item = StringFromList(0,list,";")
242                if(cmpstr(item,"none")==0)
243                        checked = 0
244                        list = "none"
245                        String/G root:Packages:NIST:VSANS:Globals:Protocols:gAbsStr = list
246                        CheckBox prot_check_9 win=V_ProtocolPanel,value=checked
247                else
248                        //force to "ask"
249                        checked = 1
250                        String/G root:Packages:NIST:VSANS:Globals:Protocols:gAbsStr = "ask"
251                        CheckBox prot_check_9 win=V_ProtocolPanel,value=checked
252                Endif
253        Endif
254       
255        //5 = averaging choices
256        list = w[5]
257        item = StringByKey("AVTYPE",list,"=",";")
258        if(cmpstr(item,"none") == 0)
259                checked = 0
260                String/G root:Packages:NIST:VSANS:Globals:Protocols:gAVE = "none"
261                CheckBox prot_check_5 win=V_ProtocolPanel,value=checked
262        else
263                checked = 1
264                String/G root:Packages:NIST:VSANS:Globals:Protocols:gAVE = list
265                CheckBox prot_check_5 win=V_ProtocolPanel,value=checked
266        Endif
267       
268//      //6 = DRK choice
269//      list = w[6]
270//      item = StringByKey("DRK",list,"=",",")
271//      //print list,item
272//      if(cmpstr(item,"none") == 0)
273//              checked = 0
274//              String/G root:Packages:NIST:VSANS:Globals:Protocols:gDRK = list
275//              CheckBox prot_check_6 win=V_ProtocolPanel,value=checked
276//      else
277//              checked = 1
278//              String/G root:Packages:NIST:VSANS:Globals:Protocols:gDRK = list
279//              CheckBox prot_check_6 win=V_ProtocolPanel,value=checked
280//      Endif
281       
282        //7 = unused
283       
284        //all has been reset, get out
285        Return (0)
286End
287
288//button action procedure that simply closes the panel
289//
290Function V_DoneProtocolButton(ctrlName) : ButtonControl
291        String ctrlName
292
293        //will gracefully close and exit the protocol panel
294       
295        DoWindow/K V_ProtocolPanel
296End
297
298//button action procedure that saves the current choices on the panel
299//as a named protocol, for later recall
300//asks for a valid name, then creates a protocol from the panel choices
301//creates a wave, and sets the current protocol name to this new protocol
302//
303//now allows the user the choice to overwrite a protocol
304//
305Function V_SaveProtocolButton(ctrlName) : ButtonControl
306        String ctrlName
307
308        Variable notDone=1, newProto=1
309        //will prompt for protocol name, and save the protocol as a text wave
310        //prompt for name of new protocol wave to save
311        do
312                Execute "V_AskForName()"
313                SVAR newProtocol = root:Packages:NIST:VSANS:Globals:Protocols:gNewStr
314               
315                //make sure it's a valid IGOR name
316                newProtocol = CleanupName(newProtocol,0)        //strict naming convention
317                String/G root:Packages:NIST:VSANS:Globals:Protocols:gNewStr=newProtocol         //reassign, if changed
318                Print "newProtocol = ",newProtocol
319               
320                SetDataFolder root:Packages:NIST:VSANS:Globals:Protocols
321                if(WaveExists( $("root:Packages:NIST:VSANS:Globals:Protocols:" + newProtocol) ) == 1)
322                        //wave already exists
323                        DoAlert 1,"That name is already in use. Do you wish to overwrite the existing protocol?"
324                        if(V_Flag==1)
325                                notDone = 0
326                                newProto = 0
327                        else
328                                notDone = 1
329                        endif
330                else
331                        //name is good
332                        notDone = 0
333                Endif
334        while(notDone)
335       
336        //current data folder is  root:Packages:NIST:VSANS:Globals:Protocols
337        if(newProto)
338                Make/O/T/N=8 $("root:Packages:NIST:VSANS:Globals:Protocols:" + newProtocol)
339        Endif
340       
341        V_MakeProtocolFromPanel( $("root:Packages:NIST:VSANS:Globals:Protocols:" + newProtocol) )
342        String/G  root:Packages:NIST:VSANS:Globals:Protocols:gProtoStr = newProtocol
343       
344        //the data folder WAS changed above, this must be reset to root:
345        SetDatafolder root:
346End
347
348//function that does the guts of reading the panel controls and globals
349//to create the necessary text fields for a protocol
350//Wave/T w (input) is an empty text wave of 8 elements for the protocol
351//on output, w[] is filled with the protocol strings as needed from the panel
352//
353Function V_MakeProtocolFromPanel(w)
354        Wave/T w
355       
356        //construct the protocol text wave form the panel
357        //it is to be parsed by ExecuteProtocol() for the actual data reduction
358        PathInfo catPathName                    //this is where the files came from
359        String pathstr=S_path,tempStr,curList
360        Variable checked,ii,numItems
361       
362        //look for checkbox, then take each item in list and prepend the path
363        //w[0] = background
364        ControlInfo/W=V_ProtocolPanel prot_check
365        checked = V_value
366        if(checked)
367                //build the list
368                //just read the global
369                SVAR str=root:Packages:NIST:VSANS:Globals:Protocols:gBGD
370                if(cmpstr(str,"ask")==0)
371                        w[0] = str              //just ask
372                else
373                        tempstr = V_ParseRunNumberList(str)
374                        if(strlen(tempStr)==0)
375                                return(1)                               //error parsing list
376                        else
377                                w[0] = tempstr
378                                str = tempstr           //set the protocol and the global
379                        endif
380                endif
381        else
382                //none used - set textwave (and global?)
383                w[0] = "none"
384                String/G root:Packages:NIST:VSANS:Globals:Protocols:gBGD = "none"
385        endif
386       
387        //w[1] = empty
388        ControlInfo/W=V_ProtocolPanel prot_check_1
389        checked = V_value
390        if(checked)
391                //build the list
392                //just read the global
393                SVAR str=root:Packages:NIST:VSANS:Globals:Protocols:gEMP
394                if(cmpstr(str,"ask")==0)
395                        w[1] = str
396                else
397                        tempstr = V_ParseRunNumberList(str)
398                        if(strlen(tempStr)==0)
399                                return(1)
400                        else
401                                w[1] = tempstr
402                                str=tempStr
403                        endif
404                endif
405        else
406                //none used - set textwave (and global?)
407                w[1] = "none"
408                String/G root:Packages:NIST:VSANS:Globals:Protocols:gEMP = "none"
409        endif
410       
411        //w[2] = div file
412        ControlInfo/W=V_ProtocolPanel prot_check_2
413        checked = V_value
414        if(checked)
415                //build the list
416                //just read the global
417                SVAR str=root:Packages:NIST:VSANS:Globals:Protocols:gDIV
418                if(cmpstr(str,"ask")==0)
419                        w[2] = str
420                else
421                        tempStr = V_ParseRunNumberList(str)
422                        if(strlen(tempStr)==0)
423                                return(1)
424                        else
425                                w[2] = tempstr
426                                str=tempstr
427                        endif
428                endif
429        else
430                //none used - set textwave (and global?)
431                w[2] = "none"
432                String/G root:Packages:NIST:VSANS:Globals:Protocols:gDIV = "none"
433        endif
434       
435        //w[3] = mask file
436        ControlInfo/W=V_ProtocolPanel prot_check_3
437        checked = V_value
438        if(checked)
439                //build the list
440                //just read the global
441                SVAR str=root:Packages:NIST:VSANS:Globals:Protocols:gMASK
442                if(cmpstr(str,"ask")==0)
443                        w[3] = str
444                else
445                        tempstr = V_ParseRunNumberList(str)
446                        if(strlen(tempstr)==0)
447                                return(1)
448                        else
449                                w[3] = tempstr
450                                str = tempstr
451                        endif
452                endif
453        else
454                //none used - set textwave (and global?)
455                w[3] = "none"
456                String/G root:Packages:NIST:VSANS:Globals:Protocols:gMASK = "none"
457        endif
458       
459        //w[4] = abs parameters
460        ControlInfo/W=V_ProtocolPanel prot_check_9
461        checked = V_value
462        if(checked)
463                //build the list
464                //just read the global
465                SVAR str=root:Packages:NIST:VSANS:Globals:Protocols:gAbsStr
466                w[4] = str
467        else
468                //none used - set textwave (and global?)
469                w[4] = "none"
470                String/G root:Packages:NIST:VSANS:Globals:Protocols:gAbsStr = "none"
471        endif
472       
473        //w[5] = averaging choices
474        ControlInfo/W=V_ProtocolPanel prot_check_5                      //do the average?
475        checked = V_value
476        if(checked)
477                //just read the global
478                SVAR avestr=root:Packages:NIST:VSANS:Globals:Protocols:gAVE
479                w[5] = avestr
480        else
481                //none used - set textwave
482                w[5] = "AVTYPE=none;"
483        endif
484       
485        //w[6]
486        //work.DRK information
487        SVAR drkStr=root:Packages:NIST:VSANS:Globals:Protocols:gDRK
488        w[6] = drkStr
489       
490        //w[7]
491        //currently unused
492        w[7] = ""
493       
494        return(0)
495End
496
497
498//
499Function V_PickSAMButton(ctrlName) : ButtonControl
500        String ctrlName
501
502
503End
504
505
506
507
508//
509// TODO
510// -- identifying "intent" using V_get can be very slow, since it reads in each and every data file.
511//   grep is a lot faster, but there can be a lot of false "hits" unless I do it correctly.
512//
513//
514//      //grep through what text I can find in the VAX binary
515//      // Grep Note: the \\b sequences limit matches to a word boundary before and after
516//      // "boondoggle", so "boondoggles" and "aboondoggle" won't match.
517//      if(gRadioVal == 2)
518//              for(ii=0;ii<num;ii+=1)
519//                      item=StringFromList(ii, newList , ";")
520////                    Grep/P=catPathName/Q/E=("(?i)\\b"+match+"\\b") item
521//                      Grep/P=catPathName/Q/E=("(?i)"+match) item
522//                      if( V_value )   // at least one instance was found
523////                            Print "found ", item,ii
524//                              list += item + ";"
525//                      endif
526//              endfor
527//
528//              newList = list
529//      endif
530
531
532Function/S V_GetBGDList()
533
534        String match="BLOCKED BEAM"
535        String list = V_getFileIntentList(match,0)
536
537//      Printf "BGD files = %s\r",list 
538        return(list)
539end
540
541
542Function/S V_GetEMPList()
543
544        String match="EMPTY CELL"
545        String list = V_getFileIntentList(match,0)
546
547//      Printf "EMP files = %s\r",list 
548        return(list)
549end
550
551
552// testStr is the "intent" string, or grep string
553// method is the method to use to find the file
554// 0 = (default) is to use the file catalog (= fastest)
555// 1 = Grep (not terribly slow)
556// 2 = read every file (bad choice)
557//
558Function/S V_getFileIntentList(testStr,method)
559        String testStr
560        Variable method
561       
562        Variable ii,num
563        String list="",item="",fname,newList,intent
564
565        // read every file...
566        if(method == 2)
567                PathInfo catPathName
568                String path = S_path
569                newList = V_GetRawDataFileList()
570                num=ItemsInList(newList)
571               
572                for(ii=0;ii<num;ii+=1)
573                        item=StringFromList(ii, newList , ";")
574                        fname = path + item
575                        intent = V_getReduction_intent(fname)
576                        if(cmpstr(intent,testStr) == 0)
577                                list += item + ";"
578                        endif
579                endfor 
580        endif
581       
582        // use Grep
583        if(method == 1)
584                newList = V_GetRawDataFileList()
585                num=ItemsInList(newList)
586                for(ii=0;ii<num;ii+=1)
587                        item=StringFromList(ii, newList , ";")
588                        Grep/P=catPathName/Q/E=("(?i)"+testStr) item
589                        if( V_value )   // at least one instance was found
590        //                              Print "found ", item,ii
591                                list += item + ";"
592                        endif
593                endfor 
594       
595        else
596        // get the list from the file catalog
597       
598                WAVE/T fileNameW = root:Packages:NIST:VSANS:CatVSHeaderInfo:Filenames
599                WAVE/T intentW = root:Packages:NIST:VSANS:CatVSHeaderInfo:Intent
600               
601                Variable np = numpnts(intentW)          //fileNameW is LONGER - so don't use numpnts(fileWave)
602                for(ii=0;ii<np;ii+=1)
603                        if(cmpstr(intentW[ii],testStr)==0)
604                                list += fileNameW[ii] + ";"
605                        endif           
606                endfor
607               
608                List = SortList(List,";",0)
609        endif
610       
611        return(list)
612end
613
614//
615// TODO
616// -- decide on the way to locate the blocked beam files. Is the enumerated text of the intent
617//    sufficiently unique to locate the file?
618// -- can I use the Data Catalog waves to locate the files  - faster?
619//    (fails if the catalog has not been read in recently enough)
620//
621//
622Function/S V_PickBGDButton(ctrlName) : ButtonControl
623        String ctrlName
624
625        String list="",item="",fname,newList,intent
626        Variable ii,num
627       
628        PathInfo catPathName
629        String path = S_path
630
631        String match="BLOCKED BEAM"
632
633//s_tic()
634// get the list from the file catalog (=0.0007s)
635//     
636        WAVE/T fileNameW = root:Packages:NIST:VSANS:CatVSHeaderInfo:Filenames
637        WAVE/T intentW = root:Packages:NIST:VSANS:CatVSHeaderInfo:Intent
638       
639        Variable np = numpnts(intentW)
640        for(ii=0;ii<np;ii+=1)
641                if(cmpstr(intentW[ii],match)==0)
642                        list += fileNameW[ii] + ";"
643                endif           
644        endfor
645       
646        List = SortList(List,";",0)
647        Printf "BGD files = %s\r",list 
648//S_toc()       
649       
650       
651////S_tic()                     // from grep = 3.3s
652//      newList = V_GetRawDataFileList()
653//      num=ItemsInList(newList)
654//     
655////    for(ii=0;ii<num;ii+=1)
656////            item=StringFromList(ii, newList , ";")
657////            fname = path + item
658////            intent = V_getReduction_intent(fname)
659////            if(cmpstr(intent,"BLOCKED BEAM") == 0)
660////                    list += item + ";"
661////            endif
662////
663////    endfor
664//      list = ""       
665//      for(ii=0;ii<num;ii+=1)
666//              item=StringFromList(ii, newList , ";")
667//              Grep/P=catPathName/Q/E=("(?i)"+match) item
668//              if( V_value )   // at least one instance was found
669////                            Print "found ", item,ii
670//                      list += item + ";"
671//              endif
672//      endfor
673//             
674//             
675//      List = SortList(List,";",0)
676//      Printf "BGD files = %s\r",list
677////S_toc()
678       
679        return(list)
680End
681
682
683//
684Function/S V_PickEMPButton(ctrlName) : ButtonControl
685        String ctrlName
686        String list="",item="",fname,newList,intent
687        Variable ii,num
688       
689        PathInfo catPathName
690        String path = S_path
691
692        String match="EMPTY CELL"
693
694// get the list from the file catalog (=0.0007s)
695//     
696        WAVE/T fileNameW = root:Packages:NIST:VSANS:CatVSHeaderInfo:Filenames
697        WAVE/T intentW = root:Packages:NIST:VSANS:CatVSHeaderInfo:Intent
698       
699        Variable np = numpnts(intentW)
700        for(ii=0;ii<np;ii+=1)
701                if(cmpstr(intentW[ii],match)==0)
702                        list += fileNameW[ii] + ";"
703                endif           
704        endfor
705       
706        List = SortList(List,";",0)
707        Printf "EMP files = %s\r",list 
708
709
710//     
711//      newList = V_GetRawDataFileList()
712//      num=ItemsInList(newList)
713//     
714////    for(ii=0;ii<num;ii+=1)
715////            item=StringFromList(ii, newList , ";")
716////            fname = path + item
717////            intent = V_getReduction_intent(fname)
718////            if(cmpstr(intent,"EMPTY CELL") == 0)
719////                    list += item + ";"
720////            endif
721////
722////    endfor
723//
724//      for(ii=0;ii<num;ii+=1)
725//              item=StringFromList(ii, newList , ";")
726//              Grep/P=catPathName/Q/E=("(?i)"+match) item
727//              if( V_value )   // at least one instance was found
728////                            Print "found ", item,ii
729//                      list += item + ";"
730//              endif
731//
732//      endfor
733//             
734//      List = SortList(List,";",0)
735//      Printf "EMP files = %s\r",list
736       
737        return(list)
738End
739
740
741Function/S V_GetDIVList()
742
743        String list="",item="",fname,newList,intent
744        Variable ii,num
745       
746        PathInfo catPathName
747        String path = S_path
748       
749        newList = V_Get_NotRawDataFileList()
750        num=ItemsInList(newList)
751       
752//      for(ii=0;ii<num;ii+=1)
753//              item=StringFromList(ii, newList , ";")
754//              fname = path + item
755//              intent = V_getReduction_intent(fname)
756//              if(cmpstr(intent,"SENSITIVITY") == 0)
757//                      list += item + ";"
758//              endif
759//
760//      endfor
761
762        String match="DIV"
763        for(ii=0;ii<num;ii+=1)
764                item=StringFromList(ii, newList , ";")
765                Grep/P=catPathName/Q/E=("(?i)\\b"+match+"\\b") item
766//              Grep/P=catPathName/Q/E=("(?i)"+match) item
767                if( V_value )   // at least one instance was found
768//                              Print "found ", item,ii
769                        if(strsearch(item,"pxp",0,2) == -1)             //does NOT contain .pxp (the current experiment will be a match)
770                                list += item + ";"
771                        endif
772                endif
773        endfor
774               
775        List = SortList(List,";",0)
776
777        return(list)
778end
779
780// TODO
781// -- find proper way to search for these files
782// -- they *may* be written to the file header(reduction block)
783// -- or grep for VSANS_DIV (in the title)
784Function/S V_PickDIVButton(ctrlName) : ButtonControl
785        String ctrlName
786        String list="",item="",fname,newList,intent
787        Variable ii,num
788       
789        PathInfo catPathName
790        String path = S_path
791       
792        newList = V_Get_NotRawDataFileList()
793        num=ItemsInList(newList)
794       
795//      for(ii=0;ii<num;ii+=1)
796//              item=StringFromList(ii, newList , ";")
797//              fname = path + item
798//              intent = V_getReduction_intent(fname)
799//              if(cmpstr(intent,"SENSITIVITY") == 0)
800//                      list += item + ";"
801//              endif
802//
803//      endfor
804
805        String match="VSANS_DIV"                //this is part of the title of a VSANS DIV file
806        for(ii=0;ii<num;ii+=1)
807                item=StringFromList(ii, newList , ";")
808                Grep/P=catPathName/Q/E=("(?i)\\b"+match+"\\b") item
809//              Grep/P=catPathName/Q/E=("(?i)"+match) item
810                if( V_value )   // at least one instance was found
811//                              Print "found ", item,ii
812                                list += item + ";"
813                endif
814
815        endfor
816               
817        List = SortList(List,";",0)
818        Printf "DIV files = %s\r",list
819       
820        return(list)
821
822End
823
824Function/S V_GetMSKList()
825
826        String list="",item="",fname,newList,intent
827        Variable ii,num
828       
829        PathInfo catPathName
830        String path = S_path
831       
832        newList = V_Get_NotRawDataFileList()
833        num=ItemsInList(newList)
834       
835//      for(ii=0;ii<num;ii+=1)
836//              item=StringFromList(ii, newList , ";")
837//              fname = path + item
838//              intent = V_getReduction_intent(fname)
839//              if(cmpstr(intent,"SENSITIVITY") == 0)
840//                      list += item + ";"
841//              endif
842//
843//      endfor
844
845        String match="MASK"             // this is part of the title of a VSANS MASK file
846        for(ii=0;ii<num;ii+=1)
847                item=StringFromList(ii, newList , ";")
848                Grep/P=catPathName/Q/E=("(?i)\\b"+match+"\\b") item
849//              Grep/P=catPathName/Q/E=("(?i)"+match) item
850                if( V_value )   // at least one instance was found
851//                              Print "found ", item,ii
852                        if(strsearch(item,"pxp",0,2) == -1)             //does NOT contain .pxp (the current experiment will be a match)
853                                list += item + ";"
854                        endif
855                endif
856
857        endfor
858               
859        List = SortList(List,";",0)
860
861        return(list)
862end
863
864
865//
866// TODO
867// -- find proper way to search for these files
868// -- they *may* be written to the file header(reduction block)
869// -- or grep for VSANS_MASK (in the title)
870Function/S V_PickMASKButton(ctrlName) : ButtonControl
871        String ctrlName
872        String list="",item="",fname,newList,intent
873        Variable ii,num
874       
875        PathInfo catPathName
876        String path = S_path
877       
878        newList = V_Get_NotRawDataFileList()
879        num=ItemsInList(newList)
880       
881//      for(ii=0;ii<num;ii+=1)
882//              item=StringFromList(ii, newList , ";")
883//              fname = path + item
884//              intent = V_getReduction_intent(fname)
885//              if(cmpstr(intent,"MASK") == 0)
886//                      list += item + ";"
887//              endif
888//
889//      endfor
890
891        String match="MASK"
892        for(ii=0;ii<num;ii+=1)
893                item=StringFromList(ii, newList , ";")
894                Grep/P=catPathName/Q/E=("(?i)\\b"+match+"\\b") item
895//              Grep/P=catPathName/Q/E=("(?i)"+match) item
896                if( V_value )   // at least one instance was found
897//                              Print "found ", item,ii
898                        list += item + ";"
899                endif
900
901        endfor
902               
903        List = SortList(List,";",0)
904        Printf "MASK files = %s\r",list
905       
906        return(list)   
907
908       
909End
910
911//button action function to reduce one file with the information specified on
912//the panel
913//a temporary protocol is created, even if the fields correspond to a named protocol
914//(only the protocol wave values are written to the data header, but the name is written to the
915//schematic - this could cause some unwanted confusion)
916//
917//if a sample file(s) is selected, only that file(s) will be reduced
918//if no file is selected, the user will be prompted with a standard open
919//dialog to select sample data file(s)
920//
921Function V_ReduceOneButton(ctrlName) : ButtonControl
922        String ctrlName
923
924        //parse the information on the panel and assign to tempProtocol wave (in protocol folder)
925        //and execute
926        String temp="root:Packages:NIST:VSANS:Globals:Protocols:tempProtocol"
927        Wave/T w=$temp
928        Variable ii=0,num=8
929        do
930                w[ii] = ""
931                ii+=1
932        while(ii<num)
933       
934        V_MakeProtocolFromPanel(w)
935
936        //the "current" protocol is the "tempProtocol" that was parsed from the panel input
937        //set the global, so that the data writing routine can find the protocol wave (fatal otherwise)
938        String/G root:Packages:NIST:VSANS:Globals:Protocols:gProtoStr="tempProtocol"
939       
940        PathInfo catPathName                    //this is where the files came from
941        String pathstr=S_path,samStr
942       
943        //take the string from the panel
944        SVAR tempStr = root:Packages:NIST:VSANS:Globals:Protocols:gSAM
945       
946        if( (strlen(tempStr) == 0) || (cmpstr(tempStr,"ask")==0) )
947                //let user select the files
948                tempStr="ask"
949                V_ExecuteProtocol(temp,tempStr)
950                return(0)
951        Else
952                //parse the list of numbers
953                //send only the filenames, without paths
954                samStr = V_ParseRunNumberList(tempStr)
955                If(strlen(samStr)==0)
956                        DoAlert 0,"The SAM file number cound not be interpreted. Please enter a valid run number or filename"
957                        return(1)
958                endif
959                tempStr=samStr          //reset the global
960                V_ExecuteProtocol(temp,samStr)
961                return(0)
962        endif
963End
964
965
966//button action function will prompt user for absolute scaling parameters
967//either from an empty beam file or by manually entering the 4 required values
968//uses the same function and shared global string as the questionnare form of reduction
969//in "protocol.ipf" - the string is root:Packages:NIST:VSANS:Globals:Protocols:gAbsStr
970//
971Function V_SetABSParamsButton(ctrlName) : ButtonControl
972        String ctrlName
973
974        //will prompt for a list of ABS parameters (4) through a global string variable
975       
976        Execute "V_AskForAbsoluteParams_Quest()"
977       
978End
979
980
981
982//the panel recreation macro
983//
984Window V_ProtocolPanel()
985        PauseUpdate; Silent 1           // building window...
986        NewPanel /W=(553,48,851,500) /K=1 as "VSANS Reduction Protocol"
987        ModifyPanel cbRGB=(56589,50441,50159), fixedSize=1
988        SetDrawLayer UserBack
989        DrawLine 2,25,300,25
990        DrawLine 2,69,300,69
991        DrawLine 2,120,300,120
992        DrawLine 2,169,300,169
993        DrawLine 2,217,300,217
994        DrawLine 2,262,300,262
995        DrawLine 2,357,300,357
996        DrawLine 2,379,300,379
997        DrawLine 2,308,300,308
998
999//
1000        Button button_help,pos={260,2},size={25,20},proc=V_ShowProtoHelp,title="?"
1001        Button button_help,help={"Show the help file for setting up a reduction protocol"}
1002        Button button_quest,pos={20,2},size={150,20},proc=V_ProtocolQuestionnaire,title="Questions"
1003        Button button_quest,help={"Run through the questionnaire for setting up a reduction protocol"}
1004       
1005        PopupMenu popup_bkg,pos={85,76},size={51,23},proc=BKGFilePopMenuProc
1006        PopupMenu popup_bkg,mode=1,value= #"V_getBGDList()"
1007        PopupMenu popup_emp,pos={85,125},size={51,23},proc=EMPFilePopMenuProc
1008        PopupMenu popup_emp,mode=1,value= #"V_getEMPList()"
1009        PopupMenu popup_div,pos={85,175},size={51,23},proc=DIVFilePopMenuProc
1010        PopupMenu popup_div,mode=1,value= #"V_getDIVList()"
1011        PopupMenu popup_msk,pos={85,268},size={51,23},proc=MSKFilePopMenuProc
1012        PopupMenu popup_msk,mode=1,value= #"V_getMSKList()"     
1013               
1014        CheckBox prot_check,pos={5,75},size={74,14},title="Background"
1015        CheckBox prot_check,help={"If checked, the specified background file will be included in the data reduction. If the file name is \"ask\", then the user will be prompted for the file"}
1016        CheckBox prot_check,value= 1
1017        CheckBox prot_check_1,pos={5,127},size={71,14},title="Empty Cell"
1018        CheckBox prot_check_1,help={"If checked, the specified empty cell file will be included in the data reduction. If the file name is \"ask\", then the user will be prompted for the file"}
1019        CheckBox prot_check_1,value= 1
1020        CheckBox prot_check_2,pos={6,175},size={72,14},title="Sensitivity"
1021        CheckBox prot_check_2,help={"If checked, the specified detector sensitivity file will be included in the data reduction. If the file name is \"ask\", then the user will be prompted for the file"}
1022        CheckBox prot_check_2,value= 1
1023        CheckBox prot_check_3,pos={9,268},size={43,14},title="Mask"
1024        CheckBox prot_check_3,help={"If checked, the specified mask file will be included in the data reduction. If the file name is \"ask\", then the user will be prompted for the file"}
1025        CheckBox prot_check_3,value= 1
1026        CheckBox prot_check_4,pos={5,30},size={53,14},title="Sample"
1027        CheckBox prot_check_4,help={"If checked, the specified sample file will be included in the data reduction. If the file name is \"ask\", then the user will be prompted for the file"}
1028        CheckBox prot_check_4,value= 1
1029        CheckBox prot_check_5,pos={6,311},size={56,14},title="Average"
1030        CheckBox prot_check_5,help={"If checked, the specified averaging will be performed at the end of the data reduction."}
1031        CheckBox prot_check_5,value= 1 
1032        CheckBox prot_check_9,pos={7,222},size={59,14},title="Absolute"
1033        CheckBox prot_check_9,help={"If checked, absolute calibration will be included in the data reduction. If the parameter list is \"ask\", then the user will be prompted for absolue parameters"}
1034        CheckBox prot_check_9,value= 1
1035       
1036
1037//      Button pick_sam,pos={214,28},size={70,20},proc=V_PickSAMButton,title="set SAM"
1038//      Button pick_sam,help={"This button will set the file selected in the File Catalog table to be the sample file"}
1039//      Button pick_bgd,pos={214,75},size={70,20},proc=V_PickBGDButton,title="set BGD"
1040//      Button pick_bgd,help={"This button will set the file selected in the File Catalog table to be the background file."}
1041//      Button pick_emp,pos={214,125},size={70,20},proc=V_PickEMPButton,title="set EMP"
1042//      Button pick_emp,help={"This button will set the file selected in the File Catalog table to be the empty cell file."}
1043//      Button pick_DIV,pos={214,173},size={70,20},proc=V_PickDIVButton,title="set DIV"
1044//      Button pick_DIV,help={"This button will set the file selected in the File Catalog table to be the sensitivity file."}
1045        Button pick_ABS,pos={214,220},size={70,20},proc=V_SetABSParamsButton,title="set ABS"
1046        Button pick_ABS,help={"This button will prompt the user for absolute scaling parameters"}       
1047//      Button pick_MASK,pos={214,266},size={70,20},proc=V_PickMASKButton,title="set MASK"
1048//      Button pick_MASK,help={"This button will set the file selected in the File Catalog table to be the mask file."}
1049
1050
1051        Button pick_AVE,pos={108,313},size={150,20},proc=V_SetAverageParamsButtonProc,title="set AVERAGE params"
1052        Button pick_AVE,help={"Prompts the user for the type of 1-D averaging to perform, as well as saving options"}
1053       
1054        SetVariable samStr,pos={6,50},size={250,15},title="file:"
1055        SetVariable samStr,help={"Filename of the sample file(s) to be used in the data reduction"}
1056        SetVariable samStr,limits={-Inf,Inf,0},value= root:Packages:NIST:VSANS:Globals:Protocols:gSAM           
1057        SetVariable bgdStr,pos={7,98},size={250,15},title="file:"
1058        SetVariable bgdStr,help={"Filename of the background file(s) to be used in the data reduction"}
1059        SetVariable bgdStr,limits={-Inf,Inf,0},value= root:Packages:NIST:VSANS:Globals:Protocols:gBGD
1060        SetVariable empStr,pos={8,148},size={250,15},title="file:"
1061        SetVariable empStr,help={"Filename of the empty cell file(s) to be used in the data reduction"}
1062        SetVariable empStr,limits={-Inf,Inf,0},value= root:Packages:NIST:VSANS:Globals:Protocols:gEMP
1063        SetVariable divStr,pos={9,197},size={250,15},title="file:"
1064        SetVariable divStr,help={"Filename of the detector sensitivity file to be used in the data reduction"}
1065        SetVariable divStr,limits={-Inf,Inf,0},value= root:Packages:NIST:VSANS:Globals:Protocols:gDIV
1066        SetVariable maskStr,pos={9,289},size={250,15},title="file:"
1067        SetVariable maskStr,help={"Filename of the mask file to be used in the data reduction"}
1068        SetVariable maskStr,limits={-Inf,Inf,0},value= root:Packages:NIST:VSANS:Globals:Protocols:gMASK
1069        SetVariable absStr,pos={7,243},size={250,15},title="parameters:"
1070        SetVariable absStr,help={"Keyword-string of values necessary for absolute scaling of data. Remaining parameters are taken from the sample file."}
1071        SetVariable absStr,limits={-Inf,Inf,0},value= root:Packages:NIST:VSANS:Globals:Protocols:gAbsStr
1072        SetVariable aveStr,pos={9,336},size={250,15},title="parameters:"
1073        SetVariable aveStr,help={"Keyword-string of choices used for averaging and saving the 1-D data files"}
1074        SetVariable aveStr,limits={-Inf,Inf,0},value= root:Packages:NIST:VSANS:Globals:Protocols:gAVE
1075
1076
1077
1078
1079
1080
1081
1082       
1083        //only show DRK if user wants to see it
1084        //if global = 1,then show => set disable = 0
1085//      CheckBox prot_check_6,pos={6,363},size={113,14},proc=DrkCheckProc,title="Use DRK correction"
1086//      CheckBox prot_check_6,help={"If checked, the selected file will be used for DRK correction. Typically this is NOT checked"}
1087//      CheckBox prot_check_6,value= 0,disable = (!root:Packages:NIST:gAllowDRK)
1088//      SetVariable drkStr,pos={120,363},size={150,15},title="."
1089//      SetVariable drkStr,help={"DRK detector count file"},disable = (!root:Packages:NIST:gAllowDRK)
1090//      SetVariable drkStr,limits={-Inf,Inf,0},value= root:Packages:NIST:VSANS:Globals:Protocols:gDRK
1091
1092        Button export_button, size={60,20},pos={125,384},title="Export",proc=V_ExportProtocol
1093        Button export_button, help={"Exports the protocol to disk for Importing into another experiment"}
1094        Button import_button, size={60,20},pos={125,406},title="Import",proc=V_ImportProtocol
1095        Button import_button,help={"Imports a protocol from disk for use in this experiment"}
1096        Button recallProt,pos={7,406},size={107,20},proc=V_RecallProtocolButton,title="Recall Protocol"
1097        Button recallProt,help={"Resets the panel to the file choices in  a previously saved protocol"}
1098        Button del_protocol,pos={7,428},size={110,20},proc=V_DeleteProtocolButton,title="Delete Protocol"
1099        Button del_protocol,help={"Use this to delete a previously saved protocol."}
1100        Button done_protocol,pos={225,428},size={45,20},proc=V_DoneProtocolButton,title="Done"
1101        Button done_protocol,help={"This button will close the panel. The panel can be recalled at any time from the SANS menu."}
1102        Button saveProtocol,pos={7,384},size={100,20},proc=V_SaveProtocolButton,title="Save Protocol"
1103        Button saveProtocol,help={"Saves the cerrent selections in the panel to a protocol which can be later recalled"}
1104        Button ReduceOne,pos={194,384},size={100,20},proc=V_ReduceOneButton,title="Reduce A File"
1105        Button ReduceOne,help={"Using the panel selections, the specified sample file will be reduced. If none is specified, the user will be prompted for a sample file"}
1106
1107EndMacro
1108
1109Function BKGFilePopMenuProc(pa) : PopupMenuControl
1110        STRUCT WMPopupAction &pa
1111
1112        switch( pa.eventCode )
1113                case 2: // mouse up
1114                        Variable popNum = pa.popNum
1115                        String popStr = pa.popStr
1116                        SVAR tempStr = root:Packages:NIST:VSANS:Globals:Protocols:gBGD
1117                        tempStr = popStr
1118                        break
1119                case -1: // control being killed
1120                        break
1121        endswitch
1122
1123        return 0
1124End
1125
1126Function EMPFilePopMenuProc(pa) : PopupMenuControl
1127        STRUCT WMPopupAction &pa
1128
1129        switch( pa.eventCode )
1130                case 2: // mouse up
1131                        Variable popNum = pa.popNum
1132                        String popStr = pa.popStr
1133                        SVAR tempStr = root:Packages:NIST:VSANS:Globals:Protocols:gEMP
1134                        tempStr = popStr
1135                        break
1136                case -1: // control being killed
1137                        break
1138        endswitch
1139
1140        return 0
1141End
1142
1143Function DIVFilePopMenuProc(pa) : PopupMenuControl
1144        STRUCT WMPopupAction &pa
1145
1146        switch( pa.eventCode )
1147                case 2: // mouse up
1148                        Variable popNum = pa.popNum
1149                        String popStr = pa.popStr
1150                        SVAR tempStr = root:Packages:NIST:VSANS:Globals:Protocols:gDIV
1151                        tempStr = popStr
1152                        break
1153                case -1: // control being killed
1154                        break
1155        endswitch
1156
1157        return 0
1158End
1159
1160Function MSKFilePopMenuProc(pa) : PopupMenuControl
1161        STRUCT WMPopupAction &pa
1162
1163        switch( pa.eventCode )
1164                case 2: // mouse up
1165                        Variable popNum = pa.popNum
1166                        String popStr = pa.popStr
1167                        SVAR tempStr = root:Packages:NIST:VSANS:Globals:Protocols:gMASK
1168                        tempStr = popStr
1169                        break
1170                case -1: // control being killed
1171                        break
1172        endswitch
1173
1174        return 0
1175End
1176
1177////activated when user checks/unchecks the box
1178////either prompts for a file using a standard dialog, or removes the current file
1179//Function V_DrkCheckProc(ctrlName,checked) : CheckBoxControl
1180//      String ctrlName
1181//      Variable checked                //Desired state, not previous state
1182//
1183//      SVAR drkStr=root:Packages:NIST:VSANS:Globals:Protocols:gDRK
1184//      if(checked==1)
1185//              //print "Unchecked on call"
1186//              //this just asks for the filename, doesn't open the file
1187//              String msgStr="Select the DRK file",fullPath="",fileStr=""
1188//              Variable refnum
1189//             
1190//              Open/D/R/T="????"/M=(msgStr)/P=catPathName refNum
1191//              fullPath = S_FileName           //fname is the full path
1192//              if(cmpstr(fullpath,"")==0)
1193//                      //user cancelled
1194//                      CheckBox prot_check_6,value=0           //keep box unchecked
1195//                      return(0)
1196//              Endif
1197//              fileStr=V_GetFileNameFromPathNoSemi(fullPath)
1198//              //Print fileStr
1199//              //update the global string
1200//              drkStr = ReplaceStringByKey("DRK",drkStr,fileStr,"=",",")
1201//              drkStr = ReplaceNumberByKey("DRKMODE", drkStr, 10 ,"=",",")
1202//      else
1203//              //print "checked on call"
1204//              drkStr="DRK=none,DRKMODE=0,"            //change the global
1205//      endif
1206//     
1207//End
1208
1209
1210Function V_ShowProtoHelp(ctrlName) : ButtonControl
1211        String ctrlName
1212        DisplayHelpTopic/Z/K=1 "VSANS Data Reduction Tutorial[VSANS Reduction Protocol]"
1213        if(V_flag !=0)
1214                DoAlert 0,"The VSANS Data Reduction Tutorial Help file could not be found"
1215        endif
1216End
1217
1218//button action procedure to get the type of average requested by the user
1219//presented as a missing parameter dialog, which is really user-UN-friendly
1220//and will need to be re-thought. Defaults of dialog are set for normal
1221//circular average, so typically click "continue" and proceed
1222//
1223Function V_SetAverageParamsButtonProc(ctrlName) : ButtonControl
1224        String ctrlName
1225       
1226//      Execute "V_GetAvgInfo_Full()"
1227        Execute "V_GetAvgInfo()"
1228       
1229        //set the global string
1230        SVAR tempStr = root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr
1231        String/G root:Packages:NIST:VSANS:Globals:Protocols:gAVE = tempStr
1232
1233End
1234
1235// TODO
1236// -- this is a trimmed down version of the "full" set of averaging options
1237//    add to this as needed to add in functionality
1238//
1239//procedure called by protocol panel to ask user for average type choices
1240// somewhat confusing and complex, but may be as good as it gets.
1241//
1242//Proc V_GetAvgInfo(av_typ,autoSave,autoName,autoPlot,side,phi,dphi,width,QCtr,QDelta)
1243Proc V_GetAvgInfo(av_typ,autoSave,autoName,binType)
1244        String av_typ,autoSave,AutoName,binType
1245//      Variable phi=0,dphi=10,width=10,Qctr = 0.01,qDelta=10
1246
1247//      Prompt av_typ, "Type of Average",popup,"Circular;Sector;Rectangular;Annular;2D_ASCII;QxQy_ASCII;PNG_Graphic;Sector_PlusMinus;"
1248        Prompt av_typ, "Type of Average",popup,"Circular;"
1249
1250// comment out above line in DEMO_MODIFIED version, and uncomment the line below (to disable PNG save)
1251//      Prompt av_typ, "Type of Average",popup,"Circular;Sector;Rectangular;Annular;2D_ASCII;QxQy_ASCII"
1252        Prompt autoSave,"Save files to disk?",popup,"Yes;No"
1253        Prompt autoName,"Auto-Name files?",popup,"Auto;Manual"
1254//      Prompt autoPlot,"Plot the averaged Data?",popup,"Yes;No"
1255//      Prompt side,"Include detector halves?",popup,"both;right;left"
1256//      Prompt phi,"Orientation Angle (-90,90) degrees (Rectangular or Sector)"
1257//      Prompt dphi, "Azimuthal range (0,45) degrees (Sector only)"
1258//      Prompt width, "Width of Rectangular average (1,128)"
1259//      Prompt Qctr, "q-value of center of annulus"
1260//      Prompt Qdelta,"Pixel width of annulus"
1261        Prompt binType,"Binning Type?",popup,"One;Two;Four;Slit Mode;"
1262
1263        //assign results of dialog to key=value string, semicolon separated
1264        //do only what is necessary, based on av_typ
1265        String/G root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr=""
1266
1267        // TODO:
1268        // hard wired value
1269        String autoPlot = "No"
1270       
1271               
1272        // all averages need these values
1273        root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "AVTYPE=" + av_typ + ";"
1274        root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "SAVE=" + autoSave + ";"
1275        root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "NAME=" + autoName + ";"
1276        root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "PLOT=" + autoPlot + ";"
1277        root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "BINTYPE=" + binType + ";"
1278       
1279//      if(cmpstr(av_typ,"Sector")==0 || cmpstr(av_typ,"Sector_PlusMinus")==0)
1280//              root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "SIDE=" + side + ";"
1281//              root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "PHI=" + num2str(phi) + ";"
1282//              root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "DPHI=" + num2str(dphi) + ";"
1283//      Endif
1284//     
1285//      if(cmpstr(av_typ,"Rectangular")==0)
1286//              root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "SIDE=" + side + ";"
1287//              root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "PHI=" + num2str(phi) + ";"
1288//              root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "WIDTH=" + num2str(width) + ";"
1289//      Endif
1290//     
1291//      if(cmpstr(av_typ,"Annular")==0)
1292//              root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "QCENTER=" + num2str(QCtr) + ";"
1293//              root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "QDELTA=" + num2str(QDelta) + ";"
1294//      Endif
1295End
1296
1297
1298// TODO
1299// -- this is the original(SANS) version, and needs to be updated for VSANS as the averaging options are
1300//    worked out
1301//
1302//procedure called by protocol panel to ask user for average type choices
1303// somewhat confusing and complex, but may be as good as it gets.
1304//
1305Proc V_GetAvgInfo_Full(av_typ,autoSave,autoName,autoPlot,side,phi,dphi,width,QCtr,QDelta)
1306        String av_typ,autoSave,AutoName,autoPlot,side
1307        Variable phi=0,dphi=10,width=10,Qctr = 0.01,qDelta=10
1308        Prompt av_typ, "Type of Average",popup,"Circular;Sector;Rectangular;Annular;2D_ASCII;QxQy_ASCII;PNG_Graphic;Sector_PlusMinus;"
1309// comment out above line in DEMO_MODIFIED version, and uncomment the line below (to disable PNG save)
1310//      Prompt av_typ, "Type of Average",popup,"Circular;Sector;Rectangular;Annular;2D_ASCII;QxQy_ASCII"
1311        Prompt autoSave,"Save files to disk?",popup,"Yes;No"
1312        Prompt autoName,"Auto-Name files?",popup,"Auto;Manual"
1313        Prompt autoPlot,"Plot the averaged Data?",popup,"Yes;No"
1314        Prompt side,"Include detector halves?",popup,"both;right;left"
1315        Prompt phi,"Orientation Angle (-90,90) degrees (Rectangular or Sector)"
1316        Prompt dphi, "Azimuthal range (0,45) degrees (Sector only)"
1317        Prompt width, "Width of Rectangular average (1,128)"
1318        Prompt Qctr, "q-value of center of annulus"
1319        Prompt Qdelta,"Pixel width of annulus"
1320
1321        //assign results of dialog to key=value string, semicolon separated
1322        //do only what is necessary, based on av_typ
1323        String/G root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr=""
1324       
1325        // all averages need these four values
1326        root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "AVTYPE=" + av_typ + ";"
1327        root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "SAVE=" + autoSave + ";"
1328        root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "NAME=" + autoName + ";"
1329        root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "PLOT=" + autoPlot + ";"
1330       
1331        if(cmpstr(av_typ,"Sector")==0 || cmpstr(av_typ,"Sector_PlusMinus")==0)
1332                root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "SIDE=" + side + ";"
1333                root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "PHI=" + num2str(phi) + ";"
1334                root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "DPHI=" + num2str(dphi) + ";"
1335        Endif
1336       
1337        if(cmpstr(av_typ,"Rectangular")==0)
1338                root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "SIDE=" + side + ";"
1339                root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "PHI=" + num2str(phi) + ";"
1340                root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "WIDTH=" + num2str(width) + ";"
1341        Endif
1342       
1343        if(cmpstr(av_typ,"Annular")==0)
1344                root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "QCENTER=" + num2str(QCtr) + ";"
1345                root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr += "QDELTA=" + num2str(QDelta) + ";"
1346        Endif
1347End
1348
1349
1350//prompts the user to pick a previously created protocol from a popup list
1351//of given the option to create a new protocol
1352//the chosen protocol is passed back to the calling procedure by a global string
1353//the popup is presented as a missing parameter dialog (called with empty parameter list)
1354//
1355Proc V_PickAProtocol(protocol)
1356        String Protocol
1357        Prompt Protocol "Pick A Protocol",popup, WaveList("*",";","TEXT:1")
1358       
1359        String/G  root:Packages:NIST:VSANS:Globals:Protocols:gProtoStr = protocol
1360End
1361
1362Proc V_DeleteAProtocol(protocol)
1363        String Protocol
1364//      Prompt Protocol "Delete A Protocol",popup, WaveList("*",";","TEXT:1")
1365        Prompt Protocol "Delete A Protocol",popup, V_DeletableProtocols()
1366
1367        String/G  root:Packages:NIST:VSANS:Globals:Protocols:gProtoStr = protocol
1368End
1369
1370Function/S V_DeletableProtocols()
1371        String list=WaveList("*",";","TEXT:1")
1372
1373        list= RemoveFromList("Base", list  , ";")
1374        list= RemoveFromList("DoAll", list  , ";")
1375        list= RemoveFromList("CreateNew", list  , ";")
1376        list= RemoveFromList("tempProtocol", list  , ";")
1377        if(cmpstr(list,"")==0)
1378                list = "_no_protocols_;"
1379        endif
1380       
1381        return(list)
1382End
1383
1384//missing paramater dialog to solicit user for a waveStr for the protocol
1385//about to be created
1386//name is passed back as a global string and calling procedure is responsible for
1387//checking for wave conflicts and valid names
1388//
1389Proc V_AskForName(protocol)
1390        String Protocol
1391        Prompt Protocol "Enter a new name for your protocol (no extension)"
1392       
1393        String/G  root:Packages:NIST:VSANS:Globals:Protocols:gNewStr = protocol
1394End
1395
1396//this is a lengthy procedure for sequentially polling the user about what data
1397//reduction steps they want to be performed during the protocol
1398//ensures that a valid protocol name was chosen, then fills out each "item"
1399//(6 total) needed for reduction
1400//it the user cancels at any point, the partial protocol will be deleted
1401//
1402Function V_ProtocolQuestionnaire(ctrlName)
1403        String ctrlName
1404
1405        String filename,cmd
1406        Variable notDone,refnum
1407       
1408        //prompt for name of new protocol wave to save
1409        do
1410                Execute "V_AskForName()"
1411                SVAR newProtoStr = root:Packages:NIST:VSANS:Globals:Protocols:gNewStr
1412               
1413                //make sure it's a valid IGOR name
1414                newProtoStr = CleanupName(newProtoStr,0)        //strict naming convention
1415                String/G root:Packages:NIST:VSANS:Globals:Protocols:gNewStr=newProtoStr         //reassign, if changed
1416                //Print "newProtoStr = ",newProtoStr
1417               
1418                SetDataFolder root:Packages:NIST:VSANS:Globals:Protocols
1419                if(WaveExists( $("root:Packages:NIST:VSANS:Globals:Protocols:" + newProtoStr) ) == 1)
1420                        //wave already exists
1421                        DoAlert 0,"that name is already in use. Please pick a new name"
1422                        notDone = 1
1423                else
1424                        //name is  good
1425                        notDone = 0
1426                Endif
1427        while(notDone)
1428       
1429        //Print "protocol questionnaire is "+newProtocol
1430       
1431        //make a new text wave (8 points) and fill it in, in response to questions
1432        SetDataFolder root:Packages:NIST:VSANS:Globals:Protocols //(redundant - full wave specs are used)
1433        Make/O/T/N=8 $("root:Packages:NIST:VSANS:Globals:Protocols:" + newProtoStr)
1434        Wave/T newProtocol = $("root:Packages:NIST:VSANS:Globals:Protocols:" + newProtoStr)
1435       
1436        //ask the questions
1437        /////
1438        //*****Multiple files in these lists are full paths/filenames which may or may not
1439        //have semicolon version numbers in the filename. Therefore, construct the list with
1440        //COMMAS as separators - to avoid messy parsing
1441        ///////
1442       
1443        String fileFilters = "VSANS Data Files (*.ngv,*.h5):.ngv,.h5;"
1444//      fileFilters += "HTML Files (*.htm,*.html):.htm,.html;"
1445        fileFilters += "All Files:.*;"
1446//////////////////////////
1447        String drkStr="",fileStr=""
1448       
1449       
1450//////////////////////////     
1451//      DoAlert 1,"Do you want to correct your data for DRK (beam off) counts?"
1452//      if(V_flag == 1)         //1=yes
1453//              //prompt for DRK  file, but don't actually open it (/D flag)
1454//              Open/D/R/F=fileFilters/M="Select the DRK file"/P=catPathName refnum
1455//              //check for cancel
1456//              if(strlen(S_filename)==0)
1457//                      //user cancelled, abort
1458//                      KillWaves/Z newProtocol
1459//                      SetDataFolder root:
1460//                      Abort "Incomplete protocol has been deleted"
1461//              Endif
1462//              //assign filename (just the name) to [6]
1463//              fileStr = V_GetFileNameFromPathNoSemi(S_filename)
1464//              drkStr = "DRK=none,DRKMODE=0,"
1465//              drkStr = ReplaceStringByKey("DRK",drkStr,fileStr,"=",",")
1466//              drkStr = ReplaceNumberByKey("DRKMODE", drkStr, 10 ,"=",",")     
1467//              newProtocol[6] = drkStr
1468//      else
1469//              //no Work.DRK desired
1470//              newProtocol[6] = "DRK=none,DRKMODE=0,"
1471//      Endif
1472//     
1473////////////
1474       
1475       
1476// TODO:
1477// -- there is a lag in the V_pick() routine as it greps through all of the files. Several seconds even when there
1478//   are 20-ish files. Pre-search for the strings? When to refresh them for new data?   
1479/////////////////
1480        DoAlert 1,"Do you want to subtract background from your data?"
1481        if(V_flag == 1)         //1=yes
1482               
1483                Prompt filename,"BKG File",popup,V_PickBGDButton("")
1484                DoPrompt "Select File",filename
1485                if (V_Flag)
1486                        return 0                                                                        // user canceled
1487                endif
1488//              //assign filename to [0]
1489                newProtocol[0] = V_GetFileNameFromPathNoSemi(fileName)
1490       
1491               
1492// OLD way, using an open file dialog   
1493// and allowing for multiple files to be added together
1494//     
1495//              //prompt for background file, but don't actually open it (/D flag)
1496//              Open/D/R/F=fileFilters/M="Select the Background data file"/P=catPathName refnum
1497//              //check for cancel
1498//              if(strlen(S_filename)==0)
1499//                      //user cancelled, abort
1500//                      KillWaves/Z newProtocol
1501//                      SetDataFolder root:
1502//                      Abort "Incomplete protocol has been deleted"
1503//              Endif
1504//              //assign filename (full path) to [0]
1505//              newProtocol[0] = V_GetFileNameFromPathNoSemi(S_filename)
1506               
1507//              notDone=1
1508//              do
1509//                      //prompt for additional background files
1510//                      DoAlert 1,"Do you want to add another background file?"
1511//                      if(V_flag == 1)         //yes
1512//                              Open/D/R/F=fileFilters/M="Select another Background data file"/P=catPathName refnum
1513//                              //check for cancel
1514//                              if(strlen(S_filename)==0)
1515//                                      //user cancelled, abort ********maybe just break out of the loop here
1516//                                      KillWaves/Z newProtocol
1517//                                      SetDataFolder root:
1518//                                      Abort "Incomplete protocol has been deleted"
1519//                              Endif
1520//                              //assign filename (full path) to [0]
1521//                              newProtocol[0] += "," + V_GetFileNameFromPathNoSemi(S_filename)         //***COMMA separated list
1522//                              notDone = 1             //keep going
1523//                      else
1524//                              notDone = 0                     //no more to add
1525//                      Endif
1526//              While(notDone)
1527//////
1528
1529        else            //no background desired
1530                newProtocol[0] = "none"
1531        Endif
1532       
1533       
1534////////////////////// 
1535        DoAlert 1,"Do you want to subtract empty cell scattering from your data?"
1536        if(V_flag == 1)         //1=yes
1537               
1538                Prompt filename,"EMP File",popup,V_PickEMPButton("")
1539                DoPrompt "Select File",filename
1540                if (V_Flag)
1541                        return 0                                                                        // user canceled
1542                endif
1543//              //assign filename to [1]
1544                newProtocol[1] = V_GetFileNameFromPathNoSemi(fileName)         
1545       
1546
1547               
1548//              //prompt for Empty cell file, but don't actually open it (/D flag)
1549//              Open/D/R/F=fileFilters/M="Select the Empty Cell data file"/P=catPathName refnum
1550//              //check for cancel
1551//              if(strlen(S_filename)==0)
1552//                      //user cancelled, abort
1553//                      KillWaves/Z newProtocol
1554//                      SetDataFolder root:
1555//                      Abort "Incomplete protocol has been deleted"
1556//              Endif
1557//              //assign filename (full path) to [1]
1558//              newProtocol[1] = V_GetFileNameFromPathNoSemi(S_filename)
1559//             
1560//              notDone=1
1561//              do
1562//                      //prompt for additional Empty Cell files
1563//                      DoAlert 1,"Do you want to add another Empty Cell file?"
1564//                      if(V_flag == 1)         //yes
1565//                              Open/D/R/F=fileFilters/M="Select another Empty Cell data file"/P=catPathName refnum
1566//                              //check for cancel
1567//                              if(strlen(S_filename)==0)
1568//                                      //user cancelled, abort ********maybe just break out of the loop here
1569//                                      KillWaves/Z newProtocol
1570//                                      SetDataFolder root:
1571//                                      Abort "Incomplete protocol has been deleted"
1572//                              Endif
1573//                              //assign filename (full path) to [1]
1574//                              newProtocol[1] += "," + V_GetFileNameFromPathNoSemi(S_filename)         //***COMMA separated list
1575//                              notDone = 1             //keep going
1576//                      else
1577//                              notDone = 0                     //no more to add
1578//                      Endif
1579//              While(notDone)
1580
1581
1582        else            //no background desired
1583                newProtocol[1] = "none"
1584        Endif
1585       
1586       
1587//////////////////////////
1588        DoAlert 1,"Do you want to correct your data for detector sensitivity?"
1589       
1590        if(V_flag == 1)         //1=yes
1591               
1592                Prompt filename,"DIV File",popup,V_PickDIVButton("")
1593                DoPrompt "Select File",filename
1594                if (V_Flag)
1595                        return 0                                                                        // user canceled
1596                endif
1597//              //assign filename to [2]
1598                newProtocol[2] = V_GetFileNameFromPathNoSemi(fileName)
1599               
1600//              //prompt for DIV  file, but don't actually open it (/D flag)
1601//              Open/D/R/F=fileFilters/M="Select the detector sensitivity file"/P=catPathName refnum
1602//              //check for cancel
1603//              if(strlen(S_filename)==0)
1604//                      //user cancelled, abort
1605//                      KillWaves/Z newProtocol
1606//                      SetDataFolder root:
1607//                      Abort "Incomplete protocol has been deleted"
1608//              Endif
1609//              //assign filename (full path) to [2]
1610//              newProtocol[2] = V_GetFileNameFromPathNoSemi(S_filename)
1611               
1612               
1613        else
1614                //no Work.DIV desired
1615                newProtocol[2] = "none"
1616        Endif   
1617//////////////////////////
1618        DoAlert 1,"Do you want to mask your files before averaging?"
1619       
1620        if(V_flag == 1)         //1=yes
1621       
1622                Prompt filename,"MASK File",popup,V_PickMASKButton("")
1623                DoPrompt "Select File",filename
1624                if (V_Flag)
1625                        return 0                                                                        // user canceled
1626                endif
1627//              //assign filename to [3]
1628                newProtocol[3] = V_GetFileNameFromPathNoSemi(fileName)
1629                       
1630//              //prompt for mask  file, but don't actually open it (/D flag)
1631//              Open/D/R/F=fileFilters/M="Select the mask file"/P=catPathName refnum
1632//              //check for cancel
1633//              if(strlen(S_filename)==0)
1634//                      //user cancelled, abort
1635//                      KillWaves/Z newProtocol
1636//                      SetDataFolder root:
1637//                      Abort "Incomplete protocol has been deleted"
1638//              Endif
1639//              //assign filename (full path) to [3]
1640//              newProtocol[3] = V_GetFileNameFromPathNoSemi(S_filename)
1641               
1642               
1643        else
1644                //no MASK desired
1645                newProtocol[3] = "none"
1646        Endif   
1647       
1648        //absolute scaling
1649       
1650        //////////////////////////
1651        //ABS parameters stored as keyword=value string
1652        DoAlert 1,"Do you want absolute scaling?"
1653        if(V_flag == 1)         //1=yes
1654                //missing param - prompt for values, put in semicolon-separated list
1655                Execute "V_AskForAbsoluteParams_Quest()"
1656                SVAR absStr =  root:Packages:NIST:VSANS:Globals:Protocols:gAbsStr
1657                newProtocol[4] = absStr
1658        else
1659                //no absolute scaling desired
1660                newProtocol[4] = "none"
1661        Endif   
1662       
1663        //type of average, plot?, auto/manual naming/saving... put in semicolon separated string
1664        //of KEY=<value> format for easy parsing
1665        //Kewords are: AVTYPE,PHI,DPHI,PLOT,SAVE,NAME,SIDE,WIDTH
1666        //note that AVTYPE,NAME,SIDE have string values, others have numerical values
1667        ///////////////////////
1668        DoAlert 1,"Do you want to average your data to I vs. q?"
1669        if(V_flag == 1)         //1=yes
1670                String/G root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr = ""
1671                Execute "V_GetAvgInfo()"                //will put up missing paramter dialog and do all the work
1672                //:gAvgInfo is reset by the Proc(), copy this string tot he protocol
1673                SVAR tempStr=root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr
1674               
1675                //get a file path for saving files, if desired
1676                /////no - save files to the same data folder as the one with the raw data
1677                //then only one path to manage.
1678                //String yesNo = StringByKey("SAVE", tempStr,"=", ";")
1679                //if(cmpstr("Yes",yesNo) == 0)          //0=yes
1680                        //NewPath/C/M="Select Folder"/O Save_path               //Save_path is the symbolic path
1681                //Endif
1682
1683                newProtocol[5] = tempStr
1684                KillStrings/Z tempStr
1685        else
1686                //no averaging desired
1687                newProtocol[5] = "AVTYPE=none"
1688        Endif
1689       
1690        //returns the name of the newly created (= currently in use) protocol wave through a global
1691        String/G root:Packages:NIST:VSANS:Globals:Protocols:gProtoStr = newProtoStr
1692       
1693        //reset the panel based on the protocol textwave (currently a string)
1694        V_ResetToSavedProtocol(newProtoStr)
1695       
1696        SetDataFolder root:
1697       
1698        return(0)
1699End
1700
1701
1702// TODO
1703// -- this function may not work properly for VSANS, but I haven't tested it yet
1704// -- I'd rather have some sort of display to show the current content of the WORK folders
1705//
1706// -- the global string fileList does not exist, so this will always fail and return zero
1707//
1708//
1709//function to check the work files (to see what's already there)
1710//and compare that with the files that are supposed to be there, according to the
1711//current protocol (to save unnecessary time re-loading files)
1712//
1713//the "type" folder is checked for all files in the list req(ested)Files
1714//note that the list of files is a full path:name;ver, while the
1715//fileList in the folder is just the name (or a list of names)
1716//
1717//returns 0 false, if files are NOT present
1718//or 1 = true, yes, the files are there as requested
1719//
1720Function V_AreFilesThere(type,reqFiles)
1721        String type,reqFiles
1722       
1723        //in general, reqFiles is a list of full paths to files - which MAY include semicolon version numbers
1724        //reqFiles MUST be constructed with COMMAS as list separators, to avoid disaster
1725        //when the version numbers are interpreted as filenames
1726       
1727        //get the number of files requested
1728        Variable nReq,nCur,match,ii
1729        nReq = ItemsInList(reqFiles,",")
1730       
1731        //get the name of the file currently in BGD - in the global fileList
1732        //fileList has NAMES ONLY - since it was derived from the file header
1733        String testStr
1734        testStr = "root:Packages:NIST:"+type+":fileList"
1735        if(Exists(testStr) == 2)                //2 if string variable exists
1736                SVAR curFiles = $testStr
1737        else
1738                //no files currently in type folder, return zero
1739                Return (0)
1740        Endif
1741        //get the number of files already in folder
1742        nCur = ItemsInList(curFiles,";")
1743        If(nCur != nReq)
1744                Return (0)              //quit now, the wrong number of files present
1745        Endif
1746        //right number of files... are the names right...
1747        //check for a match (case-sensitive!!) of each requested file in the curFile string
1748        //need to extract filenames from reqFiles, since they're the full path and name
1749       
1750        ii=0
1751        do
1752                testStr = StringFromList(ii,reqFiles,",")       //testStr is the Nth full path and filename
1753                //testStr = GetFileNameFromPathNoSemi(testStr)  //testStr will now be just the filename
1754                match = stringmatch(curFiles,testStr)
1755                If(!match)
1756                        Return (0)              //req file was not found in curFile list - get out now
1757                Endif
1758                ii+=1
1759        while(ii<nreq)
1760       
1761        Return (1)              //indicate that files are OK, no changes needed
1762End
1763
1764
1765//
1766//will add the files specified in the protocol to the "type" folder
1767//will add multiple files together if more than one file is requested
1768//(list is a comma delimited list of filenames, with NO path information)
1769//
1770// This routine NOW DOES check for the possibility that the filenames may have ";vers" from the
1771// VAX - data should be picked up from Charlotte, where it won't have version numbers.
1772//
1773Function V_AddFilesInList(type,list)
1774        String type,list
1775       
1776        //type is the work folder to put the data into, and list is a COMMA delimited list of paths/names
1777        Variable num,ii,err=0,refNum
1778        String filename,pathStr=""
1779        PathInfo catPathName                    //this is where the files are
1780        pathstr=S_path
1781       
1782        num = ItemsInList(list,",")             // comma delimited list
1783       
1784        ii=0
1785        do
1786                //FindValidFilename only needed in case of vax version numbers
1787                filename = pathStr + V_FindValidFilename(StringFromList(ii,list,","))
1788                Open/Z/R refnum as filename
1789                if(V_flag != 0)         //file not found
1790                        //Print "file not found AddFilesInList()"
1791                        //Print filename
1792                        err = 1
1793                        return(err)
1794                Endif
1795                Close refnum            //file was found and opened, so close it
1796               
1797//              Abort "Find equivalent to ReadHeaderAndData(filename)"
1798//              ReadHeaderAndData(filename)
1799                err = V_LoadHDF5Data(filename,"RAW")   
1800               
1801                if(ii == 0)
1802                        //first pass, wipe out the old contents of the work file
1803                        err =  V_Raw_to_work(type)
1804                else
1805                        err = V_Add_raw_to_work(type)
1806                Endif
1807                ii+=1
1808        while(ii<num)
1809        return(err)
1810End
1811
1812//function will reduce a sample file (or ask for file(s))
1813//using the protocol named as "protoStr" in the Protocols subfolder
1814//samStr is the file(s) or "ask" to force prompt
1815//sequentially proceeds through flowchart, doing reduction steps as needed
1816//show Schematic to debug what steps/values were used
1817//
1818//function is long, but straightforward logic
1819//
1820Function V_ExecuteProtocol(protStr,samStr)
1821        String protStr,samStr
1822        //protStr is the full path to the selected protocol wave
1823        //samStr is either "ask" or the name ONLY ofthe desired sample data file(s) (NO PATH)
1824        WAVE/T prot = $protStr
1825        SetDataFolder root:Packages:NIST:VSANS:Globals:Protocols
1826       
1827        Variable filesOK,err,notDone
1828        String activeType, msgStr, junkStr, pathStr=""
1829        PathInfo catPathName                    //this is where the files are
1830        pathStr=S_path
1831       
1832//      NVAR useXMLOutput = root:Packages:NIST:gXML_Write
1833       
1834        //Parse the instructions in the prot wave
1835        //0 - bkg
1836        //1 - emp
1837        //2 - div
1838        //3 - mask
1839        //4 - abs params c2-c5
1840        //5 - average params
1841        //6 = DRK file (**out of sequence)
1842        //7 = unused
1843
1844// for VSANS, DIV is used on each data file as it is converted to WORK, so it needs to be
1845//  the first thing in place, before any data or backgrounds are loaded
1846
1847
1848//
1849// TODO:
1850// x- this is no longer done after the COR step, and CAL is not produced as output of DIV       
1851// x- needs to be aware of the file name passed in
1852// -- PromptForPath does not exist in VSANS. Need a better (automated) way to find the file.
1853
1854//check for work.div file (prot[2])
1855//load in if needed
1856// no math is done here, DIV is applied as files are converted to WORK
1857// no need to display DIV, or update any data display
1858//
1859//              DoAlert 0,"DIV step incomplete"
1860
1861        If(cmpstr("none",prot[2])!=0)           // if !0, then there's a file requested
1862                If(cmpstr("ask",prot[2]) == 0)
1863                        //ask user for file
1864//                       junkStr = PromptForPath("Select the detector sensitivity file")
1865                        junkStr=""
1866                        If(strlen(junkStr)==0)
1867                                SetDataFolder root:
1868                                Abort "No file selected, data reduction aborted"
1869                        Endif
1870                        V_LoadHDF5Data(junkStr,"DIV")
1871                else
1872                        //assume it's a path, and that the first (and only) item is the path:file
1873                        //list processing is necessary to remove any final comma
1874                        junkStr = pathStr + StringFromList(0, prot[2],"," )
1875                        V_LoadHDF5Data(junkStr,"DIV")
1876                Endif
1877
1878        Endif
1879
1880
1881
1882
1883// TODO:
1884// -- currently does not allow adding RAW data files together
1885//     
1886
1887        //prompt for sam data => read raw data, add to sam folder
1888        //or parse file(s) from the input paramter string
1889        activeType = "SAM"
1890        msgStr = "Select sample data"
1891        //Ask for SAM file or parse
1892        do
1893                if((cmpstr(samStr,"ask") == 0) || (cmpstr(samStr,"")==0) )              //zero if strings are equal
1894                        err = V_LoadHDF5Data("","RAW")          //will prompt for file
1895                        if(err)
1896                                PathInfo/S catPathName
1897                                Abort "reduction sequence aborted"
1898                        endif
1899                        V_UpdateDisplayInformation("RAW")                       //display the new type of data that was loaded
1900                        err =  V_Raw_to_work(activeType)                //this is the first file (default)
1901                        //Ask for another SAM file
1902                        do
1903                                DoAlert 1,"Do you want to add another Sample file?"
1904                                if(V_flag == 1)         //yes
1905                                        err = V_LoadHDF5Data("","RAW")          //will prompt for file
1906                                        if(err)
1907                                                PathInfo/S catPathName
1908                                                Abort "reduction sequence aborted"
1909                                        endif
1910                                        V_UpdateDisplayInformation("RAW")                       //display the new type of data that was loaded
1911                                        err = V_Add_raw_to_work(activeType)
1912                                        notDone = 1
1913                                else
1914                                        notDone = 0
1915                                endif
1916                        while(notDone)
1917                        break
1918                Endif
1919                //"none" is not an option - you always need a sample file - "none" will never return zero
1920                //if not "ask" AND not "none" then try to parse the filenames
1921                If((cmpstr(samStr,"none") != 0) && (cmpstr(samStr,"ask") != 0))
1922                        //filesOK = AreFilesThere(activeType,samStr)            //return 1 if correct files are already there
1923                        filesOK = 0             // Feb 2008, always force a reload of files. Maybe slow, but always correct
1924                        if(!filesOK)
1925                                //add the correct file(s) to SAM
1926                               
1927                                // TODO: only one file is allowed currently
1928                                err = V_AddFilesInList(activeType,samStr)
1929                               
1930                                if(err)
1931                                        //Print "samstr = ",samStr
1932                                        Abort "SAM file not found, reset SAM file"
1933                                Endif
1934                        Endif
1935                Endif
1936        While(0)
1937        // TODO
1938        // -- this may not be the most reliable way to pas the file name
1939        SVAR file_name = root:file_Name
1940        String sameFileLoaded = file_name               //keep a copy of the sample file loaded
1941       
1942        //always update
1943        V_UpdateDisplayInformation(ActiveType)
1944       
1945        //check for bkg file  -- "ask" might not fail - "ask?" will - ? not allowed in VAX filenames
1946        // add if needed
1947        //use a "case" statement
1948        msgStr = "Select background file"
1949        activeType = "BGD"
1950        do
1951                if(cmpstr(prot[0],"ask") == 0)          //zero if strings are equal
1952                        err = V_LoadHDF5Data("","RAW")          //will prompt for file
1953                        if(err)
1954                                PathInfo/S catPathName
1955                                Abort "reduction sequence aborted"
1956                        endif
1957                        V_UpdateDisplayInformation("RAW")                       //display the new type of data that was loaded
1958                        err =  V_Raw_to_work(activeType)                //this is the first file (default)
1959                        //Ask for another BGD file
1960                        do
1961                                DoAlert 1,"Do you want to add another Background file?"
1962                                if(V_flag == 1)         //yes
1963                                        err = V_LoadHDF5Data("","RAW")          //will prompt for file
1964                                        if(err)
1965                                                PathInfo/S catPathName
1966                                                Abort "reduction sequence aborted"
1967                                        endif
1968                                        V_UpdateDisplayInformation("RAW")                       //display the new type of data that was loaded
1969                                        err = V_Add_raw_to_work(activeType)
1970                                        notDone = 1
1971                                else
1972                                        notDone = 0
1973                                endif
1974                        while(notDone)
1975                        V_UpdateDisplayInformation(ActiveType)  //update before breaking from loop
1976                        break
1977                Endif
1978                If(cmpstr(prot[0],"none") == 0)
1979                        //clean out the BKG folder?
1980                        //KillDataFolder root:BKG
1981                        //NewDataFolder/O root:BKG
1982                        break
1983                Endif
1984                //if not "ask" AND not "none" then try to parse the filenames
1985                If((cmpstr(prot[0],"none") != 0) && (cmpstr(prot[0],"ask") != 0))
1986                        //filesOK = AreFilesThere(activeType,prot[0])
1987                        filesOK = 0             // Feb 2008, always force a reload of files. Maybe slow, but always correct
1988                        if(!filesOK)
1989                                //add the correct file(s) to BGD
1990                                string bgdStr = prot[0]
1991                                err = V_AddFilesInList(activeType,bgdStr)
1992                                If(err)
1993                                        Abort "BGD file not found. Reset BGD file list"
1994                                Endif
1995                        Endif
1996                        V_UpdateDisplayInformation(ActiveType)          //update before breaking from loop
1997                Endif
1998        While(0)
1999       
2000       
2001        //check for emp file (prot[1])
2002        // add if needed
2003        msgStr = "Select empty cell data"
2004        activeType = "EMP"
2005        do
2006                if(cmpstr(prot[1],"ask") == 0)
2007                        err = V_LoadHDF5Data("","RAW")          //will prompt for file
2008                        if(err)
2009                                PathInfo/S catPathName
2010                                Abort "reduction sequence aborted"
2011                        endif
2012                        V_UpdateDisplayInformation("RAW")                       //display the new type of data that was loaded
2013                        err =  V_Raw_to_work(activeType)                //this is the first file (default)
2014                        //Ask for another EMP file
2015                        do
2016                                DoAlert 1,"Do you want to add another Empty Cell file?"
2017                                if(V_flag == 1)         //yes
2018                                        err = V_LoadHDF5Data("","RAW")          //will prompt for file
2019                                        if(err)
2020                                                PathInfo/S catPathName
2021                                                Abort "reduction sequence aborted"
2022                                        endif
2023                                        V_UpdateDisplayInformation("RAW")                       //display the new type of data that was loaded
2024                                        err = V_Add_raw_to_work(activeType)
2025                                        notDone = 1
2026                                else
2027                                        notDone = 0
2028                                endif
2029                        while(notDone)
2030                        V_UpdateDisplayInformation(ActiveType)          //update before breaking from loop
2031                        break
2032                Endif
2033                If(cmpstr(prot[1],"none") == 0)
2034                        //clean out the EMP folder?
2035                        //KillDataFolder root:Packages:NIST:EMP
2036                        //NewDataFolder/O root:Packages:NIST:EMP
2037                        break
2038                Endif
2039                //if not "ask" AND not "none" then try to parse the filenames
2040                If((cmpstr(prot[1],"none") != 0) && (cmpstr(prot[1],"ask") != 0))
2041                        //filesOK = AreFilesThere(activeType,prot[1])
2042                        filesOK = 0             // Feb 2008, always force a reload of files. Maybe slow, but always correct
2043                        if(!filesOK)
2044                                //add the correct file(s) to BGD
2045                                err = V_AddFilesInList(activeType,prot[1])
2046                                If(err)
2047                                        Abort "EMP file not found. Reset EMP file list"
2048                                Endif
2049                        Endif
2050                        V_UpdateDisplayInformation(ActiveType)  //update before breaking from loop
2051                Endif
2052        While(0)
2053       
2054        //do the CORRECT step based on the answers to emp and bkg subtraction
2055        //by setting the proper"mode"
2056        //1 = both emp and bgd subtraction
2057        //2 = only bgd subtraction
2058        //3 = only emp subtraction
2059        //4 = no subtraction
2060        //additional modes 091301
2061        //11 = emp, bgd, drk
2062        //12 = bgd and drk
2063        //13 = emp and drk
2064        //14 = no subtractions
2065        //work.drk is from proto[6]
2066        //
2067        //subtracting just the DRK data is NOT an option - it doesnt' really make any physical sense
2068        // - in this case, DRK is skipped (equivalent to mode==4)
2069        // automatically accounts for attenuators given the lookup tables and the
2070        //desired subtractions
2071        //Attenuator lookup tables are alredy implemented (NG1 = NG7)
2072        //
2073
2074
2075/////// DRK is SKIPPED
2076       
2077//      //read in the DRK data if necessary
2078//      //only one file, assumed to be RAW data
2079//      //
2080//      String fname="",drkStr=""
2081//      drkStr=StringByKey("DRK",prot[6],"=",",")
2082//      if(cmpstr(drkStr,"none") != 0)
2083//              err = ReadHeaderAndData( (pathStr+drkStr) )
2084//              if(err)
2085//                      PathInfo/S catPathName
2086//                      Abort "reduction sequence aborted"
2087//              endif
2088//              err = V_Raw_to_Work_NoNorm("DRK")
2089//      endif
2090
2091
2092       
2093        //dispatch to the proper "mode" of Correct()
2094        Variable mode=4,val
2095        do
2096                if( (cmpstr("none",prot[0]) == 0)       && (cmpstr("none",prot[1]) == 0) )
2097                        //no subtraction (mode = 4),
2098                        mode = 4
2099                Endif
2100                If((cmpstr(prot[0],"none") != 0) && (cmpstr(prot[1],"none") == 0))
2101                        //subtract BGD only
2102                        mode=2
2103                Endif
2104                If((cmpstr(prot[0],"none") == 0) && (cmpstr(prot[1],"none") != 0))
2105                        //subtract EMP only
2106                        mode=3
2107                Endif
2108                If((cmpstr(prot[0],"none") != 0) && (cmpstr(prot[1],"none") != 0))
2109                        // bkg and emp subtraction are to be done (BOTH not "none")
2110                        mode=1
2111                Endif
2112                activeType = "COR"
2113                //add in DRK mode (0= no used, 10 = used)
2114                val = NumberByKey("DRKMODE",prot[6],"=","," )
2115                mode += val
2116//              print "mode = ",mode
2117                err = V_Correct(mode)
2118                if(err)
2119                        SetDataFolder root:
2120                        Abort "error in Correct, called from executeprotocol, normal cor"
2121                endif
2122                V_UpdateDisplayInformation(ActiveType)          //update before breaking from loop
2123        While(0)
2124
2125
2126////////////////////////////////////////////////////////
2127
2128// TODO:
2129// -- calculation works, needs proper inputs (solid angle aware)
2130// --   Open beam method is only a stub - fill in calculation in V_AskForAbsoluteParams_Quest()
2131        Variable c2,c3,c4,c5,kappa_err
2132        //do absolute scaling if desired
2133                DoAlert 0,"Abs step incomplete"
2134
2135        if(cmpstr("none",prot[4])!=0)
2136                if(cmpstr("ask",prot[4])==0)
2137                        //get the params from the user
2138                        Execute "V_AskForAbsoluteParams_Quest()"
2139                        //then from the list
2140                        SVAR junkAbsStr = root:Packages:NIST:VSANS:Globals:Protocols:gAbsStr
2141                        c2 = NumberByKey("TSTAND", junkAbsStr, "=", ";")        //parse the list of values
2142                        c3 = NumberByKey("DSTAND", junkAbsStr, "=", ";")
2143                        c4 = NumberByKey("IZERO", junkAbsStr, "=", ";")
2144                        c5 = NumberByKey("XSECT", junkAbsStr, "=", ";")
2145                        kappa_err = NumberByKey("SDEV", junkAbsStr, "=", ";")
2146                else
2147                        //get the parames from the list
2148                        c2 = NumberByKey("TSTAND", prot[4], "=", ";")   //parse the list of values
2149                        c3 = NumberByKey("DSTAND", prot[4], "=", ";")
2150                        c4 = NumberByKey("IZERO", prot[4], "=", ";")
2151                        c5 = NumberByKey("XSECT", prot[4], "=", ";")
2152                        kappa_err = NumberByKey("SDEV", prot[4], "=", ";")
2153                Endif
2154                //get the sample trans and thickness from the activeType folder
2155                Variable c0 = V_getSampleTransmission(activeType)               //sample transmission
2156                Variable c1 = V_getSampleThickness(activeType)          //sample thickness
2157               
2158                err = V_Absolute_Scale(activeType,c0,c1,c2,c3,c4,c5,kappa_err)
2159                if(err)
2160                        SetDataFolder root:
2161                        Abort "Error in V_Absolute_Scale(), called from V_ExecuteProtocol"
2162                endif
2163                activeType = "ABS"
2164                V_UpdateDisplayInformation(ActiveType)                  //update before breaking from loop
2165        Endif
2166
2167//
2168// TODO -- incomplete
2169//     
2170//mask data if desired (this is done automatically  in the average step) and is
2171//not done explicitly here (if no mask in MSK folder, a null mask is created and "used")
2172       
2173        //check for mask
2174        //add mask if needed
2175        // can't properly check the filename - so for now always add
2176        //doesn't change the activeType
2177        if(cmpstr("none",prot[3])!=0)
2178                If(cmpstr("ask",prot[3])==0)
2179                        //get file from user
2180//                      junkStr = PromptForPath("Select Mask file")
2181                        DoAlert 0,"Mask step incomplete"
2182
2183                        If(strlen(junkStr)==0)
2184                                //no selection of mask file is not a fatal error, keep going, and let cirave()
2185                                //make a "null" mask
2186                                //if none desired, make sure that the old mask is deleted
2187                                //junkStr = GetDataFolder(1)
2188                                //SetDataFolder root:Packages:NIST:MSK
2189                                KillWaves/Z root:Packages:NIST:MSK:data
2190                                //SetDataFolder junkStr
2191                                DoAlert 0,"No Mask file selected, data not masked"
2192                        else
2193                                //read in the file from the dialog
2194                                V_LoadHDF5Data("","MSK")
2195                        Endif
2196                else
2197                        //just read it in from the protocol
2198                        //list processing is necessary to remove any final comma
2199                        junkStr = pathStr + StringFromList(0, prot[3],"," )
2200                        V_LoadHDF5Data(junkStr,"MSK")
2201                Endif
2202        else
2203                //if none desired, make sure that the old mask is deleted
2204// TODO
2205// x- clean out the data folder
2206// x- note that V_KillNamedDataFolder() points to RawVSANS, and won't work
2207// -- what happens if the kill fails? need error handling
2208//
2209                KillDataFolder/Z root:Packages:NIST:VSANS:MSK:
2210        Endif
2211       
2212
2213
2214
2215
2216        // average/save data as specified
2217       
2218        //Parse the keyword=<Value> string as needed, based on AVTYPE
2219       
2220        //average/plot first
2221        String av_type = StringByKey("AVTYPE",prot[5],"=",";")
2222        If(cmpstr(av_type,"none") != 0)
2223                If (cmpstr(av_type,"")==0)              //if the key could not be found... (if "ask" the string)
2224                        //get the averaging parameters from the user, as if the set button was hit
2225                        //in the panel
2226                        V_SetAverageParamsButtonProc("dummy")           //from "ProtocolAsPanel"
2227                        SVAR tempAveStr = root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr
2228                        av_type = StringByKey("AVTYPE",tempAveStr,"=",";")
2229                else
2230                        //there is info in the string, use the protocol
2231                        //set the global keyword-string to prot[5]
2232                        String/G root:Packages:NIST:VSANS:Globals:Protocols:gAvgInfoStr = prot[5]
2233                Endif
2234        Endif
2235       
2236        //convert the folder to linear scale before averaging, then revert by calling the window hook
2237        // (not needed for VSANS, data is always linear scale)
2238
2239// TODO
2240// -- this switch does nothing -- fill it in
2241//     
2242        strswitch(av_type)      //dispatch to the proper routine to average to 1D data
2243                case "none":           
2244                        //still do nothing
2245                        break                   
2246                case "2D_ASCII":       
2247                        //do nothing
2248                        break
2249                case "QxQy_ASCII":
2250                        //do nothing
2251                        break
2252                case "PNG_Graphic":
2253                        //do nothing
2254                        break
2255                case "Rectangular":
2256//                      RectangularAverageTo1D(activeType)
2257                        break
2258                case "Annular":
2259//                      AnnularAverageTo1D(activeType)
2260                        break
2261                case "Circular":
2262//                      CircularAverageTo1D(activeType)
2263                        break
2264                case "Sector":
2265//                      CircularAverageTo1D(activeType)
2266                        break
2267                case "Sector_PlusMinus":
2268//                      Sector_PlusMinus1D(activeType)
2269                        break
2270                default:       
2271                        //do nothing
2272        endswitch
2273        // bin and plot the data
2274        // TODO
2275        // x- currently this bins and plots based on the V_1D_Data panel, NOT the selections above
2276        // now takes the the binType from the protocol, and uses two steps to bin and average
2277        String binTypeStr = StringByKey("BINTYPE",prot[5],"=",";")
2278        // plotting is not really necessary, and the graph may not be open - so skip for now?
2279        Variable binType
2280        strswitch(binTypeStr)   // string switch
2281                case "One":
2282                        binType = 1
2283                        break           // exit from switch
2284                case "Two":
2285                        binType = 2
2286                        break           // exit from switch
2287                case "Four":
2288                        binType = 3
2289                        break           // exit from switch
2290                case "Slit Mode":
2291                        binType = 4
2292                        break           // exit from switch
2293
2294                default:                        // optional default expression executed
2295                        binType = 0
2296                        Abort "Binning mode not found in V_QBinAllPanels() "// when no case matches
2297        endswitch
2298       
2299        V_PlotData_Panel(binType)               //this bins and plots the *currently displayed* data
2300        V_QBinAllPanels(activeType,binType)             //bin the active reduction data
2301
2302// TODO:
2303// x- "B" detector is currently skipped - Q is not yet calculated
2304
2305        String str,winStr="V_1D_Data"
2306        sprintf str,"(\"%s\",%d,\"%s\")",activeType,binType,winStr
2307
2308       
2309        Execute ("V_Back_IQ_Graph"+str)
2310//      Print "V_Back_IQ_Graph"+str
2311        Execute ("V_Middle_IQ_Graph"+str)
2312        Execute ("V_Front_IQ_Graph"+str)
2313
2314       
2315///// end of averaging dispatch
2316
2317
2318// TODO
2319// x- how do I get the sample file name?
2320//     sameFileLoaded is the file name loaded (contains the extension)
2321//     
2322        //save data if desired
2323        String fullpath = "", newfileName=""
2324        String item = StringByKey("SAVE",prot[5],"=",";")               //does user want to save data?
2325        If( (cmpstr(item,"Yes")==0) && (cmpstr(av_type,"none") != 0) )         
2326                //then save
2327                newFileName = RemoveEnding(sameFileLoaded,".nxs.ngv")
2328               
2329                //pick ABS or AVE extension
2330                String exten = activeType
2331                if(cmpstr(exten,"ABS") != 0)
2332                        exten = "AVE"
2333                endif
2334                if(cmpstr(av_type,"2D_ASCII") == 0)
2335                        exten = "ASC"
2336                endif
2337                if(cmpstr(av_type,"QxQy_ASCII") == 0)
2338                        exten = "DAT"
2339                endif
2340               
2341//              // add an "x" to the file extension if the output is XML
2342//              // currently (2010), only for ABS and AVE (1D) output
2343//              if( cmpstr(exten,"ABS") == 0 || cmpstr(exten,"AVE") == 0 )
2344//                      if(useXMLOutput == 1)
2345//                              exten += "x"
2346//                      endif
2347//              endif
2348                               
2349                //Path is catPathName, symbolic path
2350                //if this doesn't exist, a dialog will be presented by setting dialog = 1
2351                //
2352                //
2353                Variable dialog = 0
2354                PathInfo/S catPathName
2355                item = StringByKey("NAME",prot[5],"=",";")              //Auto or Manual naming
2356                String autoname = StringByKey("AUTONAME",prot[5],"=",";")               //autoname -  will get empty string if not present
2357                If((cmpstr(item,"Manual")==0) || (cmpstr(newFileName,"") == 0))
2358                        //manual name if requested or if no name can be derived from header
2359                        fullPath = newfileName + "."+ exten //puts possible new name or null string in dialog
2360                        dialog = 1              //force dialog for user to enter name
2361                else
2362                        //auto-generate name and prepend path - won't put up any dialogs since it has all it needs
2363                        //use autoname if present
2364//                      if (cmpstr(autoname,"") != 0)
2365//                              fullPath = S_Path + autoname + "." +exten
2366//                      else
2367//                              fullPath = S_Path + newFileName+"." + exten
2368//                      endif   
2369                Endif
2370                //
2371                strswitch(av_type)     
2372                        case "Annular":
2373//                              WritePhiave_W_Protocol(activeType,fullPath,dialog)
2374                                break
2375                        case "2D_ASCII":
2376//                              Fast2DExport(activeType,fullPath,dialog)
2377                                break
2378                        case "QxQy_ASCII":
2379//                              QxQy_Export(activeType,fullPath,dialog)
2380                                break
2381                        case "PNG_Graphic":
2382//                              SaveAsPNG(activeType,fullpath,dialog)
2383                                break
2384                        default:
2385//                              if (useXMLOutput == 1)
2386//                                      WriteXMLWaves_W_Protocol(activeType,fullPath,dialog)
2387//                              else
2388//                                      WriteWaves_W_Protocol(activeType,fullpath,dialog)
2389//                              endif
2390//
2391// TODO:
2392// -- fill in all of the cases, default is only the "standard" I(q)
2393                                V_ConcatenateForSave(activeType,binType)
2394                                V_Write1DData(activeType,newFileName+"."+exten)         //don't pass the full path, just the name
2395
2396                endswitch
2397               
2398                //Print "data written to:  "+ fullpath
2399        Endif
2400       
2401        //done with everything in protocol list
2402        Return(0)
2403End
2404
2405
2406//missing parameter dialog to solicit the 4 absolute intensity parameters
2407//from the user
2408//values are passed back as a global string variable (keyword=value)
2409//
2410Proc V_AskForAbsoluteParams(c2,c3,c4,c5,I_err)
2411        Variable c2=0.95,c3=0.1,c4=1,c5=32.0,I_err=0.32
2412        Prompt c2, "Standard Transmission"
2413        Prompt c3, "Standard Thickness (cm)"
2414        Prompt c4, "I(0) from standard fit (normalized to 1E8 monitor cts)"
2415        Prompt c5, "Standard Cross-Section (cm-1)"
2416        Prompt I_err, "error in I(q=0) (one std dev)"
2417       
2418        String/G root:Packages:NIST:VSANS:Globals:Protocols:gAbsStr=""
2419       
2420        root:Packages:NIST:VSANS:Globals:Protocols:gAbsStr = "TSTAND="+num2str(c2)
2421        root:Packages:NIST:VSANS:Globals:Protocols:gAbsStr +=  ";" + "DSTAND="+num2str(c3)
2422        root:Packages:NIST:VSANS:Globals:Protocols:gAbsStr +=  ";" + "IZERO="+num2str(c4)
2423        root:Packages:NIST:VSANS:Globals:Protocols:gAbsStr +=  ";" + "XSECT="+num2str(c5)
2424        root:Packages:NIST:VSANS:Globals:Protocols:gAbsStr +=  ";" + "SDEV="+num2str(I_err)
2425       
2426End
2427
2428
2429//
2430// TODO
2431// -- fill in all of the functionality for calculation from direct beam
2432//
2433//asks the user for absolute scaling information. the user can either
2434//enter the 5 necessary values in manually (missing parameter dialog)
2435//or the user can select an empty beam file from a standard open dialog
2436//if an empty beam file is selected, the "kappa" value is automatically calculated
2437//in either case, the global keyword=value string is set.
2438//
2439Function V_AskForAbsoluteParams_Quest()
2440       
2441        Variable err,loc,refnum
2442        //ask user if he wants to use a transmision file for absolute scaling
2443        //or if he wants to enter his own information
2444        err = V_UseStdOrEmpForABS()
2445        //DoAlert 1,"<Yes> to enter your own values, <No> to select an empty beam flux file"
2446        If ( err==1 )
2447                //secondary standard selected, prompt for values
2448                Execute "V_AskForAbsoluteParams()"              //missing parameters
2449        else
2450                //empty beam flux file selected, prompt for file, and use this to calculate KAPPA
2451                Variable kappa=1
2452                       
2453                //get the necessary variables for the calculation of kappa
2454                Variable detCnt,countTime,attenTrans,monCnt,sdd,pixel
2455                String detStr
2456
2457                               
2458                //lookup table for transmission factor
2459                //determine which instrument the measurement was done on from acctStr
2460                Variable lambda, attenNo,atten_err
2461               
2462                //get the XY box, if needed
2463                Variable x1,x2,y1,y2,ct_err
2464                String filename,tempStr                 //02JUL13
2465
2466//              err = GetXYBoxFromFile(tempName,x1,x2,y1,y2)            //xy's are passed/returned by reference
2467//              Printf "Using Box X(%d,%d),Y(%d,%d)\r",x1,x2,y1,y2
2468
2469               
2470                //need the detector sensitivity file - make a guess, allow to override
2471
2472
2473                //toggle SANS_Data to linear display if needed, so that we're working with linear scaled data
2474//              Wave divData = $"root:Packages:NIST:div:Data"
2475//              Wave data = $"root:Packages:NIST:raw:data"              //this will be the linear data
2476                // correct by detector sensitivity
2477//              data /= divData
2478               
2479                // now do the sum, only in the box     
2480//              detCnt = SumCountsInBox(x1,x2,y1,y2,ct_err,"RAW")
2481
2482                //             
2483//              kappa = detCnt/countTime/attenTrans*1.0e8/(monCnt/countTime)*(pixel/sdd)^2
2484               
2485                Variable kappa_err
2486//              kappa_err = (ct_err/detCnt)^2 + (atten_err/attenTrans)^2
2487//              kappa_err = sqrt(kappa_err) * kappa
2488               
2489                // set the parameters in the global string
2490//              Execute "V_AskForAbsoluteParams(1,1,"+junkStr+",1,"+errStr+")"          //no missing parameters, no dialog
2491               
2492                //should wipe out the data in the RAW folder, since it's not really RAW now
2493//              DoWindow/K SANS_Data
2494
2495                // SRK JUL 2006 don't clear the contents - just kill the window to force new data to be loaded
2496                // - obsucre bug if "ask" in ABS section of protocol clears RAW folder, then Q-axes can't be set from RAW:RealsRead
2497
2498                Printf "Kappa was un-successfully calculated as = %g +/- %g (%g %)\r",kappa,kappa_err,(kappa_err/kappa)*100
2499        Endif
2500       
2501End
2502
2503Function V_UserSelectBox_Continue(ctrlName) :buttonControl
2504        String ctrlName
2505       
2506        DoWindow/K junkWindow           //kill panel
2507end
2508
2509Function V_SelectABS_XYBox(x1,x2,y1,y2)
2510        Variable &x1,&x2,&y1,&y2
2511       
2512        Variable err=0
2513       
2514        Variable/G root:V_marquee=1             //sets the sneaky bit to automatically update marquee coords
2515        Variable/G root:V_left,root:V_right,root:V_bottom,root:V_top    //must be global for auto-update
2516        DoWindow/F SANS_Data
2517        NewPanel/K=2 /W=(139,341,382,432) as "Select the primary beam"
2518        DoWindow/C junkWindow
2519        AutoPositionWindow/E/M=1/R=SANS_Data
2520       
2521        Drawtext 21,20 ,"Select the primary beam with the"
2522        DrawText 21,40, "marquee and press continue"
2523        Button button0,pos={80,58},size={92,20},title="Continue"
2524        Button button0,proc=V_UserSelectBox_Continue
2525       
2526        PauseForUser junkWindow,SANS_Data
2527       
2528        DoWindow/F SANS_Data
2529
2530        //GetMarquee left,bottom                        //not needed
2531        NVAR V_left=V_left
2532        NVAR V_right=V_right
2533        NVAR V_bottom=V_bottom
2534        NVAR V_top=V_top
2535       
2536        x1 = V_left
2537        x2 = V_right
2538        y1 = V_bottom
2539        y2 = V_top
2540//      Print "new values,before rounding = ",x1,x2,y1,y2
2541
2542// TODO -- replace this call
2543//      KeepSelectionInBounds(x1,x2,y1,y2)
2544        //Print "new values = ",x1,x2,y1,y2
2545       
2546        KillVariables/Z root:V_Marquee,root:V_left,root:V_right,root:V_bottom,root:V_top
2547        if((x1-x2)==0 || (y1-y2)==0)
2548                err=1
2549        endif
2550        return(err)
2551End
2552
2553Function V_UseStdOrEmpForABS()
2554               
2555        NewPanel/K=2 /W=(139,341,402,448) as "Absolute Scaling"
2556        DoWindow/C junkABSWindow
2557        ModifyPanel cbRGB=(57346,65535,49151)
2558        SetDrawLayer UserBack
2559        SetDrawEnv fstyle= 1
2560        DrawText 21,20,"Method of absolute calibration"
2561        Button button0,pos={52,33},size={150,20},proc=V_UserSelectABS_Continue,title="Empty Beam Flux"
2562        Button button1,pos={52,65},size={150,20},proc=V_UserSelectABS_Continue,title="Secondary Standard"
2563       
2564        PauseForUser junkABSWindow
2565        NVAR val = root:Packages:NIST:VSANS:Globals:tmpAbsVal
2566        return(val)
2567End
2568
2569//returns 0 if button0 (empty beam flux)
2570// or 1 if secondary standard
2571Function V_UserSelectABS_Continue(ctrlName) :buttonControl
2572        String ctrlName
2573       
2574        variable val=0
2575        If(cmpstr(ctrlName,"button0")==0)
2576                val=0           
2577        else
2578                val=1
2579        endif
2580//      print "val = ",ctrlName,val
2581        Variable/G root:Packages:NIST:VSANS:Globals:tmpAbsVal = val
2582        DoWindow/K junkABSWindow                //kill panel
2583        return(0)
2584end
2585
2586
2587//save the protocol as an IGOR text wave (.itx)
2588//
2589//
2590Function V_ExportProtocol(ctrlName) : ButtonControl
2591        String ctrlName
2592// get a list of protocols
2593        String Protocol=""
2594        SetDataFolder root:Packages:NIST:VSANS:Globals:Protocols
2595        Prompt Protocol "Pick A Protocol",popup, WaveList("*",";","")
2596        DoPrompt "Pick A Protocol to Export",Protocol
2597        if(V_flag==1)
2598                //Print "user cancel"
2599                SetDatafolder root:
2600                return(1)
2601        endif
2602//get the selection, or exit
2603        Wave/T pW= $protocol
2604        Make/O/T/N=13 tw
2605// save in the proper format (must write manually, for demo version)
2606        tw[0] = "IGOR"
2607        tw[1] = "WAVES/T \t"+protocol
2608        tw[2] = "BEGIN"
2609        tw[3,10] = "\t\""+pW[p-3]+"\""
2610        tw[11] = "END"
2611        tw[12] = "X SetScale/P x 0,1,\"\","+protocol+"; SetScale y 0,0,\"\","+protocol
2612       
2613        Variable refnum
2614        String fullPath
2615       
2616        PathInfo/S catPathName
2617        fullPath = DoSaveFileDialog("Export Protocol as",fname=Protocol,suffix="")
2618        If(cmpstr(fullPath,"")==0)
2619                //user cancel, don't write out a file
2620                Close/A
2621                Abort "no Protocol file was written"
2622        Endif
2623
2624        //actually open the file
2625        Open refNum as fullpath+".itx"
2626       
2627        wfprintf refnum, "%s\r", tw
2628        Close refnum
2629        //Print "all is well  ",protocol
2630        KillWaves/Z tw
2631        setDataFolder root:
2632        return(0)
2633
2634End
2635
2636//imports a protocol from disk into the protocols folder
2637//
2638// will overwrite existing protocols if necessary
2639//
2640//
2641Function V_ImportProtocol(ctrlName) : ButtonControl
2642        String ctrlName
2643
2644        SetDataFolder root:Packages:NIST:VSANS:Globals:Protocols
2645
2646        String fullPath
2647       
2648        PathInfo/S catPathName
2649        fullPath = DoOpenFileDialog("Import Protocol")
2650        If(cmpstr(fullPath,"")==0)
2651                //user cancel, don't write out a file
2652                Close/A
2653                Abort "no protocol was loaded"
2654        Endif
2655       
2656        LoadWave/O/T fullPath
2657       
2658        SetDataFolder root:
2659        return(0)
2660end
Note: See TracBrowser for help on using the repository browser.