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 |
|
---|
16 | namespace SOPHYA {
|
---|
17 |
|
---|
18 | struct BnTblLine;
|
---|
19 | class FitsFile;
|
---|
20 | class FitsInFile;
|
---|
21 | class FitsOutFile;
|
---|
22 |
|
---|
23 |
|
---|
24 | //
|
---|
25 | //! Class for managing Interface for SOPHYA objects to FITS Format Files (uses cfitsio lib)
|
---|
26 |
|
---|
27 | class FitsIOHandler {
|
---|
28 |
|
---|
29 |
|
---|
30 | public:
|
---|
31 |
|
---|
32 | virtual ~FitsIOHandler() {}
|
---|
33 | void Read(char flnm[],int hdunum= 0);
|
---|
34 | void Write(char flnm[]) ;
|
---|
35 | void Read(FitsInFile& ifts, int hdunum=0);
|
---|
36 | void Write(FitsOutFile& ofts) ;
|
---|
37 |
|
---|
38 |
|
---|
39 | protected:
|
---|
40 |
|
---|
41 | virtual void ReadFromFits(FitsInFile& is)=0;
|
---|
42 | virtual void WriteToFits(FitsOutFile& os) =0;
|
---|
43 |
|
---|
44 | friend class FitsInFile;
|
---|
45 | friend class FitsOutFile;
|
---|
46 | };
|
---|
47 |
|
---|
48 |
|
---|
49 | //! Class (virtual) for managing FITS format files
|
---|
50 | class FitsFile {
|
---|
51 |
|
---|
52 | public:
|
---|
53 |
|
---|
54 | enum WriteMode {append, clear, unknown};
|
---|
55 |
|
---|
56 | enum FitsExtensionType {
|
---|
57 | FitsExtensionType_NULL,
|
---|
58 | FitsExtensionType_IMAGE,
|
---|
59 | FitsExtensionType_ASCII_TBL,
|
---|
60 | FitsExtensionType_BINARY_TBL,
|
---|
61 | FitsExtensionType_EOF,
|
---|
62 | FitsExtensionType_ERROR
|
---|
63 | };
|
---|
64 | enum FitsDataType {
|
---|
65 | FitsDataType_NULL,
|
---|
66 | FitsDataType_double,
|
---|
67 | FitsDataType_float,
|
---|
68 | FitsDataType_int,
|
---|
69 | FitsDataType_char,
|
---|
70 | FitsDataType_ASCII
|
---|
71 | };
|
---|
72 |
|
---|
73 | FitsFile() { InitNull(); };
|
---|
74 | virtual ~FitsFile();
|
---|
75 | static string GetErrStatus(int status);
|
---|
76 | inline int statusF() const { return fits_status_;}
|
---|
77 | inline void firstImageOnPrimaryHeader(bool choice) {imageOnPrimary_=choice;}
|
---|
78 | inline int currentHeaderIndex() {return hdunum_;}
|
---|
79 |
|
---|
80 |
|
---|
81 | protected:
|
---|
82 |
|
---|
83 | void ResetStatus(int& status) ;
|
---|
84 | static void printerror(int&) ;
|
---|
85 | static void printerror(int&,char* texte) ;
|
---|
86 | inline void InitNull() {fptr_ = NULL; hdutype_= FitsExtensionType_NULL; hdunum_ = 0;
|
---|
87 | fits_status_ = 0; imageOnPrimary_ = true;}
|
---|
88 | fitsfile *fptr_; /**< pointer to the FITS file, defined in fitsio.h */
|
---|
89 | FitsExtensionType hdutype_; /**< image or bintable ? */
|
---|
90 | int hdunum_; /**< index of header to be read/written */
|
---|
91 | int fits_status_; /**< last status returned by fitsio library. updated only by several methods */
|
---|
92 | bool imageOnPrimary_;
|
---|
93 |
|
---|
94 | };
|
---|
95 |
|
---|
96 | //! Class for saving SOPHYA objects on FITS Format Files (uses cfitsio lib)
|
---|
97 |
|
---|
98 | class FitsInFile : public FitsFile {
|
---|
99 |
|
---|
100 | public:
|
---|
101 | FitsInFile();
|
---|
102 | FitsInFile(string const & flnm);
|
---|
103 | FitsInFile(const char * flnm);
|
---|
104 | ~FitsInFile() { ; };
|
---|
105 |
|
---|
106 | static int NbBlocks(char flnm[]);
|
---|
107 | int NbBlocks();
|
---|
108 | static void GetBlockType(char flnm[], int hdunum, FitsExtensionType& typeOfExtension, int& naxis, vector<int>& naxisn, FitsDataType& dataType, DVList& dvl );
|
---|
109 | void ReadHeader(int hdunum);
|
---|
110 |
|
---|
111 | /*! \return a reference on a DVList containing the keywords from FITS file */
|
---|
112 | inline const DVList& DVListFromFits() const { return dvl_;}
|
---|
113 |
|
---|
114 | DVList DVListFromPrimaryHeader() const;
|
---|
115 | void moveToFollowingHeader();
|
---|
116 |
|
---|
117 |
|
---|
118 |
|
---|
119 |
|
---|
120 | //////////////////////////////////////////////////////////
|
---|
121 | /////// methods for managing extensions ////////////////
|
---|
122 | //////////////////////////////////////////////////////////
|
---|
123 |
|
---|
124 |
|
---|
125 | /////////////////////////////////////////////////////////////
|
---|
126 | // methods for managing FITS IMAGE extension
|
---|
127 | ///////////////////////////////////////////////////
|
---|
128 |
|
---|
129 |
|
---|
130 |
|
---|
131 | /*! \return true if the current header corresponds to a FITS image extension */
|
---|
132 | inline bool IsFitsImage() const { return (hdutype_ == FitsExtensionType_IMAGE);}
|
---|
133 |
|
---|
134 |
|
---|
135 |
|
---|
136 | /*! \return number of dimensions of an image extension : NAXIS parameter (in FITS notations) */
|
---|
137 | inline int nbDimOfImage() const {return naxis_;}
|
---|
138 |
|
---|
139 | /*! \return a reference on a vector containing sizes of the NAXIS dimensions : NAXIS1, NAXIS2, NAXIS3 etc. */
|
---|
140 | inline const vector<int>& dimOfImageAxes() const { return naxisn_;}
|
---|
141 |
|
---|
142 |
|
---|
143 | /*! \return total number of data in the current IMAGE extension */
|
---|
144 | inline int nbOfImageData() const { return nbData_; }
|
---|
145 |
|
---|
146 | /*! \return data type of the current IMAGE extension */
|
---|
147 | inline FitsFile::FitsDataType ImageType() const {return imageDataType_;}
|
---|
148 |
|
---|
149 |
|
---|
150 | //////////////////////////////////////////////////////////////////////////
|
---|
151 | // methods for managing FITS BINARY TABLE or ASCII TABLE extension
|
---|
152 | ////////////////////////////////////////////////////////////////////////
|
---|
153 |
|
---|
154 |
|
---|
155 |
|
---|
156 |
|
---|
157 | /*! \return true if the current header corresponds to a FITS ASCII or BINTABLE extension */
|
---|
158 | inline bool IsFitsTable() const {return (hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL);}
|
---|
159 |
|
---|
160 |
|
---|
161 | static void GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
|
---|
162 | vector<int>& repeat,
|
---|
163 | vector<string>& noms,
|
---|
164 | vector<FitsDataType>& types,
|
---|
165 | vector<int>& taille_des_chaines);
|
---|
166 | FitsDataType ColTypeFromFits(int nocol) const;
|
---|
167 | string ColNameFromFits(int nocol) const;
|
---|
168 | int ColStringLengthFromFits(int nocol) const;
|
---|
169 | void GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char
|
---|
170 | ** cdata) ;
|
---|
171 | void GetBinTabLine(long NoLine, BnTblLine& ligne) ;
|
---|
172 | void GetBinTabLine(int NoLine, float* fdata) ;
|
---|
173 | void GetBinTabFCol(r_8* valeurs, int nentries, int NoCol) const;
|
---|
174 | void GetBinTabFCol(r_4* valeurs, int nentries, int NoCol) const;
|
---|
175 | void GetBinTabFCol(int_4* valeurs, int nentries, int NoCol) const;
|
---|
176 | void GetBinTabFCol(char** valeurs,int nentries, int NoCol) const;
|
---|
177 |
|
---|
178 | /////////////////////////////////////////////////////////////
|
---|
179 | // methods for managing any type of FITS extension
|
---|
180 | ////////////////////////////////////////////////////////
|
---|
181 |
|
---|
182 | /*! \return true if the current header is beyond the maximum */
|
---|
183 | inline bool IsFitsEOF() const {return (hdutype_ == FitsExtensionType_EOF);}
|
---|
184 | /*! \return true if the current header is incorrect, following a cfitsio, movavs error */
|
---|
185 | inline bool IsFitsERROR() const {return (hdutype_ == FitsExtensionType_ERROR);}
|
---|
186 |
|
---|
187 | int NbColsFromFits() const;
|
---|
188 | int NentriesFromFits(int nocol) const;
|
---|
189 |
|
---|
190 |
|
---|
191 | void GetSingleColumn(r_8* map, int nentries) const;
|
---|
192 |
|
---|
193 | void GetSingleColumn(r_4* map, int nentries) const;
|
---|
194 |
|
---|
195 | void GetSingleColumn(int_4* map, int nentries) const;
|
---|
196 |
|
---|
197 | private :
|
---|
198 |
|
---|
199 | void InitNull();
|
---|
200 | void getHeader();
|
---|
201 | static void KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum);
|
---|
202 | static void GetImageParameters (fitsfile* fileptr,FitsDataType& dataType,int& naxis,vector<int>& naxisn);
|
---|
203 |
|
---|
204 | FitsDataType imageDataType_; /**< fits-Image parameter (bitpix)*/
|
---|
205 | int naxis_; /**< fits-Image parameter */
|
---|
206 | vector<int> naxisn_; /**< fits-Image parameters : sizes of dimensions */
|
---|
207 | int nbData_; /*< fits-Image parameter: number of data */
|
---|
208 | int nrows_; /**< Bintable parameter */
|
---|
209 | vector<int> repeat_; /**< Bintable parameter */
|
---|
210 | int nbcols_; /**< Bintable parameter */
|
---|
211 | vector<string> noms_; /**< Bintable parameter: column names */
|
---|
212 | vector<FitsDataType> types_; /**< Bintable parameters: types of columns (D: double, E: float, I: integers, A: char*) */
|
---|
213 | DVList dvl_; /**< DVList for transferring keywords */
|
---|
214 | vector<int> taille_des_chaines_; /**< Bintable parameters: length of the char* variables */
|
---|
215 |
|
---|
216 | };
|
---|
217 |
|
---|
218 | //! Class for loading SOPHYA objects from FITS Format Files (uses cfitsio lib)
|
---|
219 |
|
---|
220 | class FitsOutFile : public FitsFile {
|
---|
221 |
|
---|
222 | public:
|
---|
223 |
|
---|
224 | FitsOutFile();
|
---|
225 | FitsOutFile(string const & flnm, WriteMode wrm = unknown );
|
---|
226 | FitsOutFile(const char * flnm, WriteMode wrm = unknown );
|
---|
227 | ~FitsOutFile() { if (dvlToPrimary_ != NULL) delete dvlToPrimary_;};
|
---|
228 | inline void InitNull() {dvlToPrimary_ = NULL;}
|
---|
229 |
|
---|
230 | //////////////////////////////////////////////////////////
|
---|
231 | /////// methods for managing extensions ////////////////
|
---|
232 | //////////////////////////////////////////////////////////
|
---|
233 |
|
---|
234 |
|
---|
235 |
|
---|
236 | /////////////////////////////////////////////////////////////
|
---|
237 | // methods for managing FITS IMAGE extension
|
---|
238 | ///////////////////////////////////////////////////
|
---|
239 |
|
---|
240 |
|
---|
241 | void makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList* dvl) ;
|
---|
242 | void PutImageToFits( int nbData, r_8* map) const;
|
---|
243 | void PutImageToFits(int nbData, r_4* map ) const;
|
---|
244 | void PutImageToFits(int nbData, int_4* map) const;
|
---|
245 |
|
---|
246 |
|
---|
247 |
|
---|
248 | //////////////////////////////////////////////////////////////////////////
|
---|
249 | // methods for managing FITS BINARY TABLE or ASCII TABLE extension
|
---|
250 | ////////////////////////////////////////////////////////////////////////
|
---|
251 |
|
---|
252 |
|
---|
253 |
|
---|
254 | void makeHeaderBntblOnFits ( string fieldType, vector<string> Noms, int nentries, int tfields, DVList* dvl, string extname, vector<int> taille_des_chaines) ;
|
---|
255 | void PutColToFits(int nocol, int nentries, r_8* donnees) const;
|
---|
256 | void PutColToFits(int nocol, int nentries, r_4* donnees) const;
|
---|
257 | void PutColToFits(int nocol, int nentries, int_4* donnees) const;
|
---|
258 | void PutColToFits(int nocol, int nentries, char** donnees) const;
|
---|
259 | void PutBinTabLine(long NoLine, BnTblLine& ligne) const;
|
---|
260 |
|
---|
261 |
|
---|
262 | /////////////////////////////////////////////////////////////
|
---|
263 | // methods for managing any type of FITS extension
|
---|
264 | ////////////////////////////////////////////////////////
|
---|
265 |
|
---|
266 |
|
---|
267 | void DVListIntoPrimaryHeader(DVList& dvl) ;
|
---|
268 |
|
---|
269 |
|
---|
270 |
|
---|
271 | private :
|
---|
272 |
|
---|
273 | void openoutputfitsfile(const char * flnm, WriteMode wrm);
|
---|
274 | void writeSignatureOnFits(int hdunum) const;
|
---|
275 | void addKeywordsOfDVList( DVList& dvl) const;
|
---|
276 | void addDVListOnPrimary();
|
---|
277 |
|
---|
278 | DVList* dvlToPrimary_; /**< for transferring keywords when creating primary header */
|
---|
279 | };
|
---|
280 |
|
---|
281 | struct BnTblLine
|
---|
282 | {
|
---|
283 | BnTblLine() {}
|
---|
284 | void setFormat(int dc, int fc, int ic, int cc, vector<string> names);
|
---|
285 | bool sameFormat(const BnTblLine& btl) const;
|
---|
286 |
|
---|
287 | void Print();
|
---|
288 |
|
---|
289 | vector<double> ddata_;
|
---|
290 | vector<float> fdata_;
|
---|
291 | vector<int> idata_;
|
---|
292 | vector<string> cdata_;
|
---|
293 | vector<string> ColName_;
|
---|
294 |
|
---|
295 | };
|
---|
296 |
|
---|
297 |
|
---|
298 | } // Fin du namespace
|
---|
299 |
|
---|
300 |
|
---|
301 | #endif
|
---|