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

Last change on this file since 2197 was 2197, checked in by lemeur, 23 years ago

toilette d'ete : rationalisation de types...

File size: 12.5 KB
RevLine 
[839]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
[1218]12// classes for saving/loading SOPHYA objects to/from FITS files...
13// Guy le Meur (september 2000)
[875]14
[1209]15
[1218]16namespace SOPHYA {
[1209]17
[1218]18 struct BnTblLine;
[2197]19 class BufferLine;
[1218]20 class FitsFile;
21 class FitsInFile;
22 class FitsOutFile;
[1193]23
[1353]24
25
26class FitsKeyword
27{
28
29 public:
30
31FitsKeyword();
32FitsKeyword(string comment);
33FitsKeyword(string keyname, string value, string comment);
[1418]34FitsKeyword(string keyname, string value, string comment, char type);
[1353]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
[903]51//
[1136]52//! Class for managing Interface for SOPHYA objects to FITS Format Files (uses cfitsio lib)
[875]53
[1353]54class FitsIOHandler {
[903]55
56
[1218]57 public:
[903]58
[1218]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) ;
[903]64
[839]65
[1218]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;
[1136]73 };
74
75
[1218]76//! Class (virtual) for managing FITS format files
77 class FitsFile {
[1136]78
[1218]79 public:
[1136]80
[1231]81 enum WriteMode {append, clear, unknown};
82
83 enum FitsExtensionType {
[1334]84 FitsExtensionType_NULL,
[1231]85 FitsExtensionType_IMAGE,
86 FitsExtensionType_ASCII_TBL,
[1334]87 FitsExtensionType_BINARY_TBL,
88 FitsExtensionType_EOF,
89 FitsExtensionType_ERROR
[1231]90 };
91 enum FitsDataType {
[1300]92 FitsDataType_NULL,
[1231]93 FitsDataType_double,
94 FitsDataType_float,
95 FitsDataType_int,
96 FitsDataType_char,
[1359]97 FitsDataType_ASCII,
98 FitsDataType_long,
[2197]99 FitsDataType_byte,
100 FitsDataType_short
[1231]101 };
102
[2197]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
[1218]150 FitsFile() { InitNull(); };
151 virtual ~FitsFile();
152 static string GetErrStatus(int status);
153 inline int statusF() const { return fits_status_;}
[1234]154 inline void firstImageOnPrimaryHeader(bool choice) {imageOnPrimary_=choice;}
[1300]155 inline int currentHeaderIndex() {return hdunum_;}
[1136]156
157
[1218]158 protected:
[1136]159
[2197]160
161
162
163
164
[1771]165void ResetStatus(int& status) ;
166static void printerror(int&) ;
167static void printerror(int&,char* texte) ;
168static void printerrorAndContinue(int& status, char* texte);
169inline void InitNull()
170 {
171 fptr_ = NULL;
172 hdutype_= FitsExtensionType_NULL;
173 hdunum_ = 0;
174 fits_status_ = 0; imageOnPrimary_ = true;
175 }
176inline fitsfile* fitsfilePtr() const {return fptr_;}
177
178
179
[1218]180 fitsfile *fptr_; /**< pointer to the FITS file, defined in fitsio.h */
[1334]181 FitsExtensionType hdutype_; /**< image or bintable ? */
[1218]182 int hdunum_; /**< index of header to be read/written */
183 int fits_status_; /**< last status returned by fitsio library. updated only by several methods */
[1234]184 bool imageOnPrimary_;
[1136]185
[2197]186 BufferLine bfl_;
187
188
[1218]189 };
[1136]190
[1218]191//! Class for saving SOPHYA objects on FITS Format Files (uses cfitsio lib)
[1136]192
193 class FitsInFile : public FitsFile {
194
195 public:
196 FitsInFile();
[1231]197 FitsInFile(string const & flnm);
198 FitsInFile(const char * flnm);
[1143]199 ~FitsInFile() { ; };
[1136]200
[1218]201 static int NbBlocks(char flnm[]);
[1334]202 int NbBlocks();
[1231]203 static void GetBlockType(char flnm[], int hdunum, FitsExtensionType& typeOfExtension, int& naxis, vector<int>& naxisn, FitsDataType& dataType, DVList& dvl );
[1353]204
205 void ReadHeader(int hdunum);
[1354]206bool hasKeyword(int hdunum, string keyw);
207string getStringKeyword(int hdunum, string keyname,int& retStatus);
[1353]208
[1218]209 /*! \return a reference on a DVList containing the keywords from FITS file */
210 inline const DVList& DVListFromFits() const { return dvl_;}
[1136]211
[1771]212 void GetKeywordsFromHeader (int hdunum, list<FitsKeyword>& mots_cles) const;
213
214
[1218]215 DVList DVListFromPrimaryHeader() const;
216 void moveToFollowingHeader();
[1047]217
[1136]218
219
220
221 //////////////////////////////////////////////////////////
222 /////// methods for managing extensions ////////////////
223 //////////////////////////////////////////////////////////
224
225
[1047]226/////////////////////////////////////////////////////////////
[1136]227// methods for managing FITS IMAGE extension
228///////////////////////////////////////////////////
[1047]229
230
[1218]231
232/*! \return true if the current header corresponds to a FITS image extension */
[1334]233inline bool IsFitsImage() const { return (hdutype_ == FitsExtensionType_IMAGE);}
[1047]234
[1136]235
236
[1218]237 /*! \return number of dimensions of an image extension : NAXIS parameter (in FITS notations) */
[861]238inline int nbDimOfImage() const {return naxis_;}
[1218]239
240/*! \return a reference on a vector containing sizes of the NAXIS dimensions : NAXIS1, NAXIS2, NAXIS3 etc. */
[1047]241 inline const vector<int>& dimOfImageAxes() const { return naxisn_;}
[1218]242
243
244/*! \return total number of data in the current IMAGE extension */
[839]245inline int nbOfImageData() const { return nbData_; }
[903]246
[1300]247/*! \return data type of the current IMAGE extension */
248inline FitsFile::FitsDataType ImageType() const {return imageDataType_;}
[903]249
[1047]250
[1771]251
[1136]252//////////////////////////////////////////////////////////////////////////
253// methods for managing FITS BINARY TABLE or ASCII TABLE extension
254////////////////////////////////////////////////////////////////////////
[1047]255
256
257
258
[1218]259/*! \return true if the current header corresponds to a FITS ASCII or BINTABLE extension */
[1334]260inline bool IsFitsTable() const {return (hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL);}
[1047]261
262
[1353]263
264
[1218]265 static void GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
[1136]266 vector<int>& repeat,
267 vector<string>& noms,
[1300]268 vector<FitsDataType>& types,
[1136]269 vector<int>& taille_des_chaines);
[1300]270 FitsDataType ColTypeFromFits(int nocol) const;
[1218]271 string ColNameFromFits(int nocol) const;
272 int ColStringLengthFromFits(int nocol) const;
[2197]273 const BufferLine& GetBufferLine(long NoLine);
[1218]274 void GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char
[1047]275** cdata) ;
[1218]276 void GetBinTabLine(long NoLine, BnTblLine& ligne) ;
277 void GetBinTabLine(int NoLine, float* fdata) ;
[1352]278 void GetBinTabFCol(r_8* valeurs, int nentries, int NoCol) const;
279 void GetBinTabFCol(r_4* valeurs, int nentries, int NoCol) const;
280 void GetBinTabFCol(int_4* valeurs, int nentries, int NoCol) const;
[1218]281 void GetBinTabFCol(char** valeurs,int nentries, int NoCol) const;
[1047]282
[1136]283/////////////////////////////////////////////////////////////
284// methods for managing any type of FITS extension
285////////////////////////////////////////////////////////
[1045]286
[1334]287/*! \return true if the current header is beyond the maximum */
288inline bool IsFitsEOF() const {return (hdutype_ == FitsExtensionType_EOF);}
289/*! \return true if the current header is incorrect, following a cfitsio, movavs error */
290inline bool IsFitsERROR() const {return (hdutype_ == FitsExtensionType_ERROR);}
291
[1136]292 int NbColsFromFits() const;
293 int NentriesFromFits(int nocol) const;
[1045]294
295
[1352]296 void GetSingleColumn(r_8* map, int nentries) const;
[903]297
[1352]298 void GetSingleColumn(r_4* map, int nentries) const;
[839]299
[1352]300 void GetSingleColumn(int_4* map, int nentries) const;
[1047]301
[1353]302
303
304
305
[1136]306 private :
[1047]307
[1136]308void InitNull();
[1978]309
[1771]310void getHeaderWithSophyaObject();
[1136]311static void KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum);
[1300]312static void GetImageParameters (fitsfile* fileptr,FitsDataType& dataType,int& naxis,vector<int>& naxisn);
[839]313
[1300]314 FitsDataType imageDataType_; /**< fits-Image parameter (bitpix)*/
[1218]315 int naxis_; /**< fits-Image parameter */
316 vector<int> naxisn_; /**< fits-Image parameters : sizes of dimensions */
317 int nbData_; /*< fits-Image parameter: number of data */
318 int nrows_; /**< Bintable parameter */
319 vector<int> repeat_; /**< Bintable parameter */
320 int nbcols_; /**< Bintable parameter */
321 vector<string> noms_; /**< Bintable parameter: column names */
[1300]322 vector<FitsDataType> types_; /**< Bintable parameters: types of columns (D: double, E: float, I: integers, A: char*) */
[1218]323 DVList dvl_; /**< DVList for transferring keywords */
324 vector<int> taille_des_chaines_; /**< Bintable parameters: length of the char* variables */
[839]325
[1978]326 double dnull_;
327 float fnull_;
328 int inull_;
329 string cnull_;
330
331
332
[1136]333 };
[1047]334
[1218]335//! Class for loading SOPHYA objects from FITS Format Files (uses cfitsio lib)
[875]336
[1136]337 class FitsOutFile : public FitsFile {
[875]338
[1136]339 public:
[1193]340
[1136]341 FitsOutFile();
[1231]342 FitsOutFile(string const & flnm, WriteMode wrm = unknown );
343 FitsOutFile(const char * flnm, WriteMode wrm = unknown );
[1246]344 ~FitsOutFile() { if (dvlToPrimary_ != NULL) delete dvlToPrimary_;};
345 inline void InitNull() {dvlToPrimary_ = NULL;}
[1136]346
347 //////////////////////////////////////////////////////////
348 /////// methods for managing extensions ////////////////
349 //////////////////////////////////////////////////////////
350
351
352
353/////////////////////////////////////////////////////////////
354// methods for managing FITS IMAGE extension
355///////////////////////////////////////////////////
356
357
[1221]358 void makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList* dvl) ;
[1352]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;
[1136]362
363
[1143]364
[1136]365//////////////////////////////////////////////////////////////////////////
366// methods for managing FITS BINARY TABLE or ASCII TABLE extension
367////////////////////////////////////////////////////////////////////////
368
369
370
[1221]371 void makeHeaderBntblOnFits ( string fieldType, vector<string> Noms, int nentries, int tfields, DVList* dvl, string extname, vector<int> taille_des_chaines) ;
[1353]372 void appendInputHeader(FitsInFile& headerin, int hdunum);
373 void writeAppendedHeaderOnFits();
374 void PrintHeaderToBeAppended();
375 void insertCommentLineOnHeader(string comment);
[1418]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);
[1352]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;
[1218]382 void PutColToFits(int nocol, int nentries, char** donnees) const;
383 void PutBinTabLine(long NoLine, BnTblLine& ligne) const;
[1136]384
385
[1143]386/////////////////////////////////////////////////////////////
387// methods for managing any type of FITS extension
388////////////////////////////////////////////////////////
389
390
[1246]391void DVListIntoPrimaryHeader(DVList& dvl) ;
[1143]392
393
394
[1136]395 private :
396
[1231]397 void openoutputfitsfile(const char * flnm, WriteMode wrm);
[1246]398 void writeSignatureOnFits(int hdunum) const;
399 void addKeywordsOfDVList( DVList& dvl) const;
400 void addDVListOnPrimary();
[1136]401
[1246]402 DVList* dvlToPrimary_; /**< for transferring keywords when creating primary header */
[1353]403 list<FitsKeyword> mots_cles_;
404
[1136]405 };
406
[1218]407 struct BnTblLine
408 {
409 BnTblLine() {}
[1359]410 void setFormat(int dc, int fc, int ic, int lc, int bc, int cc, vector<string> names);
[1218]411 bool sameFormat(const BnTblLine& btl) const;
[1136]412
[1218]413 void Print();
[1136]414
[1218]415 vector<double> ddata_;
416 vector<float> fdata_;
[1359]417 vector<int> idata_;
[1218]418 vector<string> cdata_;
419 vector<string> ColName_;
[1359]420 vector<long> ldata_;
421 vector<unsigned char> bdata_;
422
[1218]423 };
424
425
[1353]426
[875]427} // Fin du namespace
428
[839]429#endif
Note: See TracBrowser for help on using the repository browser.