Isis 3 Programmer Reference
HiCalUtil.h
Go to the documentation of this file.
1 #ifndef HiCalUtil_h
2 #define HiCalUtil_h
3 
26 #include <cmath>
27 #include <string>
28 #include <vector>
29 #include <iostream>
30 #include <sstream>
31 
32 #include "HiCalTypes.h"
33 #include "DbProfile.h"
34 #include "IString.h"
35 #include "Statistics.h"
36 #include "PvlKeyword.h"
37 #include "NumericalApproximation.h"
38 #include "FileName.h"
39 #include "Pvl.h"
40 #include "IException.h"
41 
42 namespace Isis {
43 
44  template <typename T> inline T MIN(const T &A, const T &B) {
45  if ( A < B ) { return (A); }
46  else { return (B); }
47  }
48 
49  template <typename T> inline T MAX(const T &A, const T &B) {
50  if ( A > B ) { return (A); }
51  else { return (B); }
52  }
53 
59 inline int ValidCount(const HiVector &v) {
60  int n = 0;
61  for (int i = 0 ; i < v.dim() ; i++) {
62  if (!IsSpecial(v[i])) n++;
63  }
64  return (n);
65 }
66 
72 inline int InValidCount(const HiVector &v) {
73  int n = 0;
74  for (int i = 0 ; i < v.dim() ; i++) {
75  if (IsSpecial(v[i])) n++;
76  }
77  return (n);
78 }
79 
85 inline int CpmmToCcd(int cpmm) {
86  const int cpmm2ccd[] = {0,1,2,3,12,4,10,11,5,13,6,7,8,9};
87  if ( (cpmm < 0) || (cpmm >= (int)(sizeof(cpmm2ccd)/sizeof(int))) ) {
88  QString mess = "CpmmToCdd: Bad CpmmNumber (" + toString(cpmm) + ")";
90  }
91  return (cpmm2ccd[cpmm]);
92 }
93 
94 
100 inline QString CcdToFilter(int ccd) {
101  if ( (ccd < 0) || (ccd > 13) ) {
102  QString mess = "CcdToFilter: Bad Ccd Number (" + toString(ccd) + ")";
103  throw IException(IException::User, mess, _FILEINFO_);
104  }
105 
106  QString filter;
107  if ( ccd <= 9 ) { filter = "RED"; }
108  else if (ccd <= 11) { filter = "IR"; }
109  else { filter = "BG"; }
110  return (filter);
111 }
112 
125 inline HiMatrix cropLines(const HiMatrix &m, int sline, int eline) {
126  int nlines(eline-sline+1);
127  HiMatrix mcrop(nlines, m.dim2());
128  for (int l = 0 ; l < nlines ; l++) {
129  for (int s = 0 ; s < m.dim2() ; s++) {
130  mcrop[l][s] = m[l+sline][s];
131  }
132  }
133  return (mcrop);
134 }
135 
148 inline HiMatrix cropSamples(const HiMatrix &m, int ssamp, int esamp) {
149  int nsamps(esamp-ssamp+1);
150  HiMatrix mcrop(m.dim1(), nsamps);
151  for (int l = 0 ; l < m.dim1() ; l++) {
152  for (int s = 0 ; s < nsamps ; s++) {
153  mcrop[l][s] = m[l][s+ssamp];
154  }
155  }
156  return (mcrop);
157 }
158 
171 inline HiVector averageLines(const HiMatrix &m, int sline = 0, int eline = -1) {
172  eline = (eline == -1) ? m.dim1() - 1 : eline;
173  HiVector v(m.dim2());
174  for (int s = 0 ; s < m.dim2() ; s++) {
175  Statistics stats;
176  for (int l = sline ; l <= eline ; l++) {
177  stats.AddData(&m[l][s], 1);
178  }
179  v[s] = stats.Average();
180  }
181  return (v);
182 }
183 
196 inline HiVector averageSamples(const HiMatrix &m, int ssamp = 0,
197  int esamp = -1) {
198  esamp = (esamp == -1) ? m.dim2() - 1 : esamp;
199  HiVector v(m.dim1());
200  for (int l = 0 ; l < m.dim1() ; l++) {
201  Statistics stats;
202  for (int s = ssamp ; s <= esamp ; s++) {
203  stats.AddData(&m[l][s], 1);
204  }
205  v[l] = stats.Average();
206  }
207  return (v);
208 }
209 
218 template <typename T>
219  T ConfKey(const DbProfile &conf, const QString &keyname, const T &defval,
220  int index = 0) {
221  if (!conf.exists(keyname)) { return (defval); }
222  if (conf.count(keyname) < index) { return (defval); }
223  QString iValue(conf.value(keyname, index));
224  T value = iValue; // This makes it work with a string?
225  return (value);
226 }
227 
236 template <typename T> int ToInteger(const T &value) {
237  return toInt(QString(value).trimmed());
238 }
239 
248 template <typename T> double ToDouble(const T &value) {
249  return toDouble(QString(value).trimmed());
250 }
251 
260 template <typename T> QString ToString(const T &value) {
261  return (toString(value).trimmed());
262 }
263 
272 inline bool IsEqual(const QString &v1, const QString &v2 = "TRUE") {
273  return (v1.toUpper() == v2.toUpper());
274 }
275 
290 inline bool IsTrueValue(const DbProfile &prof, const QString &key,
291  const QString &value = "TRUE") {
292  if ( prof.exists(key) ) {
293  return (IsEqual(prof(key), value));
294  }
295  return (false);
296 }
297 
309 inline bool SkipModule(const DbProfile &prof) {
310  return (IsTrueValue(prof, "Debug::SkipModule", "TRUE"));
311 }
312 
313 
314 inline HiMatrix appendLines(const HiMatrix &top, const HiMatrix &bottom) {
315  // Ensure same number samples
316  if (top.dim2() != bottom.dim2()) {
317  std::ostringstream mess;
318  mess << "Top buffer samples (" << top.dim2()
319  << ") do not match bottom buffer samples (" << bottom.dim2()
320  << ")";
321  throw IException(IException::User, mess.str(), _FILEINFO_);
322  }
323 
324  int nlines(top.dim1()+bottom.dim1());
325  HiMatrix mat(nlines, top.dim2());
326  for (int lt = 0 ; lt < top.dim1() ; lt++) {
327  for (int s = 0 ; s < top.dim2() ; s++) {
328  mat[lt][s] = top[lt][s];
329  }
330  }
331 
332  int topl = top.dim1();
333  for (int lb = 0 ; lb < bottom.dim1() ; lb++) {
334  for (int s = 0 ; s < top.dim2() ; s++) {
335  mat[topl+lb][s] = bottom[lb][s];
336  }
337  }
338  return (mat);
339 }
340 
341 inline HiMatrix appendSamples(const HiMatrix &left, const HiMatrix &right) {
342  // Ensure same number samples
343  if (right.dim1() != right.dim1()) {
344  std::ostringstream mess;
345  mess << "Left buffer lines (" << left.dim1()
346  << ") do not match right buffer lines (" << right.dim1()
347  << ")";
348  throw IException(IException::User, mess.str(), _FILEINFO_);
349  }
350 
351  int nsamps(left.dim2()+right.dim2());
352  HiMatrix mat(left.dim1(), nsamps);
353  for (int ll = 0 ; ll < left.dim1() ; ll++) {
354  for (int s = 0 ; s < left.dim2() ; s++) {
355  mat[ll][s] = left[ll][s];
356  }
357  }
358 
359  int lefts = left.dim2();
360  for (int lr = 0 ; lr < right.dim1() ; lr++) {
361  for (int s = 0 ; s < right.dim2() ; s++) {
362  mat[lr][lefts+s] = right[lr][s];
363  }
364  }
365  return (mat);
366 }
367 
376  public:
377  HiLineTimeEqn() : _bin(1), _ltime(1.0) { }
378  HiLineTimeEqn(int bin, double ltime) : _bin(bin), _ltime(ltime) { }
379  virtual ~HiLineTimeEqn() { }
380 
381  void setLineTime(double ltime) { _ltime = ltime; }
382  void setBin(int bin) { _bin = bin; }
383  double Time(const double line) const {
384  return (line * (_bin * _ltime * 1.0E-6));
385  }
386  double operator()(const double line) const { return (Time(line)); }
387 
388  private:
389  double _bin;
390  double _ltime;
391 };
392 
405 inline double HiTempEqn(const double temperature, const double napcm2 = 2.0,
406  const double px = 12.0) {
407  double temp = temperature + 273.0;
408  double eg = 1.1557 - (7.021E-4 * temp * temp) / (1108.0 + temp);
409  const double K = 1.38E-23;
410  const double Q = 1.6E-19;
411  return (napcm2*(px*px)*2.55E7*std::pow(temp,1.5)*std::exp(-eg*Q/2.0/K/temp));
412 }
413 
430 inline HiVector rebin(const HiVector &v, int n) {
431  if ( n == v.dim() ) { return (v); }
433  double mag((double) v.dim()/ (double) n);
434 
435  for ( int i = 0 ; i < v.dim() ; i++ ) {
436  double x((double) i);
437  if ( !IsSpecial(v[i]) ) {
438  nterp.AddData(x,v[i]);
439  }
440  }
441 
442  // Compute the spline and fill the output vector
443  HiVector vout(n);
444  for ( int j = 0 ; j < n ; j++ ) {
445  double x = (double) j * mag;
447  }
448  return (vout);
449 }
450 
459 inline void RemoveHiBlobs(Pvl &label) {
460  for ( int blob = 0 ; blob < label.objects() ; blob++ ) {
461  if ( label.object(blob).isNamed("Table") ) {
462  QString name = label.object(blob)["Name"][0];
463  if ( name.toLower() == "hirise calibration ancillary" ||
464  name.toLower() == "hirise calibration images" ||
465  name.toLower() == "hirise ancillary") {
466  label.deleteObject(blob);
467  blob--;
468  }
469  }
470  }
471  return;
472 }
473 
488 inline double GainLineStat(std::vector<double> &data) {
489 
490  // Check for special conditions
491  if (data.size() == 0) return (Null);
492  if (data.size() == 1) return (data[0]);
493 
494 #if defined(USE_AVERAGE)
495  Statistics stats;
496  stats.AddData(&data[0], data.size());
497  return (stat.Average());
498 #else
499  std::sort(data.begin(), data.end());
500  int meanIndex = data.size()/2;
501  if ((data.size() % 2) == 1) return data[meanIndex];
502  return ((data[meanIndex-1]+data[meanIndex])/2.0);
503 #endif
504 }
505 
506 }; // namespace Isis
507 #endif
PvlObject & object(const int index)
Return the object at the specified index.
Definition: PvlObject.cpp:460
const double Null
Value for an Isis Null pixel.
Definition: SpecialPixel.h:110
int CpmmToCcd(int cpmm)
Convert HiRISE Cpmm number to Ccd number.
Definition: HiCalUtil.h:85
bool SkipModule(const DbProfile &prof)
Checks profile flag to skip the current Module.
Definition: HiCalUtil.h:309
Compute HiRISE line times.
Definition: HiCalUtil.h:375
int count(const QString &key) const
Report number of values in keyword.
Definition: DbProfile.cpp:141
int objects() const
Returns the number of objects.
Definition: PvlObject.h:231
HiVector averageSamples(const HiMatrix &m, int ssamp=0, int esamp=-1)
Reduces by averaging specified samples from a buffer.
Definition: HiCalUtil.h:196
double HiTempEqn(const double temperature, const double napcm2=2.0, const double px=12.0)
Implements (classic) HiRISE temperature equation.
Definition: HiCalUtil.h:405
int InValidCount(const HiVector &v)
Counts number of invalid pixels in vector.
Definition: HiCalUtil.h:72
int toInt(const QString &string)
Global function to convert from a string to an integer.
Definition: IString.cpp:108
bool exists(const QString &key) const
Checks for the existance of a keyword.
Definition: DbProfile.h:129
void RemoveHiBlobs(Pvl &label)
Deletes HiRISE specific BLOBS from cube file.
Definition: HiCalUtil.h:459
NumericalApproximation provides various numerical analysis methods of interpolation, extrapolation and approximation of a tabulated set of x, y data.
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:226
Evaluate() returns the y-value of the nearest endpoint if a is outside of the domain.
double toDouble(const QString &string)
Global function to convert from a string to a double.
Definition: IString.cpp:164
Cubic Spline interpolation with natural boundary conditions.
A DbProfile is a container for access parameters to a database.
Definition: DbProfile.h:65
bool IsEqual(const QString &v1, const QString &v2="TRUE")
Shortened string equality test.
Definition: HiCalUtil.h:272
This class is used to accumulate statistics on double arrays.
Definition: Statistics.h:107
double Evaluate(const double a, const ExtrapType &etype=ThrowError)
Calculates interpolated or extrapolated value of tabulated data set for given domain value...
int ToInteger(const T &value)
Helper function to convert values to Integers.
Definition: HiCalUtil.h:236
bool IsTrueValue(const DbProfile &prof, const QString &key, const QString &value="TRUE")
Determines if the keyword value is the expected value.
Definition: HiCalUtil.h:290
int ValidCount(const HiVector &v)
Counts number of valid pixels in vector.
Definition: HiCalUtil.h:59
QString CcdToFilter(int ccd)
Convert HiRISE Ccd number to string filter name.
Definition: HiCalUtil.h:100
QString value(const QString &key, int nth=0) const
Returns the specified value for the given keyword.
Definition: DbProfile.cpp:160
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
A type of error that could only have occurred due to a mistake on the user&#39;s part (e...
Definition: IException.h:142
bool IsSpecial(const double d)
Returns if the input pixel is special.
Definition: SpecialPixel.h:212
HiVector rebin(const HiVector &v, int n)
Rebins a vector to a different size.
Definition: HiCalUtil.h:430
Container for cube-like labels.
Definition: Pvl.h:135
const double E
Sets some basic constants for use in ISIS programming.
Definition: Constants.h:55
double ToDouble(const T &value)
Helper function to convert values to doubles.
Definition: HiCalUtil.h:248
void deleteObject(const QString &name)
Remove an object from the current PvlObject.
Definition: PvlObject.cpp:337
HiVector averageLines(const HiMatrix &m, int sline=0, int eline=-1)
Reduces by averaging specified lines from a buffer.
Definition: HiCalUtil.h:171
bool isNamed(const QString &match) const
Returns whether the given string is equal to the container name or not.
Definition: PvlContainer.h:86
void AddData(const double x, const double y)
Add a datapoint to the set.
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
T ConfKey(const DbProfile &conf, const QString &keyname, const T &defval, int index=0)
Find a keyword in a profile using default for non-existant keywords.
Definition: HiCalUtil.h:219
HiMatrix cropSamples(const HiMatrix &m, int ssamp, int esamp)
Crop specified samples from a buffer.
Definition: HiCalUtil.h:148
double GainLineStat(std::vector< double > &data)
Return the statistics for a vector of data.
Definition: HiCalUtil.h:488
HiMatrix cropLines(const HiMatrix &m, int sline, int eline)
Crop specified lines from a buffer.
Definition: HiCalUtil.h:125
void AddData(const double *data, const unsigned int count)
Add an array of doubles to the accumulators and counters.
Definition: Statistics.cpp:154
QString ToString(const T &value)
Helper function to convert values to strings.
Definition: HiCalUtil.h:260
double Average() const
Computes and returns the average.
Definition: Statistics.cpp:313
TNT::Array2D< double > HiMatrix
2-D buffer
Definition: HiCalTypes.h:41
T MIN(const T &A, const T &B)
Implement templatized MIN fumnction.
T MAX(const T &A, const T &B)
Implement templatized MAX fumnction.
TNT::Array1D< double > HiVector
1-D Buffer
Definition: HiCalTypes.h:40