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