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: Janet Anstett <address@hidden> >Subject: Question about get_var >Organization: UCAR/Unidata >Keywords: 200303032232.h23MWC314778 Hi Janet, > I am using the C++ interface to netcdf version 3.5.0. I am using > get_var to get the variable ids from an existing netcdf file. > Sometimes the variable that I am looking for does not exist in a > particular file. I am trying to figure out a way to check the return > value to see if I did get a variable id or it didn't work. I have > checked the documentation and could not tell whether there is a way to > do this. I checked the web lists and couldn't find anything > relevant. Basically when I use get_var on a variable that doesn't > exist in the file, I get the following error message: > ncvarid: ncid 3: Variable not found > I would like to be able to check whether the variable exists in the > file without the program erroring out. Is there a way to do this? Yes, you need to change the default NcError::Behavior from verbose_fatal to something more appropriate, such as silent_nonfatal or verbose_nonfatal. That way, you can check the value of the get_var method and the library won't just exit. For documentation of the NcError class, see http://www.unidata.ucar.edu/packages/netcdf/cxxdoc.html#SEC18 As an example, just declaring: NcError* xerror = new NcError(NcError::silent_nonfatal); will change the error handling behavior until the xerror object is destroyed, either explicitly or just by going out of scope. --Russ