Isis 3 Programmer Reference
TrackingTable.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 #include "TrackingTable.h"
8 
9 #include <QList>
10 #include <QString>
11 
12 #include "FileName.h"
13 #include "IException.h"
14 #include "Pvl.h"
15 #include "SpecialPixel.h"
16 #include "Table.h"
17 #include "TableField.h"
18 #include "TableRecord.h"
19 
20 using namespace std;
21 namespace Isis {
22 
23 
27  TrackingTable::TrackingTable() {
28 
29  }
30 
31 
38  TrackingTable::TrackingTable(Table table) {
39 
40  for (int i=0; i < table.Records(); i++) {
41  TableRecord record = table[i];
42  QString nameField = QString(record["FileName"]).split("/").last();
43  QString extension(FileName(nameField).extension());
44  int found = nameField.lastIndexOf(extension);
45  if (found != -1) {
46  // clear the packing characters - get only the file name
47  nameField.remove(found + 3);
48  }
49  FileName fileName(nameField);
50  QString serialNumber = QString(record["SerialNumber"]);
51  m_fileList.append(QPair<FileName, QString>(fileName, serialNumber));
52  }
53  }
54 
55 
59  TrackingTable::~TrackingTable() {
60 
61  }
62 
63 
69  Table TrackingTable::toTable() {
70 
71  // Begin by establishing the length of the fields within the table. This would be the longest
72  // length that is needed to be stored.
73  int fieldLength = 0;
74 
75  for (int i=0; i < m_fileList.size(); i++) {
76  if (m_fileList[i].first.name().length() > fieldLength) {
77  fieldLength = m_fileList[i].first.name().length();
78  }
79  if (m_fileList[i].second.length() > fieldLength) {
80  fieldLength = m_fileList[i].second.length();
81  }
82  }
83 
84  // This record is never being used. It is simply to construct the Table object.
85  TableRecord dummyRecord;
86  TableField fileNameField("FileName", TableField::Text, fieldLength);
87  TableField serialNumberField("SerialNumber", TableField::Text, fieldLength);
88  TableField indexField("PixelValue", TableField::Integer);
89  dummyRecord += fileNameField;
90  dummyRecord += serialNumberField;
91  dummyRecord += indexField;
92  Table table(trackingTableName, dummyRecord);
93 
94  // Loop through m_fileList and add records to the table with the proper information.
95  for (int i=0; i < m_fileList.size(); i++) {
96 
97  fileNameField = m_fileList[i].first.name();
98  serialNumberField = m_fileList[i].second;
99  indexField = (int) (i + VALID_MINUI4);
100 
101  TableRecord record;
102  record += fileNameField;
103  record += serialNumberField;
104  record += indexField;
105 
106  table += record;
107 
108  }
109 
110  return table;
111  }
112 
113 
120  FileName TrackingTable::pixelToFileName(unsigned int pixel) {
121  if (pixel < VALID_MINUI4) {
122  QString msg = "Cannot convert pixel [" + toString(pixel)
123  + "] to a filename, pixel is below valid minimum ["
124  + toString(VALID_MINUI4) + "].";
125  throw IException(IException::Programmer, msg, _FILEINFO_);
126  }
127 
128  unsigned int index = pixel - VALID_MINUI4;
129  if (index >= (unsigned int)m_fileList.size()) {
130  QString msg = "Cannot convert pixel [" + toString(pixel)
131  + "] to a filename, pixel is above valid maximum ["
132  + toString(VALID_MINUI4 + m_fileList.size()) + "].";
133  throw IException(IException::Programmer, msg, _FILEINFO_);
134  }
135 
136  return m_fileList[index].first;
137  }
138 
139 
150  unsigned int TrackingTable::fileNameToPixel(FileName file, QString serialNumber) {
151  for (int i = 0; i < m_fileList.size(); i++) {
152  if (QString::compare(m_fileList[i].first.toString(), file.name()) == 0) {
153  return i + VALID_MINUI4;
154  }
155  }
156 
157  // At this point, the file is not in the internal file list so append it
158  // and return its new index.
159  m_fileList.append(QPair<FileName, QString>(file, serialNumber));
160  return m_fileList.size() - 1 + VALID_MINUI4;
161  }
162 
163 
170  QString TrackingTable::pixelToSN(unsigned int pixel) {
171  if (pixel < VALID_MINUI4) {
172  QString msg = "Cannot convert pixel [" + toString(pixel)
173  + "] to a serial number, pixel is below valid minimum ["
174  + toString(VALID_MINUI4) + "].";
175  throw IException(IException::Programmer, msg, _FILEINFO_);
176  }
177 
178  unsigned int index = pixel - VALID_MINUI4;
179  if (index >= (unsigned int)m_fileList.size()) {
180  QString msg = "Cannot convert pixel [" + toString(pixel)
181  + "] to a serial number, pixel is above valid maximum ["
182  + toString(VALID_MINUI4 + m_fileList.size()) + "].";
183  throw IException(IException::Programmer, msg, _FILEINFO_);
184  }
185 
186  return m_fileList[index].second;
187  }
188 
189 
200  int TrackingTable::fileNameToIndex(FileName file, QString serialNumber) {
201  for (int i = 0; i < m_fileList.size(); i++) {
202  if (m_fileList[i].first == file) {
203  return i;
204  }
205  }
206 
207  // At this point, the file is not in the internal file list so append it
208  // and return its new index.
209  m_fileList.append(QPair<FileName, QString>(file, serialNumber));
210  return m_fileList.size() - 1;
211  }
212 }
Isis::FileName::name
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
Definition: FileName.cpp:162
Isis::FileName
File name manipulation and expansion.
Definition: FileName.h:100
Isis::TableRecord
Definition: TableRecord.h:38
Isis::toString
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:211
Isis::Table
Class for storing Table blobs information.
Definition: Table.h:61
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::TableField::name
QString name() const
Returns the name of the TableField.
Definition: TableField.cpp:97
std
Namespace for the standard library.
QPair
This is free and unencumbered software released into the public domain.
Definition: CubeIoHandler.h:23
Isis::Table::Records
int Records() const
Returns the number of records.
Definition: Table.cpp:313
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::TableField
Class for storing an Isis::Table's field information.
Definition: TableField.h:47