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

Last change on this file since 779 was 693, checked in by srkline, 13 years ago

Corrected the identification of HFIR trans files

Added new includes for analysis and usans, with higher version numbers. the old ones now simply point to the new one. this provides a mechanism for identifying experiments that were generated and saved with an older version of the macros - the old include file will be present. All three packages now display an alert if the experiment was generated from asn old version of the macros.

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