title: How to read NetCDF "global attribute" in C++
body: <p>This is my first question:</p>
  
  <p>I am trying to read "global attributes" from a NetCDF file (using the C++ legacy API). By "global attribute" I mean an attribute that was added to a NcFile, not to a NcVar.</p>
  
  <p>For most things the "<a href="http://www.unidata.ucar.edu/software/netcdf/examples/programs/" rel="nofollow">Example netCDF programs</a>" are useful -- but there is no example for "global attributes".</p>
  
  <p>Consulting the "netcdfcpp.h" I find a few things:</p>
  
  <ul>
  <li>NcFile has a member function: <code>NcAtt* get_att(NcToken) const;</code></li>
  <li>NcAtt has NO public constructor</li>
  <li>NcAtt is friends with NcFile: <code>friend class NcFile;</code></li>
  <li>NcAtt has a private constructor: <code>NcAtt( NcFile*, NcToken);</code></li>
  <li>NcAtt has a public member function <code>NcValues* values( void ) const;</code></li>
  <li>NcValues has an API defined through the <code>ncvalues.h</code> header</li>
  </ul>
  
  <p>My coding skills are insufficient to understand how I get back at the string/int/float stored as NcValue, in an NcAtt class within NcFile.</p>
  
  <p>Attached is a example code of my problem "NetCDF_test.cpp", with the critical part missing from the implementation of the "LoadNetCDF" function.</p>
  
  <p>The code compiles OK with: (edit: also, "TestFile.nc" is created correctly)</p>
  
  <p><code>g++ -c NetCDF_test.cpp -o NetCDF_test.o</code></p>
  
  <p><code>g++ -o NCTEST NetCDF_test.o -lnetcdf_c++ -lnetcdf</code></p>
  
  <p>Example code:</p>
  
  <pre><code>#include &lt;iostream&gt; // provides screen output (i.e. std::cout&lt;&lt;)
  #include &lt;netcdfcpp.h&gt;
  
  struct MyStructure {
      std::string MyString;
      int MyInt;
      float MyFloat;
  
      MyStructure();      // default constructor
      int SaveNetCDF(std::string);  // Save the struct content to "global attributes" in NetCDF
      int LoadNetCDF(std::string);  // Load the struct content from "global attributes" in NetCDF
  
  };
  
  MyStructure::MyStructure(void)
  {
      MyString = "TestString";
      MyInt = 123;
      MyFloat = 1.23;
  }
  
  int MyStructure::SaveNetCDF(std::string OUTPUT_FILENAME)
  {
      NcError err(NcError::silent_nonfatal);
      static const int NC_ERR = 2;
      NcFile NetCDF_File(OUTPUT_FILENAME.c_str(), NcFile::Replace);
      if(!NetCDF_File.is_valid()) {return NC_ERR;}
  
      if(!(NetCDF_File.add_att("MyString",MyString.c_str()))) {return NC_ERR;}
      if(!(NetCDF_File.add_att("MyInt",MyInt))) {return NC_ERR;}
      if(!(NetCDF_File.add_att("MyFloat",MyFloat))) {return NC_ERR;}
  
      return 0;
  }
  
  int MyStructure::LoadNetCDF(std::string INPUT_FILENAME)
  {
  
      NcError err(NcError::silent_nonfatal);
      static const int NC_ERR = 2;
  
      NcFile NetCDF_File(INPUT_FILENAME.c_str(), NcFile::ReadOnly);
      if(!NetCDF_File.is_valid()) {return NC_ERR;}
  
      // ???? This is where I am stuck.
      // How do I read the global attribute from the NetCDF_File ??
      return 0;
  }
  
  
  int main()
  {
      std::cout&lt;&lt; "START OF TEST.\n";
  
      MyStructure StructureInstance;  // datamembers initialized by constructor
      StructureInstance.SaveNetCDF("TestFile.nc");
  
      StructureInstance.MyString = "Change string for sake of testing";
      StructureInstance.MyInt = -987; 
      StructureInstance.MyFloat = -9.87;
  
      StructureInstance.LoadNetCDF("TestFile.nc");    // data members are supposed to be read from file
  
      std::cout&lt;&lt; "Now the data members of StructureInstance should be TestString, 123, and 1.23\n";
      std::cout&lt;&lt; StructureInstance.MyString &lt;&lt; " ; " &lt;&lt; StructureInstance.MyInt &lt;&lt; " ; " &lt;&lt; StructureInstance.MyFloat &lt;&lt;"\n";
      std::cout&lt;&lt; "END OF TEST.\n";
  }
  </code></pre>