source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/VSANS/V_Utilities_General.ipf @ 1003

Last change on this file since 1003 was 1003, checked in by srkline, 6 years ago

* updated the detector dead time constants in NCNR_Utils for the new NISTO hardware *

other changes were to the file catalog and patch files, which are still works in progress.

File size: 29.6 KB
Line 
1#pragma TextEncoding = "MacRoman"               // For details execute DisplayHelpTopic "The TextEncoding Pragma"
2#pragma rtGlobals=3             // Use modern global access method and strict wave access.
3
4//
5//              general utilities
6//
7// for use by multiple panels and packages
8
9
10//prompts user to choose the local folder that contains the SANS Data
11//only one folder can be used, and its path is catPathName (and is a NAME, not a string)
12//this will overwrite the path selection
13//returns 1 if no path selected as error condition, or if user cancelled
14Function V_PickPath()
15       
16        //set the global string to the selected pathname
17        NewPath/O/M="pick the SANS data folder" catPathName
18        if(V_Flag != 0)
19                return(1)               //user cancelled
20        endif
21       
22        PathInfo/S catPathName
23        String dum = S_path
24        String alertStr = ""
25        alertStr = "You must set the path to Charlotte through a Mapped Network Drive, not through the Network Neighborhood"
26        //alertStr += "  Please see the manual for details."
27        if (V_flag == 0)
28                //path does not exist - no folder selected
29                String/G root:Packges:NIST:VSANS:Globals:gCatPathStr = "no folder selected"
30                return(1)
31        else
32                //set the global to the path (as a string)
33                // need 4 \ since it is the escape character
34                if(cmpstr("\\\\",dum[0,1])==0)  //Windows user going through network neighborhood
35                        DoAlert 0,alertStr
36                        KillPath catPathName
37                        return(1)
38                endif
39                String/G root:Packges:NIST:VSANS:Globals:gCatPathStr = dum
40                return(0)               //no error
41        endif
42End
43
44//
45// entry from the Main Panel
46//
47Proc V_ChangeDisplay(type)
48        String type
49        Prompt type,"WORK data type to display",popup,"RAW;SAM;EMP;BGD;ADJ;"
50
51// make sure that data exists before passing this on...
52       
53        if(DataExists(type) > 0)
54                UpdateDisplayInformation(type)
55        else
56                DoAlert 0,"No data in "+type
57        endif
58End
59
60// TODO
61//
62// very simple function to look for something in a work folder
63// -- only checks for FR data to exist, assumes everything else is there
64// -- can't use the V_get() functions, these will try to load data if it's not there!
65Function DataExists(type)
66        String type
67       
68        Wave/Z w = $("root:Packages:NIST:VSANS:"+type+":entry:instrument:detector_FR:data")
69       
70        return(WaveExists(w))
71end
72//
73// tests if two values are close enough to each other
74// very useful since ICE came to be
75//
76// tol is an absolute value (since input v1 or v2 may be zero, can't reliably
77// use a percentage
78Function CloseEnough(v1,v2,tol)
79        Variable v1, v2, tol
80
81        if(abs(v1-v2) < tol)
82                return(1)
83        else
84                return(0)
85        endif
86End
87
88
89
90// TODO:
91// -- this must be called as needed to force a re-read of the data from disk
92//    "as needed" means that when an operation is done that needs to ensure
93//     a fresh read from disk, it must take care of the kill.
94// -- the ksBaseDFPath needs to be removed. It's currently pointing to RawVSANS, which is
95//    really not used as intended anymore.
96//
97Function V_KillNamedDataFolder(fname)
98        String fname
99       
100        Variable err=0
101       
102        String folderStr = V_GetFileNameFromPathNoSemi(fname)
103        folderStr = V_RemoveDotExtension(folderStr)
104       
105        KillDataFolder/Z $(ksBaseDFPath+folderStr)
106        err = V_flag
107       
108        return(err)
109end
110
111// TODO:
112// x- this still does not quite work. If there are no sub folders present in the RawVSANS folder
113//    it still thinks there are (1) item there.
114// -- if I replace the semicolon with a comma, it thinks there are two folders present and appears
115//    to delete the RawVSANS folder itself! seems very dangerous...this is because DataFolderDir returns
116//    a comma delimited list, but with a semicolon and \r at the end. need to remove these...
117//
118// NOTE -- use V_CleanupData_w_Progress(0,1) to get a progress bar - since this will take more than
119//     a few seconds to complete, especially if a file catalog was done, or a "batch" patching, etc.
120//
121Function V_CleanOutRawVSANS()
122
123        SetDataFolder root:Packages:NIST:VSANS:RawVSANS:
124       
125        // get a list of the data folders there
126        // kill them all if possible
127        String list,item
128        Variable numFolders,ii,pt
129       
130        list = DataFolderDir(1)
131        // this has FOLDERS: at the beginning and is comma-delimited
132        list = list[8,strlen(list)]
133        pt = strsearch(list,";",inf,1)
134        list = list[0,pt-1]                     //remove the ";\r" from the end of the string
135//      print list
136       
137        numFolders = ItemsInList(list , ",")
138//      Print List
139//      print strlen(list)
140
141        for(ii=0;ii<numFolders;ii+=1)
142                item = StringFromList(ii, list ,",")
143//              Print item
144                KillDataFolder/Z $(item)
145        endfor
146
147        list = DataFolderDir(1)
148        list = list[8,strlen(list)]
149        pt = strsearch(list,";",inf,1)
150        list = list[0,pt-1]
151        numFolders = ItemsInList(list, ",")
152        Printf "%g RawVSANS folders could not be killed\r",numFolders
153               
154        SetDataFolder root:
155        return(0)
156End
157
158//
159// examples straight from Wavemetrics help file topic "Progress Windows"
160// Try simpletest(0,0) and simpletest(1,0), simpletest(0,1) and simpletest(1,1)
161//
162//
163// look for simpletest() function in Wavemetrics help file topic "Progress Windows"
164//  this is a modified version.
165//
166// call with (1,1) to get the candystripe bar
167// call with (0,1) to the the "countdown" bar as they are killed
168//
169Function V_CleanupData_w_Progress(indefinite, useIgorDraw)
170        Variable indefinite
171        Variable useIgorDraw            // True to use Igor's own draw method rather than native
172       
173        Variable num
174       
175        // is there anything there to be killed?
176        num = V_CleanOutOneRawVSANS()
177        if(num <= 0)
178                return(0)
179        endif
180       
181        // there are some folders to kill, so proceed
182       
183        NewPanel /N=ProgressPanel /W=(285,111,739,193)
184        ValDisplay valdisp0,pos={18,32},size={342,18},limits={0,num,0},barmisc={0,0}
185        ValDisplay valdisp0,value= _NUM:0
186        DrawText 20,24,"Cleaning up old files... Please Wait..."
187       
188        if( indefinite )
189                ValDisplay valdisp0,mode= 4     // candy stripe
190        else
191                ValDisplay valdisp0,mode= 3     // bar with no fractional part
192        endif
193        if( useIgorDraw )
194                ValDisplay valdisp0,highColor=(15000,45535,15000)               //(0,65535,0)
195        endif
196        Button bStop,pos={375,32},size={50,20},title="Stop"
197        DoUpdate /W=ProgressPanel /E=1  // mark this as our progress window
198
199        do
200                num = V_CleanOutOneRawVSANS()
201                if( V_Flag == 2 || num == 0 || num == -1)       // either "stop" or clean exit, or "done" exit from function
202                        break
203                endif
204               
205                ValDisplay valdisp0,value= _NUM:num,win=ProgressPanel
206                DoUpdate /W=ProgressPanel
207        while(1)
208       
209
210        KillWindow ProgressPanel
211        return(0)
212End
213
214
215// TODO:
216// x- this still does not quite work. If there are no sub folders present in the RawVSANS folder
217//    it still thinks there are (1) item there.
218// -- if I replace the semicolon with a comma, it thinks there are two folders present and appears
219//    to delete the RawVSANS folder itself! seems very dangerous...this is because DataFolderDir returns
220//    a comma delimited list, but with a semicolon and \r at the end. need to remove these...
221//
222// -- for use with progress bar, kills only one folder, returns the new number of folders left
223// -- if n(in) = n(out), nothing was able to be killed, so return "done" code
224Function V_CleanOutOneRawVSANS()
225
226        SetDataFolder root:Packages:NIST:VSANS:RawVSANS:
227       
228        // get a list of the data folders there
229        // kill them all if possible
230        String list,item
231        Variable numFolders,ii,pt,numIn
232       
233        list = DataFolderDir(1)
234        // this has FOLDERS: at the beginning and is comma-delimited
235        list = list[8,strlen(list)]
236        pt = strsearch(list,";",inf,1)
237        list = list[0,pt-1]                     //remove the ";\r" from the end of the string
238//      print list
239       
240        numFolders = ItemsInList(list , ",")
241        numIn = numFolders
242//      Print List
243//      print strlen(list)
244
245        if(numIn > 0)
246                item = StringFromList(0, list ,",")
247//              Print item
248                KillDataFolder/Z $(item)
249        endif
250
251        list = DataFolderDir(1)
252        list = list[8,strlen(list)]
253        pt = strsearch(list,";",inf,1)
254        list = list[0,pt-1]
255        numFolders = ItemsInList(list, ",")
256       
257        if(numIn == numFolders)
258                Printf "%g RawVSANS folders could not be killed\r",numFolders
259                SetDataFolder root:
260
261                return (-1)
262        endif
263       
264        SetDataFolder root:     
265        return(numFolders)
266End
267
268
269
270
271
272//given a filename of a SANS data filename of the form
273// name.anything
274//returns the name as a string without the ".fbdfasga" extension
275//
276// returns the input string if a "." can't be found (maybe it wasn't there)
277Function/S V_RemoveDotExtension(item)
278        String item
279        String invalid = item   //
280        Variable num=-1
281       
282        //find the "dot"
283        String runStr=""
284        Variable pos = strsearch(item,".",0)
285        if(pos == -1)
286                //"dot" not found
287                return (invalid)
288        else
289                //found, get all of the characters preceeding it
290                runStr = item[0,pos-1]
291                return (runStr)
292        Endif
293End
294
295//returns a string containing filename (WITHOUT the ;vers)
296//the input string is a full path to the file (Mac-style, still works on Win in IGOR)
297//with the folders separated by colons
298//
299// called by MaskUtils.ipf, ProtocolAsPanel.ipf, WriteQIS.ipf
300//
301Function/S V_GetFileNameFromPathNoSemi(fullPath)
302        String fullPath
303       
304        Variable offset1,offset2
305        String filename=""
306        //String PartialPath
307        offset1 = 0
308        do
309                offset2 = StrSearch(fullPath, ":", offset1)
310                if (offset2 == -1)                              // no more colons ?
311                        fileName = FullPath[offset1,strlen(FullPath) ]
312                        //PartialPath = FullPath[0, offset1-1]
313                        break
314                endif
315                offset1 = offset2+1
316        while (1)
317       
318        //remove version number from name, if it's there - format should be: filename;N
319        filename =  StringFromList(0,filename,";")              //returns null if error
320       
321        Return filename
322End
323
324//
325// -- this was copied directly, no changes , from PlotUtils_Macro_v40
326//
327// returns the path to the file, or null if the user cancelled
328// fancy use of optional parameters
329//
330// enforce short file names (25 characters)
331Function/S V_DoSaveFileDialog(msg,[fname,suffix])
332        String msg,fname,suffix
333        Variable refNum
334//      String message = "Save the file as"
335
336        if(ParamIsDefault(fname))
337//              Print "fname not supplied"
338                fname = ""
339        endif
340        if(ParamIsDefault(suffix))
341//              Print "suffix not supplied"
342                suffix = ""
343        endif
344       
345        String outputPath,tmpName,testStr
346        Variable badLength=0,maxLength=25,l1,l2
347       
348       
349        tmpName = fname + suffix
350       
351        do
352                badLength=0
353                Open/D/M=msg/T="????" refNum as tmpName         //OS will allow 255 characters, but then I can't read it back in!
354                outputPath = S_fileName
355               
356                testStr = ParseFilePath(0, outputPath, ":", 1, 0)               //just the filename
357                if(strlen(testStr)==0)
358                        break           //cancel, allow exit
359                endif
360                if(strlen(testStr) > maxLength)
361                        badlength = 1
362                        DoAlert 2,"File name is too long. Is\r"+testStr[0,maxLength-1]+"\rOK?"
363                        if(V_flag==3)
364                                outputPath = ""
365                                break
366                        endif
367                        if(V_flag==1)                   //my suggested name is OK, so trim the output
368                                badlength=0
369                                l1 = strlen(testStr)            //too long length
370                                l1 = l1-maxLength               //number to trim
371                                //Print outputPath
372                                l2=strlen(outputPath)
373                                outputPath = outputPath[0,l2-1-l1]
374                                //Print "modified  ",outputPath
375                        endif
376                        //if(V_flag==2)  do nothing, let it go around again
377                endif
378               
379        while(badLength)
380       
381        return outputPath
382End
383
384
385
386//
387// previous/next button needs these functions
388// as well as many other utilities that manipulate the data file names
389// and parse run numbers.
390//
391
392
393// TODO
394// x- load in the proper file
395// x- re-click the I(q) button
396// x- be sure that the globals are updated w/ filename
397// -- getting the file_name from the root: global is a poor choice.
398//     Need a better, more reliable solution than this
399// -- make a copy of "oldName" that is local and not the SVAR, as the SVAR changes
400//    when the next file is loaded in (if it's not in RawVSANS), resulting in a "skipped" file number
401//
402//displays next (or previous) file in series of run numbers
403//file is read from disk, if path is set and the file number is present
404//increment +1, adds 1 to run number, -1 subtracts one
405//
406// will automatically step a gap of 10 run numbers, but nothing larger. Don't want to loop too long
407// trying to find a file (frustrating), don't want to look past the end of the run numbers (waste)
408// -- may find a more elegant solution later.
409//
410Function V_LoadPlotAndDisplayRAW(increment)
411        Variable increment
412
413        Variable i,val
414        String filename,tmp,curFileName
415        //take the currently displayed RAW file
416        SVAR oldName = root:file_name
417        oldname = V_RemoveAllSpaces(oldname)            //
418        curFileName = oldName
419//      print oldName
420       
421        filename = oldname
422//      for (i = 0; i < abs(increment); i += 1)
423//              filename = GetPrevNextRawFile(filename,increment/abs(increment))
424//      endfor 
425        i = 1
426        val = increment
427        do
428//              print filename,val
429                filename = V_GetPrevNextRawFile(filename,val)
430//              print "new= ",filename
431               
432                val = i*increment
433                i+=1
434                tmp = ParseFilePath(0, filename, ":", 1, 0)
435
436//              print val,strlen(tmp),strlen(oldname)
437//              print cmpstr(tmp,oldname)
438
439                if(strlen(tmp) == 0)            //in some cases, a null string can be returned - handle gracefully
440                        return(0)
441                endif
442               
443        while( (cmpstr(tmp,curFileName) == 0) && i < 11)
444//      print filename
445       
446        // display the specified RAW data file
447        // this is the set of steps done in DisplayMainButtonProc(ctrlName) : ButtonControl
448        Variable err=   V_LoadHDF5Data(filename,"RAW")                  // load the data, set the global w/file name loaded
449//      Print "Load err = "+num2str(err)
450        if(!err)
451                SVAR hdfDF = root:file_name                     // last file loaded, may not be the safest way to pass
452                String folder = StringFromList(0,hdfDF,".")
453               
454                // this (in SANS) just passes directly to fRawWindowHook()
455                Execute "UpdateDisplayInformation(\"RAW\")"     // plot the data in whatever folder type
456               
457                FakeRestorePanelsButtonClick()          //so the panels display correctly
458               
459        endif
460
461        // TODO
462        // -- update the 1D plotting as needed. these are SANS calls (OK for now, but will need to be better)
463        //do the average and plot (either the default, or what is on the panel currently)
464        V_PlotData_Panel()
465       
466
467        return(0)
468End
469
470
471// Return the full path:filename that represents the previous or next file.
472// Input is current filename and increment.
473// Increment should be -1 or 1
474// -1 => previous file
475// 1 => next file
476//
477// V_CheckIfRawData(fname)
478//
479Function/S V_GetPrevNextRawFile(curfilename, prevnext)
480        String curfilename
481        Variable prevnext
482
483        String filename
484       
485        //get the run number
486        Variable num = V_GetRunNumFromFile(curfilename)
487               
488        //find the next specified file by number
489        fileName = V_FindFileFromRunNumber(num+prevnext)
490
491        if(cmpstr(fileName,"")==0)
492                //null return, do nothing
493                fileName = V_FindFileFromRunNumber(num)         //returns the full path, not just curFileName
494        Endif
495
496        Return filename
497End
498
499
500//returns a string containing the full path to the file containing the
501//run number "num". The null string is returned if no valid file can be found
502//the path "catPathName" used and is hard-wired, will abort if this path does not exist
503//the file returned will be a RAW VSANS data file, other types of files are
504//filtered out.
505//
506// called by Buttons.ipf and Transmission.ipf, and locally by parsing routines
507//
508Function/S V_FindFileFromRunNumber(num)
509        Variable num
510       
511        String fullName="",partialName="",item=""
512        //get list of raw data files in folder that match "num" (add leading zeros)
513        if( (num>9999) || (num<=0) )
514                Print "error in  FindFileFromRunNumber(num), file number too large or too small"
515                Return ("")
516        Endif
517        //make a four character string of the run number
518        String numStr=""
519        if(num<10)
520                numStr = "000"+num2str(num)
521        else
522                if(num<100)
523                        numStr = "00"+num2str(num)
524                else
525                        if(num<1000)
526                                numstr = "0"+num2str(num)
527                        else
528                                numStr = num2str(num)
529                        endif
530                Endif
531        Endif
532        //Print "numstr = ",numstr
533       
534        //make sure that path exists
535        PathInfo catPathName
536        String path = S_path
537        if (V_flag == 0)
538                Abort "folder path does not exist - use Pick Path button"
539        Endif
540        String list="",newList="",testStr=""
541       
542        list = IndexedFile(catPathName,-1,"????")       //get all files in folder
543        //find (the) one with the number in the run # location in the name
544        Variable numItems,ii,runFound,isRAW
545        numItems = ItemsInList(list,";")                //get the new number of items in the list
546        ii=0
547        do
548                //parse through the list in this order:
549                // 1 - does item contain run number (as a string) "TTTTTnnn.SAn_XXX_Tyyy"
550                // 2 - exclude by isRaw? (to minimize disk access)
551                item = StringFromList(ii, list  ,";" )
552                if(strlen(item) != 0)
553                        //find the run number, if it exists as a three character string
554                        testStr = V_GetRunNumStrFromFile(item)
555                        runFound= cmpstr(numStr,testStr)        //compare the three character strings, 0 if equal
556                        if(runFound == 0)
557                                //the run Number was found
558                                //build valid filename
559                                partialName = V_FindValidFileName(item)
560                                if(strlen(partialName) != 0)            //non-null return from FindValidFileName()
561                                        fullName = path + partialName
562                                        //check if RAW, if so,this must be the file!
563                                        isRAW = V_CheckIfRawData(fullName)
564                                        if(isRaw)
565                                                //print "is raw, ",fullname
566                                                //stop here
567                                                return(fullname)
568                                        Endif
569                                Endif
570                        Endif
571                Endif
572                ii+=1
573        while(ii<numItems)              //process all items in list
574        Return ("")     //null return if file not found in list
575End
576
577//
578// TODO x- for VSANS Nexus files, how do I quickly identify if a file is
579//   RAW VSANS data? I don't want to generate any errors, but I want to quickly
580//   weed out the reduced data sets, etc. from file catalogs.
581//      (check the instrument name...)
582
583// TODO -- as was written by SANS, this function is expecting fname to be the path:fileName
584// - but are the V_get() functions OK with getting a full path, and what do they
585//  do when they fail? I don't want them to spit up another open file dialog
586//
587Function V_CheckIfRawData(fname)
588        String fname
589       
590        Variable refnum,totalBytes
591        String testStr=""
592       
593        testStr = V_getInstrumentName(fname)
594
595        if(cmpstr(testStr,"NG3-VSANS") == 0)
596                //testStr exists, ASSUMING it's a raw VSANS data file
597                Return(1)
598        else
599                //some other file
600                Return(0)
601        Endif
602End
603
604// TODO -- need to fill in correctly by determining this from the INTENT field
605//
606Function V_isTransFile(fname)
607        String fname
608       
609        Variable refnum,totalBytes
610        String testStr=""
611       
612//      testStr = V_getInstrumentName(fname)
613
614        if(cmpstr(testStr,"NG3-VSANS") == 0)            //wrong test
615                //testStr exists, ASSUMING it's a raw VSANS data file
616                Return(1)
617        else
618                //some other file
619                Return(0)
620        Endif
621End
622
623
624Function V_GetRunNumFromFile(item)
625        String item
626       
627        String str = V_GetRunNumStrFromFile(item)
628       
629        return(str2num(str))
630end
631
632
633// TODO -- the file name structure for VSANS file is undecided
634// so some of these base functions will need to change
635//
636//given a filename of a VSANS data filename of the form
637// sansNNNN.nxs.ngv
638//returns the run number "NNNN" as a STRING of FOUR characters
639//returns "ABCD" as an invalid file number
640//
641// local function to aid in locating files by run number
642//
643Function/S V_GetRunNumStrFromFile(item)
644        String item
645        String invalid = "ABCD" //"ABCD" is not a valid run number, since it's text
646        Variable num=-1
647       
648        //find the "dot"
649        String runStr=""
650        Variable numChar = 4
651        Variable pos = strsearch(item,".",0)
652        if(pos == -1)
653                //"dot" not found
654                return (invalid)
655        else
656                //found, get the four characters preceeding it
657                if (pos <= numChar-1)
658                        //not enough characters
659                        return (invalid)
660                else
661                        runStr = item[pos-numChar,pos-1]
662                        return (runStr)
663                Endif
664        Endif
665End
666
667//Function attempts to find valid filename from partial name by checking for
668// the existence of the file on disk.
669// - checks as is
670// - strips spaces
671// - permutations of upper/lowercase
672//
673// added 11/99 - uppercase and lowercase versions of the file are tried, if necessary
674// since from marquee, the filename field (textread[0]) must be used, and can be a mix of                       //02JUL13
675// upper/lowercase letters, while the filename on the server (should) be all caps
676// now makes repeated calls to ValidFileString()
677//
678// returns a valid filename (No path prepended) or a null string
679//
680// called by any functions, both external and local
681//
682Function/S V_FindValidFilename(partialName)
683        String PartialName
684       
685        String retStr=""
686       
687        //try name with no changes - to allow for ABS files that have spaces in the names 12APR04
688        retStr = V_ValidFileString(partialName)
689        if(cmpstr(retStr,"") !=0)
690                //non-null return
691                return(retStr)
692        Endif
693       
694        //if the partial name is derived from the file header, there can be spaces at the beginning
695        //or in the middle of the filename - depending on the prefix and initials used
696        //
697        //remove any leading spaces from the name before starting
698        partialName = V_RemoveAllSpaces(partialName)
699       
700        //try name with no spaces
701        retStr = V_ValidFileString(partialName)
702        if(cmpstr(retStr,"") !=0)
703                //non-null return
704                return(retStr)
705        Endif
706       
707        //try all UPPERCASE
708        partialName = UpperStr(partialName)
709        retStr = V_ValidFileString(partialName)
710        if(cmpstr(retStr,"") !=0)
711                //non-null return
712                return(retStr)
713        Endif
714       
715        //try all lowercase (ret null if failure)
716        partialName = LowerStr(partialName)
717        retStr = V_ValidFileString(partialName)
718        if(cmpstr(retStr,"") !=0)
719                //non-null return
720                return(retStr)
721        else
722                return(retStr)
723        Endif
724End
725
726
727// Function checks for the existence of a file
728// partialName;vers (to account for VAX filenaming conventions)
729// The partial name is tried first with no version number
730//
731// *** the PATH is hard-wired to catPathName (which is assumed to exist)
732// version numers up to ;10 are tried
733// only the "name;vers" is returned if successful. The path is not prepended
734//
735// local function
736//
737// TODO -- is this really necessary anymore for the NON-VAX files of VSANS.
738// -- can this be made a pass-through, or will there be another function that is needed for VSANS?
739//
740Function/S V_ValidFileString(partialName)
741        String partialName
742       
743        String tempName = "",msg=""
744        Variable ii,refnum
745       
746        ii=0
747        do
748                if(ii==0)
749                        //first pass, try the partialName
750                        tempName = partialName
751                        Open/Z/R/T="????TEXT"/P=catPathName refnum tempName     //Does open file (/Z flag)
752                        if(V_flag == 0)
753                                //file exists
754                                Close refnum            //YES needed,
755                                break
756                        endif
757                else
758                        tempName = partialName + ";" + num2str(ii)
759                        Open/Z/R/T="????TEXT"/P=catPathName refnum tempName
760                        if(V_flag == 0)
761                                //file exists
762                                Close refnum
763                                break
764                        endif
765                Endif
766                ii+=1
767                //print "ii=",ii
768        while(ii<11)
769        //go get the selected bits of information, using tempName, which exists
770        if(ii>=11)
771                //msg = partialName + " not found. is version number > 11?"
772                //DoAlert 0, msg
773                //PathInfo catPathName
774                //Print S_Path
775                Return ("")             //use null string as error condition
776        Endif
777       
778        Return (tempName)
779End
780
781//function to remove all spaces from names when searching for filenames
782//the filename (as saved) will never have interior spaces (TTTTTnnn_AB _Bnnn)
783//but the text field in the header WILL, if less than 3 characters were used for the
784//user's initials, and can have leading spaces if prefix was less than 5 characters
785//
786//returns a string identical to the original string, except with the interior spaces removed
787//
788// local function for file name manipulation
789//
790Function/S V_RemoveAllSpaces(str)
791        String str
792       
793        String tempstr = str
794        Variable ii,spc,len             //should never be more than 2 or 3 trailing spaces in a filename
795        ii=0
796        do
797                len = strlen(tempStr)
798                spc = strsearch(tempStr," ",0)          //is the last character a space?
799                if (spc == -1)
800                        break           //no more spaces found, get out
801                endif
802                str = tempstr
803                tempStr = str[0,(spc-1)] + str[(spc+1),(len-1)] //remove the space from the string
804        While(1)        //should never be more than 2 or 3
805       
806        If(strlen(tempStr) < 1)
807                tempStr = ""            //be sure to return a null string if problem found
808        Endif
809       
810        //Print strlen(tempstr)
811       
812        Return(tempStr)
813               
814End
815
816// returns a list of raw data files in the catPathName directory on disk
817// - list is SEMICOLON-delimited
818//
819// TODO: decide how to do this...
820// (1)
821// checks each file in the directory to see if it is a RAW data file by
822// call to V_CheckIfRawData() which currently looks for the instrument name in the file.
823// -- CON - this is excruciatingly slow, and by checking a field in the file, has to load in the
824//  ENTIRE data file, and will load EVERY file in the folder. ugh.
825//
826// (2)
827// as was done for VAX files, look for a specific string in the file name as written by the acquisition
828//  (was .saN), now key on ".nxs.ngv"?
829//
830// called by PatchFiles.ipf, Tile_2D.ipf
831//
832Function/S V_GetRawDataFileList()
833       
834        //make sure that path exists
835        PathInfo catPathName
836        if (V_flag == 0)
837                Abort "Folder path does not exist - use Pick Path button on Main Panel"
838        Endif
839        String path = S_Path
840       
841        String list=IndexedFile(catPathName,-1,"????")
842        String newList="",item="",validName="",fullName=""
843        Variable num=ItemsInList(list,";"),ii
844       
845        for(ii=0;ii<num;ii+=1)
846                item = StringFromList(ii, list  ,";")
847
848                validName = V_FindValidFileName(item)
849                if(strlen(validName) != 0)              //non-null return from FindValidFileName()
850                        fullName = path + validName             
851
852        //method (1)                   
853//                      if(V_CheckIfRawData(item))
854//                              newlist += item + ";"
855//                      endif
856
857        //method (2)                   
858                        if( stringmatch(item,"*.nxs.ngv*") )
859                                newlist += item + ";"
860                        endif
861
862                       
863                endif
864                //print "ii=",ii
865        endfor
866        newList = SortList(newList,";",0)
867        return(newList)
868End
869
870
871//the following is a WaveMetrics procedure from <StrMatchList>
872// MatchList(matchStr,list,sep)
873// Returns the items of the list whose items match matchStr
874// The lists are separated by the sep character, usually ";"
875//
876// matchStr may be something like "abc", in which case it is identical to CmpStr
877// matchStr may also be "*" to match anything, "abc*" to match anything starting with "abc",
878//      "*abc" to match anything ending with "abc".
879// matchStr may also begin with "!" to indicate a match to anything not matching the rest of
880//      the pattern.
881// At most one "*" and one "!" are allowed in matchStr, otherwise the results are not guaranteed.
882//
883Function/S V_MyMatchList(matchStr,list,sep)
884        String matchStr,list,sep
885        String item,outList=""
886        Variable n=strlen(list)
887        Variable en,st=0
888        do
889                en= strsearch(list,sep,st)
890                if( en < 0 )
891                        if( st < n-1 )
892                                en= n   // no trailing separator
893                                sep=""  // don't put sep in output, either
894                        else
895                                break   // no more items in list
896                        endif
897                endif
898                item=list[st,en-1]
899                if( V_MyStrMatch(matchStr,item) == 0 )
900                        outlist += item+sep
901                Endif
902                st=en+1
903        while (st < n ) // exit is by break, above
904        return outlist
905End
906
907//the following is a WaveMetrics procedure from <StrMatchList>
908// StrMatch(matchStr,str)
909// Returns 0 if the pattern in matchStr matches str, else it returns 1
910//
911// matchStr may be something like "abc", in which case it is identical to CmpStr
912// matchStr may also be "*" to match anything, "abc*" to match anything starting with "abc",
913//      "*abc" to match anything ending with "abc".
914// matchStr may also begin with "!" to indicate a match to anything not matching the rest of
915//      the pattern.
916// At most one "*" and one "!" are allowed in matchStr, otherwise the results are not guaranteed.
917//
918Function V_MyStrMatch(matchStr,str)
919        String matchStr,str
920        Variable match = 1              // 0 means match
921        Variable invert= strsearch(matchStr,"!",0) == 0
922        if( invert )
923                matchStr[0,0]=""        // remove the "!"
924        endif
925        Variable st=0,en=strlen(str)-1
926        Variable starPos= strsearch(matchStr,"*",0)
927        if( starPos >= 0 )      // have a star
928                if( starPos == 0 )      // at start
929                        matchStr[0,0]=""                                // remove star at start
930                else                                    // at end
931                        matchStr[starPos,999999]=""     // remove star and rest of (ignored, illegal) pattern
932                endif
933                Variable len=strlen(matchStr)
934                if( len > 0 )
935                        if(starPos == 0)        // star at start, match must be at end
936                                st=en-len+1
937                        else
938                                en=len-1        // star at end, match at start
939                        endif
940                else
941                        str=""  // so that "*" matches anything
942                endif
943        endif
944        match= !CmpStr(matchStr,str[st,en])==0  // 1 or 0
945        if( invert )
946                match= 1-match
947        endif
948        return match
949End
950
951
952//input is a list of run numbers, and output is a list of filenames (not the full path)
953//*** input list must be COMMA delimited***
954//output is equivalent to selecting from the CAT table
955//if some or all of the list items are valid filenames, keep them...
956//if an error is encountered, notify of the offending element and return a null list
957//
958//output is COMMA delimited
959//
960// this routine is expecting that the "ask", "none" special cases are handled elsewhere
961//and not passed here
962//
963// called by Marquee.ipf, MultipleReduce.ipf, ProtocolAsPanel.ipf
964//
965Function/S ParseRunNumberList(list)
966        String list
967       
968        String newList="",item="",tempStr=""
969        Variable num,ii,runNum
970       
971        //expand number ranges, if any
972        list = V_ExpandNumRanges(list)
973       
974        num=itemsinlist(list,",")
975       
976        for(ii=0;ii<num;ii+=1)
977                //get the item
978                item = StringFromList(ii,list,",")
979                //is it already a valid filename?
980                tempStr=V_FindValidFilename(item) //returns filename if good, null if error
981                if(strlen(tempstr)!=0)
982                        //valid name, add to list
983                        //Print "it's a file"
984                        newList += tempStr + ","
985                else
986                        //not a valid name
987                        //is it a number?
988                        runNum=str2num(item)
989                        //print runnum
990                        if(numtype(runNum) != 0)
991                                //not a number -  maybe an error                       
992                                DoAlert 0,"List item "+item+" is not a valid run number or filename. Please enter a valid number or filename."
993                                return("")
994                        else
995                                //a run number or an error
996                                tempStr = V_GetFileNameFromPathNoSemi( V_FindFileFromRunNumber(runNum) )
997                                if(strlen(tempstr)==0)
998                                        //file not found, error
999                                        DoAlert 0,"List item "+item+" is not a valid run number. Please enter a valid number."
1000                                        return("")
1001                                else
1002                                        newList += tempStr + ","
1003                                endif
1004                        endif
1005                endif
1006        endfor          //loop over all items in list
1007       
1008        return(newList)
1009End
1010
1011//takes a comma delimited list that MAY contain number range, and
1012//expands any range of run numbers into a comma-delimited list...
1013//and returns the new list - if not a range, return unchanged
1014//
1015// local function
1016//
1017Function/S V_ExpandNumRanges(list)
1018        String list
1019       
1020        String newList="",dash="-",item,str
1021        Variable num,ii,hasDash
1022       
1023        num=itemsinlist(list,",")
1024//      print num
1025        for(ii=0;ii<num;ii+=1)
1026                //get the item
1027                item = StringFromList(ii,list,",")
1028                //does it contain a dash?
1029                hasDash = strsearch(item,dash,0)                //-1 if no dash found
1030                if(hasDash == -1)
1031                        //not a range, keep it in the list
1032                        newList += item + ","
1033                else
1034                        //has a dash (so it's a range), expand (or add null)
1035                        newList += V_ListFromDash(item)         
1036                endif
1037        endfor
1038       
1039        return newList
1040End
1041
1042//be sure to add a trailing comma to the return string...
1043//
1044// local function
1045//
1046Function/S V_ListFromDash(item)
1047        String item
1048       
1049        String numList="",loStr="",hiStr=""
1050        Variable lo,hi,ii
1051       
1052        loStr=StringFromList(0,item,"-")        //treat the range as a list
1053        hiStr=StringFromList(1,item,"-")
1054        lo=str2num(loStr)
1055        hi=str2num(hiStr)
1056        if( (numtype(lo) != 0) || (numtype(hi) !=0 ) || (lo > hi) )
1057                numList=""
1058                return numList
1059        endif
1060        for(ii=lo;ii<=hi;ii+=1)
1061                numList += num2str(ii) + ","
1062        endfor
1063       
1064        Return numList
1065End
1066
Note: See TracBrowser for help on using the repository browser.