Skip to content

Commit

Permalink
Work around bug in netcdf-cxx4. See: Unidata/netcdf-cxx4#30
Browse files Browse the repository at this point in the history
  • Loading branch information
citibeth committed Mar 28, 2016
1 parent ba5e800 commit 9b1212b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion slib/ibmisc/ConstantSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ double ConstantSet::get_as(std::string const &name,
// =======================================================
void ConstantSet::ncio(NcIO &ncio, std::string const &vname)
{
auto constants_v = get_or_add_var(ncio, vname, netCDF::ncInt64, {});
auto constants_v = get_or_add_var(ncio, vname, "int64", {});

// Get a list of the names of our constants
if (ncio.rw == 'w') {
Expand Down
4 changes: 2 additions & 2 deletions slib/ibmisc/indexing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void Indexing<TupleT, IndexT>::ncio(
netCDF::NcType ncTupleT,
std::string const &vname)
{
auto info_v = get_or_add_var(ncio, vname, netCDF::ncInt64, {});
auto info_v = get_or_add_var(ncio, vname, "int64", {});
get_or_put_att(info_v, ncio.rw, "base", ncTupleT, base);
get_or_put_att(info_v, ncio.rw, "extent", ncTupleT, extent);
get_or_put_att(info_v, ncio.rw, "indices", ncTupleT, indices);
Expand Down Expand Up @@ -153,7 +153,7 @@ void Domain<TupleT>::ncio(
netCDF::NcType ncTupleT,
std::string const &vname)
{
auto info_v = get_or_add_var(ncio, vname, netCDF::ncInt64, {});
auto info_v = get_or_add_var(ncio, vname, "int64", {});
get_or_put_att(info_v, ncio.rw, "low", ncTupleT, low);
get_or_put_att(info_v, ncio.rw, "high", ncTupleT, high);
}
Expand Down
19 changes: 18 additions & 1 deletion slib/ibmisc/netcdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,34 @@ std::vector<netCDF::NcDim> get_or_add_dims(
return ret;
}
// ====================================================
/** This works only for NetCDF-3 types. See:
https://github.com/Unidata/netcdf-cxx4/issues/30 */
netCDF::NcVar get_or_add_var(
NcIO &ncio,
std::string const &vname,
netCDF::NcType const &nc_type,
std::vector<netCDF::NcDim> const &dims)
{
std::string snc_type(nc_type.getName());
return get_or_add_var(ncio, vname, snc_type, dims);
}

/** This works for all valid NetCDF types. */
netCDF::NcVar get_or_add_var(
NcIO &ncio,
std::string const &vname,
std::string const &snc_type,
std::vector<netCDF::NcDim> const &dims)
{
netCDF::NcVar ncvar;
if (ncio.define) {
ncvar = ncio.nc->getVar(vname);
std::vector<std::string> sdims;
for (auto dim=dims.begin(); dim != dims.end(); ++dim)
sdims.push_back(dim->getName());

if (ncvar.isNull()) {
ncvar = ncio.nc->addVar(vname, nc_type, dims);
ncvar = ncio.nc->addVar(vname, snc_type, sdims);
} else {
// Check dimensions match
if (ncvar.getDimCount() != dims.size()) {
Expand Down
8 changes: 7 additions & 1 deletion slib/ibmisc/netcdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class NcIO {
define(_mode == 'd') {}

NcIO(std::string const &filePath, netCDF::NcFile::FileMode fMode = netCDF::NcFile::FileMode::read) :
_mync(filePath, fMode),
_mync(filePath, fMode, netCDF::NcFile::FileFormat::nc4),
own_nc(true),
nc(&_mync),
rw(fMode == netCDF::NcFile::FileMode::read ? 'r' : 'w'),
Expand Down Expand Up @@ -155,6 +155,12 @@ netCDF::NcVar get_or_add_var(
netCDF::NcType const &nc_type,
std::vector<netCDF::NcDim> const &dims);

netCDF::NcVar get_or_add_var(
NcIO &ncio,
std::string const &vname,
std::string const &snc_type,
std::vector<netCDF::NcDim> const &dims);

template<class TypeT>
void get_or_put_var(netCDF::NcVar &ncvar, char rw,
std::vector<size_t> const &startp,
Expand Down
4 changes: 2 additions & 2 deletions slib/spsparse/netcdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ void ncio_spsparse(
if (ncio.rw == 'w') {
dims = ibmisc::get_or_add_dims(ncio, dim_names, {A.size(), A.rank});

auto info_v = get_or_add_var(ncio, vname + ".info", netCDF::ncInt64, {});
auto info_v = get_or_add_var(ncio, vname + ".info", "int64", {});
info_v.putAtt("shape", netCDF::ncUint64, A.rank, &A.shape[0]);

get_or_add_var(ncio, vname + ".indices", netCDF::ncInt64, dims);
get_or_add_var(ncio, vname + ".indices", "int64", dims);
get_or_add_var(ncio, vname + ".vals", netCDF::ncDouble, {dims[0]});
ncio += std::bind(&nc_write_spsparse<ArrayT>, ncio.nc, &A, vname);
} else {
Expand Down
4 changes: 2 additions & 2 deletions tests/ibmisc/test_netcdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ TEST_F(NetcdfTest, blitz)
ibmisc::ncio_blitz(ncio, A, true, "A", netCDF::ncDouble, dims);
ibmisc::ncio_blitz(ncio, B, true, "B", netCDF::ncDouble, dims);

auto info_v = get_or_add_var(ncio, "info", netCDF::ncInt64, {});
auto info_v = get_or_add_var(ncio, "info", "int64", {});
get_or_put_att(info_v, ncio.rw, "strings", strings);

ncio.close();
Expand All @@ -139,7 +139,7 @@ TEST_F(NetcdfTest, blitz)
ibmisc::ncio_blitz(ncio, A2, true, "A", netCDF::ncDouble, dims);
ibmisc::ncio_blitz(ncio, B2, true, "B", netCDF::ncDouble, dims);

auto info_v = get_or_add_var(ncio, "info", netCDF::ncInt64, {});
auto info_v = get_or_add_var(ncio, "info", "int64", {});
get_or_put_att(info_v, ncio.rw, "strings", strings2);

ncio.close();
Expand Down

0 comments on commit 9b1212b

Please sign in to comment.