source: sans/SANSReduction/branches/kline_29MAR07/Put in User Procedures/SANS_Reduction_v5.00/NSORT.ipf @ 72

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

more file adjustments to clean out NCNR bits

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