source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/SANS/NSORT.ipf @ 902

Last change on this file since 902 was 902, checked in by srkline, 10 years ago

-adjusted 2D simulation to get a proper beam center into the file. Previously was off by 1 pixel in x.

  • conditions to keep locaed pixel in simulation to range [0,127]
  • in 1D sim, -ve values from noise are replaced w/ zero data value and error of one

AutoFit? allows epsilon wave again, checkbox is un-hidden.

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