[Thread Prev][Thread Next][Index]

Re: [ferret_users] check if a particular file exists



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 
  command. 

  This is the reason why i suggested to use XSEQUENCE to get the
  number of files, in my earlier reply:
  (http://ferret.pmel.noaa.gov/Ferret/Mail_Archives/fu_2007/msg00225.html)

  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
             ""
      yes?

  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".

  SUMMARY
  =======
  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."
       ELSE
           let nfiles = 0
           say "  NO File exist in pwd/cwd."
       ENDIF

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

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



Thanks and Regards,

Jaison 


> 
> 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,
> http://ferret.pmel.noaa.gov/Ferret/Documentation/Release_Notes/v600.html#fer_last_error
> 
> Ansley
> 
> 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?
> >
> > Thanks,
> > David
> >
> 
> 


[Thread Prev][Thread Next][Index]

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

Privacy Policy | Disclaimer | Accessibility Statement