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