[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 {
|
---|
| 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 |
|
---|
[1218] | 69 | FitsFile() { InitNull(); };
|
---|
| 70 | virtual ~FitsFile();
|
---|
| 71 | static string GetErrStatus(int status);
|
---|
| 72 | inline int statusF() const { return fits_status_;}
|
---|
[1234] | 73 | inline void firstImageOnPrimaryHeader(bool choice) {imageOnPrimary_=choice;}
|
---|
[1136] | 74 |
|
---|
| 75 |
|
---|
[1218] | 76 | protected:
|
---|
[1136] | 77 |
|
---|
[1218] | 78 | void ResetStatus(int& status) ;
|
---|
[1136] | 79 | static void printerror(int&) ;
|
---|
| 80 | static void printerror(int&,char* texte) ;
|
---|
[1234] | 81 | inline void InitNull() {fptr_ = NULL; hdutype_= 0; hdunum_ = 0;
|
---|
| 82 | fits_status_ = 0; imageOnPrimary_ = true;}
|
---|
[1136] | 83 |
|
---|
[1218] | 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 */
|
---|
[1234] | 88 | bool imageOnPrimary_;
|
---|
[1136] | 89 |
|
---|
[1218] | 90 | };
|
---|
[1136] | 91 |
|
---|
[1218] | 92 | //! Class for saving SOPHYA objects on FITS Format Files (uses cfitsio lib)
|
---|
[1136] | 93 |
|
---|
| 94 | class FitsInFile : public FitsFile {
|
---|
| 95 |
|
---|
| 96 | public:
|
---|
| 97 | FitsInFile();
|
---|
[1231] | 98 | FitsInFile(string const & flnm);
|
---|
| 99 | FitsInFile(const char * flnm);
|
---|
[1143] | 100 | ~FitsInFile() { ; };
|
---|
[1136] | 101 |
|
---|
[1218] | 102 | static int NbBlocks(char flnm[]);
|
---|
[1231] | 103 | static void GetBlockType(char flnm[], int hdunum, FitsExtensionType& typeOfExtension, int& naxis, vector<int>& naxisn, FitsDataType& dataType, DVList& dvl );
|
---|
[1218] | 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_;}
|
---|
[1136] | 108 |
|
---|
[1218] | 109 | DVList DVListFromPrimaryHeader() const;
|
---|
| 110 | void moveToFollowingHeader();
|
---|
[1047] | 111 |
|
---|
[1136] | 112 |
|
---|
| 113 |
|
---|
| 114 |
|
---|
| 115 | //////////////////////////////////////////////////////////
|
---|
| 116 | /////// methods for managing extensions ////////////////
|
---|
| 117 | //////////////////////////////////////////////////////////
|
---|
| 118 |
|
---|
| 119 |
|
---|
[1047] | 120 | /////////////////////////////////////////////////////////////
|
---|
[1136] | 121 | // methods for managing FITS IMAGE extension
|
---|
| 122 | ///////////////////////////////////////////////////
|
---|
[1047] | 123 |
|
---|
| 124 |
|
---|
[1218] | 125 |
|
---|
| 126 | /*! \return true if the current header corresponds to a FITS image extension */
|
---|
[1047] | 127 | inline bool IsFitsImage() const { return (hdutype_ == IMAGE_HDU);}
|
---|
| 128 |
|
---|
[1136] | 129 |
|
---|
| 130 |
|
---|
[1218] | 131 | /*! \return number of dimensions of an image extension : NAXIS parameter (in FITS notations) */
|
---|
[861] | 132 | inline int nbDimOfImage() const {return naxis_;}
|
---|
[1218] | 133 |
|
---|
| 134 | /*! \return a reference on a vector containing sizes of the NAXIS dimensions : NAXIS1, NAXIS2, NAXIS3 etc. */
|
---|
[1047] | 135 | inline const vector<int>& dimOfImageAxes() const { return naxisn_;}
|
---|
[1218] | 136 |
|
---|
| 137 |
|
---|
| 138 | /*! \return total number of data in the current IMAGE extension */
|
---|
[839] | 139 | inline int nbOfImageData() const { return nbData_; }
|
---|
[903] | 140 |
|
---|
| 141 |
|
---|
[1047] | 142 |
|
---|
[1136] | 143 | //////////////////////////////////////////////////////////////////////////
|
---|
| 144 | // methods for managing FITS BINARY TABLE or ASCII TABLE extension
|
---|
| 145 | ////////////////////////////////////////////////////////////////////////
|
---|
[1047] | 146 |
|
---|
| 147 |
|
---|
| 148 |
|
---|
| 149 |
|
---|
[1218] | 150 | /*! \return true if the current header corresponds to a FITS ASCII or BINTABLE extension */
|
---|
[1136] | 151 | inline bool IsFitsTable() const {return (hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL);}
|
---|
[1047] | 152 |
|
---|
| 153 |
|
---|
[1218] | 154 | static void GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
|
---|
[1136] | 155 | vector<int>& repeat,
|
---|
| 156 | vector<string>& noms,
|
---|
| 157 | vector<char>& types,
|
---|
| 158 | vector<int>& taille_des_chaines);
|
---|
[1218] | 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
|
---|
[1047] | 163 | ** cdata) ;
|
---|
[1218] | 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;
|
---|
[1047] | 170 |
|
---|
[1136] | 171 | /////////////////////////////////////////////////////////////
|
---|
| 172 | // methods for managing any type of FITS extension
|
---|
| 173 | ////////////////////////////////////////////////////////
|
---|
[1045] | 174 |
|
---|
[1136] | 175 | int NbColsFromFits() const;
|
---|
| 176 | int NentriesFromFits(int nocol) const;
|
---|
[1045] | 177 |
|
---|
| 178 |
|
---|
[1047] | 179 | void GetSingleColumn(double* map, int nentries) const;
|
---|
[903] | 180 |
|
---|
[1047] | 181 | void GetSingleColumn(float* map, int nentries) const;
|
---|
[839] | 182 |
|
---|
[1047] | 183 | void GetSingleColumn(int* map, int nentries) const;
|
---|
| 184 |
|
---|
[1136] | 185 | private :
|
---|
[1047] | 186 |
|
---|
[1136] | 187 | void InitNull();
|
---|
[1234] | 188 | void getHeader();
|
---|
[1136] | 189 | static void KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum);
|
---|
[903] | 190 | static void GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn);
|
---|
[839] | 191 |
|
---|
[1218] | 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 */
|
---|
[839] | 203 |
|
---|
[1136] | 204 | };
|
---|
[1047] | 205 |
|
---|
[1218] | 206 | //! Class for loading SOPHYA objects from FITS Format Files (uses cfitsio lib)
|
---|
[875] | 207 |
|
---|
[1136] | 208 | class FitsOutFile : public FitsFile {
|
---|
[875] | 209 |
|
---|
[1136] | 210 | public:
|
---|
[1193] | 211 |
|
---|
[1136] | 212 | FitsOutFile();
|
---|
[1231] | 213 | FitsOutFile(string const & flnm, WriteMode wrm = unknown );
|
---|
| 214 | FitsOutFile(const char * flnm, WriteMode wrm = unknown );
|
---|
[1246] | 215 | ~FitsOutFile() { if (dvlToPrimary_ != NULL) delete dvlToPrimary_;};
|
---|
| 216 | inline void InitNull() {dvlToPrimary_ = NULL;}
|
---|
[1136] | 217 |
|
---|
| 218 | //////////////////////////////////////////////////////////
|
---|
| 219 | /////// methods for managing extensions ////////////////
|
---|
| 220 | //////////////////////////////////////////////////////////
|
---|
| 221 |
|
---|
| 222 |
|
---|
| 223 |
|
---|
| 224 | /////////////////////////////////////////////////////////////
|
---|
| 225 | // methods for managing FITS IMAGE extension
|
---|
| 226 | ///////////////////////////////////////////////////
|
---|
| 227 |
|
---|
| 228 |
|
---|
[1221] | 229 | void makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList* dvl) ;
|
---|
[1218] | 230 | void PutImageToFits( int nbData, double* map) const;
|
---|
| 231 | void PutImageToFits(int nbData, float* map ) const;
|
---|
| 232 | void PutImageToFits(int nbData, int* map) const;
|
---|
[1136] | 233 |
|
---|
| 234 |
|
---|
[1143] | 235 |
|
---|
[1136] | 236 | //////////////////////////////////////////////////////////////////////////
|
---|
| 237 | // methods for managing FITS BINARY TABLE or ASCII TABLE extension
|
---|
| 238 | ////////////////////////////////////////////////////////////////////////
|
---|
| 239 |
|
---|
| 240 |
|
---|
| 241 |
|
---|
[1221] | 242 | void makeHeaderBntblOnFits ( string fieldType, vector<string> Noms, int nentries, int tfields, DVList* dvl, string extname, vector<int> taille_des_chaines) ;
|
---|
[1218] | 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;
|
---|
[1136] | 248 |
|
---|
| 249 |
|
---|
[1143] | 250 | /////////////////////////////////////////////////////////////
|
---|
| 251 | // methods for managing any type of FITS extension
|
---|
| 252 | ////////////////////////////////////////////////////////
|
---|
| 253 |
|
---|
| 254 |
|
---|
[1246] | 255 | void DVListIntoPrimaryHeader(DVList& dvl) ;
|
---|
[1143] | 256 |
|
---|
| 257 |
|
---|
| 258 |
|
---|
[1136] | 259 | private :
|
---|
| 260 |
|
---|
[1231] | 261 | void openoutputfitsfile(const char * flnm, WriteMode wrm);
|
---|
[1246] | 262 | void writeSignatureOnFits(int hdunum) const;
|
---|
| 263 | void addKeywordsOfDVList( DVList& dvl) const;
|
---|
| 264 | void addDVListOnPrimary();
|
---|
[1136] | 265 |
|
---|
[1246] | 266 | DVList* dvlToPrimary_; /**< for transferring keywords when creating primary header */
|
---|
[1136] | 267 | };
|
---|
| 268 |
|
---|
[1218] | 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;
|
---|
[1136] | 274 |
|
---|
[1218] | 275 | void Print();
|
---|
[1136] | 276 |
|
---|
[1218] | 277 | vector<double> ddata_;
|
---|
| 278 | vector<float> fdata_;
|
---|
| 279 | vector<int> idata_;
|
---|
| 280 | vector<string> cdata_;
|
---|
| 281 | vector<string> ColName_;
|
---|
| 282 |
|
---|
| 283 | };
|
---|
| 284 |
|
---|
| 285 |
|
---|
[875] | 286 | } // Fin du namespace
|
---|
| 287 |
|
---|
| 288 |
|
---|
[839] | 289 | #endif
|
---|