Isis Developer Reference
HiCalUtil.h
Go to the documentation of this file.
1 #ifndef HiCalUtil_h
2 #define HiCalUtil_h
3 
10 /* SPDX-License-Identifier: CC0-1.0 */
11 
12 #include <cmath>
13 #include <string>
14 #include <vector>
15 #include <iostream>
16 #include <sstream>
17 
18 #include "HiCalTypes.h"
19 #include "DbProfile.h"
20 #include "IString.h"
21 #include "Statistics.h"
22 #include "PvlKeyword.h"
23 #include "NumericalApproximation.h"
24 #include "FileName.h"
25 #include "Pvl.h"
26 #include "IException.h"
27 
28 namespace Isis {
29 
30  template <typename T> inline T MIN(const T &A, const T &B) {
31  if ( A < B ) { return (A); }
32  else { return (B); }
33  }
34 
35  template <typename T> inline T MAX(const T &A, const T &B) {
36  if ( A > B ) { return (A); }
37  else { return (B); }
38  }
39 
45 inline int ValidCount(const HiVector &v) {
46  int n = 0;
47  for (int i = 0 ; i < v.dim() ; i++) {
48  if (!IsSpecial(v[i])) n++;
49  }
50  return (n);
51 }
52 
58 inline int InValidCount(const HiVector &v) {
59  int n = 0;
60  for (int i = 0 ; i < v.dim() ; i++) {
61  if (IsSpecial(v[i])) n++;
62  }
63  return (n);
64 }
65 
71 inline int CpmmToCcd(int cpmm) {
72  const int cpmm2ccd[] = {0,1,2,3,12,4,10,11,5,13,6,7,8,9};
73  if ( (cpmm < 0) || (cpmm >= (int)(sizeof(cpmm2ccd)/sizeof(int))) ) {
74  QString mess = "CpmmToCdd: Bad CpmmNumber (" + toString(cpmm) + ")";
76  }
77  return (cpmm2ccd[cpmm]);
78 }
79 
80 
86 inline QString CcdToFilter(int ccd) {
87  if ( (ccd < 0) || (ccd > 13) ) {
88  QString mess = "CcdToFilter: Bad Ccd Number (" + toString(ccd) + ")";
90  }
91 
92  QString filter;
93  if ( ccd <= 9 ) { filter = "RED"; }
94  else if (ccd <= 11) { filter = "IR"; }
95  else { filter = "BG"; }
96  return (filter);
97 }
98 
111 inline HiMatrix cropLines(const HiMatrix &m, int sline, int eline) {
112  int nlines(eline-sline+1);
113  HiMatrix mcrop(nlines, m.dim2());
114  for (int l = 0 ; l < nlines ; l++) {
115  for (int s = 0 ; s < m.dim2() ; s++) {
116  mcrop[l][s] = m[l+sline][s];
117  }
118  }
119  return (mcrop);
120 }
121 
134 inline HiMatrix cropSamples(const HiMatrix &m, int ssamp, int esamp) {
135  int nsamps(esamp-ssamp+1);
136  HiMatrix mcrop(m.dim1(), nsamps);
137  for (int l = 0 ; l < m.dim1() ; l++) {
138  for (int s = 0 ; s < nsamps ; s++) {
139  mcrop[l][s] = m[l][s+ssamp];
140  }
141  }
142  return (mcrop);
143 }
144 
157 inline HiVector averageLines(const HiMatrix &m, int sline = 0, int eline = -1) {
158  eline = (eline == -1) ? m.dim1() - 1 : eline;
159  HiVector v(m.dim2());
160  for (int s = 0 ; s < m.dim2() ; s++) {
161  Statistics stats;
162  for (int l = sline ; l <= eline ; l++) {
163  stats.AddData(&m[l][s], 1);
164  }
165  v[s] = stats.Average();
166  }
167  return (v);
168 }
169 
182 inline HiVector averageSamples(const HiMatrix &m, int ssamp = 0,
183  int esamp = -1) {
184  esamp = (esamp == -1) ? m.dim2() - 1 : esamp;
185  HiVector v(m.dim1());
186  for (int l = 0 ; l < m.dim1() ; l++) {
187  Statistics stats;
188  for (int s = ssamp ; s <= esamp ; s++) {
189  stats.AddData(&m[l][s], 1);
190  }
191  v[l] = stats.Average();
192  }
193  return (v);
194 }
195 
204 template <typename T>
205  T ConfKey(const DbProfile &conf, const QString &keyname, const T &defval,
206  int index = 0) {
207  if (!conf.exists(keyname)) { return (defval); }
208  if (conf.count(keyname) < index) { return (defval); }
209  QString iValue(conf.value(keyname, index));
210  T value = iValue; // This makes it work with a string?
211  return (value);
212 }
213 
222 template <typename T> int ToInteger(const T &value) {
223  return toInt(QString(value).trimmed());
224 }
225 
234 template <typename T> double ToDouble(const T &value) {
235  return toDouble(QString(value).trimmed());
236 }
237 
246 template <typename T> QString ToString(const T &value) {
247  return (toString(value).trimmed());
248 }
249 
258 inline bool IsEqual(const QString &v1, const QString &v2 = "TRUE") {
259  return (v1.toUpper() == v2.toUpper());
260 }
261 
276 inline bool IsTrueValue(const DbProfile &prof, const QString &key,
277  const QString &value = "TRUE") {
278  if ( prof.exists(key) ) {
279  return (IsEqual(prof(key), value));
280  }
281  return (false);
282 }
283 
295 inline bool SkipModule(const DbProfile &prof) {
296  return (IsTrueValue(prof, "Debug::SkipModule", "TRUE"));
297 }
298 
299 
300 inline HiMatrix appendLines(const HiMatrix &top, const HiMatrix &bottom) {
301  // Ensure same number samples
302  if (top.dim2() != bottom.dim2()) {
303  std::ostringstream mess;
304  mess << "Top buffer samples (" << top.dim2()
305  << ") do not match bottom buffer samples (" << bottom.dim2()
306  << ")";
307  throw IException(IException::User, mess.str(), _FILEINFO_);
308  }
309 
310  int nlines(top.dim1()+bottom.dim1());
311  HiMatrix mat(nlines, top.dim2());
312  for (int lt = 0 ; lt < top.dim1() ; lt++) {
313  for (int s = 0 ; s < top.dim2() ; s++) {
314  mat[lt][s] = top[lt][s];
315  }
316  }
317 
318  int topl = top.dim1();
319  for (int lb = 0 ; lb < bottom.dim1() ; lb++) {
320  for (int s = 0 ; s < top.dim2() ; s++) {
321  mat[topl+lb][s] = bottom[lb][s];
322  }
323  }
324  return (mat);
325 }
326 
327 inline HiMatrix appendSamples(const HiMatrix &left, const HiMatrix &right) {
328  // Ensure same number samples
329  if (right.dim1() != right.dim1()) {
330  std::ostringstream mess;
331  mess << "Left buffer lines (" << left.dim1()
332  << ") do not match right buffer lines (" << right.dim1()
333  << ")";
334  throw IException(IException::User, mess.str(), _FILEINFO_);
335  }
336 
337  int nsamps(left.dim2()+right.dim2());
338  HiMatrix mat(left.dim1(), nsamps);
339  for (int ll = 0 ; ll < left.dim1() ; ll++) {
340  for (int s = 0 ; s < left.dim2() ; s++) {
341  mat[ll][s] = left[ll][s];
342  }
343  }
344 
345  int lefts = left.dim2();
346  for (int lr = 0 ; lr < right.dim1() ; lr++) {
347  for (int s = 0 ; s < right.dim2() ; s++) {
348  mat[lr][lefts+s] = right[lr][s];
349  }
350  }
351  return (mat);
352 }
353 
362  public:
363  HiLineTimeEqn() : _bin(1), _ltime(1.0) { }
364  HiLineTimeEqn(int bin, double ltime) : _bin(bin), _ltime(ltime) { }
365  virtual ~HiLineTimeEqn() { }
366 
367  void setLineTime(double ltime) { _ltime = ltime; }
368  void setBin(int bin) { _bin = bin; }
369  double Time(const double line) const {
370  return (line * (_bin * _ltime * 1.0E-6));
371  }
372  double operator()(const double line) const { return (Time(line)); }
373 
374  private:
375  double _bin;
376  double _ltime;
377 };
378 
391 inline double HiTempEqn(const double temperature, const double napcm2 = 2.0,
392  const double px = 12.0) {
393  double temp = temperature + 273.0;
394  double eg = 1.1557 - (7.021E-4 * temp * temp) / (1108.0 + temp);
395  const double K = 1.38E-23;
396  const double Q = 1.6E-19;
397  return (napcm2*(px*px)*2.55E7*std::pow(temp,1.5)*std::exp(-eg*Q/2.0/K/temp));
398 }
399 
416 inline HiVector rebin(const HiVector &v, int n) {
417  if ( n == v.dim() ) { return (v); }
419  double mag((double) v.dim()/ (double) n);
420 
421  for ( int i = 0 ; i < v.dim() ; i++ ) {
422  double x((double) i);
423  if ( !IsSpecial(v[i]) ) {
424  nterp.AddData(x,v[i]);
425  }
426  }
427 
428  // Compute the spline and fill the output vector
429  HiVector vout(n);
430  for ( int j = 0 ; j < n ; j++ ) {
431  double x = (double) j * mag;
433  }
434  return (vout);
435 }
436 
445 inline void RemoveHiBlobs(Pvl &label) {
446  for ( int blob = 0 ; blob < label.objects() ; blob++ ) {
447  if ( label.object(blob).isNamed("Table") ) {
448  QString name = label.object(blob)["Name"][0];
449  if ( name.toLower() == "hirise calibration ancillary" ||
450  name.toLower() == "hirise calibration images" ||
451  name.toLower() == "hirise ancillary") {
452  label.deleteObject(blob);
453  blob--;
454  }
455  }
456  }
457  return;
458 }
459 
474 inline double GainLineStat(std::vector<double> &data) {
475 
476  // Check for special conditions
477  if (data.size() == 0) return (Null);
478  if (data.size() == 1) return (data[0]);
479 
480 #if defined(USE_AVERAGE)
481  Statistics stats;
482  stats.AddData(&data[0], data.size());
483  return (stat.Average());
484 #else
485  std::sort(data.begin(), data.end());
486  int meanIndex = data.size()/2;
487  if ((data.size() % 2) == 1) return data[meanIndex];
488  return ((data[meanIndex-1]+data[meanIndex])/2.0);
489 #endif
490 }
491 
492 }; // namespace Isis
493 #endif
Isis::MAX
T MAX(const T &A, const T &B)
Implement templatized MAX fumnction.
Definition: PhotometricFunction.h:54
Isis::IsEqual
bool IsEqual(const QString &v1, const QString &v2="TRUE")
Shortened string equality test.
Definition: HiCalUtil.h:258
FileName.h
Isis::Statistics
This class is used to accumulate statistics on double arrays.
Definition: Statistics.h:94
Isis::Statistics::AddData
void AddData(const double *data, const unsigned int count)
Add an array of doubles to the accumulators and counters.
Definition: Statistics.cpp:141
Isis::ToString
QString ToString(const T &value)
Helper function to convert values to strings.
Definition: HiCalUtil.h:246
Isis::DbProfile::exists
bool exists(const QString &key) const
Checks for the existance of a keyword.
Definition: DbProfile.h:115
Isis::rebin
HiVector rebin(const HiVector &v, int n)
Rebins a vector to a different size.
Definition: HiCalUtil.h:416
Isis::HiMatrix
TNT::Array2D< double > HiMatrix
2-D buffer
Definition: HiCalTypes.h:28
Isis::HiLineTimeEqn::HiLineTimeEqn
HiLineTimeEqn(int bin, double ltime)
Definition: HiCalUtil.h:364
Isis::SkipModule
bool SkipModule(const DbProfile &prof)
Checks profile flag to skip the current Module.
Definition: HiCalUtil.h:295
Isis::HiLineTimeEqn::~HiLineTimeEqn
virtual ~HiLineTimeEqn()
Definition: HiCalUtil.h:365
Isis::ConfKey
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:205
Isis::DbProfile::count
int count(const QString &key) const
Report number of values in keyword.
Definition: DbProfile.cpp:127
Isis::Pvl
Container for cube-like labels.
Definition: Pvl.h:119
Isis::PvlObject::objects
int objects() const
Returns the number of objects.
Definition: PvlObject.h:219
NumericalApproximation.h
Isis::PvlObject::object
PvlObject & object(const int index)
Return the object at the specified index.
Definition: PvlObject.cpp:489
Isis::toString
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:211
Isis::averageLines
HiVector averageLines(const HiMatrix &m, int sline=0, int eline=-1)
Reduces by averaging specified lines from a buffer.
Definition: HiCalUtil.h:157
Isis::IsSpecial
bool IsSpecial(const double d)
Returns if the input pixel is special.
Definition: SpecialPixel.h:197
IString.h
Isis::NumericalApproximation::NearestEndpoint
@ NearestEndpoint
Evaluate() returns the y-value of the nearest endpoint if a is outside of the domain.
Definition: NumericalApproximation.h:817
Pvl.h
_FILEINFO_
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:24
Isis::averageSamples
HiVector averageSamples(const HiMatrix &m, int ssamp=0, int esamp=-1)
Reduces by averaging specified samples from a buffer.
Definition: HiCalUtil.h:182
Isis::PvlContainer::isNamed
bool isNamed(const QString &match) const
Returns whether the given string is equal to the container name or not.
Definition: PvlContainer.h:72
Isis::toInt
int toInt(const QString &string)
Global function to convert from a string to an integer.
Definition: IString.cpp:93
Isis::HiLineTimeEqn::setLineTime
void setLineTime(double ltime)
Definition: HiCalUtil.h:367
Isis::HiLineTimeEqn
Compute HiRISE line times.
Definition: HiCalUtil.h:361
Isis::ToInteger
int ToInteger(const T &value)
Helper function to convert values to Integers.
Definition: HiCalUtil.h:222
Isis::IsTrueValue
bool IsTrueValue(const DbProfile &prof, const QString &key, const QString &value="TRUE")
Determines if the keyword value is the expected value.
Definition: HiCalUtil.h:276
Isis::MIN
T MIN(const T &A, const T &B)
Implement templatized MIN fumnction.
Definition: PhotometricFunction.h:34
Isis::HiTempEqn
double HiTempEqn(const double temperature, const double napcm2=2.0, const double px=12.0)
Implements (classic) HiRISE temperature equation.
Definition: HiCalUtil.h:391
Isis::HiLineTimeEqn::operator()
double operator()(const double line) const
Definition: HiCalUtil.h:372
Isis::DbProfile
A DbProfile is a container for access parameters to a database.
Definition: DbProfile.h:51
HiCalTypes.h
Isis::HiVector
TNT::Array1D< double > HiVector
1-D Buffer
Definition: HiCalTypes.h:27
Isis::cropLines
HiMatrix cropLines(const HiMatrix &m, int sline, int eline)
Crop specified lines from a buffer.
Definition: HiCalUtil.h:111
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::InValidCount
int InValidCount(const HiVector &v)
Counts number of invalid pixels in vector.
Definition: HiCalUtil.h:58
Isis::ValidCount
int ValidCount(const HiVector &v)
Counts number of valid pixels in vector.
Definition: HiCalUtil.h:45
Isis::Null
const double Null
Value for an Isis Null pixel.
Definition: SpecialPixel.h:95
Isis::appendLines
HiMatrix appendLines(const HiMatrix &top, const HiMatrix &bottom)
Definition: HiCalUtil.h:300
Isis::appendSamples
HiMatrix appendSamples(const HiMatrix &left, const HiMatrix &right)
Definition: HiCalUtil.h:327
Isis::Statistics::Average
double Average() const
Computes and returns the average.
Definition: Statistics.cpp:300
Isis::ToDouble
double ToDouble(const T &value)
Helper function to convert values to doubles.
Definition: HiCalUtil.h:234
Statistics.h
IException.h
Isis::toDouble
double toDouble(const QString &string)
Global function to convert from a string to a double.
Definition: IString.cpp:149
Isis::CpmmToCcd
int CpmmToCcd(int cpmm)
Convert HiRISE Cpmm number to Ccd number.
Definition: HiCalUtil.h:71
Isis::PvlObject::deleteObject
void deleteObject(const QString &name)
Remove an object from the current PvlObject.
Definition: PvlObject.cpp:366
Isis::NumericalApproximation::AddData
void AddData(const double x, const double y)
Add a datapoint to the set.
Definition: NumericalApproximation.cpp:440
Isis::HiLineTimeEqn::setBin
void setBin(int bin)
Definition: HiCalUtil.h:368
Isis::NumericalApproximation::Evaluate
double Evaluate(const double a, const ExtrapType &etype=ThrowError)
Calculates interpolated or extrapolated value of tabulated data set for given domain value.
Definition: NumericalApproximation.cpp:836
PvlKeyword.h
Isis::E
const double E
Sets some basic constants for use in ISIS programming.
Definition: Constants.h:39
DbProfile.h
Isis::GainLineStat
double GainLineStat(std::vector< double > &data)
Return the statistics for a vector of data.
Definition: HiCalUtil.h:474
Isis::DbProfile::value
QString value(const QString &key, int nth=0) const
Returns the specified value for the given keyword.
Definition: DbProfile.cpp:146
Isis::cropSamples
HiMatrix cropSamples(const HiMatrix &m, int ssamp, int esamp)
Crop specified samples from a buffer.
Definition: HiCalUtil.h:134
Isis::RemoveHiBlobs
void RemoveHiBlobs(Pvl &label)
Deletes HiRISE specific BLOBS from cube file.
Definition: HiCalUtil.h:445
Isis::HiLineTimeEqn::HiLineTimeEqn
HiLineTimeEqn()
Definition: HiCalUtil.h:363
Isis::NumericalApproximation
NumericalApproximation provides various numerical analysis methods of interpolation,...
Definition: NumericalApproximation.h:726
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::NumericalApproximation::CubicNatural
@ CubicNatural
Cubic Spline interpolation with natural boundary conditions.
Definition: NumericalApproximation.h:734
Isis::IException::User
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition: IException.h:126
Isis::CcdToFilter
QString CcdToFilter(int ccd)
Convert HiRISE Ccd number to string filter name.
Definition: HiCalUtil.h:86
Isis::HiLineTimeEqn::Time
double Time(const double line) const
Definition: HiCalUtil.h:369