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

Last change on this file since 1037 was 1036, checked in by srkline, 5 years ago

added utilities to allow saving of I(q) data sets either individual or concatenated. At this point, it simply trims a nominal number of points from the beginning and end of each set before concatenation (and then sorting).

These operations will need to be augmented in the future.

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