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

Last change on this file was 1249, checked in by srkline, 3 years ago

minor changes - can't find the diff command in TortoiseSVN...

File size: 43.7 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#pragma IgorVersion = 7.00
4
5//
6//              general utilities
7//
8// for use by multiple panels and packages
9//
10
11
12//prompts user to choose the local folder that contains the VSANS Data
13//only one folder can be used, and its path is catPathName (and is a NAME, not a string)
14//this will overwrite the path selection
15//returns 1 if no path selected as error condition, or if user cancelled
16Function V_PickPath()
17       
18        //set the global string to the selected pathname
19        NewPath/O/M="pick the SANS data folder" catPathName
20        if(V_Flag != 0)
21                return(1)               //user cancelled
22        endif
23       
24        PathInfo/S catPathName
25        String dum = S_path
26        String alertStr = ""
27        alertStr = "You must set the path to Charlotte through a Mapped Network Drive, not through the Network Neighborhood"
28        //alertStr += "  Please see the manual for details."
29        if (V_flag == 0)
30                //path does not exist - no folder selected
31                String/G root:Packges:NIST:VSANS:Globals:gCatPathStr = "no folder selected"
32                return(1)
33        else
34        // SRK 2016, for windows 10, try to eliminate this restriction 
35        //---- connecting through the network neighborhood seems to be perfectly fine except for
36        //     path issues with GBLoadWave, which only affects VAX data sets
37               
38//              print igorinfo(3)
39//              //set the global to the path (as a string)
40//              // need 4 \ since it is the escape character
41//              if(cmpstr("\\\\",dum[0,1])==0)  //Windows user going through network neighborhood
42//                      DoAlert 0,alertStr
43//                      KillPath catPathName
44//                      return(1)
45//              endif
46                String/G root:Packges:NIST:VSANS:Globals:gCatPathStr = dum
47                return(0)               //no error
48        endif
49       
50End
51
52//
53// entry from the Main Panel
54//
55Proc V_ChangeDisplay(type)
56        String type
57        Prompt type,"WORK data type to display",popup,"RAW;SAM;EMP;BGD;COR;ABS;STO;SUB;ADJ;"
58
59// make sure that data exists before passing this on...
60       
61        if(V_DataExists(type) > 0)
62                V_UpdateDisplayInformation(type)
63        else
64                DoAlert 0,"No data in "+type
65        endif
66End
67
68//
69//
70// very simple function to look for something in a work folder
71// -- only checks for FR data to exist, assumes everything else is there
72// -- can't use the V_get() functions, these will try to load data if it's not there!
73Function V_DataExists(type)
74        String type
75       
76        Wave/Z w = $("root:Packages:NIST:VSANS:"+type+":entry:instrument:detector_FR:data")
77       
78        return(WaveExists(w))
79end
80
81
82
83// (DONE):
84// x- this must be called as needed to force a re-read of the data from disk
85//    "as needed" means that when an operation is done that needs to ensure
86//     a fresh read from disk, it must take care of the kill.
87//
88// ksBaseDFPath points to RawVSANS
89//
90//
91Function V_KillNamedDataFolder(fname)
92        String fname
93       
94        Variable err=0
95       
96        String folderStr = V_GetFileNameFromPathNoSemi(fname)
97        folderStr = V_RemoveDotExtension(folderStr)
98       
99        KillDataFolder/Z $(ksBaseDFPath+folderStr)
100        err = V_flag
101       
102        return(err)
103end
104
105// (DONE)
106// x- this still does not quite work. If there are no sub folders present in the RawVSANS folder
107//    it still thinks there is (1) item there.
108// x- if I replace the semicolon with a comma, it thinks there are two folders present and appears
109//    to delete the RawVSANS folder itself! seems very dangerous...this is because DataFolderDir returns
110//    a comma delimited list, but with a semicolon and \r at the end. need to remove these...
111//
112// NOTE -- use V_CleanupData_w_Progress(0,1) to get a progress bar - since this will take more than
113//     a few seconds to complete, especially if a file catalog was done, or a "batch" patching, etc.
114//
115// *** this appears to be unused, in favor of V_CleanupData_w_Progress(0,1)  **********
116//
117Function V_CleanOutRawVSANS()
118
119        SetDataFolder root:Packages:NIST:VSANS:RawVSANS:
120       
121        // get a list of the data folders there
122        // kill them all if possible
123        String list,item
124        Variable numFolders,ii,pt
125       
126        list = DataFolderDir(1)
127        // this has FOLDERS: at the beginning and is comma-delimited
128        list = list[8,strlen(list)]
129        pt = strsearch(list,";",inf,1)
130        list = list[0,pt-1]                     //remove the ";\r" from the end of the string
131//      print list
132       
133        numFolders = ItemsInList(list , ",")
134//      Print List
135//      print strlen(list)
136
137        for(ii=0;ii<numFolders;ii+=1)
138                item = StringFromList(ii, list ,",")
139//              Print item
140                KillDataFolder/Z $(item)
141        endfor
142
143        list = DataFolderDir(1)
144        list = list[8,strlen(list)]
145        pt = strsearch(list,";",inf,1)
146        list = list[0,pt-1]
147        numFolders = ItemsInList(list, ",")
148        Printf "%g RawVSANS folders could not be killed\r",numFolders
149               
150        SetDataFolder root:
151        return(0)
152End
153
154//
155// examples straight from Wavemetrics help file topic "Progress Windows"
156// Try simpletest(0,0) and simpletest(1,0), simpletest(0,1) and simpletest(1,1)
157//
158//
159// look for simpletest() function in Wavemetrics help file topic "Progress Windows"
160//  this is a modified version.
161//
162// call with (1,1) to get the candystripe bar
163// call with (0,1) to the the "countdown" bar as they are killed
164//
165Function V_CleanupData_w_Progress(indefinite, useIgorDraw)
166        Variable indefinite
167        Variable useIgorDraw            // True to use Igor's own draw method rather than native
168       
169        Variable num,numToClean
170       
171        // is there anything there to be killed?
172        num = V_CleanOutOneRawVSANS()
173        numToClean = num
174        if(num <= 0)
175                return(0)
176        endif
177       
178        // there are some folders to kill, so proceed
179       
180        Variable sc = 1
181       
182        NVAR gLaptopMode = root:Packages:NIST:VSANS:Globals:gLaptopMode
183               
184        if(gLaptopMode == 1)
185                sc = 0.7
186        endif
187       
188        NewPanel /N=ProgressPanel /W=(285*sc,111*sc,739*sc,193*sc)
189        ValDisplay valdisp0,win=ProgressPanel,pos={sc*18,32*sc},size={sc*342,18*sc},limits={0,num,0},barmisc={0,0}
190        ValDisplay valdisp0,win=ProgressPanel,value= _NUM:0
191        DrawText 20*sc,24*sc,"Cleaning up old files... Please Wait..."
192       
193        if( indefinite )
194                ValDisplay valdisp0,win=ProgressPanel,mode= 4   // candy stripe
195        else
196                ValDisplay valdisp0,win=ProgressPanel,mode= 3   // bar with no fractional part
197        endif
198        if( useIgorDraw )
199                ValDisplay valdisp0,win=ProgressPanel,highColor=(15000,45535,15000)             //(0,65535,0)
200        endif
201        Button bStop,win=ProgressPanel,pos={sc*375,32*sc},size={sc*50,20*sc},title="Stop"
202        DoUpdate /W=ProgressPanel /E=1  // mark this as our progress window
203
204        do
205                num = V_CleanOutOneRawVSANS()
206                if( V_Flag == 2 || num == 0 || num == -1)       // either "stop" or clean exit, or "done" exit from function
207                        break
208                endif
209               
210                ValDisplay valdisp0,win=ProgressPanel,value= _NUM:num
211                DoUpdate /W=ProgressPanel
212        while(1)
213       
214
215        KillWindow ProgressPanel
216        return(numToClean)
217End
218
219
220//
221// x- this still does not quite work. If there are no sub folders present in the RawVSANS folder
222//    it still thinks there is (1) item there.
223// x- if I replace the semicolon with a comma, it thinks there are two folders present and appears
224//    to delete the RawVSANS folder itself! seems very dangerous...this is because DataFolderDir returns
225//    a comma delimited list, but with a semicolon and \r at the end. need to remove these...
226//
227// x- for use with progress bar, kills only one folder, returns the new number of folders left
228// x- if n(in) = n(out), nothing was able to be killed, so return "done" code
229Function V_CleanOutOneRawVSANS()
230
231        SetDataFolder root:Packages:NIST:VSANS:RawVSANS:
232       
233        // get a list of the data folders there
234        // kill them all if possible
235        String list,item
236        Variable numFolders,ii,pt,numIn
237       
238        list = DataFolderDir(1)
239        // this has FOLDERS: at the beginning and is comma-delimited
240        list = list[8,strlen(list)]
241        pt = strsearch(list,";",inf,1)
242        list = list[0,pt-1]                     //remove the ";\r" from the end of the string
243//      print list
244       
245        numFolders = ItemsInList(list , ",")
246        numIn = numFolders
247//      Print List
248//      print strlen(list)
249
250        if(numIn > 0)
251                item = StringFromList(0, list ,",")
252//              Print item
253                KillDataFolder/Z $(item)
254        endif
255
256        list = DataFolderDir(1)
257        list = list[8,strlen(list)]
258        pt = strsearch(list,";",inf,1)
259        list = list[0,pt-1]
260        numFolders = ItemsInList(list, ",")
261       
262        if(numIn == numFolders)
263                Printf "%g RawVSANS folders could not be killed\r",numFolders
264                SetDataFolder root:
265
266                return (-1)
267        endif
268       
269        SetDataFolder root:     
270        return(numFolders)
271End
272
273
274
275
276
277//given a filename of a SANS data filename of the form
278// name.anything
279//returns the name as a string without the ".fbdfasga" extension
280//
281// returns the input string if a "." can't be found (maybe it wasn't there)
282Function/S V_RemoveDotExtension(item)
283        String item
284        String invalid = item   //
285        Variable num=-1
286       
287        //find the "dot"
288        String runStr=""
289        Variable pos = strsearch(item,".",0)
290        if(pos == -1)
291                //"dot" not found
292                return (invalid)
293        else
294                //found, get all of the characters preceeding it
295                runStr = item[0,pos-1]
296                return (runStr)
297        Endif
298End
299
300//returns a string containing filename (WITHOUT the ;vers)
301//the input string is a full path to the file (Mac-style, still works on Win in IGOR)
302//with the folders separated by colons
303//
304// called by MaskUtils.ipf, ProtocolAsPanel.ipf, WriteQIS.ipf
305//
306Function/S V_GetFileNameFromPathNoSemi(fullPath)
307        String fullPath
308       
309        Variable offset1,offset2
310        String filename=""
311        //String PartialPath
312        offset1 = 0
313        do
314                offset2 = StrSearch(fullPath, ":", offset1)
315                if (offset2 == -1)                              // no more colons ?
316                        fileName = FullPath[offset1,strlen(FullPath) ]
317                        //PartialPath = FullPath[0, offset1-1]
318                        break
319                endif
320                offset1 = offset2+1
321        while (1)
322       
323        //remove version number from name, if it's there - format should be: filename;N
324        filename =  StringFromList(0,filename,";")              //returns null if error
325       
326        Return filename
327End
328
329//
330// -- this was copied directly, no changes , from PlotUtils_Macro_v40
331//
332// returns the path to the file, or null if the user cancelled
333// fancy use of optional parameters
334//
335// enforce short file names (25 characters)
336Function/S V_DoSaveFileDialog(msg,[fname,suffix])
337        String msg,fname,suffix
338        Variable refNum
339//      String message = "Save the file as"
340
341        if(ParamIsDefault(fname))
342//              Print "fname not supplied"
343                fname = ""
344        endif
345        if(ParamIsDefault(suffix))
346//              Print "suffix not supplied"
347                suffix = ""
348        endif
349       
350        String outputPath,tmpName,testStr
351        Variable badLength=0,maxLength=25,l1,l2
352       
353       
354        tmpName = fname + suffix
355       
356        do
357                badLength=0
358                Open/D/M=msg/T="????" refNum as tmpName         //OS will allow 255 characters, but then I can't read it back in!
359                outputPath = S_fileName
360               
361                testStr = ParseFilePath(0, outputPath, ":", 1, 0)               //just the filename
362                if(strlen(testStr)==0)
363                        break           //cancel, allow exit
364                endif
365                if(strlen(testStr) > maxLength)
366                        badlength = 1
367                        DoAlert 2,"File name is too long. Is\r"+testStr[0,maxLength-1]+"\rOK?"
368                        if(V_flag==3)
369                                outputPath = ""
370                                break
371                        endif
372                        if(V_flag==1)                   //my suggested name is OK, so trim the output
373                                badlength=0
374                                l1 = strlen(testStr)            //too long length
375                                l1 = l1-maxLength               //number to trim
376                                //Print outputPath
377                                l2=strlen(outputPath)
378                                outputPath = outputPath[0,l2-1-l1]
379                                //Print "modified  ",outputPath
380                        endif
381                        //if(V_flag==2)  do nothing, let it go around again
382                else
383                        // length is OK, set the output path, exit loop and return
384                        outputPath = testStr
385                endif
386               
387        while(badLength)
388       
389        return outputPath
390End
391
392
393
394//
395// this will only load the data into RAW, overwriting whatever is there. no copy is put in rawVSANS
396//
397Function V_LoadAndPlotRAW_wName(fname)
398        String fname
399
400        Variable err=   V_LoadHDF5Data(fname,"RAW")                     // load the data
401//      Print "Load err = "+num2str(err)
402        if(!err)
403                SVAR hdfDF = root:file_name                     // last file loaded, may not be the safest way to pass
404                String folder = StringFromList(0,hdfDF,".")
405               
406                // this (in SANS) just passes directly to fRawWindowHook()
407                V_UpdateDisplayInformation("RAW")               // plot the data in whatever folder type
408                               
409                // set the global to display ONLY if the load was called from here, not from the
410                // other routines that load data (to read in values)
411                SVAR gLastFile =        root:Packages:NIST:VSANS:Globals:gLastLoadedFile
412                gLastFile = hdfDF
413        endif
414End
415
416
417
418//
419// previous/next button needs these functions
420// as well as many other utilities that manipulate the data file names
421// and parse run numbers.
422//
423
424
425// TODO
426// -- getting the file_name from the root: global is a poor choice.
427//     Need a better, more reliable solution than this
428//
429// DONE
430// x- load in the proper file
431// x- re-click the I(q) button
432// x- be sure that the globals are updated w/ filename
433// x- make a copy of "oldName" that is local and not the SVAR, as the SVAR changes
434//    when the next file is loaded in (if it's not in RawVSANS), resulting in a "skipped" file number
435//
436//displays next (or previous) file in series of run numbers
437//file is read from disk, if path is set and the file number is present
438//increment +1, adds 1 to run number, -1 subtracts one
439//
440// will automatically step a gap of 10 run numbers, but nothing larger. Don't want to loop too long
441// trying to find a file (frustrating), don't want to look past the end of the run numbers (waste)
442// -- may find a more elegant solution later.
443//
444Function V_LoadPlotAndDisplayRAW(increment)
445        Variable increment
446
447        Variable i,val
448        String filename,tmp,curFileName,str
449        //take the currently displayed RAW file
450        SVAR oldName = root:Packages:NIST:VSANS:Globals:gLastLoadedFile
451        oldname = V_RemoveAllSpaces(oldname)            //
452        curFileName = oldName
453       
454        filename = oldname
455
456        i = 1
457        val = increment
458        do
459
460                val = i*increment
461                filename = V_GetPrevNextRawFile(oldName,val)
462               
463//              printf "i=%d, file=%s\r",i,filename
464               
465                tmp = ParseFilePath(0, filename, ":", 1, 0)
466                               
467                i+=1
468        while( strlen(tmp) == 0 && i < 11)
469
470
471        if(strlen(tmp) == 0)            //in some cases, a null string can be returned - handle gracefully
472                return(0)
473        endif
474       
475        // display the specified RAW data file
476        // this is the set of steps done in DisplayMainButtonProc(ctrlName) : ButtonControl
477        Variable err=   V_LoadHDF5Data(filename,"RAW")                  // load the data, set the global w/file name loaded
478//      Print "Load err = "+num2str(err)
479        if(!err)
480                SVAR hdfDF = root:file_name                     // last file loaded, may not be the safest way to pass
481                String folder = StringFromList(0,hdfDF,".")
482               
483                // this (in SANS) just passes directly to fRawWindowHook()
484                V_UpdateDisplayInformation("RAW")       // plot the data in whatever folder type
485               
486                // set the global to display ONLY if the load was called from here, not from the
487                // other routines that load data (to read in values)
488                SVAR gLastLoad = root:Packages:NIST:VSANS:Globals:gLastLoadedFile
489                gLastLoad = hdfDF
490        endif
491
492        //
493        // x- update the 1D plotting as needed. these are SANS calls (OK for now, but will need to be better)
494        //do the average and plot (either the default, or what is on the panel currently)
495        SVAR type = root:Packages:NIST:VSANS:Globals:gCurDispType
496        type = "RAW"
497        V_PlotData_Panel()              // read the binType from the panel
498        Variable binType = V_GetBinningPopMode()
499        ControlInfo/W=V_1D_Data popup0
500        V_BinningModePopup("",binType,S_Value)          // does default circular binning and updates the graph
501
502
503        return(0)
504End
505
506
507// Return the full path:filename that represents the previous or next file.
508// Input is current filename and increment.
509// Increment should be -1 or 1
510// -1 => previous file
511// 1 => next file
512//
513// V_CheckIfRawData(fname)
514//
515Function/S V_GetPrevNextRawFile(curfilename, prevnext)
516        String curfilename
517        Variable prevnext
518
519        String filename
520       
521        //get the run number
522        Variable num = V_GetRunNumFromFile(curfilename)
523               
524        //find the next specified file by number
525        fileName = V_FindFileFromRunNumber(num+prevnext)
526
527        Return filename
528End
529
530
531//returns a string containing the full path to the file containing the
532//run number "num". The null string is returned if no valid file can be found
533//the path "catPathName" used and is hard-wired, will abort if this path does not exist
534//the file returned will be a RAW VSANS data file, other types of files are
535//filtered out.
536//
537//
538// -- with the run numbers incrementing from 1, there is no need to add leading zeros to the
539//    file names. simply add the number and go.
540//
541// called by Buttons.ipf and Transmission.ipf, and locally by parsing routines
542//
543Function/S V_FindFileFromRunNumber(num)
544        Variable num
545       
546        String fullName="",partialName="",item=""
547        //get list of raw data files in folder that match "num"
548
549        String numStr=""
550        numStr = num2istr(num)
551
552        //make sure that path exists
553        PathInfo catPathName
554        String path = S_path
555        if (V_flag == 0)
556                Abort "folder path does not exist - use Pick Path button"
557        Endif
558        String list="",newList="",testStr=""
559       
560        list = IndexedFile(catPathName,-1,"????")       //get all files in folder
561        //find (the) one with the number in the run # location in the name
562        Variable numItems,ii,runFound,isRAW
563        numItems = ItemsInList(list,";")                //get the new number of items in the list
564        ii=0
565        do
566                //parse through the list in this order:
567                // 1 - does item contain run number (as a string) "TTTTTnnn.SAn_XXX_Tyyy"
568                // 2 - exclude by isRaw? (to minimize disk access)
569                item = StringFromList(ii, list  ,";" )
570                if(strlen(item) != 0)
571                        //find the run number, if it exists as a three character string
572                        testStr = V_GetRunNumStrFromFile(item)
573                        runFound= cmpstr(numStr,testStr)        //compare the three character strings, 0 if equal
574                        if(runFound == 0)
575                                //the run Number was found
576                                //build valid filename
577                                partialName = V_FindValidFileName(item)
578                                if(strlen(partialName) != 0)            //non-null return from FindValidFileName()
579                                        fullName = path + partialName
580                                        //check if RAW, if so,this must be the file!
581                                        isRAW = V_CheckIfRawData(fullName)
582                                        if(isRaw)
583                                                //print "is raw, ",fullname
584                                                //stop here
585                                                return(fullname)
586                                        Endif
587                                Endif
588                        Endif
589                Endif
590                ii+=1
591        while(ii<numItems)              //process all items in list
592        Return ("")     //null return if file not found in list
593End
594
595//
596//  x- for VSANS Nexus files, how do I quickly identify if a file is
597//   RAW VSANS data? I don't want to generate any errors, but I want to quickly
598//   weed out the reduced data sets, etc. from file catalogs.
599//      (check the instrument name...)
600
601// (DONE) x- as was written by SANS, this function is expecting fname to be the path:fileName
602// - but are the V_get() functions OK with getting a full path, and what do they
603//  do when they fail? I don't want them to spit up another open file dialog
604//
605// x- problem -- if "sans1234.abs" is passed, then V_getStringFromHDF5(fname,path,num)
606//  will remove the extension and look for the sans1234 folder -- which may or may not be present.
607//  If it is present, then sans1234 validates as RAW data -- which is incorrect!
608// x- so I need a way to exclude everything that does not have the proper extension...
609//
610//
611Function V_CheckIfRawData(fname)
612        String fname
613       
614        String testStr=""
615
616// check for the proper raw data extension
617        if( stringmatch(fname,"*.nxs.ngv*") )
618                // name appears OK, proceed
619                testStr = V_getInstrumentName(fname)
620
621                if(cmpstr(testStr,"NG3-VSANS") == 0)
622                        //testStr exists, ASSUMING it's a raw VSANS data file
623                        Return(1)
624                else
625                        //some other file
626                        Return(0)
627                Endif
628       
629        else
630                // not a proper raw VSANS file name
631                return(0)
632               
633        endif   
634       
635
636End
637
638//  x- need to fill in correctly by determining this from the INTENT field
639//
640Function V_isTransFile(fname)
641        String fname
642       
643        Variable refnum,totalBytes
644        String testStr=""
645       
646        testStr = V_getReduction_intent(fname)
647
648        if(cmpstr(testStr,"TRANSMISSION") == 0)         //
649                //yes, a transmission file
650                Return(1)
651        else
652                //some other file intent
653                Return(0)
654        Endif
655End
656
657
658Function V_GetRunNumFromFile(item)
659        String item
660       
661        String str = V_GetRunNumStrFromFile(item)
662       
663        return(str2num(str))
664end
665
666
667// (DONE) x- the file name structure for VSANS file is undecided
668// so some of these base functions will need to change
669//
670//given a filename of a VSANS data filename of the form
671// sansNNNN.nxs.ngv
672//returns the run number "NNNN" as a STRING of (x) characters
673//
674// -- the run number incements from 1, so the number of digits is UNKNOWN
675// -- number starts at position [4] (the 5th character)
676// -- number ends with the character prior to the first "."
677//
678//returns "ABCD" as an invalid file number
679//
680// local function to aid in locating files by run number
681//
682Function/S V_GetRunNumStrFromFile(item)
683        String item
684        String invalid = "ABCD" //"ABCD" is not a valid run number, since it's text
685        Variable num=-1
686       
687        //find the "dot"
688        String runStr=""
689        Variable numChar = 4
690        Variable pos = strsearch(item,".",0)
691        if(pos == -1)
692                //"dot" not found
693                return (invalid)
694        else
695                //found, get the characters preceeding it, but still after the "sans" characters
696                if (pos-1 < 4)
697                        //not enough characters
698                        return (invalid)
699                else
700                        runStr = item[4,pos-1]
701                        return (runStr)
702                Endif
703        Endif
704End
705
706//Function attempts to find valid filename from partial name by checking for
707// the existence of the file on disk.
708// - checks as is
709// - strips spaces
710// - permutations of upper/lowercase
711//
712// added 11/99 - uppercase and lowercase versions of the file are tried, if necessary
713// since from marquee, the filename field (textread[0]) must be used, and can be a mix of                       //02JUL13
714// upper/lowercase letters, while the filename on the server (should) be all caps
715// now makes repeated calls to ValidFileString()
716//
717// returns a valid filename (No path prepended) or a null string
718//
719// called by any functions, both external and local
720//
721Function/S V_FindValidFilename(partialName)
722        String PartialName
723       
724        String retStr=""
725       
726        //try name with no changes - to allow for ABS files that have spaces in the names 12APR04
727        retStr = V_ValidFileString(partialName)
728        if(cmpstr(retStr,"") !=0)
729                //non-null return
730                return(retStr)
731        Endif
732       
733        //if the partial name is derived from the file header, there can be spaces at the beginning
734        //or in the middle of the filename - depending on the prefix and initials used
735        //
736        //remove any leading spaces from the name before starting
737        partialName = V_RemoveAllSpaces(partialName)
738       
739        //try name with no spaces
740        retStr = V_ValidFileString(partialName)
741        if(cmpstr(retStr,"") !=0)
742                //non-null return
743                return(retStr)
744        Endif
745       
746        //try all UPPERCASE
747        partialName = UpperStr(partialName)
748        retStr = V_ValidFileString(partialName)
749        if(cmpstr(retStr,"") !=0)
750                //non-null return
751                return(retStr)
752        Endif
753       
754        //try all lowercase (ret null if failure)
755        partialName = LowerStr(partialName)
756        retStr = V_ValidFileString(partialName)
757        if(cmpstr(retStr,"") !=0)
758                //non-null return
759                return(retStr)
760        else
761                return(retStr)
762        Endif
763End
764
765
766// Function checks for the existence of a file
767// partialName;vers (to account for VAX filenaming conventions)
768// The partial name is tried first with no version number
769//
770// *** the PATH is hard-wired to catPathName (which is assumed to exist)
771// version numers up to ;10 are tried
772// only the "name;vers" is returned if successful. The path is not prepended
773//
774// local function
775//
776// DONE
777// x- this is essentially a pass-through, since there are no version numbers for VSANS data files
778//    it is kept in case there are conditions in the future.
779//
780Function/S V_ValidFileString(partialName)
781        String partialName
782       
783        String tempName = "",msg=""
784        Variable ii,refnum
785       
786        ii=0
787        do
788                if(ii==0)
789                        //first pass, try the partialName
790                        tempName = partialName
791                        Open/Z/R/T="????TEXT"/P=catPathName refnum tempName     //Does open file (/Z flag)
792                        if(V_flag == 0)
793                                //file exists
794                                Close refnum            //YES needed,
795                                break
796                        endif
797                else
798                        tempName = partialName + ";" + num2istr(ii)
799                        Open/Z/R/T="????TEXT"/P=catPathName refnum tempName
800                        if(V_flag == 0)
801                                //file exists
802                                Close refnum
803                                break
804                        endif
805                Endif
806                ii+=1
807                //print "ii=",ii
808        while(ii<11)
809        //go get the selected bits of information, using tempName, which exists
810        if(ii>=11)
811                //msg = partialName + " not found. is version number > 11?"
812                //DoAlert 0, msg
813                //PathInfo catPathName
814                //Print S_Path
815                Return ("")             //use null string as error condition
816        Endif
817       
818        Return (tempName)
819End
820
821//function to remove all spaces from names when searching for filenames
822//the filename (as saved) will never have interior spaces (TTTTTnnn_AB _Bnnn)
823//but the text field in the header WILL, if less than 3 characters were used for the
824//user's initials, and can have leading spaces if prefix was less than 5 characters
825//
826//returns a string identical to the original string, except with the interior spaces removed
827//
828// local function for file name manipulation
829//
830Function/S V_RemoveAllSpaces(str)
831        String str
832       
833        String tempstr = str
834        Variable ii,spc,len             //should never be more than 2 or 3 trailing spaces in a filename
835        ii=0
836        do
837                len = strlen(tempStr)
838                spc = strsearch(tempStr," ",0)          //is the last character a space?
839                if (spc == -1)
840                        break           //no more spaces found, get out
841                endif
842                str = tempstr
843                tempStr = str[0,(spc-1)] + str[(spc+1),(len-1)] //remove the space from the string
844        While(1)        //should never be more than 2 or 3
845       
846        If(strlen(tempStr) < 1)
847                tempStr = ""            //be sure to return a null string if problem found
848        Endif
849       
850        //Print strlen(tempstr)
851       
852        Return(tempStr)
853               
854End
855
856// returns a list of raw data files in the catPathName directory on disk
857// - list is SEMICOLON-delimited
858//
859//  decide how to do this...
860// (1)
861// checks each file in the directory to see if it is a RAW data file by
862// call to V_CheckIfRawData() which currently looks for the instrument name in the file.
863// -- CON - this is excruciatingly slow, and by checking a field in the file, has to load in the
864//  ENTIRE data file, and will load EVERY file in the folder. ugh.
865//
866// (2)
867// as was done for VAX files, look for a specific string in the file name as written by the acquisition
868//  (was .saN), now key on ".nxs.ngv"?
869//
870// ** use method (2), reading each file is just way too slow
871//
872//
873Function/S V_GetRawDataFileList()
874       
875        //make sure that path exists
876        PathInfo catPathName
877        if (V_flag == 0)
878                Abort "Folder path does not exist - use Pick Path button on Main Panel"
879        Endif
880        String path = S_Path
881       
882        String list=IndexedFile(catPathName,-1,"????")
883        String newList="",item="",validName="",fullName=""
884        Variable num=ItemsInList(list,";"),ii
885       
886        for(ii=0;ii<num;ii+=1)
887                item = StringFromList(ii, list  ,";")
888
889                validName = V_FindValidFileName(item)
890                if(strlen(validName) != 0)              //non-null return from FindValidFileName()
891                        fullName = path + validName             
892
893        //method (1)                   
894//                      if(V_CheckIfRawData(item))
895//                              newlist += item + ";"
896//                      endif
897
898        //method (2)                   
899                        if( stringmatch(item,"*.nxs.ngv*") )
900                                newlist += item + ";"
901                        endif
902
903                       
904                endif
905                //print "ii=",ii
906        endfor
907        newList = SortList(newList,";",0)
908        return(newList)
909End
910
911//
912//
913// x- does this need to be more sophisticated?
914//
915// simple "not" of V_GetRawDataFileList()
916Function/S V_Get_NotRawDataFileList()
917       
918        //make sure that path exists
919        PathInfo catPathName
920        if (V_flag == 0)
921                Abort "Folder path does not exist - use Pick Path button on Main Panel"
922        Endif
923        String path = S_Path
924       
925        String list=IndexedFile(catPathName,-1,"????")
926        String newList="",item="",validName="",fullName=""
927        Variable num=ItemsInList(list,";"),ii
928       
929        for(ii=0;ii<num;ii+=1)
930                item = StringFromList(ii, list  ,";")
931
932//              validName = V_FindValidFileName(item)
933//              if(strlen(validName) != 0)              //non-null return from FindValidFileName()
934//                      fullName = path + validName             
935
936        //method (2)                   
937                        if( !stringmatch(item,"*.nxs.ngv*") )
938                                newlist += item + ";"
939                        endif
940
941                       
942//              endif
943                //print "ii=",ii
944        endfor
945        newList = SortList(newList,";",0)
946        return(newList)
947End
948
949// removes any item with ".EXT" from the list
950// don't pass the "."
951// returns a modified list
952Function/S V_RemoveEXTFromList(list,ext)
953        String list,ext
954       
955        ext = "*."+ext
956        Variable num=ItemsInList(list,";"),ii
957        String item,newList=""
958       
959        for(ii=0;ii<num;ii+=1)
960                item = StringFromList(ii, list  ,";")
961
962                if( !stringmatch(item,ext) )
963                        newlist += item + ";"
964                endif
965               
966        endfor
967        return(newList)
968End
969
970//the following is a WaveMetrics procedure from <StrMatchList>
971// MatchList(matchStr,list,sep)
972// Returns the items of the list whose items match matchStr
973// The lists are separated by the sep character, usually ";"
974//
975// matchStr may be something like "abc", in which case it is identical to CmpStr
976// matchStr may also be "*" to match anything, "abc*" to match anything starting with "abc",
977//      "*abc" to match anything ending with "abc".
978// matchStr may also begin with "!" to indicate a match to anything not matching the rest of
979//      the pattern.
980// At most one "*" and one "!" are allowed in matchStr, otherwise the results are not guaranteed.
981//
982Function/S V_MyMatchList(matchStr,list,sep)
983        String matchStr,list,sep
984        String item,outList=""
985        Variable n=strlen(list)
986        Variable en,st=0
987        do
988                en= strsearch(list,sep,st)
989                if( en < 0 )
990                        if( st < n-1 )
991                                en= n   // no trailing separator
992                                sep=""  // don't put sep in output, either
993                        else
994                                break   // no more items in list
995                        endif
996                endif
997                item=list[st,en-1]
998                if( V_MyStrMatch(matchStr,item) == 0 )
999                        outlist += item+sep
1000                Endif
1001                st=en+1
1002        while (st < n ) // exit is by break, above
1003        return outlist
1004End
1005
1006//the following is a WaveMetrics procedure from <StrMatchList>
1007// StrMatch(matchStr,str)
1008// Returns 0 if the pattern in matchStr matches str, else it returns 1
1009//
1010// matchStr may be something like "abc", in which case it is identical to CmpStr
1011// matchStr may also be "*" to match anything, "abc*" to match anything starting with "abc",
1012//      "*abc" to match anything ending with "abc".
1013// matchStr may also begin with "!" to indicate a match to anything not matching the rest of
1014//      the pattern.
1015// At most one "*" and one "!" are allowed in matchStr, otherwise the results are not guaranteed.
1016//
1017Function V_MyStrMatch(matchStr,str)
1018        String matchStr,str
1019        Variable match = 1              // 0 means match
1020        Variable invert= strsearch(matchStr,"!",0) == 0
1021        if( invert )
1022                matchStr[0,0]=""        // remove the "!"
1023        endif
1024        Variable st=0,en=strlen(str)-1
1025        Variable starPos= strsearch(matchStr,"*",0)
1026        if( starPos >= 0 )      // have a star
1027                if( starPos == 0 )      // at start
1028                        matchStr[0,0]=""                                // remove star at start
1029                else                                    // at end
1030                        matchStr[starPos,999999]=""     // remove star and rest of (ignored, illegal) pattern
1031                endif
1032                Variable len=strlen(matchStr)
1033                if( len > 0 )
1034                        if(starPos == 0)        // star at start, match must be at end
1035                                st=en-len+1
1036                        else
1037                                en=len-1        // star at end, match at start
1038                        endif
1039                else
1040                        str=""  // so that "*" matches anything
1041                endif
1042        endif
1043        match= !CmpStr(matchStr,str[st,en])==0  // 1 or 0
1044        if( invert )
1045                match= 1-match
1046        endif
1047        return match
1048End
1049
1050
1051//input is a list of run numbers, and output is a list of filenames (not the full path)
1052//*** input list must be COMMA delimited***
1053//output is equivalent to selecting from the CAT table
1054//if some or all of the list items are valid filenames, keep them...
1055//if an error is encountered, notify of the offending element and return a null list
1056//
1057//output is COMMA delimited
1058//
1059// this routine is expecting that the "ask", "none" special cases are handled elsewhere
1060//and not passed here
1061//
1062// called by Marquee.ipf, MultipleReduce.ipf, ProtocolAsPanel.ipf
1063//
1064Function/S V_ParseRunNumberList(list)
1065        String list
1066       
1067        String newList="",item="",tempStr=""
1068        Variable num,ii,runNum
1069       
1070        //expand number ranges, if any
1071        list = V_ExpandNumRanges(list)
1072       
1073        num=itemsinlist(list,",")
1074       
1075        for(ii=0;ii<num;ii+=1)
1076                //get the item
1077                item = StringFromList(ii,list,",")
1078                //is it already a valid filename?
1079                tempStr=V_FindValidFilename(item) //returns filename if good, null if error
1080                if(strlen(tempstr)!=0)
1081                        //valid name, add to list
1082                        //Print "it's a file"
1083                        newList += tempStr + ","
1084                else
1085                        //not a valid name
1086                        //is it a number?
1087                        runNum=str2num(item)
1088                        //print runnum
1089                        if(numtype(runNum) != 0)
1090                                //not a number -  maybe an error                       
1091                                DoAlert 0,"List item "+item+" is not a valid run number or filename. Please enter a valid number or filename."
1092                                return("")
1093                        else
1094                                //a run number or an error
1095                                tempStr = V_GetFileNameFromPathNoSemi( V_FindFileFromRunNumber(runNum) )
1096                                if(strlen(tempstr)==0)
1097                                        //file not found, error
1098                                        DoAlert 0,"List item "+item+" is not a valid run number. Please enter a valid number."
1099                                        return("")
1100                                else
1101                                        newList += tempStr + ","
1102                                endif
1103                        endif
1104                endif
1105        endfor          //loop over all items in list
1106       
1107        return(newList)
1108End
1109
1110//takes a comma delimited list that MAY contain number range, and
1111//expands any range of run numbers into a comma-delimited list...
1112//and returns the new list - if not a range, return unchanged
1113//
1114// local function
1115//
1116Function/S V_ExpandNumRanges(list)
1117        String list
1118       
1119        String newList="",dash="-",item,str
1120        Variable num,ii,hasDash
1121       
1122        num=itemsinlist(list,",")
1123//      print num
1124        for(ii=0;ii<num;ii+=1)
1125                //get the item
1126                item = StringFromList(ii,list,",")
1127                //does it contain a dash?
1128                hasDash = strsearch(item,dash,0)                //-1 if no dash found
1129                if(hasDash == -1)
1130                        //not a range, keep it in the list
1131                        newList += item + ","
1132                else
1133                        //has a dash (so it's a range), expand (or add null)
1134                        newList += V_ListFromDash(item)         
1135                endif
1136        endfor
1137       
1138        return newList
1139End
1140
1141//be sure to add a trailing comma to the return string...
1142//
1143// local function
1144//
1145Function/S V_ListFromDash(item)
1146        String item
1147       
1148        String numList="",loStr="",hiStr=""
1149        Variable lo,hi,ii
1150       
1151        loStr=StringFromList(0,item,"-")        //treat the range as a list
1152        hiStr=StringFromList(1,item,"-")
1153        lo=str2num(loStr)
1154        hi=str2num(hiStr)
1155        if( (numtype(lo) != 0) || (numtype(hi) !=0 ) || (lo > hi) )
1156                numList=""
1157                return numList
1158        endif
1159        for(ii=lo;ii<=hi;ii+=1)
1160                numList += num2istr(ii) + ","
1161        endfor
1162       
1163        Return numList
1164End
1165
1166//*********************
1167// List utilities
1168//*********************
1169Function/WAVE V_List2TextWave(list,sep,waveStr)
1170        String list,sep,waveStr
1171       
1172        Variable n= ItemsInList(list,sep)
1173        Make/O/T/N=(n) $waveStr= StringFromList(p,list,sep)
1174        return $waveStr
1175End
1176
1177Function/WAVE V_List2NumWave(list,sep,waveStr)
1178        String list,sep,waveStr
1179       
1180        Variable n= ItemsInList(list,sep)
1181        Make/O/D/N=(n) $waveStr= str2num( StringFromList(p,list,sep) )
1182        return $waveStr
1183End
1184
1185Function /S V_TextWave2List(w,sep)
1186        Wave/T w
1187        String sep
1188       
1189        String newList=""
1190        Variable n=numpnts(w),ii=0
1191        do
1192                newList += w[ii] + sep
1193                ii+=1
1194        while(ii<n)
1195        return(newList)
1196End
1197
1198//for numerical waves
1199Function/S V_NumWave2List(w,sep)
1200        Wave w
1201        String sep
1202       
1203        String newList="",temp=""
1204        Variable n=numpnts(w),ii=0,val
1205        do
1206                val=w[ii]
1207                temp=""
1208                sprintf temp,"%g",val
1209                newList += temp
1210                newList += sep
1211                ii+=1
1212        while(ii<n)
1213        return(newList)
1214End
1215
1216
1217/////
1218// @ IgorExchange
1219//TicToc
1220//Posted April 16th, 2009 by bgallarda
1221//      ¥       in Programming 6.10.x
1222
1223////duplicated here -- for VSANS use
1224function v_tic()
1225        variable/G tictoc = startMSTimer
1226end
1227 
1228function v_toc()
1229        NVAR/Z tictoc
1230        variable ttTime = stopMSTimer(tictoc)
1231        printf "%g seconds\r", (ttTime/1e6)
1232        killvariables/Z tictoc
1233end
1234
1235
1236
1237////// methods for filtering out different lists of files.
1238
1239// testStr is the "intent" string, or grep string
1240// method is the method to use to find the file
1241// 0 = (default) is to use the file catalog (= fastest)
1242// 1 = Grep (not terribly slow)
1243// 2 = read every file (bad choice)
1244//
1245Function/S V_getFileIntentList(testStr,method)
1246        String testStr
1247        Variable method
1248       
1249        Variable ii,num
1250        String list="",item="",fname,newList,intent
1251
1252        // read every file...
1253        if(method == 2)
1254                PathInfo catPathName
1255                String path = S_path
1256                newList = V_GetRawDataFileList()
1257                num=ItemsInList(newList)
1258               
1259                for(ii=0;ii<num;ii+=1)
1260                        item=StringFromList(ii, newList , ";")
1261                        fname = path + item
1262                        intent = V_getReduction_intent(fname)
1263                        if(cmpstr(intent,testStr) == 0)
1264                                list += item + ";"
1265                        endif
1266                endfor 
1267        endif
1268       
1269        // use Grep
1270        if(method == 1)
1271                newList = V_GetRawDataFileList()
1272                num=ItemsInList(newList)
1273                for(ii=0;ii<num;ii+=1)
1274                        item=StringFromList(ii, newList , ";")
1275                        Grep/P=catPathName/Q/E=("(?i)"+testStr) item
1276                        if( V_value )   // at least one instance was found
1277        //                              Print "found ", item,ii
1278                                list += item + ";"
1279                        endif
1280                endfor 
1281       
1282        else
1283        // get the list from the file catalog
1284       
1285                WAVE/T fileNameW = root:Packages:NIST:VSANS:CatVSHeaderInfo:Filenames
1286                WAVE/T intentW = root:Packages:NIST:VSANS:CatVSHeaderInfo:Intent
1287               
1288                Variable np = numpnts(intentW)          //fileNameW is LONGER - so don't use numpnts(fileWave)
1289                for(ii=0;ii<np;ii+=1)
1290                        if(cmpstr(intentW[ii],testStr)==0)              //this is case-INSENSITIVE (necessary, since the case is unknown)
1291                                list += fileNameW[ii] + ";"
1292                        endif           
1293                endfor
1294               
1295                List = SortList(List,";",0)
1296        endif
1297       
1298        return(list)
1299end
1300
1301
1302// testStr is the "purpose" string, or grep string
1303// method is the method to use to find the file
1304// 0 = (default) is to use the file catalog (= fastest)
1305// 1 = Grep (not terribly slow)
1306// 2 = read every file (bad choice)
1307//
1308Function/S V_getFilePurposeList(testStr,method)
1309        String testStr
1310        Variable method
1311       
1312        Variable ii,num
1313        String list="",item="",fname,newList,purpose
1314
1315        // read every file...
1316        if(method == 2)
1317                PathInfo catPathName
1318                String path = S_path
1319                newList = V_GetRawDataFileList()
1320                num=ItemsInList(newList)
1321               
1322                for(ii=0;ii<num;ii+=1)
1323                        item=StringFromList(ii, newList , ";")
1324                        fname = path + item
1325                        purpose = V_getReduction_purpose(fname)
1326                        if(cmpstr(purpose,testStr) == 0)
1327                                list += item + ";"
1328                        endif
1329                endfor 
1330        endif
1331       
1332        // use Grep
1333        if(method == 1)
1334                newList = V_GetRawDataFileList()
1335                num=ItemsInList(newList)
1336                for(ii=0;ii<num;ii+=1)
1337                        item=StringFromList(ii, newList , ";")
1338                        Grep/P=catPathName/Q/E=("(?i)"+testStr) item
1339                        if( V_value )   // at least one instance was found
1340        //                              Print "found ", item,ii
1341                                list += item + ";"
1342                        endif
1343                endfor 
1344       
1345        else
1346        // get the list from the file catalog
1347       
1348                WAVE/T fileNameW = root:Packages:NIST:VSANS:CatVSHeaderInfo:Filenames
1349                WAVE/T purposeW = root:Packages:NIST:VSANS:CatVSHeaderInfo:Purpose
1350               
1351                Variable np = numpnts(purposeW)         //fileNameW is LONGER - so don't use numpnts(fileWave)
1352                for(ii=0;ii<np;ii+=1)
1353                        if(cmpstr(purposeW[ii],testStr)==0)             //this is case-INSENSITIVE (necessary, since the case is unknown)
1354                                list += fileNameW[ii] + ";"
1355                        endif           
1356                endfor
1357               
1358                List = SortList(List,";",0)
1359        endif
1360       
1361        return(list)
1362end
1363
1364
1365// match BOTH the intent and purpose
1366// -- needed to identify the SAMPLE + SCATTERING data files.
1367//
1368//
1369// method is the method to use to find the file (currently ignored, CAT is always used)
1370// 0 = (default) is to use the file catalog (= fastest)
1371// 1 = Grep (not terribly slow)
1372// 2 = read every file (bad choice)
1373//
1374Function/S V_getFileIntentPurposeList(intent,purpose,method)
1375        String intent,purpose
1376        Variable method
1377       
1378        Variable ii,num
1379        String list="",item="",fname,newList
1380
1381//      // read every file...
1382//      if(method == 2)
1383//              PathInfo catPathName
1384//              String path = S_path
1385//              newList = V_GetRawDataFileList()
1386//              num=ItemsInList(newList)
1387//             
1388//              for(ii=0;ii<num;ii+=1)
1389//                      item=StringFromList(ii, newList , ";")
1390//                      fname = path + item
1391//                      purpose = V_getReduction_purpose(fname)
1392//                      if(cmpstr(purpose,testStr) == 0)
1393//                              list += item + ";"
1394//                      endif
1395//              endfor 
1396//      endif
1397//     
1398//      // use Grep
1399//      if(method == 1)
1400//              newList = V_GetRawDataFileList()
1401//              num=ItemsInList(newList)
1402//              for(ii=0;ii<num;ii+=1)
1403//                      item=StringFromList(ii, newList , ";")
1404//                      Grep/P=catPathName/Q/E=("(?i)"+testStr) item
1405//                      if( V_value )   // at least one instance was found
1406//      //                              Print "found ", item,ii
1407//                              list += item + ";"
1408//                      endif
1409//              endfor 
1410//     
1411//      else
1412        // get the list from the file catalog
1413       
1414                WAVE/T fileNameW = root:Packages:NIST:VSANS:CatVSHeaderInfo:Filenames
1415                WAVE/T purposeW = root:Packages:NIST:VSANS:CatVSHeaderInfo:Purpose
1416                WAVE/T intentW = root:Packages:NIST:VSANS:CatVSHeaderInfo:Intent
1417               
1418                Variable np = numpnts(purposeW)         //fileNameW is LONGER - so don't use numpnts(fileWave)
1419                for(ii=0;ii<np;ii+=1)
1420                        if(cmpstr(purposeW[ii],purpose)==0 && cmpstr(intentW[ii],intent)==0)            //this is case-INSENSITIVE (necessary, since the case is unknown)
1421                                list += fileNameW[ii] + ";"
1422                        endif           
1423                endfor
1424               
1425                List = SortList(List,";",0)
1426       
1427        return(list)
1428end
1429
1430
1431// match the INTENT and PURPOSE and GROUP_ID
1432//
1433// -- needed to identify the SAMPLE + SCATTERING data files.
1434//
1435//
1436// method is the method to use to find the file (currently ignored, CAT is always used)
1437// 0 = (default) is to use the file catalog (= fastest)
1438// 1 = Grep (not terribly slow)
1439// 2 = read every file (bad choice)
1440//
1441Function/S V_getFileIntentPurposeIDList(intent,purpose,targetID,method)
1442        String intent,purpose
1443        Variable targetID,method
1444       
1445        Variable ii,num
1446        String list="",item="",fname,newList
1447
1448//      // read every file...
1449//      if(method == 2)
1450//              PathInfo catPathName
1451//              String path = S_path
1452//              newList = V_GetRawDataFileList()
1453//              num=ItemsInList(newList)
1454//             
1455//              for(ii=0;ii<num;ii+=1)
1456//                      item=StringFromList(ii, newList , ";")
1457//                      fname = path + item
1458//                      purpose = V_getReduction_purpose(fname)
1459//                      if(cmpstr(purpose,testStr) == 0)
1460//                              list += item + ";"
1461//                      endif
1462//              endfor 
1463//      endif
1464//     
1465//      // use Grep
1466//      if(method == 1)
1467//              newList = V_GetRawDataFileList()
1468//              num=ItemsInList(newList)
1469//              for(ii=0;ii<num;ii+=1)
1470//                      item=StringFromList(ii, newList , ";")
1471//                      Grep/P=catPathName/Q/E=("(?i)"+testStr) item
1472//                      if( V_value )   // at least one instance was found
1473//      //                              Print "found ", item,ii
1474//                              list += item + ";"
1475//                      endif
1476//              endfor 
1477//     
1478//      else
1479        // get the list from the file catalog
1480       
1481                WAVE/T fileNameW = root:Packages:NIST:VSANS:CatVSHeaderInfo:Filenames
1482                WAVE/T purposeW = root:Packages:NIST:VSANS:CatVSHeaderInfo:Purpose
1483                WAVE/T intentW = root:Packages:NIST:VSANS:CatVSHeaderInfo:Intent
1484                WAVE groupIDW = root:Packages:NIST:VSANS:CatVSHeaderInfo:group_id
1485               
1486                Variable np = numpnts(purposeW)         //fileNameW is LONGER - so don't use numpnts(fileWave)
1487                for(ii=0;ii<np;ii+=1)
1488                        if(cmpstr(purposeW[ii],purpose)==0 && cmpstr(intentW[ii],intent)==0 && groupIDW[ii]==targetID)          //this is case-INSENSITIVE (necessary, since the case is unknown)
1489                                list += fileNameW[ii] + ";"
1490                        endif           
1491                endfor
1492               
1493                List = SortList(List,";",0)
1494       
1495        return(list)
1496end
1497
1498// from WM procedure, extended to three waves for I(q) data sets
1499//
1500// RemoveNaNsQIS(theQWave, theIWave, theSWave)
1501//      Removes all points in an XYZ triple if any of the three waves has the value NaN.
1502//      A NaN represents a blank or missing value.
1503//      Returns the number of points removed.
1504Function V_RemoveNaNsQIS(theXWave, theYWave, theZWave)
1505        Wave theXWave
1506        Wave theYWave
1507        Wave theZWave
1508
1509        Variable p, numPoints, numNaNs
1510        Variable xval, yval, zval
1511       
1512        numNaNs = 0
1513        p = 0                                                                                   // the loop index
1514        numPoints = numpnts(theXWave)                   // number of times to loop
1515
1516        do
1517                xval = theXWave[p]
1518                yval = theYWave[p]
1519                zval = theZWave[p]
1520                if ((numtype(xval)==2) %| (numtype(yval)==2) %| (numtype(zval)==2) )            // either is NaN?
1521                        numNaNs += 1
1522                else                                                                            // if not an outlier
1523                        theXWave[p - numNaNs] = xval            // copy to input wave
1524                        theYWave[p - numNaNs] = yval            // copy to input wave
1525                        theZWave[p - numNaNs] = zval            // copy to input wave
1526                endif
1527                p += 1
1528        while (p < numPoints)
1529       
1530        // Truncate the wave
1531        DeletePoints numPoints-numNaNs, numNaNs, theXWave, theYWave, theZWave
1532       
1533        return(numNaNs)
1534End
1535
1536
1537//
1538////// utilities for the back detector to diagnose saturation issues
1539//
1540//
1541// as of March 2019, the detector data is processed differently, with more
1542// processing done before passing the data to the file. The data is collected
1543// in 1x1 mode, then filtered (11x11) and rebinned. This increases the
1544// pixel saturation value to 16399*16 = 262384
1545// and the read background to Å 200*16
1546//
1547//
1548//
1549// old values were (pre-March 2019) =  saturation value of 16384 and read baackground of 200
1550// (before the 1x1 collection)
1551//
1552
1553
1554Proc Vm_NumberSaturated(folderStr)
1555        String folderStr="RAW"
1556        V_NumberSaturated(folderStr)
1557End
1558
1559Function V_NumberSaturated(folderStr)
1560        String folderStr
1561       
1562        Variable num,saturationValue
1563        Duplicate/O $("root:Packages:NIST:VSANS:"+folderStr+":entry:instrument:detector_B:data") tmpData
1564       
1565        saturationValue = 16399*16
1566       
1567        tmpData = (tmpData > saturationValue-1) ? NaN : tmpData
1568        WaveStats/Q tmpData
1569        num = V_numNaNs
1570       
1571        Printf "Number of saturated pixels = %d (%g %)\r",num,num/(680*1656)*100
1572        KillWaves/Z tmpData
1573       
1574        V_ColorizeSaturated()
1575        return(num)
1576end
1577
1578// turns the saturated values to lime green
1579Function V_ColorizeSaturated()
1580        ModifyImage data ctab= {0,16399*16-1,ColdWarm,0},minRGB=0,maxRGB=(32792,65535,1)
1581End
1582
1583///
1584
1585
1586Function V_PrintLoHiNum()
1587
1588        Variable lo,hi
1589        V_Find_LoHi_RunNum(lo,hi)
1590        Print "lo = ",lo
1591        Print "hi = ",hi
1592       
1593End
1594
1595//
1596// returns the lo, hi run numbers in the current directory
1597// (passed by reference)
1598//
1599//
1600// note that 1111 (is a special number for the ReadNoise file, ignore this run)
1601//
1602Function V_Find_LoHi_RunNum(lo,hi)
1603        Variable &lo,&hi
1604       
1605        String fileList="",fname=""
1606        Variable ii,num,runNum
1607       
1608        // set to values that will change
1609        lo = 1e8
1610        hi = 0
1611       
1612        // get a file listing of all raw data files
1613        fileList = V_GetRawDataFileList()
1614        num = itemsInList(fileList)
1615       
1616        for(ii=0;ii<num;ii+=1)
1617                fname = stringFromList(ii,fileList)
1618                runNum = V_GetRunNumFromFile(fname)
1619
1620                if(runNum != 1111)              //ignore this run
1621                        lo = runNum < lo ? runNum : lo          // if runNum < lo, update
1622                        hi = runNum > hi ? runNum : hi          // if runNum > hi, update
1623                endif
1624        endfor
1625       
1626        return(0)
1627End
1628
1629
Note: See TracBrowser for help on using the repository browser.