Commit 79e6207ffa3da3e7fa69a7fd9721d4953cd2b9ba

Authored by knodlseder
1 parent 04bea59f

implement saving

include/GFitsHDU.hpp
@@ -10,6 +10,11 @@ @@ -10,6 +10,11 @@
10 * (at your option) any later version. * 10 * (at your option) any later version. *
11 * * 11 * *
12 ***************************************************************************/ 12 ***************************************************************************/
  13 +/**
  14 + * @file GFitsHDU.hpp
  15 + * @brief GFitsHDU class definition.
  16 + * @author J. Knodlseder
  17 + */
13 18
14 #ifndef GFITSHDU_HPP 19 #ifndef GFITSHDU_HPP
15 #define GFITSHDU_HPP 20 #define GFITSHDU_HPP
@@ -24,8 +29,15 @@ @@ -24,8 +29,15 @@
24 /* __ Namespaces _________________________________________________________ */ 29 /* __ Namespaces _________________________________________________________ */
25 30
26 31
27 -/***************************************************************************  
28 - * GFitsHDU class definition * 32 +/***********************************************************************//**
  33 + * @class GFitsHDU
  34 + *
  35 + * @brief Implements the FITS Header Data Unit (HDU)
  36 + *
  37 + * The HDU is the basic unit of a FITS file. Each HDU consists of a header
  38 + * and a data area. The header is composed of cards and is implemented by
  39 + * the GFitsHeader class. The data are is either an image or a table and
  40 + * is implemented by the abstract GFitsData base class.
29 ***************************************************************************/ 41 ***************************************************************************/
30 class GFitsHDU { 42 class GFitsHDU {
31 43
src/fits/GFitsHDU.cpp
@@ -39,9 +39,8 @@ @@ -39,9 +39,8 @@
39 = = 39 = =
40 ==========================================================================*/ 40 ==========================================================================*/
41 41
42 -/***************************************************************************  
43 - * Constructor *  
44 - * ----------------------------------------------------------------------- * 42 +/***********************************************************************//**
  43 + * @brief Constructor
45 ***************************************************************************/ 44 ***************************************************************************/
46 GFitsHDU::GFitsHDU() 45 GFitsHDU::GFitsHDU()
47 { 46 {
@@ -53,9 +52,10 @@ GFitsHDU::GFitsHDU() @@ -53,9 +52,10 @@ GFitsHDU::GFitsHDU()
53 } 52 }
54 53
55 54
56 -/***************************************************************************  
57 - * Copy constructor *  
58 - * ----------------------------------------------------------------------- * 55 +/***********************************************************************//**
  56 + * @brief Copy constructor
  57 + *
  58 + * @param hdu HDU from which the instance should be constructed
59 ***************************************************************************/ 59 ***************************************************************************/
60 GFitsHDU::GFitsHDU(const GFitsHDU& hdu) 60 GFitsHDU::GFitsHDU(const GFitsHDU& hdu)
61 { 61 {
@@ -70,9 +70,8 @@ GFitsHDU::GFitsHDU(const GFitsHDU& hdu) @@ -70,9 +70,8 @@ GFitsHDU::GFitsHDU(const GFitsHDU& hdu)
70 } 70 }
71 71
72 72
73 -/***************************************************************************  
74 - * Destructor *  
75 - * ----------------------------------------------------------------------- * 73 +/***********************************************************************//**
  74 + * @brief Destructor
76 ***************************************************************************/ 75 ***************************************************************************/
77 GFitsHDU::~GFitsHDU() 76 GFitsHDU::~GFitsHDU()
78 { 77 {
@@ -90,9 +89,10 @@ GFitsHDU::~GFitsHDU() @@ -90,9 +89,10 @@ GFitsHDU::~GFitsHDU()
90 = = 89 = =
91 ==========================================================================*/ 90 ==========================================================================*/
92 91
93 -/***************************************************************************  
94 - * Assignment operator *  
95 - * ----------------------------------------------------------------------- * 92 +/***********************************************************************//**
  93 + * @brief Assignment operator
  94 + *
  95 + * @param hdu HDU which should be assigned
96 ***************************************************************************/ 96 ***************************************************************************/
97 GFitsHDU& GFitsHDU::operator= (const GFitsHDU& hdu) 97 GFitsHDU& GFitsHDU::operator= (const GFitsHDU& hdu)
98 { 98 {
@@ -121,9 +121,16 @@ GFitsHDU& GFitsHDU::operator= (const GFitsHDU& hdu) @@ -121,9 +121,16 @@ GFitsHDU& GFitsHDU::operator= (const GFitsHDU& hdu)
121 = = 121 = =
122 ==========================================================================*/ 122 ==========================================================================*/
123 123
124 -/***************************************************************************  
125 - * Open HDU *  
126 - * ----------------------------------------------------------------------- * 124 +/***********************************************************************//**
  125 + * @brief Open HDU
  126 + *
  127 + * @param fptr FITS file pointer
  128 + * @param hdunum Number of HDU (starting from 1)
  129 + *
  130 + * Opens an (existing) HDU in the FITS file. This method does NOT create any
  131 + * HDU if it does not exist. Opening consists of fetching all header cards
  132 + * (by opening an associated GFitsHeader instance) and of opening the data
  133 + * area (which can be of type Image or Table)
127 ***************************************************************************/ 134 ***************************************************************************/
128 void GFitsHDU::open(__fitsfile* fptr, int hdunum) 135 void GFitsHDU::open(__fitsfile* fptr, int hdunum)
129 { 136 {
@@ -179,40 +186,82 @@ void GFitsHDU::open(__fitsfile* fptr, int hdunum) @@ -179,40 +186,82 @@ void GFitsHDU::open(__fitsfile* fptr, int hdunum)
179 } 186 }
180 187
181 188
182 -/***************************************************************************  
183 - * Save HDU to FITS file *  
184 - * ----------------------------------------------------------------------- * 189 +/***********************************************************************//**
  190 + * @brief Saves HDU
  191 + *
  192 + * Save the HDU to the FITS file. In case that the HDU does not exist in the
  193 + * file it will be created (by the corresponding opening routines of the
  194 + * GFitsData instance).
  195 + * In the special case that no first HDU exists an empty primary image is
  196 + * created.
185 ***************************************************************************/ 197 ***************************************************************************/
186 -/// NOT YET IMPLEMENTED !!!  
187 void GFitsHDU::save(void) 198 void GFitsHDU::save(void)
188 { 199 {
189 -cout << "entry" << endl;  
190 // Move to HDU 200 // Move to HDU
191 int status = 0; 201 int status = 0;
192 status = __ffmahd(&m_fitsfile, (m_fitsfile.HDUposition)+1, NULL, &status); 202 status = __ffmahd(&m_fitsfile, (m_fitsfile.HDUposition)+1, NULL, &status);
193 - cout << status << endl;  
194 203
195 - // If HDU does not exist then create it now 204 + // If HDU does not yet exist in file then create it now. This works even
  205 + // in the case that the actual HDU has no associated header and/or data
  206 + // (these will be created automatically)
  207 + if (status == 107) {
  208 + status = 0;
  209 + switch (m_type) {
  210 + case 0: // Image HDU
  211 + if (m_data == NULL) m_data = new GFitsImage();
  212 + m_data->open(&m_fitsfile);
  213 + break;
  214 + case 1: // ASCII Table HDU
  215 + if (m_data == NULL) m_data = new GFitsAsciiTable();
  216 + m_data->open(&m_fitsfile);
  217 + break;
  218 + case 2: // Binary Table HDU
  219 + if (m_data == NULL) m_data = new GFitsBinTable();
  220 + m_data->open(&m_fitsfile);
  221 + break;
  222 + default:
  223 + throw GException::fits_unknown_HDU_type(G_SAVE, m_type);
  224 + break;
  225 + }
  226 + if (m_header == NULL) m_header = new GFitsHeader();
  227 + m_header->open(&m_fitsfile);
  228 + }
196 229
  230 + // If they exist then save data and header
  231 + if (m_data != NULL) {
  232 + m_data->save();
  233 + if (m_header != NULL)
  234 + m_header->save();
  235 + }
197 236
198 - //if (status != 0)  
199 - // throw GException::fits_error(G_SAVE, status);  
200 -  
201 - // Save header  
202 - //...  
203 -  
204 - // Save data  
205 - //... 237 + // ... otherwise make sure that the FITS file has at least a primary
  238 + // image.
  239 + else {
  240 +
  241 + // Special case: The first HDU does not exist. A FITS file needs at least
  242 + // one primary HDU. Thus an empty image is created.
  243 + if (m_fitsfile.HDUposition == 0) {
  244 + status = __ffcrim(&m_fitsfile, 8, 0, NULL, &status);
  245 + if (status != 0)
  246 + throw GException::fits_error(G_SAVE, status);
  247 + if (m_header != NULL)
  248 + m_header->save();
  249 + }
  250 +
  251 + } // endelse: no data were available
206 252
207 -cout << "exit" << endl;  
208 // Return 253 // Return
209 return; 254 return;
210 } 255 }
211 256
212 257
213 -/***************************************************************************  
214 - * Return pointer to column of table *  
215 - * ----------------------------------------------------------------------- * 258 +/***********************************************************************//**
  259 + * @brief Return pointer to column of table
  260 + *
  261 + * @param colname Name of FITS table column to be returned
  262 + *
  263 + * If this method is called for an image a 'fits_HDU_not_a_table' exception
  264 + * will be thrown.
216 ***************************************************************************/ 265 ***************************************************************************/
217 GFitsTableCol* GFitsHDU::column(const std::string& colname) const 266 GFitsTableCol* GFitsHDU::column(const std::string& colname) const
218 { 267 {
@@ -240,25 +289,26 @@ GFitsTableCol* GFitsHDU::column(const std::string&amp; colname) const @@ -240,25 +289,26 @@ GFitsTableCol* GFitsHDU::column(const std::string&amp; colname) const
240 } 289 }
241 290
242 291
243 -/***************************************************************************  
244 - * Setup minimal primary HDU *  
245 - * ----------------------------------------------------------------------- * 292 +/***********************************************************************//**
  293 + * @brief Setup minimal primary HDU
246 ***************************************************************************/ 294 ***************************************************************************/
247 void GFitsHDU::primary(void) 295 void GFitsHDU::primary(void)
248 { 296 {
249 // Free any allocated header and data 297 // Free any allocated header and data
250 free_members(); 298 free_members();
251 299
252 - // Allocate header  
253 - m_header = new GFitsHeader(); 300 + // Create primary image in memory
  301 + int status = 0;
  302 + __fitsfile* fptr;
  303 + status = __ffinit(&fptr, "mem://", &status);
  304 + status = __ffcrim(fptr, 8, 0, NULL, &status);
  305 +
  306 + // Open HDU
  307 + this->open(fptr,1);
254 308
255 - // Add cards  
256 - m_header->update(GFitsHeaderCard("SIMPLE", "T", "/ file does conform to FITS standard"));  
257 - m_header->update(GFitsHeaderCard("BITPIX", "8", "/ number of bits per data pixel"));  
258 - m_header->update(GFitsHeaderCard("NAXIS", "0", "/ number of data axes"));  
259 - m_header->update(GFitsHeaderCard("EXTEND", "T", "/ FITS dataset may contain extensions"));  
260 - m_header->update(GFitsHeaderCard("BZERO", "-128", "/ Make values Signed"));  
261 - m_header->update(GFitsHeaderCard("BSCALE", "1", "/ Make values Signed")); 309 + // Detach FITS file pointer
  310 + this->m_fitsfile.HDUposition = 0;
  311 + this->m_fitsfile.Fptr = NULL;
262 312
263 // Return 313 // Return
264 return; 314 return;
@@ -271,9 +321,8 @@ void GFitsHDU::primary(void) @@ -271,9 +321,8 @@ void GFitsHDU::primary(void)
271 = = 321 = =
272 ==========================================================================*/ 322 ==========================================================================*/
273 323
274 -/***************************************************************************  
275 - * Initialise class members *  
276 - * ----------------------------------------------------------------------- * 324 +/***********************************************************************//**
  325 + * @brief Initialise class members
277 ***************************************************************************/ 326 ***************************************************************************/
278 void GFitsHDU::init_members(void) 327 void GFitsHDU::init_members(void)
279 { 328 {
@@ -290,9 +339,10 @@ void GFitsHDU::init_members(void) @@ -290,9 +339,10 @@ void GFitsHDU::init_members(void)
290 } 339 }
291 340
292 341
293 -/***************************************************************************  
294 - * Copy class members *  
295 - * ----------------------------------------------------------------------- * 342 +/***********************************************************************//**
  343 + * @brief Copy class members
  344 + *
  345 + * @param hdu HDU to be copied
296 ***************************************************************************/ 346 ***************************************************************************/
297 void GFitsHDU::copy_members(const GFitsHDU& hdu) 347 void GFitsHDU::copy_members(const GFitsHDU& hdu)
298 { 348 {
@@ -308,9 +358,8 @@ void GFitsHDU::copy_members(const GFitsHDU&amp; hdu) @@ -308,9 +358,8 @@ void GFitsHDU::copy_members(const GFitsHDU&amp; hdu)
308 } 358 }
309 359
310 360
311 -/***************************************************************************  
312 - * Delete class members *  
313 - * ----------------------------------------------------------------------- * 361 +/***********************************************************************//**
  362 + * @brief Delete class members
314 ***************************************************************************/ 363 ***************************************************************************/
315 void GFitsHDU::free_members(void) 364 void GFitsHDU::free_members(void)
316 { 365 {
@@ -333,9 +382,11 @@ void GFitsHDU::free_members(void) @@ -333,9 +382,11 @@ void GFitsHDU::free_members(void)
333 = = 382 = =
334 ==========================================================================*/ 383 ==========================================================================*/
335 384
336 -/***************************************************************************  
337 - * Output operator *  
338 - * ----------------------------------------------------------------------- * 385 +/***********************************************************************//**
  386 + * @brief Output operator
  387 + *
  388 + * @param os Output stream into which the HDU will be dumped
  389 + * @param hdu HDU to be dumped
339 ***************************************************************************/ 390 ***************************************************************************/
340 ostream& operator<< (ostream& os, const GFitsHDU& hdu) 391 ostream& operator<< (ostream& os, const GFitsHDU& hdu)
341 { 392 {