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

Last change on this file since 1213 was 1209, checked in by ansari, 25 years ago

ecriture ligne a ligne

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