source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/USANS/U_CALC.ipf @ 577

Last change on this file since 577 was 577, checked in by srkline, 13 years ago

Fixed logic error in writing the XY box count values to the empty beam header that could result in INF transmission.

Updated critera for "RAW" data files to include "SIM" as well.

Added NCNR_Utils to the includes list for Analysis.

File size: 54.3 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma IgorVersion=6.1
3
4
5// USANS version of SASCALC
6
7// to simulate the intensity from a USANS experiment for planning
8// see John's instrument paper:
9//
10// J. Appl. Cryst. (2005) 38 1004-1011.
11//
12// SRK JUL 2009
13
14
15// ideas:
16//
17// - more presets
18//
19// - NEED -
20// - printable output that makes sense to an instrument scientist at least
21//   for instrument setup
22//
23// - make sure that the # points <-> increment relation meshes with ICP
24
25//
26// X plot as countrate, not absolute scale
27// X 3e-5 cutoff
28// X ? don't plot lowest angle range (but needs to be in the count time)
29// - need empty beam and empty cell count rate vs. aperture (Cd vs. Gd?)
30//
31
32
33// need to add in empty and background corrections to see "reduced" data
34// or at least compare to what the empty cell would give in the same count time
35//
36// -? model the direct beam?? currently the "red" region from -1 to 0.6 is almost entirely
37// the primary beam, so it's a bit artificial (qmin is really ~ 3e-5)
38//
39
40//
41// Need T_wide, T_rock, I peak for proper absolute scaling, but I don't know if it's really important
42// to be able to simlulate to this extent
43//
44
45
46//#include "MultScatter_MonteCarlo_2D"
47
48
49
50// Bring the UCALC panel to the front
51// ALWAYS initializes the folders and variables
52// then draws the panel if necessary
53Proc Show_UCALC()
54
55        DoWindow/F UCALC
56        if(V_Flag==0)
57                Init_UCALC()
58                UCALC_Panel()
59                CalcTotalCountTime()
60                ControlUpdate/W=UCALC U_popup0          //force a pop of the function list
61        Endif
62       
63End
64
65
66//set up the global values for the angles, # points, and count time
67Proc Init_UCALC()
68       
69        //
70        NewDataFolder/O root:Simulation                         // required to calculate the RandomDeviate
71        NewDataFolder/O root:Packages:NIST:SAS                          // required to calculate the RandomDeviate
72
73//
74        NewDataFolder/O root:Packages:NIST:USANS:SIM            //for the fake raw data
75        NewDataFolder/O/S root:Packages:NIST:USANS:Globals:U_Sim                //for constants, panel, etc
76
77        Variable/G gAngLow1 = -1
78        Variable/G gAngHigh1 = 0.6
79        Variable/G gNumPts1 = 33
80        Variable/G gCtTime1 = 25
81        Variable/G gIncr1 = 0.05       
82       
83        Variable/G gAngLow2 = 0.7
84        Variable/G gAngHigh2 = 1.9
85        Variable/G gNumPts2 = 13
86        Variable/G gCtTime2 = 100
87        Variable/G gIncr2 = 0.1
88       
89        Variable/G gAngLow3 = 2
90        Variable/G gAngHigh3 = 4.8
91        Variable/G gNumPts3 = 15
92        Variable/G gCtTime3 = 300
93        Variable/G gIncr3 = 0.2
94       
95        Variable/G gAngLow4 = 5
96        Variable/G gAngHigh4 = 9.5
97        Variable/G gNumPts4 = 10
98        Variable/G gCtTime4 = 600
99        Variable/G gIncr4 = 0.5
100       
101        Variable/G gAngLow5 = 10
102        Variable/G gAngHigh5 = 19
103        Variable/G gNumPts5 = 10
104        Variable/G gCtTime5 = 1200
105        Variable/G gIncr5 = 1
106       
107        Variable/G gAngLow6 = 20
108        Variable/G gAngHigh6 = 48
109        Variable/G gNumPts6 = 15
110        Variable/G gCtTime6 = 2000
111        Variable/G gIncr6 = 2   
112       
113        Variable/G gAngLow7 = 50
114        Variable/G gAngHigh7 = 95
115        Variable/G gNumPts7 = 10
116        Variable/G gCtTime7 = 3000
117        Variable/G gIncr7 = 5   
118       
119        // results, setup values
120        String/G gFuncStr=""
121        String/G gTotTimeStr=""
122        Variable/G gAnalyzerOmega = 7.1e-7              //solid angle of the analyzer, in steradians
123        Variable/G gBeamCurrent=25000           //beam current Ed*I     (n/s) for 5/8" diam = 25000 n/s
124        Variable/G gThick=0.1           //sample thickness (cm)
125        Variable/G gSamTrans=0.8
126        Variable/G g_1D_DoABS = 0               //=1 for abs scale, 0 for just counts
127        Variable/G g_1D_PlotCR = 1              //=1 to plot countrate
128        Variable/G g_1D_AddNoise = 1            // add in appropriate noise to simulation
129       
130// a box for the results
131        // ??   maybe not useful to report
132        Variable/G g_1DEstDetCR  = 0            // estimated detector count rate
133        Variable/G g_1DTotCts = 0               
134        Variable/G g_1DFracScatt= 0     // ??
135        Variable/G g_1DEstTrans = 0     // ? can I calculate this?
136
137// not on panel yet
138        Variable/G g_Empirical_EMP = 0          // use an emperical model for empty cell subtraction
139        Variable/G g_EmptyLevel = 0.7
140        Variable/G g_BkgLevel = 0.6
141
142        SetDataFolder root:
143       
144End
145
146//// make the display panel a graph with a control bar just as in SASCALC
147//// so that the subwindow syntax doesn't break all of the other functionality
148////
149//Window UCALC_Panel() : Graph
150//      PauseUpdate; Silent 1           // building window...
151//      Display /W=(55,44,670,850) /K=1
152//      ModifyGraph cbRGB=(36929,50412,31845)
153//      DoWindow/C UCALC
154//      DoWindow/T UCALC,"USANS Simulation"
155//      ControlBar 320
156//     
157//      GroupBox group0,pos={5,0},size={577,159},title="Instrument Setup"
158//      GroupBox group1,pos={5,165},size={240,147},title="Sample Setup"
159//      GroupBox group2,pos={327,165},size={259,147},title="Results"
160//     
161//      PopupMenu popup0,pos={17,18},size={165,20},title="Sample Aperture Diam (in)"
162//      PopupMenu popup0,mode=3,popvalue="0.625",value="0.25;0.50;0.625;0.75;1.0;1.75;2.0;"
163//      PopupMenu popup2,pos={220,18},size={165,20},title="Presets"
164//      PopupMenu popup2,mode=3,popvalue="Long Count",value="Short Count;Medium Count;Long Count;"
165//      PopupMenu popup2,proc=UCALC_PresetPopup
166//
167//      SetDataFolder root:Packages:NIST:USANS:Globals:U_Sim
168//     
169//      Variable top=44,pt=0,inc=18
170//      SetVariable setvar1a,pos={12,top},size={100,15},title="theta min",value= gAngLow1
171//      SetVariable setvar1b,pos={119,top},size={100,15},title="theta max",value= gAngHigh1
172//      SetVariable setvar1c,pos={227,top},size={100,15},title="increm",value= gIncr1
173//      SetVariable setvar1d,pos={335,top},size={100,15},title="# points",value= gNumPts1
174//      SetVariable setvar1e,pos={443,top},size={100,15},title="count (s)",value= gCtTime1
175//      SetVariable setvar1a,labelBack=(65535,32768,32768)
176//     
177//      pt += inc
178//      SetVariable setvar2a,pos={12,top+pt},size={100,15},title="theta min",value= gAngLow2
179//      SetVariable setvar2b,pos={119,top+pt},size={100,15},title="theta max",value= gAngHigh2
180//      SetVariable setvar2c,pos={227,top+pt},size={100,15},title="increm",value= gIncr2
181//      SetVariable setvar2d,pos={335,top+pt},size={100,15},title="# points",value= gNumPts2
182//      SetVariable setvar2e,pos={443,top+pt},size={100,15},title="count (s)",value= gCtTime2
183//      SetVariable setvar2a labelBack=(65535,65533,32768)
184//     
185//      pt += inc
186//      SetVariable setvar3a,pos={12,top+pt},size={100,15},title="theta min",value= gAngLow3
187//      SetVariable setvar3b,pos={119,top+pt},size={100,15},title="theta max",value= gAngHigh3
188//      SetVariable setvar3c,pos={227,top+pt},size={100,15},title="increm",value= gIncr3
189//      SetVariable setvar3d,pos={335,top+pt},size={100,15},title="# points",value= gNumPts3
190//      SetVariable setvar3e,pos={443,top+pt},size={100,15},title="count (s)",value= gCtTime3
191//      SetVariable setvar3a labelBack=(32769,65535,32768)
192//     
193//      pt += inc
194//      SetVariable setvar4a,pos={12,top+pt},size={100,15},title="theta min",value= gAngLow4
195//      SetVariable setvar4b,pos={119,top+pt},size={100,15},title="theta max",value= gAngHigh4
196//      SetVariable setvar4c,pos={227,top+pt},size={100,15},title="increm",value= gIncr4
197//      SetVariable setvar4d,pos={335,top+pt},size={100,15},title="# points",value= gNumPts4
198//      SetVariable setvar4e,pos={443,top+pt},size={100,15},title="count (s)",value= gCtTime4
199//      SetVariable setvar4a labelBack=(32768,65535,65535)
200//     
201//      pt += inc
202//      SetVariable setvar5a,pos={12,top+pt},size={100,15},title="theta min",value= gAngLow5
203//      SetVariable setvar5b,pos={119,top+pt},size={100,15},title="theta max",value= gAngHigh5
204//      SetVariable setvar5c,pos={227,top+pt},size={100,15},title="increm",value= gIncr5
205//      SetVariable setvar5d,pos={335,top+pt},size={100,15},title="# points",value= gNumPts5
206//      SetVariable setvar5e,pos={443,top+pt},size={100,15},title="count (s)",value= gCtTime5
207//      SetVariable setvar5a labelBack=(32768,54615,65535)
208//     
209//      pt += inc
210//      SetVariable setvar6a,pos={12,top+pt},size={100,15},title="theta min",value= gAngLow6
211//      SetVariable setvar6b,pos={119,top+pt},size={100,15},title="theta max",value= gAngHigh6
212//      SetVariable setvar6c,pos={227,top+pt},size={100,15},title="increm",value= gIncr6
213//      SetVariable setvar6d,pos={335,top+pt},size={100,15},title="# points",value= gNumPts6
214//      SetVariable setvar6e,pos={443,top+pt},size={100,15},title="count (s)",value= gCtTime6
215//      SetVariable setvar6a labelBack=(44253,29492,58982)
216//
217//// the action procedures and limits/increments
218//      SetVariable setvar1a proc=ThetaMinSetVarProc            //,limits={-2,0,0.1}
219//      SetVariable setvar2a proc=ThetaMinSetVarProc
220//      SetVariable setvar3a proc=ThetaMinSetVarProc
221//      SetVariable setvar4a proc=ThetaMinSetVarProc
222//      SetVariable setvar5a proc=ThetaMinSetVarProc
223//      SetVariable setvar6a proc=ThetaMinSetVarProc
224//
225////
226//      SetVariable setvar1b proc=ThetaMaxSetVarProc            //,limits={0.4,1,0.1}
227//      SetVariable setvar2b proc=ThetaMaxSetVarProc
228//      SetVariable setvar3b proc=ThetaMaxSetVarProc
229//      SetVariable setvar4b proc=ThetaMaxSetVarProc
230//      SetVariable setvar5b proc=ThetaMaxSetVarProc
231//      SetVariable setvar6b proc=ThetaMaxSetVarProc
232////
233//      SetVariable setvar1c proc=IncrSetVarProc,limits={0.01,0.1,0.01}
234//      SetVariable setvar2c proc=IncrSetVarProc,limits={0.02,0.2,0.02}
235//      SetVariable setvar3c proc=IncrSetVarProc,limits={0.05,0.4,0.05}
236//      SetVariable setvar4c proc=IncrSetVarProc,limits={0.1,1,0.1}
237//      SetVariable setvar5c proc=IncrSetVarProc,limits={0.5,5,1}
238//      SetVariable setvar6c proc=IncrSetVarProc,limits={1,10,2}
239////
240//      SetVariable setvar1d proc=NumPtsSetVarProc,limits={2,50,1}
241//      SetVariable setvar2d proc=NumPtsSetVarProc,limits={2,50,1}
242//      SetVariable setvar3d proc=NumPtsSetVarProc,limits={2,50,1}
243//      SetVariable setvar4d proc=NumPtsSetVarProc,limits={2,50,1}
244//      SetVariable setvar5d proc=NumPtsSetVarProc,limits={2,50,1}
245//      SetVariable setvar6d proc=NumPtsSetVarProc,limits={2,50,1}
246////
247//      SetVariable setvar1e proc=CtTimeSetVarProc,limits={-1,50000,1}
248//      SetVariable setvar2e proc=CtTimeSetVarProc,limits={-1,50000,10}
249//      SetVariable setvar3e proc=CtTimeSetVarProc,limits={-1,50000,10}
250//      SetVariable setvar4e proc=CtTimeSetVarProc,limits={-1,50000,30}
251//      SetVariable setvar5e proc=CtTimeSetVarProc,limits={-1,50000,100}
252//      SetVariable setvar6e proc=CtTimeSetVarProc,limits={-1,50000,100}
253//     
254//      Button button0,pos={255,180},size={60,20},fColor=(65535,65535,0),proc=U_SimPlotButtonProc,title="Plot"
255//      Button button1,pos={260,286},size={50,20},proc=U_SaveButtonProc,title="Save"
256//
257////checkbox for "easy" mode
258//      CheckBox check0 title="Simple mode?",pos={400,19},proc=EnterModeCheckProc,value=1
259//      ThetaEditMode(2)                //checked on startup
260//     
261////    instrument setup
262//      SetVariable U_setvar0_1,pos={20,211},size={160,15},title="Thickness (cm)"
263//      SetVariable U_setvar0_1,limits={0,inf,0.1},value= root:Packages:NIST:USANS:Globals:U_Sim:gThick
264//      SetVariable U_setvar0_3,pos={20,235},size={160,15},title="Sample Transmission"
265//      SetVariable U_setvar0_3,limits={0,1,0.01},value= root:Packages:NIST:USANS:Globals:U_Sim:gSamTrans
266//      PopupMenu U_popup0,pos={20,185},size={165,20},proc=Sim_USANS_ModelPopMenuProc,title="Model"
267//      PopupMenu U_popup0,mode=1,value= #"U_FunctionPopupList()"
268//      SetVariable setvar0,pos={20,259},size={120,15},title="Empty Level"
269//      SetVariable setvar0,limits={0,10,0.01},value= root:Packages:NIST:USANS:Globals:U_Sim:g_EmptyLevel
270//      SetVariable setvar0_1,pos={20,284},size={120,15},title="Bkg Level"
271//      SetVariable setvar0_1,limits={0,10,0.01},value= root:Packages:NIST:USANS:Globals:U_Sim:g_BkgLevel
272//     
273//      CheckBox check0_4 title="Show EMP?",pos={160,260},proc=ShowEMPCheckProc,value=0
274//     
275//      CheckBox check0_2,pos={253,239},size={60,14},title="CountRate?",variable= root:Packages:NIST:USANS:Globals:U_Sim:g_1D_PlotCR
276//      CheckBox check0_3,pos={262,264},size={60,14},title="Noise?",variable= root:Packages:NIST:USANS:Globals:U_Sim:g_1D_AddNoise
277//     
278//// a box for the results
279//      SetVariable totalTime,pos={338,185},size={150,15},title="Count time (h:m)",value= gTotTimeStr
280////    ValDisplay valdisp0,pos={338,210},size={220,13},title="Total detector counts"
281////    ValDisplay valdisp0,limits={0,0,0},barmisc={0,1000},value= root:Packages:NIST:USANS:Globals:U_Sim:g_1DTotCts
282//      ValDisplay valdisp0_2,pos={338,234},size={220,13},title="Fraction of beam scattered"
283//      ValDisplay valdisp0_2,limits={0,0,0},barmisc={0,1000},value= root:Packages:NIST:USANS:Globals:U_Sim:g_1DFracScatt
284//      ValDisplay valdisp0_3,pos={338,259},size={220,13},title="Estimated transmission"
285//      ValDisplay valdisp0_3,limits={0,0,0},barmisc={0,1000},value=root:Packages:NIST:USANS:Globals:U_Sim:g_1DEstTrans
286//
287//     
288//      SetDataFolder root:
289//
290//EndMacro
291
292// ??make the USANS simulation results into a graph and a separate panel.
293// the control bar at the top makes the whole thing too large, and control bars are limited
294// to 500 pix wide, which is really tight
295// left of 1055 is good for Mac, 955 is better for Win
296//
297Window UCALC_Panel() : Graph
298        PauseUpdate; Silent 1           // building window...
299        String platform=UpperStr(IgorInfo(2))
300        Variable pos=strsearch(platform,"WINDOWS",0)
301        if(pos >= 0)            //windows
302                Display /W=(55,44,855,450) /K=1
303        else            //mac
304                Display /W=(55,44,1055,544) /K=1
305        endif
306       
307        ModifyGraph cbRGB=(36929,50412,31845)
308        DoWindow/C UCALC
309        DoWindow/T UCALC,"USANS Simulation"
310        ControlBar/L 500
311       
312        GroupBox group0,pos={5,1},size={493,177},title="Instrument Setup"
313        GroupBox group1,pos={5,183},size={240,147},title="Sample Setup"
314        GroupBox group2,pos={5,343},size={259,147},title="Results"
315       
316        PopupMenu popup0,pos={17,19},size={165,20},title="Sample Aperture Diam (in)"
317        PopupMenu popup0,mode=3,popvalue="0.625",value="0.25;0.50;0.625;0.75;1.0;1.75;2.0;"
318        PopupMenu popup0,proc=UCALC_SampleAperturePopup
319        PopupMenu popup2,pos={220,19},size={165,20},title="Presets"
320        PopupMenu popup2,mode=3,popvalue="Long Count",value="Short Count;Medium Count;Long Count;"
321        PopupMenu popup2,proc=UCALC_PresetPopup
322
323        SetDataFolder root:Packages:NIST:USANS:Globals:U_Sim
324       
325        Variable top=46,pt=0,inc=18,left=0//left=533
326        SetVariable setvar1a,pos={left+17,top},size={90,15},title="theta min",value= gAngLow1
327        SetVariable setvar1b,pos={left+113,top},size={89,15},title="theta max",value= gAngHigh1
328        SetVariable setvar1c,pos={left+209,top},size={89,15},title="increm",value= gIncr1
329        SetVariable setvar1d,pos={left+299,top},size={100,15},title="# points",value= gNumPts1
330        SetVariable setvar1e,pos={left+399,top},size={93,15},title="count (s)",value= gCtTime1
331//      SetVariable setvar1a,labelBack=(65535,32768,32768)              //old rainbow
332        SetVariable setvar1a,labelBack=(49858,65535,65535)
333       
334        pt += inc
335        SetVariable setvar2a,pos={left+17,top+pt},size={90,15},title="theta min",value= gAngLow2
336        SetVariable setvar2b,pos={left+113,top+pt},size={89,15},title="theta max",value= gAngHigh2
337        SetVariable setvar2c,pos={left+209,top+pt},size={89,15},title="increm",value= gIncr2
338        SetVariable setvar2d,pos={left+299,top+pt},size={100,15},title="# points",value= gNumPts2
339        SetVariable setvar2e,pos={left+399,top+pt},size={93,15},title="count (s)",value= gCtTime2
340//      SetVariable setvar2a labelBack=(65535,65533,32768)              //old rainbow
341        SetVariable setvar2a labelBack=(21074,8995,21074)
342       
343        pt += inc
344        SetVariable setvar3a,pos={left+17,top+pt},size={90,15},title="theta min",value= gAngLow3
345        SetVariable setvar3b,pos={left+113,top+pt},size={89,15},title="theta max",value= gAngHigh3
346        SetVariable setvar3c,pos={left+209,top+pt},size={89,15},title="increm",value= gIncr3
347        SetVariable setvar3d,pos={left+299,top+pt},size={100,15},title="# points",value= gNumPts3
348        SetVariable setvar3e,pos={left+399,top+pt},size={93,15},title="count (s)",value= gCtTime3
349//      SetVariable setvar3a labelBack=(32769,65535,32768)              //old rainbow
350        SetVariable setvar3a labelBack=(0,60652,60652)
351       
352        pt += inc
353        SetVariable setvar4a,pos={left+17,top+pt},size={90,15},title="theta min",value= gAngLow4
354        SetVariable setvar4b,pos={left+113,top+pt},size={89,15},title="theta max",value= gAngHigh4
355        SetVariable setvar4c,pos={left+209,top+pt},size={89,15},title="increm",value= gIncr4
356        SetVariable setvar4d,pos={left+299,top+pt},size={100,15},title="# points",value= gNumPts4
357        SetVariable setvar4e,pos={left+399,top+pt},size={93,15},title="count (s)",value= gCtTime4
358//      SetVariable setvar4a labelBack=(32768,65535,65535)              //old rainbow
359        SetVariable setvar4a labelBack=(0,51400,0)
360       
361        pt += inc
362        SetVariable setvar5a,pos={left+17,top+pt},size={90,15},title="theta min",value= gAngLow5
363        SetVariable setvar5b,pos={left+113,top+pt},size={89,15},title="theta max",value= gAngHigh5
364        SetVariable setvar5c,pos={left+209,top+pt},size={89,15},title="increm",value= gIncr5
365        SetVariable setvar5d,pos={left+299,top+pt},size={100,15},title="# points",value= gNumPts5
366        SetVariable setvar5e,pos={left+399,top+pt},size={93,15},title="count (s)",value= gCtTime5
367//      SetVariable setvar5a labelBack=(32768,54615,65535)              //old rainbow
368        SetVariable setvar5a labelBack=(59367,49344,0)
369       
370        pt += inc
371        SetVariable setvar6a,pos={left+17,top+pt},size={90,15},title="theta min",value= gAngLow6
372        SetVariable setvar6b,pos={left+113,top+pt},size={89,15},title="theta max",value= gAngHigh6
373        SetVariable setvar6c,pos={left+209,top+pt},size={89,15},title="increm",value= gIncr6
374        SetVariable setvar6d,pos={left+299,top+pt},size={100,15},title="# points",value= gNumPts6
375        SetVariable setvar6e,pos={left+399,top+pt},size={93,15},title="count (s)",value= gCtTime6
376//      SetVariable setvar6a labelBack=(44253,29492,58982)              //old rainbow
377        SetVariable setvar6a labelBack=(54998,0,0)
378
379        pt += inc
380        SetVariable setvar7a,pos={left+17,top+pt},size={90,15},title="theta min",value= gAngLow7
381        SetVariable setvar7b,pos={left+113,top+pt},size={89,15},title="theta max",value= gAngHigh7
382        SetVariable setvar7c,pos={left+209,top+pt},size={89,15},title="increm",value= gIncr7
383        SetVariable setvar7d,pos={left+299,top+pt},size={100,15},title="# points",value= gNumPts7
384        SetVariable setvar7e,pos={left+399,top+pt},size={93,15},title="count (s)",value= gCtTime7
385//      SetVariable setvar7a labelBack=(44253,29492,58982)              //old rainbow
386        SetVariable setvar7a labelBack=(39321,21845,51657)
387
388// the action procedures and limits/increments
389        SetVariable setvar1a proc=ThetaMinSetVarProc            //,limits={-2,0,0.1}
390        SetVariable setvar2a proc=ThetaMinSetVarProc
391        SetVariable setvar3a proc=ThetaMinSetVarProc
392        SetVariable setvar4a proc=ThetaMinSetVarProc
393        SetVariable setvar5a proc=ThetaMinSetVarProc
394        SetVariable setvar6a proc=ThetaMinSetVarProc
395        SetVariable setvar7a proc=ThetaMinSetVarProc
396
397//
398        SetVariable setvar1b proc=ThetaMaxSetVarProc            //,limits={0.4,1,0.1}
399        SetVariable setvar2b proc=ThetaMaxSetVarProc
400        SetVariable setvar3b proc=ThetaMaxSetVarProc
401        SetVariable setvar4b proc=ThetaMaxSetVarProc
402        SetVariable setvar5b proc=ThetaMaxSetVarProc
403        SetVariable setvar6b proc=ThetaMaxSetVarProc
404        SetVariable setvar7b proc=ThetaMaxSetVarProc
405//
406        SetVariable setvar1c proc=IncrSetVarProc,limits={0.01,0.1,0.01}
407        SetVariable setvar2c proc=IncrSetVarProc,limits={0.02,0.2,0.02}
408        SetVariable setvar3c proc=IncrSetVarProc,limits={0.05,0.4,0.05}
409        SetVariable setvar4c proc=IncrSetVarProc,limits={0.1,1,0.1}
410        SetVariable setvar5c proc=IncrSetVarProc,limits={0.5,5,1}
411        SetVariable setvar6c proc=IncrSetVarProc,limits={1,10,2}
412        SetVariable setvar7c proc=IncrSetVarProc,limits={1,20,2}
413//
414        SetVariable setvar1d proc=NumPtsSetVarProc,limits={2,50,1}
415        SetVariable setvar2d proc=NumPtsSetVarProc,limits={2,50,1}
416        SetVariable setvar3d proc=NumPtsSetVarProc,limits={2,50,1}
417        SetVariable setvar4d proc=NumPtsSetVarProc,limits={2,50,1}
418        SetVariable setvar5d proc=NumPtsSetVarProc,limits={2,50,1}
419        SetVariable setvar6d proc=NumPtsSetVarProc,limits={2,50,1}
420        SetVariable setvar7d proc=NumPtsSetVarProc,limits={2,50,1}
421//
422        SetVariable setvar1e proc=CtTimeSetVarProc,limits={-1,50000,1}
423        SetVariable setvar2e proc=CtTimeSetVarProc,limits={-1,50000,10}
424        SetVariable setvar3e proc=CtTimeSetVarProc,limits={-1,50000,10}
425        SetVariable setvar4e proc=CtTimeSetVarProc,limits={-1,50000,30}
426        SetVariable setvar5e proc=CtTimeSetVarProc,limits={-1,50000,100}
427        SetVariable setvar6e proc=CtTimeSetVarProc,limits={-1,50000,100}
428        SetVariable setvar7e proc=CtTimeSetVarProc,limits={-1,50000,100}
429       
430        Button button0,pos={left+280,200},size={130,20},fColor=(65535,65535,0),proc=U_SimPlotButtonProc,title="Simulate USANS"
431        CheckBox check0_2,pos={left+280,250},size={60,14},title="CountRate?",variable= root:Packages:NIST:USANS:Globals:U_Sim:g_1D_PlotCR
432        CheckBox check0_3,pos={left+280,270},size={60,14},title="Noise?",variable= root:Packages:NIST:USANS:Globals:U_Sim:g_1D_AddNoise
433        CheckBox check0_4 title="Show EMP?",pos={left+280,290},proc=ShowEMPCheckProc,value=0
434
435
436//checkbox for "easy" mode
437        CheckBox check0 title="Simple mode?",pos={left+400,19},proc=EnterModeCheckProc,value=1
438        ThetaEditMode(2)                //checked on startup
439       
440//      sample setup
441        SetVariable U_setvar0_1,pos={left+20,231},size={160,15},title="Thickness (cm)"
442        SetVariable U_setvar0_1,limits={0,inf,0.1},value= root:Packages:NIST:USANS:Globals:U_Sim:gThick
443        SetVariable U_setvar0_3,pos={left+20,255},size={160,15},title="Sample Transmission"
444        SetVariable U_setvar0_3,limits={0,1,0.01},value= root:Packages:NIST:USANS:Globals:U_Sim:gSamTrans
445        PopupMenu U_popup0,pos={left+20,205},size={165,20},proc=Sim_USANS_ModelPopMenuProc,title="Model"
446        PopupMenu U_popup0,mode=1,value= #"U_FunctionPopupList()"
447       
448        //unused as of yet, not sure I'll ever need these
449//      SetVariable setvar0,pos={left+20,279},size={120,15},title="Empty Level",disable=2
450//      SetVariable setvar0,limits={0,10,0.01},value= root:Packages:NIST:USANS:Globals:U_Sim:g_EmptyLevel
451//      SetVariable setvar0_1,pos={left+20,304},size={120,15},title="Bkg Level",disable=2
452//      SetVariable setvar0_1,limits={0,10,0.01},value= root:Packages:NIST:USANS:Globals:U_Sim:g_BkgLevel
453       
454       
455       
456// a box for the results
457        SetVariable totalTime,pos={left+20,370},size={150,15},title="Count time (h:m)",value= gTotTimeStr
458        ValDisplay valdisp0_2,pos={left+20,395},size={220,13},title="Fraction of beam scattered"
459        ValDisplay valdisp0_2,limits={0,0,0},barmisc={0,1000},value= root:Packages:NIST:USANS:Globals:U_Sim:g_1DFracScatt
460        ValDisplay valdisp0_2,disable=1
461        ValDisplay valdisp0_3,pos={left+20,420},size={220,13},title="Estimated transmission"
462        ValDisplay valdisp0_3,limits={0,0,0},barmisc={0,1000},value=root:Packages:NIST:USANS:Globals:U_Sim:g_1DEstTrans
463        ValDisplay valdisp0_3,disable=1
464        Button button1,pos={left+20,400},size={150,20},proc=U_SavePanelProc,title="Save PNG"
465        Button button2,pos={left+20,430},size={150,20},proc=U_ConfigTextProc,title="Config Text"
466        Button button3,pos={left+20,460},size={150,20},proc=U_SaveButtonProc,title="Save Simulated Data"
467
468// help, done buttons
469        Button U_helpButton,pos={300,440},size={25,20},proc=showUCALCHelp,title="?"
470        Button U_helpButton,help={"Show help file for simulation of USANS Data"}
471        Button U_DoneButton,pos={350,440},size={50,20},proc=UCALCDoneButton,title="Done"
472        Button U_DoneButton,help={"This button will close the panel"}
473               
474        SetDataFolder root:
475
476EndMacro
477
478Proc UCALCDoneButton(ctrlName): ButtonControl
479        String ctrlName
480        DoWindow/K UCALC
481end
482
483Proc showUCALCHelp(ctrlName): ButtonControl
484        String ctrlName
485        DisplayHelpTopic/K=1/Z "UCALC"
486        if(V_flag !=0)
487                DoAlert 0,"The USANS Simulation Help file could not be found"
488        endif
489end
490
491// changing theta min - hold incr and #, result is new theta max
492Function ThetaMinSetVarProc(sva) : SetVariableControl
493        STRUCT WMSetVariableAction &sva
494
495        switch( sva.eventCode )
496                case 1: // mouse up
497                case 2: // Enter key
498                case 3: // Live update
499                        Variable ThetaMin = sva.dval
500                        String ns=CtrlNumber(sva.ctrlName)              //control number as a string
501                        NVAR incr = $("root:Packages:NIST:USANS:Globals:U_Sim:"+"gIncr"+ns)
502                        NVAR NumPts = $("root:Packages:NIST:USANS:Globals:U_Sim:"+"gNumPts"+ns)
503                        NVAR thetaMax = $("root:Packages:NIST:USANS:Globals:U_Sim:"+"gAngHigh"+ns)
504                       
505                        thetaMax = ThetaMin + incr*NumPts
506                       
507                        break
508        endswitch
509
510        return 0
511End
512
513
514// changing theta max - hold min and incr, result is new # of points
515// then need to recalculate the total counting time
516Function ThetaMaxSetVarProc(sva) : SetVariableControl
517        STRUCT WMSetVariableAction &sva
518
519        switch( sva.eventCode )
520                case 1: // mouse up
521                case 2: // Enter key
522                case 3: // Live update
523                        Variable ThetaMax = sva.dval
524                        String ns=CtrlNumber(sva.ctrlName)              //control number as a string
525                        NVAR thetaMin = $("root:Packages:NIST:USANS:Globals:U_Sim:"+"gAngLow"+ns)
526                        NVAR NumPts = $("root:Packages:NIST:USANS:Globals:U_Sim:"+"gNumPts"+ns)
527                        NVAR Incr = $("root:Packages:NIST:USANS:Globals:U_Sim:"+"gIncr"+ns)
528                       
529                        NumPts = trunc( (thetaMax - ThetaMin) / incr )
530               
531                        CalcTotalCountTime()
532                       
533                        break
534        endswitch
535
536        return 0
537End
538
539// changing increment - hold min and #, result is new max
540Function IncrSetVarProc(sva) : SetVariableControl
541        STRUCT WMSetVariableAction &sva
542
543        switch( sva.eventCode )
544                case 1: // mouse up
545                case 2: // Enter key
546                case 3: // Live update
547                        Variable incr = sva.dval
548                        String ns=CtrlNumber(sva.ctrlName)              //control number as a string
549                        NVAR thetaMin = $("root:Packages:NIST:USANS:Globals:U_Sim:"+"gAngLow"+ns)
550                        NVAR NumPts = $("root:Packages:NIST:USANS:Globals:U_Sim:"+"gNumPts"+ns)
551                        NVAR thetaMax = $("root:Packages:NIST:USANS:Globals:U_Sim:"+"gAngHigh"+ns)
552                       
553                        thetaMax = ThetaMin + incr*NumPts
554               
555                        break
556        endswitch
557
558        return 0
559End
560
561// changing #pts - hold min and incr, result is new max
562// then recalculate the total count time
563Function NumPtsSetVarProc(sva) : SetVariableControl
564        STRUCT WMSetVariableAction &sva
565
566        switch( sva.eventCode )
567                case 1: // mouse up
568                case 2: // Enter key
569                case 3: // Live update
570                        Variable NumPts = sva.dval
571                        String ns=CtrlNumber(sva.ctrlName)              //control number as a string
572                        NVAR thetaMin = $("root:Packages:NIST:USANS:Globals:U_Sim:"+"gAngLow"+ns)
573                        NVAR incr = $("root:Packages:NIST:USANS:Globals:U_Sim:"+"gIncr"+ns)
574                        NVAR thetaMax = $("root:Packages:NIST:USANS:Globals:U_Sim:"+"gAngHigh"+ns)
575
576// old way, like ICP                   
577//                      thetaMax = ThetaMin + incr*NumPts
578
579// new way - to spread the points out over the specified angle range
580                        incr = (thetaMax - thetaMin) / numpts
581                       
582                        CalcTotalCountTime()
583                       
584                        break
585        endswitch
586
587        return 0
588End
589
590// changing count time -
591// then recalculate the total count time
592Function CtTimeSetVarProc(sva) : SetVariableControl
593        STRUCT WMSetVariableAction &sva
594
595        switch( sva.eventCode )
596                case 1: // mouse up
597                case 2: // Enter key
598                case 3: // Live update
599                        CalcTotalCountTime()
600                        break
601        endswitch
602
603        return 0
604End
605
606// hard-wired for 7 controls
607// return value is in seconds
608// global display string is set with hrs:min
609Function CalcTotalCountTime()
610
611        Variable ii,num,totTime=0
612        String pathStr="root:Packages:NIST:USANS:Globals:U_Sim:"
613        num=7
614       
615        for(ii=1;ii<=num;ii+=1)
616                NVAR ctTime = $(pathStr+"gCtTime"+num2str(ii))
617                NVAR numPts = $(pathStr+"gNumPts"+num2str(ii))
618                if(ctTime>0)
619                        totTime += ctTime*numPts
620                endif
621        endfor
622        Variable hrs,mins
623        hrs = trunc(totTime/3600)
624        mins = trunc(mod(totTime,3600)/60)
625//      Printf "Counting time (hr:min) = %d:%d\r",hrs,mins
626       
627        SVAR str = $(pathStr+"gTotTimeStr")
628        sprintf str,"%d:%d",hrs,mins
629       
630        return(totTime)
631End
632
633//returns the control number from the name string
634// all are setvarNa
635Function/S CtrlNumber(str)
636        String str
637       
638        return(str[6])
639End
640
641// changes edit mode of the theta min/max boxes and increment for simplified setup
642// val = 2 = disable
643// val = 0 = edit enabled
644Function ThetaEditMode(val)
645        Variable val
646       
647        SetVariable setvar1a,win=UCALC,disable=val
648        SetVariable setvar1b,win=UCALC,disable=val
649        SetVariable setvar1c,win=UCALC,disable=val
650       
651        SetVariable setvar2a,win=UCALC,disable=val
652        SetVariable setvar2b,win=UCALC,disable=val
653        SetVariable setvar2c,win=UCALC,disable=val
654       
655        SetVariable setvar3a,win=UCALC,disable=val
656        SetVariable setvar3b,win=UCALC,disable=val
657        SetVariable setvar3c,win=UCALC,disable=val
658       
659        SetVariable setvar4a,win=UCALC,disable=val
660        SetVariable setvar4b,win=UCALC,disable=val
661        SetVariable setvar4c,win=UCALC,disable=val
662       
663        SetVariable setvar5a,win=UCALC,disable=val
664        SetVariable setvar5b,win=UCALC,disable=val
665        SetVariable setvar5c,win=UCALC,disable=val
666       
667        SetVariable setvar6a,win=UCALC,disable=val
668        SetVariable setvar6b,win=UCALC,disable=val
669        SetVariable setvar6c,win=UCALC,disable=val
670       
671        SetVariable setvar7a,win=UCALC,disable=val
672        SetVariable setvar7b,win=UCALC,disable=val
673        SetVariable setvar7c,win=UCALC,disable=val
674        return(0)
675End
676
677
678
679Function EnterModeCheckProc(cba) : CheckBoxControl
680        STRUCT WMCheckboxAction &cba
681
682        switch( cba.eventCode )
683                case 2: // mouse up
684                        Variable checked = cba.checked
685                       
686                        if(checked)
687                                ThetaEditMode(2)
688                        else
689                                ThetaEditMode(0)
690                        endif
691                       
692                        break
693        endswitch
694
695        return 0
696End
697
698
699Function ShowEMPCheckProc(cba) : CheckBoxControl
700        STRUCT WMCheckboxAction &cba
701
702        switch( cba.eventCode )
703                case 2: // mouse up
704                        Variable checked = cba.checked
705
706                        String list,item,popStr,qval,CR
707                        Variable OK=1
708
709                        if(exists("root:Packages:NIST:USANS:Globals:Q_2p0")==0)                 //
710                                MakeUSANSEmptyWaves()
711                        endif
712                       
713                       
714                        if(checked)
715                                // put it on the graph
716                                SetDataFolder root:Packages:NIST:USANS:Globals
717                                ControlInfo/W=UCALC popup0
718                                popStr = S_Value
719                                strswitch(popStr)       // string switch
720                                        case "0.25":            // execute if case matches expression
721                                                qval = "Q_0p25"
722                                                CR = "CR_0p25"
723                                                break           
724                                        case "0.50":   
725                                                qval = "Q_0p50"
726                                                CR = "CR_0p50"
727                                                break   
728                                        case "0.625":           
729                                                qval = "Q_0p625"
730                                                CR = "CR_0p625"
731                                                break   
732                                        case "1.75":           
733                                                qval = "Q_1p75"
734                                                CR = "CR_1p75"
735                                                break   
736                                        case "2.0":             
737                                                qval = "Q_2p0"
738                                                CR = "CR_2p0"
739                                                break   
740                                        default:                                                        // optional default expression executed
741                                                OK=0
742                                endswitch
743                               
744                                if(OK)
745                                        AppendToGraph/W=UCALC $CR vs $qval
746                                        ModifyGraph marker=19,mode($CR)=4,msize($CR)=3,rgb($CR)=(0,0,0)
747                                endif
748                               
749                        else
750                                //take it off of the graph
751                                SetDataFolder root:Packages:NIST:USANS:Globals
752                                list=WaveList("CR*", ";", "WIN:UCALC")
753                                item=StringFromList(0, list ,";")               //should be one item
754                                if(strlen(item) != 0)
755                                        RemoveFromGraph/W=UCALC $item
756                                endif
757                        endif
758                       
759                        break
760        endswitch
761
762        SetDataFolder root:
763        return 0
764End
765
766
767
768// based on the angle ranges above (with non-zero count times)
769// plot where the data points would be
770//
771Function U_SimPlotButtonProc(ba) : ButtonControl
772        STRUCT WMButtonAction &ba
773
774       
775        switch( ba.eventCode )
776                case 2: // mouse up
777                        // click code here
778                       
779                        CalcUSANS()
780                       
781                        break
782        endswitch
783
784        return 0
785End
786
787
788
789// Fills a fake USANS folder with the concatenated ranges, and converts to Q
790// root:Packages:NIST:USANS:SIM
791//
792// does the work of calculating and smearing the simluated USANS data set
793Function CalcUSANS()   
794
795        Variable num,ii,firstSet=0
796        String pathStr="root:Packages:NIST:USANS:Globals:U_Sim:gCtTime"
797        String fromType="SWAP",toType="SIM"
798        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
799       
800        num = 7                 //# of angular ranges
801       
802        // only try to plot ranges with non-zero count times
803        firstSet=0
804        for(ii=1;ii<=num;ii+=1)
805                NVAR dum = $(pathStr+num2str(ii))
806                if(dum>0)                                               
807//                      print "CtTime = ",dum
808                        firstSet += 1
809                        LoadSimulatedAngleRange(ii,"SWAP")      //overwrite what's in the SWAP folder
810                       
811                        // did not do this step
812                        //Convert2Countrate("SWAP",1)
813                        //
814                       
815                        if(firstSet==1) //first time, overwrite
816                                NewDataWaves("SWAP","SIM")
817                                //plus my two waves
818                                Duplicate/O $(USANSFolder+":"+fromType+":countingTime"),$(USANSFolder+":"+toType+":countingTime")
819                                Duplicate/O $(USANSFolder+":"+fromType+":SetNumber"),$(USANSFolder+":"+toType+":SetNumber")
820
821                        else            //append to waves in "SIM"
822                                AppendDataWaves("SWAP","SIM")
823                                // and my two waves
824                                ConcatenateData( (USANSFolder+":"+toType+":countingTime"),(USANSFolder+":"+fromType+":countingTime") )
825                                ConcatenateData( (USANSFolder+":"+toType+":SetNumber"),(USANSFolder+":"+fromType+":SetNumber") )
826
827                        endif
828                       
829                endif
830        endfor
831
832
833        //sort after all loaded - not by angle, but by Q
834// Get rid of the negative angles - the smearing integration does not like these!
835// (may add them back in later, but probably not)
836        UDoAngleSort("SIM")
837       
838        ConvertAngle2Qvals("SIM",0)
839       
840       
841       
842        //fill the data with something
843        WAVE qvals = root:Packages:NIST:USANS:SIM:qvals
844        WAVE DetCts = root:Packages:NIST:USANS:SIM:DetCts
845       
846
847        //find the Trans Cts for T_Wide
848//      FindTWideCts("EMP")
849
850//generate a "fake" 1d data folder/set named "Sim_USANS" that can be used for smearing
851// DOES NOT calculate a matrix, but instead fills 4-5-6 columns with -dQv
852// so that the trapezoid rule is used
853//     
854
855        FakeUSANSDataFolder(qvals,DetCts,0.117,"Sim_USANS")
856
857        // now calculate the smearing... instead of the counts
858        SVAR funcStr = root:Packages:NIST:USANS:Globals:U_Sim:gFuncStr          //set by the popup     
859       
860        Wave inten = root:Sim_USANS:Sim_USANS_i
861        Wave sigave = root:Sim_USANS:Sim_USANS_s
862        Wave countingTime = root:Sim_USANS:countingTime         //counting time per point in the set
863
864        Duplicate/O inten root:Sim_USANS:Smeared_inten          // a place for the smeared result for probabilities
865        Wave Smeared_inten = root:Sim_USANS:Smeared_inten
866
867        String coefStr=""
868        Variable sig_sas=0,wavelength = 2.4
869        Variable Imon
870       
871
872        NVAR omega = root:Packages:NIST:USANS:Globals:U_Sim:gAnalyzerOmega
873       
874        if(exists(funcStr) != 0)
875                FUNCREF SANSModelAAO_proto func=$("fSmeared"+funcStr)                   //a wrapper for the structure version
876                FUNCREF SANSModelAAO_proto funcUnsmeared=$(funcStr)             //unsmeared
877                coefStr = MC_getFunctionCoef(funcStr)
878               
879                if(!MC_CheckFunctionAndCoef(funcStr,coefStr))
880                        Abort "Function and coefficients do not match. You must plot the unsmeared function before simulation."
881                endif
882               
883                // do the smearing calculation
884                func($coefStr,Smeared_inten,qvals)
885
886
887                NVAR thick = root:Packages:NIST:USANS:Globals:U_Sim:gThick
888                NVAR trans = root:Packages:NIST:USANS:Globals:U_Sim:gSamTrans
889                NVAR SimDetCts = root:Packages:NIST:USANS:Globals:U_Sim:g_1DTotCts                      //summed counts (simulated)
890                NVAR estDetCR = root:Packages:NIST:USANS:Globals:U_Sim:g_1DEstDetCR                     // estimated detector count rate
891                NVAR fracScat = root:Packages:NIST:USANS:Globals:U_Sim:g_1DFracScatt            // fraction of beam captured on detector
892                NVAR estTrans = root:Packages:NIST:USANS:Globals:U_Sim:g_1DEstTrans             // estimated transmission of sample
893//              NVAR SimCountTime = root:Packages:NIST:USANS:Globals:U_Sim:gCntTime             //counting time used for simulation
894               
895                Imon = GetUSANSBeamIntensity()                          //based on the aperture size, select the beam intensity
896                Print "imon=",imon
897               
898                // calculate the scattering cross section simply to be able to estimate the transmission
899                // unfortunately, this calculation is useless in the USANS range. So although it's calculated, the
900                // results are never reported.
901                // -- the main issue is that the integration range is optimized for SANS, and is not useful for USANS
902                // -there are only a few points in the USANS range, and extending it lower caused issues before.
903                CalculateRandomDeviate(funcUnsmeared,$coefStr,wavelength,"root:Packages:NIST:SAS:ran_dev",sig_sas)
904               
905//              if(sig_sas > 100)
906//                      sprintf abortStr,"sig_sas = %g. Please check that the model coefficients have a zero background, or the low q is well-behaved.",sig_sas
907//              endif
908                estTrans = exp(-1*thick*sig_sas)                //thickness and sigma both in units of cm
909                Print "Sig_sas = ",sig_sas
910               
911               
912                Duplicate/O qvals prob_i
913                                       
914                prob_i = trans*thick*omega*Smeared_inten                        //probability of a neutron in q-bin(i)
915               
916//              Variable P_on = sum(prob_i,-inf,inf)
917//              Print "P_on = ",P_on
918                fracScat = 1-estTrans
919               
920                inten = (Imon*countingTime)*prob_i
921               
922                // do I round to an integer?
923                inten = round(inten)
924
925//              SimDetCts = sum(inten,-inf,inf)
926//              estDetCR = SimDetCts/SimCountTime
927               
928               
929                NVAR doABS = root:Packages:NIST:USANS:Globals:U_Sim:g_1D_DoABS
930                NVAR plotCR = root:Packages:NIST:USANS:Globals:U_Sim:g_1D_PlotCR
931                NVAR addNoise = root:Packages:NIST:USANS:Globals:U_Sim:g_1D_AddNoise
932                                       
933                sigave = sqrt(inten)            // assuming that N is large
934               
935                // add in random error in aveint based on the sigave
936                if(addNoise)
937                        inten += gnoise(sigave)
938                       
939                        //round to an integer again
940                        inten = round(inten)           
941                endif
942
943                // convert to absolute scale? Maybe not needed
944                // does nothing yet - need Ipeak, Twide
945//              if(doABS)
946//                      Variable kappa = thick*omega*trans*iMon*ctTime
947//                      inten /= kappa
948//                      inten /= kappa
949//              endif
950
951                // plot as countrate - maybe easier to visualize, and all of the data overlaps
952                if(plotCR)
953                        inten /= countingTime
954                        sigave /= countingTime
955                endif
956               
957                GraphSIM()
958
959        else
960                //no function plotted, no simulation can be done
961                DoAlert 0,"No function is selected or plotted, so no simulation is done. The default power law function is used."
962
963                inten = U_Power_Law_Model(1e-6,3,0,qvals)
964//              inten = U_SphereForm(1,9000,6e-6,0,qvals)               
965       
966                GraphSIM()
967
968        endif
969       
970
971end
972
973
974//sort the data in the "type"folder, based on angle
975//carry along all associated waves
976//
977// ---a duplicate of DoAngleSort(), by modified to
978// include counting time and setNumber
979//
980// also trims the beginning of each data set so that it does not include any negative or zero angles
981//
982Function UDoAngleSort(type)
983        String type
984       
985        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
986       
987        Wave angle = $(USANSFolder+":"+Type+":Angle")
988        Wave detCts = $(USANSFolder+":"+Type+":DetCts")
989        Wave ErrdetCts = $(USANSFolder+":"+Type+":ErrDetCts")
990        Wave MonCts = $(USANSFolder+":"+Type+":MonCts")
991        Wave TransCts = $(USANSFolder+":"+Type+":TransCts")
992        Wave countingTime = $(USANSFolder+":"+Type+":countingTime")
993        Wave SetNumber = $(USANSFolder+":"+Type+":SetNumber")
994       
995        Sort Angle DetCts,ErrDetCts,MonCts,TransCts,Angle,countingTime,SetNumber
996       
997        Variable ii,num,numBad,ang,val
998        num=numpnts(angle)
999        ii=0
1000        numBad=0
1001        val = 0         //cutoff value
1002        do
1003                ang = angle[ii]
1004                if(ang <= val)
1005                        numBad += 1
1006                else            //keep the points
1007                        Angle[ii-numBad] = ang
1008                        DetCts[ii-numBad] = DetCts[ii]
1009                        ErrDetCts[ii-numBad] = ErrDetCts[ii]
1010                        MonCts[ii-numBad] = MonCts[ii]
1011                        TransCts[ii-numBad] = TransCts[ii]
1012                        countingTime[ii-numBad] = countingTime[ii]
1013                        SetNumber[ii-numBad] = SetNumber[ii]
1014                endif
1015                ii += 1
1016        while(ii<num)
1017        //trim the end of the waves
1018        DeletePoints num-numBad, numBad, DetCts,ErrDetCts,MonCts,TransCts,Angle,countingTime,SetNumber
1019       
1020       
1021        return(0)
1022End
1023
1024// a simple default function
1025//
1026Function U_SphereForm(scale,radius,delrho,bkg,x)                               
1027        Variable scale,radius,delrho,bkg
1028        Variable x
1029       
1030        // variables are:                                                       
1031        //[0] scale
1032        //[1] radius (A)
1033        //[2] delrho (A-2)
1034        //[3] background (cm-1)
1035
1036       
1037        // calculates scale * f^2/Vol where f=Vol*3*delrho*((sin(qr)-qrcos(qr))/qr^3
1038        // and is rescaled to give [=] cm^-1
1039       
1040        Variable bes,f,vol,f2
1041        //
1042        //handle q==0 separately
1043        If(x==0)
1044                f = 4/3*pi*radius^3*delrho*delrho*scale*1e8 + bkg
1045                return(f)
1046        Endif
1047       
1048        bes = 3*(sin(x*radius)-x*radius*cos(x*radius))/x^3/radius^3
1049        vol = 4*pi/3*radius^3
1050        f = vol*bes*delrho              // [=] A
1051        // normalize to single particle volume, convert to 1/cm
1052        f2 = f * f / vol * 1.0e8                // [=] 1/cm
1053       
1054        return (scale*f2+bkg)   // Scale, then add in the background
1055       
1056End
1057
1058// better default function
1059Function U_Power_Law_Model(A,m,bgd,x) : FitFunc
1060        Variable A, m,bgd,x
1061//       Input (fitting) variables are:
1062        //[0] Coefficient
1063        //[1] (-) Power
1064        //[2] incoherent background
1065       
1066//      local variables
1067        Variable inten, qval
1068//      x is the q-value for the calculation
1069        qval = x
1070//      do the calculation and return the function value
1071       
1072        inten = A*qval^-m + bgd
1073        Return (inten)
1074End
1075
1076
1077// mimics LoadBT5File
1078// creates two other waves to identify the set and the counting time for that set
1079//
1080Function LoadSimulatedAngleRange(set,type)
1081        Variable set
1082        String type
1083
1084        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
1085       
1086        String s=num2str(set)
1087        NVAR angLow = $("root:Packages:NIST:USANS:Globals:U_Sim:gAngLow"+s)
1088        NVAR angHigh = $("root:Packages:NIST:USANS:Globals:U_Sim:gAngHigh"+s)
1089        NVAR numPts = $("root:Packages:NIST:USANS:Globals:U_Sim:gNumPts"+s)
1090        NVAR ctTime = $("root:Packages:NIST:USANS:Globals:U_Sim:gCtTime"+s)
1091        NVAR incr = $("root:Packages:NIST:USANS:Globals:U_Sim:gIncr"+s)
1092       
1093        Variable ii, err=0
1094       
1095        // generate q-points based on angular range from panel
1096       
1097        Make/O/D/N=(numPts) $(USANSFolder+":"+type+":Angle")
1098        Make/O/D/N=(numPts) $(USANSFolder+":"+type+":DetCts")
1099        Make/O/D/N=(numPts) $(USANSFolder+":"+type+":ErrDetCts")
1100        Make/O/D/N=(numPts) $(USANSFolder+":"+type+":MonCts")
1101        Make/O/D/N=(numPts) $(USANSFolder+":"+type+":TransCts")
1102        //
1103        Make/O/D/N=(numPts) $(USANSFolder+":"+type+":CountingTime")
1104        Make/O/D/N=(numPts) $(USANSFolder+":"+type+":SetNumber")
1105       
1106        Wave Angle = $(USANSFolder+":"+type+":Angle")
1107        Wave DetCts = $(USANSFolder+":"+type+":DetCts")
1108        Wave ErrDetCts = $(USANSFolder+":"+type+":ErrDetCts")
1109        Wave MonCts = $(USANSFolder+":"+type+":MonCts")
1110        Wave TransCts = $(USANSFolder+":"+type+":TransCts")
1111        Wave countingTime = $(USANSFolder+":"+type+":countingTime")
1112        Wave SetNumber = $(USANSFolder+":"+type+":SetNumber")
1113       
1114        countingTime = ctTime
1115        SetNumber = set
1116       
1117        for(ii=0;ii<numPts;ii+=1)
1118                Angle[ii] = angLow + ii*incr
1119                DetCts[ii] = set
1120        endfor
1121       
1122        //set the wave note for the DetCts
1123        String str=""
1124        str = "FILE:Sim "+s+";"
1125        str += "TIMEPT:"+num2str(ctTime)+";"
1126        str += "PEAKANG:0;"             //no value yet
1127        str += "STATUS:;"               //no value yet
1128        str += "LABEL:SimSet "+s+";"
1129        str += "PEAKVAL:;"              //no value yet
1130        str += "TWIDE:0;"               //no value yet
1131        Note DetCts,str
1132       
1133        return err                      // Zero signifies no error.     
1134End
1135
1136// add SIM data to the graph if it exists and is not already on the graph
1137//
1138//
1139// ** currently, I have changed the graph to not display the angle at the top. I have not yet
1140// found a way to keep the scaling of the two axes in-sync when the empty cell data is added to the
1141// graph (versus Q)
1142//
1143Function GraphSIM()
1144
1145//      SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
1146//      SetDataFolder $(USANSFolder+":SIM")
1147
1148        SetDataFolder root:Sim_USANS
1149        //is it already on the graph?
1150        String list=""
1151        list = Wavelist("Sim_USANS*",";","WIN:UCALC")
1152       
1153        if(strlen(list)!=0)
1154//              Print "SIM already on graph"
1155                SetDataFolder root:
1156                return(0)
1157        Endif
1158       
1159        //append the data if it exists
1160        If(waveExists($"Sim_USANS_i")==1)
1161                DoWindow/F UCALC
1162
1163// lines for the no-noise result vs angle
1164//              AppendToGraph/T Smeared_inten vs angle
1165                AppendToGraph Smeared_inten vs Sim_USANS_q
1166                ModifyGraph rgb(Smeared_inten)=(1,12815,52428)
1167                ModifyGraph mode(Smeared_inten)=4,marker(Smeared_inten)=19,msize(Smeared_inten)=2
1168                ModifyGraph tickUnit=1
1169
1170// colored points for the simulation with noise on top
1171                AppendToGraph Sim_USANS_i vs Sim_USANS_q
1172                ModifyGraph mode(Sim_USANS_i)=3,marker(Sim_USANS_i)=19,msize(Sim_USANS_i)=4
1173                //don't reverse the dbZ21 or the highest angle will have "invisible" light blue error bars
1174                ModifyGraph zColor(Sim_USANS_i)={setNumber,1,7,dBZ21,0}                         //better for 7 colors
1175//              ModifyGraph zColor(Sim_USANS_i)={setNumber,1,7,Rainbow,0}                       //force the colors from 1->7
1176                ModifyGraph useMrkStrokeRGB(Sim_USANS_i)=1
1177                ErrorBars/T=0 Sim_USANS_i Y,wave=(Sim_USANS_s,Sim_USANS_s)
1178               
1179                ModifyGraph log=1
1180                ModifyGraph mirror(left)=1
1181                // if no top axis, then
1182//              ModifyGraph mirror(bottom)=1
1183                ModifyGraph grid=2
1184                ModifyGraph standoff=0
1185               
1186                // to make sure that the scales are the same (but fails on zoom)
1187                NewFreeAxis/O/T top_angle
1188                ModifyFreeAxis/Z top_angle,master= bottom,hook= TransformAngleAxisHook
1189                ModifyGraph lblPos(top_angle)=50,freePos(top_angle)=0
1190                ModifyGraph log(top_angle)=1
1191               
1192                SetDrawEnv linefgc= (39321,1,1),dash= 3,linethick= 3
1193                SetDrawEnv xcoord= bottom
1194                DrawLine 3e-05,0.01,3e-05,0.99
1195               
1196                Label top_angle "Angle"
1197                Label bottom "Q (1/A)"
1198                Label left "Counts or Count Rate"
1199               
1200                Legend
1201        endif
1202       
1203        SetDataFolder root:
1204End
1205
1206Function TransformAngleAxisHook(s)
1207        STRUCT WMAxisHookStruct &s
1208
1209        s.max= s.max/5.55e-5
1210        s.min= s.min/5.55e-5
1211       
1212        return 0
1213End
1214
1215//fakes a folder with loaded 1-d usans data, no calculation of the matrix
1216Function        FakeUSANSDataFolder(qval,aveint,dqv,dataFolder)
1217        WAVE qval,aveint
1218        Variable dqv
1219        String dataFolder
1220       
1221
1222        String baseStr=dataFolder
1223        if(DataFolderExists("root:"+baseStr))
1224                SetDataFolder $("root:"+baseStr)
1225        else
1226                NewDataFolder/S $("root:"+baseStr)
1227        endif
1228
1229        ////overwrite the existing data, if it exists
1230        Duplicate/O qval, $(baseStr+"_q")
1231        Duplicate/O aveint, $(baseStr+"_i")
1232       
1233        Duplicate/O qval, $(baseStr+"_s")
1234        Wave sigave = $(baseStr+"_s")
1235
1236        sigave = sqrt(aveint)
1237
1238        // make a resolution matrix for SANS data
1239        Variable np=numpnts(qval)
1240        Make/D/O/N=(np,4) $(baseStr+"_res")
1241        Wave res=$(baseStr+"_res")
1242       
1243        res[][0] = -dQv         //sigQ
1244        res[][1] = -dQv         //qBar
1245        res[][2] = -dQv         //fShad
1246        res[][3] = qval[p]              //Qvalues
1247       
1248        // extra waves of set number and counting time for the simulation
1249        WAVE ctW = root:Packages:NIST:USANS:SIM:countingTime
1250        WAVE setW = root:Packages:NIST:USANS:SIM:setNumber
1251        WAVE ang = root:Packages:NIST:USANS:SIM:Angle
1252        Duplicate/O ctW countingTime
1253        Duplicate/O setW setNumber
1254        Duplicate/O ang Angle
1255
1256
1257        //clean up             
1258        SetDataFolder root:
1259       
1260End
1261
1262
1263Function/S U_FunctionPopupList()
1264        String list,tmp
1265        list = User_FunctionPopupList()
1266       
1267        //simplify the display, forcing smeared calculations behind the scenes
1268        tmp = FunctionList("Smear*",";","NPARAMS:1")    //smeared dependency calculations
1269        list = RemoveFromList(tmp, list ,";")
1270
1271
1272        if(strlen(list)==0)
1273                list = "No functions plotted"
1274        endif
1275       
1276        list = SortList(list)
1277       
1278        return(list)
1279End     
1280
1281Function Sim_USANS_ModelPopMenuProc(pa) : PopupMenuControl
1282        STRUCT WMPopupAction &pa
1283
1284        switch( pa.eventCode )
1285                case 2: // mouse up
1286                        Variable popNum = pa.popNum
1287                        String popStr = pa.popStr
1288                        SVAR gStr = root:Packages:NIST:USANS:Globals:U_Sim:gFuncStr
1289                        gStr = popStr
1290                       
1291                        break
1292        endswitch
1293
1294        return 0
1295End         
1296
1297// if the sample aperture is changed, AND the empty data is displayed, change to the proper data
1298Function UCALC_SampleAperturePopup(pa) : PopupMenuControl
1299        STRUCT WMPopupAction &pa
1300
1301        switch( pa.eventCode )
1302                case 2: // mouse up
1303                        Variable popNum = pa.popNum
1304                        String popStr = pa.popStr
1305
1306                        ControlInfo/W=UCALC check0_4
1307                        if(V_Value==1)          //currently checked, need to update
1308                               
1309                                STRUCT WMCheckboxAction cba
1310                                cba.checked=0           //"un-check"
1311                                cba.eventCode=2
1312                                ShowEMPCheckProc(cba)
1313                               
1314                                cba.checked=1           //"re-check"
1315                                ShowEMPCheckProc(cba)
1316                       
1317                        endif
1318                        break
1319                       
1320        endswitch
1321       
1322        return 0
1323End 
1324
1325
1326Function UCALC_PresetPopup(pa) : PopupMenuControl
1327        STRUCT WMPopupAction &pa
1328
1329        switch( pa.eventCode )
1330                case 2: // mouse up
1331                        Variable popNum = pa.popNum
1332                        String popStr = pa.popStr
1333
1334                        SetDataFolder root:Packages:NIST:USANS:Globals:U_Sim
1335                       
1336                        NVAR gAngLow1 = gAngLow1
1337                        NVAR gAngHigh1 = gAngHigh1
1338                        NVAR gNumPts1 = gNumPts1
1339                        NVAR gCtTime1 = gCtTime1
1340                        NVAR gIncr1 = gIncr1
1341                       
1342                        NVAR gAngLow2 = gAngLow2
1343                        NVAR gAngHigh2 = gAngHigh2
1344                        NVAR gNumPts2 = gNumPts2
1345                        NVAR gCtTime2 = gCtTime2
1346                        NVAR gIncr2 = gIncr2
1347                       
1348                        NVAR gAngLow3 = gAngLow3
1349                        NVAR gAngHigh3 = gAngHigh3
1350                        NVAR gNumPts3 = gNumPts3
1351                        NVAR gCtTime3 = gCtTime3
1352                        NVAR gIncr3 = gIncr3
1353                       
1354                        NVAR gAngLow4 = gAngLow4
1355                        NVAR gAngHigh4 = gAngHigh4
1356                        NVAR gNumPts4 = gNumPts4
1357                        NVAR gCtTime4 = gCtTime4
1358                        NVAR gIncr4 = gIncr4
1359                       
1360                        NVAR gAngLow5 = gAngLow5
1361                        NVAR gAngHigh5 = gAngHigh5
1362                        NVAR gNumPts5 = gNumPts5
1363                        NVAR gCtTime5 = gCtTime5
1364                        NVAR gIncr5 = gIncr5
1365                       
1366                        NVAR gAngLow6 = gAngLow6
1367                        NVAR gAngHigh6 = gAngHigh6
1368                        NVAR gNumPts6 = gNumPts6
1369                        NVAR gCtTime6 = gCtTime6
1370                        NVAR gIncr6 = gIncr6
1371                       
1372                        NVAR gAngLow7 = gAngLow7
1373                        NVAR gAngHigh7 = gAngHigh7
1374                        NVAR gNumPts7 = gNumPts7
1375                        NVAR gCtTime7 = gCtTime7
1376                        NVAR gIncr7 = gIncr7
1377                       
1378                        strswitch(popStr)       // string switch
1379                                case "Short Count":             // execute if case matches expression
1380                                       
1381                                        gAngLow1 = -1
1382                                        gAngHigh1 = 0.6
1383                                        gNumPts1 = 33
1384                                        gCtTime1 = 10
1385                                        gIncr1 = 0.05   
1386                                       
1387                                        gAngLow2 = 0.7
1388                                        gAngHigh2 = 1.9
1389                                        gNumPts2 = 6
1390                                        gCtTime2 = 60
1391                                        gIncr2 = 0.2   
1392                                       
1393                                        gAngLow3 = 2
1394                                        gAngHigh3 = 4.8
1395                                        gNumPts3 = 7
1396                                        gCtTime3 = 120
1397                                        gIncr3 = 0.4   
1398                                       
1399                                        gAngLow4 = 5
1400                                        gAngHigh4 = 9.5
1401                                        gNumPts4 = 5
1402                                        gCtTime4 = 180
1403                                        gIncr4 = 0.9   
1404                                       
1405                                        gAngLow5 = 10
1406                                        gAngHigh5 = 19
1407                                        gNumPts5 = 5
1408                                        gCtTime5 = 240
1409                                        gIncr5 = 1.8
1410                                       
1411                                        gAngLow6 = 20
1412                                        gAngHigh6 = 48
1413                                        gNumPts6 = 15
1414                                        gCtTime6 = 0
1415                                        gIncr6 = 2
1416                                       
1417                                        gAngLow7 = 50
1418                                        gAngHigh7 = 95
1419                                        gNumPts7 = 10
1420                                        gCtTime7 = 0
1421                                        gIncr7 = 5
1422                                       
1423                                        break                                           
1424                                case "Medium Count":   
1425                       
1426                                        gAngLow1 = -1
1427                                        gAngHigh1 = 0.6
1428                                        gNumPts1 = 33
1429                                        gCtTime1 = 10
1430                                        gIncr1 = 0.05   
1431                                       
1432                                        gAngLow2 = 0.7
1433                                        gAngHigh2 = 1.9
1434                                        gNumPts2 = 13
1435                                        gCtTime2 = 60
1436                                        gIncr2 = 0.1   
1437                                       
1438                                        gAngLow3 = 2
1439                                        gAngHigh3 = 4.8
1440                                        gNumPts3 = 15
1441                                        gCtTime3 = 120
1442                                        gIncr3 = 0.2   
1443                                       
1444                                        gAngLow4 = 5
1445                                        gAngHigh4 = 9.5
1446                                        gNumPts4 = 10
1447                                        gCtTime4 = 300
1448                                        gIncr4 = 0.5   
1449                                       
1450                                        gAngLow5 = 10
1451                                        gAngHigh5 = 19
1452                                        gNumPts5 = 10
1453                                        gCtTime5 = 600
1454                                        gIncr5 = 1
1455                                       
1456                                        gAngLow6 = 20
1457                                        gAngHigh6 = 48
1458                                        gNumPts6 = 15
1459                                        gCtTime6 = 1200
1460                                        gIncr6 = 2     
1461                                       
1462                                        gAngLow7 = 50
1463                                        gAngHigh7 = 95
1464                                        gNumPts7 = 10
1465                                        gCtTime7 = 0
1466                                        gIncr7 = 5
1467                                                               
1468                                        break
1469                                case "Long Count":     
1470                                       
1471                                        gAngLow1 = -1
1472                                        gAngHigh1 = 0.6
1473                                        gNumPts1 = 33
1474                                        gCtTime1 = 25
1475                                        gIncr1 = 0.05   
1476                                       
1477                                        gAngLow2 = 0.7
1478                                        gAngHigh2 = 1.9
1479                                        gNumPts2 = 13
1480                                        gCtTime2 = 100
1481                                        gIncr2 = 0.1   
1482                                       
1483                                        gAngLow3 = 2
1484                                        gAngHigh3 = 4.8
1485                                        gNumPts3 = 15
1486                                        gCtTime3 = 300
1487                                        gIncr3 = 0.2   
1488                                       
1489                                        gAngLow4 = 5
1490                                        gAngHigh4 = 9.5
1491                                        gNumPts4 = 10
1492                                        gCtTime4 = 600
1493                                        gIncr4 = 0.5   
1494                                       
1495                                        gAngLow5 = 10
1496                                        gAngHigh5 = 19
1497                                        gNumPts5 = 10
1498                                        gCtTime5 = 1200
1499                                        gIncr5 = 1
1500                                       
1501                                        gAngLow6 = 20
1502                                        gAngHigh6 = 48
1503                                        gNumPts6 = 15
1504                                        gCtTime6 = 2000
1505                                        gIncr6 = 2
1506                                       
1507                                        gAngLow7 = 50
1508                                        gAngHigh7 = 95
1509                                        gNumPts7 = 10
1510                                        gCtTime7 = 3000
1511                                        gIncr7 = 5
1512                                       
1513                                        break
1514                                default:                       
1515                        endswitch
1516                       
1517                        break
1518        endswitch
1519
1520        //update the count time
1521        CalcTotalCountTime()
1522
1523        SetDataFolder root:
1524       
1525        return 0
1526End     
1527
1528
1529// return the beam intensity based on the sample aperture diameter
1530//
1531// based on the equation in John's instrument paper
1532//
1533Function GetUSANSBeamIntensity()       
1534
1535        String popStr
1536        Variable flux,diam,rad
1537       
1538        ControlInfo/W=UCALC popup0
1539        popStr = S_Value
1540        diam=str2num(popStr)            //in inches
1541        rad = diam/2*25.4                       //radius in mm
1542       
1543        flux = 662*rad*rad-39.9*rad*rad*rad+0.70*rad*rad*rad*rad
1544       
1545        return(flux)
1546End
1547
1548// based on the angle ranges above (with non-zero count times)
1549// save fake data points into a fake BT5 data file
1550//
1551Function U_SaveButtonProc(ba) : ButtonControl
1552        STRUCT WMButtonAction &ba
1553
1554       
1555        switch( ba.eventCode )
1556                case 2: // mouse up
1557                        // click code here
1558                       
1559                        Variable num,ii,baseNumber=101,firstSet=0
1560                        String pathStr="root:Packages:NIST:USANS:Globals:U_Sim:gCtTime"
1561                        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
1562                        String baseName="SIMUL"
1563                       
1564                        num = 7                 //# of angular ranges
1565                       
1566                        // only try to plot ranges with non-zero count times
1567                        firstSet=0
1568                        for(ii=1;ii<=num;ii+=1)
1569                                NVAR dum = $(pathStr+num2str(ii))
1570                                if(dum>0)                                               
1571                                        firstSet += 1
1572                                        if(firstSet==1) //first time, ask for base name
1573                                                Prompt baseName,"Enter a base name for the files"
1574                                                Prompt baseNumber,"Enter the starting index"
1575                                                DoPrompt "Enter Information for Save",baseName,baseNumber
1576                                               
1577                                                if(V_Flag==1)           //user canceled
1578                                                        return(1)
1579                                                endif
1580                                        endif
1581                                               
1582                                        SaveFakeUSANS(baseName,baseNumber-1,ii)
1583                                       
1584                                endif
1585                        endfor
1586                                                                       
1587                        break
1588        endswitch
1589
1590        return 0
1591End
1592
1593
1594//
1595// duplicates the BT5 file format
1596//
1597Function SaveFakeUSANS(nameStr,num,set)
1598        String nameStr
1599        Variable num,set
1600               
1601        String folder = "root:Packages:NIST:USANS:SIM:"
1602        String pathStr="root:Packages:NIST:USANS:Globals:U_Sim:"
1603        String termStr="\r\n"           //VAX uses only <CR> as terminator, but only CRLF seems to FTP correctly to VAX
1604       
1605//      WAVE DetCts = $(folder+"DetCts")                //these are only dummy values
1606        WAVE DetCts = root:Sim_USANS:Sim_USANS_i
1607        WAVE angle = $(folder+"Angle")
1608        WAVE SetNumber = $(folder+"SetNumber")
1609        WAVE countingTime = $(folder+"countingTime")
1610       
1611        NVAR ang1 = $(pathStr+"gAngLow"+num2str(set))
1612        NVAR ang2 = $(pathStr+"gAngHigh"+num2str(set))
1613        NVAR incr = $(pathStr+"gIncr"+num2str(set))
1614       
1615        Variable refNum,ii,wavePts,numPts,first,last,ctTime,monCt,transDet
1616        String str,fileStr,dateStr
1617       
1618        wavePts = numpnts(angle)
1619        for(ii=0;ii<wavePts;ii+=1)
1620                if(setNumber[ii] == set)
1621                        first = ii
1622                        break
1623                endif
1624        endfor
1625               
1626        for(ii=wavePts-1;ii>=0;ii-=1)
1627                if(setNumber[ii] == set)
1628                        last = ii
1629                        break
1630                endif
1631        endfor
1632       
1633//      Print "First, last = ",first,last
1634       
1635        // set up some of the strings needed
1636        fileStr=nameStr+num2str(num+set)+".bt5"
1637        dateStr = date()// + " "+Secs2Time(DateTime,2)
1638        ctTime = countingTime[first]
1639        numPts = last-first+1
1640       
1641        MonCt = ctTime*GetUSANSBeamIntensity()
1642       
1643        transDet = 999          //bogus and constant value
1644               
1645        //actually open the file
1646        Open refNum as fileStr
1647       
1648        sprintf str,"'%s' '%s' 'I'        %d    1  'TIME'   %d  'RAW'",fileStr,dateStr,ctTime,numpts
1649        fprintf refnum,"%s"+termStr,str
1650        fprintf refnum,"%s"+termStr,"  Filename         Date            Scan       Mon    Prf  Base   #pts  Type"
1651       
1652        fprintf refnum,"Simulated USANS data"+termStr
1653
1654        fprintf refnum,"%s"+termStr,"   0    0    0    0   0  0  0     0.0000     0.00000 0.00000   0.00000    2"
1655        fprintf refnum,"%s"+termStr," Collimation      Mosaic    Wavelength   T-Start   Incr.   H-field #Det    "
1656        fprintf refnum,"%s"+termStr,"  1       0.00000    0.00000    0.00000"
1657        fprintf refnum,"  2       %9.5f    %9.5f    %9.5f"+termStr,ang1,incr,ang2
1658        fprintf refnum,"%s"+termStr,"  3      10.00000    0.00000   10.00000"
1659        fprintf refnum,"%s"+termStr,"  4      10.00000    0.00000   10.00000"
1660        fprintf refnum,"%s"+termStr,"  5       0.00000    0.00000    0.00000"
1661        fprintf refnum,"%s"+termStr,"  6       0.00000    0.00000    0.00000"
1662        fprintf refnum,"%s"+termStr," Mot:    Start       Step      End"
1663        fprintf refnum,"%s"+termStr,"     A2       MIN      MONITOR      COUNTS      EXTRA  "
1664
1665        //loop over the waves, picking out the desired set
1666        //write 2 lines each time
1667        for(ii=first;ii<=last;ii+=1)
1668                sprintf str,"      %6.3f    %6.2f    %d     %d     %d",angle[ii],ctTime/60,MonCt,DetCts[ii]*ctTime,transDet
1669                fprintf refnum,"%s"+termStr,str
1670
1671                sprintf str,"%d,%d,0,%d,0,0,0,0",MonCt,DetCts[ii]*ctTime,transDet
1672                fprintf refnum,"%s"+termStr,str
1673        endfor
1674       
1675        Close refnum
1676
1677        return(0)
1678end
1679
1680
1681// print out the USANS configuration in some reasonable format
1682Function/S USANSConfigurationText()
1683
1684        String str="",temp
1685
1686        SetDataFolder root:Packages:NIST:USANS:Globals:U_Sim
1687       
1688        // results, setup values
1689        SVAR gTotTimeStr=gTotTimeStr
1690       
1691        Variable ii,num
1692        String pathStr="root:Packages:NIST:USANS:Globals:U_Sim:"
1693        num=7
1694       
1695        str += "USANS Instrument Configuration:\r\r"
1696        str += "Theta Min  Theta Max   Increment   # Points   Count Time\r"
1697       
1698       
1699        for(ii=1;ii<=num;ii+=1)
1700                NVAR ctTime = $(pathStr+"gCtTime"+num2str(ii))
1701                if(ctTime>0)
1702                        NVAR angLow = $(pathStr+"gAngLow"+num2str(ii))
1703                        NVAR angHigh = $(pathStr+"gAngHigh"+num2str(ii))
1704                        NVAR incr = $(pathStr+"gIncr"+num2str(ii))
1705                        NVAR numPts = $(pathStr+"gNumPts"+num2str(ii))
1706                       
1707                        sprintf temp,"%9.3f  %9.3f  %9.3f  %9d  %9d\r",angLow,angHigh,incr,numPts,ctTime
1708                        str += temp
1709                endif
1710        endfor
1711       
1712       
1713        sprintf temp,"\r\rTotal Counting Time (HR:MIN) = %s\r",gTotTimeStr
1714        str += temp
1715
1716       
1717   setDataFolder root:
1718   return str                   
1719End
1720
1721Function DisplayUCALCText()
1722
1723        if(WinType("USANS_Configuration")==0)
1724                NewNotebook/F=0/K=1/N=USANS_Configuration /W=(480,44,880,369)
1725        endif
1726        //replace the text
1727        Notebook USANS_Configuration selection={startOfFile, endOfFile}
1728        Notebook USANS_Configuration font="Monaco",fSize=10,text=USANSConfigurationText()
1729        return(0)
1730end
1731
1732
1733//
1734Function U_ConfigTextProc(ba) : ButtonControl
1735        STRUCT WMButtonAction &ba
1736
1737       
1738        switch( ba.eventCode )
1739                case 2: // mouse up
1740                        // click code here
1741                       
1742                                DisplayUCALCText()                                     
1743                        break
1744        endswitch
1745
1746        return 0
1747End
1748
1749// this will save a graphic of the whole panel that then needs to be opened and printed
1750// must be a PNG @ screen resolution
1751//
1752Function U_SavePanelProc(ba) : ButtonControl
1753        STRUCT WMButtonAction &ba
1754
1755       
1756        switch( ba.eventCode )
1757                case 2: // mouse up
1758                        // click code here
1759                       
1760                        SavePICT/P=home/E=-5/B=72/SNAP=1       
1761                       
1762                        // can I reload and print?
1763                        // how will the users know where this went and what to do with it?
1764                        //                     
1765                        break
1766        endswitch
1767
1768        return 0
1769End
Note: See TracBrowser for help on using the repository browser.