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

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

many changes to get the basics of a reduction protocol working

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