Isis 3 Programmer Reference
CubeBsqHandler.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 
8 #include "CubeBsqHandler.h"
9 
10 #include <iostream>
11 
12 #include <QFile>
13 
14 #include "IException.h"
15 #include "Pvl.h"
16 #include "PvlKeyword.h"
17 #include "PvlObject.h"
18 #include "RawCubeChunk.h"
19 
20 using namespace std;
21 
22 namespace Isis {
34  CubeBsqHandler::CubeBsqHandler(QFile * dataFile,
35  const QList<int> *virtualBandList, const Pvl &labels, bool alreadyOnDisk)
36  : CubeIoHandler(dataFile, virtualBandList, labels, alreadyOnDisk) {
37  int numSamplesInChunk = sampleCount();
38  int numLinesInChunk = 1;
39  QList<int> primeFactors;
40 
41  // we want our chunk sizes to be less than 1GB
42  int sizeLimit = 1024 * 1024 * 1024;
43  int maxNumLines = (sizeLimit) / (SizeOf(pixelType()) * numSamplesInChunk);
44 
45  // we've exceed our sizeLimit; increase our limit so we can process an entire line
46  if (maxNumLines == 0)
47  maxNumLines = 1;
48 
49  numLinesInChunk = findGoodSize(maxNumLines, lineCount());
50 
51  setChunkSizes(numSamplesInChunk, numLinesInChunk, 1);
52  }
53 
54 
59  clearCache();
60  }
61 
62 
69  PvlObject &core = label.findObject("IsisCube").findObject("Core");
70  core.addKeyword(PvlKeyword("Format", "BandSequential"),
71  PvlContainer::Replace);
72  }
73 
74 
76  BigInt startByte = getChunkStartByte(chunkToFill);
77 
78  bool success = false;
79 
80  QFile * dataFile = getDataFile();
81  if(dataFile->seek(startByte)) {
82  QByteArray binaryData = dataFile->read(chunkToFill.getByteCount());
83 
84  if(binaryData.size() == chunkToFill.getByteCount()) {
85  chunkToFill.setRawData(binaryData);
86  success = true;
87  }
88  }
89 
90  if(!success) {
91  IString msg = "Reading from the file [" + dataFile->fileName() + "] "
92  "failed with reading [" +
93  QString::number(chunkToFill.getByteCount()) +
94  "] bytes at position [" + QString::number(startByte) + "]";
95  throw IException(IException::Io, msg, _FILEINFO_);
96  }
97  }
98 
99 
100  void CubeBsqHandler::writeRaw(const RawCubeChunk &chunkToWrite) {
101  BigInt startByte = getChunkStartByte(chunkToWrite);
102 
103  bool success = false;
104 
105  QFile * dataFile = getDataFile();
106  if(dataFile->seek(startByte)) {
107  BigInt dataWritten = dataFile->write(chunkToWrite.getRawData());
108 
109  if(dataWritten == chunkToWrite.getByteCount()) {
110  success = true;
111  }
112  }
113 
114  if(!success) {
115  IString msg = "Writing to the file [" + dataFile->fileName() + "] "
116  "failed with writing [" +
117  QString::number(chunkToWrite.getByteCount()) +
118  "] bytes at position [" + QString::number(startByte) + "]";
119  throw IException(IException::Io, msg, _FILEINFO_);
120  }
121  }
122 
123 
133  int CubeBsqHandler::findGoodSize(int maxSize, int dimensionSize) const {
134  int chunkDimensionSize;
135 
136  if (dimensionSize <= maxSize) {
137  chunkDimensionSize = dimensionSize;
138  }
139  else {
140  // find largest divisor of dimension size so chunks fit into cube uniformly
141  int greatestDivisor = maxSize;
142  while (dimensionSize % greatestDivisor > 0) {
143  greatestDivisor--;
144  }
145  chunkDimensionSize = greatestDivisor;
146  }
147  return chunkDimensionSize;
148  }
149 
150 
158  return getDataStartByte() + getChunkIndex(chunk) * getBytesPerChunk();
159  }
160 }
Isis::SizeOf
int SizeOf(Isis::PixelType pixelType)
Returns the number of bytes of the specified PixelType.
Definition: PixelType.h:46
Isis::RawCubeChunk
A section of raw data on the disk.
Definition: RawCubeChunk.h:27
Isis::CubeBsqHandler::findGoodSize
int findGoodSize(int maxSize, int dimensionSize) const
This method attempts to compute a good chunk line size.
Definition: CubeBsqHandler.cpp:133
Isis::IException::Io
@ Io
A type of error that occurred when performing an actual I/O operation.
Definition: IException.h:155
Isis::PvlObject
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:61
Isis::PvlKeyword
A single keyword-value pair.
Definition: PvlKeyword.h:82
QList< int >
Isis::CubeIoHandler::sampleCount
int sampleCount() const
Definition: CubeIoHandler.cpp:596
Isis::CubeIoHandler::pixelType
PixelType pixelType() const
Definition: CubeIoHandler.cpp:587
Isis::PvlContainer::addKeyword
void addKeyword(const PvlKeyword &keyword, const InsertMode mode=Append)
Add a keyword to the container.
Definition: PvlContainer.cpp:202
Isis::CubeIoHandler::getBytesPerChunk
BigInt getBytesPerChunk() const
Definition: CubeIoHandler.cpp:486
Isis::Pvl
Container for cube-like labels.
Definition: Pvl.h:119
Isis::RawCubeChunk::getRawData
QByteArray & getRawData() const
Definition: RawCubeChunk.h:38
Isis::CubeIoHandler::getDataFile
QFile * getDataFile()
Definition: CubeIoHandler.cpp:562
Isis::CubeBsqHandler::getChunkStartByte
BigInt getChunkStartByte(const RawCubeChunk &chunk) const
This is a helper method that goes from chunk to file position.
Definition: CubeBsqHandler.cpp:157
Isis::CubeBsqHandler::updateLabels
void updateLabels(Pvl &labels)
Function to update the labels with a Pvl object.
Definition: CubeBsqHandler.cpp:68
Isis::RawCubeChunk::getByteCount
int getByteCount() const
Definition: RawCubeChunk.cpp:154
Isis::CubeBsqHandler::writeRaw
virtual void writeRaw(const RawCubeChunk &chunkToWrite)
This needs to write the chunkToWrite directly to disk with no modifications to the data itself.
Definition: CubeBsqHandler.cpp:100
Isis::CubeIoHandler::lineCount
int lineCount() const
Definition: CubeIoHandler.cpp:571
Isis::CubeIoHandler::getDataStartByte
BigInt getDataStartByte() const
Definition: CubeIoHandler.cpp:551
Isis::CubeIoHandler
Handles converting buffers to and from disk.
Definition: CubeIoHandler.h:109
Isis::BigInt
long long int BigInt
Big int.
Definition: Constants.h:49
Isis::PvlObject::findObject
PvlObjectIterator findObject(const QString &name, PvlObjectIterator beg, PvlObjectIterator end)
Find the index of object with a specified name, between two indexes.
Definition: PvlObject.h:274
Isis::CubeIoHandler::getChunkIndex
int getChunkIndex(const RawCubeChunk &) const
Given a chunk, what's its index in the file.
Definition: CubeIoHandler.cpp:530
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::RawCubeChunk::setRawData
void setRawData(QByteArray rawData)
Sets the chunk's raw data.
Definition: RawCubeChunk.cpp:101
std
Namespace for the standard library.
Isis::CubeBsqHandler::readRaw
virtual void readRaw(RawCubeChunk &chunkToFill)
This needs to populate the chunkToFill with unswapped raw bytes from the disk.
Definition: CubeBsqHandler.cpp:75
Isis::CubeIoHandler::clearCache
void clearCache(bool blockForWriteCache=true) const
Free all cube chunks (cached cube data) from memory and write them to disk.
Definition: CubeIoHandler.cpp:383
Isis::IString
Adds specific functionality to C++ strings.
Definition: IString.h:165
Isis::CubeIoHandler::setChunkSizes
void setChunkSizes(int numSamples, int numLines, int numBands)
This should be called once from the child constructor.
Definition: CubeIoHandler.cpp:621
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::CubeBsqHandler::~CubeBsqHandler
~CubeBsqHandler()
The destructor writes all cached data to disk.
Definition: CubeBsqHandler.cpp:58