Isis 3 Programmer Reference
ProcessImportVicar.cpp
Go to the documentation of this file.
1 
23 #include "ProcessImportVicar.h"
24 
25 #include <iostream>
26 #include <QString>
27 #include <sstream>
28 
29 #include "Preference.h"
30 #include "IException.h"
31 #include "LineManager.h"
32 #include "Pvl.h"
33 #include "PixelType.h"
34 #include "SpecialPixel.h"
35 #include "IString.h"
36 #include "UserInterface.h"
37 
38 using namespace std;
39 
40 namespace Isis {
48  void ProcessImportVicar::SetVicarFile(const QString &vicarFile, Pvl &vicarLab) {
49  // Open vicar file
50  ifstream vicFile(vicarFile.toLatin1().data(), ios::in);
51 
52  if(!vicFile) {
53  QString msg = "Cannot open vicar file [" + vicarFile + "]";
54  throw IException(IException::User, msg, _FILEINFO_);
55  }
56 
57  try {
58  // get the starting VICAR label and convert to PVL
59  IString vicLabels = ExtractPvlLabel(0, vicFile);
60 
61  // Fill temp Pvl label for ProcessImport startprocess
62  stringstream lbl;
63  lbl << vicLabels << " End" << endl;
64  Pvl vLab;
65  lbl >> vLab;
66  vicarLab = vLab;
67 
68  // Set the fileHeaderBytes
69  SetFileHeaderBytes(vLab["LBLSIZE"]);
70 
71  // Set the dataHeaderBytes
72  SetDataHeaderBytes((int) vLab["NLB"] * (int)vLab["RECSIZE"]);
73 
74  // Are there binary prefix bytes on each image line?
75  SetDataPrefixBytes(vLab["NBB"]);
76  SetDataSuffixBytes(0);
77 
78  SetDimensions(vLab["NS"], vLab["NL"], vLab["NB"]);
79 
80  QString pixType = vLab["FORMAT"];
81  Isis::PixelType pixelType = None;
82  if(pixType == "BYTE") pixelType = UnsignedByte;
83  if(pixType == "WORD") pixelType = UnsignedWord;
84  if(pixType == "HALF") pixelType = SignedWord;
85  if(pixType == "REAL") pixelType = Real;
86  if(pixelType == None) {
87  QString msg = "Unsupported pixel type [FORMAT=" + pixType + "]";
88  throw IException(IException::Io, msg, _FILEINFO_);
89  }
90  SetPixelType(pixelType);
91 
92  QString order = vLab["INTFMT"];
93  if(order == "LOW") {
94  SetByteOrder(Lsb);
95  }
96  else {
97  SetByteOrder(Msb);
98  }
99 
100  QString organization = vLab["ORG"];
101  if(organization == "BSQ") {
102  SetOrganization(ProcessImport::BSQ);
103  }
104  else if(organization == "BIL") {
105  SetOrganization(ProcessImport::BIL);
106  }
107  else if(organization == "BIP") {
108  SetOrganization(ProcessImport::BIP);
109  }
110  else {
111  QString msg = "Unsupported file organization [" + organization + "]";
112  throw IException(IException::Io, msg, _FILEINFO_);
113  }
114 
115  // See if there is end-of-dataset labels
116  // If so read them and merge
117  if(vLab.hasKeyword("EOL")) {
118  if((int) vLab["EOL"] == 1) {
119  int startByte = (int) vLab["LBLSIZE"] +
120  (int) vLab["NLB"] * (int) vLab["RECSIZE"] +
121  (int) vLab["NL"] * (int) vLab["NB"] *
122  (int) vLab["RECSIZE"];
123  ifstream vicFile(vicarFile.toLatin1().data(), ios::in);
124 
125  QString endPvlLabel = ExtractPvlLabel(startByte, vicFile);
126  stringstream lbl;
127  lbl << endPvlLabel;
128 
129  Pvl endLab;
130  lbl >> endLab;
131  vicFile.close();
132 
133  for(int k = 0; k < endLab.keywords(); k++) {
134  vicarLab += endLab[k];
135  }
136  }
137  }
138  }
139  catch(IException &e) {
140  QString msg = "Input file [" + vicarFile + "] does not appear to be a vicar file";
141  throw IException(IException::User, msg, _FILEINFO_);
142  }
143 
144  SetInputFile(vicarFile);
145  }
146 
155  QString ProcessImportVicar::ExtractPvlLabel(int startPos, std::ifstream &vicarFile) const {
156  vicarFile.seekg(startPos, ios::beg);
157 
158  // convert the LBLSIZE to an integer
159  char *lblSizeValue = new char [1024];
160  vicarFile.seekg(QString("LBLSIZE=").size(), ios_base::cur);
161 
162  for(int pos = 0; pos < 1024 - 1; pos++) {
163  if(!vicarFile.good())
164  break;
165 
166  if(vicarFile.peek() == ' ')
167  break;
168 
169  lblSizeValue[pos] = vicarFile.get();
170  lblSizeValue[pos + 1] = '\0';
171 
172  // we're totally lost at this point
173  if(pos == 1023) {
174  QString msg = "Cannot find label size in VICAR file";
175  throw IException(IException::User, msg, _FILEINFO_);
176  }
177  }
178 
179  int lblSize = IString(lblSizeValue).ToInteger();
180  delete [] lblSizeValue;
181  lblSizeValue = NULL;
182 
183  char *buf = new char[lblSize+1];
184 
185  // Read end vicar label
186  vicarFile.seekg(startPos, ios::beg);
187  vicarFile.read(buf, lblSize);
188  buf[lblSize] = '\0';
189  vicarFile.close();
190 
191  // Transform the vicar labels into valid pvl labels
192  QString vicLabels = buf;
193 
194  bool inQuote = false;
195  for(int pos = 0; pos < vicLabels.size(); pos++) {
196  if(vicLabels[pos] == '\'' || vicLabels[pos] == '"') {
197  inQuote = !inQuote;
198  }
199 
200  if(!inQuote && vicLabels[pos] == ' ') {
201  vicLabels[pos] = '\n';
202  }
203  }
204 
205  return vicLabels;
206  }
207 } // end namespace Isis
Namespace for the standard library.
int ToInteger() const
Returns the object string as an integer.
Definition: IString.cpp:733
PixelType
Enumerations for Isis Pixel Types.
Definition: PixelType.h:43
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
Container for cube-like labels.
Definition: Pvl.h:135
Isis exception class.
Definition: IException.h:107
Adds specific functionality to C++ strings.
Definition: IString.h:181
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31