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

Last change on this file since 399 was 328, checked in by ajj, 15 years ago

Rearranging files

File size: 71.8 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma version=5.0
3#pragma IgorVersion=6.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]"
1069        if(V_flag !=0)
1070                DoAlert 0,"The SANS Data Reduction Tutorial Help file could not be found"
1071        endif
1072End
1073
1074
1075//generates the file tables of scattering files and transmission files
1076//
1077// if the tables already exist, they will be updated
1078//
1079Proc Trn_RefreshProc(ctrlName) : ButtonControl
1080        String ctrlName
1081        BuildFileTables()
1082End
1083
1084//shows a noteboox of help text describing how to calculate transmissions
1085// - better that the user read the igor or pdf file, but it's a victory to get
1086//users to read anything at all
1087//
1088Proc Trn_ShowHelpProc(ctrlName) : ButtonControl
1089        String ctrlName
1090        DisplayTransHelp()
1091End
1092
1093//sorts the tables by date
1094//
1095Proc Trn_SortFilesByDate(ctrlName) : ButtonControl
1096        String ctrlName
1097        SortTransByDate()
1098End
1099
1100//sorts the tables alphabetically by the sample label field
1101//
1102Proc Trn_SortFilesByLabel(ctrlName) : ButtonControl
1103        String ctrlName
1104        SortTransByLabel()
1105End
1106
1107//button action procedure to select the local path to the
1108//folder containing the RAW SANS data files
1109// - once the data folder is selected, all of the popup file menus are
1110//forced to update with fresh file lists
1111//
1112Function Trn_PickPathButton(PathButton) : ButtonControl
1113        String PathButton
1114       
1115        //set the global string to the selected pathname
1116        Variable err
1117        err = PickPath()
1118        if(err)
1119                return(1)               //some problem with path or user cancelled, get out
1120        endif
1121       
1122        PathInfo/S catPathName
1123        String path = S_path
1124        if (V_flag == 0)
1125                //path does not exist - no folder selected
1126                String/G root:myGlobals:TransHeaderInfo:gCatPathStr = "no folder selected"
1127        else
1128                String/G root:myGlobals:TransHeaderInfo:gCatPathStr = path
1129        endif
1130       
1131        //Update the pathStr variable box
1132        ControlUpdate/W=Trans_Panel $"Trn_setvar_0"
1133        //Generate tables
1134        Execute "BuildFileTables()"
1135       
1136End
1137
1138//button action function that asks the user to set the XY box to sum over
1139//for all transmission calculations
1140//
1141// requires the user to set the box coordinates with the marquee popup
1142//that wil properly set the keyword string, and write the values to the empty beam header
1143//
1144Function Trn_SetXYBoxButton(ctrlName) : ButtonControl
1145        String ctrlName
1146
1147        String msgStr = "Select the Empty Beam File"
1148        String filename=""
1149       
1150        //get the filename from the popup menu
1151        //and construct a full path to it
1152        SVAR partialName = root:myGlobals:TransHeaderInfo:gEMP
1153        print partialName
1154        //get a valid file based on this partialName and catPathName
1155        String tempName = FindValidFilename(partialName)
1156        if(cmpstr(tempName,"")==0)
1157                //file not found, get out
1158                Abort "Empty beam file not found Trn_SetXYBoxButton(ctrlName)"
1159        Endif
1160        //name is ok, prepend path to tempName for read routine
1161        PathInfo catPathName
1162        filename = S_path + tempName
1163       
1164        //read the file in - check for previous coordinates
1165        ReadHeaderAndData(filename)
1166        //data is displayed here (go through the normal display steps, so all is created properly
1167        String/G root:myGlobals:gDataDisplayType="RAW"
1168        fRawWindowHook()
1169       
1170        // check the empty beam file for previously selected coordinates
1171        //if they exist, set the xy string , save the normalized counts somewhere
1172        //the value was written to an unused r*4 header analysis.factor (@b494)
1173        Variable refnum,x1,x2,y1,y2,err
1174        getXYBoxFromFile(filename,x1,x2,y1,y2)
1175       
1176        //read the real count value, assign to a global
1177        Variable/G root:myGlobals:gTransCts = getBoxCounts(filename)                    //***NOTE this is NOT in the Trans sub-folder
1178        //
1179        if( ((x1-x2)==0) || ((y1-y2)==0) )              //zero width marquee in either direction
1180                //no region selected
1181               
1182                //add the empty beam file to work.SAM
1183                err = Raw_to_work("SAM")
1184       
1185                //the calling macro must change the display type
1186                String/G root:myGlobals:gDataDisplayType="SAM"          //displayed data type is sam
1187       
1188                //data is displayed here
1189                fRawWindowHook()
1190       
1191                //prompt user to select box w/marquee
1192                DoAlert 0,"Select the region to sum with the Marquee"
1193       
1194                //from here the Marquee menu selection must handle the task of setting the box
1195                //and updating the information in the file header
1196        else
1197                // region already selected, just put up the values from the file header
1198                //allow user the option of overriding the current box region
1199                msgStr = "X1="+num2str(x1)+";"
1200                msgStr += "X2="+num2str(x2)+";"
1201                msgStr += "Y1="+num2str(y1)+";"
1202                msgStr += "Y2="+num2str(y2)+";"
1203                String textStr
1204                textStr = "Override current box  "+msgStr+" ?"
1205                DoAlert 1,textStr
1206                If((V_flag)==1)
1207                        //get new box coordinates, same procedure as above
1208                        //add the empty beam file to work.SAM
1209                        err = Raw_to_work("SAM")
1210       
1211                        //the calling macro must change the display type
1212                        String/G root:myGlobals:gDataDisplayType="SAM"          //displayed data type is sam
1213       
1214                        //data is displayed here
1215                        fRawWindowHook()
1216       
1217                        //prompt user to select box w/marquee
1218                        DoAlert 0,"Select the region to sum with the Marquee"
1219       
1220                        //from here the Marquee menu selection must handle the task of setting the box
1221                        //and updating the information in the file header
1222                else
1223                        String/G root:myGlobals:TransHeaderInfo:gBox = msgStr
1224                Endif
1225        Endif
1226       
1227        UpdateBoxCoordinates()
1228        Return (0)
1229End
1230
1231
1232//button action function that assigns the selected empty beam file (run number only)
1233//and the sample trnansmissionfile (run number only) to the popup list of  sample scattering
1234//files (ALL of the list items). Assignment is done by writing the integer run numbers of
1235//trans files to the header of each of the  scattering files, in a batchwise processing
1236//of the popup list
1237//
1238//transmission are not calculated here - only the file numbers are set
1239//
1240//*********unused***********
1241//Function Trn_AssignAllFilesButton(ctrlName) : ButtonControl
1242//      String ctrlName
1243//
1244//      AssignTransFilesToData()
1245//             
1246//End
1247
1248//button action procedure to calculate the transmission of all of the sample files in the
1249//sample popup list, based on the information in EACH sample header and in the empty beam file
1250//the box must be set properly in the empty beam file, AND the empty and trans
1251//run number must also be set properly in EACH scattering file,
1252//If run numbers are not properly assigned, no trans is calculated and the
1253//header is not modified
1254//
1255//The RAW data header of EACH sample file IS modified, with the newly calculated transmission
1256//
1257//not very friendly, but forces users to assign the files before continuing
1258//
1259Function Trn_CalcAllFilesButton(ctrlName) : ButtonControl
1260        String ctrlName
1261
1262        //calculate the transmission and Patch the header (if possible)
1263        Wave/T S_GFilenames = $"root:myGlobals:TransHeaderInfo:S_Filenames"
1264        Variable num_s_files = numpnts(S_GFilenames)
1265        AssignSelTransFilesToData(0,num_s_files-1)              //do all the files (points 0 to n-1, inclusive)
1266        CalcSelTransFromHeader(0,num_s_files-1)
1267       
1268End
1269
1270//for selected scattering files in the scattering table,
1271// the "Annn" association of trans file is written to the data file,
1272// the the transmission is actually calculated
1273//
1274Function Trn_CalcSelectedFilesButton(ctrlName) : ButtonControl
1275        String ctrlName
1276 
1277   Variable scatterTableExists
1278   scatterTableExists = WinType("ScatterFileTable")
1279   if (scatterTableExists != 0)
1280                GetSelection table,ScatterFileTable,1
1281                if(V_Flag != 0)
1282                        AssignSelTransFilesToData(V_StartRow,V_EndRow)
1283                        //calculate the transmission and Patch the header (if possible)
1284                        CalcSelTransFromHeader(V_StartRow,V_EndRow)
1285                Else
1286                        DoAlert 0,"No selection from Scattering Files table"
1287                Endif
1288   Else
1289                DoAlert 0,"No file selected from Scattering Files table or no Scattering Files table available"
1290   Endif
1291End
1292
1293//
1294// I am not sure what the difference is inthe function of CalcTotalTrans and CalcWholeTrans ?
1295// do they really do anything different?
1296// is it a useful calculation at all?
1297//
1298Function TotalTrans(ctrlName) : ButtonControl
1299        String ctrlName
1300 
1301   Variable transTableExists
1302   transTableExists = WinType("TransFileTable")
1303   if (transTableExists != 0)
1304                GetSelection table,transFileTable,1
1305                if(V_Flag != 0)
1306                        AssignTotalTransFilesToData(V_StartRow,V_EndRow)
1307                        //calculate the transmission and Patch the header (if possible)
1308                        CalcTotalTrans(V_StartRow,V_EndRow)
1309                        CalcWholeTrans(V_StartRow,V_EndRow)
1310                Else
1311                        DoAlert 0,"No selection from Trans Files table"
1312                Endif
1313   Else
1314                DoAlert 0,"No file selected from Trans Files table or no Trans Files table available"
1315   Endif
1316End
1317
1318
1319//simple button procedure to close the trans panel
1320// - automatically kills the two tables as well
1321//
1322Function Trn_PanelDoneButtonProc(ctrlName) : ButtonControl
1323        String ctrlName
1324
1325        // this button will make sure all files are closed
1326        //and close the panel
1327       
1328        Close/A
1329        DoWindow/K Trans_Panel
1330        DoWindow/K ScatterFileTable
1331        DoWindow/K TransFileTable
1332End
1333
1334
1335//function to update the box coordinates of the file selected as the
1336//empty beam file - takes the file that is currently popped from the list
1337//reads the 4 "analysis" integers that hold the box coordinates
1338//resets the globals string that is displayed with the new values
1339//should be called whenever the "empty" popup is popped, to ensure
1340//that current header information is displayed
1341//
1342Function UpdateBoxCoordinates()
1343
1344        //construct a full name, and read in the label from the file
1345        //and set the global
1346        String textstr=""
1347        ControlInfo empStr
1348        SVAR item = root:myGlobals:TransHeaderInfo:gEMP
1349        String tempName = FindValidFilename(item)
1350        if(cmpstr(tempName,"")==0)
1351                //file not found, get out
1352                Abort "Empty beam file not found UpdateBoxCoordinates(ctrlName)"
1353        Endif
1354        //name is ok, prepend path to tempName for read routine
1355        PathInfo catPathName
1356        String filename = S_path + tempName
1357       
1358        Variable refnum,x1,x2,y1,y2,err
1359        GetXYBoxFromFile(filename,x1,x2,y1,y2)
1360               
1361        //and update the global string
1362        String msgStr=""
1363        msgStr = "X1="+num2str(x1)+";"
1364        msgStr += "X2="+num2str(x2)+";"
1365        msgStr += "Y1="+num2str(y1)+";"
1366        msgStr += "Y2="+num2str(y2)+";"
1367       
1368        String/G root:myGlobals:TransHeaderInfo:gBox = msgStr
1369       
1370        ControlUpdate/W=Trans_panel Trn_setvar_1
1371End
1372
1373//crude procedure to display a notebook of help information for users
1374//
1375Proc DisplayTransHelp()
1376        String nb = "Notebook0"
1377        NewNotebook/N=$nb/F=1/V=1/W=(342,302,868,674) as "Notebook0:Transmission Help"
1378        Notebook $nb defaultTab=36, statusWidth=238, pageMargins={36,72,36,72}
1379        Notebook $nb showRuler=1, rulerUnits=1, updating={1, 3600}
1380        Notebook $nb newRuler=Normal, justification=0, margins={0,0,468}, spacing={0,0,0}, tabs={}, rulerDefaults={"Geneva",10,0,(0,0,0)}
1381        Notebook $nb ruler=Normal; Notebook $nb  justification=1, fSize=14, fStyle=1, text="Transmission Help\r"
1382        Notebook $nb ruler=Normal, fSize=-1, fStyle=-1, text="\r"
1383        Notebook $nb text="This panel allows quick and easy calculation of sample transmission. The neutron transmission of a sampl"
1384        Notebook $nb text="e must be calculated and entered into the header of each sample scattering file before proper background"
1385        Notebook $nb text=" corrections or absolute scaling can be done.\r"
1386        Notebook $nb text="\r"
1387        Notebook $nb text="During data collection, you measured an empty beam  - that is a measurement with heavy attenuation of th"
1388        Notebook $nb text="e neutron beam and the beamstop mover out of line with the direct beam. Then without changing the attenu"
1389        Notebook $nb text="ation or replacing the beamstop, you put a sample in the beam, and repeated the measurement (a sample tr"
1390        Notebook $nb text="ansmission measurement). The neutron transmission of the sample is simply the ratio of the number of neu"
1391        Notebook $nb text="tron counts from the sample transmission measurement normalized by the number of neutron counts from the"
1392        Notebook $nb text=" empty beam measurement. In this way, each sample transmission file is \"linked\" to the same empty beam t"
1393        Notebook $nb text="ransmission file.\r"
1394        Notebook $nb text="\r"
1395        Notebook $nb text="This calculated transmission value must be entered into the header of the sample scattering measurement "
1396        Notebook $nb text="- that is the measurement of the sample with the beamstop covering the primary (transmitted) beam, and n"
1397        Notebook $nb text="o (or few) attenuators in place. In this way, the sample transmission file is \"linked\" to its correspond"
1398        Notebook $nb text="ing sample scattering file. This panel allows you to set up the links and calculate the transmission.Tra"
1399        Notebook $nb text="nsmisison values are automatically patched to the scattering file headers as they are calculated.\r"
1400        Notebook $nb text="\r"
1401        Notebook $nb text="To Calculate Transmissions:\r"
1402        Notebook $nb text="\r"
1403        Notebook $nb text="1) Click \"List Files\" to get two lists (tables) - one of all the sample scattering files in the folder, "
1404        Notebook $nb text="and one of the transmission files.\r"
1405        Notebook $nb text="\r"
1406        Notebook $nb text="2) Select the region of the detector to sum over from the empty beam transmission file. Do theis by clic"
1407        Notebook $nb text="king on the filename of the empty beam transmision file (from the TransmissionFiles window, in the blue "
1408        Notebook $nb text="\"T_Filenames\" column). Return to the panel, and click the \"set EMP file\" button\", and the filename shoul"
1409        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"
1410        Notebook $nb text="et XY Box\" button. The empty beam file will be displayed, and you will be instructed to select the regio"
1411        Notebook $nb text="n to sum with the marquee. On the data, click and drag a rectangle that encompasses the primary beam. Mo"
1412        Notebook $nb text="ve the cursor inside the selection, to get an \"upside-down hat\" cursor. Click to get a menu, and near th"
1413        Notebook $nb text="e bottom, select \"Set XY Box Coords\". The pixel values should be updated to the Transmission panel, and "
1414        Notebook $nb text="are written to the empty beam header for future calculations. Note that the marquee selection can also b"
1415        Notebook $nb text="e used to measure the beam center, or centroid of any selected region.\r"
1416        Notebook $nb text="\r"
1417        Notebook $nb text="3) Now you need to \"link\" the ", fStyle=2, text="sample", fStyle=-1, text=" transmission files to the "
1418        Notebook $nb fStyle=2, text="empty", fStyle=-1, text=" ", fStyle=2, text="beam", fStyle=-1
1419        Notebook $nb text=" transmission file. Do this in the TransmissionFiles window by selecting the filename (in the blue T_Fil"
1420        Notebook $nb text="enames column) and pasing it to the corresponding row(s) in the T_EMP_Filenames column. This links the e"
1421        Notebook $nb text="mpty beam transmission with the sample transmission file. Do this for every sample transmission file.\r"
1422        Notebook $nb text="\r"
1423        Notebook $nb text="4) Now you need to link the sample ", fStyle=2, text="transmission", fStyle=-1
1424        Notebook $nb text=" file to the sample ", fStyle=2, text="scattering", fStyle=-1
1425        Notebook $nb text=" file. Do this by selecting the name of the transmission file (from the blue T_Filenames column) and pas"
1426        Notebook $nb text="ting it into the corresponding row of the S_TRANS_Filenames column of the ScatteringFiles window. This l"
1427        Notebook $nb text="inks the scattering file to its corresponding sample transmission file. This for all of the sample scatt"
1428        Notebook $nb text="ering files.\r"
1429        Notebook $nb text="\r"
1430        Notebook $nb text="5) Calculate the transmissions (and automatically write the transmission value to the satterng file head"
1431        Notebook $nb text="er) by clicking \"Calculate All Files\" from the Transmission Panel. The results will be printed to the co"
1432        Notebook $nb text="mmand window at the bottom of the screen. Any improperly assigned files will be listed. For example, blo"
1433        Notebook $nb text="cked beam scattering files will return an error, since the transmssion known to be zero, and is not meas"
1434        Notebook $nb text="ured (and no files are \"linked\"). Rather than calculating the transmission of all of the files,  a range"
1435        Notebook $nb text=" of S_Filenames can be selected, then calculated by clicking \"Calculate Selected Files\".\r"
1436        Notebook $nb text="\r"
1437        Notebook $nb text="By default the lists are sorted by run number (= chronological). In some cases it may be easier to sort "
1438        Notebook $nb text="by the sample label to group several of the same sample scattering files collected at different sample-t"
1439        Notebook $nb text="o-detector distances. Its utility depends, of course, on how consistent and unique your sample labels ar"
1440        Notebook $nb text="e.\r"
1441        Notebook $nb text="\r"
1442        Notebook $nb text="The \"links\" are stored in the corresponding files, and wil be re-generated when the lists are re-generat"
1443        Notebook $nb text="ed. Unassigned links will appear as blank elements in the T_EMP_Filenames or S_TRANS_Filenames columns.\r"
1444        Notebook $nb text="\r"
1445        Notebook $nb, selection={startOfFile,startOfFile}
1446        Notebook $nb text="\r"
1447End
1448
1449
1450
1451//*******************
1452//************
1453//   A simple panel to allow users of the NG1 SANS instrument to make their
1454// transmission files "look" like a transmission measurement to the data reduction
1455// program. Transmission files are designated by an x-position of the beamstop of
1456// < -5 (cm). this simple procedure sets xpos=-10 for individual files. files
1457// con be "un-converted", which set the xpos=0.
1458// 1/30/01 SRK
1459//************
1460
1461Proc TransformToTransFile()
1462        Variable/G root:myGlobals:gConvTrans=1
1463        DoWindow/F Convert_to_Trans
1464        if(V_flag==0)
1465                Convert2Trans()
1466        endif
1467End
1468
1469//fname must be the full path and name for the file
1470Function ChangeBSXPos(fname,xpos)
1471        String fname
1472        Variable xpos
1473       
1474        Variable start
1475        //x-position starts after byte 368 in VAX files
1476        WriteBSXPosToHeader(fname,xpos)
1477        return(0)
1478End
1479
1480//sets the beamstop position to zero NOT the original position
1481Function UnConvertButtonProc(ctrlName) : ButtonControl
1482        String ctrlName
1483       
1484        //Print "UnConvert"
1485        String fullname=""
1486        ControlInfo fileNum
1487        Variable num = V_Value  //run number to find
1488       
1489        fullname = FindFileFromRunNumber(num)
1490        Print fullname
1491        //report error or change the file
1492        if(cmpstr(fullname,"")==0)
1493                Print "Unconvert - file not found"
1494        else
1495                //Print "Unconvert",fullname
1496                ChangeBSXPos(fullName,0)
1497        Endif
1498        return(0)
1499End
1500
1501//button procedure to do the conversion
1502// writes fake beamstop position to the data file
1503//
1504Function ConvertButtonProc(ctrlName) : ButtonControl
1505        String ctrlName
1506       
1507        String fullname=""
1508        ControlInfo fileNum
1509        Variable num = V_Value  //run number to find
1510       
1511        fullname = FindFileFromRunNumber(num)
1512        print fullname
1513        //report error or change the file
1514        if(cmpstr(fullname,"")==0)
1515                Print "Convert - file not found"
1516        else
1517                //Print "Convert",fullname
1518                ChangeBSXPos(fullName,-10)
1519        Endif
1520        return(0)
1521End
1522
1523//simple panel recreation macro
1524//
1525Proc Convert2Trans()
1526        PauseUpdate; Silent 1           // building window...
1527        NewPanel /W=(434,74,671,216) /K=1
1528        DoWindow/C Convert_to_Trans
1529        SetDrawLayer UserBack
1530        DrawText 18,22,"Convert files to be recognized"
1531        DrawText 43,39,"as transmission files"
1532        SetVariable fileNum,pos={43,58},size={130,15},title="File Number"
1533        SetVariable fileNum,limits={1,Inf,1},value=root:myGlobals:gConvTrans
1534        SetVariable fileNum,help={"Sets the run number of the file that is to be converted"}
1535        Button ConvButton,pos={73,79},size={80,20},proc=ConvertButtonProc,title="Convert"
1536        Button ConvButton,help={"Converts the chosen file to appear as a transmission file"}
1537        Button UndoButton,pos={68,105},size={90,20},proc=UnConvertButtonProc,title="Un-Convert"
1538        Button UndoButton,help={"Converts the chosen file to appear as a scattering file"}
1539EndMacro
1540
1541
1542/////
1543//  A quick way to fill all the T_EMP_Filenames with the selected empty beam file
1544// Note that you must go back and clear or reassign any files that are special cases
1545//
1546// IMPORTANT - this will overwrite any other file assignments (typically not a problem
1547// if you  are using only one wavelength)
1548//
1549/////
1550Function FillEMPFilenameWSelection()
1551
1552        GetSelection table,TransFileTable,3
1553        Variable row=V_startRow                 //ony need the selected row, we know the column names and location
1554        Wave/T fw = root:myGlobals:TransHeaderInfo:T_FileNames          //trans files
1555        Wave/T ew = root:myGlobals:TransHeaderInfo:T_EMP_FileNames              //empty beam reference files
1556       
1557        ew[] = fw[row]
1558       
1559        return(0)
1560End
1561
1562//given a selection of the SAMPLE files, try to find the corresponding transmission file
1563//
1564Function GuessTransFile(charsToUse,row)
1565        Variable charsToUse,row                 //number of characers to use from the beginning of the label, and the row of the sample file
1566       
1567//      GetSelection table,ScatterFileTable,3
1568//      Variable row=V_startRow                 //ony need the selected row, we know the column names and location
1569        Wave/T sw = root:myGlobals:TransHeaderInfo:S_Labels             //Sample file labels
1570        Wave/T tw = root:myGlobals:TransHeaderInfo:T_Labels             //trans file labels
1571        Wave/T tnam = root:myGlobals:TransHeaderInfo:T_FileNames        //trans file names
1572        Wave/T snam = root:myGlobals:TransHeaderInfo:S_TRANS_FileNames  //Scattering - trans correspondence
1573       
1574        Variable num,ii,found
1575        String samStr = "",testStr=""
1576       
1577        samStr = (sw[row])[0,charsToUse-1]              //string to try and match
1578        samStr += "*"
1579//      Print samStr
1580        num=numpnts(tw)
1581        ii=0
1582        do
1583                testStr=tw[ii]
1584                found = stringmatch(testStr, samStr )
1585                if(found == 1)
1586                        Print "Match Found at:  ",samStr,tnam[ii],tw[ii]
1587                        snam[row] = tnam[ii]                    //write the file name into the table
1588                        return(0)       
1589                endif
1590                ii+=1
1591        while(ii<num)
1592       
1593        //method 2
1594//      samStr = (sw[row])[0,charsToUse-1]              //string to try and match
1595////    Print samStr
1596//      num=numpnts(tw)
1597//      ii=0
1598//      do
1599//              testStr=tw[ii]
1600//              found = strsearch(testStr,samStr,0)             //start from zero, Need Igor 5 to allow 4th parameter to ignore case
1601//              if(found != -1)
1602//                      Print "Match Found at:  ",samStr,tnam[ii],tw[ii]
1603//                      snam[row] = tnam[ii]                    //write the file name into the table
1604//                      return(0)       
1605//              endif
1606//              ii+=1
1607//      while(ii<num)
1608       
1609        Print "No Match found"
1610        return(0)
1611End
1612
1613//given a single transmission file, try to find the corresponding scattering file(s)
1614//
1615// apply TWO criteria
1616// (1) the label must match "well enough"
1617// (2) the wavelength must be the same
1618//
1619// passes the matching rows in the sample table back
1620//
1621Function GuessTransToScattFiles(charsToUse,row,matchRows)
1622        Variable charsToUse,row                 //number of characers to use from the beginning of the label, and the row of the transmission file
1623        Wave matchRows          //rows where (potential) matches found. initially zero points
1624       
1625        Wave/T sw = root:myGlobals:TransHeaderInfo:S_Labels             //Sample file labels
1626        Wave/T tw = root:myGlobals:TransHeaderInfo:T_Labels             //trans file labels
1627        Wave/T tnam = root:myGlobals:TransHeaderInfo:T_FileNames        //trans file names
1628        Wave/T snam = root:myGlobals:TransHeaderInfo:S_TRANS_FileNames  //Scattering - trans correspondence
1629        Wave sLam = root:myGlobals:TransHeaderInfo:S_Lambda             //Scattering file wavelength
1630        Wave tLam = root:myGlobals:TransHeaderInfo:T_Lambda             //Transmission file wavelength
1631       
1632        Variable num,ii,found
1633        String transStr = "",testStr=""
1634       
1635        transStr = (tw[row])[0,charsToUse-1]            //string to try and match
1636        transStr += "*"
1637       
1638        //loop over ALL sample files
1639        num=numpnts(sw)
1640        ii=0
1641        do
1642                testStr=sw[ii]
1643                found = stringmatch(testStr, transStr )
1644                if( (found == 1) && (sLam[ii] == tLam[row]) )           // both must match
1645                        Print "Match Found at:  ",transStr,snam[ii],sw[ii]
1646                        InsertPoints numpnts(matchRows), 1, matchRows
1647                        matchRows[numpnts(matchRows)-1] = ii
1648                endif
1649                ii+=1
1650        while(ii<num)
1651       
1652        //method 2 *UNTESTED*
1653//      transStr = (tw[row])[0,charsToUse-1]            //string to try and match
1654//      num=numpnts(sw)
1655//      ii=0
1656//      do
1657//              testStr=sw[ii]
1658//              found = strsearch(testStr,transStr,0,2)         //start from zero, ignore case
1659//              if(found != -1)
1660//                      Print "Match Found at:  ",transStr,snam[ii],sw[ii]
1661//                      InsertPoints numpnts(matchRows), 1, matchRows
1662//                      matchRows[numpnts(matchRows)-1] = ii
1663////                    snam[ii] = tnam[row]                    //write the file name into the table
1664//              endif
1665//              ii+=1
1666//      while(ii<num)
1667       
1668//      Print "No Match found"
1669        return(0)
1670End
1671
1672//get a single selection from the Trans table
1673// find all of the matching scattering files (rows)
1674// ask the user if the selections are OK
1675// if NOT OK, do nothing
1676// if OK: make the assignments, and immediately calculate the transmission
1677//
1678Function fGuessTransToScattFiles(numChars)
1679        Variable numChars
1680       
1681        Variable ii,row
1682        Variable transTableExists
1683        Make/O/D/N=0 root:myGlobals:TransHeaderInfo:matchRows
1684        Wave matchRows=root:myGlobals:TransHeaderInfo:matchRows
1685       
1686        transTableExists = WinType("TransFileTable")
1687        if (transTableExists != 0)
1688                GetSelection table,transFileTable,1     
1689                row = V_startRow
1690                GuessTransToScattFiles(numChars,row,matchRows)
1691        else
1692                Print "Transmission->Scattering File table is not open"
1693                return (1)
1694        endif
1695       
1696        Wave/T sw = root:myGlobals:TransHeaderInfo:S_Labels             //Sample file labels
1697        Wave/T tw = root:myGlobals:TransHeaderInfo:T_Labels             //trans file labels
1698        Wave/T tnam = root:myGlobals:TransHeaderInfo:T_FileNames        //trans file names
1699        Wave/T snam = root:myGlobals:TransHeaderInfo:S_TRANS_FileNames  //Scattering - trans correspondence
1700        Wave/T samfile = root:myGlobals:TransHeaderInfo:S_FileNames     //Scattering file name
1701       
1702        // can do fancy formatted string with ...
1703        //"asdf* matches file: \\f01filen\\f00 \\K(52428,1,1)\\f01afdsfdd\\f00\\K(0,0,0)asdfasdfs"
1704        Variable num=numpnts(matchRows)
1705        String result="",tmpStr
1706        for(ii=0;ii<num;ii+=1)
1707                sprintf tmpStr,"\\f01\\K(52428,1,1)%s\\K(0,0,0)\\f00* Matches file: \\f01%s\\f00 | \\K(52428,1,1)\\f01%s\\f00\\K(0,0,0)%s\r",(tw[row])[0,numChars-1],samfile[matchRows[ii]],(sw[matchRows[ii]])[0,numchars-1],(sw[matchRows[ii]])[numchars,59]
1708                result += tmpStr
1709        endfor
1710       
1711        if(cmpstr(result,"")==0)
1712                result = "No match found for "+ (tw[row])[0,numChars-1]
1713        endif
1714       
1715//      Print result
1716        Print "*******"
1717       
1718        String/G root:myGlobals:TransHeaderInfo:gResultStr = result
1719       
1720        DoWindow/F ConfirmGuess         //it really shouldn't exist...
1721        if(V_flag==1)
1722                TitleBox title0,pos={9,5},variable=root:myGlobals:TransHeaderInfo:gResultStr
1723        else
1724                NewPanel /W=(578,44,1263,214) as "Confirm Guess"
1725                DoWindow/C ConfirmGuess
1726                TitleBox title0,pos={9,5},size={501,32}
1727                TitleBox title0,variable= root:myGlobals:TransHeaderInfo:gResultStr
1728                Button button0,pos={9,107},size={190,20},proc=DoAssignTransButtonProc,title="Assign Transmission Files"
1729                Button button2,pos={341,108},size={90,20},proc=DoTryAgainButtonProc,title="Try Again"
1730                ToolsGrid snap=1,visible=1
1731        endif
1732       
1733        PauseForUser ConfirmGuess
1734
1735// figure out from the return code what the user did...
1736// 1 = OK, accept guess (assign, and calculate immediately)
1737// 2 = try again
1738// 0 = cancel, don't do anything (not used, simply try again)
1739        NVAR guessOK = root:myGlobals:TransHeaderInfo:GuessOK
1740       
1741        // switch here...
1742        switch(guessOK) // numeric switch
1743                case 1:         
1744                        // accept guess (assign, and calculate immediately)
1745                        for(ii=0;ii<num;ii+=1)
1746                                snam[matchRows[ii]] = tnam[row]
1747                                AssignSelTransFilesToData(matchRows[ii],matchRows[ii])
1748                                CalcSelTransFromHeader(matchRows[ii],matchRows[ii])             //does only that sample file
1749                        endfor                 
1750                        break                                           
1751                case 2: //try again (with more / fewer characters?)     
1752                        //              does nothing right now
1753                        break
1754                case 0:
1755                        // do nothing
1756                        break
1757                default:                                                       
1758                        //      do nothing                                     
1759        endswitch
1760       
1761        return(0)
1762End
1763
1764// a hook attached to the Transmission Files Table
1765// given a single selected Trans File, popup gives choices
1766// to guess using "n" characters, then dispatch accordingly
1767Function GuessFromTableHook(infoStr)
1768        String infoStr
1769        String event= StringByKey("EVENT",infoStr)
1770//      Print "EVENT= ",event
1771
1772        String menuStr=""
1773        menuStr += "Guess using 3 characters;"
1774        menuStr += "Guess using 6 characters;"
1775        menuStr += "Guess using 9 characters;"
1776        menuStr += "Guess using 12 characters;"
1777        menuStr += "Guess using 15 characters;"
1778        menuStr += "Guess using 18 characters;"
1779        menuStr += "Guess using 21 characters;"
1780        menuStr += "Guess using 24 characters;"
1781       
1782        strswitch(event)
1783                case "mousedown":
1784                        Variable xpix= NumberByKey("MOUSEX",infoStr)
1785                        Variable ypix= NumberByKey("MOUSEY",infoStr)
1786                        Variable modif= NumberByKey("MODIFIERS",infoStr)
1787                        //print modif
1788                        if(modif & 2^1)         //bit 1 set, shift key is down
1789                                PopupContextualMenu/C=(xpix, ypix) menuStr
1790                                strswitch(S_selection)
1791                                        case "Guess using 3 characters":
1792                                                fGuessTransToScattFiles(3)
1793                                                break
1794                                        case "Guess using 6 characters":
1795                                                fGuessTransToScattFiles(6)
1796                                                break
1797                                        case "Guess using 9 characters":
1798                                                fGuessTransToScattFiles(9)
1799                                                break
1800                                        case "Guess using 12 characters":
1801                                                fGuessTransToScattFiles(12)
1802                                                break
1803                                        case "Guess using 15 characters":
1804                                                fGuessTransToScattFiles(15)
1805                                                break
1806                                        case "Guess using 18 characters":
1807                                                fGuessTransToScattFiles(18)
1808                                                break
1809                                        case "Guess using 21 characters":
1810                                                fGuessTransToScattFiles(21)
1811                                                break
1812                                        case "Guess using 24 characters":
1813                                                fGuessTransToScattFiles(24)
1814                                                break
1815                                endswitch               //on selection
1816                        endif
1817        endswitch       // on event
1818       
1819        return 0
1820End
1821
1822// sets a flag if the user thinks that the guess was correct, and wants to use the
1823// identified files for the transmission
1824Function DoAssignTransButtonProc(ba) : ButtonControl
1825        STRUCT WMButtonAction &ba
1826
1827        switch( ba.eventCode )
1828                case 2: // mouse up
1829                        // click code here
1830                        //Print "Assigned stuff, and left."
1831                        Variable/G root:myGlobals:TransHeaderInfo:GuessOK= 1
1832                        DoWindow/K ConfirmGuess
1833                        break
1834        endswitch
1835
1836
1837        return 0
1838End
1839
1840// files are wrong, let the user try it again
1841// sets a flag, nothing else
1842Function DoTryAgainButtonProc(ba) : ButtonControl
1843        STRUCT WMButtonAction &ba
1844
1845        switch( ba.eventCode )
1846                case 2: // mouse up
1847                        // click code here
1848                        Variable/G root:myGlobals:TransHeaderInfo:GuessOK= 2
1849                        DoWindow/K ConfirmGuess
1850                        //Print "Try Again"
1851                        break
1852        endswitch
1853       
1854        return 0
1855End
1856
1857// not used
1858Function DoCancelGuessButtonProc(ba) : ButtonControl
1859        STRUCT WMButtonAction &ba
1860
1861        switch( ba.eventCode )
1862                case 2: // mouse up
1863                        // click code here
1864                        //just kill the panel, don't do anything
1865                        Variable/G root:myGlobals:TransHeaderInfo:GuessOK= 0
1866                        DoWindow/K ConfirmGuess
1867                        break
1868        endswitch
1869       
1870        return 0
1871End
1872
1873Function GuessAllTransFiles(numChars)
1874        Variable numChars
1875       
1876        if(WinType("ScatterFileTable") ==0 )
1877                return(1)
1878        endif
1879       
1880        Wave/T sw = root:myGlobals:TransHeaderInfo:S_Labels             //Sample file labels
1881        Variable ii,num=numpnts(sw)
1882       
1883        for(ii=0;ii<num;ii+=1)
1884                GuessTransFile(numChars,ii)
1885        endfor
1886       
1887        return(0)
1888End
1889
1890Function fGuessSelectedTransFiles(numChars)
1891        Variable numChars
1892       
1893        Variable ii
1894        Variable scatterTableExists
1895        scatterTableExists = WinType("ScatterFileTable")
1896        if (scatterTableExists != 0)
1897                GetSelection table,ScatterFileTable,1   
1898                for(ii=V_StartRow;ii<=V_EndRow;ii+=1)
1899                        GuessTransFile(numChars,ii)
1900                endfor
1901        else
1902                Print "Transmission->Scattering File table is not open"
1903        endif
1904        return(0)
1905End
1906
1907Function ClearSelectedAssignments()
1908       
1909        String winStr = WinList("*", ";", "WIN:" )              //returns the target window
1910        Variable scatterTableExists, transTableExists
1911        Print winStr
1912        scatterTableExists = cmpstr(winStr,"ScatterFileTable;")
1913        if (scatterTableExists == 0)
1914                GetSelection table,ScatterFileTable,1   
1915                fClearSelectedAssignments(V_startRow,V_endRow,1)
1916        endif
1917       
1918        transTableExists = cmpstr(winStr,"TransFileTable;")
1919        if (transTableExists == 0)
1920                GetSelection table,TransFileTable,1     
1921                fClearSelectedAssignments(V_startRow,V_endRow,2)
1922        endif
1923       
1924        return(0)
1925End
Note: See TracBrowser for help on using the repository browser.