Re: [netcdfgroup] NcType problems on Ubuntu 16.04

  • To: <netcdfgroup@xxxxxxxxxxxxxxxx>
  • Subject: Re: [netcdfgroup] NcType problems on Ubuntu 16.04
  • From: <Daniel.Potter@xxxxxxxx>
  • Date: Tue, 6 Dec 2016 23:50:23 +0000
  • Ironport-phdr: 9a23:0+X6OxwamjBSlt7XCy+O+j09IxM/srCxBDY+r6Qd2uwSIJqq85mqBkHD//Il1AaPBtSAra8dwLKG+4nbGkU4qa6bt34DdJEeHzQksu4x2zIaPcieFEfgJ+TrZSFpVO5LVVti4m3peRMNQJW2aFLduGC94iAPERvjKwV1Ov71GonPhMiryuy+4ZPebgFGiTanYb5+MRq6oRnSu8ILnYZsN6E9xwfTrHBVYepW32RoJVySnxb4+Mi9+YNo/jpTtfw86cNOSL32cKskQ7NWCjQmKH0169bwtRbfVwuP52ATXXsQnxFVHgXK9hD6XpP2sivnqupw3TSRMMPqQbwoXzmp8qFmQwLqhigaLT406GHZhNJtgqJHrhyvpBJ/zIzVYI6JO/VzZbnScc8ASGdbQspdSzFND4WhZIUPFeoBOuNYopH6qlUAtxS+AwisC/3ryjNSnHH22rE60/g/HgHcxwEvA8kOsHXOrNXyLqsdS/21wbDOwD7eYf1W3jL955LJchAnufyDQbVwcc7WyUk0CwPKlEmQppL/MzyLy+sNrm6W5PdjW+K3k2MrtQ58riSvy8opkYbFmp4ZxkzB+Ch33Io5O9i1RUxmbdOhFZZdtCGXOo5rTs88Xm1koDs2x70YtZKhfyUHxo4rywDFZ/GIa4SI7AzsWeWNLTp9gX9oea6ziwqp/kWly+DwStO73EpPoyZbkdTMuG0C2hnR58SaV/dw+Fqq1yyV2ADJ8O5EJFg5la/cK5E83LE9joETsUHfHi/un0X2kbOWel0k+ue27+TnZa3rqYGTNoBoigHyL70imsmhDuQ8KAQOWXaU+Ouh1L3450H2XK5KjvwskqneqpzVP9kbqra4Aw9TzIkj9w6yAymp3dgEnXQKKUlKdA6bg4T1PlzDLuz0Aem6jlmujTtmwvXLM77hD5jIM3TPjqrtca5460FGyQozyd5f54hTCrEEOP/9VFX+tNrZDhAnNwy42fzpCNJh1oMCR22PGLSUP7/JsV+J/OIvJPOAa5UIuDrlMfgq++bujWMlmV8aZaSmw58XaHG5H/t8OEqWf2bsgtcbHWcEvwo+V/DliF2cXj5JfHu9Q6U85jUmCIKjFojDR5qijKaf0yimA51cfnpGBUyUEXf0a4WEXO8BaCSMLc99jjMLSLahS4A71RGpqQ/606FqLvfS+i0ZqJ3szsR16PfJmREv6TN7Fd6d33uTQG5pg2NbDwMxifR5rFB2xlmMy69lm9RFE91f7u9AFBoxPISawuBnXYPcQAXEK4OlQVCnT9HgOTYtUt8r69QVYlx6Xdy/2EOQlxG2CqMYwuTYTKc/9bjRiiD8
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
  • 2016 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: