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

Last change on this file since 167 was 167, checked in by srkline, 16 years ago

Changed NSORT to display rescaling and overlap constants behind the OS file save dialog, allowing the user to cancel the save it something is amiss.

No significant change in trans=1 panel behavior in Correct.ipf

Cosmetic changes to RawWindowHook? (drawing order) to look nicer w/OS native panels.

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