source: Sophya/trunk/SophyaExt/FitsIOServer/fitsfile.cc@ 1353

Last change on this file since 1353 was 1353, checked in by lemeur, 25 years ago

fonction de manipulation directe de headers

File size: 57.3 KB
RevLine 
[949]1#include "machdefs.h"
2#include <stdlib.h>
[839]3#include "fitsfile.h"
4#include "pexceptions.h"
5#include "strutil.h"
[903]6#include "anydataobj.h"
7#include "fitsspherehealpix.h"
[1136]8
9
[1218]10void BnTblLine::setFormat(int dc, int fc, int ic, int cc, vector<string> names)
11 {
12 int nbcols = dc + fc + ic + cc;
13 int maxName = names.size();
14 if (nbcols != maxName)
15 {
16 cout << " WARNING: BnTblLine:: length of vector of column names not equal to total number of columns" << endl;
17 maxName = nbcols < maxName ? nbcols : maxName;
18 }
19 ColName_ = vector<string>(nbcols);
20 for (int k=0; k < maxName; k++) ColName_[k] = names[k];
21 if (dc >0) ddata_ = vector<double>(dc);
22 if (fc >0) fdata_ = vector<float>(fc);
23 if (ic >0) idata_ = vector<int>(fc);
24 if (cc >0) cdata_ = vector<string>(fc);
25 }
26
27bool BnTblLine::sameFormat(const BnTblLine& btl) const
28 {
29 if (btl.ddata_.size() == ddata_.size() && btl.fdata_.size() == fdata_.size() && btl.idata_.size() == idata_.size() && btl.cdata_.size() == cdata_.size()) return true;
30 else return false;
31 }
32
33void BnTblLine::Print()
34 {
35 int k;
36 cout << " ********* ligne ************* " << endl;
37 cout << " *** noms de variables " << endl;
38 for (k=0; k < ColName_.size(); k++) cout << ColName_[k] << " ";
39 cout << endl;
40 cout << " *** variables doubles " << endl;
41 for (k=0; k < ddata_.size(); k++) cout << ddata_[k] << " ";
42 cout << endl;
43 cout << " *** variables float " << endl;
44 for (k=0; k < fdata_.size(); k++) cout << fdata_[k] << " ";
45 cout << endl;
46 cout << " *** variables int " << endl;
47 for (k=0; k < idata_.size(); k++) cout << idata_[k] << " ";
48 cout << endl;
49 cout << " *** variables string " << endl;
50 for (k=0; k < cdata_.size(); k++) cout << cdata_[k] << " ";
51 cout << endl;
52 cout << " ***************************** " << endl;
53 }
54
55
56
57/*!
58 \class SOPHYA::FitsIOHandler
59The class structure is analogous to Sophya-PPersist system :
60Each SOPHYA object XXX is associated with a object of class FITS_XXX
61 (inheriting from FitsFileHandler), to which input/output operations with FITS
62 files are delegated (through a class Hierarchy : FitsFile (virtual),
63 FitsInFile, FitsOutFile) . A typical example of use is the following :
64
65\verbatim
66 int m=... ;
67 SphereHEALPix<r_8> sphere1(m); // definition of the SOPHYA object
68 .... fill the sphere ....
69
70 FITS_SphereHEALPix<r_8> fits_sph1(sphere1);
71 // delegated object
72 fits_sph.Write("myfile.fits"); // writing on FITS file
73
74 FITS_SphereHEALPix<r_8> fits_sph2("myfile.fits");
75 // load a delegated object
76 // from FITS file
77 SphereHEALPix<r_8> sphere2=(SphereHEALPix<r_8>)fits_sph2;
78 // casting the delegated object
79 // into a SOPHYA object
80\endverbatim
81
82
83*/
84
85/*! \fn void SOPHYA::FitsIOHandler::Read(char flnm[],int hdunum)
86
87this method is called from inherited objects :
88
89opens a file 'flnm'
90
91gets parameters in extension-header (hdunum)
92
93calls the method 'ReadFromFits' from the inherited object
94*/
[1136]95void FitsIOHandler::Read(char flnm[],int hdunum)
[839]96{
[1136]97 FitsInFile ifts(flnm);
98 Read(ifts, hdunum);
[839]99}
[1218]100
101 /*! \fn void SOPHYA::FitsIOHandler::Read(FitsInFile& is, int hdunum)
102Read the data on extension hdunum (or primary header, if hdunum=1) from FitsInFIle. If hdunum is not addressed, , one reads the next extension, with respect to the current position.
103 */
[1136]104void FitsIOHandler::Read(FitsInFile& is, int hdunum)
105{
[1300]106 is.ReadHeader(hdunum);
[1136]107 ReadFromFits(is);
108}
109
110
[1218]111/*! \fn void SOPHYA::FitsIOHandler::Write(char flnm[])
112this method is called from inherited objects.
113
114for writing a new object in a new fits-extension :
115
[1234]116\warning By convention, primary header may contain fits-image data.
117For switching off this convention (i.e. to make sure that all data will be on fits-extensions) use the method :
[1218]118
119firstImageOnPrimaryHeader() (see below)
120
121calls the method 'WriteToFits' from the inherited object
122
123*/
[1193]124void FitsIOHandler::Write(char flnm[])
[1136]125
126{
[1231]127 FitsOutFile of(flnm, FitsFile::unknown);
[1136]128 Write(of);
129}
130
131void FitsIOHandler::Write(FitsOutFile& os)
132{
133 WriteToFits(os);
134}
135
136
[1218]137/*!
138 \class SOPHYA::FitsIOHandler
139Class (virtual) for managing FITS format files
140*/
[1136]141
[1218]142
[839]143
144FitsFile::~FitsFile()
145{
146 int status = 0;
[1175]147 if( fptr_ != NULL)
[903]148 {
149 fits_close_file(fptr_,&status);
[1175]150 // je ne fais pas delete fptr_, c'est la lib. fitsio qui a fait
151 // new...
[903]152 }
[1175]153 if( status ) printerror( status );
[839]154}
[903]155
[1136]156
157void FitsFile::printerror(int &status)
158 //*****************************************************/
159 //* Print out cfitsio error messages and exit program */
160 //*****************************************************/
[1045]161{
[1136]162 if( status )
163 {
164 fits_report_error(stderr,status);
165 throw IOExc("FitsFile:: error FITSIO status");
166 }
167 return;
168}
[903]169
[1136]170void FitsFile::printerror(int& status, char* texte)
171 //*****************************************************/
172 //* Print out cfitsio error messages and exit program */
173 //*****************************************************/
174{
175 // print out cfitsio error messages and exit program
176 // print error report
[1235]177 fits_report_error(stderr, status);
[1136]178 cout << " erreur:: " << texte << endl;
179 throw IOExc("FitsFile:: error FITSIO status");
180}
181
182void FitsFile::ResetStatus(int& status)
183{
184 fits_status_ = status;
185 status = 0;
[1235]186 fits_clear_errmsg();
[1136]187}
188
[1209]189string FitsFile::GetErrStatus(int status)
[1136]190{
191 char text[31];
192 fits_get_errstatus(status, text);
193 return string(text);
194}
195
[1218]196/*!
197 \class SOPHYA::FitsInFile
198
[1246]199class for reading SOPHYA objects from FITS Format Files (uses cfitsio lib)
[1218]200*/
201
[1136]202FitsInFile::FitsInFile()
203{
204 InitNull();
205}
[1218]206
[1231]207FitsInFile::FitsInFile(string const & flnm)
[1136]208{
[1175]209 InitNull();
210 int status = 0;
[1231]211 fits_open_file(&fptr_,flnm.c_str(),READONLY,&status);
212 if( status ) printerror( status );
213}
214
215FitsInFile::FitsInFile(const char * flnm)
216{
217 InitNull();
218 int status = 0;
[1175]219 fits_open_file(&fptr_,flnm,READONLY,&status);
220 if( status ) printerror( status );
[1136]221}
222
223
224void FitsInFile::InitNull()
225{
[1300]226 imageDataType_ = FitsDataType_NULL;
[1045]227 naxis_ = 0;
228 nbData_ = 0;
229 nrows_ = 0;
230 nbcols_ = 0;
231 naxisn_.clear();
232 repeat_.clear();
233 noms_.clear();
234 taille_des_chaines_.clear();
235 dvl_.Clear();
[1175]236
[1045]237}
238
[1218]239//////////////////////////////////////////////////////////
240// methods with general purpose
241/////////////////////////////////////////////////////////
[1045]242
[1136]243int FitsInFile::NbBlocks(char flnm[])
[903]244{
245 int status = 0;
246 int nbhdu = 0;
247 fitsfile* fileptr;
248 fits_open_file(&fileptr,flnm,READONLY,&status);
249 if( status ) printerror( status, "NbBlocks: erreur ouverture fichier" );
250 fits_get_num_hdus(fileptr, &nbhdu, &status);
251 fits_close_file(fileptr,&status);
252 return nbhdu;
253}
[1334]254int FitsInFile::NbBlocks()
255{
256 int status = 0;
257 int nbhdu = 0;
258 fits_get_num_hdus(fptr_, &nbhdu, &status);
259 return nbhdu;
260}
[903]261
[1231]262void FitsInFile::GetBlockType(char flnm[], int hdunum, FitsExtensionType& typeOfExtension, int& naxis, vector<int>& naxisn, FitsDataType& dataType, DVList& dvl )
[903]263{
264 int status = 0;
265 fitsfile* fileptr;
266 fits_open_file(&fileptr,flnm,READONLY,&status);
[1209]267 if( status ) printerror( status, "GetBlockType: erreur ouverture fichier" );
[903]268 // move to the specified HDU number
269 int hdutype = 0;
270 fits_movabs_hdu(fileptr,hdunum,&hdutype,&status);
[1209]271 if( status ) printerror( status,"GetBlockType: erreur movabs");
[903]272 if(hdutype == IMAGE_HDU)
273 {
[1231]274 typeOfExtension = FitsExtensionType_IMAGE;
[1300]275 GetImageParameters (fileptr, dataType, naxis, naxisn);
[903]276 }
277 else
278 if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
279 {
280 int nrows = 0;
281 vector<string> noms;
[1300]282 vector<FitsDataType> types;
[903]283 vector<int> taille_des_chaines;
[971]284 GetBinTabParameters(fileptr, naxis, nrows, naxisn, noms, types, taille_des_chaines);
285 int k;
286 for (k=0; k< naxisn.size(); k++) naxisn[k] *= nrows;
[903]287 if(hdutype == ASCII_TBL)
288 {
[1231]289 typeOfExtension = FitsExtensionType_ASCII_TBL;
290 dataType = FitsDataType_ASCII;
[903]291 }
292 else
293 {
[1231]294 typeOfExtension = FitsExtensionType_BINARY_TBL;
[1300]295 dataType = types[0];
[903]296 }
297 }
298 else
299 {
300 cout << " hdutype= " << hdutype << endl;
[1209]301 throw IOExc("FitsFile::GetBlockType: this HDU type is unknown");
[903]302 }
303
[971]304 KeywordsIntoDVList(fileptr, dvl, hdunum);
[903]305 fits_close_file(fileptr,&status);
306}
307
[1136]308
[1300]309void FitsInFile::ReadHeader(int hdunum)
[1045]310{
[1300]311 // InitNull();
[1045]312 int status = 0;
[1334]313 if (hdunum<0)
314 {
315 throw PException(" FITS_AutoReader::ReadObject : hdu number must be not negative");
316 }
[1234]317 if (hdunum != 0 ) hdunum_ = hdunum;
318
319 // si le numero de header non precise
320 else
[1045]321 {
[1234]322 // si c'est le premier objet a lire
323 if (hdunum_ == 0)
[1045]324 {
[1234]325 // on calcule le numero de header a lire
[1246]326 if (imageOnPrimary_ == true ) hdunum_ = 1;
[1234]327 else hdunum_ = 2;
[1045]328 }
[1234]329 // sinon objet suivant
330 else hdunum_++;
[1045]331 }
[1234]332 getHeader();
[1334]333 if ( hdutype_ == FitsExtensionType_NULL )
334 {
335 if (hdunum == 0 && hdunum_ == 1)
336 {
337 hdunum_++;
338 getHeader();
339 }
340 else
341 {
342 cout << " WARNING (FitsInFile::ReadHeader) : no SOPHYA object on HDU number : " << hdunum_ << endl;
343 }
344 }
[1045]345}
346
[1353]347bool FitsInFile::hasKeyword(string keyw, int hdunum)
348 {
349 bool has=false;
350 int status = 0;
351 if (hdunum != hdunum_ )
352 {
353 int hdutype;
354 fits_movabs_hdu(fptr_,hdunum,&hdutype,&status);
355 }
[1218]356
[1353]357 char value[FLEN_VALUE];
358 char* keyname= const_cast<char*>(keyw.c_str());
359 fits_read_keyword(fptr_,keyname,value,NULL,&status);
360 if (status == 0)
361 has = true;
362 else
363 if (status == KEY_NO_EXIST ) status =0;
364 else fits_report_error(stderr,status);
365 if (hdunum != hdunum_ )
366 {
367 int hdutype;
368 if (hdunum_ != 0)
369 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
370 else fits_movabs_hdu(fptr_,1,&hdutype,&status);
371
372 }
373 return has;
374 }
375
[1300]376void FitsInFile::GetImageParameters (fitsfile* fileptr,FitsDataType& dataType,int& naxis,vector<int>& naxisn)
[1218]377{
378 int hdunum=0;
[1334]379 // cout << " Reading a FITS image in HDU : " << fits_get_hdu_num(fileptr,&hdunum) << endl;
[1218]380 int status= 0;
381
382 // bits per pixels
[1300]383 int bitpix=0;
[1218]384 fits_read_key(fileptr,TINT,"BITPIX",&bitpix,NULL,&status);
385 if( status ) printerror( status );
[1300]386 if(bitpix == DOUBLE_IMG) dataType = FitsDataType_double;
387 else if(bitpix == FLOAT_IMG) dataType = FitsDataType_float;
388 else if(bitpix == LONG_IMG || bitpix == SHORT_IMG ) dataType = FitsDataType_int;
389 else if (bitpix == BYTE_IMG) dataType = FitsDataType_char;
390 else
391 {
392 cout << " bitpix= " << bitpix << endl;
393 throw PException(" FitsFile::GetImageParameters : unsupported FITS data type");
394 }
[1218]395
396 // number of dimensions in the FITS array
397 naxis= 0;
398 fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
399 if( status ) printerror( status );
400 // read the NAXISn keywords to get image size
401 long* naxes = new long[naxis] ;
402 int nfound;
403 fits_read_keys_lng(fileptr,"NAXIS",1,naxis,naxes,&nfound,&status);
404 if( status ) printerror( status );
405 if (nfound != naxis )
406 cout << " WARNING : " << nfound << " axes found, expected naxis= " << naxis << endl;
407 int k;
408 for (k=0; k<naxis; k++)
409 {
410 naxisn.push_back( (int)naxes[k] );
411 }
412 delete [] naxes;
413}
414
415
416
417
418 /*! \fn DVList SOPHYA::FitsInFile::DVListFromPrimaryHeader() const
419
420 \return the keywords of primary header in a DVList
421
422*/
[1143]423DVList FitsInFile::DVListFromPrimaryHeader() const
424 {
425 int status;
426 DVList dvl;
427 KeywordsIntoDVList(fptr_, dvl, 1);
428 int hdutype = 0;
429 if (hdunum_ > 0) fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
430 return dvl;
431 }
[1136]432
[1234]433void FitsInFile::getHeader()
[971]434{
[1300]435 // si hdunum_ > 1 lit le header correspondant
436 // si hdunum_ = 1 se positionne au (et lit le) premier header qui
437 // contient reellement un objet
[1234]438 int status=0;
439 if (hdunum_ < 1) throw PException(" attempt to read hdunum < 1");
[1300]440 InitNull();
[1234]441 if (hdunum_ == 1)
[839]442 {
[1234]443 // presence of image ?
444 int naxis= 0;
445 fits_read_key(fptr_,TINT,"NAXIS",&naxis,NULL,&status);
446 if( status ) printerror( status );
447 if (naxis > 0 ) // there is an image
448 {
[1334]449 hdutype_ = FitsExtensionType_IMAGE;
[1300]450 GetImageParameters (fptr_, imageDataType_, naxis_, naxisn_);
[1234]451 nbData_ = 1;
452 int k;
453 for (k=0; k<naxis_; k++) if (naxisn_[k] > 0) nbData_ *= naxisn_[k];
454 KeywordsIntoDVList(fptr_, dvl_,hdunum_);
455 }
456 else
457 {
[1334]458 hdutype_ = FitsExtensionType_NULL;
459 KeywordsIntoDVList(fptr_, dvl_,hdunum_);
[1234]460 }
[839]461 }
[1234]462 else
[839]463 {
[1234]464 int hdutype;
465 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
[1334]466
467 if( status )
[1234]468 {
[1334]469 if (status == END_OF_FILE)
470 {
471 hdutype_= FitsExtensionType_EOF;
472 status =0;
473 return;
474 }
475 else
476 {
477 cout << "WARNING (FitsInFile::getHeader) : error during movabs" << endl;
478 hdutype_= FitsExtensionType_ERROR;
479 status =0;
480 return;
481 }
482 // printerror( status,":FitsInFile::getHeader : erreur movabs");
483 }
484 if(hdutype == IMAGE_HDU)
485 {
486 hdutype_= FitsExtensionType_IMAGE;
[1300]487 GetImageParameters (fptr_, imageDataType_, naxis_, naxisn_);
[1234]488 nbData_ = 1;
489 int k;
490 for (k=0; k<naxis_; k++) if (naxisn_[k] > 0) nbData_ *= naxisn_[k];
491 KeywordsIntoDVList(fptr_, dvl_,hdunum_);
492 }
[1334]493 else if(hdutype == ASCII_TBL)
[1234]494 {
[1334]495 hdutype_= FitsExtensionType_ASCII_TBL;
[1234]496 GetBinTabParameters(fptr_,nbcols_, nrows_,repeat_, noms_, types_, taille_des_chaines_);
497 KeywordsIntoDVList(fptr_, dvl_, hdunum_);
498 }
[1334]499 else if(hdutype == BINARY_TBL)
500 {
501 hdutype_= FitsExtensionType_BINARY_TBL;
502 GetBinTabParameters(fptr_,nbcols_, nrows_,repeat_, noms_, types_, taille_des_chaines_);
503 KeywordsIntoDVList(fptr_, dvl_, hdunum_);
504 }
505 else
506 {
507 hdutype_= FitsExtensionType_NULL;
508 KeywordsIntoDVList(fptr_, dvl_, hdunum_);
509 }
[839]510 }
[971]511}
[839]512
[1136]513
[1234]514void FitsInFile::moveToFollowingHeader()
515{
516 int status = 0;
517 hdunum_++;
518 getHeader();
[1334]519 if ( hdutype_ == FitsExtensionType_NULL )
520 {
521 cout << " WARNING (FitsInFile::ReadHeader) : no SOPHYA object on HDU number : " << hdunum_ << endl;
522
523 }
[1234]524}
[1218]525
526
527
[1234]528
529
[1218]530/*! \fn int SOPHYA::FitsInFile::NbColsFromFits() const
531\return number of columns (return 1 if IMAGE)
532*/
[1136]533int FitsInFile::NbColsFromFits() const
[839]534{
[1334]535 if(hdutype_ == FitsExtensionType_BINARY_TBL) return nbcols_;
[971]536 else
[1334]537 if(hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_IMAGE) return 1;
[839]538 else
539 {
[1353]540 cout << " hdutype= " << hdutype_ << endl;
[1334]541 throw PException("FitsFile::NbColsFromFits, HDU not supported");
[839]542 }
543}
544
[1218]545/*! \fn int SOPHYA::FitsInFile::NentriesFromFits(int nocol) const
546\return number of data in the current IMAGE extension on FITS file, or number
547 of data of column number 'nocol' of the current BINTABLE extension
548*/
[1136]549int FitsInFile::NentriesFromFits(int nocol) const
[839]550{
[1334]551 if(hdutype_ == FitsExtensionType_BINARY_TBL) return nrows_*repeat_[nocol];
[1136]552 else
[1334]553 if(hdutype_ == FitsExtensionType_ASCII_TBL) return nrows_;
[1136]554 else
[1334]555 if(hdutype_ == FitsExtensionType_IMAGE) return nbData_;
[1136]556 else
[839]557 {
[1353]558 cout << "hdutype= " << hdutype_ << endl;
[1334]559 throw PException("FitsFile::NentriesFromFits, this HDU is not supported");
[839]560 }
561}
562
[1218]563/*! \fn char SOPHYA::FitsInFile::ColTypeFromFits(int nocol) const
564
565return a character denoting data type of column number 'nocol' in a BINTABLE :
566
567D : double
568
569E : float
570
571I : integer
572
573S : character string
574
575 */
576
[1300]577FitsFile::FitsDataType FitsInFile::ColTypeFromFits(int nocol) const
[839]578{
[1334]579 if(hdutype_ != FitsExtensionType_ASCII_TBL && hdutype_ != FitsExtensionType_BINARY_TBL)
[839]580 {
[1136]581 throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
[839]582 }
[1136]583 return types_[nocol];
[839]584}
[1218]585
586
587/*! \fn string SOPHYA::FitsInFile::ColNameFromFits(int nocol) const
588
589\return name of the column number 'nocol' of the current BINTABLE extension
590 */
591
[1136]592string FitsInFile::ColNameFromFits(int nocol) const
[839]593{
[1334]594 if(hdutype_ != FitsExtensionType_ASCII_TBL && hdutype_ != FitsExtensionType_BINARY_TBL)
[1045]595 {
[1136]596 throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
[1045]597 }
[1136]598 return noms_[nocol];
[839]599}
600
[1218]601/*! \fn int DSOPHYA::FitsInFile::ColStringLengthFromFits(int nocol) const
602
603 \return number of characters of each data for the column number 'nocol' (if char* typed) of the current BINTABLE extension
604*/
605
[1136]606int FitsInFile::ColStringLengthFromFits(int nocol) const
[839]607{
[1334]608 if(hdutype_ != FitsExtensionType_ASCII_TBL && hdutype_ != FitsExtensionType_BINARY_TBL)
[1136]609 {
610 throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
611 }
612 int index=-1;
613 int k;
614 for (k=0; k<=nocol; k++)
615 {
[1300]616 if (types_[k] == FitsDataType_char) index++;
[1136]617 }
618 return taille_des_chaines_[index];
[839]619}
[1218]620
621
622
623/*! \fn void SOPHYA::FitsInFile::GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char ** cdata)
624
625Get the NoLine-th 'line' from the current BINTABLE extension on FITS file,
626 */
627
[1193]628void FitsInFile::GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char ** cdata)
[839]629{
630 int status= 0;
[1136]631 int anull;
632 double dnull= 0.;
633 float fnull= 0.;
634 int inull= 0;
635 char* cnull= "";
636 int dcount = 0.;
637 int fcount = 0.;
638 int icount = 0;
639 int ccount =0;
640 int ncol;
641 long nels=1;
642 for (ncol=0; ncol<nbcols_; ncol++)
[861]643 {
[1136]644 switch (types_[ncol])
645 {
[1300]646 case FitsDataType_double :
[1136]647 fits_read_col(fptr_,TDOUBLE,ncol+1,NoLine+1,1,1,&dnull,&ddata[dcount++],&anull,&status);
648 break;
[1300]649 case FitsDataType_float :
[1136]650 fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[fcount++],&anull,&status);
651 break;
[1300]652 case FitsDataType_int :
[1136]653 fits_read_col(fptr_,TINT,ncol+1,NoLine+1,1,1,&inull,&idata[icount++],
654 &anull,&status);
655 break;
[1300]656 case FitsDataType_char :
[1136]657 fits_read_col(fptr_,TSTRING,ncol+1,NoLine+1,1,1,cnull,&cdata[ccount++],&anull,&status);
658 break;
659 }
660 if (status)
661 {
662 ResetStatus(status);
663 break;
664 }
[861]665 }
[903]666}
[839]667
[1218]668/*! \fn void SOPHYA::FitsInFile::GetBinTabLine(long NoLine, BnTblLine& ligne)
669Get the NoLine-th 'line' from the current BINTABLE extension on FITS file,
670*/
[1193]671void FitsInFile::GetBinTabLine(long NoLine, BnTblLine& ligne)
672{
673 int status= 0;
674 int anull;
675 double dnull= 0.;
676 float fnull= 0.;
677 int inull= 0;
678 char* cnull= "";
679 int dcount = 0.;
680 int fcount = 0.;
681 int icount = 0;
682 int ccount =0;
683 int ncol;
684 long nels=1;
685 for (ncol=0; ncol<nbcols_; ncol++)
686 {
687 switch (types_[ncol])
688 {
[1300]689 case FitsDataType_double :
[1193]690 fits_read_col(fptr_,TDOUBLE,ncol+1,NoLine+1,1,1,&dnull,&ligne.ddata_[dcount++],&anull,&status);
[1300]691 break;
692 case FitsDataType_float :
[1193]693 fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&ligne.fdata_[fcount++],&anull,&status);
694 break;
[1300]695 case FitsDataType_int :
[1193]696 fits_read_col(fptr_,TINT,ncol+1,NoLine+1,1,1,&inull,&ligne.idata_[icount++],
697 &anull,&status);
698 break;
[1300]699 case FitsDataType_char :
[1193]700 char* chaine = new char[taille_des_chaines_[ccount]];
701 fits_read_col(fptr_,TSTRING,ncol+1,NoLine+1,1,1,cnull,&chaine,&anull,&status);
702 ligne.cdata_[ccount++] = string(chaine);
703 break;
704 }
705 if (status)
706 {
707 ResetStatus(status);
708 break;
709 }
710 }
711}
712
[1218]713/*! \fn void SOPHYA::FitsInFile::GetBinTabLine(int NoLine, float* fdata)
714
715Get the NoLine-th float 'line' from the current BINTABLE extension on FITS file,
716*/
[1136]717void FitsInFile::GetBinTabLine(int NoLine, float* fdata)
[903]718{
[1136]719 int status= 0;
720 int anull;
721 float fnull= 0.;
722 long nels=1;
723 int ncol;
724 for (ncol=0; ncol<nbcols_; ncol++)
[861]725 {
[1136]726 fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[ncol],&anull,&status);
727 if (status)
[903]728 {
[1136]729 ResetStatus(status);
730 break;
[903]731 }
[1136]732 }
733}
[839]734
[903]735
[1218]736/*! \fn void SPOPHYA::FitsInFile::GetBinTabFCol(double* valeurs,int nentries, int NoCol) const
737
738fill the array 'valeurs' with double data from the current BINTABLE extension on FITS file, from column number 'NoCol'
739
740\param <nentries> number of data to be read
741*/
[1353]742void FitsInFile::GetBinTabFCol(double* valeurs,int nentries, int NoCol) const
[839]743 {
744 int status= 0;
745 int DTYPE;
746 long repeat,width;
747 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
748 if( DTYPE != TDOUBLE)
749 {
[1045]750 if (DTYPE == TFLOAT) cout << " WARNING: reading double from float : conversion will be made by fitsio library" << endl;
751 else
752 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non double");
[839]753 }
754 long nels=nentries;
[971]755 int anull;
[839]756 // no checking for undefined pixels
[971]757 double dnull= 0.;
758 // fits_read_key(fptr_,TDOUBLE,"BAD_DATA",&dnull,NULL,&status);
759 // if (status != 0)
760 // {
761 // dnull = -1.6375e30; // default value
762 // status = 0;
763 // }
764 if (nentries != nrows_*repeat)
765 {
766 cout << " found " << nentries << " pixels, expected: " << nrows_*repeat << endl;
767 throw PException(" FitsFile:::GetBinTabFCol ");
768 }
[839]769 fits_read_col(fptr_,TDOUBLE,NoCol+1,1,1,nels,&dnull,valeurs,
770 &anull,&status);
771 if( status ) printerror( status,"erreur lecture de colonne" );
[971]772
[839]773 }
774
[1218]775/*! \fn void SOPHYA::FitsInFile::GetBinTabFCol(float* valeurs,int nentries, int NoCol) const
776
777 same as previous method with float data
778*/
[1353]779void FitsInFile::GetBinTabFCol(float* valeurs,int nentries, int NoCol) const
[839]780 {
781 int status= 0;
782 int DTYPE;
783 long repeat,width;
784 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
785 if( DTYPE != TFLOAT)
786 {
[1045]787 if (DTYPE == TDOUBLE) cout << " WARNING: reading float from double : conversion will be made by fitsio library" << endl;
788 else
789 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
[839]790 }
791 long nels=nentries;
[971]792 int anull;
[839]793 // no checking for undefined pixels
794 float fnull= 0.;
[971]795 // fits_read_key(fptr_,TFLOAT,"BAD_DATA",&fnull,NULL,&status);
796 // if (status != 0)
797 // {
798 // fnull = -1.6375e30; // default value
799 // status = 0;
800 // }
801 if (nentries != nrows_*repeat)
802 {
803 cout << " found " << nentries << " pixels, expected: " << nrows_*repeat << endl;
804 throw PException(" FitsFile:::GetBinTabFCol ");
805 }
[839]806 fits_read_col(fptr_,TFLOAT,NoCol+1,1,1,nels,&fnull,valeurs,
807 &anull,&status);
808 if( status ) printerror( status,"erreur lecture de colonne" );
809 }
[1136]810
[1218]811/*! \fn void SOPHYA::FitsInFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
812
813 same as previous method with int data
814*/
815
[1353]816void FitsInFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
[839]817 {
818 int status= 0;
819 int DTYPE;
820 long repeat,width;
821 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
822 if( DTYPE != TLONG && DTYPE != TINT && DTYPE != TSHORT )
823 {
824 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non entier");
825 }
826 long nels=nentries;
827 // no checking for undefined pixels
828 int anull;
829 int inull= 0;
[971]830 // fits_read_key(fptr_,TINT,"BAD_DATA",&inull,NULL,&status);
831 // if (status != 0)
832 // {
833 // inull = -999999; // default value
834 // status = 0;
835 // }
836 if (nentries != nrows_*repeat)
837 {
838 cout << " found " << nentries << " pixels, expected: " << nrows_*repeat << endl;
839 throw PException(" FitsFile:::GetBinTabFCol ");
840 }
[839]841 fits_read_col(fptr_,TINT,NoCol+1,1,1,nels,&inull,valeurs,
842 &anull,&status);
843 if( status ) printerror( status,"erreur lecture de colonne" );
844 }
[1136]845
[1218]846/*! \fn void SOPHYA::FitsInFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
847
848 same as previous method with char* data
849*/
850
[1136]851void FitsInFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
[839]852 {
853 int status= 0;
854 int DTYPE;
855 long repeat,width;
856 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
[1300]857 if( DTYPE != TSTRING && DTYPE != TBYTE)
[839]858 {
[1300]859 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non string");
[839]860 }
861 long nels=nentries;
862 // no checking for undefined pixels
863 int anull;
[971]864 char* cnull= "";
865 if (nentries != nrows_*repeat/width)
866 {
867 cout << " found " << nentries << " pixels, expected: " << nrows_*repeat/width << endl;
868 throw PException(" FitsFile:::GetBinTabFCol ");
869 }
[839]870 long frow=1;
871 long felem=1;
872 fits_read_col(fptr_,TSTRING,NoCol+1,frow,felem,nels,cnull,valeurs,
873 &anull,&status);
874 if( status ) printerror( status,"erreur lecture de colonne" );
875 }
[1045]876
[1218]877/*! \fn void SOPHYA::FitsInFile::GetSingleColumn(double* map, int nentries) const
878fill the array 'map' with double data from the current extension on FITS file.
879If the extension is BINTABLE, the first column is provided.
880
881\param <nentries> number of data to be read
882*/
[1353]883void FitsInFile::GetSingleColumn(double* map, int nentries) const
[1136]884{
885 int status = 0;
[1334]886 if(hdutype_ == FitsExtensionType_IMAGE)
[1045]887 {
[1136]888
[1300]889 if(imageDataType_ != FitsDataType_double)
[1047]890 {
[1136]891 cout << " The data type on fits file is not double...";
892 cout << " Conversion to double achieved by cfitsio lib" << endl;
[1047]893 }
[1136]894
895 // no checking for undefined pixels
896 int anull;
897 double dnull= 0.;
898
899 long nels= nentries;
900 fits_read_img(fptr_,TDOUBLE,1,nels,&dnull,map,&anull,&status);
901 if( status ) printerror( status );
[1045]902 }
[1136]903 else
[1334]904 if(hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL)
[1136]905 {
906 GetBinTabFCol(map,nentries, 0);
907 }
908 else
909 {
[1353]910 cout << " hdutype= " << hdutype_ << endl;
[1334]911 throw IOExc("FitsFile::GetSingleColumn, this HDU is unknown");
[1136]912 }
[1045]913}
914
[1218]915/*! \fn void SOPHYA::FitsInFile::GetSingleColumn(float* map, int nentries) const
916same as above with float data
917*/
[1353]918void FitsInFile::GetSingleColumn(float* map, int nentries) const
[1045]919{
[1136]920 int status = 0;
[1334]921 if(hdutype_ == FitsExtensionType_IMAGE)
[1045]922 {
[1300]923 if(imageDataType_ != FitsDataType_float)
[1047]924 {
[1136]925 cout << " The data type on fits file is not float ";
926 cout << " Conversion to float achieved by cfitsio lib" << endl;
[1047]927 }
[1136]928 // no checking for undefined pixels
929 int anull;
930 float fnull= 0.;
931
932 long nels= nentries;
933 fits_read_img(fptr_,TFLOAT,1,nels,&fnull, map,&anull,&status);
934 if( status ) printerror( status );
[1045]935 }
[839]936 else
[1334]937 if(hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL)
[1136]938 {
939 GetBinTabFCol(map,nentries, 0);
940 }
[839]941 else
942 {
[1353]943 cout << " hdutype= " << hdutype_ << endl;
[1136]944 throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
[839]945 }
946}
947
[1218]948/*! \fn void SOPHYA::FitsInFile::GetSingleColumn( int* map, int nentries) const
949 same as above with int data
950*/
[1353]951void FitsInFile::GetSingleColumn( int* map, int nentries) const
[839]952{
[1136]953 int status = 0;
[1334]954 if(hdutype_ == FitsExtensionType_IMAGE)
[839]955 {
[1300]956 if(imageDataType_ != FitsDataType_int)
[1136]957 {
958 cout << " The data type on fits file is not int ";
959 cout << " Conversion to float achieved by cfitsio lib" << endl;
960 }
961 // no checking for undefined pixels
962 int anull;
963 float fnull= 0.;
964
965 long nels= nentries;
966 fits_read_img(fptr_,TINT,1,nels,&fnull,map,&anull,&status);
967 if( status ) printerror( status );
[839]968 }
969 else
[1334]970 if(hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL)
[1136]971 {
972 GetBinTabFCol(map,nentries, 0);
973 }
[839]974 else
[1136]975 {
[1353]976 cout << " hdutype= " << hdutype_ << endl;
[1136]977 throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
978 }
[839]979}
980
[1136]981void FitsInFile::GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
[903]982 vector<int>& repeat,
983 vector<string>& noms,
[1300]984 vector<FitsDataType>& types,
[903]985 vector<int>& taille_des_chaines)
[839]986{
987 int status= 0;
[903]988 int hdunum=0;
989 int hdutype=0;
990 fits_get_hdu_num(fileptr,&hdunum);
991 fits_get_hdu_type(fileptr, &hdutype, &status);
992
993 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
[839]994 {
[903]995 throw IOExc("FitsFile::GetBinTabParameters this HDU is not an ASCII table nor a binary table");
[839]996 }
[1334]997 // if(hdutype == ASCII_TBL)
998 // cout << " Reading a FITS ascii table in HDU : " << hdunum << endl;
999 // if(hdutype == BINARY_TBL)
1000 // cout << " Reading a FITS binary table in HDU : " << hdunum << endl;
[839]1001
1002 // get the number of columns
[903]1003 fits_get_num_cols(fileptr, &nbcols,&status);
[839]1004 if( status ) printerror( status );
1005
1006 // get the number of rows
1007 long naxis2= 0;
[903]1008 fits_get_num_rows(fileptr,&naxis2,&status);
[839]1009 if( status ) printerror( status );
[903]1010 nrows = (int)naxis2;
[839]1011
1012 // get the datatype, names and the repeat count
[903]1013 noms.clear();
1014 noms.reserve(nbcols);
1015 types.clear();
1016 types.reserve(nbcols);
1017 repeat.clear();
1018 repeat.reserve(nbcols);
1019 taille_des_chaines.clear();
[839]1020 char **ttype = new char*[nbcols];
[923]1021 int ii;
[1175]1022 //
1023 //
[923]1024 for (ii=0; ii < nbcols; ii++) ttype[ii]=new char[FLEN_VALUE];
[839]1025 int nfound;
[903]1026 fits_read_keys_str(fileptr, "TTYPE",1,nbcols,ttype,&nfound, &status);
[839]1027 if( status ) printerror( status,"erreur lecture des noms de colonne");
1028 int rept=0;
[1300]1029 if(hdutype == ASCII_TBL)
[839]1030 {
[1300]1031 for(ii = 0; ii < nbcols; ii++)
[839]1032 {
[1300]1033 int DTYPE;
1034 long width;
1035 long repete = 0;
1036 fits_get_coltype(fileptr,ii+1,&DTYPE,&repete,&width,&status);
1037 if( status ) printerror( status,"erreur lecture type de colonne");
1038 rept = repete;
1039 noms.push_back(string(ttype[ii]));
1040 switch (DTYPE)
1041 {
1042 case TDOUBLE :
1043 types.push_back(FitsDataType_double);
1044 break;
1045 case TFLOAT :
1046 types.push_back(FitsDataType_float);
1047 break;
1048 case TLONG :
1049 types.push_back(FitsDataType_int);
1050 break;
1051 case TSHORT :
1052 types.push_back(FitsDataType_int);
1053 break;
1054 case TSTRING :
1055 types.push_back(FitsDataType_char);
1056 taille_des_chaines.push_back(width);
1057 rept/=width;
1058 break;
1059 default :
1060 cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
1061 throw IOExc("FitsFile::GetBinTabParameters, unsupported data type of field, for ASCII table");
1062 }
1063 repeat.push_back(rept);
[839]1064 }
[1136]1065 }
[1300]1066 else
1067 {
1068 for(ii = 0; ii < nbcols; ii++)
1069 {
1070 int DTYPE;
1071 long width;
1072 long repete = 0;
1073 fits_get_coltype(fileptr,ii+1,&DTYPE,&repete,&width,&status);
1074 if( status ) printerror( status,"erreur lecture type de colonne");
1075 rept = repete;
1076 noms.push_back(string(ttype[ii]));
1077 switch (DTYPE)
1078 {
1079 case TDOUBLE :
1080 types.push_back(FitsDataType_double);
1081 break;
1082 case TFLOAT :
1083 types.push_back(FitsDataType_float);
1084 break;
1085 case TLONG :
1086 types.push_back(FitsDataType_int);
1087 break;
1088 case TINT :
1089 types.push_back(FitsDataType_int);
1090 break;
1091 case TSHORT :
1092 types.push_back(FitsDataType_int);
1093 break;
1094 case TSTRING :
1095 types.push_back(FitsDataType_char);
1096 taille_des_chaines.push_back(width);
1097 rept/=width;
1098 break;
1099 case TBYTE :
1100 types.push_back(FitsDataType_char);
1101 taille_des_chaines.push_back(width);
1102 rept/=width;
1103 break;
1104 default :
1105 cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
1106 throw IOExc("FitsFile::GetBinTabParameters, unsupported data type of field, for BINTABLE");
1107 }
1108 repeat.push_back(rept);
1109 }
1110 }
[1136]1111 for (ii=0; ii < nbcols; ii++) delete [] ttype[ii];
1112 delete [] ttype;
1113}
1114
1115void FitsInFile::KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum)
1116{
1117 int status = 0;
1118 int hdutype;
1119 fits_movabs_hdu(fileptr,hdunum,&hdutype,&status);
1120 if( status ) printerror( status,":KeywordsIntoDVList : erreur movabs");
1121 // get number of keywords
1122 int nkeys,keypos;
1123 fits_get_hdrpos(fileptr,&nkeys,&keypos,&status);
1124 if( status ) printerror( status );
1125
1126 // put keywords in a DVList object
1127 char keyname[LEN_KEYWORD]= "";
1128 char strval[FLEN_VALUE]= "";
1129 char dtype;
1130 char card[FLEN_CARD];
1131 char *comkey = "COMMENT";
[1143]1132 char comment[FLEN_COMMENT];
[1136]1133
1134 // shift with the number of mandatory keywords
[1143]1135 // int num= 8;
1136 int num= 0;
1137 // primary header
1138 if (hdunum == 1)
1139 {
1140 // read NAXIS
1141 int naxis=0;
1142 fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
1143 // number of mandatory keywords
1144 num = naxis+3;
1145 }
1146 // extensions
1147 else
1148 {
1149 if (hdutype == IMAGE_HDU)
1150 {
1151 // read NAXIS
1152 int naxis=0;
1153 fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
1154 // number of mandatory keywords
1155 num = naxis+5;
1156 }
1157 else
1158 if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
1159 {
1160 // number of mandatory keywords
1161 num = 8;
1162 }
1163 }
[1136]1164 int j;
1165 for(j = num+1; j <= nkeys; j++)
1166 {
1167 fits_read_keyn(fileptr,j,card,strval,NULL,&status);
1168 if(status) printerror(status);
1169
1170 strncpy(keyname,card,LEN_KEYWORD-1);
1171 if(strncmp(keyname,comkey,LEN_KEYWORD-1) != 0 && strlen(keyname) != 0
1172 && strlen(strval) != 0)
1173 {
1174 fits_get_keytype(strval,&dtype,&status);
1175 if(status) printerror(status);
1176
1177 strip(keyname, 'B',' ');
1178 strip(strval, 'B',' ');
1179 strip(strval, 'B','\'');
1180
1181 switch( dtype )
1182 {
1183 case 'C':
[1143]1184 fits_read_key(fileptr,TSTRING,keyname,strval,comment,&status);
1185 dvl[keyname]= strval;
1186 dvl.SetComment(keyname, comment);
[1136]1187 break;
1188 case 'I':
1189 int ival;
[1143]1190 fits_read_key(fileptr,TINT,keyname,&ival,comment,&status);
[1136]1191 dvl[keyname]= (int_4) ival; // Portage mac DY
[1143]1192 dvl.SetComment(keyname, comment);
[1136]1193 break;
1194 case 'L':
1195 int ilog;
[1143]1196 fits_read_key(fileptr,TLOGICAL,keyname,&ilog,comment,&status);
[1136]1197 dvl[keyname]= (int_4) ilog;
[1143]1198 dvl.SetComment(keyname, comment);
[1136]1199 break;
1200 case 'F':
1201 double dval;
[1143]1202 fits_read_key(fileptr,TDOUBLE,keyname,&dval,comment,&status);
[1136]1203 dvl[keyname]= dval;
[1143]1204 dvl.SetComment(keyname, comment);
[1136]1205 break;
1206 }
1207
1208 }
[839]1209 }
[1136]1210 // dvl_.Print();
1211}
1212
[1218]1213
1214/*!
1215 \class SOPHYA::FitsOutFile
1216 Class for loading SOPHYA objects from FITS Format Files (uses cfitsio lib)
1217*/
1218
[1136]1219FitsOutFile::FitsOutFile()
1220{
[1193]1221 InitNull();
[903]1222}
[839]1223
[1218]1224 /*! \fn SOPHYA::FitsOutFile::FitsOutFile(char flnm[], WriteMode wrm)
1225
1226\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)
1227
1228 */
[1231]1229
1230FitsOutFile::FitsOutFile(string const & flnm, WriteMode wrm)
[1136]1231{
[1231]1232 InitNull();
1233 openoutputfitsfile(flnm.c_str(), wrm);
1234}
[839]1235
[1231]1236FitsOutFile::FitsOutFile(const char * flnm, WriteMode wrm)
1237{
[1136]1238 InitNull();
[1231]1239 openoutputfitsfile(flnm, wrm);
1240}
1241
1242void FitsOutFile::openoutputfitsfile(const char * flnm, WriteMode wrm)
1243{
[1136]1244 int status = 0;
[839]1245
[1136]1246 // create new FITS file
[1183]1247 fits_create_file(&fptr_,flnm,&status);
1248 if( status )
[1136]1249 {
[1193]1250
1251 switch (wrm)
1252 {
[1183]1253 // si on veut ecrire a la fin de ce fichier
[1193]1254 case append :
[1183]1255 status = 0;
[1235]1256 fits_clear_errmsg();
[1183]1257 fits_open_file(&fptr_,flnm,READWRITE,&status);
1258 if( status )
1259 {
1260 cout << " error opening file: " << flnm << endl;
1261 printerror(status, "failure opening a file supposed to exist");
1262 }
1263 else cout << " file " << flnm << " opened, new objects will be appended " << endl;
1264 fits_get_num_hdus(fptr_, &hdunum_, &status);
1265 int hdutype;
1266 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1267 if( status ) printerror( status,":FitsFile::WriteF : erreur movabs");
[1193]1268 break;
1269
1270 case clear :
[1183]1271 {
1272 status = 0;
[1235]1273 fits_clear_errmsg();
[1183]1274 char* newname = new char[strlen(flnm)+1];
1275 //
1276 newname[0] = '!';
1277 newname[1] = '\0';
1278 strcat(newname, flnm);
1279 fits_create_file(&fptr_,newname,&status);
[1193]1280 delete [] newname;
[1183]1281 if (status)
1282 {
1283 cout << " error opening file: " << flnm << endl;
1284 printerror(status, "unable to open file, supposed to exist");
1285 }
[1193]1286 else cout << " WARNING : file " << flnm << " is overwritten " << endl;
1287 break;
[1183]1288 }
[1193]1289 case unknown :
1290 printerror(status, " file seems already to exist");
1291 break;
[1183]1292
[1193]1293 }
[1136]1294 }
1295}
1296
1297
1298
[1218]1299/*! \fn void SOPHYA::FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList &dvl)
1300
1301create an IMAGE header on FITS file.
1302\param <type> type of data (see method ColTypeFromFits)
1303\param <nbdim> number of dimensions : 1D, 2D, 3D etc. = NAXIS
1304\param <naxisn> array containind sizes of the different dimensions
1305*/
[1221]1306void FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList* ptr_dvl)
[1136]1307{
1308 int status = 0;
1309 long naxis = nbdim;
1310 long* naxes = new long[nbdim];
[1246]1311 bool hdunfirst= (hdunum_ == 0);
1312 if (hdunfirst)
[1136]1313 {
[1143]1314 if (imageOnPrimary_ == false)
1315 {
[1234]1316 hdunum_ = 1;
[1143]1317 fits_create_img(fptr_,FLOAT_IMG,0,naxes,&status);
[1246]1318 }
[1136]1319 }
1320 int k;
1321 for (k=0; k< nbdim; k++) naxes[k] = (long)naxisn[k];
1322 if (type == 'D')
1323 fits_create_img(fptr_,DOUBLE_IMG,naxis,naxes,&status);
1324 else
1325 if (type == 'E')
1326 fits_create_img(fptr_,FLOAT_IMG,naxis,naxes,&status);
1327 else
1328 if (type == 'I')
1329 fits_create_img(fptr_,LONG_IMG,naxis,naxes,&status);
1330 else
1331 {
1332 cout << " type of data: " << type << endl;
1333 throw PException("FitsFile:::makeHeaderImageOnFits:unprogrammed type of data ");
1334 }
[1353]1335
[1246]1336 // on ajoute eventuellement un dvlist prepare et la doc SOPHYA
[1136]1337 hdunum_++;
[1246]1338 if (hdunfirst)
1339 {
1340 addDVListOnPrimary();
1341 writeSignatureOnFits(1);
1342 }
[1143]1343
[1353]1344 // header format FITS
1345
1346 writeAppendedHeaderOnFits();
1347
1348 // write supplementary keywords (from SOPHYA)
[1143]1349 // dvl.Print();
[1221]1350 if (ptr_dvl != NULL) addKeywordsOfDVList(*ptr_dvl);
[1143]1351
[1136]1352 delete [] naxes;
1353 if( status ) printerror( status, "erreur creation HDU IMAGE" );
1354
1355}
[1218]1356
1357
1358/*! \fn void SOPHYA::FitsOutFile::PutImageToFits(int nbData, double* map) const
1359
1360write double data from array 'map'on an IMAGE extension
1361\param <nbData> number of data to be written
1362*/
[1353]1363void FitsOutFile::PutImageToFits(int nbData, double* map) const
[1136]1364{
1365 int status = 0;
1366 long npix= nbData;
1367 fits_write_img(fptr_,TDOUBLE,1,npix,map,&status);
[1209]1368 if( status ) printerror( status, "erreur ecriture PutImageToFits" );
[1136]1369}
1370
[1218]1371/*! \fn void SOPHYA::FitsOutFile::PutImageToFits(int nbData, float* map) const
1372
1373same as previous method with float data
1374*/
[1353]1375void FitsOutFile::PutImageToFits(int nbData, float* map) const
[1136]1376{
1377 int status = 0;
1378 long npix= nbData;
1379 fits_write_img(fptr_,TFLOAT,1,npix, map,&status);
[1209]1380 if( status ) printerror( status, "erreur ecriture PutImageToFits" );
[1136]1381
1382}
[1218]1383
1384 /*! \fn void SOPHYA::FitsOutFile::PutImageToFits( int nbData, int* map) const
1385
1386 same as previous method with int data */
[1353]1387void FitsOutFile::PutImageToFits( int nbData, int* map) const
[1136]1388{
1389 int status = 0;
1390
1391 long npix= nbData;
1392 fits_write_img(fptr_,TINT,1,npix,map,&status);
[1209]1393 if( status ) printerror( status, "erreur ecriture PutImageToFits" );
[1136]1394}
1395
1396
1397
[1218]1398/*! \fn void SOPHYA::FitsOutFile::makeHeaderBntblOnFits( string fieldType, vector<string> Noms, int nentries, int tfields, DVList &dvl, string extname, vector<int> taille_des_chaines)
1399
1400create an BINTABLE header on FITS file.
1401\param <fieldType> array conta
1402ining characters denoting types of the different column (see method ColTypeFromFits)
1403\param <Noms> array of the names of columns
1404\param <nentries> number of data of each column
1405\param <tfields> number of columns
1406\param <dvl> a SOPHYA DVList containing keywords to be appended
1407\param <extname> keyword EXTNAME for FITS file
1408\param <taille_des_chaines> vector containing the number of characters of data for each char* typed column, with order of appearance in 'fieldType'
1409*/
[1300]1410void FitsOutFile::makeHeaderBntblOnFits(string fieldType, vector<string> Noms, int nentries, int tfields, DVList* ptr_dvl, string extname, vector<int> taille_des_chaines)
[839]1411{
[1209]1412 int k;
[839]1413 int status = 0;
1414 long nrows;
[1300]1415 // verifications de coherences
[1209]1416
[1193]1417 if (fieldType.length() != tfields)
[839]1418 {
[1193]1419 cout << " nombre de champs :" << tfields << "nombre de types: " << fieldType.length() << endl;
[1136]1420 throw ParmError("FitsFile:: fields and types don't match");
[839]1421
1422 }
[1209]1423 if (tfields > Noms.size())
1424 {
1425 cout << " WARNING: FitsOutFile::makeHeaderBntblOnFits, length of vector of column names not equal to total number of columns" << endl;
1426 for (k=0; k<(tfields-Noms.size()); k++) Noms.push_back( string(" "));
1427 }
1428
1429 // nombre de variables "chaines de caracteres"
1430 int nbString = 0;
1431 for (k=0; k<tfields;k++) if (fieldType[k] == 'A') nbString++;
1432 // coherence de la longueur du vecteur des tailles
1433 if (nbString > taille_des_chaines.size())
1434 {
1435 cout << " WARNING: FitsOutFile::makeHeaderBntblOnFits, length of vector of string lengths not equal to total number of columns" << endl;
1436 int strSz=0;
1437 for (k=0; k<taille_des_chaines.size(); k++) if ( taille_des_chaines[k] > strSz) strSz = taille_des_chaines[k];
1438 for (k=0; k<(nbString-taille_des_chaines.size()); k++) taille_des_chaines.push_back(strSz);
1439 }
[839]1440 char ** ttype= new char*[tfields];
1441 char ** tform= new char*[tfields];
1442 char largeur[FLEN_VALUE];
1443 int noColString=0;
[971]1444 for (k=0; k<tfields;k++)
[839]1445 {
1446 char format[FLEN_VALUE];
1447
1448 if(nentries < 1024)
1449 {
1450 nrows= nentries;
1451 if (fieldType[k] == 'A')
1452 {
1453 sprintf(largeur,"%d",taille_des_chaines[noColString++]);
1454 strcpy(format,largeur);
1455 }
1456 else strcpy(format,"1");
1457 }
1458 else
1459 {
1460 nrows = nentries/1024;
1461 if(nentries%1024 != 0) nrows++;
1462 if (fieldType[k] == 'A')
1463 {
[1136]1464 char largaux[FLEN_VALUE];
1465 sprintf(largeur,"%d",taille_des_chaines[noColString]);
1466 sprintf(largaux,"%d",1024*taille_des_chaines[noColString]);
1467 noColString++;
1468 strcpy(format, largaux);
[839]1469 }
1470 else strcpy(format,"1024");
1471 }
1472 strncat(format,&fieldType[k],1);
1473 if (fieldType[k] == 'A')
1474 {
1475 strcat(format,largeur);
1476 }
[1193]1477 ttype[k] = const_cast<char*>(Noms[k].c_str());
[839]1478 tform[k]= new char[FLEN_VALUE];
1479 strcpy(tform[k],format);
1480 }
[1193]1481 char* extn = const_cast<char*>(extname.c_str());
[839]1482
1483 // create a new empty binary table onto the FITS file
1484 // physical units if they exist, are defined in the DVList object
1485 // so the NULL pointer is given for the tunit parameters.
1486 nrows=0;
1487 fits_create_tbl(fptr_,BINARY_TBL,nrows,tfields,ttype,tform,
1488 NULL,extn,&status);
1489 if( status ) printerror( status );
[1353]1490
1491 int ii;
1492 for(ii = 0; ii < tfields; ii++)
1493 {
1494 delete [] tform[ii];
1495 }
1496 delete [] ttype;
1497 delete [] tform;
1498
1499 // on ajoute eventuellement des mots-cles
1500
[1246]1501 if ( hdunum_ == 0 )
1502 {
1503 hdunum_ = 2;
1504 addDVListOnPrimary();
1505 writeSignatureOnFits(1);
1506 }
[1026]1507 else hdunum_++;
[1353]1508
1509 // header format FITS
1510
1511 writeAppendedHeaderOnFits();
1512
1513 // write SOPHYA keywords
[1221]1514 if (ptr_dvl != NULL) addKeywordsOfDVList(*ptr_dvl);
[839]1515}
1516
[1353]1517
1518
[1218]1519/*! \fn void SOPHYA::FitsOutFile::PutColToFits(int nocol, int nentries, double* donnees) const
1520
1521write double data from array 'donnees ' on column number 'nocol' of a BINTABLE extension.
1522\param <nentries> number of data to be written
1523*/
[1353]1524
1525void FitsOutFile::PutColToFits(int nocol, int nentries, double* donnees) const
[839]1526{
1527 int status = 0;
[971]1528 int hdutype;
[839]1529 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
[1209]1530 if( status ) printerror(status,"PutColToFits: le movabs a foire");
[839]1531 fits_get_hdu_type(fptr_, &hdutype, &status);
[867]1532 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1533 {
1534 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
[1209]1535 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
[867]1536 }
[839]1537 int code;
1538 long repeat, width;
1539 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1540 if( code != TDOUBLE)
1541 {
[1209]1542 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " to be written= DOUBLE " << endl;
[839]1543 }
1544 fits_write_col(fptr_,TDOUBLE,nocol+1,1,1,nentries, donnees ,&status);
1545 if( status ) printerror( status,"erreur ecriture du fichier fits" );
1546}
[1218]1547
1548
1549
1550 /*! \fn void SOPHYA::FitsOutFile::PutColToFits(int nocol, int nentries, float* donnees) const
1551
1552same as previous method with float data
1553*/
[1353]1554void FitsOutFile::PutColToFits(int nocol, int nentries, float* donnees) const
[839]1555{
1556 int status = 0;
1557 int hdutype;
1558 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
[1209]1559 if( status ) printerror(status,"PutColToFits: le movabs a foire");
[839]1560 fits_get_hdu_type(fptr_, &hdutype, &status);
1561 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1562 {
1563 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
[1209]1564 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
[839]1565 }
1566 if(hdutype == ASCII_TBL && nocol>0)
1567 {
[1209]1568 throw IOExc("FitsFile::PutColToFits, this HDU is an ASCII table, nocol>0 forbidden");
[839]1569 }
1570 int code;
1571 long repeat, width;
1572 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1573 if( code != TFLOAT)
1574 {
[1209]1575 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " (FITS code), to be written= FLOAT " << endl;
[839]1576 }
1577 fits_write_col(fptr_,TFLOAT,nocol+1,1,1,nentries, donnees ,&status);
1578 if( status ) printerror( status,"erreur ecriture du fichier fits" );
1579}
[1218]1580
1581
1582/*! \fn void FitsOutFile::PutColToFits(int nocol, int nentries, int* donnees) const
1583
1584same as previous method with int data
1585*/
[1353]1586void FitsOutFile::PutColToFits(int nocol, int nentries, int* donnees) const
[839]1587{
1588 int status = 0;
1589 int hdutype;
1590 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
[1209]1591 if( status ) printerror(status,"PutColToFits: le movabs a foire");
[839]1592 fits_get_hdu_type(fptr_, &hdutype, &status);
1593 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1594 {
1595 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
[1209]1596 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
[839]1597 }
1598 if(hdutype == ASCII_TBL && nocol>0)
1599 {
[1209]1600 throw IOExc("FitsFile::PutColToFits, this HDU is an ASCII table, nocol>0 forbidden");
[839]1601 }
1602 int code;
1603 long repeat, width;
1604 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1605 if( code != TLONG && code != TINT && code != TSHORT )
1606 {
[1209]1607 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " (FITS code), to be written= INT " << endl;
[839]1608 }
1609 fits_write_col(fptr_,TINT,nocol+1,1,1,nentries, donnees ,&status);
[971]1610 if( status ) printerror( status," ecriture du fichier fits" );
[839]1611}
[1218]1612
1613
1614/*! \fn void SOPHYA::FitsOutFile::PutColToFits(int nocol, int nentries, char** donnees) const
1615same as previous method with char* data
1616*/
[1209]1617void FitsOutFile::PutColToFits(int nocol, int nentries, char** donnees) const
[839]1618{
1619 int status = 0;
1620 int hdutype;
1621 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
[1209]1622 if( status ) printerror(status,"PutColToFits: le movabs a foire");
[839]1623 fits_get_hdu_type(fptr_, &hdutype, &status);
1624 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1625 {
1626 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
[1209]1627 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
[839]1628 }
1629 if(hdutype == ASCII_TBL && nocol>0)
1630 {
[1209]1631 throw IOExc("FitsFile::PutColToFits, this HDU is an ASCII table, nocol>0 forbidden");
[839]1632 }
1633 int code;
1634 long repeat, width;
1635 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1636 if( code != TSTRING)
1637 {
[1209]1638 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " (FITS code), to be written= char** " << endl;
[839]1639 }
1640 fits_write_col(fptr_,TSTRING,nocol+1,1,1,nentries, donnees ,&status);
1641 if( status ) printerror( status,"erreur ecriture du fichier fits" );
1642}
1643
[1209]1644void FitsOutFile::PutBinTabLine(long NoLine, BnTblLine& ligne) const
[1193]1645{
[1209]1646 // on ne fait pas de verification de type, ni de dimension ici, pour
1647 // des raisons de performances
1648 int k;
[1193]1649 int status= 0;
1650 int anull;
[1209]1651 int ncol=0;
[1193]1652 long nels=1;
[1209]1653 // int nbcols;
1654 // fits_get_num_cols(fptr_, &nbcols,&status);
1655 for (k=0; k<ligne.ddata_.size(); k++, ncol++)
[1193]1656 {
[1209]1657 fits_write_col(fptr_,TDOUBLE,ncol+1,NoLine+1,1,1, &ligne.ddata_[k] ,&status);
1658 if( status ) printerror( status, "PutBinTabLine : erreur ecriture double" );
[1193]1659 }
[1209]1660 for (k=0; k<ligne.fdata_.size(); k++, ncol++)
1661 {
1662 fits_write_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1, &ligne.fdata_[k] ,&status);
1663 if( status ) printerror( status, "PutBinTabLine : erreur ecriture float" );
1664 }
1665 for (k=0; k<ligne.idata_.size(); k++, ncol++)
1666 {
1667 fits_write_col(fptr_,TINT,ncol+1,NoLine+1,1,1, &ligne.idata_[k] ,&status);
1668 if( status ) printerror( status, "PutBinTabLine : erreur ecriture entier" );
1669 }
1670
1671 for (k=0; k<ligne.cdata_.size(); k++, ncol++)
1672 {
[1220]1673 fits_write_col(fptr_,TSTRING,ncol+1,NoLine+1,1,1, (void*)ligne.cdata_[k].c_str() ,&status);
[1209]1674 if( status ) printerror( status, "PutBinTabLine : erreur ecriture caracteres" );
1675 }
[1193]1676}
1677
1678
[1218]1679/* \fn void SOPHYA::FitsOutFile::DVListIntoPrimaryHeader(DVList& dvl) const
1680
1681Put keywords from a DVList into the primary header of the fits-file
1682*/
[1246]1683void FitsOutFile::DVListIntoPrimaryHeader(DVList& dvl)
[1143]1684{
1685 int status = 0;
1686 int hdutype;
[1246]1687 if (hdunum_ == 0)
1688 {
1689 if (dvlToPrimary_ == NULL) dvlToPrimary_ = new DVList(dvl);
1690 else dvlToPrimary_->Merge(dvl);
1691 }
1692 else
1693 {
1694 fits_movabs_hdu(fptr_,1,&hdutype,&status);
1695 addKeywordsOfDVList(dvl);
1696 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1697 }
[1143]1698}
[839]1699
[1143]1700
[1246]1701void FitsOutFile::writeSignatureOnFits(int hdunum) const
[839]1702{
1703 int status = 0;
[1246]1704 int hdutype;
[839]1705 char keyname[LEN_KEYWORD];
1706 char strval[FLEN_VALUE];
1707 char comment[FLEN_COMMENT];
[1246]1708 if (hdunum_ == 0)
1709 {
1710 cerr << " WARNING : can't write keywords on non existing primary header" << endl;
1711 return;
1712 }
1713 fits_movabs_hdu(fptr_,1,&hdutype,&status);
1714 //
[971]1715 strncpy(keyname, "CREATOR", LEN_KEYWORD);
1716 keyname[LEN_KEYWORD-1] = '\0';
1717 strcpy(strval, "SOPHYA");
1718 strcpy(comment," SOPHYA Package - FITSIOServer ");
1719 fits_write_key(fptr_, TSTRING, keyname, &strval, comment, &status);
1720 if( status ) printerror( status );
[1143]1721 fits_write_date(fptr_, &status);
[971]1722 fits_write_comment(fptr_,"..............................................", &status);
1723 fits_write_comment(fptr_, " SOPHYA package - FITSIOSever ", &status);
1724 fits_write_comment(fptr_, " (C) LAL/IN2P3-CNRS Orsay, FRANCE 2000", &status);
1725 fits_write_comment(fptr_, " (C) DAPNIA/CEA Saclay, FRANCE 2000", &status);
1726 fits_write_comment(fptr_,"..............................................", &status);
[1045]1727 if( status ) printerror( status, "erreur writeSignatureOnFits" );
[1246]1728 //
1729 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
[839]1730}
1731
[903]1732
[1246]1733void FitsOutFile::addKeywordsOfDVList( DVList& dvl) const
[1143]1734{
1735 int status = 0;
1736 fits_write_comment(fptr_,"---------- keywords from SOPHYA ---------", &status);
1737 DVList::ValList::const_iterator it;
1738 for(it = dvl.Begin(); it != dvl.End(); it++)
1739 {
[1311]1740 MuTyV::MTVType keytype= (*it).second.elval.Type();
[1143]1741 char keyname[10];
1742 strncpy(keyname,(*it).first.substr(0,64).c_str(),10);
[1183]1743 string key(keyname);
[1143]1744 char comment[FLEN_COMMENT];
1745 char strval[FLEN_VALUE]= "";
1746 char *comkey = "COMMENT";
[1353]1747 // fits_read_keyword(fptr_, keyname, strval, NULL, &status);
1748 // if (status != 0 || strncmp(keyname,comkey,LEN_KEYWORD-1) == 0 )
[1143]1749 {
[1183]1750 string coco = dvl.GetComment(key);
1751 coco.copy( comment, FLEN_COMMENT-1);
1752 int bout = (coco.length() < FLEN_COMMENT) ? coco.length() : FLEN_COMMENT-1;
1753 comment[bout]= '\0';
[1143]1754 status = 0;
1755 switch (keytype)
1756 {
[1311]1757 case MuTyV::MTVInteger :
[1143]1758 {
[1183]1759 int ival = (int)dvl.GetI(key);
1760 fits_write_key(fptr_,TINT,keyname,&ival, comment,&status);
[1143]1761 break;
1762 }
[1311]1763 case MuTyV::MTVFloat :
[1143]1764 {
[1183]1765 double dval= (double)dvl.GetD(key);
[1143]1766 fits_write_key(fptr_,TDOUBLE,keyname,&dval,comment,&status);
1767 break;
1768 }
[1311]1769 case MuTyV::MTVString :
[1143]1770 {
[1183]1771 char strvaleur[FLEN_VALUE]= "";
1772 string valChaine = dvl.GetS(key);
1773 valChaine.copy(strvaleur, FLEN_VALUE-1);
1774 int fin = (valChaine.length() < FLEN_VALUE) ? valChaine.length() : FLEN_VALUE-1;
1775 strvaleur[fin]= '\0';
1776
1777 fits_write_key(fptr_,TSTRING,keyname,&strvaleur,comment,&status);
[1143]1778 break;
1779 }
1780 }
1781 }
1782 if( status ) printerror( status,"fitsfile: probleme ecriture mot-cle du dvlist" );
1783 }
1784 fits_write_comment(fptr_,"--------------------------------------", &status);
1785}
[903]1786
1787
[1246]1788void FitsOutFile::addDVListOnPrimary()
1789 {
1790 int status = 0;
1791 int hdutype;
1792 if (hdunum_ == 0)
1793 {
1794 cerr << " WARNING : can't write keywords on non existing primary header" << endl;
1795 return;
1796 }
1797 if (dvlToPrimary_ != NULL)
1798 {
1799 fits_movabs_hdu(fptr_,1,&hdutype,&status);
1800 addKeywordsOfDVList(*dvlToPrimary_);
1801 delete dvlToPrimary_;
1802 dvlToPrimary_ = NULL;
1803 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1804 }
1805 }
[839]1806
[1353]1807
1808/*! \fn void FitsOutFile::appendInHeader(FitsInFile& infits, int hdunum)
1809
1810get a header from FitsInFile and append to the header beeing built
1811(shifting mandatory keywords)
1812*/
1813
1814void FitsOutFile::appendInputHeader(FitsInFile& infits, int hdunum)
1815{
1816 int status = 0;
1817 int hdutype;
1818 fitsfile* fptr=infits.fitsfilePtr();
1819 fits_movabs_hdu(fptr,hdunum,&hdutype,&status);
1820 if( status ) fits_report_error(stderr,status);
1821
1822 // get number of keywords
1823 int nkeys,keypos;
1824 fits_get_hdrpos(fptr,&nkeys,&keypos,&status);
1825 if( status ) fits_report_error(stderr,status);
1826 // shift with the number of mandatory keywords
1827 int num= 0;
1828 // if primary header
1829 if (hdunum == 1)
1830 {
1831 // read NAXIS
1832 int naxis=0;
1833 fits_read_key(fptr,TINT,"NAXIS",&naxis,NULL,&status);
1834 // number of mandatory keywords
1835 num = naxis+3;
1836 }
1837 // extensions
1838 else
1839 {
1840 if (hdutype == IMAGE_HDU)
1841 {
1842 // read NAXIS
1843 int naxis=0;
1844 fits_read_key(fptr,TINT,"NAXIS",&naxis,NULL,&status);
1845 // number of mandatory keywords
1846 num = naxis+5;
1847 }
1848 else
1849 if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
1850 {
1851 // number of mandatory keywords
1852 num = 8;
1853 }
1854 }
1855 int j;
1856 char keyname[LEN_KEYWORD];
1857 char value[FLEN_VALUE];
1858 char comment[FLEN_COMMENT];
1859 for(j = num+1; j <= nkeys; j++)
1860 {
1861 char dtype;
1862 fits_read_keyn(fptr,j,keyname,value,comment,&status);
1863 if(status)
1864 {
1865 fits_report_error(stderr,status);
1866 status=0;
1867 }
1868 string kn(keyname);
1869 string cm(comment);
1870 string val(value);
1871 FitsKeyword kw(kn, val, cm);
1872 mots_cles_.push_back(kw);
1873 }
1874}
1875void FitsOutFile::writeAppendedHeaderOnFits()
1876{
1877 for (list<FitsKeyword>::iterator it=mots_cles_.begin(); it !=mots_cles_.end(); it++)
1878 {
1879 (*it).writeOnFits(fptr_);
1880 }
1881 mots_cles_.clear();
1882}
1883
1884void FitsOutFile::insertKeywordOnHeader(string keyname, double value, string comment)
1885{
1886 char* cvalue = new char[16];
1887 sprintf(cvalue,"%e",value);
1888 // char* kn = const_cast<char*>(keyname.c_str());
1889 // char* cm = const_cast<char*>(comment.c_str());
1890 FitsKeyword kw(keyname, string(cvalue), comment);
1891 mots_cles_.push_back(kw);
1892}
1893
1894void FitsOutFile::insertCommentLineOnHeader(string comment)
1895{
1896 FitsKeyword kw(comment);
1897 mots_cles_.push_back(kw);
1898}
1899
1900void FitsOutFile::PrintHeaderToBeAppended()
1901{
1902 cout << " contenu du header en cours de fabrication " << endl;
1903 for (list<FitsKeyword>::iterator it=mots_cles_.begin(); it !=mots_cles_.end(); it++)
1904 {
1905 (*it).Print();
1906 }
1907}
1908
1909
1910FitsKeyword::FitsKeyword()
1911 {
1912 datatype_=' ';
1913 keyname_ = string("");
1914 dvalue_=0.;
1915 ivalue_=1;
1916 svalue_=string("");
1917 comment_=string("");
1918 }
1919
1920FitsKeyword::FitsKeyword(string comment)
1921 {
1922 datatype_=' ';
1923 keyname_=string("COMMENT");
1924 comment_=comment;
1925 }
1926
1927FitsKeyword::FitsKeyword(string keyname, string value, string comment) : keyname_(keyname), comment_(comment)
1928 {
1929 int status=0;
1930 char dtype;
1931 const char* val= value.c_str();
1932 char* valk = const_cast<char*>(val);
1933 fits_get_keytype(valk,&dtype,&status);
1934 if(status)
1935 {
1936 status=0;
1937 datatype_=' ';
1938 }
1939 else datatype_=dtype;
1940
1941 switch( datatype_ )
1942 {
1943 case 'C':
1944 {
1945 svalue_ = string(val);
1946 break;
1947 }
1948 case 'I':
1949 {
1950 ivalue_ = atoi(val);
1951 break;
1952 }
1953 case 'L':
1954 {
1955 // cout << " ATTENTION : pb a regler avec logical!" << endl;
1956 if (value[0] == 'T') ivalue_ = 1;
1957 else ivalue_ = 0;
1958 break;
1959 }
1960 case 'F':
1961 {
1962 dvalue_ = atof(val);
1963 break;
1964 }
1965 case 'X':
1966 {
1967 throw IOExc("FitsKeyword , complex keyword value not supported");
1968 }
1969 }
1970 }
1971
1972void FitsKeyword::writeOnFits(fitsfile* ptr)
1973 {
1974 int status=0;
1975 // char* keyname = new char[LEN_KEYWORD];
1976 // char* comment = new char[FLEN_COMMENT];
1977 char* keyname;
1978 char* comment;
1979 keyname = const_cast<char*>(keyname_.c_str());
1980 comment = const_cast<char*>(comment_.c_str());
1981 // get number of keywords
1982 int nkeys,keypos;
1983 fits_get_hdrpos(ptr,&nkeys,&keypos,&status);
1984 switch( datatype_ )
1985 {
1986 case 'C':
1987 {
1988 char value[FLEN_VALUE];
1989 strncpy(value,svalue_.c_str(),FLEN_VALUE) ;
1990 fits_write_key(ptr,TSTRING,keyname,&value, comment,&status);
1991 fits_report_error(stderr,status);
1992 break;
1993 }
1994 case 'I':
1995 {
1996 fits_write_key(ptr,TINT,keyname,&ivalue_, comment,&status);
1997 fits_report_error(stderr,status);
1998 break;
1999 }
2000 case 'L':
2001 {
2002 fits_write_key(ptr,TLOGICAL,keyname,&ivalue_, comment,&status);
2003 fits_report_error(stderr,status);
2004 break;
2005 }
2006 case 'F':
2007 {
2008 fits_write_key(ptr,TDOUBLE,keyname,&dvalue_, comment,&status);
2009 fits_report_error(stderr,status);
2010 break;
2011 }
2012 case 'X':
2013 {
2014 cout << "FitsKeyword : complex keyword value not supported" << endl;;
2015 }
2016 default :
2017 {
2018 char *comkey = "COMMENT";
2019 if(strncmp(keyname,comkey,LEN_KEYWORD-1) == 0)
2020 {
2021 fits_write_comment(ptr,comment,&status);
2022 fits_report_error(stderr,status);
2023 }
2024 else
2025 {
2026 cout << " WARNING (FitsKeyword::writeOnFits) : unrecognized keyword : " << keyname_ << endl;
2027 }
2028 }
2029 }
2030 }
2031
2032void FitsKeyword::Print()
2033 {
2034 switch( datatype_ )
2035 {
2036 case 'C':
2037 {
2038 cout << " mot cle : " << keyname_ << " valeur : " << svalue_ << " commentaire : " << comment_ <<endl;
2039 break;
2040 }
2041 case 'I':
2042 {
2043 cout << " mot cle : " << keyname_ << " valeur : " << ivalue_ << " commentaire : " << comment_ <<endl;
2044 break;
2045 }
2046 case 'L':
2047 {
2048 cout << " mot cle : " << keyname_ << " valeur : " << ivalue_ << " commentaire : " << comment_ <<endl;
2049 break;
2050 }
2051 case 'F':
2052 {
2053 cout << " mot cle : " << keyname_ << " valeur : " << dvalue_ << " commentaire : " << comment_ <<endl;
2054 break;
2055 }
2056 case 'X':
2057 {
2058 cout << "FitsKeyword : complex keyword value not supported" << endl;;
2059 }
2060 default :
2061 {
2062 cout << " mot cle : " << keyname_ << " commentaire : " << comment_ <<endl;
2063 }
2064 }
2065 }
Note: See TracBrowser for help on using the repository browser.