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