Isis 3 Programmer Reference
ObservationNumber.cpp
1 #include "ObservationNumber.h"
2 #include "IException.h"
3 #include "Pvl.h"
4 #include "Cube.h"
5 #include "Process.h"
7 #include "FileName.h"
8 
9 namespace Isis {
14 
19 
25  QString ObservationNumber::Compose(Pvl &label, bool def2filename) {
26  QString sn;
27  try {
28  PvlGroup snGroup = FindObservationTranslation(label);
29  sn = CreateSerialNumber(snGroup, (int)snGroup["ObservationKeys"]);
30  }
31  catch(IException &e) {
32  if(def2filename) {
33  // Try to return the filename if it exists in the label, otherwise use
34  // "Unknown" as a last resort.
35  QString snTemp = label.fileName();
36  if(!snTemp.isEmpty()) {
37  sn = FileName(snTemp).name();
38  }
39  else {
40  sn = "Unknown";
41  }
42  }
43  else {
44  sn = "Unknown";
45  }
46  }
47 
48  return sn;
49  }
50 
56  QString ObservationNumber::Compose(Cube &cube, bool def2filename) {
57  return Compose(*cube.label(), def2filename);
58  }
59 
65  QString ObservationNumber::Compose(const QString &filename, bool def2filename) {
66  Pvl p(filename);
67  return Compose(p, def2filename);
68  }
69 
76  Pvl outLabel;
77  static PvlGroup dataDir(Preference::Preferences().findGroup("DataDirectory"));
78 
79  // Get the mission name
80  static QString missionTransFile = (QString) dataDir["base"] + "/translations/MissionName2DataDir.trn";
81  static PvlToPvlTranslationManager missionXlater(missionTransFile);
82  missionXlater.SetLabel(label);
83  QString mission = missionXlater.Translate("MissionName");
84 
85  // Get the instrument name
86  static QString instTransFile = (QString) dataDir["base"] + "/translations/Instruments.trn";
87  static PvlToPvlTranslationManager instrumentXlater(instTransFile);
88  instrumentXlater.SetLabel(label);
89  QString instrument = instrumentXlater.Translate("InstrumentName");
90 
91  // We want to use this instrument's translation manager. It's much faster for
92  // ObservationNumberList if we keep the translation manager in memory, so re-reading
93  // from the disk is not necessary every time. To do this, we'll use a map to store
94  // the translation managers and observation number keys with a string identifier to find them.
95  // This identifier needs to have the mission name and the instrument name.
96  static std::map<QString, std::pair<PvlToPvlTranslationManager, PvlKeyword> > missionTranslators;
97  QString key = mission + "_" + instrument;
98  std::map<QString, std::pair<PvlToPvlTranslationManager, PvlKeyword> >::iterator
99  translationIterator = missionTranslators.find(key);
100 
101  if(translationIterator == missionTranslators.end()) {
102  // Get the file
103  FileName snFile((QString) dataDir[mission] + "/translations/" +
104  instrument + "SerialNumber????.trn");
105  snFile = snFile.highestVersion();
106 
107  // Delets the extra
108  Pvl translation(snFile.expanded());
109  PvlKeyword observationKeys;
110  if(translation.hasKeyword("ObservationKeys")) {
111  observationKeys = translation["ObservationKeys"];
112  }
113 
114  // use the translation file to generate keywords
115  missionTranslators.insert(
116  std::pair<QString, std::pair<PvlToPvlTranslationManager, PvlKeyword> >
117  (key, std::pair<PvlToPvlTranslationManager, PvlKeyword>(PvlToPvlTranslationManager(snFile.expanded()), observationKeys))
118  );
119 
120  translationIterator = missionTranslators.find(key);
121  }
122 
123  translationIterator->second.first.SetLabel(label);
124  translationIterator->second.first.Auto(outLabel);
125  PvlGroup snGroup = outLabel.findGroup("SerialNumberKeywords");
126 
127  // Delets the extra
128  if(!translationIterator->second.second.name().isEmpty()) {
129  snGroup += translationIterator->second.second;
130  }
131  else {
132  snGroup += PvlKeyword("ObservationKeys", toString(snGroup.keywords()));
133  }
134 
135  return snGroup;
136  }
137 
146  std::vector<QString> ObservationNumber::PossibleSerial(const QString &on, SerialNumberList &list) {
147  std::vector<QString> sn;
148  for(int i = 0; i < list.size(); i++) {
149  if(list.serialNumber(i).startsWith(on)) {
150  sn.push_back(list.serialNumber(i));
151  }
152  }
153  return sn;
154  }
155 
156 
157 
158 }
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 serialNumber(const QString &filename)
Return a serial number given a filename.
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.
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
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:212
virtual ~ObservationNumber()
Destroy a SerialNumber object.
virtual QString Translate(QString translationGroupName, int findex=0)
Returns a translated value.
std::vector< QString > PossibleSerial(const QString &on, SerialNumberList &list)
Creates a vector of plasible SerialNumbers from a string representing the ObservationNumber and a Ser...
Container for cube-like labels.
Definition: Pvl.h:135
int size() const
How many serial number / filename combos are in the list.
Pvl * label() const
Returns a pointer to the IsisLabel object associated with the cube.
Definition: Cube.cpp:1346
static PvlGroup FindObservationTranslation(Pvl &label)
Get Groups by translating from correct Translation table.
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.
ObservationNumber()
Create an empty SerialNumber object.
IO Handler for Isis Cubes.
Definition: Cube.h:170