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

Last change on this file since 740 was 670, checked in by srkline, 12 years ago

Corrections to NSORT and CombineFiles? to work with XML i/o.

CombineFiles? table hook now is properly active only in the upper table.

Some file filtering functions have been streamlined, but are still somewhat redundant, and could be consolidated. These are whenever listing of reduced files is requested (filter out what is known to be something else)

Commented out print statements from XML reader.

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