12 #ifndef INCLUDE_DDATASET_H_
13 #define INCLUDE_DDATASET_H_
18 #include <boost/array.hpp>
19 #include <boost/multi_array.hpp>
20 #include <boost/lexical_cast.hpp>
23 using namespace netCDF;
47 template<
typename U,
size_t rank>
48 boost::array<U, rank>
stl2boost(
const vector<U> &vec)
50 assert(vec.size() ==
rank);
51 boost::array<U, rank>
result;
74 vector<size_t> istart,
75 vector<size_t> icount,
96 ncfile->putAtt(
name, ncFloat,
count[0],
static_cast<const float*
> (ptr));
99 ncfile->putAtt(
name, ncInt,
count[0],
static_cast<const int*
> (ptr));
102 ncfile->putAtt(
name, ncShort,
count[0],
static_cast<const short*
> (ptr));
105 ncfile->putAtt(
name, ncUbyte,
count[0],
static_cast<const unsigned char*
> (ptr));
108 ncfile->putAtt(
name, ncByte,
count[0],
static_cast<const char*
> (ptr));
111 ncfile->putAtt(
name, ncChar,
count[0],
static_cast<const char*
> (ptr));
114 ncfile->putAtt(
name, ncDouble,
count[0],
static_cast<const double*
> (ptr));
117 ncfile->putAtt(
name, ncUshort,
count[0],
static_cast<const unsigned short*
> (ptr));
120 ncfile->putAtt(
name, ncUint,
count[0],
static_cast<const unsigned int*
> (ptr));
123 ncfile->putAtt(
name, ncInt64,
count[0],
static_cast<const long long*
> (ptr));
126 ncfile->putAtt(
name, ncUint64,
count[0],
static_cast<const unsigned long long*
> (ptr));
129 ncfile->putAtt(
name,
static_cast<const char*
> (ptr));
137 var.putAtt(
name, ncFloat, boost::lexical_cast<float> (sval));
140 var.putAtt(
name, ncInt, boost::lexical_cast<int> (sval));
143 var.putAtt(
name, ncShort, boost::lexical_cast<short> (sval));
146 var.putAtt(
name, ncUbyte, boost::lexical_cast<unsigned int> (sval));
149 var.putAtt(
name, ncByte, boost::lexical_cast<char> (sval));
152 var.putAtt(
name, ncChar, boost::lexical_cast<char> (sval));
155 var.putAtt(
name, ncDouble, boost::lexical_cast<double> (sval));
158 var.putAtt(
name, ncUshort, boost::lexical_cast<unsigned short> (sval));
161 var.putAtt(
name, ncUint, boost::lexical_cast<unsigned int> (sval));
164 var.putAtt(
name, ncInt64, boost::lexical_cast<long long> (sval));
167 var.putAtt(
name, ncUint64, boost::lexical_cast<unsigned long long> (sval));
170 var.putAtt(
name, sval);
176 const string&
str = sval;
186 var.setFill(
true, boost::lexical_cast<int> (
str));
189 var.setFill(
true, boost::lexical_cast<short> (
str));
192 var.setFill(
true, boost::lexical_cast<unsigned int> (
str));
195 var.setFill(
true, boost::lexical_cast<char> (
str));
198 var.setFill(
true, boost::lexical_cast<char> (
str));
201 var.setFill(
true, boost::lexical_cast<double> (
str));
204 var.setFill(
true, boost::lexical_cast<unsigned short> (
str));
207 var.setFill(
true, boost::lexical_cast<unsigned int> (
str));
210 var.setFill(
true, boost::lexical_cast<long long> (
str));
213 var.setFill(
true, boost::lexical_cast<unsigned long long> (
str));
216 var.setFill(
true,
str);
224 if(bshort &&
count.size()==2) {
226 std::map<std::string,NcVarAtt> matts = var.getAtts();
227 if (matts.find(
"scale_factor") != matts.end()) {
228 matts[
"scale_factor"].getValues(&
scale);
229 matts[
"add_offset"].getValues(&
offset);
231 boost::multi_array<short, 2> spts;
232 boost::array<size_t,2> bdimv = stl2boost<size_t,2>(
count);
234 for (
size_t i=0;
i<spts.num_elements();
i++) {
239 ((
short*)spts.origin())[
i] =
245 var.putVar(
start,
count,
static_cast<const float*
> (ptr));
249 var.putVar(
start,
count,
static_cast<const int*
> (ptr));
252 var.putVar(
start,
count,
static_cast<const short*
> (ptr));
255 var.putVar(
start,
count,
static_cast<const unsigned char*
> (ptr));
258 var.putVar(
start,
count,
static_cast<const char*
> (ptr));
261 var.putVar(
start,
count,
static_cast<const char*
> (ptr));
264 var.putVar(
start,
count,
static_cast<const double*
> (ptr));
267 var.putVar(
start,
count,
static_cast<const unsigned short*
> (ptr));
270 var.putVar(
start,
count,
static_cast<const unsigned int*
> (ptr));
273 var.putVar(
start,
count,
static_cast<const long long*
> (ptr));
276 var.putVar(
start,
count,
static_cast<const unsigned long long*
> (ptr));
279 var.putVar(
start,
count,
static_cast<const char*
> (ptr));
320 template<
typename T,
size_t ndim>
324 boost::multi_array<T, ndim>
pts;
330 vector<size_t> istart,
331 vector<size_t> icount) {
336 boost::array<size_t, ndim> bdimv = stl2boost<size_t,ndim>(
count);
344 vector<size_t> istart,
345 vector<size_t> icount)
347 NcVar var = ncgrp.getVar(
name);
350 rank = var.getDimCount();
351 vector<NcDim> dims = var.getDims();
352 size_t beg = (
rank == ndim+1) ? 1 : 0;
353 for (
size_t i=beg;
i<
rank;
i++){
354 start.push_back(istart[
i]);
355 count.push_back(icount[
i]);
357 boost::array<size_t, ndim> bdimv = stl2boost<size_t,ndim>(
count);
360 var.getVar(istart, icount, (T*)ptr);
365 std::map<std::string,NcVarAtt> matts = var.getAtts();
366 if (matts.find(
"scale_factor") != matts.end()) {
367 matts[
"scale_factor"].getValues(&factor);
368 matts[
"add_offset"].getValues(&
offset);
370 matts[
"_FillValue"].getValues(&fillvalue);
371 for (
size_t i=0;
i<pts.num_elements();
i++) {
372 if (((T*)ptr)[
i] == fillvalue || ((T*)ptr)[
i] == fillvalue-2) {
375 ((T*)ptr)[
i] = ((T*)ptr)[
i]*factor +
offset;