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

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

RT 809 fix to correct resolution calculation

  • gravity term
  • lens resolution

-- this also led to changes in the two averaging routines to pick up this flag and pass it to getResolution()
-- then two SANSBeta menu items added to crudely change the header flag (0|1) on a run-by-run basis
!! NOTE that the VAX header has not been updated to put this information in at the time of data collection!!

other minor changes...
Some changes to beta procedures for combining files (I don't think anyone uses these but me) - these changes are in NSORT.ipf.

Some changes to the MainPanel? to remove the RealTime? button (which somehow came back)

SRK 10AUG07

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