Isis 3 Programmer Reference
SerialNumber.cpp
1 #include <map>
2 
3 #include "SerialNumber.h"
4 #include "ObservationNumber.h"
5 #include "SerialNumberList.h"
6 #include "IException.h"
7 #include "Pvl.h"
8 #include "Cube.h"
9 #include "Process.h"
11 #include "FileName.h"
12 
13 namespace Isis {
18 
23 
32  QString SerialNumber::Compose(Pvl &label, bool def2filename) {
33 
34  QString sn;
35  try {
36  PvlGroup snGroup = FindSerialTranslation(label);
37  sn = CreateSerialNumber(snGroup, (int)snGroup["ObservationKeys"]);
38  }
39  catch(IException &) {
40  if(def2filename) {
41  // Try to return the filename if it exists in the label, otherwise use
42  // "Unknown" as a last resort.
43  QString snTemp = label.fileName();
44  if(!snTemp.isEmpty()) {
45  sn = FileName(snTemp).name();
46  }
47  else {
48  sn = "Unknown";
49  }
50  }
51  else {
52  sn = "Unknown";
53  }
54  }
55 
56  return sn;
57  }
58 
67  QString SerialNumber::Compose(Cube &cube, bool def2filename) {
68  return Compose(*cube.label(), def2filename);
69  }
70 
80  QString SerialNumber::Compose(const QString &filename, bool def2filename) {
81  Pvl p(filename);
82  return Compose(p, def2filename);
83  }
84 
92  Pvl outLabel;
93  static PvlGroup dataDir(Preference::Preferences().findGroup("DataDirectory"));
94 
95  // Get the mission name
96  static QString missionTransFile = (QString) dataDir["base"] + "/translations/MissionName2DataDir.trn";
97  static PvlToPvlTranslationManager missionXlater(missionTransFile);
98  missionXlater.SetLabel(label);
99  QString mission = missionXlater.Translate("MissionName");
100 
101  // Get the instrument name
102  static QString instTransFile = (QString) dataDir["base"] + "/translations/Instruments.trn";
103  static PvlToPvlTranslationManager instrumentXlater(instTransFile);
104  instrumentXlater.SetLabel(label);
105  QString instrument = instrumentXlater.Translate("InstrumentName");
106 
107  // We want to use this instrument's translation manager. It's much faster for
108  // SerialNumberList if we keep the translation manager in memory, so re-reading
109  // from the disk is not necessary every time. To do this, we'll use a map to store
110  // the translation managers with a string identifier to find them. This identifier
111  // needs to have the mission name and the instrument name.
112 
113  // Create the static map to keep the translation managers in memory
114  static std::map<QString, PvlToPvlTranslationManager> missionTranslators;
115 
116  // Determine the key for this translation manager - must have both mission and instrument
117  QString key = mission + "_" + instrument;
118 
119  // Try to find an existing translation manager with the key
120  std::map<QString, PvlToPvlTranslationManager>::iterator translationIterator = missionTranslators.find(key);
121 
122  // If we don't succeed, create one
123  if(translationIterator == missionTranslators.end()) {
124  // Get the file
125  FileName snFile((QString) dataDir[mission] + "/translations/" +
126  instrument + "SerialNumber????.trn");
127  snFile = snFile.highestVersion();
128 
129  // use the translation file to generate keywords
130  missionTranslators.insert(
131  std::pair<QString, PvlToPvlTranslationManager>(key, PvlToPvlTranslationManager(snFile.expanded()))
132  );
133 
134  translationIterator = missionTranslators.find(key);
135  }
136 
137  translationIterator->second.SetLabel(label);
138  translationIterator->second.Auto(outLabel);
139 
140  PvlGroup snGroup = outLabel.findGroup("SerialNumberKeywords");
141  snGroup += PvlKeyword("ObservationKeys", toString(snGroup.keywords()));
142 
143  return snGroup;
144  }
145 
153  QString SerialNumber::CreateSerialNumber(PvlGroup &snGroup, int keys) {
154  QString sn = snGroup["Keyword1"][0];
155  for(int i = 2; i <= keys; i++) {
156  QString keyword = QString("Keyword%1").arg(i);
157  sn += "/" + snGroup[keyword][0];
158  }
159  return sn;
160  }
161 
174  QString SerialNumber::ComposeObservation(const QString &sn, SerialNumberList &list, bool def2filename) {
175  QString filename = list.fileName(sn);
176  return ObservationNumber::Compose(filename, def2filename);
177  }
178 }
int keywords() const
Returns the number of keywords contained in the PvlContainer.
Definition: PvlContainer.h:100
static QString CreateSerialNumber(PvlGroup &snGroup, int key)
Create the SerialNumber string by concatenating the keywords in the label with &#39;/&#39; in between serialN...
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition: PvlObject.h:141
File name manipulation and expansion.
Definition: FileName.h:116
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
Definition: FileName.cpp:178
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:226
Allows applications to translate simple text files.
virtual ~SerialNumber()
Destroy a SerialNumber object.
static QString Compose(Pvl &label, bool def2filename=false)
Compose a SerialNumber from a PVL.
QString fileName(const QString &sn)
Return a filename given a serial number.
static QString Compose(Pvl &label, bool def2filename=false)
Compose a ObservationNumber from a PVL.
Contains multiple PvlContainers.
Definition: PvlGroup.h:57
A single keyword-value pair.
Definition: PvlKeyword.h:98
SerialNumber()
Create an empty SerialNumber object.
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:212
virtual QString Translate(QString translationGroupName, int findex=0)
Returns a translated value.
Container for cube-like labels.
Definition: Pvl.h:135
Pvl * label() const
Returns a pointer to the IsisLabel object associated with the cube.
Definition: Cube.cpp:1346
static QString ComposeObservation(const QString &sn, SerialNumberList &list, bool def2filename=false)
Creates the ObservationNumber from a string representing the SerialNumber and a SerialList.
QString fileName() const
Returns the filename used to initialise the Pvl object.
Definition: PvlContainer.h:246
FileName highestVersion() const
Searches the directory specified in the file name for the highest version of the file name...
Definition: FileName.cpp:329
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
Serial Number list generator.
static PvlGroup FindSerialTranslation(Pvl &label)
Get Groups by translating from correct Translation table.
IO Handler for Isis Cubes.
Definition: Cube.h:170