Isis 3 Programmer Reference
SerialNumberList.cpp
1 #include "SerialNumberList.h"
2 
3 #include <QString>
4 
5 #include "IException.h"
6 #include "FileList.h"
7 #include "FileName.h"
8 #include "ObservationNumber.h"
9 #include "Progress.h"
10 #include "Pvl.h"
11 #include "SerialNumber.h"
12 
13 namespace Isis {
21  m_checkTarget = checkTarget;
22  m_target.clear();
23  }
24 
25 
40  SerialNumberList::SerialNumberList(const QString &listfile,
41  bool checkTarget,
42  Progress *progress) {
43  m_checkTarget = checkTarget;
44  m_target.clear();
45 
46  try {
47  FileList flist(listfile);
48  if (progress != NULL) {
49  progress->SetText("Creating Isis 3 serial numbers from list file.");
50  progress->SetMaximumSteps((int) flist.size() + 1);
51  progress->CheckStatus();
52  }
53  for (int i = 0; i < flist.size(); i++) {
54  add(flist[i].toString());
55  if (progress != NULL) {
56  progress->CheckStatus();
57  }
58  }
59  }
60  catch (IException &e) {
61  QString msg = "Can't open or invalid file list [" + listfile + "].";
62  throw IException(e, IException::User, msg, _FILEINFO_);
63  }
64 
65  }
66 
67 
72  }
73 
74 
82  void SerialNumberList::remove(const QString &sn) {
83  int index = serialNumberIndex(sn);
84  QString sFileName = fileName(sn);
85 
86  // Delete the reference to this serial number in the vector and the maps
87  m_pairs.erase(m_pairs.begin() + index);
88  m_serialMap.erase(sn);
89  m_fileMap.erase(sFileName);
90  }
91 
92 
115  void SerialNumberList::add(const QString &filename, bool def2filename) {
116  Pvl p(Isis::FileName(filename).expanded());
117  PvlObject cubeObj = p.findObject("IsisCube");
118 
119  try {
120 
121  // Test the target name if desired
122  if (m_checkTarget) {
123  QString target;
124  PvlGroup targetGroup;
125  if (cubeObj.hasGroup("Instrument")) {
126  targetGroup = cubeObj.findGroup("Instrument");
127  }
128  else if (def2filename) {
129  // No instrument, try Mapping
130  if (cubeObj.hasGroup("Mapping")) {
131  targetGroup = cubeObj.findGroup("Mapping");
132  }
133  else {
134  QString msg = "Unable to find Instrument or Mapping group in "
135  + filename + " for comparing target.";
137  }
138  }
139  else {
140  // No Instrument group
141  QString msg = "Unable to find Instrument group in " + filename
142  + " for comparing target.";
144  }
145 
146  target = targetGroup["TargetName"][0];
147  target = target.toUpper();
148  if (m_target.isEmpty()) {
149  m_target = target;
150  }
151  else if (m_target != target) {
152  QString msg = "Target name of [" + target + "] from file ["
153  + filename + "] does not match [" + m_target + "].";
155  }
156  }
157 
158  // Create the SN
159  QString sn = SerialNumber::Compose(p, def2filename);
160  QString on = ObservationNumber::Compose(p, def2filename);
161  if (sn == "Unknown") {
162  QString msg = "Invalid serial number [Unknown] from file ["
163  + filename + "].";
165  }
166  else if (hasSerialNumber(sn)) {
167  int index = serialNumberIndex(sn);
168  QString msg = "Duplicate serial number [" + sn + "] from files ["
169  + SerialNumberList::fileName(sn) + "] and [" + fileName(index) + "].";
171  }
172 
173  Pair nextpair;
174  nextpair.filename = Isis::FileName(filename).expanded();
175  nextpair.serialNumber = sn;
176  nextpair.observationNumber = on;
177 
178  // Need to obtain the SpacecraftName and InstrumentId from the Instrument
179  // group for use in bundle adjustment
180  if (cubeObj.hasGroup("Instrument")) {
181  PvlGroup instGroup = cubeObj.findGroup("Instrument");
182  if (instGroup.hasKeyword("SpacecraftName") && instGroup.hasKeyword("InstrumentId")) {
183  nextpair.spacecraftName = cubeObj.findGroup("Instrument")["SpacecraftName"][0];
184  nextpair.instrumentId = cubeObj.findGroup("Instrument")["InstrumentId"][0];
185  }
186  }
187 
188  m_pairs.push_back(nextpair);
189  m_serialMap.insert(std::pair<QString, int>(sn, (int)(m_pairs.size() - 1)));
190  m_fileMap.insert(std::pair<QString, int>(nextpair.filename, (int)(m_pairs.size() - 1)));
191  }
192  catch (IException &e) {
193  QString msg = "FileName [" + Isis::FileName(filename).expanded() +
194  "] can not be added to serial number list.";
195  throw IException(e, IException::User, msg, _FILEINFO_);
196  }
197 
198  }
199 
200 
211  void SerialNumberList::add(const char *serialNumber, const char *filename) {
212  add((QString)serialNumber, (QString)filename);
213  }
214 
215 
235  void SerialNumberList::add(const QString &serialNumber, const QString &filename) {
236  Pvl p(Isis::FileName(filename).expanded());
237  PvlObject cubeObj = p.findObject("IsisCube");
238 
239  try {
240 
241  // Test the target name if desired
242  if (m_checkTarget) {
243  QString target;
244  PvlGroup targetGroup;
245  if (cubeObj.hasGroup("Instrument")) {
246  targetGroup = cubeObj.findGroup("Instrument");
247  }
248  else if (cubeObj.hasGroup("Mapping")) {
249  // No instrument, try Mapping
250  targetGroup = cubeObj.findGroup("Mapping");
251  }
252  else {
253  QString msg = "Unable to find Instrument or Mapping group in "
254  + filename + " for comparing target.";
256  }
257 
258  target = targetGroup["TargetName"][0];
259  target = target.toUpper();
260  if (m_target.isEmpty()) {
261  m_target = target;
262  }
263  else if (m_target != target) {
264  QString msg = "Target name of [" + target + "] from file ["
265  + filename + "] does not match [" + m_target + "].";
267  }
268  }
269 
270  QString observationNumber = "Unknown";
271  if (serialNumber == "Unknown") {
272  QString msg = "Invalid serial number [Unknown] from file ["
273  + filename + "].";
275  }
276  else if (hasSerialNumber(serialNumber)) {
277  int index = serialNumberIndex(serialNumber);
278  QString msg = "Duplicate, serial number [" + serialNumber + "] from files ["
280  + "] and [" + fileName(index) + "].";
282  }
283 
284  // Need to obtain the SpacecraftName and InstrumentId from the Instrument
285  // group for use in bundle adjustment
286  if (!cubeObj.hasGroup("Instrument")) {
287  QString msg = "Unable to find Instrument group in " + filename
288  + " needed for performing bundle adjustment.";
290  }
291  PvlGroup instGroup = cubeObj.findGroup("Instrument");
292  if (!instGroup.hasKeyword("SpacecraftName") || !instGroup.hasKeyword("InstrumentId")) {
293  QString msg = "Unable to find SpacecraftName or InstrumentId keywords in " + filename
294  + " needed for performing bundle adjustment.";
296  }
297 
298  Pair nextpair;
299  nextpair.filename = Isis::FileName(filename).expanded();
300  nextpair.serialNumber = serialNumber;
301  nextpair.observationNumber = observationNumber;
302 
303  // Need to obtain the SpacecraftName and InstrumentId from the Instrument
304  // group for use in bundle adjustment
305  if (cubeObj.hasGroup("Instrument")) {
306  PvlGroup instGroup = cubeObj.findGroup("Instrument");
307  if (instGroup.hasKeyword("SpacecraftName") && instGroup.hasKeyword("InstrumentId")) {
308  nextpair.spacecraftName = cubeObj.findGroup("Instrument")["SpacecraftName"][0];
309  nextpair.instrumentId = cubeObj.findGroup("Instrument")["InstrumentId"][0];
310  }
311  }
312 
313  m_pairs.push_back(nextpair);
314  m_serialMap.insert(std::pair<QString, int>(serialNumber, (int)(m_pairs.size() - 1)));
315  m_fileMap.insert(std::pair<QString, int>(nextpair.filename, (int)(m_pairs.size() - 1)));
316  }
317  catch (IException &e) {
318  QString msg = "[SerialNumber, FileName] = [" + serialNumber + ", "
319  + Isis::FileName(filename).expanded()
320  + "] can not be added to serial number list.";
321  throw IException(e, IException::User, msg, _FILEINFO_);
322  }
323 
324  }
325 
326 
335  if (m_serialMap.find(sn) == m_serialMap.end()) return false;
336  return true;
337  }
338 
339 
346  return m_pairs.size();
347  }
348 
349 
360  QString SerialNumberList::fileName(const QString &sn) {
361  if (hasSerialNumber(sn)) {
362  int index = m_serialMap.find(sn)->second;
363  return m_pairs[index].filename;
364  }
365  else {
366  QString msg = "Unable to get the FileName. The given serial number ["
367  + sn + "] does not exist in the list.";
369  }
370  }
371 
372 
387  QString SerialNumberList::serialNumber(const QString &filename) {
388  if (m_fileMap.find(Isis::FileName(filename).expanded()) == m_fileMap.end()) {
389  QString msg = "Unable to get the SerialNumber. The given file name ["
390  + Isis::FileName(filename).expanded() + "] does not exist in the list.";
392  }
393  int index = fileNameIndex(filename);
394  return m_pairs[index].serialNumber;
395  }
396 
397 
407  QString SerialNumberList::serialNumber(int index) {
408  if (index >= 0 && index < (int) m_pairs.size()) {
409  return m_pairs[index].serialNumber;
410  }
411  else {
412  QString msg = "Unable to get the SerialNumber. The given index ["
413  + toString(index) + "] is invalid.";
415  }
416  }
417 
418 
430  if (index >= 0 && index < (int) m_pairs.size()) {
431  return m_pairs[index].observationNumber;
432  }
433  else {
434  QString msg = "Unable to get the ObservationNumber. The given index ["
435  + toString(index) + "] is invalid.";
437  }
438  }
439 
440 
451  int SerialNumberList::serialNumberIndex(const QString &sn) {
452  if (hasSerialNumber(sn)) {
453  return m_serialMap.find(sn)->second;
454  }
455  else {
456  QString msg = "Unable to get the SerialNumber index. The given serial number ["
457  + sn + "] does not exist in the list.";
459  }
460  }
461 
462 
478  int SerialNumberList::fileNameIndex(const QString &filename) {
479  std::map<QString, int>::iterator pos;
480  if ((pos = m_fileMap.find(Isis::FileName(filename).expanded())) == m_fileMap.end()) {
481  QString msg = "Unable to get the FileName index. The given file name ["
482  + Isis::FileName(filename).expanded() + "] does not exist in the list.";
484  }
485  return pos->second;
486  }
487 
488 
498  QString SerialNumberList::fileName(int index) {
499  if (index >= 0 && index < (int) m_pairs.size()) {
500  return m_pairs[index].filename;
501  }
502  else {
503  QString msg = "Unable to get the FileName. The given index ["
504  + toString(index) + "] is invalid.";
506  }
507  }
508 
509 
521  if (index >= 0 && index < (int) m_pairs.size()) {
522  QString scid = (m_pairs[index].spacecraftName + "/" + m_pairs[index].instrumentId).toUpper();
523 
524  // silence 'unused-result' warnings with arbitrary cast
525  (void)scid.simplified();
526  return scid.replace(" ","");
527  }
528  else {
529  QString msg = "Unable to get the Spacecraft InstrumentId. The given index ["
530  + toString(index) + "] is invalid.";
532  }
533  }
534 
535 
546  QString SerialNumberList::spacecraftInstrumentId(const QString &sn) {
547  if (hasSerialNumber(sn)) {
548  int index = m_serialMap.find(sn)->second;
549  QString scid = (m_pairs[index].spacecraftName + "/" + m_pairs[index].instrumentId).toUpper();
550 
551  // silence 'unused-result' warnings with arbitrary cast
552  (void)scid.simplified();
553  return scid.replace(" ","");
554  }
555  else {
556  QString msg = "Unable to get the Spacecraft InstrumentId. The given serial number ["
557  + sn + "] does not exist in the list.";
559  }
560  }
561 
562 
574  std::vector<QString> SerialNumberList::possibleSerialNumbers(const QString &on) {
575  std::vector<QString> numbers;
576  for (unsigned index = 0; index < m_pairs.size(); index++) {
577  if (m_pairs[index].observationNumber == on) {
578  numbers.push_back(m_pairs[index].serialNumber);
579  }
580  }
581  if (numbers.size() > 0) {
582  return numbers;
583  }
584  else {
585  QString msg = "Unable to get the possible serial numbers. The given observation number ["
586  + on + "] does not exist in the list.";
588  }
589  }
590 
591 
592 }
int serialNumberIndex(const QString &sn)
Return a list index given a serial number.
bool hasKeyword(const QString &name) const
Check to see if a keyword exists.
void SetMaximumSteps(const int steps)
This sets the maximum number of steps in the process.
Definition: Progress.cpp:101
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition: PvlObject.h:141
bool hasSerialNumber(QString sn)
Determines whether or not the requested serial number exists in the list.
virtual ~SerialNumberList()
Destructor.
File name manipulation and expansion.
Definition: FileName.h:116
void add(const QString &filename, bool def2filename=false)
Adds a new filename / serial number pair to the SerialNumberList.
PvlObjectIterator findObject(const QString &name, PvlObjectIterator beg, PvlObjectIterator end)
Find the index of object with a specified name, between two indexes.
Definition: PvlObject.h:286
QString serialNumber(const QString &filename)
Return a serial number given a filename.
Internalizes a list of files.
Definition: FileList.h:70
bool hasGroup(const QString &name) const
Returns a boolean value based on whether the object has the specified group or not.
Definition: PvlObject.h:222
QString spacecraftInstrumentId(int index)
Return the spacecraftname/instrumentid at the given index.
std::vector< QString > possibleSerialNumbers(const QString &on)
Return possible serial numbers given an observation number.
std::map< QString, int > m_fileMap
Maps filenames to their positions in the list.
int fileNameIndex(const QString &filename)
Return a list index given a filename.
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:226
bool m_checkTarget
Specifies whether or not to check to make sure the target names match between files added to the seri...
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:162
void CheckStatus()
Checks and updates the status.
Definition: Progress.cpp:121
static QString Compose(Pvl &label, bool def2filename=false)
Compose a SerialNumber from a PVL.
std::map< QString, int > m_serialMap
Maps serial numbers to their positions in the list.
QString fileName(const QString &sn)
Return a filename given a serial number.
A serial number list entity that contains the filename serial number pair.
std::vector< Pair > m_pairs
List of serial number Pair entities.
Program progress reporter.
Definition: Progress.h:58
static QString Compose(Pvl &label, bool def2filename=false)
Compose a ObservationNumber from a PVL.
void SetText(const QString &text)
Changes the value of the text string reported just before 0% processed.
Definition: Progress.cpp:77
Contains multiple PvlContainers.
Definition: PvlGroup.h:57
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
QString m_target
Target name that the files must have if m_checkTarget is true.
A type of error that could only have occurred due to a mistake on the user&#39;s part (e...
Definition: IException.h:142
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:212
void remove(const QString &sn)
Remove the specified serial number from the list.
Container for cube-like labels.
Definition: Pvl.h:135
int size() const
How many serial number / filename combos are in the list.
QString observationNumber(int index)
Return a observation number given an index.
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
SerialNumberList(bool checkTarget=true)
Creates an empty SerialNumberList.
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:74