GFitsHDU.hpp 14.8 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
/***************************************************************************
 *            GFitsHDU.hpp - Abstract FITS extension base class            *
 * ----------------------------------------------------------------------- *
 *  copyright (C) 2008-2018 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.hpp
 * @brief Abstract FITS extension base class definition.
 * @author Juergen Knoedlseder
 */

#ifndef GFITSHDU_HPP
#define GFITSHDU_HPP

/* __ Includes ___________________________________________________________ */
#include <string>
#include "GBase.hpp"
#include "GFitsHeader.hpp"
#include "GFitsHeaderCard.hpp"


/***********************************************************************//**
 * @class GFitsHDU
 *
 * @brief Abstract FITS extension base class
 *
 * This class defines the abstract interface for a FITS extension, also known
 * as Header Data Unit (HDU). Each HDU consists of a header and a data area.
 * The header is composed of cards and is implemented by the GFitsHeader
 * class. The data are is either an image or a table which are implemented
 * as derived classes from GFitsHDU.
 *
 * @todo Implement GFitsHDU* select(const std::string& expr) that applies
 * to a table HDU for row selection.
 ***************************************************************************/
class GFitsHDU : public GBase {

    // Friend classes
    friend class GFits;

public:
    // Constructors and destructors
    GFitsHDU(void);
    GFitsHDU(const GFitsHDU& hdu);
    virtual ~GFitsHDU(void);

    // Operators
    GFitsHDU& operator=(const GFitsHDU& hdu);

    // Public enumerators
    enum HDUType {
        HT_IMAGE = 0,
        HT_ASCII_TABLE = 1,
        HT_BIN_TABLE = 2
    };

    // Pure virtual methods
    virtual void        clear(void) = 0;
    virtual GFitsHDU*   clone(void) const = 0;
    virtual std::string classname(void) const = 0;
    virtual HDUType     exttype(void) const = 0;
    virtual std::string print(const GChatter& chatter = NORMAL) const = 0;

    // Implemented methods
    int                    cards(void) const;
    const std::string&     extname(void) const;
    void                   extname(const std::string& extname);
    const int&             extno(void) const;
    void                   extno(const int& extno);
    const GFitsHeader&     header(void) const;
    void                   header(const GFitsHeader& header);
    bool                   has_card(const int& cardno) const;
    bool                   has_card(const std::string& keyname) const;
    GFitsHeaderCard&       card(const int& cardno);
    const GFitsHeaderCard& card(const int& cardno) const;
    GFitsHeaderCard&       card(const std::string& keyname);
    const GFitsHeaderCard& card(const std::string& keyname) const;
    void                   card(const GFitsHeaderCard& card);
    void                   card(const std::string& keyname,
                                const std::string& value,
                                const std::string& comment);
    void                   card(const std::string& keyname,
                                const double& value,
                                const std::string& comment);
    void                   card(const std::string& keyname,
                                const int& value,
                                const std::string& comment);
    void                   card(const std::string& keyname,
                                const bool& value,
                                const std::string& comment);
    void                   card(const std::string& keyname,
                                const char* value,
                                const std::string& comment);
    std::string            string(const std::string& keyname) const;
    double                 real(const std::string& keyname) const;
    int                    integer(const std::string& keyname) const;

protected:
    // Protected methods
    void         init_members(void);
    void         copy_members(const GFitsHDU& hdu);
    void         free_members(void);
    void         connect(void* fptr);
    void         move_to_hdu(void);
    HDUType      get_hdu_type(void) const;
    void         open(void* vptr, int hdunum);
    void         save(void);
    std::string  print_hdu(const GChatter& chatter = NORMAL) const;
    std::string  typecode(int type) const;

    // Pure virtual protected methods
    virtual void data_open(void* vptr) = 0;
    virtual void data_save(void) = 0;
    virtual void data_close(void) = 0;
    virtual void data_connect(void* vptr) = 0;

    // Protected data area
    void*        m_fitsfile;    //!< FITS file pointer pointing on actual HDU
    int          m_hdunum;      //!< HDU number (starting from 0)
    std::string  m_name;        //!< HDU name (extname)
    GFitsHeader  m_header;      //!< HDU header
};


/***********************************************************************//**
 * @brief Return number of cards in HDU header
 *
 * @return Number of cards in HDU header.
 *
 * Returns the number of cards in the header of the FITS extension.
 ***************************************************************************/
inline
int GFitsHDU::cards(void) const
{
    return (m_header.size());
}


/***********************************************************************//**
 * @brief Return extension name
 *
 * @return Extension name.
 *
 * Returns the name of the FITS extension.
 ***************************************************************************/
inline
const std::string& GFitsHDU::extname(void) const
{
    return (m_name);
}


/***********************************************************************//**
 * @brief Return extension number
 *
 * @return Extension number (starting from 0 for the primary image).
 *
 * Returns the number of the FITS extension.
 ***************************************************************************/
inline
const int& GFitsHDU::extno(void) const
{
    return (m_hdunum);
}


/***********************************************************************//**
 * @brief Set extension number
 *
 * @param[in] extno Extension number (starting from 0 for the primary image).
 *
 * Set the number of the FITS extension.
 ***************************************************************************/
inline
void GFitsHDU::extno(const int& extno)
{
    m_hdunum = extno;
    return;
}


/***********************************************************************//**
 * @brief Return extension header
 *
 * @return Extension header.
 *
 * Returns the extension header.
 ***************************************************************************/
inline
const GFitsHeader& GFitsHDU::header(void) const
{
    return (m_header);
}


/***********************************************************************//**
 * @brief Set extension header
 *
 * @param[in] header Extension header.
 *
 * Sets the extension header.
 ***************************************************************************/
inline
void GFitsHDU::header(const GFitsHeader& header)
{
    m_header = header;
    return;
}


/***********************************************************************//**
 * @brief Check existence of header card
 *
 * @param[in] cardno Number of card in header.
 * @return True if card with specified card number @p cardno exists in header.
 ***************************************************************************/
inline
bool GFitsHDU::has_card(const int& cardno) const
{
    return m_header.contains(cardno);
}


/***********************************************************************//**
 * @brief Checks for presence of header card
 *
 * @param[in] keyname Name of header card.
 * @return True if card with specified @p keyname exists in header.
 ***************************************************************************/
inline
bool GFitsHDU::has_card(const std::string& keyname) const
{
    return (m_header.contains(keyname));
}


/***********************************************************************//**
 * @brief Return header card
 *
 * @param[in] cardno Number of card in header.
 * @return Header card.
 ***************************************************************************/
inline
GFitsHeaderCard& GFitsHDU::card(const int& cardno)
{
    return (m_header.at(cardno));
}


/***********************************************************************//**
 * @brief Return header card (const version)
 *
 * @param[in] cardno Number of card in header.
 * @return Header card.
 ***************************************************************************/
inline
const GFitsHeaderCard& GFitsHDU::card(const int& cardno) const
{
    return (m_header.at(cardno));
}


/***********************************************************************//**
 * @brief Return header card
 *
 * @param[in] keyname Name of header card.
 * @return Header card.
 ***************************************************************************/
inline
GFitsHeaderCard& GFitsHDU::card(const std::string& keyname)
{
    return (m_header.at(keyname));
}


/***********************************************************************//**
 * @brief Return header card (const version)
 *
 * @param[in] keyname Name of header card.
 * @return Header card.
 ***************************************************************************/
inline
const GFitsHeaderCard& GFitsHDU::card(const std::string& keyname) const
{
    return (m_header.at(keyname));
}


/***********************************************************************//**
 * @brief Append or update header card
 *
 * @param[in] card Header card.
 ***************************************************************************/
inline
void GFitsHDU::card(const GFitsHeaderCard& card)
{
    m_header.append(card);
    return;
}


/***********************************************************************//**
 * @brief Append or update string value header card
 *
 * @param[in] keyname Name of the header card.
 * @param[in] value String value of the header card.
 * @param[in] comment Comment of the header card.
 ***************************************************************************/
inline
void GFitsHDU::card(const std::string& keyname, const std::string& value,
                    const std::string& comment)
{
    GFitsHeaderCard card(keyname, value, comment);
    m_header.append(card);
    return;
}


/***********************************************************************//**
 * @brief Append or update double precision value header card
 *
 * @param[in] keyname Name of the header card.
 * @param[in] value Double precision value of the header card.
 * @param[in] comment Comment of the header card.
 ***************************************************************************/
inline
void GFitsHDU::card(const std::string& keyname, const double& value,
                    const std::string& comment)
{
    GFitsHeaderCard card(keyname, value, comment);
    m_header.append(card);
    return;
}


/***********************************************************************//**
 * @brief Append or update integer value header card
 *
 * @param[in] keyname Name of the header card.
 * @param[in] value Integer value of the header card.
 * @param[in] comment Comment of the header card.
 ***************************************************************************/
inline
void GFitsHDU::card(const std::string& keyname, const int& value,
                    const std::string& comment)
{
    GFitsHeaderCard card(keyname,  value, comment);
    m_header.append(card);
    return;
}


/***********************************************************************//**
 * @brief Append or update boolean value header card
 *
 * @param[in] keyname Name of the header card.
 * @param[in] value Boolean value of the header card.
 * @param[in] comment Comment of the header card.
 ***************************************************************************/
inline
void GFitsHDU::card(const std::string& keyname, const bool& value,
                    const std::string& comment)
{
    GFitsHeaderCard card(keyname,  value, comment);
    m_header.append(card);
    return;
}


/***********************************************************************//**
 * @brief Append or update character value header card
 *
 * @param[in] keyname Name of the header card.
 * @param[in] value Character value of the header card.
 * @param[in] comment Comment of the header card.
 ***************************************************************************/
inline
void GFitsHDU::card(const std::string& keyname, const char* value,
                    const std::string& comment)
{
    std::string string(value);
    GFitsHeaderCard card(keyname, string, comment);
    m_header.append(card);
    return;
}


/***********************************************************************//**
 * @brief Return card value as string
 *
 * @param[in] keyname Name of header card.
 * @return String card value.
 ***************************************************************************/
inline
std::string GFitsHDU::string(const std::string& keyname) const
{
    return (m_header.at(keyname).string());
}


/***********************************************************************//**
 * @brief Return card value as double precision
 *
 * @param[in] keyname Name of header card.
 * @return Double precision card value.
 ***************************************************************************/
inline
double GFitsHDU::real(const std::string& keyname) const
{
    return (m_header.at(keyname).real());
}


/***********************************************************************//**
 * @brief Return card value as integer
 *
 * @param[in] keyname Name of header card.
 * @return Integer card value.
 ***************************************************************************/
inline
int GFitsHDU::integer(const std::string& keyname) const
{
    return (m_header.at(keyname).integer());
}

#endif /* GFITSHDU_HPP */