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