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 | class FitsFile;
|
---|
15 | class FitsInFile;
|
---|
16 | class FitsOutFile;
|
---|
17 |
|
---|
18 |
|
---|
19 |
|
---|
20 | //
|
---|
21 | //! Class for managing Interface for SOPHYA objects to FITS Format Files (uses cfitsio lib)
|
---|
22 |
|
---|
23 | /*!
|
---|
24 | The class structure is analogous to Sophya-PPersist system :
|
---|
25 | Each SOPHYA object XXX is associated with a object of class FITS_XXX
|
---|
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 :
|
---|
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 | */
|
---|
48 |
|
---|
49 | class FitsIOHandler {
|
---|
50 |
|
---|
51 |
|
---|
52 | public:
|
---|
53 |
|
---|
54 | virtual ~FitsIOHandler() {}
|
---|
55 | /*!
|
---|
56 | this method is called from inherited objects :
|
---|
57 |
|
---|
58 | opens a file 'flnm'
|
---|
59 |
|
---|
60 | gets parameters in extension-header (hdunum)
|
---|
61 |
|
---|
62 | calls the method 'ReadFromFits' from the inherited object
|
---|
63 |
|
---|
64 |
|
---|
65 | */
|
---|
66 | void Read(char flnm[],int hdunum= 0);
|
---|
67 | /*!
|
---|
68 | this method is called from inherited objects :
|
---|
69 |
|
---|
70 | for writing a new object in a new fits-extension, at the end of
|
---|
71 |
|
---|
72 | the existing file (flnm), if OldFile=true.
|
---|
73 |
|
---|
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 :
|
---|
79 |
|
---|
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 |
|
---|
86 | */
|
---|
87 | void Write(char flnm[], bool OldFile=false) ;
|
---|
88 |
|
---|
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) ;
|
---|
94 |
|
---|
95 |
|
---|
96 | protected:
|
---|
97 | virtual void ReadFromFits(FitsInFile& is)=0;
|
---|
98 | virtual void WriteToFits(FitsOutFile& os) =0;
|
---|
99 | friend class FitsInFile;
|
---|
100 | friend class FitsOutFile;
|
---|
101 |
|
---|
102 | };
|
---|
103 |
|
---|
104 |
|
---|
105 |
|
---|
106 | class FitsFile
|
---|
107 | {
|
---|
108 |
|
---|
109 | public:
|
---|
110 |
|
---|
111 | FitsFile()
|
---|
112 | {
|
---|
113 | InitNull();
|
---|
114 | };
|
---|
115 | virtual ~FitsFile();
|
---|
116 |
|
---|
117 | static string getErrStatus(int status);
|
---|
118 |
|
---|
119 |
|
---|
120 |
|
---|
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) ;
|
---|
130 | inline void InitNull() {fptr_ = NULL; hdutype_= 0; hdunum_ = 1;
|
---|
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[]);
|
---|
155 | ~FitsInFile() { ; };
|
---|
156 |
|
---|
157 |
|
---|
158 | //////////////////////////////////////////////////////////
|
---|
159 | // methods with general purpose
|
---|
160 | ///////////////////////////////////////
|
---|
161 |
|
---|
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 |
|
---|
172 | /*! return a reference on a DVList containing the keywords from FITS file
|
---|
173 | */
|
---|
174 | inline const DVList& DVListFromFits() const { return dvl_;}
|
---|
175 |
|
---|
176 | /* get the keywords of primary header in a DVList */
|
---|
177 | DVList DVListFromPrimaryHeader() const;
|
---|
178 |
|
---|
179 | void moveToFollowingHeader();
|
---|
180 |
|
---|
181 |
|
---|
182 | //////////////////////////////////////////////////////////
|
---|
183 | /////// methods for managing extensions ////////////////
|
---|
184 | //////////////////////////////////////////////////////////
|
---|
185 |
|
---|
186 |
|
---|
187 |
|
---|
188 | /////////////////////////////////////////////////////////////
|
---|
189 | // methods for managing FITS IMAGE extension
|
---|
190 | ///////////////////////////////////////////////////
|
---|
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 |
|
---|
196 |
|
---|
197 |
|
---|
198 | /*! number of dimensions of an image extension : NAXIS parameter (in FITS notations)
|
---|
199 | */
|
---|
200 | inline int nbDimOfImage() const {return naxis_;}
|
---|
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_;}
|
---|
204 | /*!
|
---|
205 | total number of data in the current IMAGE extension
|
---|
206 | */
|
---|
207 | inline int nbOfImageData() const { return nbData_; }
|
---|
208 |
|
---|
209 |
|
---|
210 |
|
---|
211 | //////////////////////////////////////////////////////////////////////////
|
---|
212 | // methods for managing FITS BINARY TABLE or ASCII TABLE extension
|
---|
213 | ////////////////////////////////////////////////////////////////////////
|
---|
214 |
|
---|
215 |
|
---|
216 |
|
---|
217 |
|
---|
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);}
|
---|
220 |
|
---|
221 |
|
---|
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);
|
---|
227 |
|
---|
228 |
|
---|
229 |
|
---|
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 | */
|
---|
241 | char ColTypeFromFits(int nocol) const;
|
---|
242 | /*! name of the column number 'nocol' of the current BINTABLE extension
|
---|
243 | */
|
---|
244 | string ColNameFromFits(int nocol) const;
|
---|
245 |
|
---|
246 | /*! number of characters of each data for the column number 'nocol' (if char* typed) of the current BINTABLE extension
|
---|
247 | */
|
---|
248 | int ColStringLengthFromFits(int nocol) const;
|
---|
249 |
|
---|
250 |
|
---|
251 |
|
---|
252 | /*!
|
---|
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 |
|
---|
263 | /*!
|
---|
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 | */
|
---|
268 | void GetBinTabFCol(double* valeurs, int nentries, int NoCol) const;
|
---|
269 |
|
---|
270 | /*! same as previous method with float data */
|
---|
271 | void GetBinTabFCol(float* valeurs, int nentries, int NoCol) const;
|
---|
272 | /*! same as previous method with int data */
|
---|
273 | void GetBinTabFCol(int* valeurs, int nentries, int NoCol) const;
|
---|
274 | /*! same as previous method with char* data */
|
---|
275 | void GetBinTabFCol(char** valeurs,int nentries, int NoCol) const;
|
---|
276 | // Write elements into the FITS data array
|
---|
277 |
|
---|
278 | /////////////////////////////////////////////////////////////
|
---|
279 | // methods for managing any type of FITS extension
|
---|
280 | ////////////////////////////////////////////////////////
|
---|
281 |
|
---|
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;
|
---|
288 |
|
---|
289 |
|
---|
290 | /*!
|
---|
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
|
---|
295 | */
|
---|
296 | void GetSingleColumn(double* map, int nentries) const;
|
---|
297 |
|
---|
298 | /*! same as above with float data */
|
---|
299 | void GetSingleColumn(float* map, int nentries) const;
|
---|
300 |
|
---|
301 | /*! same as above with int data */
|
---|
302 | void GetSingleColumn(int* map, int nentries) const;
|
---|
303 |
|
---|
304 | private :
|
---|
305 |
|
---|
306 | void InitNull();
|
---|
307 | static void KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum);
|
---|
308 | static void GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn);
|
---|
309 |
|
---|
310 |
|
---|
311 |
|
---|
312 |
|
---|
313 | //! fits-Image parameter
|
---|
314 | int bitpix_;
|
---|
315 |
|
---|
316 | //! fits-Image parameter
|
---|
317 | int naxis_;
|
---|
318 |
|
---|
319 | //! fits-Image parameters : sizes of dimensions
|
---|
320 | vector<int> naxisn_;
|
---|
321 |
|
---|
322 | //! fits-Image parameter: number of data
|
---|
323 | int nbData_;
|
---|
324 |
|
---|
325 | //! Bintable parameter
|
---|
326 | int nrows_;
|
---|
327 |
|
---|
328 | //! Bintable parameter
|
---|
329 | vector<int> repeat_;
|
---|
330 |
|
---|
331 | //! Bintable parameter
|
---|
332 | int nbcols_;
|
---|
333 |
|
---|
334 | //! Bintable parameter: column names
|
---|
335 | vector<string> noms_;
|
---|
336 |
|
---|
337 | //! Bintable parameters: types of columns (D: double, E: float, I: integers, A: char*)
|
---|
338 | vector<char> types_;
|
---|
339 |
|
---|
340 | //! Bintable parameters: length of the char* variables
|
---|
341 | vector<int> taille_des_chaines_;
|
---|
342 |
|
---|
343 | //! DVList for transferring keywords
|
---|
344 | DVList dvl_;
|
---|
345 |
|
---|
346 |
|
---|
347 | };
|
---|
348 |
|
---|
349 |
|
---|
350 | class FitsOutFile : public FitsFile {
|
---|
351 |
|
---|
352 | public:
|
---|
353 | FitsOutFile();
|
---|
354 | FitsOutFile(char flnm[], bool OldFile=false);
|
---|
355 | ~FitsOutFile() { ;};
|
---|
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 | */
|
---|
376 | void makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList &dvl) ;
|
---|
377 |
|
---|
378 |
|
---|
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.
|
---|
400 | \param <fieldType> array conta
|
---|
401 | ining characters denoting types of the different column (see method ColTypeFromFits)
|
---|
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 |
|
---|
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 |
|
---|
436 | private :
|
---|
437 |
|
---|
438 | void writeSignatureOnFits() const;
|
---|
439 | void addKeywordsOfDVList(DVList& dvl) const;
|
---|
440 |
|
---|
441 | bool imageOnPrimary_;
|
---|
442 |
|
---|
443 | };
|
---|
444 |
|
---|
445 |
|
---|
446 |
|
---|
447 | } // Fin du namespace
|
---|
448 |
|
---|
449 |
|
---|
450 | #endif
|
---|