Isis 3 Programmer Reference
SpectralDefinition1D.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 
8 #include <cmath>
9 #include <QList>
10 #include <QObject>
11 
12 #include "SpectralDefinition1D.h"
13 #include "CSVReader.h"
14 
15 namespace Isis {
16 
27  m_spectelList = NULL;
28 
29  try {
30  CSVReader csv(smileDefFilename.toString());
31 
32  if (csv.columns() != 2) {
33  QString msg = QObject::tr("Input calibration file [%1] must have 2 columns with "
34  "the format: wavelength centers, wavelength widths").
35  arg(smileDefFilename.toString());
36  throw IException(IException::User, msg, _FILEINFO_);
37  }
38 
39  if (csv.rows() < 2) {
40  QString msg = QObject::tr("Input calibration file [%1] must have at least 2 lines.").
41  arg(smileDefFilename.toString());
42  throw IException(IException::User, msg, _FILEINFO_);
43  }
44 
46 
47  CSVReader::CSVAxis centers = csv.getColumn(0);
48  CSVReader::CSVAxis widths = csv.getColumn(1);
49 
50  // Find wavelength change direction
51  if (centers[0] < centers[1]) {
53  } else{
54  m_ascendingWavelengths = false;
55  }
56 
57  // Determine number of sections and populate internal data storage
58  double lastWavelength = centers[0].toDouble();
59  QList<Spectel> *tempList = new QList<Spectel>;
60 
61  for (int i=0; i <csv.rows(); i++) {
62  if (i >= 2) {
63  //Do we change direction at this index?
64  if (((lastWavelength > centers[i].toDouble()) && m_ascendingWavelengths) ||
65  ((lastWavelength < centers[i].toDouble())&& !m_ascendingWavelengths)) {
66  m_spectelList->append(tempList); // We have a comeplete section, so add it.
67  tempList = new QList<Spectel>; // Create a new templist
68  }
69  }
70  Spectel temp(Isis::NULL8, Isis::NULL8, i+1, Isis::NULL8, centers[i].toDouble(),
71  widths[i].toDouble());
72  tempList->append(temp);
73  lastWavelength = centers[i].toDouble();
74  }
75 
76  m_spectelList->append(tempList);
77  m_numSections = m_spectelList->length();
78 
79  //Set # of bands (no real line, samp dimensions)
80  m_ns = 1;
81  m_nl = 1;
82  m_nb = csv.rows();
83 
84  } catch (IException &e) {
85  //delete dynamically allocated memory, since destructor won't be called
86  if (m_spectelList != NULL) {
87  delete m_spectelList;
88  }
89  QString msg = QObject::tr("Unable to open input file [%1]. Is it a valid CSV?").
90  arg(smileDefFilename.toString());
91  throw IException(e, IException::Unknown, msg, _FILEINFO_);
92  }
93  }
94 
100  QString temp;
101  for (int i=0; i<m_spectelList->length(); i++){
102  temp +="----Section ";
103  temp +=QString::number(i);
104  temp +="----\n";
105  for(int j=0; j<m_spectelList->at(i)->length(); j++){
106  temp+="Wavelength= ";
107  temp+=QString::number(m_spectelList->at(i)->at(j).centerWavelength());
108  temp+=", Width= ";
109  temp+=QString::number(m_spectelList->at(i)->at(j).filterWidth());
110  temp+="\n";
111  }
112  }
113  return temp;
114  }
115 
116 
124  return m_numSections;
125  }
126 
127 
137  int SpectralDefinition1D::sectionNumber(int s, int l, int b) const {
138  int section = 0;
139 
140  while (b > m_spectelList->at(section)->length()) {
141  b-=m_spectelList->at(section)->length();
142  section++;
143  }
144  return section;
145  }
146 
147 
150  }
151 
152 
155  delete m_spectelList;
156  }
157 
158 
173  Spectel SpectralDefinition1D::findSpectel(const int sample, const int line, const int band) const {
174  int tempBand = band;
175  if (m_numSections == 1) {
176  return m_spectelList->at(0)->at(tempBand - 1);
177  }
178  else {
179  for (int i=0; i<m_spectelList->length(); i++) {
180  if (tempBand <= m_spectelList->at(i)->length()) {
181  return m_spectelList->at(i)->at(tempBand - 1);
182  }
183  else {
184  tempBand -= m_spectelList->at(i)->length();
185  }
186  }
187  }
188  return Spectel();
189  }
190 
191 
192  // TODO: This should eventually make sure the returned spectel is within the filter width
205  Spectel SpectralDefinition1D::findSpectel(const Spectel &inSpectel, const int sectionNumber) const {
207  }
208 
209 
210  //For now, find wavelength the input wavelength is closest to! (Nearest-neighbor)
211  //Better alogrithm later? Interpolation?
226  Spectel SpectralDefinition1D::findSpectelByWavelength(double wavelength, const int sectionNumber)
227  const {
228  double diff;
229  double bestDiff = DBL_MAX;
230  double bestBand = -DBL_MAX;
231 
232  if (sectionNumber >= m_numSections) {
233  QString msg = QObject::tr("Input section number is greater than total number of sections.");
234  throw IException(IException::User, msg, _FILEINFO_);
235  }
236 
237  for (int i=0; i<m_spectelList->at(sectionNumber)->length(); i++) {
238  diff = m_spectelList->at(sectionNumber)->at(i).centerWavelength() - wavelength;
239  if (std::abs(diff) < std::abs(bestDiff)) {
240  bestDiff = diff;
241  bestBand = i;
242  }
243  }
244  // TODO: QList abort if at arg is out of bounds do the necessary error check and throw if needed
245  // This should never happen!
246  if (bestBand == -DBL_MAX) {
247  return Spectel(Isis::Null, Isis::Null, Isis::Null, Isis::Null, 0.0, 0.0);
248  }
249  else {
250  return m_spectelList->at(sectionNumber)->at(bestBand);
251  }
252  }
253 }
Isis::SpectralDefinition1D::m_ascendingWavelengths
bool m_ascendingWavelengths
Do the wavelengths in a given section ascend? Used to determine sections.
Definition: SpectralDefinition1D.h:57
Isis::CSVReader::getColumn
CSVAxis getColumn(int index) const
Parse and return a column specified by index order.
Definition: CSVReader.cpp:218
Isis::CSVReader::CSVAxis
Parser::TokenList CSVAxis
Row/Column token list.
Definition: CSVReader.h:247
Isis::CSVReader::columns
int columns() const
Determine the number of columns in the input source.
Definition: CSVReader.cpp:97
Isis::Spectel
Stores information about a "Spectral pixel" or spectel.
Definition: Spectel.h:26
Isis::CSVReader
Reads strings and parses them into tokens separated by a delimiter character.
Definition: CSVReader.h:239
QList
This is free and unencumbered software released into the public domain.
Definition: BoxcarCachingAlgorithm.h:13
Isis::SpectralDefinition1D::sectionCount
virtual int sectionCount() const
Returns the number of sections in this Spectral Definition.
Definition: SpectralDefinition1D.cpp:123
Isis::FileName
File name manipulation and expansion.
Definition: FileName.h:100
Isis::IException::Unknown
@ Unknown
A type of error that cannot be classified as any of the other error types.
Definition: IException.h:118
Isis::SpectralDefinition1D::m_numSections
int m_numSections
The number of different sections of the Spectral Definition.
Definition: SpectralDefinition1D.h:55
Isis::Spectel::centerWavelength
double centerWavelength() const
Gets central wavelength of spectel.
Definition: Spectel.cpp:95
Isis::SpectralDefinition::m_nb
int m_nb
Number of bands in input Cube.
Definition: SpectralDefinition.h:60
Isis::SpectralDefinition1D::~SpectralDefinition1D
virtual ~SpectralDefinition1D()
destructor
Definition: SpectralDefinition1D.cpp:154
Isis::SpectralDefinition1D::findSpectelByWavelength
Spectel findSpectelByWavelength(double wavelength, int sectionNumber) const
Finds the Spectel with the closest center wavelength (in the given sectionNumber) to the input wavele...
Definition: SpectralDefinition1D.cpp:226
Isis::SpectralDefinition1D::toString
QString toString()
Returns the QString representation of the SpectralDefinition1D.
Definition: SpectralDefinition1D.cpp:99
Isis::SpectralDefinition1D::sectionNumber
int sectionNumber(int s, int l, int b) const
Returns the section number that a spectel is in.
Definition: SpectralDefinition1D.cpp:137
Isis::SpectralDefinition::m_nl
int m_nl
Number of lines in input Cube.
Definition: SpectralDefinition.h:57
Isis::SpectralDefinition1D::SpectralDefinition1D
SpectralDefinition1D()
construct an empty 1D SpectralDef
Definition: SpectralDefinition1D.cpp:149
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::FileName::toString
QString toString() const
Returns a QString of the full file name including the file path, excluding the attributes with any Is...
Definition: FileName.cpp:515
Isis::Null
const double Null
Value for an Isis Null pixel.
Definition: SpecialPixel.h:95
Isis::toDouble
double toDouble(const QString &string)
Global function to convert from a string to a double.
Definition: IString.cpp:149
Isis::SpectralDefinition1D::m_spectelList
QList< QList< Spectel > * > * m_spectelList
Stores each center wavelength and width.
Definition: SpectralDefinition1D.h:53
Isis::SpectralDefinition1D::findSpectel
Spectel findSpectel(const int sample, const int line, const int band) const
Get the Spectel from this SpectralDefinition at a (s,l,b).
Definition: SpectralDefinition1D.cpp:173
Isis::SpectralDefinition::m_ns
int m_ns
Number of samples in input Cube.
Definition: SpectralDefinition.h:54
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::CSVReader::rows
int rows() const
Reports the number of rows in the table.
Definition: CSVReader.h:285
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