source: sans/SANSReduction/branches/kline_29MAR07/Put in User Procedures/SANS_Reduction_v5.00/Transmission.ipf @ 66

Last change on this file since 66 was 47, checked in by srkline, 16 years ago

bug fixes and performance tweaks

File size: 83.1 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma version=5.0
3#pragma IgorVersion=4.0
4
5//*************************
6// Vers. 1.2 092101
7//
8// - procedures for (easier) caluclation of transmissions along with
9//  simultaneous patching of the calculated transmission in designated
10//  raw sans files.
11// - transmission and empty beam files are linked to the dataset by their "Annn" suffix
12//   only (and their residence in the same data folder)
13// - unused (analysis) integer values are used to store these run numbers in the sample
14//   file headers so that transmissions can be automatically calculated, once the xy box is set
15//   in the empty beam file.
16//
17//***************************
18
19//main entry procedure for displaying the Trans input panel
20//also initializes the globals as needed
21//
22Proc CalcTrans()
23       
24        DoWindow/F Trans_Panel
25        If(V_flag == 0)
26                InitializeTransPanel()
27                //draw panel
28                Trans_Panel()
29                AutoPositionWindow/M=1/R=Main_Panel Trans_Panel
30        Endif
31End
32
33// The BuildFileTables routine will build two tables:  one containing the
34// data from the headers of transmission files in the folder and one
35// containing the header data from all other SANS data files.  They will
36// each have the first column empty for, in the case of transmission files,
37// the empty beam file and, in the case of scattering files, the scattering
38// run file name.
39Proc BuildFileTables()
40
41        Variable err
42        PathInfo catPathName
43        if(v_flag==0)
44                err = PickPath()//sets the local path to the data (catPathName)
45                if(err)
46                        Abort "no path to data was selected, no catalog can be made - use PickPath button"
47                Endif
48        Endif
49
50        DoWindow/F TransFileTable
51
52        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:T_Suffices"
53        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
54        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:T_Filenames"
55        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:T_Suffix"
56        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:T_Labels"
57        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:T_SDD"
58        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:T_Lambda"
59        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:T_Transmission"
60        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:T_Whole"
61        If(V_Flag==0)
62                BuildTransTableWindow()
63                ModifyTable width(:myGlobals:TransHeaderInfo:T_SDD)=40
64                ModifyTable width(:myGlobals:TransHeaderInfo:T_Lambda)=40
65                ModifyTable rgb(:myGlobals:TransHeaderInfo:T_Filenames)=(0,0,65535)
66                ModifyTable width(:myGlobals:TransHeaderInfo:T_Transmission)=60
67                ModifyTable width(:myGlobals:TransHeaderInfo:T_Whole)=60
68               
69                ModifyTable width(Point)=0
70                //ModifyTable width(:myGlobals:TransHeaderInfo:T_Transmission)=40
71        Endif
72       
73        DoWindow/F ScatterFileTable
74        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:S_Suffices"
75        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
76        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:S_Filenames"
77        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:S_Suffix"
78        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:S_Labels"
79        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:S_SDD"
80        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:S_Lambda"
81        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:S_Transmission"
82        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:S_Whole"
83        If(V_Flag==0)
84                BuildScatTableWindow()
85                ModifyTable width(:myGlobals:TransHeaderInfo:S_SDD)=40
86                ModifyTable width(:myGlobals:TransHeaderInfo:S_Lambda)=40
87                ModifyTable width(:myGlobals:TransHeaderInfo:S_Transmission)=60
88                ModifyTable rgb(:myGlobals:TransHeaderInfo:S_Filenames)=(0,0,65535)
89               
90                ModifyTable width(Point)=0
91        Endif
92
93        String temp=""
94
95        //get a list of all files in the folder, some will be junk version numbers that don't exist
96        String list,partialName,tempName
97        list = IndexedFile(catPathName,-1,"????")       //get all files in folder
98        Variable numitems,ii,ok
99
100        //remove version numbers from semicolon-delimited list
101        list =  RemoveVersNumsFromList(list)
102
103        numitems = ItemsInList(list,";")
104
105        //loop through all of the files in the list, reading CAT/SHORT information if the file is RAW SANS
106        //***version numbers have been removed***
107        String str,fullName
108        Variable lastPoint
109        ii=0
110
111        do
112                //get current item in the list
113                partialName = StringFromList(ii, list, ";")
114                //get a valid file based on this partialName and catPathName
115                tempName = FindValidFilename(partialName)
116                If(cmpstr(tempName,"")==0)              //a null string was returned
117                        //write to notebook that file was not found
118                        //if string is not a number, report the error
119                        if(str2num(partialName) == NaN)
120                                str = "this file was not found: "+partialName+"\r\r"
121                                //Notebook CatWin,font="Times",fsize=12,text=str
122                        Endif
123                else
124                        //prepend path to tempName for read routine
125                        PathInfo catPathName
126                        FullName = S_path + tempName
127                        //make sure the file is really a RAW data file
128                        ok = CheckIfRawData(fullName)
129                        if (ok)
130                                GetTransHeaderInfoToWave(fullName,tempName)
131                        Endif
132                Endif
133                ii+=1
134        while(ii<numitems)
135        //Now sort them all based on the suffix data
136        SortTransByDate()
137        //Now fill in the filenames of the ones that have already had EMP or TRANS
138        //files associated with them.
139        GetFilenamesFromSuffices()
140
141        //position the windows nicely
142        AutoPositionWindow/M=1/R=TransFileTable  ScatterFileTable
143        DoWindow/F Trans_Panel
144End
145
146//actually creates the table of transmission files
147// - the waves must exist
148//
149Function BuildTransTableWindow()
150        Wave/T T_EMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
151        Wave/T T_Filenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
152        Wave/T T_Suffix = $"root:myGlobals:TransHeaderInfo:T_Suffix"
153        Wave/T T_Labels = $"root:myGlobals:TransHeaderInfo:T_Labels"
154        Wave   T_SDD = $"root:myGlobals:TransHeaderInfo:T_SDD"
155        Wave   T_Lambda = $"root:myGlobals:TransHeaderInfo:T_Lambda"
156        Wave   T_Transmission = $"root:myGlobals:TransHeaderInfo:T_Transmission"
157        Wave   T_Whole= $"root:myGlobals:TransHeaderInfo:T_Whole"
158       
159        Edit/K=1/W=(30,30,500,250) T_EMP_Filenames, T_Filenames, T_Labels, T_SDD, T_Lambda, T_Transmission, T_Whole as "TransmissionFiles"
160        String name="TransFileTable"
161        DoWindow/C $name
162
163End
164
165//actually creates the table of scattering files
166// - the waves must exist
167//
168Function BuildScatTableWindow()
169        Wave/T S_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
170        Wave/T S_Filenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
171        Wave/T S_Suffix = $"root:myGlobals:TransHeaderInfo:S_Suffix"
172        Wave/T S_Labels = $"root:myGlobals:TransHeaderInfo:S_Labels"
173        Wave   S_SDD = $"root:myGlobals:TransHeaderInfo:S_SDD"
174        Wave   S_Lambda = $"root:myGlobals:TransHeaderInfo:S_Lambda"
175        Wave   S_Transmission = $"root:myGlobals:TransHeaderInfo:S_Transmission"
176       
177        Edit/K=1/W=(50,50,520,270) S_TRANS_Filenames, S_Filenames, S_Labels, S_SDD, S_Lambda, S_Transmission as "ScatteringFiles"
178        String name="ScatterFileTable"
179        DoWindow/C $name
180
181End
182
183//sorts each of the tables by date - does this by using the suffix "Annn"
184// which is automatically supplied by the VAX (and can't be changed)
185// - only problem may be "Z999" -> "A001" rollover
186//
187Function SortTransByDate()
188        Wave/T T_GEMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
189        Wave/T T_GSuffices = $"root:myGlobals:TransHeaderInfo:T_Suffices"
190        Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
191        Wave/T T_GSuffix = $"root:myGlobals:TransHeaderInfo:T_Suffix"
192        Wave/T T_GLabels = $"root:myGlobals:TransHeaderInfo:T_Labels"
193        Wave   T_GSDD = $"root:myGlobals:TransHeaderInfo:T_SDD"
194        Wave   T_GLambda = $"root:myGlobals:TransHeaderInfo:T_Lambda"
195        Wave   T_GTransmission = $"root:myGlobals:TransHeaderInfo:T_Transmission"
196        Wave   T_Whole= $"root:myGlobals:TransHeaderInfo:T_Whole"
197
198        Wave/T S_GTRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
199        Wave/T S_GSuffices = $"root:myGlobals:TransHeaderInfo:S_Suffices"
200        Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
201        Wave/T S_GSuffix = $"root:myGlobals:TransHeaderInfo:S_Suffix"
202        Wave/T S_GLabels = $"root:myGlobals:TransHeaderInfo:S_Labels"
203        Wave   S_GSDD = $"root:myGlobals:TransHeaderInfo:S_SDD"
204        Wave   S_GLambda = $"root:myGlobals:TransHeaderInfo:S_Lambda"
205        Wave   S_GTransmission = $"root:myGlobals:TransHeaderInfo:S_Transmission"
206        Wave   S_Whole= $"root:myGlobals:TransHeaderInfo:S_Whole"
207       
208        Sort T_GSuffix, T_GEMP_Filenames, T_GSuffix, T_GSuffices, T_GFilenames, T_GLabels, T_GSDD, T_GLambda, T_GTransmission, T_Whole
209        Sort S_GSuffix, S_GTRANS_Filenames, S_GSuffix, S_GSuffices, S_GFilenames, S_GLabels, S_GSDD, S_GLambda, S_GTransmission, S_Whole
210       
211End
212
213// sorts files alphabetically byt the sample label
214// - consistent and descriptive sample labels really pay off here
215//
216Function SortTransByLabel()
217        Wave/T T_GEMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
218        Wave/T T_GSuffices = $"root:myGlobals:TransHeaderInfo:T_Suffices"
219        Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
220        Wave/T T_GSuffix = $"root:myGlobals:TransHeaderInfo:T_Suffix"
221        Wave/T T_GLabels = $"root:myGlobals:TransHeaderInfo:T_Labels"
222        Wave   T_GSDD = $"root:myGlobals:TransHeaderInfo:T_SDD"
223        Wave   T_GLambda = $"root:myGlobals:TransHeaderInfo:T_Lambda"
224        Wave   T_GTransmission = $"root:myGlobals:TransHeaderInfo:T_Transmission"
225        Wave   T_Whole= $"root:myGlobals:TransHeaderInfo:T_Whole"
226       
227        Wave/T S_GTRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
228        Wave/T S_GSuffices = $"root:myGlobals:TransHeaderInfo:S_Suffices"
229        Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
230        Wave/T S_GSuffix = $"root:myGlobals:TransHeaderInfo:S_Suffix"
231        Wave/T S_GLabels = $"root:myGlobals:TransHeaderInfo:S_Labels"
232        Wave   S_GSDD = $"root:myGlobals:TransHeaderInfo:S_SDD"
233        Wave   S_GLambda = $"root:myGlobals:TransHeaderInfo:S_Lambda"
234        Wave   S_GTransmission = $"root:myGlobals:TransHeaderInfo:S_Transmission"
235        Wave   S_Whole= $"root:myGlobals:TransHeaderInfo:S_Whole"
236       
237        Sort T_GLabels, T_GEMP_Filenames, T_GSuffix, T_GSuffices, T_GFilenames, T_GLabels, T_GSDD, T_GLambda, T_GTransmission, T_Whole
238        Sort S_GLabels, S_GTRANS_Filenames, S_GSuffix, S_GSuffices, S_GFilenames, S_GLabels, S_GSDD, S_GLambda, S_GTransmission, S_Whole
239       
240End
241
242//reads the file and assigns header information to the proper table
243//
244//fname - is the full path:name of the datafile (used by Open)
245//sname - is the name only, used as the filename field
246//
247// takes care of all necessary open/close of file
248//
249Function GetTransHeaderInfoToWave(fname,sname)
250        String fname,sname
251       
252        Variable isTrans, xTol= -5
253
254        String textstr,temp,labelStr,date_time,suffix,assoc
255        Variable ctime,lambda,sdd,detcnt,cntrate,refNum,trans,thick,xcenter,ycenter,numatten
256        Variable lastPoint, beamstop, Whole
257
258        isTrans = CheckIfBeamstopOut(fname, xTol)
259        // decalre the correct set of waves
260        // S_Whole is only a place holder so the waves are of equal length
261        if (isTrans)
262                Wave/T GSuffices = $"root:myGlobals:TransHeaderInfo:T_Suffices"
263                Wave/T G_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
264                Wave/T GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
265                Wave/T GSuffix = $"root:myGlobals:TransHeaderInfo:T_Suffix"
266                Wave/T GLabels = $"root:myGlobals:TransHeaderInfo:T_Labels"
267                Wave GSDD = $"root:myGlobals:TransHeaderInfo:T_SDD"
268                Wave GLambda = $"root:myGlobals:TransHeaderInfo:T_Lambda"
269                Wave GTransmission = $"root:myGlobals:TransHeaderInfo:T_Transmission"
270                Wave GWhole = $"root:myGlobals:TransHeaderInfo:T_Whole"
271        Else
272                Wave/T GSuffices = $"root:myGlobals:TransHeaderInfo:S_Suffices"
273                Wave/T G_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
274                Wave/T GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
275                Wave/T GSuffix = $"root:myGlobals:TransHeaderInfo:S_Suffix"
276                Wave/T GLabels = $"root:myGlobals:TransHeaderInfo:S_Labels"
277                Wave GSDD = $"root:myGlobals:TransHeaderInfo:S_SDD"
278                Wave GLambda = $"root:myGlobals:TransHeaderInfo:S_Lambda"
279                Wave GTransmission = $"root:myGlobals:TransHeaderInfo:S_Transmission"
280                Wave GWhole = $"root:myGlobals:TransHeaderInfo:S_Whole"
281        Endif
282
283        lastPoint = numpnts(GLambda)
284       
285        Open/R refNum as fname
286       
287        InsertPoints lastPoint,1,G_TRANS_Filenames
288        G_TRANS_Filenames[lastPoint]=""
289
290        InsertPoints lastPoint,1,GFilenames
291        GFilenames[lastPoint]=sname
292       
293        //read the file suffix
294        FSetPos refNum,19
295        FReadLine/N=4 refNum,suffix
296        //print suffix
297        InsertPoints lastPoint,1,GSuffix
298        GSuffix[lastPoint]=suffix
299
300        //read any suffix that this file is associated with
301        //FSetPos refNum,91             //wrong position
302        FSetPos refNum,404
303        FReadLine/N=4 refNum,assoc
304        //print isTrans, assoc
305        InsertPoints lastPoint,1,GSuffices
306        GSuffices[lastPoint]=assoc
307           
308        // read the sample.label text field
309        FSetPos refNum,98               //will start reading at byte 99
310        FReadLine/N=60 refNum,labelStr
311        InsertPoints lastPoint,1,GLabels
312        GLabels[lastPoint]=labelStr
313
314        Close refNum
315           
316        //read the reals with GBLoadWave
317        String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
318        String strToExecute
319       
320        //Transmission
321        strToExecute = GBLoadStr + "/S=158/U=1" + "\"" + fname + "\""
322        Execute strToExecute
323        Wave w=$"tempGBWave0"
324        trans = w[0]
325        InsertPoints lastPoint,1,GTransmission
326        GTransmission[lastPoint]=trans
327       
328        //Whole Transmission
329        strToExecute = GBLoadStr + "/S=392/U=1" + "\"" + fname + "\""
330        Execute strToExecute
331        Wave w=$"tempGBWave0"
332        Whole = w[0]
333        InsertPoints lastPoint,1,GWhole
334        GWhole[lastPoint]=Whole
335           
336        //SDD
337        strToExecute = GBLoadStr + "/S=260/U=1" + "\"" + fname + "\""
338        Execute strToExecute
339        sdd = w[0]
340        InsertPoints lastPoint,1,GSDD
341        GSDD[lastPoint]=sdd
342           
343        //wavelength
344        strToExecute = GBLoadStr + "/S=292/U=1" + "\"" + fname + "\""
345        Execute strToExecute
346        lambda = w[0]
347        InsertPoints lastPoint,1,GLambda
348        GLambda[lastPoint]=lambda
349       
350//      InsertPoints lastPoint,1,GWhole
351//      GWhole[lastPoint]=1
352       
353        KillWaves/Z w
354
355End
356
357//fills in the file relations that have already been set
358//
359//
360Function GetFilenamesFromSuffices()
361        Wave/T T_EMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
362        Wave/T T_GSuffices = $"root:myGlobals:TransHeaderInfo:T_Suffices"
363        Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
364        Wave/T T_GSuffix = $"root:myGlobals:TransHeaderInfo:T_Suffix"
365       
366        Wave/T S_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
367        Wave/T S_GSuffices = $"root:myGlobals:TransHeaderInfo:S_Suffices"
368        Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
369        Wave/T S_GSuffix = $"root:myGlobals:TransHeaderInfo:S_Suffix"
370       
371        Variable num_s_files, num_t_files, ii, jj
372        num_t_files = numpnts(T_GFilenames)
373        num_s_files = numpnts(S_GFilenames)
374        //print num_t_files, num_s_files
375       
376        // Get transmission file relations
377        ii= 0
378        do
379                if (cmpstr(T_GSuffices[ii],"")!=0)
380                        jj=0
381                        do
382                                if (cmpstr(T_GSuffices[ii],T_GSuffix[jj])==0)
383                                        T_EMP_Filenames[ii]=T_GFilenames[jj]
384                                endif
385                        jj+=1
386                        while(jj<num_t_files)
387                endif
388                ii+=1
389        while(ii<num_t_files)
390 
391        // Get scattering file relations
392        ii= 0
393        do
394                if (cmpstr(S_GSuffices[ii],"")!=0)
395                        jj=0
396                        do
397                                if (cmpstr(S_GSuffices[ii],T_GSuffix[jj])==0)
398                                        S_TRANS_Filenames[ii]=T_GFilenames[jj]
399                                endif
400                        jj+=1
401                        while(jj<num_t_files)
402                endif
403                ii+=1
404        while(ii<num_s_files)
405End
406
407// gets the selected transmission filename, and puts its name as the
408//empty beam reference file
409// lists the xy box coordinates as read from the file header (zeros if not yet assigned)
410//
411Function PickEMPTransButton(ctrlName) : ButtonControl
412        String ctrlName
413       
414        Variable TransFileTableExists
415        TransFileTableExists = WinType("TransFileTable")
416        //??BUG?? V_flag returns 1 even if no selection?
417        //check manually for null selection
418        //Print "local v_flag = ",num2str(V_flag)
419        Wave/T LocFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
420        if (TransFileTableExists != 0)
421                String/G root:myGlobals:TransHeaderInfo:gEMP = ""
422                GetSelection table,TransFileTable,7
423                //Print S_Selection
424                if(strsearch(S_selection,"T_Filenames",0) == 0)
425                   //selection OK, add to list
426                        Duplicate/O/R=[V_startRow,V_endRow] LocFilenames, filenames
427                        Wave/T selectedFiles = $"filenames"
428
429                        SVAR temp = root:myGlobals:TransHeaderInfo:gEMP
430                        //
431                        temp = temp+selectedFiles[0]            //take just the first file
432                        UpdateBoxCoordinates()
433                Else
434                        DoWindow/F TransFileTable
435                        DoAlert 0,"Invalid selection from the Transmision file table. You must select a file from the T_Filenames column"
436                Endif
437        else
438                //no selection
439                DoAlert 0,"No file selected from Transmission file table or no Transmission file table available"
440        Endif
441End
442
443//initialize data folder and globals for the Trans panel as needed
444//since this operation is somewhat tangled with the Patch Panel, keep all of the
445//globals in the Patch subfolder (make sure both panels are simultaneously initialized)
446//do not create a separate Trans folder
447//
448Proc InitializeTransPanel()
449        //create the global variables needed to run the Trans Panel
450        //all are kept in root:myGlobals:TransHeaderInfo
451        If( ! (DataFolderExists("root:myGlobals:TransHeaderInfo"))  )
452                //create the data folder and the clobals for BOTH the Patch and Trans Panels
453                NewDataFolder/O root:myGlobals:TransHeaderInfo
454                CreateTransGlobals()
455        Endif
456End
457
458
459//
460Proc CreateTransGlobals()
461
462        PathInfo catPathName
463        If(V_flag==1)
464                String dum = S_path
465                String/G root:myGlobals:TransHeaderInfo:gCatPathStr = dum
466        else
467                String/G root:myGlobals:TransHeaderInfo:gCatPathStr = "no path selected"
468        endif
469
470        String/G root:myGlobals:TransHeaderInfo:gEMP = "no file selected"
471        String/G root:myGlobals:TransHeaderInfo:gBox = ""
472        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:T_Suffices"
473        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
474        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:T_Filenames"
475        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:T_Suffix"
476        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:T_Labels"
477        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:T_SDD"
478        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:T_Lambda"
479        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:T_Transmission"
480        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:T_Whole"
481       
482        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:S_Suffices"
483        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
484        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:S_Filenames"
485        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:S_Suffix"
486        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:S_Labels"
487        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:S_SDD"
488        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:S_Lambda"
489        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:S_Transmission"
490        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:S_Whole"
491End
492
493
494//function that writes out (to the header of a raw binary SANS file)
495//characters representing the empty beam file and the sample transmission file
496//the 4-character file suffix is written, rather than the run numbers
497//from this, the trans files can be located, and the
498//sample file contains all the information needed to calculate its transmission
499//
500//uses first 4 bytes of params.reserve (character field) starting at byte 404
501//(previous version (AM) used run.reserve field @ byte 91, which was not empty, and not at the start of the field!)
502//
503Function AssignSelTransFilesToData(startRow,endRow)
504        Variable startRow,endRow
505
506//      GetSelection table,ScatterFileTable,1
507       
508        Wave/T T_EMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
509        Wave/T T_GSuffices = $"root:myGlobals:TransHeaderInfo:T_Suffices"
510        Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
511        Wave/T T_GSuffix = $"root:myGlobals:TransHeaderInfo:T_Suffix"
512       
513        Wave/T S_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
514        Wave/T S_GSuffices = $"root:myGlobals:TransHeaderInfo:S_Suffices"
515        Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
516        Wave/T S_GSuffix = $"root:myGlobals:TransHeaderInfo:S_Suffix"
517       
518        Variable num_s_files, num_t_files, ii, jj
519        Variable refnum
520        num_t_files = numpnts(T_GFilenames)
521        String suffix = ""
522        PathInfo catPathName
523        String pathname = S_path
524        String filename
525
526        // Write suffix of empty beam file into transmission files
527        ii= 0
528        do
529                if (cmpstr(T_EMP_Filenames[ii],"")!=0)
530                        jj=0
531                        do
532                                if (cmpstr(T_EMP_Filenames[ii],T_GFilenames[jj])==0)
533                                        suffix=T_GSuffix[jj]
534                                        filename = pathname + T_GFilenames[ii]
535                                        Open/A/T="????TEXT" refnum as filename
536                                        //FSetPos refnum,91             //wrong position
537                                        FSetPos refnum,404
538                                        FBinWrite refnum, suffix
539                                        FStatus refnum
540                                        FSetPos refnum,V_logEOF
541                                        Close refnum
542                                endif
543                                jj+=1
544                        while(jj<num_t_files)
545                endif
546        ii+=1
547        while(ii<num_t_files)
548 
549        // Write suffix of transmission files into scattering files
550        ii= startRow
551        do
552                if (cmpstr(S_TRANS_Filenames[ii],"")!=0)
553                        jj=0
554                        do
555                                if (cmpstr(S_TRANS_Filenames[ii],T_GFilenames[jj])==0)
556                                        suffix=T_GSuffix[jj]
557                                        filename = pathname + S_GFilenames[ii]
558                                        Open/A/T="????TEXT" refnum as filename
559                                        //FSetPos refnum,91             //wrong position
560                                        FSetPos refnum,404
561                                        FBinWrite refnum, suffix
562                                        FStatus refnum
563                                        FSetPos refnum,V_logEOF
564                                        Close refnum
565                                endif
566                                jj+=1
567                        while(jj<num_t_files)
568                endif
569                ii+=1
570        while(ii<=endRow)
571        return(0)
572End
573
574// Allows to calculate transmissions without need of a scattering file. Used with the Calculate Total Trans button.
575
576Function AssignTotalTransFilesToData(startRow,endRow)
577        Variable startRow,endRow
578
579//      GetSelection table,ScatterFileTable,1
580       
581        Wave/T T_EMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
582        Wave/T T_GSuffices = $"root:myGlobals:TransHeaderInfo:T_Suffices"
583        Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
584        Wave/T T_GSuffix = $"root:myGlobals:TransHeaderInfo:T_Suffix"
585
586        Variable num_t_files, ii, jj
587        Variable refnum
588        num_t_files = numpnts(T_GFilenames)
589        String suffix = ""
590        PathInfo catPathName
591        String pathname = S_path
592        String filename
593
594        // Write suffix of empty beam file into transmission files
595        ii= 0
596        do
597                if (cmpstr(T_EMP_Filenames[ii],"")!=0)
598                        jj=0
599                        do
600                                if (cmpstr(T_EMP_Filenames[ii],T_GFilenames[jj])==0)
601                                        suffix=T_GSuffix[jj]
602                                        filename = pathname + T_GFilenames[ii]
603                                        Open/A/T="????TEXT" refnum as filename
604                                        //FSetPos refnum,91             //wrong position
605                                        FSetPos refnum,404
606                                        FBinWrite refnum, suffix
607                                        FStatus refnum
608                                        FSetPos refnum,V_logEOF
609                                        Close refnum
610                                endif
611                                jj+=1
612                        while(jj<num_t_files)
613                endif
614        ii+=1
615        while(ii<num_t_files)
616 
617        return(0)
618End
619
620
621//function that clears the selected assignments of sample trans -> sample scattering
622//also writes a trans=1 value back to the sample file
623//
624// DEC 2006 - now also writes blank suffix to clear trans->empty assignment, and Twhole=1
625//
626// target = 1 = ScatterFileTable
627// target = 2 = TransFileTable
628Function fClearSelectedAssignments(startRow,endRow,target)
629        Variable startRow,endRow,target
630       
631        Wave/T S_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
632        Wave/T S_GSuffices = $"root:myGlobals:TransHeaderInfo:S_Suffices"
633        Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
634        Wave/T S_GSuffix = $"root:myGlobals:TransHeaderInfo:S_Suffix"
635        Wave S_GTransmission =  $"root:myGlobals:TransHeaderInfo:S_Transmission"
636        Wave/T T_EMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
637        Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
638        Wave/T T_GSuffix = $"root:myGlobals:TransHeaderInfo:T_Suffix"
639        Wave T_GTransmission =  $"root:myGlobals:TransHeaderInfo:T_Transmission"
640        Wave T_GTWhole =  $"root:myGlobals:TransHeaderInfo:T_Whole"
641       
642       
643        Variable num_s_files, num_t_files, ii, jj
644        Variable refnum,zero=0
645        num_t_files = numpnts(T_GFilenames)
646        String suffix = "    "          //4 blank spaces
647        PathInfo catPathName
648        String pathname = S_path
649        String filename
650
651// If the selection is from the TransFileTable
652// - wipe out the box coordinates
653// - wipe out the suffix
654// - wipe out the transmission
655// - wipe out T_whole
656// - update the waves in the table
657
658        if(target == 2)
659                ii= startRow
660                do
661                        filename = pathname + T_GFilenames[ii]
662                       
663                        // write in zeros for the box coordinates
664                        Open/A/T="????TEXT" refnum as filename
665                        FSetPos refnum,478
666                        FBinWrite/F=3/B=3 refNum, zero
667                        FBinWrite/F=3/B=3 refNum, zero
668                        FBinWrite/F=3/B=3 refNum, zero
669                        FBinWrite/F=3/B=3 refNum, zero
670                        FStatus refnum                                  //move to the end of the file before closing
671                        FSetPos refnum,V_logEOF
672                        Close refnum
673       
674                        // write in a null suffix
675                        Open/A/T="????TEXT" refnum as filename
676                        FSetPos refnum,404                      //suffix start byte is 404
677                        FBinWrite refnum, suffix
678                        FStatus refnum
679                        FSetPos refnum,V_logEOF
680                        Close refnum
681                       
682                        //write a trans==1 to the file header of the raw data (open/close done in function)
683                        ReWriteReal(filename,1,158)             //transmission start byte is 158
684                        ReWriteReal(filename,1,392)             //WholeTrans start byte is 392
685                       
686                        //then update the table that is displayed
687                        T_EMP_Filenames[ii] = ""
688                        T_GTransmission[ii] = 1
689                        T_GTWhole[ii] = 1
690                       
691                        ii+=1
692                while(ii<=endRow)
693        endif
694       
695//      // Write suffix of empty beam file into transmission files
696//      ii= 0
697//      do
698//              if (cmpstr(T_EMP_Filenames[ii],"")!=0)
699//                      jj=0
700//                      do
701//                              if (cmpstr(T_EMP_Filenames[ii],T_GFilenames[jj])==0)
702//                                      suffix=T_GSuffix[jj]
703//                                      filename = pathname + T_GFilenames[ii]
704//                                      Open/A/T="????TEXT" refnum as filename
705//                                      //FSetPos refnum,91             //wrong position
706//                                      FSetPos refnum,404
707//                                      FBinWrite refnum, suffix
708//                                      FStatus refnum
709//                                      FSetPos refnum,V_logEOF
710//                                      Close refnum
711//                              endif
712//                              jj+=1
713//                      while(jj<num_t_files)
714//              endif
715//      ii+=1
716//      while(ii<num_t_files)
717 
718        if(target == 1)
719                // Write null suffix of transmission files into scattering files
720                ii= startRow
721                do
722                        filename = pathname + S_GFilenames[ii]
723                        Open/A/T="????TEXT" refnum as filename
724                        //FSetPos refnum,91             //wrong position
725                        FSetPos refnum,404
726                        FBinWrite refnum, suffix
727                        FStatus refnum
728                        FSetPos refnum,V_logEOF
729                        Close refnum
730                       
731                        //write a trans==1 to the file header of the raw data (open/close done in function)
732                        ReWriteReal(filename,1,158)             //transmission start byte is 158
733                       
734                        //then update the table that is displayed
735                        S_TRANS_Filenames[ii] = ""
736                        S_GTransmission[ii] = 1
737                       
738                        ii+=1
739                while(ii<=endRow)
740        endif
741       
742        return(0)
743End
744
745
746// assigns both the empty file to the trans file
747// and the trans file to the scattering file
748// - writes out 4 characters to the file headers @ byte 404
749//
750// assignments are only permanently made when the transmissions
751// are actually calculated
752//
753// NOT USED - AssignSelTransFilestoData(first,last) has been generalized to take a range
754// 3/31/04 SRK
755//Function AssignTransFilesToData()
756//
757//      Wave/T T_EMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
758//      Wave/T T_GSuffices = $"root:myGlobals:TransHeaderInfo:T_Suffices"
759//      Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
760//      Wave/T T_GSuffix = $"root:myGlobals:TransHeaderInfo:T_Suffix"
761//     
762//      Wave/T S_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
763//      Wave/T S_GSuffices = $"root:myGlobals:TransHeaderInfo:S_Suffices"
764//      Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
765//      Wave/T S_GSuffix = $"root:myGlobals:TransHeaderInfo:S_Suffix"
766//     
767//      Variable num_s_files, num_t_files, ii, jj
768//      Variable refnum
769//      num_t_files = numpnts(T_GFilenames)
770//      num_s_files = numpnts(S_GFilenames)
771//      String suffix = ""
772//      PathInfo catPathName
773//      String pathname = S_path
774//      String filename
775//
776//      // Write suffix of empty beam file into transmission files
777//      ii= 0
778//      do
779//              if (cmpstr(T_EMP_Filenames[ii],"")!=0)
780//                      jj=0
781//                      do
782//                              if (cmpstr(T_EMP_Filenames[ii],T_GFilenames[jj])==0)
783//                                      suffix=T_GSuffix[jj]
784//                                      filename = pathname + T_GFilenames[ii]
785//                                      Open/A/T="????TEXT" refnum as filename
786//                                      //FSetPos refnum,91             //wrong position
787//                                      FSetPos refnum,404
788//                                      FBinWrite refnum, suffix
789//                                      FStatus refnum
790//                                      FSetPos refnum,V_logEOF
791//                                      Close refnum
792//                              endif
793//                      jj+=1
794//                      while(jj<num_t_files)
795//              endif
796//              ii+=1
797//      while(ii<num_t_files)
798//
799//      // Write suffix of transmission files into scattering files
800//      ii= 0
801//      do
802//              if (cmpstr(S_TRANS_Filenames[ii],"")!=0)
803//                      jj=0
804//                      do
805//                              if (cmpstr(S_TRANS_Filenames[ii],T_GFilenames[jj])==0)
806//                                      suffix=T_GSuffix[jj]
807//                                      filename = pathname + S_GFilenames[ii]
808//                                      Open/A/T="????TEXT" refnum as filename
809//                                      //FSetPos refnum,91             //wrong position
810//                                      FSetPos refnum,404
811//                                      FBinWrite refnum, suffix
812//                                      FStatus refnum
813//                                      FSetPos refnum,V_logEOF
814//                                      Close refnum
815//                              endif
816//                              jj+=1
817//                      while(jj<num_t_files)
818//              endif
819//              ii+=1
820//      while(ii<num_s_files)
821//
822//      return(0)
823//End
824
825
826
827// NOT USED --- CalcSelTransFromHeader(first,last) has been generalized to accept a selection
828// and modified to account for different attenuation factors. 3/31/04 SRK
829//
830//Function CalcTransFromHeader()
831//      String filename
832//      Wave/T T_EMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
833//      Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
834//     
835//      Wave/T S_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
836//      Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
837//      Wave S_GTransmission =  $"root:myGlobals:TransHeaderInfo:S_Transmission"
838//     
839//      Variable num_s_files, num_t_files, ii, jj
840//      Variable refnum, transCts, emptyCts
841//      num_t_files = numpnts(T_GFilenames)
842//      num_s_files = numpnts(S_GFilenames)
843//      String suffix = ""
844//      PathInfo catPathName
845//      String pathname = S_path
846//      String textStr="",abortStr=""
847//      String emptyFile, transFile
848//     
849//      ii= 0
850//      do
851//              if (cmpstr(S_TRANS_Filenames[ii],"")!=0)
852//                      jj=0
853//                      do
854//                              if (cmpstr(S_TRANS_Filenames[ii],T_GFilenames[jj])==0)
855//                              //Look for an empty beam for the transmission then
856//                                      if (cmpstr(T_EMP_Filenames[jj],"")!=0)//Do the transmission calculation
857//                                              textStr = ""
858//                                              filename=pathname+S_GFilenames[ii]
859//                                              emptyFile = pathname+T_EMP_Filenames[jj]
860//                                              transFile = pathname+T_GFilenames[jj]
861//                                              //////////
862//                                              // check the empty beam file for previously selected coordinates
863//                                              //if they exist, set the xy string , save the normalized counts somewhere
864//                                              //the value was written to an unused r*4 header analysis.factor (@b494)
865//                                              Variable x1,x2,y1,y2,err
866//                                              Open/R refnum as emptyFile
867//                                              FSetPos refnum,478
868//                                              FBinRead/F=3/B=3 refnum, x1
869//                                              FBinRead/F=3/B=3 refnum, x2
870//                                              FBinRead/F=3/B=3 refnum, y1
871//                                              FBinRead/F=3/B=3 refnum, y2
872//                                              Close refnum
873//                                              //read the real count value
874//                                              String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
875//                                              String strToExecute
876//                                              strToExecute = GBLoadStr + "/S=494/U=1" + "\"" + emptyFile + "\""
877//                                              Execute strToExecute
878//                                              Wave w=$"tempGBWave0"
879//                                              Variable/G root:myGlobals:Patch:gTransCts = w[0]
880//                                              emptyCts = w[0]
881//                     
882//                                              //
883//                                              if( ((x1-x2)==0) || ((y1-y2)==0) )              //zero width marquee in either direction
884//                                                      //no region selected   
885//                                                      //prompt user to select box w/marquee
886//                                                      DoWindow/F TransFileTable
887//                                                      Abort "Use \"Set BCENT File\", then \"SetXYBox\" to select XY range in empty beam file "+T_EMP_Filenames[jj]
888//                                                      //from here the Marquee menu selection must handle the task of setting the box
889//                                                      //and updating the information in the file header
890//                                              Endif
891//             
892//                                              //read in trans file to add to SAM
893//                                              ReadHeaderAndData(transFile)
894//                                              //adds to SAM
895//                                              err = Raw_to_work("SAM")
896//                                              //sum region in SAM
897//                                              transCts =  SumCountsInBox(x1,x2,y1,y2,"SAM")
898//                                             
899//                                              //calculate trans based on empty beam value
900//                                              Variable trans= transCts/emptyCts
901//                                             
902//                                              //write out counts and transmission
903//                                              textstr += "  Trans counts = "+num2str(transCts) + "   Trans = "+num2str(trans)
904//                                              Print S_GFilenames[ii], textstr
905//                                              //Print ii,jj
906//                                              //Print  "Empty counts = ",emptycts, "Trans counts = "+num2str(transCts) + "   Trans = "+num2str(trans)
907//                                             
908//                                              //write the trans to the file header of the raw data (open/close done in function)
909//                                              Variable start = 158            //transmission start byte
910//                                              ReWriteReal(filename,trans,start)
911//                                             
912//                                              //then update the global that is displayed
913//                                              S_GTransmission[ii] = trans
914//     
915//                                      else  // There is no empty assigned
916//                                               abortStr = "Empty beam file not assigned properly for " + T_GFilenames[jj]
917//                                               Print abortStr
918//                                               //Abort abortStr
919//                                               return(1)
920//                                      endif
921//                              endif
922//                              jj+=1
923//                      while(jj<num_t_files)
924//              else //no transmission file
925//                      abortStr = "Transmission beam file not assigned properly for " + S_GFilenames[ii]
926//                      Print abortStr
927//              endif
928//              ii+=1
929//      while(ii<num_s_files)
930//      print "done"
931//      return(0)
932//End
933
934
935
936//given a selection of scattering files, calculates the transmission
937//and writes the new transmission to the file header
938//
939//given the full path;name;vers  (= filenmame), of a raw binary SANS file,
940//the transmission of the sample is calculated (if possible) from information
941//in the file header. empty beam and sample transmision run numbers are extracted
942//and the files are located from the run number (if possible)
943//from the header of the empty beam file, the XY box coordinates and "empty"
944//counts are determined
945//once all information is located, the transmission is calculated and the
946//transmission field of the sample file is automatically "Patched"
947// - updates the global variable that is displayed in the panel
948//can be run in batch mode, sequentially passing each item from a list of filenames
949//this is the single step of the batch mode
950//
951//in batch mode: execution will proceed through all of the files, reporting
952//results to the history window. If no trans could be successfully calculated
953//(for a blocked beam, for example), then that is displayed as well, and the
954//raw data is not modified
955//
956// now takes the attenuation of the sample trans and empty beam trans into account, (normally this == 1)
957// and rescales the transmission as appropriate
958// 3/31/04  SRK
959//
960Function CalcSelTransFromHeader(startRow,endRow)
961        Variable startRow,endRow
962//      GetSelection table,ScatterFileTable,1
963
964        String filename
965        Wave/T T_EMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
966        Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
967       
968        Wave/T S_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
969        Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
970        Wave S_GTransmission =  $"root:myGlobals:TransHeaderInfo:S_Transmission"
971       
972        Variable num_s_files, num_t_files, ii, jj
973        Variable refnum, transCts, emptyCts,attenRatio,lambda,trans
974        Variable x1,x2,y1,y2,err,attenEmp,attenSam
975        String suffix = "",pathName,textStr,abortStr,emptyFile,transFile,samFileStr
976        String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
977        String strToExecute
978       
979        num_t_files = numpnts(T_GFilenames)
980       
981        PathInfo catPathName
982        pathname = S_path
983
984        ii= startRow
985        do
986                if (cmpstr(S_TRANS_Filenames[ii],"")!=0)        //if there is a sample trans file assigned
987                        jj=0
988                        do
989                                if (cmpstr(S_TRANS_Filenames[ii],T_GFilenames[jj])==0)  //find it in the trans file table
990                                        if (cmpstr(T_EMP_Filenames[jj],"")!=0)          //if empty beam file assigned, proceed with the calculation
991                                                //full path+name to access all 3 files
992                                                filename=pathname+S_GFilenames[ii]
993                                                emptyFile = pathname+T_EMP_Filenames[jj]
994                                                transFile = pathname+T_GFilenames[jj]
995                                                //////////
996                                                // check the empty beam file for previously selected coordinates
997                                                //if they exist, set the xy string , save the normalized counts somewhere
998                                                //the value was written to an unused r*4 header analysis.factor (@b494)
999                                                Open/R refnum as emptyFile
1000                                                FSetPos refnum,478
1001                                                FBinRead/F=3/B=3 refnum, x1
1002                                                FBinRead/F=3/B=3 refnum, x2
1003                                                FBinRead/F=3/B=3 refnum, y1
1004                                                FBinRead/F=3/B=3 refnum, y2
1005                                                Close refnum
1006                                                //read the real count value
1007                                                strToExecute = GBLoadStr + "/S=494/U=1" + "\"" + emptyFile + "\""
1008                                                Execute strToExecute
1009                                                Wave w=$"tempGBWave0"
1010                                        //      Variable/G root:myGlobals:Patch:gTransCts = w[0]
1011                                                emptyCts = w[0]
1012                                                // read the attenuator number of the empty beam file
1013                                                strToExecute = GBLoadStr + "/S=39/U=4" + "\"" + emptyFile + "\""
1014                                                Execute strToExecute
1015                                                attenEmp = w[3]
1016                                                //
1017                                                if( ((x1-x2)==0) || ((y1-y2)==0) )              //zero width marquee in either direction
1018                                                        //no region selected    -- prompt user to select box w/marquee
1019                                                        DoWindow/F TransFileTable
1020                                                        Abort "Use \"Set BCENT File\", then \"SetXYBox\" to select XY range in empty beam file "+T_EMP_Filenames[jj]
1021                                                        //from here the Marquee menu selection must handle the task of setting the box and updating the information in the file header
1022                                                Endif
1023               
1024                                                //read in trans file then add to SAM
1025                                                ReadHeaderAndData(transFile)
1026                                                //adds to SAM
1027                                                err = Raw_to_work("SAM")
1028                                                //sum region in SAM
1029                                                transCts =  SumCountsInBox(x1,x2,y1,y2,"SAM")   
1030                                                // get the attenuator, lambda, and sample string (to get the instrument)
1031                                                WAVE/T samText = $"root:SAM:textRead"
1032                                                WAVE samReals = $"root:SAM:realsRead"
1033                                                samfileStr = samText[3]
1034                                                lambda = samReals[26]
1035                                                attenSam = samReals[3]
1036                                                //calculate the ratio of attenuation factors - assumes that same instrument used for each, AND same lambda
1037                                                AttenRatio = AttenuationFactor(samFileStr,lambda,attenEmp)/AttenuationFactor(samFileStr,lambda,attenSam)
1038                                                //calculate trans based on empty beam value and rescale by attenuation ratio
1039                                                trans= transCts/emptyCts * AttenRatio
1040                                               
1041                                                //write out counts and transmission to history window, showing the attenuator ratio, if it is not unity
1042                                                If(attenRatio==1)
1043                                                        Printf "%s\t\tTrans Counts = %g\tTrans = %g\r",S_GFilenames[ii], transCts,trans
1044                                                else
1045                                                        Printf "%s\t\tTrans Counts = %g\tTrans = %g\tAttenuatorRatio = %g\r",S_GFilenames[ii], transCts,trans,attenRatio
1046                                                endif
1047                                                //write the trans to the file header of the raw data (open/close done in function)
1048                                                ReWriteReal(filename,trans,158)         //transmission start byte is 158
1049                                               
1050                                                //then update the global that is displayed
1051                                                S_GTransmission[ii] = trans
1052                                               
1053                                        else  // There is no empty assigned
1054                                                abortStr = "Empty beam file not assigned properly for " + T_GFilenames[jj]
1055                                                Print abortStr
1056                                                //Abort abortStr
1057                                                return(1)
1058                                        endif
1059                                endif
1060                                jj+=1
1061                        while(jj<num_t_files)
1062                else //no transmission file
1063                        abortStr = "Transmission beam file not assigned properly for " + S_GFilenames[ii]
1064                        Print abortStr
1065                endif
1066                ii+=1
1067        while(ii<=endRow)
1068        print "done"
1069        return(0)
1070End
1071
1072//
1073// For the calculation of the Transmission using only the Trans Files screen
1074// Uses only the information on the transmissionFiles screen to calculate
1075// a "box" Transmission for comparison with the Whole Transmission
1076// updated 5/11/2006 by Bryan Greenwald
1077Function CalcTotalTrans(startRow,endRow)
1078        Variable startRow,endRow
1079//      GetSelection table,ScatterFileTable,1
1080
1081        String filename
1082        Wave/T T_EMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
1083       
1084        Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
1085        Wave T_GTransmission = $"root:myGlobals:TransHeaderInfo:T_Transmission"
1086       
1087//      Wave/T S_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
1088//      Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
1089//      Wave S_GTransmission =  $"root:myGlobals:TransHeaderInfo:S_Transmission"
1090        Wave GWhole= $"root:myGlobals:TransHeaderInfo:T_Whole"
1091       
1092        Variable num_t_files, ii, jj
1093        Variable refnum, transCts, emptyCts,attenRatio,lambda,trans
1094        Variable x1,x2,y1,y2,err,attenEmp,attenSam
1095        String suffix = "",pathName,textStr,abortStr,emptyFile,transFile,samFileStr
1096        String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
1097        String strToExecute
1098       
1099        num_t_files = numpnts(T_GFilenames)
1100       
1101        PathInfo catPathName
1102        pathname = S_path
1103
1104        ii= startRow
1105        do
1106                if (cmpstr(T_Emp_Filenames[ii],"")!=0)  //if there is a sample trans file assigned
1107                        jj=0
1108                        do
1109                                if (cmpstr(T_Emp_Filenames[ii],T_GFilenames[jj])==0)    //find it in the trans file table
1110                                        if (cmpstr(T_EMP_Filenames[ii],"")!=0)          //if empty beam file assigned, proceed with the calculation
1111                                                //full path+name to access all 3 files
1112                                                filename=pathname+T_GFilenames[ii]
1113                                                emptyFile = pathname+T_EMP_Filenames[ii]
1114                                                transFile = pathname+T_GFilenames[ii]
1115                                                //////////
1116                                                // check the empty beam file for previously selected coordinates
1117                                                //if they exist, set the xy string , save the normalized counts somewhere
1118                                                //the value was written to an unused r*4 header analysis.factor (@b494)
1119                                                Open/R refnum as emptyFile
1120                                                FSetPos refnum,478
1121                                                FBinRead/F=3/B=3 refnum, x1
1122                                                FBinRead/F=3/B=3 refnum, x2
1123                                                FBinRead/F=3/B=3 refnum, y1
1124                                                FBinRead/F=3/B=3 refnum, y2
1125                                                Close refnum
1126                                                //read the real count value
1127                                                strToExecute = GBLoadStr + "/S=494/U=1" + "\"" + emptyFile + "\""
1128                                                Execute strToExecute
1129                                                Wave w=$"tempGBWave0"
1130                                        //      Variable/G root:myGlobals:Patch:gTransCts = w[0]
1131                                                emptyCts = w[0]
1132                                                // read the attenuator number of the empty beam file
1133                                                strToExecute = GBLoadStr + "/S=39/U=4" + "\"" + emptyFile + "\""
1134                                                Execute strToExecute
1135                                                attenEmp = w[3]
1136                                                //
1137                                                if( ((x1-x2)==0) || ((y1-y2)==0) )              //zero width marquee in either direction
1138                                                        //no region selected    -- prompt user to select box w/marquee
1139                                                        DoWindow/F TransFileTable
1140                                                        Abort "Use \"Set BCENT File\", then \"SetXYBox\" to select XY range in empty beam file "+T_EMP_Filenames[jj]
1141                                                        //from here the Marquee menu selection must handle the task of setting the box and updating the information in the file header
1142                                                Endif
1143               
1144                                                //read in trans file then add to SAM
1145                                                ReadHeaderAndData(transFile)
1146                                                //adds to SAM
1147                                                err = Raw_to_work("SAM")
1148                                                //sum region in SAM
1149                                                transCts =  SumCountsInBox(x1,x2,y1,y2,"SAM")   
1150                                                // get the attenuator, lambda, and sample string (to get the instrument)
1151                                                WAVE/T samText = $"root:SAM:textRead"
1152                                                WAVE samReals = $"root:SAM:realsRead"
1153                                                samfileStr = samText[3]
1154                                                lambda = samReals[26]
1155                                                attenSam = samReals[3]
1156                                                //calculate the ratio of attenuation factors - assumes that same instrument used for each, AND same lambda
1157                                                AttenRatio = AttenuationFactor(samFileStr,lambda,attenEmp)/AttenuationFactor(samFileStr,lambda,attenSam)
1158                                                //calculate trans based on empty beam value and rescale by attenuation ratio
1159                                                trans= transCts/emptyCts * AttenRatio
1160                                               
1161                                                //write out counts and transmission to history window, showing the attenuator ratio, if it is not unity
1162                                                If(attenRatio==1)
1163                                                        //Printf "%s\t\tTrans Counts = %g\t Actual Trans = %g\r",T_GFilenames[ii], transCts,trans
1164                                                        Printf "%s\t\tBox Counts = %g\t Trans = %g\r",T_GFilenames[ii], transCts,trans
1165                                                else
1166                                                        //Printf "%s\t\tTrans Counts = %g\t Trans = %g\tAttenuatorRatio = %g\r",T_GFilenames[ii], transCts,trans,attenRatio
1167                                                        Printf "%s\t\tBox Counts = %g\t Trans = %g\t AttenuatorRatio = %g\r",T_GFilenames[ii], transCts,trans,attenRatio
1168                                                endif
1169                                                //write the trans to the file header of the raw data (open/close done in function)
1170                                                ReWriteReal(filename,trans,158)         //transmission start byte is 158
1171                                               
1172                                                //then update the global that is displayed
1173                                                T_GTransmission[ii] = trans
1174                                               
1175                                        else  // There is no empty assigned
1176                                                abortStr = "Empty beam file not assigned properly for " + T_GFilenames[jj]
1177                                                Print abortStr
1178                                                //Abort abortStr
1179                                                return(1)
1180                                        endif
1181                                endif
1182                                jj+=1
1183                        while(jj<num_t_files)
1184                else //no transmission file
1185                        abortStr = "Transmission beam file not assigned properly for " + T_GFilenames[ii]
1186                        Print abortStr
1187                endif
1188                ii+=1
1189        while(ii<=endRow)
1190        //print "done"
1191        return(0)
1192End
1193
1194//
1195// For the calculation of the Transmission using only the whole detector
1196// Used to compute the transmission for the TransmissionFiles table using
1197// the entire computer. For comparison with the "box" trans
1198// updated: 5/11/2006 bo Bryan Greenwald
1199Function CalcWholeTrans(startRow,endRow)
1200        Variable startRow,endRow
1201//      GetSelection table,ScatterFileTable,1
1202
1203        String filename
1204        Wave/T T_EMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
1205       
1206        Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
1207        Wave T_GTransmission = $"root:myGlobals:TransHeaderInfo:T_Transmission"
1208       
1209//      Wave/T S_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
1210//      Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
1211//      Wave S_GTransmission =  $"root:myGlobals:TransHeaderInfo:S_Transmission"
1212        Wave GWhole= $"root:myGlobals:TransHeaderInfo:T_Whole"
1213       
1214        Variable num_t_files, ii, jj
1215        Variable refnum, transCts, emptyCts,attenRatio,lambda,trans
1216        Variable x1,x2,y1,y2,err,attenEmp,attenSam
1217        String suffix = "",pathName,textStr,abortStr,emptyFile,transFile,samFileStr
1218        String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
1219        String strToExecute
1220       
1221        num_t_files = numpnts(T_GFilenames)
1222       
1223        PathInfo catPathName
1224        pathname = S_path
1225
1226        ii= startRow
1227        do
1228                if (cmpstr(T_Emp_Filenames[ii],"")!=0)  //if there is a sample trans file assigned
1229                        jj=0
1230                        do
1231                                if (cmpstr(T_Emp_Filenames[ii],T_GFilenames[jj])==0)    //find it in the trans file table
1232                                        if (cmpstr(T_EMP_Filenames[ii],"")!=0)          //if empty beam file assigned, proceed with the calculation
1233                                                //full path+name to access all 3 files
1234                                                filename=pathname+T_GFilenames[ii]
1235                                                emptyFile = pathname+T_EMP_Filenames[ii]
1236                                                transFile = pathname+T_GFilenames[ii]
1237                                                //////////
1238                                                // check the empty beam file for previously selected coordinates
1239                                                //if they exist, set the xy string , save the normalized counts somewhere
1240                                                //the value was written to an unused r*4 header analysis.factor (@b494)
1241                                                Open/R refnum as emptyFile
1242                                                FSetPos refnum,478
1243                                                FBinRead/F=3/B=3 refnum, x1
1244                                                FBinRead/F=3/B=3 refnum, x2
1245                                                FBinRead/F=3/B=3 refnum, y1
1246                                                FBinRead/F=3/B=3 refnum, y2
1247                                                Close refnum
1248                                                //read the real count value
1249                                                strToExecute = GBLoadStr + "/S=494/U=1" + "\"" + emptyFile + "\""
1250                                                Execute strToExecute
1251                                                Wave w=$"tempGBWave0"
1252                                        //      Variable/G root:myGlobals:Patch:gTransCts = w[0]
1253                                                emptyCts = w[0]
1254                                                // read the attenuator number of the empty beam file
1255                                                strToExecute = GBLoadStr + "/S=39/U=4" + "\"" + emptyFile + "\""
1256                                                Execute strToExecute
1257                                                attenEmp = w[3]
1258                                                //
1259                                                if( ((x1-x2)==0) || ((y1-y2)==0) )              //zero width marquee in either direction
1260                                                        //no region selected    -- prompt user to select box w/marquee
1261                                                        DoWindow/F TransFileTable
1262                                                        Abort "Use \"Set BCENT File\", then \"SetXYBox\" to select XY range in empty beam file "+T_EMP_Filenames[jj]
1263                                                        //from here the Marquee menu selection must handle the task of setting the box and updating the information in the file header
1264                                                Endif
1265               
1266                                                //read in trans file then add to SAM
1267                                                ReadHeaderAndData(transFile)
1268                                                //adds to SAM
1269                                                err = Raw_to_work("SAM")
1270                                                //sum region in SAM
1271                                                transCts =  SumTotalCounts("SAM")       
1272                                                // get the attenuator, lambda, and sample string (to get the instrument)
1273                                                WAVE/T samText = $"root:SAM:textRead"
1274                                                WAVE samReals = $"root:SAM:realsRead"
1275                                                samfileStr = samText[3]
1276                                                lambda = samReals[26]
1277                                                attenSam = samReals[3]
1278                                                //calculate the ratio of attenuation factors - assumes that same instrument used for each, AND same lambda
1279                                                AttenRatio = AttenuationFactor(samFileStr,lambda,attenEmp)/AttenuationFactor(samFileStr,lambda,attenSam)
1280                                                //calculate trans based on empty beam value and rescale by attenuation ratio
1281                                                trans= transCts/emptyCts * AttenRatio
1282                                               
1283                                                //write out counts and transmission to history window, showing the attenuator ratio, if it is not unity
1284                                                If(attenRatio==1)
1285                                                        //Printf "%s\t\tTrans Counts = %g\tTrans using whole detector = %g\t Tbox/Twhole = %g\r",T_GFilenames[ii], transCts,trans,T_GTransmission[ii]/trans
1286                                                        Printf "%s\t\tTotal Counts = %g\t Trans using whole detector = %g\t",T_GFilenames[ii], transCts,trans
1287                                                else
1288                                                        //Printf "%s\t\tTrans Counts = %g\tTrans = %g\tAttenuatorRatio = %g, Tbox/Twhole = %g\r",T_GFilenames[ii], transCts,trans,attenRatio, T_GTransmission[ii]/trans
1289                                                        Printf "%s\t\tTotal Counts = %g\t Trans using whole detector = %g\t AttenuatorRatio = %g",T_GFilenames[ii], transCts,trans,attenRatio
1290                                                endif
1291                                                If(T_GTransmission[ii]/trans > 0.97)
1292                                                        printf " Tbox/Twhole = %g\r",T_GTransmission[ii]/trans
1293                                                else
1294                                                        printf " !!! Tbox/Twhole is low !!! = %g\r",T_GTransmission[ii]/trans
1295                                                endif
1296                                                //write the trans to the file header of the raw data (open/close done in function)
1297                                                ReWriteReal(filename,trans,392)         //WholeTrans start byte is 392
1298                                               
1299                                                //then update the global that is displayed
1300                                                GWhole[ii] = trans
1301                                               
1302                                        else  // There is no empty assigned
1303                                                abortStr = "Empty beam file not assigned properly for " + T_GFilenames[jj]
1304                                                Print abortStr
1305                                                //Abort abortStr
1306                                                return(1)
1307                                        endif
1308                                endif
1309                                jj+=1
1310                        while(jj<num_t_files)
1311                else //no transmission file
1312                        abortStr = "Transmission beam file not assigned properly for " + T_GFilenames[ii]
1313                        Print abortStr
1314                endif
1315                ii+=1
1316        while(ii<=endRow)
1317        print "done"
1318        return(0)
1319End
1320
1321
1322//window recreation macro for the Trans Panel
1323//
1324Proc Trans_Panel()
1325        PauseUpdate; Silent 1           // building window...
1326        NewPanel /W=(173,197,540,424)/K=1 as "Calculate Transmissions"
1327        DoWindow/C Trans_Panel
1328        ModifyPanel cbRGB=(49807,47186,38011)
1329        ModifyPanel fixedSize=1
1330        SetDrawLayer UserBack
1331        DrawLine 0,56,368,56
1332        DrawLine 0,125,368,125
1333        Button pick_emp,pos={4,65},size={105,20},proc=PickEMPTransButton,title="set BCENT file"
1334        Button pick_emp,help={"This button will set the file selected in the Transmission file table to be the empty beam file."}
1335        SetVariable empStr,pos={114,67},size={250,17},title="file:"
1336        SetVariable empStr,help={"Filename of the empty beam file(s) to be used in the transmission calculation"}
1337        SetVariable empStr,fSize=10
1338        SetVariable empStr,limits={-Inf,Inf,0},value= root:myGlobals:TransHeaderInfo:gEMP
1339        Button Trn_button_1,pos={5,101},size={90,20},proc=Trn_SetXYBoxButton,title="Set XY Box"
1340        Button Trn_button_1,help={"Sets the XY box to sum over"}
1341        Button Trn_button_2,pos={174,139},size={110,20},proc=Trn_SortFilesByDate,title="Sort by Date"
1342        Button Trn_button_2,help={"Sort the scattering and transmission files by creation date."}
1343        Button Trn_button_3,pos={174,169},size={110,20},proc=Trn_SortFilesByLabel,title="Sort by Label"
1344        Button Trn_button_3,help={"Sort the scattering and transmission files by label."}
1345        Button Trn_button_4,pos={295,139},size={67,20},proc=Trn_ShowHelpProc,title="Help"
1346        Button Trn_button_4,help={"Show a help notebook for calculating transmissions."}
1347        SetVariable Trn_setvar_1,pos={135,100},size={227,17},title="Box is "
1348        SetVariable Trn_setvar_1,help={"Box coordinates to sum over"},fSize=10
1349        SetVariable Trn_setvar_1,limits={-Inf,Inf,0},value= root:myGlobals:TransHeaderInfo:gBox
1350        Button Trn_button_0,pos={1,1},size={70,20},proc=Trn_PickPathButton,title="Pick Path"
1351        Button Trn_button_0,help={"Select the folder containing the SANS data files"}
1352        Button Trn_button_9,pos={335,1},size={25,20},proc=ShowTransHelp,title="?"
1353        Button Trn_button_9,help={"Show the help file for calculating sample transmissions"}
1354        SetVariable Trn_setvar_0,pos={80,4},size={250,17},title="Path"
1355        SetVariable Trn_setvar_0,help={"Currently selected data path"},fSize=10
1356        SetVariable Trn_setvar_0,limits={-Inf,Inf,0},value= root:myGlobals:TransHeaderInfo:gCatPathStr
1357        Button Trn_button_5,pos={5,169},size={161,20},proc=Trn_CalcAllFilesButton,title="Calculate All Files"
1358        Button Trn_button_5,help={"Calculate transmission and patch headers of ALL files in the Scattering File Table."}
1359        Button Trn_button_6,pos={295,198},size={67,20},proc=Trn_PanelDoneButtonProc,title="Done"
1360        Button Trn_button_6,help={"Close the panel when done calculating transmissions"}
1361        Button Trn_button_7,pos={67,32},size={214,20},proc=Trn_RefreshProc,title="List Files"
1362        Button Trn_button_7,help={"Generate or refresh the tables of files."}
1363        Button Trn_button_8,pos={5,139},size={161,20},proc=Trn_CalcSelectedFilesButton,title="Calculate Selected Files"
1364        Button Trn_button_8,help={"Calculate transmission and patch headers of selected files in the Scattering File Table."}
1365        Button Trn_button_10,pos={5,198}, size={161,20},proc=TotalTransButtonProc
1366        Button Trn_button_10 title="Calculate Total Trans"
1367        Button Trn_button_10 help={"Calculate transmission over the whole detector and patch headers of ALL files in the data folder."}
1368EndMacro
1369
1370
1371Function TotalTransButtonProc(ctrlName) : ButtonControl
1372        String ctrlName
1373        TotalTrans(ctrlName)
1374End
1375
1376
1377Proc ShowTransHelp(ctrlName) : ButtonControl
1378        String ctrlName
1379        DisplayHelpTopic/K=1 "SANS Data Reduction Tutorial[Calculate Transmissions]"
1380End
1381
1382
1383//generates the file tables of scattering files and transmission files
1384//
1385// if the tables already exist, they will be updated
1386//
1387Proc Trn_RefreshProc(ctrlName) : ButtonControl
1388        String ctrlName
1389        BuildFileTables()
1390End
1391
1392//shows a noteboox of help text describing how to calculate transmissions
1393// - better that the user read the igor or pdf file, but it's a victory to get
1394//users to read anything at all
1395//
1396Proc Trn_ShowHelpProc(ctrlName) : ButtonControl
1397        String ctrlName
1398        DisplayTransHelp()
1399End
1400
1401//sorts the tables by date
1402//
1403Proc Trn_SortFilesByDate(ctrlName) : ButtonControl
1404        String ctrlName
1405        SortTransByDate()
1406End
1407
1408//sorts the tables alphabetically by the sample label field
1409//
1410Proc Trn_SortFilesByLabel(ctrlName) : ButtonControl
1411        String ctrlName
1412        SortTransByLabel()
1413End
1414
1415//button action procedure to select the local path to the
1416//folder containing the RAW SANS data files
1417// - once the data folder is selected, all of the popup file menus are
1418//forced to update with fresh file lists
1419//
1420Function Trn_PickPathButton(PathButton) : ButtonControl
1421        String PathButton
1422       
1423        //set the global string to the selected pathname
1424        Variable err
1425        err = PickPath()
1426        if(err)
1427                return(1)               //some problem with path or user cancelled, get out
1428        endif
1429       
1430        PathInfo/S catPathName
1431        String path = S_path
1432        if (V_flag == 0)
1433                //path does not exist - no folder selected
1434                String/G root:myGlobals:TransHeaderInfo:gCatPathStr = "no folder selected"
1435        else
1436                String/G root:myGlobals:TransHeaderInfo:gCatPathStr = path
1437        endif
1438       
1439        //Update the pathStr variable box
1440        ControlUpdate/W=Trans_Panel $"Trn_setvar_0"
1441        //Generate tables
1442        Execute "BuildFileTables()"
1443       
1444End
1445
1446//button action function that asks the user to set the XY box to sum over
1447//for all transmission calculations
1448//
1449// requires the user to set the box coordinates with the marquee popup
1450//that wil properly set the keyword string, and write the values to the empty beam header
1451//
1452Function Trn_SetXYBoxButton(ctrlName) : ButtonControl
1453        String ctrlName
1454
1455        String msgStr = "Select the Empty Beam File"
1456        String filename=""
1457       
1458        //get the filename from the popup menu
1459        //and construct a full path to it
1460        SVAR partialName = root:myGlobals:TransHeaderInfo:gEMP
1461        print partialName
1462        //get a valid file based on this partialName and catPathName
1463        String tempName = FindValidFilename(partialName)
1464        if(cmpstr(tempName,"")==0)
1465                //file not found, get out
1466                Abort "Empty beam file not found Trn_SetXYBoxButton(ctrlName)"
1467        Endif
1468        //name is ok, prepend path to tempName for read routine
1469        PathInfo catPathName
1470        filename = S_path + tempName
1471       
1472        //read the file in - check for previous coordinates
1473        ReadHeaderAndData(filename)
1474        //data is displayed here (go through the normal display steps, so all is created properly
1475        String/G root:myGlobals:gDataDisplayType="RAW"
1476        fRawWindowHook()
1477       
1478        // check the empty beam file for previously selected coordinates
1479        //if they exist, set the xy string , save the normalized counts somewhere
1480        //the value was written to an unused r*4 header analysis.factor (@b494)
1481        Variable refnum,x1,x2,y1,y2,err
1482        Open/R refnum as filename
1483        FSetPos refnum,478
1484        FBinRead/F=3/B=3 refnum, x1
1485        FBinRead/F=3/B=3 refnum, x2
1486        FBinRead/F=3/B=3 refnum, y1
1487        FBinRead/F=3/B=3 refnum, y2
1488        Close refnum
1489        //read the real count value
1490        String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
1491        String strToExecute
1492        strToExecute = GBLoadStr + "/S=494/U=1" + "\"" + filename + "\""
1493        Execute strToExecute
1494        Wave w=$"tempGBWave0"
1495        Variable/G root:myGlobals:gTransCts = w[0]                      //***NOTE this is NOT in the Trans sub-folder
1496        //
1497        if( ((x1-x2)==0) || ((y1-y2)==0) )              //zero width marquee in either direction
1498                //no region selected
1499               
1500                //add the empty beam file to work.SAM
1501                err = Raw_to_work("SAM")
1502       
1503                //the calling macro must change the display type
1504                String/G root:myGlobals:gDataDisplayType="SAM"          //displayed data type is sam
1505       
1506                //data is displayed here
1507                fRawWindowHook()
1508       
1509                //prompt user to select box w/marquee
1510                DoAlert 0,"Select the region to sum with the Marquee"
1511       
1512                //from here the Marquee menu selection must handle the task of setting the box
1513                //and updating the information in the file header
1514        else
1515                // region already selected, just put up the values from the file header
1516                //allow user the option of overriding the current box region
1517                msgStr = "X1="+num2str(x1)+";"
1518                msgStr += "X2="+num2str(x2)+";"
1519                msgStr += "Y1="+num2str(y1)+";"
1520                msgStr += "Y2="+num2str(y2)+";"
1521                String textStr
1522                textStr = "Override current box  "+msgStr+" ?"
1523                DoAlert 1,textStr
1524                If((V_flag)==1)
1525                        //get new box coordinates, same procedure as above
1526                        //add the empty beam file to work.SAM
1527                        err = Raw_to_work("SAM")
1528       
1529                        //the calling macro must change the display type
1530                        String/G root:myGlobals:gDataDisplayType="SAM"          //displayed data type is sam
1531       
1532                        //data is displayed here
1533                        fRawWindowHook()
1534       
1535                        //prompt user to select box w/marquee
1536                        DoAlert 0,"Select the region to sum with the Marquee"
1537       
1538                        //from here the Marquee menu selection must handle the task of setting the box
1539                        //and updating the information in the file header
1540                else
1541                        String/G root:myGlobals:TransHeaderInfo:gBox = msgStr
1542                Endif
1543        Endif
1544       
1545        UpdateBoxCoordinates()
1546        KillWaves/Z w
1547        Return (0)
1548End
1549
1550
1551//button action function that assigns the selected empty beam file (run number only)
1552//and the sample trnansmissionfile (run number only) to the popup list of  sample scattering
1553//files (ALL of the list items). Assignment is done by writing the integer run numbers of
1554//trans files to the header of each of the  scattering files, in a batchwise processing
1555//of the popup list
1556//
1557//transmission are not calculated here - only the file numbers are set
1558//
1559//*********unused***********
1560//Function Trn_AssignAllFilesButton(ctrlName) : ButtonControl
1561//      String ctrlName
1562//
1563//      AssignTransFilesToData()
1564//             
1565//End
1566
1567//button action procedure to calculate the transmission of all of the sample files in the
1568//sample popup list, based on the information in EACH sample header and in the empty beam file
1569//the box must be set properly in the empty beam file, AND the empty and trans
1570//run number must also be set properly in EACH scattering file,
1571//If run numbers are not properly assigned, no trans is calculated and the
1572//header is not modified
1573//
1574//The RAW data header of EACH sample file IS modified, with the newly calculated transmission
1575//
1576//not very friendly, but forces users to assign the files before continuing
1577//
1578Function Trn_CalcAllFilesButton(ctrlName) : ButtonControl
1579        String ctrlName
1580
1581        //calculate the transmission and Patch the header (if possible)
1582        Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
1583        Variable num_s_files = numpnts(S_GFilenames)
1584        AssignSelTransFilesToData(0,num_s_files-1)              //do all the files (points 0 to n-1, inclusive)
1585        CalcSelTransFromHeader(0,num_s_files-1)
1586       
1587End
1588
1589//for selected scattering files in the scattering table,
1590// the "Annn" association of trans file is written to the data file,
1591// the the transmission is actually calculated
1592//
1593Function Trn_CalcSelectedFilesButton(ctrlName) : ButtonControl
1594        String ctrlName
1595 
1596   Variable scatterTableExists
1597   scatterTableExists = WinType("ScatterFileTable")
1598   if (scatterTableExists != 0)
1599                GetSelection table,ScatterFileTable,1
1600                if(V_Flag != 0)
1601                        AssignSelTransFilesToData(V_StartRow,V_EndRow)
1602                        //calculate the transmission and Patch the header (if possible)
1603                        CalcSelTransFromHeader(V_StartRow,V_EndRow)
1604                Else
1605                        DoAlert 0,"No selection from Scattering Files table"
1606                Endif
1607   Else
1608                DoAlert 0,"No file selected from Scattering Files table or no Scattering Files table available"
1609   Endif
1610End
1611
1612// b BGtoFix -comment to describe the workings of the function
1613// since it's different than calculating the regular transmission
1614//
1615// I am not sure what the difference is inthe function of CalcTotalTrans and CalcWholeTrans ?
1616// do they really do anything different?
1617//
1618Function TotalTrans(ctrlName) : ButtonControl
1619        String ctrlName
1620 
1621   Variable transTableExists
1622   transTableExists = WinType("ScatterFileTable")               // b BGtoFix -checking for the existence of the wrong table
1623   if (transTableExists != 0)
1624                GetSelection table,transFileTable,1
1625                if(V_Flag != 0)
1626                        AssignTotalTransFilesToData(V_StartRow,V_EndRow)
1627                        //calculate the transmission and Patch the header (if possible)
1628                        CalcTotalTrans(V_StartRow,V_EndRow)
1629                        CalcWholeTrans(V_StartRow,V_EndRow)
1630                Else
1631                        DoAlert 0,"No selection from Scattering Files table"                    // b BGtoFix - really looking for files from the Trans Table...
1632                Endif
1633   Else
1634                DoAlert 0,"No file selected from Scattering Files table or no Scattering Files table available"  // b BGtoFix - really looking for files from the Trans Table...
1635   Endif
1636End
1637
1638
1639//simple button procedure to close the trans panel
1640// - automatically kills the two tables as well
1641//
1642Function Trn_PanelDoneButtonProc(ctrlName) : ButtonControl
1643        String ctrlName
1644
1645        // this button will make sure all files are closed
1646        //and close the panel
1647       
1648        Close/A
1649        DoWindow/K Trans_Panel
1650        DoWindow/K ScatterFileTable
1651        DoWindow/K TransFileTable
1652End
1653
1654
1655//function to check the header of a raw data file (full path specified by fname)
1656//checks the field of the x-position of the beamstop during data collection
1657//if the x-position is more negatice (farther to the left) than xTol(input)
1658//the the beamstop is "out" and the file is a transmission run and not a scattering run
1659//xtol typically set at -5 (cm) - trans runs have bs(x) at -10 to -15 cm
1660// function returns 1 if beamstop is out, 0 if beamstop is in
1661//
1662Function CheckIfBeamstopOut(fName,xTol)
1663        String fname
1664        Variable xTol
1665       
1666        Variable refnum,xpos
1667        //pos = 369, read one real value
1668       
1669        SetDataFolder root:
1670        String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
1671        String strToExecute=""
1672        // 1 R*4 value
1673        strToExecute = GBLoadStr + "/S=368/U=1" + "\"" + fname + "\""
1674        Execute strToExecute
1675        Wave w=$"root:tempGBWave0"
1676        xPos = w[0]
1677        KillWaves/Z w
1678        //Print "xPos = ",xpos
1679       
1680        if(xpos<=xTol)
1681                //xpos is farther left (more negative) than xtol (currently -5 cm)
1682                Return(1)
1683        else
1684                //some other file
1685                Return(0)
1686        Endif
1687End
1688
1689//function to update the box coordinates of the file selected as the
1690//empty beam file - takes the file that is currently popped from the list
1691//reads the 4 "analysis" integers that hold the box coordinates
1692//resets the globals string that is displayed with the new values
1693//should be called whenever the "empty" popup is popped, to ensure
1694//that current header information is displayed
1695//
1696Function UpdateBoxCoordinates()
1697
1698        //construct a full name, and read in the label from the file
1699        //and set the global
1700        String textstr=""
1701        ControlInfo empStr
1702        SVAR item = root:myGlobals:TransHeaderInfo:gEMP
1703        String tempName = FindValidFilename(item)
1704        if(cmpstr(tempName,"")==0)
1705                //file not found, get out
1706                Abort "Empty beam file not found UpdateBoxCoordinates(ctrlName)"
1707        Endif
1708        //name is ok, prepend path to tempName for read routine
1709        PathInfo catPathName
1710        String filename = S_path + tempName
1711       
1712        Variable refnum,x1,x2,y1,y2,err
1713        Open/R refnum as filename
1714        FSetPos refnum,478
1715        FBinRead/F=3/B=3 refnum, x1
1716        FBinRead/F=3/B=3 refnum, x2
1717        FBinRead/F=3/B=3 refnum, y1
1718        FBinRead/F=3/B=3 refnum, y2
1719        Close refnum
1720       
1721        //and update the global string
1722        String msgStr=""
1723        msgStr = "X1="+num2str(x1)+";"
1724        msgStr += "X2="+num2str(x2)+";"
1725        msgStr += "Y1="+num2str(y1)+";"
1726        msgStr += "Y2="+num2str(y2)+";"
1727       
1728        String/G root:myGlobals:TransHeaderInfo:gBox = msgStr
1729       
1730        ControlUpdate/W=Trans_panel Trn_setvar_1
1731End
1732
1733//crude procedure to display a notebook of help information for users
1734//
1735Proc DisplayTransHelp()
1736        String nb = "Notebook0"
1737        NewNotebook/N=$nb/F=1/V=1/W=(342,302,868,674) as "Notebook0:Transmission Help"
1738        Notebook $nb defaultTab=36, statusWidth=238, pageMargins={36,72,36,72}
1739        Notebook $nb showRuler=1, rulerUnits=1, updating={1, 3600}
1740        Notebook $nb newRuler=Normal, justification=0, margins={0,0,468}, spacing={0,0,0}, tabs={}, rulerDefaults={"Geneva",10,0,(0,0,0)}
1741        Notebook $nb ruler=Normal; Notebook $nb  justification=1, fSize=14, fStyle=1, text="Transmission Help\r"
1742        Notebook $nb ruler=Normal, fSize=-1, fStyle=-1, text="\r"
1743        Notebook $nb text="This panel allows quick and easy calculation of sample transmission. The neutron transmission of a sampl"
1744        Notebook $nb text="e must be calculated and entered into the header of each sample scattering file before proper background"
1745        Notebook $nb text=" corrections or absolute scaling can be done.\r"
1746        Notebook $nb text="\r"
1747        Notebook $nb text="During data collection, you measured an empty beam  - that is a measurement with heavy attenuation of th"
1748        Notebook $nb text="e neutron beam and the beamstop mover out of line with the direct beam. Then without changing the attenu"
1749        Notebook $nb text="ation or replacing the beamstop, you put a sample in the beam, and repeated the measurement (a sample tr"
1750        Notebook $nb text="ansmission measurement). The neutron transmission of the sample is simply the ratio of the number of neu"
1751        Notebook $nb text="tron counts from the sample transmission measurement normalized by the number of neutron counts from the"
1752        Notebook $nb text=" empty beam measurement. In this way, each sample transmission file is \"linked\" to the same empty beam t"
1753        Notebook $nb text="ransmission file.\r"
1754        Notebook $nb text="\r"
1755        Notebook $nb text="This calculated transmission value must be entered into the header of the sample scattering measurement "
1756        Notebook $nb text="- that is the measurement of the sample with the beamstop covering the primary (transmitted) beam, and n"
1757        Notebook $nb text="o (or few) attenuators in place. In this way, the sample transmission file is \"linked\" to its correspond"
1758        Notebook $nb text="ing sample scattering file. This panel allows you to set up the links and calculate the transmission.Tra"
1759        Notebook $nb text="nsmisison values are automatically patched to the scattering file headers as they are calculated.\r"
1760        Notebook $nb text="\r"
1761        Notebook $nb text="To Calculate Transmissions:\r"
1762        Notebook $nb text="\r"
1763        Notebook $nb text="1) Click \"List Files\" to get two lists (tables) - one of all the sample scattering files in the folder, "
1764        Notebook $nb text="and one of the transmission files.\r"
1765        Notebook $nb text="\r"
1766        Notebook $nb text="2) Select the region of the detector to sum over from the empty beam transmission file. Do theis by clic"
1767        Notebook $nb text="king on the filename of the empty beam transmision file (from the TransmissionFiles window, in the blue "
1768        Notebook $nb text="\"T_Filenames\" column). Return to the panel, and click the \"set EMP file\" button\", and the filename shoul"
1769        Notebook $nb text="d appear in the file box. The \"Box is\" field should have zeros for the x and y coordinates. Click the \"S"
1770        Notebook $nb text="et XY Box\" button. The empty beam file will be displayed, and you will be instructed to select the regio"
1771        Notebook $nb text="n to sum with the marquee. On the data, click and drag a rectangle that encompasses the primary beam. Mo"
1772        Notebook $nb text="ve the cursor inside the selection, to get an \"upside-down hat\" cursor. Click to get a menu, and near th"
1773        Notebook $nb text="e bottom, select \"Set XY Box Coords\". The pixel values should be updated to the Transmission panel, and "
1774        Notebook $nb text="are written to the empty beam header for future calculations. Note that the marquee selection can also b"
1775        Notebook $nb text="e used to measure the beam center, or centroid of any selected region.\r"
1776        Notebook $nb text="\r"
1777        Notebook $nb text="3) Now you need to \"link\" the ", fStyle=2, text="sample", fStyle=-1, text=" transmission files to the "
1778        Notebook $nb fStyle=2, text="empty", fStyle=-1, text=" ", fStyle=2, text="beam", fStyle=-1
1779        Notebook $nb text=" transmission file. Do this in the TransmissionFiles window by selecting the filename (in the blue T_Fil"
1780        Notebook $nb text="enames column) and pasing it to the corresponding row(s) in the T_EMP_Filenames column. This links the e"
1781        Notebook $nb text="mpty beam transmission with the sample transmission file. Do this for every sample transmission file.\r"
1782        Notebook $nb text="\r"
1783        Notebook $nb text="4) Now you need to link the sample ", fStyle=2, text="transmission", fStyle=-1
1784        Notebook $nb text=" file to the sample ", fStyle=2, text="scattering", fStyle=-1
1785        Notebook $nb text=" file. Do this by selecting the name of the transmission file (from the blue T_Filenames column) and pas"
1786        Notebook $nb text="ting it into the corresponding row of the S_TRANS_Filenames column of the ScatteringFiles window. This l"
1787        Notebook $nb text="inks the scattering file to its corresponding sample transmission file. This for all of the sample scatt"
1788        Notebook $nb text="ering files.\r"
1789        Notebook $nb text="\r"
1790        Notebook $nb text="5) Calculate the transmissions (and automatically write the transmission value to the satterng file head"
1791        Notebook $nb text="er) by clicking \"Calculate All Files\" from the Transmission Panel. The results will be printed to the co"
1792        Notebook $nb text="mmand window at the bottom of the screen. Any improperly assigned files will be listed. For example, blo"
1793        Notebook $nb text="cked beam scattering files will return an error, since the transmssion known to be zero, and is not meas"
1794        Notebook $nb text="ured (and no files are \"linked\"). Rather than calculating the transmission of all of the files,  a range"
1795        Notebook $nb text=" of S_Filenames can be selected, then calculated by clicking \"Calculate Selected Files\".\r"
1796        Notebook $nb text="\r"
1797        Notebook $nb text="By default the lists are sorted by run number (= chronological). In some cases it may be easier to sort "
1798        Notebook $nb text="by the sample label to group several of the same sample scattering files collected at different sample-t"
1799        Notebook $nb text="o-detector distances. Its utility depends, of course, on how consistent and unique your sample labels ar"
1800        Notebook $nb text="e.\r"
1801        Notebook $nb text="\r"
1802        Notebook $nb text="The \"links\" are stored in the corresponding files, and wil be re-generated when the lists are re-generat"
1803        Notebook $nb text="ed. Unassigned links will appear as blank elements in the T_EMP_Filenames or S_TRANS_Filenames columns.\r"
1804        Notebook $nb text="\r"
1805        Notebook $nb, selection={startOfFile,startOfFile}
1806        Notebook $nb text="\r"
1807End
1808
1809//******************
1810//lookup tables for attenuator transmissions
1811//NG3 and NG7 attenuators are physically different, so the transmissions are slightly different
1812//NG1 - (8m SANS) is not supported
1813
1814Proc MakeNG3AttenTable()
1815
1816        NewDataFolder/O root:myGlobals:Attenuators
1817        //do explicitly to avoid data folder problems, redundant, but it must work without fail
1818        Make/O/N=9 root:myGlobals:Attenuators:ng3att0
1819        Make/O/N=9 root:myGlobals:Attenuators:ng3att1
1820        Make/O/N=9 root:myGlobals:Attenuators:ng3att2
1821        Make/O/N=9 root:myGlobals:Attenuators:ng3att3
1822        Make/O/N=9 root:myGlobals:Attenuators:ng3att4
1823        Make/O/N=9 root:myGlobals:Attenuators:ng3att5
1824        Make/O/N=9 root:myGlobals:Attenuators:ng3att6
1825        Make/O/N=9 root:myGlobals:Attenuators:ng3att7
1826        Make/O/N=9 root:myGlobals:Attenuators:ng3att8
1827        Make/O/N=9 root:myGlobals:Attenuators:ng3att9
1828        Make/O/N=9 root:myGlobals:Attenuators:ng3att10
1829       
1830        //each wave has 8 elements, the transmission of att# at the wavelengths
1831        //lambda = 5,6,7,8,10,12,14,17
1832        Make/O/N=9 root:myGlobals:Attenuators:ng3lambda={5,6,7,8,10,12,14,17,20}
1833        root:myGlobals:Attenuators:ng3att0 = {1, 1, 1, 1, 1, 1, 1, 1,1 }
1834        root:myGlobals:Attenuators:ng3att1 = {0.421, 0.394, 0.371, 0.349, 0.316, 0.293, 0.274, 0.245,0.220}
1835        root:myGlobals:Attenuators:ng3att2 = {0.187, 0.164, 0.145, 0.130, 0.106, 0.0916, 0.0808, 0.0651,0.0531}
1836        root:myGlobals:Attenuators:ng3att3 = {0.0777, 0.0636, 0.0534, 0.0446, 0.0330, 0.0262, 0.0217, 0.0157 ,0.0116}
1837        root:myGlobals:Attenuators:ng3att4 = {0.0328, 0.0252, 0.0195, 0.0156, 0.0104, 7.68e-3, 5.98e-3, 3.91e-3,0.00262}
1838        root:myGlobals:Attenuators:ng3att5 = {0.0139, 9.94e-3, 7.34e-3, 5.44e-3, 3.29e-3, 2.25e-3, 1.66e-3, 9.95e-4, 6.12e-4}
1839        root:myGlobals:Attenuators:ng3att6 = {5.95e-3, 3.97e-3, 2.77e-3, 1.95e-3, 1.06e-3, 6.81e-4, 4.71e-4, 2.59e-4 , 1.45e-4}
1840        root:myGlobals:Attenuators:ng3att7 = {1.07e-3, 6.24e-4, 3.90e-4, 2.44e-4, 1.14e-4, 6.55e-5, 4.10e-5, 1.64e-5 , 7.26e-6}
1841        root:myGlobals:Attenuators:ng3att8 = {1.90e-4, 9.84e-5, 5.60e-5, 3.25e-5, 1.55e-5, 6.60e-6, 3.42e-6, 1.04e-6 , 3.48e-7}
1842        root:myGlobals:Attenuators:ng3att9 = {3.61e-5, 1.74e-5, 9.90e-6, 6.45e-6, 2.35e-6, 6.35e-7, 2.86e-7, 6.61e-8 , 1.73e-8}
1843        root:myGlobals:Attenuators:ng3att10 = {7.60e-6, 3.99e-6, 2.96e-6, 2.03e-6, 3.34e-7, 6.11e-8, 2.39e-8, 4.19e-9 , 8.60e-10}
1844
1845End
1846
1847Proc MakeNG7AttenTable()
1848
1849        NewDataFolder/O root:myGlobals:Attenuators
1850       
1851        Make/O/N=9 root:myGlobals:Attenuators:ng7att0
1852        Make/O/N=9 root:myGlobals:Attenuators:ng7att1
1853        Make/O/N=9 root:myGlobals:Attenuators:ng7att2
1854        Make/O/N=9 root:myGlobals:Attenuators:ng7att3
1855        Make/O/N=9 root:myGlobals:Attenuators:ng7att4
1856        Make/O/N=9 root:myGlobals:Attenuators:ng7att5
1857        Make/O/N=9 root:myGlobals:Attenuators:ng7att6
1858        Make/O/N=9 root:myGlobals:Attenuators:ng7att7
1859        Make/O/N=9 root:myGlobals:Attenuators:ng7att8
1860        Make/O/N=9 root:myGlobals:Attenuators:ng7att9
1861        Make/O/N=9 root:myGlobals:Attenuators:ng7att10
1862       
1863        //each wave has 8 elements, the transmission of att# at the wavelengths
1864        //lambda = 5,6,7,8,10,12,14,17
1865        Make/O/N=9 root:myGlobals:Attenuators:ng7lambda={5,6,7,8,10,12,14,17,20}
1866        root:myGlobals:Attenuators:ng7att0 = {1, 1, 1, 1, 1, 1, 1, 1 ,1}
1867        root:myGlobals:Attenuators:ng7att1 = {0.418, 0.393, 0.369, 0.347, 0.313, 0.291, 0.271, 0.244, 0.219 }
1868        root:myGlobals:Attenuators:ng7att2 = {0.189, 0.167, 0.148, 0.132, 0.109, 0.0945, 0.0830, 0.0681, 0.0560}
1869        root:myGlobals:Attenuators:ng7att3 = {0.0784, 0.0651, 0.0541, 0.0456, 0.0340, 0.0273, 0.0223, 0.0164 , 0.0121}
1870        root:myGlobals:Attenuators:ng7att4 = {0.0328, 0.0256, 0.0200, 0.0159, 0.0107, 7.98e-3, 6.14e-3, 4.09e-3 , 0.00274}
1871        root:myGlobals:Attenuators:ng7att5 = {0.0139, 0.0101, 7.43e-3, 5.58e-3, 3.42e-3, 2.36e-3, 1.70e-3, 1.03e-3 , 6.27e-4}
1872        root:myGlobals:Attenuators:ng7att6 = {5.90e-3, 4.07e-3, 2.79e-3, 1.99e-3, 1.11e-3, 7.13e-4, 4.91e-4, 2.59e-4 , 1.42e-4}
1873        root:myGlobals:Attenuators:ng7att7 = {1.04e-3, 6.37e-4, 3.85e-4, 2.46e-4, 1.16e-4, 6.86e-5, 4.10e-5, 1.64e-5 ,7.02e-6}
1874        root:myGlobals:Attenuators:ng7att8 = {1.90e-4, 1.03e-4, 5.71e-5, 3.44e-5, 1.65e-5, 6.60e-6, 3.42e-6, 1.04e-6 , 3.48e-7}
1875        root:myGlobals:Attenuators:ng7att9 = {3.58e-5, 1.87e-5, 1.05e-5, 7.00e-6, 2.35e-6, 6.35e-7, 2.86e-7, 6.61e-8 , 1.73e-8}
1876        root:myGlobals:Attenuators:ng7att10 = {7.76e-6, 4.56e-6, 3.25e-6, 2.03e-6, 3.34e-7, 6.11e-8, 2.39e-8, 4.19e-9, 8.60e-10}
1877       
1878End
1879
1880//returns the transmission of the attenuator (at NG3) given the attenuator number
1881//which must be an integer(to select the wave) and given the wavelength.
1882//the wavelength may be any value between 5 and 20 (A), and is interpolated
1883//between calibrated wavelengths for a given attenuator
1884Function LookupAttenNG3(lambda,attenNo)
1885        Variable lambda, attenNo
1886       
1887        Variable trans
1888        String attStr="root:myGlobals:Attenuators:ng3att"+num2str(trunc(attenNo))
1889        String lamStr = "root:myGlobals:Attenuators:ng3lambda"
1890       
1891        if(attenNo == 0)
1892                return (1)              //no attenuation, return trans == 1
1893        endif
1894       
1895        if( (lambda < 5) || (lambda > 20 ) )
1896                Abort "Wavelength out of calibration range (5,20). You must manually enter the absolute parameters"
1897        Endif
1898       
1899        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) )
1900                Execute "MakeNG3AttenTable()"
1901        Endif
1902        //just in case creating the tables fails....
1903        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) )
1904                Abort "Attenuator lookup waves could not be found. You must manually enter the absolute parameters"
1905        Endif
1906       
1907        //lookup the value by interpolating the wavelength
1908        //the attenuator must always be an integer
1909        Wave att = $attStr
1910        Wave lam = $lamstr
1911        trans = interp(lambda,lam,att)
1912       
1913//      Print "trans = ",trans
1914       
1915        return trans
1916End
1917
1918//returns the transmission of the attenuator (at NG7) given the attenuator number
1919//which must be an integer(to select the wave) and given the wavelength.
1920//the wavelength may be any value between 5 and 20 (A), and is interpolated
1921//between calibrated wavelengths for a given attenuator
1922//
1923// this set of tables is also used for NG5 (NG1) SANS instrument - as the attenuator has yet to be calibrated
1924Function LookupAttenNG7(lambda,attenNo)
1925        Variable lambda, attenNo
1926       
1927        Variable trans
1928        String attStr="root:myGlobals:Attenuators:ng7att"+num2str(trunc(attenNo))
1929        String lamStr = "root:myGlobals:Attenuators:ng7lambda"
1930       
1931        if(attenNo == 0)
1932                return (1)              //no attenuation, return trans == 1
1933        endif
1934       
1935        if( (lambda < 5) || (lambda > 20 ) )
1936                Abort "Wavelength out of calibration range (5,20). You must manually enter the absolute parameters"
1937        Endif
1938       
1939        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) )
1940                Execute "MakeNG7AttenTable()"
1941        Endif
1942        //just in case creating the tables fails....
1943        if(!(WaveExists($attStr)) || !(WaveExists($lamStr)) )
1944                Abort "Attenuator lookup waves could not be found. You must manually enter the absolute parameters"
1945        Endif
1946       
1947        //lookup the value by interpolating the wavelength
1948        //the attenuator must always be an integer
1949        Wave att = $attStr
1950        Wave lam = $lamstr
1951        trans = interp(lambda,lam,att)
1952       
1953        //Print "trans = ",trans
1954       
1955        return trans
1956
1957End
1958
1959//returns the proper attenuation factor based on the instrument (NG3, NG5, or NG7)
1960//NG5 values are taken from the NG7 tables (there is very little difference in the
1961//values, and NG5 attenuators have not been calibrated (as of 8/01)
1962//filestr as passed is textread[3], the default directory
1963Function AttenuationFactor(fileStr,lam,attenNo)
1964        String fileStr
1965        Variable lam,attenNo
1966       
1967        Variable attenFactor=1,loc
1968        String instr=fileStr[1,3]       //filestr is "[NGnSANSn] " or "[NGnSANSnn]" (11 characters total)
1969       
1970        strswitch(instr)
1971                case "NG3":
1972                        attenFactor = LookupAttenNG3(lam,attenNo)
1973                        break
1974                case "NG5":
1975                        //using NG7 lookup Table
1976                        attenFactor = LookupAttenNG7(lam,attenNo)
1977                        break
1978                case "NG7":
1979                        attenFactor = LookupAttenNG7(lam,attenNo)
1980                        break
1981                default:                                                       
1982                        //return error?
1983                        attenFactor=1
1984        endswitch
1985//      print "instr, lambda, attenNo,attenFactor = ",instr,lam,attenNo,attenFactor
1986        return(attenFactor)
1987End
1988
1989//*******************
1990//************
1991//   A simple panel to allow users of the NG1 SANS instrument to make their
1992// transmission files "look" like a transmission measurement to the data reduction
1993// program. Transmission files are designated by an x-position of the beamstop of
1994// < -5 (cm). this simple procedure sets xpos=-10 for individual files. files
1995// con be "un-converted", which set the xpos=0.
1996// 1/30/01 SRK
1997//************
1998
1999Proc TransformToTransFile()
2000        Variable/G root:myGlobals:gConvTrans=1
2001        DoWindow/F Convert_to_Trans
2002        if(V_flag==0)
2003                Convert2Trans()
2004        endif
2005End
2006
2007//fname must be the full path and name for the file
2008Function ChangeBSXPos(fname,xpos)
2009        String fname
2010        Variable xpos
2011       
2012        Variable start
2013        //x-position starts after byte 368
2014        //ReWriteReal() takes care of open/close on its own
2015        start = 368      //BS x-position
2016        ReWriteReal(fname,xpos,start)
2017        return(0)
2018End
2019
2020//sets the beamstop position to zero NOT the original position
2021Function UnConvertButtonProc(ctrlName) : ButtonControl
2022        String ctrlName
2023       
2024        //Print "UnConvert"
2025        String fullname=""
2026        ControlInfo fileNum
2027        Variable num = V_Value  //run number to find
2028       
2029        fullname = FindFileFromRunNumber(num)
2030        Print fullname
2031        //report error or change the file
2032        if(cmpstr(fullname,"")==0)
2033                Print "Unconvert - file not found"
2034        else
2035                //Print "Unconvert",fullname
2036                ChangeBSXPos(fullName,0)
2037        Endif
2038        return(0)
2039End
2040
2041//button procedure to do the conversion
2042// writes fake beamstop position to the data file
2043//
2044Function ConvertButtonProc(ctrlName) : ButtonControl
2045        String ctrlName
2046       
2047        String fullname=""
2048        ControlInfo fileNum
2049        Variable num = V_Value  //run number to find
2050       
2051        fullname = FindFileFromRunNumber(num)
2052        print fullname
2053        //report error or change the file
2054        if(cmpstr(fullname,"")==0)
2055                Print "Convert - file not found"
2056        else
2057                //Print "Convert",fullname
2058                ChangeBSXPos(fullName,-10)
2059        Endif
2060        return(0)
2061End
2062
2063//simple panel recreation macro
2064//
2065Proc Convert2Trans()
2066        PauseUpdate; Silent 1           // building window...
2067        NewPanel /W=(434,74,671,216) /K=1
2068        DoWindow/C Convert_to_Trans
2069        SetDrawLayer UserBack
2070        DrawText 18,22,"Convert files to be recognized"
2071        DrawText 43,39,"as transmission files"
2072        SetVariable fileNum,pos={43,58},size={130,15},title="File Number"
2073        SetVariable fileNum,limits={1,Inf,1},value=root:myGlobals:gConvTrans
2074        SetVariable fileNum,help={"Sets the run number of the file that is to be converted"}
2075        Button ConvButton,pos={73,79},size={80,20},proc=ConvertButtonProc,title="Convert"
2076        Button ConvButton,help={"Converts the chosen file to appear as a transmission file"}
2077        Button UndoButton,pos={68,105},size={90,20},proc=UnConvertButtonProc,title="Un-Convert"
2078        Button UndoButton,help={"Converts the chosen file to appear as a scattering file"}
2079EndMacro
2080
2081
2082/////
2083//  A quick way to fill all the T_EMP_Filenames with the selected empty beam file
2084// Note that you must go back and clear or reassign any files that are special cases
2085//
2086// IMPORTANT - this will overwrite any other file assignments (typically not a problem
2087// if you  are using only one wavelength)
2088//
2089/////
2090Function FillEMPFilenameWSelection()
2091
2092        GetSelection table,TransFileTable,3
2093        Variable row=V_startRow                 //ony need the selected row, we know the column names and location
2094        Wave/T fw = root:myGlobals:TransHeaderInfo:T_FileNames          //trans files
2095        Wave/T ew = root:myGlobals:TransHeaderInfo:T_EMP_FileNames              //empty beam reference files
2096       
2097        ew[] = fw[row]
2098       
2099        return(0)
2100End
2101
2102//given a selection of the sample files, try to find the corresponding transmission file
2103//
2104Function GuessTransFile(charsToUse,row)
2105        Variable charsToUse,row                 //number of characers to use from the beginning of the label, and the row of the sample file
2106       
2107//      GetSelection table,ScatterFileTable,3
2108//      Variable row=V_startRow                 //ony need the selected row, we know the column names and location
2109        Wave/T sw = root:myGlobals:TransHeaderInfo:S_Labels             //Sample file labels
2110        Wave/T tw = root:myGlobals:TransHeaderInfo:T_Labels             //trans file labels
2111        Wave/T tnam = root:myGlobals:TransHeaderInfo:T_FileNames        //trans file names
2112        Wave/T snam = root:myGlobals:TransHeaderInfo:S_TRANS_FileNames  //Scattering - trans correspondence
2113       
2114        Variable num,ii,found
2115        String samStr = "",testStr=""
2116       
2117        samStr = (sw[row])[0,charsToUse-1]              //string to try and match
2118        samStr += "*"
2119//      Print samStr
2120        num=numpnts(tw)
2121        ii=0
2122        do
2123                testStr=tw[ii]
2124                found = stringmatch(testStr, samStr )
2125                if(found == 1)
2126                        Print "Match Found at:  ",samStr,tnam[ii],tw[ii]
2127                        snam[row] = tnam[ii]                    //write the file name into the table
2128                        return(0)       
2129                endif
2130                ii+=1
2131        while(ii<num)
2132       
2133        //method 2
2134//      samStr = (sw[row])[0,charsToUse-1]              //string to try and match
2135////    Print samStr
2136//      num=numpnts(tw)
2137//      ii=0
2138//      do
2139//              testStr=tw[ii]
2140//              found = strsearch(testStr,samStr,0)             //start from zero, Need Igor 5 to allow 4th parameter to ignore case
2141//              if(found != -1)
2142//                      Print "Match Found at:  ",samStr,tnam[ii],tw[ii]
2143//                      snam[row] = tnam[ii]                    //write the file name into the table
2144//                      return(0)       
2145//              endif
2146//              ii+=1
2147//      while(ii<num)
2148       
2149        Print "No Match found"
2150        return(0)
2151End
2152
2153Function GuessAllTransFiles(numChars)
2154        Variable numChars
2155       
2156        if(WinType("ScatterFileTable") ==0 )
2157                return(1)
2158        endif
2159       
2160        Wave/T sw = root:myGlobals:TransHeaderInfo:S_Labels             //Sample file labels
2161        Variable ii,num=numpnts(sw)
2162       
2163        for(ii=0;ii<num;ii+=1)
2164                GuessTransFile(numChars,ii)
2165        endfor
2166       
2167        return(0)
2168End
2169
2170Function fGuessSelectedTransFiles(numChars)
2171        Variable numChars
2172       
2173        Variable ii
2174        Variable scatterTableExists
2175        scatterTableExists = WinType("ScatterFileTable")
2176        if (scatterTableExists != 0)
2177                GetSelection table,ScatterFileTable,1   
2178                for(ii=V_StartRow;ii<=V_EndRow;ii+=1)
2179                        GuessTransFile(numChars,ii)
2180                endfor
2181        else
2182                Print "Transmission->Scattering File table is not open"
2183        endif
2184        return(0)
2185End
2186
2187Function ClearSelectedAssignments()
2188       
2189        String winStr = WinList("*", ";", "WIN:" )              //returns the target window
2190        Variable scatterTableExists, transTableExists
2191        Print winStr
2192        scatterTableExists = cmpstr(winStr,"ScatterFileTable;")
2193        if (scatterTableExists == 0)
2194                GetSelection table,ScatterFileTable,1   
2195                fClearSelectedAssignments(V_startRow,V_endRow,1)
2196        endif
2197       
2198        transTableExists = cmpstr(winStr,"TransFileTable;")
2199        if (transTableExists == 0)
2200                GetSelection table,TransFileTable,1     
2201                fClearSelectedAssignments(V_startRow,V_endRow,2)
2202        endif
2203       
2204        return(0)
2205End
Note: See TracBrowser for help on using the repository browser.