| [839] | 1 | #ifndef FITSFILE_H | 
|---|
|  | 2 | #define FITSFILE_H | 
|---|
|  | 3 |  | 
|---|
|  | 4 | #include "ndatablock.h" | 
|---|
|  | 5 | #include "dvlist.h" | 
|---|
|  | 6 | #include "FitsIO/fitsio.h" | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #define OPENFILE    0 | 
|---|
|  | 9 | #define CREATEFILE  1 | 
|---|
|  | 10 | #define LEN_KEYWORD 9 | 
|---|
|  | 11 |  | 
|---|
| [1218] | 12 | // classes for saving/loading SOPHYA objects to/from FITS files... | 
|---|
|  | 13 | // Guy le Meur (september 2000) | 
|---|
| [875] | 14 |  | 
|---|
| [1209] | 15 |  | 
|---|
| [1218] | 16 | namespace SOPHYA { | 
|---|
| [1209] | 17 |  | 
|---|
| [1218] | 18 | struct BnTblLine; | 
|---|
|  | 19 | class FitsFile; | 
|---|
|  | 20 | class FitsInFile; | 
|---|
|  | 21 | class FitsOutFile; | 
|---|
| [1193] | 22 |  | 
|---|
| [1353] | 23 |  | 
|---|
|  | 24 |  | 
|---|
|  | 25 | class  FitsKeyword | 
|---|
|  | 26 | { | 
|---|
|  | 27 |  | 
|---|
|  | 28 | public: | 
|---|
|  | 29 |  | 
|---|
|  | 30 | FitsKeyword(); | 
|---|
|  | 31 | FitsKeyword(string comment); | 
|---|
|  | 32 | FitsKeyword(string keyname, string value, string comment); | 
|---|
|  | 33 | void writeOnFits(fitsfile* ptr); | 
|---|
|  | 34 |  | 
|---|
|  | 35 | void Print(); | 
|---|
|  | 36 |  | 
|---|
|  | 37 | private: | 
|---|
|  | 38 |  | 
|---|
|  | 39 | char datatype_; | 
|---|
|  | 40 | string keyname_; | 
|---|
|  | 41 | double dvalue_; | 
|---|
|  | 42 | int ivalue_; | 
|---|
|  | 43 | string svalue_; | 
|---|
|  | 44 | string comment_; | 
|---|
|  | 45 | }; | 
|---|
|  | 46 |  | 
|---|
|  | 47 |  | 
|---|
|  | 48 |  | 
|---|
| [903] | 49 | // | 
|---|
| [1136] | 50 | //! Class for managing Interface for SOPHYA objects to FITS Format Files (uses cfitsio lib) | 
|---|
| [875] | 51 |  | 
|---|
| [1353] | 52 | class FitsIOHandler { | 
|---|
| [903] | 53 |  | 
|---|
|  | 54 |  | 
|---|
| [1218] | 55 | public: | 
|---|
| [903] | 56 |  | 
|---|
| [1218] | 57 | virtual ~FitsIOHandler() {} | 
|---|
|  | 58 | void   Read(char flnm[],int hdunum= 0); | 
|---|
|  | 59 | void   Write(char flnm[]) ; | 
|---|
|  | 60 | void   Read(FitsInFile& ifts, int hdunum=0); | 
|---|
|  | 61 | void   Write(FitsOutFile& ofts) ; | 
|---|
| [903] | 62 |  | 
|---|
| [839] | 63 |  | 
|---|
| [1218] | 64 | protected: | 
|---|
|  | 65 |  | 
|---|
|  | 66 | virtual void    ReadFromFits(FitsInFile& is)=0; | 
|---|
|  | 67 | virtual void    WriteToFits(FitsOutFile& os) =0; | 
|---|
|  | 68 |  | 
|---|
|  | 69 | friend class FitsInFile; | 
|---|
|  | 70 | friend class FitsOutFile; | 
|---|
| [1136] | 71 | }; | 
|---|
|  | 72 |  | 
|---|
|  | 73 |  | 
|---|
| [1218] | 74 | //! Class (virtual) for managing FITS format files | 
|---|
|  | 75 | class FitsFile { | 
|---|
| [1136] | 76 |  | 
|---|
| [1218] | 77 | public: | 
|---|
| [1136] | 78 |  | 
|---|
| [1231] | 79 | enum WriteMode {append, clear, unknown}; | 
|---|
|  | 80 |  | 
|---|
|  | 81 | enum FitsExtensionType { | 
|---|
| [1334] | 82 | FitsExtensionType_NULL, | 
|---|
| [1231] | 83 | FitsExtensionType_IMAGE, | 
|---|
|  | 84 | FitsExtensionType_ASCII_TBL, | 
|---|
| [1334] | 85 | FitsExtensionType_BINARY_TBL, | 
|---|
|  | 86 | FitsExtensionType_EOF, | 
|---|
|  | 87 | FitsExtensionType_ERROR | 
|---|
| [1231] | 88 | }; | 
|---|
|  | 89 | enum FitsDataType { | 
|---|
| [1300] | 90 | FitsDataType_NULL, | 
|---|
| [1231] | 91 | FitsDataType_double, | 
|---|
|  | 92 | FitsDataType_float, | 
|---|
|  | 93 | FitsDataType_int, | 
|---|
|  | 94 | FitsDataType_char, | 
|---|
| [1359] | 95 | FitsDataType_ASCII, | 
|---|
|  | 96 | FitsDataType_long, | 
|---|
|  | 97 | FitsDataType_byte | 
|---|
| [1231] | 98 | }; | 
|---|
|  | 99 |  | 
|---|
| [1218] | 100 | FitsFile() { InitNull(); }; | 
|---|
|  | 101 | virtual ~FitsFile(); | 
|---|
|  | 102 | static string GetErrStatus(int status); | 
|---|
|  | 103 | inline  int   statusF() const { return fits_status_;} | 
|---|
| [1234] | 104 | inline void firstImageOnPrimaryHeader(bool choice) {imageOnPrimary_=choice;} | 
|---|
| [1300] | 105 | inline int currentHeaderIndex() {return hdunum_;} | 
|---|
| [1353] | 106 | inline fitsfile* fitsfilePtr() {return fptr_;} | 
|---|
| [1136] | 107 |  | 
|---|
|  | 108 |  | 
|---|
| [1218] | 109 | protected: | 
|---|
| [1136] | 110 |  | 
|---|
| [1218] | 111 | void         ResetStatus(int& status) ; | 
|---|
| [1136] | 112 | static  void printerror(int&) ; | 
|---|
|  | 113 | static  void printerror(int&,char* texte) ; | 
|---|
| [1334] | 114 | inline void  InitNull() {fptr_ = NULL; hdutype_= FitsExtensionType_NULL; hdunum_ = 0; | 
|---|
| [1234] | 115 | fits_status_ = 0; imageOnPrimary_ = true;} | 
|---|
| [1218] | 116 | fitsfile *fptr_;     /**<  pointer to the FITS file, defined in fitsio.h */ | 
|---|
| [1334] | 117 | FitsExtensionType hdutype_;        /**<  image or bintable ? */ | 
|---|
| [1218] | 118 | int hdunum_;         /**<   index of header to be read/written */ | 
|---|
|  | 119 | int fits_status_;    /**< last status returned by fitsio library. updated only by several methods */ | 
|---|
| [1234] | 120 | bool imageOnPrimary_; | 
|---|
| [1136] | 121 |  | 
|---|
| [1218] | 122 | }; | 
|---|
| [1136] | 123 |  | 
|---|
| [1218] | 124 | //! Class for saving  SOPHYA objects on FITS Format Files (uses cfitsio lib) | 
|---|
| [1136] | 125 |  | 
|---|
|  | 126 | class FitsInFile : public  FitsFile { | 
|---|
|  | 127 |  | 
|---|
|  | 128 | public: | 
|---|
|  | 129 | FitsInFile(); | 
|---|
| [1231] | 130 | FitsInFile(string const & flnm); | 
|---|
|  | 131 | FitsInFile(const char * flnm); | 
|---|
| [1143] | 132 | ~FitsInFile() { ; }; | 
|---|
| [1136] | 133 |  | 
|---|
| [1218] | 134 | static int  NbBlocks(char flnm[]); | 
|---|
| [1334] | 135 | int  NbBlocks(); | 
|---|
| [1231] | 136 | static void GetBlockType(char flnm[], int hdunum, FitsExtensionType& typeOfExtension, int& naxis, vector<int>& naxisn, FitsDataType& dataType, DVList& dvl  ); | 
|---|
| [1353] | 137 |  | 
|---|
|  | 138 | void  ReadHeader(int hdunum); | 
|---|
| [1354] | 139 | bool hasKeyword(int hdunum, string keyw); | 
|---|
|  | 140 | string getStringKeyword(int hdunum, string keyname,int& retStatus); | 
|---|
| [1353] | 141 |  | 
|---|
| [1218] | 142 | /*! \return a reference on a DVList containing the keywords from FITS file */ | 
|---|
|  | 143 | inline const DVList& DVListFromFits() const { return dvl_;} | 
|---|
| [1136] | 144 |  | 
|---|
| [1218] | 145 | DVList  DVListFromPrimaryHeader() const; | 
|---|
|  | 146 | void    moveToFollowingHeader(); | 
|---|
| [1047] | 147 |  | 
|---|
| [1136] | 148 |  | 
|---|
|  | 149 |  | 
|---|
|  | 150 |  | 
|---|
|  | 151 | ////////////////////////////////////////////////////////// | 
|---|
|  | 152 | ///////   methods for managing extensions //////////////// | 
|---|
|  | 153 | ////////////////////////////////////////////////////////// | 
|---|
|  | 154 |  | 
|---|
|  | 155 |  | 
|---|
| [1047] | 156 | ///////////////////////////////////////////////////////////// | 
|---|
| [1136] | 157 | //       methods for managing FITS IMAGE extension | 
|---|
|  | 158 | /////////////////////////////////////////////////// | 
|---|
| [1047] | 159 |  | 
|---|
|  | 160 |  | 
|---|
| [1218] | 161 |  | 
|---|
|  | 162 | /*! \return true if the current header  corresponds to a FITS image extension */ | 
|---|
| [1334] | 163 | inline bool IsFitsImage() const { return (hdutype_ == FitsExtensionType_IMAGE);} | 
|---|
| [1047] | 164 |  | 
|---|
| [1136] | 165 |  | 
|---|
|  | 166 |  | 
|---|
| [1218] | 167 | /*! \return number of dimensions of an image extension : NAXIS parameter (in FITS notations)   */ | 
|---|
| [861] | 168 | inline int nbDimOfImage() const {return naxis_;} | 
|---|
| [1218] | 169 |  | 
|---|
|  | 170 | /*! \return a reference on a vector containing sizes of the NAXIS dimensions : NAXIS1, NAXIS2, NAXIS3 etc.  */ | 
|---|
| [1047] | 171 | inline const vector<int>& dimOfImageAxes() const { return naxisn_;} | 
|---|
| [1218] | 172 |  | 
|---|
|  | 173 |  | 
|---|
|  | 174 | /*! \return total number of data in the current IMAGE extension */ | 
|---|
| [839] | 175 | inline int nbOfImageData() const { return nbData_; } | 
|---|
| [903] | 176 |  | 
|---|
| [1300] | 177 | /*! \return data type of the current IMAGE extension */ | 
|---|
|  | 178 | inline FitsFile::FitsDataType ImageType() const {return imageDataType_;} | 
|---|
| [903] | 179 |  | 
|---|
| [1047] | 180 |  | 
|---|
| [1136] | 181 | ////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 182 | //       methods for managing FITS BINARY TABLE or ASCII TABLE extension | 
|---|
|  | 183 | //////////////////////////////////////////////////////////////////////// | 
|---|
| [1047] | 184 |  | 
|---|
|  | 185 |  | 
|---|
|  | 186 |  | 
|---|
|  | 187 |  | 
|---|
| [1218] | 188 | /*! \return true if the current header  corresponds to a FITS ASCII or BINTABLE extension */ | 
|---|
| [1334] | 189 | inline bool IsFitsTable() const {return (hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL);} | 
|---|
| [1047] | 190 |  | 
|---|
|  | 191 |  | 
|---|
| [1353] | 192 |  | 
|---|
|  | 193 |  | 
|---|
| [1218] | 194 | static  void GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows, | 
|---|
| [1136] | 195 | vector<int>& repeat, | 
|---|
|  | 196 | vector<string>& noms, | 
|---|
| [1300] | 197 | vector<FitsDataType>& types, | 
|---|
| [1136] | 198 | vector<int>&  taille_des_chaines); | 
|---|
| [1300] | 199 | FitsDataType   ColTypeFromFits(int nocol) const; | 
|---|
| [1218] | 200 | string ColNameFromFits(int nocol) const; | 
|---|
|  | 201 | int    ColStringLengthFromFits(int nocol) const; | 
|---|
|  | 202 | void   GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char | 
|---|
| [1047] | 203 | ** cdata) ; | 
|---|
| [1218] | 204 | void   GetBinTabLine(long NoLine, BnTblLine& ligne) ; | 
|---|
|  | 205 | void   GetBinTabLine(int NoLine, float* fdata) ; | 
|---|
| [1352] | 206 | void   GetBinTabFCol(r_8* valeurs, int nentries, int NoCol) const; | 
|---|
|  | 207 | void   GetBinTabFCol(r_4* valeurs, int nentries, int NoCol) const; | 
|---|
|  | 208 | void   GetBinTabFCol(int_4* valeurs, int nentries,  int NoCol) const; | 
|---|
| [1218] | 209 | void   GetBinTabFCol(char** valeurs,int nentries, int NoCol) const; | 
|---|
| [1047] | 210 |  | 
|---|
| [1136] | 211 | ///////////////////////////////////////////////////////////// | 
|---|
|  | 212 | //       methods for managing any type of FITS extension | 
|---|
|  | 213 | //////////////////////////////////////////////////////// | 
|---|
| [1045] | 214 |  | 
|---|
| [1334] | 215 | /*! \return true if the current header  is beyond the maximum */ | 
|---|
|  | 216 | inline bool IsFitsEOF() const {return (hdutype_ == FitsExtensionType_EOF);} | 
|---|
|  | 217 | /*! \return true if the current header  is incorrect, following a cfitsio, movavs error */ | 
|---|
|  | 218 | inline bool IsFitsERROR() const {return (hdutype_ == FitsExtensionType_ERROR);} | 
|---|
|  | 219 |  | 
|---|
| [1136] | 220 | int     NbColsFromFits() const; | 
|---|
|  | 221 | int     NentriesFromFits(int nocol) const; | 
|---|
| [1045] | 222 |  | 
|---|
|  | 223 |  | 
|---|
| [1352] | 224 | void    GetSingleColumn(r_8* map, int nentries) const; | 
|---|
| [903] | 225 |  | 
|---|
| [1352] | 226 | void    GetSingleColumn(r_4*  map, int nentries) const; | 
|---|
| [839] | 227 |  | 
|---|
| [1352] | 228 | void    GetSingleColumn(int_4* map, int nentries) const; | 
|---|
| [1047] | 229 |  | 
|---|
| [1353] | 230 |  | 
|---|
|  | 231 |  | 
|---|
|  | 232 |  | 
|---|
|  | 233 |  | 
|---|
| [1136] | 234 | private : | 
|---|
| [1047] | 235 |  | 
|---|
| [1136] | 236 | void InitNull(); | 
|---|
| [1234] | 237 | void getHeader(); | 
|---|
| [1136] | 238 | static void KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum); | 
|---|
| [1300] | 239 | static  void GetImageParameters (fitsfile* fileptr,FitsDataType& dataType,int& naxis,vector<int>& naxisn); | 
|---|
| [839] | 240 |  | 
|---|
| [1300] | 241 | FitsDataType imageDataType_;          /**< fits-Image parameter (bitpix)*/ | 
|---|
| [1218] | 242 | int naxis_;           /**< fits-Image parameter */ | 
|---|
|  | 243 | vector<int> naxisn_;  /**< fits-Image parameters : sizes of dimensions */ | 
|---|
|  | 244 | int nbData_;          /*< fits-Image parameter: number of data */ | 
|---|
|  | 245 | int nrows_;           /**< Bintable parameter */ | 
|---|
|  | 246 | vector<int> repeat_;  /**< Bintable parameter */ | 
|---|
|  | 247 | int nbcols_;          /**< Bintable parameter */ | 
|---|
|  | 248 | vector<string> noms_; /**< Bintable parameter: column names */ | 
|---|
| [1300] | 249 | vector<FitsDataType> types_;  /**< Bintable parameters: types of columns (D: double, E: float, I: integers,  A: char*) */ | 
|---|
| [1218] | 250 | DVList dvl_;          /**< DVList for transferring keywords */ | 
|---|
|  | 251 | vector<int>  taille_des_chaines_; /**< Bintable parameters:   length of the char* variables */ | 
|---|
| [839] | 252 |  | 
|---|
| [1136] | 253 | }; | 
|---|
| [1047] | 254 |  | 
|---|
| [1218] | 255 | //! Class for loading  SOPHYA objects from FITS Format Files (uses cfitsio lib) | 
|---|
| [875] | 256 |  | 
|---|
| [1136] | 257 | class FitsOutFile : public  FitsFile { | 
|---|
| [875] | 258 |  | 
|---|
| [1136] | 259 | public: | 
|---|
| [1193] | 260 |  | 
|---|
| [1136] | 261 | FitsOutFile(); | 
|---|
| [1231] | 262 | FitsOutFile(string const & flnm, WriteMode wrm = unknown ); | 
|---|
|  | 263 | FitsOutFile(const char * flnm, WriteMode wrm = unknown ); | 
|---|
| [1246] | 264 | ~FitsOutFile() { if (dvlToPrimary_ != NULL) delete dvlToPrimary_;}; | 
|---|
|  | 265 | inline void InitNull() {dvlToPrimary_ = NULL;} | 
|---|
| [1136] | 266 |  | 
|---|
|  | 267 | ////////////////////////////////////////////////////////// | 
|---|
|  | 268 | ///////   methods for managing extensions //////////////// | 
|---|
|  | 269 | ////////////////////////////////////////////////////////// | 
|---|
|  | 270 |  | 
|---|
|  | 271 |  | 
|---|
|  | 272 |  | 
|---|
|  | 273 | ///////////////////////////////////////////////////////////// | 
|---|
|  | 274 | //       methods for managing FITS IMAGE extension | 
|---|
|  | 275 | /////////////////////////////////////////////////// | 
|---|
|  | 276 |  | 
|---|
|  | 277 |  | 
|---|
| [1221] | 278 | void makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList* dvl) ; | 
|---|
| [1352] | 279 | void PutImageToFits( int nbData, r_8* map) const; | 
|---|
|  | 280 | void PutImageToFits(int nbData, r_4* map ) const; | 
|---|
|  | 281 | void PutImageToFits(int nbData, int_4* map) const; | 
|---|
| [1136] | 282 |  | 
|---|
|  | 283 |  | 
|---|
| [1143] | 284 |  | 
|---|
| [1136] | 285 | ////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 286 | //       methods for managing FITS BINARY TABLE or ASCII TABLE extension | 
|---|
|  | 287 | //////////////////////////////////////////////////////////////////////// | 
|---|
|  | 288 |  | 
|---|
|  | 289 |  | 
|---|
|  | 290 |  | 
|---|
| [1221] | 291 | void makeHeaderBntblOnFits ( string fieldType, vector<string> Noms, int nentries, int tfields, DVList* dvl, string extname,  vector<int> taille_des_chaines) ; | 
|---|
| [1353] | 292 | void appendInputHeader(FitsInFile& headerin, int hdunum); | 
|---|
|  | 293 | void writeAppendedHeaderOnFits(); | 
|---|
|  | 294 | void PrintHeaderToBeAppended(); | 
|---|
|  | 295 | void insertCommentLineOnHeader(string comment); | 
|---|
|  | 296 | void insertKeywordOnHeader(string keyname, double value, string comment); | 
|---|
| [1352] | 297 | void PutColToFits(int nocol, int nentries, r_8* donnees) const; | 
|---|
|  | 298 | void PutColToFits(int nocol, int nentries, r_4* donnees) const; | 
|---|
|  | 299 | void PutColToFits(int nocol, int nentries, int_4* donnees) const; | 
|---|
| [1218] | 300 | void PutColToFits(int nocol, int nentries, char** donnees) const; | 
|---|
|  | 301 | void PutBinTabLine(long NoLine,  BnTblLine& ligne) const; | 
|---|
| [1136] | 302 |  | 
|---|
|  | 303 |  | 
|---|
| [1143] | 304 | ///////////////////////////////////////////////////////////// | 
|---|
|  | 305 | //       methods for managing any type of FITS extension | 
|---|
|  | 306 | //////////////////////////////////////////////////////// | 
|---|
|  | 307 |  | 
|---|
|  | 308 |  | 
|---|
| [1246] | 309 | void  DVListIntoPrimaryHeader(DVList& dvl) ; | 
|---|
| [1143] | 310 |  | 
|---|
|  | 311 |  | 
|---|
|  | 312 |  | 
|---|
| [1136] | 313 | private : | 
|---|
|  | 314 |  | 
|---|
| [1231] | 315 | void openoutputfitsfile(const char * flnm, WriteMode wrm); | 
|---|
| [1246] | 316 | void writeSignatureOnFits(int hdunum) const; | 
|---|
|  | 317 | void addKeywordsOfDVList( DVList& dvl) const; | 
|---|
|  | 318 | void addDVListOnPrimary(); | 
|---|
| [1136] | 319 |  | 
|---|
| [1246] | 320 | DVList* dvlToPrimary_; /**< for transferring keywords when creating primary header */ | 
|---|
| [1353] | 321 | list<FitsKeyword>  mots_cles_; | 
|---|
|  | 322 |  | 
|---|
| [1136] | 323 | }; | 
|---|
|  | 324 |  | 
|---|
| [1218] | 325 | struct BnTblLine | 
|---|
|  | 326 | { | 
|---|
|  | 327 | BnTblLine() {} | 
|---|
| [1359] | 328 | void setFormat(int dc, int fc, int ic, int lc, int bc, int cc, vector<string> names); | 
|---|
| [1218] | 329 | bool sameFormat(const BnTblLine& btl) const; | 
|---|
| [1136] | 330 |  | 
|---|
| [1218] | 331 | void Print(); | 
|---|
| [1136] | 332 |  | 
|---|
| [1218] | 333 | vector<double> ddata_; | 
|---|
|  | 334 | vector<float>  fdata_; | 
|---|
| [1359] | 335 | vector<int>    idata_; | 
|---|
| [1218] | 336 | vector<string>  cdata_; | 
|---|
|  | 337 | vector<string> ColName_; | 
|---|
| [1359] | 338 | vector<long>   ldata_; | 
|---|
|  | 339 | vector<unsigned char>   bdata_; | 
|---|
|  | 340 |  | 
|---|
| [1218] | 341 | }; | 
|---|
|  | 342 |  | 
|---|
|  | 343 |  | 
|---|
| [1353] | 344 |  | 
|---|
|  | 345 |  | 
|---|
|  | 346 |  | 
|---|
| [875] | 347 | } // Fin du namespace | 
|---|
|  | 348 |  | 
|---|
| [839] | 349 | #endif | 
|---|