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

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

more file adjustments to clean out NCNR bits

File size: 75.3 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 detector. For comparison with the "box" trans
1198// updated: 5/11/2006 by Bryan Greenwald
1199Function CalcWholeTrans(startRow,endRow)
1200        Variable startRow,endRow
1201//      GetSelection table,ScatterFileTable,1
1202
1203        NVAR pixelsX = root:myGlobals:gNPixelsX
1204        NVAR pixelsY = root:myGlobals:gNPixelsY
1205       
1206        String filename
1207        Wave/T T_EMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
1208       
1209        Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
1210        Wave T_GTransmission = $"root:myGlobals:TransHeaderInfo:T_Transmission"
1211       
1212//      Wave/T S_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
1213//      Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
1214//      Wave S_GTransmission =  $"root:myGlobals:TransHeaderInfo:S_Transmission"
1215        Wave GWhole= $"root:myGlobals:TransHeaderInfo:T_Whole"
1216       
1217        Variable num_t_files, ii, jj
1218        Variable refnum, transCts, emptyCts,attenRatio,lambda,trans
1219        Variable x1,x2,y1,y2,err,attenEmp,attenSam
1220        String suffix = "",pathName,textStr,abortStr,emptyFile,transFile,samFileStr
1221        String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
1222        String strToExecute
1223       
1224        num_t_files = numpnts(T_GFilenames)
1225       
1226        PathInfo catPathName
1227        pathname = S_path
1228
1229        ii= startRow
1230        do
1231                if (cmpstr(T_Emp_Filenames[ii],"")!=0)  //if there is a sample trans file assigned
1232                        jj=0
1233                        do
1234                                if (cmpstr(T_Emp_Filenames[ii],T_GFilenames[jj])==0)    //find it in the trans file table
1235                                        if (cmpstr(T_EMP_Filenames[ii],"")!=0)          //if empty beam file assigned, proceed with the calculation
1236                                                //full path+name to access all 3 files
1237                                                filename=pathname+T_GFilenames[ii]
1238                                                emptyFile = pathname+T_EMP_Filenames[ii]
1239                                                transFile = pathname+T_GFilenames[ii]
1240                                                //////////
1241                                                // check the empty beam file for previously selected coordinates
1242                                                //if they exist, set the xy string , save the normalized counts somewhere
1243                                                //the value was written to an unused r*4 header analysis.factor (@b494)
1244                                                Open/R refnum as emptyFile
1245                                                FSetPos refnum,478
1246                                                FBinRead/F=3/B=3 refnum, x1
1247                                                FBinRead/F=3/B=3 refnum, x2
1248                                                FBinRead/F=3/B=3 refnum, y1
1249                                                FBinRead/F=3/B=3 refnum, y2
1250                                                Close refnum
1251                                                //read the real count value
1252                                                strToExecute = GBLoadStr + "/S=494/U=1" + "\"" + emptyFile + "\""
1253                                                Execute strToExecute
1254                                                Wave w=$"tempGBWave0"
1255                                        //      Variable/G root:myGlobals:Patch:gTransCts = w[0]
1256                                                emptyCts = w[0]
1257                                                // read the attenuator number of the empty beam file
1258                                                strToExecute = GBLoadStr + "/S=39/U=4" + "\"" + emptyFile + "\""
1259                                                Execute strToExecute
1260                                                attenEmp = w[3]
1261                                                //
1262                                                if( ((x1-x2)==0) || ((y1-y2)==0) )              //zero width marquee in either direction
1263                                                        //no region selected    -- prompt user to select box w/marquee
1264                                                        DoWindow/F TransFileTable
1265                                                        Abort "Use \"Set BCENT File\", then \"SetXYBox\" to select XY range in empty beam file "+T_EMP_Filenames[jj]
1266                                                        //from here the Marquee menu selection must handle the task of setting the box and updating the information in the file header
1267                                                Endif
1268               
1269                                                //read in trans file then add to SAM
1270                                                ReadHeaderAndData(transFile)
1271                                                //adds to SAM
1272                                                err = Raw_to_work("SAM")
1273                                                //sum region in SAM
1274                                                transCts =  SumCountsInBox(0,pixelsX-1,0,pixelsY-1,"SAM")       
1275                                                // get the attenuator, lambda, and sample string (to get the instrument)
1276                                                WAVE/T samText = $"root:SAM:textRead"
1277                                                WAVE samReals = $"root:SAM:realsRead"
1278                                                samfileStr = samText[3]
1279                                                lambda = samReals[26]
1280                                                attenSam = samReals[3]
1281                                                //calculate the ratio of attenuation factors - assumes that same instrument used for each, AND same lambda
1282                                                AttenRatio = AttenuationFactor(samFileStr,lambda,attenEmp)/AttenuationFactor(samFileStr,lambda,attenSam)
1283                                                //calculate trans based on empty beam value and rescale by attenuation ratio
1284                                                trans= transCts/emptyCts * AttenRatio
1285                                               
1286                                                //write out counts and transmission to history window, showing the attenuator ratio, if it is not unity
1287                                                If(attenRatio==1)
1288                                                        //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
1289                                                        Printf "%s\t\tTotal Counts = %g\t Trans using whole detector = %g\t",T_GFilenames[ii], transCts,trans
1290                                                else
1291                                                        //Printf "%s\t\tTrans Counts = %g\tTrans = %g\tAttenuatorRatio = %g, Tbox/Twhole = %g\r",T_GFilenames[ii], transCts,trans,attenRatio, T_GTransmission[ii]/trans
1292                                                        Printf "%s\t\tTotal Counts = %g\t Trans using whole detector = %g\t AttenuatorRatio = %g",T_GFilenames[ii], transCts,trans,attenRatio
1293                                                endif
1294                                                If(T_GTransmission[ii]/trans > 0.97)
1295                                                        printf " Tbox/Twhole = %g\r",T_GTransmission[ii]/trans
1296                                                else
1297                                                        printf " !!! Tbox/Twhole is low !!! = %g\r",T_GTransmission[ii]/trans
1298                                                endif
1299                                                //write the trans to the file header of the raw data (open/close done in function)
1300                                                ReWriteReal(filename,trans,392)         //WholeTrans start byte is 392
1301                                               
1302                                                //then update the global that is displayed
1303                                                GWhole[ii] = trans
1304                                               
1305                                        else  // There is no empty assigned
1306                                                abortStr = "Empty beam file not assigned properly for " + T_GFilenames[jj]
1307                                                Print abortStr
1308                                                //Abort abortStr
1309                                                return(1)
1310                                        endif
1311                                endif
1312                                jj+=1
1313                        while(jj<num_t_files)
1314                else //no transmission file
1315                        abortStr = "Transmission beam file not assigned properly for " + T_GFilenames[ii]
1316                        Print abortStr
1317                endif
1318                ii+=1
1319        while(ii<=endRow)
1320        print "done"
1321        return(0)
1322End
1323
1324
1325//window recreation macro for the Trans Panel
1326//
1327Proc Trans_Panel()
1328        PauseUpdate; Silent 1           // building window...
1329        NewPanel /W=(173,197,540,424)/K=1 as "Calculate Transmissions"
1330        DoWindow/C Trans_Panel
1331        ModifyPanel cbRGB=(49807,47186,38011)
1332        ModifyPanel fixedSize=1
1333        SetDrawLayer UserBack
1334        DrawLine 0,56,368,56
1335        DrawLine 0,125,368,125
1336        Button pick_emp,pos={4,65},size={105,20},proc=PickEMPTransButton,title="set BCENT file"
1337        Button pick_emp,help={"This button will set the file selected in the Transmission file table to be the empty beam file."}
1338        SetVariable empStr,pos={114,67},size={250,17},title="file:"
1339        SetVariable empStr,help={"Filename of the empty beam file(s) to be used in the transmission calculation"}
1340        SetVariable empStr,fSize=10
1341        SetVariable empStr,limits={-Inf,Inf,0},value= root:myGlobals:TransHeaderInfo:gEMP
1342        Button Trn_button_1,pos={5,101},size={90,20},proc=Trn_SetXYBoxButton,title="Set XY Box"
1343        Button Trn_button_1,help={"Sets the XY box to sum over"}
1344        Button Trn_button_2,pos={174,139},size={110,20},proc=Trn_SortFilesByDate,title="Sort by Date"
1345        Button Trn_button_2,help={"Sort the scattering and transmission files by creation date."}
1346        Button Trn_button_3,pos={174,169},size={110,20},proc=Trn_SortFilesByLabel,title="Sort by Label"
1347        Button Trn_button_3,help={"Sort the scattering and transmission files by label."}
1348        Button Trn_button_4,pos={295,139},size={67,20},proc=Trn_ShowHelpProc,title="Help"
1349        Button Trn_button_4,help={"Show a help notebook for calculating transmissions."}
1350        SetVariable Trn_setvar_1,pos={135,100},size={227,17},title="Box is "
1351        SetVariable Trn_setvar_1,help={"Box coordinates to sum over"},fSize=10
1352        SetVariable Trn_setvar_1,limits={-Inf,Inf,0},value= root:myGlobals:TransHeaderInfo:gBox
1353        Button Trn_button_0,pos={1,1},size={70,20},proc=Trn_PickPathButton,title="Pick Path"
1354        Button Trn_button_0,help={"Select the folder containing the SANS data files"}
1355        Button Trn_button_9,pos={335,1},size={25,20},proc=ShowTransHelp,title="?"
1356        Button Trn_button_9,help={"Show the help file for calculating sample transmissions"}
1357        SetVariable Trn_setvar_0,pos={80,4},size={250,17},title="Path"
1358        SetVariable Trn_setvar_0,help={"Currently selected data path"},fSize=10
1359        SetVariable Trn_setvar_0,limits={-Inf,Inf,0},value= root:myGlobals:TransHeaderInfo:gCatPathStr
1360        Button Trn_button_5,pos={5,169},size={161,20},proc=Trn_CalcAllFilesButton,title="Calculate All Files"
1361        Button Trn_button_5,help={"Calculate transmission and patch headers of ALL files in the Scattering File Table."}
1362        Button Trn_button_6,pos={295,198},size={67,20},proc=Trn_PanelDoneButtonProc,title="Done"
1363        Button Trn_button_6,help={"Close the panel when done calculating transmissions"}
1364        Button Trn_button_7,pos={67,32},size={214,20},proc=Trn_RefreshProc,title="List Files"
1365        Button Trn_button_7,help={"Generate or refresh the tables of files."}
1366        Button Trn_button_8,pos={5,139},size={161,20},proc=Trn_CalcSelectedFilesButton,title="Calculate Selected Files"
1367        Button Trn_button_8,help={"Calculate transmission and patch headers of selected files in the Scattering File Table."}
1368        Button Trn_button_10,pos={5,198}, size={161,20},proc=TotalTransButtonProc
1369        Button Trn_button_10 title="Calculate Total Trans"
1370        Button Trn_button_10 help={"Calculate transmission over the whole detector and patch headers of ALL files in the data folder."}
1371EndMacro
1372
1373
1374Function TotalTransButtonProc(ctrlName) : ButtonControl
1375        String ctrlName
1376        TotalTrans(ctrlName)
1377End
1378
1379
1380Proc ShowTransHelp(ctrlName) : ButtonControl
1381        String ctrlName
1382        DisplayHelpTopic/K=1 "SANS Data Reduction Tutorial[Calculate Transmissions]"
1383End
1384
1385
1386//generates the file tables of scattering files and transmission files
1387//
1388// if the tables already exist, they will be updated
1389//
1390Proc Trn_RefreshProc(ctrlName) : ButtonControl
1391        String ctrlName
1392        BuildFileTables()
1393End
1394
1395//shows a noteboox of help text describing how to calculate transmissions
1396// - better that the user read the igor or pdf file, but it's a victory to get
1397//users to read anything at all
1398//
1399Proc Trn_ShowHelpProc(ctrlName) : ButtonControl
1400        String ctrlName
1401        DisplayTransHelp()
1402End
1403
1404//sorts the tables by date
1405//
1406Proc Trn_SortFilesByDate(ctrlName) : ButtonControl
1407        String ctrlName
1408        SortTransByDate()
1409End
1410
1411//sorts the tables alphabetically by the sample label field
1412//
1413Proc Trn_SortFilesByLabel(ctrlName) : ButtonControl
1414        String ctrlName
1415        SortTransByLabel()
1416End
1417
1418//button action procedure to select the local path to the
1419//folder containing the RAW SANS data files
1420// - once the data folder is selected, all of the popup file menus are
1421//forced to update with fresh file lists
1422//
1423Function Trn_PickPathButton(PathButton) : ButtonControl
1424        String PathButton
1425       
1426        //set the global string to the selected pathname
1427        Variable err
1428        err = PickPath()
1429        if(err)
1430                return(1)               //some problem with path or user cancelled, get out
1431        endif
1432       
1433        PathInfo/S catPathName
1434        String path = S_path
1435        if (V_flag == 0)
1436                //path does not exist - no folder selected
1437                String/G root:myGlobals:TransHeaderInfo:gCatPathStr = "no folder selected"
1438        else
1439                String/G root:myGlobals:TransHeaderInfo:gCatPathStr = path
1440        endif
1441       
1442        //Update the pathStr variable box
1443        ControlUpdate/W=Trans_Panel $"Trn_setvar_0"
1444        //Generate tables
1445        Execute "BuildFileTables()"
1446       
1447End
1448
1449//button action function that asks the user to set the XY box to sum over
1450//for all transmission calculations
1451//
1452// requires the user to set the box coordinates with the marquee popup
1453//that wil properly set the keyword string, and write the values to the empty beam header
1454//
1455Function Trn_SetXYBoxButton(ctrlName) : ButtonControl
1456        String ctrlName
1457
1458        String msgStr = "Select the Empty Beam File"
1459        String filename=""
1460       
1461        //get the filename from the popup menu
1462        //and construct a full path to it
1463        SVAR partialName = root:myGlobals:TransHeaderInfo:gEMP
1464        print partialName
1465        //get a valid file based on this partialName and catPathName
1466        String tempName = FindValidFilename(partialName)
1467        if(cmpstr(tempName,"")==0)
1468                //file not found, get out
1469                Abort "Empty beam file not found Trn_SetXYBoxButton(ctrlName)"
1470        Endif
1471        //name is ok, prepend path to tempName for read routine
1472        PathInfo catPathName
1473        filename = S_path + tempName
1474       
1475        //read the file in - check for previous coordinates
1476        ReadHeaderAndData(filename)
1477        //data is displayed here (go through the normal display steps, so all is created properly
1478        String/G root:myGlobals:gDataDisplayType="RAW"
1479        fRawWindowHook()
1480       
1481        // check the empty beam file for previously selected coordinates
1482        //if they exist, set the xy string , save the normalized counts somewhere
1483        //the value was written to an unused r*4 header analysis.factor (@b494)
1484        Variable refnum,x1,x2,y1,y2,err
1485        Open/R refnum as filename
1486        FSetPos refnum,478
1487        FBinRead/F=3/B=3 refnum, x1
1488        FBinRead/F=3/B=3 refnum, x2
1489        FBinRead/F=3/B=3 refnum, y1
1490        FBinRead/F=3/B=3 refnum, y2
1491        Close refnum
1492        //read the real count value
1493        String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
1494        String strToExecute
1495        strToExecute = GBLoadStr + "/S=494/U=1" + "\"" + filename + "\""
1496        Execute strToExecute
1497        Wave w=$"tempGBWave0"
1498        Variable/G root:myGlobals:gTransCts = w[0]                      //***NOTE this is NOT in the Trans sub-folder
1499        //
1500        if( ((x1-x2)==0) || ((y1-y2)==0) )              //zero width marquee in either direction
1501                //no region selected
1502               
1503                //add the empty beam file to work.SAM
1504                err = Raw_to_work("SAM")
1505       
1506                //the calling macro must change the display type
1507                String/G root:myGlobals:gDataDisplayType="SAM"          //displayed data type is sam
1508       
1509                //data is displayed here
1510                fRawWindowHook()
1511       
1512                //prompt user to select box w/marquee
1513                DoAlert 0,"Select the region to sum with the Marquee"
1514       
1515                //from here the Marquee menu selection must handle the task of setting the box
1516                //and updating the information in the file header
1517        else
1518                // region already selected, just put up the values from the file header
1519                //allow user the option of overriding the current box region
1520                msgStr = "X1="+num2str(x1)+";"
1521                msgStr += "X2="+num2str(x2)+";"
1522                msgStr += "Y1="+num2str(y1)+";"
1523                msgStr += "Y2="+num2str(y2)+";"
1524                String textStr
1525                textStr = "Override current box  "+msgStr+" ?"
1526                DoAlert 1,textStr
1527                If((V_flag)==1)
1528                        //get new box coordinates, same procedure as above
1529                        //add the empty beam file to work.SAM
1530                        err = Raw_to_work("SAM")
1531       
1532                        //the calling macro must change the display type
1533                        String/G root:myGlobals:gDataDisplayType="SAM"          //displayed data type is sam
1534       
1535                        //data is displayed here
1536                        fRawWindowHook()
1537       
1538                        //prompt user to select box w/marquee
1539                        DoAlert 0,"Select the region to sum with the Marquee"
1540       
1541                        //from here the Marquee menu selection must handle the task of setting the box
1542                        //and updating the information in the file header
1543                else
1544                        String/G root:myGlobals:TransHeaderInfo:gBox = msgStr
1545                Endif
1546        Endif
1547       
1548        UpdateBoxCoordinates()
1549        KillWaves/Z w
1550        Return (0)
1551End
1552
1553
1554//button action function that assigns the selected empty beam file (run number only)
1555//and the sample trnansmissionfile (run number only) to the popup list of  sample scattering
1556//files (ALL of the list items). Assignment is done by writing the integer run numbers of
1557//trans files to the header of each of the  scattering files, in a batchwise processing
1558//of the popup list
1559//
1560//transmission are not calculated here - only the file numbers are set
1561//
1562//*********unused***********
1563//Function Trn_AssignAllFilesButton(ctrlName) : ButtonControl
1564//      String ctrlName
1565//
1566//      AssignTransFilesToData()
1567//             
1568//End
1569
1570//button action procedure to calculate the transmission of all of the sample files in the
1571//sample popup list, based on the information in EACH sample header and in the empty beam file
1572//the box must be set properly in the empty beam file, AND the empty and trans
1573//run number must also be set properly in EACH scattering file,
1574//If run numbers are not properly assigned, no trans is calculated and the
1575//header is not modified
1576//
1577//The RAW data header of EACH sample file IS modified, with the newly calculated transmission
1578//
1579//not very friendly, but forces users to assign the files before continuing
1580//
1581Function Trn_CalcAllFilesButton(ctrlName) : ButtonControl
1582        String ctrlName
1583
1584        //calculate the transmission and Patch the header (if possible)
1585        Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
1586        Variable num_s_files = numpnts(S_GFilenames)
1587        AssignSelTransFilesToData(0,num_s_files-1)              //do all the files (points 0 to n-1, inclusive)
1588        CalcSelTransFromHeader(0,num_s_files-1)
1589       
1590End
1591
1592//for selected scattering files in the scattering table,
1593// the "Annn" association of trans file is written to the data file,
1594// the the transmission is actually calculated
1595//
1596Function Trn_CalcSelectedFilesButton(ctrlName) : ButtonControl
1597        String ctrlName
1598 
1599   Variable scatterTableExists
1600   scatterTableExists = WinType("ScatterFileTable")
1601   if (scatterTableExists != 0)
1602                GetSelection table,ScatterFileTable,1
1603                if(V_Flag != 0)
1604                        AssignSelTransFilesToData(V_StartRow,V_EndRow)
1605                        //calculate the transmission and Patch the header (if possible)
1606                        CalcSelTransFromHeader(V_StartRow,V_EndRow)
1607                Else
1608                        DoAlert 0,"No selection from Scattering Files table"
1609                Endif
1610   Else
1611                DoAlert 0,"No file selected from Scattering Files table or no Scattering Files table available"
1612   Endif
1613End
1614
1615//
1616// I am not sure what the difference is inthe function of CalcTotalTrans and CalcWholeTrans ?
1617// do they really do anything different?
1618// is it a useful calculation at all?
1619//
1620Function TotalTrans(ctrlName) : ButtonControl
1621        String ctrlName
1622 
1623   Variable transTableExists
1624   transTableExists = WinType("TransFileTable")
1625   if (transTableExists != 0)
1626                GetSelection table,transFileTable,1
1627                if(V_Flag != 0)
1628                        AssignTotalTransFilesToData(V_StartRow,V_EndRow)
1629                        //calculate the transmission and Patch the header (if possible)
1630                        CalcTotalTrans(V_StartRow,V_EndRow)
1631                        CalcWholeTrans(V_StartRow,V_EndRow)
1632                Else
1633                        DoAlert 0,"No selection from Trans Files table"
1634                Endif
1635   Else
1636                DoAlert 0,"No file selected from Trans Files table or no Trans Files table available"
1637   Endif
1638End
1639
1640
1641//simple button procedure to close the trans panel
1642// - automatically kills the two tables as well
1643//
1644Function Trn_PanelDoneButtonProc(ctrlName) : ButtonControl
1645        String ctrlName
1646
1647        // this button will make sure all files are closed
1648        //and close the panel
1649       
1650        Close/A
1651        DoWindow/K Trans_Panel
1652        DoWindow/K ScatterFileTable
1653        DoWindow/K TransFileTable
1654End
1655
1656
1657//function to check the header of a raw data file (full path specified by fname)
1658//checks the field of the x-position of the beamstop during data collection
1659//if the x-position is more negatice (farther to the left) than xTol(input)
1660//the the beamstop is "out" and the file is a transmission run and not a scattering run
1661//xtol typically set at -5 (cm) - trans runs have bs(x) at -10 to -15 cm
1662// function returns 1 if beamstop is out, 0 if beamstop is in
1663//
1664Function CheckIfBeamstopOut(fName,xTol)
1665        String fname
1666        Variable xTol
1667       
1668        Variable refnum,xpos
1669        //pos = 369, read one real value
1670       
1671        SetDataFolder root:
1672        String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
1673        String strToExecute=""
1674        // 1 R*4 value
1675        strToExecute = GBLoadStr + "/S=368/U=1" + "\"" + fname + "\""
1676        Execute strToExecute
1677        Wave w=$"root:tempGBWave0"
1678        xPos = w[0]
1679        KillWaves/Z w
1680        //Print "xPos = ",xpos
1681       
1682        if(xpos<=xTol)
1683                //xpos is farther left (more negative) than xtol (currently -5 cm)
1684                Return(1)
1685        else
1686                //some other file
1687                Return(0)
1688        Endif
1689End
1690
1691//function to update the box coordinates of the file selected as the
1692//empty beam file - takes the file that is currently popped from the list
1693//reads the 4 "analysis" integers that hold the box coordinates
1694//resets the globals string that is displayed with the new values
1695//should be called whenever the "empty" popup is popped, to ensure
1696//that current header information is displayed
1697//
1698Function UpdateBoxCoordinates()
1699
1700        //construct a full name, and read in the label from the file
1701        //and set the global
1702        String textstr=""
1703        ControlInfo empStr
1704        SVAR item = root:myGlobals:TransHeaderInfo:gEMP
1705        String tempName = FindValidFilename(item)
1706        if(cmpstr(tempName,"")==0)
1707                //file not found, get out
1708                Abort "Empty beam file not found UpdateBoxCoordinates(ctrlName)"
1709        Endif
1710        //name is ok, prepend path to tempName for read routine
1711        PathInfo catPathName
1712        String filename = S_path + tempName
1713       
1714        Variable refnum,x1,x2,y1,y2,err
1715        Open/R refnum as filename
1716        FSetPos refnum,478
1717        FBinRead/F=3/B=3 refnum, x1
1718        FBinRead/F=3/B=3 refnum, x2
1719        FBinRead/F=3/B=3 refnum, y1
1720        FBinRead/F=3/B=3 refnum, y2
1721        Close refnum
1722       
1723        //and update the global string
1724        String msgStr=""
1725        msgStr = "X1="+num2str(x1)+";"
1726        msgStr += "X2="+num2str(x2)+";"
1727        msgStr += "Y1="+num2str(y1)+";"
1728        msgStr += "Y2="+num2str(y2)+";"
1729       
1730        String/G root:myGlobals:TransHeaderInfo:gBox = msgStr
1731       
1732        ControlUpdate/W=Trans_panel Trn_setvar_1
1733End
1734
1735//crude procedure to display a notebook of help information for users
1736//
1737Proc DisplayTransHelp()
1738        String nb = "Notebook0"
1739        NewNotebook/N=$nb/F=1/V=1/W=(342,302,868,674) as "Notebook0:Transmission Help"
1740        Notebook $nb defaultTab=36, statusWidth=238, pageMargins={36,72,36,72}
1741        Notebook $nb showRuler=1, rulerUnits=1, updating={1, 3600}
1742        Notebook $nb newRuler=Normal, justification=0, margins={0,0,468}, spacing={0,0,0}, tabs={}, rulerDefaults={"Geneva",10,0,(0,0,0)}
1743        Notebook $nb ruler=Normal; Notebook $nb  justification=1, fSize=14, fStyle=1, text="Transmission Help\r"
1744        Notebook $nb ruler=Normal, fSize=-1, fStyle=-1, text="\r"
1745        Notebook $nb text="This panel allows quick and easy calculation of sample transmission. The neutron transmission of a sampl"
1746        Notebook $nb text="e must be calculated and entered into the header of each sample scattering file before proper background"
1747        Notebook $nb text=" corrections or absolute scaling can be done.\r"
1748        Notebook $nb text="\r"
1749        Notebook $nb text="During data collection, you measured an empty beam  - that is a measurement with heavy attenuation of th"
1750        Notebook $nb text="e neutron beam and the beamstop mover out of line with the direct beam. Then without changing the attenu"
1751        Notebook $nb text="ation or replacing the beamstop, you put a sample in the beam, and repeated the measurement (a sample tr"
1752        Notebook $nb text="ansmission measurement). The neutron transmission of the sample is simply the ratio of the number of neu"
1753        Notebook $nb text="tron counts from the sample transmission measurement normalized by the number of neutron counts from the"
1754        Notebook $nb text=" empty beam measurement. In this way, each sample transmission file is \"linked\" to the same empty beam t"
1755        Notebook $nb text="ransmission file.\r"
1756        Notebook $nb text="\r"
1757        Notebook $nb text="This calculated transmission value must be entered into the header of the sample scattering measurement "
1758        Notebook $nb text="- that is the measurement of the sample with the beamstop covering the primary (transmitted) beam, and n"
1759        Notebook $nb text="o (or few) attenuators in place. In this way, the sample transmission file is \"linked\" to its correspond"
1760        Notebook $nb text="ing sample scattering file. This panel allows you to set up the links and calculate the transmission.Tra"
1761        Notebook $nb text="nsmisison values are automatically patched to the scattering file headers as they are calculated.\r"
1762        Notebook $nb text="\r"
1763        Notebook $nb text="To Calculate Transmissions:\r"
1764        Notebook $nb text="\r"
1765        Notebook $nb text="1) Click \"List Files\" to get two lists (tables) - one of all the sample scattering files in the folder, "
1766        Notebook $nb text="and one of the transmission files.\r"
1767        Notebook $nb text="\r"
1768        Notebook $nb text="2) Select the region of the detector to sum over from the empty beam transmission file. Do theis by clic"
1769        Notebook $nb text="king on the filename of the empty beam transmision file (from the TransmissionFiles window, in the blue "
1770        Notebook $nb text="\"T_Filenames\" column). Return to the panel, and click the \"set EMP file\" button\", and the filename shoul"
1771        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"
1772        Notebook $nb text="et XY Box\" button. The empty beam file will be displayed, and you will be instructed to select the regio"
1773        Notebook $nb text="n to sum with the marquee. On the data, click and drag a rectangle that encompasses the primary beam. Mo"
1774        Notebook $nb text="ve the cursor inside the selection, to get an \"upside-down hat\" cursor. Click to get a menu, and near th"
1775        Notebook $nb text="e bottom, select \"Set XY Box Coords\". The pixel values should be updated to the Transmission panel, and "
1776        Notebook $nb text="are written to the empty beam header for future calculations. Note that the marquee selection can also b"
1777        Notebook $nb text="e used to measure the beam center, or centroid of any selected region.\r"
1778        Notebook $nb text="\r"
1779        Notebook $nb text="3) Now you need to \"link\" the ", fStyle=2, text="sample", fStyle=-1, text=" transmission files to the "
1780        Notebook $nb fStyle=2, text="empty", fStyle=-1, text=" ", fStyle=2, text="beam", fStyle=-1
1781        Notebook $nb text=" transmission file. Do this in the TransmissionFiles window by selecting the filename (in the blue T_Fil"
1782        Notebook $nb text="enames column) and pasing it to the corresponding row(s) in the T_EMP_Filenames column. This links the e"
1783        Notebook $nb text="mpty beam transmission with the sample transmission file. Do this for every sample transmission file.\r"
1784        Notebook $nb text="\r"
1785        Notebook $nb text="4) Now you need to link the sample ", fStyle=2, text="transmission", fStyle=-1
1786        Notebook $nb text=" file to the sample ", fStyle=2, text="scattering", fStyle=-1
1787        Notebook $nb text=" file. Do this by selecting the name of the transmission file (from the blue T_Filenames column) and pas"
1788        Notebook $nb text="ting it into the corresponding row of the S_TRANS_Filenames column of the ScatteringFiles window. This l"
1789        Notebook $nb text="inks the scattering file to its corresponding sample transmission file. This for all of the sample scatt"
1790        Notebook $nb text="ering files.\r"
1791        Notebook $nb text="\r"
1792        Notebook $nb text="5) Calculate the transmissions (and automatically write the transmission value to the satterng file head"
1793        Notebook $nb text="er) by clicking \"Calculate All Files\" from the Transmission Panel. The results will be printed to the co"
1794        Notebook $nb text="mmand window at the bottom of the screen. Any improperly assigned files will be listed. For example, blo"
1795        Notebook $nb text="cked beam scattering files will return an error, since the transmssion known to be zero, and is not meas"
1796        Notebook $nb text="ured (and no files are \"linked\"). Rather than calculating the transmission of all of the files,  a range"
1797        Notebook $nb text=" of S_Filenames can be selected, then calculated by clicking \"Calculate Selected Files\".\r"
1798        Notebook $nb text="\r"
1799        Notebook $nb text="By default the lists are sorted by run number (= chronological). In some cases it may be easier to sort "
1800        Notebook $nb text="by the sample label to group several of the same sample scattering files collected at different sample-t"
1801        Notebook $nb text="o-detector distances. Its utility depends, of course, on how consistent and unique your sample labels ar"
1802        Notebook $nb text="e.\r"
1803        Notebook $nb text="\r"
1804        Notebook $nb text="The \"links\" are stored in the corresponding files, and wil be re-generated when the lists are re-generat"
1805        Notebook $nb text="ed. Unassigned links will appear as blank elements in the T_EMP_Filenames or S_TRANS_Filenames columns.\r"
1806        Notebook $nb text="\r"
1807        Notebook $nb, selection={startOfFile,startOfFile}
1808        Notebook $nb text="\r"
1809End
1810
1811
1812
1813//*******************
1814//************
1815//   A simple panel to allow users of the NG1 SANS instrument to make their
1816// transmission files "look" like a transmission measurement to the data reduction
1817// program. Transmission files are designated by an x-position of the beamstop of
1818// < -5 (cm). this simple procedure sets xpos=-10 for individual files. files
1819// con be "un-converted", which set the xpos=0.
1820// 1/30/01 SRK
1821//************
1822
1823Proc TransformToTransFile()
1824        Variable/G root:myGlobals:gConvTrans=1
1825        DoWindow/F Convert_to_Trans
1826        if(V_flag==0)
1827                Convert2Trans()
1828        endif
1829End
1830
1831//fname must be the full path and name for the file
1832Function ChangeBSXPos(fname,xpos)
1833        String fname
1834        Variable xpos
1835       
1836        Variable start
1837        //x-position starts after byte 368
1838        //ReWriteReal() takes care of open/close on its own
1839        start = 368      //BS x-position
1840        ReWriteReal(fname,xpos,start)
1841        return(0)
1842End
1843
1844//sets the beamstop position to zero NOT the original position
1845Function UnConvertButtonProc(ctrlName) : ButtonControl
1846        String ctrlName
1847       
1848        //Print "UnConvert"
1849        String fullname=""
1850        ControlInfo fileNum
1851        Variable num = V_Value  //run number to find
1852       
1853        fullname = FindFileFromRunNumber(num)
1854        Print fullname
1855        //report error or change the file
1856        if(cmpstr(fullname,"")==0)
1857                Print "Unconvert - file not found"
1858        else
1859                //Print "Unconvert",fullname
1860                ChangeBSXPos(fullName,0)
1861        Endif
1862        return(0)
1863End
1864
1865//button procedure to do the conversion
1866// writes fake beamstop position to the data file
1867//
1868Function ConvertButtonProc(ctrlName) : ButtonControl
1869        String ctrlName
1870       
1871        String fullname=""
1872        ControlInfo fileNum
1873        Variable num = V_Value  //run number to find
1874       
1875        fullname = FindFileFromRunNumber(num)
1876        print fullname
1877        //report error or change the file
1878        if(cmpstr(fullname,"")==0)
1879                Print "Convert - file not found"
1880        else
1881                //Print "Convert",fullname
1882                ChangeBSXPos(fullName,-10)
1883        Endif
1884        return(0)
1885End
1886
1887//simple panel recreation macro
1888//
1889Proc Convert2Trans()
1890        PauseUpdate; Silent 1           // building window...
1891        NewPanel /W=(434,74,671,216) /K=1
1892        DoWindow/C Convert_to_Trans
1893        SetDrawLayer UserBack
1894        DrawText 18,22,"Convert files to be recognized"
1895        DrawText 43,39,"as transmission files"
1896        SetVariable fileNum,pos={43,58},size={130,15},title="File Number"
1897        SetVariable fileNum,limits={1,Inf,1},value=root:myGlobals:gConvTrans
1898        SetVariable fileNum,help={"Sets the run number of the file that is to be converted"}
1899        Button ConvButton,pos={73,79},size={80,20},proc=ConvertButtonProc,title="Convert"
1900        Button ConvButton,help={"Converts the chosen file to appear as a transmission file"}
1901        Button UndoButton,pos={68,105},size={90,20},proc=UnConvertButtonProc,title="Un-Convert"
1902        Button UndoButton,help={"Converts the chosen file to appear as a scattering file"}
1903EndMacro
1904
1905
1906/////
1907//  A quick way to fill all the T_EMP_Filenames with the selected empty beam file
1908// Note that you must go back and clear or reassign any files that are special cases
1909//
1910// IMPORTANT - this will overwrite any other file assignments (typically not a problem
1911// if you  are using only one wavelength)
1912//
1913/////
1914Function FillEMPFilenameWSelection()
1915
1916        GetSelection table,TransFileTable,3
1917        Variable row=V_startRow                 //ony need the selected row, we know the column names and location
1918        Wave/T fw = root:myGlobals:TransHeaderInfo:T_FileNames          //trans files
1919        Wave/T ew = root:myGlobals:TransHeaderInfo:T_EMP_FileNames              //empty beam reference files
1920       
1921        ew[] = fw[row]
1922       
1923        return(0)
1924End
1925
1926//given a selection of the sample files, try to find the corresponding transmission file
1927//
1928Function GuessTransFile(charsToUse,row)
1929        Variable charsToUse,row                 //number of characers to use from the beginning of the label, and the row of the sample file
1930       
1931//      GetSelection table,ScatterFileTable,3
1932//      Variable row=V_startRow                 //ony need the selected row, we know the column names and location
1933        Wave/T sw = root:myGlobals:TransHeaderInfo:S_Labels             //Sample file labels
1934        Wave/T tw = root:myGlobals:TransHeaderInfo:T_Labels             //trans file labels
1935        Wave/T tnam = root:myGlobals:TransHeaderInfo:T_FileNames        //trans file names
1936        Wave/T snam = root:myGlobals:TransHeaderInfo:S_TRANS_FileNames  //Scattering - trans correspondence
1937       
1938        Variable num,ii,found
1939        String samStr = "",testStr=""
1940       
1941        samStr = (sw[row])[0,charsToUse-1]              //string to try and match
1942        samStr += "*"
1943//      Print samStr
1944        num=numpnts(tw)
1945        ii=0
1946        do
1947                testStr=tw[ii]
1948                found = stringmatch(testStr, samStr )
1949                if(found == 1)
1950                        Print "Match Found at:  ",samStr,tnam[ii],tw[ii]
1951                        snam[row] = tnam[ii]                    //write the file name into the table
1952                        return(0)       
1953                endif
1954                ii+=1
1955        while(ii<num)
1956       
1957        //method 2
1958//      samStr = (sw[row])[0,charsToUse-1]              //string to try and match
1959////    Print samStr
1960//      num=numpnts(tw)
1961//      ii=0
1962//      do
1963//              testStr=tw[ii]
1964//              found = strsearch(testStr,samStr,0)             //start from zero, Need Igor 5 to allow 4th parameter to ignore case
1965//              if(found != -1)
1966//                      Print "Match Found at:  ",samStr,tnam[ii],tw[ii]
1967//                      snam[row] = tnam[ii]                    //write the file name into the table
1968//                      return(0)       
1969//              endif
1970//              ii+=1
1971//      while(ii<num)
1972       
1973        Print "No Match found"
1974        return(0)
1975End
1976
1977Function GuessAllTransFiles(numChars)
1978        Variable numChars
1979       
1980        if(WinType("ScatterFileTable") ==0 )
1981                return(1)
1982        endif
1983       
1984        Wave/T sw = root:myGlobals:TransHeaderInfo:S_Labels             //Sample file labels
1985        Variable ii,num=numpnts(sw)
1986       
1987        for(ii=0;ii<num;ii+=1)
1988                GuessTransFile(numChars,ii)
1989        endfor
1990       
1991        return(0)
1992End
1993
1994Function fGuessSelectedTransFiles(numChars)
1995        Variable numChars
1996       
1997        Variable ii
1998        Variable scatterTableExists
1999        scatterTableExists = WinType("ScatterFileTable")
2000        if (scatterTableExists != 0)
2001                GetSelection table,ScatterFileTable,1   
2002                for(ii=V_StartRow;ii<=V_EndRow;ii+=1)
2003                        GuessTransFile(numChars,ii)
2004                endfor
2005        else
2006                Print "Transmission->Scattering File table is not open"
2007        endif
2008        return(0)
2009End
2010
2011Function ClearSelectedAssignments()
2012       
2013        String winStr = WinList("*", ";", "WIN:" )              //returns the target window
2014        Variable scatterTableExists, transTableExists
2015        Print winStr
2016        scatterTableExists = cmpstr(winStr,"ScatterFileTable;")
2017        if (scatterTableExists == 0)
2018                GetSelection table,ScatterFileTable,1   
2019                fClearSelectedAssignments(V_startRow,V_endRow,1)
2020        endif
2021       
2022        transTableExists = cmpstr(winStr,"TransFileTable;")
2023        if (transTableExists == 0)
2024                GetSelection table,TransFileTable,1     
2025                fClearSelectedAssignments(V_startRow,V_endRow,2)
2026        endif
2027       
2028        return(0)
2029End
Note: See TracBrowser for help on using the repository browser.