source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/Polarization/Pol_PolarizationCorrection.ipf @ 865

Last change on this file since 865 was 865, checked in by srkline, 10 years ago

more tweaks to the polarization correction. Adjusting control positions on the panels, makeing sure that reverting the fundamental cell parameters really does just that, adding buttons to export/import protocols from the PolCor? panel.

File size: 102.9 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2
3
4
5// These procedures and calculations duplicate the work of K. Krycka and WC Chen
6// in calculating the state of the He cell and subsequent correction of scattering data
7//
8//
9// SRK May 2011
10//
11//
12//
13// there is a particular sequence of things that need to be calculated
14// lots of constants, and lots of confusing, similar notation.
15//
16//
17// for this implementation, I'll follow what is described in the "PASANS"
18// writeup by K. Krycka, and I'll try to follow the equations as numbered there
19// and keep the notation as close as possible.
20//
21// error propagation was written up elsewhere, and will be implemented as well
22// - each of the calculations based on transmissions will need to have errors
23// brought in, and carried through the calculations. Some will be simple, some
24// will probably be easiest written as expansions.
25//
26
27
28
29// This is a first pass at the input panel (step 3)
30// to gather all of the files and conditions necessary to do the polarization correction
31//
32//
33
34// I'll need space for 4 input files in, say SAM
35// - load in all of the UU files, adding together
36// - rename the UU data, error
37//
38// - repeat for the other three cross sections, in the SAM folder, there will be
39// scattering data for all four cross sections present.
40//
41// then add together the values for the coefficient matrix.
42// -- this can be tricky with rescaling for time, and adding to the proper row of the
43// coefficient matrix. I'll need to re-read either the monitor or the time from the header
44// of each file that was added so that the contributions are added to the matrix in correct proportion
45//
46// Then everything is set to do the inversion.
47// -- the result of the inversion is 4 corrected data sets, with no polarization effects. "_pc"
48//
49// Now I can one-by-one, copy the correct UU, UD, etc. into "data" and "linear_data" (and the Reals, etc)
50// and run through the corrections as if it was normal SANS data
51//
52// this whole procedure is going to be essentially a big script of existing procedures
53//
54//
55
56// **** search for TODO to find items still to be fixed in other procedures  **********
57
58//
59// TODO:
60// - Overall, I need a better way of flowing through the whole process, to be sure that values are set
61// as needed and that one step won't fail because a previous step wasn't done yet. Combining the
62// first three panels into one w/ tabs would help a lot, but that is rather complex to implement.
63// and is still not a fool-proof situation
64//
65// X- mathod to save and restore the panel state - especially the popup selections
66//
67// -- should I force the Polarization correction to be re-done just before the protocol is
68//                      executed? Then I'm sure that the PC is done. Must do for each tab (only if part of the protocol)
69//                      Except that the procedures work on the "active" tab...
70//
71// -- When multiple files are added together, there are changes made to the RealsRead (monCts, etc.). Are these
72//              properly made, and then properly copied to the "_UU", and then properly copied back to the untagged waves
73//              for use in the reduction? (now at this later date, I don't understand this question...)
74//
75// -- still not sure what is needed for absolute scaling
76//
77// -- what is the sample transmission, and exactly what +/- states are the proper measurements to use for thransmission?
78//
79// -- generate some sort of report of what was set up, and what was used in the calculation
80//
81//
82
83
84
85
86// main entry to the PolCor Panel
87Proc ShowPolCorSetup()
88
89        Variable restore=0
90        DoWindow/F PolCor_Panel
91        if(V_flag==0)
92       
93                InitProtocolPanel()             //this will reset the strings.
94               
95                restore=Initialize_PolCorPanel()
96                PolCor_Panel()
97                SetWindow PolCor_Panel hook(kill)=PolCorPanelHook               //to save the state when panel is killed
98                //disable the controls on other tabs
99                ToggleSelControls("_1_",1)
100                ToggleSelControls("_2_",1)
101               
102                //restore the entries
103                if(restore)
104                        RestorePolCorPanel()
105                endif
106               
107
108        endif
109End
110
111
112Function RestorePolCorPanel()
113        //restore the popup state
114        Wave/T/Z w=root:Packages:NIST:Polarization:PolCor_popState
115
116        Variable ii,num
117        String name,popStr,list
118       
119        list = P_GetConditionNameList()         //list of conditions
120        if(WaveExists(w))
121                num = DimSize(w,0)
122                for(ii=0;ii<num;ii+=1)
123                        name = w[ii][0]
124                        popStr = w[ii][1]
125                        if(cmpstr("none",popStr) !=0 )
126                                PopupMenu $name,win=PolCor_Panel,mode=WhichListItem(popStr, list,";",0,0),popvalue=popStr
127                        endif
128                endfor
129        endif
130        return(0)
131End
132
133
134// saves the runs+cells for the PolCor Panel, separate from what is saved when the panel is closed
135//
136//      fname = "CellDecayPanelSaveState.itx"
137//
138//
139Function SavePolCorPanelState()
140
141        SetDataFolder root:Packages:NIST:Polarization
142       
143        String listStr,item,fname,noteStr,wStr
144        Variable num,ii,refnum
145       
146        fname = "PolCorPanelSaveState.itx"
147       
148        // get a list of the List waves
149        listStr=WaveList("ListWave_*",";","")
150        num=ItemsInList(listStr,";")
151//      print listStr
152
153        Open/P=home refnum// as fname           // creates a new file, or overwrites the existing file 
154        fprintf refNum,"IGOR\r"
155                       
156        // Save each of the list waves, 2D text
157        for(ii=0;ii<num;ii+=1)
158                item = StringFromList(ii, listStr,";")
159                Wave/T tw = $item
160               
161                Write2DTextWaveToITX(tw,refnum)
162               
163                fprintf refNum,"\r"             //space between waves
164        endfor 
165
166        // get a list of the Selection waves
167        listStr=WaveList("lbSelWave_*",";","")
168        num=ItemsInList(listStr,";")   
169       
170        // Save each of the Selection waves, 2D numerical
171        for(ii=0;ii<num;ii+=1)
172                item = StringFromList(ii, listStr,";")
173                Wave w = $item
174
175                Write2DWaveToITX(w,refnum)
176
177                fprintf refNum,"\r"     
178        endfor 
179
180// save the popState wave
181        Wave/T tw=root:Packages:NIST:Polarization:PolCor_popState
182        Write2DTextWaveToITX(tw,refnum)
183       
184        Close refnum
185
186        SetDataFolder root:
187        return(0)
188End
189
190// restores the waves for the cell decay table
191//
192//      fname = "PolCorPanelSaveState.itx"
193//
194//
195Function RestorePolCorPanelState()
196
197        SetDataFolder root:Packages:NIST:Polarization
198       
199        String listStr,item,fname,noteStr,wStr
200        Variable num,ii,refnum
201       
202        fname = "PolCorPanelSaveState.itx"
203        LoadWave/O/T fname
204       
205        SetDataFolder root:
206        return(0)
207End
208
209//
210// TODO:
211// X- only re-initialize values if user wants to. maybe ask.
212//
213Function Initialize_PolCorPanel()
214
215
216        Variable ii,num
217        String name,popStr
218               
219        DoAlert 1,"Do you want to initialize, wiping out all of your entries?"
220        if(V_flag != 1)         //1== yes initialize, so everything else, restore the entries
221                return(1)                       //send back 1 to say yes, restore the saved state
222        endif
223       
224        //initialize all of the strings for the input
225        SetDataFolder root:Packages:NIST:Polarization
226       
227        //controls are labeled "name_#_UU_#" where name is the type of control, # is the tab #, and (2nd) # is the control #
228
229/// new method using a listBox for each state   
230        Make/O/T/N=2 lbTitles
231        lbTitles[0] = "Run #"
232        lbTitles[1] = "Cell"
233       
234        Make/O/N=(10,2) lbSelWave
235        lbSelWave[][0] = 2                      //Run # column is editable
236        lbSelWave[][1] = 1                      //Cell name is a popup, not editable
237       
238        Make/O/T/N=(10,2) ListWave_0_UU=""
239        Make/O/T/N=(10,2) ListWave_0_DU=""
240        Make/O/T/N=(10,2) ListWave_0_DD=""
241        Make/O/T/N=(10,2) ListWave_0_UD=""
242        Make/O/N=(10,2) lbSelWave_0_UU
243        Make/O/N=(10,2) lbSelWave_0_DU
244        Make/O/N=(10,2) lbSelWave_0_DD
245        Make/O/N=(10,2) lbSelWave_0_UD
246        lbSelWave_0_UU[][0] = 2                 //Run # column is editable
247        lbSelWave_0_DU[][0] = 2                 //Run # column is editable
248        lbSelWave_0_DD[][0] = 2                 //Run # column is editable
249        lbSelWave_0_UD[][0] = 2                 //Run # column is editable
250        lbSelWave_0_UU[][1] = 1                 //Cell name is a popup, not editable   
251        lbSelWave_0_DU[][1] = 1                 //Cell name is a popup, not editable   
252        lbSelWave_0_DD[][1] = 1                 //Cell name is a popup, not editable   
253        lbSelWave_0_UD[][1] = 1                 //Cell name is a popup, not editable   
254       
255        Make/O/T/N=(10,2) ListWave_1_UU=""
256        Make/O/T/N=(10,2) ListWave_1_DU=""
257        Make/O/T/N=(10,2) ListWave_1_DD=""
258        Make/O/T/N=(10,2) ListWave_1_UD=""
259        Make/O/N=(10,2) lbSelWave_1_UU
260        Make/O/N=(10,2) lbSelWave_1_DU
261        Make/O/N=(10,2) lbSelWave_1_DD
262        Make/O/N=(10,2) lbSelWave_1_UD
263        lbSelWave_1_UU[][0] = 2                 //Run # column is editable
264        lbSelWave_1_DU[][0] = 2                 //Run # column is editable
265        lbSelWave_1_DD[][0] = 2                 //Run # column is editable
266        lbSelWave_1_UD[][0] = 2                 //Run # column is editable
267        lbSelWave_1_UU[][1] = 1                 //Cell name is a popup, not editable   
268        lbSelWave_1_DU[][1] = 1                 //Cell name is a popup, not editable   
269        lbSelWave_1_DD[][1] = 1                 //Cell name is a popup, not editable   
270        lbSelWave_1_UD[][1] = 1                 //Cell name is a popup, not editable           
271       
272        Make/O/T/N=(10,2) ListWave_2_UU=""
273        Make/O/T/N=(10,2) ListWave_2_DU=""
274        Make/O/T/N=(10,2) ListWave_2_DD=""
275        Make/O/T/N=(10,2) ListWave_2_UD=""
276        Make/O/N=(10,2) lbSelWave_2_UU
277        Make/O/N=(10,2) lbSelWave_2_DU
278        Make/O/N=(10,2) lbSelWave_2_DD
279        Make/O/N=(10,2) lbSelWave_2_UD
280        lbSelWave_2_UU[][0] = 2                 //Run # column is editable
281        lbSelWave_2_DU[][0] = 2                 //Run # column is editable
282        lbSelWave_2_DD[][0] = 2                 //Run # column is editable
283        lbSelWave_2_UD[][0] = 2                 //Run # column is editable
284        lbSelWave_2_UU[][1] = 1                 //Cell name is a popup, not editable   
285        lbSelWave_2_DU[][1] = 1                 //Cell name is a popup, not editable   
286        lbSelWave_2_DD[][1] = 1                 //Cell name is a popup, not editable   
287        lbSelWave_2_UD[][1] = 1                 //Cell name is a popup, not editable   
288
289
290////// old method using individual setVars and popups   
291//      for(ii=0;ii<5;ii+=1)
292//              String/G $("gStr_PolCor_0_UU_"+num2str(ii)) = "none"
293//              String/G $("gStr_PolCor_0_DU_"+num2str(ii)) = "none"
294//              String/G $("gStr_PolCor_0_DD_"+num2str(ii)) = "none"
295//              String/G $("gStr_PolCor_0_UD_"+num2str(ii)) = "none"
296//      endfor
297//
298//      for(ii=0;ii<5;ii+=1)
299//              String/G $("gStr_PolCor_1_UU_"+num2str(ii)) = "none"
300//              String/G $("gStr_PolCor_1_DU_"+num2str(ii)) = "none"
301//              String/G $("gStr_PolCor_1_DD_"+num2str(ii)) = "none"
302//              String/G $("gStr_PolCor_1_UD_"+num2str(ii)) = "none"
303//      endfor
304//     
305//      for(ii=0;ii<5;ii+=1)
306//              String/G $("gStr_PolCor_2_UU_"+num2str(ii)) = "none"
307//              String/G $("gStr_PolCor_2_DU_"+num2str(ii)) = "none"
308//              String/G $("gStr_PolCor_2_DD_"+num2str(ii)) = "none"
309//              String/G $("gStr_PolCor_2_UD_"+num2str(ii)) = "none"
310//      endfor
311////////////
312
313        SetDataFolder root:
314
315
316        return(0)
317
318end
319
320
321
322
323// controls are labeled "name_#_UU_#" where name is the type of control, # is the tab #, and (2nd) # is the control #
324// -- this will allow for future use of tabs. right now 0 will be the "SAM" data
325//
326// - always visible controls will have no "_" characters
327//
328// TODO:
329// X- tabs for SAM, EMP, and BGD
330// X- input fields for the other protocol items, like the Protocol Panel
331// X- save the popup state
332// X- need a way of saving the "protocol" since the setup is so complex.
333// - generate a report of the setup.
334// X- 4-panel display (maybe a layout with labels?) Maybe a panel with 4 subwindows. Can I use color bars in them?
335//
336//
337Window PolCor_Panel()
338        PauseUpdate; Silent 1           // building window...
339        NewPanel /W=(925,44,1662,800) /K=1
340        ModifyPanel cbRGB=(64349,63913,44660)
341//      ShowTools/A
342        SetDrawEnv linethick= 2.00
343        DrawLine 10,510,600,510
344
345        TabControl PolCorTab,pos={15,27},size={708,401},proc=PolCorTabProc
346        TabControl PolCorTab,tabLabel(0)="SAM",tabLabel(1)="EMP",tabLabel(2)="BGD"
347        TabControl PolCorTab,value= 0
348       
349        // always visible
350        Button button0,pos={26,445},size={80,20},proc=LoadRawPolarizedButton,title="Load ..."
351        Button button1,pos={26,473},size={130,20},proc=PolCorButton,title="Pol Correct Data"
352        Button button2,pos={222,445},size={130,20},proc=ShowPolMatrixButton,title="Show Coef Matrix"
353        Button button3,pos={222,473},size={160,20},proc=ChangeDisplayedPolData,title="Change Displayed Data"
354        Button button4,pos={620,18},size={30,20},proc=PolCorHelpParButtonProc,title="?"
355        Button button12,pos={440,473},size={120,20},proc=Display4XSButton,title="Display 4 XS"
356        Button button13,pos={440,446},size={120,20},proc=ClearPolCorEntries,title="Clear Entries"
357
358        PopupMenu popup1,pos={210,20},size={102,20},title="Condition"
359        PopupMenu popup1, mode=1,popvalue="none",value= #"P_GetConditionNameList()"
360
361        TitleBox title0,pos={100,66},size={24,24},title="\\f01UU or + +",fSize=12
362        TitleBox title1,pos={430,66},size={24,24},title="\\f01DU or - +",fSize=12
363        TitleBox title2,pos={100,250},size={25,24},title="\\f01DD or - -",fSize=12
364        TitleBox title3,pos={430,250},size={24,24},title="\\f01UD or + -",fSize=12
365       
366        // bits to set up reduction protocol
367        Button button5,pos={126,560},size={100,20},proc=PickDIVButton,title="set DIV file"
368        Button button5,help={"This button will set the file selected in the File Catalog table to be the sensitivity file."}
369        Button button6,pos={126,590},size={100,20},proc=PickMASKButton,title="set MASK file"
370        Button button6,help={"This button will set the file selected in the File Catalog table to be the mask file."}
371        Button button7,pos={126,620},size={110,20},proc=SetABSParamsButton,title="set ABS params"
372        Button button7,help={"This button will prompt the user for absolute scaling parameters"}
373        Button button8,pos={126,650},size={150,20},proc=SetAverageParamsButtonProc,title="set AVERAGE params"
374        Button button8,help={"Prompts the user for the type of 1-D averaging to perform, as well as saving options"}
375        Button button9,pos={80,690},size={120,20},proc=ReducePolCorDataButton,title="Reduce Data"
376        Button button9,help={"Reduce PolCor data"}
377        Button button10,pos={226,690},size={120,20},proc=SavePolCorProtocolButton,title="Save Protocol"
378        Button button10,help={"Save the PolCor protocol, within this experiment only"}
379        Button button11,pos={370,690},size={120,20},proc=RecallPolCorProtocolButton,title="Recall Protocol"
380        Button button11,help={"Recall a PolCor protocol from memory"}           
381        Button button14,pos={226,720},size={120,20},proc=ExportPolCorProtocolButton,title="Export Protocol"
382        Button button14,help={"Export the PolCor protocol, saving it on disk"}
383        Button button15,pos={370,720},size={120,20},proc=ImportPolCorProtocolButton,title="Import Protocol"
384        Button button15,help={"Import a PolCor protocol from a protocol previously saved to disk"}
385               
386        SetVariable setvar0,pos={322,560},size={250,15},title="file:"
387        SetVariable setvar0,help={"Filename of the detector sensitivity file to be used in the data reduction"}
388        SetVariable setvar0,limits={-Inf,Inf,0},value= root:myGlobals:Protocols:gDIV
389        SetVariable setvar1,pos={322,590},size={250,15},title="file:"
390        SetVariable setvar1,help={"Filename of the mask file to be used in the data reduction"}
391        SetVariable setvar1,limits={-Inf,Inf,0},value= root:myGlobals:Protocols:gMASK
392        SetVariable setvar2,pos={322,620},size={250,15},title="parameters:"
393        SetVariable setvar2,help={"Keyword-string of values necessary for absolute scaling of data. Remaining parameters are taken from the sample file."}
394        SetVariable setvar2,limits={-Inf,Inf,0},value= root:myGlobals:Protocols:gAbsStr
395        SetVariable setvar3,pos={322,650},size={250,15},title="parameters:"
396        SetVariable setvar3,help={"Keyword-string of choices used for averaging and saving the 1-D data files"}
397        SetVariable setvar3,limits={-Inf,Inf,0},value= root:myGlobals:Protocols:gAVE   
398       
399        CheckBox check0,pos={10,560},size={72,14},title="Sensitivity"
400        CheckBox check0,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"}
401        CheckBox check0,value= 1
402        CheckBox check1,pos={10,590},size={72,14},title="Mask"
403        CheckBox check1,help={""}
404        CheckBox check1,value= 1
405        CheckBox check2,pos={10,620},size={72,14},title="Absolute Scale"
406        CheckBox check2,help={""}
407        CheckBox check2,value= 1
408        CheckBox check3,pos={10,650},size={72,14},title="Average and Save"
409        CheckBox check3,help={""}
410        CheckBox check3,value= 1               
411        CheckBox check4,pos={10,530},size={72,14},title="Use EMP?"
412        CheckBox check4,help={""}
413        CheckBox check4,value= 1       
414        CheckBox check5,pos={100,530},size={72,14},title="Use BGD?"
415        CheckBox check5,help={""}
416        CheckBox check5,value= 1       
417
418       
419
420// SAM Tab     
421        // UU
422        ListBox ListBox_0_UU,pos={34,102},size={200,130},proc=PolCor_FileListBoxProc,frame=2
423        ListBox ListBox_0_UU,listWave=root:Packages:NIST:Polarization:ListWave_0_UU,titleWave=root:Packages:NIST:Polarization:lbTitles
424        ListBox ListBox_0_UU,selWave=root:Packages:NIST:Polarization:lbSelWave_0_UU,mode= 6,selRow= 0,selCol= 0,editStyle= 2
425//      SetVariable setvar_0_UU_0,pos={34,102},size={70,16},title="File",fSize=10
426//      SetVariable setvar_0_UU_0,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_UU_0
427//      SetVariable setvar_0_UU_1,pos={34,125},size={70,16},title="File",fSize=10
428//      SetVariable setvar_0_UU_1,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_UU_1
429//      SetVariable setvar_0_UU_2,pos={34,149},size={70,16},title="File",fSize=10
430//      SetVariable setvar_0_UU_2,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_UU_2
431//      SetVariable setvar_0_UU_3,pos={34,173},size={70,16},title="File",fSize=10
432//      SetVariable setvar_0_UU_3,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_UU_3
433//      SetVariable setvar_0_UU_4,pos={34,197},size={70,16},title="File",fSize=10
434//      SetVariable setvar_0_UU_4,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_UU_4
435//      PopupMenu popup_0_UU_0,pos={142,99},size={210,20},title="Cell"
436//      PopupMenu popup_0_UU_0,mode=3,popvalue="none",value= #"D_CellNameList()"
437//      PopupMenu popup_0_UU_1,pos={142,122},size={102,20},title="Cell"
438//      PopupMenu popup_0_UU_1,mode=1,popvalue="none",value= #"D_CellNameList()"
439//      PopupMenu popup_0_UU_2,pos={142,146},size={102,20},title="Cell"
440//      PopupMenu popup_0_UU_2,mode=1,popvalue="none",value= #"D_CellNameList()"
441//      PopupMenu popup_0_UU_3,pos={142,170},size={102,20},title="Cell"
442//      PopupMenu popup_0_UU_3,mode=1,popvalue="none",value= #"D_CellNameList()"
443//      PopupMenu popup_0_UU_4,pos={142,194},size={102,20},title="Cell"
444//      PopupMenu popup_0_UU_4,mode=1,popvalue="none",value= #"D_CellNameList()"
445
446        // DU
447        ListBox ListBox_0_DU,pos={368,102},size={200,130},proc=PolCor_FileListBoxProc,frame=2
448        ListBox ListBox_0_DU,listWave=root:Packages:NIST:Polarization:ListWave_0_DU,titleWave=root:Packages:NIST:Polarization:lbTitles
449        ListBox ListBox_0_DU,selWave=root:Packages:NIST:Polarization:lbSelWave_0_DU,mode= 6,selRow= 0,selCol= 0,editStyle= 2
450//      SetVariable setvar_0_DU_0,pos={368,102},size={70,16},title="File",fSize=10
451//      SetVariable setvar_0_DU_0,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_DU_0
452//      SetVariable setvar_0_DU_1,pos={368,125},size={70,16},title="File",fSize=10
453//      SetVariable setvar_0_DU_1,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_DU_1
454//      SetVariable setvar_0_DU_2,pos={368,149},size={70,16},title="File",fSize=10
455//      SetVariable setvar_0_DU_2,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_DU_2
456//      SetVariable setvar_0_DU_3,pos={368,173},size={70,16},title="File",fSize=10
457//      SetVariable setvar_0_DU_3,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_DU_3
458//      SetVariable setvar_0_DU_4,pos={368,197},size={70,16},title="File",fSize=10
459//      SetVariable setvar_0_DU_4,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_DU_4
460//      PopupMenu popup_0_DU_0,pos={476,99},size={210,20},title="Cell"
461//      PopupMenu popup_0_DU_0,mode=3,popvalue="none",value= #"D_CellNameList()"
462//      PopupMenu popup_0_DU_1,pos={476,122},size={210,20},title="Cell"
463//      PopupMenu popup_0_DU_1,mode=3,popvalue="none",value= #"D_CellNameList()"
464//      PopupMenu popup_0_DU_2,pos={476,146},size={102,20},title="Cell"
465//      PopupMenu popup_0_DU_2,mode=1,popvalue="none",value= #"D_CellNameList()"
466//      PopupMenu popup_0_DU_3,pos={476,170},size={102,20},title="Cell"
467//      PopupMenu popup_0_DU_3,mode=1,popvalue="none",value= #"D_CellNameList()"
468//      PopupMenu popup_0_DU_4,pos={476,194},size={102,20},title="Cell"
469//      PopupMenu popup_0_DU_4,mode=1,popvalue="none",value= #"D_CellNameList()"
470
471// DD
472        ListBox ListBox_0_DD,pos={33,286},size={200,130},proc=PolCor_FileListBoxProc,frame=2
473        ListBox ListBox_0_DD,listWave=root:Packages:NIST:Polarization:ListWave_0_DD,titleWave=root:Packages:NIST:Polarization:lbTitles
474        ListBox ListBox_0_DD,selWave=root:Packages:NIST:Polarization:lbSelWave_0_DD,mode= 6,selRow= 0,selCol= 0,editStyle= 2
475//      SetVariable setvar_0_DD_0,pos={33,286},size={70,16},title="File",fSize=10
476//      SetVariable setvar_0_DD_0,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_DD_0
477//      SetVariable setvar_0_DD_1,pos={33,309},size={70,16},title="File",fSize=10
478//      SetVariable setvar_0_DD_1,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_DD_1
479//      SetVariable setvar_0_DD_2,pos={33,333},size={70,16},title="File",fSize=10
480//      SetVariable setvar_0_DD_2,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_DD_2
481//      SetVariable setvar_0_DD_3,pos={33,357},size={70,16},title="File",fSize=10
482//      SetVariable setvar_0_DD_3,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_DD_3
483//      SetVariable setvar_0_DD_4,pos={33,381},size={70,16},title="File",fSize=10
484//      SetVariable setvar_0_DD_4,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_DD_4
485//      PopupMenu popup_0_DD_0,pos={141,283},size={210,20},title="Cell"
486//      PopupMenu popup_0_DD_0,mode=3,popvalue="none",value= #"D_CellNameList()"
487//      PopupMenu popup_0_DD_1,pos={141,306},size={102,20},title="Cell"
488//      PopupMenu popup_0_DD_1,mode=1,popvalue="none",value= #"D_CellNameList()"
489//      PopupMenu popup_0_DD_2,pos={141,330},size={102,20},title="Cell"
490//      PopupMenu popup_0_DD_2,mode=1,popvalue="none",value= #"D_CellNameList()"
491//      PopupMenu popup_0_DD_3,pos={141,354},size={102,20},title="Cell"
492//      PopupMenu popup_0_DD_3,mode=1,popvalue="none",value= #"D_CellNameList()"
493//      PopupMenu popup_0_DD_4,pos={141,378},size={102,20},title="Cell"
494//      PopupMenu popup_0_DD_4,mode=1,popvalue="none",value= #"D_CellNameList()"
495       
496// UD
497        ListBox ListBox_0_UD,pos={368,286},size={200,130},proc=PolCor_FileListBoxProc,frame=2
498        ListBox ListBox_0_UD,listWave=root:Packages:NIST:Polarization:ListWave_0_UD,titleWave=root:Packages:NIST:Polarization:lbTitles
499        ListBox ListBox_0_UD,selWave=root:Packages:NIST:Polarization:lbSelWave_0_UD,mode= 6,selRow= 0,selCol= 0,editStyle= 2
500//      SetVariable setvar_0_UD_0,pos={368,286},size={70,16},title="File",fSize=10
501//      SetVariable setvar_0_UD_0,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_UD_0
502//      SetVariable setvar_0_UD_1,pos={368,309},size={70,16},title="File",fSize=10
503//      SetVariable setvar_0_UD_1,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_UD_1
504//      SetVariable setvar_0_UD_2,pos={368,333},size={70,16},title="File",fSize=10
505//      SetVariable setvar_0_UD_2,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_UD_2
506//      SetVariable setvar_0_UD_3,pos={368,357},size={70,16},title="File",fSize=10
507//      SetVariable setvar_0_UD_3,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_UD_3
508//      SetVariable setvar_0_UD_4,pos={368,381},size={70,16},title="File",fSize=10
509//      SetVariable setvar_0_UD_4,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_0_UD_4
510//      PopupMenu popup_0_UD_0,pos={476,283},size={210,20},title="Cell"
511//      PopupMenu popup_0_UD_0,mode=3,popvalue="none",value= #"D_CellNameList()"
512//      PopupMenu popup_0_UD_1,pos={476,306},size={210,20},title="Cell"
513//      PopupMenu popup_0_UD_1,mode=3,popvalue="none",value= #"D_CellNameList()"
514//      PopupMenu popup_0_UD_2,pos={476,330},size={102,20},title="Cell"
515//      PopupMenu popup_0_UD_2,mode=1,popvalue="none",value= #"D_CellNameList()"
516//      PopupMenu popup_0_UD_3,pos={476,354},size={102,20},title="Cell"
517//      PopupMenu popup_0_UD_3,mode=1,popvalue="none",value= #"D_CellNameList()"
518//      PopupMenu popup_0_UD_4,pos={476,378},size={102,20},title="Cell"
519//      PopupMenu popup_0_UD_4,mode=1,popvalue="none",value= #"D_CellNameList()"
520
521
522// EMP Tab     
523        // UU
524        ListBox ListBox_1_UU,pos={34,102},size={200,130},proc=PolCor_FileListBoxProc,frame=2
525        ListBox ListBox_1_UU,listWave=root:Packages:NIST:Polarization:ListWave_1_UU,titleWave=root:Packages:NIST:Polarization:lbTitles
526        ListBox ListBox_1_UU,selWave=root:Packages:NIST:Polarization:lbSelWave_1_UU,mode= 6,selRow= 0,selCol= 0,editStyle= 2
527//      SetVariable setvar_1_UU_0,pos={34,102},size={70,16},title="File",fSize=10
528//      SetVariable setvar_1_UU_0,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_UU_0
529//      SetVariable setvar_1_UU_1,pos={34,125},size={70,16},title="File",fSize=10
530//      SetVariable setvar_1_UU_1,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_UU_1
531//      SetVariable setvar_1_UU_2,pos={34,149},size={70,16},title="File",fSize=10
532//      SetVariable setvar_1_UU_2,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_UU_2
533//      SetVariable setvar_1_UU_3,pos={34,173},size={70,16},title="File",fSize=10
534//      SetVariable setvar_1_UU_3,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_UU_3
535//      SetVariable setvar_1_UU_4,pos={34,197},size={70,16},title="File",fSize=10
536//      SetVariable setvar_1_UU_4,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_UU_4
537//      PopupMenu popup_1_UU_0,pos={142,99},size={210,20},title="Cell"
538//      PopupMenu popup_1_UU_0,mode=3,popvalue="none",value= #"D_CellNameList()"
539//      PopupMenu popup_1_UU_1,pos={142,122},size={102,20},title="Cell"
540//      PopupMenu popup_1_UU_1,mode=1,popvalue="none",value= #"D_CellNameList()"
541//      PopupMenu popup_1_UU_2,pos={142,146},size={102,20},title="Cell"
542//      PopupMenu popup_1_UU_2,mode=1,popvalue="none",value= #"D_CellNameList()"
543//      PopupMenu popup_1_UU_3,pos={142,170},size={102,20},title="Cell"
544//      PopupMenu popup_1_UU_3,mode=1,popvalue="none",value= #"D_CellNameList()"
545//      PopupMenu popup_1_UU_4,pos={142,194},size={102,20},title="Cell"
546//      PopupMenu popup_1_UU_4,mode=1,popvalue="none",value= #"D_CellNameList()"
547
548        // DU
549        ListBox ListBox_1_DU,pos={368,102},size={200,130},proc=PolCor_FileListBoxProc,frame=2
550        ListBox ListBox_1_DU,listWave=root:Packages:NIST:Polarization:ListWave_1_DU,titleWave=root:Packages:NIST:Polarization:lbTitles
551        ListBox ListBox_1_DU,selWave=root:Packages:NIST:Polarization:lbSelWave_1_DU,mode= 6,selRow= 0,selCol= 0,editStyle= 2
552//      SetVariable setvar_1_DU_0,pos={368,102},size={70,16},title="File",fSize=10
553//      SetVariable setvar_1_DU_0,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_DU_0
554//      SetVariable setvar_1_DU_1,pos={368,125},size={70,16},title="File",fSize=10
555//      SetVariable setvar_1_DU_1,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_DU_1
556//      SetVariable setvar_1_DU_2,pos={368,149},size={70,16},title="File",fSize=10
557//      SetVariable setvar_1_DU_2,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_DU_2
558//      SetVariable setvar_1_DU_3,pos={368,173},size={70,16},title="File",fSize=10
559//      SetVariable setvar_1_DU_3,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_DU_3
560//      SetVariable setvar_1_DU_4,pos={368,197},size={70,16},title="File",fSize=10
561//      SetVariable setvar_1_DU_4,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_DU_4
562//      PopupMenu popup_1_DU_0,pos={476,99},size={210,20},title="Cell"
563//      PopupMenu popup_1_DU_0,mode=3,popvalue="none",value= #"D_CellNameList()"
564//      PopupMenu popup_1_DU_1,pos={476,122},size={210,20},title="Cell"
565//      PopupMenu popup_1_DU_1,mode=3,popvalue="none",value= #"D_CellNameList()"
566//      PopupMenu popup_1_DU_2,pos={476,146},size={102,20},title="Cell"
567//      PopupMenu popup_1_DU_2,mode=1,popvalue="none",value= #"D_CellNameList()"
568//      PopupMenu popup_1_DU_3,pos={476,170},size={102,20},title="Cell"
569//      PopupMenu popup_1_DU_3,mode=1,popvalue="none",value= #"D_CellNameList()"
570//      PopupMenu popup_1_DU_4,pos={476,194},size={102,20},title="Cell"
571//      PopupMenu popup_1_DU_4,mode=1,popvalue="none",value= #"D_CellNameList()"
572
573// DD
574        ListBox ListBox_1_DD,pos={33,286},size={200,130},proc=PolCor_FileListBoxProc,frame=2
575        ListBox ListBox_1_DD,listWave=root:Packages:NIST:Polarization:ListWave_1_DD,titleWave=root:Packages:NIST:Polarization:lbTitles
576        ListBox ListBox_1_DD,selWave=root:Packages:NIST:Polarization:lbSelWave_1_DD,mode= 6,selRow= 0,selCol= 0,editStyle= 2
577//      SetVariable setvar_1_DD_0,pos={33,286},size={70,16},title="File",fSize=10
578//      SetVariable setvar_1_DD_0,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_DD_0
579//      SetVariable setvar_1_DD_1,pos={33,309},size={70,16},title="File",fSize=10
580//      SetVariable setvar_1_DD_1,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_DD_1
581//      SetVariable setvar_1_DD_2,pos={33,333},size={70,16},title="File",fSize=10
582//      SetVariable setvar_1_DD_2,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_DD_2
583//      SetVariable setvar_1_DD_3,pos={33,357},size={70,16},title="File",fSize=10
584//      SetVariable setvar_1_DD_3,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_DD_3
585//      SetVariable setvar_1_DD_4,pos={33,381},size={70,16},title="File",fSize=10
586//      SetVariable setvar_1_DD_4,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_DD_4
587//      PopupMenu popup_1_DD_0,pos={141,283},size={210,20},title="Cell"
588//      PopupMenu popup_1_DD_0,mode=3,popvalue="none",value= #"D_CellNameList()"
589//      PopupMenu popup_1_DD_1,pos={141,306},size={102,20},title="Cell"
590//      PopupMenu popup_1_DD_1,mode=1,popvalue="none",value= #"D_CellNameList()"
591//      PopupMenu popup_1_DD_2,pos={141,330},size={102,20},title="Cell"
592//      PopupMenu popup_1_DD_2,mode=1,popvalue="none",value= #"D_CellNameList()"
593//      PopupMenu popup_1_DD_3,pos={141,354},size={102,20},title="Cell"
594//      PopupMenu popup_1_DD_3,mode=1,popvalue="none",value= #"D_CellNameList()"
595//      PopupMenu popup_1_DD_4,pos={141,378},size={102,20},title="Cell"
596//      PopupMenu popup_1_DD_4,mode=1,popvalue="none",value= #"D_CellNameList()"
597
598// UD
599        ListBox ListBox_1_UD,pos={368,286},size={200,130},proc=PolCor_FileListBoxProc,frame=2
600        ListBox ListBox_1_UD,listWave=root:Packages:NIST:Polarization:ListWave_1_UD,titleWave=root:Packages:NIST:Polarization:lbTitles
601        ListBox ListBox_1_UD,selWave=root:Packages:NIST:Polarization:lbSelWave_1_UD,mode= 6,selRow= 0,selCol= 0,editStyle= 2
602//      SetVariable setvar_1_UD_0,pos={368,286},size={70,16},title="File",fSize=10
603//      SetVariable setvar_1_UD_0,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_UD_0
604//      SetVariable setvar_1_UD_1,pos={368,309},size={70,16},title="File",fSize=10
605//      SetVariable setvar_1_UD_1,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_UD_1
606//      SetVariable setvar_1_UD_2,pos={368,333},size={70,16},title="File",fSize=10
607//      SetVariable setvar_1_UD_2,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_UD_2
608//      SetVariable setvar_1_UD_3,pos={368,357},size={70,16},title="File",fSize=10
609//      SetVariable setvar_1_UD_3,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_UD_3
610//      SetVariable setvar_1_UD_4,pos={368,381},size={70,16},title="File",fSize=10
611//      SetVariable setvar_1_UD_4,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_1_UD_4
612//      PopupMenu popup_1_UD_0,pos={476,283},size={210,20},title="Cell"
613//      PopupMenu popup_1_UD_0,mode=3,popvalue="none",value= #"D_CellNameList()"
614//      PopupMenu popup_1_UD_1,pos={476,306},size={210,20},title="Cell"
615//      PopupMenu popup_1_UD_1,mode=3,popvalue="none",value= #"D_CellNameList()"
616//      PopupMenu popup_1_UD_2,pos={476,330},size={102,20},title="Cell"
617//      PopupMenu popup_1_UD_2,mode=1,popvalue="none",value= #"D_CellNameList()"
618//      PopupMenu popup_1_UD_3,pos={476,354},size={102,20},title="Cell"
619//      PopupMenu popup_1_UD_3,mode=1,popvalue="none",value= #"D_CellNameList()"
620//      PopupMenu popup_1_UD_4,pos={476,378},size={102,20},title="Cell"
621//      PopupMenu popup_1_UD_4,mode=1,popvalue="none",value= #"D_CellNameList()"
622
623// TODO
624// BKG Tab -- DU, DD, UD are not shown, since the background is not dependent on the flipper states, so only one background
625// file is necessary - this is "incorrectly" labeled as UU. I'll get around to changing this in the future...
626//
627        TitleBox title_2_UU,pos={350,100},size={400,48},title="\\f01BGD files are independent of polarization\rEnter all as UU",fSize=12
628
629
630        // UU
631        ListBox ListBox_2_UU,pos={34,102},size={200,130},proc=PolCor_FileListBoxProc,frame=2
632        ListBox ListBox_2_UU,listWave=root:Packages:NIST:Polarization:ListWave_2_UU,titleWave=root:Packages:NIST:Polarization:lbTitles
633        ListBox ListBox_2_UU,selWave=root:Packages:NIST:Polarization:lbSelWave_2_UU,mode= 6,selRow= 0,selCol= 0,editStyle= 2
634//      SetVariable setvar_2_UU_0,pos={34,102},size={70,16},title="File",fSize=10
635//      SetVariable setvar_2_UU_0,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_UU_0
636//      SetVariable setvar_2_UU_1,pos={34,125},size={70,16},title="File",fSize=10
637//      SetVariable setvar_2_UU_1,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_UU_1
638//      SetVariable setvar_2_UU_2,pos={34,149},size={70,16},title="File",fSize=10
639//      SetVariable setvar_2_UU_2,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_UU_2
640//      SetVariable setvar_2_UU_3,pos={34,173},size={70,16},title="File",fSize=10
641//      SetVariable setvar_2_UU_3,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_UU_3
642//      SetVariable setvar_2_UU_4,pos={34,197},size={70,16},title="File",fSize=10
643//      SetVariable setvar_2_UU_4,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_UU_4
644//      PopupMenu popup_2_UU_0,pos={142,99},size={210,20},title="Cell"
645//      PopupMenu popup_2_UU_0,mode=3,popvalue="none",value= #"D_CellNameList()"
646//      PopupMenu popup_2_UU_1,pos={142,122},size={102,20},title="Cell"
647//      PopupMenu popup_2_UU_1,mode=1,popvalue="none",value= #"D_CellNameList()"
648//      PopupMenu popup_2_UU_2,pos={142,146},size={102,20},title="Cell"
649//      PopupMenu popup_2_UU_2,mode=1,popvalue="none",value= #"D_CellNameList()"
650//      PopupMenu popup_2_UU_3,pos={142,170},size={102,20},title="Cell"
651//      PopupMenu popup_2_UU_3,mode=1,popvalue="none",value= #"D_CellNameList()"
652//      PopupMenu popup_2_UU_4,pos={142,194},size={102,20},title="Cell"
653//      PopupMenu popup_2_UU_4,mode=1,popvalue="none",value= #"D_CellNameList()"
654
655        // DU
656////////        ListBox ListBox_2_DU,pos={368,102},size={200,130},proc=PolCor_FileListBoxProc,frame=2
657////////        ListBox ListBox_2_DU,listWave=root:Packages:NIST:Polarization:ListWave_2_DU,titleWave=root:Packages:NIST:Polarization:lbTitles
658////////        ListBox ListBox_2_DU,selWave=root:Packages:NIST:Polarization:lbSelWave_2_DU,mode= 6,selRow= 0,selCol= 0,editStyle= 2
659//      SetVariable setvar_2_DU_0,pos={368,102},size={70,16},title="File",fSize=10
660//      SetVariable setvar_2_DU_0,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_DU_0
661//      SetVariable setvar_2_DU_1,pos={368,125},size={70,16},title="File",fSize=10
662//      SetVariable setvar_2_DU_1,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_DU_1
663//      SetVariable setvar_2_DU_2,pos={368,149},size={70,16},title="File",fSize=10
664//      SetVariable setvar_2_DU_2,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_DU_2
665//      SetVariable setvar_2_DU_3,pos={368,173},size={70,16},title="File",fSize=10
666//      SetVariable setvar_2_DU_3,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_DU_3
667//      SetVariable setvar_2_DU_4,pos={368,197},size={70,16},title="File",fSize=10
668//      SetVariable setvar_2_DU_4,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_DU_4
669//      PopupMenu popup_2_DU_0,pos={476,99},size={210,20},title="Cell"
670//      PopupMenu popup_2_DU_0,mode=3,popvalue="none",value= #"D_CellNameList()"
671//      PopupMenu popup_2_DU_1,pos={476,122},size={210,20},title="Cell"
672//      PopupMenu popup_2_DU_1,mode=3,popvalue="none",value= #"D_CellNameList()"
673//      PopupMenu popup_2_DU_2,pos={476,146},size={102,20},title="Cell"
674//      PopupMenu popup_2_DU_2,mode=1,popvalue="none",value= #"D_CellNameList()"
675//      PopupMenu popup_2_DU_3,pos={476,170},size={102,20},title="Cell"
676//      PopupMenu popup_2_DU_3,mode=1,popvalue="none",value= #"D_CellNameList()"
677//      PopupMenu popup_2_DU_4,pos={476,194},size={102,20},title="Cell"
678//      PopupMenu popup_2_DU_4,mode=1,popvalue="none",value= #"D_CellNameList()"
679
680// DD
681////////        ListBox ListBox_2_DD,pos={33,286},size={200,130},proc=PolCor_FileListBoxProc,frame=2
682////////        ListBox ListBox_2_DD,listWave=root:Packages:NIST:Polarization:ListWave_2_DD,titleWave=root:Packages:NIST:Polarization:lbTitles
683////////        ListBox ListBox_2_DD,selWave=root:Packages:NIST:Polarization:lbSelWave_2_DD,mode= 6,selRow= 0,selCol= 0,editStyle= 2
684//      SetVariable setvar_2_DD_0,pos={33,286},size={70,16},title="File",fSize=10
685//      SetVariable setvar_2_DD_0,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_DD_0
686//      SetVariable setvar_2_DD_1,pos={33,309},size={70,16},title="File",fSize=10
687//      SetVariable setvar_2_DD_1,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_DD_1
688//      SetVariable setvar_2_DD_2,pos={33,333},size={70,16},title="File",fSize=10
689//      SetVariable setvar_2_DD_2,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_DD_2
690//      SetVariable setvar_2_DD_3,pos={33,357},size={70,16},title="File",fSize=10
691//      SetVariable setvar_2_DD_3,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_DD_3
692//      SetVariable setvar_2_DD_4,pos={33,381},size={70,16},title="File",fSize=10
693//      SetVariable setvar_2_DD_4,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_DD_4
694//      PopupMenu popup_2_DD_0,pos={141,283},size={210,20},title="Cell"
695//      PopupMenu popup_2_DD_0,mode=3,popvalue="none",value= #"D_CellNameList()"
696//      PopupMenu popup_2_DD_1,pos={141,306},size={102,20},title="Cell"
697//      PopupMenu popup_2_DD_1,mode=1,popvalue="none",value= #"D_CellNameList()"
698//      PopupMenu popup_2_DD_2,pos={141,330},size={102,20},title="Cell"
699//      PopupMenu popup_2_DD_2,mode=1,popvalue="none",value= #"D_CellNameList()"
700//      PopupMenu popup_2_DD_3,pos={141,354},size={102,20},title="Cell"
701//      PopupMenu popup_2_DD_3,mode=1,popvalue="none",value= #"D_CellNameList()"
702//      PopupMenu popup_2_DD_4,pos={141,378},size={102,20},title="Cell"
703//      PopupMenu popup_2_DD_4,mode=1,popvalue="none",value= #"D_CellNameList()"
704
705// UD
706////////        ListBox ListBox_2_UD,pos={368,286},size={200,130},proc=PolCor_FileListBoxProc,frame=2
707////////        ListBox ListBox_2_UD,listWave=root:Packages:NIST:Polarization:ListWave_2_UD,titleWave=root:Packages:NIST:Polarization:lbTitles
708////////        ListBox ListBox_2_UD,selWave=root:Packages:NIST:Polarization:lbSelWave_2_UD,mode= 6,selRow= 0,selCol= 0,editStyle= 2
709//      SetVariable setvar_2_UD_0,pos={368,286},size={70,16},title="File",fSize=10
710//      SetVariable setvar_2_UD_0,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_UD_0
711//      SetVariable setvar_2_UD_1,pos={368,309},size={70,16},title="File",fSize=10
712//      SetVariable setvar_2_UD_1,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_UD_1
713//      SetVariable setvar_2_UD_2,pos={368,333},size={70,16},title="File",fSize=10
714//      SetVariable setvar_2_UD_2,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_UD_2
715//      SetVariable setvar_2_UD_3,pos={368,357},size={70,16},title="File",fSize=10
716//      SetVariable setvar_2_UD_3,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_UD_3
717//      SetVariable setvar_2_UD_4,pos={368,381},size={70,16},title="File",fSize=10
718//      SetVariable setvar_2_UD_4,limits={-inf,inf,0},value= root:Packages:NIST:Polarization:gStr_PolCor_2_UD_4
719//      PopupMenu popup_2_UD_0,pos={476,283},size={210,20},title="Cell"
720//      PopupMenu popup_2_UD_0,mode=3,popvalue="none",value= #"D_CellNameList()"
721//      PopupMenu popup_2_UD_1,pos={476,306},size={210,20},title="Cell"
722//      PopupMenu popup_2_UD_1,mode=3,popvalue="none",value= #"D_CellNameList()"
723//      PopupMenu popup_2_UD_2,pos={476,330},size={102,20},title="Cell"
724//      PopupMenu popup_2_UD_2,mode=1,popvalue="none",value= #"D_CellNameList()"
725//      PopupMenu popup_2_UD_3,pos={476,354},size={102,20},title="Cell"
726//      PopupMenu popup_2_UD_3,mode=1,popvalue="none",value= #"D_CellNameList()"
727//      PopupMenu popup_2_UD_4,pos={476,378},size={102,20},title="Cell"
728//      PopupMenu popup_2_UD_4,mode=1,popvalue="none",value= #"D_CellNameList()"
729
730EndMacro
731
732// action procedure for the list box that allows the popup menu
733// -- much easier to make a contextual popup with a list box than on a table/subwindow hook
734//
735Function PolCor_FileListBoxProc(lba) : ListBoxControl
736        STRUCT WMListboxAction &lba
737
738        Variable row = lba.row
739        Variable col = lba.col
740        WAVE/T/Z listWave = lba.listWave
741        WAVE/Z selWave = lba.selWave
742       
743        switch( lba.eventCode )
744                case -1: // control being killed
745                        break
746                case 1: // mouse down
747                        if (lba.col == 1)                                                                                       // cell list
748//                              SelWave[][][0] = SelWave[p][q] & ~9                                             // de-select everything to make sure we don't leave something selected in another column
749//                              SelWave[][s.col][0] = SelWave[p][s.col] | 1                             // select all rows
750//                              ControlUpdate/W=$(s.win) $(s.ctrlName)
751                                PopupContextualMenu D_CellNameList()
752                                if (V_flag > 0)
753                                        listWave[lba.row][lba.col] = S_Selection
754                                endif
755                        endif
756                        break
757                case 3: // double click
758                        break
759                case 4: // cell selection
760                case 5: // cell selection plus shift key
761                        break
762                case 6: // begin edit
763                        break
764                case 7: // finish edit
765                        break
766                case 13: // checkbox clicked (Igor 6.2 or later)
767                        break
768        endswitch
769
770        return 0
771End
772
773
774// This hook is activated when the whole window is killed. It saves the state of the popups and list boxes.
775// -- the protocol is not saved since it can be recalled
776//
777Function PolCorPanelHook(s)
778        STRUCT WMWinHookStruct &s
779
780        Variable hookResult = 0
781
782        switch(s.eventCode)
783//              case 0:                         // Activate
784//                      // Handle activate
785//                      break
786//
787//              case 1:                         // Deactivate
788//                      // Handle deactivate
789//                      break
790                case 2:                         // kill
791                        // Handle kill
792                       
793                        // the variables with the run numbers are automatically saved and restored if not re-initialized
794                        // get a list of all of the popups
795                        String popList="",item
796                        Variable num,ii
797
798//              -- old way, with popups for the conditions
799//                      popList=ControlNameList("PolCor_Panel",";","popup_*")
800//              -- new way - list boxes automatically saved, condition popup needs to be saved
801                        popList=ControlNameList("PolCor_Panel",";","popup*")
802                        num=ItemsInList(popList,";")
803                        Make/O/T/N=(num,2) root:Packages:NIST:Polarization:PolCor_popState
804                        Wave/T w=root:Packages:NIST:Polarization:PolCor_popState
805                        for(ii=0;ii<num;ii+=1)
806                                item=StringFromList(ii, popList,";")
807                                ControlInfo/W=PolCor_Panel $item
808                                w[ii][0] = item
809                                w[ii][1] = S_Value
810                        endfor
811
812                        break
813
814        endswitch
815
816        return hookResult               // 0 if nothing done, else 1
817End
818
819// val = 1 to disable
820// val = 0 to show
821Function ToggleSelControls(str,val)
822        String str
823        Variable val
824       
825        String listStr
826        listStr = ControlNameList("PolCor_Panel", ";", "*"+str+"*")
827//      print listStr
828       
829        ModifyControlList/Z listStr  , disable=(val)
830        return(0)
831end
832
833
834Function PolCorTabProc(tca) : TabControl
835        STRUCT WMTabControlAction &tca
836
837        switch( tca.eventCode )
838                case 2: // mouse up
839                        Variable tab = tca.tab         
840                        Variable val
841//                      Print "Selected tab = ",tab
842                       
843                        val = (tab != 0)
844//                      Print "tab 0 val = ",val
845                        ToggleSelControls("_0_",val)
846                       
847                        val = (tab != 1)
848//                      Print "tab 1 val = ",val
849                        ToggleSelControls("_1_",val)
850                       
851                        val = (tab != 2)
852//                      Print "tab 2 val = ",val
853                        ToggleSelControls("_2_",val)
854                                               
855                        break
856                case -1: // control being killed
857                        break
858        endswitch
859
860        return 0
861End
862
863// 0 = SAM, 1 = EMP, 2 = BGD
864Function ChangeDataTab(tab)
865        Variable tab
866        Variable val
867       
868        TabControl PolCorTab win=PolCor_Panel,value=tab
869       
870        // as if the tab was clicked
871        val = (tab != 0)
872//                      Print "tab 0 val = ",val
873        ToggleSelControls("_0_",val)
874       
875        val = (tab != 1)
876//                      Print "tab 1 val = ",val
877        ToggleSelControls("_1_",val)
878       
879        val = (tab != 2)
880//                      Print "tab 2 val = ",val
881        ToggleSelControls("_2_",val)
882
883        return(0)                       
884End
885
886Function PolCorHelpParButtonProc(ba) : ButtonControl
887        STRUCT WMButtonAction &ba
888
889        switch( ba.eventCode )
890                case 2: // mouse up
891                        // click code here
892                        DisplayHelpTopic/Z/K=1 "Polarization Correction Panel"
893                        if(V_flag !=0)
894                                DoAlert 0,"The Polarization Correction Panel Help file could not be found"
895                        endif
896                        break
897                case -1: // control being killed
898                        break
899        endswitch
900
901        return 0
902End
903
904
905// loads the specified type of data (SAM, EMP, BGD) based on the active tab
906// loads either specified spin type or all four
907//
908//  -- during loading, the proper row of the PolMatrix is filled, based on pType
909//
910// then after all data is loaded:
911//              -the inverse Inv_PolMatrix is calculated
912//              -the error in the PolMatrix is calculated
913//              -the error in Inv_PolMatrix is calculated
914//
915// TODO:
916// (( now, defaults to trying to load ALL four data spin states ))
917//              - I'll have to fix this later if only 2 of the four are needed
918//
919Function LoadRawPolarizedButton(ba) : ButtonControl
920        STRUCT WMButtonAction &ba
921
922        switch( ba.eventCode )
923                case 2: // mouse up
924                        // click code here
925                       
926                        // depends on which tab you're on
927                        // (maybe) select UD type, maybe force all to load
928                       
929                        String pType
930//                      Prompt pType,"Pol Type",popup,"UU;DU;DD;UD;All;"
931//                      DoPrompt "Type to load",pType
932//                      if (V_Flag)
933//                              return 0                                                                        // user canceled
934//                      endif
935
936                        pType = "All"
937                       
938                        if(cmpstr(pType,"All") == 0)
939                                LoadPolarizedData("UU")
940                                LoadPolarizedData("DU")
941                                LoadPolarizedData("DD")
942                                LoadPolarizedData("UD")
943                        else
944                                LoadPolarizedData(pType)
945                        endif
946                       
947                        String type
948                        Variable row,col,flag=1
949                        Variable ii,jj,aa,bb
950                       
951// The PolMatrix is filled on loading the data sets
952// once all files are added, take the SQRT of the error matrix
953// now calculate its inverse
954// -- first checking to be sure that no rows are zero - this will result in a singular matrix otherwise         
955                        ControlInfo/W=PolCor_Panel PolCorTab
956                        type = S_value
957                        WAVE matA = $("root:Packages:NIST:"+type+":PolMatrix")
958                        WAVE matA_err = $("root:Packages:NIST:"+type+":PolMatrix_err")
959                        matA_err = sqrt(matA_err)
960                       
961                        // check for zero rows before inverting -- means not all data loaded
962                        for(row=0;row<4;row+=1)
963                                if(matA[row][0] == 0 && matA[row][1] == 0 && matA[row][2] == 0 && matA[row][3] == 0)
964                                        Print "**** some elements of PolMatrix are zero. Inverse not calculated. Be sure that all four XS are loaded."
965                                        row=10
966                                        flag=0
967                                endif
968                        endfor
969                        if(flag)                //PolMatrix OK
970                                // calculate the inverse of the coefficient matrix
971                                SetDataFolder $("root:Packages:NIST:"+type)
972                                MatrixInverse/G matA
973                                Duplicate/O M_Inverse Inv_PolMatrix
974                               
975                                // now calculate the error of the inverse matrix
976                                Duplicate/O Inv_PolMatrix Inv_PolMatrix_err
977                                Inv_PolMatrix_err=0
978                               
979                                for(aa=0;aa<4;aa+=1)
980                                        for(bb=0;bb<4;bb+=1)
981                                                for(ii=0;ii<4;ii+=1)
982                                                        for(jj=0;jj<4;jj+=1)
983                                                                Inv_PolMatrix_err[aa][bb] += (Inv_PolMatrix[aa][ii])^2 * (matA_err[ii][jj])^2 * (Inv_PolMatrix[jj][bb])^2
984                                                        endfor
985                                                endfor
986//                                              Inv_PolMatrix_err[aa][bb] = sqrt(Inv_PolMatrix_err[aa][bb])
987                                        endfor
988                                endfor
989                               
990                                Inv_PolMatrix_err = sqrt(Inv_PolMatrix_err)
991                               
992                        endif
993       
994                        SetDataFolder root:
995                       
996                        break
997                case -1: // control being killed
998                        break
999        endswitch
1000
1001        return 0
1002End
1003
1004// Loading of the polarized scattering data
1005//
1006// with the file numbers set
1007// and the conditions specified
1008//
1009// -- For the specified pType of data (=UU, DU, DD, UD)
1010//
1011// -the time midpoint is found from the list of runs (needed for PCell(t) later)
1012// -raw data is loaded (from a list of run numbers)
1013// -data and loaded waves are tagged with a suffix (_UU, _DU, etc.)
1014// -the PolMatrix of coefficients is filled (the specified row)
1015//
1016// *** IF THE DATA IS BGD ***
1017// take the file list from UU every time, but load and put this in all 4 XS locations
1018//  *************************
1019//
1020// TODO:
1021// X- pre-parsing is not done to check for valid file numbers. This should be done gracefully.
1022// X- SAM folder is currently hard-wired
1023// X- if all of the conditions are "none" - stop and report the error
1024//
1025Function LoadPolarizedData(pType)
1026        String pType
1027
1028
1029        String listStr="",runList="",parsedRuns,condStr
1030        Variable ii,num,err,row
1031                       
1032        // get the current tab
1033        String type,runStr,cellStr
1034        Variable tabNum
1035        ControlInfo/W=PolCor_Panel PolCorTab
1036        type = S_value
1037        Print "selected data type = ",type
1038        tabNum = V_Value
1039       
1040       
1041        // get a list of the file numbers to load, must be in proper data folder
1042        SetDataFolder root:Packages:NIST:Polarization
1043//                      Print "Searching  "+"gStr_PolCor_"+num2str(tabNum)+"_*"+pType+"*"
1044//      listStr = StringList("gStr_PolCor_"+num2str(tabNum)+"_*"+pType+"*", ";" )
1045
1046////////////
1047        if(tabNum == 2)         //BGD data, read from UU every time, but don't change the pType tag
1048                Wave/T lb=$("ListWave_"+num2str(tabNum)+"_"+"UU")
1049        else
1050                Wave/T lb=$("ListWave_"+num2str(tabNum)+"_"+pType)
1051        endif
1052        num = DimSize(lb,0)             //should be 10, as initialized
1053       
1054        // if the condition (for all of the sets) is "none", get out
1055        ControlInfo/W=PolCor_Panel popup1
1056        condStr = S_Value
1057        if(cmpstr(condStr, "none" ) == 0)
1058                DoAlert 0,"Condition is not set."
1059                SetDataFolder root:
1060                return(0)
1061        endif
1062       
1063//                      step through, stop at the first null run number or missing cell specification
1064        for(row=0;row<num;row+=1)
1065                runStr = lb[row][0]
1066                CellStr = lb[row][1]
1067               
1068                if(strlen(runStr) > 0 && strlen(cellStr) > 0)                   //non-null entries, go on
1069                        runList += runStr+","           // this is a comma-delimited list
1070                else
1071                        if(strlen(runStr) == 0 && strlen(cellStr) == 0)
1072                                // not a problem, don't bother reporting
1073                        else
1074                                //report the error and stop
1075                                DoAlert 0,"run number or cell is not set in row "+num2str(row)+" for type "+pType
1076                                SetDataFolder root:
1077                                return(0)
1078                        endif
1079                endif
1080        endfor
1081
1082        Print runList
1083        // check for errors
1084        parsedRuns =ParseRunNumberList(runlist)
1085        if(strlen(parsedRuns) == 0)
1086                Print "enter a valid file number before proceeding"
1087                SetDataFolder root:
1088                return(0)
1089        endif
1090        SetDataFolder root:
1091
1092       
1093        // find time midpoint for the files to load
1094        Variable tMid
1095        tMid = getTimeMidpoint(runList)
1096        Print/D "time midpoint",tmid
1097       
1098        // this adds multiple raw data files, as specified by the list
1099        err = AddFilesInList(type,parsedRuns)           // adds to a work file = type, not RAW
1100        UpdateDisplayInformation(type)
1101       
1102        TagLoadedData(type,pType)               //see also DisplayTaggedData()
1103       
1104        // now add the appropriate bits to the matrix
1105        if(!WaveExists($("root:Packages:NIST:"+type+":PolMatrix")))
1106                Make/O/D/N=(4,4) $("root:Packages:NIST:"+type+":PolMatrix")
1107                Make/O/D/N=(4,4) $("root:Packages:NIST:"+type+":PolMatrix_err")
1108        endif
1109        WAVE matA = $("root:Packages:NIST:"+type+":PolMatrix")
1110        WAVE matA_err = $("root:Packages:NIST:"+type+":PolMatrix_err")
1111
1112//                      listStr = ControlNameList("PolCor_Panel",";","*"+pType+"*")
1113
1114        //This loops over all of the files and adds the coefficients to the PolMatrix
1115        // the PolMatrix rows are cleared on pass 0 as each pType data is loaded.
1116        // this way repeated loading will always result in the correct fill
1117        // returns the error matrix as the squared error (take sqrt in calling function)               
1118        AddToPolMatrix(matA,matA_err,pType,tMid)
1119
1120
1121        SetDataFolder root:
1122       
1123        return(0)
1124End
1125
1126
1127// by definition-- the rows are:
1128//
1129//      UU = 0
1130//      DU = 1
1131//      DD = 2
1132//      UD = 3
1133//
1134//
1135// TODO:
1136// -- check all of the math
1137// -- not yet using the midpoint time, even though it is passed in (?)
1138//              exactly where in the math would I use the time midpoint? The midpoint passed in is the
1139//    midpoint of all of the files in the list. Individual contributions to the matrix are here
1140//    calculated at the (start) time of each file. So is a midpoint really necessary?
1141//
1142// -- the PolMatrix_err returned from here is the squared error!
1143//
1144Function AddToPolMatrix(matA,matA_err,pType,tMid)
1145        Wave matA,matA_err
1146        String pType
1147        Variable tMid
1148       
1149        Variable row,Psm, PsmPf, PCell,err_Psm, err_PsmPf, err_PCell
1150        Variable ii,jj,muPo,err_muPo,gam,err_gam,monCts,t1,num,fileCount
1151        Variable Po,err_Po,Pt,err_Pt,Tmaj,Tmin,err_Tmaj,err_Tmin,Te,err_Te,mu,err_mu,summedMonCts
1152
1153        Variable ea_uu, ea_ud, ea_dd, ea_du
1154        Variable ec_uu, ec_ud, ec_dd, ec_du
1155       
1156        String listStr,fname="",condStr,condNote,decayNote,cellStr,t0Str,t1Str,runStr
1157
1158        // get the current tab
1159        String type
1160        Variable tabNum
1161        ControlInfo/W=PolCor_Panel PolCorTab
1162        type = S_value
1163        Print "selected data type = ",type
1164        tabNum = V_Value
1165       
1166        SetDataFolder root:Packages:NIST:Polarization
1167//      listStr = StringList("gStr_PolCor_"+num2str(tabNum)+"_*"+pType+"*", ";" )
1168
1169////////////
1170        if(tabNum == 2)         //BGD data, read from UU every time, but don't change the pType tag
1171                Wave/T lb=$("ListWave_"+num2str(tabNum)+"_"+"UU")
1172        else
1173                Wave/T lb=$("ListWave_"+num2str(tabNum)+"_"+pType)
1174        endif
1175        num = DimSize(lb,0)             //should be 10, as initialized
1176       
1177        // if the condition (for all of the sets) is "none", get out
1178        ControlInfo/W=PolCor_Panel popup1
1179        condStr = S_Value
1180        if(cmpstr(condStr, "none" ) == 0)
1181                DoAlert 0,"Condition is not set."
1182                SetDataFolder root:
1183                return(0)
1184        endif
1185       
1186        Wave condition = $("root:Packages:NIST:Polarization:Cells:"+condStr)
1187        // get wave note from condition
1188        condNote = note(condition)
1189        // get P's from note
1190        Psm = NumberByKey("P_sm", condNote, "=", ",", 0)
1191        PsmPf = NumberByKey("P_sm_f", condNote, "=", ",", 0)
1192        err_Psm = NumberByKey("err_P_sm", condNote, "=", ",", 0)
1193        err_PsmPf = NumberByKey("err_P_sm_f", condNote, "=", ",", 0)
1194
1195//
1196//
1197//              find the proper propotions to add the matrix contributions
1198//              if only one file, this = 1, otherwise it should sum to one
1199//
1200        Make/O/D/N=10 proportion
1201        proportion = 0
1202        summedMonCts = 0
1203        // loop over the (10) rows in the listWave
1204        for(ii=0;ii<num;ii+=1)
1205                runStr =        lb[ii][0]               //the run number
1206                if(cmpstr(runStr, "" ) != 0)
1207                        fname = FindFileFromRunNumber(str2num(runStr))
1208                        proportion[ii] = getMonitorCount(fname)
1209                        summedMonCts += proportion[ii]
1210                endif
1211        endfor
1212        proportion /= summedMonCts
1213
1214        // loop over the (10) rows in the listWave
1215        fileCount=0
1216        for(ii=0;ii<num;ii+=1)
1217                runStr =        lb[ii][0]               //the run number
1218                if(cmpstr(runStr, "" ) != 0)
1219               
1220                        fileCount += 1          //one more file is added
1221                        // get run number (str)
1222                        // get file name
1223                        fname = FindFileFromRunNumber(str2num(runStr))
1224               
1225                        // get the cell string to get the Decay wave
1226                        cellStr = lb[ii][1]
1227//                      print "Cell = ",cellStr
1228                        // get info to calc Pcell (from the Decay wave)
1229                        Wave decay = $("root:Packages:NIST:Polarization:Cells:Decay_"+cellStr) 
1230                        decayNote=note(decay)
1231               
1232                        t0Str = StringByKey("T0", decayNote, "=", ",", 0)
1233                        muPo = NumberByKey("muP", decayNote, "=", ",", 0)
1234                        err_muPo = NumberByKey("err_muP", decayNote, "=", ",", 0)
1235                        gam = NumberByKey("gamma", decayNote, "=", ",", 0)
1236                        err_gam = NumberByKey("err_gamma", decayNote, "=", ",", 0)
1237                        Po = NumberByKey("P0", decayNote, "=", ",", 0)
1238                        err_Po = NumberByKey("err_P0", decayNote, "=", ",", 0)
1239                        // get the elapsed time to calculate PCell at the current file time
1240                        t1str = getFileCreationDate(fname)
1241                        t1 = ElapsedHours(t0Str,t1Str)
1242                       
1243                        PCell = Calc_PCell_atT(muPo,err_muPo,gam,err_gam,t1,err_PCell)
1244                       
1245
1246                       
1247                        SVAR cellParamStr = $("root:Packages:NIST:Polarization:Cells:gCell_"+cellStr)
1248                       
1249                        Pt = Calc_PHe_atT(Po,err_Po,gam,err_gam,t1,err_Pt)
1250                       
1251                        Tmaj = Calc_Tmaj(cellParamStr,Pt,err_Pt,err_Tmaj)
1252                        Tmin = Calc_Tmin(cellParamStr,Pt,err_Pt,err_Tmin)
1253
1254//                      printf "File: %s\r",fname
1255//                      printf "Elapsed time = %g hours\r",t1
1256//                      printf "Pcell = %g\tTMaj = %g\tTmin = %g\r",PCell,(1+PCell)/2,(1-Pcell)/2
1257//                      printf "\t\tRecalculated TMaj = %g\tTmin = %g\r",Tmaj,Tmin
1258                                               
1259                        // get file info (monitor counts)
1260//                      monCts = getMonitorCount(fname)
1261//                      monCts /= 1e8           //to get a normalized value to add proportionally
1262                       
1263                        // use the proper proportion of each file to add to each row
1264//                      monCts = proportion[ii]
1265                       
1266                        Variable err_monCts
1267                        err_monCts = sqrt(monCts)
1268                        // add appropriate values to matrix elements (switch on UU, DD, etc)
1269                        // error in monCts is a negligible contribution
1270                        strswitch(pType)
1271                                case "UU":             
1272                                        row = 0
1273                                        if(ii==0)
1274                                                matA[row][] = 0
1275                                                matA_err[row][] = 0
1276                                        endif
1277// original version
1278//                                      ea_uu = (1+Psm)/2
1279//                                      ea_du = (1-Psm)/2
1280//                                      ec_uu = (1+Pcell)/2
1281//                                      ec_du = (1-Pcell)/2
1282//                                     
1283//                                      matA[row][0] += ea_uu*ec_uu*monCts
1284//                                      matA[row][1] += ea_du*ec_uu*monCts
1285//                                      matA[row][2] += ea_du*ec_du*monCts
1286//                                      matA[row][3] += ea_uu*ec_du*monCts
1287//
1288//                                      matA_err[row][0] += (ea_uu*ec_uu*monCts)^2 * (err_Psm^2/Psm^2 + err_Pcell^2/Pcell^2)
1289//                                      matA_err[row][1] += (ea_du*ec_uu*monCts)^2 * (err_Psm^2/Psm^2 + err_Pcell^2/Pcell^2)
1290//                                      matA_err[row][2] += (ea_du*ec_du*monCts)^2 * (err_Psm^2/Psm^2 + err_Pcell^2/Pcell^2)
1291//                                      matA_err[row][3] += (ea_uu*ec_du*monCts)^2 * (err_Psm^2/Psm^2 + err_Pcell^2/Pcell^2)
1292// end original version
1293
1294// using Tmaj, Tmin calc from Po, not Pcell                                     
1295                                        matA[row][0] += (1+Psm)*Tmaj*proportion[ii]
1296                                        matA[row][1] += (1-Psm)*Tmaj*proportion[ii]
1297                                        matA[row][2] += (1-Psm)*Tmin*proportion[ii]
1298                                        matA[row][3] += (1+Psm)*Tmin*proportion[ii]
1299
1300// this seems to be too large... do I need to add the errors in proportion too? squared?
1301                                        matA_err[row][0] += proportion[ii]*( (Tmaj)^2*err_Psm^2 + (1+Psm)^2*err_Tmaj^2 )
1302                                        matA_err[row][1] += proportion[ii]*( (Tmaj)^2*err_Psm^2 + (1-Psm)^2*err_Tmaj^2 )
1303                                        matA_err[row][2] += proportion[ii]*( (Tmin)^2*err_Psm^2 + (1-Psm)^2*err_Tmin^2 )
1304                                        matA_err[row][3] += proportion[ii]*( (Tmin)^2*err_Psm^2 + (1+Psm)^2*err_Tmin^2 )
1305                                               
1306                                        break
1307                                case "DU":             
1308                                        row = 1
1309                                        if(ii==0)
1310                                                matA[row][] = 0
1311                                                matA_err[row][] = 0
1312                                        endif
1313// original version
1314//                                      ea_ud = (1-PsmPf)/2
1315//                                      ea_dd = (1+PsmPf)/2
1316//                                      ec_uu = (1+Pcell)/2
1317//                                      ec_du = (1-Pcell)/2
1318//                                     
1319//                                      matA[row][0] += ea_ud*ec_uu*monCts
1320//                                      matA[row][1] += ea_dd*ec_uu*monCts
1321//                                      matA[row][2] += ea_dd*ec_du*monCts
1322//                                      matA[row][3] += ea_ud*ec_du*monCts
1323//                                     
1324//                                      matA_err[row][0] += (ea_ud*ec_uu*monCts)^2 * (err_PsmPf^2/PsmPf^2 + err_Pcell^2/Pcell^2)
1325//                                      matA_err[row][1] += (ea_dd*ec_uu*monCts)^2 * (err_PsmPf^2/PsmPf^2 + err_Pcell^2/Pcell^2)
1326//                                      matA_err[row][2] += (ea_dd*ec_du*monCts)^2 * (err_PsmPf^2/PsmPf^2 + err_Pcell^2/Pcell^2)
1327//                                      matA_err[row][3] += (ea_ud*ec_du*monCts)^2 * (err_PsmPf^2/PsmPf^2 + err_Pcell^2/Pcell^2)
1328// original version
1329
1330// using Tmaj, Tmin calc from Po, not Pcell                                     
1331                                        matA[row][0] += (1-PsmPf)*Tmaj*proportion[ii]
1332                                        matA[row][1] += (1+PsmPf)*Tmaj*proportion[ii]
1333                                        matA[row][2] += (1+PsmPf)*Tmin*proportion[ii]
1334                                        matA[row][3] += (1-PsmPf)*Tmin*proportion[ii]
1335
1336// this seems to be too large... do I need to add the errors in proportion too? squared?
1337                                        matA_err[row][0] += proportion[ii]*( (Tmaj)^2*err_PsmPf^2 + (1-PsmPf)^2*err_Tmaj^2 )
1338                                        matA_err[row][1] += proportion[ii]*( (Tmaj)^2*err_PsmPf^2 + (1+PsmPf)^2*err_Tmaj^2 )
1339                                        matA_err[row][2] += proportion[ii]*( (Tmin)^2*err_PsmPf^2 + (1+PsmPf)^2*err_Tmin^2 )
1340                                        matA_err[row][3] += proportion[ii]*( (Tmin)^2*err_PsmPf^2 + (1-PsmPf)^2*err_Tmin^2 )
1341
1342
1343                                        break   
1344                                case "DD":             
1345                                        row = 2
1346                                        if(ii==0)
1347                                                matA[row][] = 0
1348                                                matA_err[row][] = 0
1349                                        endif
1350// original version
1351//                                      ea_ud = (1-PsmPf)/2
1352//                                      ea_dd = (1+PsmPf)/2
1353//                                      ec_ud = (1-Pcell)/2
1354//                                      ec_dd = (1+Pcell)/2
1355//                                     
1356//                                      matA[row][0] += ea_ud*ec_ud*monCts
1357//                                      matA[row][1] += ea_dd*ec_ud*monCts
1358//                                      matA[row][2] += ea_dd*ec_dd*monCts
1359//                                      matA[row][3] += ea_ud*ec_dd*monCts                                     
1360//
1361//                                      matA_err[row][0] += (ea_ud*ec_ud*monCts)^2 * (err_PsmPf^2/PsmPf^2 + err_Pcell^2/Pcell^2)
1362//                                      matA_err[row][1] += (ea_dd*ec_ud*monCts)^2 * (err_PsmPf^2/PsmPf^2 + err_Pcell^2/Pcell^2)
1363//                                      matA_err[row][2] += (ea_dd*ec_dd*monCts)^2 * (err_PsmPf^2/PsmPf^2 + err_Pcell^2/Pcell^2)
1364//                                      matA_err[row][3] += (ea_ud*ec_dd*monCts)^2 * (err_PsmPf^2/PsmPf^2 + err_Pcell^2/Pcell^2)
1365// original version
1366
1367// using Tmaj, Tmin calc from Po, not Pcell                                     
1368                                        matA[row][0] += (1-PsmPf)*Tmin*proportion[ii]
1369                                        matA[row][1] += (1+PsmPf)*Tmin*proportion[ii]
1370                                        matA[row][2] += (1+PsmPf)*Tmaj*proportion[ii]
1371                                        matA[row][3] += (1-PsmPf)*Tmaj*proportion[ii]
1372
1373// this seems to be too large... do I need to add the errors in proportion too? squared?
1374                                        matA_err[row][0] += proportion[ii]*( (Tmin)^2*err_PsmPf^2 + (1-PsmPf)^2*err_Tmin^2 )
1375                                        matA_err[row][1] += proportion[ii]*( (Tmin)^2*err_PsmPf^2 + (1+PsmPf)^2*err_Tmin^2 )
1376                                        matA_err[row][2] += proportion[ii]*( (Tmaj)^2*err_PsmPf^2 + (1+PsmPf)^2*err_Tmaj^2 )
1377                                        matA_err[row][3] += proportion[ii]*( (Tmaj)^2*err_PsmPf^2 + (1-PsmPf)^2*err_Tmaj^2 )
1378                                                                               
1379                                        break                                           
1380                                case "UD":             
1381                                        row = 3
1382                                        if(ii==0)
1383                                                matA[row][] = 0
1384                                                matA_err[row][] = 0
1385                                        endif
1386// original version
1387//                                      ea_uu = (1+Psm)/2
1388//                                      ea_du = (1-Psm)/2
1389//                                      ec_ud = (1-Pcell)/2
1390//                                      ec_dd = (1+Pcell)/2
1391//                                     
1392//                                      matA[row][0] += ea_uu*ec_ud*monCts
1393//                                      matA[row][1] += ea_du*ec_ud*monCts
1394//                                      matA[row][2] += ea_du*ec_dd*monCts
1395//                                      matA[row][3] += ea_uu*ec_dd*monCts                                     
1396//                                                                             
1397//                                      matA_err[row][0] += (ea_uu*ec_ud*monCts)^2 * (err_Psm^2/Psm^2 + err_Pcell^2/Pcell^2)
1398//                                      matA_err[row][1] += (ea_du*ec_ud*monCts)^2 * (err_Psm^2/Psm^2 + err_Pcell^2/Pcell^2)
1399//                                      matA_err[row][2] += (ea_du*ec_dd*monCts)^2 * (err_Psm^2/Psm^2 + err_Pcell^2/Pcell^2)
1400//                                      matA_err[row][3] += (ea_uu*ec_dd*monCts)^2 * (err_Psm^2/Psm^2 + err_Pcell^2/Pcell^2)
1401// original version
1402       
1403// using Tmaj, Tmin calc from Po, not Pcell                                     
1404                                        matA[row][0] += (1+Psm)*Tmin*proportion[ii]
1405                                        matA[row][1] += (1-Psm)*Tmin*proportion[ii]
1406                                        matA[row][2] += (1-Psm)*Tmaj*proportion[ii]
1407                                        matA[row][3] += (1+Psm)*Tmaj*proportion[ii]
1408
1409// this seems to be too large... do I need to add the errors in proportion too? squared?
1410                                        matA_err[row][0] += proportion[ii]*( (Tmin)^2*err_Psm^2 + (1+Psm)^2*err_Tmin^2 )
1411                                        matA_err[row][1] += proportion[ii]*( (Tmin)^2*err_Psm^2 + (1-Psm)^2*err_Tmin^2 )
1412                                        matA_err[row][2] += proportion[ii]*( (Tmaj)^2*err_Psm^2 + (1-Psm)^2*err_Tmaj^2 )
1413                                        matA_err[row][3] += proportion[ii]*( (Tmaj)^2*err_Psm^2 + (1+Psm)^2*err_Tmaj^2 )
1414                                                                                                                                                       
1415                                        break
1416                        endswitch
1417
1418                endif
1419        endfor
1420       
1421// can't take the SQRT here, since the matrix won't necessarily be full yet,
1422       
1423        SetDataFolder root:
1424        return(0)
1425End
1426
1427
1428// duplicate the correct waves for use as the PolCor result
1429// lots of extra waves, but it makes things easier down the road
1430// type is the data folder (=SAM, etc)
1431// pType is the pol extension (=UU, etc.)
1432Function MakePCResultWaves(type,pType)
1433        String type,pType
1434
1435        ptype = "_" + pType                     // add an extra underscore
1436        String pcExt = "_pc"
1437        String destPath = "root:Packages:NIST:" + type
1438        Duplicate/O $(destPath + ":data"+pType), $(destPath + ":data"+pType+pcExt)
1439        Duplicate/O $(destPath + ":linear_data"+pType),$(destPath + ":linear_data"+pType+pcExt)
1440        Duplicate/O $(destPath + ":linear_data_error"+pType),$(destPath + ":linear_data_error"+pType+pcExt)
1441        Duplicate/O $(destPath + ":textread"+pType),$(destPath + ":textread"+pType+pcExt)
1442        Duplicate/O $(destPath + ":integersread"+pType),$(destPath + ":integersread"+pType+pcExt)
1443        Duplicate/O $(destPath + ":realsread"+pType),$(destPath + ":realsread"+pType+pcExt)
1444       
1445        return(0)
1446End
1447
1448// TODO
1449// -- clean up the wave note method to get the file loaded information passed around for display
1450// a function to tag the data in a particular folder with the UD state
1451Function TagLoadedData(type,pType)
1452        String type,pType
1453
1454        ConvertFolderToLinearScale(type)
1455
1456        ptype = "_" + pType                     // add an extra underscore
1457        String destPath = "root:Packages:NIST:" + type
1458        Duplicate/O $(destPath + ":data"), $(destPath + ":data"+pType)
1459        Duplicate/O $(destPath + ":linear_data"),$(destPath + ":linear_data"+pType)
1460        Duplicate/O $(destPath + ":linear_data_error"),$(destPath + ":linear_data_error"+pType)
1461        Duplicate/O $(destPath + ":textread"),$(destPath + ":textread"+pType)
1462        Duplicate/O $(destPath + ":integersread"),$(destPath + ":integersread"+pType)
1463        Duplicate/O $(destPath + ":realsread"),$(destPath + ":realsread"+pType)
1464       
1465        SVAR FileList = $(destPath + ":FileList")               //stick the list of files as a wave note. Very inelegant...
1466        Note $(destPath + ":textread"+pType),FileList
1467       
1468       
1469        return(0)
1470End
1471
1472// TODO
1473// -- Be sure that fRawWindowHook is picking up all of the correct information - identifying what the
1474//    displayed data set really is...
1475//
1476// a procedure (easier than a function) to point the current data to the tagged data
1477Proc DisplayTaggedData(type,pType)
1478        String type="SAM",pType="UU"
1479
1480        String/G root:myGlobals:gDataDisplayType=type
1481        ConvertFolderToLinearScale(type)
1482       
1483        ptype = "_" + pType                     // add an extra underscore
1484        String destPath = "root:Packages:NIST:" + type
1485        $(destPath + ":linear_data") = $(destPath + ":linear_data"+pType)
1486        $(destPath + ":linear_data_error") = $(destPath + ":linear_data_error"+pType)
1487        $(destPath + ":textread") = $(destPath + ":textread"+pType)
1488        $(destPath + ":integersread") = $(destPath + ":integersread"+pType)
1489        $(destPath + ":realsread") = $(destPath + ":realsread"+pType)
1490
1491// make the data equal to linear data
1492        $(destPath + ":data") = $(destPath + ":linear_data"+pType)
1493
1494
1495        root:myGlobals:gDataDisplayType = type + pType
1496       
1497        UpdateDisplayInformation(type)
1498
1499        //update the displayed filename, using FileList in the current data folder
1500        String/G root:myGlobals:gCurDispFile = note($(destPath + ":textread"+pType))            //read from the wave note
1501       
1502End
1503
1504
1505// this takes the 4 loaded experimental cross sections, and solves for the
1506// polarization corrected result.
1507//
1508// exp cross sections have _UU extensions
1509// polCor result has _UU_pc
1510//
1511// error propagation through the inversion follows the paper...
1512//
1513Function PolCorButton(ba) : ButtonControl
1514        STRUCT WMButtonAction &ba
1515
1516        switch( ba.eventCode )
1517                case 2: // mouse up
1518                        // click code here
1519                       
1520                        Variable ii,jj,numRows,numCols,row,kk
1521                       
1522                        // get the current tab
1523                        String type
1524                        Variable tabNum
1525                        ControlInfo/W=PolCor_Panel PolCorTab
1526                        type = S_value
1527                        Print "selected data type = ",type
1528                        tabNum = V_Value
1529                       
1530                        // make waves for the result
1531                        // these duplicate the data and add "_pc" for later use
1532                        // linear_data_error_UU_pc etc. are created
1533                        MakePCResultWaves(type,"UU")
1534                        MakePCResultWaves(type,"DU")
1535                        MakePCResultWaves(type,"DD")
1536                        MakePCResultWaves(type,"UD")
1537                       
1538                       
1539                        SetDataFolder $("root:Packages:NIST:"+type)
1540
1541// the linear data and its errors, declare and initialize                       
1542                        WAVE linear_data_UU_pc = linear_data_UU_pc
1543                        WAVE linear_data_DU_pc = linear_data_DU_pc
1544                        WAVE linear_data_DD_pc = linear_data_DD_pc
1545                        WAVE linear_data_UD_pc = linear_data_UD_pc
1546                        WAVE linear_data_error_UU_pc = linear_data_error_UU_pc
1547                        WAVE linear_data_error_DU_pc = linear_data_error_DU_pc
1548                        WAVE linear_data_error_DD_pc = linear_data_error_DD_pc
1549                        WAVE linear_data_error_UD_pc = linear_data_error_UD_pc
1550                                               
1551                        linear_data_UU_pc = 0
1552                        linear_data_DU_pc = 0
1553                        linear_data_DD_pc = 0
1554                        linear_data_UD_pc = 0
1555                        linear_data_error_UU_pc = 0
1556                        linear_data_error_DU_pc = 0
1557                        linear_data_error_DD_pc = 0
1558                        linear_data_error_UD_pc = 0
1559                                               
1560                        // make a temp wave for the experimental data vector
1561                        Make/O/D/N=4 vecB
1562                        WAVE vecB = vecB
1563                       
1564                        // the coefficient matrix and the experimental data
1565                        WAVE matA = $("root:Packages:NIST:"+type+":PolMatrix")
1566                        WAVE linear_data_UU = linear_data_UU
1567                        WAVE linear_data_DU = linear_data_DU
1568                        WAVE linear_data_DD = linear_data_DD
1569                        WAVE linear_data_UD = linear_data_UD
1570
1571// everything needed for the error calculation
1572// the PolMatrix error matrices
1573// and the data error
1574                        WAVE inv = Inv_PolMatrix
1575                        WAVE inv_err = Inv_PolMatrix_err                       
1576                        WAVE linear_data_error_UU = linear_data_error_UU
1577                        WAVE linear_data_error_DU = linear_data_error_DU
1578                        WAVE linear_data_error_DD = linear_data_error_DD
1579                        WAVE linear_data_error_UD = linear_data_error_UD                       
1580                       
1581                        numRows = DimSize(linear_data_UU_pc, 0 )
1582                        numCols = DimSize(linear_data_UU_pc, 1 )
1583                       
1584                        // this is certainly not slow. takes < 1 second to complete the double loop.
1585                        for(ii=0;ii<numRows;ii+=1)
1586                                for(jj=0;jj<numCols;jj+=1)
1587                                        vecB[0] = linear_data_UU[ii][jj]
1588                                        vecB[1] = linear_data_DU[ii][jj]
1589                                        vecB[2] = linear_data_DD[ii][jj]
1590                                        vecB[3] = linear_data_UD[ii][jj]
1591                               
1592                                        MatrixLinearSolve/M=1 matA vecB
1593                                        // result is M_B[][0]
1594                                        WAVE M_B = M_B
1595                                        linear_data_UU_pc[ii][jj] = M_B[0][0]
1596                                        linear_data_DU_pc[ii][jj] = M_B[1][0]
1597                                        linear_data_DD_pc[ii][jj] = M_B[2][0]
1598                                        linear_data_UD_pc[ii][jj] = M_B[3][0]
1599                               
1600                       
1601                                        // and the error at each pixel, once for each of the four
1602                                        row = 0         //== UU
1603                                        for(kk=0;kk<4;kk+=1)
1604                                                linear_data_error_UU_pc[ii][jj] += inv_err[row][kk]^2*linear_data_UU[ii][jj]^2 + inv[row][kk]^2*linear_data_error_UU[ii][jj]^2
1605                                        endfor
1606                                        row = 1         //== DU
1607                                        for(kk=0;kk<4;kk+=1)
1608                                                linear_data_error_DU_pc[ii][jj] += inv_err[row][kk]^2*linear_data_DU[ii][jj]^2 + inv[row][kk]^2*linear_data_error_DU[ii][jj]^2
1609                                        endfor
1610                                        row = 2         //== DD
1611                                        for(kk=0;kk<4;kk+=1)
1612                                                linear_data_error_DD_pc[ii][jj] += inv_err[row][kk]^2*linear_data_DD[ii][jj]^2 + inv[row][kk]^2*linear_data_error_DD[ii][jj]^2
1613                                        endfor
1614                                        row = 3         //== UD
1615                                        for(kk=0;kk<4;kk+=1)
1616                                                linear_data_error_UD_pc[ii][jj] += inv_err[row][kk]^2*linear_data_UD[ii][jj]^2 + inv[row][kk]^2*linear_data_error_UD[ii][jj]^2
1617                                        endfor
1618                                       
1619                               
1620                                endfor
1621                        //      Print ii       
1622                        endfor
1623                       
1624                        // sqrt of the squared error...
1625                        linear_data_error_UU_pc = sqrt(linear_data_error_UU_pc)
1626                        linear_data_error_DU_pc = sqrt(linear_data_error_DU_pc)
1627                        linear_data_error_DD_pc = sqrt(linear_data_error_DD_pc)
1628                        linear_data_error_UD_pc = sqrt(linear_data_error_UD_pc)
1629                       
1630                       
1631                        //update the data as log of the linear. more correct to use the default scaling
1632                        // this is necessary for proper display of the data
1633                        SetDataFolder $("root:Packages:NIST:"+type)                     // this should be redundant, but I somehow eneded up in root: here???
1634
1635                        WAVE data_UU_pc = data_UU_pc
1636                        WAVE data_DU_pc = data_DU_pc
1637                        WAVE data_DD_pc = data_DD_pc
1638                        WAVE data_UD_pc = data_UD_pc
1639                        data_UU_pc = log(linear_data_UU_pc)
1640                        data_DU_pc = log(linear_data_DU_pc)
1641                        data_DD_pc = log(linear_data_DD_pc)
1642                        data_UD_pc = log(linear_data_UD_pc)
1643                       
1644                       
1645                        SetDataFolder root:
1646                       
1647                        break
1648                case -1: // control being killed
1649                        break
1650        endswitch
1651
1652        return 0
1653End
1654
1655
1656// display all 4 XS at once in the same graph
1657// - crude right now
1658//
1659Function Display4XSButton(ba) : ButtonControl
1660        STRUCT WMButtonAction &ba
1661
1662        switch( ba.eventCode )
1663                case 2: // mouse up
1664                        // click code here
1665                        String dataType,pType,str,scaling
1666                        Prompt dataType,"Display WORK data type",popup,"SAM;EMP;BGD;DIV;COR;CAL;RAW;ABS;STO;SUB;DRK;SAS;"
1667//                      Prompt pType,"Pol Type",popup,"UU;DU;DD;UD;UU_pc;DU_pc;DD_pc;UD_pc;"
1668                        Prompt scaling,"scaling",popup,"log;linear;"
1669                        DoPrompt "Change Display",dataType,scaling
1670                       
1671                        if(V_flag==0)           //continue
1672                                Display_4(dataType,scaling)
1673                        endif                   
1674                        break
1675                case -1: // control being killed
1676                        break
1677        endswitch
1678
1679        return 0
1680End
1681
1682
1683// Change the displayed SANS data by pointing the data with the
1684// specified tag (suffix) at data, linear_data, etc.
1685// all of the read waves are pointed too.
1686//
1687Function ChangeDisplayedPolData(ba) : ButtonControl
1688        STRUCT WMButtonAction &ba
1689
1690        switch( ba.eventCode )
1691                case 2: // mouse up
1692                        // click code here
1693                        String dataType,pType,str
1694                        Prompt dataType,"Display WORK data type",popup,"SAM;EMP;BGD;DIV;COR;CAL;RAW;ABS;STO;SUB;DRK;SAS;"
1695                        Prompt pType,"Pol Type",popup,"UU;DU;DD;UD;UU_pc;DU_pc;DD_pc;UD_pc;"
1696                        DoPrompt "Change Display",dataType,pType
1697                       
1698                        if(V_flag==0)           //continue
1699                                sprintf str,"DisplayTaggedData(\"%s\",\"%s\")",dataType,pType
1700                                Execute str
1701                        endif
1702                        break
1703                case -1: // control being killed
1704                        break
1705        endswitch
1706
1707        return 0
1708End
1709
1710
1711// display the 4x4 polariztion efficiency matrix in a table, and its inverse (if it exists)
1712Function ShowPolMatrixButton(ba) : ButtonControl
1713        STRUCT WMButtonAction &ba
1714
1715        switch( ba.eventCode )
1716                case 2: // mouse up
1717                        // click code here
1718                       
1719                        // get the current tab
1720                        String type
1721                        Variable tabNum
1722                        ControlInfo/W=PolCor_Panel PolCorTab
1723                        type = S_value
1724                        Print "selected data type = ",type
1725                        tabNum = V_Value
1726                       
1727                        Wave/Z Pol = $("root:Packages:NIST:"+type+":PolMatrix")
1728                        if(WaveExists(Pol))
1729                                Edit/W=(5,44,510,251)/K=1 Pol
1730                        endif
1731                        Wave/Z Inv_Pol = $("root:Packages:NIST:"+type+":Inv_PolMatrix")
1732                        if(WaveExists(Inv_Pol))
1733                                Edit/W=(6,506,511,713)/K=1 Inv_Pol
1734                        endif
1735                        Wave/Z Pol_err = $("root:Packages:NIST:"+type+":PolMatrix_err")
1736                        if(WaveExists(Pol_err))
1737                                Edit/W=(5,275,510,482)/K=1 Pol_err
1738                        endif
1739                        Wave/Z Inv_Pol_err = $("root:Packages:NIST:"+type+":Inv_PolMatrix_err")
1740                        if(WaveExists(Inv_Pol_err))
1741                                Edit/W=(6,736,511,943)/K=1 Inv_Pol_err
1742                        endif
1743                       
1744                        break
1745                case -1: // control being killed
1746                        break
1747        endswitch
1748
1749        return 0
1750End
1751
1752// return a list of conditions, prepended with "none" for use in a popup menu
1753Function/S P_GetConditionNameList()
1754        return("none;"+D_ConditionNameList())
1755End
1756
1757
1758// for a run, or a list of runs, determine the midpoint of the data collection
1759//
1760// list is passed COMMA-delimited, like normal lists of run numbers
1761//
1762// the time is returned in seconds (equivalent to a VAX date and time)
1763//
1764Function getTimeMidpoint(listStr)
1765        String listStr
1766
1767        Variable ii,t_first,runt_first,t_last,runt_last,elap,run,t1,num,tMid
1768        String fname
1769       
1770        t1=0
1771        t_first = 1e100
1772        t_last = 0
1773       
1774        num=itemsinlist(listStr,",")
1775        for(ii=0;ii<num;ii+=1)
1776                run = str2num( StringFromList(ii, listStr ,",") )
1777                fname = FindFileFromRunNumber(run)
1778                t1 = ConvertVAXDateTime2Secs(getFileCreationDate(fname))
1779                if(t1 < t_first)
1780                        t_first = t1
1781                endif
1782                if(t1 > t_last)
1783                        t_last = t1
1784                        runt_last = getCountTime(fname)         //seconds
1785                endif
1786       
1787        endfor
1788//      print/D t_last
1789//      Print/D runt_last
1790//      print/D t_first
1791       
1792        elap = (t_last + runt_last) - t_first           // from start of first file, to end of last
1793
1794        tMid = t_first + elap/2
1795        return(tMid)
1796End
1797
1798// options to reduce one or all types, in the same manner as the load.
1799//
1800// largely copied from ReduceAFile()
1801//
1802//
1803//
1804//
1805Function ReducePolCorDataButton(ctrlName) : ButtonControl
1806        String ctrlName
1807
1808
1809        String pType
1810//      Prompt pType,"Pol Type",popup,"UU;DU;DD;UD;All;"
1811//      DoPrompt "Type to load",pType
1812//      if (V_Flag)
1813//              return 0                                                                        // user canceled
1814//      endif
1815//      Print pType
1816
1817
1818        pType = "All"
1819
1820// get the protocol to use
1821// this is pulled from ReduceAFile()
1822        Variable err
1823        String waveStr
1824       
1825        //pick a protocol wave from the Protocols folder
1826        //must switch to protocols folder to get wavelist (missing parameter)
1827        SetDataFolder root:myGlobals:Protocols
1828        Execute "PickAProtocol()"
1829       
1830        //get the selected protocol wave choice through a global string variable
1831        SVAR protocolName = root:myGlobals:Protocols:gProtoStr
1832       
1833        //If "CreateNew" was selected, go to the questionnare, to make a new set
1834        //and put the name of the new Protocol wave in gProtoStr
1835        if(cmpstr("CreateNew",protocolName) == 0)
1836                return(0)
1837        Endif
1838       
1839        //give the full path:name to the executeProtocol function
1840        waveStr = "root:myGlobals:Protocols:"+protocolName
1841        //samStr is set at top to "ask", since this is the action always desired from "ReduceAFile"
1842               
1843        //return data folder to root before Macro is done
1844        SetDataFolder root:
1845       
1846        if(cmpstr(pType,"All") == 0)
1847                ExecutePolarizedProtocol(waveStr,"UU")
1848                ExecutePolarizedProtocol(waveStr,"DU")
1849                ExecutePolarizedProtocol(waveStr,"DD")
1850                ExecutePolarizedProtocol(waveStr,"UD")
1851        else
1852                ExecutePolarizedProtocol(waveStr,pType)
1853        endif   
1854       
1855       
1856        return(0)
1857End
1858
1859// very similar to ExecuteProtocol
1860//
1861//
1862// OCT 2012 - changed this to force a re-load of all of the data, and a re-calculation
1863//   of the Pol-corrected data, so that all of the "_pc" waves that are present are the
1864// correct, and current values. Only re-loads the data that is used for the particular protocol,
1865// just like a normal reduction. This is, somewhat redundant, since the data is re-loaded 4x, when
1866// it really only needs to be re-loaded 1x, but this is only a minor speed hit.
1867//
1868// -- the "extensions" now all are "_UU_pc" and similar, to use the polarization corrected data and errors
1869//
1870Function ExecutePolarizedProtocol(protStr,pType)
1871        String protStr,pType
1872
1873        //protStr is the full path to the selected protocol wave
1874        WAVE/T prot = $protStr
1875        SetDataFolder root:myGlobals:Protocols
1876       
1877        Variable filesOK,err,notDone
1878        String activeType, msgStr, junkStr, pathStr="", samStr=""
1879        PathInfo catPathName                    //this is where the files are
1880        pathStr=S_path
1881       
1882        NVAR useXMLOutput = root:Packages:NIST:gXML_Write
1883       
1884        //Parse the instructions in the prot wave
1885        //0 - bkg
1886        //1 - emp
1887        //2 - div
1888        //3 - mask
1889        //4 - abs params c2-c5
1890        //5 - average params
1891        //6 = DRK file (**out of sequence)
1892       
1893       
1894        // For each of the tabs (SAM, EMP, BGD)
1895        // -- reload the data
1896        // -- re-do the polarization correction
1897       
1898        // then, and only then, after we're sure that all of the data is correct and current, then proceed with the
1899        // correction of the data with the selected protocol
1900        String dataType,str
1901
1902        STRUCT WMButtonAction ba
1903        ba.eventCode = 2                // mouse up
1904       
1905
1906// Now ensure that the proper SAM data is loaded, then re-tag it       
1907// the Polarization corrected data is UU_pc, DU_pc, etc.
1908// this tags it for display, and puts it in the correctly named waves
1909       
1910        ChangeDataTab(0)                //SAM
1911        LoadRawPolarizedButton(ba)
1912        PolCorButton(ba)
1913       
1914        dataType="SAM"
1915        sprintf str,"DisplayTaggedData(\"%s\",\"%s\")",dataType,pType+"_pc"
1916        Execute str
1917       
1918// force a re-load of BGD data, then re-tag it 
1919        if(cmpstr(prot[0],"none") != 0)         //if BGD is used, protStr[0] = ""
1920                ChangeDataTab(2)                //BGD
1921                LoadRawPolarizedButton(ba)
1922                PolCorButton(ba)
1923               
1924                dataType="BGD"
1925                sprintf str,"DisplayTaggedData(\"%s\",\"%s\")",dataType,pType+"_pc"
1926                Execute str
1927        endif
1928
1929// force a re-load the EMP data, then re-tag it
1930        if(cmpstr(prot[1],"none") != 0)         //if EMP is used, protStr[1] = ""
1931                ChangeDataTab(1)                //EMP
1932                LoadRawPolarizedButton(ba)
1933                PolCorButton(ba)
1934       
1935                dataType="EMP"
1936                sprintf str,"DisplayTaggedData(\"%s\",\"%s\")",dataType,pType+"_pc"
1937                Execute str
1938        endif
1939               
1940//
1941// from here down, the steps are identical
1942//
1943// - with the exceptions of:
1944// - file naming. Names are additionally tagged with pType
1945// - if the protocol[0] or [1] are "" <null>, then the step  will be used
1946//   the step is only skipped if the protocol is "none"
1947//
1948
1949       
1950        //do the CORRECT step based on the answers to emp and bkg subtraction
1951        //by setting the proper"mode"
1952        //1 = both emp and bgd subtraction
1953        //2 = only bgd subtraction
1954        //3 = only emp subtraction
1955        //4 = no subtraction
1956        //additional modes 091301
1957        // ------currently, for polarized reduction, DRK mode is not allowed or recognized at all...
1958        //11 = emp, bgd, drk
1959        //12 = bgd and drk
1960        //13 = emp and drk
1961        //14 = no subtractions
1962        //work.drk is from proto[6]
1963        //
1964        //subtracting just the DRK data is NOT an option - it doesnt' really make any physical sense
1965        // - in this case, DRK is skipped (equivalent to mode==4)
1966        // automatically accounts for attenuators given the lookup tables and the
1967        //desired subtractions
1968        //Attenuator lookup tables are alredy implemented (NG1 = NG7)
1969        //
1970               
1971        //dispatch to the proper "mode" of Correct()
1972        Variable mode=4,val
1973        do
1974                if( (cmpstr("none",prot[0]) == 0)       && (cmpstr("none",prot[1]) == 0) )
1975                        //no subtraction (mode = 4),
1976                        mode = 4
1977                Endif
1978                If((cmpstr(prot[0],"none") != 0) && (cmpstr(prot[1],"none") == 0))
1979                        //subtract BGD only
1980                        mode=2
1981                Endif
1982                If((cmpstr(prot[0],"none") == 0) && (cmpstr(prot[1],"none") != 0))
1983                        //subtract EMP only
1984                        mode=3
1985                Endif
1986                If((cmpstr(prot[0],"none") != 0) && (cmpstr(prot[1],"none") != 0))
1987                        // bkg and emp subtraction are to be done (BOTH not "none")
1988                        mode=1
1989                Endif
1990                activeType = "COR"
1991//              //add in DRK mode (0= no used, 10 = used)
1992//              val = NumberByKey("DRKMODE",prot[6],"=","," )
1993//              mode += val
1994//              print "Correct mode = ",mode
1995                err = Correct(mode)
1996                if(err)
1997                        SetDataFolder root:
1998                        Abort "error in Correct, called from executeprotocol, normal cor"
1999                endif
2000                TagLoadedData(activeType,pType+"_pc")
2001                UpdateDisplayInformation(ActiveType)            //update before breaking from loop
2002        While(0)
2003       
2004        //check for work.div file (prot[2])
2005        //add if needed
2006        // can't properly check the filename - so for now add and divide, if anything other than "none"
2007        //do/skip divide step based on div answer
2008        If(cmpstr("none",prot[2])!=0)           // if !0, then there's a file requested
2009                If(cmpstr("ask",prot[2]) == 0)
2010                        //ask user for file
2011                         junkStr = PromptForPath("Select the detector sensitivity file")
2012                        If(strlen(junkStr)==0)
2013                                SetDataFolder root:
2014                                Abort "No file selected, data reduction aborted"
2015                        Endif
2016                         ReadHeaderAndWork("DIV", junkStr)
2017                else
2018                        //assume it's a path, and that the first (and only) item is the path:file
2019                        //list processing is necessary to remove any final comma
2020                        junkStr = pathStr + StringFromList(0, prot[2],"," )
2021                        ReadHeaderAndWork("DIV",junkStr)
2022                Endif
2023                //got a DIV file, select the proper type of work data to DIV (= activeType)
2024                err = Divide_work(activeType)           //returns err = 1 if data doesn't exist in specified folders
2025                If(err)
2026                        SetDataFolder root:
2027                        Abort "data missing in DIV step, call from executeProtocol"
2028                Endif
2029                activeType = "CAL"
2030                TagLoadedData(activeType,pType+"_pc")
2031                UpdateDisplayInformation(ActiveType)            //update before breaking from loop
2032        Endif
2033       
2034        Variable c2,c3,c4,c5,kappa_err
2035        //do absolute scaling if desired
2036        if(cmpstr("none",prot[4])!=0)
2037                if(cmpstr("ask",prot[4])==0)
2038                        //get the params from the user
2039                        Execute "AskForAbsoluteParams_Quest()"
2040                        //then from the list
2041                        SVAR junkAbsStr = root:myGlobals:Protocols:gAbsStr
2042                        c2 = NumberByKey("TSTAND", junkAbsStr, "=", ";")        //parse the list of values
2043                        c3 = NumberByKey("DSTAND", junkAbsStr, "=", ";")
2044                        c4 = NumberByKey("IZERO", junkAbsStr, "=", ";")
2045                        c5 = NumberByKey("XSECT", junkAbsStr, "=", ";")
2046                        kappa_err = NumberByKey("SDEV", junkAbsStr, "=", ";")
2047                else
2048                        //get the parames from the list
2049                        c2 = NumberByKey("TSTAND", prot[4], "=", ";")   //parse the list of values
2050                        c3 = NumberByKey("DSTAND", prot[4], "=", ";")
2051                        c4 = NumberByKey("IZERO", prot[4], "=", ";")
2052                        c5 = NumberByKey("XSECT", prot[4], "=", ";")
2053                        kappa_err = NumberByKey("SDEV", prot[4], "=", ";")
2054                Endif
2055                //get the sample trans and thickness from the activeType folder
2056                String destStr = "root:Packages:NIST:"+activeType+":realsread"
2057                Wave dest = $destStr
2058                Variable c0 = dest[4]           //sample transmission
2059                Variable c1 = dest[5]           //sample thickness
2060               
2061                err = Absolute_Scale(activeType,c0,c1,c2,c3,c4,c5,kappa_err)
2062                if(err)
2063                        SetDataFolder root:
2064                        Abort "Error in Absolute_Scale(), called from executeProtocol"
2065                endif
2066                activeType = "ABS"
2067                TagLoadedData(activeType,pType+"_pc")
2068                UpdateDisplayInformation(ActiveType)            //update before breaking from loop
2069        Endif
2070       
2071        //check for mask
2072        //add mask if needed
2073        // can't properly check the filename - so for now always add
2074        //doesn't change the activeType
2075        if(cmpstr("none",prot[3])!=0)
2076                If(cmpstr("ask",prot[3])==0)
2077                        //get file from user
2078                        junkStr = PromptForPath("Select Mask file")
2079                        If(strlen(junkStr)==0)
2080                                //no selection of mask file is not a fatal error, keep going, and let cirave()
2081                                //make a "null" mask
2082                                //if none desired, make sure that the old mask is deleted
2083                                //junkStr = GetDataFolder(1)
2084                                //SetDataFolder root:Packages:NIST:MSK
2085                                KillWaves/Z root:Packages:NIST:MSK:data
2086                                //SetDataFolder junkStr
2087                                DoAlert 0,"No Mask file selected, data not masked"
2088                        else
2089                                //read in the file from the dialog
2090                                ReadMCID_MASK(junkStr)
2091                        Endif
2092                else
2093                        //just read it in from the protocol
2094                        //list processing is necessary to remove any final comma
2095                        junkStr = pathStr + StringFromList(0, prot[3],"," )
2096                        ReadMCID_MASK(junkStr)
2097                Endif
2098        else
2099                //if none desired, make sure that the old mask is deleted
2100                //junkStr = GetDataFolder(1)
2101                //SetDataFolder root:Packages:NIST:MSK
2102                KillWaves/Z root:Packages:NIST:MSK:data
2103                //SetDataFolder junkStr
2104        Endif
2105       
2106        //mask data if desired (this is done automatically  in the average step) and is
2107        //not done explicitly here (if no mask in MSK folder, a null mask is created and "used")
2108       
2109        // average/save data as specified
2110       
2111        //Parse the keyword=<Value> string as needed, based on AVTYPE
2112       
2113        //average/plot first
2114        String av_type = StringByKey("AVTYPE",prot[5],"=",";")
2115        If(cmpstr(av_type,"none") != 0)
2116                If (cmpstr(av_type,"")==0)              //if the key could not be found... (if "ask" the string)
2117                        //get the averaging parameters from the user, as if the set button was hit
2118                        //in the panel
2119                        SetAverageParamsButtonProc("dummy")             //from "ProtocolAsPanel"
2120                        SVAR tempAveStr = root:myGlobals:Protocols:gAvgInfoStr
2121                        av_type = StringByKey("AVTYPE",tempAveStr,"=",";")
2122                else
2123                        //there is info in the string, use the protocol
2124                        //set the global keyword-string to prot[5]
2125                        String/G root:myGlobals:Protocols:gAvgInfoStr = prot[5]
2126                Endif
2127        Endif
2128       
2129        //convert the folder to linear scale before averaging, then revert by calling the window hook
2130        ConvertFolderToLinearScale(activeType)
2131       
2132        strswitch(av_type)      //dispatch to the proper routine to average to 1D data
2133                case "none":           
2134                        //still do nothing
2135                        break                   
2136                case "2D_ASCII":       
2137                        //do nothing
2138                        break
2139                case "QxQy_ASCII":
2140                        //do nothing
2141                        break
2142                case "PNG_Graphic":
2143                        //do nothing
2144                        break
2145                case "Rectangular":
2146                        RectangularAverageTo1D(activeType)
2147                        break
2148                case "Annular":
2149                        AnnularAverageTo1D(activeType)
2150                        break
2151                case "Circular":
2152                        CircularAverageTo1D(activeType)
2153                        break
2154                case "Sector":
2155                        CircularAverageTo1D(activeType)
2156                        break
2157                case "Sector_PlusMinus":
2158                        Sector_PlusMinus1D(activeType)
2159                        break
2160                default:       
2161                        //do nothing
2162        endswitch
2163///// end of averaging dispatch
2164        // put data back on log or lin scale as set by default
2165        fRawWindowHook()
2166       
2167        //save data if desired
2168        String fullpath = "", newfileName=""
2169        String item = StringByKey("SAVE",prot[5],"=",";")               //does user want to save data?
2170        If( (cmpstr(item,"Yes")==0) && (cmpstr(av_type,"none") != 0) )         
2171                //then save
2172                //get name from textwave of the activeType dataset
2173                String textStr = "root:Packages:NIST:"+activeType+":textread"
2174                Wave/T textPath = $textStr
2175                String tempFilename = samStr
2176                If(WaveExists(textPath) == 1)
2177#if (exists("QUOKKA")==6)
2178                        newFileName = ReplaceString(".nx.hdf", tempFilename, "")
2179#elif (exists("HFIR")==6)
2180//                      newFileName = ReplaceString(".xml",textPath[0],"")              //removes 4 chars
2181//                      newFileName = ReplaceString("SANS",newFileName,"")              //removes 4 more chars = 8
2182//                      newFileName = ReplaceString("exp",newFileName,"")                       //removes 3 more chars = 11
2183//                      newFileName = ReplaceString("scan",newFileName,"")              //removes 4 more chars = 15, should be enough?
2184                        newFileName = GetPrefixStrFromFile(textPath[0])+GetRunNumStrFromFile(textPath[0])
2185#else
2186                        newFileName = UpperStr(GetNameFromHeader(textPath[0]))          //NCNR data drops here, trims to 8 chars
2187#endif
2188                else
2189                        newFileName = ""                        //if the header is missing?
2190                        //Print "can't read the header - newfilename is null"
2191                Endif
2192               
2193                //pick ABS or AVE extension
2194                String exten = activeType
2195                if(cmpstr(exten,"ABS") != 0)
2196                        exten = "AVE"
2197                endif
2198                if(cmpstr(av_type,"2D_ASCII") == 0)
2199                        exten = "ASC"
2200                endif
2201                if(cmpstr(av_type,"QxQy_ASCII") == 0)
2202                        exten = "DAT"
2203                endif
2204               
2205                // add an "x" to the file extension if the output is XML
2206                // currently (2010), only for ABS and AVE (1D) output
2207                if( cmpstr(exten,"ABS") == 0 || cmpstr(exten,"AVE") == 0 )
2208                        if(useXMLOutput == 1)
2209                                exten += "x"
2210                        endif
2211                endif
2212                               
2213                //Path is catPathName, symbolic path
2214                //if this doesn't exist, a dialog will be presented by setting dialog = 1
2215                //
2216                // -- add in pType tag to the name for the polarization "type"
2217                //
2218                Variable dialog = 0
2219                PathInfo/S catPathName
2220                item = StringByKey("NAME",prot[5],"=",";")              //Auto or Manual naming
2221                String autoname = StringByKey("AUTONAME",prot[5],"=",";")               //autoname -  will get empty string if not present
2222                If((cmpstr(item,"Manual")==0) || (cmpstr(newFileName,"") == 0))
2223                        //manual name if requested or if no name can be derived from header
2224                        fullPath = newfileName + pType + "."+ exten //puts possible new name or null string in dialog
2225                        dialog = 1              //force dialog for user to enter name
2226                else
2227                        //auto-generate name and prepend path - won't put up any dialogs since it has all it needs
2228                        //use autoname if present
2229                        if (cmpstr(autoname,"") != 0)
2230                                fullPath = S_Path + autoname + pType + "." +exten
2231                        else
2232                                fullPath = S_Path + newFileName + pType + "." + exten
2233                        endif   
2234                Endif
2235                //
2236                strswitch(av_type)     
2237                        case "Annular":
2238                                WritePhiave_W_Protocol(activeType,fullPath,dialog)
2239                                break
2240                        case "2D_ASCII":
2241                                Fast2DExport(activeType,fullPath,dialog)
2242                                break
2243                        case "QxQy_ASCII":
2244                                QxQy_Export(activeType,fullPath,dialog)
2245                                break
2246                        case "PNG_Graphic":
2247                                SaveAsPNG(activeType,fullpath,dialog)
2248                                break
2249                        default:
2250                                if (useXMLOutput == 1)
2251                                        WriteXMLWaves_W_Protocol(activeType,fullPath,dialog)
2252                                else
2253                                        WriteWaves_W_Protocol(activeType,fullpath,dialog)
2254                                endif
2255                endswitch
2256               
2257                //Print "data written to:  "+ fullpath
2258        Endif
2259       
2260        //done with everything in protocol list
2261        return(0)
2262End
2263
2264
2265// just like the RecallProtocolButton
2266// - the reset function is different
2267//
2268Function RecallPolCorProtocolButton(ctrlName) : ButtonControl
2269        String ctrlName
2270       
2271        //will reset panel values based on a previously saved protocol
2272        //pick a protocol wave from the Protocols folder
2273        //MUST move to Protocols folder to get wavelist
2274        SetDataFolder root:myGlobals:Protocols
2275        Execute "PickAProtocol()"
2276       
2277        //get the selected protocol wave choice through a global string variable
2278        SVAR protocolName = root:myGlobals:Protocols:gProtoStr
2279
2280        //If "CreateNew" was selected, ask user to try again
2281        if(cmpstr("CreateNew",protocolName) == 0)
2282                Abort "CreateNew is for making a new Protocol. Select a previously saved Protocol"
2283        Endif
2284       
2285        //reset the panel based on the protocol textwave (currently a string)
2286        ResetToSavedPolProtocol(protocolName)
2287       
2288        SetDataFolder root:
2289        return(0)
2290end
2291
2292//function that actually parses the protocol specified by nameStr
2293//which is just the name of the wave, without a datafolder path
2294//
2295Function ResetToSavedPolProtocol(nameStr)
2296        String nameStr
2297       
2298        //allow special cases of Base and DoAll Protocols to be recalled to panel - since they "ask"
2299        //and don't need paths
2300       
2301        String catPathStr
2302        PathInfo catPathName
2303        catPathStr=S_path
2304       
2305        //SetDataFolder root:myGlobals:Protocols                //on windows, data folder seems to get reset (erratically) to root:
2306        Wave/T w=$("root:myGlobals:Protocols:" + nameStr)
2307       
2308        String fullPath="",comma=",",list="",nameList="",PathStr="",item=""
2309        Variable ii=0,numItems,checked,specialProtocol=0
2310       
2311        if((cmpstr(nameStr,"Base")==0) || (cmpstr(nameStr,"DoAll")==0))
2312                return(0)               //don't allow these
2313        Endif
2314
2315        //background = check5
2316        checked = 1
2317        nameList = w[0]
2318        If(cmpstr(nameList,"none") ==0)
2319                checked = 0
2320        Endif
2321
2322        //set the global string to display and checkbox
2323        CheckBox check5 win=PolCor_Panel,value=checked
2324
2325        //EMP = check4
2326        checked = 1
2327        nameList = w[1]
2328        If(cmpstr(nameList,"none") ==0)
2329                checked = 0
2330        Endif
2331
2332        //set the global string to display and checkbox
2333        CheckBox check4 win=PolCor_Panel,value=checked
2334       
2335       
2336        //DIV file
2337        checked = 1
2338        nameList = w[2]
2339        If(cmpstr(nameList,"none") ==0)
2340                checked = 0
2341        Endif
2342
2343        //set the global string to display and checkbox
2344        String/G root:myGlobals:Protocols:gDIV = nameList
2345        CheckBox check0 win=PolCor_Panel,value=checked
2346       
2347        //Mask file
2348        checked = 1
2349        nameList = w[3]
2350        If(cmpstr(nameList,"none") ==0)
2351                checked = 0
2352        Endif
2353
2354        //set the global string to display and checkbox
2355        String/G root:myGlobals:Protocols:gMASK = nameList
2356        CheckBox check1 win=PolCor_Panel,value=checked
2357       
2358        //4 = abs parameters
2359        list = w[4]
2360        numItems = ItemsInList(list,";")
2361        checked = 1
2362        if(numitems == 4 || numitems == 5)              //allow for protocols with no SDEV list item
2363                //correct number of parameters, assume ok
2364                String/G root:myGlobals:Protocols:gAbsStr = list
2365                CheckBox check2 win=PolCor_Panel,value=checked
2366        else
2367                item = StringFromList(0,list,";")
2368                if(cmpstr(item,"none")==0)
2369                        checked = 0
2370                        list = "none"
2371                        String/G root:myGlobals:Protocols:gAbsStr = list
2372                        CheckBox check2 win=PolCor_Panel,value=checked
2373                else
2374                        //force to "ask"
2375                        checked = 1
2376                        String/G root:myGlobals:Protocols:gAbsStr = "ask"
2377                        CheckBox check2 win=PolCor_Panel,value=checked
2378                Endif
2379        Endif
2380       
2381        //5 = averaging choices
2382        list = w[5]
2383        item = StringByKey("AVTYPE",list,"=",";")
2384        if(cmpstr(item,"none") == 0)
2385                checked = 0
2386                String/G root:myGlobals:Protocols:gAVE = "none"
2387                CheckBox check3 win=PolCor_Panel,value=checked
2388        else
2389                checked = 1
2390                String/G root:myGlobals:Protocols:gAVE = list
2391                CheckBox check3 win=PolCor_Panel,value=checked
2392        Endif
2393       
2394        //6 = DRK choice
2395
2396        //7 = unused
2397       
2398        //all has been reset, get out
2399        Return (0)
2400End
2401
2402
2403Function ExportPolCorProtocolButton(ctrlName) : ButtonControl
2404        String ctrlName
2405       
2406        ExportProtocol(ctrlName)
2407        return(0)
2408End
2409
2410
2411Function ImportPolCorProtocolButton(ctrlName) : ButtonControl
2412        String ctrlName
2413       
2414        ImportProtocol(ctrlName)
2415        return(0)
2416End
2417
2418// at a first pass, uses the regular reduction protocol         SaveProtocolButton(ctrlName)
2419//
2420// TODO
2421// X- won't work, as it uses the MakeProtocolFromPanel function... so replace this
2422//
2423Function SavePolCorProtocolButton(ctrlName) : ButtonControl
2424        String ctrlName
2425       
2426       
2427        Variable notDone=1, newProto=1
2428        //will prompt for protocol name, and save the protocol as a text wave
2429        //prompt for name of new protocol wave to save
2430        do
2431                Execute "AskForName()"
2432                SVAR newProtocol = root:myGlobals:Protocols:gNewStr
2433               
2434                //make sure it's a valid IGOR name
2435                newProtocol = CleanupName(newProtocol,0)        //strict naming convention
2436                String/G root:myGlobals:Protocols:gNewStr=newProtocol           //reassign, if changed
2437                Print "newProtocol = ",newProtocol
2438               
2439                SetDataFolder root:myGlobals:Protocols
2440                if(WaveExists( $("root:myGlobals:Protocols:" + newProtocol) ) == 1)
2441                        //wave already exists
2442                        DoAlert 1,"That name is already in use. Do you wish to overwrite the existing protocol?"
2443                        if(V_Flag==1)
2444                                notDone = 0
2445                                newProto = 0
2446                        else
2447                                notDone = 1
2448                        endif
2449                else
2450                        //name is good
2451                        notDone = 0
2452                Endif
2453        while(notDone)
2454       
2455        //current data folder is  root:myGlobals:Protocols
2456        if(newProto)
2457                Make/O/T/N=8 $("root:myGlobals:Protocols:" + newProtocol)
2458        Endif
2459       
2460//      MakeProtocolFromPanel( $("root:myGlobals:Protocols:" + newProtocol) )
2461        MakePolProtocolFromPanel( $("root:myGlobals:Protocols:" + newProtocol) )
2462        String/G  root:myGlobals:Protocols:gProtoStr = newProtocol
2463       
2464        //the data folder WAS changed above, this must be reset to root:
2465        SetDatafolder root:     
2466               
2467        return(0)
2468End
2469
2470//function that does the guts of reading the panel controls and globals
2471//to create the necessary text fields for a protocol
2472//Wave/T w (input) is an empty text wave of 8 elements for the protocol
2473//on output, w[] is filled with the protocol strings as needed from the panel
2474//
2475// -- For polarized beam protocols, don't fill in EMP or BGD, as these are handled differently, since 4 XS
2476//
2477Function MakePolProtocolFromPanel(w)
2478        Wave/T w
2479       
2480        //construct the protocol text wave form the panel
2481        //it is to be parsed by ExecuteProtocol() for the actual data reduction
2482        PathInfo catPathName                    //this is where the files came from
2483        String pathstr=S_path,tempStr,curList
2484        Variable checked,ii,numItems
2485       
2486        //look for checkboxes, then take each item in list and prepend the path
2487        //w[0] = background
2488        ControlInfo/W=PolCor_Panel check5
2489        checked = V_Value
2490        if(checked)
2491                w[0] = ""               // BKG will be used
2492        else
2493                w[0] = "none"           // BKG will not be used
2494        endif
2495       
2496        //w[1] = empty
2497        ControlInfo/W=PolCor_Panel check4
2498        checked = V_Value
2499        if(checked)
2500                w[1] = ""               // EMP will be used
2501        else
2502                w[1] = "none"           // EMP will not be used
2503        endif
2504
2505       
2506        //w[2] = div file
2507        ControlInfo/W=PolCor_Panel check0
2508        checked = V_value
2509        if(checked)
2510                //build the list
2511                //just read the global
2512                SVAR str=root:myGlobals:Protocols:gDIV
2513                if(cmpstr(str,"ask")==0)
2514                        w[2] = str
2515                else
2516                        tempStr = ParseRunNumberList(str)
2517                        if(strlen(tempStr)==0)
2518                                return(1)
2519                        else
2520                                w[2] = tempstr
2521                                str=tempstr
2522                        endif
2523                endif
2524        else
2525                //none used - set textwave (and global?)
2526                w[2] = "none"
2527                String/G root:myGlobals:Protocols:gDIV = "none"
2528        endif
2529       
2530        //w[3] = mask file
2531        ControlInfo/W=PolCor_Panel check1
2532        checked = V_value
2533        if(checked)
2534                //build the list
2535                //just read the global
2536                SVAR str=root:myGlobals:Protocols:gMASK
2537                if(cmpstr(str,"ask")==0)
2538                        w[3] = str
2539                else
2540                        tempstr = ParseRunNumberList(str)
2541                        if(strlen(tempstr)==0)
2542                                return(1)
2543                        else
2544                                w[3] = tempstr
2545                                str = tempstr
2546                        endif
2547                endif
2548        else
2549                //none used - set textwave (and global?)
2550                w[3] = "none"
2551                String/G root:myGlobals:Protocols:gMASK = "none"
2552        endif
2553       
2554        //w[4] = abs parameters
2555        ControlInfo/W=PolCor_Panel check2
2556        checked = V_value
2557        if(checked)
2558                //build the list
2559                //just read the global
2560                SVAR str=root:myGlobals:Protocols:gAbsStr
2561                w[4] = str
2562        else
2563                //none used - set textwave (and global?)
2564                w[4] = "none"
2565                String/G root:myGlobals:Protocols:gAbsStr = "none"
2566        endif
2567       
2568        //w[5] = averaging choices
2569        ControlInfo/W=PolCor_Panel check3
2570        checked = V_value
2571        if(checked)
2572                //just read the global
2573                SVAR avestr=root:myGlobals:Protocols:gAVE
2574                w[5] = avestr
2575        else
2576                //none used - set textwave
2577                w[5] = "AVTYPE=none;"
2578        endif
2579       
2580        //w[6]
2581        //work.DRK information
2582        SVAR drkStr=root:myGlobals:Protocols:gDRK
2583        w[6] = ""
2584       
2585        //w[7]
2586        //currently unused
2587        w[7] = ""
2588       
2589        return(0)
2590End
2591
2592
2593
2594// the data in the SAM, EMP, BKG folders will be either _UU or _UU_pc, depending if the data
2595// is as-loaded, or if it's been polarization corrected. This is to display the polarization-corrected "_pc"
2596// data sets ONLY. If you want to see the individual data sets, well that's just the SAM, EMP, BGD files.
2597// look at thum one-by-one...
2598//
2599// -- data in subsequent correction step folders do have the _pc extensions, the _pc data is
2600//              used for each step, so the _pc is redundant, but consistent
2601//
2602Function Display_4(type,scaling)
2603        String type,scaling
2604       
2605        String dest = "root:Packages:NIST:"+type
2606        NVAR isLogscale = $(dest + ":gIsLogScale")
2607
2608        SetDataFolder $("root:Packages:NIST:"+type)
2609       
2610        wave uu = linear_data_uu_pc
2611        wave d_uu = data_uu_pc
2612       
2613        wave du = linear_data_du_pc
2614        wave d_du = data_du_pc
2615       
2616        wave dd = linear_data_dd_pc
2617        wave d_dd = data_dd_pc
2618       
2619        wave ud = linear_data_ud_pc
2620        wave d_ud = data_ud_pc
2621               
2622        if(cmpstr(scaling,"log") == 0)
2623
2624                d_uu = log(uu)
2625                d_du = log(du)
2626                d_dd = log(dd)
2627                d_ud = log(ud)                 
2628               
2629                isLogScale = 1
2630                       
2631        else
2632
2633                d_uu = uu
2634                d_du = du
2635                d_dd = dd
2636                d_ud = ud
2637                       
2638                isLogScale = 0
2639
2640        endif
2641
2642        DoWindow/F SANS_X4
2643        if(V_flag==0)
2644                Display /W=(811,44,1479,758)/K=1
2645                ControlBar 100
2646                DoWindow/C SANS_X4
2647                DoWindow/T SANS_X4,type+"_pc"
2648                Button button0 pos={130,65},size={50,20},title="Do It",proc=Change4xsButtonProc
2649                PopupMenu popup0 pos={20,35},title="Data Type",value="SAM;EMP;BGD;COR;CAL;ABS;"         //RAW, SAS, DIV, etc, won't have _pc data and are not valid
2650                PopupMenu popup1 pos={190,35},title="Scaling",value="log;linear;"
2651                TitleBox title0 title="Only Polarization-corrected sets are displayed",pos={5,5}
2652
2653        else
2654                RemoveImage/Z data_uu_pc                //remove the old images (different data folder)
2655                RemoveImage/Z data_du_pc
2656                RemoveImage/Z data_dd_pc
2657                RemoveImage/Z data_ud_pc
2658               
2659                DoWindow/T SANS_X4,type+"_pc"
2660
2661        endif
2662       
2663        AppendImage/B=bot_uu/L=left_uu data_UU_pc
2664        ModifyImage data_UU_pc ctab= {*,*,YellowHot,0}
2665        AppendImage/B=bot_dd/L=left_dd data_DD_pc
2666        ModifyImage data_DD_pc ctab= {*,*,YellowHot,0}
2667        AppendImage/B=bot_du/L=left_du data_DU_pc
2668        ModifyImage data_DU_pc ctab= {*,*,YellowHot,0}
2669        AppendImage/B=bot_ud/L=left_ud data_UD_pc
2670        ModifyImage data_UD_pc ctab= {*,*,YellowHot,0}
2671       
2672        DoUpdate
2673                       
2674        ModifyGraph freePos(left_uu)={0,kwFraction},freePos(bot_uu)={0.6,kwFraction}
2675        ModifyGraph freePos(left_dd)={0,kwFraction},freePos(bot_dd)={0,kwFraction}
2676        ModifyGraph freePos(left_du)={0.6,kwFraction},freePos(bot_du)={0.6,kwFraction}
2677        ModifyGraph freePos(left_ud)={0.6,kwFraction},freePos(bot_ud)={0,kwFraction}
2678       
2679        ModifyGraph axisEnab(left_uu)={0.6,1},axisEnab(bot_uu)={0,0.4}
2680        ModifyGraph axisEnab(left_dd)={0,0.4},axisEnab(bot_dd)={0,0.4}
2681        ModifyGraph axisEnab(left_du)={0.6,1},axisEnab(bot_du)={0.6,1}
2682        ModifyGraph axisEnab(left_ud)={0,0.4},axisEnab(bot_ud)={0.6,1}
2683       
2684        ModifyGraph standoff=0
2685        ModifyGraph lblPosMode(left_uu)=4,lblPosMode(left_dd)=4,lblPosMode(left_du)=4,lblPosMode(left_ud)=4
2686        ModifyGraph lblPos(left_uu)=40,lblPos(left_dd)=40,lblPos(left_du)=40,lblPos(left_ud)=40
2687        Label left_uu "UU"
2688        Label left_dd "DD"
2689        Label left_du "DU"
2690        Label left_ud "UD"     
2691       
2692        ModifyGraph fSize=16
2693        // force a redraw to get the axes in the right spot
2694        DoUpdate
2695       
2696        SetDataFolder root:
2697        return(0)
2698End
2699
2700Function Change4xsButtonProc(ba) : ButtonControl
2701        STRUCT WMButtonAction &ba
2702
2703        String dataType,scaling
2704       
2705        switch( ba.eventCode )
2706                case 2: // mouse up
2707                        // click code here
2708                        ControlInfo popup0
2709                        dataType = S_Value
2710                        ControlInfo popup1
2711                        scaling = S_Value
2712                        Display_4(dataType,scaling)
2713                       
2714                        break
2715                case -1: // control being killed
2716                        break
2717        endswitch
2718
2719        return 0
2720End
2721
2722// clear the entries for all 4 XS for the currently selected Tab only
2723// clears both the run numbers and the cell assignments
2724//
2725Function ClearPolCorEntries(ba) : ButtonControl
2726        STRUCT WMButtonAction &ba
2727
2728        switch( ba.eventCode )
2729                case 2: // mouse up
2730                        // click code here
2731                        //
2732//                      String type
2733                        Variable tabNum
2734                        ControlInfo/W=PolCor_Panel PolCorTab
2735//                      type = S_value
2736//                      Print "selected data type = ",type,V_Value
2737                        tabNum = V_Value
2738                       
2739                        WAVE/T twDD = $("root:Packages:NIST:Polarization:ListWave_"+num2str(tabNum)+"_DD")
2740                        WAVE/T twUU = $("root:Packages:NIST:Polarization:ListWave_"+num2str(tabNum)+"_UU")
2741                        WAVE/T twUD = $("root:Packages:NIST:Polarization:ListWave_"+num2str(tabNum)+"_UD")
2742                        WAVE/T twDU = $("root:Packages:NIST:Polarization:ListWave_"+num2str(tabNum)+"_DU")
2743                       
2744                        twDD = ""
2745                        twUU = ""
2746                        twUD = ""
2747                        twDU = ""
2748                       
2749                        break
2750                case -1: // control being killed
2751                        break
2752        endswitch
2753
2754        return 0
2755End
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807//////////////////////////////////////////////////////////////
2808//
2809/////////// all of the functions below are unused. See the procedures for the
2810//// panels for the functions that interact with the input and calculated matrices
2811//
2812//
2813//
2814//
2815//
2816////
2817//// (/S) FindFileFromRunNumber(num) gets fname
2818//// [4] is trans, [41] is trans error, [40] is Twhole
2819////
2820//
2821//
2822//
2823//Constant kTe = 0.87           // transmission of the unfilled cell
2824//
2825//
2826//
2827//// calculation of mu
2828////
2829//// Known: Te ===> Global constant currently, may change later
2830////
2831//// Input: T He (unpolarized), using unpolarized beam
2832////                    T He cell out, using unpolarized beam
2833////                    T background, using unpolarized beam
2834////
2835//// Equation 7
2836////
2837//Function opacity_mu(T_he,T_out,T_bk)
2838//      Variable T_he,T_out,T_bk
2839//
2840//      Variable mu
2841//
2842//// using the global constant!
2843//
2844//      mu = (1/kTe)*(T_he - T_bk)/(T_out - t_bk)
2845//      mu = -1*ln(mu)
2846//
2847//      return(mu)
2848//End
2849//
2850//
2851//
2852//Proc calc_muP(mu, runT_he, runT_out, runT_bk)
2853//      Variable mu=3.108, runT_he, runT_out, runT_bk
2854//
2855//      Variable muP,T_he, T_out, T_bk
2856//      String fname
2857//     
2858//      fname = FindFileFromRunNumber(runT_he)
2859//      T_he = getDetCount(fname)/getMonitorCount(fname)/getCountTime(fname)    //use CR, not trans (since no real empty condition)
2860//     
2861//      fname = FindFileFromRunNumber(runT_out)
2862//      T_out = getDetCount(fname)/getMonitorCount(fname)/getCountTime(fname)
2863//     
2864//      fname = FindFileFromRunNumber(runT_bk)
2865//      T_bk = getDetCount(fname)/getMonitorCount(fname)/getCountTime(fname)
2866//     
2867//      muP = Cell_muP(mu, T_he, T_out, T_bk)
2868//     
2869//      Print "Count rates T_he, T_out, T_bk = ",T_he, T_out, T_bk
2870//      Print "Mu*P = ",muP
2871//      Print "Time = ",getFileCreationDate(fname)
2872//     
2873//end
2874//
2875//
2876//// ???? is this correct ????
2877//// -- check the form of the equation. It's not the same in some documents
2878////
2879//// calculation of mu.P(t) from exerimental measurements
2880////
2881//// Known: Te and mu
2882//// Input: T He cell (polarized cell), using unpolarized beam
2883////                    T He cell OUT, using unpolarized beam
2884////                    T background, using unpolarized beam
2885////
2886//// Equation 9, modified by multiplying the result by mu + moving tmp inside the acosh()
2887////
2888//Function Cell_muP(mu, T_he, T_out, T_bk)
2889//      Variable mu, T_he, T_out, T_bk
2890//
2891//// using the global constant!
2892//
2893//      Variable muP,tmp
2894//     
2895//      tmp = kTe*exp(-mu)              //note mu has been moved
2896//      muP = acosh( (T_he - T_bk)/(T_out - T_bk)  * (1/tmp))
2897//
2898//      return(muP)
2899//End
2900//
2901//
2902////
2903//// calculation of mu.P(t) from Gamma and t=0 value
2904////
2905//// Known: Gamma, muP_t0, t0, tn
2906////
2907//// times are in hours, Gamma [=] hours
2908//// tn is later than t0, so t0-tn is negative
2909////
2910//// Equation 11
2911////
2912//Function muP_at_t(Gam_He, muP_t0, t0, tn)
2913//      Variable Gam_He, muP_t0, t0, tn
2914//
2915//      Variable muP
2916//     
2917//      muP = muP_t0 * exp( (t0 - tn)/Gam_He )
2918//     
2919//      return(muP)
2920//End
2921//
2922//// Calculation of Pcell(t)
2923//// note that this is a time dependent quantity
2924////
2925//// Known: muP(t)
2926//// Input: nothing additional
2927////
2928//// Equation 10
2929////
2930//Function PCell(muP)
2931//      Variable muP
2932//     
2933//      Variable PCell
2934//      PCell = tanh(muP)
2935//     
2936//      return(PCell)
2937//End
2938//
2939//
2940//// calculation of Pf (flipper)
2941////
2942//// Known: nothing
2943//// Input: Tuu, Tdu, and Tdd, Tud
2944////                    (but exactly what measurement conditions?)
2945////                    ( are these T's also calculated quantities???? -- Equation(s) 12--)
2946////
2947//// Equation 14
2948////
2949//// (implementation of equation 13 is more complicated, and not implemented yet)
2950////
2951//Function Flipper_Pf(Tuu, Tdu, Tdd, Tud)
2952//      Variable Tuu, Tdu, Tdd, Tud
2953//     
2954//      Variable pf
2955//     
2956//      pf = (Tdd - Tdu)/(Tuu - Tud)
2957//     
2958//      return(pf)
2959//End
2960//
2961//
2962//
2963//// (this is only one of 4 methods, simply the first one listed)
2964//// ???? this equation doesn't match up with the equation in the SS handout
2965////
2966//// calculation of P'sm (supermirror)
2967////
2968//// Known: Pcell(t1), Pcell(t2) (some implementations need Pf )
2969//// Input: Tuu(t1), Tud(t2)
2970////
2971//// Equation 15??
2972////
2973//Function SupMir_Psm(Pcell1,Pcell2,Tuu,Tud)
2974//      Variable Pcell1,Pcell2,Tuu,Tud
2975//     
2976//      Variable Psm
2977//     
2978//      Psm = (Tuu - Tud)/(PCell1 + PCell2)
2979//     
2980//      return(Psm)
2981//End
Note: See TracBrowser for help on using the repository browser.