| 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 |  | 
|---|
| 12 | // classes for saving/loading SOPHYA objects to/from FITS files... | 
|---|
| 13 | // Guy le Meur (september 2000) | 
|---|
| 14 |  | 
|---|
| 15 |  | 
|---|
| 16 | namespace SOPHYA { | 
|---|
| 17 |  | 
|---|
| 18 | struct BnTblLine; | 
|---|
| 19 | class FitsFile; | 
|---|
| 20 | class FitsInFile; | 
|---|
| 21 | class FitsOutFile; | 
|---|
| 22 | enum WriteMode {append, clear, unknown}; | 
|---|
| 23 |  | 
|---|
| 24 |  | 
|---|
| 25 | // | 
|---|
| 26 | //! Class for managing Interface for SOPHYA objects to FITS Format Files (uses cfitsio lib) | 
|---|
| 27 |  | 
|---|
| 28 | class FitsIOHandler { | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 | public: | 
|---|
| 32 |  | 
|---|
| 33 | virtual ~FitsIOHandler() {} | 
|---|
| 34 | void   Read(char flnm[],int hdunum= 0); | 
|---|
| 35 | void   Write(char flnm[]) ; | 
|---|
| 36 | void   Read(FitsInFile& ifts, int hdunum=0); | 
|---|
| 37 | void   Write(FitsOutFile& ofts) ; | 
|---|
| 38 |  | 
|---|
| 39 |  | 
|---|
| 40 | protected: | 
|---|
| 41 |  | 
|---|
| 42 | virtual void    ReadFromFits(FitsInFile& is)=0; | 
|---|
| 43 | virtual void    WriteToFits(FitsOutFile& os) =0; | 
|---|
| 44 |  | 
|---|
| 45 | friend class FitsInFile; | 
|---|
| 46 | friend class FitsOutFile; | 
|---|
| 47 | }; | 
|---|
| 48 |  | 
|---|
| 49 |  | 
|---|
| 50 | //! Class (virtual) for managing FITS format files | 
|---|
| 51 | class FitsFile { | 
|---|
| 52 |  | 
|---|
| 53 | public: | 
|---|
| 54 |  | 
|---|
| 55 | FitsFile() { InitNull(); }; | 
|---|
| 56 | virtual ~FitsFile(); | 
|---|
| 57 | static string GetErrStatus(int status); | 
|---|
| 58 | inline  int   statusF() const { return fits_status_;} | 
|---|
| 59 |  | 
|---|
| 60 |  | 
|---|
| 61 | protected: | 
|---|
| 62 |  | 
|---|
| 63 | void         ResetStatus(int& status) ; | 
|---|
| 64 | static  void printerror(int&) ; | 
|---|
| 65 | static  void printerror(int&,char* texte) ; | 
|---|
| 66 | inline void  InitNull() {fptr_ = NULL; hdutype_= 0; hdunum_ = 1; | 
|---|
| 67 | fits_status_ = 0;} | 
|---|
| 68 |  | 
|---|
| 69 | fitsfile *fptr_;     /**<  pointer to the FITS file, defined in fitsio.h */ | 
|---|
| 70 | int hdutype_;        /**<  image or bintable ? */ | 
|---|
| 71 | int hdunum_;         /**<   index of header to be read/written */ | 
|---|
| 72 | int fits_status_;    /**< last status returned by fitsio library. updated only by several methods */ | 
|---|
| 73 |  | 
|---|
| 74 | }; | 
|---|
| 75 |  | 
|---|
| 76 | //! Class for saving  SOPHYA objects on FITS Format Files (uses cfitsio lib) | 
|---|
| 77 |  | 
|---|
| 78 | class FitsInFile : public  FitsFile { | 
|---|
| 79 |  | 
|---|
| 80 | public: | 
|---|
| 81 | FitsInFile(); | 
|---|
| 82 | FitsInFile(char flnm[]); | 
|---|
| 83 | ~FitsInFile() { ; }; | 
|---|
| 84 |  | 
|---|
| 85 | static int  NbBlocks(char flnm[]); | 
|---|
| 86 | static void GetBlockType(char flnm[], int hdunum, string& typeOfExtension, int& naxis, vector<int>& naxisn, string& dataType, DVList& dvl  ); | 
|---|
| 87 | void        ReadFInit(int hdunum); | 
|---|
| 88 |  | 
|---|
| 89 | /*! \return a reference on a DVList containing the keywords from FITS file */ | 
|---|
| 90 | inline const DVList& DVListFromFits() const { return dvl_;} | 
|---|
| 91 |  | 
|---|
| 92 | DVList  DVListFromPrimaryHeader() const; | 
|---|
| 93 | void    moveToFollowingHeader(); | 
|---|
| 94 |  | 
|---|
| 95 |  | 
|---|
| 96 |  | 
|---|
| 97 |  | 
|---|
| 98 | ////////////////////////////////////////////////////////// | 
|---|
| 99 | ///////   methods for managing extensions //////////////// | 
|---|
| 100 | ////////////////////////////////////////////////////////// | 
|---|
| 101 |  | 
|---|
| 102 |  | 
|---|
| 103 | ///////////////////////////////////////////////////////////// | 
|---|
| 104 | //       methods for managing FITS IMAGE extension | 
|---|
| 105 | /////////////////////////////////////////////////// | 
|---|
| 106 |  | 
|---|
| 107 |  | 
|---|
| 108 |  | 
|---|
| 109 | /*! \return true if the current header  corresponds to a FITS image extension */ | 
|---|
| 110 | inline bool IsFitsImage() const { return (hdutype_ == IMAGE_HDU);} | 
|---|
| 111 |  | 
|---|
| 112 |  | 
|---|
| 113 |  | 
|---|
| 114 | /*! \return number of dimensions of an image extension : NAXIS parameter (in FITS notations)   */ | 
|---|
| 115 | inline int nbDimOfImage() const {return naxis_;} | 
|---|
| 116 |  | 
|---|
| 117 | /*! \return a reference on a vector containing sizes of the NAXIS dimensions : NAXIS1, NAXIS2, NAXIS3 etc.  */ | 
|---|
| 118 | inline const vector<int>& dimOfImageAxes() const { return naxisn_;} | 
|---|
| 119 |  | 
|---|
| 120 |  | 
|---|
| 121 | /*! \return total number of data in the current IMAGE extension */ | 
|---|
| 122 | inline int nbOfImageData() const { return nbData_; } | 
|---|
| 123 |  | 
|---|
| 124 |  | 
|---|
| 125 |  | 
|---|
| 126 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| 127 | //       methods for managing FITS BINARY TABLE or ASCII TABLE extension | 
|---|
| 128 | //////////////////////////////////////////////////////////////////////// | 
|---|
| 129 |  | 
|---|
| 130 |  | 
|---|
| 131 |  | 
|---|
| 132 |  | 
|---|
| 133 | /*! \return true if the current header  corresponds to a FITS ASCII or BINTABLE extension */ | 
|---|
| 134 | inline bool IsFitsTable() const {return (hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL);} | 
|---|
| 135 |  | 
|---|
| 136 |  | 
|---|
| 137 | static  void GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows, | 
|---|
| 138 | vector<int>& repeat, | 
|---|
| 139 | vector<string>& noms, | 
|---|
| 140 | vector<char>& types, | 
|---|
| 141 | vector<int>&  taille_des_chaines); | 
|---|
| 142 | char   ColTypeFromFits(int nocol) const; | 
|---|
| 143 | string ColNameFromFits(int nocol) const; | 
|---|
| 144 | int    ColStringLengthFromFits(int nocol) const; | 
|---|
| 145 | void   GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char | 
|---|
| 146 | ** cdata) ; | 
|---|
| 147 | void   GetBinTabLine(long NoLine, BnTblLine& ligne) ; | 
|---|
| 148 | void   GetBinTabLine(int NoLine, float* fdata) ; | 
|---|
| 149 | void   GetBinTabFCol(double* valeurs, int nentries, int NoCol) const; | 
|---|
| 150 | void   GetBinTabFCol(float* valeurs, int nentries, int NoCol) const; | 
|---|
| 151 | void   GetBinTabFCol(int* valeurs, int nentries,  int NoCol) const; | 
|---|
| 152 | void   GetBinTabFCol(char** valeurs,int nentries, int NoCol) const; | 
|---|
| 153 |  | 
|---|
| 154 | ///////////////////////////////////////////////////////////// | 
|---|
| 155 | //       methods for managing any type of FITS extension | 
|---|
| 156 | //////////////////////////////////////////////////////// | 
|---|
| 157 |  | 
|---|
| 158 | int     NbColsFromFits() const; | 
|---|
| 159 | int     NentriesFromFits(int nocol) const; | 
|---|
| 160 |  | 
|---|
| 161 |  | 
|---|
| 162 | void    GetSingleColumn(double* map, int nentries) const; | 
|---|
| 163 |  | 
|---|
| 164 | void    GetSingleColumn(float*  map, int nentries) const; | 
|---|
| 165 |  | 
|---|
| 166 | void    GetSingleColumn(int* map, int nentries) const; | 
|---|
| 167 |  | 
|---|
| 168 | private : | 
|---|
| 169 |  | 
|---|
| 170 | void InitNull(); | 
|---|
| 171 | static void KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum); | 
|---|
| 172 | static  void GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn); | 
|---|
| 173 |  | 
|---|
| 174 | int bitpix_;          /**< fits-Image parameter */ | 
|---|
| 175 | int naxis_;           /**< fits-Image parameter */ | 
|---|
| 176 | vector<int> naxisn_;  /**< fits-Image parameters : sizes of dimensions */ | 
|---|
| 177 | int nbData_;          /*< fits-Image parameter: number of data */ | 
|---|
| 178 | int nrows_;           /**< Bintable parameter */ | 
|---|
| 179 | vector<int> repeat_;  /**< Bintable parameter */ | 
|---|
| 180 | int nbcols_;          /**< Bintable parameter */ | 
|---|
| 181 | vector<string> noms_; /**< Bintable parameter: column names */ | 
|---|
| 182 | vector<char> types_;  /**< Bintable parameters: types of columns (D: double, E: float, I: integers,  A: char*) */ | 
|---|
| 183 | DVList dvl_;          /**< DVList for transferring keywords */ | 
|---|
| 184 | vector<int>  taille_des_chaines_; /**< Bintable parameters:   length of the char* variables */ | 
|---|
| 185 |  | 
|---|
| 186 | }; | 
|---|
| 187 |  | 
|---|
| 188 | //! Class for loading  SOPHYA objects from FITS Format Files (uses cfitsio lib) | 
|---|
| 189 |  | 
|---|
| 190 | class FitsOutFile : public  FitsFile { | 
|---|
| 191 |  | 
|---|
| 192 | public: | 
|---|
| 193 |  | 
|---|
| 194 |  | 
|---|
| 195 | FitsOutFile(); | 
|---|
| 196 | FitsOutFile(char flnm[], WriteMode wrm = unknown ); | 
|---|
| 197 | ~FitsOutFile() { ;}; | 
|---|
| 198 | inline void InitNull() {imageOnPrimary_=false;} | 
|---|
| 199 |  | 
|---|
| 200 | ////////////////////////////////////////////////////////// | 
|---|
| 201 | ///////   methods for managing extensions //////////////// | 
|---|
| 202 | ////////////////////////////////////////////////////////// | 
|---|
| 203 |  | 
|---|
| 204 |  | 
|---|
| 205 |  | 
|---|
| 206 | ///////////////////////////////////////////////////////////// | 
|---|
| 207 | //       methods for managing FITS IMAGE extension | 
|---|
| 208 | /////////////////////////////////////////////////// | 
|---|
| 209 |  | 
|---|
| 210 |  | 
|---|
| 211 | inline void firstImageOnPrimaryHeader() {imageOnPrimary_=true;} | 
|---|
| 212 | void makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList* dvl) ; | 
|---|
| 213 | void PutImageToFits( int nbData, double* map) const; | 
|---|
| 214 | void PutImageToFits(int nbData, float* map ) const; | 
|---|
| 215 | void PutImageToFits(int nbData, int* map) const; | 
|---|
| 216 |  | 
|---|
| 217 |  | 
|---|
| 218 |  | 
|---|
| 219 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| 220 | //       methods for managing FITS BINARY TABLE or ASCII TABLE extension | 
|---|
| 221 | //////////////////////////////////////////////////////////////////////// | 
|---|
| 222 |  | 
|---|
| 223 |  | 
|---|
| 224 |  | 
|---|
| 225 | void makeHeaderBntblOnFits ( string fieldType, vector<string> Noms, int nentries, int tfields, DVList* dvl, string extname,  vector<int> taille_des_chaines) ; | 
|---|
| 226 | void PutColToFits(int nocol, int nentries, double* donnees) const; | 
|---|
| 227 | void PutColToFits(int nocol, int nentries, float* donnees) const; | 
|---|
| 228 | void PutColToFits(int nocol, int nentries, int* donnees) const; | 
|---|
| 229 | void PutColToFits(int nocol, int nentries, char** donnees) const; | 
|---|
| 230 | void PutBinTabLine(long NoLine,  BnTblLine& ligne) const; | 
|---|
| 231 |  | 
|---|
| 232 |  | 
|---|
| 233 | ///////////////////////////////////////////////////////////// | 
|---|
| 234 | //       methods for managing any type of FITS extension | 
|---|
| 235 | //////////////////////////////////////////////////////// | 
|---|
| 236 |  | 
|---|
| 237 |  | 
|---|
| 238 | void  DVListIntoPrimaryHeader(DVList& dvl) const; | 
|---|
| 239 |  | 
|---|
| 240 |  | 
|---|
| 241 |  | 
|---|
| 242 | private : | 
|---|
| 243 |  | 
|---|
| 244 | void writeSignatureOnFits() const; | 
|---|
| 245 | void addKeywordsOfDVList(DVList& dvl) const; | 
|---|
| 246 |  | 
|---|
| 247 | bool imageOnPrimary_; | 
|---|
| 248 |  | 
|---|
| 249 | }; | 
|---|
| 250 |  | 
|---|
| 251 | struct BnTblLine | 
|---|
| 252 | { | 
|---|
| 253 | BnTblLine() {} | 
|---|
| 254 | void setFormat(int dc, int fc, int ic, int cc, vector<string> names); | 
|---|
| 255 | bool sameFormat(const BnTblLine& btl) const; | 
|---|
| 256 |  | 
|---|
| 257 | void Print(); | 
|---|
| 258 |  | 
|---|
| 259 | vector<double> ddata_; | 
|---|
| 260 | vector<float>  fdata_; | 
|---|
| 261 | vector<int>    idata_; | 
|---|
| 262 | vector<string>  cdata_; | 
|---|
| 263 | vector<string> ColName_; | 
|---|
| 264 |  | 
|---|
| 265 | }; | 
|---|
| 266 |  | 
|---|
| 267 |  | 
|---|
| 268 | } // Fin du namespace | 
|---|
| 269 |  | 
|---|
| 270 |  | 
|---|
| 271 | #endif | 
|---|