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"
24#include "FileName.h"
25#include "Pvl.h"
26#include "IException.h"
27
28namespace Isis {
29
35inline int ValidCount(const HiVector &v) {
36 int n = 0;
37 for (int i = 0 ; i < v.dim() ; i++) {
38 if (!IsSpecial(v[i])) n++;
39 }
40 return (n);
41}
42
48inline int InValidCount(const HiVector &v) {
49 int n = 0;
50 for (int i = 0 ; i < v.dim() ; i++) {
51 if (IsSpecial(v[i])) n++;
52 }
53 return (n);
54}
55
61inline int CpmmToCcd(int cpmm) {
62 const int cpmm2ccd[] = {0,1,2,3,12,4,10,11,5,13,6,7,8,9};
63 if ( (cpmm < 0) || (cpmm >= (int)(sizeof(cpmm2ccd)/sizeof(int))) ) {
64 QString mess = "CpmmToCdd: Bad CpmmNumber (" + toString(cpmm) + ")";
66 }
67 return (cpmm2ccd[cpmm]);
68}
69
70
76inline QString CcdToFilter(int ccd) {
77 if ( (ccd < 0) || (ccd > 13) ) {
78 QString mess = "CcdToFilter: Bad Ccd Number (" + toString(ccd) + ")";
80 }
81
82 QString filter;
83 if ( ccd <= 9 ) { filter = "RED"; }
84 else if (ccd <= 11) { filter = "IR"; }
85 else { filter = "BG"; }
86 return (filter);
87}
88
101inline HiMatrix cropLines(const HiMatrix &m, int sline, int eline) {
102 int nlines(eline-sline+1);
103 HiMatrix mcrop(nlines, m.dim2());
104 for (int l = 0 ; l < nlines ; l++) {
105 for (int s = 0 ; s < m.dim2() ; s++) {
106 mcrop[l][s] = m[l+sline][s];
107 }
108 }
109 return (mcrop);
110}
111
124inline HiMatrix cropSamples(const HiMatrix &m, int ssamp, int esamp) {
125 int nsamps(esamp-ssamp+1);
126 HiMatrix mcrop(m.dim1(), nsamps);
127 for (int l = 0 ; l < m.dim1() ; l++) {
128 for (int s = 0 ; s < nsamps ; s++) {
129 mcrop[l][s] = m[l][s+ssamp];
130 }
131 }
132 return (mcrop);
133}
134
147inline HiVector averageLines(const HiMatrix &m, int sline = 0, int eline = -1) {
148 eline = (eline == -1) ? m.dim1() - 1 : eline;
149 HiVector v(m.dim2());
150 for (int s = 0 ; s < m.dim2() ; s++) {
151 Statistics stats;
152 for (int l = sline ; l <= eline ; l++) {
153 stats.AddData(&m[l][s], 1);
154 }
155 v[s] = stats.Average();
156 }
157 return (v);
158}
159
172inline HiVector averageSamples(const HiMatrix &m, int ssamp = 0,
173 int esamp = -1) {
174 esamp = (esamp == -1) ? m.dim2() - 1 : esamp;
175 HiVector v(m.dim1());
176 for (int l = 0 ; l < m.dim1() ; l++) {
177 Statistics stats;
178 for (int s = ssamp ; s <= esamp ; s++) {
179 stats.AddData(&m[l][s], 1);
180 }
181 v[l] = stats.Average();
182 }
183 return (v);
184}
185
194template <typename T>
195 T ConfKey(const DbProfile &conf, const QString &keyname, const T &defval,
196 int index = 0) {
197 if (!conf.exists(keyname)) { return (defval); }
198 if (conf.count(keyname) < index) { return (defval); }
199 QString iValue(conf.value(keyname, index));
200 T value = iValue; // This makes it work with a string?
201 return (value);
202}
203
212template <typename T> int ToInteger(const T &value) {
213 return toInt(QString(value).trimmed());
214}
215
224template <typename T> double ToDouble(const T &value) {
225 return toDouble(QString(value).trimmed());
226}
227
236template <typename T> QString ToString(const T &value) {
237 return (toString(value).trimmed());
238}
239
248inline bool IsEqual(const QString &v1, const QString &v2 = "TRUE") {
249 return (v1.toUpper() == v2.toUpper());
250}
251
266inline bool IsTrueValue(const DbProfile &prof, const QString &key,
267 const QString &value = "TRUE") {
268 if ( prof.exists(key) ) {
269 return (IsEqual(prof(key), value));
270 }
271 return (false);
272}
273
285inline bool SkipModule(const DbProfile &prof) {
286 return (IsTrueValue(prof, "Debug::SkipModule", "TRUE"));
287}
288
289
290inline HiMatrix appendLines(const HiMatrix &top, const HiMatrix &bottom) {
291 // Ensure same number samples
292 if (top.dim2() != bottom.dim2()) {
293 std::ostringstream mess;
294 mess << "Top buffer samples (" << top.dim2()
295 << ") do not match bottom buffer samples (" << bottom.dim2()
296 << ")";
297 throw IException(IException::User, mess.str(), _FILEINFO_);
298 }
299
300 int nlines(top.dim1()+bottom.dim1());
301 HiMatrix mat(nlines, top.dim2());
302 for (int lt = 0 ; lt < top.dim1() ; lt++) {
303 for (int s = 0 ; s < top.dim2() ; s++) {
304 mat[lt][s] = top[lt][s];
305 }
306 }
307
308 int topl = top.dim1();
309 for (int lb = 0 ; lb < bottom.dim1() ; lb++) {
310 for (int s = 0 ; s < top.dim2() ; s++) {
311 mat[topl+lb][s] = bottom[lb][s];
312 }
313 }
314 return (mat);
315}
316
317inline HiMatrix appendSamples(const HiMatrix &left, const HiMatrix &right) {
318 // Ensure same number samples
319 if (right.dim1() != right.dim1()) {
320 std::ostringstream mess;
321 mess << "Left buffer lines (" << left.dim1()
322 << ") do not match right buffer lines (" << right.dim1()
323 << ")";
324 throw IException(IException::User, mess.str(), _FILEINFO_);
325 }
326
327 int nsamps(left.dim2()+right.dim2());
328 HiMatrix mat(left.dim1(), nsamps);
329 for (int ll = 0 ; ll < left.dim1() ; ll++) {
330 for (int s = 0 ; s < left.dim2() ; s++) {
331 mat[ll][s] = left[ll][s];
332 }
333 }
334
335 int lefts = left.dim2();
336 for (int lr = 0 ; lr < right.dim1() ; lr++) {
337 for (int s = 0 ; s < right.dim2() ; s++) {
338 mat[lr][lefts+s] = right[lr][s];
339 }
340 }
341 return (mat);
342}
343
352 public:
353 HiLineTimeEqn() : _bin(1), _ltime(1.0) { }
354 HiLineTimeEqn(int bin, double ltime) : _bin(bin), _ltime(ltime) { }
355 virtual ~HiLineTimeEqn() { }
356
357 void setLineTime(double ltime) { _ltime = ltime; }
358 void setBin(int bin) { _bin = bin; }
359 double Time(const double line) const {
360 return (line * (_bin * _ltime * 1.0E-6));
361 }
362 double operator()(const double line) const { return (Time(line)); }
363
364 private:
365 double _bin;
366 double _ltime;
367};
368
381inline double HiTempEqn(const double temperature, const double napcm2 = 2.0,
382 const double px = 12.0) {
383 double temp = temperature + 273.0;
384 double eg = 1.1557 - (7.021E-4 * temp * temp) / (1108.0 + temp);
385 const double K = 1.38E-23;
386 const double Q = 1.6E-19;
387 return (napcm2*(px*px)*2.55E7*std::pow(temp,1.5)*std::exp(-eg*Q/2.0/K/temp));
388}
389
406inline HiVector rebin(const HiVector &v, int n) {
407 if ( n == v.dim() ) { return (v); }
409 double mag((double) v.dim()/ (double) n);
410
411 for ( int i = 0 ; i < v.dim() ; i++ ) {
412 double x((double) i);
413 if ( !IsSpecial(v[i]) ) {
414 nterp.AddData(x,v[i]);
415 }
416 }
417
418 // Compute the spline and fill the output vector
419 HiVector vout(n);
420 for ( int j = 0 ; j < n ; j++ ) {
421 double x = (double) j * mag;
422 vout[j] = nterp.Evaluate(x, NumericalApproximation::NearestEndpoint);
423 }
424 return (vout);
425}
426
435inline void RemoveHiBlobs(Pvl &label) {
436 for ( int blob = 0 ; blob < label.objects() ; blob++ ) {
437 if ( label.object(blob).isNamed("Table") ) {
438 QString name = label.object(blob)["Name"][0];
439 if ( name.toLower() == "hirise calibration ancillary" ||
440 name.toLower() == "hirise calibration images" ||
441 name.toLower() == "hirise ancillary") {
442 label.deleteObject(blob);
443 blob--;
444 }
445 }
446 }
447 return;
448}
449
464inline double GainLineStat(std::vector<double> &data) {
465
466 // Check for special conditions
467 if (data.size() == 0) return (Null);
468 if (data.size() == 1) return (data[0]);
469
470#if defined(USE_AVERAGE)
471 Statistics stats;
472 stats.AddData(&data[0], data.size());
473 return (stat.Average());
474#else
475 std::sort(data.begin(), data.end());
476 int meanIndex = data.size()/2;
477 if ((data.size() % 2) == 1) return data[meanIndex];
478 return ((data[meanIndex-1]+data[meanIndex])/2.0);
479#endif
480}
481
482}; // namespace Isis
483#endif
#define _FILEINFO_
Macro for the filename and line number.
Definition IException.h:24
A DbProfile is a container for access parameters to a database.
Definition DbProfile.h:51
Compute HiRISE line times.
Definition HiCalUtil.h:351
HiLineTimeEqn(int bin, double ltime)
Definition HiCalUtil.h:354
double Time(const double line) const
Definition HiCalUtil.h:359
void setBin(int bin)
Definition HiCalUtil.h:358
virtual ~HiLineTimeEqn()
Definition HiCalUtil.h:355
HiLineTimeEqn()
Definition HiCalUtil.h:353
double operator()(const double line) const
Definition HiCalUtil.h:362
void setLineTime(double ltime)
Definition HiCalUtil.h:357
Isis exception class.
Definition IException.h:91
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition IException.h:126
NumericalApproximation provides various numerical analysis methods of interpolation,...
Definition NumericalApproximation.h:726
@ CubicNatural
Cubic Spline interpolation with natural boundary conditions.
Definition NumericalApproximation.h:734
@ NearestEndpoint
Evaluate() returns the y-value of the nearest endpoint if a is outside of the domain.
Definition NumericalApproximation.h:817
bool isNamed(const QString &match) const
Returns whether the given string is equal to the container name or not.
Definition PvlContainer.h:72
Container for cube-like labels.
Definition Pvl.h:119
void deleteObject(const QString &name)
Remove an object from the current PvlObject.
Definition PvlObject.cpp:372
int objects() const
Returns the number of objects.
Definition PvlObject.h:221
PvlObject & object(const int index)
Return the object at the specified index.
Definition PvlObject.cpp:495
This class is used to accumulate statistics on double arrays.
Definition Statistics.h:93
void AddData(const double *data, const unsigned int count)
Add an array of doubles to the accumulators and counters.
Definition Statistics.cpp:292
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
QString CcdToFilter(int ccd)
Convert HiRISE Ccd number to string filter name.
Definition HiCalUtil.h:76
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition IString.cpp:211
int toInt(const QString &string)
Global function to convert from a string to an integer.
Definition IString.cpp:93
double HiTempEqn(const double temperature, const double napcm2=2.0, const double px=12.0)
Implements (classic) HiRISE temperature equation.
Definition HiCalUtil.h:381
HiMatrix cropSamples(const HiMatrix &m, int ssamp, int esamp)
Crop specified samples from a buffer.
Definition HiCalUtil.h:124
HiVector rebin(const HiVector &v, int n)
Rebins a vector to a different size.
Definition HiCalUtil.h:406
int ValidCount(const HiVector &v)
Counts number of valid pixels in vector.
Definition HiCalUtil.h:35
HiMatrix appendSamples(const HiMatrix &left, const HiMatrix &right)
Definition HiCalUtil.h:317
HiMatrix appendLines(const HiMatrix &top, const HiMatrix &bottom)
Definition HiCalUtil.h:290
TNT::Array2D< double > HiMatrix
2-D buffer
Definition HiCalTypes.h:28
int InValidCount(const HiVector &v)
Counts number of invalid pixels in vector.
Definition HiCalUtil.h:48
HiVector averageSamples(const HiMatrix &m, int ssamp=0, int esamp=-1)
Reduces by averaging specified samples from a buffer.
Definition HiCalUtil.h:172
int CpmmToCcd(int cpmm)
Convert HiRISE Cpmm number to Ccd number.
Definition HiCalUtil.h:61
double GainLineStat(std::vector< double > &data)
Return the statistics for a vector of data.
Definition HiCalUtil.h:464
const double Null
Value for an Isis Null pixel.
Definition SpecialPixel.h:95
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:195
bool IsTrueValue(const DbProfile &prof, const QString &key, const QString &value="TRUE")
Determines if the keyword value is the expected value.
Definition HiCalUtil.h:266
void RemoveHiBlobs(Pvl &label)
Deletes HiRISE specific BLOBS from cube file.
Definition HiCalUtil.h:435
bool IsSpecial(const double d)
Returns if the input pixel is special.
Definition SpecialPixel.h:197
TNT::Array1D< double > HiVector
1-D Buffer
Definition HiCalTypes.h:27
QString ToString(const T &value)
Helper function to convert values to strings.
Definition HiCalUtil.h:236
HiVector averageLines(const HiMatrix &m, int sline=0, int eline=-1)
Reduces by averaging specified lines from a buffer.
Definition HiCalUtil.h:147
double ToDouble(const T &value)
Helper function to convert values to doubles.
Definition HiCalUtil.h:224
double toDouble(const QString &string)
Global function to convert from a string to a double.
Definition IString.cpp:149
HiMatrix cropLines(const HiMatrix &m, int sline, int eline)
Crop specified lines from a buffer.
Definition HiCalUtil.h:101
bool SkipModule(const DbProfile &prof)
Checks profile flag to skip the current Module.
Definition HiCalUtil.h:285
int ToInteger(const T &value)
Helper function to convert values to Integers.
Definition HiCalUtil.h:212
bool IsEqual(const QString &v1, const QString &v2="TRUE")
Shortened string equality test.
Definition HiCalUtil.h:248