USGS

Isis 3.0 Object Programmers' Reference

Home

SpectralDefinition1D.cpp
Go to the documentation of this file.
1 
24 #include <cmath>
25 #include <QList>
26 
27 #include "SpectralDefinition1D.h"
28 #include "CSVReader.h"
29 
30 namespace Isis {
31 
42  m_spectelList = NULL;
43 
44  try {
45  CSVReader csv(smileDefFilename.toString());
46 
47  if (csv.columns() != 2) {
48  QString msg = QObject::tr("Input calibration file [%1] must have 2 columns with "
49  "the format: wavelength centers, wavelength widths").
50  arg(smileDefFilename.toString());
52  }
53 
54  if (csv.rows() < 2) {
55  QString msg = QObject::tr("Input calibration file [%1] must have at least 2 lines.").
56  arg(smileDefFilename.toString());
58  }
59 
61 
62  CSVReader::CSVAxis centers = csv.getColumn(0);
63  CSVReader::CSVAxis widths = csv.getColumn(1);
64 
65  // Find wavelength change direction
66  if (centers[0] < centers[1]) {
68  } else{
69  m_ascendingWavelengths = false;
70  }
71 
72  // Determine number of sections and populate internal data storage
73  double lastWavelength = centers[0].toDouble();
74  QList<Spectel> *tempList = new QList<Spectel>;
75 
76  for (int i=0; i <csv.rows(); i++) {
77  if (i >= 2) {
78  //Do we change direction at this index?
79  if (((lastWavelength > centers[i].toDouble()) && m_ascendingWavelengths) ||
80  ((lastWavelength < centers[i].toDouble())&& !m_ascendingWavelengths)) {
81  m_spectelList->append(tempList); // We have a comeplete section, so add it.
82  tempList = new QList<Spectel>; // Create a new templist
83  }
84  }
85  Spectel temp(Isis::NULL8, Isis::NULL8, i+1, Isis::NULL8, centers[i].toDouble(),
86  widths[i].toDouble());
87  tempList->append(temp);
88  lastWavelength = centers[i].toDouble();
89  }
90 
91  m_spectelList->append(tempList);
92  m_numSections = m_spectelList->length();
93 
94  //Set # of bands (no real line, samp dimensions)
95  m_ns = 1;
96  m_nl = 1;
97  m_nb = csv.rows();
98 
99  } catch (IException &e) {
100  //delete dynamically allocated memory, since destructor won't be called
101  if (m_spectelList != NULL) {
102  delete m_spectelList;
103  }
104  QString msg = QObject::tr("Unable to open input file [%1]. Is it a valid CSV?").
105  arg(smileDefFilename.toString());
106  throw IException(e, IException::Unknown, msg, _FILEINFO_);
107  }
108  }
109 
115  QString temp;
116  for (int i=0; i<m_spectelList->length(); i++){
117  temp +="----Section ";
118  temp +=QString::number(i);
119  temp +="----\n";
120  for(int j=0; j<m_spectelList->at(i)->length(); j++){
121  temp+="Wavelength= ";
122  temp+=QString::number(m_spectelList->at(i)->at(j).centerWavelength());
123  temp+=", Width= ";
124  temp+=QString::number(m_spectelList->at(i)->at(j).filterWidth());
125  temp+="\n";
126  }
127  }
128  return temp;
129  }
130 
131 
139  return m_numSections;
140  }
141 
142 
152  int SpectralDefinition1D::sectionNumber(int s, int l, int b) const {
153  int section = 0;
154 
155  while (b > m_spectelList->at(section)->length()) {
156  b-=m_spectelList->at(section)->length();
157  section++;
158  }
159  return section;
160  }
161 
162 
165  }
166 
167 
170  delete m_spectelList;
171  }
172 
173 
188  Spectel SpectralDefinition1D::findSpectel(const int sample, const int line, const int band) const {
189  int tempBand = band;
190  if (m_numSections == 1) {
191  return m_spectelList->at(0)->at(tempBand - 1);
192  }
193  else {
194  for (int i=0; i<m_spectelList->length(); i++) {
195  if (tempBand <= m_spectelList->at(i)->length()) {
196  return m_spectelList->at(i)->at(tempBand - 1);
197  }
198  else {
199  tempBand -= m_spectelList->at(i)->length();
200  }
201  }
202  }
203  return Spectel();
204  }
205 
206 
207  // TODO: This should eventually make sure the returned spectel is within the filter width
220  Spectel SpectralDefinition1D::findSpectel(const Spectel &inSpectel, const int sectionNumber) const {
222  }
223 
224 
225  //For now, find wavelength the input wavelength is closest to! (Nearest-neighbor)
226  //Better alogrithm later? Interpolation?
241  Spectel SpectralDefinition1D::findSpectelByWavelength(double wavelength, const int sectionNumber)
242  const {
243  double diff;
244  double bestDiff = DBL_MAX;
245  double bestBand = -DBL_MAX;
246 
247  if (sectionNumber >= m_numSections) {
248  QString msg = QObject::tr("Input section number is greater than total number of sections.");
250  }
251 
252  for (int i=0; i<m_spectelList->at(sectionNumber)->length(); i++) {
253  diff = m_spectelList->at(sectionNumber)->at(i).centerWavelength() - wavelength;
254  if (std::abs(diff) < std::abs(bestDiff)) {
255  bestDiff = diff;
256  bestBand = i;
257  }
258  }
259  // TODO: QList abort if at arg is out of bounds do the necessary error check and throw if needed
260  // This should never happen!
261  if (bestBand == -DBL_MAX) {
262  return Spectel(Isis::Null, Isis::Null, Isis::Null, Isis::Null, 0.0, 0.0);
263  }
264  else {
265  return m_spectelList->at(sectionNumber)->at(bestBand);
266  }
267  }
268 }