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