source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/SANS/Transmission.ipf @ 577

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

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

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

Added NCNR_Utils to the includes list for Analysis.

File size: 73.2 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma version=5.0
3#pragma IgorVersion=6.1
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       
161        SetWindow kwTopWin hook=GuessFromTableHook, hookevents=1        // mouse down events
162       
163        String name="TransFileTable"
164        DoWindow/C $name
165
166End
167
168//actually creates the table of scattering files
169// - the waves must exist
170//
171Function BuildScatTableWindow()
172        Wave/T S_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
173        Wave/T S_Filenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
174        Wave/T S_Suffix = $"root:myGlobals:TransHeaderInfo:S_Suffix"
175        Wave/T S_Labels = $"root:myGlobals:TransHeaderInfo:S_Labels"
176        Wave   S_SDD = $"root:myGlobals:TransHeaderInfo:S_SDD"
177        Wave   S_Lambda = $"root:myGlobals:TransHeaderInfo:S_Lambda"
178        Wave   S_Transmission = $"root:myGlobals:TransHeaderInfo:S_Transmission"
179       
180        Edit/K=1/W=(50,50,520,270) S_TRANS_Filenames, S_Filenames, S_Labels, S_SDD, S_Lambda, S_Transmission as "ScatteringFiles"
181        String name="ScatterFileTable"
182        DoWindow/C $name
183
184End
185
186//sorts each of the tables by date - does this by using the suffix "Annn"
187// which is automatically supplied by the VAX (and can't be changed)
188// - only problem may be "Z999" -> "A001" rollover
189//
190Function SortTransByDate()
191        Wave/T T_GEMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
192        Wave/T T_GSuffices = $"root:myGlobals:TransHeaderInfo:T_Suffices"
193        Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
194        Wave/T T_GSuffix = $"root:myGlobals:TransHeaderInfo:T_Suffix"
195        Wave/T T_GLabels = $"root:myGlobals:TransHeaderInfo:T_Labels"
196        Wave   T_GSDD = $"root:myGlobals:TransHeaderInfo:T_SDD"
197        Wave   T_GLambda = $"root:myGlobals:TransHeaderInfo:T_Lambda"
198        Wave   T_GTransmission = $"root:myGlobals:TransHeaderInfo:T_Transmission"
199        Wave   T_Whole= $"root:myGlobals:TransHeaderInfo:T_Whole"
200
201        Wave/T S_GTRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
202        Wave/T S_GSuffices = $"root:myGlobals:TransHeaderInfo:S_Suffices"
203        Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
204        Wave/T S_GSuffix = $"root:myGlobals:TransHeaderInfo:S_Suffix"
205        Wave/T S_GLabels = $"root:myGlobals:TransHeaderInfo:S_Labels"
206        Wave   S_GSDD = $"root:myGlobals:TransHeaderInfo:S_SDD"
207        Wave   S_GLambda = $"root:myGlobals:TransHeaderInfo:S_Lambda"
208        Wave   S_GTransmission = $"root:myGlobals:TransHeaderInfo:S_Transmission"
209        Wave   S_Whole= $"root:myGlobals:TransHeaderInfo:S_Whole"
210       
211        Sort T_GSuffix, T_GEMP_Filenames, T_GSuffix, T_GSuffices, T_GFilenames, T_GLabels, T_GSDD, T_GLambda, T_GTransmission, T_Whole
212        Sort S_GSuffix, S_GTRANS_Filenames, S_GSuffix, S_GSuffices, S_GFilenames, S_GLabels, S_GSDD, S_GLambda, S_GTransmission, S_Whole
213       
214End
215
216// sorts files alphabetically byt the sample label
217// - consistent and descriptive sample labels really pay off here
218//
219Function SortTransByLabel()
220        Wave/T T_GEMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
221        Wave/T T_GSuffices = $"root:myGlobals:TransHeaderInfo:T_Suffices"
222        Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
223        Wave/T T_GSuffix = $"root:myGlobals:TransHeaderInfo:T_Suffix"
224        Wave/T T_GLabels = $"root:myGlobals:TransHeaderInfo:T_Labels"
225        Wave   T_GSDD = $"root:myGlobals:TransHeaderInfo:T_SDD"
226        Wave   T_GLambda = $"root:myGlobals:TransHeaderInfo:T_Lambda"
227        Wave   T_GTransmission = $"root:myGlobals:TransHeaderInfo:T_Transmission"
228        Wave   T_Whole= $"root:myGlobals:TransHeaderInfo:T_Whole"
229       
230        Wave/T S_GTRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
231        Wave/T S_GSuffices = $"root:myGlobals:TransHeaderInfo:S_Suffices"
232        Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
233        Wave/T S_GSuffix = $"root:myGlobals:TransHeaderInfo:S_Suffix"
234        Wave/T S_GLabels = $"root:myGlobals:TransHeaderInfo:S_Labels"
235        Wave   S_GSDD = $"root:myGlobals:TransHeaderInfo:S_SDD"
236        Wave   S_GLambda = $"root:myGlobals:TransHeaderInfo:S_Lambda"
237        Wave   S_GTransmission = $"root:myGlobals:TransHeaderInfo:S_Transmission"
238        Wave   S_Whole= $"root:myGlobals:TransHeaderInfo:S_Whole"
239       
240        Sort T_GLabels, T_GEMP_Filenames, T_GSuffix, T_GSuffices, T_GFilenames, T_GLabels, T_GSDD, T_GLambda, T_GTransmission, T_Whole
241        Sort S_GLabels, S_GTRANS_Filenames, S_GSuffix, S_GSuffices, S_GFilenames, S_GLabels, S_GSDD, S_GLambda, S_GTransmission, S_Whole
242       
243End
244
245//reads the file and assigns header information to the proper table
246//
247//fname - is the full path:name of the datafile (used by Open)
248//sname - is the name only, used as the filename field
249//
250// takes care of all necessary open/close of file
251//
252Function GetTransHeaderInfoToWave(fname,sname)
253        String fname,sname
254       
255        Variable isTrans
256
257        Variable lastPoint
258
259        isTrans = isTransFile(fname)
260        // decalre the correct set of waves
261        // S_Whole is only a place holder so the waves are of equal length
262        if (isTrans)
263                Wave/T GSuffices = $"root:myGlobals:TransHeaderInfo:T_Suffices"
264                Wave/T G_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
265                Wave/T GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
266                Wave/T GSuffix = $"root:myGlobals:TransHeaderInfo:T_Suffix"
267                Wave/T GLabels = $"root:myGlobals:TransHeaderInfo:T_Labels"
268                Wave GSDD = $"root:myGlobals:TransHeaderInfo:T_SDD"
269                Wave GLambda = $"root:myGlobals:TransHeaderInfo:T_Lambda"
270                Wave GTransmission = $"root:myGlobals:TransHeaderInfo:T_Transmission"
271                Wave GWhole = $"root:myGlobals:TransHeaderInfo:T_Whole"
272        Else
273                Wave/T GSuffices = $"root:myGlobals:TransHeaderInfo:S_Suffices"
274                Wave/T G_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
275                Wave/T GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
276                Wave/T GSuffix = $"root:myGlobals:TransHeaderInfo:S_Suffix"
277                Wave/T GLabels = $"root:myGlobals:TransHeaderInfo:S_Labels"
278                Wave GSDD = $"root:myGlobals:TransHeaderInfo:S_SDD"
279                Wave GLambda = $"root:myGlobals:TransHeaderInfo:S_Lambda"
280                Wave GTransmission = $"root:myGlobals:TransHeaderInfo:S_Transmission"
281                Wave GWhole = $"root:myGlobals:TransHeaderInfo:S_Whole"
282        Endif
283
284        lastPoint = numpnts(GLambda)
285       
286        InsertPoints lastPoint,1,G_TRANS_Filenames
287        G_TRANS_Filenames[lastPoint]=""
288
289        InsertPoints lastPoint,1,GFilenames
290        GFilenames[lastPoint]=sname
291       
292        //read the file suffix
293        InsertPoints lastPoint,1,GSuffix
294        GSuffix[lastPoint]=getSuffix(fname)
295
296        //read any suffix that this file is associated with
297        InsertPoints lastPoint,1,GSuffices
298        GSuffices[lastPoint]=getAssociatedFileSuffix(fname)
299           
300        // read the sample.label text field
301        InsertPoints lastPoint,1,GLabels
302        GLabels[lastPoint]=getSampleLabel(fname)
303           
304
305        //Transmission
306        InsertPoints lastPoint,1,GTransmission
307        GTransmission[lastPoint]=getSampleTrans(fname)
308       
309        //Whole detector Transmission
310        InsertPoints lastPoint,1,GWhole
311        GWhole[lastPoint]=getSampleTransWholeDetector(fname)
312           
313        //SDD
314        InsertPoints lastPoint,1,GSDD
315        GSDD[lastPoint]=getSDD(fname)
316           
317        //wavelength
318        InsertPoints lastPoint,1,GLambda
319        GLambda[lastPoint]=getWavelength(fname)
320       
321        return(0)
322End
323
324//fills in the file relations that have already been set
325//
326//
327Function GetFilenamesFromSuffices()
328        Wave/T T_EMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
329        Wave/T T_GSuffices = $"root:myGlobals:TransHeaderInfo:T_Suffices"
330        Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
331        Wave/T T_GSuffix = $"root:myGlobals:TransHeaderInfo:T_Suffix"
332       
333        Wave/T S_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
334        Wave/T S_GSuffices = $"root:myGlobals:TransHeaderInfo:S_Suffices"
335        Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
336        Wave/T S_GSuffix = $"root:myGlobals:TransHeaderInfo:S_Suffix"
337       
338        Variable num_s_files, num_t_files, ii, jj
339        num_t_files = numpnts(T_GFilenames)
340        num_s_files = numpnts(S_GFilenames)
341        //print num_t_files, num_s_files
342       
343        // Get transmission file relations
344        ii= 0
345        do
346                if (cmpstr(T_GSuffices[ii],"")!=0)
347                        jj=0
348                        do
349                                if (cmpstr(T_GSuffices[ii],T_GSuffix[jj])==0)
350                                        T_EMP_Filenames[ii]=T_GFilenames[jj]
351                                endif
352                        jj+=1
353                        while(jj<num_t_files)
354                endif
355                ii+=1
356        while(ii<num_t_files)
357 
358        // Get scattering file relations
359        ii= 0
360        do
361                if (cmpstr(S_GSuffices[ii],"")!=0)
362                        jj=0
363                        do
364                                if (cmpstr(S_GSuffices[ii],T_GSuffix[jj])==0)
365                                        S_TRANS_Filenames[ii]=T_GFilenames[jj]
366                                endif
367                        jj+=1
368                        while(jj<num_t_files)
369                endif
370                ii+=1
371        while(ii<num_s_files)
372End
373
374// gets the selected transmission filename, and puts its name as the
375//empty beam reference file
376// lists the xy box coordinates as read from the file header (zeros if not yet assigned)
377//
378Function PickEMPTransButton(ctrlName) : ButtonControl
379        String ctrlName
380       
381        Variable TransFileTableExists
382        TransFileTableExists = WinType("TransFileTable")
383        //??BUG?? V_flag returns 1 even if no selection?
384        //check manually for null selection
385        //Print "local v_flag = ",num2str(V_flag)
386        Wave/T LocFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
387        if (TransFileTableExists != 0)
388                String/G root:myGlobals:TransHeaderInfo:gEMP = ""
389                GetSelection table,TransFileTable,7
390                //Print S_Selection
391                if(strsearch(S_selection,"T_Filenames",0) == 0)
392                   //selection OK, add to list
393                        Duplicate/O/R=[V_startRow,V_endRow] LocFilenames, filenames
394                        Wave/T selectedFiles = $"filenames"
395
396                        SVAR temp = root:myGlobals:TransHeaderInfo:gEMP
397                        //
398                        temp = temp+selectedFiles[0]            //take just the first file
399                       
400                        UpdateBoxCoordinates()
401                       
402                        //check to make sure that if the box is set, that the count value in the box is set too...
403                        // if the box is set by a protocol, then the counts are not set -> and the transmission will be INF
404                        String tempName = FindValidFilename(temp)
405                        if(cmpstr(tempName,"")==0)
406                                //file not found, get out
407                                Abort "Empty beam file not found UpdateBoxCoordinates(ctrlName)"
408                        Endif
409                        //name is ok, prepend path to tempName for read routine
410                        PathInfo catPathName
411                        String filename = S_path + tempName
412                        Variable boxCounts = getBoxCounts(filename)
413                       
414                        variable x1,x2,y1,y2
415                        getXYBoxFromFile(filename,x1,x2,y1,y2)
416                       
417                        if( (boxCounts == 0) && (x1!=0) && (x2!=0) && (y1!=0) && (y2!=0) )
418                                DoAlert 0,"Box Counts were not recorded. Please re-select the box using SetXYBox"
419                        endif
420                       
421                       
422                Else
423                        DoWindow/F TransFileTable
424                        DoAlert 0,"Invalid selection from the Transmision file table. You must select a file from the T_Filenames column"
425                Endif
426        else
427                //no selection
428                DoAlert 0,"No file selected from Transmission file table or no Transmission file table available"
429        Endif
430End
431
432//initialize data folder and globals for the Trans panel as needed
433//since this operation is somewhat tangled with the Patch Panel, keep all of the
434//globals in the Patch subfolder (make sure both panels are simultaneously initialized)
435//do not create a separate Trans folder
436//
437Proc InitializeTransPanel()
438        //create the global variables needed to run the Trans Panel
439        //all are kept in root:myGlobals:TransHeaderInfo
440        If( ! (DataFolderExists("root:myGlobals:TransHeaderInfo"))  )
441                //create the data folder and the clobals for BOTH the Patch and Trans Panels
442                NewDataFolder/O root:myGlobals:TransHeaderInfo
443                CreateTransGlobals()
444        Endif
445End
446
447
448//
449Proc CreateTransGlobals()
450
451        PathInfo catPathName
452        If(V_flag==1)
453                String dum = S_path
454                String/G root:myGlobals:TransHeaderInfo:gCatPathStr = dum
455        else
456                String/G root:myGlobals:TransHeaderInfo:gCatPathStr = "no path selected"
457        endif
458
459        String/G root:myGlobals:TransHeaderInfo:gEMP = "no file selected"
460        String/G root:myGlobals:TransHeaderInfo:gBox = ""
461        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:T_Suffices"
462        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
463        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:T_Filenames"
464        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:T_Suffix"
465        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:T_Labels"
466        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:T_SDD"
467        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:T_Lambda"
468        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:T_Transmission"
469        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:T_Whole"
470       
471        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:S_Suffices"
472        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
473        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:S_Filenames"
474        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:S_Suffix"
475        Make/O/T/N=0 $"root:myGlobals:TransHeaderInfo:S_Labels"
476        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:S_SDD"
477        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:S_Lambda"
478        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:S_Transmission"
479        Make/O/D/N=0 $"root:myGlobals:TransHeaderInfo:S_Whole"
480End
481
482
483//function that writes out (to the header of a raw binary SANS file)
484//characters representing the empty beam file and the sample transmission file
485//the 4-character file suffix is written, rather than the run numbers
486//from this, the trans files can be located, and the
487//sample file contains all the information needed to calculate its transmission
488//
489//uses first 4 bytes of params.reserve (character field) starting at byte 404
490//(previous version (AM) used run.reserve field @ byte 91, which was not empty, and not at the start of the field!)
491//
492Function AssignSelTransFilesToData(startRow,endRow)
493        Variable startRow,endRow
494
495//      GetSelection table,ScatterFileTable,1
496       
497        Wave/T T_EMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
498        Wave/T T_GSuffices = $"root:myGlobals:TransHeaderInfo:T_Suffices"
499        Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
500        Wave/T T_GSuffix = $"root:myGlobals:TransHeaderInfo:T_Suffix"
501       
502        Wave/T S_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
503        Wave/T S_GSuffices = $"root:myGlobals:TransHeaderInfo:S_Suffices"
504        Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
505        Wave/T S_GSuffix = $"root:myGlobals:TransHeaderInfo:S_Suffix"
506       
507        Variable num_s_files, num_t_files, ii, jj
508        Variable refnum
509        num_t_files = numpnts(T_GFilenames)
510        String suffix = ""
511        PathInfo catPathName
512        String pathname = S_path
513        String filename
514
515        // Write suffix of empty beam file into transmission files
516        ii= 0
517        do
518                if (cmpstr(T_EMP_Filenames[ii],"")!=0)
519                        jj=0
520                        do
521                                if (cmpstr(T_EMP_Filenames[ii],T_GFilenames[jj])==0)
522                                        suffix=T_GSuffix[jj]
523                                        filename = pathname + T_GFilenames[ii]
524                                        WriteAssocFileSuffixToHeader(filename,suffix)
525                                endif
526                                jj+=1
527                        while(jj<num_t_files)
528                endif
529        ii+=1
530        while(ii<num_t_files)
531 
532        // Write suffix of transmission files into scattering files
533        ii= startRow
534        do
535                if (cmpstr(S_TRANS_Filenames[ii],"")!=0)
536                        jj=0
537                        do
538                                if (cmpstr(S_TRANS_Filenames[ii],T_GFilenames[jj])==0)
539                                        suffix=T_GSuffix[jj]
540                                        filename = pathname + S_GFilenames[ii]
541                                        WriteAssocFileSuffixToHeader(filename,suffix)
542                                endif
543                                jj+=1
544                        while(jj<num_t_files)
545                endif
546                ii+=1
547        while(ii<=endRow)
548        return(0)
549End
550
551// Allows to calculate transmissions without need of a scattering file. Used with the Calculate Total Trans button.
552
553Function AssignTotalTransFilesToData(startRow,endRow)
554        Variable startRow,endRow
555
556//      GetSelection table,ScatterFileTable,1
557       
558        Wave/T T_EMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
559        Wave/T T_GSuffices = $"root:myGlobals:TransHeaderInfo:T_Suffices"
560        Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
561        Wave/T T_GSuffix = $"root:myGlobals:TransHeaderInfo:T_Suffix"
562
563        Variable num_t_files, ii, jj
564        Variable refnum
565        num_t_files = numpnts(T_GFilenames)
566        String suffix = ""
567        PathInfo catPathName
568        String pathname = S_path
569        String filename
570
571        // Write suffix of empty beam file into transmission files
572        ii= 0
573        do
574                if (cmpstr(T_EMP_Filenames[ii],"")!=0)
575                        jj=0
576                        do
577                                if (cmpstr(T_EMP_Filenames[ii],T_GFilenames[jj])==0)
578                                        suffix=T_GSuffix[jj]
579                                        filename = pathname + T_GFilenames[ii]
580                                        WriteAssocFileSuffixToHeader(filename,suffix)
581                                endif
582                                jj+=1
583                        while(jj<num_t_files)
584                endif
585        ii+=1
586        while(ii<num_t_files)
587 
588        return(0)
589End
590
591
592//function that clears the selected assignments of sample trans -> sample scattering
593//also writes a trans=1 value back to the sample file
594//
595// DEC 2006 - now also writes blank suffix to clear trans->empty assignment, and Twhole=1
596//
597// target = 1 = ScatterFileTable
598// target = 2 = TransFileTable
599Function fClearSelectedAssignments(startRow,endRow,target)
600        Variable startRow,endRow,target
601       
602        Wave/T S_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
603        Wave/T S_GSuffices = $"root:myGlobals:TransHeaderInfo:S_Suffices"
604        Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
605        Wave/T S_GSuffix = $"root:myGlobals:TransHeaderInfo:S_Suffix"
606        Wave S_GTransmission =  $"root:myGlobals:TransHeaderInfo:S_Transmission"
607        Wave/T T_EMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
608        Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
609        Wave/T T_GSuffix = $"root:myGlobals:TransHeaderInfo:T_Suffix"
610        Wave T_GTransmission =  $"root:myGlobals:TransHeaderInfo:T_Transmission"
611        Wave T_GTWhole =  $"root:myGlobals:TransHeaderInfo:T_Whole"
612       
613       
614        Variable num_s_files, num_t_files, ii, jj
615        Variable refnum,zero=0
616        num_t_files = numpnts(T_GFilenames)
617        String suffix = "    "          //4 blank spaces
618        PathInfo catPathName
619        String pathname = S_path
620        String filename
621
622// If the selection is from the TransFileTable
623// - wipe out the box coordinates
624// - wipe out the suffix
625// - wipe out the transmission
626// - wipe out T_whole
627// - update the waves in the table
628
629        if(target == 2)
630                ii= startRow
631                do
632                        filename = pathname + T_GFilenames[ii]
633                       
634                        // write in zeros for the box coordinates
635                        WriteXYBoxToHeader(filename,0,0,0,0)
636                       
637                        // write in a null suffix
638                        WriteAssocFileSuffixToHeader(filename,suffix)
639                       
640                        //write a trans==1 to the file header of the raw data
641                        WriteTransmissionToHeader(filename,1)           //sample trans
642                       
643                        WriteWholeTransToHeader(filename,1)             //WholeTrans start byte is 392
644                       
645                        //then update the table that is displayed
646                        T_EMP_Filenames[ii] = ""
647                        T_GTransmission[ii] = 1
648                        T_GTWhole[ii] = 1
649                       
650                        ii+=1
651                while(ii<=endRow)
652        endif
653       
654 
655        if(target == 1)
656                // Write null suffix of transmission files into scattering files
657                ii= startRow
658                do
659                        filename = pathname + S_GFilenames[ii]
660                        // write in a null suffix
661                        WriteAssocFileSuffixToHeader(filename,suffix)
662                       
663                        //write a trans==1 to the file header of the raw data (open/close done in function)
664                        WriteTransmissionToHeader(filename,1)           //sample trans
665                       
666                        //then update the table that is displayed
667                        S_TRANS_Filenames[ii] = ""
668                        S_GTransmission[ii] = 1
669                       
670                        ii+=1
671                while(ii<=endRow)
672        endif
673       
674        return(0)
675End
676
677//given a selection of scattering files, calculates the transmission
678//and writes the new transmission to the file header
679//
680//given the full path;name;vers  (= filenmame), of a raw binary SANS file,
681//the transmission of the sample is calculated (if possible) from information
682//in the file header. empty beam and sample transmision run numbers are extracted
683//and the files are located from the run number (if possible)
684//from the header of the empty beam file, the XY box coordinates and "empty"
685//counts are determined
686//once all information is located, the transmission is calculated and the
687//transmission field of the sample file is automatically "Patched"
688// - updates the global variable that is displayed in the panel
689//can be run in batch mode, sequentially passing each item from a list of filenames
690//this is the single step of the batch mode
691//
692//in batch mode: execution will proceed through all of the files, reporting
693//results to the history window. If no trans could be successfully calculated
694//(for a blocked beam, for example), then that is displayed as well, and the
695//raw data is not modified
696//
697// now takes the attenuation of the sample trans and empty beam trans into account, (normally this == 1)
698// and rescales the transmission as appropriate
699// 3/31/04  SRK
700//
701Function CalcSelTransFromHeader(startRow,endRow)
702        Variable startRow,endRow
703//      GetSelection table,ScatterFileTable,1
704
705        String filename
706        Wave/T T_EMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
707        Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
708       
709        Wave/T S_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
710        Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
711        Wave S_GTransmission =  $"root:myGlobals:TransHeaderInfo:S_Transmission"
712       
713        Variable num_s_files, num_t_files, ii, jj
714        Variable refnum, transCts, emptyCts,attenRatio,lambda,trans
715        Variable x1,x2,y1,y2,err,attenEmp,attenSam
716        String suffix = "",pathName,textStr,abortStr,emptyFile,transFile,samFileStr
717        String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
718        String strToExecute
719       
720        num_t_files = numpnts(T_GFilenames)
721       
722        PathInfo catPathName
723        pathname = S_path
724
725        ii= startRow
726        do
727                if (cmpstr(S_TRANS_Filenames[ii],"")!=0)        //if there is a sample trans file assigned
728                        jj=0
729                        do
730                                if (cmpstr(S_TRANS_Filenames[ii],T_GFilenames[jj])==0)  //find it in the trans file table
731                                        if (cmpstr(T_EMP_Filenames[jj],"")!=0)          //if empty beam file assigned, proceed with the calculation
732                                                //full path+name to access all 3 files
733                                                filename=pathname+S_GFilenames[ii]
734                                                emptyFile = pathname+T_EMP_Filenames[jj]
735                                                transFile = pathname+T_GFilenames[jj]
736                                                //////////
737                                                // check the empty beam file for previously selected coordinates
738                                                //if they exist, set the xy string , save the normalized counts somewhere
739                                                //the value was written to an unused r*4 header analysis.factor (@b494)
740                                                GetXYBoxFromFile(emptyFile,x1,x2,y1,y2)
741                                               
742                                                //read the real count value
743                                                emptyCts = getBoxCounts(emptyFile)
744                                                // read the attenuator number of the empty beam file
745                                                attenEmp = getAttenNumber(emptyFile)
746                                                //
747                                                if( ((x1-x2)==0) || ((y1-y2)==0) )              //zero width marquee in either direction
748                                                        //no region selected    -- prompt user to select box w/marquee
749                                                        DoWindow/F TransFileTable
750                                                        Abort "Use \"Set BCENT File\", then \"SetXYBox\" to select XY range in empty beam file "+T_EMP_Filenames[jj]
751                                                        //from here the Marquee menu selection must handle the task of setting the box and updating the information in the file header
752                                                Endif
753               
754                                                //read in trans file then add to SAM
755                                                ReadHeaderAndData(transFile)
756                                                //adds to SAM
757                                                err = Raw_to_work("SAM")
758                                                //sum region in SAM
759                                                transCts =  SumCountsInBox(x1,x2,y1,y2,"SAM")   
760                                                // get the attenuator, lambda, and sample string (to get the instrument)
761                                                WAVE/T samText = $"root:Packages:NIST:SAM:textRead"
762                                                WAVE samReals = $"root:Packages:NIST:SAM:realsRead"
763                                                samfileStr = samText[3]
764                                                lambda = samReals[26]
765                                                attenSam = samReals[3]
766                                                //calculate the ratio of attenuation factors - assumes that same instrument used for each, AND same lambda
767                                                AttenRatio = AttenuationFactor(samFileStr,lambda,attenEmp)/AttenuationFactor(samFileStr,lambda,attenSam)
768                                                //calculate trans based on empty beam value and rescale by attenuation ratio
769                                                trans= transCts/emptyCts * AttenRatio
770                                               
771                                                //write out counts and transmission to history window, showing the attenuator ratio, if it is not unity
772                                                If(attenRatio==1)
773                                                        Printf "%s\t\tTrans Counts = %g\tTrans = %g\r",S_GFilenames[ii], transCts,trans
774                                                else
775                                                        Printf "%s\t\tTrans Counts = %g\tTrans = %g\tAttenuatorRatio = %g\r",S_GFilenames[ii], transCts,trans,attenRatio
776                                                endif
777                                                //write the trans to the file header of the raw data (open/close done in function)
778                                                WriteTransmissionToHeader(filename,trans)
779                                               
780                                                //then update the global that is displayed
781                                                S_GTransmission[ii] = trans
782                                               
783                                        else  // There is no empty assigned
784                                                abortStr = "Empty beam file not assigned properly for " + T_GFilenames[jj]
785                                                Print abortStr
786                                                //Abort abortStr
787                                                return(1)
788                                        endif
789                                endif
790                                jj+=1
791                        while(jj<num_t_files)
792                else //no transmission file
793                        abortStr = "Transmission beam file not assigned properly for " + S_GFilenames[ii]
794                        Print abortStr
795                endif
796                ii+=1
797        while(ii<=endRow)
798        print "done"
799        return(0)
800End
801
802//
803// For the calculation of the Transmission using only the Trans Files screen
804// Uses only the information on the transmissionFiles screen to calculate
805// a "box" Transmission for comparison with the Whole Transmission
806// updated 5/11/2006 by Bryan Greenwald
807Function CalcTotalTrans(startRow,endRow)
808        Variable startRow,endRow
809//      GetSelection table,ScatterFileTable,1
810
811        String filename
812        Wave/T T_EMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
813       
814        Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
815        Wave T_GTransmission = $"root:myGlobals:TransHeaderInfo:T_Transmission"
816       
817//      Wave/T S_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
818//      Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
819//      Wave S_GTransmission =  $"root:myGlobals:TransHeaderInfo:S_Transmission"
820        Wave GWhole= $"root:myGlobals:TransHeaderInfo:T_Whole"
821       
822        Variable num_t_files, ii, jj
823        Variable refnum, transCts, emptyCts,attenRatio,lambda,trans
824        Variable x1,x2,y1,y2,err,attenEmp,attenSam
825        String suffix = "",pathName,textStr,abortStr,emptyFile,transFile,samFileStr
826        String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
827        String strToExecute
828       
829        num_t_files = numpnts(T_GFilenames)
830       
831        PathInfo catPathName
832        pathname = S_path
833
834        ii= startRow
835        do
836                if (cmpstr(T_Emp_Filenames[ii],"")!=0)  //if there is a sample trans file assigned
837                        jj=0
838                        do
839                                if (cmpstr(T_Emp_Filenames[ii],T_GFilenames[jj])==0)    //find it in the trans file table
840                                        if (cmpstr(T_EMP_Filenames[ii],"")!=0)          //if empty beam file assigned, proceed with the calculation
841                                                //full path+name to access all 3 files
842                                                filename=pathname+T_GFilenames[ii]
843                                                emptyFile = pathname+T_EMP_Filenames[ii]
844                                                transFile = pathname+T_GFilenames[ii]
845                                                //////////
846                                                // check the empty beam file for previously selected coordinates
847                                                //if they exist, set the xy string , save the normalized counts somewhere
848                                                //the value was written to an unused r*4 header analysis.factor (@b494)
849                                                getXYBoxFromFile(emptyFile,x1,x2,y1,y2)
850                                               
851                                                //read the real count value
852                                                emptyCts = getBoxCounts(emptyFile)
853                                                // read the attenuator number of the empty beam file
854                                                attenEmp = getAttenNumber(emptyFile)
855                                                //
856                                                if( ((x1-x2)==0) || ((y1-y2)==0) )              //zero width marquee in either direction
857                                                        //no region selected    -- prompt user to select box w/marquee
858                                                        DoWindow/F TransFileTable
859                                                        Abort "Use \"Set BCENT File\", then \"SetXYBox\" to select XY range in empty beam file "+T_EMP_Filenames[jj]
860                                                        //from here the Marquee menu selection must handle the task of setting the box and updating the information in the file header
861                                                Endif
862               
863                                                //read in trans file then add to SAM
864                                                ReadHeaderAndData(transFile)
865                                                //adds to SAM
866                                                err = Raw_to_work("SAM")
867                                                //sum region in SAM
868                                                transCts =  SumCountsInBox(x1,x2,y1,y2,"SAM")   
869                                                // get the attenuator, lambda, and sample string (to get the instrument)
870                                                WAVE/T samText = $"root:Packages:NIST:SAM:textRead"
871                                                WAVE samReals = $"root:Packages:NIST:SAM:realsRead"
872                                                samfileStr = samText[3]
873                                                lambda = samReals[26]
874                                                attenSam = samReals[3]
875                                                //calculate the ratio of attenuation factors - assumes that same instrument used for each, AND same lambda
876                                                AttenRatio = AttenuationFactor(samFileStr,lambda,attenEmp)/AttenuationFactor(samFileStr,lambda,attenSam)
877                                                //calculate trans based on empty beam value and rescale by attenuation ratio
878                                                trans= transCts/emptyCts * AttenRatio
879                                               
880                                                //write out counts and transmission to history window, showing the attenuator ratio, if it is not unity
881                                                If(attenRatio==1)
882                                                        //Printf "%s\t\tTrans Counts = %g\t Actual Trans = %g\r",T_GFilenames[ii], transCts,trans
883                                                        Printf "%s\t\tBox Counts = %g\t Trans = %g\r",T_GFilenames[ii], transCts,trans
884                                                else
885                                                        //Printf "%s\t\tTrans Counts = %g\t Trans = %g\tAttenuatorRatio = %g\r",T_GFilenames[ii], transCts,trans,attenRatio
886                                                        Printf "%s\t\tBox Counts = %g\t Trans = %g\t AttenuatorRatio = %g\r",T_GFilenames[ii], transCts,trans,attenRatio
887                                                endif
888                                                //write the trans to the file header of the raw data (open/close done in function)
889                                                WriteTransmissionToHeader(filename,trans)               //transmission start byte is 158
890                                               
891                                                //then update the global that is displayed
892                                                T_GTransmission[ii] = trans
893                                               
894                                        else  // There is no empty assigned
895                                                abortStr = "Empty beam file not assigned properly for " + T_GFilenames[jj]
896                                                Print abortStr
897                                                //Abort abortStr
898                                                return(1)
899                                        endif
900                                endif
901                                jj+=1
902                        while(jj<num_t_files)
903                else //no transmission file
904                        abortStr = "Transmission beam file not assigned properly for " + T_GFilenames[ii]
905                        Print abortStr
906                endif
907                ii+=1
908        while(ii<=endRow)
909        //print "done"
910        return(0)
911End
912
913//
914// For the calculation of the Transmission using only the whole detector
915// Used to compute the transmission for the TransmissionFiles table using
916// the entire detector. For comparison with the "box" trans
917// updated: 5/11/2006 by Bryan Greenwald
918Function CalcWholeTrans(startRow,endRow)
919        Variable startRow,endRow
920//      GetSelection table,ScatterFileTable,1
921
922        NVAR pixelsX = root:myGlobals:gNPixelsX
923        NVAR pixelsY = root:myGlobals:gNPixelsY
924       
925        String filename
926        Wave/T T_EMP_Filenames = $"root:myGlobals:TransHeaderInfo:T_EMP_Filenames"
927       
928        Wave/T T_GFilenames = $"root:myGlobals:TransHeaderInfo:T_Filenames"
929        Wave T_GTransmission = $"root:myGlobals:TransHeaderInfo:T_Transmission"
930       
931//      Wave/T S_TRANS_Filenames = $"root:myGlobals:TransHeaderInfo:S_TRANS_Filenames"
932//      Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
933//      Wave S_GTransmission =  $"root:myGlobals:TransHeaderInfo:S_Transmission"
934        Wave GWhole= $"root:myGlobals:TransHeaderInfo:T_Whole"
935       
936        Variable num_t_files, ii, jj
937        Variable refnum, transCts, emptyCts,attenRatio,lambda,trans
938        Variable x1,x2,y1,y2,err,attenEmp,attenSam
939        String suffix = "",pathName,textStr,abortStr,emptyFile,transFile,samFileStr
940        String GBLoadStr="GBLoadWave/O/N=tempGBwave/T={2,2}/J=2/W=1/Q"
941        String strToExecute
942       
943        num_t_files = numpnts(T_GFilenames)
944       
945        PathInfo catPathName
946        pathname = S_path
947
948        ii= startRow
949        do
950                if (cmpstr(T_Emp_Filenames[ii],"")!=0)  //if there is a sample trans file assigned
951                        jj=0
952                        do
953                                if (cmpstr(T_Emp_Filenames[ii],T_GFilenames[jj])==0)    //find it in the trans file table
954                                        if (cmpstr(T_EMP_Filenames[ii],"")!=0)          //if empty beam file assigned, proceed with the calculation
955                                                //full path+name to access all 3 files
956                                                filename=pathname+T_GFilenames[ii]
957                                                emptyFile = pathname+T_EMP_Filenames[ii]
958                                                transFile = pathname+T_GFilenames[ii]
959                                                //////////
960                                                // check the empty beam file for previously selected coordinates
961                                                //if they exist, set the xy string , save the normalized counts somewhere
962                                                //the value was written to an unused r*4 header analysis.factor (@b494)
963                                               
964                                                getXYBoxFromFile(emptyFile,x1,x2,y1,y2)
965                                                //read the real count value
966                                                emptyCts = getBoxCounts(emptyFile)
967                                                // read the attenuator number of the empty beam file
968                                                attenEmp = getAttenNumber(emptyFile)
969                                                //
970                                                if( ((x1-x2)==0) || ((y1-y2)==0) )              //zero width marquee in either direction
971                                                        //no region selected    -- prompt user to select box w/marquee
972                                                        DoWindow/F TransFileTable
973                                                        Abort "Use \"Set BCENT File\", then \"SetXYBox\" to select XY range in empty beam file "+T_EMP_Filenames[jj]
974                                                        //from here the Marquee menu selection must handle the task of setting the box and updating the information in the file header
975                                                Endif
976               
977                                                //read in trans file then add to SAM
978                                                ReadHeaderAndData(transFile)
979                                                //adds to SAM
980                                                err = Raw_to_work("SAM")
981                                                //sum region in SAM
982                                                transCts =  SumCountsInBox(0,pixelsX-1,0,pixelsY-1,"SAM")       
983                                                // get the attenuator, lambda, and sample string (to get the instrument)
984                                                WAVE/T samText = $"root:Packages:NIST:SAM:textRead"
985                                                WAVE samReals = $"root:Packages:NIST:SAM:realsRead"
986                                                samfileStr = samText[3]
987                                                lambda = samReals[26]
988                                                attenSam = samReals[3]
989                                                //calculate the ratio of attenuation factors - assumes that same instrument used for each, AND same lambda
990                                                AttenRatio = AttenuationFactor(samFileStr,lambda,attenEmp)/AttenuationFactor(samFileStr,lambda,attenSam)
991                                                //calculate trans based on empty beam value and rescale by attenuation ratio
992                                                trans= transCts/emptyCts * AttenRatio
993                                               
994                                                //write out counts and transmission to history window, showing the attenuator ratio, if it is not unity
995                                                If(attenRatio==1)
996                                                        //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
997                                                        Printf "%s\t\tTotal Counts = %g\t Trans using whole detector = %g\t",T_GFilenames[ii], transCts,trans
998                                                else
999                                                        //Printf "%s\t\tTrans Counts = %g\tTrans = %g\tAttenuatorRatio = %g, Tbox/Twhole = %g\r",T_GFilenames[ii], transCts,trans,attenRatio, T_GTransmission[ii]/trans
1000                                                        Printf "%s\t\tTotal Counts = %g\t Trans using whole detector = %g\t AttenuatorRatio = %g",T_GFilenames[ii], transCts,trans,attenRatio
1001                                                endif
1002                                                If(T_GTransmission[ii]/trans > 0.95)            //95%, from conversation w/Lionel
1003                                                        printf " Tbox/Twhole = %g\r",T_GTransmission[ii]/trans
1004                                                else
1005                                                        printf " !!! Tbox/Twhole is low !!! = %g\r",T_GTransmission[ii]/trans
1006                                                endif
1007                                                //write the trans to the file header of the raw data (open/close done in function)
1008                                                WriteWholeTransToHeader(filename,trans)
1009                                               
1010                                                //then update the global that is displayed
1011                                                GWhole[ii] = trans
1012                                               
1013                                        else  // There is no empty assigned
1014                                                abortStr = "Empty beam file not assigned properly for " + T_GFilenames[jj]
1015                                                Print abortStr
1016                                                //Abort abortStr
1017                                                return(1)
1018                                        endif
1019                                endif
1020                                jj+=1
1021                        while(jj<num_t_files)
1022                else //no transmission file
1023                        abortStr = "Transmission beam file not assigned properly for " + T_GFilenames[ii]
1024                        Print abortStr
1025                endif
1026                ii+=1
1027        while(ii<=endRow)
1028        print "done"
1029        return(0)
1030End
1031
1032
1033//window recreation macro for the Trans Panel
1034//
1035Proc Trans_Panel()
1036        PauseUpdate; Silent 1           // building window...
1037        NewPanel /W=(173,197,540,424)/K=1 as "Calculate Transmissions"
1038        DoWindow/C Trans_Panel
1039        ModifyPanel cbRGB=(49807,47186,38011)
1040        ModifyPanel fixedSize=1
1041        SetDrawLayer UserBack
1042        DrawLine 0,56,368,56
1043        DrawLine 0,125,368,125
1044        Button pick_emp,pos={4,65},size={105,20},proc=PickEMPTransButton,title="set BCENT file"
1045        Button pick_emp,help={"This button will set the file selected in the Transmission file table to be the empty beam file."}
1046        SetVariable empStr,pos={114,67},size={250,17},title="file:"
1047        SetVariable empStr,help={"Filename of the empty beam file(s) to be used in the transmission calculation"}
1048        SetVariable empStr,fSize=10
1049        SetVariable empStr,limits={-Inf,Inf,0},value= root:myGlobals:TransHeaderInfo:gEMP
1050        Button Trn_button_1,pos={5,101},size={90,20},proc=Trn_SetXYBoxButton,title="Set XY Box"
1051        Button Trn_button_1,help={"Sets the XY box to sum over"}
1052        Button Trn_button_2,pos={174,139},size={110,20},proc=Trn_SortFilesByDate,title="Sort by Date"
1053        Button Trn_button_2,help={"Sort the scattering and transmission files by creation date."}
1054        Button Trn_button_3,pos={174,169},size={110,20},proc=Trn_SortFilesByLabel,title="Sort by Label"
1055        Button Trn_button_3,help={"Sort the scattering and transmission files by label."}
1056        Button Trn_button_4,pos={295,139},size={67,20},proc=Trn_ShowHelpProc,title="Help"
1057        Button Trn_button_4,help={"Show a help notebook for calculating transmissions."}
1058        SetVariable Trn_setvar_1,pos={135,100},size={227,17},title="Box is "
1059        SetVariable Trn_setvar_1,help={"Box coordinates to sum over"},fSize=10
1060        SetVariable Trn_setvar_1,limits={-Inf,Inf,0},value= root:myGlobals:TransHeaderInfo:gBox
1061        Button Trn_button_0,pos={1,1},size={70,20},proc=Trn_PickPathButton,title="Pick Path"
1062        Button Trn_button_0,help={"Select the folder containing the SANS data files"}
1063        Button Trn_button_9,pos={335,1},size={25,20},proc=ShowTransHelp,title="?"
1064        Button Trn_button_9,help={"Show the help file for calculating sample transmissions"}
1065        SetVariable Trn_setvar_0,pos={80,4},size={250,17},title="Path"
1066        SetVariable Trn_setvar_0,help={"Currently selected data path"},fSize=10
1067        SetVariable Trn_setvar_0,limits={-Inf,Inf,0},value= root:myGlobals:TransHeaderInfo:gCatPathStr
1068        Button Trn_button_5,pos={5,169},size={161,20},proc=Trn_CalcAllFilesButton,title="Calculate All Files"
1069        Button Trn_button_5,help={"Calculate transmission and patch headers of ALL files in the Scattering File Table."}
1070        Button Trn_button_6,pos={295,198},size={67,20},proc=Trn_PanelDoneButtonProc,title="Done"
1071        Button Trn_button_6,help={"Close the panel when done calculating transmissions"}
1072        Button Trn_button_7,pos={67,32},size={214,20},proc=Trn_RefreshProc,title="List Files"
1073        Button Trn_button_7,help={"Generate or refresh the tables of files."}
1074        Button Trn_button_8,pos={5,139},size={161,20},proc=Trn_CalcSelectedFilesButton,title="Calculate Selected Files"
1075        Button Trn_button_8,help={"Calculate transmission and patch headers of selected files in the Scattering File Table."}
1076        Button Trn_button_10,pos={5,198}, size={161,20},proc=TotalTransButtonProc
1077        Button Trn_button_10 title="Calculate Total Trans"
1078        Button Trn_button_10 help={"Calculate transmission over the whole detector and patch headers of ALL files in the data folder."}
1079EndMacro
1080
1081
1082Function TotalTransButtonProc(ctrlName) : ButtonControl
1083        String ctrlName
1084        TotalTrans(ctrlName)
1085End
1086
1087
1088Proc ShowTransHelp(ctrlName) : ButtonControl
1089        String ctrlName
1090        DisplayHelpTopic/Z/K=1 "SANS Data Reduction Tutorial[Calculate Transmissions]"
1091        if(V_flag !=0)
1092                DoAlert 0,"The SANS Data Reduction Tutorial Help file could not be found"
1093        endif
1094End
1095
1096
1097//generates the file tables of scattering files and transmission files
1098//
1099// if the tables already exist, they will be updated
1100//
1101Proc Trn_RefreshProc(ctrlName) : ButtonControl
1102        String ctrlName
1103        BuildFileTables()
1104End
1105
1106//shows a noteboox of help text describing how to calculate transmissions
1107// - better that the user read the igor or pdf file, but it's a victory to get
1108//users to read anything at all
1109//
1110Proc Trn_ShowHelpProc(ctrlName) : ButtonControl
1111        String ctrlName
1112        DisplayTransHelp()
1113End
1114
1115//sorts the tables by date
1116//
1117Proc Trn_SortFilesByDate(ctrlName) : ButtonControl
1118        String ctrlName
1119        SortTransByDate()
1120End
1121
1122//sorts the tables alphabetically by the sample label field
1123//
1124Proc Trn_SortFilesByLabel(ctrlName) : ButtonControl
1125        String ctrlName
1126        SortTransByLabel()
1127End
1128
1129//button action procedure to select the local path to the
1130//folder containing the RAW SANS data files
1131// - once the data folder is selected, all of the popup file menus are
1132//forced to update with fresh file lists
1133//
1134Function Trn_PickPathButton(PathButton) : ButtonControl
1135        String PathButton
1136       
1137        //set the global string to the selected pathname
1138        Variable err
1139        err = PickPath()
1140        if(err)
1141                return(1)               //some problem with path or user cancelled, get out
1142        endif
1143       
1144        PathInfo/S catPathName
1145        String path = S_path
1146        if (V_flag == 0)
1147                //path does not exist - no folder selected
1148                String/G root:myGlobals:TransHeaderInfo:gCatPathStr = "no folder selected"
1149        else
1150                String/G root:myGlobals:TransHeaderInfo:gCatPathStr = path
1151        endif
1152       
1153        //Update the pathStr variable box
1154        ControlUpdate/W=Trans_Panel $"Trn_setvar_0"
1155        //Generate tables
1156        Execute "BuildFileTables()"
1157       
1158End
1159
1160//button action function that asks the user to set the XY box to sum over
1161//for all transmission calculations
1162//
1163// requires the user to set the box coordinates with the marquee popup
1164//that wil properly set the keyword string, and write the values to the empty beam header
1165//
1166Function Trn_SetXYBoxButton(ctrlName) : ButtonControl
1167        String ctrlName
1168
1169        String msgStr = "Select the Empty Beam File"
1170        String filename=""
1171       
1172        //get the filename from the popup menu
1173        //and construct a full path to it
1174        SVAR partialName = root:myGlobals:TransHeaderInfo:gEMP
1175        print partialName
1176        //get a valid file based on this partialName and catPathName
1177        String tempName = FindValidFilename(partialName)
1178        if(cmpstr(tempName,"")==0)
1179                //file not found, get out
1180                Abort "Empty beam file not found Trn_SetXYBoxButton(ctrlName)"
1181        Endif
1182        //name is ok, prepend path to tempName for read routine
1183        PathInfo catPathName
1184        filename = S_path + tempName
1185       
1186        //read the file in - check for previous coordinates
1187        ReadHeaderAndData(filename)
1188        //data is displayed here (go through the normal display steps, so all is created properly
1189        String/G root:myGlobals:gDataDisplayType="RAW"
1190        fRawWindowHook()
1191       
1192        // check the empty beam file for previously selected coordinates
1193        //if they exist, set the xy string , save the normalized counts somewhere
1194        //the value was written to an unused r*4 header analysis.factor (@b494)
1195        Variable refnum,x1,x2,y1,y2,err
1196        getXYBoxFromFile(filename,x1,x2,y1,y2)
1197       
1198        //read the real count value, assign to a global
1199        Variable/G root:myGlobals:gTransCts = getBoxCounts(filename)                    //***NOTE this is NOT in the Trans sub-folder
1200        //
1201        if( ((x1-x2)==0) || ((y1-y2)==0) )              //zero width marquee in either direction
1202                //no region selected
1203               
1204                //add the empty beam file to work.SAM
1205                err = Raw_to_work("SAM")
1206       
1207                //the calling macro must change the display type
1208                String/G root:myGlobals:gDataDisplayType="SAM"          //displayed data type is sam
1209       
1210                //data is displayed here
1211                fRawWindowHook()
1212       
1213                //prompt user to select box w/marquee
1214                DoAlert 0,"Select the region to sum with the Marquee"
1215       
1216                //from here the Marquee menu selection must handle the task of setting the box
1217                //and updating the information in the file header
1218        else
1219                // region already selected, just put up the values from the file header
1220                //allow user the option of overriding the current box region
1221                msgStr = "X1="+num2str(x1)+";"
1222                msgStr += "X2="+num2str(x2)+";"
1223                msgStr += "Y1="+num2str(y1)+";"
1224                msgStr += "Y2="+num2str(y2)+";"
1225                String textStr
1226                textStr = "Override current box  "+msgStr+" ?"
1227                DoAlert 1,textStr
1228                If((V_flag)==1)
1229                        //get new box coordinates, same procedure as above
1230                        //add the empty beam file to work.SAM
1231                        err = Raw_to_work("SAM")
1232       
1233                        //the calling macro must change the display type
1234                        String/G root:myGlobals:gDataDisplayType="SAM"          //displayed data type is sam
1235       
1236                        //data is displayed here
1237                        fRawWindowHook()
1238       
1239                        //prompt user to select box w/marquee
1240                        DoAlert 0,"Select the region to sum with the Marquee"
1241       
1242                        //from here the Marquee menu selection must handle the task of setting the box
1243                        //and updating the information in the file header
1244                else
1245                        String/G root:myGlobals:TransHeaderInfo:gBox = msgStr
1246                Endif
1247        Endif
1248       
1249        UpdateBoxCoordinates()
1250        Return (0)
1251End
1252
1253
1254//button action function that assigns the selected empty beam file (run number only)
1255//and the sample trnansmissionfile (run number only) to the popup list of  sample scattering
1256//files (ALL of the list items). Assignment is done by writing the integer run numbers of
1257//trans files to the header of each of the  scattering files, in a batchwise processing
1258//of the popup list
1259//
1260//transmission are not calculated here - only the file numbers are set
1261//
1262//*********unused***********
1263//Function Trn_AssignAllFilesButton(ctrlName) : ButtonControl
1264//      String ctrlName
1265//
1266//      AssignTransFilesToData()
1267//             
1268//End
1269
1270//button action procedure to calculate the transmission of all of the sample files in the
1271//sample popup list, based on the information in EACH sample header and in the empty beam file
1272//the box must be set properly in the empty beam file, AND the empty and trans
1273//run number must also be set properly in EACH scattering file,
1274//If run numbers are not properly assigned, no trans is calculated and the
1275//header is not modified
1276//
1277//The RAW data header of EACH sample file IS modified, with the newly calculated transmission
1278//
1279//not very friendly, but forces users to assign the files before continuing
1280//
1281Function Trn_CalcAllFilesButton(ctrlName) : ButtonControl
1282        String ctrlName
1283
1284        //calculate the transmission and Patch the header (if possible)
1285        Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
1286        Variable num_s_files = numpnts(S_GFilenames)
1287        AssignSelTransFilesToData(0,num_s_files-1)              //do all the files (points 0 to n-1, inclusive)
1288        CalcSelTransFromHeader(0,num_s_files-1)
1289       
1290End
1291
1292//for selected scattering files in the scattering table,
1293// the "Annn" association of trans file is written to the data file,
1294// the the transmission is actually calculated
1295//
1296Function Trn_CalcSelectedFilesButton(ctrlName) : ButtonControl
1297        String ctrlName
1298 
1299   Variable scatterTableExists
1300   scatterTableExists = WinType("ScatterFileTable")
1301   if (scatterTableExists != 0)
1302                GetSelection table,ScatterFileTable,1
1303                if(V_Flag != 0)
1304                        AssignSelTransFilesToData(V_StartRow,V_EndRow)
1305                        //calculate the transmission and Patch the header (if possible)
1306                        CalcSelTransFromHeader(V_StartRow,V_EndRow)
1307                Else
1308                        DoAlert 0,"No selection from Scattering Files table"
1309                Endif
1310   Else
1311                DoAlert 0,"No file selected from Scattering Files table or no Scattering Files table available"
1312   Endif
1313End
1314
1315//
1316// I am not sure what the difference is inthe function of CalcTotalTrans and CalcWholeTrans ?
1317// do they really do anything different?
1318// is it a useful calculation at all?
1319//
1320Function TotalTrans(ctrlName) : ButtonControl
1321        String ctrlName
1322 
1323   Variable transTableExists
1324   transTableExists = WinType("TransFileTable")
1325   if (transTableExists != 0)
1326                GetSelection table,transFileTable,1
1327                if(V_Flag != 0)
1328                        AssignTotalTransFilesToData(V_StartRow,V_EndRow)
1329                        //calculate the transmission and Patch the header (if possible)
1330                        CalcTotalTrans(V_StartRow,V_EndRow)
1331                        CalcWholeTrans(V_StartRow,V_EndRow)
1332                Else
1333                        DoAlert 0,"No selection from Trans Files table"
1334                Endif
1335   Else
1336                DoAlert 0,"No file selected from Trans Files table or no Trans Files table available"
1337   Endif
1338End
1339
1340
1341//simple button procedure to close the trans panel
1342// - automatically kills the two tables as well
1343//
1344Function Trn_PanelDoneButtonProc(ctrlName) : ButtonControl
1345        String ctrlName
1346
1347        // this button will make sure all files are closed
1348        //and close the panel
1349       
1350        Close/A
1351        DoWindow/K Trans_Panel
1352        DoWindow/K ScatterFileTable
1353        DoWindow/K TransFileTable
1354End
1355
1356
1357//function to update the box coordinates of the file selected as the
1358//empty beam file - takes the file that is currently popped from the list
1359//reads the 4 "analysis" integers that hold the box coordinates
1360//resets the globals string that is displayed with the new values
1361//should be called whenever the "empty" popup is popped, to ensure
1362//that current header information is displayed
1363//
1364Function UpdateBoxCoordinates()
1365
1366        //construct a full name, and read in the label from the file
1367        //and set the global
1368        String textstr=""
1369        ControlInfo empStr
1370        SVAR item = root:myGlobals:TransHeaderInfo:gEMP
1371        String tempName = FindValidFilename(item)
1372        if(cmpstr(tempName,"")==0)
1373                //file not found, get out
1374                Abort "Empty beam file not found UpdateBoxCoordinates(ctrlName)"
1375        Endif
1376        //name is ok, prepend path to tempName for read routine
1377        PathInfo catPathName
1378        String filename = S_path + tempName
1379       
1380        Variable refnum,x1,x2,y1,y2,err
1381        GetXYBoxFromFile(filename,x1,x2,y1,y2)
1382       
1383        //and update the global string
1384        String msgStr=""
1385        msgStr = "X1="+num2str(x1)+";"
1386        msgStr += "X2="+num2str(x2)+";"
1387        msgStr += "Y1="+num2str(y1)+";"
1388        msgStr += "Y2="+num2str(y2)+";"
1389       
1390        String/G root:myGlobals:TransHeaderInfo:gBox = msgStr
1391       
1392        ControlUpdate/W=Trans_panel Trn_setvar_1
1393End
1394
1395//crude procedure to display a notebook of help information for users
1396//
1397Proc DisplayTransHelp()
1398        String nb = "Notebook0"
1399        NewNotebook/N=$nb/F=1/V=1/W=(342,302,868,674) as "Notebook0:Transmission Help"
1400        Notebook $nb defaultTab=36, statusWidth=238, pageMargins={36,72,36,72}
1401        Notebook $nb showRuler=1, rulerUnits=1, updating={1, 3600}
1402        Notebook $nb newRuler=Normal, justification=0, margins={0,0,468}, spacing={0,0,0}, tabs={}, rulerDefaults={"Geneva",10,0,(0,0,0)}
1403        Notebook $nb ruler=Normal; Notebook $nb  justification=1, fSize=14, fStyle=1, text="Transmission Help\r"
1404        Notebook $nb ruler=Normal, fSize=-1, fStyle=-1, text="\r"
1405        Notebook $nb text="This panel allows quick and easy calculation of sample transmission. The neutron transmission of a sampl"
1406        Notebook $nb text="e must be calculated and entered into the header of each sample scattering file before proper background"
1407        Notebook $nb text=" corrections or absolute scaling can be done.\r"
1408        Notebook $nb text="\r"
1409        Notebook $nb text="During data collection, you measured an empty beam  - that is a measurement with heavy attenuation of th"
1410        Notebook $nb text="e neutron beam and the beamstop mover out of line with the direct beam. Then without changing the attenu"
1411        Notebook $nb text="ation or replacing the beamstop, you put a sample in the beam, and repeated the measurement (a sample tr"
1412        Notebook $nb text="ansmission measurement). The neutron transmission of the sample is simply the ratio of the number of neu"
1413        Notebook $nb text="tron counts from the sample transmission measurement normalized by the number of neutron counts from the"
1414        Notebook $nb text=" empty beam measurement. In this way, each sample transmission file is \"linked\" to the same empty beam t"
1415        Notebook $nb text="ransmission file.\r"
1416        Notebook $nb text="\r"
1417        Notebook $nb text="This calculated transmission value must be entered into the header of the sample scattering measurement "
1418        Notebook $nb text="- that is the measurement of the sample with the beamstop covering the primary (transmitted) beam, and n"
1419        Notebook $nb text="o (or few) attenuators in place. In this way, the sample transmission file is \"linked\" to its correspond"
1420        Notebook $nb text="ing sample scattering file. This panel allows you to set up the links and calculate the transmission.Tra"
1421        Notebook $nb text="nsmisison values are automatically patched to the scattering file headers as they are calculated.\r"
1422        Notebook $nb text="\r"
1423        Notebook $nb text="To Calculate Transmissions:\r"
1424        Notebook $nb text="\r"
1425        Notebook $nb text="1) Click \"List Files\" to get two lists (tables) - one of all the sample scattering files in the folder, "
1426        Notebook $nb text="and one of the transmission files.\r"
1427        Notebook $nb text="\r"
1428        Notebook $nb text="2) Select the region of the detector to sum over from the empty beam transmission file. Do theis by clic"
1429        Notebook $nb text="king on the filename of the empty beam transmision file (from the TransmissionFiles window, in the blue "
1430        Notebook $nb text="\"T_Filenames\" column). Return to the panel, and click the \"set EMP file\" button\", and the filename shoul"
1431        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"
1432        Notebook $nb text="et XY Box\" button. The empty beam file will be displayed, and you will be instructed to select the regio"
1433        Notebook $nb text="n to sum with the marquee. On the data, click and drag a rectangle that encompasses the primary beam. Mo"
1434        Notebook $nb text="ve the cursor inside the selection, to get an \"upside-down hat\" cursor. Click to get a menu, and near th"
1435        Notebook $nb text="e bottom, select \"Set XY Box Coords\". The pixel values should be updated to the Transmission panel, and "
1436        Notebook $nb text="are written to the empty beam header for future calculations. Note that the marquee selection can also b"
1437        Notebook $nb text="e used to measure the beam center, or centroid of any selected region.\r"
1438        Notebook $nb text="\r"
1439        Notebook $nb text="3) Now you need to \"link\" the ", fStyle=2, text="sample", fStyle=-1, text=" transmission files to the "
1440        Notebook $nb fStyle=2, text="empty", fStyle=-1, text=" ", fStyle=2, text="beam", fStyle=-1
1441        Notebook $nb text=" transmission file. Do this in the TransmissionFiles window by selecting the filename (in the blue T_Fil"
1442        Notebook $nb text="enames column) and pasing it to the corresponding row(s) in the T_EMP_Filenames column. This links the e"
1443        Notebook $nb text="mpty beam transmission with the sample transmission file. Do this for every sample transmission file.\r"
1444        Notebook $nb text="\r"
1445        Notebook $nb text="4) Now you need to link the sample ", fStyle=2, text="transmission", fStyle=-1
1446        Notebook $nb text=" file to the sample ", fStyle=2, text="scattering", fStyle=-1
1447        Notebook $nb text=" file. Do this by selecting the name of the transmission file (from the blue T_Filenames column) and pas"
1448        Notebook $nb text="ting it into the corresponding row of the S_TRANS_Filenames column of the ScatteringFiles window. This l"
1449        Notebook $nb text="inks the scattering file to its corresponding sample transmission file. This for all of the sample scatt"
1450        Notebook $nb text="ering files.\r"
1451        Notebook $nb text="\r"
1452        Notebook $nb text="5) Calculate the transmissions (and automatically write the transmission value to the satterng file head"
1453        Notebook $nb text="er) by clicking \"Calculate All Files\" from the Transmission Panel. The results will be printed to the co"
1454        Notebook $nb text="mmand window at the bottom of the screen. Any improperly assigned files will be listed. For example, blo"
1455        Notebook $nb text="cked beam scattering files will return an error, since the transmssion known to be zero, and is not meas"
1456        Notebook $nb text="ured (and no files are \"linked\"). Rather than calculating the transmission of all of the files,  a range"
1457        Notebook $nb text=" of S_Filenames can be selected, then calculated by clicking \"Calculate Selected Files\".\r"
1458        Notebook $nb text="\r"
1459        Notebook $nb text="By default the lists are sorted by run number (= chronological). In some cases it may be easier to sort "
1460        Notebook $nb text="by the sample label to group several of the same sample scattering files collected at different sample-t"
1461        Notebook $nb text="o-detector distances. Its utility depends, of course, on how consistent and unique your sample labels ar"
1462        Notebook $nb text="e.\r"
1463        Notebook $nb text="\r"
1464        Notebook $nb text="The \"links\" are stored in the corresponding files, and wil be re-generated when the lists are re-generat"
1465        Notebook $nb text="ed. Unassigned links will appear as blank elements in the T_EMP_Filenames or S_TRANS_Filenames columns.\r"
1466        Notebook $nb text="\r"
1467        Notebook $nb, selection={startOfFile,startOfFile}
1468        Notebook $nb text="\r"
1469End
1470
1471
1472
1473//*******************
1474//************
1475//   A simple panel to allow users of the NG1 SANS instrument to make their
1476// transmission files "look" like a transmission measurement to the data reduction
1477// program. Transmission files are designated by an x-position of the beamstop of
1478// < -5 (cm). this simple procedure sets xpos=-10 for individual files. files
1479// con be "un-converted", which set the xpos=0.
1480// 1/30/01 SRK
1481//************
1482
1483Proc TransformToTransFile()
1484        Variable/G root:myGlobals:gConvTrans=1
1485        DoWindow/F Convert_to_Trans
1486        if(V_flag==0)
1487                Convert2Trans()
1488        endif
1489End
1490
1491//fname must be the full path and name for the file
1492Function ChangeBSXPos(fname,xpos)
1493        String fname
1494        Variable xpos
1495       
1496        Variable start
1497        //x-position starts after byte 368 in VAX files
1498        WriteBSXPosToHeader(fname,xpos)
1499        return(0)
1500End
1501
1502//sets the beamstop position to zero NOT the original position
1503Function UnConvertButtonProc(ctrlName) : ButtonControl
1504        String ctrlName
1505       
1506        //Print "UnConvert"
1507        String fullname=""
1508        ControlInfo fileNum
1509        Variable num = V_Value  //run number to find
1510       
1511        fullname = FindFileFromRunNumber(num)
1512        Print fullname
1513        //report error or change the file
1514        if(cmpstr(fullname,"")==0)
1515                Print "Unconvert - file not found"
1516        else
1517                //Print "Unconvert",fullname
1518                ChangeBSXPos(fullName,0)
1519        Endif
1520        return(0)
1521End
1522
1523//button procedure to do the conversion
1524// writes fake beamstop position to the data file
1525//
1526Function ConvertButtonProc(ctrlName) : ButtonControl
1527        String ctrlName
1528       
1529        String fullname=""
1530        ControlInfo fileNum
1531        Variable num = V_Value  //run number to find
1532       
1533        fullname = FindFileFromRunNumber(num)
1534        print fullname
1535        //report error or change the file
1536        if(cmpstr(fullname,"")==0)
1537                Print "Convert - file not found"
1538        else
1539                //Print "Convert",fullname
1540                ChangeBSXPos(fullName,-10)
1541        Endif
1542        return(0)
1543End
1544
1545//simple panel recreation macro
1546//
1547Proc Convert2Trans()
1548        PauseUpdate; Silent 1           // building window...
1549        NewPanel /W=(434,74,671,216) /K=1
1550        DoWindow/C Convert_to_Trans
1551        SetDrawLayer UserBack
1552        DrawText 18,22,"Convert files to be recognized"
1553        DrawText 43,39,"as transmission files"
1554        SetVariable fileNum,pos={43,58},size={130,15},title="File Number"
1555        SetVariable fileNum,limits={1,Inf,1},value=root:myGlobals:gConvTrans
1556        SetVariable fileNum,help={"Sets the run number of the file that is to be converted"}
1557        Button ConvButton,pos={73,79},size={80,20},proc=ConvertButtonProc,title="Convert"
1558        Button ConvButton,help={"Converts the chosen file to appear as a transmission file"}
1559        Button UndoButton,pos={68,105},size={90,20},proc=UnConvertButtonProc,title="Un-Convert"
1560        Button UndoButton,help={"Converts the chosen file to appear as a scattering file"}
1561EndMacro
1562
1563
1564/////
1565//  A quick way to fill all the T_EMP_Filenames with the selected empty beam file
1566// Note that you must go back and clear or reassign any files that are special cases
1567//
1568// IMPORTANT - this will overwrite any other file assignments (typically not a problem
1569// if you  are using only one wavelength)
1570//
1571/////
1572Function FillEMPFilenameWSelection()
1573
1574        GetSelection table,TransFileTable,3
1575        Variable row=V_startRow                 //ony need the selected row, we know the column names and location
1576        Wave/T fw = root:myGlobals:TransHeaderInfo:T_FileNames          //trans files
1577        Wave/T ew = root:myGlobals:TransHeaderInfo:T_EMP_FileNames              //empty beam reference files
1578       
1579        ew[] = fw[row]
1580       
1581        return(0)
1582End
1583
1584//given a selection of the SAMPLE files, try to find the corresponding transmission file
1585//
1586Function GuessTransFile(charsToUse,row)
1587        Variable charsToUse,row                 //number of characers to use from the beginning of the label, and the row of the sample file
1588       
1589//      GetSelection table,ScatterFileTable,3
1590//      Variable row=V_startRow                 //ony need the selected row, we know the column names and location
1591        Wave/T sw = root:myGlobals:TransHeaderInfo:S_Labels             //Sample file labels
1592        Wave/T tw = root:myGlobals:TransHeaderInfo:T_Labels             //trans file labels
1593        Wave/T tnam = root:myGlobals:TransHeaderInfo:T_FileNames        //trans file names
1594        Wave/T snam = root:myGlobals:TransHeaderInfo:S_TRANS_FileNames  //Scattering - trans correspondence
1595       
1596        Variable num,ii,found
1597        String samStr = "",testStr=""
1598       
1599        samStr = (sw[row])[0,charsToUse-1]              //string to try and match
1600        samStr += "*"
1601//      Print samStr
1602        num=numpnts(tw)
1603        ii=0
1604        do
1605                testStr=tw[ii]
1606                found = stringmatch(testStr, samStr )
1607                if(found == 1)
1608                        Print "Match Found at:  ",samStr,tnam[ii],tw[ii]
1609                        snam[row] = tnam[ii]                    //write the file name into the table
1610                        return(0)       
1611                endif
1612                ii+=1
1613        while(ii<num)
1614       
1615        //method 2
1616//      samStr = (sw[row])[0,charsToUse-1]              //string to try and match
1617////    Print samStr
1618//      num=numpnts(tw)
1619//      ii=0
1620//      do
1621//              testStr=tw[ii]
1622//              found = strsearch(testStr,samStr,0)             //start from zero, Need Igor 5 to allow 4th parameter to ignore case
1623//              if(found != -1)
1624//                      Print "Match Found at:  ",samStr,tnam[ii],tw[ii]
1625//                      snam[row] = tnam[ii]                    //write the file name into the table
1626//                      return(0)       
1627//              endif
1628//              ii+=1
1629//      while(ii<num)
1630       
1631        Print "No Match found"
1632        return(0)
1633End
1634
1635//given a single transmission file, try to find the corresponding scattering file(s)
1636//
1637// apply TWO criteria
1638// (1) the label must match "well enough"
1639// (2) the wavelength must be the same "enough", given the unnecessary precision reported by ICE
1640//
1641// (for example, "6" can be anything from 6.0008 to 6.0020 in a recent experiment)
1642//
1643// passes the matching rows in the sample table back
1644//
1645Function GuessTransToScattFiles(charsToUse,row,matchRows)
1646        Variable charsToUse,row                 //number of characers to use from the beginning of the label, and the row of the transmission file
1647        Wave matchRows          //rows where (potential) matches found. initially zero points
1648       
1649        Wave/T sw = root:myGlobals:TransHeaderInfo:S_Labels             //Sample file labels
1650        Wave/T tw = root:myGlobals:TransHeaderInfo:T_Labels             //trans file labels
1651        Wave/T tnam = root:myGlobals:TransHeaderInfo:T_FileNames        //trans file names
1652        Wave/T snam = root:myGlobals:TransHeaderInfo:S_TRANS_FileNames  //Scattering - trans correspondence
1653        Wave sLam = root:myGlobals:TransHeaderInfo:S_Lambda             //Scattering file wavelength
1654        Wave tLam = root:myGlobals:TransHeaderInfo:T_Lambda             //Transmission file wavelength
1655       
1656        Variable num,ii,found,tolerance=0.01            //0.01 = 1% tolerance
1657        String transStr = "",testStr=""
1658       
1659        transStr = (tw[row])[0,charsToUse-1]            //string to try and match
1660        transStr += "*"
1661       
1662        //loop over ALL sample files
1663        num=numpnts(sw)
1664        ii=0
1665        do
1666                testStr=sw[ii]
1667                found = stringmatch(testStr, transStr )
1668                if( (found == 1) && (abs(sLam[ii] - tLam[row]) < tolerance) )           // both must match
1669                        Print "Match Found at:  ",transStr,snam[ii],sw[ii]
1670                        InsertPoints numpnts(matchRows), 1, matchRows
1671                        matchRows[numpnts(matchRows)-1] = ii
1672                endif
1673                ii+=1
1674        while(ii<num)
1675       
1676        //method 2 *UNTESTED*
1677//      transStr = (tw[row])[0,charsToUse-1]            //string to try and match
1678//      num=numpnts(sw)
1679//      ii=0
1680//      do
1681//              testStr=sw[ii]
1682//              found = strsearch(testStr,transStr,0,2)         //start from zero, ignore case
1683//              if(found != -1)
1684//                      Print "Match Found at:  ",transStr,snam[ii],sw[ii]
1685//                      InsertPoints numpnts(matchRows), 1, matchRows
1686//                      matchRows[numpnts(matchRows)-1] = ii
1687////                    snam[ii] = tnam[row]                    //write the file name into the table
1688//              endif
1689//              ii+=1
1690//      while(ii<num)
1691       
1692//      Print "No Match found"
1693        return(0)
1694End
1695
1696//get a single selection from the Trans table
1697// find all of the matching scattering files (rows)
1698// ask the user if the selections are OK
1699// if NOT OK, do nothing
1700// if OK: make the assignments, and immediately calculate the transmission
1701//
1702Function fGuessTransToScattFiles(numChars)
1703        Variable numChars
1704       
1705        Variable ii,row
1706        Variable transTableExists
1707        Make/O/D/N=0 root:myGlobals:TransHeaderInfo:matchRows
1708        Wave matchRows=root:myGlobals:TransHeaderInfo:matchRows
1709       
1710        transTableExists = WinType("TransFileTable")
1711        if (transTableExists != 0)
1712                GetSelection table,transFileTable,1     
1713                row = V_startRow
1714                GuessTransToScattFiles(numChars,row,matchRows)
1715        else
1716                Print "Transmission->Scattering File table is not open"
1717                return (1)
1718        endif
1719       
1720        Wave/T sw = root:myGlobals:TransHeaderInfo:S_Labels             //Sample file labels
1721        Wave/T tw = root:myGlobals:TransHeaderInfo:T_Labels             //trans file labels
1722        Wave/T tnam = root:myGlobals:TransHeaderInfo:T_FileNames        //trans file names
1723        Wave/T snam = root:myGlobals:TransHeaderInfo:S_TRANS_FileNames  //Scattering - trans correspondence
1724        Wave/T samfile = root:myGlobals:TransHeaderInfo:S_FileNames     //Scattering file name
1725       
1726        // can do fancy formatted string with ...
1727        //"asdf* matches file: \\f01filen\\f00 \\K(52428,1,1)\\f01afdsfdd\\f00\\K(0,0,0)asdfasdfs"
1728        Variable num=numpnts(matchRows)
1729        String result="",tmpStr
1730        for(ii=0;ii<num;ii+=1)
1731                sprintf tmpStr,"\\f01\\K(52428,1,1)%s\\K(0,0,0)\\f00* Matches file: \\f01%s\\f00 | \\K(52428,1,1)\\f01%s\\f00\\K(0,0,0)%s\r",(tw[row])[0,numChars-1],samfile[matchRows[ii]],(sw[matchRows[ii]])[0,numchars-1],(sw[matchRows[ii]])[numchars,59]
1732                result += tmpStr
1733        endfor
1734       
1735        if(cmpstr(result,"")==0)
1736                result = "No match found for "+ (tw[row])[0,numChars-1]
1737        endif
1738       
1739//      Print result
1740        Print "*******"
1741       
1742        String/G root:myGlobals:TransHeaderInfo:gResultStr = result
1743       
1744        DoWindow/F ConfirmGuess         //it really shouldn't exist...
1745        if(V_flag==1)
1746                TitleBox title0,pos={9,5},variable=root:myGlobals:TransHeaderInfo:gResultStr
1747        else
1748                NewPanel /W=(578,44,1263,214) as "Confirm Guess"
1749                DoWindow/C ConfirmGuess
1750                TitleBox title0,pos={9,5},size={501,32}
1751                TitleBox title0,variable= root:myGlobals:TransHeaderInfo:gResultStr
1752                Button button0,pos={9,107},size={190,20},proc=DoAssignTransButtonProc,title="Assign Transmission Files"
1753                Button button2,pos={341,108},size={90,20},proc=DoTryAgainButtonProc,title="Try Again"
1754                ToolsGrid snap=1,visible=1
1755        endif
1756       
1757        PauseForUser ConfirmGuess
1758
1759// figure out from the return code what the user did...
1760// 1 = OK, accept guess (assign, and calculate immediately)
1761// 2 = try again
1762// 0 = cancel, don't do anything (not used, simply try again)
1763        NVAR guessOK = root:myGlobals:TransHeaderInfo:GuessOK
1764       
1765        // switch here...
1766        switch(guessOK) // numeric switch
1767                case 1:         
1768                        // accept guess (assign, and calculate immediately)
1769                        for(ii=0;ii<num;ii+=1)
1770                                snam[matchRows[ii]] = tnam[row]
1771                                AssignSelTransFilesToData(matchRows[ii],matchRows[ii])
1772                                CalcSelTransFromHeader(matchRows[ii],matchRows[ii])             //does only that sample file
1773                        endfor                 
1774                        break                                           
1775                case 2: //try again (with more / fewer characters?)     
1776                        //              does nothing right now
1777                        break
1778                case 0:
1779                        // do nothing
1780                        break
1781                default:                                                       
1782                        //      do nothing                                     
1783        endswitch
1784       
1785        return(0)
1786End
1787
1788// a hook attached to the Transmission Files Table
1789// given a single selected Trans File, popup gives choices
1790// to guess using "n" characters, then dispatch accordingly
1791Function GuessFromTableHook(infoStr)
1792        String infoStr
1793        String event= StringByKey("EVENT",infoStr)
1794//      Print "EVENT= ",event
1795
1796        String menuStr=""
1797        menuStr += "Guess using N characters;"
1798        menuStr += "Guess using 3 characters;"
1799        menuStr += "Guess using 6 characters;"
1800        menuStr += "Guess using 9 characters;"
1801        menuStr += "Guess using 12 characters;"
1802        menuStr += "Guess using 15 characters;"
1803        menuStr += "Guess using 18 characters;"
1804        menuStr += "Guess using 21 characters;"
1805        menuStr += "Guess using 24 characters;"
1806       
1807        strswitch(event)
1808                case "mousedown":
1809                        Variable xpix= NumberByKey("MOUSEX",infoStr)
1810                        Variable ypix= NumberByKey("MOUSEY",infoStr)
1811                        Variable modif= NumberByKey("MODIFIERS",infoStr)
1812                        //print modif
1813                        if(modif & 2^1)         //bit 1 set, shift key is down
1814                                PopupContextualMenu/C=(xpix, ypix) menuStr
1815                                strswitch(S_selection)
1816                                        case "Guess using 3 characters":
1817                                                fGuessTransToScattFiles(3)
1818                                                break
1819                                        case "Guess using 6 characters":
1820                                                fGuessTransToScattFiles(6)
1821                                                break
1822                                        case "Guess using 9 characters":
1823                                                fGuessTransToScattFiles(9)
1824                                                break
1825                                        case "Guess using 12 characters":
1826                                                fGuessTransToScattFiles(12)
1827                                                break
1828                                        case "Guess using 15 characters":
1829                                                fGuessTransToScattFiles(15)
1830                                                break
1831                                        case "Guess using 18 characters":
1832                                                fGuessTransToScattFiles(18)
1833                                                break
1834                                        case "Guess using 21 characters":
1835                                                fGuessTransToScattFiles(21)
1836                                                break
1837                                        case "Guess using 24 characters":
1838                                                fGuessTransToScattFiles(24)
1839                                                break
1840                                        case "Guess using N characters":
1841                                                Variable num=10
1842                                                Prompt num, "Number of characters"
1843                                                DoPrompt "Enter the number of characters to match", num
1844                                                if (V_Flag)
1845                                                        break   //user cancel, do nothing
1846                                                endif
1847                                                fGuessTransToScattFiles(num)
1848                                                break
1849                                endswitch               //on selection
1850                        endif
1851        endswitch       // on event
1852       
1853        return 0
1854End
1855
1856// sets a flag if the user thinks that the guess was correct, and wants to use the
1857// identified files for the transmission
1858Function DoAssignTransButtonProc(ba) : ButtonControl
1859        STRUCT WMButtonAction &ba
1860
1861        switch( ba.eventCode )
1862                case 2: // mouse up
1863                        // click code here
1864                        //Print "Assigned stuff, and left."
1865                        Variable/G root:myGlobals:TransHeaderInfo:GuessOK= 1
1866                        DoWindow/K ConfirmGuess
1867                        break
1868        endswitch
1869
1870
1871        return 0
1872End
1873
1874// files are wrong, let the user try it again
1875// sets a flag, nothing else
1876Function DoTryAgainButtonProc(ba) : ButtonControl
1877        STRUCT WMButtonAction &ba
1878
1879        switch( ba.eventCode )
1880                case 2: // mouse up
1881                        // click code here
1882                        Variable/G root:myGlobals:TransHeaderInfo:GuessOK= 2
1883                        DoWindow/K ConfirmGuess
1884                        //Print "Try Again"
1885                        break
1886        endswitch
1887       
1888        return 0
1889End
1890
1891// not used
1892Function DoCancelGuessButtonProc(ba) : ButtonControl
1893        STRUCT WMButtonAction &ba
1894
1895        switch( ba.eventCode )
1896                case 2: // mouse up
1897                        // click code here
1898                        //just kill the panel, don't do anything
1899                        Variable/G root:myGlobals:TransHeaderInfo:GuessOK= 0
1900                        DoWindow/K ConfirmGuess
1901                        break
1902        endswitch
1903       
1904        return 0
1905End
1906
1907Function GuessAllTransFiles(numChars)
1908        Variable numChars
1909       
1910        if(WinType("ScatterFileTable") ==0 )
1911                return(1)
1912        endif
1913       
1914        Wave/T sw = root:myGlobals:TransHeaderInfo:S_Labels             //Sample file labels
1915        Variable ii,num=numpnts(sw)
1916       
1917        for(ii=0;ii<num;ii+=1)
1918                GuessTransFile(numChars,ii)
1919        endfor
1920       
1921        return(0)
1922End
1923
1924Function fGuessSelectedTransFiles(numChars)
1925        Variable numChars
1926       
1927        Variable ii
1928        Variable scatterTableExists
1929        scatterTableExists = WinType("ScatterFileTable")
1930        if (scatterTableExists != 0)
1931                GetSelection table,ScatterFileTable,1   
1932                for(ii=V_StartRow;ii<=V_EndRow;ii+=1)
1933                        GuessTransFile(numChars,ii)
1934                endfor
1935        else
1936                Print "Transmission->Scattering File table is not open"
1937        endif
1938        return(0)
1939End
1940
1941Function ClearSelectedAssignments()
1942       
1943        String winStr = WinList("*", ";", "WIN:" )              //returns the target window
1944        Variable scatterTableExists, transTableExists
1945        Print winStr
1946        scatterTableExists = cmpstr(winStr,"ScatterFileTable;")
1947        if (scatterTableExists == 0)
1948                GetSelection table,ScatterFileTable,1   
1949                fClearSelectedAssignments(V_startRow,V_endRow,1)
1950        endif
1951       
1952        transTableExists = cmpstr(winStr,"TransFileTable;")
1953        if (transTableExists == 0)
1954                GetSelection table,TransFileTable,1     
1955                fClearSelectedAssignments(V_startRow,V_endRow,2)
1956        endif
1957       
1958        return(0)
1959End
Note: See TracBrowser for help on using the repository browser.