7 #include "ExportPdsTable.h"
13 #include "EndianSwapper.h"
15 #include "IException.h"
17 #include "PvlKeyword.h"
18 #include "PvlObject.h"
20 #include "TableField.h"
21 #include "TableRecord.h"
38 ExportPdsTable::ExportPdsTable(
Table isisTable) {
40 m_outputRecordBytes = 0;
41 m_pdsByteOrder =
"LSB";
42 m_isisTable =
new Table(isisTable);
43 m_numRows = isisTable.
Records();
44 m_rowBytes = m_isisTable->RecordSize();
51 ExportPdsTable::~ExportPdsTable() {
67 PvlObject ExportPdsTable::exportTable(
char *pdsTableBuffer,
68 int outputFileRecordBytes,
69 QString pdsTableByteOrder) {
73 m_outputRecordBytes = outputFileRecordBytes;
74 if (m_rowBytes > m_outputRecordBytes) {
76 "Unable to export Isis::Table object to PDS. The "
77 "Isis::Table record size [" +
toString(m_rowBytes)
78 +
"] is larger than the record bytes allowed in the "
79 "PDS file [" +
toString(m_outputRecordBytes) +
"].",
84 m_pdsByteOrder = pdsTableByteOrder.toUpper();
86 if (m_pdsByteOrder !=
"MSB" && m_pdsByteOrder !=
"LSB") {
87 QString msg =
"Unable to export the Isis Table [" + m_isisTable->Name()
88 +
"] to a PDS table using the requested byte order ["
89 + m_pdsByteOrder +
"]. Valid values are MSB or LSB.";
90 throw IException(IException::Unknown, msg, _FILEINFO_);
95 int numRowNulls = outputFileRecordBytes - m_rowBytes;
96 char endRowPadding[numRowNulls];
97 for (
int i = 0; i < numRowNulls; i++) {
98 endRowPadding[i] =
'\0';
106 for(
int recIndex = 0; recIndex < m_isisTable->Records(); recIndex++) {
109 Pack(record, rowBuffer, endianSwap);
110 int i = recIndex*m_outputRecordBytes;
111 memmove(pdsTableBuffer + i, &rowBuffer, record.
RecordSize());
112 memmove(pdsTableBuffer + i + m_rowBytes, &endRowPadding, numRowNulls);
114 buffsize+=numRowNulls;
116 return fillMetaData();
126 QString pdsTableName = formatPdsTableName();
127 PvlObject pdsTableLabelInfo(pdsTableName);
137 for(
int fieldIndex = 0; fieldIndex < m_isisTable->RecordFields(); fieldIndex++) {
139 TableField field = (*m_isisTable)[0][fieldIndex];
145 if (field.
type() == TableField::Text) {
148 for(
int i = 0; i < field.
size(); i++) {
152 else if (field.
type() == TableField::Integer) {
153 if (m_pdsByteOrder ==
"MSB") {
155 columnBytes =
sizeof(int);
160 columnBytes =
sizeof(int);
163 else if (field.
type() == TableField::Double) {
164 if (m_pdsByteOrder ==
"MSB") {
166 columnBytes =
sizeof(double);
171 columnBytes =
sizeof(double);
174 else if (field.
type() == TableField::Real) {
175 if (m_pdsByteOrder ==
"MSB") {
177 columnBytes =
sizeof(float);
182 columnBytes =
sizeof(float);
188 QString msg =
"Unable to export Isis::Table object to PDS. Invalid "
189 "field type found for [" + field.
name() +
"].";
190 throw IException(IException::Programmer, msg, _FILEINFO_);
193 startByte += columnBytes;
197 return pdsTableLabelInfo;
205 QString ExportPdsTable::formatPdsTableName() {
206 return ExportPdsTable::formatPdsTableName(m_isisTable->Name());
219 QString ExportPdsTable::formatPdsTableName(QString isisTableName) {
220 QString tableName = isisTableName.simplified();
221 QString pdsTableName;
222 pdsTableName.push_back(tableName[0]);
223 for (
int i = 1 ; i < tableName.size() ; i++) {
224 if (tableName[i] >= 65 && tableName[i] <= 90) {
225 pdsTableName.push_back(
'_');
226 pdsTableName.push_back(tableName[i]);
229 pdsTableName.push_back(tableName[i]);
232 pdsTableName = pdsTableName.toUpper();
233 if (pdsTableName.indexOf(
"_TABLE") != pdsTableName.length() - 6) {
234 pdsTableName +=
"_TABLE";
253 for(
int fieldIndex = 0; fieldIndex < record.
Fields(); fieldIndex++) {
260 vector<double> fieldValues = field;
261 for(
unsigned int i = 0; i < fieldValues.size(); i++) {
262 double value = endianSwap->
Double(&fieldValues[i]);
263 memmove(buffer + startByte, &value, 8);
264 startByte +=
sizeof(double);
268 vector<int> fieldValues = field;
269 for(
unsigned int i = 0; i < fieldValues.size(); i++) {
270 int value = endianSwap->
Int(&fieldValues[i]);
271 memmove(buffer + startByte, &value, 4);
272 startByte +=
sizeof(int);
278 for(
int i = 0; i < field.
size(); i++) {
279 if(i < (
int)val.length()) {
280 buffer[startByte] = val[i].toLatin1();
286 buffer[startByte] = 0;
292 vector<float> fieldValues = field;
293 for(
unsigned int i = 0; i < fieldValues.size(); i++) {
294 float value = endianSwap->
Float(&fieldValues[i]);
295 memmove(buffer + startByte, &value, 4);
296 startByte +=
sizeof(float);
302 QString msg =
"Unable to export Isis::Table object to PDS. Invalid "
303 "field type found for [" + field.
name() +
"].";
304 throw IException(IException::Programmer, msg, _FILEINFO_);
309 if (startByte != m_rowBytes) {
313 QString msg =
"Unable to export Isis::Table object [" + m_isisTable->Name()
314 +
"] to PDS. Record lengths are uneven.";
315 throw IException(IException::Unknown, msg, _FILEINFO_);