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: NCAR/CGD > Keywords: 199404210213.AA28466 Hi Charlie, > the ncvarid function i'm using doesn't seem to be returning > a -1 when there's an error, like the documentation says. > here's some output from the xdbx debugger: > > (xdbx) print cdf_id > cdf_id = 0 > (xdbx) print dim_names[dim_index] > dim_names[dim_index] = 0x19888 "num_layersp2" > (xdbx) print ncvarid(cdf_id,dim_names[dim_index]) > ncvarid: variable "num_layersp2" not found > > execution completed, exit code is 3 > program exited with 3 > (xdbx) > > as you can see, ncvarid complains that the given name was not found, > and, as far as i can tell, exits the program on its own, seemingly > with a return code of 3. my program, however, is trying to trap > these errors a la > > /* see if the dimension is a coordinate, i.e., if it is stored as a > variable */ > if( > ((coord_id=ncvarid(cdf_id,dim_names[dim_index])) == -1) || > False ){ > (void)fprintf(fp_err,"Dimension %s is not a > coordinate\n",dim_names[dim_index]); > }else{ > etc.... > > and so my error trapping never works because i'm not getting a -1 returned. > can you think of what i might be doing wrong? if you can't think of > anything then could you check to make > sure ncvarid in netcdf 2.3.2 works as advertised? If you want to trap errors from netCDF functions, you need to override the default error-handling behavior. Here's what the User's Guide says about how to do this, in the section on "Error Handling": By default all netCDF library routines print an error message and exit when an error has occurred. If this error behavior is acceptable, you never need to check error returns, since any condition that would result in an error will print an explanatory message and exit. ... In the C interface, errors may be handled more flexibly by setting the external integer ncopts, declared in the file `netcdf.h'. Two aspects of the error-handling behavior can be modified independently: the suppression of error messages, and the fatality of errors. The default behavior is specified by the assignment ncopts = NC_VERBOSE | NC_FATAL; where NC_VERBOSE and NC_FATAL are predefined constants from the include file `netcdf.h'. If you want error messages but do not wish errors to be fatal, turn off the fatal error flag with: ncopts = NC_VERBOSE; If you want neither error messages nor fatal errors, turn off both flags with: ncopts = 0; In either case, you should check the return value after each call to a netCDF function. The integer -1 is returned whenever an error occurs and NC_FATAL is off, so you can detect error returns and handle the errors appropriately. Another externally-defined integer, ncerr, contains a netCDF-specific error code that can be used after an error has occurred to determine what the nature of the error was. The names and descriptions of netCDF error codes are included in the file `netcdf.h'. So I think all you need to do is set ncopts appropriately at the beginning of your program. Please let us know if this edoesn't fix the problem. __________________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden P.O. Box 3000 (303)497-8645 Boulder, Colorado 80307-3000