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

Last change on this file since 2860 was 2860, checked in by ansari, 20 years ago

Ajout constructeur de copie (pas parfait) pour FitsInOutFile + modifs classes FitsFile/FitsInFile/FitsOutFile pour heriter de FitsInOutFile avec constructeur a partir de FitsInOutFile - Reza 20/12/2005

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