[netcdfgroup] Issue with netcdf_cxx4 Nctype

Dear netCDF group

To be more precise the following code 


#include <string>
#include <netcdf>
#include <vector>
using namespace::std;
using namespace netCDF;
int main (int nbarg, char** argv) {
  
  netCDF::NcFile ncFile("test.nc", netCDF::NcFile::replace);
  NcType ncFloat = ncFile.getType("float",netCDF::NcGroup::ParentsAndCurrent);
  //NcType ncFloat=NC_FLOAT;
  std::vector<float> myFloats = { 1., 2., 3., 4., 5. };
  NcDim ncDim = ncFile.addDim("myFloatsDim", myFloats.size());
  NcVar ncVar = ncFile.addVar("myFloats", ncFloat, ncDim);
  ncVar.putVar(myFloats.data());
  
  
  NcType typeString = 
ncFile.getType("string",netCDF::NcGroup::ParentsAndCurrent);
  //NcType typeString = NC_STRING;
  //NcType typeString =ncString;
  std::vector<std::string> myStrings = { "a", "bb", "ccc", "dddd" };
  ncDim = ncFile.addDim("myStringsDim", myStrings.size());
  ncVar = ncFile.addVar("myStrings", typeString,ncDim);
  ncVar.putVar(myStrings.data());
}

Compiles with this command: g++ -g -std=c++11 testnetcdf.cc -lnetcdf_c++4 
-lnetcdf -lhdf5_hl -lhdf5 -lz
Gives thess results:

With libnecdf 4.4.0 and libnetcdfcxx4 4.2.1 (library given by ubuntu 16.04) 
this throws an exception at the line ncVar.putVar(myStrings.data()). If you add 
a try catch block it fails silently
With libnecdf 4.6.1 and libnetcdfcxx4 4.3.0 this cause a core dump. 
Nothing change is you use several definition of NcType.

But if you use char* in place of string:

  std::vector<const char *> myChars;
  
  for (const auto& it : myStrings) {
    myChars.push_back( it.c_str() );
  }
  ncVar.putVar(myChars.data());


It compiles, run and the file test.nc is correct with the last version of the 
library. With libnecdf 4.4.0 and libnetcdfcxx4 4.2.1 it throws the exception or 
failed as with the string.


Thanks for your attention

Virginie Trinité 


-----Message d'origine-----
De : netcdfgroup-bounces@xxxxxxxxxxxxxxxx 
[mailto:netcdfgroup-bounces@xxxxxxxxxxxxxxxx] De la part de TRINITE Virginie
Envoyé : jeudi 16 août 2018 17:38
À : 'netcdfgroup@xxxxxxxxxxxxxxxx List'
Objet : [netcdfgroup] Issue with netcdf_cxx4 Nctype

Dear netCDF group

I have the same problem that was related by Daniel Potter in this message 
https://www.unidata.ucar.edu/mailing_lists/archives/netcdfgroup/2016/msg00320.html

Further googling this problem, I see that it seems to come from this issue: 
https://github.com/Unidata/netcdf-cxx4/issues/30.
I do not understand exactly what the problem come from but I see that the last 
version of netcdf-cxx4 (v4.3.0) speaks to fix it.
So I compile the last version of netcdf-cxx4 and the last version of netcdf 
(4.6.1).
The problem is still their (impossible to add a variable of type string) but 
now it fails with a core dump that arrive in the HDF5 library and no more throw 
an exception.
I try the suggestion of Elizabeth A. Fischer but it seems that no type is 
associated with my NcFile.
Have you any suggestion?

Thanks for your attention

Virginie Trinité

_______________________________________________
NOTE: All exchanges posted to Unidata maintained email lists are recorded in 
the Unidata inquiry tracking system and made publicly available through the 
web.  Users who post to any of the lists we maintain are reminded to remove any 
personal information that they do not want to be made public.


netcdfgroup mailing list
netcdfgroup@xxxxxxxxxxxxxxxx
For list information or to unsubscribe,  visit: 
http://www.unidata.ucar.edu/mailing_lists/ 



  • 2018 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: