Isis 3 Programmer Reference
CubeAttribute.cpp
Go to the documentation of this file.
1 
23 #include "CubeAttribute.h"
24 
25 #include <iostream>
26 
27 #include <QDebug>
28 
29 #include "FileName.h"
30 #include "IException.h"
31 #include "Preference.h"
32 #include "SpecialPixel.h"
33 
34 
35 using namespace std;
36 
37 namespace Isis {
38  //---------------------------------------------------------------------------
39  // CubeAttributeInput Implementation
40  //---------------------------------------------------------------------------
41  CubeAttributeInput::CubeAttributeInput() : CubeAttribute<CubeAttributeInput>(testers()) {
42  }
43 
44 
46  CubeAttribute<CubeAttributeInput>(testers(), fileName) {
47  }
48 
49 
51  }
52 
53 
63 // void CubeAttributeInput::Set(const FileName &fileName) {
64 // Reset();
65 //
66 // QString str(fileName.attributes());
67 //
68 // // Get rid of any white space
69 // str.ConvertWhiteSpace();
70 // str.Compress();
71 // str.Remove(" ");
72 // str.UpCase();
73 //
74 // // Look at each comma delimited token
75 // QString commaTok;
76 // while((commaTok = str.Token(",")).length() > 0) {
77 // // Is this token a range of bands
78 // if (commaTok.find('-') != string::npos) {
79 // QString dashTok;
80 // int start = commaTok.Token("-").ToInteger();
81 // int end = commaTok.Token("-").ToInteger();
82 // int direction;
83 // direction = (start <= end) ? 1 : -1;
84 // // Save the entire range of bands
85 // for (int band = start; band != end; band += direction) {
86 // m_bands.push_back(Isis::QString(band));
87 // }
88 // m_bands.push_back(Isis::QString(end));
89 // }
90 // // This token is a single band specification
91 // else {
92 // m_bands.push_back(commaTok);
93 // }
94 // }
95 // }
96 
97 
98  vector<QString> CubeAttributeInput::bands() const {
99  vector<QString> result;
100 
101  QString str = toString().remove(QRegExp("^\\+"));
102 
103  QStringList strSplit = str.split(",", QString::SkipEmptyParts);
104  foreach (QString commaTok, strSplit) {
105  // Is this token a range of bands
106  if (commaTok.contains('-')) {
107  QString dashTok;
108  int start = toInt(commaTok.mid(0, commaTok.indexOf("-")));
109  int end = toInt(commaTok.mid(commaTok.indexOf("-") + 1));
110  int direction;
111  direction = (start <= end) ? 1 : -1;
112  // Save the entire range of bands
113  for (int band = start; band != end; band += direction) {
114  result.push_back(Isis::toString(band));
115  }
116  result.push_back(Isis::toString(end));
117  }
118  // This token is a single band specification
119  else {
120  result.push_back(commaTok);
121  }
122  }
123 
124  return result;
125  }
126 
127 
129  return toString(bands());
130  }
131 
132 
133  void CubeAttributeInput::setBands(const vector<QString> &bands) {
134  setAttributes("+" + toString(bands));
135  }
136 
137 
138  bool CubeAttributeInput::isBandRange(QString attribute) const {
139  return QRegExp("[0-9,\\-]+").exactMatch(attribute);
140  }
141 
142 
143  QString CubeAttributeInput::toString(const vector<QString> &bands) {
144  QString result;
145  for (unsigned int i = 0; i < bands.size(); i++) {
146  if (i > 0)
147  result += ",";
148 
149  result += bands[i];
150  }
151 
152  return result;
153  }
154 
155 
156  QList<bool (CubeAttributeInput::*)(QString) const> CubeAttributeInput::testers() {
158 
159  result.append(&CubeAttributeInput::isBandRange);
160 
161  return result;
162  }
163 
164 
165  //---------------------------------------------------------------------------
166  // CubeAttributeOutput Implementation
167  //---------------------------------------------------------------------------
169  }
170 
171 
173  : CubeAttribute<CubeAttributeOutput>(testers(), fileName) {
174  }
175 
176 
178  }
179 
180 
182  bool result = false;
183 
184  QStringList pixelTypeAtts = attributeList(&CubeAttributeOutput::isPixelType);
185 
186  if (pixelTypeAtts.isEmpty() || pixelTypeAtts.last() == "PROPAGATE")
187  result = true;
188 
189  return result;
190  }
191 
192 
194  return attributeList(&CubeAttributeOutput::isRange).isEmpty();
195  }
196 
197 
198 // void CubeAttributeOutput::Set(const FileName &fileName) {
199 // Reset();
200 //
201 // Isis::QString str(fileName.attributes());
202 //
203 // // Remove any white space
204 // str.ConvertWhiteSpace();
205 // str.Compress();
206 // str.Remove(" ");
207 // str.UpCase();
208 // str.TrimHead("+");
209 //
210 // // Look at each "+" separate attribute
211 // Isis::QString tok;
212 // while((tok = str.Token("+")).length() > 0) {
213 //
214 // // If there is a ":" in this token then it is assumed to be a min:max
215 // if (tok.find(":") != string::npos) {
216 //
217 // // Pull out the minimum
218 // Isis::QString colonTok = tok;
219 // Isis::QString min = colonTok.Token(":");
220 // if (min.length() > 0) {
221 // m_minimum = min.ToDouble();
222 // }
223 // else {
224 // m_minimum = 0.0;
225 // }
226 //
227 // // Pull out the maximum
228 // Isis::QString max = colonTok.Token(":");
229 // if (max.length() > 0) {
230 // m_maximum = max.ToDouble();
231 // }
232 // else {
233 // m_maximum = 0.0;
234 // }
235 // m_rangeType = RangeSet;
236 // }
237 //
238 // // Parse any pixel type attributes
239 // else if (tok == "8BIT" || tok == "8-BIT" || tok == "UNSIGNEDBYTE") {
240 // m_pixelType = Isis::UnsignedByte;
241 // m_pixelTypeDef = "SET";
242 // }
243 // else if (tok == "16BIT" || tok == "16-BIT" || tok == "SIGNEDWORD") {
244 // m_pixelType = Isis::SignedWord;
245 // m_pixelTypeDef = "SET";
246 // }
247 // else if (tok == "32BIT" || tok == "32-BIT" || tok == "REAL") {
248 // m_pixelType = Isis::Real;
249 // m_pixelTypeDef = "SET";
250 // }
251 // else if (tok == "PROPAGATE") {
252 // m_pixelType = Isis::None;
253 // m_pixelTypeDef = "PROPAGATE";
254 // }
255 //
256 // // Parse any file formats
257 // else if (tok == "TILE") {
258 // m_format = Cube::Tile;
259 // }
260 // else if (tok == "BSQ" || tok == "BANDSEQUENTIAL") {
261 // m_format = Cube::Bsq;
262 // }
263 //
264 // // Parse any byte order
265 // else if (tok == "LSB") {
266 // m_order = Isis::Lsb;
267 // }
268 // else if (tok == "MSB") {
269 // m_order = Isis::Msb;
270 // }
271 //
272 // // Parse any label type
273 // else if (tok == "ATTACHED") {
274 // m_labelAttachment = Isis::AttachedLabel;
275 // }
276 // else if (tok == "DETACHED") {
277 // m_labelAttachment = Isis::DetachedLabel;
278 // }
279 // }
280 // }
281 
282 
284  Cube::Format result = Cube::Tile;
285 
286  QStringList formatList = attributeList(&CubeAttributeOutput::isFileFormat);
287 
288  if (!formatList.isEmpty()) {
289  QString formatString = formatList.last();
290 
291  if (formatString == "BSQ" || formatString == "BANDSEQUENTIAL")
292  result = Cube::Bsq;
293  }
294 
295  return result;
296  }
297 
298 
300  return toString(fileFormat());
301  }
302 
303 
305  setAttribute((fmt == Cube::Tile)? "Tile" : "BandSequential",
306  &CubeAttributeOutput::isFileFormat);
307  }
308 
309 
311  double result = Null;
312 
313  if (!propagateMinimumMaximum()) {
314  QString range = attributeList(&CubeAttributeOutput::isRange).last();
315 
316  QStringList rangeList = range.split(":");
317  if (rangeList.count() == 2 && rangeList.first() != "")
318  result = toDouble(rangeList.first());
319  }
320 
321  return result;
322  }
323 
324 
326  double result = Null;
327 
328  if (!propagateMinimumMaximum()) {
329  QString range = attributeList(&CubeAttributeOutput::isRange).last();
330 
331  QStringList rangeList = range.split(":");
332  if (rangeList.count() == 2 && rangeList.last() != "")
333  result = toDouble(rangeList.last());
334  }
335 
336  return result;
337  }
338 
339 
341  if (!IsSpecial(min)) {
342  QString newRange = Isis::toString(min) + ":";
343 
344  if (!IsSpecial(maximum()))
345  newRange += Isis::toString(maximum());
346 
347  setAttribute(newRange, &CubeAttributeOutput::isRange);
348  }
349  else if (!IsSpecial(maximum())) {
350  setAttribute(":" + Isis::toString(maximum()), &CubeAttributeOutput::isRange);
351  }
352  else {
353  setAttribute("", &CubeAttributeOutput::isRange);
354  }
355  }
356 
357 
359  if (!IsSpecial(max)) {
360  QString newRange = ":" + Isis::toString(max);
361 
362  if (!IsSpecial(minimum()))
363  newRange = Isis::toString(minimum()) + newRange;
364 
365  setAttribute(newRange, &CubeAttributeOutput::isRange);
366  }
367  else if (!IsSpecial(minimum())) {
368  setAttribute(Isis::toString(minimum()) + ":", &CubeAttributeOutput::isRange);
369  }
370  else {
371  setAttribute("", &CubeAttributeOutput::isRange);
372  }
373  }
374 
375 
377  PixelType result = None;
378 
379  if (!propagatePixelType()) {
380  QString pixelTypeAtt = attributeList(&CubeAttributeOutput::isPixelType).last();
381 
382  if (pixelTypeAtt == "8BIT" || pixelTypeAtt == "8-BIT" || pixelTypeAtt == "UNSIGNEDBYTE") {
383  result = UnsignedByte;
384  }
385  else if (pixelTypeAtt == "16BIT" || pixelTypeAtt == "16-BIT" || pixelTypeAtt == "SIGNEDWORD") {
386  result = SignedWord;
387  }
388  else if (pixelTypeAtt == "16UBIT" || pixelTypeAtt == "16-UBIT" || pixelTypeAtt == "UNSIGNEDWORD") {
389  result = UnsignedWord;
390  }
391  else if (pixelTypeAtt == "32BIT" || pixelTypeAtt == "32-BIT" || pixelTypeAtt == "REAL") {
392  result = Real;
393  }
394  else if (pixelTypeAtt == "32UINT" || pixelTypeAtt == "32-UINT" || pixelTypeAtt == "UNSIGNEDINTEGER") {
395  result = UnsignedInteger;
396  }
397  else if (pixelTypeAtt == "32INT" || pixelTypeAtt == "32-INT" || pixelTypeAtt == "SIGNEDINTEGER") {
398  result = SignedInteger;
399  }
400  }
401 
402  return result;
403  }
404 
405 
407  setAttribute(PixelTypeName(type), &CubeAttributeOutput::isPixelType);
408  }
409 
410 
412  setAttribute(LabelAttachmentName(attachment), &CubeAttributeOutput::isLabelAttachment);
413  }
414 
415 
416  LabelAttachment CubeAttributeOutput::labelAttachment() const {
418 
419  QStringList labelAttachmentAtts = attributeList(&CubeAttributeOutput::isLabelAttachment);
420  if (!labelAttachmentAtts.isEmpty()) {
421  QString labelAttachmentAtt = labelAttachmentAtts.last();
422 
423  if (labelAttachmentAtt == "DETACHED")
424  result = DetachedLabel;
425  else if (labelAttachmentAtt == "EXTERNAL")
426  result = ExternalLabel;
427  }
428 
429  return result;
430  }
431 
432 
433  bool CubeAttributeOutput::isByteOrder(QString attribute) const {
434  return QRegExp("(M|L)SB").exactMatch(attribute);
435  }
436 
437 
438  bool CubeAttributeOutput::isFileFormat(QString attribute) const {
439  return QRegExp("(BANDSEQUENTIAL|BSQ|TILE)").exactMatch(attribute);
440  }
441 
442 
443  bool CubeAttributeOutput::isLabelAttachment(QString attribute) const {
444  return QRegExp("(ATTACHED|DETACHED|EXTERNAL)").exactMatch(attribute);
445  }
446 
447 
448  bool CubeAttributeOutput::isPixelType(QString attribute) const {
449  QString expressions = "(8-?BIT|16-?BIT|32-?BIT|UNSIGNEDBYTE|SIGNEDWORD|UNSIGNEDWORD|REAL";
450  expressions += "|32-?UINT|32-?INT|UNSIGNEDINTEGER|SIGNEDINTEGER)";
451  return QRegExp(expressions).exactMatch(attribute);
452 
453  }
454 
455 
456  bool CubeAttributeOutput::isRange(QString attribute) const {
457  return QRegExp("[\\-+E0-9.]*:[\\-+E0-9.]*").exactMatch(attribute);
458  }
459 
460 
462  QString result = "Tile";
463 
464  if (format == Cube::Bsq)
465  result = "BandSequential";
466 
467  return result;
468  }
469 
470 
472  ByteOrder result = IsLsb()? Lsb : Msb;
473 
474  QStringList byteOrderAtts = attributeList(&CubeAttributeOutput::isByteOrder);
475 
476  if (!byteOrderAtts.isEmpty()) {
477  QString byteOrderAtt = byteOrderAtts.last();
478  result = (byteOrderAtt == "LSB")? Lsb : Msb;
479  }
480 
481  return result;
482  }
483 
484 
486  return ByteOrderName(byteOrder());
487  }
488 
489 
491  setAttribute((order == Msb)? "MSB" : "LSB",
492  &CubeAttributeOutput::isByteOrder);
493  }
494 
495 
496  QList<bool (CubeAttributeOutput::*)(QString) const> CubeAttributeOutput::testers() {
498 
499  result.append(&CubeAttributeOutput::isByteOrder);
500  result.append(&CubeAttributeOutput::isFileFormat);
501  result.append(&CubeAttributeOutput::isLabelAttachment);
502  result.append(&CubeAttributeOutput::isPixelType);
503  result.append(&CubeAttributeOutput::isRange);
504 
505  return result;
506  }
507 }
Manipulate and parse attributes of input cube filenames.
The label is pointing to an external DN file - the label is also external to the data.
Definition: CubeAttribute.h:56
const double Null
Value for an Isis Null pixel.
Definition: SpecialPixel.h:110
PixelType pixelType() const
Return the pixel type as an Isis::PixelType.
ByteOrder byteOrder() const
Return the byte order as an Isis::ByteOrder.
File name manipulation and expansion.
Definition: FileName.h:116
int toInt(const QString &string)
Global function to convert from a string to an integer.
Definition: IString.cpp:108
QString PixelTypeName(Isis::PixelType pixelType)
Returns string name of PixelType enumeration entered as input parameter.
Definition: PixelType.h:82
Namespace for the standard library.
ByteOrder
Tests the current architecture for byte order.
Definition: Endian.h:59
bool propagatePixelType() const
Return true if the pixel type is to be propagated from an input cube.
void setAttributes(const FileName &fileName)
Replaces the current attributes with the attributes in the given file name.
bool propagateMinimumMaximum() const
Return true if the min/max are to be propagated from an input cube.
void setLabelAttachment(LabelAttachment attachment)
Set the label attachment type to the parameter value.
void setFileFormat(Cube::Format fmt)
Set the format to the fmt parameter.
QString byteOrderString() const
Return the byte order as a string.
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:226
double toDouble(const QString &string)
Global function to convert from a string to a double.
Definition: IString.cpp:164
PixelType
Enumerations for Isis Pixel Types.
Definition: PixelType.h:43
CubeAttributeOutput()
Constructs an empty CubeAttributeOutput.
~CubeAttributeInput()
Destroys the object.
The input label is in a separate data file from the image.
Definition: CubeAttribute.h:49
Cubes are stored in tile format, that is the order of the pixels in the file (on disk) is BSQ within ...
Definition: Cube.h:234
void setAttribute(QString newValue, bool(CubeAttributeOutput ::*tester)(QString) const)
Set the attribute(s) for which tester returns true to newValue.
Manipulate and parse attributes of output cube filenames.
bool IsLsb()
Return true if this host is an LSB first machine and false if it is not.
Definition: Endian.h:84
QString LabelAttachmentName(LabelAttachment labelType)
Return the string representation of the contents of a variable of type LabelAttachment.
Definition: CubeAttribute.h:68
double maximum() const
Return the output cube attribute maximum.
bool IsSpecial(const double d)
Returns if the input pixel is special.
Definition: SpecialPixel.h:212
The input label is embedded in the image file.
Definition: CubeAttribute.h:48
Cube::Format fileFormat() const
Return the file format an Cube::Format.
void setPixelType(PixelType type)
Set the pixel type to that given by the parameter.
QString fileFormatString() const
Return the file format as a string.
void setMinimum(double min)
Set the output cube attribute minimum.
Cubes are stored in band-sequential format, that is the order of the pixels in the file (on disk) is:...
Definition: Cube.h:201
QString bandsString() const
Return a string representation of all the bands.
~CubeAttributeOutput()
Destroys the object.
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
std::vector< QString > bands() const
Return a vector of the input bands specified.
Parent class for CubeAttributeInput and CubeAttributeOutput.
void setBands(const std::vector< QString > &bands)
Set the band attribute according to the list of bands.
double minimum() const
Return the output cube attribute minimum.
CubeAttributeInput()
Constructs an empty CubeAttributeInput.
LabelAttachment
Input cube label type tracker.
Definition: CubeAttribute.h:47
Format
These are the possible storage formats of Isis3 cubes.
Definition: Cube.h:180
QString toString() const
Return a string-representation of this cube attributes.
void setByteOrder(ByteOrder order)
Set the order according to the parameter order.
void setMaximum(double max)
Set the output cube attribute maximum.
QStringList attributeList(bool(CubeAttributeOutput ::*tester)(QString) const) const
Get a list of attributes that the tester returns true on.