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

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

Changes to:
Polarization - to correct naming errors when saving polarization panel states. Names were too long.

Event Mode - improved the re-loading of split oscillatory data sets. Also allows easier editing of the list of split files to re-load for decimation.

FFT/real space - Modified gizmo to automatically color the different SLDs present. Limitied to 5 different SLDs due to a limitation of the voxelgram. If more than 5 SLDs are present (solvent does not count), then the extra ones are not displayed. Also modified KR-3Cyl procedures to be able to save/import a table of coordinates.

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