This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
>Organization: NOAA >Keywords: 199501101741.AA11198 netCDF Hi Cathy, > Hi- I came across a behavior of netcdf that I'm not sure is a bug. When I > use the fortran call call ncopn(file,writetype,icode) and look at icode to > see if a file is netcdf or not, I always get a 0, even if the file is not > a netcdf file or even if it doesn't exist. When I check the return from > the function (netid=ncopn.....) I get a -1 for nonnetcdf files and a 0 for > netcdf files. > > Since the icode is clearly meant as an error code, I would expect to check > that in fortran and not the value of netid? > > Coudl someone tell me if my interpretaion of icode is correct? I was skeptical that such a bug could lurk undiscovered in the netCDF sources for so long, but it looks it is a bug in both the C and Fortran interfaces. In the C interface, when ncopen() is called on a nonexistent file, -1 is returned, but the global error variable `ncerr' is set to 0 instead of NC_SYSERR. The Fortran interface uses `ncerr' to set its return code, so it returns 0 in this case. Hence if we fix the C behavior, the Fortran will work OK. However, things seem to work OK in the case of trying to open an existing file that is not a netCDF file, as shown by the results I get in running the test program below: program opbug C This program will open bogus files and check error returns include 'netcdf.inc' C Turn off fatal, verbose errors, so we can check return code call ncpopt(0) C Attempt to open a nonexistent file. ncid = ncopn("tooth_fairy.nc", NCNOWRIT, icode) print *,"icode=",icode, ", ncid=", ncid C Attempt to open non-netCDF file print *, "icode=",icode, ", ncid=", ncid end Running this on my system produces the following output: icode= 0, ncid= -1 icode= 19, ncid= -1 which shows the bug for the first case, but seems to verify the second case is working OK. I'll file this bug with a few others we have to fix in the next release. Thanks for reporting it! ______________________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden P.O. Box 3000 http://www.unidata.ucar.edu/ Boulder, CO 80307-3000 ______________________________________________________________________________