From 070fb313bd341cbba613e076abb6f768f65db95f Mon Sep 17 00:00:00 2001 From: knodlseder <> Date: Thu, 13 Mar 2008 06:01:45 +0000 Subject: [PATCH] add logical FITS table column --- include/GFitsTableLogCol.hpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/GammaLib.hpp | 1 + src/fits/GFitsTable.cpp | 8 ++++++++ src/fits/GFitsTableLogCol.cpp | 563 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/test_GFits.cpp | 365 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 1018 insertions(+), 3 deletions(-) create mode 100644 include/GFitsTableLogCol.hpp create mode 100644 src/fits/GFitsTableLogCol.cpp diff --git a/include/GFitsTableLogCol.hpp b/include/GFitsTableLogCol.hpp new file mode 100644 index 0000000..8dc5dd0 --- /dev/null +++ b/include/GFitsTableLogCol.hpp @@ -0,0 +1,84 @@ +/*************************************************************************** + * GFitsTableLogCol.hpp - FITS table logical column class * + * ----------------------------------------------------------------------- * + * copyright : (C) 2008 by Jurgen Knodlseder * + * ----------------------------------------------------------------------- * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +/** + * @file GFitsTableLogCol.hpp + * @brief GFitsTableLogCol class definition. + * @author J. Knodlseder + */ + +#ifndef GFITSTABLELOGCOL_HPP +#define GFITSTABLELOGCOL_HPP + +/* __ Includes ___________________________________________________________ */ +#include "GFitsCfitsio.hpp" +#include "GFitsTableCol.hpp" + +/* __ Namespaces _________________________________________________________ */ + + +/* __ Structures _________________________________________________________ */ + + +/***********************************************************************//** + * @class GFitsTableLogCol + * + * @brief Interface for FITS table logical column + * + * This class implements a FITS table logical column. + ***************************************************************************/ +class GFitsTableLogCol : public GFitsTableCol { + + // I/O friends + friend ostream& operator<< (ostream& os, const GFitsTableLogCol& column); + +public: + // Constructors and destructors + GFitsTableLogCol(); + GFitsTableLogCol(const std::string& name, const int& length, + const int& size = 1); + GFitsTableLogCol(const GFitsTableLogCol& column); + virtual ~GFitsTableLogCol(); + + // Operators + GFitsTableLogCol& operator= (const GFitsTableLogCol& column); + char& operator() (const int& row, const int& inx = 0); + const char& operator() (const int& row, const int& inx = 0) const; + + // Methods + std::string string(const int& row, const int& col = 0); + double real(const int& row, const int& col = 0); + int integer(const int& row, const int& col = 0); + char* data(void); + void set_nullval(const char* value); + +private: + // Private methods + void init_members(void); + void copy_members(const GFitsTableLogCol& column); + void free_members(void); + void save(void); + GFitsTableLogCol* clone(void) const; + std::string ascii_format(void) const; + std::string binary_format(void) const; + void alloc_data(void); + void init_data(void); + void fetch_data(void); + void* ptr_data(void) { return m_data; } + void* ptr_nulval(void) { return m_nulval; } + + // Private data area + char* m_data; //!< Data area + char* m_nulval; //!< NULL value +}; + +#endif /* GFITSTABLELOGCOL_HPP */ diff --git a/include/GammaLib.hpp b/include/GammaLib.hpp index 4e3b822..f90c139 100644 --- a/include/GammaLib.hpp +++ b/include/GammaLib.hpp @@ -38,6 +38,7 @@ #include "GFitsAsciiTable.hpp" #include "GFitsBinTable.hpp" #include "GFitsTableCol.hpp" +#include "GFitsTableLogCol.hpp" #include "GFitsTableStrCol.hpp" #include "GFitsTableShtCol.hpp" #include "GFitsTableLngCol.hpp" diff --git a/src/fits/GFitsTable.cpp b/src/fits/GFitsTable.cpp index 22ca7c5..3557150 100644 --- a/src/fits/GFitsTable.cpp +++ b/src/fits/GFitsTable.cpp @@ -17,6 +17,7 @@ #include "GException.hpp" #include "GTools.hpp" #include "GFitsTable.hpp" +#include "GFitsTableLogCol.hpp" #include "GFitsTableStrCol.hpp" #include "GFitsTableShtCol.hpp" #include "GFitsTableLngCol.hpp" @@ -504,6 +505,9 @@ void GFitsTable::open(__fitsfile* fptr) // Allocate column switch (typecode) { + case __TLOGICAL: + m_columns[i] = new GFitsTableLogCol(); + break; case __TSTRING: m_columns[i] = new GFitsTableStrCol(); break; @@ -920,6 +924,10 @@ ostream& operator<< (ostream& os, const GFitsTable& table) for (int i = 0; i < table.m_cols; ++i) { if (table.m_columns[i] != NULL) { switch (table.m_columns[i]->type()) { + case __TLOGICAL: + os << " " << *((GFitsTableLogCol*)table.m_columns[i]) + << endl; + break; case __TSTRING: os << " " << *((GFitsTableStrCol*)table.m_columns[i]) << endl; diff --git a/src/fits/GFitsTableLogCol.cpp b/src/fits/GFitsTableLogCol.cpp new file mode 100644 index 0000000..234f41b --- /dev/null +++ b/src/fits/GFitsTableLogCol.cpp @@ -0,0 +1,563 @@ +/*************************************************************************** + * GFitsTableLogCol.cpp - FITS table logical column class * + * ----------------------------------------------------------------------- * + * copyright : (C) 2008 by Jurgen Knodlseder * + * ----------------------------------------------------------------------- * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * ----------------------------------------------------------------------- * + ***************************************************************************/ + +/* __ Includes ___________________________________________________________ */ +#include +#include "GException.hpp" +#include "GTools.hpp" +#include "GFitsTableLogCol.hpp" + +/* __ Namespaces _________________________________________________________ */ + +/* __ Method name definitions ____________________________________________ */ +#define G_STRING "GFitsTableLogCol::string(const int&, const int&)" +#define G_REAL "GFitsTableLogCol::real(const int&, const int&)" +#define G_INTEGER "GFitsTableLogCol::integer(const int&, const int&)" + +/* __ Macros _____________________________________________________________ */ + +/* __ Coding definitions _________________________________________________ */ + +/* __ Debug definitions __________________________________________________ */ + + +/*========================================================================== + = = + = GFitsTableLogCol constructors/destructors = + = = + ==========================================================================*/ + +/***********************************************************************//** + * @brief Constructor + ***************************************************************************/ +GFitsTableLogCol::GFitsTableLogCol() : GFitsTableCol() +{ + // Initialise class members for clean destruction + init_members(); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Constructor + * + * @param[in] name Name of column. + * @param[in] length Length of column. + * @param[in] size Vector size of column. + ***************************************************************************/ +GFitsTableLogCol::GFitsTableLogCol(const std::string& name, + const int& length, + const int& size) + : GFitsTableCol(name, length, size, 2) +{ + // Initialise class members for clean destruction + init_members(); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Copy constructor + * + * @param[in] column Column from which class instance should be built. + ***************************************************************************/ +GFitsTableLogCol::GFitsTableLogCol(const GFitsTableLogCol& column) + : GFitsTableCol(column) +{ + // Initialise class members for clean destruction + init_members(); + + // Copy members + copy_members(column); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Destructor + ***************************************************************************/ +GFitsTableLogCol::~GFitsTableLogCol() +{ + // Free members + free_members(); + + // Return + return; +} + + +/*========================================================================== + = = + = GFitsTableLogCol operators = + = = + ==========================================================================*/ + +/***********************************************************************//** + * @brief Assignment operator + * + * @param[in] column Column which should be assigned + ***************************************************************************/ +GFitsTableLogCol& GFitsTableLogCol::operator= (const GFitsTableLogCol& column) +{ + // Execute only if object is not identical + if (this != &column) { + + // Copy base class members + this->GFitsTableCol::operator=(column); + + // Free members + free_members(); + + // Initialise private members for clean destruction + init_members(); + + // Copy members + copy_members(column); + + } // endif: object was not identical + + // Return this object + return *this; +} + + +/***********************************************************************//** + * @brief Column data access operator + * + * @param[in] row Row of column to access. + * @param[in] inx Vector index in column row to access + * + * Provides access to data in a column. No range checking is performed. + * Use one of + * GFitsTableLogCol::string(ix,iy), + * GFitsTableLogCol::real(ix;iy) or + * GFitsTableLogCol::integer(ix;iy) + * if range checking is required. + ***************************************************************************/ +char& GFitsTableLogCol::operator() (const int& row, const int& inx) +{ + // If data are not available then load them now + if (m_data == NULL) fetch_data(); + + // Calculate pixel offset + int offset = row * m_number + inx; + + // Return data bin + return m_data[offset]; +} + + +/***********************************************************************//** + * @brief Column data access operator (const variant) + * + * @param[in] row Row of column to access. + * @param[in] inx Vector index in column row to access + * + * Provides access to data in a column. No range checking is performed. + * Use one of + * GFitsTableLogCol::string(ix,iy), + * GFitsTableLogCol::real(ix;iy) or + * GFitsTableLogCol::integer(ix;iy) + * if range checking is required. + ***************************************************************************/ +const char& GFitsTableLogCol::operator() (const int& row, const int& inx) + const +{ + // If data are not available then load them now + if (m_data == NULL) ((GFitsTableLogCol*)this)->fetch_data(); + + // Calculate pixel offset + int offset = row * m_number + inx; + + // Return data bin + return m_data[offset]; +} + + +/*========================================================================== + = = + = GFitsTableLogCol public methods = + = = + ==========================================================================*/ + +/***********************************************************************//** + * @brief Get string value + * + * @param[in] row Table row. + * @param[in] inx Table column vector index. + * + * @exception GException::out_of_range + * Table row or vector index are out of valid range. + * + * Returns value of specified row and vector index as string. + ***************************************************************************/ +std::string GFitsTableLogCol::string(const int& row, const int& inx) +{ + // If data are not available then load them now + if (m_data == NULL) fetch_data(); + + // Check row value + if (row < 0 || row >= m_length) + throw GException::out_of_range(G_STRING, row, 0, m_length-1); + + // Check inx value + if (inx < 0 || inx >= m_number) + throw GException::out_of_range(G_STRING, inx, 0, m_number-1); + + // Get index + int offset = row * m_number + inx; + + // Convert logical into string + std::string result = (m_data[offset]) ? "T" : "F"; + + // Return value + return result; +} + + +/***********************************************************************//** + * @brief Get double precision value + * + * @param[in] row Table row. + * @param[in] inx Table column vector index. + * + * @exception GException::out_of_range + * Table row or vector index are out of valid range. + * + * Returns value of specified row and vector index as double precision. + ***************************************************************************/ +double GFitsTableLogCol::real(const int& row, const int& inx) +{ + // If data are not available then load them now + if (m_data == NULL) fetch_data(); + + // Check row value + if (row < 0 || row >= m_length) + throw GException::out_of_range(G_REAL, row, 0, m_length-1); + + // Check inx value + if (inx < 0 || inx >= m_number) + throw GException::out_of_range(G_REAL, inx, 0, m_number-1); + + // Get index + int offset = row * m_number + inx; + + // Convert logcial into double + double value = (double)m_data[offset]; + + // Return value + return value; +} + + +/***********************************************************************//** + * @brief Get integer value + * + * @param[in] row Table row. + * @param[in] inx Table column vector index. + * + * @exception GException::out_of_range + * Table row or vector index are out of valid range. + * + * Returns value of specified row and vector index as integer. + ***************************************************************************/ +int GFitsTableLogCol::integer(const int& row, const int& inx) +{ + // If data are not available then load them now + if (m_data == NULL) fetch_data(); + + // Check row value + if (row < 0 || row >= m_length) + throw GException::out_of_range(G_INTEGER, row, 0, m_length-1); + + // Check col value + if (inx < 0 || inx >= m_number) + throw GException::out_of_range(G_INTEGER, inx, 0, m_number-1); + + // Get index + int offset = row * m_number + inx; + + // Convert logical into int + int value = (int)m_data[offset]; + + // Return value + return value; +} + + +/***********************************************************************//** + * @brief Return pointer to logical column + ***************************************************************************/ +char* GFitsTableLogCol::data(void) +{ + return m_data; +} + + +/***********************************************************************//** + * @brief Set NULL value + * + * @param[in] value Pointer on NULL value + * + * Allows the specification of the FITS table NULL value. If value=NULL the + * data will not be screened for NULL values. + ***************************************************************************/ +void GFitsTableLogCol::set_nullval(const char* value) +{ + // If NULL value is empty then reset the NULL value + if (value == NULL) { + if (m_nulval != NULL) delete m_nulval; + m_nulval = NULL; + } + + // ... otherwise copy value into NULL value + else { + if (m_nulval != NULL) delete m_nulval; + m_nulval = new char; + *m_nulval = *value; + } + + // Re-load column +/* + if (m_data != NULL) { + save(); + load(); + } +*/ + // Return + return; +} + + +/*========================================================================== + = = + = GFitsTableLogCol private methods = + = = + ==========================================================================*/ + +/***********************************************************************//** + * @brief Initialise class members + ***************************************************************************/ +void GFitsTableLogCol::init_members(void) +{ + // Initialise members + m_type = __TLOGICAL; + m_data = NULL; + m_nulval = NULL; + + // Return + return; +} + + +/***********************************************************************//** + * @brief Copy class members + * + * @param[in] column Column for which members should be copied. + ***************************************************************************/ +void GFitsTableLogCol::copy_members(const GFitsTableLogCol& column) +{ + // Copy attributes + + // Copy column data + if (column.m_data != NULL && m_size > 0) { + if (m_data != NULL) delete [] m_data; + m_data = new char[m_size]; + memcpy(m_data, column.m_data, m_size*sizeof(char)); + } + + // Copy NULL value + if (column.m_nulval != NULL) { + if (m_nulval != NULL) delete m_nulval; + m_nulval = new char; + *m_nulval = *column.m_nulval; + } + + // Return + return; +} + + +/***********************************************************************//** + * @brief Delete class members + ***************************************************************************/ +void GFitsTableLogCol::free_members(void) +{ + // Free memory + if (m_data != NULL) delete [] m_data; + if (m_nulval != NULL) delete m_nulval; + + // Mark memory as freed + m_data = NULL; + m_nulval = NULL; + + // Return + return; +} + + +/***********************************************************************//** + * @brief Save table column into FITS file + * + * The table column is only saved if it is linked to a FITS file and if the + * data are indeed present in the class instance. This avoids saving of data + * that have not been modified. + * + * Refer to GFitsTableCol::save_column() for more information. + ***************************************************************************/ +void GFitsTableLogCol::save(void) +{ + // Save column + save_column(); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Clone column + ***************************************************************************/ +GFitsTableLogCol* GFitsTableLogCol::clone(void) const +{ + return new GFitsTableLogCol(*this); +} + + +/***********************************************************************//** + * @brief Returns format string of ASCII table + ***************************************************************************/ +std::string GFitsTableLogCol::ascii_format(void) const +{ + // Initialize format string + std::string format; + + // Set type code + format.append("L"); + + // Set width + format.append(str(m_width)); + + // Return format + return format; +} + + +/***********************************************************************//** + * @brief Returns format string of binary table + ***************************************************************************/ +std::string GFitsTableLogCol::binary_format(void) const +{ + // Initialize format string + std::string format; + + // Set number of elements + format.append(str(m_number)); + + // Set type code + format.append("L"); + + // Return format + return format; +} + + +/***********************************************************************//** + * @brief Allocates column data + ***************************************************************************/ +void GFitsTableLogCol::alloc_data(void) +{ + // Free any existing memory + if (m_data != NULL) delete [] m_data; + + // Mark pointer as free + m_data = NULL; + + // Allocate new data + if (m_size > 0) + m_data = new char[m_size]; + + // Return + return; +} + + +/***********************************************************************//** + * @brief Initialise column data + ***************************************************************************/ +void GFitsTableLogCol::init_data(void) +{ + // Initialise data if they exist + if (m_data != NULL) { + for (int i = 0; i < m_size; ++i) + m_data[i] = 0; + } + + // Return + return; +} + + +/***********************************************************************//** + * @brief Fetch column data + * + * If a FITS file is attached to the column the data are loaded into memory + * from the FITS file. If no FITS file is attached, memory is allocated + * to hold the column data and all cells are set to 0. + * + * Refer to GFitsTableCol::load_column for more information. + ***************************************************************************/ +void GFitsTableLogCol::fetch_data(void) +{ + // Save column + load_column(); + + // Return + return; +} + + +/*========================================================================== + = = + = GFitsTableLogCol friends = + = = + ==========================================================================*/ + +/***********************************************************************//** + * @brief Output operator + * + * @param[in] os Output stream + * @param[in] column Column to put in output stream + ***************************************************************************/ +ostream& operator<< (ostream& os, const GFitsTableLogCol& column) +{ + // Dump column in output stream + column.dump_column(os, column.m_data); + + // Return output stream + return os; +} + + +/*========================================================================== + = = + = Other functions used by GFitsTableLogCol = + = = + ==========================================================================*/ diff --git a/test/test_GFits.cpp b/test/test_GFits.cpp index 9ddccca..8813101 100644 --- a/test/test_GFits.cpp +++ b/test/test_GFits.cpp @@ -2040,6 +2040,367 @@ void test_bintable_string(void) /*************************************************************************** + * Test: FITS binary table test * + ***************************************************************************/ +void test_bintable_logical(void) +{ + // Set filename + std::string filename = "test_bintable_logical.fits"; + + // Dump header + cout << "Test GFitsTableLogCol: "; + + // Remove FITS file + std::string cmd = "rm -rf "+ filename; + system(cmd.c_str()); + + // Allocate reference sums + int sum = 0; + int sum10 = 0; + int sum_int = 0; + int sum10_int = 0; + std::string sum_str; + std::string sum10_str; + + // Build tables + try { + // Open FITS file + GFits fits; + fits.open(filename); + + // Set number of rows + int nrows = 10; + int nvec = 10; + + // Initial control sums + float tot_flt = 0.0; + double tot_dbl = 0.0; + int tot_int = 0; + long tot_lng = 0; + std::string tot_str; + + // + // ===== L O G I C A L ===== + // + + // Set table + GFitsTableLogCol col = GFitsTableLogCol("LOGICAL", nrows); + for (int i = 0; i < nrows; ++i) { + char val = (i % 2) ? 1 : 0; + col(i) = val; + sum += val; + sum_int += int(val); + if (val) + sum_str += ":T"; + else + sum_str += ":F"; + } + cout << "."; + + // Check table (operator access) + tot_int = 0; + for (int i = 0; i < nrows; ++i) + tot_int += col(i); + if (tot_int != sum) { + cout << endl << "TEST ERROR: GFitsTableLogCol::operator()"; + cout << endl << " Reference sum: " << sum; + cout << endl << " Derived sum: " << tot_int << endl; + throw; + } + cout << "."; + + // Check table (real access) + tot_dbl = 0.0; + for (int i = 0; i < nrows; ++i) + tot_dbl += col.real(i); + if (!dequal(tot_dbl, double(sum))) { + cout << endl << "TEST ERROR: GFitsTableLogCol::real()"; + cout << endl << " Reference sum: " << sum; + cout << endl << " Derived sum: " << tot_dbl << endl; + throw; + } + cout << "."; + + // Check table (int access) + tot_int = 0; + for (int i = 0; i < nrows; ++i) + tot_int += col.integer(i); + if (tot_int != sum_int) { + cout << endl << "TEST ERROR: GFitsTableLogCol::integer()"; + cout << endl << " Reference sum: " << sum_int; + cout << endl << " Derived sum: " << tot_int << endl; + throw; + } + cout << "."; + + // Check table (string access) + tot_str.clear(); + for (int i = 0; i < nrows; ++i) + tot_str += ":"+col.string(i); + if (tot_str != sum_str) { + cout << endl << "TEST ERROR: GFitsTableLogCol::string()"; + cout << endl << " Reference sum: " << sum_str; + cout << endl << " Derived sum: " << tot_str << endl; + throw; + } + cout << "."; + + // + // ===== L O G I C A L 1 0 ===== + // + + // Set table + GFitsTableLogCol col10 = GFitsTableLogCol("LOGICAL10", nrows, nvec); + for (int i = 0; i < nrows; ++i) { + for (int j = 0; j < nvec; ++j) { + char val = (i % 2) * (j % 2); + col10(i,j) = val; + sum10 += val; + sum10_int += int(val); + if (val) + sum10_str += ":T"; + else + sum10_str += ":F"; + } + } + cout << "."; + + // Check table (operator access) + tot_int = 0; + for (int i = 0; i < nrows; ++i) { + for (int j = 0; j < nvec; ++j) + tot_int += col10(i,j); + } + if (tot_int != sum10) { + cout << endl << "TEST ERROR: GFitsTableLogCol::operator() - 10"; + cout << endl << " Reference sum: " << sum10; + cout << endl << " Derived sum: " << tot_int << endl; + throw; + } + cout << "."; + + // Check table (real access) + tot_dbl = 0.0; + for (int i = 0; i < nrows; ++i) { + for (int j = 0; j < nvec; ++j) + tot_dbl += col10.real(i,j); + } + if (!dequal(tot_dbl, sum10)) { + cout << endl << "TEST ERROR: GFitsTableLogCol::real() - 10"; + cout << endl << " Reference sum: " << sum10; + cout << endl << " Derived sum: " << tot_dbl << endl; + throw; + } + cout << "."; + + // Check table (int access) + tot_int = 0; + for (int i = 0; i < nrows; ++i) { + for (int j = 0; j < nvec; ++j) + tot_int += col10.integer(i,j); + } + if (tot_int != sum10_int) { + cout << endl << "TEST ERROR: GFitsTableLogCol::integer() - 10"; + cout << endl << " Reference sum: " << sum10_int; + cout << endl << " Derived sum: " << tot_int << endl; + throw; + } + cout << "."; + + // Check table (string access) + tot_str.clear(); + for (int i = 0; i < nrows; ++i) { + for (int j = 0; j < nvec; ++j) + tot_str += ":"+col10.string(i,j); + } + if (tot_str != sum10_str) { + cout << endl << "TEST ERROR: GFitsTableLogCol::string() - 10"; + cout << endl << " Reference sum: " << sum10_str; + cout << endl << " Derived sum: " << tot_str << endl; + throw; + } + cout << "."; + + // + // ===== W R I T E T A B L E ===== + // + + // Build binary table + GFitsBinTable table = GFitsBinTable(nrows); + table.append_column(col); + table.insert_column(0, col10); + + // Create HDU and append to FILE file + GFitsHDU hdu(table); + fits.append_hdu(hdu); + + // Save FITS file + fits.save(); + cout << "."; + + } + catch (exception &e) { + cout << endl << "TEST ERROR: Unable to build tables." << endl; + cout << e.what() << endl; + throw; + } + cout << "."; + + + //================================================================ + //================================================================ + //================================================================ + // Read tables + //================================================================ + //================================================================ + //================================================================ + try { + // Open FITS file + GFits fits; + fits.open(filename); + + // Set number of rows + int nrows = 10; + int nvec = 10; + + // Initial control sums + float tot_flt = 0.0; + short tot_sht = 0; + double tot_dbl = 0.0; + int tot_int = 0; + std::string tot_str; + + // + // ===== L O G I C A L ===== + // + + // Get column + GFitsTableLogCol* col = + (GFitsTableLogCol*)fits.hdu(2)->column("LOGICAL"); + + // Check table (operator access) + tot_int = 0; + for (int i = 0; i < nrows; ++i) + tot_int += (*col)(i); + if (tot_int != sum) { + cout << endl << "TEST ERROR: GFitsTableLogCol::operator()"; + cout << endl << " Reference sum: " << sum; + cout << endl << " Derived sum: " << tot_int << endl; + throw; + } + cout << "."; + + // Check table (real access) + tot_dbl = 0.0; + for (int i = 0; i < nrows; ++i) + tot_dbl += col->real(i); + if (!dequal(tot_dbl, double(sum))) { + cout << endl << "TEST ERROR: GFitsTableLogCol::real()"; + cout << endl << " Reference sum: " << sum; + cout << endl << " Derived sum: " << tot_dbl << endl; + throw; + } + cout << "."; + + // Check table (int access) + tot_int = 0; + for (int i = 0; i < nrows; ++i) + tot_int += col->integer(i); + if (tot_int != sum_int) { + cout << endl << "TEST ERROR: GFitsTableLogCol::integer()"; + cout << endl << " Reference sum: " << sum_int; + cout << endl << " Derived sum: " << tot_int << endl; + throw; + } + cout << "."; + + // Check single precision table (string access) + tot_str.clear(); + for (int i = 0; i < nrows; ++i) + tot_str += ":"+col->string(i); + if (tot_str != sum_str) { + cout << endl << "TEST ERROR: GFitsTableLogCol::string()"; + cout << endl << " Reference sum: " << sum_str; + cout << endl << " Derived sum: " << tot_str << endl; + throw; + } + cout << "."; + + // + // ===== L O G I C A L 1 0 ===== + // + + // Get column + GFitsTableLogCol* col10 = + (GFitsTableLogCol*)fits.hdu(2)->column("LOGICAL10"); + + // Check table (operator access) + tot_int = 0; + for (int i = 0; i < nrows; ++i) { + for (int j = 0; j < nvec; ++j) + tot_int += (*col10)(i,j); + } + if (tot_int != sum10) { + cout << endl << "TEST ERROR: GFitsTableLogCol::operator() - 10"; + cout << endl << " Reference sum: " << sum10; + cout << endl << " Derived sum: " << tot_int << endl; + throw; + } + cout << "."; + + // Check table (real access) + tot_dbl = 0.0; + for (int i = 0; i < nrows; ++i) { + for (int j = 0; j < nvec; ++j) + tot_dbl += col10->real(i,j); + } + if (!dequal(tot_dbl, sum10)) { + cout << endl << "TEST ERROR: GFitsTableLogCol::real() - 10"; + cout << endl << " Reference sum: " << sum10; + cout << endl << " Derived sum: " << tot_dbl << endl; + throw; + } + cout << "."; + + // Check table (int access) + tot_int = 0; + for (int i = 0; i < nrows; ++i) { + for (int j = 0; j < nvec; ++j) + tot_int += col10->integer(i,j); + } + if (tot_int != sum10_int) { + cout << endl << "TEST ERROR: GFitsTableLogCol::integer() - 10"; + cout << endl << " Reference sum: " << sum10_int; + cout << endl << " Derived sum: " << tot_int << endl; + throw; + } + cout << "."; + + // Check single precision table (string access) + tot_str.clear(); + for (int i = 0; i < nrows; ++i) { + for (int j = 0; j < nvec; ++j) + tot_str += ":"+col10->string(i,j); + } + if (tot_str != sum10_str) { + cout << endl << "TEST ERROR: GFitsTableLogCol::string() - 10"; + cout << endl << " Reference sum: " << sum10_str; + cout << endl << " Derived sum: " << tot_str << endl; + throw; + } + cout << "."; + } + catch (exception &e) { + cout << endl << "TEST ERROR: Unable to build tables." << endl; + cout << e.what() << endl; + throw; + } + cout << ". ok." << endl; + +} + + +/*************************************************************************** * Main test function * ***************************************************************************/ int main(void) @@ -2058,10 +2419,8 @@ int main(void) test_bintable_short(); test_bintable_long(); test_bintable_string(); + test_bintable_logical(); - //test_open(); - //test_columns(); - // Return return 0; } -- libgit2 0.21.4