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