[Thread Prev][Thread Next][Index]

Re: [ferret_users] Check file exist before saving it



Hi all
Thanks for the replies.  This sounds like a good topic for an FAQ.  There's one more way that occurs to me, the TEST_OPENDAP function.  It returns the flag that's sent back from the netCDF library on a request to open the dataset.

If the flag is zero, the file exists and can be correctly opened.
yes? list test_opendap("z_wide_variation.nc")
             VARIABLE : TEST_OPENDAP("z_wide_variation.nc")
             X        : 1
          0.0000

If the file exists but is not a valid netCDF file or opendap url, you get various other codes depending on the error (invalid remote  url, not-a-nc file, etc.)

yes? list test_opendap("ferret.jnl")
             VARIABLE : TEST_OPENDAP("ferret.jnl")
             X        : 1
         -51.00

If the file does not exist, you 'll get -2.
yes? list test_opendap("this_file_doesnt_exist.nc")
             VARIABLE : TEST_OPENDAP("this_file_doesnt_exist.nc")
             X        : 1
          2.000


So you could test for the result of the function being zero.

Ansley

On 10/25/2011 2:19 AM, Ryo Furue wrote:
Hi Dan,

| I would like to check file existence before saving it.
|
You can use shell commands for that:

  let ret = {spawn:"[ -e CTL.nc ]; echo $?"}
  if `ret[i=1] eq "0"` then
    ! . . . file exists . . .
  else
    ! . . . file does not exist . . .
  endif

The shell command "[ -e file ]" returns 0 if the file exists
or a different value if it does not.

The command "echo $?" just prints the return value of the
preceding command to the standard output.

The SPAWN _expression_ of Ferret catches the standard output of the
shell command(s).

There may be a Ferret-proper way.

Regards,
Ryo

[Thread Prev][Thread Next][Index]
Contact Us
Dept of Commerce / NOAA / OAR / PMEL / Ferret

Privacy Policy | Disclaimer | Accessibility Statement