00001
00014 #include "FitsFileBase.h"
00015
00016 #include <iostream>
00017
00018 #include <cassert>
00019
00020 using std::string;
00021
00022 using namespace hippodraw;
00023
00024 FitsFileBase::FitsFileBase ( const std::string & filename, bool write)
00025 {
00026 if (!write)
00027 {
00028 m_status = 0;
00029 fits_open_file( &m_fptr, filename.c_str(), READONLY, &m_status );
00030 }
00031 else
00032 {
00033 m_status = 0;
00034
00035
00036 std::string fn = "!";
00037 fn += filename;
00038 fits_create_file( &m_fptr, fn.c_str(), &m_status );
00039 }
00040 }
00041
00042 FitsFileBase::~FitsFileBase()
00043 {
00044 if ( m_fptr != 0 ) {
00045 m_status = 0;
00046 fits_close_file ( m_fptr, &m_status );
00047 }
00048 }
00049
00050 void FitsFileBase::clearErrorMessageStack( void )
00051 {
00052 fits_clear_errmsg();
00053 }
00054
00055
00056 FitsFileBase::HduType
00057 FitsFileBase::
00058 convert ( int i )
00059 {
00060 static HduType table[] = { Image, Atable, Btable, Any };
00061
00062 assert ( i < 3 && i >= 0 );
00063
00064 return table [ i ];
00065 }
00066
00067 FitsFileBase::HduType
00068 FitsFileBase::
00069 getHduType () const
00070 {
00071 int hdutype;
00072 m_status = 0;
00073 fits_get_hdu_type ( m_fptr, &hdutype, &m_status );
00074
00075 return convert ( hdutype );
00076 }
00077
00078 FitsFileBase::ImageType
00079 FitsFileBase::
00080 getImageType () const
00081 {
00082 ImageType type = NoImg;
00083 static ImageType table[]
00084 = { ByteImg, ShortImg, LongImg, FloatImg, DoubleImg };
00085
00086 int bitpix;
00087 m_status = 0;
00088 fits_get_img_type ( m_fptr, & bitpix, & m_status );
00089 if ( bitpix < 5 && bitpix >= 0 ) {
00090 type = table [ bitpix ];
00091 }
00092
00093 return type;
00094 }
00095
00096 int
00097 FitsFileBase::
00098 getImageDimensions () const
00099 {
00100 int naxis = 0;
00101 m_status = 0;
00102 fits_get_img_dim ( m_fptr, & naxis, & m_status );
00103 assert ( m_status == 0 );
00104
00105 return naxis;
00106 }
00107
00108 int
00109 FitsFileBase::
00110 getNumberOfHDU () const
00111 {
00112 int hdunum = 0;
00113 m_status = 0;
00114 fits_get_num_hdus ( m_fptr, &hdunum, &m_status );
00115
00116 return hdunum;
00117 }
00118
00119 int
00120 FitsFileBase::
00121 moveToHDU ( int hdunum )
00122 {
00123 int hdutype;
00124 m_status = 0;
00125 fits_movabs_hdu ( m_fptr, hdunum + 1,
00126 &hdutype, &m_status );
00127
00128 return m_status;
00129 }
00130
00131 int
00132 FitsFileBase::
00133 moveToHDU ( const std::string & name )
00134 {
00135 char * extname = const_cast < char * > ( name.c_str() );
00136 m_status = 0;
00137 fits_movnam_hdu ( m_fptr, ANY_HDU, extname, 0, &m_status );
00138
00139 return m_status;
00140 }
00141
00142 int
00143 FitsFileBase::
00144 getHDUNumber () const
00145 {
00146 int number = 0;
00147
00148 m_status = 0;
00149 fits_get_hdu_num ( m_fptr, &number );
00150
00151 return number - 1;
00152 }
00153
00154 int FitsFileBase::numKeywords() const
00155 {
00156 int keyexist, morekeys;
00157 m_status = 0;
00158 fits_get_hdrspace( m_fptr, &keyexist, &morekeys, &m_status );
00159 return keyexist;
00160 }
00161
00162 double
00163 FitsFileBase::
00164 doubleValueForKey ( const char * key ) const
00165 {
00166 double value;
00167 m_status = 0;
00168 fits_read_key( m_fptr, TDOUBLE,
00169 const_cast<char *>(key), &value, 0, &m_status );
00170 return value;
00171 }
00172
00173 bool
00174 FitsFileBase::
00175 hasKey ( const char * key ) const
00176 {
00177 char value [ FLEN_VALUE ];
00178 m_status = 0;
00179 fits_read_keyword ( m_fptr, const_cast < char * > ( key ),
00180 value, 0, & m_status );
00181
00182 return m_status == 0;
00183 }
00184
00185 int
00186 FitsFileBase::
00187 intValueForKey ( const char * key ) const
00188 {
00189 int value;
00190 m_status = 0;
00191 fits_read_key( m_fptr, TINT,
00192 const_cast<char *>(key), &value, 0, &m_status );
00193
00194 return value;
00195 }
00196
00197 string
00198 FitsFileBase::
00199 stringValueForKey ( const char * key ) const
00200 {
00201 char value [ FLEN_VALUE ];
00202 m_status = 0;
00203 fits_read_key ( m_fptr, TSTRING,
00204 const_cast<char *>(key), &value, 0, &m_status );
00205
00206 return string ( value );
00207 }
00208
00209 int
00210 FitsFileBase::
00211 status () const
00212 {
00213 return m_status;
00214 }
00215
00216 long
00217 FitsFileBase::
00218 getNumberOfRows () const
00219 {
00220 long number = 0;
00221
00222 int hdutype;
00223 m_status = 0;
00224 fits_get_hdu_type ( m_fptr, &hdutype, &m_status );
00225
00226 if ( hdutype == IMAGE_HDU ) {
00227 int nx = intValueForKey ( "NAXIS1" );
00228 int ny = intValueForKey ( "NAXIS2" );
00229 number = nx * ny;
00230 }
00231 else {
00232 m_status = 0;
00233 fits_get_num_rows ( m_fptr, & number, & m_status );
00234 }
00235
00236 return number;
00237 }
00238
00239 int
00240 FitsFileBase::
00241 getNumberOfColumns () const
00242 {
00243 int ncols = 0;
00244 m_status = 0;
00245 fits_get_num_cols ( m_fptr, &ncols, &m_status );
00246
00247 return ncols;
00248 }