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

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

revu l'ouverture des fichiers

File size: 12.6 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 extension-header (hdunum)
61
62calls the method 'ReadFromFits' from the inherited object
63
64
65*/
66 void Read(char flnm[],int hdunum= 0);
67/*!
68this method is called from inherited objects :
69
70for writing a new object in a new fits-extension :
71
72???
73
74 at the end of
75
76the existing file (flnm), if OldFile=true.
77
78If OldFile=false, an exception occurs
79
80By convention, primary header does not contain fits-image data : i.e.
81all data are fits-extensions. The first relevant header will have hdunum=2.
82For switching off this convention use the method :
83
84firstImageOnPrimaryHeader() (see below)
85
86In that case do not forget to precise hdunum=1 when reading data on primary header.
87
88calls the method 'WriteToFits' from the inherited object
89
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
93*/
94 void Write(char flnm[], string WriteMode= string("unknown")) ;
95
96 /*!
97Read 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) ;
101
102
103 protected:
104 virtual void ReadFromFits(FitsInFile& is)=0;
105 virtual void WriteToFits(FitsOutFile& os) =0;
106 friend class FitsInFile;
107 friend class FitsOutFile;
108
109 };
110
111
112
113class FitsFile
114{
115
116public:
117
118FitsFile()
119{
120 InitNull();
121};
122 virtual ~FitsFile();
123
124 static string getErrStatus(int status);
125
126
127
128
129inline int statusF() const { return fits_status_;}
130
131
132protected:
133
134 void ResetStatus(int& status) ;
135 static void printerror(int&) ;
136 static void printerror(int&,char* texte) ;
137 inline void InitNull() {fptr_ = NULL; hdutype_= 0; hdunum_ = 1;
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[]);
162 ~FitsInFile() { ; };
163
164
165//////////////////////////////////////////////////////////
166// methods with general purpose
167///////////////////////////////////////
168
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
179 /*! return a reference on a DVList containing the keywords from FITS file
180 */
181inline const DVList& DVListFromFits() const { return dvl_;}
182
183/* get the keywords of primary header in a DVList */
184DVList DVListFromPrimaryHeader() const;
185
186void moveToFollowingHeader();
187
188
189 //////////////////////////////////////////////////////////
190 /////// methods for managing extensions ////////////////
191 //////////////////////////////////////////////////////////
192
193
194
195/////////////////////////////////////////////////////////////
196// methods for managing FITS IMAGE extension
197///////////////////////////////////////////////////
198
199
200/*! return true if the current header corresponds to a FITS image extension */
201inline bool IsFitsImage() const { return (hdutype_ == IMAGE_HDU);}
202
203
204
205 /*! number of dimensions of an image extension : NAXIS parameter (in FITS notations)
206 */
207inline int nbDimOfImage() const {return naxis_;}
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_;}
211/*!
212 total number of data in the current IMAGE extension
213 */
214inline int nbOfImageData() const { return nbData_; }
215
216
217
218//////////////////////////////////////////////////////////////////////////
219// methods for managing FITS BINARY TABLE or ASCII TABLE extension
220////////////////////////////////////////////////////////////////////////
221
222
223
224
225/*! return true if the current header corresponds to a FITS ASCII or BINTABLE extension */
226inline bool IsFitsTable() const {return (hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL);}
227
228
229static 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);
234
235
236
237 /*! return a character denoting data type of column number 'nocol' in a BINTABLE :
238
239D : double
240
241E : float
242
243I : integer
244
245S : character string
246
247 */
248 char ColTypeFromFits(int nocol) const;
249 /*! name of the column number 'nocol' of the current BINTABLE extension
250 */
251 string ColNameFromFits(int nocol) const;
252
253 /*! number of characters of each data for the column number 'nocol' (if char* typed) of the current BINTABLE extension
254 */
255 int ColStringLengthFromFits(int nocol) const;
256
257
258
259 /*!
260get 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 /*!
266get the NoLine-th 'line' from the current BINTABLE extension on FITS file,
267 */
268 void GetBinTabLine(int NoLine, float* fdata) ;
269
270/*!
271fill 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 */
275 void GetBinTabFCol(double* valeurs, int nentries, int NoCol) const;
276
277 /*! same as previous method with float data */
278 void GetBinTabFCol(float* valeurs, int nentries, int NoCol) const;
279 /*! same as previous method with int data */
280 void GetBinTabFCol(int* valeurs, int nentries, int NoCol) const;
281 /*! same as previous method with char* data */
282 void GetBinTabFCol(char** valeurs,int nentries, int NoCol) const;
283 // Write elements into the FITS data array
284
285/////////////////////////////////////////////////////////////
286// methods for managing any type of FITS extension
287////////////////////////////////////////////////////////
288
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;
295
296
297/*!
298fill the array 'map' with double data from the current extension on FITS file.
299If the extension is BINTABLE, the first column is provided.
300
301\param <nentries> number of data to be read
302 */
303 void GetSingleColumn(double* map, int nentries) const;
304
305 /*! same as above with float data */
306 void GetSingleColumn(float* map, int nentries) const;
307
308 /*! same as above with int data */
309 void GetSingleColumn(int* map, int nentries) const;
310
311 private :
312
313void InitNull();
314static void KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum);
315static void GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn);
316
317
318
319
320 //! fits-Image parameter
321 int bitpix_;
322
323 //! fits-Image parameter
324 int naxis_;
325
326 //! fits-Image parameters : sizes of dimensions
327 vector<int> naxisn_;
328
329 //! fits-Image parameter: number of data
330 int nbData_;
331
332 //! Bintable parameter
333 int nrows_;
334
335 //! Bintable parameter
336 vector<int> repeat_;
337
338 //! Bintable parameter
339 int nbcols_;
340
341 //! Bintable parameter: column names
342 vector<string> noms_;
343
344 //! Bintable parameters: types of columns (D: double, E: float, I: integers, A: char*)
345 vector<char> types_;
346
347 //! Bintable parameters: length of the char* variables
348 vector<int> taille_des_chaines_;
349
350 //! DVList for transferring keywords
351 DVList dvl_;
352
353
354 };
355
356
357 class FitsOutFile : public FitsFile {
358
359 public:
360 FitsOutFile();
361 FitsOutFile(char flnm[], string WriteMode= string("unknown"));
362 ~FitsOutFile() { ;};
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 */
383 void makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList &dvl) ;
384
385
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.
407\param <fieldType> array conta
408ining characters denoting types of the different column (see method ColTypeFromFits)
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
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 */
439void DVListIntoPrimaryHeader(DVList& dvl) const;
440
441
442
443 private :
444
445 void writeSignatureOnFits() const;
446 void addKeywordsOfDVList(DVList& dvl) const;
447
448 bool imageOnPrimary_;
449
450 };
451
452
453
454} // Fin du namespace
455
456
457#endif
Note: See TracBrowser for help on using the repository browser.