diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ee0351..8f3ba59 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,8 +15,10 @@ find_path(NETCDFCPP_INCLUDE_PATH NAMES netcdfcpp.h netcdf) find_path(GSL_INCLUDE_PATH NAMES gsl/gsl_blas.h) IF(EXISTS ${NETCDFCPP_INCLUDE_PATH}/netcdf) + SET(FOUND_NETCDF4 1) FILE(WRITE ${CMAKE_BINARY_DIR}/src/ctool_netcdf_ver.hpp "#define NETCDFCPP4 1") ELSE(EXISTS ${NETCDFCPP_INCLUDE_PATH}/netcdf) + SET(FOUND_NETCDF3 1) FILE(WRITE ${CMAKE_BINARY_DIR}/src/ctool_netcdf_ver.hpp "#undef NETCDFCPP4") ENDIF(EXISTS ${NETCDFCPP_INCLUDE_PATH}/netcdf) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 18bcaf2..3ce8b84 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,11 +6,16 @@ SET(CosmoTool_SRCS loadRamses.cpp octTree.cpp powerSpectrum.cpp - yorick.cpp miniargs.cpp growthFactor.cpp ) +IF(FOUND_NETCDF3) + SET(CosmoTool_SRCS ${CosmoTool_SRCS} yorick_nc3.cpp) +ELSEIF(FOUND_NETCDF4) + SET(CosmoTool_SRCS ${CosmoTool_SRCS} yorick_nc4.cpp) +ENDIF(FOUN_NETCDF3) + if (HDF5_FOUND) set(CosmoTool_SRCS ${CosmoTool_SRCS} h5_readFlash.cpp diff --git a/src/yorick_nc4.cpp b/src/yorick_nc4.cpp index d760009..8bdeda0 100644 --- a/src/yorick_nc4.cpp +++ b/src/yorick_nc4.cpp @@ -1,11 +1,7 @@ #include "ctool_netcdf_ver.hpp" #include "config.hpp" -#ifdef NETCDFCPP4 #include -using namespace netCDF -#else -#include -#endif +using namespace netCDF; #include #include "yorick.hpp" #include @@ -17,24 +13,24 @@ class NetCDF_handle { public: NcFile *outFile; - NcVar *curVar; - long *curPos; - long *counts; - long *dimList; + NcVar curVar; + vector curPos; + vector counts; + vector dimList; uint32_t rank; - NetCDF_handle(NcFile *f, NcVar *v, long *dimList, uint32_t rank); + NetCDF_handle(NcFile *f, NcVar v, vector& dimList, uint32_t rank); virtual ~NetCDF_handle(); }; -NetCDF_handle::NetCDF_handle(NcFile *f, NcVar *v, long *dimList, uint32_t rank) +NetCDF_handle::NetCDF_handle(NcFile *f, NcVar v, vector& dimList, uint32_t rank) { this->outFile = f; this->curVar = v; this->dimList = dimList; this->rank = rank; - this->counts = new long[rank]; - this->curPos = new long[rank]; + this->counts.resize(rank); + this->curPos.resize(rank); for (long i = 0; i < rank; i++) this->curPos[i] = 0; @@ -45,7 +41,6 @@ NetCDF_handle::NetCDF_handle(NcFile *f, NcVar *v, long *dimList, uint32_t rank) NetCDF_handle::~NetCDF_handle() { - delete[] dimList; delete outFile; } @@ -53,7 +48,7 @@ template class InputGenCDF: public NetCDF_handle, public ProgressiveInputImpl { public: - InputGenCDF(NcFile *f, NcVar *v, long *dimList, uint32_t rank) + InputGenCDF(NcFile *f, NcVar v, vector& dimList, uint32_t rank) : NetCDF_handle(f,v,dimList,rank) {} virtual ~InputGenCDF() {} @@ -62,13 +57,12 @@ public: { T a; - curVar->set_cur(curPos); - curVar->get(&a, counts); + curVar.getVar(curPos, counts, &a); curPos[rank-1]++; for (long i = rank-1; i >= 1; i--) { - if (curPos[i] == dimList[i]) + if (curPos[i] == dimList[i].getSize()) { curPos[i-1]++; curPos[i] = 0; @@ -88,20 +82,19 @@ template class OutputGenCDF: public NetCDF_handle, public ProgressiveOutputImpl { public: - OutputGenCDF(NcFile *f, NcVar *v, long *dimList, uint32_t rank) + OutputGenCDF(NcFile *f, NcVar v, vector& dimList, uint32_t rank) : NetCDF_handle(f,v,dimList,rank) {} virtual ~OutputGenCDF() {} virtual void put(T a) { - curVar->set_cur(curPos); - curVar->put(&a, counts); + curVar.putVar(curPos, counts, &a); curPos[rank-1]++; for (long i = rank-1; i >= 1; i--) { - if (curPos[i] == dimList[i]) + if (curPos[i] == dimList[i].getSize()) { curPos[i-1]++; curPos[i] = 0; @@ -110,40 +103,19 @@ public: } }; -template -class NetCDF_type -{ -public: - static const NcType t = (NcType)-1; -}; +template NcType& get_NetCDF_type(); -template<> -class NetCDF_type -{ -public: - static const NcType t = ncInt; -}; +#define IMPL_TYPE(T,ncT) \ +template<> \ +NcType& get_NetCDF_type() \ +{ \ + return ncT; \ +} -template<> -class NetCDF_type -{ -public: - static const NcType t = ncInt; -}; - -template<> -class NetCDF_type -{ -public: - static const NcType t = ncFloat; -}; - -template<> -class NetCDF_type -{ -public: - static const NcType t = ncDouble; -}; +IMPL_TYPE(int,ncInt); +IMPL_TYPE(unsigned int,ncInt); +IMPL_TYPE(double,ncDouble); +IMPL_TYPE(float,ncFloat); namespace CosmoTool { template @@ -151,25 +123,23 @@ namespace CosmoTool { ProgressiveOutput::saveArrayProgressive(const std::string& fname, uint32_t *dimList, uint32_t rank) { - NcFile *f = new NcFile(fname.c_str(), NcFile::Replace); + NcFile *f = new NcFile(fname, NcFile::replace); - assert(f->is_valid()); - - const NcDim **dimArray = new const NcDim *[rank]; + vector dimArray; for (uint32_t i = 0; i < rank; i++) { char dimName[255]; sprintf(dimName, "dim%d", i); - dimArray[i] = f->add_dim(dimName, dimList[rank-1-i]); + dimArray.push_back(f->addDim(dimName, dimList[rank-1-i])); } - NcVar *v = f->add_var("array", NetCDF_type::t, rank, dimArray); + NcVar v = f->addVar("array", get_NetCDF_type(), dimArray); - long *ldimList = new long[rank]; + vector ldimList; for (uint32_t i = 0; i < rank; i++) - ldimList[rank-1-i] = dimList[i]; + ldimList.push_back(dimArray[rank-1-i]); OutputGenCDF *impl = new OutputGenCDF(f, v, ldimList, rank); return ProgressiveOutput(impl); @@ -180,20 +150,18 @@ namespace CosmoTool { ProgressiveInput::loadArrayProgressive(const std::string& fname, uint32_t *&dimList, uint32_t& rank) { - NcFile *f = new NcFile(fname.c_str(), NcFile::ReadOnly); + NcFile *f = new NcFile(fname, NcFile::read); - assert(f->is_valid()); + NcVar v = f->getVar("array"); - NcVar *v = f->get_var("array"); - - rank = v->num_dims(); - long *ldimList = v->edges(); + rank = v.getDimCount(); + vector vdimlist = v.getDims(); dimList = new uint32_t[rank]; for (uint32_t i = 0; i < rank; i++) { - dimList[rank-i-1] = ldimList[i]; + dimList[rank-i-1] = vdimlist[i].getSize(); } - InputGenCDF *impl = new InputGenCDF(f, v, ldimList, rank); + InputGenCDF *impl = new InputGenCDF(f, v, vdimlist, rank); return ProgressiveInput(impl); } @@ -202,24 +170,20 @@ namespace CosmoTool { void saveArray(const std::string& fname, T *array, uint32_t *dimList, uint32_t rank) { - NcFile f(fname.c_str(), NcFile::Replace); + NcFile f(fname.c_str(), NcFile::replace); - assert(f.is_valid()); - - const NcDim **dimArray = new const NcDim *[rank]; + vector dimArray; for (uint32_t i = 0; i < rank; i++) { char dimName[255]; sprintf(dimName, "dim%d", i); - dimArray[i] = f.add_dim(dimName, dimList[i]); + dimArray.push_back(f.addDim(dimName, dimList[i])); } - NcVar *v = f.add_var("array", NetCDF_type::t, rank, dimArray); + NcVar v = f.addVar("array", get_NetCDF_type(), dimArray); - long *edge = v->edges(); - v->put(array, edge); - delete[] edge; + v.putVar(array); } template @@ -227,26 +191,25 @@ namespace CosmoTool { T*&array, uint32_t *&dimList, uint32_t& rank) throw (NoSuchFileException) { - NcFile f(fname.c_str(), NcFile::ReadOnly); + NcFile f(fname.c_str(), NcFile::read); - if (!f.is_valid()) - throw NoSuchFileException(fname); + //if (!f.is_valid()) + // throw NoSuchFileException(fname); - NcVar *v = f.get_var("array"); - rank = v->num_dims(); - long *edge = v->edges(); + NcVar v = f.getVar("array"); + vector dims = v.getDims(); + rank = v.getDimCount(); uint32_t fullSize = 1; dimList = new uint32_t[rank]; for (int i = 0; i < rank; i++) { - dimList[i] = edge[i]; - fullSize *= edge[i]; + dimList[i] = dims[i].getSize(); + fullSize *= dimList[i]; } if (fullSize != 0) { array = new T[fullSize]; - v->get(array, edge); + v.getVar(array); } - delete[] edge; } template class ProgressiveInput;