Isis 3 Programmer Reference
CameraFactory.cpp
Go to the documentation of this file.
1 
23 #include "CameraFactory.h"
24 
25 #include "Camera.h"
26 #include "Plugin.h"
27 #include "IException.h"
28 #include "FileName.h"
29 
30 using namespace std;
31 
32 namespace Isis {
33  Plugin CameraFactory::m_cameraPlugin;
34 
46  Camera *CameraFactory::Create(Cube &cube) {
47  // Try to load a plugin file in the current working directory and then
48  // load the system file
49  initPlugin();
50 
51  try {
52  // First get the spacecraft and instrument and combine them
53  Pvl &lab = *cube.label();
54  PvlGroup &inst = lab.findGroup("Instrument", Isis::Pvl::Traverse);
55  QString spacecraft = (QString) inst["SpacecraftName"];
56  QString name = (QString) inst["InstrumentId"];
57  spacecraft = spacecraft.toUpper();
58  name = name.toUpper();
59  QString group = spacecraft + "/" + name;
60  group = group.remove(" ");
61 
62  PvlGroup &kerns = lab.findGroup("Kernels", Isis::Pvl::Traverse);
63  // Default version 1 for backwards compatibility (spiceinit'd cubes before camera model versioning)
64  if (!kerns.hasKeyword("CameraVersion")) {
65  kerns.addKeyword(PvlKeyword("CameraVersion", "1"));
66  }
67 
68  int cameraOriginalVersion = (int)kerns["CameraVersion"];
69  int cameraNewestVersion = CameraVersion(cube);
70 
71  if (cameraOriginalVersion != cameraNewestVersion) {
72  string msg = "The camera model used to create a camera for this cube is out of date, " \
73  "please re-run spiceinit on the file or process with an old Isis version " \
74  "that has the correct camera model.";
75  throw IException(IException::Unknown, msg, _FILEINFO_);
76  }
77 
78  // See if we have a camera model plugin
79  QFunctionPointer ptr;
80  try {
81  ptr = m_cameraPlugin.GetPlugin(group);
82  }
83  catch(IException &e) {
84  QString msg = "Unsupported camera model, unable to find plugin for ";
85  msg += "SpacecraftName [" + spacecraft + "] with InstrumentId [";
86  msg += name + "]";
87  throw IException(e, IException::Unknown, msg, _FILEINFO_);
88  }
89 
90  // Now cast that pointer in the proper way
91  Camera * (*plugin)(Isis::Cube &cube);
92  plugin = (Camera * ( *)(Isis::Cube &cube)) ptr;
93 
94  // Create the projection as requested
95  return (*plugin)(cube);
96  }
97  catch(IException &e) {
98  string message = "Unable to initialize camera model from group [Instrument]";
99  throw IException(e, IException::Unknown, message, _FILEINFO_);
100  }
101  }
102 
103 
107  void CameraFactory::initPlugin() {
108  if (m_cameraPlugin.fileName() == "") {
109  FileName localFile("Camera.plugin");
110  if (localFile.fileExists())
111  m_cameraPlugin.read(localFile.expanded());
112 
113  FileName systemFile("$ISISROOT/lib/Camera.plugin");
114  if (systemFile.fileExists())
115  m_cameraPlugin.read(systemFile.expanded());
116  }
117  }
118 
119 
127  int CameraFactory::CameraVersion(Cube &cube) {
128  return CameraVersion(*cube.label());
129  }
130 
131 
139  int CameraFactory::CameraVersion(Pvl &lab) {
140  // Try to load a plugin file in the current working directory and then
141  // load the system file
142  initPlugin();
143 
144  try {
145  // First get the spacecraft and instrument and combine them
146  PvlGroup &inst = lab.findGroup("Instrument", Isis::Pvl::Traverse);
147  QString spacecraft = (QString) inst["SpacecraftName"];
148  QString name = (QString) inst["InstrumentId"];
149  spacecraft = spacecraft.toUpper();
150  name = name.toUpper();
151  QString group = spacecraft + "/" + name;
152  group = group.remove(" ");
153 
154  PvlGroup plugin;
155  try {
156  bool found = false;
157  // Find the most recent (last) version of the camera model
158  for (int i = m_cameraPlugin.groups() - 1; i >= 0; i--) {
159  if (m_cameraPlugin.group(i) == group) {
160  plugin = m_cameraPlugin.group(i);
161  found = true;
162  break;
163  }
164  }
165  if (!found) {
166  QString msg = "Unable to find PVL group [" + group + "].";
167  throw IException(IException::Unknown, msg, _FILEINFO_);
168  }
169  }
170  catch(IException &e) {
171  QString msg = "Unsupported camera model, unable to find plugin for ";
172  msg += "SpacecraftName [" + spacecraft + "] with InstrumentId [";
173  msg += name + "]";
174  throw IException(e, IException::Unknown, msg, _FILEINFO_);
175  }
176 
177  if (!plugin.hasKeyword("Version")) {
178  QString msg = "Camera model identified by [" + group + "] does not have a version number";
179  throw IException(IException::Programmer, msg, _FILEINFO_);
180  }
181 
182  return (int)plugin["Version"];
183  }
184  catch(IException &e) {
185  string msg = "Unable to locate latest camera model version number from group [Instrument]";
186  throw IException(e, IException::Unknown, msg, _FILEINFO_);
187  }
188  }
189 } // end namespace isis
bool hasKeyword(const QString &name) const
Check to see if a keyword exists.
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
Namespace for the standard library.
Search child objects.
Definition: PvlObject.h:170
void addKeyword(const PvlKeyword &keyword, const InsertMode mode=Append)
Add a keyword to the container.
Contains multiple PvlContainers.
Definition: PvlGroup.h:57
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
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
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
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
bool fileExists() const
Returns true if the file exists; false otherwise.
Definition: FileName.cpp:465
IO Handler for Isis Cubes.
Definition: Cube.h:170