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

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

Initial file shuffling, partially done.
Created 3 new files:
SANS_Utilities
NCNR_Utils
NCNR_DataReadWrite

(SANS_Utilites may eventually become NCNR-specific, but I'm not sure)

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