National Oceanic and
Atmospheric Administration
United States Department of Commerce

Determining if a dataset exists

Determining if a dataset exists


How can I check for a files's existence before trying to open it, or before writing to it?


yes? use
yes? save/clobber/i=1:10/j=1:10/l=2/ var1,var2

yes? use ""

We might want to check before trying to open, or a remote URL, or trying to write to


'The /APPEND or /CLOBBER qualifier can be used as a partial solution for files that the script is writing.

! If file exists then append, otherwise create and write to it
SAVE/APPEND/ var1, var2

! If the file exists then overwrite it, otherwise create and write to it
SAVE/CLOBBER/ var1, var2

For opening datasets, of course Ferret returns an error if it cannot successfully open a dataset.

We may want more control so that a script can exit gracefully or do different commands depending on the datasets's existence.


There are at least two solutions. One uses the TEST_OPENDAP function. It returns the flag that's sent back from the netCDF library on a request to open the dataset, and so it is useful for testing remote datasets as well as local ones.

If the result of the function is zero, the file exists and can be correctly opened.

yes? list test_opendap("")
X : 1

yes? use tripolar_subset

If the dataset 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")
X : 1

If the file does not exist, the error code is 2.

yes? list test_opendap("")
X : 1

So a script that tests for existence might be,

 let isfile = `test_opendap("")` if `isfile EQ 0` then ! . . . file exists . . . elif `isfile EQ 2` THEN ! . . . file does not exist . . .
! . . . error opening the file . . .

TEST_OPENDAP is only for checking on netCDF files or OPeNDAP URL's. A second kind of solution might be needed when using non-NetCDF files. For local files, is the output of SPAWN and shell commands, such as,

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