From 1f9b685e828bbe9e26d0bd27a023224181d6edb3 Mon Sep 17 00:00:00 2001 From: knodlseder <> Date: Thu, 28 Feb 2008 11:05:54 +0000 Subject: [PATCH] initial implementation of FITS table and column classes --- configure | 1 - configure.ac | 3 +-- include/GException.hpp | 25 +++++++++++++++++++++++++ include/GFits.hpp | 42 +++++++++++++++++++++++++++--------------- include/GFitsAsciiTable.hpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/GFitsBinTable.hpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/GFitsCfitsio.hpp | 24 ++++++++++++++++++++++-- include/GFitsData.hpp | 24 ++++++++++++------------ include/GFitsHDU.hpp | 29 +++++++++++++++++++++++------ include/GFitsHeader.hpp | 30 +++++++++++++++++++++++++++++- include/GFitsHeaderCard.hpp | 21 +++++++++++++++++++++ include/GFitsImage.hpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/GFitsTableCol.hpp | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/GFitsTableDblCol.hpp | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/GammaLib.hpp | 5 +++++ include/Makefile.am | 5 +++++ include/Makefile.in | 5 +++++ src/GException.cpp | 182 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------- src/Makefile.am | 5 +++++ src/Makefile.in | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- src/fits/GFits.cpp | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------- src/fits/GFitsAsciiTable.cpp | 272 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/fits/GFitsBinTable.cpp | 335 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/fits/GFitsData.cpp | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- src/fits/GFitsHDU.cpp | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------- src/fits/GFitsHeader.cpp | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------- src/fits/GFitsHeaderCard.cpp | 242 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------ src/fits/GFitsImage.cpp | 210 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/fits/GFitsTableCol.cpp | 192 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/fits/GFitsTableDblCol.cpp | 339 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/test_GFits.cpp | 13 +++++++++++++ 31 files changed, 2688 insertions(+), 281 deletions(-) create mode 100644 include/GFitsAsciiTable.hpp create mode 100644 include/GFitsBinTable.hpp create mode 100644 include/GFitsImage.hpp create mode 100644 include/GFitsTableCol.hpp create mode 100644 include/GFitsTableDblCol.hpp create mode 100644 src/fits/GFitsAsciiTable.cpp create mode 100644 src/fits/GFitsBinTable.cpp create mode 100644 src/fits/GFitsImage.cpp create mode 100644 src/fits/GFitsTableCol.cpp create mode 100644 src/fits/GFitsTableDblCol.cpp diff --git a/configure b/configure index 9f3c74b..b9e5326 100755 --- a/configure +++ b/configure @@ -18726,7 +18726,6 @@ RANGE_CHECK="no" if test "${enable_range_check+set}" = set; then enableval="$enable_range_check" RANGE_CHECK="$enableval" - fi; if test "x$RANGE_CHECK" = "xyes"; then diff --git a/configure.ac b/configure.ac index cc92a16..4f9ee3e 100644 --- a/configure.ac +++ b/configure.ac @@ -79,8 +79,7 @@ dnl ============================== RANGE_CHECK="no" AC_ARG_ENABLE(range_check, [ --enable-range-check performs index range checking], - [ RANGE_CHECK="$enableval" - ]) + [ RANGE_CHECK="$enableval" ]) if test "x$RANGE_CHECK" = "xyes"; then AC_DEFINE([G_RANGE_CHECK], [1], [Define if range checking should be performed]) fi diff --git a/include/GException.hpp b/include/GException.hpp index fb7e6e8..d96fa00 100644 --- a/include/GException.hpp +++ b/include/GException.hpp @@ -58,6 +58,7 @@ public: // Out of range class out_of_range : public GExceptionHandler { public: + out_of_range(string origin, int inx, int min, int max); out_of_range(string origin, int inx, int elements); out_of_range(string origin, int row, int col, int rows, int cols); }; @@ -134,12 +135,36 @@ public: fits_open_error(string origin, string filename, int status); }; + // FITS file has already been opened + class fits_already_opened : public GExceptionHandler { + public: + fits_already_opened(string origin, string filename); + }; + // FITS keyword not found error class fits_key_not_found : public GExceptionHandler { public: fits_key_not_found(string origin, string keyname, int status); }; + // FITS unknown HDU type + class fits_unknown_HDU_type : public GExceptionHandler { + public: + fits_unknown_HDU_type(string origin, int type); + }; + + // FITS unknown HDU type + class fits_HDU_not_a_table : public GExceptionHandler { + public: + fits_HDU_not_a_table(string origin, int type); + }; + + // FITS invalid type + class fits_invalid_type : public GExceptionHandler { + public: + fits_invalid_type(string origin, string message); + }; + }; #endif /* GEXCEPTION_HPP */ diff --git a/include/GFits.hpp b/include/GFits.hpp index aa73e99..64a80c7 100644 --- a/include/GFits.hpp +++ b/include/GFits.hpp @@ -27,7 +27,6 @@ ***************************************************************************/ class GFits { -// Public methods public: // Constructors and destructors GFits(); @@ -38,26 +37,39 @@ public: GFits& operator= (const GFits& fits); // Methods - void open(std::string filename); - void close(void); - //GFitsHDU* hdu(std::string extname); - //GFitsHDU* hdu(int extno); + void open(const std::string filename); + void save(void); + void saveto(const std::string filename, int clobber); + void close(void); + GFitsHDU* hdu(std::string extname); + GFitsHDU* hdu(int extno); -// Methods and data that are available to derived classes -protected: - // Protected methods +private: + // Private methods + void init_members(void); + void copy_members(const GFits& fits); + void free_members(void); - // Protected data area + // Private data area std::string m_filename; // FITS file name __fitsfile* m_fitsfile; // FITS file pointer int m_num_hdu; // Number of HDUs in file GFitsHDU* m_hdu; // Pointers to HDUs - -// Methods that are available to the base class only -private: - void init_members(void); - void copy_members(const GFits& fits); - void free_members(void); }; + +/*************************************************************************** + * Inline methods * + ***************************************************************************/ +inline +GFitsHDU* GFits::hdu(int extno) +{ + #if defined(G_RANGE_CHECK) + if (extno < 1 || extno > m_num_hdu) + throw GException::out_of_range("GFits::hdu(int)", extno, 1, m_num_hdu); + #endif + return &(m_hdu[extno+1]); +} + + #endif /* GFITS_HPP */ diff --git a/include/GFitsAsciiTable.hpp b/include/GFitsAsciiTable.hpp new file mode 100644 index 0000000..a2e15c6 --- /dev/null +++ b/include/GFitsAsciiTable.hpp @@ -0,0 +1,68 @@ +/*************************************************************************** + * GFitsAsciiTable.hpp - FITS ASCII table class * + * ----------------------------------------------------------------------- * + * copyright : (C) 2008 by Jurgen Knodlseder * + * ----------------------------------------------------------------------- * + * * + * 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 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#ifndef GFITSASCIITABLE_HPP +#define GFITSASCIITABLE_HPP + +/* __ Includes ___________________________________________________________ */ +#include "GFitsCfitsio.hpp" +#include "GFitsData.hpp" +#include "GFitsTableCol.hpp" + +/* __ Namespaces _________________________________________________________ */ + + +/*************************************************************************** + * GFitsAsciiTable class definition * + ***************************************************************************/ +class GFitsAsciiTable : public GFitsData { + +public: + // Constructors and destructors + GFitsAsciiTable(); + GFitsAsciiTable(const GFitsAsciiTable& table); + virtual ~GFitsAsciiTable(); + + // Operators + GFitsAsciiTable& operator= (const GFitsAsciiTable& table); + + // Methods + void open(__fitsfile* fptr); + void close(void); + GFitsAsciiTable* clone(void) const; + GFitsTableCol* column(const std::string colname); + GFitsTableCol* column(const int colnum); + +private: + // Private methods + void init_members(void); + void copy_members(const GFitsAsciiTable& table); + void free_members(void); + + // Private data area + int m_rows; + int m_cols; + GFitsTableCol** m_columns; +}; + + +/*************************************************************************** + * Inline methods * + ***************************************************************************/ +inline +GFitsAsciiTable* GFitsAsciiTable::clone(void) const +{ + return new GFitsAsciiTable(*this); +} + +#endif /* GFITSASCIITABLE_HPP */ diff --git a/include/GFitsBinTable.hpp b/include/GFitsBinTable.hpp new file mode 100644 index 0000000..f13d367 --- /dev/null +++ b/include/GFitsBinTable.hpp @@ -0,0 +1,72 @@ +/*************************************************************************** + * GFitsBinTable.hpp - FITS binary table class * + * ----------------------------------------------------------------------- * + * copyright : (C) 2008 by Jurgen Knodlseder * + * ----------------------------------------------------------------------- * + * * + * 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 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#ifndef GFITSBINTABLE_HPP +#define GFITSBINTABLE_HPP + +/* __ Includes ___________________________________________________________ */ +#include +#include "GFitsCfitsio.hpp" +#include "GFitsData.hpp" +#include "GFitsTableCol.hpp" + +/* __ Namespaces _________________________________________________________ */ + + +/* __ Structures _________________________________________________________ */ + + +/*************************************************************************** + * GFitsBinTable class definition * + ***************************************************************************/ +class GFitsBinTable : public GFitsData { + +public: + // Constructors and destructors + GFitsBinTable(); + GFitsBinTable(const GFitsBinTable& table); + virtual ~GFitsBinTable(); + + // Operators + GFitsBinTable& operator= (const GFitsBinTable& table); + + // Methods + void open(__fitsfile* fptr); + void close(void); + GFitsBinTable* clone(void) const; + GFitsTableCol* column(const std::string colname); + GFitsTableCol* column(const int colnum); + +private: + // Private methods + void init_members(void); + void copy_members(const GFitsBinTable& table); + void free_members(void); + + // Private data area + int m_rows; + int m_cols; + GFitsTableCol** m_columns; +}; + + +/*************************************************************************** + * Inline methods * + ***************************************************************************/ +inline +GFitsBinTable* GFitsBinTable::clone(void) const +{ + return new GFitsBinTable(*this); +} + +#endif /* GFITSBINTABLE_HPP */ diff --git a/include/GFitsCfitsio.hpp b/include/GFitsCfitsio.hpp index 3a80e19..80c98db 100644 --- a/include/GFitsCfitsio.hpp +++ b/include/GFitsCfitsio.hpp @@ -39,7 +39,13 @@ #define __ffgkey(A, B, C, D, E) ffgkey(A, B, C, D, E) #define __ffgkyn(A, B, C, D, E, F) ffgkyn(A, B, C, D, E, F) #define __ffuky(A, B, C, D, E, F) ffuky(A, B, C, D, E, F) -#define __TSTRING TSTRING +#define __ffgnrw(A, B, C) ffgnrw(A, B, C) +#define __ffgncl(A, B, C) ffgncl(A, B, C) +#define __ffgtcl(A, B, C, D, E, F) ffgtcl(A, B, C, D, E, F) +#define __ffgcv(A, B, C, D, E, F, G, H, I, J) ffgcv(A, B, C, D, E, F, G, H, I, J) +#define __TLOGICAL TLOGICAL +#define __TSTRING TSTRING +#define __TDOUBLE TDOUBLE /* __ Type definition ____________________________________________________ */ typedef fitsfile __fitsfile; @@ -60,7 +66,21 @@ typedef fitsfile __fitsfile; #define __ffgkey(A, B, C, D, E) __dummy() #define __ffgkyn(A, B, C, D, E, F) __dummy() #define __ffuky(A, B, C, D, E, F) __dummy() -#define __TSTRING 0 +#define __ffgnrw(A, B, C) __dummy() +#define __ffgncl(A, B, C) __dummy() +#define __ffgtcl(A, B, C, D, E, F) __dummy() +#define __ffgcv(A, B, C, D, E, F, G, H, I, J) __dummy() +#define __TBIT 1 +#define __TSHORT 11 +#define __TLOGICAL 14 +#define __TSTRING 16 +#define __TSHORT 21 +#define __TLONG 41 +#define __TLONGLONG 81 +#define __TFLOAT 42 +#define __TDOUBLE 82 +#define __TCOMPLEX 83 +#define __TDBLCOMPLEX 163 /* __ Type definition ____________________________________________________ */ typedef int __fitsfile; diff --git a/include/GFitsData.hpp b/include/GFitsData.hpp index 95145e5..f6a05d0 100644 --- a/include/GFitsData.hpp +++ b/include/GFitsData.hpp @@ -15,37 +15,37 @@ #define GFITSDATA_HPP /* __ Includes ___________________________________________________________ */ -//#include "GVector.hpp" +#include "GFitsCfitsio.hpp" /* __ Namespaces _________________________________________________________ */ -using namespace std; /*************************************************************************** - * GFitsData class definition * + * GFitsData class definition * ***************************************************************************/ class GFitsData { -// Public methods public: // Constructors and destructors - //GFitsData(); - //GFitsData(const GFitsData& data); - //virtual ~GFitsData(); + GFitsData(); + GFitsData(const GFitsData& data); + virtual ~GFitsData(); // Operators + virtual GFitsData& operator= (const GFitsData& data); // Methods + virtual void open(__fitsfile* fptr) = 0; + virtual void close(void) = 0; + virtual GFitsData* clone(void) const = 0; -// Methods and data that are available to derived classes protected: // Protected methods + void init_members(void); + void copy_members(const GFitsData& data); + void free_members(void); // Protected data area - // this is a test - -// Methods that are available to the base class only -private: }; #endif /* GFITSDATA_HPP */ diff --git a/include/GFitsHDU.hpp b/include/GFitsHDU.hpp index 7eb2e17..0d40ffb 100644 --- a/include/GFitsHDU.hpp +++ b/include/GFitsHDU.hpp @@ -17,6 +17,9 @@ /* __ Includes ___________________________________________________________ */ #include "GFitsHeader.hpp" #include "GFitsData.hpp" +#include "GFitsImage.hpp" +#include "GFitsAsciiTable.hpp" +#include "GFitsBinTable.hpp" /* __ Namespaces _________________________________________________________ */ @@ -37,10 +40,14 @@ public: GFitsHDU& operator= (const GFitsHDU& hdu); // Methods - void open(__fitsfile* fptr, int hdunum); - void close(void); - GFitsHeader* header(void); - GFitsData* data(void); + void open(__fitsfile* fptr, int hdunum); + void save(void); + std::string extname(void) const; + int extno(void) const; + int exttype(void) const; + GFitsHeader* header(void) const; + GFitsData* data(void) const; + GFitsTableCol* column(const std::string colname) const; private: // Private methods @@ -54,8 +61,18 @@ private: std::string m_name; // HDU name int m_num; // HDU number (starting from 1) int m_type; // HDU type - GFitsHeader m_header; // HDU header - GFitsData m_data; // HDU data + GFitsHeader* m_header; // HDU header + GFitsData* m_data; // HDU data }; + +/*************************************************************************** + * Inline methods * + ***************************************************************************/ +inline std::string GFitsHDU::extname(void) const { return m_name; } +inline int GFitsHDU::extno(void) const { return m_num; } +inline int GFitsHDU::exttype(void) const { return m_type; } +inline GFitsHeader* GFitsHDU::header(void) const { return m_header; } +inline GFitsData* GFitsHDU::data(void) const { return m_data; } + #endif /* GFITSHDU_HPP */ diff --git a/include/GFitsHeader.hpp b/include/GFitsHeader.hpp index cd7002e..22d0df5 100644 --- a/include/GFitsHeader.hpp +++ b/include/GFitsHeader.hpp @@ -15,6 +15,7 @@ #define GFITSHEADER_HPP /* __ Includes ___________________________________________________________ */ +#include "GFitsCfitsio.hpp" #include "GFitsHeaderCard.hpp" /* __ Namespaces _________________________________________________________ */ @@ -37,9 +38,16 @@ public: // Methods void open(__fitsfile* fptr); + void close(void); GFitsHeaderCard* card(const std::string keyname); GFitsHeaderCard* card(const int cardno); - std::string value(const std::string keyname); + std::string string(const std::string keyname); + std::string string(const int cardno); + double real(const std::string keyname); + double real(const int cardno); + int integer(const std::string keyname); + int integer(const int cardno); + GFitsHeader* clone(void) const; private: // Private methods @@ -53,4 +61,24 @@ private: GFitsHeaderCard* m_card; }; + +/*************************************************************************** + * Inline methods * + ***************************************************************************/ +inline +GFitsHeaderCard* GFitsHeader::card(const std::string keyname) +{ + return GFitsHeader::card_ptr(keyname); +} +inline +GFitsHeaderCard* GFitsHeader::card(const int cardno) +{ + return (cardno >= 0 && cardno < m_num_cards) ? &(m_card[cardno]) : NULL; +} +inline +GFitsHeader* GFitsHeader::clone(void) const +{ + return new GFitsHeader(*this); +} + #endif /* GFITSHEADER_HPP */ diff --git a/include/GFitsHeaderCard.hpp b/include/GFitsHeaderCard.hpp index 30ac907..75dc78d 100644 --- a/include/GFitsHeaderCard.hpp +++ b/include/GFitsHeaderCard.hpp @@ -36,6 +36,10 @@ public: // Methods std::string keyname(void); std::string value(void); + int value_type(void); + std::string string(void); + double real(void); + int integer(void); std::string unit(void); std::string comment(void); void set_keyname(std::string& keyname); @@ -51,12 +55,29 @@ private: void init_members(void); void copy_members(const GFitsHeaderCard& card); void free_members(void); + int get_value_type(void); // Private data area std::string m_keyname; std::string m_value; + int m_value_type; std::string m_unit; std::string m_comment; }; + +/*************************************************************************** + * Inline methods * + ***************************************************************************/ +inline std::string GFitsHeaderCard::keyname(void) { return m_keyname; } +inline std::string GFitsHeaderCard::value(void) { return m_value; } +inline int GFitsHeaderCard::value_type(void) { return m_value_type; } +inline std::string GFitsHeaderCard::unit(void) { return m_unit; } +inline std::string GFitsHeaderCard::comment(void) { return m_comment; } +inline void GFitsHeaderCard::set_keyname(std::string& keyname) { m_keyname = keyname; } +inline void GFitsHeaderCard::set_value(std::string& value) { m_value = value; } +inline void GFitsHeaderCard::set_unit(std::string& unit) { m_unit = unit; } +inline void GFitsHeaderCard::set_comment(std::string& comment) { m_comment = comment; } + + #endif /* GFITSHEADERCARD_HPP */ diff --git a/include/GFitsImage.hpp b/include/GFitsImage.hpp new file mode 100644 index 0000000..223e4d1 --- /dev/null +++ b/include/GFitsImage.hpp @@ -0,0 +1,62 @@ +/*************************************************************************** + * GFitsImage.hpp - FITS image class * + * ----------------------------------------------------------------------- * + * copyright : (C) 2008 by Jurgen Knodlseder * + * ----------------------------------------------------------------------- * + * * + * 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 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#ifndef GFITSIMAGE_HPP +#define GFITSIMAGE_HPP + +/* __ Includes ___________________________________________________________ */ +#include "GFitsCfitsio.hpp" +#include "GFitsData.hpp" + +/* __ Namespaces _________________________________________________________ */ + + +/*************************************************************************** + * GFitsImage class definition * + ***************************************************************************/ +class GFitsImage : public GFitsData { + +public: + // Constructors and destructors + GFitsImage(); + GFitsImage(const GFitsImage& image); + ~GFitsImage(); + + // Operators + GFitsImage& operator= (const GFitsImage& image); + + // Methods + void open(__fitsfile* fptr); + void close(void); + GFitsImage* clone(void) const; + +private: + // Private methods + void init_members(void); + void copy_members(const GFitsImage& image); + void free_members(void); + + // Private data area +}; + + +/*************************************************************************** + * Inline methods * + ***************************************************************************/ +inline +GFitsImage* GFitsImage::clone(void) const +{ + return new GFitsImage(*this); +} + +#endif /* GFITSIMAGE_HPP */ diff --git a/include/GFitsTableCol.hpp b/include/GFitsTableCol.hpp new file mode 100644 index 0000000..26cd5bf --- /dev/null +++ b/include/GFitsTableCol.hpp @@ -0,0 +1,99 @@ +/*************************************************************************** + * GFitsTableCol.hpp - FITS table column abstract base class * + * ----------------------------------------------------------------------- * + * copyright : (C) 2008 by Jurgen Knodlseder * + * ----------------------------------------------------------------------- * + * * + * 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 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#ifndef GFITSTABLECOL_HPP +#define GFITSTABLECOL_HPP + +/* __ Includes ___________________________________________________________ */ +#include "GFitsCfitsio.hpp" + +/* __ Namespaces _________________________________________________________ */ + + +/*************************************************************************** + * GFitsTableCol class definition * + ***************************************************************************/ +class GFitsTableCol { + +public: + // Constructors and destructors + GFitsTableCol(); + GFitsTableCol(const GFitsTableCol& column); + virtual ~GFitsTableCol(); + + // Operators + GFitsTableCol& operator= (const GFitsTableCol& column); + + // Virtual Methods + virtual std::string string(const int& row, const int& col = 0) = 0; + virtual double real(const int& row, const int& col = 0) = 0; + virtual int integer(const int& row, const int& col = 0) = 0; + virtual GFitsTableCol* clone(void) const = 0; + virtual std::string* ptr_string(void) = 0; + virtual double* ptr_double(void) = 0; + virtual float* ptr_float(void) = 0; + virtual short* ptr_short(void) = 0; + virtual long* ptr_long(void) = 0; + virtual int* ptr_int(void) = 0; + + // Base class Methods + void set_name(const std::string name); + void set_colnum(const int colnum); + void set_type(const int type); + void set_repeat(const int repeat); + void set_width(const int width); + void set_length(const int length); + void set_fitsfile(const __fitsfile* fptr); + std::string name(void); + int colnum(void); + int type(void); + int repeat(void); + int width(void); + int length(void); + +protected: + // Protected data area + std::string m_name; + int m_colnum; + int m_type; + int m_repeat; + int m_width; + int m_length; + __fitsfile* m_fitsfile; + +private: + // Private methods + void init_members(void); + void copy_members(const GFitsTableCol& column); + void free_members(void); +}; + + +/*************************************************************************** + * Inline methods * + ***************************************************************************/ +inline void GFitsTableCol::set_name(const std::string name) { m_name = name; } +inline void GFitsTableCol::set_colnum(const int colnum) { m_colnum = colnum; } +inline void GFitsTableCol::set_type(const int type) { m_type = type; } +inline void GFitsTableCol::set_repeat(const int repeat) { m_repeat = repeat; } +inline void GFitsTableCol::set_width(const int width) { m_width = width; } +inline void GFitsTableCol::set_length(const int length) { m_length = length; } +inline void GFitsTableCol::set_fitsfile(const __fitsfile* fptr) { m_fitsfile = (__fitsfile*)fptr; } +inline std::string GFitsTableCol::name(void) { return m_name; } +inline int GFitsTableCol::colnum(void) { return m_colnum; } +inline int GFitsTableCol::type(void) { return m_type; } +inline int GFitsTableCol::repeat(void) { return m_repeat; } +inline int GFitsTableCol::width(void) { return m_width; } +inline int GFitsTableCol::length(void) { return m_length; } + +#endif /* GFITSTABLECOL_HPP */ diff --git a/include/GFitsTableDblCol.hpp b/include/GFitsTableDblCol.hpp new file mode 100644 index 0000000..e7af0dd --- /dev/null +++ b/include/GFitsTableDblCol.hpp @@ -0,0 +1,77 @@ +/*************************************************************************** + * GFitsTableDblCol.hpp - FITS table double column class * + * ----------------------------------------------------------------------- * + * copyright : (C) 2008 by Jurgen Knodlseder * + * ----------------------------------------------------------------------- * + * * + * 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 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#ifndef GFITSTABLEDBLCOL_HPP +#define GFITSTABLEDBLCOL_HPP + +/* __ Includes ___________________________________________________________ */ +#include "GFitsCfitsio.hpp" +#include "GFitsTableCol.hpp" + +/* __ Namespaces _________________________________________________________ */ + + +/* __ Structures _________________________________________________________ */ + + +/*************************************************************************** + * GFitsTableDblCol class definition * + ***************************************************************************/ +class GFitsTableDblCol : public GFitsTableCol { + +public: + // Constructors and destructors + GFitsTableDblCol(); + GFitsTableDblCol(const GFitsTableDblCol& column); + virtual ~GFitsTableDblCol(); + + // Operators + GFitsTableDblCol& operator= (const GFitsTableDblCol& column); + + // Methods + std::string string(const int& row, const int& col = 0); + double real(const int& row, const int& col = 0); + int integer(const int& row, const int& col = 0); + GFitsTableDblCol* clone(void) const; + std::string* ptr_string(void); + double* ptr_double(void); + float* ptr_float(void); + short* ptr_short(void); + long* ptr_long(void); + int* ptr_int(void); + +private: + // Private methods + void init_members(void); + void copy_members(const GFitsTableDblCol& column); + void free_members(void); + void load(void); + + // Private data area + int m_size; // Size of data area + double* m_data; // Data area + double m_nulval; // NULL value +}; + + +/*************************************************************************** + * Inline methods * + ***************************************************************************/ +inline double* GFitsTableDblCol::ptr_double(void) { return m_data; } +inline +GFitsTableDblCol* GFitsTableDblCol::clone(void) const +{ + return new GFitsTableDblCol(*this); +} + +#endif /* GFITSTABLEDBLCOL_HPP */ diff --git a/include/GammaLib.hpp b/include/GammaLib.hpp index 7eed81e..67ff82a 100644 --- a/include/GammaLib.hpp +++ b/include/GammaLib.hpp @@ -32,6 +32,11 @@ #include "GFitsHeader.hpp" #include "GFitsHeaderCard.hpp" #include "GFitsData.hpp" +#include "GFitsImage.hpp" +#include "GFitsAsciiTable.hpp" +#include "GFitsBinTable.hpp" +#include "GFitsTableCol.hpp" +#include "GFitsTableDblCol.hpp" #include "GVector.hpp" #include "GMatrixBase.hpp" diff --git a/include/Makefile.am b/include/Makefile.am index e8e5283..aec8565 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -7,6 +7,11 @@ pkginclude_HEADERS = GammaLib.hpp \ GFitsHeaderCard.hpp \ GFitsHeader.hpp \ GFitsData.hpp \ + GFitsImage.hpp \ + GFitsAsciiTable.hpp \ + GFitsBinTable.hpp \ + GFitsTableCol.hpp \ + GFitsTableDblCol.hpp \ GFitsHDU.hpp \ GFits.hpp \ GFitPar.hpp \ diff --git a/include/Makefile.in b/include/Makefile.in index 5ec9a31..e4b4447 100644 --- a/include/Makefile.in +++ b/include/Makefile.in @@ -113,6 +113,11 @@ pkginclude_HEADERS = GammaLib.hpp \ GFitsHeaderCard.hpp \ GFitsHeader.hpp \ GFitsData.hpp \ + GFitsImage.hpp \ + GFitsAsciiTable.hpp \ + GFitsBinTable.hpp \ + GFitsTableCol.hpp \ + GFitsTableDblCol.hpp \ GFitsHDU.hpp \ GFits.hpp \ GFitPar.hpp \ diff --git a/src/GException.cpp b/src/GException.cpp index 9dec441..fef188b 100644 --- a/src/GException.cpp +++ b/src/GException.cpp @@ -20,6 +20,23 @@ using namespace std; /*************************************************************************** + * Private conversion functions * + ***************************************************************************/ +std::string str(int value) +{ + ostringstream s_value; + s_value << value; + return s_value.str(); +} +std::string str(double value) +{ + ostringstream s_value; + s_value << scientific << value; + return s_value.str(); +} + + +/*************************************************************************** * Exception handler base class definition * ***************************************************************************/ const char* GExceptionHandler::what() const throw() @@ -34,10 +51,8 @@ const char* GExceptionHandler::what() const throw() ***************************************************************************/ GException::mem_alloc::mem_alloc(string origin, unsigned num) { - ostringstream s_num; - s_num << num; m_origin = origin; - m_message = "Memory allocation error (" + s_num.str() + " elements)"; + m_message = "Memory allocation error (" + str((int)num) + " elements)"; } @@ -52,18 +67,25 @@ GException::empty::empty(string origin) /*************************************************************************** + * Index out of range * + ***************************************************************************/ +GException::out_of_range::out_of_range(string origin, int inx, int min, int max) +{ + m_origin = origin; + m_message = "Index (" + str(inx) + ") out of range [" + str(min) + + "," + str(max) + "]"; +} + + +/*************************************************************************** * Vector index out of range * ***************************************************************************/ GException::out_of_range::out_of_range(string origin, int inx, int elements) { m_origin = origin; if (elements > 0) { - ostringstream s_inx; - ostringstream s_elements; - s_inx << inx; - s_elements << elements-1; - m_message = "Vector index (" + s_inx.str() + ") out of range [0," + - s_elements.str() + "]"; + m_message = "Vector index (" + str(inx) + ") out of range [0," + + str(elements-1) + "]"; } else { m_message = "Empty vector"; @@ -71,24 +93,15 @@ GException::out_of_range::out_of_range(string origin, int inx, int elements) } - /*************************************************************************** * Matrix row or column out of range * ***************************************************************************/ GException::out_of_range::out_of_range(string origin, int row, int col, int rows, int cols) { - m_origin = origin; - ostringstream s_row; - ostringstream s_col; - ostringstream s_rows; - ostringstream s_cols; - s_row << row; - s_col << col; - s_rows << rows-1; - s_cols << cols-1; - m_message = "Matrix element (" + s_row.str() + "," + s_col.str() + - ") out of range ([0," + s_rows.str() + "], [0," + - s_cols.str() + "])"; + m_origin = origin; + m_message = "Matrix element (" + str(row) + "," + str(col) + + ") out of range ([0," + str(rows) + "], [0," + + str(cols) + "])"; } @@ -97,13 +110,9 @@ GException::out_of_range::out_of_range(string origin, int row, int col, int rows ***************************************************************************/ GException::vector_mismatch::vector_mismatch(string origin, int size1, int size2) { - m_origin = origin; - ostringstream s_size1; - ostringstream s_size2; - s_size1 << size1; - s_size2 << size2; - m_message = "Vector dimensions differ (" + s_size1.str() + " <-> " + - s_size2.str() + ")"; + m_origin = origin; + m_message = "Vector dimensions differ (" + str(size1) + " <-> " + + str(size2) + ")"; } @@ -112,11 +121,9 @@ GException::vector_mismatch::vector_mismatch(string origin, int size1, int size2 ***************************************************************************/ GException::vector_bad_cross_dim::vector_bad_cross_dim(string origin, int elements) { - m_origin = origin; - ostringstream s_elements; - s_elements << elements; + m_origin = origin; m_message = "Vector cross product only defined for 3 dimensions but vector size is " + - s_elements.str(); + str(elements); } @@ -125,16 +132,10 @@ GException::vector_bad_cross_dim::vector_bad_cross_dim(string origin, int elemen ***************************************************************************/ GException::matrix_vector_mismatch::matrix_vector_mismatch(string origin, int num, int rows, int cols) { - m_origin = origin; - ostringstream s_num; - ostringstream s_rows; - ostringstream s_cols; - s_num << num; - s_rows << rows; - s_cols << cols; - m_message = "Vector dimension [" + s_num.str() + + m_origin = origin; + m_message = "Vector dimension [" + str(num) + "] is incompatible with matrix size [" + - s_rows.str() + "," + s_cols.str() + "]"; + str(rows) + "," + str(cols) + "]"; } @@ -143,18 +144,9 @@ GException::matrix_vector_mismatch::matrix_vector_mismatch(string origin, int nu ***************************************************************************/ GException::matrix_mismatch::matrix_mismatch(string origin, int rows1, int cols1, int rows2, int cols2) { - m_origin = origin; - ostringstream s_rows1; - ostringstream s_rows2; - ostringstream s_cols1; - ostringstream s_cols2; - s_rows1 << rows1; - s_cols1 << cols1; - s_rows2 << rows2; - s_cols2 << cols2; - m_message = "Matrix mismatch: M1(" + s_rows1.str() + "," + s_cols1.str() + - ") incompatible with M2(" + s_rows2.str() + "," + - s_cols2.str() + ")"; + m_origin = origin; + m_message = "Matrix mismatch: M1(" + str(rows1) + "," + str(cols1) + + ") incompatible with M2(" + str(rows2) + "," + str(cols2) + ")"; } @@ -163,13 +155,8 @@ GException::matrix_mismatch::matrix_mismatch(string origin, int rows1, int cols1 ***************************************************************************/ GException::matrix_not_rectangular::matrix_not_rectangular(string origin, int rows, int cols) { - m_origin = origin; - ostringstream s_rows; - ostringstream s_cols; - s_rows << rows; - s_cols << cols; - m_message = "Matrix is not rectangular [" + s_rows.str() + "," + s_cols.str() + - "]"; + m_origin = origin; + m_message = "Matrix is not rectangular [" + str(rows) + "," + str(cols) + "]"; } @@ -178,13 +165,9 @@ GException::matrix_not_rectangular::matrix_not_rectangular(string origin, int ro ***************************************************************************/ GException::matrix_not_pos_definite::matrix_not_pos_definite(string origin, int row, double sum) { - m_origin = origin; - ostringstream s_rows; - ostringstream s_sum; - s_rows << row; - s_sum << scientific << sum; - m_message = "Matrix is not positive definite (sum " + s_sum.str() + - " occured in row/column " + s_rows.str() + ")"; + m_origin = origin; + m_message = "Matrix is not positive definite (sum " + str(sum) + + " occured in row/column " + str(row) + ")"; } @@ -193,13 +176,8 @@ GException::matrix_not_pos_definite::matrix_not_pos_definite(string origin, int ***************************************************************************/ GException::matrix_not_symmetric::matrix_not_symmetric(string origin, int cols, int rows) { - m_origin = origin; - ostringstream s_rows; - ostringstream s_cols; - s_rows << rows; - s_cols << cols; - m_message = "Matrix is not symmetric [" + s_rows.str() + "," + - s_cols.str() + "]"; + m_origin = origin; + m_message = "Matrix is not symmetric [" + str(rows) + "," + str(cols) + "]"; } @@ -228,16 +206,10 @@ GException::matrix_zero::matrix_zero(string origin) ***************************************************************************/ GException::invalid_order::invalid_order(string origin, int order, int min_order, int max_order) { - m_origin = origin; - ostringstream s_order; - ostringstream s_min_order; - ostringstream s_max_order; - s_order << order; - s_min_order << min_order; - s_max_order << max_order; - m_message = "Invalid ordering type " + s_order.str() + - "requested; must be comprised in [" + s_min_order.str() + - "," + s_max_order.str() + "]"; + m_origin = origin; + m_message = "Invalid ordering type " + str(order) + + "requested; must be comprised in [" + str(min_order) + + "," + str(max_order) + "]"; } @@ -266,6 +238,16 @@ GException::fits_open_error::fits_open_error(string origin, string filename, int /*************************************************************************** + * FITS file already opened * + ***************************************************************************/ +GException::fits_already_opened::fits_already_opened(string origin, string filename) +{ + m_origin = origin; + m_message = "GFits object has already the FITS file '" + filename + "' opened"; +} + + +/*************************************************************************** * FITS keyword not found error * ***************************************************************************/ GException::fits_key_not_found::fits_key_not_found(string origin, string keyname, int status) @@ -273,3 +255,33 @@ GException::fits_key_not_found::fits_key_not_found(string origin, string keyname m_origin = origin; m_message = "Keyword '" + keyname + "' not found in header"; } + + +/*************************************************************************** + * Unknown HDU type found * + ***************************************************************************/ +GException::fits_unknown_HDU_type::fits_unknown_HDU_type(string origin, int type) +{ + m_origin = origin; + m_message = "HDU type " + str(type) + " is not known"; +} + + +/*************************************************************************** + * HDU is not a table * + ***************************************************************************/ +GException::fits_HDU_not_a_table::fits_HDU_not_a_table(string origin, int type) +{ + m_origin = origin; + m_message = "HDU is not a table (type " + str(type) + ")"; +} + + +/*************************************************************************** + * HDU is not a table * + ***************************************************************************/ +GException::fits_invalid_type::fits_invalid_type(string origin, string message) +{ + m_origin = origin; + m_message = message; +} diff --git a/src/Makefile.am b/src/Makefile.am index 26825e2..daef142 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -15,6 +15,11 @@ files = GException.cpp \ fits/GFitsHeaderCard.cpp \ fits/GFitsHeader.cpp \ fits/GFitsData.cpp \ + fits/GFitsImage.cpp \ + fits/GFitsAsciiTable.cpp \ + fits/GFitsBinTable.cpp \ + fits/GFitsTableCol.cpp \ + fits/GFitsTableDblCol.cpp \ fits/GFitsHDU.cpp \ fits/GFits.cpp \ fit/GFitPar.cpp \ diff --git a/src/Makefile.in b/src/Makefile.in index d21c1a2..2d22ba4 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -120,6 +120,11 @@ files = GException.cpp \ fits/GFitsHeaderCard.cpp \ fits/GFitsHeader.cpp \ fits/GFitsData.cpp \ + fits/GFitsImage.cpp \ + fits/GFitsAsciiTable.cpp \ + fits/GFitsBinTable.cpp \ + fits/GFitsTableCol.cpp \ + fits/GFitsTableDblCol.cpp \ fits/GFitsHDU.cpp \ fits/GFits.cpp \ fit/GFitPar.cpp \ @@ -144,10 +149,11 @@ LTLIBRARIES = $(lib_LTLIBRARIES) libgamma_la_LIBADD = am__objects_1 = GException.lo GData.lo GResponse.lo GObservation.lo \ - GFitsHeaderCard.lo GFitsHeader.lo GFitsData.lo GFitsHDU.lo \ - GFits.lo GFitPar.lo GVector.lo GMatrixBase.lo GMatrix.lo \ - GSymMatrix.lo GSparseMatrix.lo GSparseSymbolic.lo \ - GSparseNumeric.lo + GFitsHeaderCard.lo GFitsHeader.lo GFitsData.lo GFitsImage.lo \ + GFitsAsciiTable.lo GFitsBinTable.lo GFitsTableCol.lo \ + GFitsTableDblCol.lo GFitsHDU.lo GFits.lo GFitPar.lo GVector.lo \ + GMatrixBase.lo GMatrix.lo GSymMatrix.lo GSparseMatrix.lo \ + GSparseSymbolic.lo GSparseNumeric.lo am_libgamma_la_OBJECTS = $(am__objects_1) libgamma_la_OBJECTS = $(am_libgamma_la_OBJECTS) @@ -214,6 +220,11 @@ GObservation.lo: obs/GObservation.cpp GFitsHeaderCard.lo: fits/GFitsHeaderCard.cpp GFitsHeader.lo: fits/GFitsHeader.cpp GFitsData.lo: fits/GFitsData.cpp +GFitsImage.lo: fits/GFitsImage.cpp +GFitsAsciiTable.lo: fits/GFitsAsciiTable.cpp +GFitsBinTable.lo: fits/GFitsBinTable.cpp +GFitsTableCol.lo: fits/GFitsTableCol.cpp +GFitsTableDblCol.lo: fits/GFitsTableDblCol.cpp GFitsHDU.lo: fits/GFitsHDU.cpp GFits.lo: fits/GFits.cpp GFitPar.lo: fit/GFitPar.cpp @@ -296,6 +307,51 @@ GFitsData.obj: fits/GFitsData.cpp GFitsData.lo: fits/GFitsData.cpp $(LIBTOOL) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o GFitsData.lo `test -f 'fits/GFitsData.cpp' || echo '$(srcdir)/'`fits/GFitsData.cpp +GFitsImage.o: fits/GFitsImage.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o GFitsImage.o `test -f 'fits/GFitsImage.cpp' || echo '$(srcdir)/'`fits/GFitsImage.cpp + +GFitsImage.obj: fits/GFitsImage.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o GFitsImage.obj `cygpath -w fits/GFitsImage.cpp` + +GFitsImage.lo: fits/GFitsImage.cpp + $(LIBTOOL) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o GFitsImage.lo `test -f 'fits/GFitsImage.cpp' || echo '$(srcdir)/'`fits/GFitsImage.cpp + +GFitsAsciiTable.o: fits/GFitsAsciiTable.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o GFitsAsciiTable.o `test -f 'fits/GFitsAsciiTable.cpp' || echo '$(srcdir)/'`fits/GFitsAsciiTable.cpp + +GFitsAsciiTable.obj: fits/GFitsAsciiTable.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o GFitsAsciiTable.obj `cygpath -w fits/GFitsAsciiTable.cpp` + +GFitsAsciiTable.lo: fits/GFitsAsciiTable.cpp + $(LIBTOOL) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o GFitsAsciiTable.lo `test -f 'fits/GFitsAsciiTable.cpp' || echo '$(srcdir)/'`fits/GFitsAsciiTable.cpp + +GFitsBinTable.o: fits/GFitsBinTable.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o GFitsBinTable.o `test -f 'fits/GFitsBinTable.cpp' || echo '$(srcdir)/'`fits/GFitsBinTable.cpp + +GFitsBinTable.obj: fits/GFitsBinTable.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o GFitsBinTable.obj `cygpath -w fits/GFitsBinTable.cpp` + +GFitsBinTable.lo: fits/GFitsBinTable.cpp + $(LIBTOOL) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o GFitsBinTable.lo `test -f 'fits/GFitsBinTable.cpp' || echo '$(srcdir)/'`fits/GFitsBinTable.cpp + +GFitsTableCol.o: fits/GFitsTableCol.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o GFitsTableCol.o `test -f 'fits/GFitsTableCol.cpp' || echo '$(srcdir)/'`fits/GFitsTableCol.cpp + +GFitsTableCol.obj: fits/GFitsTableCol.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o GFitsTableCol.obj `cygpath -w fits/GFitsTableCol.cpp` + +GFitsTableCol.lo: fits/GFitsTableCol.cpp + $(LIBTOOL) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o GFitsTableCol.lo `test -f 'fits/GFitsTableCol.cpp' || echo '$(srcdir)/'`fits/GFitsTableCol.cpp + +GFitsTableDblCol.o: fits/GFitsTableDblCol.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o GFitsTableDblCol.o `test -f 'fits/GFitsTableDblCol.cpp' || echo '$(srcdir)/'`fits/GFitsTableDblCol.cpp + +GFitsTableDblCol.obj: fits/GFitsTableDblCol.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o GFitsTableDblCol.obj `cygpath -w fits/GFitsTableDblCol.cpp` + +GFitsTableDblCol.lo: fits/GFitsTableDblCol.cpp + $(LIBTOOL) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o GFitsTableDblCol.lo `test -f 'fits/GFitsTableDblCol.cpp' || echo '$(srcdir)/'`fits/GFitsTableDblCol.cpp + GFitsHDU.o: fits/GFitsHDU.cpp $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o GFitsHDU.o `test -f 'fits/GFitsHDU.cpp' || echo '$(srcdir)/'`fits/GFitsHDU.cpp diff --git a/src/fits/GFits.cpp b/src/fits/GFits.cpp index 6e90221..62fff75 100644 --- a/src/fits/GFits.cpp +++ b/src/fits/GFits.cpp @@ -20,6 +20,9 @@ /* __ Namespaces _________________________________________________________ */ /* __ Method name definitions ____________________________________________ */ +#define G_OPEN "GFits::open(std::string)" +#define G_SAVETO "GFits::saveto(const std::string, int)" +#define G_FREE_MEM "GFits::free_members()" /* __ Macros _____________________________________________________________ */ @@ -121,23 +124,25 @@ GFits& GFits::operator= (const GFits& fits) * Open FITS file * * ----------------------------------------------------------------------- * ***************************************************************************/ -void GFits::open(std::string filename) +void GFits::open(const std::string filename) { + // Don't allow opening if another file is already open + if (m_fitsfile != NULL) + throw GException::fits_already_opened(G_OPEN, m_filename); + // Open FITS file int status = 0; status = __ffopen(&m_fitsfile, filename.c_str(), 1, &status); - if (status != 0) { - throw GException::fits_open_error("GFits::open(std::string)", filename, status); - } + if (status != 0) + throw GException::fits_open_error(G_OPEN, filename, status); // Store FITS file attributes m_filename = filename; // Determine number of HDUs status = __ffthdu(m_fitsfile, &m_num_hdu, &status); - if (status != 0) { - throw GException::fits_error("GFits::open(std::string)", status); - } + if (status != 0) + throw GException::fits_error(G_OPEN, status); // Allocate HDUs if (m_hdu != NULL) delete [] m_hdu; @@ -153,25 +158,99 @@ void GFits::open(std::string filename) /*************************************************************************** - * Close FITS file * + * Save FITS file * * ----------------------------------------------------------------------- * ***************************************************************************/ -void GFits::close(void) +void GFits::save(void) { - // Close FITS file + // Save all HDUs + for (int i = 0; i < m_num_hdu; ++i) + m_hdu[i].save(); + + // Return + return; +} + + +/*************************************************************************** + * Save to specified FITS file * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFits::saveto(const std::string filename, int clobber) +{ + // Create a copy of existing object. Recall that the copy does not + // carry over the FITS filename and pointer. Those will be automatically + // reset to the initial values in the new object. + GFits new_fits = *this; + + // Check if specified FITS file exists. If yes, saving will only be + // allowed if clobber is true ... int status = 0; - status = __ffclos(m_fitsfile, &status); + status = __ffopen(&(new_fits.m_fitsfile), filename.c_str(), 1, &status); + if (status == 0) { + if (!clobber) { + throw GException::fits_open_error(G_SAVETO, filename, status); + //throw file_exists ... + } + //delete existing file + //... + } - // Handle error - if (status != 0) { - throw GException::fits_error("GFits::close(std::string)", status); + // ... otherwise create a new FITS file now + else { + // ... } + // Save all HDUs + for (int i = 0; i < m_num_hdu; ++i) + new_fits.m_hdu[i].save(); + // Return return; } +/*************************************************************************** + * Close FITS file * + * ----------------------------------------------------------------------- * + * Closing detaches a FITS file from the GFits object and returns a clean * + * empty object. * + ***************************************************************************/ +void GFits::close(void) +{ + // Close file and free all members + free_members(); + + // Initialise members + init_members(); + + // Return + return; +} + + +/*************************************************************************** + * Get pointer to HDU * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsHDU* GFits::hdu(std::string extname) +{ + // Initialise result to NULL pointer + GFitsHDU* ptr = NULL; + + // Search for specified extension + for (int i = 0; i < m_num_hdu; ++i) { + if (m_hdu[i].extname() == extname) { + ptr = &(m_hdu[i]); + break; + } + } + + // Return pointer + return ptr; +} + + /*========================================================================== = = = GFits private methods = @@ -198,12 +277,17 @@ void GFits::init_members(void) /*************************************************************************** * Copy class members * * ----------------------------------------------------------------------- * + * The function does not copy the FITS filename and FITS file pointer. * + * This prevents that several copies of the FITS file pointer exist in * + * different instances of GFits, which would lead to confusion since one * + * instance could close the file while for another it still would be * + * opened. The rule ONE INSTANCE - ONE FILE applies. * ***************************************************************************/ void GFits::copy_members(const GFits& fits) { - // Copy GFits attributes - m_filename = fits.m_filename; - m_fitsfile = fits.m_fitsfile; + // Reset FITS file attributes + m_filename.clear(); + m_fitsfile = NULL; // Copy HDUs if (fits.m_hdu != NULL && fits.m_num_hdu > 0) { @@ -224,6 +308,14 @@ void GFits::copy_members(const GFits& fits) ***************************************************************************/ void GFits::free_members(void) { + // If FITS file has been opened then close it now + if (m_fitsfile != NULL) { + int status = 0; + status = __ffclos(m_fitsfile, &status); + if (status != 0) + throw GException::fits_error(G_FREE_MEM, status); + } + // Free memory if (m_hdu != NULL) delete [] m_hdu; diff --git a/src/fits/GFitsAsciiTable.cpp b/src/fits/GFitsAsciiTable.cpp new file mode 100644 index 0000000..f9a956b --- /dev/null +++ b/src/fits/GFitsAsciiTable.cpp @@ -0,0 +1,272 @@ +/*************************************************************************** + * GFitsAsciiTable.cpp - FITS ASCII table class * + * ----------------------------------------------------------------------- * + * copyright : (C) 2008 by Jurgen Knodlseder * + * ----------------------------------------------------------------------- * + * * + * 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 2 of the License, or * + * (at your option) any later version. * + * * + * ----------------------------------------------------------------------- * + ***************************************************************************/ + +/* __ Includes ___________________________________________________________ */ +#include "GException.hpp" +#include "GFitsAsciiTable.hpp" +#include // cout, cerr + +/* __ Namespaces _________________________________________________________ */ + +/* __ Method name definitions ____________________________________________ */ + +/* __ Macros _____________________________________________________________ */ + +/* __ Coding definitions _________________________________________________ */ + +/* __ Debug definitions __________________________________________________ */ + + + +/*========================================================================== + = = + = GFitsAsciiTable constructors/destructors = + = = + ==========================================================================*/ + +/*************************************************************************** + * Constructor * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsAsciiTable::GFitsAsciiTable() : GFitsData() +{ + // Initialise class members for clean destruction + init_members(); + + // Return + return; +} + + +/*************************************************************************** + * Copy constructor * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsAsciiTable::GFitsAsciiTable(const GFitsAsciiTable& table) : GFitsData(table) +{ + // Initialise class members for clean destruction + init_members(); + + // Copy members + copy_members(table); + + // Return + return; +} + + +/*************************************************************************** + * Destructor * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsAsciiTable::~GFitsAsciiTable() +{ + // Free members + free_members(); + + // Return + return; +} + + +/*========================================================================== + = = + = GFitsAsciiTable operators = + = = + ==========================================================================*/ + +/*************************************************************************** + * Assignment operator * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsAsciiTable& GFitsAsciiTable::operator= (const GFitsAsciiTable& table) +{ + // Execute only if object is not identical + if (this != &table) { + + // Copy base class members + this->GFitsData::operator=(table); + + // Free members + free_members(); + + // Initialise private members for clean destruction + init_members(); + + // Copy members + copy_members(table); + + } // endif: object was not identical + + // Return this object + return *this; +} + + +/*========================================================================== + = = + = GFitsAsciiTable public methods = + = = + ==========================================================================*/ + +/*************************************************************************** + * Open Table * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsAsciiTable::open(__fitsfile* fptr) +{ + cout << "open ASCII table" << endl; + // Return + return; +} + + +/*************************************************************************** + * Close Table * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsAsciiTable::close(void) +{ + // Free members + free_members(); + + // Initialise members + init_members(); + + // Return + return; +} + + +/*************************************************************************** + * Return pointer to column with specified name * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsTableCol* GFitsAsciiTable::column(const std::string colname) +{ + // Initialise pointer + GFitsTableCol* ptr = NULL; + + // If there are columns then search for the specified name + if (m_columns != NULL) { + for (int i = 0; i < m_cols; ++i) { + if (m_columns[i]->name() == colname) { + ptr = m_columns[i]; + break; + } + } + } + + // Return column pointer + return ptr; +} + + +/*************************************************************************** + * Return pointer to column with number * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsTableCol* GFitsAsciiTable::column(const int colnum) +{ + // Initialise pointer + GFitsTableCol* ptr = NULL; + + // If there are columns then search for the specified name + if (m_columns != NULL) { + ptr = m_columns[colnum]; + } + + // Return column pointer + return ptr; +} + + + + +/*========================================================================== + = = + = GFitsAsciiTable private methods = + = = + ==========================================================================*/ + +/*************************************************************************** + * Initialise class members * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsAsciiTable::init_members(void) +{ + // Initialise members + m_rows = 0; + m_cols = 0; + m_columns = NULL; + + // Return + return; +} + + +/*************************************************************************** + * Copy class members * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsAsciiTable::copy_members(const GFitsAsciiTable& table) +{ + // Copy attributes + m_rows = table.m_rows; + m_cols = table.m_cols; + + // Copy column definition + if (table.m_columns != NULL && m_cols > 0) { + m_columns = new GFitsTableCol*[m_cols]; + for (int i = 0; i < m_cols; ++i) + m_columns[i] = table.m_columns[i]->clone(); + } + + // Return + return; +} + + +/*************************************************************************** + * Delete class members * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsAsciiTable::free_members(void) +{ + // Free memory + for (int i = 0; i < m_cols; ++i) { + if (m_columns[i] != NULL) delete m_columns[i]; + } + if (m_columns != NULL) delete [] m_columns; + + // Mark memory as freed + m_columns = NULL; + + // Return + return; +} + + +/*========================================================================== + = = + = GFitsAsciiTable friends = + = = + ==========================================================================*/ + + +/*========================================================================== + = = + = Other functions used by GFitsAsciiTable = + = = + ==========================================================================*/ diff --git a/src/fits/GFitsBinTable.cpp b/src/fits/GFitsBinTable.cpp new file mode 100644 index 0000000..43b7ceb --- /dev/null +++ b/src/fits/GFitsBinTable.cpp @@ -0,0 +1,335 @@ +/*************************************************************************** + * GFitsBinTable.cpp - FITS binary table class * + * ----------------------------------------------------------------------- * + * copyright : (C) 2008 by Jurgen Knodlseder * + * ----------------------------------------------------------------------- * + * * + * 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 2 of the License, or * + * (at your option) any later version. * + * * + * ----------------------------------------------------------------------- * + ***************************************************************************/ + +/* __ Includes ___________________________________________________________ */ +#include "GException.hpp" +#include "GFitsBinTable.hpp" +#include "GFitsTableDblCol.hpp" +#include // cout, cerr + +/* __ Namespaces _________________________________________________________ */ + +/* __ Method name definitions ____________________________________________ */ +#define G_OPEN "GFitsBinTable::open(fitsfile*)" + +/* __ Macros _____________________________________________________________ */ + +/* __ Coding definitions _________________________________________________ */ + +/* __ Debug definitions __________________________________________________ */ + + +/*========================================================================== + = = + = GFitsBinTable constructors/destructors = + = = + ==========================================================================*/ + +/*************************************************************************** + * Constructor * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsBinTable::GFitsBinTable() : GFitsData() +{ + // Initialise class members for clean destruction + init_members(); + + // Return + return; +} + + +/*************************************************************************** + * Copy constructor * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsBinTable::GFitsBinTable(const GFitsBinTable& table) : GFitsData(table) +{ + // Initialise class members for clean destruction + init_members(); + + // Copy members + copy_members(table); + + // Return + return; +} + + +/*************************************************************************** + * Destructor * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsBinTable::~GFitsBinTable() +{ + // Free members + free_members(); + + // Return + return; +} + + +/*========================================================================== + = = + = GFitsBinTable operators = + = = + ==========================================================================*/ + +/*************************************************************************** + * Assignment operator * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsBinTable& GFitsBinTable::operator= (const GFitsBinTable& table) +{ + // Execute only if object is not identical + if (this != &table) { + + // Copy base class members + this->GFitsData::operator=(table); + + // Free members + free_members(); + + // Initialise private members for clean destruction + init_members(); + + // Copy members + copy_members(table); + + } // endif: object was not identical + + // Return this object + return *this; +} + + +/*========================================================================== + = = + = GFitsBinTable public methods = + = = + ==========================================================================*/ + +/*************************************************************************** + * Open Table * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsBinTable::open(__fitsfile* fptr) +{ + // Determine number of rows in table + int status = 0; + long nrows = 0; + status = __ffgnrw(fptr, &nrows, &status); + if (status != 0) + throw GException::fits_error(G_OPEN, status); + else + m_rows = (int)nrows; + + // Determine number of columns in table + status = __ffgncl(fptr, &m_cols, &status); + if (status != 0) + throw GException::fits_error(G_OPEN, status); + + // Allocate memory for column pointers + if (m_columns != NULL) delete [] m_columns; + m_columns = new GFitsTableCol*[m_cols]; + + // Get table column information + int typecode = 0; + long repeat = 0; + long width = 0; + for (int i = 0; i < m_cols; ++i) { + + // Get column name + char keyname[10]; + char value[80]; + sprintf(keyname, "TTYPE%d", i+1); + status = __ffgkey(fptr, keyname, value, NULL, &status); + if (status != 0) + throw GException::fits_error(G_OPEN, status); + value[strlen(value)-1] = '\0'; + + // Get column definition + status = __ffgtcl(fptr, i+1, &typecode, &repeat, &width, &status); + if (status != 0) + throw GException::fits_error(G_OPEN, status); + + // Allocate column + switch (typecode) { + case __TDOUBLE: + m_columns[i] = new GFitsTableDblCol(); + break; + default: + m_columns[i] = NULL; + m_columns[i] = new GFitsTableDblCol(); // TO BE REMOVED LATER AND REPLACED BY THROW !!! + break; + } + + // Store column definition + m_columns[i]->set_name(&(value[1])); + m_columns[i]->set_colnum(i+1); + m_columns[i]->set_type(typecode); + m_columns[i]->set_repeat(repeat); + m_columns[i]->set_width(width); + m_columns[i]->set_length(m_rows); + m_columns[i]->set_fitsfile(fptr); + + cout << m_columns[i]->name() << " : " + << m_columns[i]->colnum() << " : " + << m_columns[i]->repeat() << " : " + << m_columns[i]->width() << " : " + << m_columns[i]->length() << endl; + } + + // Return + return; +} + + +/*************************************************************************** + * Close Table * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsBinTable::close(void) +{ + // Free members + free_members(); + + // Initialise members + init_members(); + + // Return + return; +} + + +/*************************************************************************** + * Return pointer to column with specified name * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsTableCol* GFitsBinTable::column(const std::string colname) +{ + // Initialise pointer + GFitsTableCol* ptr = NULL; + + // If there are columns then search for the specified name + if (m_columns != NULL) { + for (int i = 0; i < m_cols; ++i) { + if (m_columns[i]->name() == colname) { + ptr = m_columns[i]; + break; + } + } + } + + // Return column pointer + return ptr; +} + + +/*************************************************************************** + * Return pointer to column with number * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsTableCol* GFitsBinTable::column(const int colnum) +{ + // Initialise pointer + GFitsTableCol* ptr = NULL; + + // If there are columns then search for the specified name + if (m_columns != NULL) { + ptr = m_columns[colnum]; + } + + // Return column pointer + return ptr; +} + + +/*========================================================================== + = = + = GFitsBinTable private methods = + = = + ==========================================================================*/ + +/*************************************************************************** + * Initialise class members * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsBinTable::init_members(void) +{ + // Initialise members + m_rows = 0; + m_cols = 0; + m_columns = NULL; + + // Return + return; +} + + +/*************************************************************************** + * Copy class members * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsBinTable::copy_members(const GFitsBinTable& table) +{ + // Copy attributes + m_rows = table.m_rows; + m_cols = table.m_cols; + + // Copy column definition + if (table.m_columns != NULL && m_cols > 0) { + m_columns = new GFitsTableCol*[m_cols]; + for (int i = 0; i < m_cols; ++i) + m_columns[i] = table.m_columns[i]->clone(); + } + + // Return + return; +} + + +/*************************************************************************** + * Delete class members * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsBinTable::free_members(void) +{ + // Free memory + for (int i = 0; i < m_cols; ++i) { + if (m_columns[i] != NULL) delete m_columns[i]; + } + if (m_columns != NULL) delete [] m_columns; + + // Mark memory as freed + m_columns = NULL; + + // Return + return; +} + + +/*========================================================================== + = = + = GFitsBinTable friends = + = = + ==========================================================================*/ + + +/*========================================================================== + = = + = Other functions used by GFitsBinTable = + = = + ==========================================================================*/ diff --git a/src/fits/GFitsData.cpp b/src/fits/GFitsData.cpp index 6eebfd6..02ec67b 100644 --- a/src/fits/GFitsData.cpp +++ b/src/fits/GFitsData.cpp @@ -17,7 +17,6 @@ #include "GFitsData.hpp" /* __ Namespaces _________________________________________________________ */ -using namespace std; /* __ Method name definitions ____________________________________________ */ @@ -35,6 +34,50 @@ using namespace std; = = ==========================================================================*/ +/*************************************************************************** + * Constructor * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsData::GFitsData() +{ + // Initialise class members for clean destruction + init_members(); + + // Return + return; +} + + +/*************************************************************************** + * Copy constructor * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsData::GFitsData(const GFitsData& data) +{ + // Initialise class members for clean destruction + init_members(); + + // Copy members + copy_members(data); + + // Return + return; +} + + +/*************************************************************************** + * Destructor * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsData::~GFitsData() +{ + // Free members + free_members(); + + // Return + return; +} + /*========================================================================== = = @@ -42,20 +85,111 @@ using namespace std; = = ==========================================================================*/ +/*************************************************************************** + * Assignment operator * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsData& GFitsData::operator= (const GFitsData& data) +{ + // Execute only if object is not identical + if (this != &data) { + + // Free members + free_members(); + + // Initialise private members for clean destruction + init_members(); + + // Copy members + copy_members(data); + + } // endif: object was not identical + + // Return this object + return *this; +} + /*========================================================================== = = - = GFitsData methods = + = GFitsData public methods = = = ==========================================================================*/ +/*************************************************************************** + * Open Data * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsData::open(__fitsfile* fptr) +{ + // Return + return; +} + + +/*************************************************************************** + * Close Data * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsData::close(void) +{ + // Free members + free_members(); + + // Initialise members + init_members(); + + // Return + return; +} + /*========================================================================== = = - = GFitsData private functions = + = GFitsData private methods = = = ==========================================================================*/ +/*************************************************************************** + * Initialise class members * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsData::init_members(void) +{ + // Initialise members + + // Return + return; +} + + +/*************************************************************************** + * Copy class members * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsData::copy_members(const GFitsData& data) +{ + // Copy attributes + + // Copy data + + // Return + return; +} + + +/*************************************************************************** + * Delete class members * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsData::free_members(void) +{ + // Free memory + + // Return + return; +} + /*========================================================================== = = diff --git a/src/fits/GFitsHDU.cpp b/src/fits/GFitsHDU.cpp index d74bb93..600849f 100644 --- a/src/fits/GFitsHDU.cpp +++ b/src/fits/GFitsHDU.cpp @@ -20,6 +20,9 @@ /* __ Namespaces _________________________________________________________ */ /* __ Method name definitions ____________________________________________ */ +#define G_OPEN "GFitsHDU::open(int)" +#define G_COLUMN "GFitsHDU::column(std::string)" +#define G_MOVE2HDU "GFitsHDU::move2hdu(void)" /* __ Macros _____________________________________________________________ */ @@ -132,26 +135,40 @@ void GFitsHDU::open(__fitsfile* fptr, int hdunum) // Get HDU type int status = 0; status = __ffghdt(m_fitsfile, &m_type, &status); - if (status != 0) { - throw GException::fits_error("GFitsHDU::open(int)", status); - } - - // Open HDU header - m_header.open(m_fitsfile); + if (status != 0) + throw GException::fits_error(G_OPEN, status); + + // Allocate and open HDU header + if (m_header != NULL) delete m_header; + m_header = new GFitsHeader(); + m_header->open(m_fitsfile); // Open HDU data area - // + if (m_data != NULL) delete m_data; + switch (m_type) { + case 0: // Image HDU + m_data = new GFitsImage(); + break; + case 1: // ASCII Table HDU + m_data = new GFitsAsciiTable(); + break; + case 2: // Binary Table HDU + m_data = new GFitsBinTable(); + break; + default: + throw GException::fits_unknown_HDU_type(G_OPEN, m_type); + break; + } + m_data->open(m_fitsfile); // Get HDU name from header - GFitsHeaderCard* extname = m_header.card("EXTNAME"); - if (extname == NULL) { + m_name = m_header->string("EXTNAME"); + if (m_name.length() == 0) { if (hdunum == 1) m_name = "Primary"; else m_name = "NoName"; } - else - m_name = extname->value(); cout << "HDU #" << hdunum << ": " << m_type << " : " << m_name << endl; @@ -161,35 +178,51 @@ void GFitsHDU::open(__fitsfile* fptr, int hdunum) /*************************************************************************** - * Close HDU * + * Save HDU to FITS file * * ----------------------------------------------------------------------- * ***************************************************************************/ -void GFitsHDU::close(void) +void GFitsHDU::save(void) { + // Move to HDU + move2hdu(); + + // Save header + //... + + // Save data + //... + // Return return; } - /*************************************************************************** - * Return pointer to HDU header * + * Return pointer to column of table * * ----------------------------------------------------------------------- * ***************************************************************************/ -GFitsHeader* GFitsHDU::header(void) +GFitsTableCol* GFitsHDU::column(const std::string colname) const { - // Return header pointer - return &m_header; -} - - -/*************************************************************************** - * Return pointer to HDU data * - * ----------------------------------------------------------------------- * - ***************************************************************************/ -GFitsData* GFitsHDU::data(void) -{ - // Return data pointer - return &m_data; + // Initialise pointer + GFitsTableCol* ptr = NULL; + + // Get pointer to table column + switch (m_type) { + case 0: // Image HDU + throw GException::fits_HDU_not_a_table(G_COLUMN, m_type); + break; + case 1: // ASCII Table HDU + ptr = ((GFitsAsciiTable*)this->data())->column(colname); + break; + case 2: // Binary Table HDU + ptr = ((GFitsBinTable*)this->data())->column(colname); + break; + default: + throw GException::fits_unknown_HDU_type(G_COLUMN, m_type); + break; + } + + // Return pointer + return ptr; } @@ -208,8 +241,8 @@ void GFitsHDU::init_members(void) // Initialise members m_fitsfile = NULL; m_type = 0; - //m_header = NULL; - //m_data = NULL; + m_header = NULL; + m_data = NULL; // Return return; @@ -219,14 +252,21 @@ void GFitsHDU::init_members(void) /*************************************************************************** * Copy class members * * ----------------------------------------------------------------------- * + * The function does not copy the FITS file pointer. This prevents that * + * several copies of the FITS file pointer exist in different instances of * + * GFitsHDU, which would lead to confusion since one instance could close * + * the file while for another it still would be opened. * + * The rule ONE INSTANCE - ONE FILE applies. * ***************************************************************************/ void GFitsHDU::copy_members(const GFitsHDU& hdu) { - // Copy membres - m_fitsfile = hdu.m_fitsfile; - m_type = hdu.m_type; - m_header = hdu.m_header; - m_data = hdu.m_data; + // Reset FITS file attributes + m_fitsfile = NULL; + + // Copy other membres + m_type = hdu.m_type; + m_header = hdu.m_header->clone(); + m_data = hdu.m_data->clone(); // Return return; @@ -239,6 +279,14 @@ void GFitsHDU::copy_members(const GFitsHDU& hdu) ***************************************************************************/ void GFitsHDU::free_members(void) { + // Free memory + if (m_header != NULL) delete m_header; + if (m_data != NULL) delete m_data; + + // Signal free pointers + m_header = NULL; + m_data = NULL; + // Return return; } @@ -253,9 +301,8 @@ void GFitsHDU::move2hdu(void) // Move FITS file pointer to HDU int status = 0; status = __ffmahd(m_fitsfile, m_num, NULL, &status); - if (status != 0) { - throw GException::fits_error("GFitsHDU::move2hdu(void)", status); - } + if (status != 0) + throw GException::fits_error(G_MOVE2HDU, status); // Return return; diff --git a/src/fits/GFitsHeader.cpp b/src/fits/GFitsHeader.cpp index b38e0e7..e19d4ea 100644 --- a/src/fits/GFitsHeader.cpp +++ b/src/fits/GFitsHeader.cpp @@ -20,6 +20,7 @@ /* __ Namespaces _________________________________________________________ */ /* __ Method name definitions ____________________________________________ */ +#define G_OPEN "GFitsHeader::open(int)" /* __ Macros _____________________________________________________________ */ @@ -118,7 +119,7 @@ GFitsHeader& GFitsHeader::operator= (const GFitsHeader& header) ==========================================================================*/ /*************************************************************************** - * Open HDU * + * Open Header * * ----------------------------------------------------------------------- * ***************************************************************************/ void GFitsHeader::open(__fitsfile* fptr) @@ -126,9 +127,8 @@ void GFitsHeader::open(__fitsfile* fptr) // Determine number of cards in header int status = 0; status = __ffghsp(fptr, &m_num_cards, NULL, &status); - if (status != 0) { - throw GException::fits_error("GFitsHeader::open(int)", status); - } + if (status != 0) + throw GException::fits_error(G_OPEN, status); // Drop any old cards if (m_card != NULL) delete [] m_card; @@ -139,7 +139,15 @@ void GFitsHeader::open(__fitsfile* fptr) // Read all cards for (int i = 0; i < m_num_cards; ++i) { m_card[i].read(fptr, i+1); - //cout << m_card[i].keyname() << " " << m_card[i].value() << " " << m_card[i].comment() << endl; + /* + cout << m_card[i].keyname() << " " + << m_card[i].value() << " " + << m_card[i].value_type() << " " + << m_card[i].string() << " " + << m_card[i].real() << " " + << m_card[i].integer() << " " + << m_card[i].comment() << endl; + */ } // Return @@ -148,29 +156,121 @@ void GFitsHeader::open(__fitsfile* fptr) /*************************************************************************** - * Get specified header card * + * Close Header * * ----------------------------------------------------------------------- * ***************************************************************************/ -GFitsHeaderCard* GFitsHeader::card(const std::string keyname) +void GFitsHeader::close(void) { - // Return card pointer - return GFitsHeader::card_ptr(keyname); + // Free members + free_members(); + + // Initialise members + init_members(); + + // Return + return; } /*************************************************************************** - * Get specified header card * + * Get specified header card value as string * * ----------------------------------------------------------------------- * ***************************************************************************/ -GFitsHeaderCard* GFitsHeader::card(const int cardno) +std::string GFitsHeader::string(const std::string keyname) { - // Set card pointer - GFitsHeaderCard* ptr = (cardno >= 0 && cardno < m_num_cards) - ? &(m_card[cardno]) : NULL; + // Get card pointer + GFitsHeaderCard* ptr = GFitsHeader::card(keyname); - // Return card pointer - return ptr; + // Get string value + std::string value = (ptr != NULL) ? ptr->string() : ""; + + // Return string + return value; +} + + +/*************************************************************************** + * Get specified header card value as string * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +std::string GFitsHeader::string(const int cardno) +{ + // Get card pointer + GFitsHeaderCard* ptr = GFitsHeader::card(cardno); + + // Get string value + std::string value = (ptr != NULL) ? ptr->string() : ""; + + // Return string + return value; +} + + +/*************************************************************************** + * Get specified header card value as double * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +double GFitsHeader::real(const std::string keyname) +{ + // Get card pointer + GFitsHeaderCard* ptr = GFitsHeader::card(keyname); + + // Get string value + double value = (ptr != NULL) ? ptr->real() : 0.0; + + // Return double + return value; +} + + +/*************************************************************************** + * Get specified header card value as double * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +double GFitsHeader::real(const int cardno) +{ + // Get card pointer + GFitsHeaderCard* ptr = GFitsHeader::card(cardno); + + // Get string value + double value = (ptr != NULL) ? ptr->real() : 0.0; + + // Return double + return value; +} + + +/*************************************************************************** + * Get specified header card value as int * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +int GFitsHeader::integer(const std::string keyname) +{ + // Get card pointer + GFitsHeaderCard* ptr = GFitsHeader::card(keyname); + + // Get string value + int value = (ptr != NULL) ? ptr->integer() : 0; + + // Return double + return value; +} + + +/*************************************************************************** + * Get specified header card value as int * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +int GFitsHeader::integer(const int cardno) +{ + // Get card pointer + GFitsHeaderCard* ptr = GFitsHeader::card(cardno); + + // Get string value + int value = (ptr != NULL) ? ptr->integer() : 0; + // Return double + return value; } diff --git a/src/fits/GFitsHeaderCard.cpp b/src/fits/GFitsHeaderCard.cpp index 66ab03d..95b718d 100644 --- a/src/fits/GFitsHeaderCard.cpp +++ b/src/fits/GFitsHeaderCard.cpp @@ -19,11 +19,26 @@ /* __ Namespaces _________________________________________________________ */ /* __ Method name definitions ____________________________________________ */ +#define G_READ_NUM "GFitsHeaderCard::read(fitsfile*, int)" +#define G_READ_STR "GFitsHeaderCard::read(fitsfile*, std::string)" +#define G_WRITE "GFitsHeaderCard::write(fitsfile*)" + +/* __ Definitions ________________________________________________________ */ +#define CT_UNKNOWN 0 +#define CT_INVALID 1 +#define CT_STRING 2 +#define CT_INT 3 +#define CT_FLOAT 4 +#define CT_BOOL 5 +#define CT_COMMENT 6 +#define CT_HISTORY 7 /* __ Macros _____________________________________________________________ */ /* __ Coding definitions _________________________________________________ */ +/* __ Enumerations _______________________________________________________ */ + /* __ Debug definitions __________________________________________________ */ @@ -115,86 +130,99 @@ GFitsHeaderCard& GFitsHeaderCard::operator= (const GFitsHeaderCard& card) ==========================================================================*/ /*************************************************************************** - * Get keyname * + * Return value as string * * ----------------------------------------------------------------------- * ***************************************************************************/ -std::string GFitsHeaderCard::keyname(void) +std::string GFitsHeaderCard::string(void) { - return m_keyname; -} - - -/*************************************************************************** - * Get value * - * ----------------------------------------------------------------------- * - ***************************************************************************/ -std::string GFitsHeaderCard::value(void) -{ - return m_value; -} - + // Initialize return value to empty string + std::string result; + + // Type dependent conversion + switch (m_value_type) { + case CT_STRING: + int length = m_value.length() - 2; + if (length > 0) + result = m_value.substr(1, length); + break; + case CT_INT: + case CT_FLOAT: + case CT_BOOL: + result = m_value; + break; + default: + break; + } + + // Return string + return result; -/*************************************************************************** - * Get unit * - * ----------------------------------------------------------------------- * - ***************************************************************************/ -std::string GFitsHeaderCard::unit(void) -{ - return m_unit; } /*************************************************************************** - * Get comment * + * Return value as double * * ----------------------------------------------------------------------- * ***************************************************************************/ -std::string GFitsHeaderCard::comment(void) +double GFitsHeaderCard::real(void) { - return m_comment; -} - + // Initialize return value to 0.0 + double result = 0.0; + + // Type dependent conversion + switch (m_value_type) { + case CT_STRING: + int length = m_value.length() - 2; + if (length > 0) + result = atof(m_value.substr(1, length).c_str()); + break; + case CT_INT: + case CT_FLOAT: + result = atof(m_value.c_str()); + break; + case CT_BOOL: + result = (m_value == "T") ? 1.0 : 0.0; + break; + default: + break; + } + + // Return string + return result; -/*************************************************************************** - * Set keyname * - * ----------------------------------------------------------------------- * - ***************************************************************************/ -void GFitsHeaderCard::set_keyname(std::string& keyname) -{ - m_keyname = keyname; - return; } /*************************************************************************** - * Set value * + * Return value as int * * ----------------------------------------------------------------------- * ***************************************************************************/ -void GFitsHeaderCard::set_value(std::string& value) +int GFitsHeaderCard::integer(void) { - m_value = value; - return; -} - - -/*************************************************************************** - * Set unit * - * ----------------------------------------------------------------------- * - ***************************************************************************/ -void GFitsHeaderCard::set_unit(std::string& unit) -{ - m_unit = unit; - return; -} - + // Initialize return value to 0 + int result = 0; + + // Type dependent conversion + switch (m_value_type) { + case CT_STRING: + int length = m_value.length() - 2; + if (length > 0) + result = atoi(m_value.substr(1, length).c_str()); + break; + case CT_INT: + case CT_FLOAT: + result = atoi(m_value.c_str()); + break; + case CT_BOOL: + result = (m_value == "T") ? 1 : 0; + break; + default: + break; + } + + // Return string + return result; -/*************************************************************************** - * Set comment * - * ----------------------------------------------------------------------- * - ***************************************************************************/ -void GFitsHeaderCard::set_comment(std::string& comment) -{ - m_comment = comment; - return; } @@ -210,14 +238,16 @@ void GFitsHeaderCard::read(__fitsfile* fptr, int keynum) char comment[80]; int status = 0; status = __ffgkyn(fptr, keynum, keyname, value, comment, &status); - if (status != 0) { - throw GException::fits_error("GFitsHeaderCard::read(fitsfile*, int)", status); - } + if (status != 0) + throw GException::fits_error(G_READ_NUM, status); // Store result m_keyname.assign(keyname); m_value.assign(value); m_comment.assign(comment); + + // Determine card type + m_value_type = get_value_type(); // Return return; @@ -237,18 +267,19 @@ void GFitsHeaderCard::read(__fitsfile* fptr, std::string keyname) status = __ffgkey(fptr, (char*)keyname.c_str(), value, comment, &status); // Catch error - if (status == 202) { // Keyword not found - throw GException::fits_key_not_found("GFitsHeaderCard::read(fitsfile*, std::string)", keyname, status); - } - else if (status != 0) { // Any other error - throw GException::fits_error("GFitsHeaderCard::read(fitsfile*, std::string)", status); - } + if (status == 202) // Keyword not found + throw GException::fits_key_not_found(G_READ_STR, keyname, status); + else if (status != 0) // Any other error + throw GException::fits_error(G_READ_STR, status); // Store result m_keyname = keyname; m_value.assign(value); m_comment.assign(comment); + // Determine card type + m_value_type = get_value_type(); + // Return return; } @@ -264,17 +295,14 @@ void GFitsHeaderCard::write(__fitsfile* fptr) int status = 0; status = __ffuky(fptr, __TSTRING, (char*)m_keyname.c_str(), (char*)m_value.c_str(), (char*)m_comment.c_str(), &status); - if (status != 0) { - throw GException::fits_error("GFitsHeaderCard::write(fitsfile*", status); - } + if (status != 0) + throw GException::fits_error(G_WRITE, status); // Return return; } - - /*========================================================================== = = = GFitsHeaderCard private methods = @@ -290,6 +318,7 @@ void GFitsHeaderCard::init_members(void) // Initialise members m_keyname.clear(); m_value.clear(); + m_value_type = CT_UNKNOWN; m_unit.clear(); m_comment.clear(); @@ -305,10 +334,11 @@ void GFitsHeaderCard::init_members(void) void GFitsHeaderCard::copy_members(const GFitsHeaderCard& card) { // Copy membres - m_keyname = card.m_keyname; - m_value = card.m_value; - m_unit = card.m_unit; - m_comment = card.m_comment; + m_keyname = card.m_keyname; + m_value = card.m_value; + m_value_type = card.m_value_type; + m_unit = card.m_unit; + m_comment = card.m_comment; // Return return; @@ -324,3 +354,59 @@ void GFitsHeaderCard::free_members(void) // Return return; } + + +/*************************************************************************** + * Determine card value type * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +int GFitsHeaderCard::get_value_type(void) +{ + // Get index of last string element + int last = m_value.length() - 1; + + // If value is empty then we either have a COMMENT or HISTORY card or + // we don't know the type + if (last < 0) { + if (m_keyname == "COMMENT") + return CT_COMMENT; + if (m_keyname == "HISTORY") + return CT_HISTORY; + return CT_UNKNOWN; + } + + // If value is enclosed in parentheses we have a string + if (m_value[0] == '\x27' && m_value[last] == '\x27') + return CT_STRING; + + // If value has only one digit and is either 'F' or 'T' we have a + // Boolean + if (last == 0 && (m_value[0] == 'F' || m_value[0] == 'T')) + return CT_BOOL; + + // If value is composed only of numeric or '+' or '-' characters we + // have an integer. If in addition we have '.' or 'e' or 'E' we (may) + // have a float. + int found_int = 0; + int found_float = 0; + for (int i = 0; i <= last; ++i) { + if ((m_value[i] >= '0' && m_value[i] <= '9') || m_value[i] == '+' || m_value[i] == '-') + found_int = 1; + else if (m_value[i] == '.' || m_value[i] == 'e' || m_value[i] == 'E') + found_float = 1; + else + return CT_INVALID; + } + + // If we are still alive we should have now either a float or an integer + if (found_int) { + if (found_float) { + return CT_FLOAT; + } + else { + return CT_INT; + } + } + else + return CT_INVALID; +} diff --git a/src/fits/GFitsImage.cpp b/src/fits/GFitsImage.cpp new file mode 100644 index 0000000..88762ab --- /dev/null +++ b/src/fits/GFitsImage.cpp @@ -0,0 +1,210 @@ +/*************************************************************************** + * GFitsImage.cpp - FITS image class * + * ----------------------------------------------------------------------- * + * copyright : (C) 2008 by Jurgen Knodlseder * + * ----------------------------------------------------------------------- * + * * + * 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 2 of the License, or * + * (at your option) any later version. * + * * + * ----------------------------------------------------------------------- * + ***************************************************************************/ + +/* __ Includes ___________________________________________________________ */ +#include "GException.hpp" +#include "GFitsImage.hpp" +#include // cout, cerr + +/* __ Namespaces _________________________________________________________ */ + +/* __ Method name definitions ____________________________________________ */ + +/* __ Macros _____________________________________________________________ */ + +/* __ Coding definitions _________________________________________________ */ + +/* __ Debug definitions __________________________________________________ */ + + + +/*========================================================================== + = = + = GFitsImage constructors/destructors = + = = + ==========================================================================*/ + +/*************************************************************************** + * Constructor * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsImage::GFitsImage() : GFitsData() +{ + // Initialise class members for clean destruction + init_members(); + + // Return + return; +} + + +/*************************************************************************** + * Copy constructor * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsImage::GFitsImage(const GFitsImage& image) : GFitsData(image) +{ + // Initialise class members for clean destruction + init_members(); + + // Copy members + copy_members(image); + + // Return + return; +} + + +/*************************************************************************** + * Destructor * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsImage::~GFitsImage() +{ + // Free members + free_members(); + + // Return + return; +} + + +/*========================================================================== + = = + = GFitsImage operators = + = = + ==========================================================================*/ + +/*************************************************************************** + * Assignment operator * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsImage& GFitsImage::operator= (const GFitsImage& image) +{ + // Execute only if object is not identical + if (this != &image) { + + // Copy base class members + this->GFitsData::operator=(image); + + // Free members + free_members(); + + // Initialise private members for clean destruction + init_members(); + + // Copy members + copy_members(image); + + } // endif: object was not identical + + // Return this object + return *this; +} + + +/*========================================================================== + = = + = GFitsImage public methods = + = = + ==========================================================================*/ + +/*************************************************************************** + * Open Image * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsImage::open(__fitsfile* fptr) +{ + cout << "open image" << endl; + // Return + return; +} + + +/*************************************************************************** + * Close Image * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsImage::close(void) +{ + // Free members + free_members(); + + // Initialise members + init_members(); + + // Return + return; +} + + +/*========================================================================== + = = + = GFitsImage private methods = + = = + ==========================================================================*/ + +/*************************************************************************** + * Initialise class members * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsImage::init_members(void) +{ + // Initialise members + + // Return + return; +} + + +/*************************************************************************** + * Copy class members * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsImage::copy_members(const GFitsImage& image) +{ + // Copy attributes + + // Copy data + + // Return + return; +} + + +/*************************************************************************** + * Delete class members * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsImage::free_members(void) +{ + // Free memory + + // Return + return; +} + + +/*========================================================================== + = = + = GFitsImage friends = + = = + ==========================================================================*/ + + +/*========================================================================== + = = + = Other functions used by GFitsImage = + = = + ==========================================================================*/ diff --git a/src/fits/GFitsTableCol.cpp b/src/fits/GFitsTableCol.cpp new file mode 100644 index 0000000..663f1f6 --- /dev/null +++ b/src/fits/GFitsTableCol.cpp @@ -0,0 +1,192 @@ +/*************************************************************************** + * GFitsTableCol.cpp - FITS table column abstract base class * + * ----------------------------------------------------------------------- * + * copyright : (C) 2008 by Jurgen Knodlseder * + * ----------------------------------------------------------------------- * + * * + * 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 2 of the License, or * + * (at your option) any later version. * + * * + * ----------------------------------------------------------------------- * + ***************************************************************************/ + +/* __ Includes ___________________________________________________________ */ +#include "GException.hpp" +#include "GFitsTableCol.hpp" +#include // cout, cerr + +/* __ Namespaces _________________________________________________________ */ + +/* __ Method name definitions ____________________________________________ */ + +/* __ Macros _____________________________________________________________ */ + +/* __ Coding definitions _________________________________________________ */ + +/* __ Debug definitions __________________________________________________ */ + + +/*========================================================================== + = = + = GFitsTableCol constructors/destructors = + = = + ==========================================================================*/ + +/*************************************************************************** + * Constructor * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsTableCol::GFitsTableCol() +{ + // Initialise class members for clean destruction + init_members(); + + // Return + return; +} + + +/*************************************************************************** + * Copy constructor * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsTableCol::GFitsTableCol(const GFitsTableCol& column) +{ + // Initialise class members for clean destruction + init_members(); + + // Copy members + copy_members(column); + + // Return + return; +} + + +/*************************************************************************** + * Destructor * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsTableCol::~GFitsTableCol() +{ + // Free members + free_members(); + + // Return + return; +} + + +/*========================================================================== + = = + = GFitsTableCol operators = + = = + ==========================================================================*/ + +/*************************************************************************** + * Assignment operator * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsTableCol& GFitsTableCol::operator= (const GFitsTableCol& column) +{ + // Execute only if object is not identical + if (this != &column) { + + // Free members + free_members(); + + // Initialise private members for clean destruction + init_members(); + + // Copy members + copy_members(column); + + } // endif: object was not identical + + // Return this object + return *this; +} + + +/*========================================================================== + = = + = GFitsTableCol public methods = + = = + ==========================================================================*/ + + +/*========================================================================== + = = + = GFitsTableCol private methods = + = = + ==========================================================================*/ + +/*************************************************************************** + * Initialise class members * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsTableCol::init_members(void) +{ + // Initialise members + m_name.clear(); + m_colnum = 0; + m_type = 0; + m_repeat = 0; + m_width = 0; + m_length = 0; + m_fitsfile = NULL; + + // Return + return; +} + + +/*************************************************************************** + * Copy class members * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsTableCol::copy_members(const GFitsTableCol& column) +{ + // Copy attributes + m_name = column.m_name; + m_colnum = column.m_colnum; + m_type = column.m_type; + m_repeat = column.m_repeat; + m_width = column.m_width; + m_length = column.m_length; + m_fitsfile = column.m_fitsfile; + + // Return + return; +} + + +/*************************************************************************** + * Delete class members * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsTableCol::free_members(void) +{ + // Free memory + + // Mark memory as freed + + // Return + return; +} + + +/*========================================================================== + = = + = GFitsTableCol friends = + = = + ==========================================================================*/ + + +/*========================================================================== + = = + = Other functions used by GFitsTableCol = + = = + ==========================================================================*/ diff --git a/src/fits/GFitsTableDblCol.cpp b/src/fits/GFitsTableDblCol.cpp new file mode 100644 index 0000000..bf29c80 --- /dev/null +++ b/src/fits/GFitsTableDblCol.cpp @@ -0,0 +1,339 @@ +/*************************************************************************** + * GFitsTableDblCol.cpp - FITS table double column class * + * ----------------------------------------------------------------------- * + * copyright : (C) 2008 by Jurgen Knodlseder * + * ----------------------------------------------------------------------- * + * * + * 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 2 of the License, or * + * (at your option) any later version. * + * * + * ----------------------------------------------------------------------- * + ***************************************************************************/ + +/* __ Includes ___________________________________________________________ */ +#include "GException.hpp" +#include "GFitsTableDblCol.hpp" +#include // cout, cerr + +/* __ Namespaces _________________________________________________________ */ + +/* __ Method name definitions ____________________________________________ */ +#define G_STRING "GFitsTableDblCol::string(const int&, const int&)" +#define G_REAL "GFitsTableDblCol::real(const int&, const int&)" +#define G_INTEGER "GFitsTableDblCol::integer(const int&, const int&)" +#define G_PTR_STRING "GFitsTableDblCol::ptr_string()" +#define G_PTR_FLOAT "GFitsTableDblCol::ptr_float()" +#define G_PTR_SHORT "GFitsTableDblCol::ptr_short()" +#define G_PTR_LONG "GFitsTableDblCol::ptr_long()" +#define G_PTR_INT "GFitsTableDblCol::ptr_int()" +#define G_LOAD "GFitsTableDblCol::load()" + +/* __ Macros _____________________________________________________________ */ + +/* __ Coding definitions _________________________________________________ */ + +/* __ Debug definitions __________________________________________________ */ + + +/*========================================================================== + = = + = GFitsTableDblCol constructors/destructors = + = = + ==========================================================================*/ + +/*************************************************************************** + * Constructor * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsTableDblCol::GFitsTableDblCol() : GFitsTableCol() +{ + // Initialise class members for clean destruction + init_members(); + + // Return + return; +} + + +/*************************************************************************** + * Copy constructor * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsTableDblCol::GFitsTableDblCol(const GFitsTableDblCol& column) : GFitsTableCol(column) +{ + // Initialise class members for clean destruction + init_members(); + + // Copy members + copy_members(column); + + // Return + return; +} + + +/*************************************************************************** + * Destructor * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsTableDblCol::~GFitsTableDblCol() +{ + // Free members + free_members(); + + // Return + return; +} + + +/*========================================================================== + = = + = GFitsTableDblCol operators = + = = + ==========================================================================*/ + +/*************************************************************************** + * Assignment operator * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +GFitsTableDblCol& GFitsTableDblCol::operator= (const GFitsTableDblCol& column) +{ + // Execute only if object is not identical + if (this != &column) { + + // Copy base class members + this->GFitsTableCol::operator=(column); + + // Free members + free_members(); + + // Initialise private members for clean destruction + init_members(); + + // Copy members + copy_members(column); + + } // endif: object was not identical + + // Return this object + return *this; +} + + +/*========================================================================== + = = + = GFitsTableDblCol public methods = + = = + ==========================================================================*/ + +/*************************************************************************** + * Get string value * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +std::string GFitsTableDblCol::string(const int& row, const int& col) +{ + // Check row value + if (row < 0 || row >= m_length) + throw GException::out_of_range(G_STRING, row, 0, m_length-1); + + // Check col value + if (col < 0 || col >= m_repeat) + throw GException::out_of_range(G_STRING, col, 0, m_repeat-1); + + // Make sure that data are loaded + if (m_data == NULL) + load(); + + // Get index + int inx = row * m_repeat + col; + + // Convert double into string + ostringstream s_value; + s_value << scientific << m_data[inx]; + + // Return value + return s_value.str(); +} + + +/*************************************************************************** + * Get real value * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +double GFitsTableDblCol::real(const int& row, const int& col) +{ + // Check row value + if (row < 0 || row >= m_length) + throw GException::out_of_range(G_REAL, row, 0, m_length-1); + + // Check col value + if (col < 0 || col >= m_repeat) + throw GException::out_of_range(G_REAL, col, 0, m_repeat-1); + + // Make sure that data are loaded + if (m_data == NULL) + load(); + + // Get index + int inx = row * m_repeat + col; + + // Return value + return m_data[inx]; +} + + +/*************************************************************************** + * Get int value * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +int GFitsTableDblCol::integer(const int& row, const int& col) +{ + // Check row value + if (row < 0 || row >= m_length) + throw GException::out_of_range(G_INTEGER, row, 0, m_length-1); + + // Check col value + if (col < 0 || col >= m_repeat) + throw GException::out_of_range(G_INTEGER, col, 0, m_repeat-1); + + // Make sure that data are loaded + if (m_data == NULL) + load(); + + // Get index + int inx = row * m_repeat + col; + + // Convert double into int + int value = (int)m_data[inx]; + + // Return value + return value; +} + + +/*************************************************************************** + * Access to invalid data pointers * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +std::string* GFitsTableDblCol::ptr_string(void) +{ + throw GException::fits_invalid_type(G_PTR_STRING, "No pointer allowed to array"); +} +float* GFitsTableDblCol::ptr_float(void) +{ + throw GException::fits_invalid_type(G_PTR_FLOAT, "No pointer allowed to array"); +} +short* GFitsTableDblCol::ptr_short(void) +{ + throw GException::fits_invalid_type(G_PTR_SHORT, "No pointer allowed to array"); +} +long* GFitsTableDblCol::ptr_long(void) +{ + throw GException::fits_invalid_type(G_PTR_LONG, "No pointer allowed to array"); +} +int* GFitsTableDblCol::ptr_int(void) +{ + throw GException::fits_invalid_type(G_PTR_INT, "No pointer allowed to array"); +} + + +/*========================================================================== + = = + = GFitsTableDblCol private methods = + = = + ==========================================================================*/ + +/*************************************************************************** + * Initialise class members * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsTableDblCol::init_members(void) +{ + // Initialise members + m_size = 0; + m_data = NULL; + m_nulval = 0.0; + + // Return + return; +} + + +/*************************************************************************** + * Copy class members * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsTableDblCol::copy_members(const GFitsTableDblCol& column) +{ + // Copy attributes + m_size = column.m_size; + m_nulval = column.m_nulval; + + // Copy column data + if (column.m_data != NULL && m_size > 0) { + if (m_data != NULL) delete [] m_data; + m_data = new double[m_size]; + memcpy(m_data, column.m_data, m_size*sizeof(double)); + } + + // Return + return; +} + + +/*************************************************************************** + * Delete class members * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsTableDblCol::free_members(void) +{ + // Free memory + if (m_data != NULL) delete [] m_data; + + // Mark memory as freed + m_data = NULL; + + // Return + return; +} + + +/*************************************************************************** + * Load column data * + * ----------------------------------------------------------------------- * + ***************************************************************************/ +void GFitsTableDblCol::load(void) +{ + // Calculate size of memory + m_size = m_repeat * m_length; + + // Allocate memory + if (m_data != NULL) delete [] m_data; + m_data = new double[m_size]; + + // Load column data + int status = 0; + status = __ffgcv(m_fitsfile, __TDOUBLE, m_colnum, 1, 1, m_size, + &m_nulval, m_data, NULL, &status); + if (status != 0) + throw GException::fits_error(G_LOAD, status); + + // Return + return; +} + + +/*========================================================================== + = = + = GFitsTableDblCol friends = + = = + ==========================================================================*/ + + +/*========================================================================== + = = + = Other functions used by GFitsTableDblCol = + = = + ==========================================================================*/ diff --git a/test/test_GFits.cpp b/test/test_GFits.cpp index daf0294..1c38d02 100644 --- a/test/test_GFits.cpp +++ b/test/test_GFits.cpp @@ -28,6 +28,19 @@ void test_open(void) try { GFits fits; fits.open("srcid.fits"); + + // Access data via conversion functions + cout << fits.hdu("LAT_POINT_SOURCE_CATALOG")->column("ID_3EG_PROB_1")->string(0) << endl; + cout << fits.hdu("LAT_POINT_SOURCE_CATALOG")->column("ID_3EG_PROB_1")->real(0) << endl; + cout << fits.hdu("LAT_POINT_SOURCE_CATALOG")->column("ID_3EG_PROB_1")->integer(0) << endl; + + // Access data via pointer + double* ptr = fits.hdu("LAT_POINT_SOURCE_CATALOG")->column("ID_3EG_PROB_1")->ptr_double(); + cout << ptr[0] << endl; + + GFitsTableDblCol column = *((GFitsTableDblCol*)fits.hdu("LAT_POINT_SOURCE_CATALOG")->column("ID_3EG_PROB_1")); + cout << column.real(0) << endl; + } catch (exception &e) { std::cout << std::endl << "TEST ERROR: Unable to open FITS file." << std::endl; -- libgit2 0.21.4