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

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

extension bntblLineRW aux long et byte

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