USGS

Isis 3.0 Object Programmers' Reference

Home

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.toAscii().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 == "HALF") pixelType = SignedWord;
84  if(pixType == "REAL") pixelType = Real;
85  if(pixelType == None) {
86  QString msg = "Unsupported pixel type [FORMAT=" + pixType + "]";
87  throw IException(IException::Io, msg, _FILEINFO_);
88  }
89  SetPixelType(pixelType);
90 
91  QString order = vLab["INTFMT"];
92  if(order == "LOW") {
93  SetByteOrder(Lsb);
94  }
95  else {
96  SetByteOrder(Msb);
97  }
98 
99  QString organization = vLab["ORG"];
100  if(organization == "BSQ") {
101  SetOrganization(ProcessImport::BSQ);
102  }
103  else if(organization == "BIL") {
104  SetOrganization(ProcessImport::BIL);
105  }
106  else if(organization == "BIP") {
107  SetOrganization(ProcessImport::BIP);
108  }
109  else {
110  QString msg = "Unsupported file organization [" + organization + "]";
111  throw IException(IException::Io, msg, _FILEINFO_);
112  }
113 
114  // See if there is end-of-dataset labels
115  // If so read them and merge
116  if(vLab.hasKeyword("EOL")) {
117  if((int) vLab["EOL"] == 1) {
118  int startByte = (int) vLab["LBLSIZE"] +
119  (int) vLab["NLB"] * (int) vLab["RECSIZE"] +
120  (int) vLab["NL"] * (int) vLab["NB"] *
121  (int) vLab["RECSIZE"];
122  ifstream vicFile(vicarFile.toAscii().data(), ios::in);
123 
124  QString endPvlLabel = ExtractPvlLabel(startByte, vicFile);
125  stringstream lbl;
126  lbl << endPvlLabel;
127 
128  Pvl endLab;
129  lbl >> endLab;
130  vicFile.close();
131 
132  for(int k = 0; k < endLab.keywords(); k++) {
133  vicarLab += endLab[k];
134  }
135  }
136  }
137  }
138  catch(IException &e) {
139  QString msg = "Input file [" + vicarFile + "] does not appear to be a vicar file";
140  throw IException(IException::User, msg, _FILEINFO_);
141  }
142 
143  SetInputFile(vicarFile);
144  }
145 
154  QString ProcessImportVicar::ExtractPvlLabel(int startPos, std::ifstream &vicarFile) const {
155  vicarFile.seekg(startPos, ios::beg);
156 
157  // convert the LBLSIZE to an integer
158  char *lblSizeValue = new char [1024];
159  vicarFile.seekg(QString("LBLSIZE=").size(), ios_base::cur);
160 
161  for(int pos = 0; pos < 1024 - 1; pos++) {
162  if(!vicarFile.good())
163  break;
164 
165  if(vicarFile.peek() == ' ')
166  break;
167 
168  lblSizeValue[pos] = vicarFile.get();
169  lblSizeValue[pos + 1] = '\0';
170 
171  // we're totally lost at this point
172  if(pos == 1023) {
173  QString msg = "Cannot find label size in VICAR file";
174  throw IException(IException::User, msg, _FILEINFO_);
175  }
176  }
177 
178  int lblSize = IString(lblSizeValue).ToInteger();
179  delete [] lblSizeValue;
180  lblSizeValue = NULL;
181 
182  char *buf = new char[lblSize+1];
183 
184  // Read end vicar label
185  vicarFile.seekg(startPos, ios::beg);
186  vicarFile.read(buf, lblSize);
187  buf[lblSize] = '\0';
188  vicarFile.close();
189 
190  // Transform the vicar labels into valid pvl labels
191  QString vicLabels = buf;
192 
193  bool inQuote = false;
194  for(int pos = 0; pos < vicLabels.size(); pos++) {
195  if(vicLabels[pos] == '\'' || vicLabels[pos] == '"') {
196  inQuote = !inQuote;
197  }
198 
199  if(!inQuote && vicLabels[pos] == ' ') {
200  vicLabels[pos] = '\n';
201  }
202  }
203 
204  return vicLabels;
205  }
206 } // end namespace Isis