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.
>To: address@hidden >From: Gregory Roland Ruetsch <address@hidden> >Subject: ncvid problem >Organization: Stanford >Keywords: 199702130101.SAA08277 Hi Greg, > A have a question regarding how to determine if a variable is present > in a netcdf file. I want to postprocess a group of files, some of > which contain a variable 'theta' and some of which don't. I would > like to determine this at run time to avoid setting flags in the code > and recompiling. I have the following code segment: > > integer ncid, vid, rcode, scalar_field > ... > > ncid = NCOPN('nc.ic', NCNOWRIT, rcode) > vid = NCVID(ncid, 'theta', scalar_field) > call NCCLOS(ncid, rcode) > > if (scalar_field == 0) then > > > so that I am using the error parameter in the NCVID call to determine > if 'theta' exists in the file. However, when I run the code I get the > following error: > > ncvarid: variable "theta" not found > > and the code quits. Is there a better (one that works) way to do this? Yes, although the default error handling behavior of netCDF functions is to exit on error, this behavior is under programmer control. You can independently control the fatality of errors and the appearance of messages from errors detected in netCDF library calls. See the section on Error Handling in the NetCDF User's Guide for how to do this (page 32 in the 2.4 User's Guide) or on-line at: http://www.unidata.ucar.edu/packages/netcdf/guide_6.html#SEC37 This explains that you will need to add an initial call to CALL NCPOPT(0) to set the error handling so you can handle all errors. --Russ _____________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu