USGS

Isis 3.0 Object Programmers' Reference

Home

CubeBsqHandler.cpp
Go to the documentation of this file.
1 
24 #include "CubeBsqHandler.h"
25 
26 #include <iostream>
27 
28 #include <QFile>
29 
30 #include "IException.h"
31 #include "Pvl.h"
32 #include "PvlKeyword.h"
33 #include "PvlObject.h"
34 #include "RawCubeChunk.h"
35 
36 using namespace std;
37 
38 namespace Isis {
50  CubeBsqHandler::CubeBsqHandler(QFile * dataFile,
51  const QList<int> *virtualBandList, const Pvl &labels, bool alreadyOnDisk)
52  : CubeIoHandler(dataFile, virtualBandList, labels, alreadyOnDisk) {
53  int numSamplesInChunk = sampleCount();
54  int numLinesInChunk = 1;
55  QList<int> primeFactors;
56 
57  // we want our chunk sizes to be less than 1GB
58  int sizeLimit = 1024 * 1024 * 1024;
59  int maxNumLines = (sizeLimit) / (SizeOf(pixelType()) * numSamplesInChunk);
60 
61  // we've exceed our sizeLimit; increase our limit so we can process an entire line
62  if (maxNumLines == 0)
63  maxNumLines = 1;
64 
65  numLinesInChunk = findGoodSize(maxNumLines, lineCount());
66 
67  setChunkSizes(numSamplesInChunk, numLinesInChunk, 1);
68  }
69 
70 
75  clearCache();
76  }
77 
78 
80  PvlObject &core = label.findObject("IsisCube").findObject("Core");
81  core.addKeyword(PvlKeyword("Format", "BandSequential"),
82  PvlContainer::Replace);
83  }
84 
85 
87  BigInt startByte = getChunkStartByte(chunkToFill);
88 
89  bool success = false;
90 
91  QFile * dataFile = getDataFile();
92  if(dataFile->seek(startByte)) {
93  QByteArray binaryData = dataFile->read(chunkToFill.getByteCount());
94 
95  if(binaryData.size() == chunkToFill.getByteCount()) {
96  chunkToFill.setRawData(binaryData);
97  success = true;
98  }
99  }
100 
101  if(!success) {
102  IString msg = "Reading from the file [" + dataFile->fileName() + "] "
103  "failed with reading [" +
104  QString::number(chunkToFill.getByteCount()) +
105  "] bytes at position [" + QString::number(startByte) + "]";
106  throw IException(IException::Io, msg, _FILEINFO_);
107  }
108  }
109 
110 
111  void CubeBsqHandler::writeRaw(const RawCubeChunk &chunkToWrite) {
112  BigInt startByte = getChunkStartByte(chunkToWrite);
113 
114  bool success = false;
115 
116  QFile * dataFile = getDataFile();
117  if(dataFile->seek(startByte)) {
118  BigInt dataWritten = dataFile->write(chunkToWrite.getRawData());
119 
120  if(dataWritten == chunkToWrite.getByteCount()) {
121  success = true;
122  }
123  }
124 
125  if(!success) {
126  IString msg = "Writing to the file [" + dataFile->fileName() + "] "
127  "failed with writing [" +
128  QString::number(chunkToWrite.getByteCount()) +
129  "] bytes at position [" + QString::number(startByte) + "]";
130  throw IException(IException::Io, msg, _FILEINFO_);
131  }
132  }
133 
134 
144  int CubeBsqHandler::findGoodSize(int maxSize, int dimensionSize) const {
145  int chunkDimensionSize;
146 
147  if (dimensionSize <= maxSize) {
148  chunkDimensionSize = dimensionSize;
149  }
150  else {
151  // find largest divisor of dimension size so chunks fit into cube uniformly
152  int greatestDivisor = maxSize;
153  while (dimensionSize % greatestDivisor > 0) {
154  greatestDivisor--;
155  }
156  chunkDimensionSize = greatestDivisor;
157  }
158  return chunkDimensionSize;
159  }
160 
161 
168  BigInt CubeBsqHandler::getChunkStartByte(const RawCubeChunk &chunk) const {
169  return getDataStartByte() + getChunkIndex(chunk) * getBytesPerChunk();
170  }
171 }