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

Last change on this file since 1353 was 1353, checked in by lemeur, 25 years ago

fonction de manipulation directe de headers

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