NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.
To learn about what's going on, see About the Archive Site.
I see the following in the API notes for netcdf::NcGroup::AddVar: Adds a netCDF variable. The NcType<http://www.nrel.colostate.edu/projects/irc/public/Documents/Software/netCDF/cpp4/html/classnetcdf_1_1NcType.html> and NcDim<http://www.nrel.colostate.edu/projects/irc/public/Documents/Software/netCDF/cpp4/html/classnetcdf_1_1NcDim.html> objects must be non-null, and be defined in either the current group or a parent group. An NcNullType<http://www.nrel.colostate.edu/projects/irc/public/Documents/Software/netCDF/cpp4/html/classnetcdf_1_1NcNullType.html> exception is thrown if the NcType<http://www.nrel.colostate.edu/projects/irc/public/Documents/Software/netCDF/cpp4/html/classnetcdf_1_1NcType.html> object is invalid. An NcNullDim exception is thrown if the NcDim<http://www.nrel.colostate.edu/projects/irc/public/Documents/Software/netCDF/cpp4/html/classnetcdf_1_1NcDim.html> object is invalid. So the NcType must be defined in the current or parent group. How do I define a type in a group? I cannot see an appropriate function for doing this. Are types automatically "defined" in a group depending on what type of netCDF file has been opened (e.g. legacy, 3, 4 etc)? I am opening the file as netCDF::NcFile::nc4. Is this the correct way to add variables? In netcdf version 4.1.3, both work. In version 4.4.0 the first one works, but the second one doesn't. auto ncFloat = ncFile.getType("float",netCDF::NcGroup::ParentsAndCurrent); std::vector<float> myFloats = { 1., 2., 3., 4., 5. }; auto ncDim = ncFile.addDim("myFloatsDim", myFloats.size()); auto ncVar = ncFile.addVar("myFloats", ncFloat, {ncDim}); ncVar.putVar(myFloats.data()); auto ncString = ncFile.getType("string",netCDF::NcGroup::ParentsAndCurrent); std::vector<std::string> myStrings = { "a", "bb", "ccc", "dddd" }; auto ncDim = ncFile.addDim("myStringsDim", myStrings.size()); auto ncVar = ncFile.addVar("myStrings", ncString, {ncDim}); std::vector<const char *> myChars; for ( auto s : myStrings ) myChars.push_back( s.c_str() ); ncVar.putVar(myChars.data()); I am a bit confused as to why I am seeing this different behaviour with version 4.4.0. Best regards, Daniel ________________________________ From: Potter, Daniel (Energy, Newcastle) Sent: Monday, 5 December 2016 11:02 AM To: elizabeth.fischer@xxxxxxxxxxxx Cc: netcdfgroup@xxxxxxxxxxxxxxxx Subject: Re: [netcdfgroup] NcType problems on Ubuntu 16.04 Hi Elizabeth, Thank-you for your help and suggestion. I have tried using NcGroup::getType(), but I still get exceptions when I used non-netCDF3 types. e.g.: netCDF::NcFile ncFile("test.nc", netCDF::NcFile::replace, netCDF::NcFile::nc4); // works auto ncFloat = ncFile.getType("float",netCDF::NcGroup::ParentsAndCurrent); std::cout << "ncFloat.getName() = " << ncFloat.getName() << std::endl; // throws NcBadType auto ncString = ncFile.getType("string",netCDF::NcGroup::ParentsAndCurrent); std::cout << "ncString.getName() = " << ncString.getName() << std::endl; // throws NcBadType auto ncUint = ncFile.getType("uint",netCDF::NcGroup::ParentsAndCurrent); std::cout << "ncUint.getName() = " << ncUint.getName() << std::endl; Again, this works on Ubuntu 14.04 with netCDF 4.1.3 but not on Ubuntu 16.04 with netCDF 4.4.0. As far as I can tell, this means I cannot make variables of type string, uint etc? Best regards, Daniel ________________________________ From: Elizabeth A. Fischer <elizabeth.fischer@xxxxxxxxxxxx> Sent: Friday, 2 December 2016 5:07 PM To: Potter, Daniel (Energy, Newcastle) Cc: netcdfgroup@xxxxxxxxxxxxxxxx Subject: Re: [netcdfgroup] NcType problems on Ubuntu 16.04 Daniel, I believe you're using the "new" (NetCDF4) C++ interface? It has known problems with NcType, specifically for any types not in the NetCDF3 data model. I solved them by using strings instead of the symbolic enums; some of the API functions take strings. If I need a NcType object, I use the following function to get one: inline netCDF::NcType nc_type(netCDF::NcVar ncvar, std::string sntype) { return ncvar.getParentGroup().getType(sntype, netCDF::NcGroup::ParentsAndCurrent); } -- Elizabeth
netcdfgroup
archives: