source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/SANS/QKK_NSORT.ipf @ 986

Last change on this file since 986 was 986, checked in by srkline, 7 years ago

fixing issues with QUOKKA reduction procedures that generate errors in Igor 7

updated the QKK_NSORT file to match the new 4-file NSORT. this was a complete, direct copy of the file as there were no Ansto -specific bits.

File size: 81.9 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma version=5.0
3#pragma IgorVersion=6.1
4
5//****************************
6// Vers. 1.2 092101
7//
8// NSORT panel for combining and inter-normalizing 2 or 3 datasets
9// that have previously been averaged
10//
11// MAR 2016 SRK added functionality to combine 4 datasets
12//
13// - handles 3 or 6-column datasets
14// allows manual scaling
15// allows user to interactively delete data points from either end of any datset
16//
17//*********************
18
19//main entry point for displaying the nsort panel
20//initializes folder/globals as needed
21//
22Proc ShowNSORTPanel()
23        DoWindow/F NSORT_Panel
24        if(V_flag==0)
25                InitNSORTPanel()
26                NSORT_Panel()
27        Endif
28        SetDataFolder root:
29        PathInfo/S catPathName
30        String junk = S_path
31        if (V_flag == 0)
32                //path does not exist - no folder selected
33                String/G root:myGlobals:NSORT:gPathStr = "no folder selected"
34        else
35                String/G root:myGlobals:NSORT:gPathStr = junk
36        endif
37        LowQPopMenuProc("",1,"")
38        MedQPopMenuProc("",1,"")
39        HighQPopMenuProc("",1,"")
40        HighestQPopMenuProc("",1,"")
41End
42
43//initializes globals/folder for the NSORT panel as needed
44//all of the globals are stored in root:myGlobals:NSORT folder
45//the globals are the values displayed in the panel
46//
47Proc InitNSORTPanel()
48
49        //set up the global variables needed for the NSORT panel
50        NewDataFolder/O root:myGlobals:NSORT
51        Variable/G root:myGlobals:NSORT:gScale1_2 = 1
52        Variable/G root:myGlobals:NSORT:gScale2_3 = 1
53        Variable/G root:myGlobals:NSORT:gScale3_4 = 1
54        //
55        //save the number of points to trim from beginning/end of the data files
56        //
57        Variable/G root:myGlobals:NSORT:gPtsBeg1 = NumVarOrDefault("root:myGlobals:NSORT:gPtsBeg1", 0 )
58        Variable/G root:myGlobals:NSORT:gPtsEnd1 = NumVarOrDefault("root:myGlobals:NSORT:gPtsEnd1", 0 )
59        Variable/G root:myGlobals:NSORT:gPtsBeg2 = NumVarOrDefault("root:myGlobals:NSORT:gPtsBeg2", 0 )
60        Variable/G root:myGlobals:NSORT:gPtsEnd2 = NumVarOrDefault("root:myGlobals:NSORT:gPtsEnd2", 0 )
61        Variable/G root:myGlobals:NSORT:gPtsBeg3 = NumVarOrDefault("root:myGlobals:NSORT:gPtsBeg3", 0 )
62        Variable/G root:myGlobals:NSORT:gPtsEnd3 = NumVarOrDefault("root:myGlobals:NSORT:gPtsEnd3", 0 )
63        Variable/G root:myGlobals:NSORT:gPtsBeg4 = NumVarOrDefault("root:myGlobals:NSORT:gPtsBeg4", 0 )
64        Variable/G root:myGlobals:NSORT:gPtsEnd4 = NumVarOrDefault("root:myGlobals:NSORT:gPtsEnd4", 0 )
65       
66        Variable/G root:myGlobals:NSORT:gColumns1 = 0
67        Variable/G root:myGlobals:NSORT:gColumns2 = 0
68        Variable/G root:myGlobals:NSORT:gColumns3 = 0
69        Variable/G root:myGlobals:NSORT:gColumns4 = 0
70        Variable/G root:myGlobals:NSORT:gNormToNum = 1
71        String/G root:myGlobals:NSORT:gPathStr = ""
72        String/G root:myGlobals:NSORT:gDataPopList = "none"
73        String/G root:myGlobals:NSORT:gDataPopList_3 = "none"
74       
75        SetDataFolder root:             //(redundant)
76End
77
78//New loader that uses data folders etc...
79//AJJ Jan 2010
80Function LoadDataForNSORT(fileStr,setNum)
81        String fileStr          //full path:name to a valid file
82        Variable setNum //number of set (used for naming) = 0, 1, or 2 (ONLY), (2016) 3 is now valid for 4th data set
83       
84        Variable err=0
85
86        String nm0,nm1,nm2
87        //String firstFileName = S_fileName
88        Variable pt=0,begPts,endPts,numCols
89       
90        NVAR gColumns1 = root:myGlobals:NSORT:gColumns1
91        NVAR gColumns2 = root:myGlobals:NSORT:gColumns2
92        NVAR gColumns3 = root:myGlobals:NSORT:gColumns3
93        NVAR gColumns4 = root:myGlobals:NSORT:gColumns4
94        NVAR begPts1 = root:myGlobals:NSORT:gPtsBeg1
95        NVAR endPts1 = root:myGlobals:NSORT:gPtsEnd1
96        NVAR begPts2 = root:myGlobals:NSORT:gPtsBeg2
97        NVAR endPts2 = root:myGlobals:NSORT:gPtsEnd2
98        NVAR begPts3 = root:myGlobals:NSORT:gPtsBeg3
99        NVAR endPts3 = root:myGlobals:NSORT:gPtsEnd3
100        NVAR begPts4 = root:myGlobals:NSORT:gPtsBeg4
101        NVAR endPts4 = root:myGlobals:NSORT:gPtsEnd4
102       
103        String cmd
104        String typStr= "", trimStr=""
105       
106        switch (setNum)
107                case 1:
108                        sprintf cmd , "A_LoadOneDDataToName(\"%s\",\"%s\",%d,%d)",fileStr,"LowQSet",0,1
109                        Execute cmd
110                        typStr = "LowQSet"
111                        trimStr = "TrimLowQSet"
112                        begPts = begPts1
113                        endPts = endPts1
114                        break
115                case 2:
116                        sprintf cmd , "A_LoadOneDDataToName(\"%s\",\"%s\",%d,%d)",fileStr,"MedQSet",0,1
117                        Execute cmd             
118                        typStr = "MedQSet"
119                        trimStr = "TrimMedQSet"
120                        begPts = begPts2
121                        endPts = endPts2
122                        break
123                case 3:
124                        sprintf cmd , "A_LoadOneDDataToName(\"%s\",\"%s\",%d,%d)",fileStr,"HighQSet",0,1
125                        Execute cmd
126                        typStr = "HighQSet"
127                        trimStr = "TrimHighQSet"
128                        begPts = begPts3
129                        endPts = endPts3
130                        break
131                case 4:
132                        sprintf cmd , "A_LoadOneDDataToName(\"%s\",\"%s\",%d,%d)",fileStr,"HighestQSet",0,1
133                        Execute cmd
134                        typStr = "HighestQSet"
135                        trimStr = "TrimHighestQSet"
136                        begPts = begPts4
137                        endPts = endPts4
138                        break
139        endswitch
140               
141        String typPrefix = "root:"+typStr+":"+typStr
142        String trimPrefix = "root:"+typStr+":"+trimStr
143       
144        if (WaveExists($(typPrefix+"_res")))
145                //6 col data loaded
146//              print "6 col data loaded"
147                numCols = 6
148                Duplicate/O $(typPrefix+"_q") $(trimPrefix+"_q")
149                Duplicate/O $(typPrefix+"_i") $(trimPrefix+"_i")
150                Duplicate/O $(typPrefix+"_s") $(trimPrefix+"_s")
151                Duplicate/O $(typPrefix+"_res") $(trimPrefix+"_res")
152                //Trimmed data set
153//              Duplicate/O $(typPrefix+"_q"),$(trimPrefix+"_q")
154//              Duplicate/O $(typPrefix+"_i"),$(trimPrefix+"_i")
155//              Duplicate/O $(typPrefix+"_s"),$(trimPrefix+"_s")
156                WaveStats/Q $(typPrefix+"_q")                   //get info about the original q-values read in
157                pt = V_npnts-endPts
158                DeletePoints pt,endPts,$(trimPrefix+"_q"),$(trimPrefix+"_i"),$(trimPrefix+"_s"),$(trimPrefix+"_res")    //delete end points first
159                DeletePoints 0,begPts,$(trimPrefix+"_q"),$(trimPrefix+"_i"),$(trimPrefix+"_s"),$(trimPrefix+"_res")     //then delete points from beginning                     
160        else
161                //Assume
162                //3 col data loaded
163//              print "Assuming 3 col data loaded"
164                numcols = 3
165                Duplicate/O $(typPrefix+"_q") $(trimPrefix+"_q")
166                Duplicate/O $(typPrefix+"_i") $(trimPrefix+"_i")
167                Duplicate/O $(typPrefix+"_s") $(trimPrefix+"_s")               
168                //Trimmed data set
169//              Duplicate/O $(typPrefix+"_q"),$(trimPrefix+"_q")
170//              Duplicate/O $(typPrefix+"_i"),$(trimPrefix+"_i")
171//              Duplicate/O $(typPrefix+"_s"),$(trimPrefix+"_s")
172                WaveStats/Q $(typPrefix+"_q")                   //get info about the original q-values read in
173                pt = V_npnts-endPts
174                DeletePoints pt,endPts,$(trimPrefix+"_q"),$(trimPrefix+"_i"),$(trimPrefix+"_s") //delete end points first
175                DeletePoints 0,begPts,$(trimPrefix+"_q"),$(trimPrefix+"_i"),$(trimPrefix+"_s")  //then delete points from beginning                                                     
176        endif
177
178        switch (setNum)
179                case 1:
180                        gColumns1 = numCols
181                        break
182                case 2:
183                        gColumns2 = numCols
184                        break
185                case 3:
186                        gColumns3 = numCols
187                        break
188                case 4:
189                        gColumns4 = numCols
190                        break
191        endswitch
192       
193        return(0)
194       
195End
196
197Function WriteNSORTedFile(q3,i3,sig3,firstFileName,secondFileName,thirdFileName,fourthFileName,normTo,norm12,norm23,norm34,[res])
198        Wave q3,i3,sig3,res
199        String firstFileName,secondFileName,thirdFileName,fourthfileName,normTo
200        Variable norm12,norm23,norm34
201
202        NVAR useXMLOutput = root:Packages:NIST:gXML_Write
203
204        if (useXMLOutput == 1)
205                if(WaveExists(res))
206                        WriteNSORTedXMLFile(q3,i3,sig3,firstFileName,secondFileName,thirdFileName,fourthFileName,normTo,norm12,norm23,norm34,res=res)
207                else
208                        WriteNSORTedXMLFile(q3,i3,sig3,firstFileName,secondFileName,thirdFileName,fourthFileName,normTo,norm12,norm23,norm34)
209                endif
210        else
211                if(WaveExists(res))
212                        WriteOLDNSORTedFile(q3,i3,sig3,firstFileName,secondFileName,thirdFileName,fourthFileName,normTo,norm12,norm23,norm34,res=res)
213                else
214                        WriteOLDNSORTedFile(q3,i3,sig3,firstFileName,secondFileName,thirdFileName,fourthFileName,normTo,norm12,norm23,norm34)
215                endif           
216        endif
217
218End
219
220
221Function WriteOLDNSORTedFile(q3,i3,sig3,firstFileName,secondFileName,thirdFileName,fourthFileName,normTo,norm12,norm23,norm34,[res])
222        Wave q3,i3,sig3,res
223        String firstFileName,secondFileName,thirdFileName,fourthFileName,normTo
224        Variable norm12,norm23,norm34
225
226        Variable err=0,refNum,numCols,dialog=1
227        String fullPath="",formatStr="",str2
228        //check each wave - else REALLY FATAL error when writing file
229        If(!(WaveExists(q3)))
230                err = 1
231                return err
232        Endif
233        If(!(WaveExists(i3)))
234                err = 1
235                return err
236        Endif
237        If(!(WaveExists(sig3)))
238                err = 1
239                return err
240        Endif
241       
242        if(WaveExists(res))
243                numCols = 6
244        else
245                numCols = 3
246        endif
247       
248// 05SEP05 SRK -- added to automatically combine files from a table - see the end of NSORT.ipf for details
249// - use the flag set in DoCombineFiles() to decide if the table entries should be used
250//Ê Êroot:myGlobals:CombineTable:useTable= (1) (0)
251//if(exists("root:myGlobals:CombineTable:SaveName"))
252        NVAR/Z useTable = root:myGlobals:CombineTable:useTable
253        if(NVAR_Exists(useTable) && useTable==1)
254                SVAR str=root:myGlobals:CombineTable:SaveNameStr        //messy, but pass in as a global
255                fullPath = str
256//              str2 = "Is the file name "+str+" correct?"
257//              DoAlert 1,str2
258//              if(V_flag==1)
259                        dialog=0                //bypass the dialog if the name is good (assumed, since DoAlert is bypassed)
260//              endif
261        endif
262       
263        if(dialog)
264                PathInfo/S catPathName
265                fullPath = DoSaveFileDialog("Save data as",fname="",suffix=".ABS")              //won't actually open the file
266                If(cmpstr(fullPath,"")==0)
267                        //user cancel, don't write out a file
268                        Close/A
269                        Abort "no data file was written"
270                Endif
271                //Print "dialog fullpath = ",fullpath
272        Endif
273       
274//      // read in the header information from each of the combined files and put this information in the file header
275//      String dum,hdr1="none\r\n",hdr2="none\r\n",hdr3="none\r\n"
276//      PathInfo catPathName
277//     
278//      // the first file exists, check anyways
279//      if(cmpstr(firstFileName,"none") !=0)
280//              Open/R refNum as S_Path+firstFileName
281//              FReadLine refNum, dum
282//              FReadLine refNum, hdr1                  //just grab the second line
283//              Close refNum
284//      endif
285//      //second file
286//      if(cmpstr(secondFileName,"none") !=0)
287//              Open/R refNum as S_Path+secondFileName
288//              FReadLine refNum, dum
289//              FReadLine refNum, hdr2                  //just grab the second line
290//              Close refNum
291//      endif
292//      // third file
293//      if(cmpstr(thirdFileName,"none") !=0)
294//              Open/R refNum as S_Path+thirdFileName
295//              FReadLine refNum, dum
296//              FReadLine refNum, hdr3                  //just grab the second line
297//              Close refNum
298//      endif
299
300       
301        //actually open the file
302        Open refNum as fullpath
303       
304        fprintf refnum, "COMBINED FILE CREATED: %s \r\n",date()
305       
306//      fprintf refnum, "FIRST File %s",hdr1            //new, Mar 2008
307//      fprintf refnum, "SECOND File %s",hdr2           //new, Mar 2008
308//      fprintf refnum, "THIRD File %s",hdr3            //new, Mar 2008
309       
310        fprintf refNum, "NSORT-ed   %s\t  +  %s\t  +  %s\t + %s\r\n",firstFileName, secondFileName,thirdFileName,fourthFileName
311        fprintf refNum, "normalized to   %s\r\n",normTo
312        fprintf refNum, "multiplicative factor 1-2 = %12.8g\t multiplicative factor 2-3 = %12.8g\t multiplicative factor 3-4 = %12.8g\r\n",norm12,norm23,norm34
313
314        if (numCols == 3)
315                formatStr = "%15.4g %15.4g %15.4g\r\n"
316                fprintf refnum, "The 3 columns are | Q (1/A) | I(Q) (1/cm) | std. dev. I(Q) (1/cm) |\r\n"
317                wfprintf refnum, formatStr, q3,i3,sig3
318        elseif (numCols == 6)
319                Make/O/N=(dimsize(res,0)) sigq3 = res[p][0]
320                Make/O/N=(dimsize(res,0)) qbar3 = res[p][1]
321                Make/O/N=(dimsize(res,0)) fs3 = res[p][2]
322       
323                formatStr = "%15.4g %15.4g %15.4g %15.4g %15.4g %15.4g\r\n"     
324                fprintf refnum, "The 6 columns are | Q (1/A) | I(Q) (1/cm) | std. dev. I(Q) (1/cm) | sigmaQ | meanQ | ShadowFactor|\r\n"
325                wfprintf refnum, formatStr, q3,i3,sig3,sigq3,qbar3,fs3
326        endif
327       
328        Close refnum
329       
330        Return err
331End
332
333
334//gets the scaling constant to make (best) overlap of the specified datasets
335//the scaling value is an average value of the individual scaling values for
336//every data point (at the low q end) of set 2 that overlaps with set 1
337//(as if set 1 were held fixed)
338//num2 is the highest point number in set 2 that can overlap with set 1
339//(num2+1) individual scaling values are computed
340//wave2 must be multiplied by norm to rescale to wave1
341//the scale factor is the return value
342//
343Function NCNR_GetScalingInOverlap(num2,wave1q,wave1i,wave2q,wave2i)
344        Variable num2           //largest point number of wave2 in overlap region
345        Wave wave1q,wave1i,wave2q,wave2i                //1 = first dataset, 2= second dataset
346
347        Variable ii,ival1,newi,ratio
348        ratio=0
349        ii=0
350        do
351                //get scaling factor at each point of wave 2 in the overlap region
352                newi = interp(wave2q[ii],wave1q,wave1i)         //get the intensity of wave1 at an overlap point
353                ratio += newi/wave2i[ii]                                        //get the scale factor
354                //Print "ratio = ",ratio
355                ii+=1
356        while(ii<=num2)
357        Variable val
358        val = ratio/(num2+1)            // +1 counts for point zero
359        //Print "val = ",val
360
361        Variable tol=1.05                       //5% is the preferred number (for Andrew and Lionel, at least)
362
363        ControlInfo/W=NSORT_Panel WarningCheck
364        if(( V_Value==1 ) && ( (val > tol) || (val < 1/tol) ) )
365                String str=""
366                sprintf str,"The scaling factor is more than a factor of %g from 1. Proceed with caution.\r",tol
367                DoAlert 0,str
368        endif
369       
370        Return val
371End
372
373Function ShowNSORTHelp(ctrlName) : ButtonControl
374        String ctrlName
375        DisplayHelpTopic/Z/K=1 "SANS Data Reduction Tutorial[Sort and Combine Averaged Datasets]"
376        if(V_flag !=0)
377                DoAlert 0,"The SANS Data Reduction Tutorial Help file could not be found"
378        endif
379End
380
381//button action procedure that simply closes the NSORT panel when done
382//and removes the NSORT-specific waves that were created
383// - the graph window must be killed first, so that the waves will not
384//be in use
385//
386Function NSORT_DoneButton(ctrlName) : ButtonControl
387        String ctrlName
388       
389        DoWindow/K NSORT_Panel
390       
391        DoWindow/K NSORT_Graph
392       
393        //clean up the temporary waves in the root: folder
394        SetDataFolder root:
395
396        KillDataFolder/Z LowQSet
397        KillDataFolder/Z MedQSet
398        KillDataFolder/Z HighQSet
399        KillDataFolder/Z HighestQSet
400
401End
402
403//button action procedure that plots dataset specified
404//on an NSORT graph window.
405//switch is on input controlName (low-med-high set)
406//parses partial filename from corresponding popup menu
407//builds a valid filename, loads the data (re-loads if already on graph)
408//and plots twice - once for the full datset (open symbols)
409//and once for the "trimmed" dataset (solid symbols, same color)
410//
411Function Plot_0_Button(ctrlName) : ButtonControl
412        String ctrlName
413
414        String tempName="",partialName=""
415        Variable setNum,err
416        //switch on ctrlName string - Plot_1, Plot_2, Plot_3
417       
418        strswitch(ctrlName)     // string switch
419                case "Plot_1":         
420                        //low-q
421                        setNum = 1
422                        ControlInfo/W=NSORT_Panel popup_1
423                        break
424                case "Plot_2":         
425                        //medium-q
426                        setNum = 2
427                        ControlInfo/W=NSORT_Panel popup_2
428                        break
429                case "Plot_3":         
430                        //high-q
431                        setNum = 3
432                        ControlInfo/W=NSORT_Panel popup_3
433                        break
434                case "Plot_4":         
435                        //highest-q
436                        setNum = 4
437                        ControlInfo/W=NSORT_Panel popup_4
438                        break
439        endswitch
440       
441
442        //find the file from the partial filename
443        If( (cmpstr(S_value,"")==0) || (cmpstr(S_value,"none")==0) )
444                //null selection, or "none" from any popup
445                Abort "no file selected in popup menu"
446        else
447                //selection not null
448                partialName = S_value
449                //Print partialName
450        Endif
451        //get a valid file based on this partialName and catPathName
452        tempName = FindValidFilename(partialName)
453       
454        //prepend path to tempName for read routine
455        PathInfo catPathName
456        tempName = S_path + tempName
457       
458        //load in the data (into the root directory)
459        err = LoadDataForNSORT(tempName,setNum)
460       
461        //bring the plot to the front, and put the new data on it
462        //and put cursors on the plotted dataset
463        //if the dataset is already on the graph, it will have been overwritten and updated by the load routine
464        //actually plot it twice, open(opaque) circles for the full dataset,
465        // then solid (filled) circles for the points actually kept
466        String list="",searchStr=""
467        Variable isOnPlot=0
468       
469//      DoWindow/F NSORT_Graph
470        if(WinType("NSORT_Graph")==0)
471                //no window, create one
472                strswitch(ctrlName)     // string switch
473                        case "Plot_1":         
474                                //low-q
475                                Display/K=1
476                                DoWindow/C NSORT_Graph
477                                DisplayLowSet()
478                                break
479                        case "Plot_2":         
480                                //medium-q
481                                Display/K=1
482                                DoWindow/C NSORT_Graph
483                                DisplayMedSet()
484                                break
485                        case "Plot_3":         
486                                //high-q
487                                Display/K=1
488                                DoWindow/C NSORT_Graph
489                                DisplayHighSet()
490                                break
491                        case "Plot_4":         
492                                //highest-q
493                                Display/K=1
494                                DoWindow/C NSORT_Graph
495                                DisplayHighestSet()
496                                break
497                endswitch
498                Legend
499        else
500                //plot already exists, waves have been updated
501                //make sure that the desired waves are actually on the graph, and add them if they aren't
502                list = TraceNameList("NSORT_Graph",";",1)
503       
504                strswitch(ctrlName)     // string switch
505                        case "Plot_1":         
506                                //low-q
507                                isOnPlot = strsearch(list, "LowQSet_i", 0)              // isOnPlot == -1 if it is NOT found in the list
508                                if(isOnPlot == -1)
509                                        DisplayLowSet()
510                                Endif
511                                break
512                        case "Plot_2":         
513                                //medium-q
514                                isOnPlot = strsearch(list, "MedQSet_i", 0)              // isOnPlot == -1 if it is NOT found in the list
515                                if(isOnPlot == -1)
516                                        DisplayMedSet()
517                                Endif
518                                break
519                        case "Plot_3":         
520                                //high-q
521                                isOnPlot = strsearch(list, "HighQSet_i", 0)             // isOnPlot == -1 if it is NOT found in the list
522                                if(isOnPlot == -1)
523                                        DisplayHighSet()
524                                Endif
525                                break
526                        case "Plot_4":         
527                                //highest-q
528                                isOnPlot = strsearch(list, "HighestQSet_i", 0)          // isOnPlot == -1 if it is NOT found in the list
529                                if(isOnPlot == -1)
530                                        DisplayHighestSet()
531                                Endif
532                                break
533                endswitch       
534       
535        Endif
536       
537       
538        //the stripPoints variable boxes should also update the graph, if necessary
539        return(0)
540End
541
542//adds both highest-q sets (full and trimmed) to the graph, which is
543//assumed to exist along with the high-q waves
544//
545Function DisplayHighestSet()
546        //function assumes that the window "NSORT_Graph" already exists
547//      DoWindow/F NSORT_Graph
548
549        SetDataFolder root:HighestQSet:
550        AppendToGraph/W=NSORT_Graph $"HighestQSet_i" vs $"HighestQSet_q"
551        ModifyGraph/W=NSORT_Graph log=1,mode=3,marker($"HighestQSet_i")=8,msize=2,rgb($"HighestQSet_i")=(65535,32896,0),opaque($"HighestQSet_i")=1
552        ErrorBars/W=NSORT_Graph/T=0 $"HighestQSet_i" Y,wave=($"HighestQSet_s",$"HighestQSet_s")
553        AppendToGraph/W=NSORT_Graph $"TrimHighestQSet_i" vs $"TrimHighestQSet_q"
554        ModifyGraph/W=NSORT_Graph mode($"TrimHighestQSet_i")=3,marker($"TrimHighestQSet_i")=19,msize=2,rgb($"TrimHighestQSet_i")=(65535,32896,0)
555        SetDataFolder root:
556End
557
558//adds both high-q sets (full and trimmed) to the graph, which is
559//assumed to exist along with the high-q waves
560//
561Function DisplayHighSet()
562        //function assumes that the window "NSORT_Graph" already exists
563//      DoWindow/F NSORT_Graph
564
565        SetDataFolder root:HighQSet:
566        AppendToGraph/W=NSORT_Graph $"HighQSet_i" vs $"HighQSet_q"
567        ModifyGraph/W=NSORT_Graph log=1,mode=3,marker($"HighQSet_i")=8,msize=2,rgb($"HighQSet_i")=(0,0,65535),opaque($"HighQSet_i")=1
568        ErrorBars/W=NSORT_Graph/T=0 $"HighQSet_i" Y,wave=($"HighQSet_s",$"HighQSet_s")
569        AppendToGraph/W=NSORT_Graph $"TrimHighQSet_i" vs $"TrimHighQSet_q"
570        ModifyGraph/W=NSORT_Graph mode($"TrimHighQSet_i")=3,marker($"TrimHighQSet_i")=19,msize=2,rgb($"TrimHighQSet_i")=(0,0,65535)
571        SetDataFolder root:
572End
573
574//adds both med-q sets (full and trimmed) to the graph, which is
575//assumed to exist along with the med-q waves
576//
577Function DisplayMedSet()
578        //function assumes that the window "NSORT_Graph" already exists
579//      DoWindow/F NSORT_Graph
580       
581        SetDataFolder root:MedQSet:
582        AppendToGraph/W=NSORT_Graph $"MedQSet_i" vs $"MedQSet_q"
583        ModifyGraph/W=NSORT_Graph log=1,mode=3,marker($"MedQSet_i")=8,msize=2,rgb($"MedQSet_i")=(65535,0,0),opaque($"MedQSet_i")=1
584        ErrorBars/W=NSORT_Graph/T=0 $"MedQSet_i" Y,wave=($"MedQSet_s",$"MedQSet_s")
585        AppendToGraph/W=NSORT_Graph $"TrimMedQSet_i" vs $"TrimMedQSet_q"
586        ModifyGraph/W=NSORT_Graph mode($"TrimMedQSet_i")=3,marker($"TrimMedQSet_i")=19,msize=2,rgb($"TrimMedQSet_i")=(65535,0,0)
587        SetDataFolder root:
588End
589
590//adds both low-q sets (full and trimmed) to the graph, which is
591//assumed to exist along with the low-q waves
592//
593Function DisplayLowSet()
594        //function assumes that the window "NSORT_Graph" already exists
595//      DoWindow/F NSORT_Graph
596
597        SetDataFolder root:LowQSet:
598        AppendToGraph/W=NSORT_Graph $"LowQSet_i" vs $"LowQSet_q"
599        ModifyGraph/W=NSORT_Graph log=1,mode=3,marker($"LowQSet_i")=8,msize=2,rgb($"LowQSet_i")=(2,39321,1),opaque($"LowQSet_i")=1
600        ErrorBars/W=NSORT_Graph/T=0 $"LowQSet_i" Y,wave=($"LowQSet_s",$"LowQSet_s")
601        AppendToGraph/W=NSORT_Graph $"TrimLowQSet_i" vs $"TrimLowQSet_q"
602        ModifyGraph/W=NSORT_Graph mode($"TrimLowQSet_i")=3,marker($"TrimLowQSet_i")=19,msize=2,rgb($"TrimLowQSet_i")=(2,39321,1)
603        ModifyGraph tickUnit(left)=1
604        SetDataFolder root:
605End
606
607//button action procedure to set both the main global of the catPath string
608//and also the duplicate global string used in the NSORT folder
609//after path selected, the popup menus are updated
610//
611Function NSORTPickPathButton(ctrlName) : ButtonControl
612        String ctrlName
613
614        Variable err = PickPath()               //sets global path value
615        SVAR pathStr = root:myGlobals:gCatPathStr
616       
617        //set the global string for NSORT to the selected pathname
618        String/G root:myGlobals:NSORT:gPathStr = pathStr
619       
620        //call each of the popup menu proc's to re-set the menu choices
621        //setting the checkboxes to force update
622//      CheckBox check_0,win=NSORT_Panel,value=1
623//      CheckBox check_1,win=NSORT_Panel,value=1
624//      CheckBox check_2,win=NSORT_Panel,value=1
625        LowQPopMenuProc("popup_1",1,"")
626        MedQPopMenuProc("popup_2",1,"")
627        HighQPopMenuProc("popup_3",1,"")
628        HighestQPopMenuProc("popup_4",1,"")
629       
630End
631
632
633//action procedure associated with the setvar box
634//when a value is entered, the global value is set, and the corresponding dataset
635//is updated on the plot, showing the new result of removing this number of points
636//
637//      SetVar boxes are named beg_N and end_N (so 4th element is the number)
638//
639// 1 == LowQ
640// 2 == MedQ
641// 3 == HighQ
642// 4 == HighestQ
643//
644//"Plot_1" is the low-q button name
645//"Plot_2" is the med-q button name
646//"Plot_3" is the high-q button name
647//"Plot_4" is the high-q button name
648//
649//calling plot_0_Button() responds as if that named button were pressed
650// and gets the proper number to trim directly from the SetVar
651//
652Function SetBegOrEnd(ctrlName,varNum,varStr,varName) : SetVariableControl
653        String ctrlName
654        Variable varNum
655        String varStr
656        String varName
657       
658//  global is automatically updated as the value is entered
659        String numStr= num2Str( str2num(ctrlName[4]) )
660        Plot_0_Button("Plot_"+numStr)
661        DoWindow/F NSORT_Panel
662End
663
664//this will---
665//re-load the data sets (since they may not have been loaded if they were not plotted)
666// apply the scaling to the datasets (so that they will show up in the graph)
667//and actually write the file
668//
669// then "pop" the  lists to get the new file lists with the new name in the list
670//
671Function WriteNSORTFileButton(ctrlName) : ButtonControl
672        String ctrlName
673               
674        // Put here the dialog that says if ANY of the datasets had 3-column data, the results will all have only three columns
675        // Set the number of output columns
676        Variable isAThree = 0, isASix = 0,err
677        String fileStr="",tempName
678
679        NVAR Columns1 = root:myGlobals:NSORT:gColumns1
680        NVAR Columns2 = root:myGlobals:NSORT:gColumns2
681        NVAR Columns3 = root:myGlobals:NSORT:gColumns3
682        NVAR Columns4 = root:myGlobals:NSORT:gColumns4
683        if( (Columns1 == 3) || (Columns2 == 3) || (Columns3 == 3) || (Columns4 == 3))
684                isAThree = 1
685        endif
686        if( (Columns1 == 6) || (Columns2 == 6) || (Columns3 == 6) || (Columns4 == 6))
687                isASix = 1
688        endif
689        if( (isAThree == 1) && (isASix == 1))
690                DoAlert 0, "These files contained a mixture of 3-column and 6-column data.  Only 3 columns were output."
691        endif
692       
693        //is there just one data set? if so, then dispatch to a simpler routine, since no normalization is needed
694        ControlInfo/W=NSORT_Panel popup_2               //if MedQSet is "none", then so is HighQSet and HighestQSet
695        fileStr = S_Value
696        if(cmpstr(fileStr,"none") == 0)
697                // just like in the rescaling routines, always RELOAD the data  !!!
698                //load file1
699                ControlInfo/W=NSORT_Panel popup_1
700                fileStr = S_Value
701                //get a valid file based on this partialName and catPathName
702                tempName = FindValidFilename(fileStr)
703               
704                //prepend path to tempName for read routine
705                PathInfo catPathName
706                tempName = S_path + tempName
707                err = LoadDataForNSORT(tempName,1)
708                //////end load file1
709       
710                //send just the trimmed (LowQ) set to be written out
711                WAVE lowq = $"root:LowQSet:TrimLowQSet_q"
712                WAVE lowi = $"root:LowQSet:TrimLowQSet_i"
713                WAVE lows = $"root:LowQSet:TrimLowQSet_s"
714//              WAVE/Z lowsq = $"root:LowQSet:TrimLowQSet_sq"           //these may not exist
715//              WAVE/Z lowqb = $"root:LowQSet:TrimLowQSet_qb"
716//              WAVE/Z lowfs = $"root:LowQSet:TrimLowQSet_fs"
717                WAVE/Z lowres = $"root:LowQSet:TrimLowQSet_res"
718                NVAR scaleFactor= root:myGlobals:NSORT:gScale1_2
719               
720                //
721                lowi *= scaleFactor
722                lows *= scaleFactor
723               
724                ControlInfo/W=NSORT_Panel PreviewCheck
725                if( V_Value==1 )                //if ==1, just preview and exit
726                        return(0)
727                endif
728                       
729                ControlInfo/W=NSORT_Panel popup_1
730                if(isAThree)
731                        WriteNSORTedFile(lowq,lowi,lows,S_Value,"none","none","none",S_Value,scaleFactor,1,1)
732                else
733                        WriteNSORTedFile(lowq,lowi,lows,S_Value,"none","none","none",S_Value,scaleFactor,1,1,res=lowres)
734                endif
735                //  just get the new list and return - don't actually "pop" the menu, or the selected item will change
736                SVAR popList = root:myGlobals:NSORT:gDataPopList
737                SVAR popList_3 = root:myGlobals:NSORT:gDataPopList_3
738                popList  = ReducedDataFileList("")
739                popList_3 = "none;" +  ReducedDataFileList("")
740                return(0)
741        endif
742               
743               
744        //two or more datasets, combine them
745        //have they been manually or auto-normalized?
746        ControlInfo/W=NSORT_Panel AutoCheck
747        Variable checked = V_Value
748       
749        //do the normalization and update the global scale factors displayed in the Panel
750        err = DoAutoScaleFromPanel(checked)                     // DoAutoScaleFromPanel writes out the datafile
751
752        //  just get the new list - don't actually "pop" the menu, or the selected item will change
753        SVAR popList = root:myGlobals:NSORT:gDataPopList
754        SVAR popList_3 = root:myGlobals:NSORT:gDataPopList_3
755        popList  = ReducedDataFileList("")
756        popList_3 = "none;" +  ReducedDataFileList("")
757       
758        return(0)
759End
760
761//window recreation macro for NSORT Panel
762//
763Window NSORT_Panel()
764        PauseUpdate; Silent 1           // building window...
765        NewPanel /W=(569,69,944,584)//K=2
766        ModifyPanel cbRGB=(49151,53155,65535)
767        ModifyPanel fixedSize=1
768        SetDrawLayer UserBack
769        SetDrawEnv fstyle= 5
770        DrawText 35,20,"NSORT - Rescale and combine 1-D files"
771        DrawLine 0,55,346,55
772        DrawLine 0,128,346,128
773        DrawLine 0,214,346,214
774        DrawLine 0,295,346,295
775        DrawLine 0,372,347,372
776        DrawLine 0,460,346,460
777       
778        SetDrawEnv fstyle= 5
779        DrawText 5,74,"Low Q:"
780        SetDrawEnv fstyle= 5
781        DrawText 5,148,"Medium Q:"
782        SetDrawEnv fstyle= 5
783        DrawText 8,234,"High Q: (or none)"
784        SetDrawEnv fstyle= 5
785        DrawText 8,314,"Highest Q: (or none)"
786        SetDrawEnv fstyle= 4
787        DrawText 178,75,"Delete Points?"
788        SetDrawEnv fstyle= 4
789        DrawText 178,146,"Delete Points?"
790        SetDrawEnv fstyle= 4
791        DrawText 184,236,"Delete Points?"
792        SetDrawEnv fstyle= 4
793        DrawText 184,316,"Delete Points?"
794        DrawText 31,456,"To Manually scale data, enter scale factors above"
795       
796        Button NSORT_Done,pos={274,483},size={50,20},proc=NSORT_DoneButton,title="Done"
797        Button NSORT_Done,help={"closes the panel"}
798        Button Plot_1,pos={279,63},size={50,20},proc=Plot_0_Button,title="Plot"
799        Button Plot_1,help={"Plots the dataset from the popup, showing the full set as open circles and the trimmed set as solid circles"}
800        Button Plot_2,pos={283,144},size={50,20},proc=Plot_0_Button,title="Plot"
801        Button Plot_2,help={"Plots the dataset from the popup, showing the full set as open circles and the trimmed set as solid circles"}
802        Button Plot_3,pos={284,223},size={50,20},proc=Plot_0_Button,title="Plot"
803        Button Plot_3,help={"Plots the dataset from the popup, showing the full set as open circles and the trimmed set as solid circles"}
804        Button Plot_4,pos={284.00,303.00},size={50.00,20.00},proc=Plot_0_Button,title="Plot"
805        Button Plot_4,help={"Plots the dataset from the popup, showing the full set as open circles and the trimmed set as solid circles"}
806
807        Button PathButton,pos={6,26},size={80,20},proc=NSORTPickPathButton,title="Pick Path"
808        Button PathButton,help={"Select the local path to the folder containing your SANS data"}
809        Button helpButton,pos={340,26},size={25,20},proc=ShowNSORTHelp,title="?"
810        Button helpButton,help={"Show the help file for sorting and internormalizing 1-D data sets"}
811        SetVariable setPath,pos={95,29},size={240,14},title="Path:",fSize=10
812        SetVariable setPath,limits={0,0,0},value= root:myGlobals:NSORT:gPathStr
813        SetVariable setPath,help={"The current path to the local folder with SANS data"}
814        SetVariable end_1,pos={182,101},size={80,14},proc=SetBegOrEnd,title="End Pts"
815        SetVariable end_1,fSize=10,help={"How many points to remove from the high-q end of this dataset"}
816        SetVariable end_1,limits={-Inf,Inf,0},value= root:myGlobals:NSORT:gPtsEnd1
817        SetVariable end_2,pos={182,176},size={80,14},proc=SetBegOrEnd,title="End Pts"
818        SetVariable end_2,fSize=10,help={"How many points to remove from the high-q end of this dataset"}
819        SetVariable end_2,limits={-Inf,Inf,0},value= root:myGlobals:NSORT:gPtsEnd2
820        SetVariable end_3,pos={182,269},size={80,14},proc=SetBegOrEnd,title="End Pts"
821        SetVariable end_3,fSize=10,help={"How many points to remove from the high-q end of this dataset"}
822        SetVariable end_3,limits={-Inf,Inf,0},value= root:myGlobals:NSORT:gPtsEnd3
823        SetVariable end_4,pos={182.00,349.00},size={80.00,16.00},proc=SetBegOrEnd,title="End Pts"
824        SetVariable end_4,help={"How many points to remove from the high-q end of this dataset"}
825        SetVariable end_4,fSize=10
826        SetVariable end_4,limits={-inf,inf,0},value= root:myGlobals:NSORT:gPtsEnd4
827        SetVariable beg_1,pos={182,79},size={80,14},proc=SetBegOrEnd,title="Beg Pts"
828        SetVariable beg_1,fSize=10,help={"How many points to remove from the low-q end of this dataset"}
829        SetVariable beg_1,limits={-Inf,Inf,0},value= root:myGlobals:NSORT:gPtsBeg1
830        SetVariable beg_2,pos={182,155},size={80,14},proc=SetBegOrEnd,title="Beg Pts"
831        SetVariable beg_2,fSize=10,help={"How many points to remove from the low-q end of this dataset"}
832        SetVariable beg_2,limits={-Inf,Inf,0},value= root:myGlobals:NSORT:gPtsBeg2
833        SetVariable beg_3,pos={182,246},size={80,14},proc=SetBegOrEnd,title="Beg Pts"
834        SetVariable beg_3,fSize=10,help={"How many points to remove from the low-q end of this dataset"}
835        SetVariable beg_3,limits={-Inf,Inf,0},value= root:myGlobals:NSORT:gPtsBeg3
836        SetVariable beg_4,pos={182.00,326.00},size={80.00,16.00},proc=SetBegOrEnd,title="Beg Pts"
837        SetVariable beg_4,help={"How many points to remove from the low-q end of this dataset"}
838        SetVariable beg_4,fSize=10
839        SetVariable beg_4,limits={-inf,inf,0},value= root:myGlobals:NSORT:gPtsBeg4
840        Button DoCombine,pos={13,483},size={160,20},proc=WriteNSORTFileButton,title="Write Combined File"
841        Button DoCombine,help={"Combine and normalize the selected files as specifed"}
842        SetVariable scale_12,pos={159,381},size={160,14},proc=SetScale_12,title="Mult factor 1-2"
843        SetVariable scale_12,fSize=10,help={"Factor that will multiply medium-q set to scale to low-q set"}
844        SetVariable scale_12,limits={-Inf,Inf,0},value= root:myGlobals:NSORT:gScale1_2
845        SetVariable scale_23,pos={159,401},size={160,14},proc=SetScale_23,title="Mult factor 2-3"
846        SetVariable scale_23,fSize=10,help={"Factor that will multiply high-q set to scale to medium-q set"}
847        SetVariable scale_23,limits={-Inf,Inf,0},value= root:myGlobals:NSORT:gScale2_3
848        SetVariable scale_34,pos={159.00,421.00},size={160.00,16.00},proc=SetScale_34,title="Mult factor 3-4"
849        SetVariable scale_34,help={"Factor that will multiply highest-q set to scale to high-q set"}
850        SetVariable scale_34,fSize=10
851        SetVariable scale_34,limits={-inf,inf,0},value= root:myGlobals:NSORT:gScale3_4
852        CheckBox check1,pos={5,105},size={160,20},proc=CheckProc,title="Normalize to this file",value=1
853        CheckBox check1,help={"If checked, the combined dataset will be normalized to this dataset"}
854        CheckBox check2,pos={5,185},size={160,20},proc=CheckProc,title="Normalize to this file",value=0
855        CheckBox check2,help={"If checked, the combined dataset will be normalized to this dataset"}
856        CheckBox check3,pos={4,270},size={160,20},proc=CheckProc,title="Normalize to this file",value=0
857        CheckBox check3,help={"If checked, the combined dataset will be normalized to this dataset"}
858        CheckBox check4,pos={4.00,350.00},size={107.00,16.00},proc=CheckProc,title="Normalize to this file"
859        CheckBox check4,help={"If checked, the combined dataset will be normalized to this dataset"}
860        CheckBox check4,value= 0
861        PopupMenu popup_1,pos={6,77},size={99,19},proc=LowQPopMenuProc
862        PopupMenu popup_1,mode=1,value= #"root:myGlobals:NSORT:gDataPopList"
863        PopupMenu popup_1,help={"Choose the dataset with the lowest overall q-value (longest detector distance)"}
864        PopupMenu popup_2,pos={6,153},size={99,19},proc=MedQPopMenuProc
865        PopupMenu popup_2,mode=1,value= #"root:myGlobals:NSORT:gDataPopList_3"
866        PopupMenu popup_2,help={"Choose the dataset with the intermediate q-values (\"medium\" detector distance)"}
867        PopupMenu popup_3,pos={6,239},size={99,19},proc=HighQPopMenuProc
868        PopupMenu popup_3,mode=1,value= #"root:myGlobals:NSORT:gDataPopList_3"
869        PopupMenu popup_3,help={"Choose the dataset with the highest overall q-value (shortest detector distance), or NONE if no third set desired"}
870        PopupMenu popup_4,pos={6.00,319.00},size={58.00,23.00},proc=HighestQPopMenuProc
871        PopupMenu popup_4,help={"Choose the dataset with the highest overall q-value (shortest detector distance), or NONE if no fourth set desired"}
872        PopupMenu popup_4,mode=1,popvalue="none",value= #"root:myGlobals:NSORT:gDataPopList_3"
873        CheckBox AutoCheck,pos={14,386},size={100,20},title="Auto Scale",value=0
874        CheckBox AutoCheck,help={"If checked, the scale factor will be automatically determined, if not checked, the current values in the fields will be used"}
875        CheckBox PreviewCheck,pos={15,465},size={74,14},title="Preview Only",value= 0
876        CheckBox WarningCheck,pos={111,465},size={93,14},title="Overlap warning?",value= 1
877EndMacro
878
879//sets the scale factor (multiplicative) between sets 1 and 2
880//re-sets the global variable
881//
882Function SetScale_12(ctrlName,varNum,varStr,varName) : SetVariableControl
883        String ctrlName
884        Variable varNum
885        String varStr
886        String varName
887
888        Variable/G root:myGlobals:NSORT:gScale1_2 = varNum
889       
890End
891
892//sets the scale factor (multiplicative) between sets 2 and 3
893//re-sets the global variable
894//
895Function SetScale_23(ctrlName,varNum,varStr,varName) : SetVariableControl
896        String ctrlName
897        Variable varNum
898        String varStr
899        String varName
900
901        Variable/G root:myGlobals:NSORT:gScale2_3 = varNum
902End
903
904//sets the scale factor (multiplicative) between sets 3 and 4
905//re-sets the global variable
906//
907Function SetScale_34(ctrlName,varNum,varStr,varName) : SetVariableControl
908        String ctrlName
909        Variable varNum
910        String varStr
911        String varName
912
913        Variable/G root:myGlobals:NSORT:gScale3_4 = varNum
914End
915
916//control procedures for the checkboxes to specify which file is to be
917//held fixed (so all other files are normalized to the checked file
918//the three checkboxes behave as "radio buttons" - only one can be checked
919//
920Function CheckProc(ctrlName,checked) : CheckBoxControl
921        String ctrlName
922        Variable checked
923       
924        //controls the three checkboxes to act as "radio buttons" to have only one file to
925        //normalize to.
926        //all three boxes should call this routine
927       
928        //do the "radio button control"
929        do
930                if(cmpstr(ctrlName,"check2") == 0)
931                        CheckBox check1 value=0
932                        CheckBox check2 value=1
933                        CheckBox check3 value=0
934                        CheckBox check4 value=0
935                        Variable/G root:myGlobals:NSORT:gNormToNum = 2
936                        break
937                Endif
938                if(cmpstr(ctrlName,"check3") == 0)
939                        CheckBox check1 value=0
940                        CheckBox check2 value=0
941                        CheckBox check3 value=1
942                        CheckBox check4 value=0
943                        Variable/G root:myGlobals:NSORT:gNormToNum = 3
944                        break
945                Endif
946                if(cmpstr(ctrlName,"check4") == 0)
947                        CheckBox check1 value=0
948                        CheckBox check2 value=0
949                        CheckBox check3 value=0
950                        CheckBox check4 value=1
951                        Variable/G root:myGlobals:NSORT:gNormToNum = 4
952                        break
953                Endif
954                //default case is normalize to file1
955                CheckBox check1 value=1
956                CheckBox check2 value=0
957                CheckBox check3 value=0
958                        CheckBox check4 value=0
959                Variable/G root:myGlobals:NSORT:gNormToNum = 1
960        While(0)
961       
962        ControlUpdate/A/W=NSORT_Panel
963        DoUpdate
964               
965End
966
967//when menu is popped, it gets a valid list to display and updates the control
968//
969// 2002- always refreshes, as new (fast) filter is used
970Function LowQPopMenuProc(ctrlName,popNum,popStr) : PopupMenuControl
971        String ctrlName
972        Variable popNum
973        String popStr
974
975        String/G root:myGlobals:NSORT:gDataPopList = ReducedDataFileList("")
976        ControlUpdate popup_1
977
978        return(0)
979End
980
981//when menu is popped, it gets a valid list to display and updates the control
982//
983Function MedQPopMenuProc(ctrlName,popNum,popStr) : PopupMenuControl
984        String ctrlName
985        Variable popNum
986        String popStr
987               
988        String/G root:myGlobals:NSORT:gDataPopList_3 = "none;" +  ReducedDataFileList("")
989        ControlUpdate popup_2
990        if(cmpstr(popStr,"none")==0)
991                PopupMenu popup_3,mode=1        //force "none" (item #1) to be the selection
992                CheckBox AutoCheck,value=0      //un-check the auto-scale checkbox
993                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
994                RemoveFromGraph/Z MedQSet_i,TrimMedQSet_i,HighQSet_i,TrimHighQSet_i             //remove the data from the graph
995        Endif   
996        return(0)
997End
998
999//when menu is popped, it gets a valid list to display and updates the control
1000// - will be different, since set 3 can also be "none" if only 2 sets
1001//are to be NSORTed
1002//
1003Function HighQPopMenuProc(ctrlName,popNum,popStr) : PopupMenuControl
1004        String ctrlName
1005        Variable popNum
1006        String popStr
1007
1008        //add the option "none" to the file list (which should already end with a semicolon)
1009        String/G root:myGlobals:NSORT:gDataPopList_3 = "none;" +  ReducedDataFileList("")
1010
1011        ControlUpdate/W=NSORT_Panel popup_3
1012        if(cmpstr(popStr,"none")==0)
1013                RemoveFromGraph/Z HighQSet_i,TrimHighQSet_i             //remove the data from the graph
1014        Endif   
1015        ControlInfo/W=NSORT_Panel popup_2
1016        if(cmpstr(S_Value,"none")==0)
1017                PopupMenu popup_3,win=NSORT_Panel,mode=1        //force "none" (item #1) to be the selection if medium is none
1018        endif
1019        return(0)       
1020End
1021
1022
1023//when menu is popped, it gets a valid list to display and updates the control
1024// - will be different, since set 3 can also be "none" if only 2 sets
1025//are to be NSORTed
1026//
1027Function HighestQPopMenuProc(ctrlName,popNum,popStr) : PopupMenuControl
1028        String ctrlName
1029        Variable popNum
1030        String popStr
1031
1032        //add the option "none" to the file list (which should already end with a semicolon)
1033        String/G root:myGlobals:NSORT:gDataPopList_3 = "none;" +  ReducedDataFileList("")
1034
1035        ControlUpdate/W=NSORT_Panel popup_4
1036        if(cmpstr(popStr,"none")==0)
1037                RemoveFromGraph/Z HighestQSet_i,TrimHighestQSet_i               //remove the data from the graph
1038        Endif   
1039        ControlInfo/W=NSORT_Panel popup_2
1040        if(cmpstr(S_Value,"none")==0)
1041                PopupMenu popup_3,win=NSORT_Panel,mode=1        //force "none" (item #1) to be the selection if medium is none
1042                PopupMenu popup_4,win=NSORT_Panel,mode=1        //force "none" (item #1) to be the selection if medium is none
1043        endif
1044        ControlInfo/W=NSORT_Panel popup_3
1045        if(cmpstr(S_Value,"none")==0)
1046                PopupMenu popup_4,win=NSORT_Panel,mode=1        //force "none" (item #1) to be the selection if medium is none
1047        endif
1048        return(0)       
1049End
1050
1051//be sure to use the "Trim.." datasets that have had the bad points removed
1052//and then do the scaling based on the choices in the panel
1053//input (auto) is a switch
1054//
1055Function DoAutoScaleFromPanel(auto)
1056        Variable auto           //if auto == 1, do the scaling, if 0, use manual scale values
1057
1058        NVAR normTo = root:myGlobals:NSORT:gNormToNum
1059        Variable err=0,setNum,norm12,norm23,norm34
1060        String fileStr="",tempName="",name1="",name2="",name3="",normToStr="",name4=""
1061       
1062//Set the number of output columns
1063        Variable numOutputColumns = 0
1064
1065        NVAR Columns1 = root:myGlobals:NSORT:gColumns1
1066        NVAR Columns2 = root:myGlobals:NSORT:gColumns2
1067        NVAR Columns3 = root:myGlobals:NSORT:gColumns3
1068        NVAR Columns4 = root:myGlobals:NSORT:gColumns4
1069        if( (Columns1 == 3) || (Columns2 == 3) || (Columns3 == 3) || (Columns4 == 3) )
1070                numOutputColumns = 3
1071        else
1072                if( (Columns1 == 6) && (Columns2 == 6) && ((Columns3 == 0) || (Columns3 == 6)) || (Columns4 == 6) )
1073                        numOutputColumns = 6
1074                endif
1075        endif
1076
1077        //rescale 1-2
1078       
1079        //load file1
1080        ControlInfo/W=NSORT_Panel popup_1
1081        fileStr = S_Value
1082        name1 = fileStr
1083        setNum = 1
1084        //get a valid file based on this partialName and catPathName
1085        tempName = FindValidFilename(fileStr)
1086       
1087        //prepend path to tempName for read routine
1088        PathInfo catPathName
1089        tempName = S_path + tempName
1090        err = LoadDataForNSORT(tempName,setNum)
1091        //////end load file1
1092       
1093        //load file2
1094        ControlInfo/W=NSORT_Panel popup_2
1095        fileStr = S_Value
1096        name2 = fileStr
1097        setNum = 2
1098        //get a valid file based on this partialName and catPathName
1099        tempName = FindValidFilename(fileStr)
1100       
1101        //prepend path to tempName for read routine
1102        PathInfo catPathName
1103        tempName = S_path + tempName
1104        err = LoadDataForNSORT(tempName,setNum)
1105        //////end load file2
1106       
1107        //load file3 , if necessary
1108        ControlInfo/W=NSORT_Panel popup_3
1109        fileStr = S_Value
1110        name3 = fileStr
1111        setNum = 3
1112        if(cmpstr(fileStr,"none") == 0)
1113                //do nothing
1114        else
1115                //get a valid file based on this partialName and catPathName
1116                tempName = FindValidFilename(fileStr)
1117       
1118                //prepend path to tempName for read routine
1119                PathInfo catPathName
1120                tempName = S_path + tempName
1121                err = LoadDataForNSORT(tempName,setNum)
1122        Endif
1123        //////end load file3
1124       
1125        //load file4 , if necessary
1126        ControlInfo/W=NSORT_Panel popup_4
1127        fileStr = S_Value
1128        name4 = fileStr
1129        setNum = 4
1130        if(cmpstr(fileStr,"none") == 0)
1131                //do nothing
1132        else
1133                //get a valid file based on this partialName and catPathName
1134                tempName = FindValidFilename(fileStr)
1135       
1136                //prepend path to tempName for read routine
1137                PathInfo catPathName
1138                tempName = S_path + tempName
1139                err = LoadDataForNSORT(tempName,setNum)
1140        Endif
1141        //////end load file4
1142       
1143       
1144        //assign filename of file to normalize to
1145        switch(normTo)
1146                case 1:
1147                        normToStr = name1
1148                        break
1149                case 2:
1150                        normToStr = name2
1151                        break
1152                case 3:
1153                        normToStr = name3
1154                        break
1155                case 4:
1156                        normToStr = name4
1157                        break
1158        endswitch
1159
1160
1161        Variable n1,n2,n12,num2
1162        Variable n3,n123
1163        Variable n4,n1234
1164       
1165   if(numOutputColumns == 3) //Start the 3-column specific stuff here.
1166                //order points in sets 1-2, indexing overlap region
1167                //put result in temporary waves
1168                WaveStats/Q $"root:LowQSet:TrimLowQSet_q"
1169                n1 = V_npnts
1170                WaveStats/Q $"root:LowQSet:TrimMedQSet_q"
1171                n2 = V_npnts
1172                n12 = n1+ n2
1173               
1174                Make/O/N=(n12) q12,i12,sig12
1175                WAVE lowq = $"root:LowQSet:TrimLowQSet_q"
1176                WAVE medq = $"root:MedQSet:TrimMedQSet_q"
1177                WAVE lowi = $"root:LowQSet:TrimLowQSet_i"
1178                WAVE medi =  $"root:MedQSet:TrimMedQSet_i"
1179                WAVE lows = $"root:LowQSet:TrimLowQSet_s"
1180                WAVE meds = $"root:MedQSet:TrimMedQSet_s"
1181                q12[0,n1-1] = lowq[p]
1182                q12[n1,n1+n2-1]= medq[p-n1]
1183                i12[0,n1-1] = lowi[p]
1184                i12[n1,n1+n2-1]= medi[p-n1]
1185                sig12[0,n1-1] = lows[p]
1186                sig12[n1,n1+n2-1]= meds[p-n1]
1187               
1188                Sort q12, q12,i12,sig12
1189                /////////////////
1190               
1191                //find the maximum point number of set 2  in the overlap region
1192                FindLevel/P/Q medq,(lowq[n1-1])
1193                num2 = trunc(V_levelX)
1194                //Print "num2 = ",num2
1195               
1196                if (auto)
1197                        //there must be overlap points to use auto-scaling
1198                        if(numtype(num2) != 0)
1199                                Abort "There are no data points in the overlap region. Either reduce the number of deleted points or use manual scaling."
1200                        endif
1201                        //do auto-scaling of data
1202                        norm12 = NCNR_GetScalingInOverlap(num2,lowq,lowi,medq,medi)
1203                        //Set the global variable for the 1-2 scale factor
1204                        Variable/G root:myGlobals:NSORT:gScale1_2 = norm12
1205                else
1206                        //use the value from the panel ( which is the global)
1207                        NVAR temp12 = root:myGlobals:NSORT:gScale1_2
1208                        norm12 = temp12
1209                Endif
1210               
1211                If(normTo== 2)
1212                        //normalize to second file, so multiply 1st by 1/norm
1213                        norm12 = 1/norm12
1214                        lowi *= norm12
1215                        lows *= norm12
1216                else
1217                        //normalize to first file, OR THIRD FILE so multiply 2nd by norm
1218                        medi *= norm12
1219                        meds *= norm12
1220                Endif
1221               
1222                //Print "NSORT-ed ",name1," + ", name2
1223                //Print "normalized to ",normTo
1224                //Print "multiplicative factor = ",norm12
1225               
1226               
1227                //Make the combined, scaled dataset by overwriting the old sets
1228                Make/O/N=(n12) q12,i12,sig12
1229                q12[0,n1-1] = lowq[p]
1230                q12[n1,n1+n2-1]= medq[p-n1]
1231                i12[0,n1-1] = lowi[p]
1232                i12[n1,n1+n2-1]= medi[p-n1]
1233                sig12[0,n1-1] = lows[p]
1234                sig12[n1,n1+n2-1]= meds[p-n1]
1235               
1236                Sort q12, q12,i12,sig12
1237                //at this point 1-2 are combined
1238               
1239                ControlUpdate/A/W=NSORT_Panel
1240                DoUpdate
1241               
1242                //do we need to continue, or write out the set here and stop?
1243                if(cmpstr(name3,"none") == 0)
1244                        //stop here
1245                        norm23 = 1              //norm23 was not used
1246                        Variable/G root:myGlobals:NSORT:gScale2_3 = 1
1247                        //If any of them have three columns write three column data
1248                       
1249                        ControlInfo/W=NSORT_Panel PreviewCheck
1250                        if( V_Value==0 )                //if zero skip the preview and write out the file
1251                                err=WriteNSORTedFile(q12,i12,sig12,name1,name2,name3,name4,normToStr,norm12,norm23,norm34)
1252                        endif
1253                        //cleanup waves before exiting
1254                        KillWaves/Z q12,i12,sig12
1255                        return err
1256                Endif
1257               
1258                //need to add the third file... which was already loaded at the top of the function
1259                /////
1260                //order points in sets 12-3, indexing overlap region
1261                //put result in temporary waves
1262                WaveStats/Q q12
1263                n12 = V_npnts
1264                WaveStats/Q $"root:HighQSet:TrimHighQSet_q"
1265                n3 = V_npnts
1266                n123 = n12+ n3
1267               
1268                Make/O/N=(n123) q123,i123,sig123
1269                WAVE highq = $"root:HighQSet:TrimHighQSet_q"
1270                WAVE highi = $"root:HighQSet:TrimHighQSet_i"
1271                WAVE highs = $"root:HighQSet:TrimHighQSet_s"
1272       
1273                q123[0,n12-1] = q12[p]
1274                q123[n1,n12+n3-1]= highq[p-n12]
1275                i123[0,n12-1] = i12[p]
1276                i123[n1,n12+n3-1]= highi[p-n12]
1277                sig123[0,n12-1] = sig12[p]
1278                sig123[n1,n12+n3-1]= highs[p-n12]
1279               
1280                Sort q123, q123,i123,sig123
1281                /////////////////
1282               
1283                //find the maximum point number of set 2  in the overlap region
1284                FindLevel/P/Q highq,(q12[n12-1])
1285                num2 = trunc(V_levelX)
1286                //Print "num2 = ",num2
1287               
1288                if (auto)
1289                        //there must be overlap points to use auto-scaling
1290                        if(numtype(num2) != 0)
1291                                Abort "There are no data points in the overlap region. Either reduce the number of deleted points or use manual scaling."
1292                        endif
1293                        //do auto-scaling of data
1294                        norm23 = NCNR_GetScalingInOverlap(num2,q12,i12,highq,highi)
1295                        //Set the global variable for the 12 - 3 scale factor
1296                        Variable/G root:myGlobals:NSORT:gScale2_3 = norm23
1297                else
1298                        //use the value from the panel ( which is the global)
1299                        NVAR temp23 = root:myGlobals:NSORT:gScale2_3
1300                        norm23 = temp23
1301                Endif
1302               
1303                If( (normTo== 1) || (normTo ==2) )
1304                        //normalize to first or second file, so multiply third by norm23
1305                        highi *= norm23
1306                        highs *= norm23
1307                else
1308                        //normalize to THIRD file, 1-2 by 1/norm23
1309                        norm23 = 1/norm23
1310                        i12 *= norm23
1311                        sig12 *= norm23
1312                        // for the display, scale the trimmed sets 1 and 2
1313                        lowi *= norm23
1314                        lows *= norm23
1315                        medi *= norm23
1316                        meds *= norm23
1317                Endif
1318               
1319                ControlUpdate/A/W=NSORT_Panel
1320                DoUpdate
1321
1322                //Print "NSORT-ed ",name1," + ", name2, " + ", name3
1323                //Print "normalized to ",normTo
1324                //Print "multiplicative factor 1-2 = ",norm12," multiplicative factor 12 - 3 = ",norm23
1325               
1326               
1327                Make/O/N=(n123) q123,i123,sig123
1328                q123[0,n12-1] = q12[p]
1329                q123[n12,n12+n3-1]= highq[p-n12]
1330                i123[0,n12-1] = i12[p]
1331                i123[n12,n12+n3-1]= highi[p-n12]
1332                sig123[0,n12-1] = sig12[p]
1333                sig123[n12,n12+n3-1]= highs[p-n12]
1334               
1335                Sort q123, q123,i123,sig123
1336                //at this point 12 - 3 are combined
1337                //write out the set here and stop
1338       
1339                ControlInfo/W=NSORT_Panel PreviewCheck
1340                if( V_Value==0 )                //if zero skip the preview and write out the file
1341                        err=WriteNSORTedFile(q123,i123,sig123,name1,name2,name3,name4,normToStr,norm12,norm23,norm34)
1342                endif
1343                //cleanup waves before exiting
1344                KillWaves/Z q12,i12,sig12,q123,i123,sig123
1345               
1346               
1347                //do we need to continue, or write out the set here and stop?
1348                if(cmpstr(name4,"none") == 0)
1349                        //stop here
1350                        norm34 = 1              //norm34 was not used
1351                        Variable/G root:myGlobals:NSORT:gScale3_4 = 1
1352                        //If any of them have three columns write three column data
1353                       
1354                        ControlInfo/W=NSORT_Panel PreviewCheck
1355                        if( V_Value==0 )                //if zero skip the preview and write out the file
1356                                err=WriteNSORTedFile(q123,i123,sig123,name1,name2,name3,name4,normToStr,norm12,norm23,norm34)
1357                        endif
1358                        //cleanup waves before exiting
1359                        KillWaves/Z q123,i123,sig123
1360                        return err
1361                Endif
1362               
1363                //need to add the fourth file... which was already loaded at the top of the function
1364                /////
1365                //order points in sets 123-4, indexing overlap region
1366                //put result in temporary waves
1367                WaveStats/Q q123
1368                n123 = V_npnts
1369                WaveStats/Q $"root:HighestQSet:TrimHighestQSet_q"
1370                n4 = V_npnts
1371                n1234 = n123 + n4
1372               
1373                Make/O/N=(n1234) q1234,i1234,sig1234
1374                WAVE highestq = $"root:HighestQSet:TrimHighestQSet_q"
1375                WAVE highesti = $"root:HighestQSet:TrimHighestQSet_i"
1376                WAVE highests = $"root:HighestQSet:TrimHighestQSet_s"
1377       
1378                q1234[0,n123-1] = q123[p]
1379                q1234[n12,n123+n4-1]= highestq[p-n123]
1380                i1234[0,n123-1] = i123[p]
1381                i1234[n12,n123+n4-1]= highesti[p-n123]
1382                sig1234[0,n123-1] = sig123[p]
1383                sig1234[n12,n123+n4-1]= highests[p-n123]
1384               
1385                Sort q1234, q1234,i1234,sig1234
1386                /////////////////
1387               
1388                //find the maximum point number of set 2  in the overlap region
1389                FindLevel/P/Q highestq,(q123[n123-1])
1390                num2 = trunc(V_levelX)
1391                //Print "num2 = ",num2
1392               
1393                if (auto)
1394                        //there must be overlap points to use auto-scaling
1395                        if(numtype(num2) != 0)
1396                                Abort "There are no data points in the overlap region. Either reduce the number of deleted points or use manual scaling."
1397                        endif
1398                        //do auto-scaling of data
1399                        norm34 = NCNR_GetScalingInOverlap(num2,q123,i123,highestq,highesti)
1400                        //Set the global variable for the 123 - 4 scale factor
1401                        Variable/G root:myGlobals:NSORT:gScale3_4 = norm34
1402                else
1403                        //use the value from the panel ( which is the global)
1404                        NVAR temp34 = root:myGlobals:NSORT:gScale3_4
1405                        norm34 = temp34
1406                Endif
1407               
1408// normalization
1409                switch (normTo)
1410                        case 1:
1411                        case 2:
1412                        case 3:
1413                                //normalize to first or second or third file, so multiply fourth by norm34
1414                                highesti *= norm34
1415                                highests *= norm34
1416                                break
1417                        case 4:
1418                                //normalize to FOURTH file, 123 by multiplicative factor 1/norm34
1419                                norm34 = 1/norm34
1420                                i123 *= norm34
1421                                sig123 *= norm34
1422                                // for the display, scale the trimmed sets 1 and 2 and 3
1423                                lowi *= norm34
1424                                lows *= norm34
1425                                medi *= norm34
1426                                meds *= norm34
1427                                highi *= norm34
1428                                highs *= norm34
1429                                break
1430                endswitch
1431                               
1432                ControlUpdate/A/W=NSORT_Panel
1433                DoUpdate
1434
1435                //Print "NSORT-ed ",name1," + ", name2, " + ", name3
1436                //Print "normalized to ",normTo
1437                //Print "multiplicative factor 1-2 = ",norm12," multiplicative factor 12 - 3 = ",norm23
1438               
1439               
1440                Make/O/N=(n1234) q1234,i1234,sig1234
1441                q1234[0,n123-1] = q123[p]
1442                q1234[n123,n123+n4-1]= highestq[p-n123]
1443                i1234[0,n123-1] = i123[p]
1444                i1234[n123,n123+n4-1]= highesti[p-n123]
1445                sig1234[0,n123-1] = sig123[p]
1446                sig1234[n123,n123+n4-1]= highests[p-n123]
1447               
1448                Sort q123, q123,i123,sig123
1449                //at this point 12 - 3 are combined
1450                //write out the set here and stop
1451       
1452                ControlInfo/W=NSORT_Panel PreviewCheck
1453                if( V_Value==0 )                //if zero skip the preview and write out the file
1454                        err=WriteNSORTedFile(q123,i123,sig123,name1,name2,name3,name4,normToStr,norm12,norm23,norm34)
1455                endif
1456                //cleanup waves before exiting
1457                KillWaves/Z q12,i12,sig12,q123,i123,sig123
1458               
1459               
1460               
1461               
1462                //combined dataset will already be displayed if the NSORT_Graph is open
1463       
1464                ////////////////
1465                return err
1466   endif // End the 3-column specific stuff here
1467
1468   if(numOutputColumns == 6) // Start the 6-column specific stuff here
1469                //order points in sets 1-2, indexing overlap region
1470                //put result in temporary waves
1471                WaveStats/Q $"root:LowQSet:TrimLowQSet_q"
1472                n1 = V_npnts
1473                WaveStats/Q $"root:MedQSet:TrimMedQSet_q"
1474                n2 = V_npnts
1475                n12 = n1+ n2
1476               
1477                Make/O/N=(n12) q12,i12,sig12,sq12,qb12,fs12
1478                Make/O/N=(n12,3) res12
1479                WAVE lowq = $"root:LowQSet:TrimLowQSet_q"
1480                WAVE medq = $"root:MedQSet:TrimMedQSet_q"
1481                WAVE lowi = $"root:LowQSet:TrimLowQSet_i"
1482                WAVE medi =  $"root:MedQSet:TrimMedQSet_i"
1483                WAVE lows = $"root:LowQSet:TrimLowQSet_s"
1484                WAVE meds = $"root:MedQSet:TrimMedQSet_s"
1485                WAVE lowres = $"root:LowQSet:TrimLowQSet_res"
1486                WAVE medres = $"root:MedQSet:TrimMedQSet_res"
1487               
1488                q12[0,n1-1] = lowq[p]
1489                q12[n1,n1+n2-1]= medq[p-n1]
1490                i12[0,n1-1] = lowi[p]
1491                i12[n1,n1+n2-1]= medi[p-n1]
1492                sig12[0,n1-1] = lows[p]
1493                sig12[n1,n1+n2-1]= meds[p-n1]
1494                sq12[0,n1-1] = lowres[p][0]
1495                sq12[n1,n1+n2-1]= medres[p-n1][0]
1496                qb12[0,n1-1] = lowres[p][1]
1497                qb12[n1,n1+n2-1]= medres[p-n1][1]
1498                fs12[0,n1-1] = lowres[p][2]
1499                fs12[n1,n1+n2-1]= medres[p-n1][2]
1500
1501               
1502                Sort q12, q12,i12,sig12,sq12,qb12,fs12
1503                /////////////////
1504               
1505                //find the maximum point number of set 2  in the overlap region
1506                FindLevel/P/Q medq,(lowq[n1-1])
1507                num2 = trunc(V_levelX)
1508                //Print "num2 = ",num2
1509               
1510                if (auto)
1511                        //there must be overlap points to use auto-scaling
1512                        if(numtype(num2) != 0)
1513                                Abort "There are no data points in the overlap region. Either reduce the number of deleted points or use manual scaling."
1514                        endif
1515                        //do auto-scaling of data
1516                        norm12 = NCNR_GetScalingInOverlap(num2,lowq,lowi,medq,medi)
1517                        //Set the global variable for the 1-2 scale factor
1518                        Variable/G root:myGlobals:NSORT:gScale1_2 = norm12
1519                else
1520                        //use the value from the panel ( which is the global)
1521                        NVAR temp12 = root:myGlobals:NSORT:gScale1_2
1522                        norm12 = temp12
1523                Endif
1524               
1525                If(normTo== 2)
1526                        //normalize to second file, so multiply 1st by 1/norm
1527                        norm12 = 1/norm12
1528                        lowi *= norm12
1529                        lows *= norm12
1530                else
1531                        //normalize to first file, OR THIRD FILE so multiply 2nd by norm
1532                        medi *= norm12
1533                        meds *= norm12
1534                Endif
1535               
1536                //Print "NSORT-ed ",name1," + ", name2
1537                //Print "normalized to ",normTo
1538                //Print "multiplicative factor = ",norm12
1539                ControlUpdate/A/W=NSORT_Panel
1540                DoUpdate
1541
1542               
1543                //Make the combined, scaled dataset by overwriting the old sets
1544                Make/O/N=(n12) q12,i12,sig12,sq12,qb12,fs12
1545                Make/O/N=(n12,3) res12
1546                q12[0,n1-1] = lowq[p]
1547                q12[n1,n1+n2-1]= medq[p-n1]
1548                i12[0,n1-1] = lowi[p]
1549                i12[n1,n1+n2-1]= medi[p-n1]
1550                sig12[0,n1-1] = lows[p]
1551                sig12[n1,n1+n2-1]= meds[p-n1]
1552                sq12[0,n1-1] = lowres[p][0]
1553                sq12[n1,n1+n2-1]= medres[p-n1][0]
1554                qb12[0,n1-1] = lowres[p][1]
1555                qb12[n1,n1+n2-1]= medres[p-n1][1]
1556                fs12[0,n1-1] = lowres[p][2]
1557                fs12[n1,n1+n2-1]= medres[p-n1][2]
1558
1559               
1560                Sort q12, q12,i12,sig12,sq12,qb12,fs12
1561                //at this point 1-2 are combined
1562                //do we need to continue, or write out the set here and stop?
1563                if(cmpstr(name3,"none") == 0)
1564                        //stop here
1565                        norm23 = 1              //norm23 was not used
1566                        Variable/G root:myGlobals:NSORT:gScale2_3 = 1
1567                       
1568                        ControlInfo/W=NSORT_Panel PreviewCheck
1569                        if( V_Value==0 )                //if zero skip the preview and write out the file
1570                                res12[][0] = sq12[p]
1571                                res12[][1] = qb12[p]
1572                                res12[][2] = fs12[p]
1573                                err=WriteNSORTedFile(q12,i12,sig12,name1,name2,name3,name4,normToStr,norm12,norm23,norm34,res=res12)
1574                        endif
1575                        // always clean up waves before exiting
1576                        KillWaves/Z q12,i12,sig12,sq12,qb12,fs12
1577                        return err
1578                Endif
1579               
1580                //need to add the third file... which was already loaded at the top of the function
1581                /////
1582                //order points in sets 12-3, indexing overlap region
1583                //put result in temporary waves
1584                WaveStats/Q q12
1585                n12 = V_npnts
1586                WaveStats/Q $"root:HighQSet:TrimHighQSet_q"
1587                n3 = V_npnts
1588                n123 = n12+ n3
1589               
1590                Make/O/N=(n123) q123,i123,sig123,sq123,qb123,fs123
1591                Make/O/N=(n123,3) res123
1592                WAVE highq = $"root:HighQSet:TrimHighQSet_q"
1593                WAVE highi = $"root:HighQSet:TrimHighQSet_i"
1594                WAVE highs = $"root:HighQSet:TrimHighQSet_s"
1595                WAVE highres = $"root:HighQSet:TrimHighQSet_res"
1596       
1597       
1598                q123[0,n12-1] = q12[p]
1599                q123[n12,n12+n3-1]= highq[p-n12]
1600                i123[0,n12-1] = i12[p]
1601                i123[n12,n12+n3-1]= highi[p-n12]
1602                sig123[0,n12-1] = sig12[p]
1603                sig123[n12,n12+n3-1]= highs[p-n12]
1604                sq123[0,n12-1] = sq12[p]
1605                sq123[n12,n12+n3-1]= highres[p-n12][0]
1606                qb123[0,n12-1] = qb12[p]
1607                qb123[n12,n12+n3-1]= highres[p-n12][1]
1608                fs123[0,n12-1] = fs12[p]
1609                fs123[n12,n12+n3-1]= highres[p-n12][2]
1610
1611               
1612                Sort q123, q123,i123,sig123,sq123,qb123,fs123
1613                /////////////////
1614               
1615                //find the maximum point number of set 2  in the overlap region
1616                FindLevel/P/Q highq,(q12[n12-1])
1617                num2 = trunc(V_levelX)
1618                //Print "num2 = ",num2
1619               
1620                if (auto)
1621                        //there must be overlap points to use auto-scaling
1622                        if(numtype(num2) != 0)
1623                                Abort "There are no data points in the overlap region. Either reduce the number of deleted points or use manual scaling."
1624                        endif
1625                        //do auto-scaling of data
1626                        norm23 = NCNR_GetScalingInOverlap(num2,q12,i12,highq,highi)
1627                        //Set the global variable for the 12 - 3 scale factor
1628                        Variable/G root:myGlobals:NSORT:gScale2_3 = norm23
1629                else
1630                        //use the value from the panel ( which is the global)
1631                        NVAR temp23 = root:myGlobals:NSORT:gScale2_3
1632                        norm23 = temp23
1633                Endif
1634               
1635                If( (normTo== 1) || (normTo ==2) )
1636                        //normalize to first or second file, so multiply third by norm23
1637                        highi *= norm23
1638                        highs *= norm23
1639                else
1640                        //normalize to THIRD file, 1-2 by 1/norm23
1641                        norm23 = 1/norm23
1642                        i12 *= norm23
1643                        sig12 *= norm23
1644                        // for the display, scale the trimmed sets 1 and 2
1645                        lowi *= norm23
1646                        lows *= norm23
1647                        medi *= norm23
1648                        meds *= norm23
1649                Endif
1650               
1651                //Print "NSORT-ed ",name1," + ", name2, " + ", name3
1652                //Print "normalized to ",normTo
1653                //Print "multiplicative factor 1-2 = ",norm12," multiplicative factor 12 - 3 = ",norm23
1654                ControlUpdate/A/W=NSORT_Panel
1655                DoUpdate
1656               
1657                Make/O/N=(n123) q123,i123,sig123
1658                Make/O/N=(n123,3) res123
1659                q123[0,n12-1] = q12[p]
1660                q123[n12,n12+n3-1]= highq[p-n12]
1661                i123[0,n12-1] = i12[p]
1662                i123[n12,n12+n3-1]= highi[p-n12]
1663                sig123[0,n12-1] = sig12[p]
1664                sig123[n12,n12+n3-1]= highs[p-n12]
1665                sq123[0,n12-1] = sq12[p]
1666                sq123[n12,n12+n3-1]= highres[p-n12][0]
1667                qb123[0,n12-1] = qb12[p]
1668                qb123[n12,n12+n3-1]= highres[p-n12][1]
1669                fs123[0,n12-1] = fs12[p]
1670                fs123[n12,n12+n3-1]= highres[p-n12][2]
1671
1672               
1673                Sort q123, q123,i123,sig123,sq123,qb123,fs123
1674                //at this point 12 - 3 are combined
1675               
1676                //do we need to continue, or write out the set here and stop?
1677                if(cmpstr(name4,"none") == 0)
1678                        //stop here
1679               
1680                        ControlInfo/W=NSORT_Panel PreviewCheck
1681                        if( V_Value==0 )                //if zero skip the preview and write out the file
1682                                res123[][0] = sq123[p]
1683                                res123[][1] = qb123[p]
1684                                res123[][2] = fs123[p]
1685                                err=WriteNSORTedFile(q123,i123,sig123,name1,name2,name3,name4,normToStr,norm12,norm23,norm34,res=res123)
1686                        endif
1687                        // always clean up waves before exiting
1688                        KillWaves/Z q12,i12,sig12,q123,i123,sig123,sq123,qb123,fs123 //,res123
1689                        return err
1690                Endif
1691               
1692                // new 2016 - combine the 4th data set
1693
1694                //need to add the fourth file... which was already loaded at the top of the function
1695                /////
1696                //order points in sets 123-4, indexing overlap region
1697                //put result in temporary waves
1698                WaveStats/Q q123
1699                n123 = V_npnts
1700                WaveStats/Q $"root:HighestQSet:TrimHighestQSet_q"
1701                n4 = V_npnts
1702                n1234 = n123 + n4
1703               
1704                Make/O/N=(n1234) q1234,i1234,sig1234,sq1234,qb1234,fs1234
1705                Make/O/N=(n1234,3) res1234
1706                WAVE highestq = $"root:HighestQSet:TrimHighestQSet_q"
1707                WAVE highesti = $"root:HighestQSet:TrimHighestQSet_i"
1708                WAVE highests = $"root:HighestQSet:TrimHighestQSet_s"
1709                WAVE highestres = $"root:HighestQSet:TrimHighestQSet_res"
1710       
1711       
1712                q1234[0,n123-1] = q123[p]
1713                q1234[n123,n123+n4-1]= highestq[p-n123]
1714                i1234[0,n123-1] = i123[p]
1715                i1234[n123,n123+n4-1]= highesti[p-n123]
1716                sig1234[0,n123-1] = sig123[p]
1717                sig1234[n123,n123+n4-1]= highests[p-n123]
1718                sq1234[0,n123-1] = sq123[p]
1719                sq1234[n123,n123+n4-1]= highestres[p-n123][0]
1720                qb1234[0,n123-1] = qb123[p]
1721                qb1234[n123,n123+n4-1]= highestres[p-n123][1]
1722                fs1234[0,n123-1] = fs123[p]
1723                fs1234[n123,n123+n4-1]= highestres[p-n123][2]
1724
1725               
1726                Sort q1234, q1234,i1234,sig1234,sq1234,qb1234,fs1234
1727                /////////////////
1728               
1729                //find the maximum point number of set 2  in the overlap region
1730                FindLevel/P/Q highestq,(q123[n123-1])
1731                num2 = trunc(V_levelX)
1732                //Print "num2 = ",num2
1733               
1734                if (auto)
1735                        //there must be overlap points to use auto-scaling
1736                        if(numtype(num2) != 0)
1737                                Abort "There are no data points in the overlap region. Either reduce the number of deleted points or use manual scaling."
1738                        endif
1739                        //do auto-scaling of data
1740                        norm34 = NCNR_GetScalingInOverlap(num2,q123,i123,highestq,highesti)
1741                        //Set the global variable for the 12 - 3 scale factor
1742                        Variable/G root:myGlobals:NSORT:gScale3_4 = norm34
1743                else
1744                        //use the value from the panel ( which is the global)
1745                        NVAR temp34 = root:myGlobals:NSORT:gScale3_4
1746                        norm34 = temp34
1747                Endif
1748
1749// normalization
1750                switch (normTo)
1751                        case 1:
1752                        case 2:
1753                        case 3:
1754                                //normalize to first or second or third file, so multiply fourth by norm34
1755                                highesti *= norm34
1756                                highests *= norm34
1757                                break
1758                        case 4:
1759                                //normalize to FOURTH file, 123 by multiplicative factor 1/norm34
1760                                norm34 = 1/norm34
1761                                i123 *= norm34
1762                                sig123 *= norm34
1763                                // for the display, scale the trimmed sets 1 and 2 and 3
1764                                lowi *= norm34
1765                                lows *= norm34
1766                                medi *= norm34
1767                                meds *= norm34
1768                                highi *= norm34
1769                                highs *= norm34
1770                                break
1771                endswitch
1772               
1773       
1774                //Print "NSORT-ed ",name1," + ", name2, " + ", name3
1775                //Print "normalized to ",normTo
1776                //Print "multiplicative factor 1-2 = ",norm12," multiplicative factor 12 - 3 = ",norm23
1777                ControlUpdate/A/W=NSORT_Panel
1778                DoUpdate
1779               
1780                Make/O/N=(n1234) q1234,i1234,sig1234
1781                Make/O/N=(n1234,3) res1234
1782                q1234[0,n123-1] = q123[p]
1783                q1234[n123,n123+n4-1]= highestq[p-n123]
1784                i1234[0,n123-1] = i123[p]
1785                i1234[n123,n123+n4-1]= highesti[p-n123]
1786                sig1234[0,n123-1] = sig123[p]
1787                sig1234[n123,n123+n4-1]= highests[p-n123]
1788                sq1234[0,n123-1] = sq123[p]
1789                sq1234[n123,n123+n4-1]= highestres[p-n123][0]
1790                qb1234[0,n123-1] = qb123[p]
1791                qb1234[n123,n123+n4-1]= highestres[p-n123][1]
1792                fs1234[0,n123-1] = fs123[p]
1793                fs1234[n123,n123+n4-1]= highestres[p-n123][2]
1794               
1795                Sort q1234, q1234,i1234,sig1234,sq1234,qb1234,fs1234
1796                //at this point 123 - 4 are combined
1797               
1798//write out the set here and stop
1799                        //stop here
1800       
1801                ControlInfo/W=NSORT_Panel PreviewCheck
1802                if( V_Value==0 )                //if zero skip the preview and write out the file
1803                        res1234[][0] = sq1234[p]
1804                        res1234[][1] = qb1234[p]
1805                        res1234[][2] = fs1234[p]
1806                        err=WriteNSORTedFile(q1234,i1234,sig1234,name1,name2,name3,name4,normToStr,norm12,norm23,norm34,res=res1234)
1807                endif
1808                // always clean up waves before exiting
1809                KillWaves/Z q123,i123,sig123,q1234,i1234,sig1234,sq1234,qb1234,fs1234 //,res1234
1810                return err
1811
1812                //combined dataset will already be displayed if the NSORT_Graph is open
1813       
1814                ////////////////
1815                return err
1816   endif // End the if(6-column) specific stuff here
1817       
1818       
1819End
1820
1821
1822
1823
1824
1825
1826/////////////////////////////////////////////////////////////
1827// testing, may speed up NSORT, NCNR-specific naming scheme of
1828// run numbers and a run prefix
1829//
1830// it is assumed that you are combining data from the current reduction session,
1831// so that the XML y/n hasn't changed.
1832//
1833Function Set3NSORTFiles(low,med,hi,pref)
1834        Variable low,med,hi
1835        String pref
1836       
1837        //make strings from the numbers
1838        String absStr="",ext
1839        Variable popNum
1840        DoWindow/F NSORT_Panel
1841       
1842        SVAR lowQPopStr = root:myGlobals:NSORT:gDataPopList
1843        SVAR medHiQPopStr = root:myGlobals:NSORT:gDataPopList_3
1844       
1845        NVAR useXMLOutput = root:Packages:NIST:gXML_Write
1846        if(useXMLOutput)
1847                ext = ".ABSx"
1848        else
1849                ext = ".ABS"
1850        endif
1851       
1852        //lowQ menu
1853        absStr = pref+RunDigitString(low)+ext
1854        popNum = WhichListItem(absStr,lowQPopStr,";",0)
1855        if(popNum == -1)
1856                Abort "Could not find file: " + absStr +" aborting...  Be sure that your output format is the same as the input"
1857        endif
1858        popNum += 1             // add 1 to get the item number
1859        PopupMenu popup_1,win=NSORT_Panel,mode=(popNum)
1860       
1861        //medQ (a different list for the popup)
1862        absStr = pref+RunDigitString(med)+ext
1863        popNum = WhichListItem(absStr,medHiQPopStr,";",0)
1864        if(popNum == -1)
1865                Abort "Could not find file: "+absStr+" aborting...  Be sure that your output format is the same as the input"
1866        endif
1867        popNum += 1             // add 1 to get the item number
1868        PopupMenu popup_2,win=NSORT_Panel,mode=(popNum)
1869       
1870       
1871        //highQ (same pop list as medQ)
1872        if(hi != 0)
1873                absStr = pref+RunDigitString(hi)+ext
1874                popNum = WhichListItem(absStr,medHiQPopStr,";",0)
1875                if(popNum == -1)
1876                        Abort "Could not find file: "+absStr+" aborting...  Be sure that your output format is the same as the input"
1877                endif
1878                popNum += 1             // add 1 to get the item number
1879                PopupMenu popup_3,win=NSORT_Panel,mode=(popNum)
1880        else
1881                PopupMenu popup_3,win=NSORT_Panel,mode=(1)
1882        endif
1883End
1884
1885/////////////////////////////////////////////////////////////
1886// testing, may speed up NSORT, NCNR-specific naming scheme of
1887// run numbers and a run prefix
1888//
1889// it is assumed that you are combining data from the current reduction session,
1890// so that the XML y/n hasn't changed.
1891//
1892// updated to use 4 files
1893//
1894// TODO:
1895// -- still assuming ABS
1896//
1897Function Set4NSORTFiles(low,med,hi,highest,pref)
1898        Variable low,med,hi,highest
1899        String pref
1900       
1901        //make strings from the numbers
1902        String absStr="",ext
1903        Variable popNum
1904        DoWindow/F NSORT_Panel
1905       
1906        SVAR lowQPopStr = root:myGlobals:NSORT:gDataPopList
1907        SVAR medHiQPopStr = root:myGlobals:NSORT:gDataPopList_3
1908       
1909        NVAR useXMLOutput = root:Packages:NIST:gXML_Write
1910        if(useXMLOutput)
1911                ext = ".ABSx"
1912        else
1913                ext = ".ABS"
1914        endif
1915       
1916        //lowQ menu
1917        absStr = pref+RunDigitString(low)+ext
1918        popNum = WhichListItem(absStr,lowQPopStr,";",0)
1919        if(popNum == -1)
1920                Abort "Could not find file: " + absStr +" aborting...  Be sure that your output format is the same as the input"
1921        endif
1922        popNum += 1             // add 1 to get the item number
1923        PopupMenu popup_1,win=NSORT_Panel,mode=(popNum)
1924       
1925        //medQ (a different list for the popup)
1926        absStr = pref+RunDigitString(med)+ext
1927        popNum = WhichListItem(absStr,medHiQPopStr,";",0)
1928        if(popNum == -1)
1929                Abort "Could not find file: "+absStr+" aborting...  Be sure that your output format is the same as the input"
1930        endif
1931        popNum += 1             // add 1 to get the item number
1932        PopupMenu popup_2,win=NSORT_Panel,mode=(popNum)
1933       
1934       
1935        //highQ (same pop list as medQ)
1936        if(hi != 0)
1937                absStr = pref+RunDigitString(hi)+ext
1938                popNum = WhichListItem(absStr,medHiQPopStr,";",0)
1939                if(popNum == -1)
1940                        Abort "Could not find file: "+absStr+" aborting...  Be sure that your output format is the same as the input"
1941                endif
1942                popNum += 1             // add 1 to get the item number
1943                PopupMenu popup_3,win=NSORT_Panel,mode=(popNum)
1944        else
1945                PopupMenu popup_3,win=NSORT_Panel,mode=(1)
1946        endif
1947       
1948        //highestQ (same pop list as medQ)
1949        if(highest != 0)
1950                absStr = pref+RunDigitString(highest)+ext
1951                popNum = WhichListItem(absStr,medHiQPopStr,";",0)
1952                if(popNum == -1)
1953                        Abort "Could not find file: "+absStr+" aborting...  Be sure that your output format is the same as the input"
1954                endif
1955                popNum += 1             // add 1 to get the item number
1956                PopupMenu popup_4,win=NSORT_Panel,mode=(popNum)
1957        else
1958                PopupMenu popup_4,win=NSORT_Panel,mode=(1)
1959        endif
1960       
1961        return(0)
1962End
1963
1964//more beta procedures - to create a table of scattering runs to combine with NSORT
1965Proc CreateTableToCombine(ctrlName)
1966        String ctrlName
1967       
1968        NewDataFolder/O root:myGlobals:CombineTable
1969//      DoWindow/F CombineTable
1970       
1971        Make/O/T/N=0 $"root:myGlobals:CombineTable:Filenames"
1972        Make/O/T/N=0 $"root:myGlobals:CombineTable:Suffix"
1973        Make/O/T/N=0 $"root:myGlobals:CombineTable:Labels"
1974        Make/O/D/N=0 $"root:myGlobals:CombineTable:SDD"
1975        Make/O/D/N=0 $"root:myGlobals:CombineTable:RunNumber"
1976        Make/O/D/N=0 $"root:myGlobals:CombineTable:IsTrans"
1977
1978
1979        AppendToTable/W=CombinePanel#GroupedFiles root:myGlobals:CombineTable:Labels, root:myGlobals:CombineTable:SDD, root:myGlobals:CombineTable:RunNumber
1980
1981        ModifyTable/W=CombinePanel#GroupedFiles width(:myGlobals:CombineTable:SDD)=40
1982        ModifyTable/W=CombinePanel#GroupedFiles width(:myGlobals:CombineTable:Labels)=180
1983        ModifyTable/W=CombinePanel#GroupedFiles width(Point)=0          //JUN04, remove point numbers - confuses users since point != run
1984
1985
1986        //get a list of all files in the folder, some will be junk version numbers that don't exist     
1987        String list,partialName,tempName,temp=""
1988        list = IndexedFile(catPathName,-1,"????")       //get all files in folder
1989        Variable numitems,ii,ok
1990       
1991        //remove version numbers from semicolon-delimited list
1992        list =  RemoveVersNumsFromList(list)
1993        numitems = ItemsInList(list,";")
1994       
1995        //loop through all of the files in the list, reading CAT/SHORT information if the file is RAW SANS
1996        //***version numbers have been removed***
1997        String str,fullName
1998        Variable lastPoint
1999        ii=0
2000       
2001        Make/T/O/N=0 notRAWlist
2002        do
2003                //get current item in the list
2004                partialName = StringFromList(ii, list, ";")
2005                //get a valid file based on this partialName and catPathName
2006                tempName = FindValidFilename(partialName)
2007                If(cmpstr(tempName,"")==0)              //a null string was returned
2008                        //write to notebook that file was not found
2009                        //if string is not a number, report the error
2010                        if(numtype(str2num(partialName)) == 2)
2011                                str = "this file was not found: "+partialName+"\r\r"
2012                                //Notebook CatWin,font="Times",fsize=12,text=str
2013                        Endif
2014                else
2015                        //prepend path to tempName for read routine
2016                        PathInfo catPathName
2017                        FullName = S_path + tempName
2018                        //make sure the file is really a RAW data file
2019                        ok = CheckIfRawData(fullName)
2020                        if (!ok)
2021                                //write to notebook that file was not a RAW SANS file
2022                                lastPoint = numpnts(notRAWlist)
2023                                InsertPoints lastPoint,1,notRAWlist
2024                                notRAWlist[lastPoint]=tempname
2025                        else
2026                                //go write the header information to the Notebook
2027                                GetHeaderInfoToCombineWave(fullName,tempName)
2028                        Endif
2029                Endif
2030                ii+=1
2031        while(ii<numitems)
2032//Now sort them all based on the suffix data (orders them as collected)
2033//      SortCombineWaves()
2034// sort by label
2035        SortCombineByLabel()
2036// remove the transmission waves
2037//
2038        RemoveTransFilesFromCombine()
2039//
2040        SetDataFolder root:
2041       
2042        Killwaves/Z notRAWlist
2043End
2044
2045
2046Function RemoveTransFilesFromCombine()
2047        Wave/T filenames = $"root:myGlobals:CombineTable:Filenames"
2048        Wave/T suffix = $"root:myGlobals:CombineTable:Suffix"
2049        Wave/T labels = $"root:myGlobals:CombineTable:Labels"
2050        Wave sdd = $"root:myGlobals:CombineTable:SDD"
2051        Wave runnum = $"root:myGlobals:CombineTable:RunNumber"
2052        Wave isTrans = $"root:myGlobals:CombineTable:IsTrans"
2053       
2054        Variable num=numpnts(isTrans),ii
2055        ii=num-1
2056        do
2057                if(isTrans[ii] != 0)
2058                        DeletePoints ii, 1, filenames,suffix,labels,sdd,runnum,isTrans
2059                endif
2060                ii-=1
2061        while(ii>=0)
2062        return(0)
2063End
2064
2065//reads header information and puts it in the appropriate waves for display in the table.
2066//fname is the full path for opening (and reading) information from the file
2067//which alreay was found to exist. sname is the file;vers to be written out,
2068//avoiding the need to re-extract it from fname.
2069Function GetHeaderInfoToCombineWave(fname,sname)
2070        String fname,sname
2071       
2072        String textstr,temp,lbl,date_time,suffix
2073        Variable ctime,lambda,sdd,detcnt,cntrate,refNum,trans,thick,xcenter,ycenter,numatten
2074        Variable lastPoint, beamstop
2075
2076        Wave/T GFilenames = $"root:myGlobals:CombineTable:Filenames"
2077        Wave/T GSuffix = $"root:myGlobals:CombineTable:Suffix"
2078        Wave/T GLabels = $"root:myGlobals:CombineTable:Labels"
2079        Wave GSDD = $"root:myGlobals:CombineTable:SDD"
2080        Wave GRunNumber = $"root:myGlobals:CombineTable:RunNumber"
2081        Wave GIsTrans = $"root:myGlobals:CombineTable:IsTrans"
2082       
2083        lastPoint = numpnts(GLambda)
2084               
2085        InsertPoints lastPoint,1,GFilenames
2086        GFilenames[lastPoint]=sname
2087       
2088        //read the file suffix
2089        InsertPoints lastPoint,1,GSuffix
2090        GSuffix[lastPoint]=getSuffix(fname)
2091
2092        // read the sample.label text field
2093        InsertPoints lastPoint,1,GLabels
2094        GLabels[lastPoint]=getSampleLabel(fname)
2095       
2096        //read in the SDD
2097        InsertPoints lastPoint,1,GSDD
2098        GSDD[lastPoint]= getSDD(fname)
2099
2100        //the run number (not displayed in the table, but carried along)
2101        InsertPoints lastPoint,1,GRunNumber
2102        GRunNumber[lastPoint] = GetRunNumFromFile(sname)
2103
2104        // 0 if the file is a scattering  file, 1 (truth) if the file is a transmission file
2105        InsertPoints lastPoint,1,GIsTrans
2106        GIsTrans[lastPoint]  = isTransFile(fname)               //returns one if beamstop is "out"
2107       
2108        KillWaves/Z w
2109        return(0)
2110End
2111
2112//sorts all of the waves of header information using the suffix (A123)
2113//the result is that all of the data is in the order that it was collected,
2114// regardless of how the prefix or run numbers were changed by the user
2115Function SortCombineWaves()
2116        Wave/T GFilenames = $"root:myGlobals:CombineTable:Filenames"
2117        Wave/T GSuffix = $"root:myGlobals:CombineTable:Suffix"
2118        Wave/T GLabels = $"root:myGlobals:CombineTable:Labels"
2119        Wave GSDD = $"root:myGlobals:CombineTable:SDD"
2120        Wave GRunNumber = $"root:myGlobals:CombineTable:RunNumber"
2121        Wave GIsTrans = $"root:myGlobals:CombineTable:IsTrans"
2122
2123//      Sort GSuffix, GSuffix, GFilenames, GLabels, GDateTime, GSDD, GLambda, GCntTime, GTotCnts, GCntRate, GTransmission, GThickness, GXCenter, GYCenter, GNumAttens,GRunNumber,GIsTrans
2124        Sort GSuffix, GSuffix, GFilenames, GLabels, GSDD, GRunNumber, GIsTrans
2125        return(0)
2126End
2127
2128//sorts all of the waves of header information using the suffix (A123)
2129//the result is that all of the data is in the order that it was collected,
2130// regardless of how the prefix or run numbers were changed by the user
2131Function SortCombineByLabel()
2132        Wave/T GFilenames = $"root:myGlobals:CombineTable:Filenames"
2133        Wave/T GSuffix = $"root:myGlobals:CombineTable:Suffix"
2134        Wave/T GLabels = $"root:myGlobals:CombineTable:Labels"
2135        Wave GSDD = $"root:myGlobals:CombineTable:SDD"
2136        Wave GRunNumber = $"root:myGlobals:CombineTable:RunNumber"
2137        Wave GIsTrans = $"root:myGlobals:CombineTable:IsTrans"
2138
2139        Sort GLabels, GSuffix, GFilenames, GLabels, GSDD, GRunNumber, GIsTrans
2140//      Sort {GLabels, GSDD}, GSuffix, GFilenames, GLabels, GSDD, GRunNumber, GIsTrans          //sort on GLabels, GSDD breaks the tie
2141        return(0)
2142End
2143
2144//main procedure, called from the menu
2145// sets a flag (temporarily) to use the names from the table
2146// during the procedure that writes the data files.
2147//
2148//
2149Function DoCombineFiles(ctrlName)
2150        String ctrlName
2151       
2152       
2153        if(WinType("NSORT_Panel") == 0)
2154                DoAlert 0, "The SORT Panel must be open to combine the files"
2155                return(0)
2156        endif
2157       
2158        DoAlert 1,"Do you have all the assignments set in the bottom table? If not, < No > will exit."
2159        if(V_flag == 2)
2160                return(0)               //no, get out
2161        endif
2162        // pop all of the menus to make sure that they are properly populated
2163        LowQPopMenuProc("",1,"")
2164        MedQPopMenuProc("",1,"")
2165        HighQPopMenuProc("",1,"")
2166        HighestQPopMenuProc("",1,"")
2167       
2168//      String savedDataFolder = GetDataFolder(1)               // save
2169        Wave LowRun = root:myGlobals:CombineTable:LowRun
2170        Wave MediumRun = root:myGlobals:CombineTable:MediumRun
2171        Wave HighRun = root:myGlobals:CombineTable:HighRun
2172        Wave HighestRun = root:myGlobals:CombineTable:HighestRun
2173        Wave/T prefix = root:myGlobals:CombineTable:Prefix
2174        Wave/T saveName = root:myGlobals:CombineTable:saveName
2175
2176        Variable/G root:myGlobals:CombineTable:useTable=1
2177       
2178        Variable num=numpnts(lowRun),ii,lowFile,medFile,hiFile,highestFile
2179        String prefixStr = ""
2180        Pathinfo catPathName
2181        String path=S_Path
2182       
2183        ii=0
2184        do
2185                lowFile = LowRun[ii]
2186                medFile = MediumRun[ii]
2187                hiFile = highRun[ii]
2188                highestFile = highestRun[ii]
2189                prefixStr = prefix[ii]
2190               
2191                Set4NSORTFiles(lowFile,medFile,hiFile,highestFile,prefixStr)            //set the files and pop the NSORT popups
2192               
2193                //pass the new file name in as a global (ugh!)
2194                String/G root:myGlobals:CombineTable:SaveNameStr = path+saveName[ii]
2195                //combine the files and write the data
2196                WriteNSORTFileButton("")
2197               
2198                Print "wrote file : ",path+saveName[ii]
2199                ii+=1
2200        while(ii<num)
2201
2202        Variable/G root:myGlobals:CombineTable:useTable=0               //turn this off immediately
2203       
2204        return(0)
2205End
2206
2207
2208// only respond to clicks in the subwindow (table) rather than everywhere. Hooks can't be set for subwindows
2209//
2210//// Window hook example:
2211//  WINDOW:CombinePanel;HCSPEC:CombinePanel#GroupedFiles;EVENT:mouseup;MOUSEX:152;MOUSEY:143;TICKS:7722029;MODIFIERS:0;
2212//
2213Function CombineTableHook(infoStr)
2214        String infoStr
2215        String event= StringByKey("EVENT",infoStr)
2216        String subwin = StringByKey("HCSPEC",infoStr)
2217//      Print subwin
2218//      Print infoStr
2219//      Print "EVENT= ",event
2220        if(cmpstr(subwin,"CombinePanel#GroupedFiles")==0)
2221                strswitch(event)
2222                        case "mousedown":
2223                                Variable xpix= NumberByKey("MOUSEX",infoStr)
2224                                Variable ypix= NumberByKey("MOUSEY",infoStr)
2225                                Variable modif= NumberByKey("MODIFIERS",infoStr)
2226                                //print modif
2227                                if(modif & 2^1)         //bit 1 set, shift key is down
2228                                        PopupContextualMenu/C=(xpix, ypix) "combine;"
2229                                        strswitch(S_selection)
2230                                                case "combine":
2231                                                        //Print "combine the files"
2232                                                        SendSelectionToTable()
2233                                                        break
2234                                        endswitch               //on selection
2235                                endif
2236                endswitch       // on event
2237        endif
2238        return 0
2239End
2240
2241//ASSUMES 3 FILES!!!!
2242Function SendSelectionToTable()
2243
2244        DoWindow/F CombinePanel
2245        if(V_flag==0)
2246//              Make/O/N=0 $"root:myGlobals:CombineTable:Low"
2247//              Make/O/N=0 $"root:myGlobals:CombineTable:Medium"
2248//              Make/O/N=0 $"root:myGlobals:CombineTable:High"
2249//              Make/O/T/N=0 $"root:myGlobals:CombineTable:Prefix"
2250//              Make/O/T/N=0 $"root:myGlobals:CombineTable:SaveName"
2251//              edit Low,Medium,High,Prefix,SaveName as "Run Numbers to Combine"
2252//              DoWindow/C ToCombine
2253
2254                return(0)
2255               
2256        else
2257                Wave low = $"root:myGlobals:CombineTable:LowRun"
2258                Wave medium = $"root:myGlobals:CombineTable:MediumRun"
2259                Wave high = $"root:myGlobals:CombineTable:HighRun"
2260                Wave highest = $"root:myGlobals:CombineTable:HighestRun"
2261                Wave/T prefix = $"root:myGlobals:CombineTable:Prefix"
2262                Wave/T saveName = $"root:myGlobals:CombineTable:SaveName"
2263               
2264                Wave/T gLabels = $"root:myGlobals:CombineTable:Labels"
2265                Wave gSDD = $"root:myGlobals:CombineTable:SDD"
2266                Wave gRunNumber = $"root:myGlobals:CombineTable:RunNumber"
2267                Wave/T filenames = $"root:myGlobals:CombineTable:FileNames"
2268        endif
2269       
2270        GetSelection table,CombinePanel#GroupedFiles,3
2271//      Print V_startRow, V_endRow
2272       
2273        //prompt for combined name, give the user a chance to cancel
2274        Variable num=V_endRow-V_startRow+1
2275        Variable ii
2276        String saveStr=""
2277        Prompt saveStr,"saved file name for "+ gLabels[V_StartRow]      //+tmpLbl[1]
2278        DoPrompt "Enter the combined file name",saveStr
2279        if(V_flag==1)
2280                return(1)               //user cancel, get out before anything is set
2281        endif
2282
2283        if( !(num==2 || num==3 || num==4) )
2284                Abort "invalid table selection - must select either 2 or 3 or 4 files to combine"
2285        endif
2286        Make/O/T/N=(4) tmpLbl
2287        Make/O/N=(4) tmpSDD,tmpRun
2288       
2289        //initialize with fake values in case 2 or 3 files are combined
2290        tmpLbl = ""
2291        tmpSDD = 0.01           //fake sdd in meters to always be the "highest" Q
2292        tmpRun = 0                      //pass a run number of zero to be later interpreted as "none"
2293       
2294        //fill in the real values from the selection
2295        for(ii=V_startRow;ii<=V_endRow;ii+=1)
2296                tmpLbl[ii-V_startRow] = gLabels[ii]
2297                tmpSDD[ii-V_startRow] = gSDD[ii]
2298                tmpRun[ii-V_startRow] = gRunNumber[ii]
2299        endfor
2300//      if(num==2)      // then "highest" q run needs to be forced to zero
2301//              ii=2
2302//              tmpLbl[ii] = ""
2303//              tmpSDD[ii] = 0.01               //fake sdd in meters to always be the "highest" Q
2304//              tmpRun[ii] = 0                  //pass a run number of zero to be later interpreted as "none"
2305//      endif
2306        Sort tmpSDD, tmpSDD,tmpLbl,tmpRun
2307       
2308//      Print tmpSDD
2309       
2310        num=numpnts(low)
2311        InsertPoints num, 1, low,medium,high,highest,prefix,SaveName
2312        low[num] = tmpRun[3]
2313        medium[num] = tmpRun[2]
2314        high[num] = tmpRun[1]
2315        highest[num] = tmpRun[0]
2316        prefix[num] = GetPrefixStrFromFile(filenames[ii])
2317        saveName[num] = saveStr
2318
2319        KillWaves/Z tmpLbl,tmpRun,tmpSDD
2320        return(0)
2321end
2322
2323
2324////////////////////////
2325// replaces the beta menu items
2326//
2327
2328Proc ShowCombinePanel()
2329        DoWindow/F CombinePanel
2330        if(V_flag==0)
2331                CombinePanel()
2332                CreateTableToCombine("")
2333                DoAlert 1,"Do you want to clear the list of runs and file names to combine?"
2334                TableToCombineAndSave(V_flag==1)                // clear and initialize, if desired
2335        endif
2336end
2337
2338Proc CombinePanel()
2339        PauseUpdate; Silent 1           // building window...
2340        NewPanel /W=(546,442,1197,915) /K=1 as "Sort and Combine Data Files"
2341        ModifyPanel cbRGB=(49151,53155,65535)
2342        DoWindow/C CombinePanel
2343        Button button0_0,pos={20,20},size={160,20},proc=CreateTableToCombine,title="List Files to Combine"
2344        Button button0_1,pos={206,20},size={140,20},proc=DoCombineFiles,title="Combine Files"
2345        Button button0_2,pos={509,40},size={60,20},proc=CombinePanelDone,title="Done"
2346        Button button0_3,pos={522,14},size={30,20},proc=ShowCombineHelp,title="?"
2347        Button button0_4,pos={500,220},size={120,20},proc=ClearCombineTable,title="Clear Table?"
2348        Edit/W=(20,54,368,249)/HOST=#
2349        ModifyTable format=1,width=0
2350        RenameWindow #,GroupedFiles
2351        SetActiveSubwindow ##
2352        Edit/W=(20,263,634,455)/HOST=#
2353        ModifyTable format=1
2354        RenameWindow #,RunNumbersToCombine
2355        SetActiveSubwindow ##
2356        SetWindow kwTopWin hook=CombineTableHook, hookevents=1  // mouse down events
2357EndMacro
2358
2359Proc ShowCombineHelp(ctrlName): ButtonControl
2360        String ctrlName
2361        DisplayHelpTopic/K=1/Z "SANS Data Reduction Tutorial[Batch Combine Data Files]"
2362        if(V_flag !=0)
2363                DoAlert 0,"The SANS Data Reduction Tutorial Help file could not be found"
2364        endif
2365end
2366
2367Function CombinePanelDone(ctrlName)
2368        String ctrlName
2369       
2370        DoWindow/K CombinePanel
2371        return(0)
2372end
2373
2374Function ClearCombineTable(ctrlName)
2375        String ctrlName
2376       
2377        DoAlert 1,"Do you want to clear the list of runs and file names to combine?"
2378        TableToCombineAndSave(V_flag==1)                // clear and initialize, if desired
2379        return(0)
2380end
2381
2382Function TableToCombineAndSave(clear)
2383        Variable clear
2384       
2385        if(clear)
2386                // make the waves and table for the sets to combine
2387                Make/O/N=0 $"root:myGlobals:CombineTable:LowRun"
2388                Make/O/N=0 $"root:myGlobals:CombineTable:MediumRun"
2389                Make/O/N=0 $"root:myGlobals:CombineTable:HighRun"
2390                Make/O/N=0 $"root:myGlobals:CombineTable:HighestRun"
2391                Make/O/T/N=0 $"root:myGlobals:CombineTable:Prefix"
2392                Make/O/T/N=0 $"root:myGlobals:CombineTable:SaveName"
2393        endif
2394        SetDataFolder root:myGlobals:CombineTable
2395       
2396        // make the second table
2397        AppendToTable/W=CombinePanel#RunNumbersToCombine LowRun,MediumRun,HighRun,HighestRun,Prefix,SaveName
2398       
2399        SetDataFolder root:
2400End
2401
2402
2403
2404/////////////////////////////////
2405// currently unused
2406//
2407Proc MakeCombineTable_byName()
2408        NewDataFolder/O root:myGlobals:CombineTable                     //in case it doesn't exist yet
2409        Make/O/T/N=1 lowQfile,medQfile,hiQfile,saveName
2410        Edit/W=(330,148,973,360) lowQfile,medQfile,hiQfile,saveName
2411        ModifyTable format(Point)=1,width(lowQfile)=120,width(medQfile)=120,width(hiQfile)=120
2412        ModifyTable width(saveName)=120
2413End
2414
2415//
2416// currently unused
2417//
2418// Another beta procedure, to allow files to be combined quickly
2419// - make 4 waves (text) with the low, med, hi, and wave names
2420// (if there is no hiQ, then pass a wave with "" for all entries)
2421// - then pass the waves, and the save will work like in the DoCombineFiles
2422//
2423// - the named files must be there - there is no error checking
2424// - the NSORT panel must be open with the proper selections of beg,end and autoscale, etc.
2425//
2426// - could write a little proc to generate a table to fill in and a button to call this
2427// - and then think of quick ways to populate the table with file names (and minimize typos)
2428//
2429Function DoCombineFiles_byName(lowW,medW,hiW,saveW)
2430        Wave/T lowW,medW,hiW,saveW
2431               
2432        if(WinType("NSORT_Panel") == 0)
2433                DoAlert 0, "The SORT Panel must be open to combine the files"
2434                return(0)
2435        endif
2436       
2437        // pop all of the menus to make sure that they are properly populated
2438        LowQPopMenuProc("",1,"")
2439        MedQPopMenuProc("",1,"")
2440        HighQPopMenuProc("",1,"")
2441       
2442        Variable num=numpnts(lowW),ii
2443        String lowFile,medFile,hiFile
2444       
2445        Pathinfo catPathName
2446        String path=S_Path
2447       
2448////////        Make/O/D/N=(numpnts(lowW)) scale_4m
2449        NVAR scale12 = root:myGlobals:NSORT:gScale1_2
2450
2451       
2452// this variable must exist and be set to 1 to be able to automatically name files
2453// and use the global saveNameStr that is passed in
2454// -- turn this off when done   
2455        Variable/G root:myGlobals:CombineTable:useTable=1               
2456
2457        ii=0
2458        do
2459                lowFile = lowW[ii]
2460                medFile = medW[ii]
2461                hiFile = hiW[ii]
2462               
2463                //Set3NSORTFiles(lowFile,medFile,hiFile,prefixStr)              //set the files and pop the NSORT popups
2464                //lowQ menu
2465                PopupMenu popup_1 win=NSORT_Panel,popmatch=lowFile
2466               
2467                // mediumQ menu
2468                if(strlen(medFile)!=0)
2469                        PopupMenu popup_2 win=NSORT_Panel,popmatch=medFile
2470                else
2471                        PopupMenu popup_2,win=NSORT_Panel,popmatch="none"       //set to "none"
2472                endif
2473       
2474                //highQ (same pop list as medQ)
2475                if(strlen(hiFile)!=0)
2476                        PopupMenu popup_3 win=NSORT_Panel,popmatch=hiFile
2477                else
2478                        PopupMenu popup_3,win=NSORT_Panel,popmatch="none"       //set to "none"
2479                endif
2480               
2481               
2482                //pass the new file name in as a global (ugh!)
2483                String/G root:myGlobals:CombineTable:SaveNameStr = path+saveW[ii]
2484                //combine the files and write the data
2485                WriteNSORTFileButton("")
2486               
2487//////          scale_4m[ii] = scale12
2488               
2489                Print "wrote file : ",path+saveW[ii]
2490                ii+=1
2491        while(ii<num)
2492
2493        Variable/G root:myGlobals:CombineTable:useTable=0               //turn this off immediately
2494       
2495        return(0)
2496End
Note: See TracBrowser for help on using the repository browser.