Determining if a dataset exists
Question:
How can I check for a files's existence before trying to open it, or before writing to it?
Example:
yes? use test1.nc yes? save/clobber/i=1:10/j=1:10/l=2/file=test2.nc var1,var2 yes? use "http://ferret.pmel.noaa.gov/pmel/thredds/dodsC/data/PMEL/coads_climatology.nc"
We might want to check before trying to open test1.nc, or a remote URL, or trying to write to test2.nc.
Explanation:
'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/file=test2.nc var1, var2 ! If the file exists then overwrite it, otherwise create and write to it SAVE/CLOBBER/file=test2.nc 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.
Solution:
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("tripolar_subset.nc") VARIABLE : TEST_OPENDAP("tripolar_subset.nc") X : 1 0.0000 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") VARIABLE : TEST_OPENDAP("ferret.jnl") X : 1 -51.00
If the file does not exist, the error code is 2.
yes? list test_opendap("this_file_doesnt_exist.nc") VARIABLE : TEST_OPENDAP("this_file_doesnt_exist.nc") X : 1 2.00
So a script that tests for existence might be,
let isfile = `test_opendap("test2.nc")` if `isfile EQ 0` then ! . . . file exists . . . elif `isfile EQ 2` THEN ! . . . file does not exist . . . else ! . . . error opening the file . . . endif
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