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

Last change on this file since 994 was 994, checked in by srkline, 7 years ago

more changes, lots of files.

File size: 17.9 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//given a filename of a SANS data filename of the form
112// name.anything
113//returns the name as a string without the ".fbdfasga" extension
114//
115// returns the input string if a "." can't be found (maybe it wasn't there)
116Function/S V_RemoveDotExtension(item)
117        String item
118        String invalid = item   //
119        Variable num=-1
120       
121        //find the "dot"
122        String runStr=""
123        Variable pos = strsearch(item,".",0)
124        if(pos == -1)
125                //"dot" not found
126                return (invalid)
127        else
128                //found, get all of the characters preceeding it
129                runStr = item[0,pos-1]
130                return (runStr)
131        Endif
132End
133
134//returns a string containing filename (WITHOUT the ;vers)
135//the input string is a full path to the file (Mac-style, still works on Win in IGOR)
136//with the folders separated by colons
137//
138// called by MaskUtils.ipf, ProtocolAsPanel.ipf, WriteQIS.ipf
139//
140Function/S V_GetFileNameFromPathNoSemi(fullPath)
141        String fullPath
142       
143        Variable offset1,offset2
144        String filename=""
145        //String PartialPath
146        offset1 = 0
147        do
148                offset2 = StrSearch(fullPath, ":", offset1)
149                if (offset2 == -1)                              // no more colons ?
150                        fileName = FullPath[offset1,strlen(FullPath) ]
151                        //PartialPath = FullPath[0, offset1-1]
152                        break
153                endif
154                offset1 = offset2+1
155        while (1)
156       
157        //remove version number from name, if it's there - format should be: filename;N
158        filename =  StringFromList(0,filename,";")              //returns null if error
159       
160        Return filename
161End
162
163//
164// -- this was copied directly, no changes , from PlotUtils_Macro_v40
165//
166// returns the path to the file, or null if the user cancelled
167// fancy use of optional parameters
168//
169// enforce short file names (25 characters)
170Function/S V_DoSaveFileDialog(msg,[fname,suffix])
171        String msg,fname,suffix
172        Variable refNum
173//      String message = "Save the file as"
174
175        if(ParamIsDefault(fname))
176//              Print "fname not supplied"
177                fname = ""
178        endif
179        if(ParamIsDefault(suffix))
180//              Print "suffix not supplied"
181                suffix = ""
182        endif
183       
184        String outputPath,tmpName,testStr
185        Variable badLength=0,maxLength=25,l1,l2
186       
187       
188        tmpName = fname + suffix
189       
190        do
191                badLength=0
192                Open/D/M=msg/T="????" refNum as tmpName         //OS will allow 255 characters, but then I can't read it back in!
193                outputPath = S_fileName
194               
195                testStr = ParseFilePath(0, outputPath, ":", 1, 0)               //just the filename
196                if(strlen(testStr)==0)
197                        break           //cancel, allow exit
198                endif
199                if(strlen(testStr) > maxLength)
200                        badlength = 1
201                        DoAlert 2,"File name is too long. Is\r"+testStr[0,maxLength-1]+"\rOK?"
202                        if(V_flag==3)
203                                outputPath = ""
204                                break
205                        endif
206                        if(V_flag==1)                   //my suggested name is OK, so trim the output
207                                badlength=0
208                                l1 = strlen(testStr)            //too long length
209                                l1 = l1-maxLength               //number to trim
210                                //Print outputPath
211                                l2=strlen(outputPath)
212                                outputPath = outputPath[0,l2-1-l1]
213                                //Print "modified  ",outputPath
214                        endif
215                        //if(V_flag==2)  do nothing, let it go around again
216                endif
217               
218        while(badLength)
219       
220        return outputPath
221End
222
223
224
225//
226// previous/next button needs these functions
227// as well as many other utilities that manipulate the data file names
228// and parse run numbers.
229//
230
231
232// TODO
233// x- load in the proper file
234// x- re-click the I(q) button
235// x- be sure that the globals are updated w/ filename
236// -- getting the file_name from the root: global is a poor choice.
237//     Need a better, more reliable solution than this
238// -- make a copy of "oldName" that is local and not the SVAR, as the SVAR changes
239//    when the next file is loaded in (if it's not in RawVSANS), resulting in a "skipped" file number
240//
241//displays next (or previous) file in series of run numbers
242//file is read from disk, if path is set and the file number is present
243//increment +1, adds 1 to run number, -1 subtracts one
244//
245// will automatically step a gap of 10 run numbers, but nothing larger. Don't want to loop too long
246// trying to find a file (frustrating), don't want to look past the end of the run numbers (waste)
247// -- may find a more elegant solution later.
248//
249Function V_LoadPlotAndDisplayRAW(increment)
250        Variable increment
251
252        Variable i,val
253        String filename,tmp,curFileName
254        //take the currently displayed RAW file
255        SVAR oldName = root:file_name
256        oldname = V_RemoveAllSpaces(oldname)            //
257        curFileName = oldName
258//      print oldName
259       
260        filename = oldname
261//      for (i = 0; i < abs(increment); i += 1)
262//              filename = GetPrevNextRawFile(filename,increment/abs(increment))
263//      endfor 
264        i = 1
265        val = increment
266        do
267//              print filename,val
268                filename = V_GetPrevNextRawFile(filename,val)
269//              print "new= ",filename
270               
271                val = i*increment
272                i+=1
273                tmp = ParseFilePath(0, filename, ":", 1, 0)
274
275//              print val,strlen(tmp),strlen(oldname)
276//              print cmpstr(tmp,oldname)
277
278                if(strlen(tmp) == 0)            //in some cases, a null string can be returned - handle gracefully
279                        return(0)
280                endif
281               
282        while( (cmpstr(tmp,curFileName) == 0) && i < 11)
283//      print filename
284       
285        // display the specified RAW data file
286        // this is the set of steps done in DisplayMainButtonProc(ctrlName) : ButtonControl
287        Variable err=   V_LoadHDF5Data(filename,"RAW")                  // load the data, set the global w/file name loaded
288//      Print "Load err = "+num2str(err)
289        if(!err)
290                SVAR hdfDF = root:file_name                     // last file loaded, may not be the safest way to pass
291                String folder = StringFromList(0,hdfDF,".")
292               
293                // this (in SANS) just passes directly to fRawWindowHook()
294                Execute "UpdateDisplayInformation(\"RAW\")"     // plot the data in whatever folder type
295               
296                FakeRestorePanelsButtonClick()          //so the panels display correctly
297               
298        endif
299
300        // TODO
301        // -- update the 1D plotting as needed. these are SANS calls (OK for now, but will need to be better)
302        //do the average and plot (either the default, or what is on the panel currently)
303        V_PlotData_Panel()
304       
305
306        return(0)
307End
308
309
310// Return the full path:filename that represents the previous or next file.
311// Input is current filename and increment.
312// Increment should be -1 or 1
313// -1 => previous file
314// 1 => next file
315Function/S V_GetPrevNextRawFile(curfilename, prevnext)
316        String curfilename
317        Variable prevnext
318
319        String filename
320       
321        //get the run number
322        Variable num = V_GetRunNumFromFile(curfilename)
323               
324        //find the next specified file by number
325        fileName = V_FindFileFromRunNumber(num+prevnext)
326
327        if(cmpstr(fileName,"")==0)
328                //null return, do nothing
329                fileName = V_FindFileFromRunNumber(num)         //returns the full path, not just curFileName
330        Endif
331
332        Return filename
333End
334
335
336//returns a string containing the full path to the file containing the
337//run number "num". The null string is returned if no valid file can be found
338//the path "catPathName" used and is hard-wired, will abort if this path does not exist
339//the file returned will be a RAW SANS data file, other types of files are
340//filtered out.
341//
342// called by Buttons.ipf and Transmission.ipf, and locally by parsing routines
343//
344Function/S V_FindFileFromRunNumber(num)
345        Variable num
346       
347        String fullName="",partialName="",item=""
348        //get list of raw data files in folder that match "num" (add leading zeros)
349        if( (num>9999) || (num<=0) )
350                Print "error in  FindFileFromRunNumber(num), file number too large or too small"
351                Return ("")
352        Endif
353        //make a four character string of the run number
354        String numStr=""
355        if(num<10)
356                numStr = "000"+num2str(num)
357        else
358                if(num<100)
359                        numStr = "00"+num2str(num)
360                else
361                        if(num<1000)
362                                numstr = "0"+num2str(num)
363                        else
364                                numStr = num2str(num)
365                        endif
366                Endif
367        Endif
368        //Print "numstr = ",numstr
369       
370        //make sure that path exists
371        PathInfo catPathName
372        String path = S_path
373        if (V_flag == 0)
374                Abort "folder path does not exist - use Pick Path button"
375        Endif
376        String list="",newList="",testStr=""
377       
378        list = IndexedFile(catPathName,-1,"????")       //get all files in folder
379        //find (the) one with the number in the run # location in the name
380        Variable numItems,ii,runFound,isRAW
381        numItems = ItemsInList(list,";")                //get the new number of items in the list
382        ii=0
383        do
384                //parse through the list in this order:
385                // 1 - does item contain run number (as a string) "TTTTTnnn.SAn_XXX_Tyyy"
386                // 2 - exclude by isRaw? (to minimize disk access)
387                item = StringFromList(ii, list  ,";" )
388                if(strlen(item) != 0)
389                        //find the run number, if it exists as a three character string
390                        testStr = V_GetRunNumStrFromFile(item)
391                        runFound= cmpstr(numStr,testStr)        //compare the three character strings, 0 if equal
392                        if(runFound == 0)
393                                //the run Number was found
394                                //build valid filename
395                                partialName = V_FindValidFileName(item)
396                                if(strlen(partialName) != 0)            //non-null return from FindValidFileName()
397                                        fullName = path + partialName
398                                        //check if RAW, if so,this must be the file!
399                                        isRAW = V_CheckIfRawData(fullName)
400                                        if(isRaw)
401                                                //stop here
402                                                return(fullname)
403                                        Endif
404                                Endif
405                        Endif
406                Endif
407                ii+=1
408        while(ii<numItems)              //process all items in list
409        Return ("")     //null return if file not found in list
410End
411
412//
413// TODO -- for VSANS Nexus files, how do I quickly identify if a file is
414//   RAW VSANS data? I don't want to generate any errors, but I want to quickly
415//   weed out the reduced data sets, etc. from file catalogs.
416//
417//function to test a binary file to see if it is a RAW binary SANS file
418//first checks the total bytes in the file (which for raw data is 33316 bytes)
419//**note that the "DIV" file will also show up as a raw file by the run field
420//should be listed in CAT/SHORT and in patch windows
421//
422//Function then checks the file fname (full path:file) for "RAW" run.type field
423//if not found, the data is not raw data and zero is returned
424//
425// called by many procedures (both external and local)
426//
427// TODO -- as was written by SANS, this function is expecting fname to be the path:fileName
428// - but are the V_get() functions OK with getting a full path, and what do they
429//  do when they fail? I don't want them to spit up another open file dialog
430//
431Function V_CheckIfRawData(fname)
432        String fname
433       
434        Variable refnum,totalBytes
435        String testStr=""
436       
437        testStr = V_getInstrumentName(fname)
438       
439        if(cmpstr(testStr,"") != 0)
440                //testStr exists, ASSUMING it's a raw VSANS data file
441                Return(1)
442        else
443                //some other file
444                Return(0)
445        Endif
446End
447
448
449Function V_GetRunNumFromFile(item)
450        String item
451       
452        String str = V_GetRunNumStrFromFile(item)
453       
454        return(str2num(str))
455end
456
457
458// TODO -- the file name structure for VSANS file is undecided
459// so some of these base functions will need to change
460//
461//given a filename of a VSANS data filename of the form
462// sansNNNN.nxs.ngv
463//returns the run number "NNNN" as a STRING of FOUR characters
464//returns "ABCD" as an invalid file number
465//
466// local function to aid in locating files by run number
467//
468Function/S V_GetRunNumStrFromFile(item)
469        String item
470        String invalid = "ABCD" //"ABCD" is not a valid run number, since it's text
471        Variable num=-1
472       
473        //find the "dot"
474        String runStr=""
475        Variable numChar = 4
476        Variable pos = strsearch(item,".",0)
477        if(pos == -1)
478                //"dot" not found
479                return (invalid)
480        else
481                //found, get the four characters preceeding it
482                if (pos <= numChar-1)
483                        //not enough characters
484                        return (invalid)
485                else
486                        runStr = item[pos-numChar,pos-1]
487                        return (runStr)
488                Endif
489        Endif
490End
491
492//Function attempts to find valid filename from partial name by checking for
493// the existence of the file on disk.
494// - checks as is
495// - strips spaces
496// - permutations of upper/lowercase
497//
498// added 11/99 - uppercase and lowercase versions of the file are tried, if necessary
499// since from marquee, the filename field (textread[0]) must be used, and can be a mix of                       //02JUL13
500// upper/lowercase letters, while the filename on the server (should) be all caps
501// now makes repeated calls to ValidFileString()
502//
503// returns a valid filename (No path prepended) or a null string
504//
505// called by any functions, both external and local
506//
507Function/S V_FindValidFilename(partialName)
508        String PartialName
509       
510        String retStr=""
511       
512        //try name with no changes - to allow for ABS files that have spaces in the names 12APR04
513        retStr = V_ValidFileString(partialName)
514        if(cmpstr(retStr,"") !=0)
515                //non-null return
516                return(retStr)
517        Endif
518       
519        //if the partial name is derived from the file header, there can be spaces at the beginning
520        //or in the middle of the filename - depending on the prefix and initials used
521        //
522        //remove any leading spaces from the name before starting
523        partialName = V_RemoveAllSpaces(partialName)
524       
525        //try name with no spaces
526        retStr = V_ValidFileString(partialName)
527        if(cmpstr(retStr,"") !=0)
528                //non-null return
529                return(retStr)
530        Endif
531       
532        //try all UPPERCASE
533        partialName = UpperStr(partialName)
534        retStr = V_ValidFileString(partialName)
535        if(cmpstr(retStr,"") !=0)
536                //non-null return
537                return(retStr)
538        Endif
539       
540        //try all lowercase (ret null if failure)
541        partialName = LowerStr(partialName)
542        retStr = V_ValidFileString(partialName)
543        if(cmpstr(retStr,"") !=0)
544                //non-null return
545                return(retStr)
546        else
547                return(retStr)
548        Endif
549End
550
551
552// Function checks for the existence of a file
553// partialName;vers (to account for VAX filenaming conventions)
554// The partial name is tried first with no version number
555//
556// *** the PATH is hard-wired to catPathName (which is assumed to exist)
557// version numers up to ;10 are tried
558// only the "name;vers" is returned if successful. The path is not prepended
559//
560// local function
561//
562// TODO -- is this really necessary anymore for the NON-VAX files of VSANS.
563// -- can this be made a pass-through, or will there be another function that is needed for VSANS?
564//
565Function/S V_ValidFileString(partialName)
566        String partialName
567       
568        String tempName = "",msg=""
569        Variable ii,refnum
570       
571        ii=0
572        do
573                if(ii==0)
574                        //first pass, try the partialName
575                        tempName = partialName
576                        Open/Z/R/T="????TEXT"/P=catPathName refnum tempName     //Does open file (/Z flag)
577                        if(V_flag == 0)
578                                //file exists
579                                Close refnum            //YES needed,
580                                break
581                        endif
582                else
583                        tempName = partialName + ";" + num2str(ii)
584                        Open/Z/R/T="????TEXT"/P=catPathName refnum tempName
585                        if(V_flag == 0)
586                                //file exists
587                                Close refnum
588                                break
589                        endif
590                Endif
591                ii+=1
592                //print "ii=",ii
593        while(ii<11)
594        //go get the selected bits of information, using tempName, which exists
595        if(ii>=11)
596                //msg = partialName + " not found. is version number > 11?"
597                //DoAlert 0, msg
598                //PathInfo catPathName
599                //Print S_Path
600                Return ("")             //use null string as error condition
601        Endif
602       
603        Return (tempName)
604End
605
606//function to remove all spaces from names when searching for filenames
607//the filename (as saved) will never have interior spaces (TTTTTnnn_AB _Bnnn)
608//but the text field in the header WILL, if less than 3 characters were used for the
609//user's initials, and can have leading spaces if prefix was less than 5 characters
610//
611//returns a string identical to the original string, except with the interior spaces removed
612//
613// local function for file name manipulation
614//
615Function/S V_RemoveAllSpaces(str)
616        String str
617       
618        String tempstr = str
619        Variable ii,spc,len             //should never be more than 2 or 3 trailing spaces in a filename
620        ii=0
621        do
622                len = strlen(tempStr)
623                spc = strsearch(tempStr," ",0)          //is the last character a space?
624                if (spc == -1)
625                        break           //no more spaces found, get out
626                endif
627                str = tempstr
628                tempStr = str[0,(spc-1)] + str[(spc+1),(len-1)] //remove the space from the string
629        While(1)        //should never be more than 2 or 3
630       
631        If(strlen(tempStr) < 1)
632                tempStr = ""            //be sure to return a null string if problem found
633        Endif
634       
635        //Print strlen(tempstr)
636       
637        Return(tempStr)
638               
639End
Note: See TracBrowser for help on using the repository browser.