source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/USANS/Main_USANS.ipf @ 1147

Last change on this file since 1147 was 1147, checked in by srkline, 4 years ago

bug fix for Igor 8 - removing the setIgorOption forceCOLORONCOLOR for windows graphics. No longer needed in Igor 8, and generates an error. removed this line in the initialization step in all packages.

bug fix for the defining of a mask for sector averaging in VSANS. beam center in [cm] was incorrectly converted to [pixels]. No other calculations were affected.

  • Property eol-style set to native
File size: 42.4 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma Version=2.21
3#pragma IgorVersion=6.1
4
5//********************
6// 101101 Vers. 1
7//
8// Main initialization procedures for USANS reduction
9// initializes globals and oflders
10// draws the main panel for user interaction
11// action procedures for the USANS_Panel
12//
13//
14//
15// 09 NOV 04 vers 1.1
16// - updated dOmega and dQv
17// - write out dQv to 6-column data sets for analysis compatibility
18//
19//
20//********************
21
22Menu "USANS"
23        "USANS Reduction Panel",ShowUSANSPanel()
24        "Build USANS Notebook"
25        "Desmear USANS Data",Desmear()
26        "Add two raw BT5 files",SelectFilesToAdd()
27        "Add two data sets",ShowUSANSAddPanel()
28        "-"
29        Submenu "1D Utilities"
30                "Load and Plot Manager", Show_Plot_Manager()
31                "Convert to 6 Columns",Convert3ColTo6Col()
32                "ReWrite Experimental Data",MakeDMPanel()               //,ReWrite1DData()      // SRK SEP10
33                "1D Arithmetic Panel",MakeDAPanel()
34                "ReBin 1D Data",OpenRebin()
35        end
36        "-"
37//      "Load USANS Data",A_LoadOneDData()
38//      "-"
39        "USANS Simulator",Show_UCALC()
40        "-"
41        "NCNR Preferences",Show_Preferences_Panel()
42        "Feedback or Bug Report",OpenTracTicketPage()
43        "Open Help Movie Page",OpenHelpMoviePage()
44        "Check for Updates",CheckForLatestVersion()
45End
46
47// Bring the USANS_Panel to the front
48// ALWAYS initializes the folders and variables
49// then draws the panel if necessary
50//
51Proc ShowUSANSPanel()
52
53        if(itemsinlist(WinList("USANS_Includes.ipf", ";","INCLUDE:6"),";") != 0)
54                //must be opening a v2.2 or earlier template
55                DoAlert 0,"This experiment was created with an old version (v2.2 or earlier) of the macros. I'll try to make this work, but please start new work with a current template"
56        endif
57       
58        //version number
59        Variable/G root:USANS_RED_VERSION=2.4                   //distribution as of March 2019
60       
61        Init_MainUSANS()
62        DoWindow/F USANS_Panel
63        if(V_Flag==0)
64                USANS_Panel()
65        Endif
66       
67        DoIgorMenu "Control" "Retrieve All Windows"
68
69End
70
71// initializes the folders and globals for use with the USANS_Panel
72// waves for the listboxes must exist before the panel is drawn
73// "dummy" values for the COR_Graph are set here
74// instrumental constants are set here as well
75//
76Proc Init_MainUSANS()
77        NewDataFolder/O root:Packages
78        NewDataFolder/O root:Packages:NIST
79        NewDataFolder/O root:Packages:NIST:USANS
80        NewDataFolder/O root:Packages:NIST:USANS:Globals
81        NewDataFolder/O/S root:Packages:NIST:USANS:Globals:MainPanel
82       
83       
84        if(cmpstr("Macintosh",IgorInfo(2)) == 0)
85                String/G root:Packages:NIST:gAngstStr = num2char(-127)
86//              Variable/G root:myGlobals:gIsMac = 1
87        else
88                //either Windows or Windows NT
89                String/G root:Packages:NIST:gAngstStr = num2char(-59)
90//              Variable/G root:myGlobals:gIsMac = 0
91                //SetIgorOption to keep some PC's (graphics cards?) from smoothing the 2D image
92                // SRK APRIL 2019 - removed this, does not exist in Igor 8 on WIN, and cause an error.
93//              Execute "SetIgorOption WinDraw,forceCOLORONCOLOR=1"
94        endif
95       
96        String/G root:Packages:NIST:USANS:Globals:gUSANSFolder  = "root:Packages:NIST:USANS"
97        String USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
98        //NB This is also hardcoded a bit further down - search for "WHY WHY WHY" AJJ Sept 08
99       
100
101        Make/O/T/N=1 fileWave,samWave,empWave,curWave //Added curWave Sept 06 A. Jackson
102        fileWave=""
103        samWave=""
104        empWave=""
105        curWave="" //Added Sept 06 A. Jackson
106        //Wave for handling Current Data AJJ Sept 06
107        Make/O/N=1 SAMisCurrent,EMPisCurrent
108        SAMisCurrent = 0
109        EMPisCurrent = 0
110        Make/O/T/N=5 statusWave=""
111        Make/O/B/U/N=1 selFileW
112        Make/O/B/U/N=1 cselFileW
113        //for the graph control bar
114        Variable/G gTransWide = 1
115        Variable/G gTransRock = 1
116        Variable/G gEmpCts = 0.76                       //default values as of 15 DEC 05 J. Barker
117        Variable/G gBkgCts = 0.62                       //default values as of 15 DEC 05 J. Barker
118        Variable/G gThick = 0.1
119        Variable/G gTypeCheck=1
120        Variable/G gTransRatio=1
121        //Text filter for data files AJJ Sept 06
122        String/G FilterStr
123        Variable/G gUseCurrentData = 0
124       
125        SetDataFolder root:
126       
127        NewDataFolder/O $(USANSFolder+":RAW")
128        NewDataFolder/O $(USANSFolder+":SAM")
129        NewDataFolder/O $(USANSFolder+":COR")
130        NewDataFolder/O $(USANSFolder+":EMP")
131        NewDataFolder/O $(USANSFolder+":BKG")
132        NewDataFolder/O $(USANSFolder+":SWAP")
133        NewDataFolder/O $(USANSFolder+":Graph")
134       
135        //dummy waves for bkg and emp levels
136        Make/O $(USANSFolder+":EMP:empLevel"),$(USANSFolder+":BKG:bkgLevel")
137        //WHY WHY WHY????? - because dependencies can only involve globals. No locals allowed, since the dependency
138        // must remain in existence after the function is finished
139        //Explicit dependency
140        root:Packages:NIST:USANS:EMP:empLevel := root:Packages:NIST:USANS:Globals:MainPanel:gEmpCts //dependency to connect to SetVariable in panel
141        root:Packages:NIST:USANS:BKG:bkgLevel := root:Packages:NIST:USANS:Globals:MainPanel:gBkgCts
142
143// initializes facility specific constants to define the instrument     
144        Init_USANS_Facility()   
145
146        //initializes preferences. this includes XML y/n, and SANS Reduction items.
147        // if they already exist, they won't be overwritten
148       
149        Execute "Initialize_Preferences()"     
150       
151
152End
153
154//draws the USANS_Panel, the main control panel for the macros
155//
156
157Window USANS_Panel()
158        PauseUpdate; Silent 1           // building window...
159        NewPanel /W=(600,44,1015,493)/K=1 as "USANS_Panel"
160        SetDrawLayer UserBack
161        SetDrawEnv fstyle= 1
162        DrawText 12,53,"Data Files"
163        SetDrawEnv fstyle= 1
164        DrawText 157,192,"Empty Scans"
165        SetDrawEnv fstyle= 1
166        DrawText 154,54,"Sample Scans"
167        DrawLine 6,337,398,337
168        DrawLine 5,33,393,33
169        SetDrawEnv fstyle= 1
170        DrawText 140,357,"Raw Data Header"
171        SetDrawEnv fstyle= 1
172//      DrawText 293,55,"Current Data"
173        ListBox fileLB,pos={5,55},size={110,230},proc=FileListBoxProc
174        ListBox fileLB,listWave=root:Packages:NIST:USANS:Globals:MainPanel:fileWave
175        ListBox fileLB,selWave=root:Packages:NIST:USANS:Globals:MainPanel:selFileW,mode= 4
176        ListBox samLB,pos={149,55},size={110,90},listWave=root:Packages:NIST:USANS:Globals:MainPanel:samWave
177        ListBox samLB,mode= 1,selRow= -1
178        Button ClearSamButton,pos={227,148},size={35,20},proc=ClearButtonProc,title="Clr"
179        Button ClearSamButton,help={"Clears the list of sample scans"}
180        Button ClearEmpButton,pos={227,286},size={35,20},proc=ClearButtonProc,title="Clr"
181        Button ClearEmpButton,help={"Clears the list of empty scans"}
182        Button RefreshButton,pos={9,310},size={104,20},proc=RefreshListButtonProc,title="Refresh"
183        Button RefreshButton,help={"Refreshes the list of raw ICP data files"}
184        Button DelSamButton,pos={183,148},size={35,20},proc=DelSamButtonProc,title="Del"
185        Button DelSamButton,help={"Deletes the selected file(s) from the list of sample scans"}
186        Button DelEmpButton,pos={183,286},size={35,20},proc=DelEmpButtonProc,title="Del"
187        Button DelEmpButton,help={"Deletes the selected file(s) from the list of empty scans"}
188        ListBox empLB,pos={151,194},size={110,90}
189        ListBox empLB,listWave=root:Packages:NIST:USANS:Globals:MainPanel:empWave,mode= 1,selRow= 0
190        Button toSamList,pos={120,55},size={25,90},proc=toSamListButtonProc,title="S\r->"
191        Button toSamList,help={"Adds the selected file(s) to the list of sample scans"}
192        Button toEmpList,pos={120,195},size={25,90},proc=toEmptyListButtonProc,title="E\r->"
193        Button toEmpList,help={"Adds the selected file(s) to the list of empty (cell) scans"}
194        ListBox StatusLB,pos={11,358},size={386,77}
195        ListBox StatusLB,listWave=root:Packages:NIST:USANS:Globals:MainPanel:statusWave
196        Button pickPathButton,pos={6,8},size={80,20},proc=PickBT5PathButton,title="DataPath..."
197        Button pickPathButton,help={"Select the data folder where the raw ICP data files are located"}
198        Button PlotSelectedSAMButton,pos={140,148},size={35,20},proc=PlotSelectedSAMButtonProc,title="Plot"
199        Button PlotSelectedSAMButton,help={"Plot the selected sample scattering files in the COR_Graph"}
200        Button PlotSelectedEMPButton,pos={140,286},size={35,20},proc=PlotSelectedEMPButtonProc,title="Plot"
201        Button PlotSelectedEMPButton,help={"Plot the selected empty cell scattering files in the COR_Graph"}
202        Button pickSavePathButton,pos={97,8},size={80,20},proc=PickSaveButtonProc,title="SavePath..."
203        Button pickSavePathButton,help={"Select the data folder where data is to be saved to disk"}
204        Button USANSHelpButton,pos={341,6},size={50,20},proc=USANSHelpButtonProc,title="Help"
205        Button USANSHelpButton,help={"Show the USANS reduction help file"}
206        Button RefreshCurrent,pos={298,310},size={95,20},proc=RefreshCurrentButtonProc,title="Refresh",disable=2
207        Button RefreshCurrent,help={"Updates data files on Charlotte and gets current data file name"}
208        Button AddCurToSAM,pos={264,55},size={25,90},proc=CurtoSamListButtonProc,title="S\r<-",disable=2
209        Button AddCurToSAM,help={"Adds the current data file to the list of sample scans"}
210        Button AddCurToEMP,pos={265,194},size={25,90},proc=CurtoEmptyListButtonProc,title="E\r<-",disable=2
211        Button AddCurToEMP,help={"Adds the current data file to the list of empty scans"}
212        ListBox CurFileBox,pos={295,55},size={100,230},proc=FileListBoxProc,disable=1
213        ListBox CurFileBox,listWave=root:Packages:NIST:USANS:Globals:MainPanel:curWave,mode=1
214        SetVariable FilterSetVar,pos={8,289},size={106,18},title="Filter",fSize=12
215        SetVariable FilterSetVar,value= root:Packages:NIST:USANS:Globals:MainPanel:FilterStr
216//      CheckBox UseCurrentData,pos={298,290},size={10,10},proc=UseCurrentDataProc,title="Enable Current Data"
217//      CheckBox UseCurrentData,value=0
218        Button USANSFeedback,pos={220,6},size={100,20},proc=OpenTracTicketPage,title="Feedback"
219       
220EndMacro
221
222
223
224//draws a simple graph of the monitor counts, transmission counts, and detector counts
225// plots the selected raw data file when "plot raw" is selected from the USANS_Panel
226//
227Proc GraphRawData()
228        PauseUpdate; Silent 1           // building window...
229        String fldrSav= GetDataFolder(1)
230        String USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
231        SetDataFolder $(USANSFolder+":RAW:")
232        //String textStr = StringForRawGraph()
233        //textStr=StringByKey("FILE",textStr,":",";")+" MONRATE:"+num2str(mean(MonCts)/NumberByKey("TIMEPT",textStr,":",";"))
234        Display /W=(600,525,1015,850)/L=left1/B=bottom1 /K=1 DetCts vs Angle as "Raw Data"
235        ModifyGraph margin(top)=50
236        //Display /W=(600,525,1015,850) /K=1 DetCts vs Angle as "Raw Data"
237        DoWindow/C RawDataWin
238        //AppendToGraph/L=left1 /B=bottom1 DetCts vs Angle
239        AppendToGraph/L=left2/B=bottom1 TransCts vs Angle
240        AppendToGraph/L=left3/B=bottom1 MonCts vs Angle
241        SetAxis/A/N=2 left1
242        SetAxis/A/N=2 left2
243        SetAxis/A/N=2 left3
244        ModifyGraph mode=4, marker=19
245        ModifyGraph rgb(TransCts)=(1,4,52428),rgb(MonCts)=(1,39321,19939)
246        ModifyGraph msize=1,grid=1,mirror=2,standoff=1,lblPos=50,tickUnit=1,notation=1,freePos={0.1,kwFraction}
247        ModifyGraph nticks(left2)=2
248        ModifyGraph nticks(left3)=2
249        ModifyGraph mirror(bottom1)=0
250        ModifyGraph axisEnab(left1)={0.1,0.5},gridEnab(left1)={0.1,1}
251        ModifyGraph axisEnab(left2)={0.57,0.77},gridEnab(left2)={0.1,1}
252        ModifyGraph axisEnab(left3)={0.8,1},gridEnab={0.1,1}
253        ModifyGraph axisEnab(bottom1)={0.1,1},gridEnab(bottom1)={0.1,1}
254        ErrorBars/T=0 DetCts Y,wave=(ErrDetCts,ErrDetCts)
255       
256        if(root:Packages:NIST:gRawUSANSisQvalues == 1)
257                TextBox/F=0/E=2/A=MB/Y=2/N=text1 "Q (1/A)"
258                ModifyGraph log(bottom1)=1
259        else
260                TextBox/F=0/E=2/A=MB/Y=2/N=text1 "Angle"
261        endif
262       
263       
264        TextBox/F=0/O=90/E=2/A=LC/X=2/N=text2 "Counts"
265        //TextBox/N=text1/A=RC/X=0.50/Y=-2 textStr
266        //Label bottom1 "Angle (degrees)"
267        Label left1 " "
268        Label left2 " "
269        Label left3 " "
270        TitleForRawGraph()
271        SetDataFolder fldrSav
272End
273
274// plots the selected EMP files onto the COR_Graph
275// Does the following:
276// - loads raw data
277// - normalizes counts to time and 1E6 monitor counts
278// sorts by angle
279// finds zero angle (and peak height)
280// converts to q-values
281// finds T wide
282// updates the graph
283//
284Function PlotSelectedEMPButtonProc(ctrlName) : ButtonControl
285        String ctrlName
286
287        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
288
289        //get selected files from listbox (everything)
290        //use the listBox wave directly
291        Wave/T listW=$(USANSFolder+":Globals:MainPanel:empWave")
292        //Wave for indication of current data set AJJ Sept 2006
293        Wave isCurrent = $(USANSFolder+":Globals:MainPanel:EMPisCurrent")
294        Variable ii,num=numpnts(listW)
295        String fname="",fpath="",curPathStr=""
296        PathInfo bt5PathName
297        fpath = S_Path
298        PathInfo bt5CurPathName
299        curPathStr = S_Path
300               
301        if(cmpstr("",listW[0])==0)
302                return(0)               //null item in 1st position, exit
303        Endif
304       
305        //load, normalize, and append
306        //loop over the number of items in the list
307        for(ii=0;ii<num;ii+=1)
308               
309                //Check to see if file is current data set AJJ Sept 06
310                if (isCurrent[ii] ==  1)
311                        fname = curPathStr + listw[ii]
312                else
313                        fname = fpath + listw[ii]
314                endif
315
316               
317                LoadBT5File(fname,"SWAP")       //overwrite what's in the SWAP folder
318                Convert2Countrate("SWAP",1)
319                if(ii==0)       //first time, overwrite
320                        NewDataWaves("SWAP","EMP")
321                else            //append to waves in "EMP"
322                        AppendDataWaves("SWAP","EMP")
323                endif
324        endfor
325        //sort after all loaded
326        DoAngleSort("EMP")
327       
328        //find the peak and convert to Q-values
329        Variable zeroAngle = FindZeroAngle("EMP")
330        if(zeroAngle == -9999)
331                DoAlert 0,"Couldn't find a peak - using zero as zero angle"
332                zeroAngle = 0
333        Endif
334        ConvertAngle2Qvals("EMP",zeroAngle)
335       
336        //find the Trans Cts for T_Wide
337        FindTWideCts("EMP")
338       
339        //copy the data to plot to the root:Graph directory, and give clear names
340        if(WaveExists($(USANSFolder+":EMP:Qvals")))
341                Duplicate/O $(USANSFolder+":EMP:Qvals"),$(USANSFolder+":Graph:Qvals_EMP")
342        Endif
343        Duplicate/O $(USANSFolder+":EMP:Angle"),$(USANSFolder+":Graph:Angle_EMP")
344        Duplicate/O $(USANSFolder+":EMP:DetCts"),$(USANSFolder+":Graph:DetCts_EMP")
345        Duplicate/O $(USANSFolder+":EMP:ErrDetCts"),$(USANSFolder+":Graph:ErrDetCts_EMP")
346       
347        //now plot the data (or just bring the graph to the front)
348        DoCORGraph()
349End
350
351// plots the selected SAM files onto the COR_Graph
352// Does the following:
353// - loads raw data
354// - normalizes counts to time and 1E6 monitor counts
355// sorts by angle
356// finds zero angle (and peak height)
357// converts to q-values
358// finds T wide
359// updates the graph
360//
361Function PlotSelectedSAMButtonProc(ctrlName) : ButtonControl
362        String ctrlName
363
364        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
365
366        //get selected files from listbox (everything)
367        //use the listBox wave directly
368        Wave/T listW=$(USANSFolder+":Globals:MainPanel:samWave")
369        //Wave for indication of current data set AJJ Sept 2006
370        Wave isCurrent = $(USANSFolder+":Globals:MainPanel:SAMisCurrent")
371        Variable ii,num=numpnts(listW)
372        String fname="",fpath="",curPathStr=""
373        PathInfo bt5PathName
374        fpath = S_Path
375        PathInfo bt5CurPathName
376        curPathStr = S_Path
377       
378//      print fpath
379       
380        if(cmpstr("",listW[0])==0)
381                return(0)               //null item in 1st position, exit
382        Endif
383       
384        //load, normalize, and append
385        //loop over the number of items in the list
386        for(ii=0;ii<num;ii+=1)
387                fname = fpath + listw[ii]
388               
389                //Check to see if file is current data set AJJ Sept 06
390                if (isCurrent[ii] ==  1)
391                        fname = curPathStr + listw[ii]
392                else
393                        fname = fpath + listw[ii]
394                endif
395                               
396                LoadBT5File(fname,"SWAP")       //overwrite what's in the SWAP folder
397                Convert2Countrate("SWAP",1)
398                if(ii==0)       //first time, overwrite
399                        NewDataWaves("SWAP","SAM")
400                else            //append to waves in "SAM"
401                        AppendDataWaves("SWAP","SAM")
402                endif
403        endfor
404        //sort after all loaded
405        DoAngleSort("SAM")
406       
407        //find the peak and convert to Q-values
408        // MAR 2019
409        // for new NICE raw data collected in terms of q-values rather than angle, all of these function still
410        // work correctly, even though they are named "angle". The conversion is simply set to == 1 so that
411        // q == q
412        Variable zeroAngle = FindZeroAngle("SAM")
413        if(zeroAngle == -9999)
414                DoAlert 0,"Couldn't find a peak - using zero as zero angle"
415                zeroAngle = 0
416        Endif
417        ConvertAngle2Qvals("SAM",zeroAngle)
418        //find the Trans Cts for T_Wide
419        FindTWideCts("SAM")
420        //
421        //copy the data to plot to the root:Graph directory, and give clear names
422        if(WaveExists($(USANSFolder+":SAM:Qvals")))
423                Duplicate/O $(USANSFolder+":SAM:Qvals"),$(USANSFolder+":Graph:Qvals_SAM")
424        Endif
425        Duplicate/O $(USANSFolder+":SAM:Angle"),$(USANSFolder+":Graph:Angle_SAM")
426        Duplicate/O $(USANSFolder+":SAM:DetCts"),$(USANSFolder+":Graph:DetCts_SAM")
427        Duplicate/O $(USANSFolder+":SAM:ErrDetCts"),$(USANSFolder+":Graph:ErrDetCts_SAM")
428       
429        //now plot the data (or just bring the graph to the front)
430        DoCORGraph()
431End
432
433//sort the data in the "type"folder, based on angle
434//carry along all associated waves
435//
436Function DoAngleSort(type)
437        String type
438       
439        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
440       
441        Wave angle = $(USANSFolder+":"+Type+":Angle")
442        Wave detCts = $(USANSFolder+":"+Type+":DetCts")
443        Wave ErrdetCts = $(USANSFolder+":"+Type+":ErrDetCts")
444        Wave MonCts = $(USANSFolder+":"+Type+":MonCts")
445        Wave TransCts = $(USANSFolder+":"+Type+":TransCts")
446       
447        Sort Angle DetCts,ErrDetCts,MonCts,TransCts,Angle
448        return(0)
449End
450
451// converts to countrate per 1E6 monitor counts (default value)
452// by dividing by the counting time (reported in header in seconds per point)
453// and the globally defined monitor count
454//
455// works on data in "type" folder
456//
457// note that trans detector counts are NOT normalized to 1E6 mon cts (not necessary)
458//
459// 28 Mar 2019
460//  converted CtTime to be a wave, loaded in with the raw data
461// for old-style data, this is a constant value. for new q-value data, each point
462// can be counted for a different time.
463//
464Function Convert2Countrate(type,doNorm)
465        String type
466        Variable doNorm
467       
468        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
469       
470        String noteStr = note($(USANSFolder+":"+Type+":DetCts"))
471       
472        Variable ctTimeVal
473//      Variable ctTime
474//      ctTime = NumberByKey("TIMEPT",noteStr,":",";")
475
476//      print ctTime
477        //normalize by counting time
478        Wave detCts = $(USANSFolder+":"+Type+":DetCts")
479        Wave ErrdetCts = $(USANSFolder+":"+Type+":ErrDetCts")
480        Wave MonCts = $(USANSFolder+":"+Type+":MonCts")
481        Wave TransCts = $(USANSFolder+":"+Type+":TransCts")
482        Wave ctTime = $(USANSFolder+":"+Type+":ctTime")
483       
484        detCts /= ctTime
485        ErrDetCts /= ctTime
486        MonCts /= ctTime
487        TransCts /= ctTime
488       
489        //normalize to monitor countrate [=] counts/monitor
490        //trans countrate does not need to be normalized
491        if(doNorm)
492                NVAR defaultMCR=$(USANSFolder+":Globals:MainPanel:gDefaultMCR")
493                DetCts /= monCts/defaultMCR
494                ErrDetCts /= MonCts/defaultMCR
495        endif
496       
497        //adjust the note (now on basis of 1 second)
498        ctTimeVal = 1
499        noteStr = ReplaceNumberByKey("TIMEPT",noteStr,ctTimeVal,":",";")
500        Note/K detCts
501        Note detCts,noteStr
502       
503        return(0)
504End
505
506
507// copies data from one folder to another
508//
509// used for the first set, simply obliterate the old waves in the folder
510//
511Function NewDataWaves(fromType,toType)
512        String fromType,toType
513       
514        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
515
516        Duplicate/O $(USANSFolder+":"+fromType+":Angle"),$(USANSFolder+":"+toType+":Angle")
517        Duplicate/O $(USANSFolder+":"+fromType+":DetCts"),$(USANSFolder+":"+toType+":DetCts")
518        Duplicate/O $(USANSFolder+":"+fromType+":ErrDetCts"),$(USANSFolder+":"+toType+":ErrDetCts")
519        Duplicate/O $(USANSFolder+":"+fromType+":MonCts"),$(USANSFolder+":"+toType+":MonCts")
520        Duplicate/O $(USANSFolder+":"+fromType+":TransCts"),$(USANSFolder+":"+toType+":TransCts")
521       
522        //check for qvals wave, move if it's there
523        if(WaveExists($(USANSFolder+":"+fromType+":Qvals")))
524                Duplicate/O $(USANSFolder+":"+fromType+":Qvals"),$(USANSFolder+":"+toType+":Qvals")
525        Endif
526       
527End
528
529//to add additional data to a folder, need to concatenate the data in the waves
530//and need to update the wave note associated with "DetCts" to include the additional file
531//
532Function AppendDataWaves(fromType,toType)
533        String fromType,toType
534
535        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
536       
537        String fromNote="",toNote="",newNote="",fromfile="",toFile=""
538       
539       
540        ConcatenateData( (USANSFolder+":"+toType+":Angle"),(USANSFolder+":"+fromType+":Angle") ) //appends "from" at the end of "to"
541        ConcatenateData( (USANSFolder+":"+toType+":DetCts"),(USANSFolder+":"+fromType+":DetCts") )
542        ConcatenateData( (USANSFolder+":"+toType+":ErrDetCts"),(USANSFolder+":"+fromType+":ErrDetCts") )
543        ConcatenateData( (USANSFolder+":"+toType+":MonCts"),(USANSFolder+":"+fromType+":MonCts") )
544        ConcatenateData( (USANSFolder+":"+toType+":TransCts"),(USANSFolder+":"+fromType+":TransCts") )
545       
546
547        //adjust the wavenote, to account for the new dataset
548        fromNote = note($(USANSFolder+":"+fromType+":DetCts"))
549        fromFile = StringByKey("FILE",fromNote,":",";")
550        toNote = note($(USANSFolder+":"+toType+":DetCts"))
551        toFile = StringByKey("FILE",toNote,":",";")
552        toFile += "," + fromfile
553        toNote = ReplaceStringByKey("FILE",toNote,toFile,":",";")
554        Note/K $(USANSFolder+":"+toType+":DetCts")
555        Note $(USANSFolder+":"+toType+":DetCts"),toNote
556       
557        Return(0)
558End
559
560// action procedure to select the raw data path where the bt5 files are located
561//
562Function PickBT5PathButton(PathButton) : ButtonControl
563        String PathButton
564       
565        //Print "DataPathButton Proc"
566        Variable err = PickBT5Path()            //=1 if error
567
568End
569
570//pick the data folder to save the data to - in general must be a local disk, not the ICP database....
571//
572Function PickSaveButtonProc(ctrlName) : ButtonControl
573        String ctrlName
574       
575        NewPath/O/M="SAVE data to the selected folder" savePathName
576        PathInfo/S savePathName
577        String dum = S_path
578        if (V_flag == 0)
579                return(1)
580        else
581                return(0)               //no error
582        endif
583End
584
585// Show the help file, don't necessarily keep it with the experiment (/K=1)
586Function USANSHelpButtonProc(ctrlName) : ButtonControl
587        String ctrlName
588       
589        DisplayHelpTopic/Z/K=1 "USANS Data Reduction"
590        if(V_flag !=0)
591                DoAlert 0,"The USANS Data Reduction Help file could not be found"
592        endif
593        return(0)
594End
595
596//prompts user to choose the local folder that contains the BT5 Data
597//only one folder can be used, and its path is bt5PathName (and is a NAME, not a string)
598//this will overwrite the path selection
599//returns 1 if no path selected as error condition
600Function PickBT5Path()
601       
602        NVAR isChecked = root:Packages:NIST:USANS:Globals:MainPanel:gUseCurrentData
603       
604        //set the global string to the selected pathname
605        NewPath/O/M="pick the BT5 data folder" bt5PathName
606        PathInfo/S bt5PathName
607        String dum = S_path
608        if (V_flag == 0)
609                //Path does not exist
610                return(1)
611        else
612                if (isChecked == 1)
613                        NewPath/O/M="Select Current Data" bt5CurPathName, getCurrentPath(S_Path)
614                        return(0)               //no error
615                Endif
616        endif
617End
618
619//action procedure to load and plot a raw data file
620// loads the data based on selected file and bt5pathname
621// draws a new graph if necessary
622// otherwise just updates the data
623//
624Function PlotRawButtonProc(ctrlName) : ButtonControl
625        String ctrlName
626
627        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
628
629        String fname=""
630       
631        if (cmpstr(ctrlName,"FileLB") == 0)
632                //Print "PlotRaw Button"
633                //take the selection, and simply plot the counts vs. angle - don't save the data anyplace special
634                //get the selected wave
635                Wave/T fileWave=$(USANSFolder+":Globals:MainPanel:fileWave")
636                Wave sel=$(USANSFolder+":Globals:MainPanel:selFileW")
637                Variable ii=0,num=numpnts(sel),err
638
639               
640                PathInfo bt5PathName
641                fname = S_Path
642                do
643                        if(sel[ii] == 1)
644                                fname+=filewave[ii]
645                                break
646                        endif
647                        ii+=1
648                while(ii<num)
649        elseif (cmpstr(ctrlName,"CurFileBox" )== 0)
650                Wave/T fileWave=$(USANSFolder+":Globals:MainPanel:curWave")
651                PathInfo bt5CurPathName
652                fname = S_Path
653                fname+=filewave[0]
654        endif
655        //Print fname
656        if(strlen(fileWave[ii]) > 0 && ii < numpnts(fileWave))          //make sure that this is not a click in blank space
657                err = LoadBT5File(fname,"RAW")
658                if(err)
659                        return(err)
660                endif
661                //if the "Raw Data" Graph exists, do nothing - else draw it
662                //DoWindow/F RawDataWin
663                if(WinType("RawDataWin")!=1)
664                        Execute "GraphRawData()"
665                else
666                        //just update the textbox
667                        //String textStr=StringForRawGraph()
668                        //TextBox/W=RawDataWin/C/N=text1/A=RC/X=0.50/Y=-2 textStr
669                        //TextBox/W=RawDataWin/C/E=2/A=MT/X=0/Y=0/N=text0 textStr
670                        TitleForRawGraph()
671                Endif
672        endif
673        //bring the panel back to the front
674        DoWindow/F USANS_Panel
675End
676
677// action procedure for the raw data listbox
678// responds to selection or (shift)selection events
679// by acting if the "status" button was pressed
680//(note that the status button is obsolete and not drawn on the USANS_Panel, but
681// I kept the nomenclature)
682//
683Function FileListBoxProc(ctrlName,row,col,event)
684        String ctrlName
685        Variable row,col,event
686       
687        if (cmpstr(ctrlName, "fileLB") == 0)
688                //event == 4 is a selection
689                //event == 5 is a selection + shift key
690                if( (event==4) || (event==5) )
691                        StatusButtonProc(ctrlName)              //act as if status button was pressed
692                        PlotRawButtonProc(ctrlName)                                     //automatically plots the raw data
693                Endif
694                return(0)
695        elseif (cmpstr(ctrlName,"CurFileBox") == 0)
696                //print "Selected current data"
697                if  (event == 4)
698                        StatusButtonProc(ctrlName)
699                        PlotRawButtonProc(ctrlName)
700                endif
701                return(0)
702        endif
703        return(1)
704End
705
706// displays the status of the selected file in the raw data file list box
707// - spits the information out to a second listbox
708// - called automatically as an action when there is a selection in the file listbox
709// not used as a button procedure anymore
710//
711Function StatusButtonProc(ctrlName)
712        String ctrlName
713
714        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
715
716        String fname=""
717
718
719        if(cmpstr(ctrlName,"fileLB")==0)
720                //Print "Status Button"
721                //display the (first) selected wave
722                Wave/T fileWave=$(USANSFolder+":Globals:MainPanel:fileWave")
723                Wave sel=$(USANSFolder+":Globals:MainPanel:selFileW")
724                Variable ii=0,num=numpnts(sel)
725               
726                PathInfo bt5PathName
727                fname = S_Path
728                do
729                        if(sel[ii] == 1)
730                                fname+=filewave[ii]
731                                break
732                        endif
733                        ii+=1
734                while(ii<num)
735        elseif(cmpstr(ctrlName,"CurFileBox")==0)
736                Wave/T fileWave=$(USANSFolder+":Globals:MainPanel:curWave")
737                PathInfo bt5CurPathName
738                fname = S_Path
739                fname+=filewave[0]
740        endif           
741        //Print fname
742        if(strlen(fileWave[ii]) > 0 && ii < numpnts(fileWave))          //make sure that this is not a click in blank space
743                ReadBT5Header(fname)
744        endif
745End
746
747// copies the selected files from the raw file list box to the sam file listbox
748//
749// makes sure that any null items are removed from the wave attached to the listbox
750//
751Function toSamListButtonProc(ctrlName) : ButtonControl
752        String ctrlName
753
754        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
755
756        //Print "toSamList button"
757        Wave/T fileWave=$(USANSFolder+":Globals:MainPanel:fileWave")
758        Wave/T samWave=$(USANSFolder+":Globals:MainPanel:samWave")
759        Wave sel=$(USANSFolder+":Globals:MainPanel:selFileW")
760        //Wave to indicate Current status
761        Wave isCurrent = $(USANSFolder+":Globals:MainPanel:SAMisCurrent")
762
763       
764        Variable num=numpnts(sel),ii=0
765        variable lastPt=numpnts(samWave)
766        do
767                if(sel[ii] == 1)
768                        InsertPoints lastPt,1, samWave
769                        samWave[lastPt]=filewave[ii]
770                        InsertPoints lastPt, 1, isCurrent
771                        isCurrent[lastPt] = 0
772                        lastPt +=1
773                endif
774                ii+=1
775        while(ii<num)
776       
777        //clean out any (null) elements
778        num=numpnts(samwave)
779        for(ii=0;ii<num;ii+=1)
780                if(cmpstr(samWave[ii],"") ==0)
781                        DeletePoints ii,1,samWave
782                        DeletePoints ii,1,isCurrent
783                        num -= 1
784                Endif
785        Endfor
786       
787        return(0)
788End
789
790// copies the selected files from the raw file list box to the sam file listbox
791//
792// makes sure that any null items are removed from the wave attached to the listbox
793//
794Function toEmptyListButtonProc(ctrlName) : ButtonControl
795        String ctrlName
796
797        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
798
799        //Print "toEmptyList button"
800        Wave/T fileWave=$(USANSFolder+":Globals:MainPanel:fileWave")
801        Wave/T empWave=$(USANSFolder+":Globals:MainPanel:empWave")
802        Wave sel=$(USANSFolder+":Globals:MainPanel:selFileW")
803        //Wave to indicate Current status
804        Wave isCurrent = $(USANSFolder+":Globals:MainPanel:EMPisCurrent")
805
806
807       
808        Variable num=numpnts(sel),ii=0
809        variable lastPt=numpnts(empWave)
810        do
811                if(sel[ii] == 1)
812                        InsertPoints lastPt,1, empWave
813                        empWave[lastPt]=filewave[ii]
814                        InsertPoints lastPt, 1, isCurrent
815                        isCurrent[lastPt] = 0
816                        lastPt +=1
817                endif
818                ii+=1
819        while(ii<num)
820       
821        //clean out any (null) elements
822        num=numpnts(empwave)
823        for(ii=0;ii<num;ii+=1)
824                if(cmpstr(empWave[ii],"") ==0)
825                        DeletePoints ii,1,empWave
826                        DeletePoints ii,1,isCurrent
827                        num -= 1
828                Endif
829        Endfor
830       
831        return(0)
832End
833
834//deletes the selected file from the sam list
835// multiple selections are not allowed
836// the cor_graph is not updated
837//
838Function DelSamButtonProc(ctrlName) : ButtonControl
839        String ctrlName
840
841        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
842
843        ControlInfo SamLB
844        Variable selRow=V_Value
845        Wave lw=$(S_DataFolder + S_Value)
846        DeletePoints selRow,1,lw       
847        //Clear out current flag AJJ Sept O6
848        Wave isCurrent = $(USANSFolder+":Globals:MainPanel:SAMisCurrent")
849        DeletePoints selRow, 1, isCurrent       
850End
851
852//deletes the selected file from the emp list
853// multiple selections are not allowed
854// the cor_graph is not updated
855//
856Function DelEmpButtonProc(ctrlName) : ButtonControl
857        String ctrlName
858
859        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
860
861        ControlInfo EmpLB
862        Variable selRow=V_Value
863        Wave lw=$(S_DataFolder + S_Value)
864        DeletePoints selRow,1,lw
865        //Clear out current flag AJJ Sept O6
866        Wave isCurrent = $(USANSFolder+":Globals:MainPanel:EMPisCurrent")
867        DeletePoints selRow, 1, isCurrent       
868End
869
870//refreshes the file list presented in the raw data file listbox
871//bt5PathName is hard-wired in, will prompt if none exists
872// reads directly from disk
873//
874// EXCLUDES all files that do not match "*.bt5*" (not case sensitive) (*.ext*)
875//
876// sorts the list to alphabetical order
877//
878// modified to use the correct folder, switching on the control name (AddPanel added)
879//
880Function RefreshListButtonProc(ctrlName) : ButtonControl
881        String ctrlName
882       
883        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
884        SVAR ext = root:Packages:NIST:USANS:Globals:MainPanel:gUExt
885
886        String folderStr=""
887        if(cmpstr(ctrlName,"RefreshButton")==0)
888                //from MainUSANS panel
889                folderStr = "MainPanel"
890        else
891                folderStr = "AddPanel"
892        endif
893       
894        SVAR FilterStr = $(USANSFolder+":Globals:"+folderStr+":FilterStr")
895        print FilterStr
896        String filter
897       
898        //check for path and force user to pick a path
899        do
900                PathInfo bt5PathName
901                if(V_Flag)
902                        break
903                Endif
904                PickBT5Path()
905        while(V_Flag==0)
906       
907        //Get the filter and determine the match string required AJJ Sept06
908        if (stringmatch(FilterStr,"!"))
909                filter = FilterStr+"*"+ext+"*"
910        else
911                filter = "*"+ext+"*"
912        endif
913       
914        //get all the files, then trim the list
915        String list=IndexedFile(bt5PathName,-1,"????")
916        String newList="",item=""
917        Variable num=ItemsInList(list,";"),ii
918        for(ii=0;ii<num;ii+=1)
919                item = StringFromList(ii, list  ,";")
920               
921                if( stringmatch(item,filter) )          //ONLY keep files that match the filter + *.bt5 AJJ Sept 06
922                        newlist += item + ";"
923                endif
924                //print "ii=",ii
925        endfor
926        newList = SortList(newList,";",0)       //get them in order
927        num=ItemsInList(newlist,";")
928        Wave/T fileWave = $(USANSFolder+":Globals:"+folderStr+":fileWave")
929        Wave selFileW = $(USANSFolder+":Globals:"+folderStr+":selFileW")
930        Redimension/N=(num) fileWave
931        Redimension/N=(num) selFileW
932        fileWave=""
933        selFileW = 0
934        fileWave = StringFromList(p,newlist,";")        //  ! quick and easy assignment of the list
935        Sort filewave,filewave
936       
937End
938
939//clears either the sample or empty scan Lists...
940//Also clears the data folders and the COR_Graph
941//
942// very useful to make sure that the data is really what you think it is on the cor_graph
943//
944Function ClearButtonProc(ctrlName) : ButtonControl
945        String ctrlName
946
947        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
948               
949        SetDataFolder $(USANSFolder+":Globals:MainPanel")
950        strswitch(ctrlName)
951                case "ClearSamButton":
952                        Make/O/T/N=1 samWave
953                        samWave=""
954                        //Clear out current flags AJJ Sept O6
955                        Make/O/N=1 SAMisCurrent,EMPisCurrent
956                        SAMisCurrent = 0
957                        EMPisCurrent = 0
958                        //clear the graph, then the data folders as well
959                        CleanOutGraph("SAM")
960                        CleanOutFolder("SAM")
961                        CleanOutGraph("COR")
962                        CleanOutFolder("COR")
963                        break
964                case "ClearEmpButton":
965                        Make/O/T/N=1 empWave
966                        empWave=""
967                        //Clear out current flags AJJ Sept O6
968                        Make/O/N=1 SAMisCurrent,EMPisCurrent
969                        SAMisCurrent = 0
970                        EMPisCurrent = 0
971                        //clear the graph, then the data folders as well
972                        CleanOutGraph("EMP")
973                        CleanOutFolder("EMP")
974                        CleanOutGraph("COR")
975                        CleanOutFolder("COR")
976                        break
977        endswitch
978        // uncheck the cursors
979        UseCrsrCheckProc("",0)
980       
981        DoWindow/F USANS_Panel          //focus back to MainPanel
982        SetDataFolder root:
983End
984
985//reads header information, to display on main panel only
986// called from the action procedure of the raw data listbox
987//
988// puts the pertinent information in the "statusWave" that is associated
989//with the status listbox (which will automatically update)
990//
991Function ReadBT5Header(fname)
992        String fname
993       
994        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
995       
996        Variable err=0,refNum
997        Wave/T statusWave=$(USANSFolder+":Globals:MainPanel:statusWave")
998       
999        Open/R refNum as fname          //READ-ONLY.......if fname is "", a dialog will be presented
1000        if(refnum==0)
1001                return(1)               //user cancelled
1002        endif
1003        //read in the ASCII data line-by-line
1004        Variable numLinesLoaded = 0,firstchar
1005        Variable v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,ii,valuesRead
1006        String buffer ="",s1,s2,s3,s4,s5,s6,s7,s8,s9,s10
1007       
1008        //parse the first line
1009        FReadLine refnum,buffer
1010        sscanf buffer, "%s%s%s%s%s%s%g%g%s%g%s",s1,s2,s3,s4,s5,s6,v1,v2,s7,v3,s8
1011        statusWave[0] = "FILE: "+s1
1012        statusWave[1] = "DATE: "+s2+" "+s3+" "+s4+" "+s5
1013       
1014        //v1 is the time per point (sec)
1015        //v2  is the monitor prefactor. Let's multiply the time by the prefactor. AJJ 5 March 07
1016        statusWave[2] = "TIME/PT: "+num2istr(v1*v2)+" sec"
1017       
1018        //skip the next line
1019        FReadLine refnum,buffer
1020        //the next line is the title, use it all except the last character - causes formatting oddities in listBox
1021        FReadLine refnum,buffer
1022        statusWave[3] = "TITLE: "+ buffer[0,strlen(buffer)-2]
1023       
1024        //skip the next 3 lines
1025        For(ii=0;ii<3;ii+=1)
1026                FReadLine refnum,buffer
1027        EndFor
1028       
1029        //parse the angular range from the next line
1030        FReadLine refnum,buffer
1031        sscanf buffer,"%g%g%g%g",v1,v2,v3,v4
1032        statusWave[4] = "RANGE: "+num2str(v2)+" to "+num2str(v4)+" step "+num2str(v3)
1033       
1034        Close refNum            // Close the file, read-only, so don't need to move to EOF first
1035        return err                      // Zero signifies no error.     
1036End
1037
1038// given a string that is the wavenote of the just-loaded raw data file
1039// pick out the file and time list items (and values)
1040//and return a string that is to be used for the textbox in the graph of raw data
1041//
1042Function TitleForRawGraph()
1043
1044        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
1045
1046        WAVE detCts=$(USANSFolder+":RAW:detCts")
1047        WAVE monCts = $(USANSFolder+":RAW:monCts")
1048        String str=note(detCts)
1049       
1050        String retStr="\\JC"
1051        retStr += StringByKey("FILE",str,":",";")+"\r"
1052        retStr += "Count Time: "+StringByKey("TIMEPT",str,":",";")
1053        retStr += " \tMonitor Rate: "+num2str(mean(monCts)/NumberByKey("TIMEPT",str,":",";"))+"\r"
1054        retStr += "\\s(DetCts) DetCts \\s(TransCts) TransCts \\s(MonCts) MonCts"
1055       
1056        TextBox/W=RawDataWin/C/E=2/A=MT/X=5/Y=0/N=text0 retStr
1057End
1058
1059
1060//concatenates datasets (full names are passed in as a string)
1061//tacks w2 onto the end of w1 (so keep w1)
1062//
1063// copied from a wavemetrics procedure (concatenatewaves?)
1064Function ConcatenateData(w1, w2)
1065        String w1, w2
1066       
1067        Variable numPoints1, numPoints2
1068
1069        if (Exists(w1) == 0)
1070                Duplicate $w2, $w1
1071        else
1072                String wInfo=WaveInfo($w2, 0)
1073                numPoints1 = numpnts($w1)
1074                numPoints2 = numpnts($w2)
1075                Redimension/N=(numPoints1 + numPoints2) $w1
1076                Wave/D ww1=$w1
1077                Wave/D ww2=$w2
1078                ww1[numPoints1, ] = ww2[p-numPoints1]
1079        endif
1080End
1081
1082//takes the given inputs, subtracts EMP and BKG from the SAM data
1083//the user must supply the correct sample thickness, BKGLevel, and EMPLevel for extrapolation
1084Function DoCorrectData()
1085
1086        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
1087       
1088        //constants
1089//      NVAR  thetaH = root:Globals:MainPanel:gTheta_H                  //Darwin FWHM
1090//      NVAR  thetaV = root:Globals:MainPanel:gTheta_V                  //Vertical divergence
1091        NVAR dOmega =  $(USANSFolder+":Globals:MainPanel:gDomega")                      //Solid angle of detector
1092        NVAR defaultMCR = $(USANSFolder+":Globals:MainPanel:gDefaultMCR")
1093               
1094        //waves
1095        Wave iqSAM = $(USANSFolder+":SAM:DetCts")
1096        Wave errSAM = $(USANSFolder+":SAM:ErrDetCts")
1097        Wave qvalSAM = $(USANSFolder+":SAM:Qvals")
1098        Wave iqEMP = $(USANSFolder+":EMP:DetCts")
1099        Wave errEMP = $(USANSFolder+":EMP:ErrDetCts")
1100        Wave qvalEMP = $(USANSFolder+":EMP:Qvals")
1101        //BKG,EMP levels,trans,thick
1102        NVAR bkgLevel = $(USANSFolder+":Globals:MainPanel:gBkgCts")
1103        NVAR empLevel =  $(USANSFolder+":Globals:MainPanel:gEmpCts")
1104        NVAR Trock =  $(USANSFolder+":Globals:MainPanel:gTransRock")
1105        NVAR Twide =  $(USANSFolder+":Globals:MainPanel:gTransWide")
1106        NVAR thick =  $(USANSFolder+":Globals:MainPanel:gThick")
1107        //New waves in COR folder, same length as SAM data
1108        Duplicate/O iqSAM,$(USANSFolder+":COR:DetCts")
1109        Duplicate/O errSAM,$(USANSFolder+":COR:ErrDetCts")
1110        Duplicate/O qvalSAM,$(USANSFolder+":COR:Qvals")
1111        Wave iqCOR = $(USANSFolder+":COR:DetCts")
1112        Wave qvalCOR = $(USANSFolder+":COR:Qvals")
1113        Wave errCOR = $(USANSFolder+":COR:ErrDetCts")
1114       
1115        //correction done here
1116        //q-values of EMP must be interpolated to match SAM data
1117        //use the extrapolated value of EMP beyind its measured range
1118        Variable num=numpnts(iqSAM),ii,scale,tempI,temperr,maxq,wq
1119        maxq = qvalEMP[(numpnts(qvalEMP)-1)]            //maximum measure q-value for the empty
1120       
1121        for(ii=0;ii<num;ii+=1)
1122                wq = qvalSAM[ii]        //q-point of the sample
1123                if(wq<maxq)
1124                        tempI = interp(wq,qvalEMP,iqEMP)
1125                        temperr = interp(wq,qvalEMP,errEMP)
1126                else
1127                        tempI = empLevel
1128                        //temperr = sqrt(empLevel)
1129                        temperr = 0             //JGB 5/31/01
1130                endif
1131                iqCOR[ii] = iqSAM[ii] - Trock*tempI - (1-Trock)*bkglevel
1132                errCOR[ii] = sqrt(errSAM[ii]^2 + Trock^2*temperr^2)             //Trock^2; JGB 5/31/01
1133        endfor
1134       
1135        String str=note(iqEMP)
1136        Variable pkHtEMP=NumberByKey("PEAKVAL", str,":",";")
1137        //absolute scaling factor
1138        scale = 1/(Twide*thick*dOmega*pkHtEMP)
1139        iqCOR *= scale
1140        errCOR *= scale
1141       
1142        //copy to Graph directory to plot
1143        Duplicate/O $(USANSFolder+":COR:Qvals"),$(USANSFolder+":Graph:Qvals_COR")
1144        Duplicate/O $(USANSFolder+":COR:DetCts"),$(USANSFolder+":Graph:DetCts_COR")
1145        Duplicate/O $(USANSFolder+":COR:ErrDetCts"),$(USANSFolder+":Graph:ErrDetCts_COR")
1146       
1147        //now plot the data (or just bring the graph to the front)
1148        DoCORGraph()
1149        return(0)
1150End
1151
1152//cleans out the specified data folder (type)
1153// and clears out the same-named data from the Graph folder
1154// kills what is not in use
1155//
1156Function CleanOutFolder(type)
1157        String type
1158
1159        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder       
1160       
1161        SetDataFolder $(USANSFolder+":"+type)
1162        Killwaves/Z DetCts,Qvals,ErrDetCts,Angle,MonCts,TransCts
1163       
1164        SetDataFolder $(USANSFolder+":Graph")
1165        KillWaves/Z $("DetCts_"+type),$("ErrDetCts_"+type),$("Qvals_"+type)
1166       
1167        SetDataFolder root:
1168End
1169
1170// removes the selected datatype from the COR_Graph
1171//                     
1172Function CleanOutGraph(type)
1173        String type
1174       
1175        DoWindow/F COR_Graph
1176        if(V_flag)
1177                RemoveFromGraph/W=COR_Graph/Z $("DetCts_"+type)
1178        endif
1179End
1180
1181//Edits by AJJ, September 2006
1182//Add functions to get data from Current Folder
1183
1184Function RefreshCurrentButtonProc(ctrlName) : ButtonControl
1185        String ctrlName
1186       
1187        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder       
1188        SVAR ext = root:Packages:NIST:USANS:Globals:MainPanel:gUExt
1189
1190        //Prod the web update page
1191        //This is a horrible kludge that doesn't really work as the
1192        //webscript does not update the current run as of Sept 26 2006
1193        //Will get Nick M to look into it
1194        BrowseURL/Z "http://www-i.ncnr.nist.gov/icpdata/recent.php?action=1&instr=bt5"
1195       
1196        Print "Waiting 20s for update..."
1197        Sleep/S 20
1198        Print "Waited 20s, new file should be there."
1199       
1200        //check for path and force user to pick a path
1201        //assume that if bt5PathName has been assigned, so has bt5CurPathName
1202        do
1203                PathInfo bt5PathName
1204                if(V_Flag)
1205                        break
1206                Endif
1207                PickBT5Path()
1208        while(V_Flag==0)
1209       
1210        PathInfo bt5CurPathName
1211        if(V_flag==0)
1212                NewPath/O/M="Select Current Data" bt5CurPathName, getCurrentPath(S_Path)
1213        endif
1214               
1215        //get all the files, then trim the list
1216        String list=IndexedFile(bt5CurPathName,-1,"????")
1217        String newList="",item=""
1218        Variable num=ItemsInList(list,";"),ii
1219        for(ii=0;ii<num;ii+=1)
1220                item = StringFromList(ii, list  ,";")
1221                if( stringmatch(item,"*"+ext+"*") )             //ONLY keep files with ".bt5" in the name (NOT case-sensitive)
1222                        newlist += item + ";"
1223                endif
1224                //print "ii=",ii
1225        endfor
1226        newList = SortList(newList,";",0)       //get them in order
1227        num=ItemsInList(newlist,";")
1228        Wave/T curWave = $(USANSFolder+":Globals:MainPanel:curWave")
1229        Redimension/N=(num) curWave
1230        curWave=""
1231        curWave = StringFromList(p,newlist,";") //  ! quick and easy assignment of the list
1232        Sort curwave,curwave
1233       
1234End
1235
1236//Reduced version of toSamListButtonProc for putting current data into Sample data
1237Function CurtoSamListButtonProc(ctrlName) : ButtonControl
1238        String ctrlName
1239
1240        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
1241
1242        Wave/T curWave=$(USANSFolder+":Globals:MainPanel:curWave")
1243        Wave/T samWave=$(USANSFolder+":Globals:MainPanel:samWave")
1244        Wave isCurrent = $(USANSFolder+":Globals:MainPanel:SAMisCurrent")
1245       
1246        Variable num, ii = 0
1247        variable lastPt=numpnts(samWave)
1248       
1249        InsertPoints lastPt,1, samWave
1250        samWave[lastPt]=curwave[ii]
1251        InsertPoints lastPt, 1, isCurrent
1252        isCurrent[lastPt] = 1
1253       
1254        //clean out any (null) elements
1255        num = numpnts(samwave)
1256        for(ii=0;ii<num;ii+=1)
1257                if(cmpstr(samWave[ii],"") ==0)
1258                        DeletePoints ii,1,samWave
1259                        DeletePoints ii,1,isCurrent
1260                        num -= 1
1261                Endif
1262        Endfor
1263       
1264        return(0)
1265End
1266
1267//Reduced version of toEmptyListButtonProc for putting current data into Empty data
1268Function CurtoEmptyListButtonProc(ctrlName) : ButtonControl
1269        String ctrlName
1270
1271        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
1272
1273        Wave/T curWave=$(USANSFolder+":Globals:MainPanel:curWave")
1274        Wave/T empWave=$(USANSFolder+":Globals:MainPanel:empWave")
1275        Wave isCurrent = $(USANSFolder+":Globals:MainPanel:EMPisCurrent")
1276               
1277        Variable num, ii=0
1278        variable lastPt=numpnts(empWave)
1279
1280        InsertPoints lastPt,1, empWave
1281        empWave[lastPt]=curwave[ii]
1282        InsertPoints lastPt, 1, isCurrent
1283        isCurrent[lastPt] = 1
1284
1285       
1286        //clean out any (null) elements
1287        num=numpnts(empwave)
1288        for(ii=0;ii<num;ii+=1)
1289                if(cmpstr(empWave[ii],"") ==0)
1290                        DeletePoints ii,1,empWave
1291                        DeletePoints ii,1,isCurrent
1292                        num -= 1
1293                Endif
1294        Endfor
1295       
1296       
1297        return(0)
1298End
1299
1300Function UseCurrentDataProc(ctrlName,checked) : CheckBoxControl
1301        String ctrlName
1302        Variable checked
1303
1304        SVAR USANSFolder = root:Packages:NIST:USANS:Globals:gUSANSFolder
1305       
1306        NVAR isChecked = $(USANSFolder+":Globals:MainPanel:gUseCurrentData")
1307       
1308        if (checked == 1)
1309                DoAlert 1, "Enabling Current Data requires access to the NCNR Network.\rDo you wish to continue?"
1310                if(V_flag == 1)
1311                        ModifyControl/Z CurFileBox, disable=0
1312                        ModifyControl/Z AddCurToSAM, disable=0
1313                        ModifyControl/Z AddCurToEMP, disable=0
1314                        ModifyControl/Z RefreshCurrent,disable=0
1315                        isChecked = 1
1316                endif
1317        else
1318                ModifyControl/Z CurFileBox, disable=1
1319                ModifyControl/Z AddCurToSAM, disable=2
1320                ModifyControl/Z AddCurToEMP, disable=2
1321                ModifyControl/Z RefreshCurrent,disable=2       
1322                isChecked = 0
1323        endif
1324
1325End
1326
1327//Function to get path for Current file
1328Function/S getCurrentPath(inPath)
1329        String inPath
1330       
1331        Variable pos=0,mpos = 0
1332        Variable i = 0,j = 0
1333        // The saga continues...
1334        // Originally used ParseFilePath - not in Igor 4
1335        // Switched to strsearch, but useful searching from end of string not in Igor 4
1336        // Now have ugly loops going through the string, but should work.
1337
1338        for( i = 0;i<4; i+=1)   
1339                do
1340                        pos = strsearch(inPath,":",j)
1341                        if (pos != -1)
1342                                mpos = pos
1343                        endif
1344                        j = pos+1
1345                while (pos !=-1)
1346               
1347                inPath = inPath[0,mpos -1]
1348                pos = 0
1349                mpos = 0
1350                j = 0
1351        endfor
1352       
1353        inPath = inPath + ":current:"
1354
1355        return inPath
1356       
1357End
Note: See TracBrowser for help on using the repository browser.