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

Last change on this file since 1703 was 1703, checked in by lemeur, 24 years ago

lecture de spheres fits, sans NSIDE ni LASTPIX

File size: 10.7 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
12// classes for saving/loading SOPHYA objects to/from FITS files...
13// Guy le Meur (september 2000)
14
15
16namespace SOPHYA {
17
18 struct BnTblLine;
19 class FitsFile;
20 class FitsInFile;
21 class FitsOutFile;
22
23
24
25class FitsKeyword
26{
27
28 public:
29
30FitsKeyword();
31FitsKeyword(string comment);
32FitsKeyword(string keyname, string value, string comment);
33FitsKeyword(string keyname, string value, string comment, char type);
34void writeOnFits(fitsfile* ptr);
35
36void Print();
37
38 private:
39
40 char datatype_;
41 string keyname_;
42 double dvalue_;
43 int ivalue_;
44 string svalue_;
45 string comment_;
46};
47
48
49
50//
51//! Class for managing Interface for SOPHYA objects to FITS Format Files (uses cfitsio lib)
52
53class FitsIOHandler {
54
55
56 public:
57
58 virtual ~FitsIOHandler() {}
59 void Read(char flnm[],int hdunum= 0);
60 void Write(char flnm[]) ;
61 void Read(FitsInFile& ifts, int hdunum=0);
62 void Write(FitsOutFile& ofts) ;
63
64
65 protected:
66
67 virtual void ReadFromFits(FitsInFile& is)=0;
68 virtual void WriteToFits(FitsOutFile& os) =0;
69
70 friend class FitsInFile;
71 friend class FitsOutFile;
72 };
73
74
75//! Class (virtual) for managing FITS format files
76 class FitsFile {
77
78 public:
79
80 enum WriteMode {append, clear, unknown};
81
82 enum FitsExtensionType {
83 FitsExtensionType_NULL,
84 FitsExtensionType_IMAGE,
85 FitsExtensionType_ASCII_TBL,
86 FitsExtensionType_BINARY_TBL,
87 FitsExtensionType_EOF,
88 FitsExtensionType_ERROR
89 };
90 enum FitsDataType {
91 FitsDataType_NULL,
92 FitsDataType_double,
93 FitsDataType_float,
94 FitsDataType_int,
95 FitsDataType_char,
96 FitsDataType_ASCII,
97 FitsDataType_long,
98 FitsDataType_byte
99 };
100
101 FitsFile() { InitNull(); };
102 virtual ~FitsFile();
103 static string GetErrStatus(int status);
104 inline int statusF() const { return fits_status_;}
105 inline void firstImageOnPrimaryHeader(bool choice) {imageOnPrimary_=choice;}
106 inline int currentHeaderIndex() {return hdunum_;}
107 inline fitsfile* fitsfilePtr() {return fptr_;}
108
109
110 protected:
111
112 void ResetStatus(int& status) ;
113 static void printerror(int&) ;
114 static void printerror(int&,char* texte) ;
115 static void printerrorAndContinue(int& status, char* texte);
116 inline void InitNull() {fptr_ = NULL; hdutype_= FitsExtensionType_NULL; hdunum_ = 0;
117 fits_status_ = 0; imageOnPrimary_ = true;}
118 fitsfile *fptr_; /**< pointer to the FITS file, defined in fitsio.h */
119 FitsExtensionType hdutype_; /**< image or bintable ? */
120 int hdunum_; /**< index of header to be read/written */
121 int fits_status_; /**< last status returned by fitsio library. updated only by several methods */
122 bool imageOnPrimary_;
123
124 };
125
126//! Class for saving SOPHYA objects on FITS Format Files (uses cfitsio lib)
127
128 class FitsInFile : public FitsFile {
129
130 public:
131 FitsInFile();
132 FitsInFile(string const & flnm);
133 FitsInFile(const char * flnm);
134 ~FitsInFile() { ; };
135
136 static int NbBlocks(char flnm[]);
137 int NbBlocks();
138 static void GetBlockType(char flnm[], int hdunum, FitsExtensionType& typeOfExtension, int& naxis, vector<int>& naxisn, FitsDataType& dataType, DVList& dvl );
139
140 void ReadHeader(int hdunum);
141bool hasKeyword(int hdunum, string keyw);
142string getStringKeyword(int hdunum, string keyname,int& retStatus);
143
144 /*! \return a reference on a DVList containing the keywords from FITS file */
145 inline const DVList& DVListFromFits() const { return dvl_;}
146
147 DVList DVListFromPrimaryHeader() const;
148 void moveToFollowingHeader();
149
150
151
152
153 //////////////////////////////////////////////////////////
154 /////// methods for managing extensions ////////////////
155 //////////////////////////////////////////////////////////
156
157
158/////////////////////////////////////////////////////////////
159// methods for managing FITS IMAGE extension
160///////////////////////////////////////////////////
161
162
163
164/*! \return true if the current header corresponds to a FITS image extension */
165inline bool IsFitsImage() const { return (hdutype_ == FitsExtensionType_IMAGE);}
166
167
168
169 /*! \return number of dimensions of an image extension : NAXIS parameter (in FITS notations) */
170inline int nbDimOfImage() const {return naxis_;}
171
172/*! \return a reference on a vector containing sizes of the NAXIS dimensions : NAXIS1, NAXIS2, NAXIS3 etc. */
173 inline const vector<int>& dimOfImageAxes() const { return naxisn_;}
174
175
176/*! \return total number of data in the current IMAGE extension */
177inline int nbOfImageData() const { return nbData_; }
178
179/*! \return data type of the current IMAGE extension */
180inline FitsFile::FitsDataType ImageType() const {return imageDataType_;}
181
182
183//////////////////////////////////////////////////////////////////////////
184// methods for managing FITS BINARY TABLE or ASCII TABLE extension
185////////////////////////////////////////////////////////////////////////
186
187
188
189
190/*! \return true if the current header corresponds to a FITS ASCII or BINTABLE extension */
191inline bool IsFitsTable() const {return (hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL);}
192
193
194
195
196 static void GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
197 vector<int>& repeat,
198 vector<string>& noms,
199 vector<FitsDataType>& types,
200 vector<int>& taille_des_chaines);
201 FitsDataType ColTypeFromFits(int nocol) const;
202 string ColNameFromFits(int nocol) const;
203 int ColStringLengthFromFits(int nocol) const;
204 void GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char
205** cdata) ;
206 void GetBinTabLine(long NoLine, BnTblLine& ligne) ;
207 void GetBinTabLine(int NoLine, float* fdata) ;
208 void GetBinTabFCol(r_8* valeurs, int nentries, int NoCol) const;
209 void GetBinTabFCol(r_4* valeurs, int nentries, int NoCol) const;
210 void GetBinTabFCol(int_4* valeurs, int nentries, int NoCol) const;
211 void GetBinTabFCol(char** valeurs,int nentries, int NoCol) const;
212
213/////////////////////////////////////////////////////////////
214// methods for managing any type of FITS extension
215////////////////////////////////////////////////////////
216
217/*! \return true if the current header is beyond the maximum */
218inline bool IsFitsEOF() const {return (hdutype_ == FitsExtensionType_EOF);}
219/*! \return true if the current header is incorrect, following a cfitsio, movavs error */
220inline bool IsFitsERROR() const {return (hdutype_ == FitsExtensionType_ERROR);}
221
222 int NbColsFromFits() const;
223 int NentriesFromFits(int nocol) const;
224
225
226 void GetSingleColumn(r_8* map, int nentries) const;
227
228 void GetSingleColumn(r_4* map, int nentries) const;
229
230 void GetSingleColumn(int_4* map, int nentries) const;
231
232
233
234
235
236 private :
237
238void InitNull();
239void getHeader();
240static void KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum);
241static void GetImageParameters (fitsfile* fileptr,FitsDataType& dataType,int& naxis,vector<int>& naxisn);
242
243 FitsDataType imageDataType_; /**< fits-Image parameter (bitpix)*/
244 int naxis_; /**< fits-Image parameter */
245 vector<int> naxisn_; /**< fits-Image parameters : sizes of dimensions */
246 int nbData_; /*< fits-Image parameter: number of data */
247 int nrows_; /**< Bintable parameter */
248 vector<int> repeat_; /**< Bintable parameter */
249 int nbcols_; /**< Bintable parameter */
250 vector<string> noms_; /**< Bintable parameter: column names */
251 vector<FitsDataType> types_; /**< Bintable parameters: types of columns (D: double, E: float, I: integers, A: char*) */
252 DVList dvl_; /**< DVList for transferring keywords */
253 vector<int> taille_des_chaines_; /**< Bintable parameters: length of the char* variables */
254
255 };
256
257//! Class for loading SOPHYA objects from FITS Format Files (uses cfitsio lib)
258
259 class FitsOutFile : public FitsFile {
260
261 public:
262
263 FitsOutFile();
264 FitsOutFile(string const & flnm, WriteMode wrm = unknown );
265 FitsOutFile(const char * flnm, WriteMode wrm = unknown );
266 ~FitsOutFile() { if (dvlToPrimary_ != NULL) delete dvlToPrimary_;};
267 inline void InitNull() {dvlToPrimary_ = NULL;}
268
269 //////////////////////////////////////////////////////////
270 /////// methods for managing extensions ////////////////
271 //////////////////////////////////////////////////////////
272
273
274
275/////////////////////////////////////////////////////////////
276// methods for managing FITS IMAGE extension
277///////////////////////////////////////////////////
278
279
280 void makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList* dvl) ;
281 void PutImageToFits( int nbData, r_8* map) const;
282 void PutImageToFits(int nbData, r_4* map ) const;
283 void PutImageToFits(int nbData, int_4* map) const;
284
285
286
287//////////////////////////////////////////////////////////////////////////
288// methods for managing FITS BINARY TABLE or ASCII TABLE extension
289////////////////////////////////////////////////////////////////////////
290
291
292
293 void makeHeaderBntblOnFits ( string fieldType, vector<string> Noms, int nentries, int tfields, DVList* dvl, string extname, vector<int> taille_des_chaines) ;
294 void appendInputHeader(FitsInFile& headerin, int hdunum);
295 void writeAppendedHeaderOnFits();
296 void PrintHeaderToBeAppended();
297 void insertCommentLineOnHeader(string comment);
298 void insertKeywordOnHeader(string keyname, double value, string comment);
299 void insertKeywordOnHeader(string keyname, int value, string comment);
300 void insertKeywordOnHeader(string keyname, string value, string comment);
301 void PutColToFits(int nocol, int nentries, r_8* donnees) const;
302 void PutColToFits(int nocol, int nentries, r_4* donnees) const;
303 void PutColToFits(int nocol, int nentries, int_4* donnees) const;
304 void PutColToFits(int nocol, int nentries, char** donnees) const;
305 void PutBinTabLine(long NoLine, BnTblLine& ligne) const;
306
307
308/////////////////////////////////////////////////////////////
309// methods for managing any type of FITS extension
310////////////////////////////////////////////////////////
311
312
313void DVListIntoPrimaryHeader(DVList& dvl) ;
314
315
316
317 private :
318
319 void openoutputfitsfile(const char * flnm, WriteMode wrm);
320 void writeSignatureOnFits(int hdunum) const;
321 void addKeywordsOfDVList( DVList& dvl) const;
322 void addDVListOnPrimary();
323
324 DVList* dvlToPrimary_; /**< for transferring keywords when creating primary header */
325 list<FitsKeyword> mots_cles_;
326
327 };
328
329 struct BnTblLine
330 {
331 BnTblLine() {}
332 void setFormat(int dc, int fc, int ic, int lc, int bc, int cc, vector<string> names);
333 bool sameFormat(const BnTblLine& btl) const;
334
335 void Print();
336
337 vector<double> ddata_;
338 vector<float> fdata_;
339 vector<int> idata_;
340 vector<string> cdata_;
341 vector<string> ColName_;
342 vector<long> ldata_;
343 vector<unsigned char> bdata_;
344
345 };
346
347
348
349
350
351} // Fin du namespace
352
353#endif
Note: See TracBrowser for help on using the repository browser.