Isis 3 Programmer Reference
SpectralDefinition1D.cpp
Go to the documentation of this file.
1 
24 #include <cmath>
25 #include <QList>
26 #include <QObject>
27 
28 #include "SpectralDefinition1D.h"
29 #include "CSVReader.h"
30 
31 namespace Isis {
32 
43  m_spectelList = NULL;
44 
45  try {
46  CSVReader csv(smileDefFilename.toString());
47 
48  if (csv.columns() != 2) {
49  QString msg = QObject::tr("Input calibration file [%1] must have 2 columns with "
50  "the format: wavelength centers, wavelength widths").
51  arg(smileDefFilename.toString());
53  }
54 
55  if (csv.rows() < 2) {
56  QString msg = QObject::tr("Input calibration file [%1] must have at least 2 lines.").
57  arg(smileDefFilename.toString());
59  }
60 
62 
63  CSVReader::CSVAxis centers = csv.getColumn(0);
64  CSVReader::CSVAxis widths = csv.getColumn(1);
65 
66  // Find wavelength change direction
67  if (centers[0] < centers[1]) {
69  } else{
70  m_ascendingWavelengths = false;
71  }
72 
73  // Determine number of sections and populate internal data storage
74  double lastWavelength = centers[0].toDouble();
75  QList<Spectel> *tempList = new QList<Spectel>;
76 
77  for (int i=0; i <csv.rows(); i++) {
78  if (i >= 2) {
79  //Do we change direction at this index?
80  if (((lastWavelength > centers[i].toDouble()) && m_ascendingWavelengths) ||
81  ((lastWavelength < centers[i].toDouble())&& !m_ascendingWavelengths)) {
82  m_spectelList->append(tempList); // We have a comeplete section, so add it.
83  tempList = new QList<Spectel>; // Create a new templist
84  }
85  }
86  Spectel temp(Isis::NULL8, Isis::NULL8, i+1, Isis::NULL8, centers[i].toDouble(),
87  widths[i].toDouble());
88  tempList->append(temp);
89  lastWavelength = centers[i].toDouble();
90  }
91 
92  m_spectelList->append(tempList);
93  m_numSections = m_spectelList->length();
94 
95  //Set # of bands (no real line, samp dimensions)
96  m_ns = 1;
97  m_nl = 1;
98  m_nb = csv.rows();
99 
100  } catch (IException &e) {
101  //delete dynamically allocated memory, since destructor won't be called
102  if (m_spectelList != NULL) {
103  delete m_spectelList;
104  }
105  QString msg = QObject::tr("Unable to open input file [%1]. Is it a valid CSV?").
106  arg(smileDefFilename.toString());
107  throw IException(e, IException::Unknown, msg, _FILEINFO_);
108  }
109  }
110 
116  QString temp;
117  for (int i=0; i<m_spectelList->length(); i++){
118  temp +="----Section ";
119  temp +=QString::number(i);
120  temp +="----\n";
121  for(int j=0; j<m_spectelList->at(i)->length(); j++){
122  temp+="Wavelength= ";
123  temp+=QString::number(m_spectelList->at(i)->at(j).centerWavelength());
124  temp+=", Width= ";
125  temp+=QString::number(m_spectelList->at(i)->at(j).filterWidth());
126  temp+="\n";
127  }
128  }
129  return temp;
130  }
131 
132 
140  return m_numSections;
141  }
142 
143 
153  int SpectralDefinition1D::sectionNumber(int s, int l, int b) const {
154  int section = 0;
155 
156  while (b > m_spectelList->at(section)->length()) {
157  b-=m_spectelList->at(section)->length();
158  section++;
159  }
160  return section;
161  }
162 
163 
166  }
167 
168 
171  delete m_spectelList;
172  }
173 
174 
189  Spectel SpectralDefinition1D::findSpectel(const int sample, const int line, const int band) const {
190  int tempBand = band;
191  if (m_numSections == 1) {
192  return m_spectelList->at(0)->at(tempBand - 1);
193  }
194  else {
195  for (int i=0; i<m_spectelList->length(); i++) {
196  if (tempBand <= m_spectelList->at(i)->length()) {
197  return m_spectelList->at(i)->at(tempBand - 1);
198  }
199  else {
200  tempBand -= m_spectelList->at(i)->length();
201  }
202  }
203  }
204  return Spectel();
205  }
206 
207 
208  // TODO: This should eventually make sure the returned spectel is within the filter width
221  Spectel SpectralDefinition1D::findSpectel(const Spectel &inSpectel, const int sectionNumber) const {
223  }
224 
225 
226  //For now, find wavelength the input wavelength is closest to! (Nearest-neighbor)
227  //Better alogrithm later? Interpolation?
242  Spectel SpectralDefinition1D::findSpectelByWavelength(double wavelength, const int sectionNumber)
243  const {
244  double diff;
245  double bestDiff = DBL_MAX;
246  double bestBand = -DBL_MAX;
247 
248  if (sectionNumber >= m_numSections) {
249  QString msg = QObject::tr("Input section number is greater than total number of sections.");
251  }
252 
253  for (int i=0; i<m_spectelList->at(sectionNumber)->length(); i++) {
254  diff = m_spectelList->at(sectionNumber)->at(i).centerWavelength() - wavelength;
255  if (std::abs(diff) < std::abs(bestDiff)) {
256  bestDiff = diff;
257  bestBand = i;
258  }
259  }
260  // TODO: QList abort if at arg is out of bounds do the necessary error check and throw if needed
261  // This should never happen!
262  if (bestBand == -DBL_MAX) {
263  return Spectel(Isis::Null, Isis::Null, Isis::Null, Isis::Null, 0.0, 0.0);
264  }
265  else {
266  return m_spectelList->at(sectionNumber)->at(bestBand);
267  }
268  }
269 }
const double Null
Value for an Isis Null pixel.
Definition: SpecialPixel.h:110
File name manipulation and expansion.
Definition: FileName.h:116
double centerWavelength() const
Gets central wavelength of spectel.
Definition: Spectel.cpp:111
QString toString()
Returns the QString representation of the SpectralDefinition1D.
bool m_ascendingWavelengths
Do the wavelengths in a given section ascend? Used to determine sections.
double toDouble(const QString &string)
Global function to convert from a string to a double.
Definition: IString.cpp:164
#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
A type of error that cannot be classified as any of the other error types.
Definition: IException.h:134
Parser::TokenList CSVAxis
Row/Column token list.
Definition: CSVReader.h:263
Reads strings and parses them into tokens separated by a delimiter character.
Definition: CSVReader.h:255
QList< QList< Spectel > * > * m_spectelList
Stores each center wavelength and width.
int m_nl
Number of lines in input Cube.
int m_nb
Number of bands in input Cube.
QString toString() const
Returns a QString of the full file name including the file path, excluding the attributes with any Is...
Definition: FileName.cpp:531
int m_ns
Number of samples in input Cube.
Spectel findSpectelByWavelength(double wavelength, int sectionNumber) const
Finds the Spectel with the closest center wavelength (in the given sectionNumber) to the input wavele...
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
virtual int sectionCount() const
Returns the number of sections in this Spectral Definition.
virtual ~SpectralDefinition1D()
destructor
Stores information about a "Spectral pixel" or spectel.
Definition: Spectel.h:43
int m_numSections
The number of different sections of the Spectral Definition.
int sectionNumber(int s, int l, int b) const
Returns the section number that a spectel is in.
SpectralDefinition1D()
construct an empty 1D SpectralDef
Spectel findSpectel(const int sample, const int line, const int band) const
Get the Spectel from this SpectralDefinition at a (s,l,b).