57 ImportPdsTable::ImportPdsTable() {
60 m_tableName =
"TABLE";
82 ImportPdsTable::ImportPdsTable(
const QString &pdsLabFile,
83 const QString &pdsTableFile,
84 const QString &pdsTableName) {
85 m_tableName = pdsTableName;
86 load(pdsLabFile, pdsTableFile);
92 ImportPdsTable::~ImportPdsTable() {
115 void ImportPdsTable::load(
const QString &pdsLabFile,
116 const QString &pdsTableFile) {
120 loadLabel(pdsLabFile, tempTblFile);
121 if (!pdsTableFile.isEmpty()) tempTblFile = pdsTableFile;
127 int tableStartRecord =
toInt(tableFile.baseName());
128 tempTblFile = pdsLabFile;
129 m_pdsTableStart = tableStartRecord;
134 if (!tableFile.fileExists()) {
137 + tableFile.name().toLower());
138 if (!tableFileLowercase.fileExists()) {
139 IString msg =
"Unable to import PDS table. Neither of the following "
140 "possible table files were found: ["
141 + tableFile.expanded() +
"] or ["
142 + tableFileLowercase.expanded() +
"]";
145 tableFile = tableFileLowercase.expanded();
146 tempTblFile = tableFile.expanded();
150 if (m_pdsTableType ==
"ASCII") {
151 loadTable(tempTblFile);
153 m_pdsTableFile = tempTblFile;
167 bool ImportPdsTable::hasColumn(
const QString &colName)
const {
168 return (findColumn(colName) != 0);
189 QString ImportPdsTable::getColumnName(
const unsigned int &index,
190 const bool &formatted)
const {
191 if ((
int) index >= columns() - 1) {
192 QString msg =
"Unable to import the binary PDS table [" + m_tableName
193 +
"] into Isis. The requested column index ["
194 +
toString((
int) index) +
"] exceeds the last column index ["
198 QString name = m_coldesc[index].m_name;
199 if (formatted) name = getFormattedName(name);
222 for (
int i = 0 ; i < columns() ; i++) {
223 QString name = m_coldesc[i].m_name;
224 if (formatted) name = getFormattedName(name);
225 colnames.push_back(name);
245 QString ImportPdsTable::getType(
const QString &colName)
const {
270 bool ImportPdsTable::setType(
const QString &colName,
271 const QString &dataType) {
276 return (column != 0);
290 Table ImportPdsTable::importTable(
const QString &isisTableName) {
293 Table table(isisTableName, record);
294 fillTable(table, m_coldesc, record);
298 QString msg =
"Unable to import the PDS table [" + m_tableName
299 +
"] from the PDS file [" + m_pdsTableFile +
"] into Isis.";
322 Table ImportPdsTable::importTable(
const QString &colnames,
323 const QString &isisTableName) {
324 return (importTable(colnames.split(
","), isisTableName));
342 const QString &isisTableName) {
344 for (
int i = 0 ; i < colnames.size() ; i++) {
345 const ColumnDescr *descr = findColumn(colnames[i]);
347 QString msg =
"Unable to import the PDS table [" + m_tableName
348 +
"] into Isis. The requested column name ["
349 + colnames[i] +
"] does not "
353 ctypes.push_back(*descr);
358 Table table(isisTableName, record);
359 fillTable(table, ctypes, record);
368 void ImportPdsTable::init() {
393 void ImportPdsTable::loadLabel(
const QString &pdsLabFile,
394 QString &pdsTableFile) {
399 QString msg =
"The PDS file " + pdsLabFile +
400 " does not have the required TABLE object, ["
401 + m_tableName +
"]. The PDS label file is probably invalid";
407 m_recordBytes = (int) tabObj.
findKeyword(
"RECORD_BYTES");
410 m_recordBytes = (int) label.
findKeyword(
"RECORD_BYTES");
413 QString trueTableName;
416 trueTableName = m_tableName;
418 + label[
"^" + m_tableName][0];
420 else if (tabObj.
objects() == 1) {
422 tableDetails = &tabObj.
object(0);
424 + tabObj[
"^" + trueTableName][0];
427 int ncols = (int) tableDetails->
findKeyword(
"COLUMNS");
428 m_pdsTableType = QString(tableDetails->
findKeyword(
"INTERCHANGE_FORMAT"));
429 if (m_pdsTableType !=
"ASCII" && m_pdsTableType.toUpper() !=
"BINARY") {
430 QString msg =
"Unable to import the PDS table [" + m_tableName
431 +
"] from the PDS file ["
432 + pdsTableFile +
"] into Isis. "
433 "The PDS INTERCHANGE_FORMAT [" + m_pdsTableType
434 +
"] is not supported. Valid values are ASCII or BINARY.";
437 m_rowBytes = tableDetails->
findKeyword(
"ROW_BYTES");
442 while (colobj != tableDetails->
endObject()) {
443 if (colobj->isNamed(
"COLUMN")) {
444 m_coldesc.push_back(getColumnDescription(*colobj, icol));
451 if (ncols != columns()) {
453 msg <<
"Number of columns in the COLUMNS label keyword (" << ncols
454 <<
") does not match number of COLUMN objects found ("
457 throw iException::Message(iException::Programmer, msg.str(),
_FILEINFO_);
459 cout << msg.str() << endl;
481 void ImportPdsTable::loadTable(
const QString &pdsTableFile) {
485 QString tempTblFile(pdsTableFile);
490 while (tfile.
GetLine(tline,
false)) {
491 if (irow >= m_trows)
break;
494 for (
int i = 0 ; i < columns() ; i++) {
495 cols.push_back(getColumnValue(tline, m_coldesc[i]));
497 m_rows.push_back(cols);
523 cd.
m_name = colobj[
"NAME"][0];
525 if (m_pdsTableType ==
"ASCII") {
526 cd.
m_dataType = getGenericType(colobj[
"DATA_TYPE"][0]).toUpper();
529 cd.
m_dataType = colobj[
"DATA_TYPE"][0].toUpper();
552 const QString &colName) {
553 QString cName = getFormattedName(colName);
554 ColumnTypes::iterator col = m_coldesc.begin();
555 while (col != m_coldesc.end()) {
556 QString c = getFormattedName(col->m_name);
557 if (c.toUpper() == cName.toUpper()) {
return (&(*col)); }
578 const QString &colName)
const {
579 QString cName = getFormattedName(colName);
580 ColumnTypes::const_iterator col = m_coldesc.begin();
581 while (col != m_coldesc.end()) {
582 QString c = getFormattedName(col->m_name);
583 if (c.toUpper() == cName.toUpper()) {
return (&(*col)); }
600 QString ImportPdsTable::getColumnValue(
const QString &tline,
624 QString ImportPdsTable::getFormattedName(
625 const QString &colname)
const {
626 QString cname = QString(colname).replace(QRegExp(
"[(),]"),
" ").simplified();
628 bool uppercase =
true;
630 for (
int i = 0 ; i < cname.size() ; i++) {
632 oString.push_back(cname[i].toUpper());
635 else if ( (cname[i] ==
' ') || (cname[i] ==
'_') ) {
639 oString.push_back(cname[i].toLower());
664 QString ImportPdsTable::getGenericType(
const QString &ttype)
const {
665 return ttype.split(
"_").last();
688 QString name = getFormattedName(cdesc.
m_name);
689 if (m_pdsTableType ==
"ASCII") {
690 if ( dtype ==
"INTEGER" ) {
691 return (
TableField(name, TableField::Integer));
693 else if ( ((dtype ==
"DOUBLE" )
695 || (dtype ==
"FLOAT")) ) {
696 return (
TableField(name, TableField::Double));
703 return makeFieldFromBinaryTable(cdesc);
721 TableRecord ImportPdsTable::makeRecord(
const ColumnTypes &ctypes) {
723 for (
unsigned int i = 0 ; i < ctypes.size() ; i++) {
752 data.
Trim(
" \t\r\n");
756 data.
Trim(
" \t\r\n");
760 QString str(tfield.
size(),
' ');
761 str.insert(0, data.
Trim(
" \t\r\n").
ToQt());
766 QString msg =
"Conversion failure of column " + cdesc.
m_name;
788 const ColumnTypes &ctypes,
790 for (
unsigned int i = 0 ; i < ctypes.size() ; i++) {
791 extract(cols, ctypes[i], record[i]);
808 void ImportPdsTable::fillTable(
Table &table,
const ColumnTypes &cols,
810 if (m_pdsTableType ==
"ASCII") {
811 for (
unsigned int i = 0 ; i < m_rows.size() ; i++) {
813 table += extract(m_rows[i], cols, record);
816 QString msg =
"Failed to convert data in row [" +
toString((
int) i) +
"]";
822 QString tempTblFile = m_pdsTableFile;
823 ifstream pdsFileStream(tempTblFile.toAscii().data(), ifstream::binary);
824 if (!pdsFileStream) {
825 IString msg =
"Unable to open file containing PDS table ["
826 + tempTblFile +
"].";
830 char *rowBuffer =
new char[m_recordBytes];
831 for (
int i = 1; i < m_pdsTableStart; i++) {
832 pdsFileStream.read(rowBuffer, m_recordBytes);
837 for (
int rowIndex = 0; rowIndex < m_trows; rowIndex++) {
838 rowBuffer =
new char[m_recordBytes];
839 pdsFileStream.read(rowBuffer, m_recordBytes);
840 TableRecord rec = extractBinary(rowBuffer, record);
857 int ImportPdsTable::columns()
const {
858 return (m_coldesc.size());
868 int ImportPdsTable::rows()
const {
869 return (m_rows.size());
889 for (
int colIndex = 0; colIndex < columns(); colIndex++) {
890 QString columnName = m_coldesc[colIndex].m_name;
891 for (
int fieldIndex = 0 ; fieldIndex < record.
Fields() ; fieldIndex++) {
892 QString fieldName = record[fieldIndex].name();
893 if (fieldName == columnName) {
894 int startByte = m_coldesc[colIndex].m_startByte;
895 int numBytes = m_coldesc[colIndex].m_numBytes;
896 if (record[fieldIndex].isInteger()) {
898 memmove(&columnValue, &rowBuffer[startByte], numBytes);
900 int fieldValue = endianSwap.
Int(&columnValue);
901 record[fieldIndex] = fieldValue;
903 else if (record[fieldIndex].isDouble()) {
906 memmove(&columnValue, &rowBuffer[startByte], numBytes);
907 double fieldValue = endianSwap.
Double(&columnValue);
908 record[fieldIndex] = fieldValue;
910 else if (record[fieldIndex].isReal()) {
913 memmove(&columnValue, &rowBuffer[startByte], numBytes);
914 float fieldValue = endianSwap.
Float(&columnValue);
915 record[fieldIndex] = fieldValue;
917 else if (record[fieldIndex].isText()) {
918 QString fieldValue(numBytes,
'\0');
919 for (
int byte = 0; byte < numBytes; byte++) {
920 fieldValue[byte] = rowBuffer[startByte + byte];
922 record[fieldIndex] = fieldValue;
947 QString name = cdesc.
m_name;
948 if (dataType ==
"MSB_INTEGER" || dataType ==
"INTEGER"
949 || dataType ==
"SUN_INTEGER" || dataType ==
"MAC_INTEGER") {
951 QString msg =
"Only 4 byte integer values are supported in Isis. "
952 "PDS Column [" + cdesc.
m_name
953 +
"] has an integer DATA_TYPE with [BYTES = "
957 setPdsByteOrder(
"MSB");
960 else if (dataType ==
"LSB_INTEGER" || dataType ==
"VAX_INTEGER"
961 || dataType ==
"PC_INTEGER" ) {
963 QString msg =
"Only 4 byte integer values are supported in Isis. "
964 "PDS Column [" + cdesc.
m_name
965 +
"] has an integer DATA_TYPE with [BYTES = "
969 setPdsByteOrder(
"LSB");
972 else if (dataType ==
"FLOAT"
973 || dataType ==
"REAL"
974 || dataType ==
"SUN_REAL"
975 || dataType ==
"MAC_REAL"
976 || dataType ==
"IEEE_REAL" ) {
977 setPdsByteOrder(
"MSB");
985 IString msg =
"Only 4 byte or 8 byte real values are supported in Isis. "
986 "PDS Column [" + cdesc.
m_name
987 +
"] has a real DATA_TYPE with [BYTES = "
992 else if (dataType ==
"PC_REAL") {
993 setPdsByteOrder(
"LSB");
1001 QString msg =
"Only 4 byte or 8 byte real values are supported in Isis. "
1002 "PDS Column [" + cdesc.
m_name
1003 +
"] has a real DATA_TYPE with [BYTES = "
1008 else if (dataType.contains(
"CHARACTER")
1009 || dataType.contains(
"ASCII")
1010 || dataType ==
"DATE" || dataType ==
"TIME" ) {
1016 +
"] has an unsupported DATA_TYPE ["
1029 void ImportPdsTable::setPdsByteOrder(QString byteOrder) {
1030 if (!m_byteOrder.isEmpty() && m_byteOrder != byteOrder) {
1031 QString msg =
"Unable import the binary PDS table [" + m_tableName
1032 +
"]. The column DATA_TYPE values indicate differing byte "
1036 m_byteOrder = byteOrder;