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

Last change on this file since 1208 was 1193, checked in by ansari, 25 years ago

amelioration lecture ligne a ligne

File size: 14.2 KB
Line 
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
12namespace SOPHYA {
13
14struct BnTblLine
15 {
16BnTblLine() {}
17void setFormat(int dc, int fc, int ic, int cc, vector<string> names)
18 {
19 int nbcols = dc + fc + ic + cc;
20 ColName_ = vector<string>(nbcols);
21 for (int k=0; k < nbcols; k++) ColName_ = names;
22 if (dc >0) ddata_ = vector<double>(dc);
23 if (fc >0) fdata_ = vector<float>(fc);
24 if (ic >0) idata_ = vector<int>(fc);
25 if (cc >0) cdata_ = vector<string>(fc);
26 }
27 void Print()
28 {
29 int k;
30 int compt = 0;
31 cout << " ********* ligne ************* " << endl;
32 cout << " *** noms de variables " << endl;
33 for (k=0; k < compt + ColName_.size(); k++) cout << ColName_[k] << " ";
34 cout << endl;
35 cout << " *** variables doubles " << endl;
36 for (k=0; k < ddata_.size(); k++) cout << ddata_[k] << " ";
37 cout << endl;
38 cout << " *** variables float " << endl;
39 for (k=0; k < fdata_.size(); k++) cout << fdata_[k] << " ";
40 cout << endl;
41 cout << " *** variables int " << endl;
42 for (k=0; k < idata_.size(); k++) cout << idata_[k] << " ";
43 cout << endl;
44 cout << " *** variables string " << endl;
45 for (k=0; k < cdata_.size(); k++) cout << cdata_[k] << " ";
46 cout << endl;
47 cout << " ***************************** " << endl;
48 }
49 vector<double> ddata_;
50 vector<float> fdata_;
51 vector<int> idata_;
52 vector<string> cdata_;
53 vector<string> ColName_;
54
55 };
56
57 class FitsFile;
58 class FitsInFile;
59 class FitsOutFile;
60 enum WriteMode {append, clear, unknown};
61
62
63//
64//! Class for managing Interface for SOPHYA objects to FITS Format Files (uses cfitsio lib)
65
66/*!
67The class structure is analogous to Sophya-PPersist system :
68Each SOPHYA object XXX is associated with a object of class FITS_XXX
69 (inheriting from FitsFileHandler), to which input/output operations with FITS
70 files are delegated (through a class Hierarchy : FitsFile (virtual),
71 FitsInFile, FitsOutFile) . A typical example of use is the following :
72
73\verbatim
74 int m=... ;
75 SphereHEALPix<r_8> sphere1(m); // definition of the SOPHYA object
76 .... fill the sphere ....
77
78 FITS_SphereHEALPix<r_8> fits_sph1(sphere1);
79 // delegated object
80 fits_sph.Write("myfile.fits"); // writing on FITS file
81
82 FITS_SphereHEALPix<r_8> fits_sph2("myfile.fits");
83 // load a delegated object
84 // from FITS file
85 SphereHEALPix<r_8> sphere2=(SphereHEALPix<r_8>)fits_sph2;
86 // casting the delegated object
87 // into a SOPHYA object
88\endverbatim
89
90*/
91
92class FitsIOHandler {
93
94
95 public:
96
97virtual ~FitsIOHandler() {}
98/*!
99this method is called from inherited objects :
100
101opens a file 'flnm'
102
103gets parameters in extension-header (hdunum)
104
105calls the method 'ReadFromFits' from the inherited object
106
107
108*/
109 void Read(char flnm[],int hdunum= 0);
110/*!
111this method is called from inherited objects :
112
113for writing a new object in a new fits-extension :
114
115???
116
117 at the end of
118
119the existing file (flnm), if OldFile=true.
120
121If OldFile=false, an exception occurs
122
123By convention, primary header does not contain fits-image data : i.e.
124all data are fits-extensions. The first relevant header will have hdunum=2.
125For switching off this convention use the method :
126
127firstImageOnPrimaryHeader() (see below)
128
129In that case do not forget to precise hdunum=1 when reading data on primary header.
130
131calls the method 'WriteToFits' from the inherited object
132
133*/
134 void Write(char flnm[]) ;
135
136 /*!
137Read the data on extension hdunum (or primary header, if hdunum=1) from FitsInFIle. With default value for hdunum, one reads the next extension, with respect to the current position.
138 */
139 void Read(FitsInFile& ifts, int hdunum=0);
140 void Write(FitsOutFile& ofts) ;
141
142
143 protected:
144 virtual void ReadFromFits(FitsInFile& is)=0;
145 virtual void WriteToFits(FitsOutFile& os) =0;
146 friend class FitsInFile;
147 friend class FitsOutFile;
148 private :
149 };
150
151
152
153class FitsFile
154{
155
156public:
157
158FitsFile()
159{
160 InitNull();
161};
162 virtual ~FitsFile();
163
164 static string getErrStatus(int status);
165
166
167
168
169inline int statusF() const { return fits_status_;}
170
171
172protected:
173
174 void ResetStatus(int& status) ;
175 static void printerror(int&) ;
176 static void printerror(int&,char* texte) ;
177 inline void InitNull() {fptr_ = NULL; hdutype_= 0; hdunum_ = 1;
178 fits_status_ = 0;}
179
180 //! pointer to the FITS file, defined in fitsio.h
181 fitsfile *fptr_;
182
183 //! image or bintable ?
184 int hdutype_;
185
186//! index of header to be read/written
187 int hdunum_;
188
189 //! last status returned by fitsio library. updated only by several methods
190 int fits_status_;
191
192
193};
194
195
196 class FitsInFile : public FitsFile {
197
198 public:
199 FitsInFile();
200 // FitsInFile(char flnm[], int hdunum=0);
201 FitsInFile(char flnm[]);
202 ~FitsInFile() { ; };
203
204
205//////////////////////////////////////////////////////////
206// methods with general purpose
207///////////////////////////////////////
208
209
210
211 static int NbBlocks(char flnm[]);
212 static void getBlockType(char flnm[], int hdunum, string& typeOfExtension, int& naxis, vector<int>& naxisn, string& dataType, DVList& dvl );
213
214
215
216 // void ReadFInit(char flnm[],int hdunum=0);
217 void ReadFInit(int hdunum);
218
219 /*! return a reference on a DVList containing the keywords from FITS file
220 */
221inline const DVList& DVListFromFits() const { return dvl_;}
222
223/* get the keywords of primary header in a DVList */
224DVList DVListFromPrimaryHeader() const;
225
226void moveToFollowingHeader();
227
228
229 //////////////////////////////////////////////////////////
230 /////// methods for managing extensions ////////////////
231 //////////////////////////////////////////////////////////
232
233
234
235/////////////////////////////////////////////////////////////
236// methods for managing FITS IMAGE extension
237///////////////////////////////////////////////////
238
239
240/*! return true if the current header corresponds to a FITS image extension */
241inline bool IsFitsImage() const { return (hdutype_ == IMAGE_HDU);}
242
243
244
245 /*! number of dimensions of an image extension : NAXIS parameter (in FITS notations)
246 */
247inline int nbDimOfImage() const {return naxis_;}
248/*! a reference on a vector containing sizes of the NAXIS dimensions : NAXIS1, NAXIS2, NAXIS3 wtc.
249 */
250 inline const vector<int>& dimOfImageAxes() const { return naxisn_;}
251/*!
252 total number of data in the current IMAGE extension
253 */
254inline int nbOfImageData() const { return nbData_; }
255
256
257
258//////////////////////////////////////////////////////////////////////////
259// methods for managing FITS BINARY TABLE or ASCII TABLE extension
260////////////////////////////////////////////////////////////////////////
261
262
263
264
265/*! return true if the current header corresponds to a FITS ASCII or BINTABLE extension */
266inline bool IsFitsTable() const {return (hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL);}
267
268
269static void GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
270 vector<int>& repeat,
271 vector<string>& noms,
272 vector<char>& types,
273 vector<int>& taille_des_chaines);
274
275
276
277 /*! return a character denoting data type of column number 'nocol' in a BINTABLE :
278
279D : double
280
281E : float
282
283I : integer
284
285S : character string
286
287 */
288 char ColTypeFromFits(int nocol) const;
289 /*! name of the column number 'nocol' of the current BINTABLE extension
290 */
291 string ColNameFromFits(int nocol) const;
292
293 /*! number of characters of each data for the column number 'nocol' (if char* typed) of the current BINTABLE extension
294 */
295 int ColStringLengthFromFits(int nocol) const;
296
297
298
299 /*!
300get the NoLine-th 'line' from the current BINTABLE extension on FITS file,
301 */
302 void GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char
303** cdata) ;
304 /*!
305get the NoLine-th 'line' from the current BINTABLE extension on FITS file,
306 */
307 void GetBinTabLine(long NoLine, BnTblLine& ligne) ;
308
309 /*!
310get the NoLine-th 'line' from the current BINTABLE extension on FITS file,
311 */
312 void GetBinTabLine(int NoLine, float* fdata) ;
313
314/*!
315fill the array 'valeurs' with double data from the current BINTABLE extension on FITS file, from column number 'NoCol'
316
317\param <nentries> number of data to be read
318 */
319 void GetBinTabFCol(double* valeurs, int nentries, int NoCol) const;
320
321 /*! same as previous method with float data */
322 void GetBinTabFCol(float* valeurs, int nentries, int NoCol) const;
323 /*! same as previous method with int data */
324 void GetBinTabFCol(int* valeurs, int nentries, int NoCol) const;
325 /*! same as previous method with char* data */
326 void GetBinTabFCol(char** valeurs,int nentries, int NoCol) const;
327 // Write elements into the FITS data array
328
329/////////////////////////////////////////////////////////////
330// methods for managing any type of FITS extension
331////////////////////////////////////////////////////////
332
333 /*! return number of columns (return 1 if IMAGE) */
334 int NbColsFromFits() const;
335 /*! number of data in the current IMAGE extension on FITS file, or number
336 of data of column number 'nocol' of the current BINTABLE extension
337 */
338 int NentriesFromFits(int nocol) const;
339
340
341/*!
342fill the array 'map' with double data from the current extension on FITS file.
343If the extension is BINTABLE, the first column is provided.
344
345\param <nentries> number of data to be read
346 */
347 void GetSingleColumn(double* map, int nentries) const;
348
349 /*! same as above with float data */
350 void GetSingleColumn(float* map, int nentries) const;
351
352 /*! same as above with int data */
353 void GetSingleColumn(int* map, int nentries) const;
354
355 private :
356
357void InitNull();
358static void KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum);
359static void GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn);
360
361
362
363
364 //! fits-Image parameter
365 int bitpix_;
366
367 //! fits-Image parameter
368 int naxis_;
369
370 //! fits-Image parameters : sizes of dimensions
371 vector<int> naxisn_;
372
373 //! fits-Image parameter: number of data
374 int nbData_;
375
376 //! Bintable parameter
377 int nrows_;
378
379 //! Bintable parameter
380 vector<int> repeat_;
381
382 //! Bintable parameter
383 int nbcols_;
384
385 //! Bintable parameter: column names
386 vector<string> noms_;
387
388 //! Bintable parameters: types of columns (D: double, E: float, I: integers, A: char*)
389 vector<char> types_;
390
391 //! Bintable parameters: length of the char* variables
392 vector<int> taille_des_chaines_;
393
394 //! DVList for transferring keywords
395 DVList dvl_;
396
397
398 };
399
400
401 class FitsOutFile : public FitsFile {
402
403 public:
404
405
406 FitsOutFile();
407 /*!
408\param <WriteMode> enum , WriteMode = clear -> if alreadyy exists, the file will be overwritten (else created) ; WriteMode = append -> further objects will be appended to the file if it exists (else : file created). WriteMode = unknown -> file created if does not exist, else : exception. (the last situation is the default)
409
410 */
411 FitsOutFile(char flnm[], WriteMode wrm = unknown );
412 ~FitsOutFile() { ;};
413 inline void InitNull() {imageOnPrimary_=false;}
414
415 //////////////////////////////////////////////////////////
416 /////// methods for managing extensions ////////////////
417 //////////////////////////////////////////////////////////
418
419
420
421/////////////////////////////////////////////////////////////
422// methods for managing FITS IMAGE extension
423///////////////////////////////////////////////////
424
425
426 inline void firstImageOnPrimaryHeader() {imageOnPrimary_=true;}
427
428 /*! create an IMAGE header on FITS file.
429\param <type> type of data (see method ColTypeFromFits)
430\param <nbdim> number of dimensions : 1D, 2D, 3D etc. = NAXIS
431\param <naxisn> array containind sizes of the different dimensions
432 */
433 void makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList &dvl) ;
434
435
436 /*! write double data from array 'map'on an IMAGE extension
437\param <nbData> number of data to be written
438
439 */
440 void putImageToFits( int nbData, double* map) const;
441
442 /*! same as previous method with float data */
443 void putImageToFits(int nbData, float* map ) const;
444
445 /*! same as previous method with int data */
446 void putImageToFits(int nbData, int* map) const;
447
448
449
450//////////////////////////////////////////////////////////////////////////
451// methods for managing FITS BINARY TABLE or ASCII TABLE extension
452////////////////////////////////////////////////////////////////////////
453
454
455
456 /*! create an BINTABLE header on FITS file.
457\param <fieldType> array conta
458ining characters denoting types of the different column (see method ColTypeFromFits)
459\param <Noms> array of the names of columns
460\param <nentries> number of data of each column
461\param <tfields> number of columns
462\param <dvl> a SOPHYA DVList containing keywords to be appended
463\param <extname> keyword EXTNAME for FITS file
464\param <taille_des_chaines> vector containing the number of characters of data for each char* typed column, with order of appearance in 'fieldType'
465 */
466 void makeHeaderBntblOnFits ( string fieldType, vector<string> Noms, int nentries, int tfields, DVList &dvl, string extname, vector<int> taille_des_chaines) ;
467
468 /*! write double data from array 'donnees ' on column number 'nocol' of a BINTABLE extension.
469\param <nentries> number of data to be written
470
471 */
472 void putColToFits(int nocol, int nentries, double* donnees) const;
473
474 /*! same as previous method with float data */
475 void putColToFits(int nocol, int nentries, float* donnees) const;
476
477 /*! same as previous method with int data */
478 void putColToFits(int nocol, int nentries, int* donnees) const;
479
480 /*! same as previous method with char* data */
481 void putColToFits(int nocol, int nentries, char** donnees) const;
482
483 void putBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char ** cdata) const;
484
485
486/////////////////////////////////////////////////////////////
487// methods for managing any type of FITS extension
488////////////////////////////////////////////////////////
489
490
491/* put keywords from a DVList into the primary header of the fits-file */
492void DVListIntoPrimaryHeader(DVList& dvl) const;
493
494
495
496 private :
497
498 void writeSignatureOnFits() const;
499 void addKeywordsOfDVList(DVList& dvl) const;
500
501 bool imageOnPrimary_;
502
503 };
504
505
506
507} // Fin du namespace
508
509
510#endif
Note: See TracBrowser for help on using the repository browser.