source: sans/SANSReduction/trunk/Put in User Procedures/SANS_Reduction_v5.00/Transmission.ipf @ 224

Last change on this file since 224 was 224, checked in by srkline, 15 years ago

Updated files for an "improved" way of assigning transmission files. works with a shift-click on the transmission file and a table hook to do the search. User gets to say y/n to the match. no cutting or pasting needed.

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