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

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

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

File size: 59.5 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma version=5.0
3#pragma IgorVersion=4.0
4
5//****************************
6// Vers. 1.2 092101
7//
8// NSORT panel for combining and inter-normalizing 2 or 3 datasets
9// that have previously been averaged
10//
11// - handles 3 or 6-column datasets
12// allows manual scaling
13// allows user to interactively delete data points from either end of any datset
14//
15//*********************
16
17//main entry point for displaying the nsort panel
18//initializes folder/globals as needed
19//
20Proc ShowNSORTPanel()
21        DoWindow/F NSORT_Panel
22        if(V_flag==0)
23                InitNSORTPanel()
24                NSORT_Panel()
25        Endif
26        SetDataFolder root:
27        PathInfo/S catPathName
28        String junk = S_path
29        if (V_flag == 0)
30                //path does not exist - no folder selected
31                String/G root:myGlobals:NSORT:gPathStr = "no folder selected"
32        else
33                String/G root:myGlobals:NSORT:gPathStr = junk
34        endif
35        LowQPopMenuProc("",1,"")
36        MedQPopMenuProc("",1,"")
37        HighQPopMenuProc("",1,"")
38End
39
40//initializes globals/folder for the NSORT panel as needed
41//all of the globals are stored in root:myGlobals:NSORT folder
42//the globals are the values displayed in the panel
43//
44Proc InitNSORTPanel()
45
46        //set up the global variables needed for the NSORT panel
47        NewDataFolder/O root:myGlobals:NSORT
48        Variable/G root:myGlobals:NSORT:gScale1_2 = 1
49        Variable/G root:myGlobals:NSORT:gScale2_3 = 1
50        //
51        //save the number of points to trim from beginning/end of the data files
52        //
53        Variable/G root:myGlobals:NSORT:gPtsBeg1 = NumVarOrDefault("root:myGlobals:NSORT:gPtsBeg1", 0 )
54        Variable/G root:myGlobals:NSORT:gPtsEnd1 = NumVarOrDefault("root:myGlobals:NSORT:gPtsEnd1", 0 )
55        Variable/G root:myGlobals:NSORT:gPtsBeg2 = NumVarOrDefault("root:myGlobals:NSORT:gPtsBeg2", 0 )
56        Variable/G root:myGlobals:NSORT:gPtsEnd2 = NumVarOrDefault("root:myGlobals:NSORT:gPtsEnd2", 0 )
57        Variable/G root:myGlobals:NSORT:gPtsBeg3 = NumVarOrDefault("root:myGlobals:NSORT:gPtsBeg3", 0 )
58        Variable/G root:myGlobals:NSORT:gPtsEnd3 = NumVarOrDefault("root:myGlobals:NSORT:gPtsEnd3", 0 )
59       
60        Variable/G root:myGlobals:NSORT:gColumns1 = 0
61        Variable/G root:myGlobals:NSORT:gColumns2 = 0
62        Variable/G root:myGlobals:NSORT:gColumns3 = 0
63        Variable/G root:myGlobals:NSORT:gNormToNum = 1
64        String/G root:myGlobals:NSORT:gPathStr = ""
65        String/G root:myGlobals:NSORT:gDataPopList = "none"
66        String/G root:myGlobals:NSORT:gDataPopList_3 = "none"
67       
68        SetDataFolder root:             //(redundant)
69End
70
71
72//loads datasets (3-column or 6-column) for use in NSORTing
73//identical in function to normal file loading routine LoadQISData()
74//except that this routine builds custom names to keep track of
75//high-med-low q datasets (as defined by setNum input)
76//
77Function LoadDataForNSORT(fileStr,setNum)
78        String fileStr          //full path:name to a valid file
79        Variable setNum //number of set (used for naming) = 0, 1, or 2 (ONLY)
80       
81        Variable err=0
82       
83        LoadWave/G/D/A/Q fileStr                // (/Q) option suppresses printing to history
84        String nm0,nm1,nm2
85        String firstFileName = S_fileName
86        Variable pt=0,begPts,endPts
87
88        NVAR gColumns1 = root:myGlobals:NSORT:gColumns1
89        NVAR gColumns2 = root:myGlobals:NSORT:gColumns2
90        NVAR gColumns3 = root:myGlobals:NSORT:gColumns3
91        NVAR begPts1 = root:myGlobals:NSORT:gPtsBeg1
92        NVAR endPts1 = root:myGlobals:NSORT:gPtsEnd1
93        NVAR begPts2 = root:myGlobals:NSORT:gPtsBeg2
94        NVAR endPts2 = root:myGlobals:NSORT:gPtsEnd2
95        NVAR begPts3 = root:myGlobals:NSORT:gPtsBeg3
96        NVAR endPts3 = root:myGlobals:NSORT:gPtsEnd3
97       
98        String typStr="",trimStr=""
99       
100        do
101                if(V_flag == 3)  //Three waves loaded   
102                        // put the names of the three loaded waves into local names
103                        nm0 = StringFromList(0, S_waveNames ,";" )
104                        nm1 = StringFromList(1, S_waveNames ,";" )
105                        nm2 = StringFromList(2, S_waveNames ,";" )
106       
107                        //rename to give desired names - can't use rename if waves exist - it will generate an error
108                        if(setNum == 1)
109                                gColumns1 = 3
110                                typStr = "LowQSet"
111                                trimStr = "TrimLowQSet"
112                                begPts = begPts1
113                                endPts = endPts1
114                        else
115                                if(setNum == 2)
116                                        gColumns2 = 3
117                                        typStr = "MedQSet"
118                                        trimStr = "TrimMedQSet"
119                                        begPts = begPts2
120                                        endPts = endPts2
121                                else
122                                        gColumns3 = 3
123                                        typStr="HighQSet"
124                                        trimStr = "TrimHighQSet"
125                                        begPts = begPts3
126                                        endPts = endPts3
127                                Endif
128                        Endif
129                        Duplicate/O $nm0, $(typStr+"_q")
130                        Duplicate/O $nm1, $(typStr+"_i")
131                        Duplicate/O $nm2, $(typStr+"_s")
132                        KillWaves/Z $nm0,$nm1,$nm2
133                        //create the "trimmed" dataset too, start by duplicating the original sets
134                        Duplicate/O $(typStr+"_q"),$(trimStr+"_q")
135                        Duplicate/O $(typStr+"_i"),$(trimStr+"_i")
136                        Duplicate/O $(typStr+"_s"),$(trimStr+"_s")
137                        WaveStats/Q $(typStr+"_q")                      //get info about the original q-values read in
138                        pt = V_npnts-endPts
139                        DeletePoints pt,endPts,$(trimStr+"_q"),$(trimStr+"_i"),$(trimStr+"_s")  //delete end points first
140                        DeletePoints 0,begPts,$(trimStr+"_q"),$(trimStr+"_i"),$(trimStr+"_s")   //then delete points from beginning
141               
142                        Return err
143                        break
144                endif //End of what happens if 3 columns are loaded
145       
146                if(V_flag == 6)  //Six waves loaded
147                        String nm3,nm4,nm5
148                        // put the names of the three loaded waves into local names
149                        nm0 = StringFromList(0, S_waveNames ,";" )
150                        nm1 = StringFromList(1, S_waveNames ,";" )
151                        nm2 = StringFromList(2, S_waveNames ,";" )
152                        nm3 = StringFromList(3, S_waveNames ,";" )
153                        nm4 = StringFromList(4, S_waveNames ,";" )
154                        nm5 = StringFromList(5, S_waveNames ,";" )
155               
156                        if(setNum == 1)
157                                gColumns1 = 6
158                                typStr = "LowQSet"
159                                trimStr = "TrimLowQSet"
160                                begPts = begPts1
161                                endPts = endPts1
162                        else
163                                if(setNum == 2)
164                                        gColumns2 = 6
165                                        typStr = "MedQSet"
166                                        trimStr = "TrimMedQSet"
167                                        begPts = begPts2
168                                        endPts = endPts2
169                                else
170                                        gColumns3 = 6
171                                        typStr="HighQSet"
172                                        trimStr = "TrimHighQSet"
173                                        begPts = begPts3
174                                        endPts = endPts3
175                                Endif
176                        Endif
177                        Duplicate/O $nm0, $(typStr+"_q")
178                        Duplicate/O $nm1, $(typStr+"_i")
179                        Duplicate/O $nm2, $(typStr+"_s")
180                        Duplicate/O $nm3, $(typStr+"_sq")
181                        Duplicate/O $nm4, $(typStr+"_qb")
182                        Duplicate/O $nm5, $(typStr+"_fs")
183                        KillWaves/Z $nm0,$nm1,$nm2,$nm3,$nm4,$nm5
184                        //create the "trimmed" dataset too
185                        Duplicate/O $(typStr+"_q"),$(trimStr+"_q")
186                        Duplicate/O $(typStr+"_i"),$(trimStr+"_i")
187                        Duplicate/O $(typStr+"_s"),$(trimStr+"_s")
188                        Duplicate/O $(typStr+"_sq"),$(trimStr+"_sq")
189                        Duplicate/O $(typStr+"_qb"),$(trimStr+"_qb")
190                        Duplicate/O $(typStr+"_fs"),$(trimStr+"_fs")
191                        WaveStats/Q $(typStr+"_q")                      //get info about the original q-values read in
192                        pt = V_npnts-endPts
193                        DeletePoints pt,endPts,$(trimStr+"_q"),$(trimStr+"_i"),$(trimStr+"_s"),$(trimStr+"_sq"),$(trimStr+"_qb"),$(trimStr+"_fs")       //delete end points first
194                        DeletePoints 0,begPts,$(trimStr+"_q"),$(trimStr+"_i"),$(trimStr+"_s"),$(trimStr+"_sq"),$(trimStr+"_qb"),$(trimStr+"_fs")        //then delete points from beginning
195                               
196                        Return err
197                        break
198                endif //End of Six waves loaded
199       
200                //Next stuff gets executed if not 3 or 6 (example, if 0 )
201                err=1
202                return err
203        while(0)
204End
205
206//writes out the NSORTed file, includeing header information about how the file
207//was genereated - from which files, and how it was normalized
208//protocol info makes no sense here - go back to the original, separate files
209//if you want to know that information
210//
211// this is the 3-column version  - there's a separate function for 6-column data
212//will ALWAYS prompt for a filename
213//
214Function Write3ColNSORTedFile(q3,i3,sig3,firstFileName,secondFileName,thirdFileName,normTo,norm12,norm23)
215        Wave q3,i3,sig3
216        String firstFileName,secondFileName,thirdFileName,normTo
217        Variable norm12,norm23
218
219        Variable err=0,refNum
220        String fullPath=""
221        //check each wave - else REALLY FATAL error when writing file
222        If(!(WaveExists(q3)))
223                err = 1
224                return err
225        Endif
226        If(!(WaveExists(i3)))
227                err = 1
228                return err
229        Endif
230        If(!(WaveExists(sig3)))
231                err = 1
232                return err
233        Endif
234       
235        Variable dialog = 1
236        if(dialog)
237                Open/D refnum as fullpath               //won't actually open the file
238                If(cmpstr(S_filename,"")==0)
239                        //user cancel, don't write out a file
240                        Close/A
241                        Abort "no data file was written"
242                Endif
243                fullpath = S_filename
244                //Print "dialog fullpath = ",fullpath
245        Endif
246       
247        String formatStr = "%15.4g %15.4g %15.4g\r\n"
248        //actually open the file
249        Open refNum as fullpath
250       
251        fprintf refnum,"FILE CREATED: %s \r\n",date()
252        fprintf refNum, "NSORT-ed   %s \t  +  %s\t  + %s\r\n",firstFileName, secondFileName,thirdFileName
253        fprintf refNum, "normalized to   %s\r\n",normTo
254        fprintf refNum, "multiplicative factor 1-2 = %12.8g\t multiplicative factor 2-3 = %12.8g\r\n",norm12,norm23
255        fprintf refnum,"The 3 columns are | Q (1/A) | I(Q) (1/cm) | std. dev. I(Q) (1/cm) |\r\n"
256        wfprintf refnum, formatStr, q3,i3,sig3
257       
258        Close refnum
259       
260        Return err
261End
262
263//writes out the NSORTed file, includeing header information about how the file
264//was generated - from which files, and how it was normalized
265//protocol info makes no sense here - go back to the original, separate files
266//if you want to know that information
267//
268// this is the 6-column version  - there's a separate function for 3-column data
269//will ALWAYS prompt for a filename
270//
271Function Write6ColNSORTedFile(q3,i3,sig3,sq3,qb3,fs3,firstFileName,secondFileName,thirdFileName,normTo,norm12,norm23)
272        Wave q3,i3,sig3,sq3,qb3,fs3
273        String firstFileName,secondFileName,thirdFileName,normTo
274        Variable norm12,norm23
275
276        Variable err=0,refNum
277        String fullPath=""
278        //check each wave - else REALLY FATAL error when writing file   
279        If(!(WaveExists(q3)))
280                err = 1
281                return err
282        Endif
283        If(!(WaveExists(i3)))
284                err = 1
285                return err
286        Endif
287        If(!(WaveExists(sig3)))
288                err = 1
289                return err
290        Endif
291        If(!(WaveExists(sq3)))
292                err = 1
293                return err
294        Endif
295        If(!(WaveExists(qb3)))
296                err = 1
297                return err
298        Endif
299        If(!(WaveExists(fs3)))
300                err = 1
301                return err
302        Endif
303       
304        Variable dialog = 1
305        // 05SEP05 SRK -- added to automatically combine files from a table - see the end of NSORT.ipf for details
306        // - use the flag set in DoCombineFiles() to decide if the table entries should be used
307        //   root:myGlobals:CombineTable:useTable= (1) (0)
308        //if(exists("root:myGlobals:CombineTable:SaveName"))
309        NVAR/Z useTable = root:myGlobals:CombineTable:useTable
310        if(NVAR_Exists(useTable) && useTable==1)
311                SVAR str=$"root:myGlobals:CombineTable:SaveNameStr"             //messy, but pass in as a global
312                fullPath = str
313                String str2 = "Is the file name "+str+" correct?"
314                DoAlert 1,str2
315                if(V_flag==1)
316                        dialog=0        //bypass the dialog if the name is good
317                endif
318        endif
319       
320        if(dialog)
321                Open/D refnum as fullpath               //won't actually open the file
322                If(cmpstr(S_filename,"")==0)
323                        //user cancel, don't write out a file
324                        Close/A
325                        Abort "no data file was written"
326                Endif
327                fullpath = S_filename
328                //Print "dialog fullpath = ",fullpath
329        Endif
330       
331        String formatStr = "%15.4g %15.4g %15.4g %15.4g %15.4g %15.4g\r\n"
332        //actually open the file
333        Open refNum as fullpath
334       
335        fprintf refnum,"FILE CREATED: %s \r\n",date()
336        fprintf refNum, "NSORT-ed   %s \t  +  %s\t  + %s\r\n",firstFileName, secondFileName,thirdFileName
337        fprintf refNum, "normalized to   %s\r\n",normTo
338        fprintf refNum, "multiplicative factor 1-2 = %12.8g\t multiplicative factor 2-3 = %12.8g\r\n",norm12,norm23
339        fprintf refnum,"The 6 columns are | Q (1/A) | I(Q) (1/cm) | std. dev. I(Q) (1/cm) | sigmaQ | meanQ | ShadowFactor|\r\n"
340        wfprintf refnum, formatStr, q3,i3,sig3,sq3,qb3,fs3
341       
342        Close refnum
343       
344        Return err
345End
346
347//gets the scaling constant to make (best) overlap of the specified datasets
348//the scaling value is an average value of the individual scaling values for
349//every data point (at the low q end) of set 2 that overlaps with set 1
350//(as if set 1 were held fixed)
351//num2 is the highest point number in set 2 that can overlap with set 1
352//(num2+1) individual scaling values are computed
353//wave2 must be multiplied by norm to rescale to wave1
354//the scale factor is the return value
355//
356Function GetScalingInOverlap(num2,wave1q,wave1i,wave2q,wave2i)
357        Variable num2           //largest point number of wave2 in overlap region
358        Wave wave1q,wave1i,wave2q,wave2i                //1 = first dataset, 2= second dataset
359
360        Variable ii,ival1,newi,ratio
361        ratio=0
362        ii=0
363        do
364                //get scaling factor at each point of wave 2 in the overlap region
365                newi = interp(wave2q[ii],wave1q,wave1i)         //get the intensity of wave1 at an overlap point
366                ratio += newi/wave2i[ii]                                        //get the scale factor
367                //Print "ratio = ",ratio
368                ii+=1
369        while(ii<=num2)
370        Variable val
371        val = ratio/(num2+1)            // +1 counts for point zero
372        //Print "val = ",val
373
374        Variable tol=1.05                       //5% is the preferred number (for Andrew and Lionel, at least)
375
376        ControlInfo/W=NSORT_Panel WarningCheck
377        if(( V_Value==1 ) && ( (val > tol) || (val < 1/tol) ) )
378                String str=""
379                sprintf str,"The scaling factor is more than a factor of %g from 1. Proceed with caution.\r",tol
380                DoAlert 0,str
381        endif
382       
383        Return val
384End
385
386Function ShowNSORTHelp(ctrlName) : ButtonControl
387        String ctrlName
388        DisplayHelpTopic/K=1 "SANS Data Reduction Tutorial[Sort and Combine Averaged Datasets]"
389End
390
391//button action procedure that simply closes the NSORT panel when done
392//and removes the NSORT-specific waves that were created
393// - the graph window must be killed first, so that the waves will not
394//be in use
395//
396Function NSORT_DoneButton(ctrlName) : ButtonControl
397        String ctrlName
398       
399        DoWindow/K NSORT_Panel
400       
401        DoWindow/K NSORT_Graph
402       
403        //clean up the temporary waves in the root: folder
404        SetDataFolder root:
405       
406        KillWaves/Z LowQSet_q,LowQSet_i,LowQSet_s,LowQSet_sq,LowQSet_qb,LowQSet_fs
407        KillWaves/Z TrimLowQSet_q,TrimLowQSet_i,TrimLowQSet_s,TrimLowQSet_sq,TrimLowQSet_qb,TrimLowQSet_fs
408        KillWaves/Z MedQSet_q,MedQSet_i,MedQSet_s,MedQSet_sq,MedQSet_qb,MedQSet_fs
409        KillWaves/Z TrimMedQSet_q,TrimMedQSet_i,TrimMedQSet_s,TrimMedQSet_sq,TrimMedQSet_qb,TrimMedQSet_fs
410        KillWaves/Z HighQSet_q,HighQSet_i,HighQSet_s,HighQSet_sq,HighQSet_qb,HighQSet_fs
411        KillWaves/Z TrimHighQSet_q,TrimHighQSet_i,TrimHighQSet_s,TrimHighQSet_sq,TrimHighQSet_qb,TrimHighQSet_fs
412
413End
414
415//button action procedure that plots dataset specified
416//on an NSORT graph window.
417//switch is on input controlName (low-med-high set)
418//parses partial filename from corresponding popup menu
419//builds a valid filename, loads the data (re-loads if already on graph)
420//and plots twice - once for the full datset (open symbols)
421//and once for the "trimmed" dataset (solid symbols, same color)
422//
423Function Plot_0_Button(ctrlName) : ButtonControl
424        String ctrlName
425
426        String tempName="",partialName=""
427        Variable setNum,err
428        //switch on ctrlName string - Plot_1, Plot_2, Plot_3
429        if(cmpstr(ctrlName,"Plot_1")==0)
430                //low-q
431                setNum = 1
432                ControlInfo $"popup_1"
433        else
434                if(cmpstr(ctrlName,"Plot_2")==0)
435                        //medium-q
436                        setNum = 2
437                        ControlInfo $"popup_2"
438                else
439                        //high-q
440                        setNum = 3
441                        ControlInfo $"popup_3"
442                Endif
443        Endif
444       
445        //find the file from the partial filename
446        If( (cmpstr(S_value,"")==0) || (cmpstr(S_value,"none")==0) )
447                //null selection, or "none" from any popup
448                Abort "no file selected in popup menu"
449        else
450                //selection not null
451                partialName = S_value
452                //Print partialName
453        Endif
454        //get a valid file based on this partialName and catPathName
455        tempName = FindValidFilename(partialName)
456       
457        //prepend path to tempName for read routine
458        PathInfo catPathName
459        tempName = S_path + tempName
460       
461        //load in the data (into the root directory)
462        err = LoadDataForNSORT(tempName,setNum)
463       
464        //bring the plot to the front, and put the new data on it
465        //and put cursors on the plotted dataset
466        //if the dataset is already on the graph, it will have been overwritten and updated by the load routine
467        //actually plot it twice, open(opaque) circles for the full dataset,
468        // then solid (filled) circles for the points actually kept
469        String list="",searchStr=""
470        Variable isOnPlot=0
471       
472        DoWindow/F NSORT_Graph
473        if(V_flag == 0)
474                //no window, create one
475                if(cmpstr(ctrlName,"Plot_1")==0)
476                        //low-q
477                        Display/K=1
478                        DoWindow/C NSORT_Graph
479                        DisplayLowSet()
480                else
481                        if(cmpstr(ctrlName,"Plot_2")==0)
482                                //medium-q
483                                Display/K=1
484                                DoWindow/C NSORT_Graph
485                                DisplayMedSet()
486                        else
487                                //high-q
488                                Display/K=1
489                                DoWindow/C NSORT_Graph
490                                DisplayHighSet()
491                        Endif
492                Endif
493                Legend
494        else
495                //plot already exists, waves have been updated
496                //make sure that the desired waves are actually on the graph, and add them if they aren't
497                list = TraceNameList("NSORT_Graph",";",1)
498       
499                if(cmpstr(ctrlName,"Plot_1")==0)
500                        //low-q
501                        isOnPlot = strsearch(list, "LowQSet_i", 0)              // isOnPlot == -1 if it is NOT found in the list
502                        if(isOnPlot == -1)
503                                DisplayLowSet()
504                        Endif
505                else
506                        if(cmpstr(ctrlName,"Plot_2")==0)
507                                //medium-q
508                                isOnPlot = strsearch(list, "MedQSet_i", 0)              // isOnPlot == -1 if it is NOT found in the list
509                                if(isOnPlot == -1)
510                                        DisplayMedSet()
511                                Endif
512                        else
513                                //high-q
514                                isOnPlot = strsearch(list, "HighQSet_i", 0)             // isOnPlot == -1 if it is NOT found in the list
515                                if(isOnPlot == -1)
516                                        DisplayHighSet()
517                                Endif
518                        Endif
519                Endif
520        Endif
521        //the stripPoints variable boxes should also update the graph, if necessary
522       
523End
524
525//adds both high-q sets (full and trimmed) to the graph, which is
526//assumed to exist along with the high-q waves
527//
528Function DisplayHighSet()
529        //function assumes that the window "NSORT_Graph" already exists
530        DoWindow/F NSORT_Graph
531        AppendToGraph $"HighQSet_i" vs $"HighQSet_q"
532        ModifyGraph log=1,mode=3,marker($"HighQSet_i")=8,msize=2,rgb($"HighQSet_i")=(0,0,65535),opaque($"HighQSet_i")=1
533        ErrorBars $"HighQSet_i" Y,wave=($"HighQSet_s",$"HighQSet_s")
534        AppendToGraph $"TrimHighQSet_i" vs $"TrimHighQSet_q"
535        ModifyGraph mode($"TrimHighQSet_i")=3,marker($"TrimHighQSet_i")=19,msize=2,rgb($"TrimHighQSet_i")=(0,0,65535)
536End
537
538//adds both med-q sets (full and trimmed) to the graph, which is
539//assumed to exist along with the med-q waves
540//
541Function DisplayMedSet()
542        //function assumes that the window "NSORT_Graph" already exists
543        DoWindow/F NSORT_Graph
544        AppendToGraph $"MedQSet_i" vs $"MedQSet_q"
545        ModifyGraph log=1,mode=3,marker($"MedQSet_i")=8,msize=2,rgb($"MedQSet_i")=(65535,0,0),opaque($"MedQSet_i")=1
546        ErrorBars $"MedQSet_i" Y,wave=($"MedQSet_s",$"MedQSet_s")
547        AppendToGraph $"TrimMedQSet_i" vs $"TrimMedQSet_q"
548        ModifyGraph mode($"TrimMedQSet_i")=3,marker($"TrimMedQSet_i")=19,msize=2,rgb($"TrimMedQSet_i")=(65535,0,0)
549End
550
551//adds both low-q sets (full and trimmed) to the graph, which is
552//assumed to exist along with the low-q waves
553//
554Function DisplayLowSet()
555        //function assumes that the window "NSORT_Graph" already exists
556        DoWindow/F NSORT_Graph
557        AppendToGraph $"LowQSet_i" vs $"LowQSet_q"
558        ModifyGraph log=1,mode=3,marker($"LowQSet_i")=8,msize=2,rgb($"LowQSet_i")=(2,39321,1),opaque($"LowQSet_i")=1
559        ErrorBars $"LowQSet_i" Y,wave=($"LowQSet_s",$"LowQSet_s")
560        AppendToGraph $"TrimLowQSet_i" vs $"TrimLowQSet_q"
561        ModifyGraph mode($"TrimLowQSet_i")=3,marker($"TrimLowQSet_i")=19,msize=2,rgb($"TrimLowQSet_i")=(2,39321,1)
562End
563
564//button action procedure to set both the main global of the catPath string
565//and also the duplicate global string used in the NSORT folder
566//after path selected, the popup menus are updated
567//
568Function NSORTPickPathButton(ctrlName) : ButtonControl
569        String ctrlName
570
571        Variable err = PickPath()               //sets global path value
572        SVAR pathStr = root:myGlobals:gCatPathStr
573       
574        //set the global string for NSORT to the selected pathname
575        String/G root:myGlobals:NSORT:gPathStr = pathStr
576       
577        //call each of the popup menu proc's to re-set the menu choices
578        //setting the checkboxes to force update
579//      CheckBox check_0,win=NSORT_Panel,value=1
580//      CheckBox check_1,win=NSORT_Panel,value=1
581//      CheckBox check_2,win=NSORT_Panel,value=1
582        LowQPopMenuProc("popup_1",1,"")
583        MedQPopMenuProc("popup_2",1,"")
584        HighQPopMenuProc("popup_3",1,"")
585       
586End
587
588
589//action procedure associated with the setvar box
590//when a value is entered, the global value is set, and the corresponding dataset
591//is updated on the plot, showing the new result of removing this number of points
592//
593//      SetVar boxes are named beg_N and end_N (so 4th element is the number)
594//
595// 1 == LowQ
596// 2 == MedQ
597// 3 == HighQ
598//
599//"Plot_1" is the low-q button name
600//"Plot_2" is the med-q button name
601//"Plot_3" is the high-q button name
602//
603//calling plot_0_Button() responds as if that named button were pressed
604// and gets the proper number to trim directly from the SetVar
605//
606Function SetBegOrEnd(ctrlName,varNum,varStr,varName) : SetVariableControl
607        String ctrlName
608        Variable varNum
609        String varStr
610        String varName
611       
612//  global is automatically updated as the value is entered
613        String numStr= num2Str( str2num(ctrlName[4]) )
614        Plot_0_Button("Plot_"+numStr)
615        DoWindow/F NSORT_Panel
616End
617
618//this will---
619//re-load the data sets (since they may not have been loaded if they were not plotted)
620// apply the scaling to the datasets (so that they will show up in the graph)
621//and actually write the file
622//
623// then "pop" the  lists to get the new file lists with the new name in the list
624//
625Function WriteNSORTFileButton(ctrlName) : ButtonControl
626        String ctrlName
627               
628        // Put here the dialog that says if ANY of the datasets had 3-column data, the results will all have only three columns
629        // Set the number of output columns
630        Variable isAThree = 0, isASix = 0
631        String fileStr=""
632
633        NVAR Columns1 = root:myGlobals:NSORT:gColumns1
634        NVAR Columns2 = root:myGlobals:NSORT:gColumns2
635        NVAR Columns3 = root:myGlobals:NSORT:gColumns3
636        if( (Columns1 == 3) || (Columns2 == 3) || (Columns3 == 3) )
637                isAThree = 1
638        endif
639        if( (Columns1 == 6) || (Columns2 == 6) || (Columns3 == 6) )
640                isASix = 1
641        endif
642        if( (isAThree == 1) && (isASix == 1))
643                DoAlert 0, "These files contained a mixture of 3-column and 6-column data.  Only 3 columns were output."
644        endif
645       
646        //is there just one data set? if so, then dispatch to a simpler routine, since no normalization is needed
647        ControlInfo popup_2             //if MedQSet is "none", then so is HighQSet
648        fileStr = S_Value
649        if(cmpstr(fileStr,"none") == 0)
650                //send just the trimmed (LowQ) set to be written out
651                WAVE lowq = $"TrimLowQSet_q"
652                WAVE lowi = $"TrimLowQSet_i"
653                WAVE lows = $"TrimLowQSet_s"
654                WAVE/Z lowsq = $"TrimLowQSet_sq"
655                WAVE/Z lowqb = $"TrimLowQSet_qb"
656                WAVE/Z lowfs = $"TrimLowQSet_fs"
657                NVAR scaleFactor= root:myGlobals:NSORT:gScale1_2
658                //
659                lowi *= scaleFactor
660                lows *= scaleFactor
661               
662                ControlInfo popup_1
663                if(isAThree)
664                        Write3ColNSORTedFile(lowq,lowi,lows,S_Value,"none","none",S_Value,scaleFactor,1)
665                else
666                        Write6ColNSORTedFile(lowq,lowi,lows,lowsq,lowqb,lowfs,S_Value,"none","none",S_Value,scaleFactor,1)
667                endif
668                //  just get the new list and return - don't actually "pop" the menu, or the selected item will change
669                SVAR popList = root:myGlobals:NSORT:gDataPopList
670                SVAR popList_3 = root:myGlobals:NSORT:gDataPopList_3
671                popList  = ReducedDataFileList("")
672                popList_3 = "none;" +  ReducedDataFileList("")
673                return(0)
674        endif
675               
676               
677        //two or more datasets, combine them
678        //have they been manually or auto-normalized?
679        ControlInfo AutoCheck
680        Variable checked = V_Value
681       
682        //do the normalization and update the global scale factors displayed in the Panel
683        Variable err = DoAutoScaleFromPanel(checked)                    // DoAutoScaleFromPanel writes out the datafile
684
685        //  just get the new list - don't actually "pop" the menu, or the selected item will change
686        SVAR popList = root:myGlobals:NSORT:gDataPopList
687        SVAR popList_3 = root:myGlobals:NSORT:gDataPopList_3
688        popList  = ReducedDataFileList("")
689        popList_3 = "none;" +  ReducedDataFileList("")
690       
691        return(0)
692End
693
694//window recreation macro for NSORT Panel
695//
696Window NSORT_Panel()
697        PauseUpdate; Silent 1           // building window...
698        NewPanel /W=(569,69,944,485)/K=2
699        ModifyPanel cbRGB=(49151,53155,65535)
700        ModifyPanel fixedSize=1
701        SetDrawLayer UserBack
702        SetDrawEnv fstyle= 5
703        DrawText 35,20,"NSORT - Rescale and combine 1-D files"
704        DrawLine 0,55,346,55
705        DrawLine 0,128,346,128
706        DrawLine 0,214,346,214
707        DrawLine 0,295,346,295
708        SetDrawEnv fstyle= 5
709        DrawText 5,74,"Low Q:"
710        SetDrawEnv fstyle= 5
711        DrawText 5,148,"Medium Q:"
712        SetDrawEnv fstyle= 5
713        DrawText 8,234,"High Q: (or none)"
714        SetDrawEnv fstyle= 4
715        DrawText 178,75,"Delete Points?"
716        SetDrawEnv fstyle= 4
717        DrawText 178,146,"Delete Points?"
718        SetDrawEnv fstyle= 4
719        DrawText 184,236,"Delete Points?"
720        DrawLine 0,363,346,363
721        DrawText 31,357,"To Manually scale data, enter scale factors above"
722        Button NSORT_Done,pos={274,387},size={50,20},proc=NSORT_DoneButton,title="Done"
723        Button NSORT_Done,help={"closes the panel"}
724        Button Plot_1,pos={279,63},size={50,20},proc=Plot_0_Button,title="Plot"
725        Button Plot_1,help={"Plots the dataset from the popup, showing the full set as open circles and the trimmed set as solid circles"}
726        Button Plot_2,pos={283,144},size={50,20},proc=Plot_0_Button,title="Plot"
727        Button Plot_2,help={"Plots the dataset from the popup, showing the full set as open circles and the trimmed set as solid circles"}
728        Button Plot_3,pos={284,223},size={50,20},proc=Plot_0_Button,title="Plot"
729        Button Plot_3,help={"Plots the dataset from the popup, showing the full set as open circles and the trimmed set as solid circles"}
730        Button PathButton,pos={6,26},size={80,20},proc=NSORTPickPathButton,title="Pick Path"
731        Button PathButton,help={"Select the local path to the folder containing your SANS data"}
732        Button helpButton,pos={340,26},size={25,20},proc=ShowNSORTHelp,title="?"
733        Button helpButton,help={"Show the help file for sorting and internormalizing 1-D data sets"}
734        SetVariable setPath,pos={95,29},size={240,14},title="Path:",fSize=10
735        SetVariable setPath,limits={0,0,0},value= root:myGlobals:NSORT:gPathStr
736        SetVariable setPath,help={"The current path to the local folder with SANS data"}
737        SetVariable end_1,pos={182,101},size={80,14},proc=SetBegOrEnd,title="End Pts"
738        SetVariable end_1,fSize=10,help={"How many points to remove from the high-q end of this dataset"}
739        SetVariable end_1,limits={-Inf,Inf,0},value= root:myGlobals:NSORT:gPtsEnd1
740        SetVariable end_2,pos={182,176},size={80,14},proc=SetBegOrEnd,title="End Pts"
741        SetVariable end_2,fSize=10,help={"How many points to remove from the high-q end of this dataset"}
742        SetVariable end_2,limits={-Inf,Inf,0},value= root:myGlobals:NSORT:gPtsEnd2
743        SetVariable end_3,pos={182,269},size={80,14},proc=SetBegOrEnd,title="End Pts"
744        SetVariable end_3,fSize=10,help={"How many points to remove from the high-q end of this dataset"}
745        SetVariable end_3,limits={-Inf,Inf,0},value= root:myGlobals:NSORT:gPtsEnd3
746        SetVariable beg_1,pos={182,79},size={80,14},proc=SetBegOrEnd,title="Beg Pts"
747        SetVariable beg_1,fSize=10,help={"How many points to remove from the low-q end of this dataset"}
748        SetVariable beg_1,limits={-Inf,Inf,0},value= root:myGlobals:NSORT:gPtsBeg1
749        SetVariable beg_2,pos={182,155},size={80,14},proc=SetBegOrEnd,title="Beg Pts"
750        SetVariable beg_2,fSize=10,help={"How many points to remove from the low-q end of this dataset"}
751        SetVariable beg_2,limits={-Inf,Inf,0},value= root:myGlobals:NSORT:gPtsBeg2
752        SetVariable beg_3,pos={182,246},size={80,14},proc=SetBegOrEnd,title="Beg Pts"
753        SetVariable beg_3,fSize=10,help={"How many points to remove from the low-q end of this dataset"}
754        SetVariable beg_3,limits={-Inf,Inf,0},value= root:myGlobals:NSORT:gPtsBeg3
755        Button DoCombine,pos={13,387},size={160,20},proc=WriteNSORTFileButton,title="Write Combined File"
756        Button DoCombine,help={"Combine and normalize the selected files as specifed"}
757        SetVariable scale_12,pos={159,305},size={160,14},proc=SetScale_12,title="Mult factor 1-2"
758        SetVariable scale_12,fSize=10,help={"Factor that will multiply medium-q set to scale to low-q set"}
759        SetVariable scale_12,limits={-Inf,Inf,0},value= root:myGlobals:NSORT:gScale1_2
760        SetVariable scale_23,pos={159,325},size={160,14},proc=SetScale_23,title="Mult factor 2-3"
761        SetVariable scale_23,fSize=10,help={"Factor that will multiply high-q set to scale to medium-q set"}
762        SetVariable scale_23,limits={-Inf,Inf,0},value= root:myGlobals:NSORT:gScale2_3
763        CheckBox check1,pos={5,105},size={160,20},proc=CheckProc,title="Normalize to this file",value=1
764        CheckBox check1,help={"If checked, the combined dataset will be normalized to this dataset"}
765        CheckBox check2,pos={5,185},size={160,20},proc=CheckProc,title="Normalize to this file",value=0
766        CheckBox check2,help={"If checked, the combined dataset will be normalized to this dataset"}
767        CheckBox check3,pos={4,270},size={160,20},proc=CheckProc,title="Normalize to this file",value=0
768        CheckBox check3,help={"If checked, the combined dataset will be normalized to this dataset"}
769        PopupMenu popup_1,pos={6,77},size={99,19},proc=LowQPopMenuProc
770        PopupMenu popup_1,mode=1,value= #"root:myGlobals:NSORT:gDataPopList"
771        PopupMenu popup_1,help={"Choose the dataset with the lowest overall q-value (longest detector distance)"}
772        PopupMenu popup_2,pos={6,153},size={99,19},proc=MedQPopMenuProc
773        PopupMenu popup_2,mode=1,value= #"root:myGlobals:NSORT:gDataPopList_3"
774        PopupMenu popup_2,help={"Choose the dataset with the intermediate q-values (\"medium\" detector distance)"}
775        PopupMenu popup_3,pos={6,239},size={99,19},proc=HighQPopMenuProc
776        PopupMenu popup_3,mode=1,value= #"root:myGlobals:NSORT:gDataPopList_3"
777        PopupMenu popup_3,help={"Choose the dataset with the highest overall q-value (shortest detector distance), or NONE if no third set desired"}
778        CheckBox AutoCheck,pos={14,310},size={100,20},title="Auto Scale",value=0
779        CheckBox AutoCheck,help={"If checked, the scale factor will be automatically determined, if not checked, the current values in the fields will be used"}
780        CheckBox PreviewCheck,pos={15,369},size={74,14},title="Preview Only",value= 0
781        CheckBox WarningCheck,pos={111,369},size={93,14},title="Overlap warning?",value= 1
782EndMacro
783
784//sets the scale factor (multiplicative) between sets 1 and 2
785//re-sets the global variable
786//
787Function SetScale_12(ctrlName,varNum,varStr,varName) : SetVariableControl
788        String ctrlName
789        Variable varNum
790        String varStr
791        String varName
792
793        Variable/G root:myGlobals:NSORT:gScale1_2 = varNum
794       
795End
796
797//sets the scale factor (multiplicative) between sets 2 and 3
798//re-sets the global variable
799//
800Function SetScale_23(ctrlName,varNum,varStr,varName) : SetVariableControl
801        String ctrlName
802        Variable varNum
803        String varStr
804        String varName
805
806        Variable/G root:myGlobals:NSORT:gScale2_3 = varNum
807End
808
809//control procedures for the checkboxes to specify which file is to be
810//held fixed (so all other files are normalized to the checked file
811//the three checkboxes behave as "radio buttons" - only one can be checked
812//
813Function CheckProc(ctrlName,checked) : CheckBoxControl
814        String ctrlName
815        Variable checked
816       
817        //controls the three checkboxes to act as "radio buttons" to have only one file to
818        //normalize to.
819        //all three boxes should call this routine
820       
821        //do the "radio button control"
822        do
823                if(cmpstr(ctrlName,"check2") == 0)
824                        CheckBox check1 value=0
825                        CheckBox check2 value=1
826                        CheckBox check3 value=0
827                        Variable/G root:myGlobals:NSORT:gNormToNum = 2
828                        break
829                Endif
830                if(cmpstr(ctrlName,"check3") == 0)
831                        CheckBox check1 value=0
832                        CheckBox check2 value=0
833                        CheckBox check3 value=1
834                        Variable/G root:myGlobals:NSORT:gNormToNum = 3
835                        break
836                Endif
837                //default case is normalize to file1
838                CheckBox check1 value=1
839                CheckBox check2 value=0
840                CheckBox check3 value=0
841                Variable/G root:myGlobals:NSORT:gNormToNum = 1
842        While(0)
843        ControlUpdate/A/W=NSORT_Panel
844                DoUpdate
845               
846End
847
848//when menu is popped, it gets a valid list to display and updates the control
849//
850// 2002- always refreshes, as new (fast) filter is used
851Function LowQPopMenuProc(ctrlName,popNum,popStr) : PopupMenuControl
852        String ctrlName
853        Variable popNum
854        String popStr
855
856        String/G root:myGlobals:NSORT:gDataPopList = ReducedDataFileList("")
857        ControlUpdate popup_1
858
859        return(0)
860End
861
862//when menu is popped, it gets a valid list to display and updates the control
863//
864Function MedQPopMenuProc(ctrlName,popNum,popStr) : PopupMenuControl
865        String ctrlName
866        Variable popNum
867        String popStr
868               
869        String/G root:myGlobals:NSORT:gDataPopList_3 = "none;" +  ReducedDataFileList("")
870        ControlUpdate popup_2
871        if(cmpstr(popStr,"none")==0)
872                PopupMenu popup_3,mode=1        //force "none" (item #1) to be the selection
873                CheckBox AutoCheck,value=0      //un-check the auto-scale checkbox
874                DoAlert 0,"You have only one data set. Auto Scaling has been unchecked and Mult Factor 1-2 will be applied to your data. Remember to re-check this as needed"// remind the user of this fact
875                RemoveFromGraph/Z MedQSet_i,TrimMedQSet_i,HighQSet_i,TrimHighQSet_i             //remove the data from the graph
876        Endif   
877        return(0)
878End
879
880//when menu is popped, it gets a valid list to display and updates the control
881// - will be different, since set 3 can also be "none" if only 2 sets
882//are to be NSORTed
883//
884Function HighQPopMenuProc(ctrlName,popNum,popStr) : PopupMenuControl
885        String ctrlName
886        Variable popNum
887        String popStr
888
889        //add the option "none" to the file list (which should already end with a semicolon)
890        String/G root:myGlobals:NSORT:gDataPopList_3 = "none;" +  ReducedDataFileList("")
891
892        ControlUpdate popup_3
893        if(cmpstr(popStr,"none")==0)
894                RemoveFromGraph/Z HighQSet_i,TrimHighQSet_i             //remove the data from the graph
895        Endif   
896        ControlInfo popup_2
897        if(cmpstr(S_Value,"none")==0)
898                PopupMenu popup_3,mode=1        //force "none" (item #1) to be the selection if medium is none
899        endif
900        return(0)       
901End
902
903
904//be sure to use the "Trim.." datasets that have had the bad points removed
905//and then do the scaling based on the choices in the panel
906//input (auto) is a switch
907//
908Function DoAutoScaleFromPanel(auto)
909        Variable auto           //if auto == 1, do the scaling, if 0, use manual scale values
910
911        NVAR normTo = root:myGlobals:NSORT:gNormToNum
912        Variable err=0,setNum,norm12,norm23
913        String fileStr="",tempName="",name1="",name2="",name3="",normToStr=""
914       
915//Set the number of output columns
916        Variable numOutputColumns = 0
917
918        NVAR Columns1 = root:myGlobals:NSORT:gColumns1
919        NVAR Columns2 = root:myGlobals:NSORT:gColumns2
920        NVAR Columns3 = root:myGlobals:NSORT:gColumns3
921        if( (Columns1 == 3) || (Columns2 == 3) || (Columns3 == 3) )
922                numOutputColumns = 3
923        else
924                if( (Columns1 == 6) && (Columns2 == 6) && ((Columns3 == 0) || (Columns3 == 6)) )
925                        numOutputColumns = 6
926                endif
927        endif
928
929        //rescale 1-2
930       
931        //load file1
932        ControlInfo $"popup_1"
933        fileStr = S_Value
934        name1 = fileStr
935        setNum = 1
936        //get a valid file based on this partialName and catPathName
937        tempName = FindValidFilename(fileStr)
938       
939        //prepend path to tempName for read routine
940        PathInfo catPathName
941        tempName = S_path + tempName
942        err = LoadDataForNSORT(tempName,setNum)
943        //////end load file1
944       
945        //load file2
946        ControlInfo $"popup_2"
947        fileStr = S_Value
948        name2 = fileStr
949        setNum = 2
950        //get a valid file based on this partialName and catPathName
951        tempName = FindValidFilename(fileStr)
952       
953        //prepend path to tempName for read routine
954        PathInfo catPathName
955        tempName = S_path + tempName
956        err = LoadDataForNSORT(tempName,setNum)
957        //////end load file2
958       
959        //load file3 , if necessary
960        ControlInfo $"popup_3"
961        fileStr = S_Value
962        name3 = fileStr
963        setNum = 3
964        if(cmpstr(fileStr,"none") == 0)
965                //do nothing
966        else
967                //get a valid file based on this partialName and catPathName
968                tempName = FindValidFilename(fileStr)
969       
970                //prepend path to tempName for read routine
971                PathInfo catPathName
972                tempName = S_path + tempName
973                err = LoadDataForNSORT(tempName,setNum)
974        Endif
975        //////end load file3
976       
977        //assign filename of file to normalize to
978        if(normTo == 1)
979                normToStr = name1
980        else
981                if(normTo == 2)
982                        normToStr = name2
983                else
984                        normToStr = name3
985                Endif
986        Endif
987
988        Variable n1,n2,n12,num2
989        Variable n3,n123
990       
991   if(numOutputColumns == 3) //Start the 3-column specific stuff here.
992                //order points in sets 1-2, indexing overlap region
993                //put result in temporary waves
994                WaveStats/Q $"TrimLowQSet_q"
995                n1 = V_npnts
996                WaveStats/Q $"TrimMedQSet_q"
997                n2 = V_npnts
998                n12 = n1+ n2
999               
1000                Make/O/N=(n12) q12,i12,sig12
1001                WAVE lowq = $"TrimLowQSet_q"
1002                WAVE medq = $"TrimMedQSet_q"
1003                WAVE lowi = $"TrimLowQSet_i"
1004                WAVE medi =  $"TrimMedQSet_i"
1005                WAVE lows = $"TrimLowQSet_s"
1006                WAVE meds = $"TrimMedQSet_s"
1007                q12[0,n1-1] = lowq[p]
1008                q12[n1,n1+n2-1]= medq[p-n1]
1009                i12[0,n1-1] = lowi[p]
1010                i12[n1,n1+n2-1]= medi[p-n1]
1011                sig12[0,n1-1] = lows[p]
1012                sig12[n1,n1+n2-1]= meds[p-n1]
1013               
1014                Sort q12, q12,i12,sig12
1015                /////////////////
1016               
1017                //find the maximum point number of set 2  in the overlap region
1018                FindLevel/P/Q medq,(lowq[n1-1])
1019                num2 = trunc(V_levelX)
1020                //Print "num2 = ",num2
1021               
1022                if (auto)
1023                        //there must be overlap points to use auto-scaling
1024                        if(numtype(num2) != 0)
1025                                Abort "There are no data points in the overlap region. Either reduce the number of deleted points or use manual scaling."
1026                        endif
1027                        //do auto-scaling of data
1028                        norm12 = GetScalingInOverlap(num2,lowq,lowi,medq,medi)
1029                        //Set the global variable for the 1-2 scale factor
1030                        Variable/G root:myGlobals:NSORT:gScale1_2 = norm12
1031                else
1032                        //use the value from the panel ( which is the global)
1033                        NVAR temp12 = root:myGlobals:NSORT:gScale1_2
1034                        norm12 = temp12
1035                Endif
1036               
1037                If(normTo== 2)
1038                        //normalize to second file, so multiply 1st by 1/norm
1039                        norm12 = 1/norm12
1040                        lowi *= norm12
1041                        lows *= norm12
1042                else
1043                        //normalize to first file, OR THIRD FILE so multiply 2nd by norm
1044                        medi *= norm12
1045                        meds *= norm12
1046                Endif
1047               
1048                //Print "NSORT-ed ",name1," + ", name2
1049                //Print "normalized to ",normTo
1050                //Print "multiplicative factor = ",norm12
1051               
1052               
1053                //Make the combined, scaled dataset by overwriting the old sets
1054                Make/O/N=(n12) q12,i12,sig12
1055                q12[0,n1-1] = lowq[p]
1056                q12[n1,n1+n2-1]= medq[p-n1]
1057                i12[0,n1-1] = lowi[p]
1058                i12[n1,n1+n2-1]= medi[p-n1]
1059                sig12[0,n1-1] = lows[p]
1060                sig12[n1,n1+n2-1]= meds[p-n1]
1061               
1062                Sort q12, q12,i12,sig12
1063                //at this point 1-2 are combined
1064               
1065                ControlUpdate/A/W=NSORT_Panel
1066                DoUpdate
1067               
1068                //do we need to continue, or write out the set here and stop?
1069                if(cmpstr(name3,"none") == 0)
1070                        //stop here
1071                        norm23 = 1              //norm23 was not used
1072                        Variable/G root:myGlobals:NSORT:gScale2_3 = 1
1073                        //If any of them have three columns write three column data
1074                       
1075                        ControlInfo/W=NSORT_Panel PreviewCheck
1076                        if( V_Value==0 )                //if zero skip the preview and write out the file
1077                                err=Write3ColNSORTedFile(q12,i12,sig12,name1,name2,name3,normToStr,norm12,norm23)
1078                        endif
1079                        //cleanup waves before exiting
1080                        KillWaves/Z q12,i12,sig12
1081                        return err
1082                Endif
1083               
1084                //need to add the third file... which was already loaded at the top of the function
1085                /////
1086                //order points in sets 12-3, indexing overlap region
1087                //put result in temporary waves
1088                WaveStats/Q q12
1089                n12 = V_npnts
1090                WaveStats/Q $"TrimHighQSet_q"
1091                n3 = V_npnts
1092                n123 = n12+ n3
1093               
1094                Make/O/N=(n123) q123,i123,sig123
1095                WAVE highq = $"TrimHighQSet_q"
1096                WAVE highi = $"TrimHighQSet_i"
1097                WAVE highs = $"TrimHighQSet_s"
1098       
1099                q123[0,n12-1] = q12[p]
1100                q123[n1,n12+n3-1]= highq[p-n12]
1101                i123[0,n12-1] = i12[p]
1102                i123[n1,n12+n3-1]= highi[p-n12]
1103                sig123[0,n12-1] = sig12[p]
1104                sig123[n1,n12+n3-1]= highs[p-n12]
1105               
1106                Sort q123, q123,i123,sig123
1107                /////////////////
1108               
1109                //find the maximum point number of set 2  in the overlap region
1110                FindLevel/P/Q highq,(q12[n12-1])
1111                num2 = trunc(V_levelX)
1112                //Print "num2 = ",num2
1113               
1114                if (auto)
1115                        //there must be overlap points to use auto-scaling
1116                        if(numtype(num2) != 0)
1117                                Abort "There are no data points in the overlap region. Either reduce the number of deleted points or use manual scaling."
1118                        endif
1119                        //do auto-scaling of data
1120                        norm23 = GetScalingInOverlap(num2,q12,i12,highq,highi)
1121                        //Set the global variable for the 12 - 3 scale factor
1122                        Variable/G root:myGlobals:NSORT:gScale2_3 = norm23
1123                else
1124                        //use the value from the panel ( which is the global)
1125                        NVAR temp23 = root:myGlobals:NSORT:gScale2_3
1126                        norm23 = temp23
1127                Endif
1128               
1129                If( (normTo== 1) || (normTo ==2) )
1130                        //normalize to first or second file, so multiply third by norm23
1131                        highi *= norm23
1132                        highs *= norm23
1133                else
1134                        //normalize to THIRD file, 1-2 by 1/norm23
1135                        norm23 = 1/norm23
1136                        i12 *= norm23
1137                        sig12 *= norm23
1138                Endif
1139               
1140                ControlUpdate/A/W=NSORT_Panel
1141                DoUpdate
1142
1143                //Print "NSORT-ed ",name1," + ", name2, " + ", name3
1144                //Print "normalized to ",normTo
1145                //Print "multiplicative factor 1-2 = ",norm12," multiplicative factor 12 - 3 = ",norm23
1146               
1147               
1148                Make/O/N=(n123) q123,i123,sig123
1149                q123[0,n12-1] = q12[p]
1150                q123[n12,n12+n3-1]= highq[p-n12]
1151                i123[0,n12-1] = i12[p]
1152                i123[n12,n12+n3-1]= highi[p-n12]
1153                sig123[0,n12-1] = sig12[p]
1154                sig123[n12,n12+n3-1]= highs[p-n12]
1155               
1156                Sort q123, q123,i123,sig123
1157                //at this point 12 - 3 are combined
1158                //write out the set here and stop
1159       
1160                ControlInfo/W=NSORT_Panel PreviewCheck
1161                if( V_Value==0 )                //if zero skip the preview and write out the file
1162                        err=Write3ColNSORTedFile(q123,i123,sig123,name1,name2,name3,normToStr,norm12,norm23)
1163                endif
1164                //cleanup waves before exiting
1165                KillWaves/Z q12,i12,sig12,q123,i123,sig123
1166                //combined dataset will already be displayed if the NSORT_Graph is open
1167       
1168                ////////////////
1169                return err
1170   endif // End the 3-column specific stuff here
1171
1172   if(numOutputColumns == 6) // Start the 6-column specific stuff here
1173                //order points in sets 1-2, indexing overlap region
1174                //put result in temporary waves
1175                WaveStats/Q $"TrimLowQSet_q"
1176                n1 = V_npnts
1177                WaveStats/Q $"TrimMedQSet_q"
1178                n2 = V_npnts
1179                n12 = n1+ n2
1180               
1181                Make/O/N=(n12) q12,i12,sig12,sq12,qb12,fs12
1182                WAVE lowq = $"TrimLowQSet_q"
1183                WAVE medq = $"TrimMedQSet_q"
1184                WAVE lowi = $"TrimLowQSet_i"
1185                WAVE medi =  $"TrimMedQSet_i"
1186                WAVE lows = $"TrimLowQSet_s"
1187                WAVE meds = $"TrimMedQSet_s"
1188                WAVE lowsq = $"TrimLowQSet_sq"
1189                WAVE medsq = $"TrimMedQSet_sq"
1190                WAVE lowqb = $"TrimLowQSet_qb"
1191                WAVE medqb =  $"TrimMedQSet_qb"
1192                WAVE lowfs = $"TrimLowQSet_fs"
1193                WAVE medfs = $"TrimMedQSet_fs"
1194       
1195                q12[0,n1-1] = lowq[p]
1196                q12[n1,n1+n2-1]= medq[p-n1]
1197                i12[0,n1-1] = lowi[p]
1198                i12[n1,n1+n2-1]= medi[p-n1]
1199                sig12[0,n1-1] = lows[p]
1200                sig12[n1,n1+n2-1]= meds[p-n1]
1201                sq12[0,n1-1] = lowsq[p]
1202                sq12[n1,n1+n2-1]= medsq[p-n1]
1203                qb12[0,n1-1] = lowqb[p]
1204                qb12[n1,n1+n2-1]= medqb[p-n1]
1205                fs12[0,n1-1] = lowfs[p]
1206                fs12[n1,n1+n2-1]= medfs[p-n1]
1207               
1208                Sort q12, q12,i12,sig12,sq12,qb12,fs12
1209                /////////////////
1210               
1211                //find the maximum point number of set 2  in the overlap region
1212                FindLevel/P/Q medq,(lowq[n1-1])
1213                num2 = trunc(V_levelX)
1214                //Print "num2 = ",num2
1215               
1216                if (auto)
1217                        //there must be overlap points to use auto-scaling
1218                        if(numtype(num2) != 0)
1219                                Abort "There are no data points in the overlap region. Either reduce the number of deleted points or use manual scaling."
1220                        endif
1221                        //do auto-scaling of data
1222                        norm12 = GetScalingInOverlap(num2,lowq,lowi,medq,medi)
1223                        //Set the global variable for the 1-2 scale factor
1224                        Variable/G root:myGlobals:NSORT:gScale1_2 = norm12
1225                else
1226                        //use the value from the panel ( which is the global)
1227                        NVAR temp12 = root:myGlobals:NSORT:gScale1_2
1228                        norm12 = temp12
1229                Endif
1230               
1231                If(normTo== 2)
1232                        //normalize to second file, so multiply 1st by 1/norm
1233                        norm12 = 1/norm12
1234                        lowi *= norm12
1235                        lows *= norm12
1236                else
1237                        //normalize to first file, OR THIRD FILE so multiply 2nd by norm
1238                        medi *= norm12
1239                        meds *= norm12
1240                Endif
1241               
1242                //Print "NSORT-ed ",name1," + ", name2
1243                //Print "normalized to ",normTo
1244                //Print "multiplicative factor = ",norm12
1245                ControlUpdate/A/W=NSORT_Panel
1246                DoUpdate
1247
1248               
1249                //Make the combined, scaled dataset by overwriting the old sets
1250                Make/O/N=(n12) q12,i12,sig12,sq12,qb12,fs12
1251                q12[0,n1-1] = lowq[p]
1252                q12[n1,n1+n2-1]= medq[p-n1]
1253                i12[0,n1-1] = lowi[p]
1254                i12[n1,n1+n2-1]= medi[p-n1]
1255                sig12[0,n1-1] = lows[p]
1256                sig12[n1,n1+n2-1]= meds[p-n1]
1257                sq12[0,n1-1] = lowsq[p]
1258                sq12[n1,n1+n2-1]= medsq[p-n1]
1259                qb12[0,n1-1] = lowqb[p]
1260                qb12[n1,n1+n2-1]= medqb[p-n1]
1261                fs12[0,n1-1] = lowfs[p]
1262                fs12[n1,n1+n2-1]= medfs[p-n1]
1263               
1264                Sort q12, q12,i12,sig12,sq12,qb12,fs12
1265                //at this point 1-2 are combined
1266                //do we need to continue, or write out the set here and stop?
1267                if(cmpstr(name3,"none") == 0)
1268                        //stop here
1269                        norm23 = 1              //norm23 was not used
1270                        Variable/G root:myGlobals:NSORT:gScale2_3 = 1
1271                       
1272                        ControlInfo/W=NSORT_Panel PreviewCheck
1273                        if( V_Value==0 )                //if zero skip the preview and write out the file
1274                                //If any of them have three columns write three column data
1275                                err=Write6ColNSORTedFile(q12,i12,sig12,sq12,qb12,fs12,name1,name2,name3,normToStr,norm12,norm23)
1276                        endif
1277                        // always clean up waves before exiting
1278                        KillWaves/Z q12,i12,sig12,sq12,qb12,fs12
1279                        return err
1280                Endif
1281               
1282                //need to add the third file... which was already loaded at the top of the function
1283                /////
1284                //order points in sets 12-3, indexing overlap region
1285                //put result in temporary waves
1286                WaveStats/Q q12
1287                n12 = V_npnts
1288                WaveStats/Q $"TrimHighQSet_q"
1289                n3 = V_npnts
1290                n123 = n12+ n3
1291               
1292                Make/O/N=(n123) q123,i123,sig123,sq123,qb123,fs123
1293                WAVE highq = $"TrimHighQSet_q"
1294                WAVE highi = $"TrimHighQSet_i"
1295                WAVE highs = $"TrimHighQSet_s"
1296                WAVE highsq = $"TrimHighQSet_sq"
1297                WAVE highqb = $"TrimHighQSet_qb"
1298                WAVE highfs = $"TrimHighQSet_fs"
1299       
1300                q123[0,n12-1] = q12[p]
1301                q123[n1,n12+n3-1]= highq[p-n12]
1302                i123[0,n12-1] = i12[p]
1303                i123[n1,n12+n3-1]= highi[p-n12]
1304                sig123[0,n12-1] = sig12[p]
1305                sig123[n1,n12+n3-1]= highs[p-n12]
1306                sq123[0,n12-1] = sq12[p]
1307                sq123[n1,n12+n3-1]= highsq[p-n12]
1308                qb123[0,n12-1] = qb12[p]
1309                qb123[n1,n12+n3-1]= highqb[p-n12]
1310                fs123[0,n12-1] = fs12[p]
1311                fs123[n1,n12+n3-1]= highfs[p-n12]
1312               
1313                Sort q123, q123,i123,sig123,sq123,qb123,fs123
1314                /////////////////
1315               
1316                //find the maximum point number of set 2  in the overlap region
1317                FindLevel/P/Q highq,(q12[n12-1])
1318                num2 = trunc(V_levelX)
1319                //Print "num2 = ",num2
1320               
1321                if (auto)
1322                        //there must be overlap points to use auto-scaling
1323                        if(numtype(num2) != 0)
1324                                Abort "There are no data points in the overlap region. Either reduce the number of deleted points or use manual scaling."
1325                        endif
1326                        //do auto-scaling of data
1327                        norm23 = GetScalingInOverlap(num2,q12,i12,highq,highi)
1328                        //Set the global variable for the 12 - 3 scale factor
1329                        Variable/G root:myGlobals:NSORT:gScale2_3 = norm23
1330                else
1331                        //use the value from the panel ( which is the global)
1332                        NVAR temp23 = root:myGlobals:NSORT:gScale2_3
1333                        norm23 = temp23
1334                Endif
1335               
1336                If( (normTo== 1) || (normTo ==2) )
1337                        //normalize to first or second file, so multiply third by norm23
1338                        highi *= norm23
1339                        highs *= norm23
1340                else
1341                        //normalize to THIRD file, 1-2 by 1/norm23
1342                        norm23 = 1/norm23
1343                        i12 *= norm23
1344                        sig12 *= norm23
1345                Endif
1346               
1347                //Print "NSORT-ed ",name1," + ", name2, " + ", name3
1348                //Print "normalized to ",normTo
1349                //Print "multiplicative factor 1-2 = ",norm12," multiplicative factor 12 - 3 = ",norm23
1350                ControlUpdate/A/W=NSORT_Panel
1351                DoUpdate
1352               
1353                Make/O/N=(n123) q123,i123,sig123,sq123,qb123,fs123
1354                q123[0,n12-1] = q12[p]
1355                q123[n12,n12+n3-1]= highq[p-n12]
1356                i123[0,n12-1] = i12[p]
1357                i123[n12,n12+n3-1]= highi[p-n12]
1358                sig123[0,n12-1] = sig12[p]
1359                sig123[n12,n12+n3-1]= highs[p-n12]
1360                sq123[0,n12-1] = sq12[p]
1361                sq123[n12,n12+n3-1]= highsq[p-n12]
1362                qb123[0,n12-1] = qb12[p]
1363                qb123[n12,n12+n3-1]= highqb[p-n12]
1364                fs123[0,n12-1] = fs12[p]
1365                fs123[n12,n12+n3-1]= highfs[p-n12]
1366               
1367                Sort q123, q123,i123,sig123,sq123,qb123,fs123
1368                //at this point 12 - 3 are combined
1369                //write out the set here and stop
1370       
1371                ControlInfo/W=NSORT_Panel PreviewCheck
1372                if( V_Value==0 )                //if zero skip the preview and write out the file
1373                        err=Write6ColNSORTedFile(q123,i123,sig123,sq123,qb123,fs123,name1,name2,name3,normToStr,norm12,norm23)
1374                endif
1375                //cleanup waves before exiting
1376                KillWaves/Z q12,i12,sig12,sq12,qb12,fs12,q123,i123,sig123,sq123,qb123,fs123
1377                //combined dataset will already be displayed if the NSORT_Graph is open
1378       
1379                ////////////////
1380                return err
1381   endif // End the 6-column specific stuff here
1382       
1383End
1384
1385
1386
1387/////////////////////////////////////////////////////////////
1388// testing, may speed up NSORT, NCNR-specific naming scheme of
1389// run numbers and a run prefix
1390Proc Set3NSORTFiles(low,med,hi,pref)
1391        Variable low=1,med=2,hi=3
1392        String pref="LIPID"
1393       
1394        //make strings from the numbers
1395        String absStr=""
1396        Variable popNum
1397        DoWindow/F NSORT_Panel
1398       
1399        //lowQ menu
1400        absStr = pref+ThreeDigitString(low)+".ABS"
1401        popNum = 1+WhichListItem(absStr,root:myGlobals:NSORT:gDataPopList,";",0)
1402        PopupMenu popup_1,win=NSORT_Panel,mode=(popNum)
1403        //medQ (a different list for the popup)
1404        absStr = pref+ThreeDigitString(med)+".ABS"
1405        popNum = 1+WhichListItem(absStr,root:myGlobals:NSORT:gDataPopList_3,";",0)
1406        PopupMenu popup_2,win=NSORT_Panel,mode=(popNum)
1407        //highQ (same pop list as medQ)
1408        if(hi != 0)
1409                absStr = pref+ThreeDigitString(hi)+".ABS"
1410                popNum = 1+WhichListItem(absStr,root:myGlobals:NSORT:gDataPopList_3,";",0)
1411                PopupMenu popup_3,win=NSORT_Panel,mode=(popNum)
1412        endif
1413End
1414
1415//make a three character string of the run number
1416Function/S ThreeDigitString(num)
1417        Variable num
1418       
1419        String numStr=""
1420        if(num<10)
1421                numStr = "00"+num2str(num)
1422        else
1423                if(num<100)
1424                        numStr = "0"+num2str(num)
1425                else
1426                        numStr = num2str(num)
1427                Endif
1428        Endif
1429        //Print "numstr = ",numstr
1430        return(numstr)
1431End
1432
1433//more beta procedures - to create a table of scattering runs to combine with NSORT
1434Proc CreateTableToCombine()
1435       
1436        NewDataFolder/O root:myGlobals:CombineTable
1437        DoWindow/F CombineTable
1438       
1439        Make/O/T/N=0 $"root:myGlobals:CombineTable:Filenames"
1440        Make/O/T/N=0 $"root:myGlobals:CombineTable:Suffix"
1441        Make/O/T/N=0 $"root:myGlobals:CombineTable:Labels"
1442        Make/O/D/N=0 $"root:myGlobals:CombineTable:SDD"
1443        Make/O/D/N=0 $"root:myGlobals:CombineTable:RunNumber"
1444        Make/O/D/N=0 $"root:myGlobals:CombineTable:IsTrans"
1445
1446        If(V_Flag==0)
1447                BuildCombineTableWindow()
1448                ModifyTable width(:myGlobals:CombineTable:SDD)=40
1449                ModifyTable width(:myGlobals:CombineTable:Labels)=180
1450               
1451                ModifyTable width(Point)=0              //JUN04, remove point numbers - confuses users since point != run
1452        Endif
1453
1454        //get a list of all files in the folder, some will be junk version numbers that don't exist     
1455        String list,partialName,tempName,temp=""
1456        list = IndexedFile(catPathName,-1,"????")       //get all files in folder
1457        Variable numitems,ii,ok
1458       
1459        //remove version numbers from semicolon-delimited list
1460        list =  RemoveVersNumsFromList(list)
1461        numitems = ItemsInList(list,";")
1462       
1463        //loop through all of the files in the list, reading CAT/SHORT information if the file is RAW SANS
1464        //***version numbers have been removed***
1465        String str,fullName
1466        Variable lastPoint
1467        ii=0
1468       
1469        Make/T/O/N=0 notRAWlist
1470        do
1471                //get current item in the list
1472                partialName = StringFromList(ii, list, ";")
1473                //get a valid file based on this partialName and catPathName
1474                tempName = FindValidFilename(partialName)
1475                If(cmpstr(tempName,"")==0)              //a null string was returned
1476                        //write to notebook that file was not found
1477                        //if string is not a number, report the error
1478                        if(str2num(partialName) == NaN)
1479                                str = "this file was not found: "+partialName+"\r\r"
1480                                //Notebook CatWin,font="Times",fsize=12,text=str
1481                        Endif
1482                else
1483                        //prepend path to tempName for read routine
1484                        PathInfo catPathName
1485                        FullName = S_path + tempName
1486                        //make sure the file is really a RAW data file
1487                        ok = CheckIfRawData(fullName)
1488                        if (!ok)
1489                                //write to notebook that file was not a RAW SANS file
1490                                lastPoint = numpnts(notRAWlist)
1491                                InsertPoints lastPoint,1,notRAWlist
1492                                notRAWlist[lastPoint]=tempname
1493                        else
1494                                //go write the header information to the Notebook
1495                                GetHeaderInfoToCombineWave(fullName,tempName)
1496                        Endif
1497                Endif
1498                ii+=1
1499        while(ii<numitems)
1500//Now sort them all based on the suffix data (orders them as collected)
1501//      SortCombineWaves()
1502// sort by label
1503        SortCombineByLabel()
1504// remove the transmission waves
1505//
1506        RemoveTransFilesFromCombine()
1507//
1508// make the waves and table for the sets to combine
1509        Make/O/N=0 $"root:myGlobals:CombineTable:LowRun"
1510        Make/O/N=0 $"root:myGlobals:CombineTable:MediumRun"
1511        Make/O/N=0 $"root:myGlobals:CombineTable:HighRun"
1512        Make/O/T/N=0 $"root:myGlobals:CombineTable:Prefix"
1513        Make/O/T/N=0 $"root:myGlobals:CombineTable:SaveName"
1514        MakeTableToCombine()
1515
1516End
1517
1518
1519Function RemoveTransFilesFromCombine()
1520        Wave/T filenames = $"root:myGlobals:CombineTable:Filenames"
1521        Wave/T suffix = $"root:myGlobals:CombineTable:Suffix"
1522        Wave/T labels = $"root:myGlobals:CombineTable:Labels"
1523        Wave sdd = $"root:myGlobals:CombineTable:SDD"
1524        Wave runnum = $"root:myGlobals:CombineTable:RunNumber"
1525        Wave isTrans = $"root:myGlobals:CombineTable:IsTrans"
1526       
1527        Variable num=numpnts(isTrans),ii
1528        ii=num-1
1529        do
1530                if(isTrans[ii] != 0)
1531                        DeletePoints ii, 1, filenames,suffix,labels,sdd,runnum,isTrans
1532                endif
1533                ii-=1
1534        while(ii>=0)
1535        return(0)
1536End
1537
1538Function MakeTabletoCombine()
1539
1540        Wave low = $"root:myGlobals:CombineTable:LowRun"
1541        Wave medium = $"root:myGlobals:CombineTable:MediumRun"
1542        Wave high = $"root:myGlobals:CombineTable:HighRun"
1543        Wave/T prefix = $"root:myGlobals:CombineTable:Prefix"
1544        Wave/T saveName = $"root:myGlobals:CombineTable:SaveName"
1545
1546        DoWindow/F ToCombine
1547        if(V_flag==0)
1548                edit Low,Medium,High,Prefix,SaveName as "Run Numbers to Combine"
1549                DoWindow/C ToCombine
1550        endif
1551        AutoPositionWindow/M=1/R=CombineTable toCombine
1552       
1553       
1554        /////
1555//      SetWindow kwTopWin hook=CombineTableHook, hookevents=1  // mouse down events
1556       
1557end
1558
1559Function BuildCombineTableWindow()
1560        Wave/T Filenames = $"root:myGlobals:CombineTable:Filenames"
1561        Wave/T Labels = $"root:myGlobals:CombineTable:Labels"
1562        Wave SDD = $"root:myGlobals:CombineTable:SDD"
1563        Wave/T suffix = $"root:myGlobals:CombineTable:Suffix"
1564        Wave runnum = $"root:myGlobals:CombineTable:RunNumber"
1565        Wave isTrans = $"root:myGlobals:CombineTable:IsTrans"
1566       
1567//      Edit Filenames, Labels, DateAndTime, SDD, Lambda, CntTime, TotCnts, CntRate, Transmission, Thickness, XCenter, YCenter, NumAttens as "Files to Combine"
1568        Edit Labels, SDD, runNum as "Files to Combine"
1569        SetWindow kwTopWin hook=CombineTableHook, hookevents=1  // mouse down events
1570
1571        String name="CombineTable"
1572        DoWindow/C $name
1573        return(0)
1574End
1575
1576//reads header information and puts it in the appropriate waves for display in the table.
1577//fname is the full path for opening (and reading) information from the file
1578//which alreay was found to exist. sname is the file;vers to be written out,
1579//avoiding the need to re-extract it from fname.
1580Function GetHeaderInfoToCombineWave(fname,sname)
1581        String fname,sname
1582       
1583        String textstr,temp,lbl,date_time,suffix
1584        Variable ctime,lambda,sdd,detcnt,cntrate,refNum,trans,thick,xcenter,ycenter,numatten
1585        Variable lastPoint, beamstop
1586
1587        Wave/T GFilenames = $"root:myGlobals:CombineTable:Filenames"
1588        Wave/T GSuffix = $"root:myGlobals:CombineTable:Suffix"
1589        Wave/T GLabels = $"root:myGlobals:CombineTable:Labels"
1590        Wave GSDD = $"root:myGlobals:CombineTable:SDD"
1591        Wave GRunNumber = $"root:myGlobals:CombineTable:RunNumber"
1592        Wave GIsTrans = $"root:myGlobals:CombineTable:IsTrans"
1593       
1594        lastPoint = numpnts(GLambda)
1595               
1596        InsertPoints lastPoint,1,GFilenames
1597        GFilenames[lastPoint]=sname
1598       
1599        //read the file suffix
1600        InsertPoints lastPoint,1,GSuffix
1601        GSuffix[lastPoint]=getSuffix(fname)
1602
1603        // read the sample.label text field
1604        InsertPoints lastPoint,1,GLabels
1605        GLabels[lastPoint]=getSampleLabel(fname)
1606       
1607        //read in the SDD
1608        InsertPoints lastPoint,1,GSDD
1609        GSDD[lastPoint]= getSDD(fname)
1610
1611        //the run number (not displayed in the table, but carried along)
1612        InsertPoints lastPoint,1,GRunNumber
1613        GRunNumber[lastPoint] = GetRunNumFromFile(sname)
1614
1615        // 0 if the file is a scattering  file, 1 (truth) if the file is a transmission file
1616        InsertPoints lastPoint,1,GIsTrans
1617        GIsTrans[lastPoint]  = isTransFile(fname)               //returns one if beamstop is "out"
1618       
1619        KillWaves/Z w
1620        return(0)
1621End
1622
1623//sorts all of the waves of header information using the suffix (A123)
1624//the result is that all of the data is in the order that it was collected,
1625// regardless of how the prefix or run numbers were changed by the user
1626Function SortCombineWaves()
1627        Wave/T GFilenames = $"root:myGlobals:CombineTable:Filenames"
1628        Wave/T GSuffix = $"root:myGlobals:CombineTable:Suffix"
1629        Wave/T GLabels = $"root:myGlobals:CombineTable:Labels"
1630        Wave GSDD = $"root:myGlobals:CombineTable:SDD"
1631        Wave GRunNumber = $"root:myGlobals:CombineTable:RunNumber"
1632        Wave GIsTrans = $"root:myGlobals:CombineTable:IsTrans"
1633
1634//      Sort GSuffix, GSuffix, GFilenames, GLabels, GDateTime, GSDD, GLambda, GCntTime, GTotCnts, GCntRate, GTransmission, GThickness, GXCenter, GYCenter, GNumAttens,GRunNumber,GIsTrans
1635        Sort GSuffix, GSuffix, GFilenames, GLabels, GSDD, GRunNumber, GIsTrans
1636        return(0)
1637End
1638
1639//sorts all of the waves of header information using the suffix (A123)
1640//the result is that all of the data is in the order that it was collected,
1641// regardless of how the prefix or run numbers were changed by the user
1642Function SortCombineByLabel()
1643        Wave/T GFilenames = $"root:myGlobals:CombineTable:Filenames"
1644        Wave/T GSuffix = $"root:myGlobals:CombineTable:Suffix"
1645        Wave/T GLabels = $"root:myGlobals:CombineTable:Labels"
1646        Wave GSDD = $"root:myGlobals:CombineTable:SDD"
1647        Wave GRunNumber = $"root:myGlobals:CombineTable:RunNumber"
1648        Wave GIsTrans = $"root:myGlobals:CombineTable:IsTrans"
1649
1650        Sort GLabels, GSuffix, GFilenames, GLabels, GSDD, GRunNumber, GIsTrans
1651//      Sort {GLabels, GSDD}, GSuffix, GFilenames, GLabels, GSDD, GRunNumber, GIsTrans          //sort on GLabels, GSDD breaks the tie
1652        return(0)
1653End
1654
1655//main procedure, called from the menu
1656// sets a flag (temporarily) to use the names from the table
1657// during the procedure that writes the data files.
1658//
1659//
1660Proc  DoCombineFiles()
1661       
1662        // pop all of the menus to make sure that they are properly populated
1663        LowQPopMenuProc("",1,"")
1664        MedQPopMenuProc("",1,"")
1665        HighQPopMenuProc("",1,"")
1666       
1667        String savedDataFolder = GetDataFolder(1)               // save
1668        SetDataFolder root:myGlobals:CombineTable:
1669
1670        Variable/G useTable=1
1671       
1672        Variable num=numpnts(lowRun),ii,lowFile,medFile,hiFile
1673        String prefixStr = ""
1674        Pathinfo catPathName
1675        String path=S_Path
1676       
1677        ii=0
1678        do
1679                lowFile = LowRun[ii]
1680                medFile = MediumRun[ii]
1681                hiFile = highRun[ii]
1682                prefixStr = prefix[ii]
1683               
1684                Set3NSORTFiles(lowFile,medFile,hiFile,prefixStr)                //set the files and pop the NSORT popups
1685               
1686                //pass the new file name in as a global (ugh!)
1687                String/G root:myGlobals:CombineTable:SaveNameStr = path+saveName[ii]
1688                //combine the files and write the data
1689                WriteNSORTFileButton("")
1690               
1691                ii+=1
1692        while(ii<num)
1693
1694        Variable/G useTable=0
1695       
1696        SetDataFolder savedDataFolder
1697
1698End
1699
1700
1701// Commentized lines here are incomplete - and NON-FUNCTIONING
1702//
1703//// Window hook example:
1704//
1705Function CombineTableHook(infoStr)
1706        String infoStr
1707        String event= StringByKey("EVENT",infoStr)
1708//      Print "EVENT= ",event
1709        strswitch(event)
1710                case "mousedown":
1711                        Variable xpix= NumberByKey("MOUSEX",infoStr)
1712                        Variable ypix= NumberByKey("MOUSEY",infoStr)
1713                        Variable modif= NumberByKey("MODIFIERS",infoStr)
1714                        //print modif
1715                        if(modif & 2^1)         //bit 1 set, shift key is down
1716                                PopupContextualMenu/C=(xpix, ypix) "combine;"
1717                                strswitch(S_selection)
1718                                        case "combine":
1719                                                //Print "combine the files"
1720                                                SendSelectionToTable()
1721                                                break
1722        //                              case "no":
1723        //                                      break
1724        //                              case "maybe":
1725        //                                      // do something because "maybe" was chosen
1726        //                                      break
1727                                endswitch               //on selection
1728                        endif
1729        endswitch       // on event
1730       
1731        return 0
1732End
1733
1734//ASSUMES 3 FILES!!!!
1735Function SendSelectionToTable()
1736
1737        DoWindow/F CombineTable
1738        if(V_flag==0)
1739//              Make/O/N=0 $"root:myGlobals:CombineTable:Low"
1740//              Make/O/N=0 $"root:myGlobals:CombineTable:Medium"
1741//              Make/O/N=0 $"root:myGlobals:CombineTable:High"
1742//              Make/O/T/N=0 $"root:myGlobals:CombineTable:Prefix"
1743//              Make/O/T/N=0 $"root:myGlobals:CombineTable:SaveName"
1744//              edit Low,Medium,High,Prefix,SaveName as "Run Numbers to Combine"
1745//              DoWindow/C ToCombine
1746
1747                return(0)
1748               
1749        else
1750                Wave low = $"root:myGlobals:CombineTable:LowRun"
1751                Wave medium = $"root:myGlobals:CombineTable:MediumRun"
1752                Wave high = $"root:myGlobals:CombineTable:HighRun"
1753                Wave/T prefix = $"root:myGlobals:CombineTable:Prefix"
1754                Wave/T saveName = $"root:myGlobals:CombineTable:SaveName"
1755               
1756                Wave/T gLabels = $"root:myGlobals:CombineTable:Labels"
1757                Wave gSDD = $"root:myGlobals:CombineTable:SDD"
1758                Wave gRunNumber = $"root:myGlobals:CombineTable:RunNumber"
1759                Wave/T filenames = $"root:myGlobals:CombineTable:FileNames"
1760        endif
1761       
1762        GetSelection table,CombineTable,3
1763//      Print V_startRow, V_endRow
1764       
1765        Variable num=V_endRow-V_startRow+1
1766        Variable ii
1767        Make/O/T/N=(num) tmpLbl
1768        Make/O/N=(num) tmpSDD,tmpRun
1769        for(ii=V_startRow;ii<=V_endRow;ii+=1)
1770                tmpLbl[ii-V_startRow] = gLabels[ii]
1771                tmpSDD[ii-V_startRow] = gSDD[ii]
1772                tmpRun[ii-V_startRow] = gRunNumber[ii]
1773        endfor
1774        Sort tmpSDD, tmpSDD,tmpLbl,tmpRun
1775       
1776//      Print tmpSDD
1777       
1778        num=numpnts(low)
1779        InsertPoints num, 1, low,medium,high,prefix,SaveName
1780        low[num] = tmpRun[2]
1781        medium[num] = tmpRun[1]
1782        high[num] = tmpRun[0]
1783        prefix[num] = GetPrefixStrFromFile(filenames[ii])
1784       
1785        //prompt for combined name
1786        String saveStr=""
1787        Prompt saveStr,"saved file name"
1788        DoPrompt "Enter the combined file name",saveStr
1789        saveName[num] = saveStr
1790       
1791        return(0)
1792end
1793
1794//given a filename of a SANS data filename of the form
1795//TTTTTnnn.SAn_TTT_Txxx
1796//returns the prefix "TTTTT" as some number of characters
1797//returns "" as an invalid file prefix
1798//
1799// NCNR-specifc, does not really belong here - but it's a beta procedure anyhow...
1800//
1801Function/S GetPrefixStrFromFile(item)
1802        String item
1803        String invalid = ""     //"" is not a valid run prefix, since it's text
1804        Variable num=-1
1805       
1806        //find the "dot"
1807        String runStr=""
1808        Variable pos = strsearch(item,".",0)
1809        if(pos == -1)
1810                //"dot" not found
1811                return (invalid)
1812        else
1813                //found, skip the three characters preceeding it
1814                if (pos <=3)
1815                        //not enough characters
1816                        return (invalid)
1817                else
1818                        runStr = item[0,pos-4]
1819                        return (runStr)
1820                Endif
1821        Endif
1822End
Note: See TracBrowser for help on using the repository browser.