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
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
[875]12namespace SOPHYA {
13
[1193]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
[1136]57 class FitsFile;
58 class FitsInFile;
59 class FitsOutFile;
[1193]60 enum WriteMode {append, clear, unknown};
[1136]61
62
[903]63//
[1136]64//! Class for managing Interface for SOPHYA objects to FITS Format Files (uses cfitsio lib)
[875]65
[903]66/*!
[1136]67The class structure is analogous to Sophya-PPersist system :
[903]68Each SOPHYA object XXX is associated with a object of class FITS_XXX
[1136]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 :
[903]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*/
[839]91
[1136]92class FitsIOHandler {
[839]93
94
[1136]95 public:
96
97virtual ~FitsIOHandler() {}
[903]98/*!
99this method is called from inherited objects :
100
[1136]101opens a file 'flnm'
[903]102
[1143]103gets parameters in extension-header (hdunum)
[903]104
[1047]105calls the method 'ReadFromFits' from the inherited object
[903]106
[1136]107
[903]108*/
[1175]109 void Read(char flnm[],int hdunum= 0);
[903]110/*!
111this method is called from inherited objects :
112
[1183]113for writing a new object in a new fits-extension :
[903]114
[1183]115???
116
117 at the end of
118
[1136]119the existing file (flnm), if OldFile=true.
[903]120
[1136]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 :
[903]126
[1136]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
[903]133*/
[1193]134 void Write(char flnm[]) ;
[903]135
[1136]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) ;
[903]141
142
[1136]143 protected:
144 virtual void ReadFromFits(FitsInFile& is)=0;
145 virtual void WriteToFits(FitsOutFile& os) =0;
146 friend class FitsInFile;
147 friend class FitsOutFile;
[1193]148 private :
[1136]149 };
150
151
152
153class FitsFile
154{
155
156public:
157
[1175]158FitsFile()
159{
160 InitNull();
161};
[1136]162 virtual ~FitsFile();
163
[1047]164 static string getErrStatus(int status);
[903]165
166
[1047]167
[1136]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) ;
[1175]177 inline void InitNull() {fptr_ = NULL; hdutype_= 0; hdunum_ = 1;
[1136]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[]);
[1143]202 ~FitsInFile() { ; };
[1136]203
204
[1047]205//////////////////////////////////////////////////////////
[1136]206// methods with general purpose
207///////////////////////////////////////
[1047]208
[1136]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
[1047]219 /*! return a reference on a DVList containing the keywords from FITS file
220 */
[1136]221inline const DVList& DVListFromFits() const { return dvl_;}
[1047]222
[1143]223/* get the keywords of primary header in a DVList */
224DVList DVListFromPrimaryHeader() const;
225
[1136]226void moveToFollowingHeader();
[1047]227
228
[1136]229 //////////////////////////////////////////////////////////
230 /////// methods for managing extensions ////////////////
231 //////////////////////////////////////////////////////////
232
233
234
[1047]235/////////////////////////////////////////////////////////////
[1136]236// methods for managing FITS IMAGE extension
237///////////////////////////////////////////////////
[1047]238
239
240/*! return true if the current header corresponds to a FITS image extension */
241inline bool IsFitsImage() const { return (hdutype_ == IMAGE_HDU);}
242
[1136]243
244
[903]245 /*! number of dimensions of an image extension : NAXIS parameter (in FITS notations)
246 */
[861]247inline int nbDimOfImage() const {return naxis_;}
[1047]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_;}
[903]251/*!
252 total number of data in the current IMAGE extension
253 */
[839]254inline int nbOfImageData() const { return nbData_; }
[903]255
256
[1047]257
[1136]258//////////////////////////////////////////////////////////////////////////
259// methods for managing FITS BINARY TABLE or ASCII TABLE extension
260////////////////////////////////////////////////////////////////////////
[1047]261
262
263
264
[1136]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);}
[1047]267
268
[1136]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);
[1047]274
275
276
[903]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 */
[839]288 char ColTypeFromFits(int nocol) const;
[903]289 /*! name of the column number 'nocol' of the current BINTABLE extension
290 */
[839]291 string ColNameFromFits(int nocol) const;
[903]292
293 /*! number of characters of each data for the column number 'nocol' (if char* typed) of the current BINTABLE extension
294 */
[839]295 int ColStringLengthFromFits(int nocol) const;
[903]296
297
298
299 /*!
[1047]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) ;
[1193]304 /*!
305get the NoLine-th 'line' from the current BINTABLE extension on FITS file,
306 */
307 void GetBinTabLine(long NoLine, BnTblLine& ligne) ;
[1047]308
309 /*!
310get the NoLine-th 'line' from the current BINTABLE extension on FITS file,
311 */
312 void GetBinTabLine(int NoLine, float* fdata) ;
313
[1136]314/*!
[903]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 */
[839]319 void GetBinTabFCol(double* valeurs, int nentries, int NoCol) const;
[903]320
321 /*! same as previous method with float data */
[839]322 void GetBinTabFCol(float* valeurs, int nentries, int NoCol) const;
[903]323 /*! same as previous method with int data */
[839]324 void GetBinTabFCol(int* valeurs, int nentries, int NoCol) const;
[903]325 /*! same as previous method with char* data */
[839]326 void GetBinTabFCol(char** valeurs,int nentries, int NoCol) const;
327 // Write elements into the FITS data array
[903]328
[1136]329/////////////////////////////////////////////////////////////
330// methods for managing any type of FITS extension
331////////////////////////////////////////////////////////
[1045]332
[1136]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;
[1045]339
340
[1136]341/*!
[1047]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
[903]346 */
[1047]347 void GetSingleColumn(double* map, int nentries) const;
[903]348
[1047]349 /*! same as above with float data */
350 void GetSingleColumn(float* map, int nentries) const;
[839]351
[1047]352 /*! same as above with int data */
353 void GetSingleColumn(int* map, int nentries) const;
354
[1136]355 private :
[1047]356
[1136]357void InitNull();
358static void KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum);
[903]359static void GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn);
[839]360
361
[1136]362
[839]363
[903]364 //! fits-Image parameter
[839]365 int bitpix_;
[903]366
367 //! fits-Image parameter
[839]368 int naxis_;
[903]369
370 //! fits-Image parameters : sizes of dimensions
[861]371 vector<int> naxisn_;
[903]372
373 //! fits-Image parameter: number of data
[839]374 int nbData_;
375
[903]376 //! Bintable parameter
[839]377 int nrows_;
[903]378
379 //! Bintable parameter
[839]380 vector<int> repeat_;
381
[903]382 //! Bintable parameter
[839]383 int nbcols_;
384
[903]385 //! Bintable parameter: column names
386 vector<string> noms_;
[1136]387
[903]388 //! Bintable parameters: types of columns (D: double, E: float, I: integers, A: char*)
389 vector<char> types_;
[1136]390
[903]391 //! Bintable parameters: length of the char* variables
392 vector<int> taille_des_chaines_;
[839]393
[903]394 //! DVList for transferring keywords
[839]395 DVList dvl_;
[1045]396
[1047]397
[1136]398 };
[1047]399
[875]400
[1136]401 class FitsOutFile : public FitsFile {
[875]402
[1136]403 public:
[1193]404
405
[1136]406 FitsOutFile();
[1193]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 );
[1143]412 ~FitsOutFile() { ;};
[1136]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 */
[1143]433 void makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList &dvl) ;
[1136]434
[1143]435
[1136]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.
[1143]457\param <fieldType> array conta
458ining characters denoting types of the different column (see method ColTypeFromFits)
[1136]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 */
[1193]466 void makeHeaderBntblOnFits ( string fieldType, vector<string> Noms, int nentries, int tfields, DVList &dvl, string extname, vector<int> taille_des_chaines) ;
[1136]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
[1193]483 void putBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char ** cdata) const;
484
485
[1143]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
[1136]496 private :
497
498 void writeSignatureOnFits() const;
[1143]499 void addKeywordsOfDVList(DVList& dvl) const;
[1136]500
501 bool imageOnPrimary_;
502
503 };
504
505
506
[875]507} // Fin du namespace
508
509
[839]510#endif
Note: See TracBrowser for help on using the repository browser.