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

Last change on this file since 949 was 949, checked in by srkline, 8 years ago

Marquee.ipf -- added procedures (to the end of the file) that are extensions of the "BoxSum?" functionality. Instead of a rectangular box, these will sum over a series of files, using a defined region that is a q-annulus (+/-), or sectors, or arcs. This is expected to be useful for event mode data.

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