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

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

Significant changes to the base READ of individual data fields from data files. Now, if the field requested is from a WORK file, and it does not exist, an error condition is returned (or a null wave). Calling procedures are responsible for handling errors. This prevents a string of open file dialogs if fields are missing from a file if they were never in the file to begin with (like sensor logs, polarization hardware, etc.)

New get/write calls were added for the updated temperature sensor fields.

group_ID is now only in the sample block, not the duplicated in the reduction block, and is correctly a number not a string.

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