Commit 070fb313bd341cbba613e076abb6f768f65db95f

Authored by knodlseder
1 parent a17dbbd9

add logical FITS table column

include/GFitsTableLogCol.hpp 0 → 100644
  1 +/***************************************************************************
  2 + * GFitsTableLogCol.hpp - FITS table logical column class *
  3 + * ----------------------------------------------------------------------- *
  4 + * copyright : (C) 2008 by Jurgen Knodlseder *
  5 + * ----------------------------------------------------------------------- *
  6 + * *
  7 + * This program is free software; you can redistribute it and/or modify *
  8 + * it under the terms of the GNU General Public License as published by *
  9 + * the Free Software Foundation; either version 2 of the License, or *
  10 + * (at your option) any later version. *
  11 + * *
  12 + ***************************************************************************/
  13 +/**
  14 + * @file GFitsTableLogCol.hpp
  15 + * @brief GFitsTableLogCol class definition.
  16 + * @author J. Knodlseder
  17 + */
  18 +
  19 +#ifndef GFITSTABLELOGCOL_HPP
  20 +#define GFITSTABLELOGCOL_HPP
  21 +
  22 +/* __ Includes ___________________________________________________________ */
  23 +#include "GFitsCfitsio.hpp"
  24 +#include "GFitsTableCol.hpp"
  25 +
  26 +/* __ Namespaces _________________________________________________________ */
  27 +
  28 +
  29 +/* __ Structures _________________________________________________________ */
  30 +
  31 +
  32 +/***********************************************************************//**
  33 + * @class GFitsTableLogCol
  34 + *
  35 + * @brief Interface for FITS table logical column
  36 + *
  37 + * This class implements a FITS table logical column.
  38 + ***************************************************************************/
  39 +class GFitsTableLogCol : public GFitsTableCol {
  40 +
  41 + // I/O friends
  42 + friend ostream& operator<< (ostream& os, const GFitsTableLogCol& column);
  43 +
  44 +public:
  45 + // Constructors and destructors
  46 + GFitsTableLogCol();
  47 + GFitsTableLogCol(const std::string& name, const int& length,
  48 + const int& size = 1);
  49 + GFitsTableLogCol(const GFitsTableLogCol& column);
  50 + virtual ~GFitsTableLogCol();
  51 +
  52 + // Operators
  53 + GFitsTableLogCol& operator= (const GFitsTableLogCol& column);
  54 + char& operator() (const int& row, const int& inx = 0);
  55 + const char& operator() (const int& row, const int& inx = 0) const;
  56 +
  57 + // Methods
  58 + std::string string(const int& row, const int& col = 0);
  59 + double real(const int& row, const int& col = 0);
  60 + int integer(const int& row, const int& col = 0);
  61 + char* data(void);
  62 + void set_nullval(const char* value);
  63 +
  64 +private:
  65 + // Private methods
  66 + void init_members(void);
  67 + void copy_members(const GFitsTableLogCol& column);
  68 + void free_members(void);
  69 + void save(void);
  70 + GFitsTableLogCol* clone(void) const;
  71 + std::string ascii_format(void) const;
  72 + std::string binary_format(void) const;
  73 + void alloc_data(void);
  74 + void init_data(void);
  75 + void fetch_data(void);
  76 + void* ptr_data(void) { return m_data; }
  77 + void* ptr_nulval(void) { return m_nulval; }
  78 +
  79 + // Private data area
  80 + char* m_data; //!< Data area
  81 + char* m_nulval; //!< NULL value
  82 +};
  83 +
  84 +#endif /* GFITSTABLELOGCOL_HPP */
... ...
include/GammaLib.hpp
... ... @@ -38,6 +38,7 @@
38 38 #include "GFitsAsciiTable.hpp"
39 39 #include "GFitsBinTable.hpp"
40 40 #include "GFitsTableCol.hpp"
  41 +#include "GFitsTableLogCol.hpp"
41 42 #include "GFitsTableStrCol.hpp"
42 43 #include "GFitsTableShtCol.hpp"
43 44 #include "GFitsTableLngCol.hpp"
... ...
src/fits/GFitsTable.cpp
... ... @@ -17,6 +17,7 @@
17 17 #include "GException.hpp"
18 18 #include "GTools.hpp"
19 19 #include "GFitsTable.hpp"
  20 +#include "GFitsTableLogCol.hpp"
20 21 #include "GFitsTableStrCol.hpp"
21 22 #include "GFitsTableShtCol.hpp"
22 23 #include "GFitsTableLngCol.hpp"
... ... @@ -504,6 +505,9 @@ void GFitsTable::open(__fitsfile* fptr)
504 505  
505 506 // Allocate column
506 507 switch (typecode) {
  508 + case __TLOGICAL:
  509 + m_columns[i] = new GFitsTableLogCol();
  510 + break;
507 511 case __TSTRING:
508 512 m_columns[i] = new GFitsTableStrCol();
509 513 break;
... ... @@ -920,6 +924,10 @@ ostream&amp; operator&lt;&lt; (ostream&amp; os, const GFitsTable&amp; table)
920 924 for (int i = 0; i < table.m_cols; ++i) {
921 925 if (table.m_columns[i] != NULL) {
922 926 switch (table.m_columns[i]->type()) {
  927 + case __TLOGICAL:
  928 + os << " " << *((GFitsTableLogCol*)table.m_columns[i])
  929 + << endl;
  930 + break;
923 931 case __TSTRING:
924 932 os << " " << *((GFitsTableStrCol*)table.m_columns[i])
925 933 << endl;
... ...
src/fits/GFitsTableLogCol.cpp 0 → 100644
  1 +/***************************************************************************
  2 + * GFitsTableLogCol.cpp - FITS table logical column class *
  3 + * ----------------------------------------------------------------------- *
  4 + * copyright : (C) 2008 by Jurgen Knodlseder *
  5 + * ----------------------------------------------------------------------- *
  6 + * *
  7 + * This program is free software; you can redistribute it and/or modify *
  8 + * it under the terms of the GNU General Public License as published by *
  9 + * the Free Software Foundation; either version 2 of the License, or *
  10 + * (at your option) any later version. *
  11 + * *
  12 + * ----------------------------------------------------------------------- *
  13 + ***************************************************************************/
  14 +
  15 +/* __ Includes ___________________________________________________________ */
  16 +#include <iostream>
  17 +#include "GException.hpp"
  18 +#include "GTools.hpp"
  19 +#include "GFitsTableLogCol.hpp"
  20 +
  21 +/* __ Namespaces _________________________________________________________ */
  22 +
  23 +/* __ Method name definitions ____________________________________________ */
  24 +#define G_STRING "GFitsTableLogCol::string(const int&, const int&)"
  25 +#define G_REAL "GFitsTableLogCol::real(const int&, const int&)"
  26 +#define G_INTEGER "GFitsTableLogCol::integer(const int&, const int&)"
  27 +
  28 +/* __ Macros _____________________________________________________________ */
  29 +
  30 +/* __ Coding definitions _________________________________________________ */
  31 +
  32 +/* __ Debug definitions __________________________________________________ */
  33 +
  34 +
  35 +/*==========================================================================
  36 + = =
  37 + = GFitsTableLogCol constructors/destructors =
  38 + = =
  39 + ==========================================================================*/
  40 +
  41 +/***********************************************************************//**
  42 + * @brief Constructor
  43 + ***************************************************************************/
  44 +GFitsTableLogCol::GFitsTableLogCol() : GFitsTableCol()
  45 +{
  46 + // Initialise class members for clean destruction
  47 + init_members();
  48 +
  49 + // Return
  50 + return;
  51 +}
  52 +
  53 +
  54 +/***********************************************************************//**
  55 + * @brief Constructor
  56 + *
  57 + * @param[in] name Name of column.
  58 + * @param[in] length Length of column.
  59 + * @param[in] size Vector size of column.
  60 + ***************************************************************************/
  61 +GFitsTableLogCol::GFitsTableLogCol(const std::string& name,
  62 + const int& length,
  63 + const int& size)
  64 + : GFitsTableCol(name, length, size, 2)
  65 +{
  66 + // Initialise class members for clean destruction
  67 + init_members();
  68 +
  69 + // Return
  70 + return;
  71 +}
  72 +
  73 +
  74 +/***********************************************************************//**
  75 + * @brief Copy constructor
  76 + *
  77 + * @param[in] column Column from which class instance should be built.
  78 + ***************************************************************************/
  79 +GFitsTableLogCol::GFitsTableLogCol(const GFitsTableLogCol& column)
  80 + : GFitsTableCol(column)
  81 +{
  82 + // Initialise class members for clean destruction
  83 + init_members();
  84 +
  85 + // Copy members
  86 + copy_members(column);
  87 +
  88 + // Return
  89 + return;
  90 +}
  91 +
  92 +
  93 +/***********************************************************************//**
  94 + * @brief Destructor
  95 + ***************************************************************************/
  96 +GFitsTableLogCol::~GFitsTableLogCol()
  97 +{
  98 + // Free members
  99 + free_members();
  100 +
  101 + // Return
  102 + return;
  103 +}
  104 +
  105 +
  106 +/*==========================================================================
  107 + = =
  108 + = GFitsTableLogCol operators =
  109 + = =
  110 + ==========================================================================*/
  111 +
  112 +/***********************************************************************//**
  113 + * @brief Assignment operator
  114 + *
  115 + * @param[in] column Column which should be assigned
  116 + ***************************************************************************/
  117 +GFitsTableLogCol& GFitsTableLogCol::operator= (const GFitsTableLogCol& column)
  118 +{
  119 + // Execute only if object is not identical
  120 + if (this != &column) {
  121 +
  122 + // Copy base class members
  123 + this->GFitsTableCol::operator=(column);
  124 +
  125 + // Free members
  126 + free_members();
  127 +
  128 + // Initialise private members for clean destruction
  129 + init_members();
  130 +
  131 + // Copy members
  132 + copy_members(column);
  133 +
  134 + } // endif: object was not identical
  135 +
  136 + // Return this object
  137 + return *this;
  138 +}
  139 +
  140 +
  141 +/***********************************************************************//**
  142 + * @brief Column data access operator
  143 + *
  144 + * @param[in] row Row of column to access.
  145 + * @param[in] inx Vector index in column row to access
  146 + *
  147 + * Provides access to data in a column. No range checking is performed.
  148 + * Use one of
  149 + * GFitsTableLogCol::string(ix,iy),
  150 + * GFitsTableLogCol::real(ix;iy) or
  151 + * GFitsTableLogCol::integer(ix;iy)
  152 + * if range checking is required.
  153 + ***************************************************************************/
  154 +char& GFitsTableLogCol::operator() (const int& row, const int& inx)
  155 +{
  156 + // If data are not available then load them now
  157 + if (m_data == NULL) fetch_data();
  158 +
  159 + // Calculate pixel offset
  160 + int offset = row * m_number + inx;
  161 +
  162 + // Return data bin
  163 + return m_data[offset];
  164 +}
  165 +
  166 +
  167 +/***********************************************************************//**
  168 + * @brief Column data access operator (const variant)
  169 + *
  170 + * @param[in] row Row of column to access.
  171 + * @param[in] inx Vector index in column row to access
  172 + *
  173 + * Provides access to data in a column. No range checking is performed.
  174 + * Use one of
  175 + * GFitsTableLogCol::string(ix,iy),
  176 + * GFitsTableLogCol::real(ix;iy) or
  177 + * GFitsTableLogCol::integer(ix;iy)
  178 + * if range checking is required.
  179 + ***************************************************************************/
  180 +const char& GFitsTableLogCol::operator() (const int& row, const int& inx)
  181 + const
  182 +{
  183 + // If data are not available then load them now
  184 + if (m_data == NULL) ((GFitsTableLogCol*)this)->fetch_data();
  185 +
  186 + // Calculate pixel offset
  187 + int offset = row * m_number + inx;
  188 +
  189 + // Return data bin
  190 + return m_data[offset];
  191 +}
  192 +
  193 +
  194 +/*==========================================================================
  195 + = =
  196 + = GFitsTableLogCol public methods =
  197 + = =
  198 + ==========================================================================*/
  199 +
  200 +/***********************************************************************//**
  201 + * @brief Get string value
  202 + *
  203 + * @param[in] row Table row.
  204 + * @param[in] inx Table column vector index.
  205 + *
  206 + * @exception GException::out_of_range
  207 + * Table row or vector index are out of valid range.
  208 + *
  209 + * Returns value of specified row and vector index as string.
  210 + ***************************************************************************/
  211 +std::string GFitsTableLogCol::string(const int& row, const int& inx)
  212 +{
  213 + // If data are not available then load them now
  214 + if (m_data == NULL) fetch_data();
  215 +
  216 + // Check row value
  217 + if (row < 0 || row >= m_length)
  218 + throw GException::out_of_range(G_STRING, row, 0, m_length-1);
  219 +
  220 + // Check inx value
  221 + if (inx < 0 || inx >= m_number)
  222 + throw GException::out_of_range(G_STRING, inx, 0, m_number-1);
  223 +
  224 + // Get index
  225 + int offset = row * m_number + inx;
  226 +
  227 + // Convert logical into string
  228 + std::string result = (m_data[offset]) ? "T" : "F";
  229 +
  230 + // Return value
  231 + return result;
  232 +}
  233 +
  234 +
  235 +/***********************************************************************//**
  236 + * @brief Get double precision value
  237 + *
  238 + * @param[in] row Table row.
  239 + * @param[in] inx Table column vector index.
  240 + *
  241 + * @exception GException::out_of_range
  242 + * Table row or vector index are out of valid range.
  243 + *
  244 + * Returns value of specified row and vector index as double precision.
  245 + ***************************************************************************/
  246 +double GFitsTableLogCol::real(const int& row, const int& inx)
  247 +{
  248 + // If data are not available then load them now
  249 + if (m_data == NULL) fetch_data();
  250 +
  251 + // Check row value
  252 + if (row < 0 || row >= m_length)
  253 + throw GException::out_of_range(G_REAL, row, 0, m_length-1);
  254 +
  255 + // Check inx value
  256 + if (inx < 0 || inx >= m_number)
  257 + throw GException::out_of_range(G_REAL, inx, 0, m_number-1);
  258 +
  259 + // Get index
  260 + int offset = row * m_number + inx;
  261 +
  262 + // Convert logcial into double
  263 + double value = (double)m_data[offset];
  264 +
  265 + // Return value
  266 + return value;
  267 +}
  268 +
  269 +
  270 +/***********************************************************************//**
  271 + * @brief Get integer value
  272 + *
  273 + * @param[in] row Table row.
  274 + * @param[in] inx Table column vector index.
  275 + *
  276 + * @exception GException::out_of_range
  277 + * Table row or vector index are out of valid range.
  278 + *
  279 + * Returns value of specified row and vector index as integer.
  280 + ***************************************************************************/
  281 +int GFitsTableLogCol::integer(const int& row, const int& inx)
  282 +{
  283 + // If data are not available then load them now
  284 + if (m_data == NULL) fetch_data();
  285 +
  286 + // Check row value
  287 + if (row < 0 || row >= m_length)
  288 + throw GException::out_of_range(G_INTEGER, row, 0, m_length-1);
  289 +
  290 + // Check col value
  291 + if (inx < 0 || inx >= m_number)
  292 + throw GException::out_of_range(G_INTEGER, inx, 0, m_number-1);
  293 +
  294 + // Get index
  295 + int offset = row * m_number + inx;
  296 +
  297 + // Convert logical into int
  298 + int value = (int)m_data[offset];
  299 +
  300 + // Return value
  301 + return value;
  302 +}
  303 +
  304 +
  305 +/***********************************************************************//**
  306 + * @brief Return pointer to logical column
  307 + ***************************************************************************/
  308 +char* GFitsTableLogCol::data(void)
  309 +{
  310 + return m_data;
  311 +}
  312 +
  313 +
  314 +/***********************************************************************//**
  315 + * @brief Set NULL value
  316 + *
  317 + * @param[in] value Pointer on NULL value
  318 + *
  319 + * Allows the specification of the FITS table NULL value. If value=NULL the
  320 + * data will not be screened for NULL values.
  321 + ***************************************************************************/
  322 +void GFitsTableLogCol::set_nullval(const char* value)
  323 +{
  324 + // If NULL value is empty then reset the NULL value
  325 + if (value == NULL) {
  326 + if (m_nulval != NULL) delete m_nulval;
  327 + m_nulval = NULL;
  328 + }
  329 +
  330 + // ... otherwise copy value into NULL value
  331 + else {
  332 + if (m_nulval != NULL) delete m_nulval;
  333 + m_nulval = new char;
  334 + *m_nulval = *value;
  335 + }
  336 +
  337 + // Re-load column
  338 +/*
  339 + if (m_data != NULL) {
  340 + save();
  341 + load();
  342 + }
  343 +*/
  344 + // Return
  345 + return;
  346 +}
  347 +
  348 +
  349 +/*==========================================================================
  350 + = =
  351 + = GFitsTableLogCol private methods =
  352 + = =
  353 + ==========================================================================*/
  354 +
  355 +/***********************************************************************//**
  356 + * @brief Initialise class members
  357 + ***************************************************************************/
  358 +void GFitsTableLogCol::init_members(void)
  359 +{
  360 + // Initialise members
  361 + m_type = __TLOGICAL;
  362 + m_data = NULL;
  363 + m_nulval = NULL;
  364 +
  365 + // Return
  366 + return;
  367 +}
  368 +
  369 +
  370 +/***********************************************************************//**
  371 + * @brief Copy class members
  372 + *
  373 + * @param[in] column Column for which members should be copied.
  374 + ***************************************************************************/
  375 +void GFitsTableLogCol::copy_members(const GFitsTableLogCol& column)
  376 +{
  377 + // Copy attributes
  378 +
  379 + // Copy column data
  380 + if (column.m_data != NULL && m_size > 0) {
  381 + if (m_data != NULL) delete [] m_data;
  382 + m_data = new char[m_size];
  383 + memcpy(m_data, column.m_data, m_size*sizeof(char));
  384 + }
  385 +
  386 + // Copy NULL value
  387 + if (column.m_nulval != NULL) {
  388 + if (m_nulval != NULL) delete m_nulval;
  389 + m_nulval = new char;
  390 + *m_nulval = *column.m_nulval;
  391 + }
  392 +
  393 + // Return
  394 + return;
  395 +}
  396 +
  397 +
  398 +/***********************************************************************//**
  399 + * @brief Delete class members
  400 + ***************************************************************************/
  401 +void GFitsTableLogCol::free_members(void)
  402 +{
  403 + // Free memory
  404 + if (m_data != NULL) delete [] m_data;
  405 + if (m_nulval != NULL) delete m_nulval;
  406 +
  407 + // Mark memory as freed
  408 + m_data = NULL;
  409 + m_nulval = NULL;
  410 +
  411 + // Return
  412 + return;
  413 +}
  414 +
  415 +
  416 +/***********************************************************************//**
  417 + * @brief Save table column into FITS file
  418 + *
  419 + * The table column is only saved if it is linked to a FITS file and if the
  420 + * data are indeed present in the class instance. This avoids saving of data
  421 + * that have not been modified.
  422 + *
  423 + * Refer to GFitsTableCol::save_column() for more information.
  424 + ***************************************************************************/
  425 +void GFitsTableLogCol::save(void)
  426 +{
  427 + // Save column
  428 + save_column();
  429 +
  430 + // Return
  431 + return;
  432 +}
  433 +
  434 +
  435 +/***********************************************************************//**
  436 + * @brief Clone column
  437 + ***************************************************************************/
  438 +GFitsTableLogCol* GFitsTableLogCol::clone(void) const
  439 +{
  440 + return new GFitsTableLogCol(*this);
  441 +}
  442 +
  443 +
  444 +/***********************************************************************//**
  445 + * @brief Returns format string of ASCII table
  446 + ***************************************************************************/
  447 +std::string GFitsTableLogCol::ascii_format(void) const
  448 +{
  449 + // Initialize format string
  450 + std::string format;
  451 +
  452 + // Set type code
  453 + format.append("L");
  454 +
  455 + // Set width
  456 + format.append(str(m_width));
  457 +
  458 + // Return format
  459 + return format;
  460 +}
  461 +
  462 +
  463 +/***********************************************************************//**
  464 + * @brief Returns format string of binary table
  465 + ***************************************************************************/
  466 +std::string GFitsTableLogCol::binary_format(void) const
  467 +{
  468 + // Initialize format string
  469 + std::string format;
  470 +
  471 + // Set number of elements
  472 + format.append(str(m_number));
  473 +
  474 + // Set type code
  475 + format.append("L");
  476 +
  477 + // Return format
  478 + return format;
  479 +}
  480 +
  481 +
  482 +/***********************************************************************//**
  483 + * @brief Allocates column data
  484 + ***************************************************************************/
  485 +void GFitsTableLogCol::alloc_data(void)
  486 +{
  487 + // Free any existing memory
  488 + if (m_data != NULL) delete [] m_data;
  489 +
  490 + // Mark pointer as free
  491 + m_data = NULL;
  492 +
  493 + // Allocate new data
  494 + if (m_size > 0)
  495 + m_data = new char[m_size];
  496 +
  497 + // Return
  498 + return;
  499 +}
  500 +
  501 +
  502 +/***********************************************************************//**
  503 + * @brief Initialise column data
  504 + ***************************************************************************/
  505 +void GFitsTableLogCol::init_data(void)
  506 +{
  507 + // Initialise data if they exist
  508 + if (m_data != NULL) {
  509 + for (int i = 0; i < m_size; ++i)
  510 + m_data[i] = 0;
  511 + }
  512 +
  513 + // Return
  514 + return;
  515 +}
  516 +
  517 +
  518 +/***********************************************************************//**
  519 + * @brief Fetch column data
  520 + *
  521 + * If a FITS file is attached to the column the data are loaded into memory
  522 + * from the FITS file. If no FITS file is attached, memory is allocated
  523 + * to hold the column data and all cells are set to 0.
  524 + *
  525 + * Refer to GFitsTableCol::load_column for more information.
  526 + ***************************************************************************/
  527 +void GFitsTableLogCol::fetch_data(void)
  528 +{
  529 + // Save column
  530 + load_column();
  531 +
  532 + // Return
  533 + return;
  534 +}
  535 +
  536 +
  537 +/*==========================================================================
  538 + = =
  539 + = GFitsTableLogCol friends =
  540 + = =
  541 + ==========================================================================*/
  542 +
  543 +/***********************************************************************//**
  544 + * @brief Output operator
  545 + *
  546 + * @param[in] os Output stream
  547 + * @param[in] column Column to put in output stream
  548 + ***************************************************************************/
  549 +ostream& operator<< (ostream& os, const GFitsTableLogCol& column)
  550 +{
  551 + // Dump column in output stream
  552 + column.dump_column(os, column.m_data);
  553 +
  554 + // Return output stream
  555 + return os;
  556 +}
  557 +
  558 +
  559 +/*==========================================================================
  560 + = =
  561 + = Other functions used by GFitsTableLogCol =
  562 + = =
  563 + ==========================================================================*/
... ...
test/test_GFits.cpp
... ... @@ -2040,6 +2040,367 @@ void test_bintable_string(void)
2040 2040  
2041 2041  
2042 2042 /***************************************************************************
  2043 + * Test: FITS binary table test *
  2044 + ***************************************************************************/
  2045 +void test_bintable_logical(void)
  2046 +{
  2047 + // Set filename
  2048 + std::string filename = "test_bintable_logical.fits";
  2049 +
  2050 + // Dump header
  2051 + cout << "Test GFitsTableLogCol: ";
  2052 +
  2053 + // Remove FITS file
  2054 + std::string cmd = "rm -rf "+ filename;
  2055 + system(cmd.c_str());
  2056 +
  2057 + // Allocate reference sums
  2058 + int sum = 0;
  2059 + int sum10 = 0;
  2060 + int sum_int = 0;
  2061 + int sum10_int = 0;
  2062 + std::string sum_str;
  2063 + std::string sum10_str;
  2064 +
  2065 + // Build tables
  2066 + try {
  2067 + // Open FITS file
  2068 + GFits fits;
  2069 + fits.open(filename);
  2070 +
  2071 + // Set number of rows
  2072 + int nrows = 10;
  2073 + int nvec = 10;
  2074 +
  2075 + // Initial control sums
  2076 + float tot_flt = 0.0;
  2077 + double tot_dbl = 0.0;
  2078 + int tot_int = 0;
  2079 + long tot_lng = 0;
  2080 + std::string tot_str;
  2081 +
  2082 + //
  2083 + // ===== L O G I C A L =====
  2084 + //
  2085 +
  2086 + // Set table
  2087 + GFitsTableLogCol col = GFitsTableLogCol("LOGICAL", nrows);
  2088 + for (int i = 0; i < nrows; ++i) {
  2089 + char val = (i % 2) ? 1 : 0;
  2090 + col(i) = val;
  2091 + sum += val;
  2092 + sum_int += int(val);
  2093 + if (val)
  2094 + sum_str += ":T";
  2095 + else
  2096 + sum_str += ":F";
  2097 + }
  2098 + cout << ".";
  2099 +
  2100 + // Check table (operator access)
  2101 + tot_int = 0;
  2102 + for (int i = 0; i < nrows; ++i)
  2103 + tot_int += col(i);
  2104 + if (tot_int != sum) {
  2105 + cout << endl << "TEST ERROR: GFitsTableLogCol::operator()";
  2106 + cout << endl << " Reference sum: " << sum;
  2107 + cout << endl << " Derived sum: " << tot_int << endl;
  2108 + throw;
  2109 + }
  2110 + cout << ".";
  2111 +
  2112 + // Check table (real access)
  2113 + tot_dbl = 0.0;
  2114 + for (int i = 0; i < nrows; ++i)
  2115 + tot_dbl += col.real(i);
  2116 + if (!dequal(tot_dbl, double(sum))) {
  2117 + cout << endl << "TEST ERROR: GFitsTableLogCol::real()";
  2118 + cout << endl << " Reference sum: " << sum;
  2119 + cout << endl << " Derived sum: " << tot_dbl << endl;
  2120 + throw;
  2121 + }
  2122 + cout << ".";
  2123 +
  2124 + // Check table (int access)
  2125 + tot_int = 0;
  2126 + for (int i = 0; i < nrows; ++i)
  2127 + tot_int += col.integer(i);
  2128 + if (tot_int != sum_int) {
  2129 + cout << endl << "TEST ERROR: GFitsTableLogCol::integer()";
  2130 + cout << endl << " Reference sum: " << sum_int;
  2131 + cout << endl << " Derived sum: " << tot_int << endl;
  2132 + throw;
  2133 + }
  2134 + cout << ".";
  2135 +
  2136 + // Check table (string access)
  2137 + tot_str.clear();
  2138 + for (int i = 0; i < nrows; ++i)
  2139 + tot_str += ":"+col.string(i);
  2140 + if (tot_str != sum_str) {
  2141 + cout << endl << "TEST ERROR: GFitsTableLogCol::string()";
  2142 + cout << endl << " Reference sum: " << sum_str;
  2143 + cout << endl << " Derived sum: " << tot_str << endl;
  2144 + throw;
  2145 + }
  2146 + cout << ".";
  2147 +
  2148 + //
  2149 + // ===== L O G I C A L 1 0 =====
  2150 + //
  2151 +
  2152 + // Set table
  2153 + GFitsTableLogCol col10 = GFitsTableLogCol("LOGICAL10", nrows, nvec);
  2154 + for (int i = 0; i < nrows; ++i) {
  2155 + for (int j = 0; j < nvec; ++j) {
  2156 + char val = (i % 2) * (j % 2);
  2157 + col10(i,j) = val;
  2158 + sum10 += val;
  2159 + sum10_int += int(val);
  2160 + if (val)
  2161 + sum10_str += ":T";
  2162 + else
  2163 + sum10_str += ":F";
  2164 + }
  2165 + }
  2166 + cout << ".";
  2167 +
  2168 + // Check table (operator access)
  2169 + tot_int = 0;
  2170 + for (int i = 0; i < nrows; ++i) {
  2171 + for (int j = 0; j < nvec; ++j)
  2172 + tot_int += col10(i,j);
  2173 + }
  2174 + if (tot_int != sum10) {
  2175 + cout << endl << "TEST ERROR: GFitsTableLogCol::operator() - 10";
  2176 + cout << endl << " Reference sum: " << sum10;
  2177 + cout << endl << " Derived sum: " << tot_int << endl;
  2178 + throw;
  2179 + }
  2180 + cout << ".";
  2181 +
  2182 + // Check table (real access)
  2183 + tot_dbl = 0.0;
  2184 + for (int i = 0; i < nrows; ++i) {
  2185 + for (int j = 0; j < nvec; ++j)
  2186 + tot_dbl += col10.real(i,j);
  2187 + }
  2188 + if (!dequal(tot_dbl, sum10)) {
  2189 + cout << endl << "TEST ERROR: GFitsTableLogCol::real() - 10";
  2190 + cout << endl << " Reference sum: " << sum10;
  2191 + cout << endl << " Derived sum: " << tot_dbl << endl;
  2192 + throw;
  2193 + }
  2194 + cout << ".";
  2195 +
  2196 + // Check table (int access)
  2197 + tot_int = 0;
  2198 + for (int i = 0; i < nrows; ++i) {
  2199 + for (int j = 0; j < nvec; ++j)
  2200 + tot_int += col10.integer(i,j);
  2201 + }
  2202 + if (tot_int != sum10_int) {
  2203 + cout << endl << "TEST ERROR: GFitsTableLogCol::integer() - 10";
  2204 + cout << endl << " Reference sum: " << sum10_int;
  2205 + cout << endl << " Derived sum: " << tot_int << endl;
  2206 + throw;
  2207 + }
  2208 + cout << ".";
  2209 +
  2210 + // Check table (string access)
  2211 + tot_str.clear();
  2212 + for (int i = 0; i < nrows; ++i) {
  2213 + for (int j = 0; j < nvec; ++j)
  2214 + tot_str += ":"+col10.string(i,j);
  2215 + }
  2216 + if (tot_str != sum10_str) {
  2217 + cout << endl << "TEST ERROR: GFitsTableLogCol::string() - 10";
  2218 + cout << endl << " Reference sum: " << sum10_str;
  2219 + cout << endl << " Derived sum: " << tot_str << endl;
  2220 + throw;
  2221 + }
  2222 + cout << ".";
  2223 +
  2224 + //
  2225 + // ===== W R I T E T A B L E =====
  2226 + //
  2227 +
  2228 + // Build binary table
  2229 + GFitsBinTable table = GFitsBinTable(nrows);
  2230 + table.append_column(col);
  2231 + table.insert_column(0, col10);
  2232 +
  2233 + // Create HDU and append to FILE file
  2234 + GFitsHDU hdu(table);
  2235 + fits.append_hdu(hdu);
  2236 +
  2237 + // Save FITS file
  2238 + fits.save();
  2239 + cout << ".";
  2240 +
  2241 + }
  2242 + catch (exception &e) {
  2243 + cout << endl << "TEST ERROR: Unable to build tables." << endl;
  2244 + cout << e.what() << endl;
  2245 + throw;
  2246 + }
  2247 + cout << ".";
  2248 +
  2249 +
  2250 + //================================================================
  2251 + //================================================================
  2252 + //================================================================
  2253 + // Read tables
  2254 + //================================================================
  2255 + //================================================================
  2256 + //================================================================
  2257 + try {
  2258 + // Open FITS file
  2259 + GFits fits;
  2260 + fits.open(filename);
  2261 +
  2262 + // Set number of rows
  2263 + int nrows = 10;
  2264 + int nvec = 10;
  2265 +
  2266 + // Initial control sums
  2267 + float tot_flt = 0.0;
  2268 + short tot_sht = 0;
  2269 + double tot_dbl = 0.0;
  2270 + int tot_int = 0;
  2271 + std::string tot_str;
  2272 +
  2273 + //
  2274 + // ===== L O G I C A L =====
  2275 + //
  2276 +
  2277 + // Get column
  2278 + GFitsTableLogCol* col =
  2279 + (GFitsTableLogCol*)fits.hdu(2)->column("LOGICAL");
  2280 +
  2281 + // Check table (operator access)
  2282 + tot_int = 0;
  2283 + for (int i = 0; i < nrows; ++i)
  2284 + tot_int += (*col)(i);
  2285 + if (tot_int != sum) {
  2286 + cout << endl << "TEST ERROR: GFitsTableLogCol::operator()";
  2287 + cout << endl << " Reference sum: " << sum;
  2288 + cout << endl << " Derived sum: " << tot_int << endl;
  2289 + throw;
  2290 + }
  2291 + cout << ".";
  2292 +
  2293 + // Check table (real access)
  2294 + tot_dbl = 0.0;
  2295 + for (int i = 0; i < nrows; ++i)
  2296 + tot_dbl += col->real(i);
  2297 + if (!dequal(tot_dbl, double(sum))) {
  2298 + cout << endl << "TEST ERROR: GFitsTableLogCol::real()";
  2299 + cout << endl << " Reference sum: " << sum;
  2300 + cout << endl << " Derived sum: " << tot_dbl << endl;
  2301 + throw;
  2302 + }
  2303 + cout << ".";
  2304 +
  2305 + // Check table (int access)
  2306 + tot_int = 0;
  2307 + for (int i = 0; i < nrows; ++i)
  2308 + tot_int += col->integer(i);
  2309 + if (tot_int != sum_int) {
  2310 + cout << endl << "TEST ERROR: GFitsTableLogCol::integer()";
  2311 + cout << endl << " Reference sum: " << sum_int;
  2312 + cout << endl << " Derived sum: " << tot_int << endl;
  2313 + throw;
  2314 + }
  2315 + cout << ".";
  2316 +
  2317 + // Check single precision table (string access)
  2318 + tot_str.clear();
  2319 + for (int i = 0; i < nrows; ++i)
  2320 + tot_str += ":"+col->string(i);
  2321 + if (tot_str != sum_str) {
  2322 + cout << endl << "TEST ERROR: GFitsTableLogCol::string()";
  2323 + cout << endl << " Reference sum: " << sum_str;
  2324 + cout << endl << " Derived sum: " << tot_str << endl;
  2325 + throw;
  2326 + }
  2327 + cout << ".";
  2328 +
  2329 + //
  2330 + // ===== L O G I C A L 1 0 =====
  2331 + //
  2332 +
  2333 + // Get column
  2334 + GFitsTableLogCol* col10 =
  2335 + (GFitsTableLogCol*)fits.hdu(2)->column("LOGICAL10");
  2336 +
  2337 + // Check table (operator access)
  2338 + tot_int = 0;
  2339 + for (int i = 0; i < nrows; ++i) {
  2340 + for (int j = 0; j < nvec; ++j)
  2341 + tot_int += (*col10)(i,j);
  2342 + }
  2343 + if (tot_int != sum10) {
  2344 + cout << endl << "TEST ERROR: GFitsTableLogCol::operator() - 10";
  2345 + cout << endl << " Reference sum: " << sum10;
  2346 + cout << endl << " Derived sum: " << tot_int << endl;
  2347 + throw;
  2348 + }
  2349 + cout << ".";
  2350 +
  2351 + // Check table (real access)
  2352 + tot_dbl = 0.0;
  2353 + for (int i = 0; i < nrows; ++i) {
  2354 + for (int j = 0; j < nvec; ++j)
  2355 + tot_dbl += col10->real(i,j);
  2356 + }
  2357 + if (!dequal(tot_dbl, sum10)) {
  2358 + cout << endl << "TEST ERROR: GFitsTableLogCol::real() - 10";
  2359 + cout << endl << " Reference sum: " << sum10;
  2360 + cout << endl << " Derived sum: " << tot_dbl << endl;
  2361 + throw;
  2362 + }
  2363 + cout << ".";
  2364 +
  2365 + // Check table (int access)
  2366 + tot_int = 0;
  2367 + for (int i = 0; i < nrows; ++i) {
  2368 + for (int j = 0; j < nvec; ++j)
  2369 + tot_int += col10->integer(i,j);
  2370 + }
  2371 + if (tot_int != sum10_int) {
  2372 + cout << endl << "TEST ERROR: GFitsTableLogCol::integer() - 10";
  2373 + cout << endl << " Reference sum: " << sum10_int;
  2374 + cout << endl << " Derived sum: " << tot_int << endl;
  2375 + throw;
  2376 + }
  2377 + cout << ".";
  2378 +
  2379 + // Check single precision table (string access)
  2380 + tot_str.clear();
  2381 + for (int i = 0; i < nrows; ++i) {
  2382 + for (int j = 0; j < nvec; ++j)
  2383 + tot_str += ":"+col10->string(i,j);
  2384 + }
  2385 + if (tot_str != sum10_str) {
  2386 + cout << endl << "TEST ERROR: GFitsTableLogCol::string() - 10";
  2387 + cout << endl << " Reference sum: " << sum10_str;
  2388 + cout << endl << " Derived sum: " << tot_str << endl;
  2389 + throw;
  2390 + }
  2391 + cout << ".";
  2392 + }
  2393 + catch (exception &e) {
  2394 + cout << endl << "TEST ERROR: Unable to build tables." << endl;
  2395 + cout << e.what() << endl;
  2396 + throw;
  2397 + }
  2398 + cout << ". ok." << endl;
  2399 +
  2400 +}
  2401 +
  2402 +
  2403 +/***************************************************************************
2043 2404 * Main test function *
2044 2405 ***************************************************************************/
2045 2406 int main(void)
... ... @@ -2058,10 +2419,8 @@ int main(void)
2058 2419 test_bintable_short();
2059 2420 test_bintable_long();
2060 2421 test_bintable_string();
  2422 + test_bintable_logical();
2061 2423  
2062   - //test_open();
2063   - //test_columns();
2064   -
2065 2424 // Return
2066 2425 return 0;
2067 2426 }
... ...