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

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

mots-cles maison type string

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