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

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

normalisation des DVList

File size: 12.2 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= 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() { 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() { ; };
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
173/* get the keywords of primary header in a DVList */
174DVList DVListFromPrimaryHeader() const;
175
176void moveToFollowingHeader();
177
178
179 //////////////////////////////////////////////////////////
180 /////// methods for managing extensions ////////////////
181 //////////////////////////////////////////////////////////
182
183
184
185/////////////////////////////////////////////////////////////
186// methods for managing FITS IMAGE extension
187///////////////////////////////////////////////////
188
189
190/*! return true if the current header corresponds to a FITS image extension */
191inline bool IsFitsImage() const { return (hdutype_ == IMAGE_HDU);}
192
193
194
195 /*! number of dimensions of an image extension : NAXIS parameter (in FITS notations)
196 */
197inline int nbDimOfImage() const {return naxis_;}
198/*! a reference on a vector containing sizes of the NAXIS dimensions : NAXIS1, NAXIS2, NAXIS3 wtc.
199 */
200 inline const vector<int>& dimOfImageAxes() const { return naxisn_;}
201/*!
202 total number of data in the current IMAGE extension
203 */
204inline int nbOfImageData() const { return nbData_; }
205
206
207
208//////////////////////////////////////////////////////////////////////////
209// methods for managing FITS BINARY TABLE or ASCII TABLE extension
210////////////////////////////////////////////////////////////////////////
211
212
213
214
215/*! return true if the current header corresponds to a FITS ASCII or BINTABLE extension */
216inline bool IsFitsTable() const {return (hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL);}
217
218
219static void GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
220 vector<int>& repeat,
221 vector<string>& noms,
222 vector<char>& types,
223 vector<int>& taille_des_chaines);
224
225
226
227 /*! return a character denoting data type of column number 'nocol' in a BINTABLE :
228
229D : double
230
231E : float
232
233I : integer
234
235S : character string
236
237 */
238 char ColTypeFromFits(int nocol) const;
239 /*! name of the column number 'nocol' of the current BINTABLE extension
240 */
241 string ColNameFromFits(int nocol) const;
242
243 /*! number of characters of each data for the column number 'nocol' (if char* typed) of the current BINTABLE extension
244 */
245 int ColStringLengthFromFits(int nocol) const;
246
247
248
249 /*!
250get the NoLine-th 'line' from the current BINTABLE extension on FITS file,
251 */
252 void GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char
253** cdata) ;
254
255 /*!
256get the NoLine-th 'line' from the current BINTABLE extension on FITS file,
257 */
258 void GetBinTabLine(int NoLine, float* fdata) ;
259
260/*!
261fill the array 'valeurs' with double data from the current BINTABLE extension on FITS file, from column number 'NoCol'
262
263\param <nentries> number of data to be read
264 */
265 void GetBinTabFCol(double* valeurs, int nentries, int NoCol) const;
266
267 /*! same as previous method with float data */
268 void GetBinTabFCol(float* valeurs, int nentries, int NoCol) const;
269 /*! same as previous method with int data */
270 void GetBinTabFCol(int* valeurs, int nentries, int NoCol) const;
271 /*! same as previous method with char* data */
272 void GetBinTabFCol(char** valeurs,int nentries, int NoCol) const;
273 // Write elements into the FITS data array
274
275/////////////////////////////////////////////////////////////
276// methods for managing any type of FITS extension
277////////////////////////////////////////////////////////
278
279 /*! return number of columns (return 1 if IMAGE) */
280 int NbColsFromFits() const;
281 /*! number of data in the current IMAGE extension on FITS file, or number
282 of data of column number 'nocol' of the current BINTABLE extension
283 */
284 int NentriesFromFits(int nocol) const;
285
286
287/*!
288fill the array 'map' with double data from the current extension on FITS file.
289If the extension is BINTABLE, the first column is provided.
290
291\param <nentries> number of data to be read
292 */
293 void GetSingleColumn(double* map, int nentries) const;
294
295 /*! same as above with float data */
296 void GetSingleColumn(float* map, int nentries) const;
297
298 /*! same as above with int data */
299 void GetSingleColumn(int* map, int nentries) const;
300
301 private :
302
303void InitNull();
304static void KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum);
305static void GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn);
306
307
308
309
310 //! fits-Image parameter
311 int bitpix_;
312
313 //! fits-Image parameter
314 int naxis_;
315
316 //! fits-Image parameters : sizes of dimensions
317 vector<int> naxisn_;
318
319 //! fits-Image parameter: number of data
320 int nbData_;
321
322 //! Bintable parameter
323 int nrows_;
324
325 //! Bintable parameter
326 vector<int> repeat_;
327
328 //! Bintable parameter
329 int nbcols_;
330
331 //! Bintable parameter: column names
332 vector<string> noms_;
333
334 //! Bintable parameters: types of columns (D: double, E: float, I: integers, A: char*)
335 vector<char> types_;
336
337 //! Bintable parameters: length of the char* variables
338 vector<int> taille_des_chaines_;
339
340 //! DVList for transferring keywords
341 DVList dvl_;
342
343
344 };
345
346
347 class FitsOutFile : public FitsFile {
348
349 public:
350 FitsOutFile();
351 FitsOutFile(char flnm[], bool OldFile=false);
352 ~FitsOutFile() { ;};
353 inline void InitNull() {imageOnPrimary_=false;}
354
355 //////////////////////////////////////////////////////////
356 /////// methods for managing extensions ////////////////
357 //////////////////////////////////////////////////////////
358
359
360
361/////////////////////////////////////////////////////////////
362// methods for managing FITS IMAGE extension
363///////////////////////////////////////////////////
364
365
366 inline void firstImageOnPrimaryHeader() {imageOnPrimary_=true;}
367
368 /*! create an IMAGE header on FITS file.
369\param <type> type of data (see method ColTypeFromFits)
370\param <nbdim> number of dimensions : 1D, 2D, 3D etc. = NAXIS
371\param <naxisn> array containind sizes of the different dimensions
372 */
373 void makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList &dvl) ;
374
375
376 /*! write double data from array 'map'on an IMAGE extension
377\param <nbData> number of data to be written
378
379 */
380 void putImageToFits( int nbData, double* map) const;
381
382 /*! same as previous method with float data */
383 void putImageToFits(int nbData, float* map ) const;
384
385 /*! same as previous method with int data */
386 void putImageToFits(int nbData, int* map) const;
387
388
389
390//////////////////////////////////////////////////////////////////////////
391// methods for managing FITS BINARY TABLE or ASCII TABLE extension
392////////////////////////////////////////////////////////////////////////
393
394
395
396 /*! create an BINTABLE header on FITS file.
397\param <fieldType> array conta
398ining characters denoting types of the different column (see method ColTypeFromFits)
399\param <Noms> array of the names of columns
400\param <nentries> number of data of each column
401\param <tfields> number of columns
402\param <dvl> a SOPHYA DVList containing keywords to be appended
403\param <extname> keyword EXTNAME for FITS file
404\param <taille_des_chaines> vector containing the number of characters of data for each char* typed column, with order of appearance in 'fieldType'
405 */
406 void makeHeaderBntblOnFits ( char* fieldType, char** Noms, int nentries, int tfields, DVList &dvl, char* extname, vector<int> taille_des_chaines) ;
407
408 /*! write double data from array 'donnees ' on column number 'nocol' of a BINTABLE extension.
409\param <nentries> number of data to be written
410
411 */
412 void putColToFits(int nocol, int nentries, double* donnees) const;
413
414 /*! same as previous method with float data */
415 void putColToFits(int nocol, int nentries, float* donnees) const;
416
417 /*! same as previous method with int data */
418 void putColToFits(int nocol, int nentries, int* donnees) const;
419
420 /*! same as previous method with char* data */
421 void putColToFits(int nocol, int nentries, char** donnees) const;
422
423/////////////////////////////////////////////////////////////
424// methods for managing any type of FITS extension
425////////////////////////////////////////////////////////
426
427
428/* put keywords from a DVList into the primary header of the fits-file */
429void DVListIntoPrimaryHeader(DVList& dvl) const;
430
431
432
433 private :
434
435 void writeSignatureOnFits() const;
436 void addKeywordsOfDVList(DVList& dvl) const;
437
438 bool imageOnPrimary_;
439
440 };
441
442
443
444} // Fin du namespace
445
446
447#endif
Note: See TracBrowser for help on using the repository browser.