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
20using namespace std;
21
22namespace Isis {
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
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
160}
CubeBsqHandler(QFile *dataFile, const QList< int > *virtualBandList, const Pvl &label, bool alreadyOnDisk)
Construct a BSQ IO handler.
virtual void readRaw(RawCubeChunk &chunkToFill)
This needs to populate the chunkToFill with unswapped raw bytes from the disk.
virtual void writeRaw(const RawCubeChunk &chunkToWrite)
This needs to write the chunkToWrite directly to disk with no modifications to the data itself.
int findGoodSize(int maxSize, int dimensionSize) const
This method attempts to compute a good chunk line size.
BigInt getChunkStartByte(const RawCubeChunk &chunk) const
This is a helper method that goes from chunk to file position.
void updateLabels(Pvl &labels)
Function to update the labels with a Pvl object.
~CubeBsqHandler()
The destructor writes all cached data to disk.
Handles converting buffers to and from disk.
void clearCache(bool blockForWriteCache=true) const
Free all cube chunks (cached cube data) from memory and write them to disk.
PixelType pixelType() const
void setChunkSizes(int numSamples, int numLines, int numBands)
This should be called once from the child constructor.
BigInt getBytesPerChunk() const
BigInt getDataStartByte() const
int getChunkIndex(const RawCubeChunk &) const
Given a chunk, what's its index in the file.
Isis exception class.
Definition IException.h:91
@ Io
A type of error that occurred when performing an actual I/O operation.
Definition IException.h:155
Adds specific functionality to C++ strings.
Definition IString.h:165
Container for cube-like labels.
Definition Pvl.h:119
A single keyword-value pair.
Definition PvlKeyword.h:87
Contains Pvl Groups and Pvl Objects.
Definition PvlObject.h:61
PvlObjectIterator findObject(const QString &name, PvlObjectIterator beg, PvlObjectIterator end)
Find the index of object with a specified name, between two indexes.
Definition PvlObject.h:276
A section of raw data on the disk.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
int SizeOf(Isis::PixelType pixelType)
Returns the number of bytes of the specified PixelType.
Definition PixelType.h:46
long long int BigInt
Big int.
Definition Constants.h:49
Namespace for the standard library.