Ignore:
Timestamp:
Apr 27, 2016 4:16:09 PM (7 years ago)
Author:
srkline
Message:

more changes, lots of files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sans/Dev/trunk/NCNR_User_Procedures/Reduction/VSANS/V_Utilities_General.ipf

    r987 r994  
    4949        Prompt type,"WORK data type to display",popup,"RAW;SAM;EMP;BGD;ADJ;" 
    5050 
    51         UpdateDisplayInformation(type) 
    52 End 
    53  
     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 
    5472// 
    5573// tests if two values are close enough to each other 
     
    204222 
    205223 
     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 TracChangeset for help on using the changeset viewer.