[Thread Prev][Thread Next][Index]

Re: [ferret_users] check if a particular file exists

Thank you Jaison,
The RETURN=iend command is failing for these string variables. We'll look into why.
   yes? LET a = {1,3,4}
   yes? say `a,return=iend`

Jaison Kurian wrote:
Hi Ansley,
            A minor correction to the suggested solution :

On Wed, 18 Apr 2007, Ansley Manke wrote:

Hi David,
You can use the SPAWN command, see "spawn, string variables" in the 
Users Guide index.

    *yes? LET a = {"dummy", SPAWN:"ls myfile.nc"}
    yes? LET nfiles = `a,RETURN=IEND`
**    yes? IF `nfiles EQ 2` THEN SET DATA myfile.nc *

If the file exists then nfiles will be 2, otherwise it'll be 1.


  Unfortunately, none of the Ferret versions (5.53,5.7,5.8 & 6.0) on
  my Linux PC gives the intended result (nfiles=2 or 1):

  The above example will give nfiles a value, which is always 2000 
  plus the number of dummy filenames used in the definition of "a",
  irrespective of whether "myfile.nc" exists in pwd or not. 
  This means that, in the presence of SPAWN command, Ferret have a
  bug/difficulty in extracting the number of entries using RETURN 

  This is the reason why i suggested to use XSEQUENCE to get the
  number of files, in my earlier reply:

  However, if the file "myfile.nc" does not exist in pwd, then the 
  RETURN=IEND command will yield "1" instead of "0", irrespective of
  whether we have used XSEQUENCE or not. See this example :

      yes? let a = {spawn:"ls nonexistent.nc"}
      yes? list a
           ls: nonexistent.nc: No such file or directory
                VARIABLE : {SPAWN:"ls nonexistent.nc"}
                X        : 1

  Though the string "a" is blank, it does have a X-size of 1. So the
  RETURN=IEND will give "1" in this case, but we expected "0".

  The foolproof method for checking the existence of a file is to use 
  the "string length" of the variable. Here is an example :

       let fex = {spawn:"ls *.nc"}
       IF `STRLEN(fex[i=1]) GT 0` THEN
           let files  = XSEQUENCE(fex)
           let nfiles = `files,r=isize`
           say "  `nfiles` File/s exist in pwd/cwd."
           let nfiles = 0
           say "  NO File exist in pwd/cwd."

  Now, we can use the above logical construct, or we can make a new
  one using "nfiles"

       IF `nfiles GT 0` THEN
             do something
             do something else

Thanks and Regards,


Another possibility if you don't mind creating an error condition 
intentionally, is to SET MODE IGNORE_ERROR, try to open the file, and 
then check for the existence of the symbol RET_LAST_ERROR; this is 
described in the Version 6.0 release notes,


David Wang wrote:
Hello Ferreters,

I'd like to make a conditional construct so that if a particular 
netcdf file exists in the local directory, open it, if not, do 
something else. this is quite trivial in MATLAB using exist function, 
but seems nontrivial for me in FERRET. Does anyone have any ideas?




~>   ~>   ~>   ~>   ~>   ~>   ~>   ~>   ~>   ~>   ~> 

Ansley Manke, NOAA/PMEL   ansley.b.manke@xxxxxxxx
7600 Sand Point Way NE, Seattle WA
Phone 206-526-6246,  FAX 206-526-6744

[Thread Prev][Thread Next][Index]

Contact Us
Dept of Commerce / NOAA / OAR / PMEL / TMAP

Privacy Policy | Disclaimer | Accessibility Statement