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

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

debug/check fits : gestion NTuple colonnes double + init hdunum_ ds FitsFile(FitsInOutFile&) + correction gestion TArray/TMatrix/TVector ds fitsarrhand.h - Reza 17/01/2006

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