GFitsHDU.cpp 16.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
/***************************************************************************
 *                   GFitsHDU.cpp  - FITS HDU handling class               *
 * ----------------------------------------------------------------------- *
 *  copyright (C) 2008-2014 by Juergen Knoedlseder                         *
 * ----------------------------------------------------------------------- *
 *                                                                         *
 *  This program is free software: you can redistribute it and/or modify   *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation, either version 3 of the License, or      *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *  This program is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
 *                                                                         *
 ***************************************************************************/
/**
 * @file GFitsHDU.cpp
 * @brief GFitsHDU class implementation.
 * @author Juergen Knoedlseder
 */

/* __ Includes ___________________________________________________________ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <cstdlib>
#include "GException.hpp"
#include "GTools.hpp"
#include "GFitsCfitsio.hpp"
#include "GFits.hpp"
#include "GFitsHDU.hpp"
#include "GFitsHeaderCard.hpp"
#include "GTools.hpp"

/* __ Method name definitions ____________________________________________ */
#define G_OPEN                                          "GFitsHDU::open(int)"
#define G_SAVE                                             "GFitsHDU::save()"
#define G_HEADER                                         "GFitsHDU::header()"
#define G_DATA                                             "GFitsHDU::data()"
#define G_COLUMN                             "GFitsHDU::column(std::string&)"
#define G_CONNECT                                  "GFitsHDU::connect(void*)"
#define G_MOVE_TO_HDU                               "GFitsHDU::move_to_hdu()"
#define G_GET_HDU_TYPE                             "GFitsHDU::get_hdu_type()"
#define G_NEW_IMAGE                                   "GFitsHDU::new_image()"

/* __ Definitions ________________________________________________________ */
#define DEBUG 0

/* __ Macros _____________________________________________________________ */

/* __ Coding definitions _________________________________________________ */

/* __ Debug definitions __________________________________________________ */



/*==========================================================================
 =                                                                         =
 =                         Constructors/destructors                        =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Void constructor
 ***************************************************************************/
GFitsHDU::GFitsHDU(void)
{
    // Initialise class members for clean destruction
    init_members();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Copy constructor
 *
 * @param[in] hdu HDU from which the instance should be constructed.
 ***************************************************************************/
GFitsHDU::GFitsHDU(const GFitsHDU& hdu)
{
    // Initialise class members for clean destruction
    init_members();

    // Copy members
    copy_members(hdu);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Destructor
 ***************************************************************************/
GFitsHDU::~GFitsHDU(void)
{
    // Free members
    free_members();

    // Return
    return;
}


/*==========================================================================
 =                                                                         =
 =                               Operators                                 =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Assignment operator
 *
 * @param[in] hdu HDU which should be assigned.
 ***************************************************************************/
GFitsHDU& GFitsHDU::operator= (const GFitsHDU& hdu)
{
    // Execute only if object is not identical
    if (this != &hdu) {

        // Free members
        free_members();

        // Initialise private members for clean destruction
        init_members();

        // Copy members
        copy_members(hdu);

    } // endif: object was not identical

    // Return this object
    return *this;
}


/*==========================================================================
 =                                                                         =
 =                             Public methods                              =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Set HDU extension name (EXTNAME keyword)
 *
 * @param[in] extname Name of HDU.
 *
 * This method sets the extension name of the HDU. The extension name will
 * be saved in the 'EXTNAME' header keyword. The header attached to the
 * HDU will be automatically updated by this method.
 ***************************************************************************/
void GFitsHDU::extname(const std::string& extname)
{
    // Set extension name
    m_name = extname;

    // If no header exists then create one now
    //if (m_header == NULL) m_header = new GFitsHeader();

    // Update header
    m_header.append(GFitsHeaderCard("EXTNAME", extname,
                                    "name of this extension"));

    // Return
    return;
}


/*==========================================================================
 =                                                                         =
 =                            Protected methods                            =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Connect HDU to FITS file
 *
 * @param[in] vptr FITS file pointer.
 *
 * Connects the HDU to the file specified by the FITS file pointer. Sets
 * also the HDU number (or extension number, starting from 0). This method
 * does nothing if the file pointer in not valid.
 ***************************************************************************/
void GFitsHDU::connect(void* vptr)
{
    // Continue only if file pointer is valid
    if (vptr != NULL) {

        // Connect HDU by copying the file pointer
        FPTR_COPY(m_fitsfile, vptr);

        // Extract HDU number from file pointer
        m_hdunum = FPTR(m_fitsfile)->HDUposition;

        // Connect data
        data_connect(vptr);

    } // endif: file pointer was valid

    // Return
    return;
}


/***********************************************************************//**
 * @brief Move FITS file pointer to HDU
 *
 * @exception GException::fits_file_not_open
 *            No FITS file has been opened.
 *
 * Moves FITS file pointer to the actual HDU. This operation should preceed
 * any FITS file manipulation.
 ***************************************************************************/
void GFitsHDU::move_to_hdu(void)
{
    // Throw an exception if FITS file is not open
    if (FPTR(m_fitsfile)->Fptr == NULL) {
        throw GException::fits_file_not_open(G_MOVE_TO_HDU, 
              "Open file before moving file pointer to HDU.");
    }

    // Move to HDU
    gammalib::fits_move_to_hdu(G_MOVE_TO_HDU, m_fitsfile, m_hdunum+1);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Get HDU type from FITS file
 *
 * @exception GException::fits_file_not_open
 *            No FITS file has been opened.
 * @exception GException::fits_hdu_not_found
 *            Requested HDU not found.
 ***************************************************************************/
GFitsHDU::HDUType GFitsHDU::get_hdu_type(void) const
{
    // Initialise HDU type
    int type = 0;

    // Throw an exception if FITS file is not open
    if (FPTR(m_fitsfile)->Fptr == NULL) {
        throw GException::fits_file_not_open(G_GET_HDU_TYPE, 
              "Open file before requesting HDU type.");
    }

    // Get HDU type
    int status = 0;
    status     = __ffghdt(FPTR(m_fitsfile), &type, &status);
    if (status != 0) {
        throw GException::fits_error(G_GET_HDU_TYPE, status);
    }

    // Return HDU type
    return (HDUType)type;
}


/***********************************************************************//**
 * @brief Open HDU
 *
 * @param[in] vptr FITS file pointer.
 * @param[in] hdunum Number of HDU (starting from 0).
 *
 * @exception GException::fits_file_not_open
 *            FITS file pointer does not point to an open FITS file
 * @exception GException::fits_error
 *            Unable to open FITS HDU.
 *
 * Opens an (existing) HDU in the FITS file. This method does NOT create any
 * HDU if it does not exist. Opening consists of fetching all header cards
 * (by opening an associated GFitsHeader instance) and of opening the data
 * area (which can be of type Image or Table)
 ***************************************************************************/
void GFitsHDU::open(void* vptr, int hdunum)
{
    // Verify that FITS file pointer is valid
    if (vptr == NULL) {
        throw GException::fits_file_not_open(G_OPEN,
              "FITS file pointer does not point to an open FITS file.");
    }

    // Move to HDU
    gammalib::fits_move_to_hdu(G_OPEN, vptr, hdunum+1);

    // Save the FITS file pointer and the HDU number
    FPTR_COPY(m_fitsfile, vptr);
    m_hdunum = hdunum;

    // Load HDU header
    m_header.load(FPTR(m_fitsfile));

    // Open HDU data area
    data_open(FPTR(m_fitsfile));

    // Get HDU name from header. If no name was found and this is the primary
    // HDU then set the name to "Primary", otherwise to "NoName".
    if (m_header.contains("EXTNAME")) {
        m_name = gammalib::strip_whitespace(m_header.string("EXTNAME"));
    }
    else {
        m_name.clear();
    }
    if (m_name.length() == 0) {
        if (hdunum == 0) {
            m_name = "Primary";
        }
        else {
            m_name = "NoName";
        }
    }

    // Return
    return;
}


/***********************************************************************//**
 * @brief Saves HDU
 *
 * Save the HDU to the FITS file. In case that the HDU does not exist in the
 * file it will be created (by the save_data() method). In the special case
 * that no first HDU exists, an empty primary image is created.
 *
 * @todo Put the m_header.save(FPTR(m_fitsfile)) call in the data_save()
 * methods
 ***************************************************************************/
void GFitsHDU::save(void)
{
    // Debug header
    #if DEBUG
    std::cout << "GFitsHDU::save() -->" << std::endl;
    #endif

    // Save data. This method allows saving past the file and will
    // physically append a new HDU to a FITS file. It has to be called
    // before the saving of the header cards, since header card
    // saving requires the HDU to exist in the FITS file.
    data_save();

    // Save header cards (assumes implicitely that the HDU exists in
    // FITS file)
    m_header.save(FPTR(m_fitsfile));

    // Debug trailer
    #if DEBUG
    std::cout << "<-- GFitsHDU::save" << std::endl;
    #endif

    // Return
    return;
}


/***********************************************************************//**
 * @brief Print basic HDU information
 *
 * @param[in] chatter Chattiness (defaults to NORMAL).
 * @return String containing basic HDU information.
 ***************************************************************************/
std::string GFitsHDU::print_hdu(const GChatter& chatter) const
{
    // Initialise result string
    std::string result;

    // Continue only if chatter is not silent
    if (chatter != SILENT) {

        // Append HDU information
        result.append(gammalib::parformat("HDU number"));
        result.append(gammalib::str(m_hdunum)+"\n");
        result.append(gammalib::parformat("HDU name")+m_name);

    } // endif: chatter was not silent

    // Return result
    return result;
}


/***********************************************************************//**
 * @brief Return typecode as string
 *
 * @param[in] type Type code.
 ***************************************************************************/
std::string GFitsHDU::typecode(int type) const
{
    // Allocate string
    std::string result;

    // Set typecode
    switch (type) {
    case __TBIT:
        result = "bit";
        break;
    case __TBYTE:
        result = "unsigned byte";
        break;
    case __TSBYTE:
        result = "signed byte";
        break;
    case __TLOGICAL:
        result = "boolean";
        break;
    case __TSTRING:
        result = "string";
        break;
    case __TUSHORT:
        result = "unsigned short integer";
        break;
    case __TSHORT:
        result = "short integer";
        break;
    case __TULONG:
        result = "unsigned long integer";
        break;
    case __TLONG:
        result = "long integer";
        break;
    case __TFLOAT:
        result = "single precision floating point";
        break;
    case __TLONGLONG:
        result = "long long integer";
        break;
    case __TDOUBLE:
        result = "double precision floating point";
        break;
    default:
        result = "unsupported format";
        break;
    }

    // Return result
    return result;
}


/*==========================================================================
 =                                                                         =
 =                             Private methods                             =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Initialise class members
 *
 * Sets all class members to well defined values.
 ***************************************************************************/
void GFitsHDU::init_members(void)
{
    // Allocate FITS file pointer. As the pointer is of type void the C-style
    // memory allocation function malloc is used as this function does not
    // deal with types but always returns a void pointer
    m_fitsfile = malloc(sizeof(__fitsfile));

    // Initialise FITS file pointer
    FPTR(m_fitsfile)->HDUposition = 0;
    FPTR(m_fitsfile)->Fptr        = NULL;

    // Initialise members
    m_hdunum = 0;
    m_name.clear();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Copy class members
 *
 * @param[in] hdu HDU to be copied.
 *
 * Assumes that all memory has been freed correctly before calling.
 ***************************************************************************/
void GFitsHDU::copy_members(const GFitsHDU& hdu)
{
    // Copy members
    FPTR_COPY(m_fitsfile, hdu.m_fitsfile);
    m_hdunum = hdu.m_hdunum;
    m_name   = hdu.m_name;
    m_header = hdu.m_header;

    // Return
    return;
}


/***********************************************************************//**
 * @brief Delete class members
 ***************************************************************************/
void GFitsHDU::free_members(void)
{
    // Free memory. Note that the malloc function was used for allocation,
    // hence the free function needs to be called for freeing.
    if (m_fitsfile != NULL) free(m_fitsfile);

    // Signal free pointers
    m_fitsfile = NULL;

    // Return
    return;
}