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
22using namespace std;
23
24namespace 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"]);
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") {
87 }
88 else if(organization == "BIL") {
90 }
91 else if(organization == "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 exception class.
Definition IException.h:91
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition IException.h:126
@ Io
A type of error that occurred when performing an actual I/O operation.
Definition IException.h:155
Adds specific functionality to C++ strings.
Definition IString.h:165
int ToInteger() const
Returns the object string as an integer.
Definition IString.cpp:718
void SetPixelType(const Isis::PixelType type)
Sets the pixel type of the input file.
void SetFileHeaderBytes(const int bytes)
This method sets the number of bytes in the header of a file.
void SetOrganization(const ProcessImport::Interleave org)
Sets the organization of the input cube.
void SetDataPrefixBytes(const int bytes)
This method sets the number of bytes at the beginning of each data record of a file.
void SetInputFile(const QString &file)
Sets the name of the input file to be read in the import StartProcess method and verifies its existan...
void SetDataHeaderBytes(const int bytes)
This method sets the number of bytes in the header of each datablock of a file.
void SetByteOrder(const Isis::ByteOrder order)
Sets the byte order of the input file.
@ BIL
Band Interleaved By Line Format (i.e.
@ BIP
Band Interleaved By Pixel Format (i.e.
@ BSQ
Band Sequential Format (i.e.
void SetDataSuffixBytes(const int bytes)
This method sets the number of bytes at the end of each data record of a file.
void SetDimensions(const int ns, const int nl, const int nb)
Sets the physical size of the input cube.
QString ExtractPvlLabel(int startPos, std::ifstream &vicarFile) const
Returns a valid PVL label based on the start position in the VICAR file.
void SetVicarFile(const QString &vicarFile, Pvl &vicarLab)
Opens a vicar file which can then be immediately imported by invoking the inherited StartProcess meth...
Container for cube-like labels.
Definition Pvl.h:119
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
PixelType
Enumerations for Isis Pixel Types.
Definition PixelType.h:27
Namespace for the standard library.