2013-03-03 00:42:56 +01:00
|
|
|
/*+
|
|
|
|
This is CosmoTool (./src/yorick_nc4.cpp) -- Copyright (C) Guilhem Lavaux (2007-2013)
|
|
|
|
|
|
|
|
guilhem.lavaux@gmail.com
|
|
|
|
|
|
|
|
This software is a computer program whose purpose is to provide a toolbox for cosmological
|
|
|
|
data analysis (e.g. filters, generalized Fourier transforms, power spectra, ...)
|
|
|
|
|
|
|
|
This software is governed by the CeCILL license under French law and
|
|
|
|
abiding by the rules of distribution of free software. You can use,
|
|
|
|
modify and/ or redistribute the software under the terms of the CeCILL
|
|
|
|
license as circulated by CEA, CNRS and INRIA at the following URL
|
|
|
|
"http://www.cecill.info".
|
|
|
|
|
|
|
|
As a counterpart to the access to the source code and rights to copy,
|
|
|
|
modify and redistribute granted by the license, users are provided only
|
|
|
|
with a limited warranty and the software's author, the holder of the
|
|
|
|
economic rights, and the successive licensors have only limited
|
|
|
|
liability.
|
|
|
|
|
|
|
|
In this respect, the user's attention is drawn to the risks associated
|
|
|
|
with loading, using, modifying and/or developing or reproducing the
|
|
|
|
software by the user in light of its specific status of free software,
|
|
|
|
that may mean that it is complicated to manipulate, and that also
|
|
|
|
therefore means that it is reserved for developers and experienced
|
|
|
|
professionals having in-depth computer knowledge. Users are therefore
|
|
|
|
encouraged to load and test the software's suitability as regards their
|
|
|
|
requirements in conditions enabling the security of their systems and/or
|
|
|
|
data to be ensured and, more generally, to use and operate it in the
|
|
|
|
same conditions as regards security.
|
|
|
|
|
|
|
|
The fact that you are presently reading this means that you have had
|
|
|
|
knowledge of the CeCILL license and that you accept its terms.
|
|
|
|
+*/
|
2013-03-06 04:39:33 +01:00
|
|
|
|
2012-09-16 00:53:47 +02:00
|
|
|
#include "ctool_netcdf_ver.hpp"
|
2008-12-02 18:00:28 +01:00
|
|
|
#include "config.hpp"
|
2012-09-16 00:53:47 +02:00
|
|
|
#include <netcdf>
|
2012-09-19 15:15:09 +02:00
|
|
|
using namespace netCDF;
|
2008-12-02 18:00:28 +01:00
|
|
|
#include <fstream>
|
|
|
|
#include "yorick.hpp"
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
using namespace CosmoTool;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
class NetCDF_handle
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NcFile *outFile;
|
2012-09-19 15:15:09 +02:00
|
|
|
NcVar curVar;
|
|
|
|
vector<size_t> curPos;
|
|
|
|
vector<size_t> counts;
|
|
|
|
vector<NcDim> dimList;
|
2008-12-02 18:00:28 +01:00
|
|
|
uint32_t rank;
|
|
|
|
|
2012-09-19 15:15:09 +02:00
|
|
|
NetCDF_handle(NcFile *f, NcVar v, vector<NcDim>& dimList, uint32_t rank);
|
2008-12-02 18:00:28 +01:00
|
|
|
virtual ~NetCDF_handle();
|
|
|
|
};
|
|
|
|
|
2012-09-19 15:15:09 +02:00
|
|
|
NetCDF_handle::NetCDF_handle(NcFile *f, NcVar v, vector<NcDim>& dimList, uint32_t rank)
|
2008-12-02 18:00:28 +01:00
|
|
|
{
|
|
|
|
this->outFile = f;
|
|
|
|
this->curVar = v;
|
|
|
|
this->dimList = dimList;
|
|
|
|
this->rank = rank;
|
2012-09-19 15:15:09 +02:00
|
|
|
this->counts.resize(rank);
|
|
|
|
this->curPos.resize(rank);
|
2008-12-02 18:00:28 +01:00
|
|
|
|
|
|
|
for (long i = 0; i < rank; i++)
|
|
|
|
this->curPos[i] = 0;
|
|
|
|
|
|
|
|
for (long i = 0; i < rank; i++)
|
|
|
|
this->counts[i] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
NetCDF_handle::~NetCDF_handle()
|
|
|
|
{
|
|
|
|
delete outFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class InputGenCDF: public NetCDF_handle, public ProgressiveInputImpl<T>
|
|
|
|
{
|
|
|
|
public:
|
2012-09-19 15:15:09 +02:00
|
|
|
InputGenCDF(NcFile *f, NcVar v, vector<NcDim>& dimList, uint32_t rank)
|
2008-12-02 18:00:28 +01:00
|
|
|
: NetCDF_handle(f,v,dimList,rank)
|
|
|
|
{}
|
|
|
|
virtual ~InputGenCDF() {}
|
|
|
|
|
|
|
|
virtual T read()
|
|
|
|
{
|
|
|
|
T a;
|
|
|
|
|
2012-09-19 15:15:09 +02:00
|
|
|
curVar.getVar(curPos, counts, &a);
|
2008-12-02 18:00:28 +01:00
|
|
|
|
|
|
|
curPos[rank-1]++;
|
|
|
|
for (long i = rank-1; i >= 1; i--)
|
|
|
|
{
|
2012-09-19 15:15:09 +02:00
|
|
|
if (curPos[i] == dimList[i].getSize())
|
2008-12-02 18:00:28 +01:00
|
|
|
{
|
|
|
|
curPos[i-1]++;
|
|
|
|
curPos[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void seek(uint32_t *pos)
|
|
|
|
{
|
|
|
|
for (long i = rank-1; i >= 0; i--)
|
|
|
|
curPos[i] = pos[rank-1-i];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class OutputGenCDF: public NetCDF_handle, public ProgressiveOutputImpl<T>
|
|
|
|
{
|
|
|
|
public:
|
2012-09-19 15:15:09 +02:00
|
|
|
OutputGenCDF(NcFile *f, NcVar v, vector<NcDim>& dimList, uint32_t rank)
|
2008-12-02 18:00:28 +01:00
|
|
|
: NetCDF_handle(f,v,dimList,rank)
|
|
|
|
{}
|
|
|
|
virtual ~OutputGenCDF() {}
|
|
|
|
|
|
|
|
virtual void put(T a)
|
|
|
|
{
|
2012-09-19 15:15:09 +02:00
|
|
|
curVar.putVar(curPos, counts, &a);
|
2008-12-02 18:00:28 +01:00
|
|
|
|
|
|
|
curPos[rank-1]++;
|
|
|
|
for (long i = rank-1; i >= 1; i--)
|
|
|
|
{
|
2012-09-19 15:15:09 +02:00
|
|
|
if (curPos[i] == dimList[i].getSize())
|
2008-12-02 18:00:28 +01:00
|
|
|
{
|
2013-07-17 12:11:55 +02:00
|
|
|
curPos[i-1]++;
|
|
|
|
curPos[i] = 0;
|
|
|
|
}
|
2008-12-02 18:00:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-09-19 15:15:09 +02:00
|
|
|
template<typename T> NcType& get_NetCDF_type();
|
2011-05-31 16:08:44 +02:00
|
|
|
|
2012-09-19 15:15:09 +02:00
|
|
|
#define IMPL_TYPE(T,ncT) \
|
|
|
|
template<> \
|
|
|
|
NcType& get_NetCDF_type<T>() \
|
|
|
|
{ \
|
|
|
|
return ncT; \
|
|
|
|
}
|
2008-12-02 18:00:28 +01:00
|
|
|
|
2012-09-19 15:15:09 +02:00
|
|
|
IMPL_TYPE(int,ncInt);
|
|
|
|
IMPL_TYPE(unsigned int,ncInt);
|
|
|
|
IMPL_TYPE(double,ncDouble);
|
|
|
|
IMPL_TYPE(float,ncFloat);
|
2008-12-02 18:00:28 +01:00
|
|
|
|
|
|
|
namespace CosmoTool {
|
|
|
|
template<typename T>
|
|
|
|
ProgressiveOutput<T>
|
2012-05-03 17:45:38 +02:00
|
|
|
ProgressiveOutput<T>::saveArrayProgressive(const std::string& fname, uint32_t *dimList,
|
2008-12-02 18:00:28 +01:00
|
|
|
uint32_t rank)
|
|
|
|
{
|
2012-09-19 15:15:09 +02:00
|
|
|
NcFile *f = new NcFile(fname, NcFile::replace);
|
2008-12-02 18:00:28 +01:00
|
|
|
|
2012-09-19 15:15:09 +02:00
|
|
|
vector<NcDim> dimArray;
|
2008-12-02 18:00:28 +01:00
|
|
|
for (uint32_t i = 0; i < rank; i++)
|
|
|
|
{
|
|
|
|
char dimName[255];
|
|
|
|
|
|
|
|
sprintf(dimName, "dim%d", i);
|
2012-09-19 15:15:09 +02:00
|
|
|
dimArray.push_back(f->addDim(dimName, dimList[rank-1-i]));
|
2008-12-02 18:00:28 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 15:15:09 +02:00
|
|
|
NcVar v = f->addVar("array", get_NetCDF_type<T>(), dimArray);
|
2008-12-02 18:00:28 +01:00
|
|
|
|
2012-09-19 15:15:09 +02:00
|
|
|
vector<NcDim> ldimList;
|
2008-12-02 18:00:28 +01:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < rank; i++)
|
2013-07-17 12:11:55 +02:00
|
|
|
ldimList.push_back(dimArray[i]);
|
2008-12-02 18:00:28 +01:00
|
|
|
|
|
|
|
OutputGenCDF<T> *impl = new OutputGenCDF<T>(f, v, ldimList, rank);
|
|
|
|
return ProgressiveOutput<T>(impl);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
ProgressiveInput<T>
|
2012-05-03 17:45:38 +02:00
|
|
|
ProgressiveInput<T>::loadArrayProgressive(const std::string& fname, uint32_t *&dimList,
|
2008-12-02 18:00:28 +01:00
|
|
|
uint32_t& rank)
|
|
|
|
{
|
2012-09-19 15:15:09 +02:00
|
|
|
NcFile *f = new NcFile(fname, NcFile::read);
|
2008-12-02 18:00:28 +01:00
|
|
|
|
2012-09-19 15:15:09 +02:00
|
|
|
NcVar v = f->getVar("array");
|
2008-12-02 18:00:28 +01:00
|
|
|
|
2012-09-19 15:15:09 +02:00
|
|
|
rank = v.getDimCount();
|
|
|
|
vector<NcDim> vdimlist = v.getDims();
|
2008-12-02 18:00:28 +01:00
|
|
|
dimList = new uint32_t[rank];
|
|
|
|
for (uint32_t i = 0; i < rank; i++)
|
|
|
|
{
|
2012-09-19 15:15:09 +02:00
|
|
|
dimList[rank-i-1] = vdimlist[i].getSize();
|
2008-12-02 18:00:28 +01:00
|
|
|
}
|
2012-09-19 15:15:09 +02:00
|
|
|
InputGenCDF<T> *impl = new InputGenCDF<T>(f, v, vdimlist, rank);
|
2008-12-02 18:00:28 +01:00
|
|
|
|
|
|
|
return ProgressiveInput<T>(impl);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2012-05-03 17:45:38 +02:00
|
|
|
void saveArray(const std::string& fname,
|
2008-12-02 18:00:28 +01:00
|
|
|
T *array, uint32_t *dimList, uint32_t rank)
|
|
|
|
{
|
2012-09-19 15:15:09 +02:00
|
|
|
NcFile f(fname.c_str(), NcFile::replace);
|
2008-12-02 18:00:28 +01:00
|
|
|
|
2012-09-19 15:15:09 +02:00
|
|
|
vector<NcDim> dimArray;
|
2008-12-02 18:00:28 +01:00
|
|
|
for (uint32_t i = 0; i < rank; i++)
|
|
|
|
{
|
|
|
|
char dimName[255];
|
|
|
|
|
|
|
|
sprintf(dimName, "dim%d", i);
|
2012-09-19 15:15:09 +02:00
|
|
|
dimArray.push_back(f.addDim(dimName, dimList[i]));
|
2008-12-02 18:00:28 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 15:15:09 +02:00
|
|
|
NcVar v = f.addVar("array", get_NetCDF_type<T>(), dimArray);
|
2008-12-02 18:00:28 +01:00
|
|
|
|
2012-09-19 15:15:09 +02:00
|
|
|
v.putVar(array);
|
2008-12-02 18:00:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2012-05-03 17:45:38 +02:00
|
|
|
void loadArray(const std::string& fname,
|
2008-12-02 18:00:28 +01:00
|
|
|
T*&array, uint32_t *&dimList, uint32_t& rank)
|
2010-03-02 17:59:41 +01:00
|
|
|
throw (NoSuchFileException)
|
2008-12-02 18:00:28 +01:00
|
|
|
{
|
2012-09-19 15:15:09 +02:00
|
|
|
NcFile f(fname.c_str(), NcFile::read);
|
2008-12-02 18:00:28 +01:00
|
|
|
|
2012-09-19 15:15:09 +02:00
|
|
|
//if (!f.is_valid())
|
|
|
|
// throw NoSuchFileException(fname);
|
2008-12-02 18:00:28 +01:00
|
|
|
|
2012-09-19 15:15:09 +02:00
|
|
|
NcVar v = f.getVar("array");
|
|
|
|
vector<NcDim> dims = v.getDims();
|
|
|
|
rank = v.getDimCount();
|
2008-12-02 18:00:28 +01:00
|
|
|
uint32_t fullSize = 1;
|
|
|
|
dimList = new uint32_t[rank];
|
|
|
|
for (int i = 0; i < rank; i++)
|
|
|
|
{
|
2012-09-19 15:15:09 +02:00
|
|
|
dimList[i] = dims[i].getSize();
|
|
|
|
fullSize *= dimList[i];
|
2008-12-02 18:00:28 +01:00
|
|
|
}
|
2011-06-22 20:57:06 +02:00
|
|
|
if (fullSize != 0) {
|
|
|
|
array = new T[fullSize];
|
2012-09-19 15:15:09 +02:00
|
|
|
v.getVar(array);
|
2011-06-22 20:57:06 +02:00
|
|
|
}
|
2008-12-02 18:00:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template class ProgressiveInput<int>;
|
|
|
|
template class ProgressiveInput<float>;
|
|
|
|
template class ProgressiveInput<double>;
|
|
|
|
|
|
|
|
template class ProgressiveOutput<int>;
|
|
|
|
template class ProgressiveOutput<float>;
|
|
|
|
template class ProgressiveOutput<double>;
|
|
|
|
|
2012-05-03 17:45:38 +02:00
|
|
|
template void loadArray<int>(const std::string& fname,
|
2008-12-02 18:00:28 +01:00
|
|
|
int*& array, uint32_t *&dimList, uint32_t& rank);
|
2012-05-03 17:45:38 +02:00
|
|
|
template void loadArray<float>(const std::string& fname,
|
2008-12-02 18:00:28 +01:00
|
|
|
float*& array, uint32_t *&dimList, uint32_t& rank);
|
2012-05-03 17:45:38 +02:00
|
|
|
template void loadArray<double>(const std::string& fname,
|
2008-12-02 18:00:28 +01:00
|
|
|
double*& array, uint32_t *&dimList, uint32_t& rank);
|
|
|
|
|
2012-05-03 17:45:38 +02:00
|
|
|
template void saveArray<int>(const std::string& fname,
|
2008-12-02 18:00:28 +01:00
|
|
|
int *array, uint32_t *dimList, uint32_t rank);
|
2012-05-03 17:45:38 +02:00
|
|
|
template void saveArray<float>(const std::string& fname,
|
2008-12-02 18:00:28 +01:00
|
|
|
float *array, uint32_t *dimList, uint32_t rank);
|
2012-05-03 17:45:38 +02:00
|
|
|
template void saveArray<double>(const std::string& fname,
|
2008-12-02 18:00:28 +01:00
|
|
|
double *array, uint32_t *dimList, uint32_t rank);
|
|
|
|
|
|
|
|
}
|