32 #include <boost/date_time/posix_time/posix_time_types.hpp> 33 #include <boost/math/special_functions/fpclassify.hpp> 34 #include <boost/regex.hpp> 38 #include <boost/shared_array.hpp> 45 int round(
double num);
60 template<
class InputIterator>
63 if (start == end)
return "";
65 InputIterator current = start++;
66 while (start != end) {
67 buffer << *current << delim;
85 template<
typename InputIterator>
92 InputIterator cur = start;
94 double v1Diff, v2Diff;
97 v1Diff = abs(x-*start);
101 double vDiff = fabs(x-*cur);
102 if (vDiff <= v2Diff) {
103 if (vDiff < v1Diff) {
109 }
else if (*cur != v1) {
132 template<
typename InputIterator>
140 InputIterator lowest = start;
141 InputIterator heighest = start;
142 InputIterator cur = start;
143 double lowDiff = x - *cur;
144 double heighDiff = *cur -x;
150 while (++cur != end) {
152 double diff = x - *cur;
153 if (diff < lowDiff) {
158 double diff = *cur - x;
159 if (diff < heighDiff) {
165 if (lowDiff == maxDiff ||
166 heighDiff == maxDiff) {
171 return make_pair<typename iterator_traits<InputIterator>::difference_type,
typename iterator_traits<InputIterator>::difference_type>(
distance(start, lowest),
distance(start, heighest));
181 template<
class InputIterator>
184 if (start == end)
return "";
186 InputIterator current = start++;
187 while (start != end) {
188 buffer << **current << delim;
288 if (current ==
"...") {
289 size_t currentPos = vals.
size();
290 if (currentPos < 2) {
291 throw CDMException(
"tokenizeDotted: cannot use ... expansion at position " +
type2string(currentPos-1) +
", need at least two values before");
293 T last = vals[currentPos-1];
294 T dist = last - vals[currentPos-2];
295 T curVal = last + dist;
297 double direction = (dist > 0) ? 1 : -1;
298 if (++tok != tokens.
end()) {
299 T afterDotVal = string2type<T>(*tok);
301 double roundError = direction*dist*-1.e-5;
302 while ((curVal - afterDotVal)*direction < roundError) {
307 vals.push_back(afterDotVal);
310 T val = string2type<T>(current);
318 template<
typename OUT>
320 template<
typename IN>
321 OUT
operator()(
const IN& in) {
return static_cast<OUT
>(in); }
331 return boost::math::isnan(x);
358 template<
typename IN,
typename OUT>
363 double oldScaleNewScaleInv_;
364 double oldOffsetMinusNewOffsetNewScaleInv_;
367 ScaleValue(
double oldFill,
double oldScale,
double oldOffset,
double newFill,
double newScale,
double newOffset) :
368 oldFill_(static_cast<IN>(oldFill)), oldScaleNewScaleInv_(oldScale/newScale),
369 oldOffsetMinusNewOffsetNewScaleInv_((oldOffset-newOffset)/newScale),
370 newFill_(static_cast<OUT>(newFill)) {}
372 return (in == oldFill_ || mifi_isnan<IN>(in))
374 :
static_cast<OUT
>(oldScaleNewScaleInv_*in + oldOffsetMinusNewOffsetNewScaleInv_);
384 template<
typename IN,
typename OUT>
391 boost::shared_ptr<UnitsConverter> uconv_;
396 ScaleValueUnits(
double oldFill,
double oldScale,
double oldOffset, boost::shared_ptr<UnitsConverter> uconv,
double newFill,
double newScale,
double newOffset) :
397 oldFill_(static_cast<IN>(oldFill)), oldScale_(oldScale), oldOffset_(oldOffset),
399 newFill_(static_cast<OUT>(newFill)), newScaleInv_(1/newScale), newOffset_(newOffset) {}
401 return (in == oldFill_ || mifi_isnan<IN>(in))
403 :
static_cast<OUT
>((uconv_->convert(oldScale_*in + oldOffset_)-newOffset_)*newScaleInv_);
411 template<
typename IN,
typename OUT>
419 oldFill_(static_cast<IN>(oldFill)), newFill_(static_cast<OUT>(newFill)) {}
423 :
static_cast<OUT
>(in);
440 boost::shared_array<T>
ptr;
basic_string< char > string
SharedArrayConstCastDeleter(boost::shared_array< T > ptr)
Definition: Utils.h:437
T string2type(std::string s)
Definition: Utils.h:230
Varargs & operator()(T arg)
Definition: Utils.h:246
boost::shared_array< const T > makeSharedArrayConst(const boost::shared_array< T > &sa)
Definition: Utils.h:446
ScaleValueUnits(double oldFill, double oldScale, double oldOffset, boost::shared_ptr< UnitsConverter > uconv, double newFill, double newScale, double newOffset)
Definition: Utils.h:396
epoch_seconds posixTime2epochTime(const boost::posix_time::ptime &time)
OUT operator()(const IN &in) const
Definition: Utils.h:371
std::vector< T > tokenizeDotted(const std::string &str, const std::string &delimiter=",")
Definition: Utils.h:282
int mifi_isnan(C x)
Definition: Utils.h:330
OUT operator()(const IN &in) const
Definition: Utils.h:400
basic_ostringstream< char > ostringstream
basic_stringstream< char > stringstream
Definition: CDMException.h:36
std::string string2lowerCase(const std::string &str)
void scanFiles(std::vector< std::string > &files, const std::string &dir, int depth, const boost::regex ®exp, bool matchFileOnly)
Varargs(T arg)
Definition: Utils.h:247
ScaleValue(double oldFill, double oldScale, double oldOffset, double newFill, double newScale, double newOffset)
Definition: Utils.h:367
Definition: C_CDMReader.h:35
std::string join(InputIterator start, InputIterator end, std::string delim=",")
Definition: Utils.h:61
std::string type2string< double >(double in)
void operator()(C *)
Definition: Utils.h:438
const_iterator end() const
void push_back(const value_type &__x)
const_iterator begin() const
iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
OUT operator()(const IN &in) const
Definition: Utils.h:420
void globFiles(std::vector< std::string > &files, const std::string &glob)
std::string trim(const std::string &str)
std::pair< typename std::iterator_traits< InputIterator >::difference_type, typename std::iterator_traits< InputIterator >::difference_type > find_closest_neighbor_distinct_elements(InputIterator start, InputIterator end, double x)
Definition: Utils.h:134
std::string type2string(T in)
Definition: Utils.h:216
std::pair< typename std::iterator_traits< InputIterator >::difference_type, typename std::iterator_traits< InputIterator >::difference_type > find_closest_distinct_elements(InputIterator start, InputIterator end, double x)
Definition: Utils.h:87
ChangeMissingValue(double oldFill, double newFill)
Definition: Utils.h:418
long epoch_seconds
Definition: Utils.h:269
std::string joinPtr(InputIterator start, InputIterator end, std::string delim=",")
Definition: Utils.h:182
boost::shared_array< T > ptr
Definition: Utils.h:440
void str(const __string_type &__s)
OUT operator()(const IN &in)
Definition: Utils.h:321
_Iterator::difference_type difference_type
_Iterator::value_type value_type
std::vector< std::string > tokenize(const std::string &str, const std::string &delimiters=" ")
T normalizeLongitude180(T in)
Definition: Utils.h:257
std::vector< T > args
Definition: Utils.h:244