source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/SANS/SANS_Utilities.ipf @ 665

Last change on this file since 665 was 647, checked in by srkline, 13 years ago

(1) fixed file numbering scheme in HFIR_Utils to generate 4-digit number
(2) moved transmission beta operations to new buttons on the transmission panel
(3) cleared transmissin beta operations from the beta menu

File size: 14.5 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma version=5.0
3#pragma IgorVersion=6.1
4
5// contains general utility procedures for:
6// file open dialogs
7// paths
8// lists to waves (and the reverse)
9//
10// and BETA procedures for:
11// generating MRED lists
12// guessing of transmission file assignments
13//
14// - these files may or may not be NCNR-specific. They may eventually
15// need to be moved into NCNR_Utils, or to more appropriate locations
16//
17//
18// 29MAR07 SRK
19//
20
21
22//////////////////////
23// "intelligent" differentiation of the data files based on information gathered while
24// getting the FIle Catalog. Uses some waves that are generated there, but not displayed in the table
25//
26// See CatVSTable.ipf for where these files are created (and sorted to keep them together)
27////////////
28
29//testing - unused
30//
31Function/S TextWave2SemiList(textW)
32        Wave/T textW
33       
34        String list=""
35        Variable num=numpnts(textW),ii=0
36        do
37                list += textw[ii] + ";"
38                ii+=1
39        while(ii<num)
40        return(list)
41End
42
43Function/S NumWave2CommaList(numW)
44        Wave numW
45       
46        String list=""
47        Variable num=numpnts(numW),ii=0
48        do
49                list += num2Str(numW[ii]) + ","
50                ii+=1
51        while(ii<num)
52        return(list)
53End
54
55// utility function to convert a list (string) of semicolon-delimited
56//items to a text wave
57Function List2TextWave(str,tw)
58        String str
59        wave/T tw
60       
61        Variable num=ItemsinList(str,";"),ii=0
62        Redimension/N=(num) tw
63        do
64                tw[ii] = StringFromList(ii, str ,";")
65                ii+=1
66        while(ii<num)
67        return(0)
68       
69End
70
71// generates a list of the procedure files in the experiment
72// putting the results in a wave named "tw", editing and sorting the wave
73//
74Proc ListIncludedFiles()
75        Make/O/T/N=2 tw
76        String str=""
77        //str = WinList("*", ";","INCLUDE:6")
78        str = WinList("*", ";","WIN:128")
79        List2TextWave(str,tw)
80        Edit tw
81        Sort tw tw
82End
83
84// returns a comma delimited list of run numbers based on the run numbers collected
85// during the building of the File Catalog (why do it twice)
86Function/S RunNumberList()
87
88        Wave w= $"root:myGlobals:CatVSHeaderInfo:RunNumber"
89        String list=""
90        if(WaveExists(w) == 0)
91                list = ""
92        else
93                list=NumWave2CommaList(w)
94        endif
95        return(list)
96End
97
98// list is a comma delimited list of run numbers, from the File Catalog
99// - scan through the list and remove numbers that are not transmission files
100//
101Function/S isTransList(list)
102        String list
103       
104        //scan through the list, find the corresponding point number, and see what isTrans says
105        Variable ii,num,temp
106        String newList=""
107        num=ItemsInList(list ,",")
108        for(ii=0;ii<num;ii+=1)
109                temp = str2num( StringFromList(ii, list ,",") )
110                if(RunNumIsTransFile(temp))
111                        newList += num2str(temp) + ","
112                endif
113        endfor
114       
115        return(newList)
116End
117
118//truth if run number is a transmission file
119// based on whatever is currently in the File Catalog
120//
121// Can't use findlevel - it assumes a monotonic RunNumber wave
122Function RunNumIsTransFile(num)
123        Variable num
124       
125        Wave isTrans = $"root:myGlobals:CatVSHeaderInfo:IsTrans"
126        Wave RunNumber = $"root:myGlobals:CatVSHeaderInfo:RunNumber"
127       
128        if( (WaveExists(isTrans) == 0 ) ||  (WaveExists(RunNumber) == 0 ) )
129                return(0)
130        endif
131       
132        Variable pts=numpnts(RunNumber),ii
133        for(ii=0;ii<pts;ii+=1)
134                if(RunNumber[ii] == num)
135                        return(isTrans[ii])
136                endif
137        endfor
138//      FindLevel/P/Q RunNumber,num
139//
140//      if(isTrans[V_LevelX]==1)
141//              return(1)
142//      else
143//              return(0)
144//      endif
145End
146
147//truth if run number is at the given sample to detector distance
148// based on whatever is currently in the File Catalog
149//
150// need fuzzy comparison, since SDD = 1.33 may actually be represented in FP as 1.33000004      !!!
151Function RunNumIsAtSDD(num,sdd)
152        Variable num,sdd
153       
154        Wave w = $"root:myGlobals:CatVSHeaderInfo:SDD"
155        Wave RunNumber = $"root:myGlobals:CatVSHeaderInfo:RunNumber"
156       
157        if( (WaveExists(w) == 0 ) ||  (WaveExists(RunNumber) == 0 ) )
158                return(0)
159        endif
160        Variable pts=numpnts(RunNumber),ii
161        for(ii=0;ii<pts;ii+=1)
162                if(RunNumber[ii] == num)
163                        if(abs(w[ii] - sdd) < 0.001     )               //if numerically within 0.001 meter, they're the same
164                                return(1)
165                        else
166                                return(0)
167                        endif
168                endif
169        endfor
170End
171
172
173// list is a comma delimited list of run numbers, from the File Catalog
174// - scan through the list and remove numbers that are not at the specified SDD
175//
176Function/S atSDDList(list,sdd)
177        String list
178        Variable sdd
179       
180        //scan through the list, find the corresponding point number, and see what SDD the run is at
181        Variable ii,num,temp
182        String newList=""
183        num=ItemsInList(list ,",")
184        for(ii=0;ii<num;ii+=1)
185                temp = str2num( StringFromList(ii, list ,",") )
186                if(RunNumIsAtSDD(temp,sdd))
187                        newList += num2str(temp) + ","
188                endif
189        endfor
190       
191        return(newList)
192End
193
194//given a comma-delimited list, remove those that are trans files
195//
196Function/S removeTrans(list)
197        String list
198       
199        //scan through the list, and remove those that are trans files
200        Variable ii,num,temp
201//      String newList=""
202        num=ItemsInList(list ,",")
203        for(ii=0;ii<num;ii+=1)
204                temp = str2num( StringFromList(ii, list ,",") )
205                if(RunNumIsTransFile(temp))
206                        list = RemoveFromList(num2str(temp),list,",")
207                        ii -= 1                 //item ii was just deleted (everything moves to fill in)
208                        num -= 1                // and the list is shorter now
209                endif
210        endfor
211       
212        return(list)
213End
214
215Function setMREDFileList(str)
216        String str
217       
218        SVAR/Z list = root:myGlobals:MRED:gFileNumList
219        if(SVAR_Exists(list)==0)                //check for myself
220                DoAlert 0,"The Multiple Reduce Panel must be open for you to use this function"
221                Return(1)
222        endif
223       
224        list = str
225       
226        //force an update If the SVAR exists, then the panel does too - MRED cleans up after itself when done
227        DoWindow/F Multiple_Reduce_Panel                        //bring to front
228        MRedPopMenuProc("MRFilesPopup",0,"")            //parse the list, pop the menu
229       
230        return(0)
231End
232
233Proc FillEMPUsingSelection()
234        FillEMPFilenameWSelection("")
235End
236
237Proc GuessEveryTransFile(num)
238        Variable num=6
239        GuessAllTransFiles(num)
240End
241
242Proc GuessSelectedTransFiles(num)
243        Variable num=6
244        fGuessSelectedTransFiles(num)
245End
246
247Proc ClearSelectedTransAssignments()
248        ClearSelectedAssignments("")
249End
250
251Proc CreateRunNumList()
252        String/G rStr=""
253        rStr=RunNumberList()
254        Print "The list is stored in root:rStr"
255        print rStr
256End
257
258Proc TransList()
259        String/G rStr=""
260        rStr=RunNumberList()
261        rStr=isTransList(rStr)
262        print rStr
263End
264
265Proc ScatteringAtSDDList(sdd)
266        Variable sdd=13
267       
268        String/G rStr=""
269        rStr=RunNumberList()
270        rStr=removeTrans(rStr)
271        rStr=atSDDList(rStr,sdd)
272       
273        //for Igor 4, the search is case-sensitive, so use all permutations
274        // in Igor 5, use the proper flag in strsearch() inside FindStringInLabel()
275        rStr = RemoveEmptyBlocked(rStr,"EMPTY")
276//      rStr = RemoveEmptyBlocked(rStr,"Empty")
277//      rStr = RemoveEmptyBlocked(rStr,"empty")
278//      rStr = RemoveEmptyBlocked(rStr,"MT Cell")
279        rStr = RemoveEmptyBlocked(rStr,"MT CELL")
280//      rStr = RemoveEmptyBlocked(rStr,"mt cell")
281        rStr = RemoveEmptyBlocked(rStr,"BLOCKED")
282//      rStr = RemoveEmptyBlocked(rStr,"Blocked")
283//      rStr = RemoveEmptyBlocked(rStr,"blocked")
284       
285        print rStr
286End
287
288Proc FillMREDList()
289        setMREDFileList(rStr)
290        DoUpdate
291End
292
293
294//num passed in is the run number, as in the list
295// ii is the index of all of the files from the catalog
296//return will be -1 if string not found, >=0 if found
297//
298Function FindStringInLabel(num,findThisStr)
299        Variable num
300        String findThisStr
301       
302        Wave/T w = $"root:myGlobals:CatVSHeaderInfo:Labels"
303        Wave RunNumber = $"root:myGlobals:CatVSHeaderInfo:RunNumber"
304       
305        if( (WaveExists(w) == 0 ) ||  (WaveExists(RunNumber) == 0 ) )
306                return(0)
307        endif
308       
309        Variable pts=numpnts(RunNumber),ii,loc
310        for(ii=0;ii<pts;ii+=1)
311                if(RunNumber[ii] == num)
312//                      loc = strsearch(w[ii], findThisStr, 0)                  //Igor 4 version is case-sensitive
313                        loc = strsearch(w[ii], findThisStr, 0 ,2)               //2==case insensitive, but Igor 5 specific
314                        if(loc != -1)
315                                Print "Remove w[ii] = ",num,"  ",w[ii]
316                        endif
317                endif
318        endfor
319       
320        return(loc)             //return will be -1 if string not found, >=0 if found
321end
322
323//rStr is the global string, already atSDD (so there should be only one instance of
324// empty and one instance of blocked
325//
326//scan through the list, and remove those that are have "empty" or "blocked" in the label
327// or anything that is listed in StrToFind
328//
329Function/S RemoveEmptyBlocked(list,StrToFind)
330        String list,StrToFind
331       
332        Variable ii,num,temp
333        num=ItemsInList(list ,",")
334        for(ii=0;ii<num;ii+=1)
335                temp = str2num( StringFromList(ii, list ,",") )
336                if(FindStringInLabel(temp,StrToFind) != -1)
337                        list = RemoveFromList(num2str(temp),list,",")
338                        ii -= 1                 //item ii was just deleted (everything moves to fill in)
339                        num -= 1                // and the list is shorter now
340                endif
341        endfor
342        //print list
343        return(list)
344end
345
346// input is a single run number to remove from the list
347//  - typically EC and BN - before sending to MRED
348Proc RemoveRunFromList(remList)
349        String remList=""
350         
351        rStr = RemoveFromList(remList, rStr ,",")
352end
353
354
355//////////general folder utilities
356
357//prompts user to choose the local folder that contains the SANS Data
358//only one folder can be used, and its path is catPathName (and is a NAME, not a string)
359//this will overwrite the path selection
360//returns 1 if no path selected as error condition, or if user cancelled
361Function PickPath()
362       
363        //set the global string to the selected pathname
364        NewPath/O/M="pick the SANS data folder" catPathName
365        if(V_Flag != 0)
366                return(1)               //user cancelled
367        endif
368       
369        PathInfo/S catPathName
370        String dum = S_path
371        String alertStr = ""
372        alertStr = "You must set the path to Charlotte through a Mapped Network Drive, not through the Network Neighborhood"
373        //alertStr += "  Please see the manual for details."
374        if (V_flag == 0)
375                //path does not exist - no folder selected
376                String/G root:myGlobals:gCatPathStr = "no folder selected"
377                return(1)
378        else
379                //set the global to the path (as a string)
380                // need 4 \ since it is the escape character
381                if(cmpstr("\\\\",dum[0,1])==0)  //Windoze user going through network neighborhood
382                        DoAlert 0,alertStr
383                        KillPath catPathName
384                        return(1)
385                endif
386                String/G root:myGlobals:gCatPathStr = dum
387                // these are now set in theire respective procedures, since the folders don't exist yet!
388//              String/G root:myGlobals:Patch:gCatPathStr = dum //and the global used by Patch and Trans
389//              String/G root:myGlobals:TransHeaderInfo:gCatPathStr = dum       //and the global used by Patch and Trans
390                return(0)               //no error
391        endif
392End
393
394//a utility function that prompts the user for a file (of any type)
395//and returns the full path:name;vers string required to open the file
396//the file is NOT opened by this routine (/D flag)
397//a null string is returned if no file is selected
398//"msgStr" is the message displayed in the dialog, informing the user what
399//file is desired
400//
401Function/S PromptForPath(msgStr)
402        String msgStr
403        String fullPath
404        Variable refnum
405       
406        //this just asks for the filename, doesn't open the file
407        Open/D/R/T="????"/M=(msgStr) refNum
408        fullPath = S_FileName           //fname is the full path
409        //      Print refnum,fullPath
410       
411        //null string is returned in S_FileName if user cancelled, and is passed back to calling  function
412        Return(fullPath)
413End
414
415
416
417//procedure is not called from anywhere, for debugging purposes only
418//not for gerneral users, since it Kills data folders, requiring
419//re-initialization of the experiment
420//
421Proc ClearWorkFolders()
422
423        //not foolproof - will generage an error if any wavs, etc.. are in use.
424        KillDataFolder root:Packages:NIST:RAW
425        KillDataFolder root:Packages:NIST:SAM
426        KillDataFolder root:Packages:NIST:EMP
427        KillDataFolder root:Packages:NIST:BGD
428        KillDataFolder root:Packages:NIST:COR
429        KillDataFolder root:Packages:NIST:DIV
430        KillDataFolder root:Packages:NIST:MSK
431        KillDataFolder root:Packages:NIST:ABS
432        KillDataFolder root:Packages:NIST:CAL
433        SetDataFolder root:
434       
435End
436
437//not used - but potentially very useful for ensuring that old
438// data in work folders is not accidentally being used
439//
440Function ClearWorkFolder(type)
441        String type
442       
443        SetDataFolder $("root:Packages:NIST:"+type)
444        KillWaves/A/Z
445        KillStrings/A/Z
446        KillVariables/A/Z
447       
448        SetDataFolder root:
449End
450
451
452//procedure is not called from anywhere, for debugging purposes only
453//not for gerneral users, but could be useful in reducon clutter
454//
455Proc ClearRootFolder()
456
457        DoAlert 1,"Are you sure you want to delete everything from the root level?"
458        SetDataFolder root:
459        KillWaves/A/Z
460        KillStrings/A/Z
461        KillVariables/A/Z
462       
463End
464
465/////////string matching
466
467
468//the following is a WaveMetrics procedure from <StrMatchList>
469// MatchList(matchStr,list,sep)
470// Returns the items of the list whose items match matchStr
471// The lists are separated by the sep character, usually ";"
472//
473// matchStr may be something like "abc", in which case it is identical to CmpStr
474// matchStr may also be "*" to match anything, "abc*" to match anything starting with "abc",
475//      "*abc" to match anything ending with "abc".
476// matchStr may also begin with "!" to indicate a match to anything not matching the rest of
477//      the pattern.
478// At most one "*" and one "!" are allowed in matchStr, otherwise the results are not guaranteed.
479//
480Function/S MyMatchList(matchStr,list,sep)
481        String matchStr,list,sep
482        String item,outList=""
483        Variable n=strlen(list)
484        Variable en,st=0
485        do
486                en= strsearch(list,sep,st)
487                if( en < 0 )
488                        if( st < n-1 )
489                                en= n   // no trailing separator
490                                sep=""  // don't put sep in output, either
491                        else
492                                break   // no more items in list
493                        endif
494                endif
495                item=list[st,en-1]
496                if( MyStrMatch(matchStr,item) == 0 )
497                        outlist += item+sep
498                Endif
499                st=en+1
500        while (st < n ) // exit is by break, above
501        return outlist
502End
503
504//the following is a WaveMetrics procedure from <StrMatchList>
505// StrMatch(matchStr,str)
506// Returns 0 if the pattern in matchStr matches str, else it returns 1
507//
508// matchStr may be something like "abc", in which case it is identical to CmpStr
509// matchStr may also be "*" to match anything, "abc*" to match anything starting with "abc",
510//      "*abc" to match anything ending with "abc".
511// matchStr may also begin with "!" to indicate a match to anything not matching the rest of
512//      the pattern.
513// At most one "*" and one "!" are allowed in matchStr, otherwise the results are not guaranteed.
514//
515Function MyStrMatch(matchStr,str)
516        String matchStr,str
517        Variable match = 1              // 0 means match
518        Variable invert= strsearch(matchStr,"!",0) == 0
519        if( invert )
520                matchStr[0,0]=""        // remove the "!"
521        endif
522        Variable st=0,en=strlen(str)-1
523        Variable starPos= strsearch(matchStr,"*",0)
524        if( starPos >= 0 )      // have a star
525                if( starPos == 0 )      // at start
526                        matchStr[0,0]=""                                // remove star at start
527                else                                    // at end
528                        matchStr[starPos,999999]=""     // remove star and rest of (ignored, illegal) pattern
529                endif
530                Variable len=strlen(matchStr)
531                if( len > 0 )
532                        if(starPos == 0)        // star at start, match must be at end
533                                st=en-len+1
534                        else
535                                en=len-1        // star at end, match at start
536                        endif
537                else
538                        str=""  // so that "*" matches anything
539                endif
540        endif
541        match= !CmpStr(matchStr,str[st,en])==0  // 1 or 0
542        if( invert )
543                match= 1-match
544        endif
545        return match
546End
Note: See TracBrowser for help on using the repository browser.