| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#ifndef _STATFACTORY_H_ |
| 22 |
#define _STATFACTORY_H_ |
| 23 |
|
| 24 |
#include <iostream> |
| 25 |
#include <vector> |
| 26 |
#include <map> |
| 27 |
|
| 28 |
#include <cmath> |
| 29 |
#include <assert.h> |
| 30 |
#include <string> |
| 31 |
#include <sstream> |
| 32 |
#include <fstream> |
| 33 |
#include <algorithm> |
| 34 |
#include <gsl/gsl_fit.h> |
| 35 |
#include <gsl/gsl_errno.h> |
| 36 |
#include <gsl/gsl_spline.h> |
| 37 |
#include <gsl/gsl_rng.h> |
| 38 |
#include <gsl/gsl_randist.h> |
| 39 |
#include <gsl/gsl_cdf.h> |
| 40 |
#include <gsl/gsl_statistics.h> |
| 41 |
#include <gsl/gsl_sort.h> |
| 42 |
|
| 43 |
namespace statfactory |
| 44 |
{ |
| 45 |
|
| 46 |
class StatFactory{ |
| 47 |
|
| 48 |
public: |
| 49 |
enum INTERPOLATION_TYPE {undefined=0,linear=1,polynomial=2,cspline=3,cspline_periodic=4,akima=5,akima_periodic=6}; |
| 50 |
|
| 51 |
enum DISTRIBUTION_TYPE {none=0,uniform=1,gaussian=2}; |
| 52 |
|
| 53 |
StatFactory(void){}; |
| 54 |
virtual ~StatFactory(void){}; |
| 55 |
INTERPOLATION_TYPE getInterpolationType(const std::string interpolationType){ |
| 56 |
std::map<std::string, INTERPOLATION_TYPE> m_interpMap; |
| 57 |
initMap(m_interpMap); |
| 58 |
return m_interpMap[interpolationType]; |
| 59 |
}; |
| 60 |
DISTRIBUTION_TYPE getDistributionType(const std::string distributionType){ |
| 61 |
std::map<std::string, DISTRIBUTION_TYPE> m_distMap; |
| 62 |
initDist(m_distMap); |
| 63 |
return m_distMap[distributionType]; |
| 64 |
}; |
| 65 |
|
| 66 |
static void allocAcc(gsl_interp_accel *&acc){ |
| 67 |
acc = gsl_interp_accel_alloc (); |
| 68 |
}; |
| 69 |
|
| 70 |
static void getSpline(const std::string type, int size, gsl_spline *& spline){ |
| 71 |
std::map<std::string, INTERPOLATION_TYPE> m_interpMap; |
| 72 |
initMap(m_interpMap); |
| 73 |
switch(m_interpMap[type]){ |
| 74 |
case(polynomial): |
| 75 |
spline=gsl_spline_alloc(gsl_interp_polynomial,size); |
| 76 |
break; |
| 77 |
case(cspline): |
| 78 |
spline=gsl_spline_alloc(gsl_interp_cspline,size); |
| 79 |
break; |
| 80 |
case(cspline_periodic): |
| 81 |
spline=gsl_spline_alloc(gsl_interp_cspline_periodic,size); |
| 82 |
break; |
| 83 |
case(akima): |
| 84 |
spline=gsl_spline_alloc(gsl_interp_akima,size); |
| 85 |
break; |
| 86 |
case(akima_periodic): |
| 87 |
spline=gsl_spline_alloc(gsl_interp_akima_periodic,size); |
| 88 |
break; |
| 89 |
case(linear): |
| 90 |
default: |
| 91 |
spline=gsl_spline_alloc(gsl_interp_linear,size); |
| 92 |
break; |
| 93 |
} |
| 94 |
if(!spline){ |
| 95 |
std::string errorString="Warning: could not get gsl spline"; |
| 96 |
throw(errorString); |
| 97 |
} |
| 98 |
}; |
| 99 |
static int initSpline(gsl_spline *spline, const double *x, const double *y, int size){ |
| 100 |
return gsl_spline_init (spline, x, y, size); |
| 101 |
}; |
| 102 |
static double evalSpline(gsl_spline *spline, double x, gsl_interp_accel *acc){ |
| 103 |
return gsl_spline_eval (spline, x, acc); |
| 104 |
}; |
| 105 |
|
| 106 |
static gsl_rng* getRandomGenerator(unsigned long int theSeed){ |
| 107 |
gsl_rng * r; |
| 108 |
|
| 109 |
r = gsl_rng_alloc (gsl_rng_mt19937); |
| 110 |
gsl_rng_set(r,theSeed); |
| 111 |
return r; |
| 112 |
} |
| 113 |
static void freeRandomGenerator(gsl_rng* r){ |
| 114 |
|
| 115 |
gsl_rng_free(r); |
| 116 |
} |
| 117 |
void getNodataValues(std::vector<double>& nodatav) const{nodatav=m_noDataValues;}; |
| 118 |
bool isNoData(const double& value) const{ |
| 119 |
if(m_noDataValues.empty()) |
| 120 |
return false; |
| 121 |
else |
| 122 |
return find(m_noDataValues.begin(),m_noDataValues.end(),value)!=m_noDataValues.end(); |
| 123 |
}; |
| 124 |
unsigned int pushNodDataValue(double noDataValue){ |
| 125 |
if(find(m_noDataValues.begin(),m_noDataValues.end(),noDataValue)==m_noDataValues.end()) |
| 126 |
m_noDataValues.push_back(noDataValue); |
| 127 |
return m_noDataValues.size(); |
| 128 |
}; |
| 129 |
unsigned int clearNoDataValues(){ |
| 130 |
m_noDataValues.clear(); |
| 131 |
return m_noDataValues.size(); |
| 132 |
}; |
| 133 |
unsigned int setNoDataValues(std::vector<double> vnodata){ |
| 134 |
m_noDataValues=vnodata; |
| 135 |
return m_noDataValues.size(); |
| 136 |
}; |
| 137 |
double getRandomValue(const gsl_rng* r, const std::string type, double a=0, double b=1) const{ |
| 138 |
std::map<std::string, DISTRIBUTION_TYPE> m_distMap; |
| 139 |
initDist(m_distMap); |
| 140 |
double randValue=0; |
| 141 |
switch(m_distMap[type]){ |
| 142 |
case(none): |
| 143 |
randValue = a; |
| 144 |
break; |
| 145 |
case(uniform): |
| 146 |
randValue = a+(b-a)*gsl_rng_uniform(r); |
| 147 |
break; |
| 148 |
case(gaussian): |
| 149 |
randValue = a+gsl_ran_gaussian(r, b); |
| 150 |
break; |
| 151 |
default: |
| 152 |
std::string errorString="Error: distribution not valid"; |
| 153 |
throw(errorString); |
| 154 |
break; |
| 155 |
} |
| 156 |
return randValue; |
| 157 |
}; |
| 158 |
|
| 159 |
template<class T> T mymin(const typename std::vector<T>& v) const; |
| 160 |
template<class T> T mymax(const typename std::vector<T>& v) const; |
| 161 |
template<class T> T mymin(const typename std::vector<T>& v, T minConstraint) const; |
| 162 |
template<class T> T mymax(const typename std::vector<T>& v, T maxConstraint) const; |
| 163 |
template<class T> T absmin(const typename std::vector<T>& v) const; |
| 164 |
template<class T> T absmax(const typename std::vector<T>& v) const; |
| 165 |
|
| 166 |
|
| 167 |
template<class T> typename std::vector<T>::const_iterator mymin(const typename std::vector<T>& v, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end) const; |
| 168 |
template<class T> typename std::vector<T>::iterator mymin(const typename std::vector<T>& v, typename std::vector<T>::iterator begin, typename std::vector<T>::iterator end) const; |
| 169 |
template<class T> typename std::vector<T>::const_iterator mymin(const typename std::vector<T>& v, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end, T minConstraint) const; |
| 170 |
template<class T> typename std::vector<T>::iterator mymin(const typename std::vector<T>& v, typename std::vector<T>::iterator begin, typename std::vector<T>::iterator end, T minConstraint) const; |
| 171 |
template<class T> typename std::vector<T>::const_iterator mymax(const std::vector<T>& v, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end) const; |
| 172 |
template<class T> typename std::vector<T>::iterator mymax(const std::vector<T>& v, typename std::vector<T>::iterator begin, typename std::vector<T>::iterator end) const; |
| 173 |
template<class T> typename std::vector<T>::const_iterator mymax(const std::vector<T>& v, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end, T maxConstraint) const; |
| 174 |
template<class T> typename std::vector<T>::iterator mymax(const std::vector<T>& v, typename std::vector<T>::iterator begin, typename std::vector<T>::iterator end, T maxConstraint) const; |
| 175 |
template<class T> typename std::vector<T>::const_iterator absmin(const std::vector<T>& v, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end) const; |
| 176 |
template<class T> typename std::vector<T>::const_iterator absmax(const std::vector<T>& v, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end) const; |
| 177 |
|
| 178 |
template<class T> void minmax(const std::vector<T>& v, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end, T& theMin, T& theMax) const; |
| 179 |
template<class T> T sum(const std::vector<T>& v) const; |
| 180 |
template<class T> double mean(const std::vector<T>& v) const; |
| 181 |
template<class T> void eraseNoData(std::vector<T>& v) const; |
| 182 |
void eraseBelow(std::vector<double>& v, double min) const; |
| 183 |
void eraseAbove(std::vector<double>& v, double max) const; |
| 184 |
void setThreshold(std::vector<double>& v, double min, double max) const; |
| 185 |
template<class T> unsigned int nvalid(const std::vector<T>& v) const; |
| 186 |
template<class T> T median(const std::vector<T>& v) const; |
| 187 |
template<class T> double var(const std::vector<T>& v) const; |
| 188 |
template<class T> double moment(const std::vector<T>& v, int n) const; |
| 189 |
template<class T> double cmoment(const std::vector<T>& v, int n) const; |
| 190 |
template<class T> double skewness(const std::vector<T>& v) const; |
| 191 |
template<class T> double kurtosis(const std::vector<T>& v) const; |
| 192 |
template<class T> void meanVar(const std::vector<T>& v, double& m1, double& v1) const; |
| 193 |
template<class T1, class T2> void scale2byte(const std::vector<T1>& input, std::vector<T2>& output, unsigned char lbound=0, unsigned char ubound=255) const; |
| 194 |
template<class T> void distribution(const std::vector<T>& input, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end, std::vector<double>& output, int nbin, T &minimum, T &maximum, double sigma=0, const std::string &filename="") const; |
| 195 |
template<class T> void distribution(const std::vector<T>& input, std::vector<double>& output, int nbin, double sigma=0, const std::string &filename="") const{distribution(input,input.begin(),input.end(),output,nbin,0,0,sigma,filename);}; |
| 196 |
template<class T> void distribution2d(const std::vector<T>& inputX, const std::vector<T>& inputY, std::vector< std::vector<double> >& output, int nbin, T& minX, T& maxX, T& minY, T& maxY, double sigma=0, const std::string& filename="") const; |
| 197 |
template<class T> void cumulative (const std::vector<T>& input, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end, std::vector<int>& output, int nbin, T &minimum, T &maximum) const; |
| 198 |
template<class T> void percentiles (const std::vector<T>& input, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end, std::vector<T>& output, int nbin, T &minimum, T &maximum, const std::string &filename="") const; |
| 199 |
template<class T> T percentile(const std::vector<T>& input, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end, double percent, T minimum=0, T maximum=0) const; |
| 200 |
template<class T> void signature(const std::vector<T>& input, double& k, double& alpha, double& beta, double e) const; |
| 201 |
void signature(double m1, double m2, double& k, double& alpha, double& beta, double e) const; |
| 202 |
template<class T> void normalize(const std::vector<T>& input, std::vector<double>& output) const; |
| 203 |
template<class T> void normalize_pct(std::vector<T>& input) const; |
| 204 |
template<class T> double rmse(const std::vector<T>& x, const std::vector<T>& y) const; |
| 205 |
template<class T> double nrmse(const std::vector<T>& x, const std::vector<T>& y) const; |
| 206 |
template<class T> double cvrmse(const std::vector<T>& x, const std::vector<T>& y) const; |
| 207 |
template<class T> double correlation(const std::vector<T>& x, const std::vector<T>& y, int delay=0) const; |
| 208 |
|
| 209 |
template<class T> double gsl_covariance(const std::vector<T>& x, const std::vector<T>& y) const; |
| 210 |
template<class T> double cross_correlation(const std::vector<T>& x, const std::vector<T>& y, int maxdelay, std::vector<T>& z) const; |
| 211 |
template<class T> double linear_regression(const std::vector<T>& x, const std::vector<T>& y, double &c0, double &c1) const; |
| 212 |
template<class T> double linear_regression_err(const std::vector<T>& x, const std::vector<T>& y, double &c0, double &c1) const; |
| 213 |
template<class T> void interpolateNoData(const std::vector<double>& wavelengthIn, const std::vector<T>& input, const std::string& type, std::vector<T>& output, bool verbose=false) const; |
| 214 |
template<class T> void interpolateUp(const std::vector<double>& wavelengthIn, const std::vector<T>& input, const std::vector<double>& wavelengthOut, const std::string& type, std::vector<T>& output, bool verbose=false) const; |
| 215 |
template<class T> void interpolateUp(const std::vector<double>& wavelengthIn, const std::vector< std::vector<T> >& input, const std::vector<double>& wavelengthOut, const std::string& type, std::vector< std::vector<T> >& output, bool verbose=false) const; |
| 216 |
|
| 217 |
|
| 218 |
template<class T> void interpolateUp(const std::vector<T>& input, std::vector<T>& output, int nbin) const; |
| 219 |
template<class T> void nearUp(const std::vector<T>& input, std::vector<T>& output) const; |
| 220 |
template<class T> void interpolateUp(double* input, int dim, std::vector<T>& output, int nbin); |
| 221 |
template<class T> void interpolateDown(const std::vector<T>& input, std::vector<T>& output, int nbin) const; |
| 222 |
template<class T> void interpolateDown(double* input, int dim, std::vector<T>& output, int nbin); |
| 223 |
|
| 224 |
private: |
| 225 |
static void initMap(std::map<std::string, INTERPOLATION_TYPE>& m_interpMap){ |
| 226 |
|
| 227 |
m_interpMap["linear"]=linear; |
| 228 |
m_interpMap["polynomial"]=polynomial; |
| 229 |
m_interpMap["cspline"]=cspline; |
| 230 |
m_interpMap["cspline_periodic"]=cspline_periodic; |
| 231 |
m_interpMap["akima"]=akima; |
| 232 |
m_interpMap["akima_periodic"]=akima_periodic; |
| 233 |
} |
| 234 |
static void initDist(std::map<std::string, DISTRIBUTION_TYPE>& m_distMap){ |
| 235 |
|
| 236 |
m_distMap["gaussian"]=gaussian; |
| 237 |
m_distMap["uniform"]=uniform; |
| 238 |
m_distMap["none"]=none; |
| 239 |
} |
| 240 |
std::vector<double> m_noDataValues; |
| 241 |
}; |
| 242 |
|
| 243 |
|
| 244 |
template<class T> inline typename std::vector<T>::const_iterator StatFactory::mymin(const std::vector<T>& v, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end) const |
| 245 |
{ |
| 246 |
bool isValid=false; |
| 247 |
typename std::vector<T>::const_iterator tmpIt; |
| 248 |
for(typename std::vector<T>::const_iterator it = begin; it!=end; ++it){ |
| 249 |
if(!isNoData(*it)){ |
| 250 |
if(isValid){ |
| 251 |
if(*tmpIt>*it) |
| 252 |
tmpIt=it; |
| 253 |
} |
| 254 |
else{ |
| 255 |
tmpIt=it; |
| 256 |
isValid=true; |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
if(isValid) |
| 261 |
return tmpIt; |
| 262 |
else if(m_noDataValues.size()) |
| 263 |
return m_noDataValues[0]; |
| 264 |
else{ |
| 265 |
std::string errorString="Error: no valid data found"; |
| 266 |
throw(errorString); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
template<class T> inline typename std::vector<T>::iterator StatFactory::mymin(const std::vector<T>& v, typename std::vector<T>::iterator begin, typename std::vector<T>::iterator end) const |
| 271 |
{ |
| 272 |
bool isValid=false; |
| 273 |
typename std::vector<T>::iterator tmpIt; |
| 274 |
for (typename std::vector<T>::const_iterator it = begin; it!=end; ++it){ |
| 275 |
if(!isNoData(*it)){ |
| 276 |
if(isValid){ |
| 277 |
if(*tmpIt>*it) |
| 278 |
tmpIt=it; |
| 279 |
} |
| 280 |
else{ |
| 281 |
tmpIt=it; |
| 282 |
isValid=true; |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
if(isValid) |
| 287 |
return tmpIt; |
| 288 |
else if(m_noDataValues.size()) |
| 289 |
return m_noDataValues[0]; |
| 290 |
else{ |
| 291 |
std::string errorString="Error: no valid data found"; |
| 292 |
throw(errorString); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
template<class T> inline typename std::vector<T>::const_iterator StatFactory::mymin(const std::vector<T>& v, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end, T minConstraint) const |
| 297 |
{ |
| 298 |
bool isValid=false; |
| 299 |
typename std::vector<T>::const_iterator tmpIt; |
| 300 |
T minValue=minConstraint; |
| 301 |
for (typename std::vector<T>::const_iterator it = begin; it!=end; ++it){ |
| 302 |
if(isNoData(*it)) |
| 303 |
continue; |
| 304 |
if(isValid){ |
| 305 |
if((minConstraint<=*it)&&(*it<minValue)){ |
| 306 |
tmpIt=it; |
| 307 |
minValue=*it; |
| 308 |
} |
| 309 |
} |
| 310 |
else{ |
| 311 |
if(*it<minValue) |
| 312 |
continue; |
| 313 |
tmpIt=it; |
| 314 |
minValue=*it; |
| 315 |
isValid=true; |
| 316 |
} |
| 317 |
} |
| 318 |
if(isValid) |
| 319 |
return tmpIt; |
| 320 |
else if(m_noDataValues.size()) |
| 321 |
return m_noDataValues[0]; |
| 322 |
else{ |
| 323 |
std::string errorString="Error: no valid data found"; |
| 324 |
throw(errorString); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
template<class T> inline typename std::vector<T>::iterator StatFactory::mymin(const std::vector<T>& v, typename std::vector<T>::iterator begin, typename std::vector<T>::iterator end, T minConstraint) const |
| 329 |
{ |
| 330 |
bool isValid=false; |
| 331 |
typename std::vector<T>::iterator tmpIt; |
| 332 |
T minValue=minConstraint; |
| 333 |
for (typename std::vector<T>::const_iterator it = begin; it!=end; ++it){ |
| 334 |
if(isNoData(*it)) |
| 335 |
continue; |
| 336 |
if(isValid){ |
| 337 |
if((minConstraint<=*it)&&(*it<minValue)){ |
| 338 |
tmpIt=it; |
| 339 |
minValue=*it; |
| 340 |
} |
| 341 |
} |
| 342 |
else{ |
| 343 |
if(*it<minConstraint) |
| 344 |
continue; |
| 345 |
tmpIt=it; |
| 346 |
minValue=*it; |
| 347 |
isValid=true; |
| 348 |
} |
| 349 |
} |
| 350 |
if(isValid) |
| 351 |
return tmpIt; |
| 352 |
else if(m_noDataValues.size()) |
| 353 |
return m_noDataValues[0]; |
| 354 |
else{ |
| 355 |
std::string errorString="Error: no valid data found"; |
| 356 |
throw(errorString); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
template<class T> inline typename std::vector<T>::const_iterator StatFactory::mymax(const std::vector<T>& v, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end) const |
| 361 |
{ |
| 362 |
bool isValid=false; |
| 363 |
typename std::vector<T>::const_iterator tmpIt; |
| 364 |
for (typename std::vector<T>::iterator it = begin; it!=end; ++it){ |
| 365 |
if(isNoData(*it)) |
| 366 |
continue; |
| 367 |
if(isValid){ |
| 368 |
if(*tmpIt<*it) |
| 369 |
tmpIt=it; |
| 370 |
} |
| 371 |
else{ |
| 372 |
tmpIt=it; |
| 373 |
isValid=true; |
| 374 |
} |
| 375 |
} |
| 376 |
if(isValid) |
| 377 |
return tmpIt; |
| 378 |
else if(m_noDataValues.size()) |
| 379 |
return m_noDataValues[0]; |
| 380 |
else{ |
| 381 |
std::string errorString="Error: no valid data found"; |
| 382 |
throw(errorString); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
template<class T> inline typename std::vector<T>::iterator StatFactory::mymax(const std::vector<T>& v, typename std::vector<T>::iterator begin, typename std::vector<T>::iterator end) const |
| 387 |
{ |
| 388 |
bool isValid=false; |
| 389 |
typename std::vector<T>::iterator tmpIt; |
| 390 |
for (typename std::vector<T>::iterator it = begin; it!=end; ++it){ |
| 391 |
if(isNoData(*it)) |
| 392 |
continue; |
| 393 |
if(isValid){ |
| 394 |
if(*tmpIt<*it) |
| 395 |
tmpIt=it; |
| 396 |
} |
| 397 |
else{ |
| 398 |
tmpIt=it; |
| 399 |
isValid=true; |
| 400 |
} |
| 401 |
} |
| 402 |
if(isValid) |
| 403 |
return tmpIt; |
| 404 |
else |
| 405 |
return end; |
| 406 |
} |
| 407 |
|
| 408 |
template<class T> inline typename std::vector<T>::const_iterator StatFactory::mymax(const std::vector<T>& v, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end, T maxConstraint) const |
| 409 |
{ |
| 410 |
bool isValid=false; |
| 411 |
typename std::vector<T>::const_iterator tmpIt; |
| 412 |
T maxValue=maxConstraint; |
| 413 |
for (typename std::vector<T>::const_iterator it = begin; it!=end; ++it){ |
| 414 |
if(isNoData(*it)) |
| 415 |
continue; |
| 416 |
if(isValid){ |
| 417 |
if((maxConstraint>=*it)&&(*it>maxValue)){ |
| 418 |
tmpIt=it; |
| 419 |
maxValue=*it; |
| 420 |
} |
| 421 |
} |
| 422 |
else{ |
| 423 |
if(*it>maxConstraint) |
| 424 |
continue; |
| 425 |
tmpIt=it; |
| 426 |
maxValue=*it; |
| 427 |
isValid=true; |
| 428 |
} |
| 429 |
} |
| 430 |
if(isValid) |
| 431 |
return tmpIt; |
| 432 |
else |
| 433 |
return end; |
| 434 |
} |
| 435 |
|
| 436 |
template<class T> inline typename std::vector<T>::iterator StatFactory::mymax(const std::vector<T>& v, typename std::vector<T>::iterator begin, typename std::vector<T>::iterator end, T maxConstraint) const |
| 437 |
{ |
| 438 |
bool isValid=false; |
| 439 |
typename std::vector<T>::iterator tmpIt=v.end(); |
| 440 |
T maxValue=maxConstraint; |
| 441 |
for (typename std::vector<T>::const_iterator it = begin; it!=end; ++it){ |
| 442 |
if(isNoData(*it)) |
| 443 |
continue; |
| 444 |
if(isValid){ |
| 445 |
if((maxConstraint>=*it)&&(*it>maxValue)){ |
| 446 |
tmpIt=it; |
| 447 |
maxValue=*it; |
| 448 |
} |
| 449 |
} |
| 450 |
else{ |
| 451 |
if(*it>maxValue) |
| 452 |
continue; |
| 453 |
tmpIt=it; |
| 454 |
maxValue=*it; |
| 455 |
isValid=true; |
| 456 |
} |
| 457 |
} |
| 458 |
if(isValid) |
| 459 |
return tmpIt; |
| 460 |
else |
| 461 |
return end; |
| 462 |
} |
| 463 |
|
| 464 |
template<class T> inline T StatFactory::mymin(const std::vector<T>& v) const |
| 465 |
{ |
| 466 |
bool isValid=false; |
| 467 |
if(v.empty()){ |
| 468 |
std::string errorString="Error: vector is empty"; |
| 469 |
throw(errorString); |
| 470 |
} |
| 471 |
T minValue; |
| 472 |
for (typename std::vector<T>::const_iterator it = v.begin(); it!=v.end(); ++it){ |
| 473 |
if(isNoData(*it)) |
| 474 |
continue; |
| 475 |
if(isValid){ |
| 476 |
if(minValue>*it) |
| 477 |
minValue=*it; |
| 478 |
} |
| 479 |
else{ |
| 480 |
minValue=*it; |
| 481 |
isValid=true; |
| 482 |
} |
| 483 |
} |
| 484 |
if(isValid) |
| 485 |
return minValue; |
| 486 |
else if(m_noDataValues.size()) |
| 487 |
return m_noDataValues[0]; |
| 488 |
else{ |
| 489 |
std::string errorString="Error: no valid data found"; |
| 490 |
throw(errorString); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
template<class T> inline T StatFactory::mymin(const std::vector<T>& v, T minConstraint) const |
| 495 |
{ |
| 496 |
bool isValid=false; |
| 497 |
T minValue=minConstraint; |
| 498 |
for (typename std::vector<T>::const_iterator it = v.begin(); it!=v.end(); ++it){ |
| 499 |
if(isNoData(*it)) |
| 500 |
continue; |
| 501 |
if(isValid){ |
| 502 |
if((minConstraint<=*it)&&(*it<minValue)) |
| 503 |
minValue=*it; |
| 504 |
} |
| 505 |
else{ |
| 506 |
if(*it<minValue) |
| 507 |
continue; |
| 508 |
minValue=*it; |
| 509 |
isValid=true; |
| 510 |
} |
| 511 |
} |
| 512 |
if(isValid) |
| 513 |
return minValue; |
| 514 |
else if(m_noDataValues.size()) |
| 515 |
return m_noDataValues[0]; |
| 516 |
else{ |
| 517 |
std::string errorString="Error: no valid data found"; |
| 518 |
throw(errorString); |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
template<class T> inline T StatFactory::mymax(const std::vector<T>& v) const |
| 523 |
{ |
| 524 |
bool isValid=false; |
| 525 |
if(v.empty()){ |
| 526 |
std::string errorString="Error: vector is empty"; |
| 527 |
throw(errorString); |
| 528 |
} |
| 529 |
T maxValue; |
| 530 |
for (typename std::vector<T>::const_iterator it = v.begin(); it!=v.end(); ++it){ |
| 531 |
if(isNoData(*it)) |
| 532 |
continue; |
| 533 |
if(isValid){ |
| 534 |
if(maxValue<*it) |
| 535 |
maxValue=*it; |
| 536 |
} |
| 537 |
else{ |
| 538 |
maxValue=*it; |
| 539 |
isValid=true; |
| 540 |
} |
| 541 |
} |
| 542 |
if(isValid) |
| 543 |
return maxValue; |
| 544 |
else if(m_noDataValues.size()) |
| 545 |
return m_noDataValues[0]; |
| 546 |
else{ |
| 547 |
std::string errorString="Error: no valid data found"; |
| 548 |
throw(errorString); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
template<class T> inline T StatFactory::mymax(const std::vector<T>& v, T maxConstraint) const |
| 553 |
{ |
| 554 |
bool isValid=false; |
| 555 |
T maxValue=maxConstraint; |
| 556 |
for (typename std::vector<T>::const_iterator it = v.begin(); it!=v.end(); ++it){ |
| 557 |
if(isNoData(*it)) |
| 558 |
continue; |
| 559 |
if(isValid){ |
| 560 |
if((*it<=maxConstraint)&&(*it>maxValue)) |
| 561 |
maxValue=*it; |
| 562 |
} |
| 563 |
else{ |
| 564 |
if(*it>maxValue) |
| 565 |
continue; |
| 566 |
maxValue=*it; |
| 567 |
isValid=true; |
| 568 |
} |
| 569 |
} |
| 570 |
if(isValid) |
| 571 |
return maxValue; |
| 572 |
else if(m_noDataValues.size()) |
| 573 |
return m_noDataValues[0]; |
| 574 |
else{ |
| 575 |
std::string errorString="Error: no valid data found"; |
| 576 |
throw(errorString); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
template<class T> inline typename std::vector<T>::const_iterator StatFactory::absmin(const std::vector<T>& v, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end) const |
| 581 |
{ |
| 582 |
bool isValid=false; |
| 583 |
if(v.empty()){ |
| 584 |
std::string errorString="Error: vector is empty"; |
| 585 |
throw(errorString); |
| 586 |
} |
| 587 |
typename std::vector<T>::const_iterator tmpIt; |
| 588 |
for (typename std::vector<T>::const_iterator it = begin; it!=end; ++it){ |
| 589 |
if(isNoData(*it)) |
| 590 |
continue; |
| 591 |
if(isValid){ |
| 592 |
if(abs(*tmpIt)>abs(*it)) |
| 593 |
tmpIt=it; |
| 594 |
} |
| 595 |
else{ |
| 596 |
tmpIt=it; |
| 597 |
isValid=true; |
| 598 |
} |
| 599 |
} |
| 600 |
if(isValid) |
| 601 |
return tmpIt; |
| 602 |
else |
| 603 |
return end; |
| 604 |
} |
| 605 |
|
| 606 |
template<class T> inline T StatFactory::absmin(const std::vector<T>& v) const |
| 607 |
{ |
| 608 |
typename std::vector<T>::const_iterator tmpIt; |
| 609 |
tmpIt=absmin(v, v.begin(), v.end()); |
| 610 |
if(tmpIt!=v.end()) |
| 611 |
return *tmpIt; |
| 612 |
else if(m_noDataValues.size()) |
| 613 |
return m_noDataValues[0]; |
| 614 |
else{ |
| 615 |
std::string errorString="Error: no valid data found"; |
| 616 |
throw(errorString); |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
template<class T> inline typename std::vector<T>::const_iterator StatFactory::absmax(const std::vector<T>& v, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end) const |
| 621 |
{ |
| 622 |
bool isValid=false; |
| 623 |
typename std::vector<T>::const_iterator tmpIt; |
| 624 |
for (typename std::vector<T>::const_iterator it = begin; it!=end; ++it){ |
| 625 |
if(isNoData(*it)) |
| 626 |
continue; |
| 627 |
if(isValid){ |
| 628 |
if(abs(*tmpIt)<abs(*it)) |
| 629 |
tmpIt=it; |
| 630 |
} |
| 631 |
else{ |
| 632 |
tmpIt=it; |
| 633 |
isValid=true; |
| 634 |
} |
| 635 |
} |
| 636 |
if(isValid) |
| 637 |
return tmpIt; |
| 638 |
else |
| 639 |
return end; |
| 640 |
} |
| 641 |
|
| 642 |
template<class T> inline T StatFactory::absmax(const std::vector<T>& v) const |
| 643 |
{ |
| 644 |
typename std::vector<T>::const_iterator tmpIt; |
| 645 |
tmpIt=absmax(v, v.begin(), v.end()); |
| 646 |
if(tmpIt!=v.end()) |
| 647 |
return *tmpIt; |
| 648 |
else if(m_noDataValues.size()) |
| 649 |
return m_noDataValues[0]; |
| 650 |
else{ |
| 651 |
std::string errorString="Error: no valid data found"; |
| 652 |
throw(errorString); |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
template<class T> inline void StatFactory::minmax(const std::vector<T>& v, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end, T& theMin, T& theMax) const |
| 657 |
{ |
| 658 |
bool isConstraint=(theMax>theMin); |
| 659 |
double minConstraint=theMin; |
| 660 |
double maxConstraint=theMax; |
| 661 |
bool isValid=false; |
| 662 |
for (typename std::vector<T>::const_iterator it = begin; it!=end; ++it){ |
| 663 |
if(isNoData(*it)) |
| 664 |
continue; |
| 665 |
if(isValid){ |
| 666 |
if(isConstraint){ |
| 667 |
if(*it<minConstraint) |
| 668 |
continue; |
| 669 |
if(*it>maxConstraint) |
| 670 |
continue; |
| 671 |
} |
| 672 |
if(*it<theMin) |
| 673 |
theMin=*it; |
| 674 |
if(*it>theMax) |
| 675 |
theMax=*it; |
| 676 |
} |
| 677 |
else{ |
| 678 |
if(isConstraint){ |
| 679 |
if(*it<minConstraint) |
| 680 |
continue; |
| 681 |
if(*it>maxConstraint) |
| 682 |
continue; |
| 683 |
} |
| 684 |
theMin=*it; |
| 685 |
theMax=*it; |
| 686 |
isValid=true; |
| 687 |
} |
| 688 |
} |
| 689 |
if(!isValid){ |
| 690 |
if(m_noDataValues.size()){ |
| 691 |
theMin=m_noDataValues[0]; |
| 692 |
theMax=m_noDataValues[0]; |
| 693 |
} |
| 694 |
else{ |
| 695 |
std::string errorString="Error: no valid data found"; |
| 696 |
throw(errorString); |
| 697 |
} |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
template<class T> inline T StatFactory::sum(const std::vector<T>& v) const |
| 702 |
{ |
| 703 |
bool isValid=false; |
| 704 |
typename std::vector<T>::const_iterator it; |
| 705 |
T tmpSum=0; |
| 706 |
for (it = v.begin(); it!= v.end(); ++it){ |
| 707 |
if(isNoData(*it)) |
| 708 |
continue; |
| 709 |
isValid=true; |
| 710 |
tmpSum+=*it; |
| 711 |
} |
| 712 |
if(isValid) |
| 713 |
return tmpSum; |
| 714 |
else if(m_noDataValues.size()) |
| 715 |
return m_noDataValues[0]; |
| 716 |
else{ |
| 717 |
std::string errorString="Error: no valid data found"; |
| 718 |
throw(errorString); |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
template<class T> inline double StatFactory::mean(const std::vector<T>& v) const |
| 723 |
{ |
| 724 |
typename std::vector<T>::const_iterator it; |
| 725 |
T tmpSum=0; |
| 726 |
unsigned int validSize=0; |
| 727 |
for (it = v.begin(); it!= v.end(); ++it){ |
| 728 |
if(isNoData(*it)) |
| 729 |
continue; |
| 730 |
++validSize; |
| 731 |
tmpSum+=*it; |
| 732 |
} |
| 733 |
if(validSize) |
| 734 |
return static_cast<double>(tmpSum)/validSize; |
| 735 |
else if(m_noDataValues.size()) |
| 736 |
return m_noDataValues[0]; |
| 737 |
else{ |
| 738 |
std::string errorString="Error: no valid data found"; |
| 739 |
throw(errorString); |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
struct isAbove |
| 744 |
{ |
| 745 |
isAbove(double maxValue) : m_max(maxValue) {} |
| 746 |
double m_max; |
| 747 |
bool operator()(double value) |
| 748 |
{ |
| 749 |
return value>m_max; |
| 750 |
} |
| 751 |
}; |
| 752 |
|
| 753 |
struct isBelow |
| 754 |
{ |
| 755 |
isBelow(double minValue) : m_min(minValue) {} |
| 756 |
double m_min; |
| 757 |
bool operator()(double value) |
| 758 |
{ |
| 759 |
return value<m_min; |
| 760 |
} |
| 761 |
}; |
| 762 |
|
| 763 |
struct notWithin |
| 764 |
{ |
| 765 |
notWithin(double minValue, double maxValue) : m_min(minValue), m_max(maxValue) {} |
| 766 |
double m_min; |
| 767 |
double m_max; |
| 768 |
bool operator()(double value) |
| 769 |
{ |
| 770 |
return value<m_min||value>m_max; |
| 771 |
} |
| 772 |
}; |
| 773 |
|
| 774 |
|
| 775 |
inline void StatFactory::setThreshold(std::vector<double>& v, double min, double max) const |
| 776 |
{ |
| 777 |
|
| 778 |
v.erase( std::remove_if(v.begin(), v.end(), notWithin(min,max)), v.end() ); |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
inline void StatFactory::eraseAbove(std::vector<double>& v, double max) const |
| 783 |
{ |
| 784 |
|
| 785 |
v.erase( std::remove_if(v.begin(), v.end(), isAbove(max)), v.end() ); |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
inline void StatFactory::eraseBelow(std::vector<double>& v, double min) const |
| 790 |
{ |
| 791 |
|
| 792 |
v.erase( std::remove_if(v.begin(), v.end(), isBelow(min)), v.end() ); |
| 793 |
} |
| 794 |
|
| 795 |
template<class T> inline void StatFactory::eraseNoData(std::vector<T>& v) const |
| 796 |
{ |
| 797 |
|
| 798 |
std::vector<double>::const_iterator vit=m_noDataValues.begin(); |
| 799 |
for(vit=m_noDataValues.begin();vit!=m_noDataValues.end();++vit) |
| 800 |
v.erase( std::remove(v.begin(), v.end(), *vit), v.end() ); |
| 801 |
} |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
template<class T> unsigned int StatFactory::nvalid(const std::vector<T>& v) const{ |
| 817 |
std::vector<T> tmpV=v; |
| 818 |
eraseNoData(tmpV); |
| 819 |
return(tmpV.size()); |
| 820 |
} |
| 821 |
|
| 822 |
template<class T> T StatFactory::median(const std::vector<T>& v) const |
| 823 |
{ |
| 824 |
if(v.empty()){ |
| 825 |
std::ostringstream s; |
| 826 |
s<<"Error: input is empty"; |
| 827 |
throw(s.str()); |
| 828 |
} |
| 829 |
std::vector<double> inputSort; |
| 830 |
inputSort.assign(v.begin(),v.end()); |
| 831 |
size_t stride=1; |
| 832 |
gsl_sort(&(inputSort[0]),stride,inputSort.size()); |
| 833 |
return gsl_stats_median_from_sorted_data(&(inputSort[0]),stride,inputSort.size()); |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
} |
| 850 |
|
| 851 |
template<class T> double StatFactory::var(const std::vector<T>& v) const |
| 852 |
{ |
| 853 |
if(v.empty()){ |
| 854 |
std::ostringstream s; |
| 855 |
s<<"Error: input is empty"; |
| 856 |
throw(s.str()); |
| 857 |
} |
| 858 |
size_t stride=1; |
| 859 |
return gsl_stats_variance(&(v[0]),stride,v.size()); |
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
} |
| 883 |
|
| 884 |
template<class T> double StatFactory::moment(const std::vector<T>& v, int n) const |
| 885 |
{ |
| 886 |
unsigned int validSize=0; |
| 887 |
typename std::vector<T>::const_iterator it; |
| 888 |
double m=0; |
| 889 |
|
| 890 |
for(it = v.begin(); it!= v.end(); ++it){ |
| 891 |
if(isNoData(*it)) |
| 892 |
continue; |
| 893 |
m+=pow((*it),n); |
| 894 |
++validSize; |
| 895 |
} |
| 896 |
if(validSize) |
| 897 |
return m/validSize; |
| 898 |
else if(m_noDataValues.size()) |
| 899 |
return m_noDataValues[0]; |
| 900 |
else{ |
| 901 |
std::string errorString="Error: no valid data found"; |
| 902 |
throw(errorString); |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
|
| 907 |
template<class T> double StatFactory::cmoment(const std::vector<T>& v, int n) const |
| 908 |
{ |
| 909 |
unsigned int validSize=0; |
| 910 |
typename std::vector<T>::const_iterator it; |
| 911 |
double m=0; |
| 912 |
double m1=mean(v); |
| 913 |
for(it = v.begin(); it!= v.end(); ++it){ |
| 914 |
if(isNoData(*it)) |
| 915 |
continue; |
| 916 |
m+=pow((*it-m1),n); |
| 917 |
++validSize; |
| 918 |
} |
| 919 |
if(validSize) |
| 920 |
return m/validSize; |
| 921 |
else if(m_noDataValues.size()) |
| 922 |
return m_noDataValues[0]; |
| 923 |
else{ |
| 924 |
std::string errorString="Error: no valid data found"; |
| 925 |
throw(errorString); |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
template<class T> double StatFactory::skewness(const std::vector<T>& v) const |
| 930 |
{ |
| 931 |
|
| 932 |
return cmoment(v,3)/pow(var(v),1.5); |
| 933 |
} |
| 934 |
|
| 935 |
template<class T> double StatFactory::kurtosis(const std::vector<T>& v) const |
| 936 |
{ |
| 937 |
|
| 938 |
double m2=cmoment(v,2); |
| 939 |
double m4=cmoment(v,4); |
| 940 |
return m4/m2/m2-3.0; |
| 941 |
} |
| 942 |
|
| 943 |
template<class T> void StatFactory::meanVar(const std::vector<T>& v, double& m1, double& v1) const |
| 944 |
{ |
| 945 |
if(v.empty()){ |
| 946 |
std::ostringstream s; |
| 947 |
s<<"Error: input is empty"; |
| 948 |
throw(s.str()); |
| 949 |
} |
| 950 |
size_t stride=1; |
| 951 |
m1=gsl_stats_mean(&(v[0]),stride,v.size()); |
| 952 |
v1=gsl_stats_variance(&(v[0]),stride,v.size()); |
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
} |
| 979 |
|
| 980 |
template<class T1, class T2> void StatFactory::scale2byte(const std::vector<T1>& input, std::vector<T2>& output, unsigned char lbound, unsigned char ubound) const |
| 981 |
{ |
| 982 |
output.resize(input.size()); |
| 983 |
T1 minimum=mymin(input); |
| 984 |
T1 maximum=mymax(input); |
| 985 |
if(minimum>=maximum){ |
| 986 |
std::string errorString="Error: no valid data found"; |
| 987 |
throw(errorString); |
| 988 |
} |
| 989 |
double scale=(ubound-lbound)/(maximum-minimum); |
| 990 |
|
| 991 |
for (int i=0;i<input.size();++i){ |
| 992 |
double value=scale*(input[i]-(minimum))+lbound; |
| 993 |
output[i]=static_cast<T2>(value); |
| 994 |
} |
| 995 |
} |
| 996 |
|
| 997 |
template<class T> void StatFactory::distribution(const std::vector<T>& input, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end, std::vector<double>& output, int nbin, T &minimum, T &maximum, double sigma, const std::string &filename) const |
| 998 |
{ |
| 999 |
double minValue=0; |
| 1000 |
double maxValue=0; |
| 1001 |
minmax(input,begin,end,minimum,maximum); |
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
if(maximum<=minimum){ |
| 1008 |
std::ostringstream s; |
| 1009 |
s<<"Error: could not calculate distribution (min>=max)"; |
| 1010 |
throw(s.str()); |
| 1011 |
} |
| 1012 |
if(!nbin){ |
| 1013 |
std::string errorString="Error: nbin not defined"; |
| 1014 |
throw(errorString); |
| 1015 |
} |
| 1016 |
if(!input.size()){ |
| 1017 |
std::string errorString="Error: no valid data found"; |
| 1018 |
throw(errorString); |
| 1019 |
} |
| 1020 |
if(output.size()!=nbin){ |
| 1021 |
output.resize(nbin); |
| 1022 |
for(int i=0;i<nbin;output[i++]=0); |
| 1023 |
} |
| 1024 |
bool isValid=false; |
| 1025 |
typename std::vector<T>::const_iterator it; |
| 1026 |
for(it=begin;it!=end;++it){ |
| 1027 |
if(*it<minimum) |
| 1028 |
continue; |
| 1029 |
if(*it>maximum) |
| 1030 |
continue; |
| 1031 |
if(isNoData(*it)) |
| 1032 |
continue; |
| 1033 |
isValid=true; |
| 1034 |
if(sigma>0){ |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
for(int ibin=0;ibin<nbin;++ibin){ |
| 1041 |
double icenter=minimum+static_cast<double>(maximum-minimum)*(ibin+0.5)/nbin; |
| 1042 |
double thePdf=gsl_ran_gaussian_pdf(*it-icenter, sigma); |
| 1043 |
output[ibin]+=thePdf; |
| 1044 |
} |
| 1045 |
} |
| 1046 |
else{ |
| 1047 |
int theBin=0; |
| 1048 |
if(*it==maximum) |
| 1049 |
theBin=nbin-1; |
| 1050 |
else if(*it>minimum && *it<maximum) |
| 1051 |
theBin=static_cast<int>(static_cast<double>((nbin-1)*(*it)-minimum)/(maximum-minimum)); |
| 1052 |
++output[theBin]; |
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
} |
| 1058 |
} |
| 1059 |
if(!isValid){ |
| 1060 |
std::string errorString="Error: no valid data found"; |
| 1061 |
throw(errorString); |
| 1062 |
} |
| 1063 |
else if(!filename.empty()){ |
| 1064 |
std::ofstream outputfile; |
| 1065 |
outputfile.open(filename.c_str()); |
| 1066 |
if(!outputfile){ |
| 1067 |
std::ostringstream s; |
| 1068 |
s<<"Error opening distribution file , " << filename; |
| 1069 |
throw(s.str()); |
| 1070 |
} |
| 1071 |
for(int ibin=0;ibin<nbin;++ibin) |
| 1072 |
outputfile << minimum+static_cast<double>(maximum-minimum)*(ibin+0.5)/nbin << " " << static_cast<double>(output[ibin])/input.size() << std::endl; |
| 1073 |
outputfile.close(); |
| 1074 |
} |
| 1075 |
} |
| 1076 |
|
| 1077 |
template<class T> void StatFactory::distribution2d(const std::vector<T>& inputX, const std::vector<T>& inputY, std::vector< std::vector<double> >& output, int nbin, T& minX, T& maxX, T& minY, T& maxY, double sigma, const std::string& filename) const |
| 1078 |
{ |
| 1079 |
if(inputX.empty()){ |
| 1080 |
std::ostringstream s; |
| 1081 |
s<<"Error: inputX is empty"; |
| 1082 |
throw(s.str()); |
| 1083 |
} |
| 1084 |
if(inputX.size()!=inputY.size()){ |
| 1085 |
std::ostringstream s; |
| 1086 |
s<<"Error: inputX is empty"; |
| 1087 |
throw(s.str()); |
| 1088 |
} |
| 1089 |
unsigned long int npoint=inputX.size(); |
| 1090 |
if(maxX<=minX) |
| 1091 |
minmax(inputX,inputX.begin(),inputX.end(),minX,maxX); |
| 1092 |
if(maxX<=minX){ |
| 1093 |
std::ostringstream s; |
| 1094 |
s<<"Error: could not calculate distribution (minX>=maxX)"; |
| 1095 |
throw(s.str()); |
| 1096 |
} |
| 1097 |
if(maxY<=minY) |
| 1098 |
minmax(inputY,inputY.begin(),inputY.end(),minY,maxY); |
| 1099 |
if(maxY<=minY){ |
| 1100 |
std::ostringstream s; |
| 1101 |
s<<"Error: could not calculate distribution (minY>=maxY)"; |
| 1102 |
throw(s.str()); |
| 1103 |
} |
| 1104 |
if(nbin<=1){ |
| 1105 |
std::ostringstream s; |
| 1106 |
s<<"Error: nbin must be larger than 1"; |
| 1107 |
throw(s.str()); |
| 1108 |
} |
| 1109 |
output.resize(nbin); |
| 1110 |
for(int i=0;i<nbin;++i){ |
| 1111 |
output[i].resize(nbin); |
| 1112 |
for(int j=0;j<nbin;++j) |
| 1113 |
output[i][j]=0; |
| 1114 |
} |
| 1115 |
int binX=0; |
| 1116 |
int binY=0; |
| 1117 |
for(unsigned long int ipoint=0;ipoint<npoint;++ipoint){ |
| 1118 |
if(inputX[ipoint]==maxX) |
| 1119 |
binX=nbin-1; |
| 1120 |
else |
| 1121 |
binX=static_cast<int>(static_cast<double>(inputX[ipoint]-minX)/(maxX-minX)*nbin); |
| 1122 |
if(inputY[ipoint]==maxY) |
| 1123 |
binY=nbin-1; |
| 1124 |
else |
| 1125 |
binY=static_cast<int>(static_cast<double>(inputY[ipoint]-minY)/(maxY-minY)*nbin); |
| 1126 |
if(binX<0){ |
| 1127 |
std::ostringstream s; |
| 1128 |
s<<"Error: binX is smaller than 0"; |
| 1129 |
throw(s.str()); |
| 1130 |
} |
| 1131 |
if(output.size()<=binX){ |
| 1132 |
std::ostringstream s; |
| 1133 |
s<<"Error: output size must be larger than binX"; |
| 1134 |
throw(s.str()); |
| 1135 |
} |
| 1136 |
if(binY<0){ |
| 1137 |
std::ostringstream s; |
| 1138 |
s<<"Error: binY is smaller than 0"; |
| 1139 |
throw(s.str()); |
| 1140 |
} |
| 1141 |
if(output.size()<=binY){ |
| 1142 |
std::ostringstream s; |
| 1143 |
s<<"Error: output size must be larger than binY"; |
| 1144 |
throw(s.str()); |
| 1145 |
} |
| 1146 |
if(sigma>0){ |
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
for(int ibinX=0;ibinX<nbin;++ibinX){ |
| 1154 |
double centerX=minX+static_cast<double>(maxX-minX)*ibinX/nbin; |
| 1155 |
double pdfX=gsl_ran_gaussian_pdf(inputX[ipoint]-centerX, sigma); |
| 1156 |
for(int ibinY=0;ibinY<nbin;++ibinY){ |
| 1157 |
|
| 1158 |
double centerY=minY+static_cast<double>(maxY-minY)*ibinY/nbin; |
| 1159 |
double pdfY=gsl_ran_gaussian_pdf(inputY[ipoint]-centerY, sigma); |
| 1160 |
output[ibinX][binY]+=pdfX*pdfY; |
| 1161 |
} |
| 1162 |
} |
| 1163 |
} |
| 1164 |
else |
| 1165 |
++output[binX][binY]; |
| 1166 |
} |
| 1167 |
if(!filename.empty()){ |
| 1168 |
std::ofstream outputfile; |
| 1169 |
outputfile.open(filename.c_str()); |
| 1170 |
if(!outputfile){ |
| 1171 |
std::ostringstream s; |
| 1172 |
s<<"Error opening distribution file , " << filename; |
| 1173 |
throw(s.str()); |
| 1174 |
} |
| 1175 |
for(int binX=0;binX<nbin;++binX){ |
| 1176 |
outputfile << std::endl; |
| 1177 |
for(int binY=0;binY<nbin;++binY){ |
| 1178 |
double binValueX=0; |
| 1179 |
if(nbin==maxX-minX+1) |
| 1180 |
binValueX=minX+binX; |
| 1181 |
else |
| 1182 |
binValueX=minX+static_cast<double>(maxX-minX)*(binX+0.5)/nbin; |
| 1183 |
double binValueY=0; |
| 1184 |
if(nbin==maxY-minY+1) |
| 1185 |
binValueY=minY+binY; |
| 1186 |
else |
| 1187 |
binValueY=minY+static_cast<double>(maxY-minY)*(binY+0.5)/nbin; |
| 1188 |
double value=0; |
| 1189 |
value=static_cast<double>(output[binX][binY])/npoint; |
| 1190 |
outputfile << binValueX << " " << binValueY << " " << value << std::endl; |
| 1191 |
|
| 1192 |
|
| 1193 |
} |
| 1194 |
} |
| 1195 |
outputfile.close(); |
| 1196 |
} |
| 1197 |
} |
| 1198 |
|
| 1199 |
|
| 1200 |
template<class T> void StatFactory::percentiles (const std::vector<T>& input, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end, std::vector<T>& output, int nbin, T &minimum, T &maximum, const std::string &filename) const |
| 1201 |
{ |
| 1202 |
if(maximum<=minimum) |
| 1203 |
minmax(input,begin,end,minimum,maximum); |
| 1204 |
if(maximum<=minimum){ |
| 1205 |
std::ostringstream s; |
| 1206 |
s<<"Error: maximum must be at least minimum"; |
| 1207 |
throw(s.str()); |
| 1208 |
} |
| 1209 |
if(nbin<=1){ |
| 1210 |
std::ostringstream s; |
| 1211 |
s<<"Error: nbin must be larger than 1"; |
| 1212 |
throw(s.str()); |
| 1213 |
} |
| 1214 |
if(input.empty()){ |
| 1215 |
std::ostringstream s; |
| 1216 |
s<<"Error: input is empty"; |
| 1217 |
throw(s.str()); |
| 1218 |
} |
| 1219 |
output.resize(nbin); |
| 1220 |
std::vector<T> inputSort; |
| 1221 |
inputSort.assign(begin,end); |
| 1222 |
typename std::vector<T>::iterator vit=inputSort.begin(); |
| 1223 |
while(vit!=inputSort.end()){ |
| 1224 |
if(*vit<minimum||*vit>maximum) |
| 1225 |
inputSort.erase(vit); |
| 1226 |
else |
| 1227 |
++vit; |
| 1228 |
} |
| 1229 |
eraseNoData(inputSort); |
| 1230 |
std::sort(inputSort.begin(),inputSort.end()); |
| 1231 |
vit=inputSort.begin(); |
| 1232 |
std::vector<T> inputBin; |
| 1233 |
for(int ibin=0;ibin<nbin;++ibin){ |
| 1234 |
inputBin.clear(); |
| 1235 |
while(inputBin.size()<inputSort.size()/nbin&&vit!=inputSort.end()){ |
| 1236 |
inputBin.push_back(*vit); |
| 1237 |
++vit; |
| 1238 |
} |
| 1239 |
if(inputBin.size()){ |
| 1240 |
output[ibin]=mymax(inputBin); |
| 1241 |
} |
| 1242 |
} |
| 1243 |
if(!filename.empty()){ |
| 1244 |
std::ofstream outputfile; |
| 1245 |
outputfile.open(filename.c_str()); |
| 1246 |
if(!outputfile){ |
| 1247 |
std::ostringstream s; |
| 1248 |
s<<"error opening distribution file , " << filename; |
| 1249 |
throw(s.str()); |
| 1250 |
} |
| 1251 |
for(int ibin=0;ibin<nbin;++ibin) |
| 1252 |
outputfile << ibin*100.0/nbin << " " << static_cast<double>(output[ibin])/input.size() << std::endl; |
| 1253 |
outputfile.close(); |
| 1254 |
} |
| 1255 |
} |
| 1256 |
|
| 1257 |
|
| 1258 |
template<class T> T StatFactory::percentile(const std::vector<T>& input, typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end, double percent, T minimum, T maximum) const |
| 1259 |
{ |
| 1260 |
if(input.empty()){ |
| 1261 |
std::ostringstream s; |
| 1262 |
s<<"Error: input is empty"; |
| 1263 |
throw(s.str()); |
| 1264 |
} |
| 1265 |
std::vector<double> inputSort; |
| 1266 |
inputSort.assign(begin,end); |
| 1267 |
typename std::vector<double>::iterator vit=inputSort.begin(); |
| 1268 |
while(vit!=inputSort.end()){ |
| 1269 |
if(maximum>minimum){ |
| 1270 |
if(*vit<minimum||*vit>maximum) |
| 1271 |
inputSort.erase(vit); |
| 1272 |
} |
| 1273 |
else |
| 1274 |
++vit; |
| 1275 |
} |
| 1276 |
eraseNoData(inputSort); |
| 1277 |
std::sort(inputSort.begin(),inputSort.end()); |
| 1278 |
return gsl_stats_quantile_from_sorted_data(&(inputSort[0]),1,inputSort.size(),percent/100.0); |
| 1279 |
} |
| 1280 |
|
| 1281 |
template<class T> void StatFactory::signature(const std::vector<T>& input, double&k, double& alpha, double& beta, double e) const |
| 1282 |
{ |
| 1283 |
double m1=moment(input,1); |
| 1284 |
double m2=moment(input,2); |
| 1285 |
signature(m1,m2,k,alpha,beta,e); |
| 1286 |
} |
| 1287 |
|
| 1288 |
|
| 1289 |
template<class T> void StatFactory::normalize(const std::vector<T>& input, std::vector<double>& output) const{ |
| 1290 |
double total=sum(input); |
| 1291 |
if(total){ |
| 1292 |
output.resize(input.size()); |
| 1293 |
for(int index=0;index<input.size();++index) |
| 1294 |
output[index]=input[index]/total; |
| 1295 |
} |
| 1296 |
else |
| 1297 |
output=input; |
| 1298 |
} |
| 1299 |
|
| 1300 |
|
| 1301 |
template<class T> void StatFactory::normalize_pct(std::vector<T>& input) const{ |
| 1302 |
double total=sum(input); |
| 1303 |
if(total){ |
| 1304 |
typename std::vector<T>::iterator it; |
| 1305 |
for(it=input.begin();it!=input.end();++it) |
| 1306 |
*it=100.0*(*it)/total; |
| 1307 |
} |
| 1308 |
} |
| 1309 |
|
| 1310 |
template<class T> double StatFactory::rmse(const std::vector<T>& x, const std::vector<T>& y) const{ |
| 1311 |
if(x.size()!=y.size()){ |
| 1312 |
std::ostringstream s; |
| 1313 |
s<<"Error: x and y not equal in size"; |
| 1314 |
throw(s.str()); |
| 1315 |
} |
| 1316 |
if(x.empty()){ |
| 1317 |
std::ostringstream s; |
| 1318 |
s<<"Error: x is empty"; |
| 1319 |
throw(s.str()); |
| 1320 |
} |
| 1321 |
double mse=0; |
| 1322 |
for(int isample=0;isample<x.size();++isample){ |
| 1323 |
if(isNoData(x[isample])||isNoData(y[isample])) |
| 1324 |
continue; |
| 1325 |
double e=x[isample]-y[isample]; |
| 1326 |
mse+=e*e/x.size(); |
| 1327 |
} |
| 1328 |
return sqrt(mse); |
| 1329 |
} |
| 1330 |
|
| 1331 |
|
| 1332 |
template<class T> double StatFactory::nrmse(const std::vector<T>& x, const std::vector<T>& y) const{ |
| 1333 |
if(x.size()!=y.size()){ |
| 1334 |
std::ostringstream s; |
| 1335 |
s<<"Error: x and y not equal in size"; |
| 1336 |
throw(s.str()); |
| 1337 |
} |
| 1338 |
if(x.empty()){ |
| 1339 |
std::ostringstream s; |
| 1340 |
s<<"Error: x is empty"; |
| 1341 |
throw(s.str()); |
| 1342 |
} |
| 1343 |
std::vector<T> tmpX=x; |
| 1344 |
eraseNoData(tmpX); |
| 1345 |
std::vector<T> tmpY=y; |
| 1346 |
eraseNoData(tmpY); |
| 1347 |
double maxY=mymax(y); |
| 1348 |
double minY=mymin(y); |
| 1349 |
double rangeY=maxY-minY; |
| 1350 |
double mse=0; |
| 1351 |
for(int isample=0;isample<x.size();++isample){ |
| 1352 |
double e=x[isample]-y[isample]; |
| 1353 |
mse+=e*e/x.size(); |
| 1354 |
} |
| 1355 |
return sqrt(mse)/rangeY; |
| 1356 |
} |
| 1357 |
|
| 1358 |
|
| 1359 |
template<class T> double StatFactory::cvrmse(const std::vector<T>& x, const std::vector<T>& y) const{ |
| 1360 |
if(x.size()!=y.size()){ |
| 1361 |
std::ostringstream s; |
| 1362 |
s<<"Error: x and y not equal in size"; |
| 1363 |
throw(s.str()); |
| 1364 |
} |
| 1365 |
if(x.empty()){ |
| 1366 |
std::ostringstream s; |
| 1367 |
s<<"Error: x is empty"; |
| 1368 |
throw(s.str()); |
| 1369 |
} |
| 1370 |
std::vector<T> tmpX=x; |
| 1371 |
eraseNoData(tmpX); |
| 1372 |
std::vector<T> tmpY=y; |
| 1373 |
eraseNoData(tmpY); |
| 1374 |
double maxY=mymax(tmpY); |
| 1375 |
double minY=mymin(tmpY); |
| 1376 |
double rangeY=maxY-minY; |
| 1377 |
double mse=0; |
| 1378 |
for(int isample=0;isample<x.size();++isample){ |
| 1379 |
double e=x[isample]-y[isample]; |
| 1380 |
mse+=e*e/x.size(); |
| 1381 |
} |
| 1382 |
return sqrt(mse)/mean(tmpY); |
| 1383 |
} |
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
template<class T> double StatFactory::gsl_covariance(const std::vector<T>& x, const std::vector<T>& y) const{ |
| 1390 |
return(gsl_stats_covariance(&(x[0]),1,&(y[0]),1,x.size())); |
| 1391 |
} |
| 1392 |
|
| 1393 |
|
| 1394 |
template<class T> double StatFactory::correlation(const std::vector<T>& x, const std::vector<T>& y, int delay) const{ |
| 1395 |
double meanX=0; |
| 1396 |
double meanY=0; |
| 1397 |
double varX=0; |
| 1398 |
double varY=0; |
| 1399 |
double sXY=0; |
| 1400 |
meanVar(x,meanX,varX); |
| 1401 |
meanVar(y,meanY,varY); |
| 1402 |
double denom = sqrt(varX*varY); |
| 1403 |
bool isValid=false; |
| 1404 |
if(denom){ |
| 1405 |
|
| 1406 |
sXY = 0; |
| 1407 |
for (int i=0;i<x.size();++i) { |
| 1408 |
int j = i + delay; |
| 1409 |
if (j < 0 || j >= y.size()) |
| 1410 |
continue; |
| 1411 |
else if(isNoData(x[i])||isNoData(y[j])) |
| 1412 |
continue; |
| 1413 |
else{ |
| 1414 |
isValid=true; |
| 1415 |
if(i<0){ |
| 1416 |
std::ostringstream s; |
| 1417 |
s<<"Error: i must be positive"; |
| 1418 |
throw(s.str()); |
| 1419 |
} |
| 1420 |
if(i>=x.size()){ |
| 1421 |
std::ostringstream s; |
| 1422 |
s<<"Error: i must be smaller than x.size()"; |
| 1423 |
throw(s.str()); |
| 1424 |
} |
| 1425 |
if(j<0){ |
| 1426 |
std::ostringstream s; |
| 1427 |
s<<"Error: j must be positive"; |
| 1428 |
throw(s.str()); |
| 1429 |
} |
| 1430 |
if(j>=y.size()){ |
| 1431 |
std::ostringstream s; |
| 1432 |
s<<"Error: j must be smaller than y.size()"; |
| 1433 |
throw(s.str()); |
| 1434 |
} |
| 1435 |
sXY += (x[i] - meanX) * (y[j] - meanY); |
| 1436 |
} |
| 1437 |
} |
| 1438 |
if(isValid){ |
| 1439 |
double minSize=(x.size()<y.size())?x.size():y.size(); |
| 1440 |
return(sXY / denom / (minSize-1)); |
| 1441 |
} |
| 1442 |
else if(m_noDataValues.size()) |
| 1443 |
return m_noDataValues[0]; |
| 1444 |
else{ |
| 1445 |
std::string errorString="Error: no valid data found"; |
| 1446 |
throw(errorString); |
| 1447 |
} |
| 1448 |
} |
| 1449 |
else |
| 1450 |
return 0; |
| 1451 |
} |
| 1452 |
|
| 1453 |
|
| 1454 |
template<class T> double StatFactory::cross_correlation(const std::vector<T>& x, const std::vector<T>& y, int maxdelay, std::vector<T>& z) const{ |
| 1455 |
z.clear(); |
| 1456 |
double sumCorrelation=0; |
| 1457 |
for (int delay=-maxdelay;delay<maxdelay;delay++) { |
| 1458 |
z.push_back(correlation(x,y,delay)); |
| 1459 |
sumCorrelation+=z.back(); |
| 1460 |
} |
| 1461 |
return sumCorrelation; |
| 1462 |
} |
| 1463 |
|
| 1464 |
|
| 1465 |
template<class T> double StatFactory::linear_regression(const std::vector<T>& x, const std::vector<T>& y, double &c0, double &c1) const{ |
| 1466 |
if(x.size()!=y.size()){ |
| 1467 |
std::ostringstream s; |
| 1468 |
s<<"Error: x and y not equal in size"; |
| 1469 |
throw(s.str()); |
| 1470 |
} |
| 1471 |
if(x.empty()){ |
| 1472 |
std::ostringstream s; |
| 1473 |
s<<"Error: x is empty"; |
| 1474 |
throw(s.str()); |
| 1475 |
} |
| 1476 |
double cov00; |
| 1477 |
double cov01; |
| 1478 |
double cov11; |
| 1479 |
double sumsq; |
| 1480 |
gsl_fit_linear(&(x[0]),1,&(y[0]),1,x.size(),&c0,&c1,&cov00,&cov01,&cov11,&sumsq); |
| 1481 |
return (1-sumsq/var(y)/(y.size()-1)); |
| 1482 |
} |
| 1483 |
|
| 1484 |
|
| 1485 |
template<class T> double StatFactory::linear_regression_err(const std::vector<T>& x, const std::vector<T>& y, double &c0, double &c1) const{ |
| 1486 |
if(x.size()!=y.size()){ |
| 1487 |
std::ostringstream s; |
| 1488 |
s<<"Error: x and y not equal in size"; |
| 1489 |
throw(s.str()); |
| 1490 |
} |
| 1491 |
if(x.empty()){ |
| 1492 |
std::ostringstream s; |
| 1493 |
s<<"Error: x is empty"; |
| 1494 |
throw(s.str()); |
| 1495 |
} |
| 1496 |
double cov00; |
| 1497 |
double cov01; |
| 1498 |
double cov11; |
| 1499 |
double sumsq; |
| 1500 |
gsl_fit_linear(&(x[0]),1,&(y[0]),1,x.size(),&c0,&c1,&cov00,&cov01,&cov11,&sumsq); |
| 1501 |
return sqrt((sumsq)/(y.size())); |
| 1502 |
} |
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
template<class T> void StatFactory::interpolateNoData(const std::vector<double>& wavelengthIn, const std::vector<T>& input, const std::string& type, std::vector<T>& output, bool verbose) const{ |
| 1508 |
if(wavelengthIn.empty()){ |
| 1509 |
std::ostringstream s; |
| 1510 |
s<<"Error: wavelengthIn is empty"; |
| 1511 |
throw(s.str()); |
| 1512 |
} |
| 1513 |
std::vector<double> wavelengthOut=wavelengthIn; |
| 1514 |
std::vector<T> validIn=input; |
| 1515 |
if(input.size()!=wavelengthIn.size()){ |
| 1516 |
std::ostringstream s; |
| 1517 |
s<<"Error: x and y not equal in size"; |
| 1518 |
throw(s.str()); |
| 1519 |
} |
| 1520 |
output.clear(); |
| 1521 |
|
| 1522 |
if(m_noDataValues.size()){ |
| 1523 |
typename std::vector<T>::iterator itValue=validIn.begin(); |
| 1524 |
typename std::vector<T>::iterator itWavelength=wavelengthOut.begin(); |
| 1525 |
while(itValue!=validIn.end()&&itWavelength!=wavelengthOut.end()){ |
| 1526 |
if(isNoData(*itValue)){ |
| 1527 |
validIn.erase(itValue); |
| 1528 |
wavelengthOut.erase(itWavelength); |
| 1529 |
} |
| 1530 |
else{ |
| 1531 |
++itValue; |
| 1532 |
++itWavelength; |
| 1533 |
} |
| 1534 |
} |
| 1535 |
if(validIn.size()>1){ |
| 1536 |
try{ |
| 1537 |
interpolateUp(wavelengthOut, validIn, wavelengthIn, type, output, verbose); |
| 1538 |
} |
| 1539 |
catch(...){ |
| 1540 |
output=input; |
| 1541 |
} |
| 1542 |
} |
| 1543 |
else |
| 1544 |
output=input; |
| 1545 |
} |
| 1546 |
else |
| 1547 |
output=input; |
| 1548 |
} |
| 1549 |
|
| 1550 |
template<class T> void StatFactory::interpolateUp(const std::vector<double>& wavelengthIn, const std::vector<T>& input, const std::vector<double>& wavelengthOut, const std::string& type, std::vector<T>& output, bool verbose) const{ |
| 1551 |
if(wavelengthIn.empty()){ |
| 1552 |
std::ostringstream s; |
| 1553 |
s<<"Error: wavelengthIn is empty"; |
| 1554 |
throw(s.str()); |
| 1555 |
} |
| 1556 |
if(input.size()!=wavelengthIn.size()){ |
| 1557 |
std::ostringstream s; |
| 1558 |
s<<"Error: input and wavelengthIn not equal in size"; |
| 1559 |
throw(s.str()); |
| 1560 |
} |
| 1561 |
if(wavelengthOut.empty()){ |
| 1562 |
std::ostringstream s; |
| 1563 |
s<<"Error: wavelengthOut is empty"; |
| 1564 |
throw(s.str()); |
| 1565 |
} |
| 1566 |
gsl_set_error_handler_off(); |
| 1567 |
|
| 1568 |
int nband=wavelengthIn.size(); |
| 1569 |
output.clear(); |
| 1570 |
gsl_interp_accel *acc; |
| 1571 |
allocAcc(acc); |
| 1572 |
gsl_spline *spline; |
| 1573 |
getSpline(type,nband,spline); |
| 1574 |
assert(spline); |
| 1575 |
assert(&(wavelengthIn[0])); |
| 1576 |
assert(&(input[0])); |
| 1577 |
int status=initSpline(spline,&(wavelengthIn[0]),&(input[0]),nband); |
| 1578 |
if(status){ |
| 1579 |
std::string errorString="Could not initialize spline"; |
| 1580 |
throw(errorString); |
| 1581 |
} |
| 1582 |
for(int index=0;index<wavelengthOut.size();++index){ |
| 1583 |
if(wavelengthOut[index]<*wavelengthIn.begin()){ |
| 1584 |
output.push_back(*(input.begin())); |
| 1585 |
continue; |
| 1586 |
} |
| 1587 |
else if(wavelengthOut[index]>wavelengthIn.back()){ |
| 1588 |
output.push_back(input.back()); |
| 1589 |
continue; |
| 1590 |
} |
| 1591 |
double dout=evalSpline(spline,wavelengthOut[index],acc); |
| 1592 |
output.push_back(dout); |
| 1593 |
} |
| 1594 |
gsl_spline_free(spline); |
| 1595 |
gsl_interp_accel_free(acc); |
| 1596 |
} |
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
template<class T> void StatFactory::interpolateUp(const std::vector<T>& input, std::vector<T>& output, int nbin) const |
| 1631 |
{ |
| 1632 |
if(input.empty()){ |
| 1633 |
std::ostringstream s; |
| 1634 |
s<<"Error: input is empty"; |
| 1635 |
throw(s.str()); |
| 1636 |
} |
| 1637 |
if(!nbin){ |
| 1638 |
std::ostringstream s; |
| 1639 |
s<<"Error: nbin must be larger than 0"; |
| 1640 |
throw(s.str()); |
| 1641 |
} |
| 1642 |
output.clear(); |
| 1643 |
int dim=input.size(); |
| 1644 |
for(int i=0;i<dim;++i){ |
| 1645 |
double deltaX=0; |
| 1646 |
double left=input[i]; |
| 1647 |
if(i<dim-1){ |
| 1648 |
double right=(i<dim-1)? input[i+1]:input[i]; |
| 1649 |
deltaX=(right-left)/static_cast<double>(nbin); |
| 1650 |
for(int x=0;x<nbin;++x){ |
| 1651 |
output.push_back(left+x*deltaX); |
| 1652 |
} |
| 1653 |
} |
| 1654 |
else |
| 1655 |
output.push_back(input.back()); |
| 1656 |
} |
| 1657 |
} |
| 1658 |
|
| 1659 |
|
| 1660 |
template<class T> void StatFactory::nearUp(const std::vector<T>& input, std::vector<T>& output) const |
| 1661 |
{ |
| 1662 |
if(input.empty()){ |
| 1663 |
std::ostringstream s; |
| 1664 |
s<<"Error: input is empty"; |
| 1665 |
throw(s.str()); |
| 1666 |
} |
| 1667 |
if(output.size()<input.size()){ |
| 1668 |
std::ostringstream s; |
| 1669 |
s<<"Error: output size is smaller than input size"; |
| 1670 |
throw(s.str()); |
| 1671 |
} |
| 1672 |
int dimInput=input.size(); |
| 1673 |
int dimOutput=output.size(); |
| 1674 |
|
| 1675 |
for(int iin=0;iin<dimInput;++iin){ |
| 1676 |
for(int iout=0;iout<dimOutput/dimInput;++iout){ |
| 1677 |
int indexOutput=iin*dimOutput/dimInput+iout; |
| 1678 |
if(indexOutput>=output.size()){ |
| 1679 |
std::ostringstream s; |
| 1680 |
s<<"Error: indexOutput must be smaller than output.size()"; |
| 1681 |
throw(s.str()); |
| 1682 |
} |
| 1683 |
output[indexOutput]=input[iin]; |
| 1684 |
} |
| 1685 |
} |
| 1686 |
} |
| 1687 |
|
| 1688 |
|
| 1689 |
template<class T> void StatFactory::interpolateUp(double* input, int dim, std::vector<T>& output, int nbin) |
| 1690 |
{ |
| 1691 |
if(!nbin){ |
| 1692 |
std::ostringstream s; |
| 1693 |
s<<"Error: nbin must be larger than 0"; |
| 1694 |
throw(s.str()); |
| 1695 |
} |
| 1696 |
output.clear(); |
| 1697 |
for(int i=0;i<dim;++i){ |
| 1698 |
double deltaX=0; |
| 1699 |
double left=input[i]; |
| 1700 |
if(i<dim-1){ |
| 1701 |
double right=(i<dim-1)? input[i+1]:input[i]; |
| 1702 |
deltaX=(right-left)/static_cast<double>(nbin); |
| 1703 |
for(int x=0;x<nbin;++x){ |
| 1704 |
output.push_back(left+x*deltaX); |
| 1705 |
} |
| 1706 |
} |
| 1707 |
else |
| 1708 |
output.push_back(input[dim-1]); |
| 1709 |
} |
| 1710 |
} |
| 1711 |
|
| 1712 |
|
| 1713 |
template<class T> void StatFactory::interpolateDown(const std::vector<T>& input, std::vector<T>& output, int nbin) const |
| 1714 |
{ |
| 1715 |
if(input.empty()){ |
| 1716 |
std::ostringstream s; |
| 1717 |
s<<"Error: input is empty"; |
| 1718 |
throw(s.str()); |
| 1719 |
} |
| 1720 |
if(!nbin){ |
| 1721 |
std::ostringstream s; |
| 1722 |
s<<"Error: nbin must be larger than 0"; |
| 1723 |
throw(s.str()); |
| 1724 |
} |
| 1725 |
output.clear(); |
| 1726 |
int dim=input.size(); |
| 1727 |
int x=0; |
| 1728 |
output.push_back(input[0]); |
| 1729 |
for(int i=1;i<dim;++i){ |
| 1730 |
if(i%nbin) |
| 1731 |
continue; |
| 1732 |
else{ |
| 1733 |
x=(i-1)/nbin+1; |
| 1734 |
output.push_back(input[i]); |
| 1735 |
} |
| 1736 |
} |
| 1737 |
} |
| 1738 |
|
| 1739 |
|
| 1740 |
template<class T> void StatFactory::interpolateDown(double* input, int dim, std::vector<T>& output, int nbin) |
| 1741 |
{ |
| 1742 |
if(!nbin){ |
| 1743 |
std::ostringstream s; |
| 1744 |
s<<"Error: nbin must be larger than 0"; |
| 1745 |
throw(s.str()); |
| 1746 |
} |
| 1747 |
output.clear(); |
| 1748 |
int x=0; |
| 1749 |
output.push_back(input[0]); |
| 1750 |
for(int i=1;i<dim;++i){ |
| 1751 |
if(i%nbin) |
| 1752 |
continue; |
| 1753 |
else{ |
| 1754 |
x=(i-1)/nbin+1; |
| 1755 |
output.push_back(input[i]); |
| 1756 |
} |
| 1757 |
} |
| 1758 |
} |
| 1759 |
} |
| 1760 |
|
| 1761 |
#endif |
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
|