Git Product home page Git Product logo

netcdf-cxx4's Introduction

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;
  }
}

netcdf-cxx4's People

Contributors

5tefan avatar archangegabriel avatar bilke avatar edhartnett avatar firegurafiku avatar garyjg avatar hellkite500 avatar hmaarrfk avatar htmlboss avatar jarlela avatar jlec avatar jschueller avatar kmuehlbauer avatar luthaf avatar nschloe avatar opoplawski avatar orbea avatar oxelson avatar russrew avatar slayoo avatar twsearle avatar wardf avatar zedthree avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

netcdf-cxx4's Issues

Documentation?

Links to online documentation from the top of example files are dead:

Full documentation of the netCDF C++ API can be found at:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-cxx

Unfortunately, some search engines are also linking there for queries on netCDF C++ documentation and some internal Unidata links point there.

Also the directory cxx4 from * has no Doxyfile so running doxygen as instructed by the README does nothing.

*Edit: My mistake, I was building from the release linked here http://www.unidata.ucar.edu/downloads/netcdf/netcdf-cxx/index.jsp, which is little out of date.

4.3.1 requires HDF5 but configure doesn't support it

The 4.3.1 release requires HDF5 for the bzip2 sources, however configure doesn't support HDF5 and won't complain if hdf5.h is not present/found.

As hdf5.h & libhdf5.so are installed in a non-standard locations on Debian systems (/usr/include/hdf5/serial & /usr/lib/$(DEB_HOST_MULTIARCH)/hdf5/serial respectively), having the libhdf5-dev package installed is not sufficient.

OS: Debian GNU/Linux bullseye/sid
Buildsystem: Autotools

Add ncFile::redef()

While reviewing the C++ code for the upcoming release (including additions to bring the C++ API up to parity with the C API), it appears that there is no way to invoke nc_redef() from the C++ API. This needs to be addressed, and will be shortly. Tagging @AodhanSweeney to take a look at this.

Is netCDF-C 4.3.3 compatible?

Hello,

I'm using VS 2013 + pre-built netCDF-C 4.3.3 lib to run this example. But the compiler keeps telling me that some unresolved external symbols. Do you have any similar report or any solutions? Or it just because I didn't link the dll and lib correctly, do you have some detailed installment instruction.

NetCDF-CXX4 Fails a Unit Test

The unit test cxx4_test_type is failing, I believe on the type ncInt64. I've build things here with HDF5 enabled. As you can see below in the examples, this NetCDF had no problem writing nc4 files.

In real life (outside of unit tests), I'm not able to create any int64 variables with netcdf-cxx4. (I AM able to do so via ncgen, or via the Python interface).

Attached are build logs for netcdf and netcdf-cxx4.

netcdf-build.txt
netcdf-cxx4-build.txt

Help... any ideas?

Thank you,
-- Elizabeth

[me@ankeli cxx4]$ ./cxx4_test_type 
Opening file "firstFile.cdf" with NcFile::replace
Testing addGroup("groupName")                                -----------   passed
Testing getName()                                        unknown error
NetCDF: Not a valid data type or _FillValue type mismatch
file: /home/me/tmp/netcdf-cxx4/cxx4/ncType.cpp  line:97[me@ankeli cxx4]$ 
[me@ankeli cxx4]$ ../examples/
CMakeFiles/                    examples_sfc_pres_temp_rd      examples_simple_xy_wr
examples_pres_temp_4D_rd       examples_sfc_pres_temp_wr      examples_simple_xy_wr_formats
examples_pres_temp_4D_wr       examples_simple_xy_rd          
[me@ankeli cxx4]$ ../examples/examples_simple_xy_wr_formats 
*** SUCCESS creating nc4 file
*** SUCCESS creating nc4classic file
*** SUCCESS creating classic file
*** SUCCESS creating classic64 file
[me@ankeli cxx4]$ git info
== Remote URL: origin   https://github.com/Unidata/netcdf-cxx4.git (fetch)
origin  https://github.com/Unidata/netcdf-cxx4.git (push)

== Local Branches:
* master

== Configuration (.git/config)
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = https://github.com/Unidata/netcdf-cxx4.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
    rebase = true

== Most Recent Commit
commit f42eb9dbbcde1e1e232129f053442657f84cf390
Merge: 365ab9c 0bf071d
Author: Ward Fisher <[email protected]>
Date:   Fri Mar 4 16:00:56 2016 -0700

    Merge branch 'master' into doxygen

Type 'git log' for more commits, or 'git show' for full commit details.


[me@ankeli cxx4]$ ldd ./cxx4_test_type | grep hdf5
    libhdf5_hl.so.10 => /home/me/spack2/opt/spack/linux-x86_64/gcc-4.9.3/hdf5-1.8.16-3dh2iibcu4ahjzgujcruz6b7agezsba5/lib/libhdf5_hl.so.10 (0x00007f74a3a63000)
    libhdf5.so.10 => /home/me/spack2/opt/spack/linux-x86_64/gcc-4.9.3/hdf5-1.8.16-3dh2iibcu4ahjzgujcruz6b7agezsba5/lib/libhdf5.so.10 (0x00007f74a3555000)

Issue with using a local build of netcdf-c and netcdf-c++

I'm running Ubuntu 20, netcdf-c 4.7.4 and the "working" branch of netcdf-cxx.

I'm compiling netcdf with

cmake -D CMAKE_INSTALL_PREFIX=~/Projects/libraries/netcdf-c/install

I'm compiling netcdf-cxx with the command:

cmake -D CMAKE_PREFIX_PATH=~/Projects/libraries/netcdf-c/install -D CMAKE_INSTALL_PREFIX=~/Projects/libraries/netcdf-cxx/install ..

In my own application where I would like to use those two librarires I'm compiling with

cmake -D CMAKE_PREFIX_PATH="~/Projects/libraries/netcdf-c/install;~/Projects/libraries/netcdf-cxx/install" ..

In my cmake file the netcdf related things are

find_package(netCDF REQUIRED)

and

target_link_libraries(${PROJECT_NAME}
    netcdf
    netcdf_c++4
    )

Both netcdf-c and netcdf-xx compile and install fine, but when I try to build my application with make I get the following error message: "/usr/local/include/netcdf:5:10: fatal error: netcdf.h: No such file or directory"

I'm not sure why it's not finding the netcdf-cxx header files. Furthermore when I try to use "find_package(netCDFCxx REQUIRED)
" i get the cmake error "Could not find a package configuration file provided by "netCDFCxx" with any of the following names:`

Do you have any idea where I may be going wrong? Please let me know if I can provide any additional information.

Documentation

I would reopen #27 if I could, as the Unidata website remains in unfortunate condition.

The situation could be slightly improved by providing the C++ interface documentation via Github Pages. Since the docs/ dir is occupied, the easiest way to do this is probably with a gh-pages branch.

I've published the documentation on my own fork. It would be better to have that available from this repo though.

Having run Doxygen and with gh-pages installed from the nodejs ecosystem, publishing the documentation is as simple as

gh-pages -d cxx4/doc/html/

Please consider doing this! Thanks.

Conan Package

Hello there,

I'm currently working with netcdf-cxx4 in a project. Everything works great so far. I also started to look into DevOps tools for c++ recently, and started to use Conan to manage my c++ dependencies. I figure I can't be the only one who would really like there to be an official Conan package.

I tried creating a package myself, but couldn't get it to work. I had struggles getting netcdf-cxx4 to link to hdf inside the conanfile.py. I then found this repository: conan-netcdf-cxx4, but I could not get that to work either.

I would of course be open to work on this as well. I was already able to create hdf5 and netcdf-c conan packages. So I could provide those.

Append data to existing NetCDF file

Hello there,

This is not much of an issue, but more of a "how-to" question, because I failed miserably searching online for solutions. How to append new values to an existed variable in an existed NetCDF file?

Normally when we write the data to the file, we create an object with all the data and then call put once to put all the data to the file. The problem I'm trying to solve is that, because of the RAM limit, I can't read the entire data into RAM at once, which forces me to add the data to the file multiple times. How could I do that?

Thank you very much. And if I'm posting at a wrong place, please inform me the proper place to seek for answers.

Configure failed: NetCDF must be built with netCDF-4 enabled

Hi,

I tried to install netcdf-cxx4.3.0 on a cluster called the TSCC (Triton Shared Computing Cluster) in UCSD, California, USA. But when configuring, I got a failure like:
configure: error: NetCDF must be built with netCDF-4 enabled.
I want to ask if anyone knows what might go wrong.

Let me explain the details:

(A) Background:
(1) TSCC was a cluster that already had many softwares to use, including netCDF, but seemingly only the version for C. Since I wanted to use a framework called BOUT++, which required netcdf-c++, I tried to install netcdf-c++ in my (2) personal directory.
(3) The directory of TSCC's netcdf-c: /opt/netcdf/4.3.2/gnu/openmpi_ib/
(4) The directory I installed local netcdf-c++: /oasis/tscc/scratch/fchang/netcdf4/netcdf-cxx4-4.3.0/install

(B) Steps I took:
(1) cd /oasis/tscc/scratch/fchang/netcdf4
(2) wget ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-cxx4-4.3.0.tar.gz
(3) tar -xzvf netcdf-cxx4-4.3.0.tar.gz
(4) cd netcdf-cxx4-4.3.0
(5) mkdir install
(6) ./configure -prefix="/oasis/tscc/scratch/fchang/netcdf4/netcdf-cxx4-4.3.0/install" CPPFLAGS="-I/opt/netcdf/4.3.2/gnu/openmpi_ib/include" LDFLAGs="-L/opt/netcdf/4.3.2/gnu/openmpi_ib/lib" LD_LIBRARY_PATH=/opt/netcdf/4.3.2/gnu/openmpi_ib/lib:$LD_LIBRARY_PATH

(C) Error message
(1) The error message shown:
checking for library containing nc_create... no
checking for nc_def_opaque... no
checking for nccreate... no
checking for nc_set_log_level... no
checking for oc_open... no
checking for nc_use_parallel_enabled... no
configure: error: NetCDF must be built with netCDF-4 enabled.
(2) I've also tried installing zlib-1.2.11 >> hdf5-1.10.5 >> netcdf-c-4.7.0 in my directory successfully (confirmed by make check), and intalling netcdf-cxx4-4.3.0 by including the local netcdf-c-4.7.0. But this still gave the same error message.
(3) I put a full version of the message shown, and the config.log in this zip file:
Failure Report.zip

I hope someone is willing to help me on this.
Thanks!

Installation directory for windows dll

When cross building for windows with mingw, the usual convention is to install dlls into bin rather than lib. The small change below achieves this:

diff --git a/cxx4/CMakeLists.txt b/cxx4/CMakeLists.txt
index 431eb45..9d55faa 100644
--- a/cxx4/CMakeLists.txt
+++ b/cxx4/CMakeLists.txt
@@ -44,4 +44,5 @@ INSTALL(
 INSTALL(
   TARGETS netcdf-cxx4
   DESTINATION ${CMAKE_INSTALL_LIBDIR}
+  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
   )

const return values

With Intel 17.0.0, I'm getting warnings like:

ncDim.h(47): warning #858: type qualifier on return type is meaningless
const int getId() const {return myId;};

for every include of <netcdf>. Is there any reason why a const int is returned instead of a int?

Unprefixed cache variables in CMakeLists.txt

Hi all!

While I was making changes for #34, I've noticed that the project's CMakeLists.txt defines a lot of cache variables with simple, unprefixed names, and I think they should be fixed. Please, don't treat this message as a critic attack. Of course, I could rename those variables myself, but I cannot PR such a breaking change without a prior discussion.

set(PACKAGE "netcdf-cxx4" CACHE STRING "")
SET(BUILDNAME_PREFIX "" CACHE STRING "")
SET(BUILDNAME_SUFFIX "" CACHE STRING "")
SET(BUILDNAME "${TMP_BUILDNAME}" CACHE STRING "Build name variable for CDash")
set(TMP_BUILDNAME "${osname}-${osrel}-${cpu}" CACHE STRING "Build name variable for CDash")

I don't exactly know how CDash operates, but does it really require us putting such names into CMake cache? May they be turned into, say, NCXX_DASH_PACKAGE and NCXX_DASH_BUILDNAME? Maybe it's also worth adding NCXX_ENABLE_DASH option in order not to pollute end user's cache unless they asked?

SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/"
  CACHE INTERNAL "Location of our custom CMake modules.")

Does it really have to be a cache variable? Also, isn't it better to write `list(APPEND CMAKE_MODULE_PATH ...)" in order to have previous search path lost?

# Set the build type.
IF(NOT CMAKE_BUILD_TYPE)
  SET(CMAKE_BUILD_TYPE DEBUG CACHE STRING
       "Choose the type of build, options are: None, Debug, Release."
    FORCE)
ENDIF()

AFAIK, CMAKE_BUILD_TYPE is always in cache. It may be empty, though, but it's not worse than "None" option. Also, why to enforce debug value?

SET(CTEST_MEMORYCHECK_COMMAND valgrind CACHE STRING "")

What about just find_program(NCXX_VALGRIND_COMMAND valgrind REQUIRED) which would create corresponding cache item automatically?

SET(HAVE_DOT YES CACHE STRING "")
SET(SITE "${NCXX_CTEST_SITE}" CACHE STRING "")
SET(BUILD_PARALLEL ${NC_IS_PARALLEL} CACHE STRING "")

Some more names which require prefixing.


Note that I'm thinking about the situation when NetCDF-C++ is a part of large software project linking lots of libraries which are sharing single global cache.

Cmake macro ERROR Microsoft Visual Studio generator

When trying to build netCDF-cxx4 on WIndows using CMake the following error occurs:
CMake Error at CMakeLists.txt:405 (CHECK_LIBRARY_EXISTS):
CHECK_LIBRARY_EXISTS Macro invoked with incorrect arguments for macro named: CHECK_LIBRARY_EXISTS

The problem lies couple of line prior to that:
400: FIND_PACKAGE(HDF5 NAMES ${SEARCH_PACKAGE_NAME} COMPONENTS C HL NO_MODULES REQUIRED ${NC_HDF5_LINK_TYPE})
This line implies that find_package() runs in the config mode, thus it will not create variable HDF5_C_LIBRARY_hdf5 that is used in line 405.

I fixed it with using FIND_PACKAGE(HDF5 COMPONENTS C HL REQUIRED ${NC_HDF5_LINK_TYPE})
at line 400 and changing the name of the HDF5_C_LIBRARY_hdf5 variable to HDF5_C_LIBRARIES
which is actually getting created by FindHDF5.cmake module.

Typesafe attribute accessors

class NcAtt needs modern typesafe ways to retrieve attribute values. This is especially true for retrieving multiple (vector) valued strings.

The following methods should be included:

    getValue(TypeT &)      // Retrieves the first value
    getValues(std::vector<TypeT> &)     // Retrieves all values

"getValues"(string) makes no sense: it retrieves a single value, not all values.

test_type addMember()/getMemberNameFromValue() fails on ppc64 (big-endian)

This test is failing on Fedora Rawhide ppc64:

    cout <<left<<setw(57)<<"Testing creating new Enum Type";
    NcEnumType enumType1(ncFile.addEnumType("enumType_1",NcEnumType::nc_SHORT));
    cout <<"    -----------   passed\n";

    cout <<left<<setw(57)<<"Testing NcEnumType::addMember()";
    enumType1.addMember("Monday",1);
    enumType1.addMember("Tuesday",7);
    enumType1.addMember("Wednesday",-20);
    cout <<"    -----------   passed\n";

    cout <<left<<setw(57)<<"Testing NcEnumType::getBaseType() == and !=";
    if(enumType1.getBaseType() != ncShort) throw NcException("Error in test 20.1",__FILE__,__LINE__);
    if(!(enumType1.getBaseType() == ncShort)) throw NcException("Error in test 20.2",__FILE__,__LINE__);
    cout <<"    -----------   passed\n";

    cout <<left<<setw(57)<<"Testing NcEnumType::getMemberCount()";
    if(enumType1.getMemberCount() != 3) throw NcException("Error in test 21.1",__FILE__,__LINE__);
    cout <<"    -----------   passed\n";

    cout <<left<<setw(57)<<"Testing NcEnumType::getMemberNameFromIndex(index)";
    if(enumType1.getMemberNameFromIndex(0) != "Monday") throw NcException("Error in test 22.1",__FILE__,__LINE__);
    if(enumType1.getMemberNameFromIndex(1) != "Tuesday") throw NcException("Error in test 22.2",__FILE__,__LINE__);
    if(enumType1.getMemberNameFromIndex(2) != "Wednesday") throw NcException("Error in test 22.3",__FILE__,__LINE__);
    cout <<"    -----------   passed\n";

    cout <<left<<setw(57)<<"Testing NcEnumType::getMemberNameFromValue(index)";
    if(enumType1.getMemberNameFromValue(1) != "Monday") throw NcException("Error in test 23.1",__FILE__,__LINE__);

This is because at the initial setup:

    enumType1.addMember("Monday",1);

addMember() ends up getting called:

netCDF::NcEnumType::addMember<int> (name="Tuesday", memberValue=7, this=<optimized out>, 
    this=<optimized out>) at ../../cxx4/ncEnumType.h:73
73              ncCheck(nc_insert_enum(groupId, myId, name.c_str(), (void*) &memberValue),__FILE__,__LINE__);

But since the enum type was set to NcEnumType::nc_SHORT, only 2 bytes of the value is copied into the member value:

#0  NC4_insert_enum (ncid=<optimized out>, typeid1=35, identifier=0x3fffffffe380 "Tuesday", 
    value=0x3fffffffd938) at ../../libsrc4/nc4type.c:696
(gdb) print *type
$6 = {l = {next = 0x0, prev = 0x20081190}, name = 0x20081680 "enumType_1", nc_typeid = 35, 
  rc = 1, hdf_typeid = 0, native_hdf_typeid = 0, endianness = 0, size = 2, committed = NC_FALSE, 
  nc_type_class = 15, u = {e = {num_members = 1, enum_member = 0x20081160, base_nc_typeid = 3, 
      base_hdf_typeid = 0}, c = {num_fields = 1, field = 0x20081160}, v = {base_nc_typeid = 1, 
      base_hdf_typeid = 0}}}
(gdb) 
903        memcpy(member->value, value, size);
(gdb) print *(int *)value
$8 = 7
(gdb) print *(short *)value
$9 = 0
(gdb) print member->value
$10 = (void *) 0x20081780
(gdb) print *(int *)member->value
$11 = 0
(gdb) print *(short *)member->value
$12 = 0

This truncation works okay on little-endian, but not big-endian. I can fix the test with:

    enumType1.addMember("Monday",(short)1);
    enumType1.addMember("Tuesday",(short)7);
    enumType1.addMember("Wednesday",(short)-20);

But I'm not sure that this is the best way to cast these values or if there is some other expectation that just work without the cast.

NetCDF-C++4 API v.4.3.1: unused variable warning

Hello, the latest C++4 APIs v.4.3.1 have the following compiler warning

netcdf4/include/ncGroup.h:18:14: error: โ€˜netCDF::file_idโ€™ defined but not used [-Werror=unused-variable]
static int file_id;
^

which is preventing compilation with warning treated as errors in programs that compile and link against NetCDF shared objects + include files.

Fail to compile the example code

Hey,
I have just installed the netcdf-cxx4 package according to the installation steps.
I am trying to compile the example code with both g++ and clang++ compilers, but none of them works. From the clang++ compiler, I get numerous errors, and the g++ compiler cannot find the netcdf source file (" fatal error: netcdf: No such file or directory").

This is the output of nc-config --all :
`This netCDF 4.6.1 has been built with the following features:

--cc -> /opt/concourse/worker/volumes/live/a8e407e3-d4ec-44ee-693f-1c4d86eb0106/volume/libnetcdf_1539979321820/_build_env/bin/x86_64-apple-darwin13.4.0-clang
--cflags -> -I/Users/gilaverbuch/miniconda3/include
--libs -> -L/Users/gilaverbuch/miniconda3/lib -lnetcdf -lmfhdf -ldf -lhdf5_hl -lhdf5 -lpthread -lz -ldl -lm -lcurl

--has-c++ -> no
--cxx ->

--has-c++4 -> yes
--cxx4 -> /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
--cxx4flags -> -I/usr/local/include
--cxx4libs -> -L/usr/local/lib -lnetcdf-cxx4 -lnetcdf

--has-fortran-> yes
--fc -> nf-config not yet implemented for cmake builds
--fflags -> nf-config not yet implemented for cmake builds
--flibs -> nf-config not yet implemented for cmake builds
--has-f90 -> nf-config not yet implemented for cmake builds
--has-f03 -> nf-config not yet implemented for cmake builds

--has-dap -> yes
--has-dap2 -> yes
--has-dap4 -> yes
--has-nc2 -> yes
--has-nc4 -> yes
--has-hdf5 -> yes
--has-hdf4 -> yes
--has-logging-> yes
--has-pnetcdf-> no
--has-szlib -> no
--has-cdf5 -> yes
--has-parallel-> no

--prefix -> /Users/gilaverbuch/miniconda3
--includedir-> /Users/gilaverbuch/miniconda3/include
--libdir -> /Users/gilaverbuch/miniconda3/lib
--version -> netCDF 4.6.1`

Does anyone have an idea of how to solve it?

Thanks a lot!

Exception handling with NcException

Hi, I'm pretty new to C++, so please let me know if I've got this wrong.

In the example files the catch-all exception handling looks something like this:

try
   {
        /* Code that might throw an exception */
   }
   catch(NcException& e) // Catch-all exceptions
     {
       e.what(); // Will this print exception information? It should return a const char *, right?
       cout<<"FAILURE*************************************"<<endl;
       return NC_ERR;
     }

What I am wondering about is how do you actually get any useful information out of the exception handling? I've tried to read a NcFile into a map like so:

try
{
    NcFile datafile(filename, NcFile::read);
    NcVar data = datafile.getVar("data");

    map<string, NcVarAtt> m = data.getAtts();

    if (data.isNull())
        cout << "Data is null" << endl;
}
catch (NcException& e)
{
    // Neither e.what() or string(e.what()) prints anything useful
    cerr << "An exception occured while reading a file: " << e.what() << endl;
}

Which essentially prints garbage values:

An exception occured while reading a file: p๏ฟฝ

If I remove the whole try-catch block and just don't handle the exception, i get this:

terminate called after throwing an instance of 'netCDF::exceptions::NcBadId'
  what():  
Aborted (core dumped)

Any idea why the e.what() function only returns garbage values (the "p" seems to always crop up)?
As per the documentation for std::exception::what it should return a const char *.
Or have i completely misunderstood exception handling in C++?

Building shared libraries on Windows is not possible

There is a problem with building shared libraries with Cmake on Windows.
If BUILD_SHARED_LIBS is ON the following happens:
netcdf-cxx4.dll,
netcdf-cxx4.ilk,
netcdf-cxx4.pbd
are created bu netcdf-cxx4.lib is not, however in the macro add_bin_tests netcdf-cxx4.lib is specifically linked.
The cause of the problem is that there are no exports specified in the sources for netcdf-cxx4 library thus VS does not create a import .lib library.

getVar not correctly returning variable values

I'm seeing some very odd behaviour with the getVar method. Specifically, I'm seeing it return what looks to be uninitialised data, 0s, and repeated data when reading back an entire record's grid.

I'm seeing this behaviour across the following machines:
Machine details:
Fedora 27 -- netcdf-cxx4-devel 4.3.0-3 (from dnf); netcdf-4.4.1.1-6 (from dnf)
MacOSX High Sierra-- netcdf-cxx4 (built from git); netcdf-4.5.0 (via homebrew)
Ubuntu 16.04 -- netcdf (built from src, 4.5.1.-dev); netcdf-cxx4 (latest git); hdf5 5.1.10.1 (built from src)
Building with gcc and clang.

Test program is as follows:

#include <iostream>
#include <netcdf>
#include <string>

int main()
{

 	netCDF::NcFile data;
 	data.open("small_nc.nc", netCDF::NcFile::read);
	
	 auto dims = data.getDims();
    for(auto itr : dims)
    {
    	std::cout << itr.first << "\t"<<itr.second.getSize()<< std::endl;
    }

	const size_t datetime_length = 6;
 	const int NLAT = 4;
	const int NLON = 4;


	double lat[NLAT][NLON];
	double lon[NLAT][NLON];

	// read 1 record
	std::vector<size_t> startp, countp;
    startp.push_back(0);
    startp.push_back(0);
    startp.push_back(0);

    countp.push_back(1);
    countp.push_back(NLAT);
    countp.push_back(NLON);

	netCDF::NcVar latVar, lonVar;
	latVar = data.getVar("gridlat_0");
	if(latVar.isNull()) return -1;

	lonVar = data.getVar("gridlon_0");
	if(lonVar.isNull()) return -1;


	latVar.getVar(startp,countp, lat);
	lonVar.getVar(startp,countp, lon);

    for(int y = 0; y < NLAT; y++)
    {
    	for(int x = 0; x < NLON; x++ )
    	{
    		std::cout << "array -- lat=" << lat[y][x] << "\tlon=" << lon[y][x] << std::endl;


            std::vector<size_t> startp_2, countp_2;
            startp_2.push_back(0);
            startp_2.push_back(y);
            startp_2.push_back(x);

            countp_2.push_back(1);
            countp_2.push_back(1);
            countp_2.push_back(1);

            double dlat,dlon;
            latVar.getVar(startp_2,countp_2, &dlat);
            lonVar.getVar(startp_2,countp_2, &dlon);

            std::cout << "index -- lat=" << dlat << "\tlon=" << dlon << std::endl;
     	}
    }
	return 0;
}

I get the following output

datetime	6
xgrid_0	4
ygrid_0	4
array -- lat=8.06696e+10	lon=-6.29051e+14
index -- lat=44.6896	lon=-129.906
array -- lat=8.0874e+10	lon=-6.27106e+14
index -- lat=44.6896	lon=-129.906
array -- lat=6.9101e-310	lon=0
index -- lat=44.6896	lon=-129.906
array -- lat=6.9101e-310	lon=0
index -- lat=44.6896	lon=-129.906
array -- lat=0	lon=0
index -- lat=44.6956	lon=-129.879
array -- lat=0	lon=0
index -- lat=44.6956	lon=-129.879
array -- lat=9.88131e-324	lon=0
index -- lat=44.6956	lon=-129.879
array -- lat=0	lon=0
index -- lat=44.6956	lon=-129.879
array -- lat=0	lon=0
index -- lat=44.7015	lon=-129.851
array -- lat=0	lon=0
index -- lat=44.7015	lon=-129.851
array -- lat=0	lon=6.91692e-323
index -- lat=44.7015	lon=-129.851
array -- lat=0	lon=6.91692e-323
index -- lat=44.7015	lon=-129.851
array -- lat=3.45846e-323	lon=0
index -- lat=44.7075	lon=-129.823
array -- lat=0	lon=0
index -- lat=44.7075	lon=-129.823
array -- lat=0	lon=-nan
index -- lat=44.7075	lon=-129.823
array -- lat=0	lon=-nan
index -- lat=44.7075	lon=-129.823

As you can see, the array reads are a mess, where the single value read appears to be correct. However, on a much larger and production ncfile, I get output like the following:

[index -- lat=45.732	lon=-123.944
array -- lat=0	lon=0
index -- lat=45.732	lon=-123.944
array -- lat=0	lon=0
index -- lat=45.732	lon=-123.944
array -- lat=0	lon=0
index -- lat=45.732	lon=-123.944
array -- lat=0	lon=0
index -- lat=45.732	lon=-123.944
array -- lat=0	lon=0
index -- lat=45.732	lon=-123.944
array -- lat=0	lon=0
index -- lat=45.732	lon=-123.944
array -- lat=0	lon=0
index -- lat=45.732	lon=-123.944
array -- lat=0	lon=0
index -- lat=45.732	lon=-123.944
array -- lat=0	lon=0
index -- lat=45.732	lon=-123.944
array -- lat=0	lon=0
index -- lat=45.732	lon=-123.944
array -- lat=0	lon=0
index -- lat=45.732	lon=-123.944
array -- lat=0	lon=0
index -- lat=45.732	lon=-123.944
array -- lat=0	lon=0
index -- lat=45.732	lon=-123.944
array -- lat=0	lon=0
index -- lat=45.732	lon=-123.944
array -- lat=0	lon=0
index -- lat=45.732	lon=-123.944
array -- lat=0	lon=0
index -- lat=45.732	lon=-123.944
array -- lat=0	lon=0
index -- lat=45.732	lon=-123.944

where the array calls all return 0, and the individual calls end up returning duplicate data.

I've attached the files for this mwe below. It contains the tests data I've been using.
mwe.zip

Build fails due to missing LIBADD dependency

the below code solved the build failures on cygwin

--- origsrc/netcdf-cxx4-4.3.1/cxx4/Makefile.am  2019-09-12 20:34:58.000000000 +0200
+++ src/netcdf-cxx4-4.3.1/cxx4/Makefile.am      2019-11-29 14:49:21.929609300 +0100
@@ -13,6 +13,8 @@ lib_LTLIBRARIES = libnetcdf_c++4.la
 # http://www.gnu.org/s/libtool/manual/html_node/Updating-version-info.html
 libnetcdf_c__4_la_LDFLAGS = -version-info 2:0:1 -no-undefined

+libnetcdf_c__4_la_LIBADD = -lnetcdf
+
 # These headers will be installed in the users header directory.
 include_HEADERS = netcdf ncAtt.h ncCheck.h ncDim.h ncException.h       \
 ncGroup.h ncOpaqueType.h ncVar.h ncVlenType.h ncCompoundType.h         \
--- origsrc/netcdf-cxx4-4.3.1/plugins/Makefile.am       2019-09-12 20:34:58.000000000 +0200
+++ src/netcdf-cxx4-4.3.1/plugins/Makefile.am   2019-11-29 15:09:25.382242000 +0100
@@ -20,9 +20,11 @@ lib_LTLIBRARIES = libh5bzip2.la

 libh5bzip2_la_SOURCES = ${HDF5PLUGINSRC}
 libh5bzip2_la_LDFLAGS = -module -avoid-version -shared -export-dynamic -no-undefined
+libh5bzip2_la_LIBADD = -lhdf5

 libmisc_la_SOURCES = H5Zmisc.c H5Zutil.c h5misc.h
 libmisc_la_LDFLAGS = -module -avoid-version -shared -export-dynamic -no-undefined -rpath ${abs_builddir}
+libmisc_la_LIBADD = -lhdf5


 EXTRA_DIST=${PLUGINSRC} ${BZIP2SRC} ${PLUGINHDRS} ${BZIP2HDRS} \

Undefined Reference to netCDF

To report a non-security related issue, please provide:

*NETcdf 4.3.1 c++

  • Linux Mint, c++, cmake 3.17.3
  • Have gone through the install procedure which worked after moving some hdf5 files to the location it was looking for them. Fully completed and tests successful. Then tried to run a simple program with following commands.

#include

netCDF::NcFile dataFile("simple_xy.nc", netCDF::NcFile::replace);

program then crashes with the following

/home/matthew/CLionProjects/2DCodeVersion1.0/Diff_Water_Tensor_2D.cpp:14: undefined reference to netCDF::NcFile::NcFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, netCDF::NcFile::FileMode)' /home/matthew/CLionProjects/2DCodeVersion1.0/Diff_Water_Tensor_2D.cpp:14: undefined reference to netCDF::NcFile::~NcFile()'

NetCDF-C++: Build on Windows 10 x64

Hi,

I already downloaded binary NetCDF-C installer (called netCDF4.7.1-NC4-DAP-64.exe) from the offical website (link here) installed it and add the installed folder to my PATH.
So I tested it in the Windows console runing: ncdump.exe...and all works good!

Now I cloned the NetCDF-CXX4 repository and tried to compile it, but I got a lot of CMake errors.

My system is:
Windows 10 x64
Visual Studio 2019 Enterprise
CMake 3.14.4
NetCDF-CXX 4.3.1

What I did:
Inside the cloned NetCDF-CXX4 folder, I created a folder called "build".
Inside the build folder I typed: cmake ..

Here is the console error:

 cmake ..
-- Building for: Visual Studio 16 2019
-- The C compiler identification is MSVC 19.23.28105.4
-- The CXX compiler identification is MSVC 19.23.28105.4
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x64/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found bash: C:/Program Files/Git/usr/bin/bash.exe
-- Looking for nc_use_parallel_enabled in netcdf
CMake Error at C:/Users/lamar/Desktop/netcdf-cxx4-4.3.1/build/CMakeFiles/CMakeTmp/CMakeLists.txt:17 (add_executable):
  Target "cmTC_7cc31" links to target "hdf5::hdf5-shared" but the target was
  not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?


CMake Error at C:/Users/lamar/Desktop/netcdf-cxx4-4.3.1/build/CMakeFiles/CMakeTmp/CMakeLists.txt:17 (add_executable):
  Target "cmTC_7cc31" links to target "hdf5::hdf5_hl-shared" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?


CMake Error at C:/Users/lamar/Desktop/netcdf-cxx4-4.3.1/build/CMakeFiles/CMakeTmp/CMakeLists.txt:17 (add_executable):
  Target "cmTC_7cc31" links to target "hdf5::hdf5-shared" but the target was
  not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?


CMake Error at C:/Users/lamar/Desktop/netcdf-cxx4-4.3.1/build/CMakeFiles/CMakeTmp/CMakeLists.txt:17 (add_executable):
  Target "cmTC_7cc31" links to target "hdf5::hdf5_hl-shared" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?


CMake Error at C:/Users/lamar/Desktop/netcdf-cxx4-4.3.1/build/CMakeFiles/CMakeTmp/CMakeLists.txt:17 (add_executable):
  Target "cmTC_7cc31" links to target "hdf5::hdf5-shared" but the target was
  not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?


CMake Error at C:/Users/lamar/Desktop/netcdf-cxx4-4.3.1/build/CMakeFiles/CMakeTmp/CMakeLists.txt:17 (add_executable):
  Target "cmTC_7cc31" links to target "hdf5::hdf5_hl-shared" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?


CMake Error at C:/Users/lamar/Desktop/netcdf-cxx4-4.3.1/build/CMakeFiles/CMakeTmp/CMakeLists.txt:17 (add_executable):
  Target "cmTC_7cc31" links to target "hdf5::hdf5-shared" but the target was
  not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?


CMake Error at C:/Users/lamar/Desktop/netcdf-cxx4-4.3.1/build/CMakeFiles/CMakeTmp/CMakeLists.txt:17 (add_executable):
  Target "cmTC_7cc31" links to target "hdf5::hdf5_hl-shared" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?


CMake Error at C:/Users/lamar/Desktop/netcdf-cxx4-4.3.1/build/CMakeFiles/CMakeTmp/CMakeLists.txt:17 (add_executable):
  Target "cmTC_7cc31" links to target "hdf5::hdf5-shared" but the target was
  not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?


CMake Error at C:/Users/lamar/Desktop/netcdf-cxx4-4.3.1/build/CMakeFiles/CMakeTmp/CMakeLists.txt:17 (add_executable):
  Target "cmTC_7cc31" links to target "hdf5::hdf5_hl-shared" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?


CMake Error at C:/Program Files/cmake/share/cmake-3.14/Modules/CheckLibraryExists.cmake:67 (try_compile):
  Failed to generate test project build system.
Call Stack (most recent call first):
  CMakeLists.txt:382 (CHECK_LIBRARY_EXISTS)


-- Configuring incomplete, errors occurred!
See also "C:/Users/lamar/Desktop/netcdf-cxx4-4.3.1/build/CMakeFiles/CMakeOutput.log".

What I have to do to build the NetCDF-CXX4 static/dynamic libraries in my system?

Libaray

| Congratulations! You have successfully installed the        |
| new netCDF-4 C++ Libaray  

CMake Config files missing on install.

It seems that the current code is missing the correct CMake files allowing the netCDFCxx package to be found in other projects. This is the error returned by ccmake:

CMake Error at CMakeLists.txt:100 (find_package):
By not providing "FindnetCDFCxx.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by
"netCDFCxx", but CMake did not find one.

Could not find a package configuration file provided by "netCDFCxx" with
any of the following names:

netCDFCxxConfig.cmake
netcdfcxx-config.cmake

Add the installation prefix of "netCDFCxx" to CMAKE_PREFIX_PATH or set
"netCDFCxx_DIR" to a directory containing one of the above files. If
"netCDFCxx" provides a separate development package or SDK, be sure it has
been installed.

After being called in CMakeLists with:
find_package(netCDFCxx REQUIRED)
include_directories(${NETCDFCxx_INCLUDE_PATH})

The netCDFCxx installer only installs the CMake file netCDFCxxConfigVersion.cmake in:
./lib64/cmake/netCDFCxx

No access to the NC_SHARE flag.

The NC_SHARE flag can not be accessed from this c++ library. Is there any particular reason for this? You can read more about the NC_SHARE flage here.

ncxx4-config missing in cmake-build

Version 4.3.0

Everything builds fine with cmake, but the ncxx4-config file isn't generated. Will this be added in the near future or do we have to switch back to configure?

Possible autotools bug with in-source builds of 4.3.1 release tarball

All,

I recently downloaded 4.3.1 into a set of libraries I maintain for a weather model. What I usually do is build for one compiler/MPI stack, clean, distclean, and then build another compiler/MPI stack. And, with 4.3.1, something interesting happened: I couldn't!

It turns out when I did a make distclean, the file examples/tst_filter.sh was removed and so the next configure step died. I think it's because in examples/Makefile.in there is:

CONFIG_CLEAN_FILES = tst_filter.sh findplugin.sh

and CONFIG_CLEAN_FILES seems to be cleaned when make distclean is run...I think...autotools confuses me.

So, I did what I should have done before and look at the README and you recommend out-of-source builds. An added mkdir -p and a few edits and, voila, I can build multiple times. Huzzah!

In a way, it's not a bug since the instructions do say to do out-of-source builds. I don't know enough autotools to know if you can make it not do in-source builds (like you can with CMake), but I thought I'd pass this along.

PS: I will be moving to building the netcdf-c/fortran/cxx4 with CMake soon. It's just I know our code works and links with the autotools builds. If I go CMake, I'll want to change the install pattern for netcdf and that's bit longer term validation process. (Plus I can just clone/submodule the repo then and not need release tarballs!)

4.3.x not including parallel I/O support

To report a non-security related issue, please provide:

  • the version of the software with which you are encountering an issue:
    4.3.0, 4.3.1
  • environmental information (i.e. Operating System, compiler info, java version, python version, etc.):
    conda-forge builds on both linux and osx with both openmpi and mpich, see:
    conda-forge/netcdf-cxx4-feedstock#27
  • a description of the issue with the steps needed to reproduce it:

netcdf-c has been build with parallel support using cmake flags:

-DENABLE_PARALLEL4=ON -DENABLE_PARALLEL_TESTS=ON

see: https://github.com/conda-forge/libnetcdf-feedstock/blob/master/recipe/build.sh#L4

A test install of libnetcdf with mpich indicates that a parallel build was indeed made, e.g. include/netcdf_par.h exists.

$ nc-config --all

This netCDF 4.7.1 has been built with the following features: 

  --cc            -> /home/xylar/miniconda3/envs/test/bin/mpicc
  --cflags        -> -I/home/xylar/miniconda3/envs/test/include
  --libs          -> -L/home/xylar/miniconda3/envs/test/lib -lnetcdf
  --static        -> -lmfhdf -ldf -lhdf5_hl -lhdf5 -lrt -lpthread -lz -ldl -lm -lcurl

  --has-c++       -> no
  --cxx           -> 

  --has-c++4      -> no
  --cxx4          -> 

  --has-fortran   -> no
  --has-dap       -> yes
  --has-dap2      -> yes
  --has-dap4      -> yes
  --has-nc2       -> yes
  --has-nc4       -> yes
  --has-hdf5      -> yes
  --has-hdf4      -> yes
  --has-logging   -> no
  --has-pnetcdf   -> no
  --has-szlib     -> no
  --has-cdf5      -> yes
  --has-parallel4 -> yes
  --has-parallel  -> yes

  --prefix        -> /home/xylar/miniconda3/envs/test
  --includedir    -> /home/xylar/miniconda3/envs/test/include
  --libdir        -> /home/xylar/miniconda3/envs/test/lib
  --version       -> netCDF 4.7.1

However, when I try to build netcdf-cxx4 with cmake and linking in the parallel version of libnetcdf, I always see:

-- Looking for nc_use_parallel_enabled in netcdf
-- Looking for nc_use_parallel_enabled in netcdf - not found

Again, see: conda-forge/netcdf-cxx4-feedstock#27

There's not a clear indication that any attempt is being made to build with parallel I/O.

Are there flags I can pass to cmake to tell it that I want a parallel build? I searched the documentation but didn't see anything obvious.

Missing bounds check in netCDF4-C++ NcVar::getDim()

Originally reported by @citibeth via esupport:


I believe that NcVar::getDim() is not bounds checking its input parameter.
See below.

Thanks,
-- Elizabeth

I'm causing malloc() problems with the following code:

for (int k=0; k<RANK; ++k) {
printf("    DD3.1 %ld %d\n", ncvar.getDimCount(), k);
                            ...
netCDF::NcDim dim(ncvar.getDim(k));

               }

This creates the output:

DD3.1 2 0
DD3.2
DD3.3
DD3.4
DD3.1 2 1
DD3.2
DD3.3
DD3.4
DD3.1 2 2

test_grid(86063,0x7fff729ac310) malloc: *** error for object
0x7fd38053bdf8: pointer being freed was not allocated

*** set a breakpoint in malloc_error_break to debug

Windows support via CMake

It should be fairly straightforward to add official Visual Studio support with CMake. The library will currently build, but the tests fail. The CMake files for the C++ interface need to be overhauled anyways, so may as well do this in one fell swoop.

install netcdfc success, but compile netcdfc++ fail

I install "netCDF4.7.3-NC4-64.exe", and download "netcdf-cxx4-4.3.1" release version.
then I create one test project in qt, write "vector dims;" , it is ok , but write "NcDim nd;" it is wrong, can not be compiled.
the error is " LNK2019: public: __cdecl netCDF::NcDim::NcDim(void) (??0NcDim@netCDF@@qeaa@XZ) "
and other classes which in the "cxx4" folder have the same error. eg. NcFile, ncFloat ...

anybody can help me? thank you .

underlinked libnetcdf_c++4.so.1: undefined symbol: nc_*

As reported by Andreas Beckmann in Debian Bug #949828:

libnetcdf_c++4.so.1 uses a bunch of nc_* symbols, but is not linked
against any netcdf library:

ldd /usr/lib/x86_64-linux-gnu/libnetcdf_c++4.so.1
        linux-vdso.so.1 (0x00007ffe6adc1000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fed279c3000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fed27803000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fed277e9000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fed276a4000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fed27bf5000)

While -lnetcdf is added in LDFLAGS, the library is not added to the NEEDED section:

$ objdump -x /usr/lib/x86_64-linux-gnu/libnetcdf_c++4.so.1 | grep NEEDED
  NEEDED               libstdc++.so.6
  NEEDED               libc.so.6
  NEEDED               libgcc_s.so.1

Adding -lnetcdf to CXXFLAGS fixes the issue.

Errors with rvalue references in C++11 mode

The following code throw a NcBadId exception at the auto attr = wrap.file.getAtt("Conventions"); line.

#include <string>
#include <netcdf>

using namespace std;
using namespace netCDF;

class Wrapper {
public:
    Wrapper(string name, string mode) {
        if (mode == "r")
            file = NcFile(name, NcFile::read);
        else if (mode == "w")
            file = NcFile(name, NcFile::write);
    }
    NcFile file;
};

int main(int argc, char** argv) {
    Wrapper wrap("file.nc", "r");
    auto attr = wrap.file.getAtt("Conventions");
    return 0;
}

While when I use direct initialisation for the file member, everything works.

class Wrapper {
public:
     Wrapper(string name, string mode) : file(name, NcFile::read) {}
    NcFile file;
};

Maybe the library is using the default generated operator=(const NcFile&), and this one is not correct. My compiler is clang based on LLVM 3.5.

undefined symbols on windows

Hi there.

I have installed the pre-built binary netcdf-c library according to here. But when I go and build netcdf-cxx4 using CMake, I get the following errors.

CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x912): undefined reference to `nc_inq_att'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x9e9): undefined reference to `nc_get_att_double'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0xa7b): undefined reference to `nc_get_att_text'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0xbb9): undefined reference to `nc_open'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0xe00): undefined reference to `nc_inq_dimid'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x11f1): undefined reference to `nc_inq_varid'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x1348): undefined reference to `nc_close'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x17bc): undefined reference to `nc_open'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x17d7): undefined reference to `nc_inq_dimid'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x1806): undefined reference to `nc_inq_dimlen'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x1c42): undefined reference to `nc_open'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x1f75): undefined reference to `nc_inq_format'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x21c8): undefined reference to `nc_inq'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x22f7): undefined reference to `nc_inq_unlimdims'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x2418): undefined reference to `nc_inq_dim'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x266c): undefined reference to `nc_inq_varid'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x26af): undefined reference to `nc_inq_varnatts'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x2783): undefined reference to `nc_inq_attname'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x2b22): undefined reference to `nc_inq_var'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x2ce9): undefined reference to `nc_inq_dim'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x2eb2): undefined reference to `nc_inq_attname'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x32e1): undefined reference to `nc_inq_attname'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x3425): undefined reference to `nc_close'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x3a12): undefined reference to `nc_inq_varndims'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x3a7d): undefined reference to `nc_inq_var'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x3aee): undefined reference to `nc_inq_dimlen'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x3d98): undefined reference to `nc_open'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x3dc9): undefined reference to `nc_inq_varid'
CAnalogsIO/libCAnalogsIO.a(NcdfIO.cpp.obj):NcdfIO.cpp:(.text+0x3e5a): undefined reference to `nc_get_var_double'
collect2.exe: error: ld returned 1 exit status
make[2]: *** [canalogs.exe] Error 1
make[1]: *** [CMakeFiles/canalogs.dir/all] Error 2
make: *** [all] Error 2

Any ideas? Thank you.

/usr/include/netcdf

In the installation of netcdf-cxx4, one finds the funny file /usr/include/netcdf without a file name extension. Is that an include file? If yes, it should be probably be names .h or .hpp.

Inconsistent behavior of getAtt() function

In examples/sfc_pres_temp_rd.cpp the following code does not run as expected

105 att = latVar.getAtt("units");
106 if(att.isNull()) return NC_ERR;

If the attribute "units" is nonexistent, the NC_ERR is never returned, because att.isNull() is also never evaluated. If an attribute of a variable is not defined the function getAtt() throws an exception and the program is stopped!

Check for its behavior in cxx4/ncVar.cpp

// Gets attribute by name.
NcVarAtt NcVar::getAtt(const string& name) const
{
  map<string,NcVarAtt> attributeList = getAtts();
  map<string,NcVarAtt>::iterator myIter;
  myIter = attributeList.find(name);
  if(myIter == attributeList.end()){
    string msg("Attribute '"+name+"' not found");
    throw NcException(msg.c_str(),__FILE__,__LINE__);
  }
  return NcVarAtt(myIter->second);
}

Shouldn't getAtt() return a NcVarAtt with isNull() == true?

Antonio

Errors in make check compiling with PGI 16.4 compilers

I am trying to compile netcdf-cxx4-4.3.0 on a CentOS 7.2 64-bit machine, and I am getting failures from 'make check'.

This is with the PGI compilers, version 16.4, HDF5, version 1.8.16, and NetCDF, version 4.4.1, all recently build, and all of which pass their tests upon making.

The configure options for netcdf-cxx4-4.3.0 is just the prefix, and the resulting nc-config --all reports

$ ./nc-config --all

This netCDF 4.4.1 has been built with the following features: 

  --cc        -> pgcc
  --cflags    ->  -I/sw/arcts/centos7/netcdf/4.4.1/pgi-16.4-hdf5-1.8.16/include -DpgiFortran -I/sw/arcts/centos7/hdf5/1.8.16-pgi-16.4/include
  --libs      -> 

  --has-c++   -> no
  --cxx       -> 
  --has-c++4  -> no
  --cxx4      -> 

  --fc        -> 
  --fflags    -> 
  --flibs     -> 
  --has-f90   -> no
  --has-f03   -> no

  --has-dap   -> yes
  --has-nc2   -> yes
  --has-nc4   -> yes
  --has-hdf5  -> yes
  --has-hdf4  -> no
  --has-logging-> no
  --has-pnetcdf-> no
  --has-szlib -> 

  --prefix    -> /sw/arcts/centos7/netcdf/4.4.1/pgi-16.4-hdf5-1.8.16
  --includedir-> /sw/arcts/centos7/netcdf/4.4.1/pgi-16.4-hdf5-1.8.16/include
  --version   -> netCDF 4.4.1

My configure line for netcdf-cxx4-4.3.0 is just configure --prefix=$PREFIX and I set the following environment variables.

export CC=pgcc
export CXX=pgc++
export FC=pgfortran
export F90=pgfortran
export F77=pgfortran
export CPP='pgcc -E'
export CXXCPP='pgc++ -E'
export CFLAGS='-O tp=p7-64'
export CXXFLAGS='-O tp=p7-64'
export FFLAGS='-O tp=p7-64'

 # These are set for the NetCDF C library build
export CPPFLAGS="-DpgiFortran -I${HDF5_INCLUDE}"
export CFLAGS="$CFLAGS ${CPPFLAGS} -L${HDF5_LIB} -lhdf5 -lz"
export LDFLAGS="-L${HDF5_LIB}"

 # These are set for the NetCDF C++ library build
export CPPFLAGS="-DpgiFortran -I${PREFIX}/include -I${HDF5_INCLUDE}"
export LDFLAGS="-L${PREFIX}/lib -L${HDF5_LIB}"

Rerunning each test program by hand results in the following failures

test_att
Opening file "firstFile.cdf" with NcFile::replace
Testing addGroup("groupName")                              -----------   passed
 [ . . . . ]
Testing getValues()                                        -----------   passed
terminate called after throwing an instance of 'netCDF::exceptions::NcEnoGrp'
  what():  NetCDF: No group found.
file: ncGroup.cpp  line:129
Testing attCount([netCDF::Location])                   Aborted (core dumped)

test_dim
Opening file "firstFile.cdf" with NcFile::replace
Testing addGroup("groupName")                              -----------   passed
 [ . . . . ]
Testing NcDim::getName()                                   -----------   passed
terminate called after throwing an instance of 'netCDF::exceptions::NcEnoGrp'
  what():  NetCDF: No group found.
file: ncGroup.cpp  line:129
Testing NcGroup::getDimCount([netCDF::Location])       Aborted (core dumped)

test_group
Opening file "firstFile.cdf" with NcFile::replace
Testing addGroup("groupName")                         -----------   passed
terminate called after throwing an instance of 'netCDF::exceptions::NcEnoGrp'
  what():  NetCDF: No group found.
file: ncGroup.cpp  line:129
Testing getGroupCount([netCDF::Location])         Aborted (core dumped)

test_open_close
terminate called after throwing an instance of 'netCDF::exceptions::NcException'
  what():  No such file or directory
file: ncFile.cpp  line:63
Attempting to open a file that doesn't exist... Aborted (core dumped)

test_type
Opening file "firstFile.cdf" with NcFile::replace
Testing addGroup("groupName")                                -----------   passed
[ . . . .  all passed . . . . ]
Testing VLEN ncFile::putVar() and NcFile::getVar()           -----------   passed
Testing COMPOUND with VLEN ncFile::putVar() and NcFile::getVar()    -----------   passed

test_var
Opening file "firstFile.cdf" with NcFile::replace
Testing addGroup("groupName")                              -----------   passed
 [ . . . . ]
terminate called after throwing an instance of 'netCDF::exceptions::NcEnoGrp'
  what():  NetCDF: No group found.
file: ncGroup.cpp  line:129
Testing addVar("varName","typeName","dimName")         Aborted (core dumped)

test_var2
terminate called after throwing an instance of 'netCDF::exceptions::NcEnoGrp'
  what():  NetCDF: No group found.
file: ncGroup.cpp  line:129
Aborted (core dumped)

The only warnings I get from compiling are

"ncDim.h", line 47: warning: type qualifier on return type is meaningless
      const int getId() const {return myId;};

several that look like

"test_type.cpp", line 162: warning: conversion from pointer to smaller integer
      compoundType1.addMember("member1",ncByte,offsetof(struct1,mem1));

with additional warnings about the pointer conversion to smaller integer on lines 163, 164, 168, 169, 170, 390, 391, 393, 518, and 519. There are also these warnings

"test_type.cpp", line 412: warning: variable "dummyStruct2" was set but never
          used
        struct3 dummyStruct2[2];
                ^
"test_type.cpp", line 533: warning: expression has no effect
        dummyData2.mem1[0];
        ^

I recently built using virtually the same procedure and compiler options with gcc 4.8.5, gcc 4.9.3, gcc 5.4.0, and Intel 16.0.3.

Please let me know if there is additional information you would like.

problem building netcdf-cxx4 with CMake GUI for MS visual Studio 2017

Hello
I am trying to building netcdf-cxx (last available version) using CMake 3.17.0 for MS Visual Studio 2017
I have previously built hdf5 (debug and release) version 1.12.0 and netcdf (debug and release) version 4.7.3

The configuration process fails and this is what I get

Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.18363.
Found bash: C:/Program Files/Git/bin/bash.exe
Found netcdf: C:/Software/netcdf/4.7.3/build/liblib/Debug/netcdf.lib
CMake Error at CMakeLists.txt:405 (CHECK_LIBRARY_EXISTS):
CHECK_LIBRARY_EXISTS Macro invoked with incorrect arguments for macro
named: CHECK_LIBRARY_EXISTS

Plugin support requires libhdf5 with H5Free support. Your libhdf5 install does not provide H5Free. Please install a newer version of libhdf5 if you require plugin compression support.

NetCDF C Configuration Summary

==============================

General


NetCDF Version: 4.3.2-developer
Configured On:
Host System: --
Build Directory: C:/Software/netcdf/cxx/buid
Install Prefix:

Compiling Options


C Compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe
CFLAGS: /DWIN32 /D_WINDOWS /W3 /MDd /Zi /Ob0 /Od /RTC1
CPPFLAGS: /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Zi /Ob0 /Od /RTC1
LDFLAGS: /machine:x64 /debug /INCREMENTAL
AM_CFLAGS:
AM_CPPFLAGS:
AM_LDFLAGS:
Shared Library: yes
Static Library: no
Extra libraries:

Configuring incomplete, errors occurred!
See also "C:/Software/netcdf/cxx/buid/CMakeFiles/CMakeOutput.log".
See also "C:/Software/netcdf/cxx/buid/CMakeFiles/CMakeError.log".

any help would truly be appreciated.

best
jac

C++11 Move Constructors

When using C++11, move constructors are needed for NcFile and other relevant classes. The following should work, but doesn't.

class NcIO {
    netCDF::NcFile _mync;
public:

    NcIO(std::string const &filePath, netCDF::NcFile::FileMode fMode) :
        _mync(netCDF::NcFile(filePath, fMode)) {}
};

Add license

Under what license is this library provided? Please add a LICENSE file to the repository.

configure fails on netCDF-4 enabled netCDF-C

I'm trying to compile NetCDF-C++4 (4.3.0 release); the NetCDF-C version is compiled with NetCDF-4 (output nc-config --all: --has-nc4 -> yes), yet a configure of netCDF-C++4 returns:

configure: error: NetCDF must be built with netCDF-4 enabled.

Are there any other reasons why the configure script might return this error, since NetCDF-C is compiled with netCDF-4 enabled?

What are some useful contributions?

I work on several projects that all make heavy use of netCDF and I'd like to give back by making some contributions. Are there any particular areas where you would appreciate some work?
For example, upgrading to C++11/14, fixing warnings, modernising the CMake, API improvements, etc

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.