Go to file
2024-01-17 17:14:46 -08:00
cxx4 Updating build system to enforce C++11 standard. 2024-01-17 17:14:46 -08:00
docs docs migration 2021-11-09 16:20:52 -07:00
examples Catch exception by reference 2022-02-23 17:56:37 +00:00
m4 Added m4 directory; this will supress a warning otherwise thrown by 'autoreconf -i -f'. 2013-08-07 12:45:49 -06:00
plugins plugins: Add a missing -lhdf5 linker flag. 2021-05-07 09:40:11 -07:00
.gitignore docs migration 2021-11-09 16:20:52 -07:00
.travis.yml Updated travis.yml to respect .wif and .dmh branches as not requiring testing. 2018-03-07 13:53:43 -07:00
cmake_uninstall.cmake.in Adding install/uninstall framework similar to that found in netcdf-c. 2016-01-25 14:49:16 -07:00
CMakeInstallation.cmake fixed doc references 2021-11-09 16:43:57 -07:00
CMakeLists.txt Updating build system to enforce C++11 standard. 2024-01-17 17:14:46 -08:00
configure.ac Updating build system to enforce C++11 standard. 2024-01-17 17:14:46 -08:00
COPYRIGHT new 2011-08-17 18:56:14 +00:00
CTestConfig.cmake.in Added coveragte tests, other ctest-related configuration. 2015-04-15 03:29:27 -06:00
CTestCustom.cmake Initisl fleshing out of cmake file in cxx4 directory. 2015-04-13 01:42:05 -06:00
libnetcdf-cxx.settings.in Removed some unrelated stuff in libnetcdf-cxx.settings template. 2015-04-24 16:46:07 -06:00
Makefile.am docs migration 2021-11-09 16:20:52 -07:00
ncxx4-config.cmake.in Whitespace cleanup ncxx4-config 2019-06-07 16:02:46 +01:00
ncxx4-config.in Whitespace cleanup ncxx4-config 2019-06-07 16:02:46 +01:00
netcdf-cxx4.pc.in Fix multilib and underlinking problems in pkg-config files 2014-01-01 18:22:05 +01:00
netCDFCxxConfig.cmake.in CMake: Use namespaced target name for netCDF-C library 2020-05-16 13:49:25 +01:00
README.md Include example in readme 2019-06-11 14:28:18 +01:00
RELEASE_NOTES.md Updated release notes. 2019-12-02 15:16:31 -07:00
test_common.in Adding shell script infrastructure. 2019-07-08 09:39:16 -06:00

Build Status

netcdf-cxx4

Official GitHub repository for netCDF-4 C++ library.

Note: The latest release of the historic C++ libraries, netCDF-4.2, may be downloaded from the following page:

Introduction

Lynton Appel, of the Culham Centre for Fusion Energy (CCFE) in Oxfordshire, has developed and contributed a netCDF-4 C++ library that depends on an installed netCDF-4 C library. The netCDF-4 C++ API was developed for use in managing fusion research data from CCFE's innovative MAST (Mega Amp Spherical Tokamak) experiment.

Appel's C++ implementation is a complete read/write interface for netCDF-4, but can also be used as an alternative to the older netCDF-3 C++ interface, to write classic-format netCDF-3 files as well as netCDF-4 classic model files. The new API is implemented as a layer over the netCDF-4 C interface, which means bug fixes and performance enhancements in the C interface will be immediately available to C++ developers as well. It replaces a previous partial netCDF-4 C++ interface developed by Shanna Forbes.

The new API makes use of standard C++ features such as namespaces, exceptions, and templates, none of which were included in the first netCDF-3 C++ API developed in the mid-90's. The earlier netCDF-3 C++ API is still supported and available in the source distribution, but developers who are thinking of eventually upgrading to use of the enhanced data model should consider using Lynton's new API.

We're grateful for Appel's development and CCFE's contribution of the new open-source code for the netCDF-4 C++ API, and hope C++ developers in the netCDF community will find it useful! Feedback is appreciated, and should be directed to Lynton Appel.

Installation

The C++ interface requires the C library to have been build with the netCDF-4 API (this is the default in recent versions). You can check by running:

$ nc-config --has-nc4
yes

The simplest way to build the C++ interface is with CMake:

mkdir build
cd build
cmake ..
make
ctest
make install

Make sure that either nc-config is in your PATH, or that the location of netCDFConfig.cmake is in CMAKE_PREFIX_PATH.

There is also an autotools-based build system:

mkdir build
cd build
../configure
make
make check
make install

Note that the "configure" script must be generated using

autoreconf -if

To build the C++ interface guide, change to the cxx4 directory of the distribution and enter

doxygen

By default, HTML documentation will be installed in cxx4/doc/html; other options may be specified according to the settings contained in the file "Doxyfile" (details of alternative settings are documented at doxygen). Note that as a prerequisite for generating the documentation, the system will need to have doxygen and Graphviz installed.

Examples of usage

Here is an example of writing a 2D array to a file, and then reading it back in:

#include <iostream>
#include <netcdf>

// We are writing 2D data, a 6 x 12 grid
constexpr int nx = 6;
constexpr int ny = 12;

// Return this in event of a problem
constexpr int nc_err = 2;

int main() {
  // The default behavior of the C++ API is to throw an exception if
  // an error occurs
  try {
    // This is the data array we will write. It will just be filled
    // with a progression of numbers for this example.
    int dataOut[nx][ny];

    // Create some pretend data. If this wasn't an example program, we
    // would have some real data to write, for example, model output.
    for (int i = 0; i < nx; i++) {
      for (int j = 0; j < ny; j++) {
        dataOut[i][j] = i * ny + j;
      }
    }

    // Create the file. The Replace parameter tells netCDF to overwrite
    // this file, if it already exists.
    netCDF::NcFile dataFile("simple_xy.nc", netCDF::NcFile::replace);

    // Create netCDF dimensions
    auto xDim = dataFile.addDim("x", nx);
    auto yDim = dataFile.addDim("y", ny);

    // Define the variable. The type of the variable in this case is
    // ncInt (32-bit integer)
    auto data = dataFile.addVar("data", netCDF::ncInt, {xDim, yDim});

    // Write the data to the file. Although netCDF supports reading
    // and writing subsets of data, in this case we write all the data
    // in one operation.
    data.putVar(dataOut);

    // The file will be automatically close when the NcFile object goes
    // out of scope. This frees up any internal netCDF resources
    // associated with the file, and flushes any buffers.
  } catch (netCDF::exceptions::NcException &e) {
    std::cout << e.what() << std::endl;
    return nc_err;
  }

  // Now read the data back in
  try {
    // This is the array we will read into
    int dataIn[nx][ny];

    // Open the file for read access
    netCDF::NcFile dataFile("simple_xy.nc", netCDF::NcFile::read);

    // Retrieve the variable named "data"
    auto data = dataFile.getVar("data");
    if (data.isNull())
      return nc_err;
    data.getVar(dataIn);

    // Check the values.
    for (int i = 0; i < nx; i++) {
      for (int j = 0; j < ny; j++) {
        if (dataIn[i][j] != i * ny + j) {
          return nc_err;
        }
      }
    }
  } catch (netCDF::exceptions::NcException &e) {
    std::cout << e.what() << std::endl;
    return nc_err;
  }
}