FitsFileBase.cxx
Go to the documentation of this file.
1 
14 #include "FitsFileBase.h"
15 
16 #include <iostream>
17 
18 #include <cassert>
19 
20 using std::string;
21 
22 using namespace hippodraw;
23 
24 FitsFileBase::FitsFileBase ( const std::string & filename, bool write)
25 {
26  if (!write)
27  {
28  m_status = 0;
29  fits_open_file( &m_fptr, filename.c_str(), READONLY, &m_status );
30  }
31  else
32  {
33  m_status = 0;
34 
35  // Overwrite the FITS file.
36  std::string fn = "!";
37  fn += filename;
38  fits_create_file( &m_fptr, fn.c_str(), &m_status );
39  }
40 }
41 
43 {
44  if ( m_fptr != 0 ) {
45  m_status = 0;
46  fits_close_file ( m_fptr, &m_status );
47  }
48 }
49 
51 {
52  fits_clear_errmsg();
53 }
54 
55 
58 convert ( int i )
59 {
60  static HduType table[] = { Image, Atable, Btable, Any };
61 
62  assert ( i < 3 && i >= 0 );
63 
64  return table [ i ];
65 }
66 
69 getHduType () const
70 {
71  int hdutype;
72  m_status = 0;
73  fits_get_hdu_type ( m_fptr, &hdutype, &m_status );
74 
75  return convert ( hdutype );
76 }
77 
80 getImageType () const
81 {
83  static ImageType table[]
85 
86  int bitpix;
87  m_status = 0;
88  fits_get_img_type ( m_fptr, & bitpix, & m_status );
89  if ( bitpix < 5 && bitpix >= 0 ) {
90  type = table [ bitpix ];
91  }
92 
93  return type;
94 }
95 
96 int
99 {
100  int naxis = 0;
101  m_status = 0;
102  fits_get_img_dim ( m_fptr, & naxis, & m_status );
103  assert ( m_status == 0 );
104 
105  return naxis;
106 }
107 
108 int
111 {
112  int hdunum = 0;
113  m_status = 0;
114  fits_get_num_hdus ( m_fptr, &hdunum, &m_status );
115 
116  return hdunum;
117 }
118 
119 int
121 moveToHDU ( int hdunum )
122 {
123  int hdutype;
124  m_status = 0;
125  fits_movabs_hdu ( m_fptr, hdunum + 1, // count like Fortran
126  &hdutype, &m_status );
127 
128  return m_status;
129 }
130 
131 int
133 moveToHDU ( const std::string & name )
134 {
135  char * extname = const_cast < char * > ( name.c_str() );
136  m_status = 0;
137  fits_movnam_hdu ( m_fptr, ANY_HDU, extname, 0, &m_status );
138 
139  return m_status;
140 }
141 
142 int
144 getHDUNumber () const
145 {
146  int number = 0;
147  //int retval =
148  m_status = 0;
149  fits_get_hdu_num ( m_fptr, &number );
150 
151  return number - 1; // count like C++
152 }
153 
155 {
156  int keyexist, morekeys;
157  m_status = 0;
158  fits_get_hdrspace( m_fptr, &keyexist, &morekeys, &m_status );
159  return keyexist;
160 }
161 
162 double
164 doubleValueForKey ( const char * key ) const
165 {
166  double value;
167  m_status = 0;
168  fits_read_key( m_fptr, TDOUBLE,
169  const_cast<char *>(key), &value, 0, &m_status );
170  return value;
171 }
172 
173 bool
175 hasKey ( const char * key ) const
176 {
177  char value [ FLEN_VALUE ];
178  m_status = 0;
179  fits_read_keyword ( m_fptr, const_cast < char * > ( key ),
180  value, 0, & m_status );
181 
182  return m_status == 0;
183 }
184 
185 int
187 intValueForKey ( const char * key ) const
188 {
189  int value;
190  m_status = 0;
191  fits_read_key( m_fptr, TINT,
192  const_cast<char *>(key), &value, 0, &m_status );
193 
194  return value;
195 }
196 
197 string
199 stringValueForKey ( const char * key ) const
200 {
201  char value [ FLEN_VALUE ];
202  m_status = 0;
203  fits_read_key ( m_fptr, TSTRING,
204  const_cast<char *>(key), &value, 0, &m_status );
205 
206  return string ( value );
207 }
208 
209 int
211 status () const
212 {
213  return m_status;
214 }
215 
216 long
219 {
220  long number = 0;
221 
222  int hdutype;
223  m_status = 0;
224  fits_get_hdu_type ( m_fptr, &hdutype, &m_status );
225 
226  if ( hdutype == IMAGE_HDU ) {
227  int nx = intValueForKey ( "NAXIS1" );
228  int ny = intValueForKey ( "NAXIS2" );
229  number = nx * ny;
230  }
231  else { // table
232  m_status = 0;
233  fits_get_num_rows ( m_fptr, & number, & m_status );
234  }
235 
236  return number;
237 }
238 
239 int
242 {
243  int ncols = 0;
244  m_status = 0;
245  fits_get_num_cols ( m_fptr, &ncols, &m_status );
246 
247  return ncols;
248 }

Generated for HippoDraw Class Library by doxygen