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