00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef PLANCK_FITSHANDLE_H
00035 #define PLANCK_FITSHANDLE_H
00036
00037 #include <string>
00038 #include <vector>
00039 #include "fitsio.h"
00040 #include "arr.h"
00041 #include "datatypes.h"
00042
00043
00044
00045
00046 template<typename T> struct FITSUTIL {};
00047
00048 template<> struct FITSUTIL<signed char>
00049 { enum { DTYPE=TBYTE }; };
00050 template<> struct FITSUTIL<short>
00051 { enum { DTYPE=TSHORT }; };
00052 template<> struct FITSUTIL<int>
00053 { enum { DTYPE=TINT }; };
00054 template<> struct FITSUTIL<long>
00055 { enum { DTYPE=TLONG }; };
00056 template<> struct FITSUTIL<long long>
00057 { enum { DTYPE=TLONGLONG }; };
00058 template<> struct FITSUTIL<float>
00059 { enum { DTYPE=TFLOAT }; };
00060 template<> struct FITSUTIL<double>
00061 { enum { DTYPE=TDOUBLE }; };
00062
00063
00064
00065 inline int ftc2type (int ftc)
00066 {
00067 switch (ftc)
00068 {
00069 case TLOGICAL : return PLANCK_BOOL;
00070 case TBYTE : return PLANCK_INT8;
00071 case TSHORT : return PLANCK_INT16;
00072 case TINT32BIT: return PLANCK_INT32;
00073 case TLONGLONG: return PLANCK_INT64;
00074 case TFLOAT : return PLANCK_FLOAT32;
00075 case TDOUBLE : return PLANCK_FLOAT64;
00076 case TSTRING : return PLANCK_STRING;
00077 default: throw Message_error ("ftc2type: unsupported component type");
00078 }
00079 }
00080
00081
00082
00083 inline int type2ftc (int type)
00084 {
00085 switch (type)
00086 {
00087 case PLANCK_BOOL : return TLOGICAL;
00088 case PLANCK_INT8 : return TBYTE;
00089 case PLANCK_INT16 : return TSHORT;
00090 case PLANCK_INT32 : return TINT32BIT;
00091 case PLANCK_INT64 : return TLONGLONG;
00092 case PLANCK_FLOAT32: return TFLOAT;
00093 case PLANCK_FLOAT64: return TDOUBLE;
00094 case PLANCK_STRING : return TSTRING;
00095 default: throw Message_error ("type2ftc: unsupported component type");
00096 }
00097 }
00098
00099
00100 class fitscolumn
00101 {
00102 private:
00103 std::string name_, unit_;
00104 int64 repcount_;
00105 int type_;
00106
00107 public:
00108 fitscolumn()
00109 : repcount_(0), type_(-1) {}
00110
00111
00112 fitscolumn (const std::string &nm, const std::string &un, int64 rc,
00113 int tp)
00114 : name_(nm), unit_(un), repcount_(rc), type_(tp) {}
00115
00116
00117 const std::string &name() const {return name_;}
00118
00119 const std::string &unit() const {return unit_;}
00120
00121 int64 repcount() const {return repcount_;}
00122
00123 int type() const {return type_;}
00124 };
00125
00126
00127 class fitshandle
00128 {
00129 private:
00130 enum { INVALID = -4711 };
00131
00132 mutable int status;
00133 fitsfile *fptr;
00134 int hdutype_, bitpix_;
00135 std::vector<int64> axes_;
00136 std::vector<fitscolumn> columns_;
00137 int64 nrows_;
00138
00139 void check_errors() const;
00140
00141 void clean_data();
00142 void clean_all();
00143
00144 void assert_connected (const std::string &func) const
00145 {
00146 planck_assert (hdutype_!=INVALID,
00147 func + ": not connected to a HDU");
00148 }
00149 void assert_table_hdu (const std::string &func, unsigned int col) const
00150 {
00151 planck_assert ((hdutype_==ASCII_TBL) || (hdutype_==BINARY_TBL),
00152 func + ": HDU is not a table");
00153 planck_assert (col>0 && col<=columns_.size(),
00154 func + ": column number out of range");
00155 }
00156 void assert_image_hdu (const std::string &func) const
00157 {
00158 planck_assert ((hdutype_==IMAGE_HDU), func + ": HDU is not an image");
00159 }
00160
00161 void init_image();
00162 void init_asciitab();
00163 void init_bintab();
00164 void init_data();
00165
00166 void check_key_present(const std::string &name)const ;
00167
00168 void read_col (int colnum, void *data, int64 ndata, int dtype,
00169 int64 offset) const;
00170 void write_col (int colnum, const void *data, int64 ndata, int dtype,
00171 int64 offset);
00172
00173 public:
00174
00175 typedef enum { CREATE,
00176 OPEN
00177 } openmethod;
00178
00179
00180
00181
00182
00183 fitshandle ()
00184 : status(0), fptr(0), hdutype_(INVALID), bitpix_(INVALID), nrows_(0) {}
00185
00186
00187
00188 fitshandle (const std::string &fname, openmethod mode=OPEN,
00189 int rwmode=READONLY)
00190 : status(0), fptr(0), hdutype_(INVALID), bitpix_(INVALID), nrows_(0)
00191 {
00192 if (mode==OPEN)
00193 open (fname, rwmode);
00194 else
00195 create (fname);
00196 }
00197
00198
00199
00200
00201 fitshandle (const std::string &fname, int hdunum, int rwmode=READONLY)
00202 : status(0), fptr(0), hdutype_(INVALID), bitpix_(INVALID), nrows_(0)
00203 {
00204 open (fname, rwmode);
00205 goto_hdu (hdunum);
00206 }
00207
00208
00209 ~fitshandle() { clean_all(); }
00210
00211
00212
00213
00214 void open (const std::string &fname, int rwmode=READONLY);
00215
00216 void create (const std::string &fname);
00217
00218 void close () { clean_all(); }
00219
00220 static void delete_file (const std::string &name);
00221
00222 void goto_hdu (int hdu);
00223
00224 int num_hdus () const;
00225
00226 void assert_pdmtype (const std::string &pdmtype) const;
00227
00228
00229 void insert_bintab (const std::vector<fitscolumn> &cols,
00230 const std::string &extname="xtension");
00231
00232
00233
00234 void insert_asctab (const std::vector<fitscolumn> &cols,
00235 const std::string &extname="xtension");
00236
00237
00238 void insert_image (int btpx, const std::vector<int64> &Axes);
00239
00240
00241 template<typename T>
00242 void insert_image (int btpx, const arr2<T> &data);
00243
00244
00245
00246 void write_checksum();
00247
00248
00249
00250
00251
00252
00253
00254
00255 int bitpix() const
00256 {
00257 assert_image_hdu ("fitshandle::bitpix()");
00258 return bitpix_;
00259 }
00260
00261 int hdutype() const {return hdutype_;}
00262
00263 const std::vector<int64> &axes() const
00264 {
00265 assert_image_hdu ("fitshandle::axes()");
00266 return axes_;
00267 }
00268
00269 const std::string &colname(int i) const
00270 {
00271 assert_table_hdu("fitshandle::colname()",i);
00272 return columns_[i-1].name();
00273 }
00274
00275 const std::string &colunit(int i) const
00276 {
00277 assert_table_hdu("fitshandle::colunit()",i);
00278 return columns_[i-1].unit();
00279 }
00280
00281 int64 repcount(int i) const
00282 {
00283 assert_table_hdu("fitshandle::repcount()",i);
00284 return columns_[i-1].repcount();
00285 }
00286
00287 int coltype(int i) const
00288 {
00289 assert_table_hdu("fitshandle::coltype()",i);
00290 return columns_[i-1].type();
00291 }
00292
00293 int ncols() const
00294 {
00295 assert_table_hdu("fitshandle::ncols()",1);
00296 return columns_.size();
00297 }
00298
00299 int64 nrows() const
00300 {
00301 assert_table_hdu("fitshandle::nrows()",1);
00302 return nrows_;
00303 }
00304
00305
00306 int64 nelems(int i) const
00307 {
00308 assert_table_hdu("fitshandle::nelems()",i);
00309 if (columns_[i-1].type()==TSTRING) return nrows_;
00310 return nrows_*columns_[i-1].repcount();
00311 }
00312
00313
00314
00315
00316
00317
00318
00319
00320 void copy_header (const fitshandle &orig);
00321
00322
00323
00324 void copy_historified_header (const fitshandle &orig);
00325
00326
00327
00328 void get_all_keys (std::vector<std::string> &keys) const;
00329
00330
00331
00332 template<typename T> void add_key (const std::string &name, const T &value,
00333 const std::string &comment="");
00334
00335 template<typename T> void update_key (const std::string &name,
00336 const T &value, const std::string &comment="");
00337
00338 void delete_key (const std::string &name);
00339
00340 void add_comment (const std::string &comment);
00341
00342 template<typename T> void get_key (const std::string &name, T &value) const;
00343
00344 template<typename T> T get_key (const std::string &name) const
00345 { T tmp; get_key(name, tmp); return tmp; }
00346
00347 bool key_present (const std::string &name) const;
00348
00349 int get_key_type(const std::string &name) const;
00350
00351
00352
00353
00354
00355
00356 void read_column_raw_void
00357 (int colnum, void *data, int type, int64 num, int64 offset=0) const;
00358
00359
00360 template<typename T> void read_column_raw
00361 (int colnum, T *data, int64 num, int64 offset=0) const
00362 { read_column_raw_void (colnum, data, typehelper<T>::id, num, offset); }
00363
00364
00365 template<typename T> void read_column
00366 (int colnum, arr<T> &data, int64 offset=0) const
00367 { read_column_raw (colnum, &(data[0]), data.size(), offset); }
00368
00369 template<typename T> void read_column
00370 (int colnum, T &data, int64 offset=0) const
00371 { read_column_raw (colnum, &data, 1, offset); }
00372
00373
00374 template<typename T> void read_entire_column
00375 (int colnum, arr<T> &data) const
00376 { data.alloc(nelems(colnum)); read_column (colnum, data); }
00377
00378
00379 void write_column_raw_void
00380 (int colnum, const void *data, int type, int64 num, int64 offset=0);
00381
00382
00383 template<typename T> void write_column_raw
00384 (int colnum, const T *data, int64 num, int64 offset=0)
00385 { write_column_raw_void (colnum, data, typehelper<T>::id, num, offset); }
00386
00387
00388 template<typename T> void write_column
00389 (int colnum, const arr<T> &data, int64 offset=0)
00390 { write_column_raw (colnum, &(data[0]), data.size(), offset); }
00391
00392 template<typename T> void write_column
00393 (int colnum, const T &data, int64 offset=0)
00394 { write_column_raw (colnum, &data, 1, offset); }
00395
00396
00397
00398
00399
00400
00401
00402 template<typename T> void read_image (arr2<T> &data) const;
00403
00404 template<typename T> void read_image (arr3<T> &data) const;
00405
00406
00407
00408 template<typename T> void read_subimage
00409 (arr2<T> &data, int xl, int yl) const;
00410
00411
00412
00413 template<typename T> void read_subimage (arr<T> &data, int64 offset=0)
00414 const;
00415
00416
00417 template<typename T> void write_image (const arr2<T> &data);
00418
00419
00420
00421 template<typename T> void write_subimage (const arr<T> &data,
00422 int64 offset=0);
00423
00424
00425
00426 void add_healpix_keys (int datasize);
00427 };
00428
00429
00430
00431
00432 template<> void fitshandle::add_key(const std::string &name,
00433 const bool &value, const std::string &comment);
00434 template<> void fitshandle::add_key (const std::string &name,
00435 const std::string &value, const std::string &comment);
00436 template<> void fitshandle::update_key(const std::string &name,
00437 const bool &value, const std::string &comment);
00438 template<> void fitshandle::update_key (const std::string &name,
00439 const std::string &value, const std::string &comment);
00440 template<> void fitshandle::get_key(const std::string &name,bool &value) const;
00441 template<> void fitshandle::get_key (const std::string &name,
00442 std::string &value) const;
00443
00444 #endif