Missing updates...

This commit is contained in:
Guilhem Lavaux 2012-09-19 09:15:09 -04:00
parent fe289507c5
commit c7d0974857
3 changed files with 60 additions and 90 deletions

View File

@ -15,8 +15,10 @@ find_path(NETCDFCPP_INCLUDE_PATH NAMES netcdfcpp.h netcdf)
find_path(GSL_INCLUDE_PATH NAMES gsl/gsl_blas.h) find_path(GSL_INCLUDE_PATH NAMES gsl/gsl_blas.h)
IF(EXISTS ${NETCDFCPP_INCLUDE_PATH}/netcdf) IF(EXISTS ${NETCDFCPP_INCLUDE_PATH}/netcdf)
SET(FOUND_NETCDF4 1)
FILE(WRITE ${CMAKE_BINARY_DIR}/src/ctool_netcdf_ver.hpp "#define NETCDFCPP4 1") FILE(WRITE ${CMAKE_BINARY_DIR}/src/ctool_netcdf_ver.hpp "#define NETCDFCPP4 1")
ELSE(EXISTS ${NETCDFCPP_INCLUDE_PATH}/netcdf) ELSE(EXISTS ${NETCDFCPP_INCLUDE_PATH}/netcdf)
SET(FOUND_NETCDF3 1)
FILE(WRITE ${CMAKE_BINARY_DIR}/src/ctool_netcdf_ver.hpp "#undef NETCDFCPP4") FILE(WRITE ${CMAKE_BINARY_DIR}/src/ctool_netcdf_ver.hpp "#undef NETCDFCPP4")
ENDIF(EXISTS ${NETCDFCPP_INCLUDE_PATH}/netcdf) ENDIF(EXISTS ${NETCDFCPP_INCLUDE_PATH}/netcdf)

View File

@ -6,11 +6,16 @@ SET(CosmoTool_SRCS
loadRamses.cpp loadRamses.cpp
octTree.cpp octTree.cpp
powerSpectrum.cpp powerSpectrum.cpp
yorick.cpp
miniargs.cpp miniargs.cpp
growthFactor.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) if (HDF5_FOUND)
set(CosmoTool_SRCS ${CosmoTool_SRCS} set(CosmoTool_SRCS ${CosmoTool_SRCS}
h5_readFlash.cpp h5_readFlash.cpp

View File

@ -1,11 +1,7 @@
#include "ctool_netcdf_ver.hpp" #include "ctool_netcdf_ver.hpp"
#include "config.hpp" #include "config.hpp"
#ifdef NETCDFCPP4
#include <netcdf> #include <netcdf>
using namespace netCDF using namespace netCDF;
#else
#include <netcdfcpp.h>
#endif
#include <fstream> #include <fstream>
#include "yorick.hpp" #include "yorick.hpp"
#include <assert.h> #include <assert.h>
@ -17,24 +13,24 @@ class NetCDF_handle
{ {
public: public:
NcFile *outFile; NcFile *outFile;
NcVar *curVar; NcVar curVar;
long *curPos; vector<size_t> curPos;
long *counts; vector<size_t> counts;
long *dimList; vector<NcDim> dimList;
uint32_t rank; uint32_t rank;
NetCDF_handle(NcFile *f, NcVar *v, long *dimList, uint32_t rank); NetCDF_handle(NcFile *f, NcVar v, vector<NcDim>& dimList, uint32_t rank);
virtual ~NetCDF_handle(); 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<NcDim>& dimList, uint32_t rank)
{ {
this->outFile = f; this->outFile = f;
this->curVar = v; this->curVar = v;
this->dimList = dimList; this->dimList = dimList;
this->rank = rank; this->rank = rank;
this->counts = new long[rank]; this->counts.resize(rank);
this->curPos = new long[rank]; this->curPos.resize(rank);
for (long i = 0; i < rank; i++) for (long i = 0; i < rank; i++)
this->curPos[i] = 0; 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() NetCDF_handle::~NetCDF_handle()
{ {
delete[] dimList;
delete outFile; delete outFile;
} }
@ -53,7 +48,7 @@ template<typename T>
class InputGenCDF: public NetCDF_handle, public ProgressiveInputImpl<T> class InputGenCDF: public NetCDF_handle, public ProgressiveInputImpl<T>
{ {
public: public:
InputGenCDF(NcFile *f, NcVar *v, long *dimList, uint32_t rank) InputGenCDF(NcFile *f, NcVar v, vector<NcDim>& dimList, uint32_t rank)
: NetCDF_handle(f,v,dimList,rank) : NetCDF_handle(f,v,dimList,rank)
{} {}
virtual ~InputGenCDF() {} virtual ~InputGenCDF() {}
@ -62,13 +57,12 @@ public:
{ {
T a; T a;
curVar->set_cur(curPos); curVar.getVar(curPos, counts, &a);
curVar->get(&a, counts);
curPos[rank-1]++; curPos[rank-1]++;
for (long i = rank-1; i >= 1; i--) for (long i = rank-1; i >= 1; i--)
{ {
if (curPos[i] == dimList[i]) if (curPos[i] == dimList[i].getSize())
{ {
curPos[i-1]++; curPos[i-1]++;
curPos[i] = 0; curPos[i] = 0;
@ -88,20 +82,19 @@ template<typename T>
class OutputGenCDF: public NetCDF_handle, public ProgressiveOutputImpl<T> class OutputGenCDF: public NetCDF_handle, public ProgressiveOutputImpl<T>
{ {
public: public:
OutputGenCDF(NcFile *f, NcVar *v, long *dimList, uint32_t rank) OutputGenCDF(NcFile *f, NcVar v, vector<NcDim>& dimList, uint32_t rank)
: NetCDF_handle(f,v,dimList,rank) : NetCDF_handle(f,v,dimList,rank)
{} {}
virtual ~OutputGenCDF() {} virtual ~OutputGenCDF() {}
virtual void put(T a) virtual void put(T a)
{ {
curVar->set_cur(curPos); curVar.putVar(curPos, counts, &a);
curVar->put(&a, counts);
curPos[rank-1]++; curPos[rank-1]++;
for (long i = rank-1; i >= 1; i--) for (long i = rank-1; i >= 1; i--)
{ {
if (curPos[i] == dimList[i]) if (curPos[i] == dimList[i].getSize())
{ {
curPos[i-1]++; curPos[i-1]++;
curPos[i] = 0; curPos[i] = 0;
@ -110,40 +103,19 @@ public:
} }
}; };
template<typename T> template<typename T> NcType& get_NetCDF_type();
class NetCDF_type
{
public:
static const NcType t = (NcType)-1;
};
template<> #define IMPL_TYPE(T,ncT) \
class NetCDF_type<int> template<> \
{ NcType& get_NetCDF_type<T>() \
public: { \
static const NcType t = ncInt; return ncT; \
}; }
template<> IMPL_TYPE(int,ncInt);
class NetCDF_type<unsigned int> IMPL_TYPE(unsigned int,ncInt);
{ IMPL_TYPE(double,ncDouble);
public: IMPL_TYPE(float,ncFloat);
static const NcType t = ncInt;
};
template<>
class NetCDF_type<float>
{
public:
static const NcType t = ncFloat;
};
template<>
class NetCDF_type<double>
{
public:
static const NcType t = ncDouble;
};
namespace CosmoTool { namespace CosmoTool {
template<typename T> template<typename T>
@ -151,25 +123,23 @@ namespace CosmoTool {
ProgressiveOutput<T>::saveArrayProgressive(const std::string& fname, uint32_t *dimList, ProgressiveOutput<T>::saveArrayProgressive(const std::string& fname, uint32_t *dimList,
uint32_t rank) uint32_t rank)
{ {
NcFile *f = new NcFile(fname.c_str(), NcFile::Replace); NcFile *f = new NcFile(fname, NcFile::replace);
assert(f->is_valid()); vector<NcDim> dimArray;
const NcDim **dimArray = new const NcDim *[rank];
for (uint32_t i = 0; i < rank; i++) for (uint32_t i = 0; i < rank; i++)
{ {
char dimName[255]; char dimName[255];
sprintf(dimName, "dim%d", i); 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>::t, rank, dimArray); NcVar v = f->addVar("array", get_NetCDF_type<T>(), dimArray);
long *ldimList = new long[rank]; vector<NcDim> ldimList;
for (uint32_t i = 0; i < rank; i++) for (uint32_t i = 0; i < rank; i++)
ldimList[rank-1-i] = dimList[i]; ldimList.push_back(dimArray[rank-1-i]);
OutputGenCDF<T> *impl = new OutputGenCDF<T>(f, v, ldimList, rank); OutputGenCDF<T> *impl = new OutputGenCDF<T>(f, v, ldimList, rank);
return ProgressiveOutput<T>(impl); return ProgressiveOutput<T>(impl);
@ -180,20 +150,18 @@ namespace CosmoTool {
ProgressiveInput<T>::loadArrayProgressive(const std::string& fname, uint32_t *&dimList, ProgressiveInput<T>::loadArrayProgressive(const std::string& fname, uint32_t *&dimList,
uint32_t& rank) 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.getDimCount();
vector<NcDim> vdimlist = v.getDims();
rank = v->num_dims();
long *ldimList = v->edges();
dimList = new uint32_t[rank]; dimList = new uint32_t[rank];
for (uint32_t i = 0; i < rank; i++) for (uint32_t i = 0; i < rank; i++)
{ {
dimList[rank-i-1] = ldimList[i]; dimList[rank-i-1] = vdimlist[i].getSize();
} }
InputGenCDF<T> *impl = new InputGenCDF<T>(f, v, ldimList, rank); InputGenCDF<T> *impl = new InputGenCDF<T>(f, v, vdimlist, rank);
return ProgressiveInput<T>(impl); return ProgressiveInput<T>(impl);
} }
@ -202,24 +170,20 @@ namespace CosmoTool {
void saveArray(const std::string& fname, void saveArray(const std::string& fname,
T *array, uint32_t *dimList, uint32_t rank) 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()); vector<NcDim> dimArray;
const NcDim **dimArray = new const NcDim *[rank];
for (uint32_t i = 0; i < rank; i++) for (uint32_t i = 0; i < rank; i++)
{ {
char dimName[255]; char dimName[255];
sprintf(dimName, "dim%d", i); 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>::t, rank, dimArray); NcVar v = f.addVar("array", get_NetCDF_type<T>(), dimArray);
long *edge = v->edges(); v.putVar(array);
v->put(array, edge);
delete[] edge;
} }
template<typename T> template<typename T>
@ -227,26 +191,25 @@ namespace CosmoTool {
T*&array, uint32_t *&dimList, uint32_t& rank) T*&array, uint32_t *&dimList, uint32_t& rank)
throw (NoSuchFileException) throw (NoSuchFileException)
{ {
NcFile f(fname.c_str(), NcFile::ReadOnly); NcFile f(fname.c_str(), NcFile::read);
if (!f.is_valid()) //if (!f.is_valid())
throw NoSuchFileException(fname); // throw NoSuchFileException(fname);
NcVar *v = f.get_var("array"); NcVar v = f.getVar("array");
rank = v->num_dims(); vector<NcDim> dims = v.getDims();
long *edge = v->edges(); rank = v.getDimCount();
uint32_t fullSize = 1; uint32_t fullSize = 1;
dimList = new uint32_t[rank]; dimList = new uint32_t[rank];
for (int i = 0; i < rank; i++) for (int i = 0; i < rank; i++)
{ {
dimList[i] = edge[i]; dimList[i] = dims[i].getSize();
fullSize *= edge[i]; fullSize *= dimList[i];
} }
if (fullSize != 0) { if (fullSize != 0) {
array = new T[fullSize]; array = new T[fullSize];
v->get(array, edge); v.getVar(array);
} }
delete[] edge;
} }
template class ProgressiveInput<int>; template class ProgressiveInput<int>;