source: Sophya/trunk/SophyaExt/FitsIOServer/fitsfile.h@ 1138

Last change on this file since 1138 was 1136, checked in by ansari, 25 years ago

structuration a la ppersist+ convention sur hdu

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