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
15namespace 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
45 m_spectelList = new QList< QList<Spectel> *>;
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{
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
151
152
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 {
206 return findSpectelByWavelength(inSpectel.centerWavelength(), sectionNumber);
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
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) {
248 }
249 else {
250 return m_spectelList->at(sectionNumber)->at(bestBand);
251 }
252 }
253}
Reads strings and parses them into tokens separated by a delimiter character.
Definition CSVReader.h:239
Parser::TokenList CSVAxis
Row/Column token list.
Definition CSVReader.h:247
File name manipulation and expansion.
Definition FileName.h:100
Isis exception class.
Definition IException.h:91
@ Unknown
A type of error that cannot be classified as any of the other error types.
Definition IException.h:118
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition IException.h:126
Stores information about a "Spectral pixel" or spectel.
Definition Spectel.h:26
SpectralDefinition1D()
construct an empty 1D SpectralDef
virtual ~SpectralDefinition1D()
destructor
Spectel findSpectel(const int sample, const int line, const int band) const
Get the Spectel from this SpectralDefinition at a (s,l,b).
Spectel findSpectelByWavelength(double wavelength, int sectionNumber) const
Finds the Spectel with the closest center wavelength (in the given sectionNumber) to the input wavele...
virtual int sectionCount() const
Returns the number of sections in this Spectral Definition.
QList< QList< Spectel > * > * m_spectelList
Stores each center wavelength and width.
int sectionNumber(int s, int l, int b) const
Returns the section number that a spectel is in.
QString toString()
Returns the QString representation of the SpectralDefinition1D.
bool m_ascendingWavelengths
Do the wavelengths in a given section ascend? Used to determine sections.
int m_numSections
The number of different sections of the Spectral Definition.
int m_nl
Number of lines in input Cube.
int m_ns
Number of samples in input Cube.
int m_nb
Number of bands in input Cube.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
const double Null
Value for an Isis Null pixel.
double toDouble(const QString &string)
Global function to convert from a string to a double.
Definition IString.cpp:149