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

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

Changes to the polarization reduction, tickets#355, #356, #362,

--saving of the panel states is now something that can be done from a menu item. (there is too much clutter on the panels to add more buttons, unless there is a great outcry).
--"?" has ben added to table columns that require input - coloring of individual columns can't be done with the matrices in the table.
-- the background is now polarization-independent
--the flow of the protocol execution now forces are-load and re--polarization correction every time for SAM, EMP, BGD (trivial) so that the correct files are in the correct locations, every time.

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