Commit 4f8446be6953fc461de3545b2cf77364a380b9d7

Authored by knodlseder
1 parent 1fff8b90

replace pointers by values in HDU and column appending

include/GFits.hpp
@@ -20,7 +20,6 @@ @@ -20,7 +20,6 @@
20 #define GFITS_HPP 20 #define GFITS_HPP
21 21
22 /* __ Includes ___________________________________________________________ */ 22 /* __ Includes ___________________________________________________________ */
23 -#include <vector>  
24 #include "GFitsCfitsio.hpp" 23 #include "GFitsCfitsio.hpp"
25 #include "GFitsHDU.hpp" 24 #include "GFitsHDU.hpp"
26 25
@@ -52,7 +51,7 @@ public: @@ -52,7 +51,7 @@ public:
52 51
53 // Methods 52 // Methods
54 void open(const std::string& filename); 53 void open(const std::string& filename);
55 - void append(const GFitsHDU* hdu); 54 + void append_hdu(const GFitsHDU& hdu);
56 void save(void); 55 void save(void);
57 void saveto(const std::string& filename, int clobber); 56 void saveto(const std::string& filename, int clobber);
58 void close(void); 57 void close(void);
include/GFitsTable.hpp
@@ -58,8 +58,8 @@ public: @@ -58,8 +58,8 @@ public:
58 void save(void); 58 void save(void);
59 void close(void); 59 void close(void);
60 GFitsTable* clone(void) const = 0; 60 GFitsTable* clone(void) const = 0;
61 - void append_column(GFitsTableCol* column);  
62 - void insert_column(int colnum, GFitsTableCol* column); 61 + void append_column(GFitsTableCol& column);
  62 + void insert_column(int colnum, GFitsTableCol& column);
63 void append_rows(const int& nrows); 63 void append_rows(const int& nrows);
64 void insert_rows(const int& rownum, const int& nrows); 64 void insert_rows(const int& rownum, const int& nrows);
65 GFitsTableCol* column(const std::string& colname); 65 GFitsTableCol* column(const std::string& colname);
@@ -81,7 +81,7 @@ protected: @@ -81,7 +81,7 @@ protected:
81 int m_type; //!< Table type (1=ASCII, 2=Binary) 81 int m_type; //!< Table type (1=ASCII, 2=Binary)
82 int m_rows; //!< Number of rows in table 82 int m_rows; //!< Number of rows in table
83 int m_cols; //!< Number of columns in table 83 int m_cols; //!< Number of columns in table
84 - GFitsTableCol** m_columns; //!< Array of column pointers 84 + GFitsTableCol** m_columns; //!< Array of table columns
85 }; 85 };
86 86
87 87
include/GFitsTableCol.hpp
@@ -70,7 +70,7 @@ protected: @@ -70,7 +70,7 @@ protected:
70 std::string m_name; //!< Column name 70 std::string m_name; //!< Column name
71 std::string m_format; //!< Column format 71 std::string m_format; //!< Column format
72 std::string m_unit; //!< Column unit 72 std::string m_unit; //!< Column unit
73 - int m_colnum; //!< Column number (starting from 1) 73 + int m_colnum; //!< Column number in FITS file (starting from 1)
74 int m_type; //!< Column type 74 int m_type; //!< Column type
75 int m_repeat; //!< Repeat value of column 75 int m_repeat; //!< Repeat value of column
76 int m_width; //!< Width of column 76 int m_width; //!< Width of column
src/fits/GFits.cpp
@@ -189,19 +189,19 @@ void GFits::open(const std::string&amp; filename) @@ -189,19 +189,19 @@ void GFits::open(const std::string&amp; filename)
189 /***********************************************************************//** 189 /***********************************************************************//**
190 * @brief Append HDU to FITS file 190 * @brief Append HDU to FITS file
191 * 191 *
192 - * @param[in] hdu Pointer to FITS HDU that should be appended 192 + * @param[in] hdu FITS HDU that should be appended
193 * 193 *
194 * Append HDU to the next free position in a FITS file. In case that no HDU 194 * Append HDU to the next free position in a FITS file. In case that no HDU
195 * exists so far in the FITS file and if the HDU to append is NOT an image, 195 * exists so far in the FITS file and if the HDU to append is NOT an image,
196 * an empty primary image will be inserted as first HDU in the FITS file. 196 * an empty primary image will be inserted as first HDU in the FITS file.
197 * This guarantees the compatibility with the FITS standard. 197 * This guarantees the compatibility with the FITS standard.
198 ***************************************************************************/ 198 ***************************************************************************/
199 -void GFits::append(const GFitsHDU* hdu) 199 +void GFits::append_hdu(const GFitsHDU& hdu)
200 { 200 {
201 // Determine number of HDUs to add. If there are no HDUs so far and if 201 // Determine number of HDUs to add. If there are no HDUs so far and if
202 // the HDU to append is not an image then we have to add a primary 202 // the HDU to append is not an image then we have to add a primary
203 // image first. In this case we add 2 new HDUs. 203 // image first. In this case we add 2 new HDUs.
204 - int n_add = (m_num_hdu == 0 && hdu->m_type != 0) ? 2 : 1; 204 + int n_add = (m_num_hdu == 0 && hdu.m_type != 0) ? 2 : 1;
205 205
206 // Create memory to hold HDUs 206 // Create memory to hold HDUs
207 GFitsHDU* tmp = new GFitsHDU[m_num_hdu+n_add]; 207 GFitsHDU* tmp = new GFitsHDU[m_num_hdu+n_add];
@@ -233,7 +233,7 @@ void GFits::append(const GFitsHDU* hdu) @@ -233,7 +233,7 @@ void GFits::append(const GFitsHDU* hdu)
233 } 233 }
234 234
235 // Append new HDU to list 235 // Append new HDU to list
236 - m_hdu[m_num_hdu] = *hdu; 236 + m_hdu[m_num_hdu] = hdu;
237 237
238 // Set FITS file pointer for new HDU 238 // Set FITS file pointer for new HDU
239 __fitsfile fptr = *m_fitsfile; 239 __fitsfile fptr = *m_fitsfile;
src/fits/GFitsTable.cpp
@@ -347,23 +347,14 @@ void GFitsTable::close(void) @@ -347,23 +347,14 @@ void GFitsTable::close(void)
347 /***********************************************************************//** 347 /***********************************************************************//**
348 * @brief Append column to the table 348 * @brief Append column to the table
349 * 349 *
350 - * @param[in] column Pointer to column which should be appended  
351 - *  
352 - * @exception GException::fits_no_data  
353 - * A NULL column pointer was passed to the method.  
354 - * This method requires a valid column pointer. 350 + * @param[in] column Column which should be appended to table
355 * 351 *
356 * A column will be appended at the end of the table. See 352 * A column will be appended at the end of the table. See
357 * GFitsTable::insert_column 353 * GFitsTable::insert_column
358 * for more details on the method. 354 * for more details on the method.
359 ***************************************************************************/ 355 ***************************************************************************/
360 -void GFitsTable::append_column(GFitsTableCol* column) 356 +void GFitsTable::append_column(GFitsTableCol& column)
361 { 357 {
362 - // Throw exception if column pointer is empty  
363 - if (column == NULL)  
364 - throw GException::fits_no_data(G_APPEND_COLUMN,  
365 - "Column pointer was NULL");  
366 -  
367 // Inserting at the end correspond to appending 358 // Inserting at the end correspond to appending
368 insert_column(m_cols, column); 359 insert_column(m_cols, column);
369 360
@@ -376,14 +367,13 @@ void GFitsTable::append_column(GFitsTableCol* column) @@ -376,14 +367,13 @@ void GFitsTable::append_column(GFitsTableCol* column)
376 * @brief Insert column into the table 367 * @brief Insert column into the table
377 * 368 *
378 * @param[in] colnum Column number to be inserted 369 * @param[in] colnum Column number to be inserted
379 - * @param[in] column Pointer to column which should be inserted 370 + * @param[in] columnColumn which should be inserted into the table
380 * 371 *
381 - * @exception GException::fits_no_data  
382 - * A NULL column pointer was passed to the method.  
383 - * This method requires a valid column pointer.  
384 * @exception GException::fits_bad_col_length 372 * @exception GException::fits_bad_col_length
385 * The length of the column is incompatible with the number of 373 * The length of the column is incompatible with the number of
386 * rows in the table. 374 * rows in the table.
  375 + * @exception GException::fits_unknown_coltype
  376 + * FITS column is of unknown type.
387 * 377 *
388 * A column will be inserted at position 'colnum' of the table. If the 378 * A column will be inserted at position 'colnum' of the table. If the
389 * position is beyond the end of the table the column will be appended. 379 * position is beyond the end of the table the column will be appended.
@@ -395,13 +385,8 @@ void GFitsTable::append_column(GFitsTableCol* column) @@ -395,13 +385,8 @@ void GFitsTable::append_column(GFitsTableCol* column)
395 * The length of the column to be inserted has to be identical to the number 385 * The length of the column to be inserted has to be identical to the number
396 * of rows in the table. 386 * of rows in the table.
397 ***************************************************************************/ 387 ***************************************************************************/
398 -void GFitsTable::insert_column(int colnum, GFitsTableCol* column) 388 +void GFitsTable::insert_column(int colnum, GFitsTableCol& column)
399 { 389 {
400 - // Throw exception if column pointer is empty  
401 - if (column == NULL)  
402 - throw GException::fits_no_data(G_INSERT_COLUMN,  
403 - "Column pointer was NULL");  
404 -  
405 // Make sure that 'colnum' is valid. Since we add one column the total 390 // Make sure that 'colnum' is valid. Since we add one column the total
406 // number of columns at return will be m_cols+1, hence the column 391 // number of columns at return will be m_cols+1, hence the column
407 // index is comprised between [0,m_cols] 392 // index is comprised between [0,m_cols]
@@ -411,18 +396,13 @@ void GFitsTable::insert_column(int colnum, GFitsTableCol* column) @@ -411,18 +396,13 @@ void GFitsTable::insert_column(int colnum, GFitsTableCol* column)
411 // If the table is empty and has 0 rows then set the number of rows in 396 // If the table is empty and has 0 rows then set the number of rows in
412 // the table to the length of the column 397 // the table to the length of the column
413 if (m_columns == NULL && m_rows == 0) 398 if (m_columns == NULL && m_rows == 0)
414 - m_rows = column->length(); 399 + m_rows = column.length();
415 400
416 // Throw exception if the column length is incompatible with number of 401 // Throw exception if the column length is incompatible with number of
417 // rows in the table 402 // rows in the table
418 - if (m_rows != column->length()) 403 + if (m_rows != column.length())
419 throw GException::fits_bad_col_length(G_INSERT_COLUMN, 404 throw GException::fits_bad_col_length(G_INSERT_COLUMN,
420 - column->length(), m_rows);  
421 -  
422 - // If there are no rows in the table then set the number of rows to  
423 - // the length of the column  
424 - if (m_rows == 0 && column->length() > 0)  
425 - m_rows = column->length(); 405 + column.length(), m_rows);
426 406
427 // If no column data exist then allocate them now 407 // If no column data exist then allocate them now
428 if (m_columns == NULL) { 408 if (m_columns == NULL) {
@@ -462,11 +442,35 @@ void GFitsTable::insert_column(int colnum, GFitsTableCol* column) @@ -462,11 +442,35 @@ void GFitsTable::insert_column(int colnum, GFitsTableCol* column)
462 442
463 } // endelse: we made space to insert a column 443 } // endelse: we made space to insert a column
464 444
465 - // Insert column pointer  
466 - m_columns[colnum] = column; 445 + // Type dependent copy of column
  446 + switch (column.m_type) {
  447 + case __TSTRING:
  448 + m_columns[colnum] = new GFitsTableStrCol;
  449 + *m_columns[colnum] = column;
  450 + break;
  451 + case __TSHORT:
  452 + m_columns[colnum] = new GFitsTableShtCol;
  453 + *m_columns[colnum] = column;
  454 + break;
  455 + case __TLONG:
  456 + m_columns[colnum] = new GFitsTableLngCol;
  457 + *m_columns[colnum] = column;
  458 + break;
  459 + case __TFLOAT:
  460 + m_columns[colnum] = new GFitsTableFltCol;
  461 + *m_columns[colnum] = column;
  462 + break;
  463 + case __TDOUBLE:
  464 + m_columns[colnum] = new GFitsTableDblCol;
  465 + *m_columns[colnum] = column;
  466 + break;
  467 + default:
  468 + throw GException::fits_unknown_coltype(G_INSERT_COLUMN, column.m_type);
  469 + break;
  470 + }
467 471
468 // Set column attributes 472 // Set column attributes
469 - m_columns[colnum]->m_colnum = colnum + 1; // IS THIS REALLY NEEDED??? 473 + //m_columns[colnum]->m_colnum = colnum + 1; // IS THIS REALLY NEEDED???
470 474
471 // Return 475 // Return
472 return; 476 return;
@@ -618,11 +622,18 @@ void GFitsTable::copy_members(const GFitsTable&amp; table) @@ -618,11 +622,18 @@ void GFitsTable::copy_members(const GFitsTable&amp; table)
618 622
619 /***********************************************************************//** 623 /***********************************************************************//**
620 * @brief Delete class members 624 * @brief Delete class members
  625 + *
  626 + * De-allocates all column pointers
621 ***************************************************************************/ 627 ***************************************************************************/
622 void GFitsTable::free_members(void) 628 void GFitsTable::free_members(void)
623 { 629 {
624 - // Free memory (don't free associated column pointers explicitely!!!)  
625 - if (m_columns != NULL) delete [] m_columns; 630 + // Free memory
  631 + if (m_columns != NULL) {
  632 + for (int i = 0; i < m_cols; ++i) {
  633 + if (m_columns[i] != NULL) delete m_columns[i];
  634 + }
  635 + delete [] m_columns;
  636 + }
626 637
627 // Mark memory as freed 638 // Mark memory as freed
628 m_columns = NULL; 639 m_columns = NULL;
src/fits/GFitsTableCol.cpp
@@ -152,54 +152,6 @@ void GFitsTableCol::name(const std::string&amp; name) @@ -152,54 +152,6 @@ void GFitsTableCol::name(const std::string&amp; name)
152 152
153 153
154 /***********************************************************************//** 154 /***********************************************************************//**
155 - * @brief Set column number  
156 - *  
157 - * @param[in] colnum Number of column.  
158 - ***************************************************************************/  
159 -/*  
160 -void GFitsTableCol::colnum(const int& colnum)  
161 -{  
162 - // Set column number  
163 - m_colnum = colnum;  
164 -  
165 - // Return  
166 - return;  
167 -}  
168 -*/  
169 -  
170 -/***********************************************************************//**  
171 - * @brief Set column type  
172 - *  
173 - * @param[in] type Type of column.  
174 - ***************************************************************************/  
175 -/*  
176 -void GFitsTableCol::type(const int& type)  
177 -{  
178 - // Set column type  
179 - m_type = type;  
180 -  
181 - // Return  
182 - return;  
183 -}  
184 -*/  
185 -  
186 -/***********************************************************************//**  
187 - * @brief Set column length  
188 - *  
189 - * @param[in] length Length of column.  
190 - ***************************************************************************/  
191 -/*  
192 -void GFitsTableCol::length(const int& length)  
193 -{  
194 - // Set column length  
195 - m_length = length;  
196 -  
197 - // Return  
198 - return;  
199 -}  
200 -*/  
201 -  
202 -/***********************************************************************//**  
203 * @brief Get column name 155 * @brief Get column name
204 ***************************************************************************/ 156 ***************************************************************************/
205 std::string GFitsTableCol::name(void) 157 std::string GFitsTableCol::name(void)
@@ -371,10 +323,58 @@ ostream&amp; operator&lt;&lt; (ostream&amp; os, const GFitsTableCol&amp; column) @@ -371,10 +323,58 @@ ostream&amp; operator&lt;&lt; (ostream&amp; os, const GFitsTableCol&amp; column)
371 // Put header in stream 323 // Put header in stream
372 os << "'" << column.m_name << "'"; 324 os << "'" << column.m_name << "'";
373 os << " [" << column.m_colnum << "] "; 325 os << " [" << column.m_colnum << "] ";
374 - os << column.m_type << " repeat=";  
375 - os << column.m_repeat << " width=";  
376 - os << column.m_width << " length=";  
377 - os << column.m_length << endl; 326 +
  327 + // Set column type
  328 + switch (column.m_type) {
  329 + case 1:
  330 + os << "TBIT";
  331 + break;
  332 + case 11:
  333 + os << "TBYTE";
  334 + break;
  335 + case 14:
  336 + os << "TLOGICAL";
  337 + break;
  338 + case 16:
  339 + os << "TSTRING";
  340 + break;
  341 + case 21:
  342 + os << "TSHORT";
  343 + break;
  344 + case 31:
  345 + os << "TINT";
  346 + break;
  347 + case 41:
  348 + os << "TLONG";
  349 + break;
  350 + case 42:
  351 + os << "TFLOAT";
  352 + break;
  353 + case 81:
  354 + os << "TLONGLONG";
  355 + break;
  356 + case 82:
  357 + os << "TDOUBLE";
  358 + break;
  359 + case 83:
  360 + os << "TCOMPLEX";
  361 + break;
  362 + case 163:
  363 + os << "TDBLCOMPLEX";
  364 + break;
  365 + default:
  366 + os << "<unknown type>";
  367 + break;
  368 + }
  369 +
  370 + // Set vector length
  371 + os << " repeat=" << column.m_repeat;
  372 +
  373 + // Set width
  374 + os << " width=" << column.m_width;
  375 +
  376 + // Set length
  377 + os << " length=" << column.m_length << endl;
378 378
379 // Return output stream 379 // Return output stream
380 return os; 380 return os;
src/fits/GFitsTableDblCol.cpp
@@ -367,6 +367,7 @@ void GFitsTableDblCol::set_nullval(const double* value) @@ -367,6 +367,7 @@ void GFitsTableDblCol::set_nullval(const double* value)
367 void GFitsTableDblCol::init_members(void) 367 void GFitsTableDblCol::init_members(void)
368 { 368 {
369 // Initialise members 369 // Initialise members
  370 + m_type = __TDOUBLE;
370 m_size = 0; 371 m_size = 0;
371 m_anynul = 0; 372 m_anynul = 0;
372 m_data = NULL; 373 m_data = NULL;
src/fits/GFitsTableFltCol.cpp
@@ -295,6 +295,7 @@ void GFitsTableFltCol::set_nullval(const float* value) @@ -295,6 +295,7 @@ void GFitsTableFltCol::set_nullval(const float* value)
295 void GFitsTableFltCol::init_members(void) 295 void GFitsTableFltCol::init_members(void)
296 { 296 {
297 // Initialise members 297 // Initialise members
  298 + m_type = __TFLOAT;
298 m_size = 0; 299 m_size = 0;
299 m_anynul = 0; 300 m_anynul = 0;
300 m_data = NULL; 301 m_data = NULL;
src/fits/GFitsTableLngCol.cpp
@@ -349,6 +349,7 @@ void GFitsTableLngCol::set_nullval(const long* value) @@ -349,6 +349,7 @@ void GFitsTableLngCol::set_nullval(const long* value)
349 void GFitsTableLngCol::init_members(void) 349 void GFitsTableLngCol::init_members(void)
350 { 350 {
351 // Initialise members 351 // Initialise members
  352 + m_type = __TLONG;
352 m_size = 0; 353 m_size = 0;
353 m_anynul = 0; 354 m_anynul = 0;
354 m_data = NULL; 355 m_data = NULL;
src/fits/GFitsTableShtCol.cpp
@@ -286,6 +286,7 @@ void GFitsTableShtCol::set_nullval(const short* value) @@ -286,6 +286,7 @@ void GFitsTableShtCol::set_nullval(const short* value)
286 void GFitsTableShtCol::init_members(void) 286 void GFitsTableShtCol::init_members(void)
287 { 287 {
288 // Initialise members 288 // Initialise members
  289 + m_type = __TSHORT;
289 m_size = 0; 290 m_size = 0;
290 m_anynul = 0; 291 m_anynul = 0;
291 m_data = NULL; 292 m_data = NULL;
src/fits/GFitsTableStrCol.cpp
@@ -287,6 +287,7 @@ void GFitsTableStrCol::set_nullstr(const std::string string) @@ -287,6 +287,7 @@ void GFitsTableStrCol::set_nullstr(const std::string string)
287 void GFitsTableStrCol::init_members(void) 287 void GFitsTableStrCol::init_members(void)
288 { 288 {
289 // Initialise members 289 // Initialise members
  290 + m_type = __TSTRING;
290 m_size = 0; 291 m_size = 0;
291 m_num_subs = 0; 292 m_num_subs = 0;
292 m_data = NULL; 293 m_data = NULL;
src/rsp/GLATResponse.cpp
@@ -284,10 +284,10 @@ void GLATResponse::save(const std::string&amp; rspname) const @@ -284,10 +284,10 @@ void GLATResponse::save(const std::string&amp; rspname) const
284 hdu_psf_sigma.extname("SIGMA"); 284 hdu_psf_sigma.extname("SIGMA");
285 285
286 // Append HDUs to FITS file 286 // Append HDUs to FITS file
287 - fits.append(&hdu_psf_bounds);  
288 - fits.append(&hdu_psf_psf);  
289 - fits.append(&hdu_psf_norm);  
290 - fits.append(&hdu_psf_sigma); 287 + fits.append_hdu(hdu_psf_bounds);
  288 + fits.append_hdu(hdu_psf_psf);
  289 + fits.append_hdu(hdu_psf_norm);
  290 + fits.append_hdu(hdu_psf_sigma);
291 291
292 // Save FITS file 292 // Save FITS file
293 fits.save(); 293 fits.save();
test/test_GFits.cpp
@@ -306,7 +306,7 @@ void test_create(void) @@ -306,7 +306,7 @@ void test_create(void)
306 fits.open("test.fits"); 306 fits.open("test.fits");
307 GFitsDblImage image; 307 GFitsDblImage image;
308 GFitsHDU hdu(image); 308 GFitsHDU hdu(image);
309 - fits.append(&hdu); 309 + fits.append_hdu(hdu);
310 //cout << fits; 310 //cout << fits;
311 fits.save(); 311 fits.save();
312 } 312 }
@@ -334,7 +334,7 @@ void test_create(void) @@ -334,7 +334,7 @@ void test_create(void)
334 } 334 }
335 } 335 }
336 GFitsHDU hdu(image); 336 GFitsHDU hdu(image);
337 - fits.append(&hdu); 337 + fits.append_hdu(hdu);
338 //cout << fits; 338 //cout << fits;
339 fits.save(); 339 fits.save();
340 } 340 }
@@ -372,23 +372,27 @@ void test_create(void) @@ -372,23 +372,27 @@ void test_create(void)
372 cout << "."; 372 cout << ".";
373 373
374 // Attach binary table 374 // Attach binary table
  375 +cout << "start" << endl;
375 try { 376 try {
376 // Re-open FITS file 377 // Re-open FITS file
377 GFits fits; 378 GFits fits;
378 fits.open("test.fits"); 379 fits.open("test.fits");
  380 +cout << "1" << endl;
379 381
380 // Create binary Table with 10 rows 382 // Create binary Table with 10 rows
381 int nrows = 10; 383 int nrows = 10;
382 GFitsBinTable table = GFitsBinTable(nrows); 384 GFitsBinTable table = GFitsBinTable(nrows);
383 GFitsTableDblCol first = GFitsTableDblCol(nrows); 385 GFitsTableDblCol first = GFitsTableDblCol(nrows);
384 GFitsTableDblCol second = GFitsTableDblCol(nrows); 386 GFitsTableDblCol second = GFitsTableDblCol(nrows);
385 - table.append_column(&first);  
386 - table.append_column(&second); 387 +cout << "2" << endl;
  388 + table.append_column(first);
  389 +cout << "3" << endl;
  390 + table.append_column(second);
387 cout << table; 391 cout << table;
388 392
389 // Create HDU and append to FILE file 393 // Create HDU and append to FILE file
390 GFitsHDU hdu(table); 394 GFitsHDU hdu(table);
391 - fits.append(&hdu); 395 + fits.append_hdu(hdu);
392 // cout << fits; 396 // cout << fits;
393 397
394 // Save FITS file 398 // Save FITS file