Commit 72fc1852188f1b99a121b6b90b56c838110e7484

Authored by knodlseder
1 parent 4f8446be

start implementing table column saving

include/GException.hpp
... ... @@ -169,6 +169,7 @@ public:
169 169 class fits_hdu_not_found : public GExceptionHandler {
170 170 public:
171 171 fits_hdu_not_found(string origin, string extname, int status = 0);
  172 + fits_hdu_not_found(string origin, int extno, int status = 0);
172 173 };
173 174  
174 175 // FITS unknown HDU type
... ...
include/GFitsCfitsio.hpp
... ... @@ -47,6 +47,7 @@
47 47 #define __ffgncl(A, B, C) ffgncl(A, B, C)
48 48 #define __ffgsv(A, B, C, D, E, F, G, H, I) ffgsv(A, B, C, D, E, F, G, H, I)
49 49 #define __ffgtcl(A, B, C, D, E, F) ffgtcl(A, B, C, D, E, F)
  50 +#define __fficol(A, B, C, D, E) fficol(A, B, C, D, E)
50 51 #define __ffinit(A, B, C) ffinit(A, B, C)
51 52 #define __ffopen(A, B, C, D) ffopen(A, B, C, D)
52 53 #define __ffmahd(A, B, C, D) ffmahd(A, B, C, D)
... ... @@ -97,6 +98,7 @@ typedef fitsfile __fitsfile;
97 98 #define __ffgncl(A, B, C) __dummy()
98 99 #define __ffgsv(A, B, C, D, E, F, G, H, I) __dummy()
99 100 #define __ffgtcl(A, B, C, D, E, F) __dummy()
  101 +#define __fficol(A, B, C, D, E) __dummy()
100 102 #define __ffinit(A, B, C) __dummy()
101 103 #define __ffmahd(A, B, C, D) __dummy()
102 104 #define __ffopen(A, B, C, D) __dummy()
... ...
include/GFitsTableCol.hpp
... ... @@ -51,6 +51,7 @@ public:
51 51 GFitsTableCol& operator= (const GFitsTableCol& column);
52 52  
53 53 // Virtual Methods
  54 + virtual void save(void) = 0;
54 55 virtual std::string string(const int& row, const int& inx = 0) = 0;
55 56 virtual double real(const int& row, const int& inx = 0) = 0;
56 57 virtual int integer(const int& row, const int& inx = 0) = 0;
... ...
include/GFitsTableDblCol.hpp
... ... @@ -51,6 +51,7 @@ public:
51 51 const double& operator() (const int& row, const int& inx = 0) const;
52 52  
53 53 // Methods
  54 + void save(void);
54 55 std::string string(const int& row, const int& inx = 0);
55 56 double real(const int& row, const int& inx = 0);
56 57 int integer(const int& row, const int& inx = 0);
... ...
include/GFitsTableFltCol.hpp
... ... @@ -48,6 +48,7 @@ public:
48 48 GFitsTableFltCol& operator= (const GFitsTableFltCol& column);
49 49  
50 50 // Methods
  51 + void save(void);
51 52 std::string string(const int& row, const int& col = 0);
52 53 double real(const int& row, const int& col = 0);
53 54 int integer(const int& row, const int& col = 0);
... ...
include/GFitsTableLngCol.hpp
... ... @@ -50,6 +50,7 @@ public:
50 50 const long& operator() (const int& row, const int& inx = 0) const;
51 51  
52 52 // Methods
  53 + void save(void);
53 54 std::string string(const int& row, const int& col = 0);
54 55 double real(const int& row, const int& col = 0);
55 56 int integer(const int& row, const int& col = 0);
... ...
include/GFitsTableShtCol.hpp
... ... @@ -39,6 +39,7 @@ public:
39 39 GFitsTableShtCol& operator= (const GFitsTableShtCol& column);
40 40  
41 41 // Methods
  42 + void save(void);
42 43 std::string string(const int& row, const int& col = 0);
43 44 double real(const int& row, const int& col = 0);
44 45 int integer(const int& row, const int& col = 0);
... ...
include/GFitsTableStrCol.hpp
... ... @@ -39,6 +39,7 @@ public:
39 39 GFitsTableStrCol& operator= (const GFitsTableStrCol& column);
40 40  
41 41 // Methods
  42 + void save(void);
42 43 std::string string(const int& row, const int& col = 0);
43 44 double real(const int& row, const int& col = 0);
44 45 int integer(const int& row, const int& col = 0);
... ...
src/GException.cpp
... ... @@ -353,6 +353,24 @@ GException::fits_hdu_not_found::fits_hdu_not_found(string origin,
353 353  
354 354  
355 355 /***********************************************************************//**
  356 + * @brief FITS error: HDU not found in FITS file
  357 + *
  358 + * @param[in] origin Method that throws the error.
  359 + * @param[in] extno Extension number (starting from 1).
  360 + * @param[in] status cfitsio status.
  361 + ***************************************************************************/
  362 +GException::fits_hdu_not_found::fits_hdu_not_found(string origin,
  363 + int extno,
  364 + int status)
  365 +{
  366 + m_origin = origin;
  367 + m_message = "HDU number '" + str(extno) + "' not found in FITS file";
  368 + if (status != 0)
  369 + m_message += " (status=" + str(status) + ")";
  370 +}
  371 +
  372 +
  373 +/***********************************************************************//**
356 374 * @brief FITS error: HDU of unknown type found
357 375 *
358 376 * @param[in] origin Method that throws the error.
... ...
src/fits/GFitsTable.cpp
... ... @@ -166,6 +166,13 @@ GFitsTable& GFitsTable::operator= (const GFitsTable& table)
166 166 *
167 167 * @param[in] fptr FITS file pointer
168 168 *
  169 + * @exception GException::fits_hdu_not_found
  170 + * Specified HDU not found in FITS file.
  171 + * @exception GException::fits_error
  172 + * A CFITSIO error occured during loading the table.
  173 + * @exception GException::fits_unknown_coltype
  174 + * FITS column of unknown type has been found in the FITS file.
  175 + *
169 176 * Builds a description of the table in memory.
170 177 * Columns are not loaded but column descriptors are allocated.
171 178 * The column data will only be loaded once it needs to be accessed.
... ... @@ -176,7 +183,8 @@ void GFitsTable::open(__fitsfile* fptr)
176 183 int status = 0;
177 184 status = __ffmahd(fptr, (fptr->HDUposition)+1, NULL, &status);
178 185 if (status != 0)
179   - throw GException::fits_error(G_OPEN, status);
  186 + throw GException::fits_hdu_not_found(G_OPEN, (fptr->HDUposition)+1,
  187 + status);
180 188  
181 189 // Save FITS file pointer
182 190 m_fitsfile = *fptr;
... ... @@ -259,6 +267,11 @@ void GFitsTable::open(__fitsfile* fptr)
259 267  
260 268 /***********************************************************************//**
261 269 * @brief Save FITS table
  270 + *
  271 + * @exception GException::fits_error
  272 + * A CFITSIO error occured in this method.
  273 + * @exception GException::fits_bad_col_length
  274 + * Table columns have inconsistent lengths.
262 275 ***************************************************************************/
263 276 void GFitsTable::save(void)
264 277 {
... ... @@ -322,6 +335,89 @@ void GFitsTable::save(void)
322 335 else if (status != 0)
323 336 throw GException::fits_error(G_SAVE, status);
324 337  
  338 + // Determine number of columns in table
  339 + int num_cols = 0;
  340 + status = __ffgncl(&m_fitsfile, &num_cols, &status);
  341 + if (status != 0)
  342 + throw GException::fits_error(G_SAVE, status);
  343 +
  344 + // If we have no columns in the table then delete all columns from FITS table
  345 + if (m_columns == NULL) {
  346 + // TBD: Delete all columns from FITS table
  347 + }
  348 +
  349 + // ... else update the FITS table
  350 + else {
  351 +
  352 + // Make sure that all columns have the same length. Columns with zero
  353 + // length will not be considered
  354 + int length = 0;
  355 + for (int i = 0; i < m_cols; ++i) {
  356 + if (m_columns[i] != NULL && m_columns[i]->length() > 0) {
  357 + if (length == 0)
  358 + length = m_columns[i]->length();
  359 + else if (m_columns[i]->length() != length) {
  360 + throw GException::fits_bad_col_length(G_SAVE,
  361 + m_columns[i]->length(),
  362 + length);
  363 + }
  364 + }
  365 + }
  366 +
  367 + // If the table length differs from number of rows in the FITS file
  368 + // then ????
  369 + // TBD
  370 +
  371 + // Update all columns. The 'm_colnum' field specifies where in the
  372 + // FITS file the column resides. If 'm_colnum=0' then we append
  373 + // another column
  374 + for (int i = 0; i < m_cols; ++i) {
  375 + if (m_columns[i] != NULL) {
  376 + if (m_columns[i]->m_colnum == 0) {
  377 +
  378 +cout << "needs appending of column to FITS file " << num_cols << endl;
  379 + // Append column to FITS file
  380 + status = __fficol(&m_fitsfile, num_cols, get_ttype(i),
  381 + get_tform(i), &status);
  382 + if (status != 0)
  383 + throw GException::fits_error(G_SAVE, status);
  384 +
  385 + // Update the 'm_colnum' field in table
  386 + m_columns[i]->m_colnum = num_cols;
  387 +
  388 + // Increment number of columns in FITS file
  389 + num_cols++;
  390 + }
  391 + else {
  392 + cout << "write column into " << m_columns[i]->m_colnum << endl;
  393 + //m_columns[i]->save();
  394 + }
  395 + }
  396 + }
  397 +
  398 + // Delete all unused columns of FITS file (from last to first!!!)
  399 + for (int colnum = num_cols; colnum > 0; --colnum) {
  400 +
  401 + // Initialise column usage flag
  402 + int used = 0;
  403 +
  404 + // Search column number in table
  405 + for (int i = 0; i < m_cols; ++i) {
  406 + if (m_columns[i] != NULL && m_columns[i]->m_colnum == colnum) {
  407 + used = 1;
  408 + break;
  409 + }
  410 + }
  411 +
  412 + // Delete unused column
  413 + if (!used) {
  414 + // delete FITS column 'colnum'
  415 + }
  416 +
  417 + } // endfor: Looped over all FITS columns
  418 +
  419 + } // endelse: FITS table has been updated
  420 +
325 421 //cout << "GFitsTable::save exit" << endl;
326 422 // Return
327 423 return;
... ... @@ -469,8 +565,13 @@ void GFitsTable::insert_column(int colnum, GFitsTableCol&amp; column)
469 565 break;
470 566 }
471 567  
472   - // Set column attributes
473   - //m_columns[colnum]->m_colnum = colnum + 1; // IS THIS REALLY NEEDED???
  568 + // Set column number (this is important to known from which column in the
  569 + // FITS file the data should be read
  570 + //// NO NO NO !!! Only for columns that exist in the FITS file !!!
  571 + //// m_columns[colnum]->m_colnum = colnum + 1;
  572 + // Reset column number since column does not already exist in FITS
  573 + // file
  574 + m_columns[colnum]->m_colnum = 0;
474 575  
475 576 // Return
476 577 return;
... ...
src/fits/GFitsTableDblCol.cpp
... ... @@ -20,6 +20,7 @@
20 20 /* __ Namespaces _________________________________________________________ */
21 21  
22 22 /* __ Method name definitions ____________________________________________ */
  23 +#define G_SAVE "GFitsTableDblCol::save(void)"
23 24 #define G_STRING "GFitsTableDblCol::string(const int&, const int&)"
24 25 #define G_REAL "GFitsTableDblCol::real(const int&, const int&)"
25 26 #define G_INTEGER "GFitsTableDblCol::integer(const int&, const int&)"
... ... @@ -200,6 +201,36 @@ const double&amp; GFitsTableDblCol::operator() (const int&amp; row, const int&amp; inx)
200 201 ==========================================================================*/
201 202  
202 203 /***********************************************************************//**
  204 + * @brief Save table column into FITS file
  205 + *
  206 + * @exception GException::fits_hdu_not_found
  207 + * Specified HDU not found in FITS file.
  208 + ***************************************************************************/
  209 +void GFitsTableDblCol::save(void)
  210 +{
  211 + // Continue only if a FITS file is connected
  212 + if (m_fitsfile.Fptr != NULL) {
  213 +
  214 + // Move to the HDU
  215 + int status = 0;
  216 + status = __ffmahd(&m_fitsfile, (m_fitsfile.HDUposition)+1, NULL,
  217 + &status);
  218 + if (status != 0)
  219 + throw GException::fits_hdu_not_found(G_SAVE,
  220 + (m_fitsfile.HDUposition)+1,
  221 + status);
  222 +
  223 + // Save the column data
  224 + // TBD
  225 +
  226 + } // endif: FITS file was connected
  227 +
  228 + // Return
  229 + return;
  230 +}
  231 +
  232 +
  233 +/***********************************************************************//**
203 234 * @brief Get string value
204 235 *
205 236 * @param[in] row Table row.
... ...