USGS

Isis 3.0 Object Programmers' Reference

Home

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 == "32BIT" || pixelTypeAtt == "32-BIT" || pixelTypeAtt == "REAL") {
389  result = Real;
390  }
391  }
392 
393  return result;
394  }
395 
396 
398  setAttribute(PixelTypeName(type), &CubeAttributeOutput::isPixelType);
399  }
400 
401 
403  setAttribute(LabelAttachmentName(attachment), &CubeAttributeOutput::isLabelAttachment);
404  }
405 
406 
407  LabelAttachment CubeAttributeOutput::labelAttachment() const {
409 
410  QStringList labelAttachmentAtts = attributeList(&CubeAttributeOutput::isLabelAttachment);
411  if (!labelAttachmentAtts.isEmpty()) {
412  QString labelAttachmentAtt = labelAttachmentAtts.last();
413 
414  if (labelAttachmentAtt == "DETACHED")
415  result = DetachedLabel;
416  else if (labelAttachmentAtt == "EXTERNAL")
417  result = ExternalLabel;
418  }
419 
420  return result;
421  }
422 
423 
424  bool CubeAttributeOutput::isByteOrder(QString attribute) const {
425  return QRegExp("(M|L)SB").exactMatch(attribute);
426  }
427 
428 
429  bool CubeAttributeOutput::isFileFormat(QString attribute) const {
430  return QRegExp("(BANDSEQUENTIAL|BSQ|TILE)").exactMatch(attribute);
431  }
432 
433 
434  bool CubeAttributeOutput::isLabelAttachment(QString attribute) const {
435  return QRegExp("(ATTACHED|DETACHED|EXTERNAL)").exactMatch(attribute);
436  }
437 
438 
439  bool CubeAttributeOutput::isPixelType(QString attribute) const {
440  return QRegExp("(8-?BIT|16-?BIT|32-?BIT|UNSIGNEDBYTE|SIGNEDWORD|REAL)").exactMatch(attribute);
441  }
442 
443 
444 
445  bool CubeAttributeOutput::isRange(QString attribute) const {
446  return QRegExp("[\\-+E0-9.]*:[\\-+E0-9.]*").exactMatch(attribute);
447  }
448 
449 
451  QString result = "Tile";
452 
453  if (format == Cube::Bsq)
454  result = "BandSequential";
455 
456  return result;
457  }
458 
459 
461  ByteOrder result = IsLsb()? Lsb : Msb;
462 
463  QStringList byteOrderAtts = attributeList(&CubeAttributeOutput::isByteOrder);
464 
465  if (!byteOrderAtts.isEmpty()) {
466  QString byteOrderAtt = byteOrderAtts.last();
467  result = (byteOrderAtt == "LSB")? Lsb : Msb;
468  }
469 
470  return result;
471  }
472 
473 
475  return ByteOrderName(byteOrder());
476  }
477 
478 
480  setAttribute((order == Msb)? "MSB" : "LSB",
481  &CubeAttributeOutput::isByteOrder);
482  }
483 
484 
485  QList<bool (CubeAttributeOutput::*)(QString) const> CubeAttributeOutput::testers() {
487 
488  result.append(&CubeAttributeOutput::isByteOrder);
489  result.append(&CubeAttributeOutput::isFileFormat);
490  result.append(&CubeAttributeOutput::isLabelAttachment);
491  result.append(&CubeAttributeOutput::isPixelType);
492  result.append(&CubeAttributeOutput::isRange);
493 
494  return result;
495  }
496 }