Isis 3 Programmer Reference
SerialNumber.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 #include <map>
8 
9 #include "SerialNumber.h"
10 #include "ObservationNumber.h"
11 #include "SerialNumberList.h"
12 #include "IException.h"
13 #include "Pvl.h"
14 #include "Cube.h"
15 #include "Process.h"
16 #include "PvlToPvlTranslationManager.h"
17 #include "FileName.h"
18 
19 namespace Isis {
24 
29 
38  QString SerialNumber::Compose(Pvl &label, bool def2filename) {
39 
40  QString sn;
41  try {
42  PvlGroup snGroup = FindSerialTranslation(label);
43  sn = CreateSerialNumber(snGroup, (int)snGroup["ObservationKeys"]);
44  }
45  catch(IException &) {
46  if(def2filename) {
47  // Try to return the filename if it exists in the label, otherwise use
48  // "Unknown" as a last resort.
49  QString snTemp = label.fileName();
50  if(!snTemp.isEmpty()) {
51  sn = FileName(snTemp).name();
52  }
53  else {
54  sn = "Unknown";
55  }
56  }
57  else {
58  sn = "Unknown";
59  }
60  }
61 
62  return sn;
63  }
64 
73  QString SerialNumber::Compose(Cube &cube, bool def2filename) {
74  return Compose(*cube.label(), def2filename);
75  }
76 
86  QString SerialNumber::Compose(const QString &filename, bool def2filename) {
87  Pvl p(filename);
88  return Compose(p, def2filename);
89  }
90 
98  Pvl outLabel;
99 
100  // check if label has CSM information
101  if(label.findObject("IsisCube").hasGroup("CsmInfo")) {
102  static QString csmTransFile = "$ISISROOT/appdata/translations/CsmSerialNumber.trn";
103  PvlToPvlTranslationManager csmTranslator(label, csmTransFile);
104  csmTranslator.Auto(outLabel);
105  }
106  else {
107  // Get the mission name
108  static QString missionTransFile = "$ISISROOT/appdata/translations/MissionName2DataDir.trn";
109  static PvlToPvlTranslationManager missionXlater(missionTransFile);
110  missionXlater.SetLabel(label);
111  QString mission = missionXlater.Translate("MissionName");
112 
113  // Get the instrument name
114  static QString instTransFile = "$ISISROOT/appdata/translations/Instruments.trn";
115  static PvlToPvlTranslationManager instrumentXlater(instTransFile);
116  instrumentXlater.SetLabel(label);
117  QString instrument = instrumentXlater.Translate("InstrumentName");
118 
119  // We want to use this instrument's translation manager. It's much faster for
120  // SerialNumberList if we keep the translation manager in memory, so re-reading
121  // from the disk is not necessary every time. To do this, we'll use a map to store
122  // the translation managers with a string identifier to find them. This identifier
123  // needs to have the mission name and the instrument name.
124 
125  // Create the static map to keep the translation managers in memory
126  static std::map<QString, PvlToPvlTranslationManager> missionTranslators;
127 
128  // Determine the key for this translation manager - must have both mission and instrument
129  QString key = mission + "_" + instrument;
130 
131  // Try to find an existing translation manager with the key
132  std::map<QString, PvlToPvlTranslationManager>::iterator translationIterator = missionTranslators.find(key);
133 
134  // If we don't succeed, create one
135  if(translationIterator == missionTranslators.end()) {
136  // Get the file
137 
138  FileName snFile((QString) "$ISISROOT/appdata/translations/" + mission + instrument + "SerialNumber.trn");
139 
140  // use the translation file to generate keywords
141  missionTranslators.insert(
142  std::pair<QString, PvlToPvlTranslationManager>(key, PvlToPvlTranslationManager(snFile.expanded()))
143  );
144 
145  translationIterator = missionTranslators.find(key);
146  }
147  translationIterator->second.SetLabel(label);
148  translationIterator->second.Auto(outLabel);
149  }
150 
151  PvlGroup snGroup = outLabel.findGroup("SerialNumberKeywords");
152  snGroup += PvlKeyword("ObservationKeys", toString(snGroup.keywords()));
153 
154  return snGroup;
155  }
156 
164  QString SerialNumber::CreateSerialNumber(PvlGroup &snGroup, int keys) {
165  QString sn = snGroup["Keyword1"][0];
166  for(int i = 2; i <= keys; i++) {
167  QString keyword = QString("Keyword%1").arg(i);
168  sn += "/" + snGroup[keyword][0];
169  }
170  return sn;
171  }
172 
185  QString SerialNumber::ComposeObservation(const QString &sn, SerialNumberList &list, bool def2filename) {
186  QString filename = list.fileName(sn);
187  return ObservationNumber::Compose(filename, def2filename);
188  }
189 }
Isis::PvlObject::findGroup
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition: PvlObject.h:129
Isis::PvlKeyword
A single keyword-value pair.
Definition: PvlKeyword.h:82
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::PvlToPvlTranslationManager::Translate
virtual QString Translate(QString translationGroupName, int findex=0)
Returns a translated value.
Definition: PvlToPvlTranslationManager.cpp:107
Isis::SerialNumber::~SerialNumber
virtual ~SerialNumber()
Destroy a SerialNumber object.
Definition: SerialNumber.cpp:28
Isis::SerialNumber::Compose
static QString Compose(Pvl &label, bool def2filename=false)
Compose a SerialNumber from a PVL.
Definition: SerialNumber.cpp:38
Isis::Pvl
Container for cube-like labels.
Definition: Pvl.h:119
Isis::toString
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:211
Isis::SerialNumberList
Serial Number list generator.
Definition: SerialNumberList.h:64
Isis::PvlToPvlTranslationManager
Allows applications to translate simple text files.
Definition: PvlToPvlTranslationManager.h:65
Isis::FileName::expanded
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:196
Isis::PvlGroup
Contains multiple PvlContainers.
Definition: PvlGroup.h:41
Isis::SerialNumber::SerialNumber
SerialNumber()
Create an empty SerialNumber object.
Definition: SerialNumber.cpp:23
Isis::PvlObject::findObject
PvlObjectIterator findObject(const QString &name, PvlObjectIterator beg, PvlObjectIterator end)
Find the index of object with a specified name, between two indexes.
Definition: PvlObject.h:274
Isis::SerialNumber::ComposeObservation
static QString ComposeObservation(const QString &sn, SerialNumberList &list, bool def2filename=false)
Creates the ObservationNumber from a string representing the SerialNumber and a SerialList.
Definition: SerialNumber.cpp:185
Isis::PvlContainer::fileName
QString fileName() const
Returns the filename used to initialise the Pvl object.
Definition: PvlContainer.h:232
Isis::ObservationNumber::Compose
static QString Compose(Pvl &label, bool def2filename=false)
Compose a ObservationNumber from a PVL.
Definition: ObservationNumber.cpp:31
Isis::Cube
IO Handler for Isis Cubes.
Definition: Cube.h:167
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::Cube::label
Pvl * label() const
Returns a pointer to the IsisLabel object associated with the cube.
Definition: Cube.cpp:1701
Isis::PvlContainer::keywords
int keywords() const
Returns the number of keywords contained in the PvlContainer.
Definition: PvlContainer.h:86
Isis::SerialNumberList::fileName
QString fileName(const QString &sn)
Return a filename given a serial number.
Definition: SerialNumberList.cpp:399
Isis::SerialNumber::CreateSerialNumber
static QString CreateSerialNumber(PvlGroup &snGroup, int key)
Create the SerialNumber string by concatenating the keywords in the label with '/' in between serialN...
Definition: SerialNumber.cpp:164
Isis::PvlToPvlTranslationManager::Auto
void Auto(Pvl &outputLabel)
Automatically translate all the output names found in the translation table If a output name does not...
Definition: PvlToPvlTranslationManager.cpp:199
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::SerialNumber::FindSerialTranslation
static PvlGroup FindSerialTranslation(Pvl &label)
Get Groups by translating from correct Translation table.
Definition: SerialNumber.cpp:97