From 4f8446be6953fc461de3545b2cf77364a380b9d7 Mon Sep 17 00:00:00 2001 From: knodlseder <> Date: Tue, 4 Mar 2008 21:04:21 +0000 Subject: [PATCH] replace pointers by values in HDU and column appending --- include/GFits.hpp | 3 +-- include/GFitsTable.hpp | 6 +++--- include/GFitsTableCol.hpp | 2 +- src/fits/GFits.cpp | 8 ++++---- src/fits/GFitsTable.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++++---------------------------------- src/fits/GFitsTableCol.cpp | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------- src/fits/GFitsTableDblCol.cpp | 1 + src/fits/GFitsTableFltCol.cpp | 1 + src/fits/GFitsTableLngCol.cpp | 1 + src/fits/GFitsTableShtCol.cpp | 1 + src/fits/GFitsTableStrCol.cpp | 1 + src/rsp/GLATResponse.cpp | 8 ++++---- test/test_GFits.cpp | 14 +++++++++----- 13 files changed, 124 insertions(+), 105 deletions(-) diff --git a/include/GFits.hpp b/include/GFits.hpp index 257c811..96f7990 100644 --- a/include/GFits.hpp +++ b/include/GFits.hpp @@ -20,7 +20,6 @@ #define GFITS_HPP /* __ Includes ___________________________________________________________ */ -#include #include "GFitsCfitsio.hpp" #include "GFitsHDU.hpp" @@ -52,7 +51,7 @@ public: // Methods void open(const std::string& filename); - void append(const GFitsHDU* hdu); + void append_hdu(const GFitsHDU& hdu); void save(void); void saveto(const std::string& filename, int clobber); void close(void); diff --git a/include/GFitsTable.hpp b/include/GFitsTable.hpp index e038369..e5eb38a 100644 --- a/include/GFitsTable.hpp +++ b/include/GFitsTable.hpp @@ -58,8 +58,8 @@ public: void save(void); void close(void); GFitsTable* clone(void) const = 0; - void append_column(GFitsTableCol* column); - void insert_column(int colnum, GFitsTableCol* column); + void append_column(GFitsTableCol& column); + void insert_column(int colnum, GFitsTableCol& column); void append_rows(const int& nrows); void insert_rows(const int& rownum, const int& nrows); GFitsTableCol* column(const std::string& colname); @@ -81,7 +81,7 @@ protected: int m_type; //!< Table type (1=ASCII, 2=Binary) int m_rows; //!< Number of rows in table int m_cols; //!< Number of columns in table - GFitsTableCol** m_columns; //!< Array of column pointers + GFitsTableCol** m_columns; //!< Array of table columns }; diff --git a/include/GFitsTableCol.hpp b/include/GFitsTableCol.hpp index 256e10a..73880c5 100644 --- a/include/GFitsTableCol.hpp +++ b/include/GFitsTableCol.hpp @@ -70,7 +70,7 @@ protected: std::string m_name; //!< Column name std::string m_format; //!< Column format std::string m_unit; //!< Column unit - int m_colnum; //!< Column number (starting from 1) + int m_colnum; //!< Column number in FITS file (starting from 1) int m_type; //!< Column type int m_repeat; //!< Repeat value of column int m_width; //!< Width of column diff --git a/src/fits/GFits.cpp b/src/fits/GFits.cpp index 1623563..461c0fb 100644 --- a/src/fits/GFits.cpp +++ b/src/fits/GFits.cpp @@ -189,19 +189,19 @@ void GFits::open(const std::string& filename) /***********************************************************************//** * @brief Append HDU to FITS file * - * @param[in] hdu Pointer to FITS HDU that should be appended + * @param[in] hdu FITS HDU that should be appended * * Append HDU to the next free position in a FITS file. In case that no HDU * exists so far in the FITS file and if the HDU to append is NOT an image, * an empty primary image will be inserted as first HDU in the FITS file. * This guarantees the compatibility with the FITS standard. ***************************************************************************/ -void GFits::append(const GFitsHDU* hdu) +void GFits::append_hdu(const GFitsHDU& hdu) { // Determine number of HDUs to add. If there are no HDUs so far and if // the HDU to append is not an image then we have to add a primary // image first. In this case we add 2 new HDUs. - int n_add = (m_num_hdu == 0 && hdu->m_type != 0) ? 2 : 1; + int n_add = (m_num_hdu == 0 && hdu.m_type != 0) ? 2 : 1; // Create memory to hold HDUs GFitsHDU* tmp = new GFitsHDU[m_num_hdu+n_add]; @@ -233,7 +233,7 @@ void GFits::append(const GFitsHDU* hdu) } // Append new HDU to list - m_hdu[m_num_hdu] = *hdu; + m_hdu[m_num_hdu] = hdu; // Set FITS file pointer for new HDU __fitsfile fptr = *m_fitsfile; diff --git a/src/fits/GFitsTable.cpp b/src/fits/GFitsTable.cpp index 13d374d..f191681 100644 --- a/src/fits/GFitsTable.cpp +++ b/src/fits/GFitsTable.cpp @@ -347,23 +347,14 @@ void GFitsTable::close(void) /***********************************************************************//** * @brief Append column to the table * - * @param[in] column Pointer to column which should be appended - * - * @exception GException::fits_no_data - * A NULL column pointer was passed to the method. - * This method requires a valid column pointer. + * @param[in] column Column which should be appended to table * * A column will be appended at the end of the table. See * GFitsTable::insert_column * for more details on the method. ***************************************************************************/ -void GFitsTable::append_column(GFitsTableCol* column) +void GFitsTable::append_column(GFitsTableCol& column) { - // Throw exception if column pointer is empty - if (column == NULL) - throw GException::fits_no_data(G_APPEND_COLUMN, - "Column pointer was NULL"); - // Inserting at the end correspond to appending insert_column(m_cols, column); @@ -376,14 +367,13 @@ void GFitsTable::append_column(GFitsTableCol* column) * @brief Insert column into the table * * @param[in] colnum Column number to be inserted - * @param[in] column Pointer to column which should be inserted + * @param[in] columnColumn which should be inserted into the table * - * @exception GException::fits_no_data - * A NULL column pointer was passed to the method. - * This method requires a valid column pointer. * @exception GException::fits_bad_col_length * The length of the column is incompatible with the number of * rows in the table. + * @exception GException::fits_unknown_coltype + * FITS column is of unknown type. * * A column will be inserted at position 'colnum' of the table. If the * position is beyond the end of the table the column will be appended. @@ -395,13 +385,8 @@ void GFitsTable::append_column(GFitsTableCol* column) * The length of the column to be inserted has to be identical to the number * of rows in the table. ***************************************************************************/ -void GFitsTable::insert_column(int colnum, GFitsTableCol* column) +void GFitsTable::insert_column(int colnum, GFitsTableCol& column) { - // Throw exception if column pointer is empty - if (column == NULL) - throw GException::fits_no_data(G_INSERT_COLUMN, - "Column pointer was NULL"); - // Make sure that 'colnum' is valid. Since we add one column the total // number of columns at return will be m_cols+1, hence the column // index is comprised between [0,m_cols] @@ -411,18 +396,13 @@ void GFitsTable::insert_column(int colnum, GFitsTableCol* column) // If the table is empty and has 0 rows then set the number of rows in // the table to the length of the column if (m_columns == NULL && m_rows == 0) - m_rows = column->length(); + m_rows = column.length(); // Throw exception if the column length is incompatible with number of // rows in the table - if (m_rows != column->length()) + if (m_rows != column.length()) throw GException::fits_bad_col_length(G_INSERT_COLUMN, - column->length(), m_rows); - - // If there are no rows in the table then set the number of rows to - // the length of the column - if (m_rows == 0 && column->length() > 0) - m_rows = column->length(); + column.length(), m_rows); // If no column data exist then allocate them now if (m_columns == NULL) { @@ -462,11 +442,35 @@ void GFitsTable::insert_column(int colnum, GFitsTableCol* column) } // endelse: we made space to insert a column - // Insert column pointer - m_columns[colnum] = column; + // Type dependent copy of column + switch (column.m_type) { + case __TSTRING: + m_columns[colnum] = new GFitsTableStrCol; + *m_columns[colnum] = column; + break; + case __TSHORT: + m_columns[colnum] = new GFitsTableShtCol; + *m_columns[colnum] = column; + break; + case __TLONG: + m_columns[colnum] = new GFitsTableLngCol; + *m_columns[colnum] = column; + break; + case __TFLOAT: + m_columns[colnum] = new GFitsTableFltCol; + *m_columns[colnum] = column; + break; + case __TDOUBLE: + m_columns[colnum] = new GFitsTableDblCol; + *m_columns[colnum] = column; + break; + default: + throw GException::fits_unknown_coltype(G_INSERT_COLUMN, column.m_type); + break; + } // Set column attributes - m_columns[colnum]->m_colnum = colnum + 1; // IS THIS REALLY NEEDED??? + //m_columns[colnum]->m_colnum = colnum + 1; // IS THIS REALLY NEEDED??? // Return return; @@ -618,11 +622,18 @@ void GFitsTable::copy_members(const GFitsTable& table) /***********************************************************************//** * @brief Delete class members + * + * De-allocates all column pointers ***************************************************************************/ void GFitsTable::free_members(void) { - // Free memory (don't free associated column pointers explicitely!!!) - if (m_columns != NULL) delete [] m_columns; + // Free memory + if (m_columns != NULL) { + for (int i = 0; i < m_cols; ++i) { + if (m_columns[i] != NULL) delete m_columns[i]; + } + delete [] m_columns; + } // Mark memory as freed m_columns = NULL; diff --git a/src/fits/GFitsTableCol.cpp b/src/fits/GFitsTableCol.cpp index 023f13a..c143553 100644 --- a/src/fits/GFitsTableCol.cpp +++ b/src/fits/GFitsTableCol.cpp @@ -152,54 +152,6 @@ void GFitsTableCol::name(const std::string& name) /***********************************************************************//** - * @brief Set column number - * - * @param[in] colnum Number of column. - ***************************************************************************/ -/* -void GFitsTableCol::colnum(const int& colnum) -{ - // Set column number - m_colnum = colnum; - - // Return - return; -} -*/ - -/***********************************************************************//** - * @brief Set column type - * - * @param[in] type Type of column. - ***************************************************************************/ -/* -void GFitsTableCol::type(const int& type) -{ - // Set column type - m_type = type; - - // Return - return; -} -*/ - -/***********************************************************************//** - * @brief Set column length - * - * @param[in] length Length of column. - ***************************************************************************/ -/* -void GFitsTableCol::length(const int& length) -{ - // Set column length - m_length = length; - - // Return - return; -} -*/ - -/***********************************************************************//** * @brief Get column name ***************************************************************************/ std::string GFitsTableCol::name(void) @@ -371,10 +323,58 @@ ostream& operator<< (ostream& os, const GFitsTableCol& column) // Put header in stream os << "'" << column.m_name << "'"; os << " [" << column.m_colnum << "] "; - os << column.m_type << " repeat="; - os << column.m_repeat << " width="; - os << column.m_width << " length="; - os << column.m_length << endl; + + // Set column type + switch (column.m_type) { + case 1: + os << "TBIT"; + break; + case 11: + os << "TBYTE"; + break; + case 14: + os << "TLOGICAL"; + break; + case 16: + os << "TSTRING"; + break; + case 21: + os << "TSHORT"; + break; + case 31: + os << "TINT"; + break; + case 41: + os << "TLONG"; + break; + case 42: + os << "TFLOAT"; + break; + case 81: + os << "TLONGLONG"; + break; + case 82: + os << "TDOUBLE"; + break; + case 83: + os << "TCOMPLEX"; + break; + case 163: + os << "TDBLCOMPLEX"; + break; + default: + os << ""; + break; + } + + // Set vector length + os << " repeat=" << column.m_repeat; + + // Set width + os << " width=" << column.m_width; + + // Set length + os << " length=" << column.m_length << endl; // Return output stream return os; diff --git a/src/fits/GFitsTableDblCol.cpp b/src/fits/GFitsTableDblCol.cpp index 4fb4723..265d913 100644 --- a/src/fits/GFitsTableDblCol.cpp +++ b/src/fits/GFitsTableDblCol.cpp @@ -367,6 +367,7 @@ void GFitsTableDblCol::set_nullval(const double* value) void GFitsTableDblCol::init_members(void) { // Initialise members + m_type = __TDOUBLE; m_size = 0; m_anynul = 0; m_data = NULL; diff --git a/src/fits/GFitsTableFltCol.cpp b/src/fits/GFitsTableFltCol.cpp index b6a937b..2bf2d98 100644 --- a/src/fits/GFitsTableFltCol.cpp +++ b/src/fits/GFitsTableFltCol.cpp @@ -295,6 +295,7 @@ void GFitsTableFltCol::set_nullval(const float* value) void GFitsTableFltCol::init_members(void) { // Initialise members + m_type = __TFLOAT; m_size = 0; m_anynul = 0; m_data = NULL; diff --git a/src/fits/GFitsTableLngCol.cpp b/src/fits/GFitsTableLngCol.cpp index 8d0c1e8..7258f5d 100644 --- a/src/fits/GFitsTableLngCol.cpp +++ b/src/fits/GFitsTableLngCol.cpp @@ -349,6 +349,7 @@ void GFitsTableLngCol::set_nullval(const long* value) void GFitsTableLngCol::init_members(void) { // Initialise members + m_type = __TLONG; m_size = 0; m_anynul = 0; m_data = NULL; diff --git a/src/fits/GFitsTableShtCol.cpp b/src/fits/GFitsTableShtCol.cpp index 5beef5a..53cfb61 100644 --- a/src/fits/GFitsTableShtCol.cpp +++ b/src/fits/GFitsTableShtCol.cpp @@ -286,6 +286,7 @@ void GFitsTableShtCol::set_nullval(const short* value) void GFitsTableShtCol::init_members(void) { // Initialise members + m_type = __TSHORT; m_size = 0; m_anynul = 0; m_data = NULL; diff --git a/src/fits/GFitsTableStrCol.cpp b/src/fits/GFitsTableStrCol.cpp index e8ee1bf..fb45a37 100644 --- a/src/fits/GFitsTableStrCol.cpp +++ b/src/fits/GFitsTableStrCol.cpp @@ -287,6 +287,7 @@ void GFitsTableStrCol::set_nullstr(const std::string string) void GFitsTableStrCol::init_members(void) { // Initialise members + m_type = __TSTRING; m_size = 0; m_num_subs = 0; m_data = NULL; diff --git a/src/rsp/GLATResponse.cpp b/src/rsp/GLATResponse.cpp index 159c7c6..472f162 100644 --- a/src/rsp/GLATResponse.cpp +++ b/src/rsp/GLATResponse.cpp @@ -284,10 +284,10 @@ void GLATResponse::save(const std::string& rspname) const hdu_psf_sigma.extname("SIGMA"); // Append HDUs to FITS file - fits.append(&hdu_psf_bounds); - fits.append(&hdu_psf_psf); - fits.append(&hdu_psf_norm); - fits.append(&hdu_psf_sigma); + fits.append_hdu(hdu_psf_bounds); + fits.append_hdu(hdu_psf_psf); + fits.append_hdu(hdu_psf_norm); + fits.append_hdu(hdu_psf_sigma); // Save FITS file fits.save(); diff --git a/test/test_GFits.cpp b/test/test_GFits.cpp index 5ff5ec6..9caf478 100644 --- a/test/test_GFits.cpp +++ b/test/test_GFits.cpp @@ -306,7 +306,7 @@ void test_create(void) fits.open("test.fits"); GFitsDblImage image; GFitsHDU hdu(image); - fits.append(&hdu); + fits.append_hdu(hdu); //cout << fits; fits.save(); } @@ -334,7 +334,7 @@ void test_create(void) } } GFitsHDU hdu(image); - fits.append(&hdu); + fits.append_hdu(hdu); //cout << fits; fits.save(); } @@ -372,23 +372,27 @@ void test_create(void) cout << "."; // Attach binary table +cout << "start" << endl; try { // Re-open FITS file GFits fits; fits.open("test.fits"); +cout << "1" << endl; // Create binary Table with 10 rows int nrows = 10; GFitsBinTable table = GFitsBinTable(nrows); GFitsTableDblCol first = GFitsTableDblCol(nrows); GFitsTableDblCol second = GFitsTableDblCol(nrows); - table.append_column(&first); - table.append_column(&second); +cout << "2" << endl; + table.append_column(first); +cout << "3" << endl; + table.append_column(second); cout << table; // Create HDU and append to FILE file GFitsHDU hdu(table); - fits.append(&hdu); + fits.append_hdu(hdu); // cout << fits; // Save FITS file -- libgit2 0.21.4