| 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 | namespace SOPHYA {
 | 
|---|
| 13 | 
 | 
|---|
| 14 | //
 | 
|---|
| 15 | //! Virtual Class for managing Interface for SOPHYA objects to FITS Format Files (uses cfitsio lib)
 | 
|---|
| 16 | 
 | 
|---|
| 17 | /*! 
 | 
|---|
| 18 | 
 | 
|---|
| 19 | Each SOPHYA object XXX is associated with a object of class FITS_XXX 
 | 
|---|
| 20 |  (inheriting from FitsFile), to which input/output operations with FITS 
 | 
|---|
| 21 |  files are delegated. A typical example of use is the following : 
 | 
|---|
| 22 | 
 | 
|---|
| 23 | \verbatim
 | 
|---|
| 24 |   int m=... ;
 | 
|---|
| 25 |   SphereHEALPix<r_8> sphere1(m);           // definition of the SOPHYA object
 | 
|---|
| 26 |   .... fill the sphere ....
 | 
|---|
| 27 | 
 | 
|---|
| 28 |   FITS_SphereHEALPix<r_8> fits_sph1(sphere1); 
 | 
|---|
| 29 |                                            // delegated object
 | 
|---|
| 30 |   fits_sph.Write("myfile.fits");           // writing on FITS file
 | 
|---|
| 31 | 
 | 
|---|
| 32 |    FITS_SphereHEALPix<r_8> fits_sph2("myfile.fits"); 
 | 
|---|
| 33 |                                            // load a delegated object 
 | 
|---|
| 34 |                                            // from FITS file
 | 
|---|
| 35 |    SphereHEALPix<r_8> sphere2=(SphereHEALPix<r_8>)fits_sph2; 
 | 
|---|
| 36 |                                            // casting the delegated object
 | 
|---|
| 37 |                                            // into a SOPHYA object
 | 
|---|
| 38 | \endverbatim
 | 
|---|
| 39 | 
 | 
|---|
| 40 | */
 | 
|---|
| 41 | class FitsFile
 | 
|---|
| 42 | {
 | 
|---|
| 43 | 
 | 
|---|
| 44 | public:
 | 
|---|
| 45 | 
 | 
|---|
| 46 |   FitsFile();
 | 
|---|
| 47 |   virtual ~FitsFile();
 | 
|---|
| 48 |   static int NbBlocks(char flnm[]);
 | 
|---|
| 49 |   static void getBlockType(char flnm[], int hdunum, string& typeOfExtension, int& naxis, vector<int>& naxisn, string& dataType, DVList& dvl  );
 | 
|---|
| 50 | 
 | 
|---|
| 51 |   protected: 
 | 
|---|
| 52 |  
 | 
|---|
| 53 | /*!
 | 
|---|
| 54 | this method is called from inherited objects : 
 | 
|---|
| 55 | 
 | 
|---|
| 56 | moves to header 'hdunum' on file 'flnm'
 | 
|---|
| 57 | 
 | 
|---|
| 58 | gets parameters in header 
 | 
|---|
| 59 | 
 | 
|---|
| 60 | calls the method 'ReadFromFits' from the inherited  object
 | 
|---|
| 61 | 
 | 
|---|
| 62 | */ 
 | 
|---|
| 63 |   void   ReadF(char flnm[],int hdunum= 0);
 | 
|---|
| 64 |   //  FitsFile* ReadFInit(char flnm[],int hdunum=0); 
 | 
|---|
| 65 |   void ReadFInit(char flnm[],int hdunum=0); 
 | 
|---|
| 66 |  
 | 
|---|
| 67 | /*!
 | 
|---|
| 68 | this method is called from inherited objects : 
 | 
|---|
| 69 | 
 | 
|---|
| 70 | opens a file 'flnm' 
 | 
|---|
| 71 | 
 | 
|---|
| 72 | gets parameters in header 
 | 
|---|
| 73 | 
 | 
|---|
| 74 | calls the method 'ReadFromFits' from the inherited  object
 | 
|---|
| 75 | 
 | 
|---|
| 76 | */ 
 | 
|---|
| 77 |   void    WriteF(char flnm[], bool OldFile=false);
 | 
|---|
| 78 |  //    virtual void    ReadFromFits(FitsFile& ff)=0;           
 | 
|---|
| 79 | 
 | 
|---|
| 80 | 
 | 
|---|
| 81 | 
 | 
|---|
| 82 |   virtual void    ReadFromFits()=0;           
 | 
|---|
| 83 |   virtual void    WriteToFits()=0;           
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   static string getErrStatus(int status);
 | 
|---|
| 86 | 
 | 
|---|
| 87 | 
 | 
|---|
| 88 |   //////////////////////////////////////////////////////////
 | 
|---|
| 89 |   ///////   methods for managing extensions ////////////////
 | 
|---|
| 90 |   //////////////////////////////////////////////////////////
 | 
|---|
| 91 | 
 | 
|---|
| 92 | //////////////////////////////////////////////////////////
 | 
|---|
| 93 |     //     methods with general purpose
 | 
|---|
| 94 |     ///////////////////////////////////////
 | 
|---|
| 95 | 
 | 
|---|
| 96 |   /*! return a reference on a DVList containing the keywords from FITS file
 | 
|---|
| 97 |    */
 | 
|---|
| 98 |   inline const DVList& DVListFromFits() const { return dvl_;}
 | 
|---|
| 99 | 
 | 
|---|
| 100 |   void moveToFollowingHeader();
 | 
|---|
| 101 |   int statusF() const; 
 | 
|---|
| 102 | 
 | 
|---|
| 103 | 
 | 
|---|
| 104 | /////////////////////////////////////////////////////////////
 | 
|---|
| 105 |   //       methods for managing FITS IMAGE extension
 | 
|---|
| 106 |   ///////////////////////////////////////////////////
 | 
|---|
| 107 | 
 | 
|---|
| 108 | 
 | 
|---|
| 109 |   //read
 | 
|---|
| 110 |   //----
 | 
|---|
| 111 | 
 | 
|---|
| 112 | /*! return true if the current header  corresponds to a FITS image extension */
 | 
|---|
| 113 | inline bool IsFitsImage() const { return (hdutype_ == IMAGE_HDU);}
 | 
|---|
| 114 | 
 | 
|---|
| 115 |   /*! number of dimensions of an image extension : NAXIS parameter (in FITS notations)
 | 
|---|
| 116 |    */
 | 
|---|
| 117 | inline int nbDimOfImage() const {return naxis_;}
 | 
|---|
| 118 | /*! a reference on a vector containing sizes of the NAXIS dimensions : NAXIS1, NAXIS2, NAXIS3 wtc.
 | 
|---|
| 119 |  */
 | 
|---|
| 120 |  inline const vector<int>& dimOfImageAxes() const { return naxisn_;}
 | 
|---|
| 121 | /*!
 | 
|---|
| 122 |  total number of data in the current IMAGE extension
 | 
|---|
| 123 |  */
 | 
|---|
| 124 | inline int nbOfImageData() const { return nbData_; }
 | 
|---|
| 125 | 
 | 
|---|
| 126 | 
 | 
|---|
| 127 | //write
 | 
|---|
| 128 | //-----
 | 
|---|
| 129 | 
 | 
|---|
| 130 | 
 | 
|---|
| 131 |   /*! create an IMAGE header on FITS file.
 | 
|---|
| 132 | \param <type> type of data (see method ColTypeFromFits)
 | 
|---|
| 133 | \param <nbdim>  number of dimensions : 1D, 2D, 3D etc. = NAXIS
 | 
|---|
| 134 | \param <naxisn>  array containind sizes of the different dimensions
 | 
|---|
| 135 |   */
 | 
|---|
| 136 |   void makeHeaderImageOnFits(char type, int nbdim, int* naxisn) ;
 | 
|---|
| 137 | 
 | 
|---|
| 138 |   /*! write double data from array 'map'on an IMAGE extension 
 | 
|---|
| 139 | \param <nbData>  number of data to be written
 | 
|---|
| 140 | 
 | 
|---|
| 141 |    */
 | 
|---|
| 142 |   void putImageToFits( int nbData, double* map) const;
 | 
|---|
| 143 | 
 | 
|---|
| 144 |   /*! same as previous method with float data */
 | 
|---|
| 145 |   void putImageToFits(int nbData, float* map ) const;
 | 
|---|
| 146 | 
 | 
|---|
| 147 |   /*! same as previous method with int data */
 | 
|---|
| 148 |   void putImageToFits(int nbData, int* map) const;
 | 
|---|
| 149 | 
 | 
|---|
| 150 | 
 | 
|---|
| 151 | 
 | 
|---|
| 152 | //////////////////////////////////////////////////////////////////////////
 | 
|---|
| 153 |   //       methods for managing FITS BINARY TABLE or ASCII TABLE extension
 | 
|---|
| 154 |   ////////////////////////////////////////////////////////////////////////
 | 
|---|
| 155 | 
 | 
|---|
| 156 | 
 | 
|---|
| 157 | 
 | 
|---|
| 158 | // read 
 | 
|---|
| 159 | //-----
 | 
|---|
| 160 | 
 | 
|---|
| 161 | /*! return true if the current header  corresponds to a FITS ASCII or BINTABLE extension */
 | 
|---|
| 162 | inline bool IsFitsTable() const {return (hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL);}
 | 
|---|
| 163 |  /*! return number of columns (return 1 if IMAGE) */
 | 
|---|
| 164 |   int     NbColsFromFits() const;
 | 
|---|
| 165 |   /*! number of data in the current IMAGE extension on FITS file, or number
 | 
|---|
| 166 |  of data of column number 'nocol' of the current BINTABLE extension
 | 
|---|
| 167 |   */
 | 
|---|
| 168 |   int     NentriesFromFits(int nocol) const;
 | 
|---|
| 169 |   /*! return a character denoting data type of column number 'nocol' in a BINTABLE :
 | 
|---|
| 170 | 
 | 
|---|
| 171 | D : double
 | 
|---|
| 172 | 
 | 
|---|
| 173 | E : float 
 | 
|---|
| 174 | 
 | 
|---|
| 175 | I : integer
 | 
|---|
| 176 |     
 | 
|---|
| 177 | S : character string 
 | 
|---|
| 178 | 
 | 
|---|
| 179 |   */
 | 
|---|
| 180 |   char    ColTypeFromFits(int nocol) const;
 | 
|---|
| 181 |   /*! name of the column number 'nocol' of the current BINTABLE extension
 | 
|---|
| 182 |    */
 | 
|---|
| 183 |   string  ColNameFromFits(int nocol) const;
 | 
|---|
| 184 | 
 | 
|---|
| 185 |   /*! number of characters of each data  for the column number 'nocol' (if char* typed) of the current BINTABLE extension
 | 
|---|
| 186 |    */
 | 
|---|
| 187 |   int     ColStringLengthFromFits(int nocol) const;
 | 
|---|
| 188 | 
 | 
|---|
| 189 | 
 | 
|---|
| 190 | 
 | 
|---|
| 191 |   /*!
 | 
|---|
| 192 | get the NoLine-th 'line'  from the current BINTABLE extension on FITS file,
 | 
|---|
| 193 |   */
 | 
|---|
| 194 |   void GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char
 | 
|---|
| 195 | ** cdata) ;
 | 
|---|
| 196 | 
 | 
|---|
| 197 |   /*!
 | 
|---|
| 198 | get the NoLine-th 'line'  from the current BINTABLE extension on FITS file,
 | 
|---|
| 199 |   */
 | 
|---|
| 200 |   void GetBinTabLine(int NoLine, float* fdata) ;
 | 
|---|
| 201 | 
 | 
|---|
| 202 | 
 | 
|---|
| 203 | 
 | 
|---|
| 204 | 
 | 
|---|
| 205 | 
 | 
|---|
| 206 |   /*!
 | 
|---|
| 207 | fill the array 'valeurs' with double data from the current BINTABLE extension on FITS file, from column number 'NoCol'
 | 
|---|
| 208 | 
 | 
|---|
| 209 | \param <nentries>  number of data to be read
 | 
|---|
| 210 |   */
 | 
|---|
| 211 |   void GetBinTabFCol(double* valeurs, int nentries, int NoCol) const;
 | 
|---|
| 212 | 
 | 
|---|
| 213 |   /*! same as previous method with float data */
 | 
|---|
| 214 |   void GetBinTabFCol(float* valeurs, int nentries, int NoCol) const;
 | 
|---|
| 215 |   /*! same as previous method with int data */
 | 
|---|
| 216 |   void GetBinTabFCol(int* valeurs, int nentries,  int NoCol) const;
 | 
|---|
| 217 |   /*! same as previous method with char* data */
 | 
|---|
| 218 |   void GetBinTabFCol(char** valeurs,int nentries, int NoCol) const;
 | 
|---|
| 219 |   // Write elements into the FITS data array
 | 
|---|
| 220 | 
 | 
|---|
| 221 | 
 | 
|---|
| 222 | 
 | 
|---|
| 223 |   //write
 | 
|---|
| 224 |   //-----
 | 
|---|
| 225 | 
 | 
|---|
| 226 |   /*! create an BINTABLE header on FITS file.
 | 
|---|
| 227 | \param <fieldType> array containing characters denoting types of the different column (see method ColTypeFromFits)
 | 
|---|
| 228 | \param <Noms>  array of the names of columns
 | 
|---|
| 229 | \param <nentries>  number of data of each column
 | 
|---|
| 230 | \param <tfields> number of columns
 | 
|---|
| 231 | \param <dvl> a SOPHYA DVList containing keywords to be appended
 | 
|---|
| 232 | \param <extname> keyword EXTNAME for FITS file
 | 
|---|
| 233 | \param <taille_des_chaines> vector containing the number of characters of  data  for each char* typed column, with order of appearance in 'fieldType'
 | 
|---|
| 234 |    */
 | 
|---|
| 235 |   void makeHeaderBntblOnFits ( char* fieldType, char** Noms, int nentries, int tfields, DVList &dvl, char* extname,  vector<int> taille_des_chaines) ;
 | 
|---|
| 236 | 
 | 
|---|
| 237 |   /*! write double data from array 'donnees ' on column number 'nocol' of a BINTABLE  extension.
 | 
|---|
| 238 | \param <nentries>  number of data to be written
 | 
|---|
| 239 | 
 | 
|---|
| 240 |    */
 | 
|---|
| 241 |   void putColToFits(int nocol, int nentries, double* donnees) const;
 | 
|---|
| 242 | 
 | 
|---|
| 243 |   /*! same as previous method with float data */
 | 
|---|
| 244 |   void putColToFits(int nocol, int nentries, float* donnees) const;
 | 
|---|
| 245 | 
 | 
|---|
| 246 |   /*! same as previous method with int data */
 | 
|---|
| 247 |   void putColToFits(int nocol, int nentries, int* donnees) const;
 | 
|---|
| 248 | 
 | 
|---|
| 249 |   /*! same as previous method with char* data */
 | 
|---|
| 250 |   void putColToFits(int nocol, int nentries, char** donnees) const;
 | 
|---|
| 251 | 
 | 
|---|
| 252 | 
 | 
|---|
| 253 | /////////////////////////////////////////////////////////////
 | 
|---|
| 254 |   //       methods for managing any type of FITS extension
 | 
|---|
| 255 |   ////////////////////////////////////////////////////////
 | 
|---|
| 256 | 
 | 
|---|
| 257 | 
 | 
|---|
| 258 |   /*!
 | 
|---|
| 259 | fill the array 'map' with double data from the current extension on FITS file.
 | 
|---|
| 260 | If the extension is BINTABLE, the first column is provided. 
 | 
|---|
| 261 | 
 | 
|---|
| 262 | \param <nentries>  number of data to be read
 | 
|---|
| 263 |   */
 | 
|---|
| 264 |   void    GetSingleColumn(double* map, int nentries) const;
 | 
|---|
| 265 | 
 | 
|---|
| 266 |   /*! same as above with float data */
 | 
|---|
| 267 |   void    GetSingleColumn(float*  map, int nentries) const;
 | 
|---|
| 268 | 
 | 
|---|
| 269 |   /*! same as above with int data */
 | 
|---|
| 270 |   void    GetSingleColumn(int* map, int nentries) const;
 | 
|---|
| 271 | 
 | 
|---|
| 272 | 
 | 
|---|
| 273 | 
 | 
|---|
| 274 | private:
 | 
|---|
| 275 | 
 | 
|---|
| 276 |    void InitNull();
 | 
|---|
| 277 |    void ResetStatus(int& status) ; 
 | 
|---|
| 278 |    static  void printerror(int&) ;
 | 
|---|
| 279 |    static  void printerror(int&,char* texte) ;
 | 
|---|
| 280 | 
 | 
|---|
| 281 | static  void GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn);
 | 
|---|
| 282 | static  void GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
 | 
|---|
| 283 |                                   vector<int>& repeat,
 | 
|---|
| 284 |                                   vector<string>& noms, 
 | 
|---|
| 285 |                                   vector<char>& types,   
 | 
|---|
| 286 |                                   vector<int>&  taille_des_chaines);
 | 
|---|
| 287 | static void KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum); 
 | 
|---|
| 288 |   void writeSignatureOnFits() const; 
 | 
|---|
| 289 | 
 | 
|---|
| 290 |   //! pointer to the FITS file, defined in fitsio.h 
 | 
|---|
| 291 |   fitsfile *fptr_; 
 | 
|---|
| 292 |   
 | 
|---|
| 293 |   //!  image or bintable ?
 | 
|---|
| 294 |   int hdutype_;
 | 
|---|
| 295 | 
 | 
|---|
| 296 | //! index of header to be read/written
 | 
|---|
| 297 |   int hdunum_;  
 | 
|---|
| 298 | 
 | 
|---|
| 299 | 
 | 
|---|
| 300 |   //! fits-Image parameter 
 | 
|---|
| 301 |   int bitpix_;
 | 
|---|
| 302 | 
 | 
|---|
| 303 |   //! fits-Image parameter 
 | 
|---|
| 304 |   int naxis_;
 | 
|---|
| 305 | 
 | 
|---|
| 306 |   //! fits-Image parameters : sizes of dimensions
 | 
|---|
| 307 |   vector<int> naxisn_;
 | 
|---|
| 308 | 
 | 
|---|
| 309 |   //! fits-Image parameter: number of data 
 | 
|---|
| 310 |   int nbData_;
 | 
|---|
| 311 | 
 | 
|---|
| 312 |   //! Bintable parameter
 | 
|---|
| 313 |   int nrows_;
 | 
|---|
| 314 | 
 | 
|---|
| 315 |   //! Bintable parameter
 | 
|---|
| 316 |   vector<int> repeat_;
 | 
|---|
| 317 | 
 | 
|---|
| 318 |   //! Bintable parameter
 | 
|---|
| 319 |   int nbcols_;
 | 
|---|
| 320 | 
 | 
|---|
| 321 |   //! Bintable parameter: column names
 | 
|---|
| 322 |   vector<string> noms_; 
 | 
|---|
| 323 |  
 | 
|---|
| 324 |   //! Bintable parameters: types of columns (D: double, E: float, I: integers,  A: char*)
 | 
|---|
| 325 |   vector<char> types_;   
 | 
|---|
| 326 |     
 | 
|---|
| 327 |   //! Bintable parameters:   length of the char* variables                 
 | 
|---|
| 328 |   vector<int>  taille_des_chaines_;
 | 
|---|
| 329 | 
 | 
|---|
| 330 |   //! DVList for transferring keywords
 | 
|---|
| 331 |   DVList dvl_;
 | 
|---|
| 332 | 
 | 
|---|
| 333 | 
 | 
|---|
| 334 | 
 | 
|---|
| 335 |   //! last status returned by fitsio library. updated only by several methods
 | 
|---|
| 336 |   int fits_status_;
 | 
|---|
| 337 | };
 | 
|---|
| 338 | 
 | 
|---|
| 339 | 
 | 
|---|
| 340 | } // Fin du namespace
 | 
|---|
| 341 | 
 | 
|---|
| 342 | 
 | 
|---|
| 343 | #endif
 | 
|---|