[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 |
|
---|
[875] | 12 | namespace SOPHYA {
|
---|
| 13 |
|
---|
[1136] | 14 | class FitsFile;
|
---|
| 15 | class FitsInFile;
|
---|
| 16 | class FitsOutFile;
|
---|
| 17 |
|
---|
| 18 |
|
---|
| 19 |
|
---|
[903] | 20 | //
|
---|
[1136] | 21 | //! Class for managing Interface for SOPHYA objects to FITS Format Files (uses cfitsio lib)
|
---|
[875] | 22 |
|
---|
[903] | 23 | /*!
|
---|
[1136] | 24 | The class structure is analogous to Sophya-PPersist system :
|
---|
[903] | 25 | Each SOPHYA object XXX is associated with a object of class FITS_XXX
|
---|
[1136] | 26 | (inheriting from FitsFileHandler), to which input/output operations with FITS
|
---|
| 27 | files are delegated (through a class Hierarchy : FitsFile (virtual),
|
---|
| 28 | FitsInFile, FitsOutFile) . A typical example of use is the following :
|
---|
[903] | 29 |
|
---|
| 30 | \verbatim
|
---|
| 31 | int m=... ;
|
---|
| 32 | SphereHEALPix<r_8> sphere1(m); // definition of the SOPHYA object
|
---|
| 33 | .... fill the sphere ....
|
---|
| 34 |
|
---|
| 35 | FITS_SphereHEALPix<r_8> fits_sph1(sphere1);
|
---|
| 36 | // delegated object
|
---|
| 37 | fits_sph.Write("myfile.fits"); // writing on FITS file
|
---|
| 38 |
|
---|
| 39 | FITS_SphereHEALPix<r_8> fits_sph2("myfile.fits");
|
---|
| 40 | // load a delegated object
|
---|
| 41 | // from FITS file
|
---|
| 42 | SphereHEALPix<r_8> sphere2=(SphereHEALPix<r_8>)fits_sph2;
|
---|
| 43 | // casting the delegated object
|
---|
| 44 | // into a SOPHYA object
|
---|
| 45 | \endverbatim
|
---|
| 46 |
|
---|
| 47 | */
|
---|
[839] | 48 |
|
---|
[1136] | 49 | class FitsIOHandler {
|
---|
[839] | 50 |
|
---|
| 51 |
|
---|
[1136] | 52 | public:
|
---|
| 53 |
|
---|
| 54 | virtual ~FitsIOHandler() {}
|
---|
[903] | 55 | /*!
|
---|
| 56 | this method is called from inherited objects :
|
---|
| 57 |
|
---|
[1136] | 58 | opens a file 'flnm'
|
---|
[903] | 59 |
|
---|
[1143] | 60 | gets parameters in extension-header (hdunum)
|
---|
[903] | 61 |
|
---|
[1047] | 62 | calls the method 'ReadFromFits' from the inherited object
|
---|
[903] | 63 |
|
---|
[1136] | 64 |
|
---|
[903] | 65 | */
|
---|
[1175] | 66 | void Read(char flnm[],int hdunum= 0);
|
---|
[903] | 67 | /*!
|
---|
| 68 | this method is called from inherited objects :
|
---|
| 69 |
|
---|
[1136] | 70 | for writing a new object in a new fits-extension, at the end of
|
---|
[903] | 71 |
|
---|
[1136] | 72 | the existing file (flnm), if OldFile=true.
|
---|
[903] | 73 |
|
---|
[1136] | 74 | If OldFile=false, an exception occurs
|
---|
| 75 |
|
---|
| 76 | By convention, primary header does not contain fits-image data : i.e.
|
---|
| 77 | all data are fits-extensions. The first relevant header will have hdunum=2.
|
---|
| 78 | For switching off this convention use the method :
|
---|
[903] | 79 |
|
---|
[1136] | 80 | firstImageOnPrimaryHeader() (see below)
|
---|
| 81 |
|
---|
| 82 | In that case do not forget to precise hdunum=1 when reading data on primary header.
|
---|
| 83 |
|
---|
| 84 | calls the method 'WriteToFits' from the inherited object
|
---|
| 85 |
|
---|
[903] | 86 | */
|
---|
[1136] | 87 | void Write(char flnm[], bool OldFile=false) ;
|
---|
[903] | 88 |
|
---|
[1136] | 89 | /*!
|
---|
| 90 | Read the data on extension hdunum (or primary header, if hdunum=1) from FitsInFIle. With default value for hdunum, one reads the next extension, with respect to the current position.
|
---|
| 91 | */
|
---|
| 92 | void Read(FitsInFile& ifts, int hdunum=0);
|
---|
| 93 | void Write(FitsOutFile& ofts) ;
|
---|
[903] | 94 |
|
---|
| 95 |
|
---|
[1136] | 96 | protected:
|
---|
| 97 | virtual void ReadFromFits(FitsInFile& is)=0;
|
---|
| 98 | virtual void WriteToFits(FitsOutFile& os) =0;
|
---|
| 99 | friend class FitsInFile;
|
---|
| 100 | friend class FitsOutFile;
|
---|
[903] | 101 |
|
---|
[1136] | 102 | };
|
---|
| 103 |
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 | class FitsFile
|
---|
| 107 | {
|
---|
| 108 |
|
---|
| 109 | public:
|
---|
| 110 |
|
---|
[1175] | 111 | FitsFile()
|
---|
| 112 | {
|
---|
| 113 | InitNull();
|
---|
| 114 | };
|
---|
[1136] | 115 | virtual ~FitsFile();
|
---|
| 116 |
|
---|
[1047] | 117 | static string getErrStatus(int status);
|
---|
[903] | 118 |
|
---|
| 119 |
|
---|
[1047] | 120 |
|
---|
[1136] | 121 |
|
---|
| 122 | inline int statusF() const { return fits_status_;}
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | protected:
|
---|
| 126 |
|
---|
| 127 | void ResetStatus(int& status) ;
|
---|
| 128 | static void printerror(int&) ;
|
---|
| 129 | static void printerror(int&,char* texte) ;
|
---|
[1175] | 130 | inline void InitNull() {fptr_ = NULL; hdutype_= 0; hdunum_ = 1;
|
---|
[1136] | 131 | fits_status_ = 0;}
|
---|
| 132 |
|
---|
| 133 | //! pointer to the FITS file, defined in fitsio.h
|
---|
| 134 | fitsfile *fptr_;
|
---|
| 135 |
|
---|
| 136 | //! image or bintable ?
|
---|
| 137 | int hdutype_;
|
---|
| 138 |
|
---|
| 139 | //! index of header to be read/written
|
---|
| 140 | int hdunum_;
|
---|
| 141 |
|
---|
| 142 | //! last status returned by fitsio library. updated only by several methods
|
---|
| 143 | int fits_status_;
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 | };
|
---|
| 147 |
|
---|
| 148 |
|
---|
| 149 | class FitsInFile : public FitsFile {
|
---|
| 150 |
|
---|
| 151 | public:
|
---|
| 152 | FitsInFile();
|
---|
| 153 | // FitsInFile(char flnm[], int hdunum=0);
|
---|
| 154 | FitsInFile(char flnm[]);
|
---|
[1143] | 155 | ~FitsInFile() { ; };
|
---|
[1136] | 156 |
|
---|
| 157 |
|
---|
[1047] | 158 | //////////////////////////////////////////////////////////
|
---|
[1136] | 159 | // methods with general purpose
|
---|
| 160 | ///////////////////////////////////////
|
---|
[1047] | 161 |
|
---|
[1136] | 162 |
|
---|
| 163 |
|
---|
| 164 | static int NbBlocks(char flnm[]);
|
---|
| 165 | static void getBlockType(char flnm[], int hdunum, string& typeOfExtension, int& naxis, vector<int>& naxisn, string& dataType, DVList& dvl );
|
---|
| 166 |
|
---|
| 167 |
|
---|
| 168 |
|
---|
| 169 | // void ReadFInit(char flnm[],int hdunum=0);
|
---|
| 170 | void ReadFInit(int hdunum);
|
---|
| 171 |
|
---|
[1047] | 172 | /*! return a reference on a DVList containing the keywords from FITS file
|
---|
| 173 | */
|
---|
[1136] | 174 | inline const DVList& DVListFromFits() const { return dvl_;}
|
---|
[1047] | 175 |
|
---|
[1143] | 176 | /* get the keywords of primary header in a DVList */
|
---|
| 177 | DVList DVListFromPrimaryHeader() const;
|
---|
| 178 |
|
---|
[1136] | 179 | void moveToFollowingHeader();
|
---|
[1047] | 180 |
|
---|
| 181 |
|
---|
[1136] | 182 | //////////////////////////////////////////////////////////
|
---|
| 183 | /////// methods for managing extensions ////////////////
|
---|
| 184 | //////////////////////////////////////////////////////////
|
---|
| 185 |
|
---|
| 186 |
|
---|
| 187 |
|
---|
[1047] | 188 | /////////////////////////////////////////////////////////////
|
---|
[1136] | 189 | // methods for managing FITS IMAGE extension
|
---|
| 190 | ///////////////////////////////////////////////////
|
---|
[1047] | 191 |
|
---|
| 192 |
|
---|
| 193 | /*! return true if the current header corresponds to a FITS image extension */
|
---|
| 194 | inline bool IsFitsImage() const { return (hdutype_ == IMAGE_HDU);}
|
---|
| 195 |
|
---|
[1136] | 196 |
|
---|
| 197 |
|
---|
[903] | 198 | /*! number of dimensions of an image extension : NAXIS parameter (in FITS notations)
|
---|
| 199 | */
|
---|
[861] | 200 | inline int nbDimOfImage() const {return naxis_;}
|
---|
[1047] | 201 | /*! a reference on a vector containing sizes of the NAXIS dimensions : NAXIS1, NAXIS2, NAXIS3 wtc.
|
---|
| 202 | */
|
---|
| 203 | inline const vector<int>& dimOfImageAxes() const { return naxisn_;}
|
---|
[903] | 204 | /*!
|
---|
| 205 | total number of data in the current IMAGE extension
|
---|
| 206 | */
|
---|
[839] | 207 | inline int nbOfImageData() const { return nbData_; }
|
---|
[903] | 208 |
|
---|
| 209 |
|
---|
[1047] | 210 |
|
---|
[1136] | 211 | //////////////////////////////////////////////////////////////////////////
|
---|
| 212 | // methods for managing FITS BINARY TABLE or ASCII TABLE extension
|
---|
| 213 | ////////////////////////////////////////////////////////////////////////
|
---|
[1047] | 214 |
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 |
|
---|
[1136] | 218 | /*! return true if the current header corresponds to a FITS ASCII or BINTABLE extension */
|
---|
| 219 | inline bool IsFitsTable() const {return (hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL);}
|
---|
[1047] | 220 |
|
---|
| 221 |
|
---|
[1136] | 222 | static void GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
|
---|
| 223 | vector<int>& repeat,
|
---|
| 224 | vector<string>& noms,
|
---|
| 225 | vector<char>& types,
|
---|
| 226 | vector<int>& taille_des_chaines);
|
---|
[1047] | 227 |
|
---|
| 228 |
|
---|
| 229 |
|
---|
[903] | 230 | /*! return a character denoting data type of column number 'nocol' in a BINTABLE :
|
---|
| 231 |
|
---|
| 232 | D : double
|
---|
| 233 |
|
---|
| 234 | E : float
|
---|
| 235 |
|
---|
| 236 | I : integer
|
---|
| 237 |
|
---|
| 238 | S : character string
|
---|
| 239 |
|
---|
| 240 | */
|
---|
[839] | 241 | char ColTypeFromFits(int nocol) const;
|
---|
[903] | 242 | /*! name of the column number 'nocol' of the current BINTABLE extension
|
---|
| 243 | */
|
---|
[839] | 244 | string ColNameFromFits(int nocol) const;
|
---|
[903] | 245 |
|
---|
| 246 | /*! number of characters of each data for the column number 'nocol' (if char* typed) of the current BINTABLE extension
|
---|
| 247 | */
|
---|
[839] | 248 | int ColStringLengthFromFits(int nocol) const;
|
---|
[903] | 249 |
|
---|
| 250 |
|
---|
| 251 |
|
---|
| 252 | /*!
|
---|
[1047] | 253 | get the NoLine-th 'line' from the current BINTABLE extension on FITS file,
|
---|
| 254 | */
|
---|
| 255 | void GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char
|
---|
| 256 | ** cdata) ;
|
---|
| 257 |
|
---|
| 258 | /*!
|
---|
| 259 | get the NoLine-th 'line' from the current BINTABLE extension on FITS file,
|
---|
| 260 | */
|
---|
| 261 | void GetBinTabLine(int NoLine, float* fdata) ;
|
---|
| 262 |
|
---|
[1136] | 263 | /*!
|
---|
[903] | 264 | fill the array 'valeurs' with double data from the current BINTABLE extension on FITS file, from column number 'NoCol'
|
---|
| 265 |
|
---|
| 266 | \param <nentries> number of data to be read
|
---|
| 267 | */
|
---|
[839] | 268 | void GetBinTabFCol(double* valeurs, int nentries, int NoCol) const;
|
---|
[903] | 269 |
|
---|
| 270 | /*! same as previous method with float data */
|
---|
[839] | 271 | void GetBinTabFCol(float* valeurs, int nentries, int NoCol) const;
|
---|
[903] | 272 | /*! same as previous method with int data */
|
---|
[839] | 273 | void GetBinTabFCol(int* valeurs, int nentries, int NoCol) const;
|
---|
[903] | 274 | /*! same as previous method with char* data */
|
---|
[839] | 275 | void GetBinTabFCol(char** valeurs,int nentries, int NoCol) const;
|
---|
| 276 | // Write elements into the FITS data array
|
---|
[903] | 277 |
|
---|
[1136] | 278 | /////////////////////////////////////////////////////////////
|
---|
| 279 | // methods for managing any type of FITS extension
|
---|
| 280 | ////////////////////////////////////////////////////////
|
---|
[1045] | 281 |
|
---|
[1136] | 282 | /*! return number of columns (return 1 if IMAGE) */
|
---|
| 283 | int NbColsFromFits() const;
|
---|
| 284 | /*! number of data in the current IMAGE extension on FITS file, or number
|
---|
| 285 | of data of column number 'nocol' of the current BINTABLE extension
|
---|
| 286 | */
|
---|
| 287 | int NentriesFromFits(int nocol) const;
|
---|
[1045] | 288 |
|
---|
| 289 |
|
---|
[1136] | 290 | /*!
|
---|
[1047] | 291 | fill the array 'map' with double data from the current extension on FITS file.
|
---|
| 292 | If the extension is BINTABLE, the first column is provided.
|
---|
| 293 |
|
---|
| 294 | \param <nentries> number of data to be read
|
---|
[903] | 295 | */
|
---|
[1047] | 296 | void GetSingleColumn(double* map, int nentries) const;
|
---|
[903] | 297 |
|
---|
[1047] | 298 | /*! same as above with float data */
|
---|
| 299 | void GetSingleColumn(float* map, int nentries) const;
|
---|
[839] | 300 |
|
---|
[1047] | 301 | /*! same as above with int data */
|
---|
| 302 | void GetSingleColumn(int* map, int nentries) const;
|
---|
| 303 |
|
---|
[1136] | 304 | private :
|
---|
[1047] | 305 |
|
---|
[1136] | 306 | void InitNull();
|
---|
| 307 | static void KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum);
|
---|
[903] | 308 | static void GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn);
|
---|
[839] | 309 |
|
---|
| 310 |
|
---|
[1136] | 311 |
|
---|
[839] | 312 |
|
---|
[903] | 313 | //! fits-Image parameter
|
---|
[839] | 314 | int bitpix_;
|
---|
[903] | 315 |
|
---|
| 316 | //! fits-Image parameter
|
---|
[839] | 317 | int naxis_;
|
---|
[903] | 318 |
|
---|
| 319 | //! fits-Image parameters : sizes of dimensions
|
---|
[861] | 320 | vector<int> naxisn_;
|
---|
[903] | 321 |
|
---|
| 322 | //! fits-Image parameter: number of data
|
---|
[839] | 323 | int nbData_;
|
---|
| 324 |
|
---|
[903] | 325 | //! Bintable parameter
|
---|
[839] | 326 | int nrows_;
|
---|
[903] | 327 |
|
---|
| 328 | //! Bintable parameter
|
---|
[839] | 329 | vector<int> repeat_;
|
---|
| 330 |
|
---|
[903] | 331 | //! Bintable parameter
|
---|
[839] | 332 | int nbcols_;
|
---|
| 333 |
|
---|
[903] | 334 | //! Bintable parameter: column names
|
---|
| 335 | vector<string> noms_;
|
---|
[1136] | 336 |
|
---|
[903] | 337 | //! Bintable parameters: types of columns (D: double, E: float, I: integers, A: char*)
|
---|
| 338 | vector<char> types_;
|
---|
[1136] | 339 |
|
---|
[903] | 340 | //! Bintable parameters: length of the char* variables
|
---|
| 341 | vector<int> taille_des_chaines_;
|
---|
[839] | 342 |
|
---|
[903] | 343 | //! DVList for transferring keywords
|
---|
[839] | 344 | DVList dvl_;
|
---|
[1045] | 345 |
|
---|
[1047] | 346 |
|
---|
[1136] | 347 | };
|
---|
[1047] | 348 |
|
---|
[875] | 349 |
|
---|
[1136] | 350 | class FitsOutFile : public FitsFile {
|
---|
[875] | 351 |
|
---|
[1136] | 352 | public:
|
---|
| 353 | FitsOutFile();
|
---|
| 354 | FitsOutFile(char flnm[], bool OldFile=false);
|
---|
[1143] | 355 | ~FitsOutFile() { ;};
|
---|
[1136] | 356 | inline void InitNull() {imageOnPrimary_=false;}
|
---|
| 357 |
|
---|
| 358 | //////////////////////////////////////////////////////////
|
---|
| 359 | /////// methods for managing extensions ////////////////
|
---|
| 360 | //////////////////////////////////////////////////////////
|
---|
| 361 |
|
---|
| 362 |
|
---|
| 363 |
|
---|
| 364 | /////////////////////////////////////////////////////////////
|
---|
| 365 | // methods for managing FITS IMAGE extension
|
---|
| 366 | ///////////////////////////////////////////////////
|
---|
| 367 |
|
---|
| 368 |
|
---|
| 369 | inline void firstImageOnPrimaryHeader() {imageOnPrimary_=true;}
|
---|
| 370 |
|
---|
| 371 | /*! create an IMAGE header on FITS file.
|
---|
| 372 | \param <type> type of data (see method ColTypeFromFits)
|
---|
| 373 | \param <nbdim> number of dimensions : 1D, 2D, 3D etc. = NAXIS
|
---|
| 374 | \param <naxisn> array containind sizes of the different dimensions
|
---|
| 375 | */
|
---|
[1143] | 376 | void makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList &dvl) ;
|
---|
[1136] | 377 |
|
---|
[1143] | 378 |
|
---|
[1136] | 379 | /*! write double data from array 'map'on an IMAGE extension
|
---|
| 380 | \param <nbData> number of data to be written
|
---|
| 381 |
|
---|
| 382 | */
|
---|
| 383 | void putImageToFits( int nbData, double* map) const;
|
---|
| 384 |
|
---|
| 385 | /*! same as previous method with float data */
|
---|
| 386 | void putImageToFits(int nbData, float* map ) const;
|
---|
| 387 |
|
---|
| 388 | /*! same as previous method with int data */
|
---|
| 389 | void putImageToFits(int nbData, int* map) const;
|
---|
| 390 |
|
---|
| 391 |
|
---|
| 392 |
|
---|
| 393 | //////////////////////////////////////////////////////////////////////////
|
---|
| 394 | // methods for managing FITS BINARY TABLE or ASCII TABLE extension
|
---|
| 395 | ////////////////////////////////////////////////////////////////////////
|
---|
| 396 |
|
---|
| 397 |
|
---|
| 398 |
|
---|
| 399 | /*! create an BINTABLE header on FITS file.
|
---|
[1143] | 400 | \param <fieldType> array conta
|
---|
| 401 | ining characters denoting types of the different column (see method ColTypeFromFits)
|
---|
[1136] | 402 | \param <Noms> array of the names of columns
|
---|
| 403 | \param <nentries> number of data of each column
|
---|
| 404 | \param <tfields> number of columns
|
---|
| 405 | \param <dvl> a SOPHYA DVList containing keywords to be appended
|
---|
| 406 | \param <extname> keyword EXTNAME for FITS file
|
---|
| 407 | \param <taille_des_chaines> vector containing the number of characters of data for each char* typed column, with order of appearance in 'fieldType'
|
---|
| 408 | */
|
---|
| 409 | void makeHeaderBntblOnFits ( char* fieldType, char** Noms, int nentries, int tfields, DVList &dvl, char* extname, vector<int> taille_des_chaines) ;
|
---|
| 410 |
|
---|
| 411 | /*! write double data from array 'donnees ' on column number 'nocol' of a BINTABLE extension.
|
---|
| 412 | \param <nentries> number of data to be written
|
---|
| 413 |
|
---|
| 414 | */
|
---|
| 415 | void putColToFits(int nocol, int nentries, double* donnees) const;
|
---|
| 416 |
|
---|
| 417 | /*! same as previous method with float data */
|
---|
| 418 | void putColToFits(int nocol, int nentries, float* donnees) const;
|
---|
| 419 |
|
---|
| 420 | /*! same as previous method with int data */
|
---|
| 421 | void putColToFits(int nocol, int nentries, int* donnees) const;
|
---|
| 422 |
|
---|
| 423 | /*! same as previous method with char* data */
|
---|
| 424 | void putColToFits(int nocol, int nentries, char** donnees) const;
|
---|
| 425 |
|
---|
[1143] | 426 | /////////////////////////////////////////////////////////////
|
---|
| 427 | // methods for managing any type of FITS extension
|
---|
| 428 | ////////////////////////////////////////////////////////
|
---|
| 429 |
|
---|
| 430 |
|
---|
| 431 | /* put keywords from a DVList into the primary header of the fits-file */
|
---|
| 432 | void DVListIntoPrimaryHeader(DVList& dvl) const;
|
---|
| 433 |
|
---|
| 434 |
|
---|
| 435 |
|
---|
[1136] | 436 | private :
|
---|
| 437 |
|
---|
| 438 | void writeSignatureOnFits() const;
|
---|
[1143] | 439 | void addKeywordsOfDVList(DVList& dvl) const;
|
---|
[1136] | 440 |
|
---|
| 441 | bool imageOnPrimary_;
|
---|
| 442 |
|
---|
| 443 | };
|
---|
| 444 |
|
---|
| 445 |
|
---|
| 446 |
|
---|
[875] | 447 | } // Fin du namespace
|
---|
| 448 |
|
---|
| 449 |
|
---|
[839] | 450 | #endif
|
---|