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
19namespace 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}
IO Handler for Isis Cubes.
Definition Cube.h:168
Pvl * label() const
Returns a pointer to the IsisLabel object associated with the cube.
Definition Cube.cpp:1707
File name manipulation and expansion.
Definition FileName.h:100
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
Definition FileName.cpp:162
Isis exception class.
Definition IException.h:91
static QString Compose(Pvl &label, bool def2filename=false)
Compose a ObservationNumber from a PVL.
QString fileName() const
Returns the filename used to initialise the Pvl object.
Contains multiple PvlContainers.
Definition PvlGroup.h:41
Container for cube-like labels.
Definition Pvl.h:119
A single keyword-value pair.
Definition PvlKeyword.h:87
PvlObjectIterator findObject(const QString &name, PvlObjectIterator beg, PvlObjectIterator end)
Find the index of object with a specified name, between two indexes.
Definition PvlObject.h:276
Allows applications to translate simple text files.
static PvlGroup FindSerialTranslation(Pvl &label)
Get Groups by translating from correct Translation table.
SerialNumber()
Create an empty SerialNumber object.
static QString Compose(Pvl &label, bool def2filename=false)
Compose a SerialNumber from a PVL.
static QString CreateSerialNumber(PvlGroup &snGroup, int key)
Create the SerialNumber string by concatenating the keywords in the label with '/' in between serialN...
static QString ComposeObservation(const QString &sn, SerialNumberList &list, bool def2filename=false)
Creates the ObservationNumber from a string representing the SerialNumber and a SerialList.
virtual ~SerialNumber()
Destroy a SerialNumber object.
Serial Number list generator.
QString fileName(const QString &sn)
Return a filename given a serial number.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition IString.cpp:211