Isis 3 Programmer Reference
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 
85  PvlObject &core = label.findObject("IsisCube").findObject("Core");
86  core.addKeyword(PvlKeyword("Format", "BandSequential"),
87  PvlContainer::Replace);
88  }
89 
90 
92  BigInt startByte = getChunkStartByte(chunkToFill);
93 
94  bool success = false;
95 
96  QFile * dataFile = getDataFile();
97  if(dataFile->seek(startByte)) {
98  QByteArray binaryData = dataFile->read(chunkToFill.getByteCount());
99 
100  if(binaryData.size() == chunkToFill.getByteCount()) {
101  chunkToFill.setRawData(binaryData);
102  success = true;
103  }
104  }
105 
106  if(!success) {
107  IString msg = "Reading from the file [" + dataFile->fileName() + "] "
108  "failed with reading [" +
109  QString::number(chunkToFill.getByteCount()) +
110  "] bytes at position [" + QString::number(startByte) + "]";
111  throw IException(IException::Io, msg, _FILEINFO_);
112  }
113  }
114 
115 
116  void CubeBsqHandler::writeRaw(const RawCubeChunk &chunkToWrite) {
117  BigInt startByte = getChunkStartByte(chunkToWrite);
118 
119  bool success = false;
120 
121  QFile * dataFile = getDataFile();
122  if(dataFile->seek(startByte)) {
123  BigInt dataWritten = dataFile->write(chunkToWrite.getRawData());
124 
125  if(dataWritten == chunkToWrite.getByteCount()) {
126  success = true;
127  }
128  }
129 
130  if(!success) {
131  IString msg = "Writing to the file [" + dataFile->fileName() + "] "
132  "failed with writing [" +
133  QString::number(chunkToWrite.getByteCount()) +
134  "] bytes at position [" + QString::number(startByte) + "]";
135  throw IException(IException::Io, msg, _FILEINFO_);
136  }
137  }
138 
139 
149  int CubeBsqHandler::findGoodSize(int maxSize, int dimensionSize) const {
150  int chunkDimensionSize;
151 
152  if (dimensionSize <= maxSize) {
153  chunkDimensionSize = dimensionSize;
154  }
155  else {
156  // find largest divisor of dimension size so chunks fit into cube uniformly
157  int greatestDivisor = maxSize;
158  while (dimensionSize % greatestDivisor > 0) {
159  greatestDivisor--;
160  }
161  chunkDimensionSize = greatestDivisor;
162  }
163  return chunkDimensionSize;
164  }
165 
166 
174  return getDataStartByte() + getChunkIndex(chunk) * getBytesPerChunk();
175  }
176 }
long long int BigInt
Big int.
Definition: Constants.h:65
virtual void readRaw(RawCubeChunk &chunkToFill)
This needs to populate the chunkToFill with unswapped raw bytes from the disk.
void clearCache(bool blockForWriteCache=true) const
Free all cube chunks (cached cube data) from memory and write them to disk.
int getChunkIndex(const RawCubeChunk &) const
Given a chunk, what&#39;s its index in the file.
void setRawData(QByteArray rawData)
Sets the chunk&#39;s raw data.
void setChunkSizes(int numSamples, int numLines, int numBands)
This should be called once from the child constructor.
PvlObjectIterator findObject(const QString &name, PvlObjectIterator beg, PvlObjectIterator end)
Find the index of object with a specified name, between two indexes.
Definition: PvlObject.h:286
int SizeOf(Isis::PixelType pixelType)
Returns the number of bytes of the specified PixelType.
Definition: PixelType.h:62
~CubeBsqHandler()
The destructor writes all cached data to disk.
Namespace for the standard library.
BigInt getDataStartByte() const
void addKeyword(const PvlKeyword &keyword, const InsertMode mode=Append)
Add a keyword to the container.
A type of error that occurred when performing an actual I/O operation.
Definition: IException.h:171
Handles converting buffers to and from disk.
Unless noted otherwise, the portions of Isis written by the USGS are public domain.
void updateLabels(Pvl &labels)
Function to update the labels with a Pvl object.
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
A single keyword-value pair.
Definition: PvlKeyword.h:98
A section of raw data on the disk.
Definition: RawCubeChunk.h:42
virtual void writeRaw(const RawCubeChunk &chunkToWrite)
This needs to write the chunkToWrite directly to disk with no modifications to the data itself...
BigInt getBytesPerChunk() const
Container for cube-like labels.
Definition: Pvl.h:135
BigInt getChunkStartByte(const RawCubeChunk &chunk) const
This is a helper method that goes from chunk to file position.
Isis exception class.
Definition: IException.h:107
Adds specific functionality to C++ strings.
Definition: IString.h:181
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
int getByteCount() const
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:74
QByteArray & getRawData() const
Definition: RawCubeChunk.h:53
int findGoodSize(int maxSize, int dimensionSize) const
This method attempts to compute a good chunk line size.
PixelType pixelType() const