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

Last change on this file since 1703 was 1703, checked in by lemeur, 24 years ago

lecture de spheres fits, sans NSIDE ni LASTPIX

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