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

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

interface avec spheres theta-phi

File size: 62.8 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 // }
[1752]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 // }
[1752]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);
[1752]901 cout << " en lecture fits trouve le type " << DTYPE << endl;
902 if( DTYPE != TLONG && DTYPE != TINT)
[839]903 {
[1752]904 throw IOExc("FitsFile::GetBinTabFCol, probleme de lecture d'entiers");
[839]905 }
906 long nels=nentries;
907 // no checking for undefined pixels
908 int anull;
909 int inull= 0;
[971]910 // fits_read_key(fptr_,TINT,"BAD_DATA",&inull,NULL,&status);
911 // if (status != 0)
912 // {
913 // inull = -999999; // default value
914 // status = 0;
915 // }
[1752]916 // if (nentries != nrows_*repeat)
917 // {
918 // cout << " found " << nentries << " pixels, expected: " << nrows_*repeat << endl;
919 // throw PException(" FitsFile:::GetBinTabFCol ");
920 // }
921
922
923
924 // voir commentaire dans putColToFits()
[839]925 fits_read_col(fptr_,TINT,NoCol+1,1,1,nels,&inull,valeurs,
926 &anull,&status);
[1703]927 if( status ) printerrorAndContinue( status,"erreur lecture de colonne" );
[839]928 }
[1136]929
[1218]930/*! \fn void SOPHYA::FitsInFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
931
932 same as previous method with char* data
933*/
934
[1136]935void FitsInFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
[839]936 {
937 int status= 0;
938 int DTYPE;
939 long repeat,width;
940 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
[1300]941 if( DTYPE != TSTRING && DTYPE != TBYTE)
[839]942 {
[1300]943 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non string");
[839]944 }
945 long nels=nentries;
946 // no checking for undefined pixels
947 int anull;
[971]948 char* cnull= "";
[1752]949 // if (nentries != nrows_*repeat/width)
950 // {
951 // cout << " found " << nentries << " pixels, expected: " << nrows_*repeat/width << endl;
952 // throw PException(" FitsFile:::GetBinTabFCol ");
953 // }
[839]954 long frow=1;
955 long felem=1;
956 fits_read_col(fptr_,TSTRING,NoCol+1,frow,felem,nels,cnull,valeurs,
957 &anull,&status);
[1703]958 if( status ) printerrorAndContinue( status,"erreur lecture de colonne" );
[839]959 }
[1045]960
[1218]961/*! \fn void SOPHYA::FitsInFile::GetSingleColumn(double* map, int nentries) const
962fill the array 'map' with double data from the current extension on FITS file.
963If the extension is BINTABLE, the first column is provided.
964
965\param <nentries> number of data to be read
966*/
[1379]967void FitsInFile::GetSingleColumn(r_8* map, int nentries) const
[1136]968{
969 int status = 0;
[1334]970 if(hdutype_ == FitsExtensionType_IMAGE)
[1045]971 {
[1136]972
[1300]973 if(imageDataType_ != FitsDataType_double)
[1047]974 {
[1136]975 cout << " The data type on fits file is not double...";
976 cout << " Conversion to double achieved by cfitsio lib" << endl;
[1047]977 }
[1136]978
979 // no checking for undefined pixels
980 int anull;
981 double dnull= 0.;
982
983 long nels= nentries;
984 fits_read_img(fptr_,TDOUBLE,1,nels,&dnull,map,&anull,&status);
985 if( status ) printerror( status );
[1045]986 }
[1136]987 else
[1334]988 if(hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL)
[1136]989 {
990 GetBinTabFCol(map,nentries, 0);
991 }
992 else
993 {
[1379]994 cout << " hdutype= " << (int) hdutype_ << endl;
[1334]995 throw IOExc("FitsFile::GetSingleColumn, this HDU is unknown");
[1136]996 }
[1045]997}
998
[1218]999/*! \fn void SOPHYA::FitsInFile::GetSingleColumn(float* map, int nentries) const
1000same as above with float data
1001*/
[1379]1002void FitsInFile::GetSingleColumn(r_4* map, int nentries) const
[1045]1003{
[1136]1004 int status = 0;
[1334]1005 if(hdutype_ == FitsExtensionType_IMAGE)
[1045]1006 {
[1300]1007 if(imageDataType_ != FitsDataType_float)
[1047]1008 {
[1136]1009 cout << " The data type on fits file is not float ";
1010 cout << " Conversion to float achieved by cfitsio lib" << endl;
[1047]1011 }
[1136]1012 // no checking for undefined pixels
1013 int anull;
1014 float fnull= 0.;
1015
1016 long nels= nentries;
1017 fits_read_img(fptr_,TFLOAT,1,nels,&fnull, map,&anull,&status);
1018 if( status ) printerror( status );
[1045]1019 }
[839]1020 else
[1334]1021 if(hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL)
[1136]1022 {
1023 GetBinTabFCol(map,nentries, 0);
1024 }
[839]1025 else
1026 {
[1379]1027 cout << " hdutype= " << (int) hdutype_ << endl;
[1136]1028 throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
[839]1029 }
1030}
1031
[1218]1032/*! \fn void SOPHYA::FitsInFile::GetSingleColumn( int* map, int nentries) const
1033 same as above with int data
1034*/
[1379]1035void FitsInFile::GetSingleColumn( int_4* map, int nentries) const
[839]1036{
[1136]1037 int status = 0;
[1334]1038 if(hdutype_ == FitsExtensionType_IMAGE)
[839]1039 {
[1300]1040 if(imageDataType_ != FitsDataType_int)
[1136]1041 {
1042 cout << " The data type on fits file is not int ";
1043 cout << " Conversion to float achieved by cfitsio lib" << endl;
1044 }
1045 // no checking for undefined pixels
1046 int anull;
1047 float fnull= 0.;
1048
1049 long nels= nentries;
1050 fits_read_img(fptr_,TINT,1,nels,&fnull,map,&anull,&status);
1051 if( status ) printerror( status );
[839]1052 }
1053 else
[1334]1054 if(hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL)
[1136]1055 {
1056 GetBinTabFCol(map,nentries, 0);
1057 }
[839]1058 else
[1136]1059 {
[1379]1060 cout << " hdutype= " << (int) hdutype_ << endl;
[1136]1061 throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
1062 }
[839]1063}
1064
[1136]1065void FitsInFile::GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
[903]1066 vector<int>& repeat,
1067 vector<string>& noms,
[1300]1068 vector<FitsDataType>& types,
[903]1069 vector<int>& taille_des_chaines)
[839]1070{
1071 int status= 0;
[903]1072 int hdunum=0;
1073 int hdutype=0;
1074 fits_get_hdu_num(fileptr,&hdunum);
1075 fits_get_hdu_type(fileptr, &hdutype, &status);
1076
1077 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
[839]1078 {
[903]1079 throw IOExc("FitsFile::GetBinTabParameters this HDU is not an ASCII table nor a binary table");
[839]1080 }
[1334]1081 // if(hdutype == ASCII_TBL)
1082 // cout << " Reading a FITS ascii table in HDU : " << hdunum << endl;
1083 // if(hdutype == BINARY_TBL)
1084 // cout << " Reading a FITS binary table in HDU : " << hdunum << endl;
[839]1085
1086 // get the number of columns
[903]1087 fits_get_num_cols(fileptr, &nbcols,&status);
[839]1088 if( status ) printerror( status );
1089
1090 // get the number of rows
1091 long naxis2= 0;
[903]1092 fits_get_num_rows(fileptr,&naxis2,&status);
[839]1093 if( status ) printerror( status );
[903]1094 nrows = (int)naxis2;
[839]1095
1096 // get the datatype, names and the repeat count
[903]1097 noms.clear();
1098 noms.reserve(nbcols);
1099 types.clear();
1100 types.reserve(nbcols);
1101 repeat.clear();
1102 repeat.reserve(nbcols);
1103 taille_des_chaines.clear();
[839]1104 char **ttype = new char*[nbcols];
[923]1105 int ii;
[1175]1106 //
1107 //
[923]1108 for (ii=0; ii < nbcols; ii++) ttype[ii]=new char[FLEN_VALUE];
[839]1109 int nfound;
[903]1110 fits_read_keys_str(fileptr, "TTYPE",1,nbcols,ttype,&nfound, &status);
[839]1111 if( status ) printerror( status,"erreur lecture des noms de colonne");
1112 int rept=0;
[1300]1113 if(hdutype == ASCII_TBL)
[839]1114 {
[1300]1115 for(ii = 0; ii < nbcols; ii++)
[839]1116 {
[1300]1117 int DTYPE;
1118 long width;
1119 long repete = 0;
1120 fits_get_coltype(fileptr,ii+1,&DTYPE,&repete,&width,&status);
1121 if( status ) printerror( status,"erreur lecture type de colonne");
1122 rept = repete;
1123 noms.push_back(string(ttype[ii]));
1124 switch (DTYPE)
1125 {
1126 case TDOUBLE :
1127 types.push_back(FitsDataType_double);
1128 break;
1129 case TFLOAT :
1130 types.push_back(FitsDataType_float);
1131 break;
1132 case TLONG :
[1359]1133 types.push_back(FitsDataType_long);
[1300]1134 break;
1135 case TSHORT :
1136 types.push_back(FitsDataType_int);
1137 break;
1138 case TSTRING :
1139 types.push_back(FitsDataType_char);
1140 taille_des_chaines.push_back(width);
1141 rept/=width;
1142 break;
1143 default :
1144 cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
1145 throw IOExc("FitsFile::GetBinTabParameters, unsupported data type of field, for ASCII table");
1146 }
1147 repeat.push_back(rept);
[839]1148 }
[1136]1149 }
[1300]1150 else
1151 {
1152 for(ii = 0; ii < nbcols; ii++)
1153 {
1154 int DTYPE;
1155 long width;
1156 long repete = 0;
1157 fits_get_coltype(fileptr,ii+1,&DTYPE,&repete,&width,&status);
1158 if( status ) printerror( status,"erreur lecture type de colonne");
1159 rept = repete;
1160 noms.push_back(string(ttype[ii]));
1161 switch (DTYPE)
1162 {
1163 case TDOUBLE :
1164 types.push_back(FitsDataType_double);
1165 break;
1166 case TFLOAT :
1167 types.push_back(FitsDataType_float);
1168 break;
1169 case TLONG :
[1359]1170 types.push_back(FitsDataType_long);
[1300]1171 break;
1172 case TINT :
1173 types.push_back(FitsDataType_int);
1174 break;
1175 case TSHORT :
1176 types.push_back(FitsDataType_int);
1177 break;
1178 case TSTRING :
1179 types.push_back(FitsDataType_char);
1180 taille_des_chaines.push_back(width);
1181 rept/=width;
1182 break;
1183 case TBYTE :
[1359]1184 types.push_back(FitsDataType_byte);
[1300]1185 break;
1186 default :
1187 cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
1188 throw IOExc("FitsFile::GetBinTabParameters, unsupported data type of field, for BINTABLE");
1189 }
1190 repeat.push_back(rept);
1191 }
1192 }
[1136]1193 for (ii=0; ii < nbcols; ii++) delete [] ttype[ii];
1194 delete [] ttype;
1195}
1196
1197void FitsInFile::KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum)
1198{
1199 int status = 0;
1200 int hdutype;
1201 fits_movabs_hdu(fileptr,hdunum,&hdutype,&status);
1202 if( status ) printerror( status,":KeywordsIntoDVList : erreur movabs");
1203 // get number of keywords
1204 int nkeys,keypos;
1205 fits_get_hdrpos(fileptr,&nkeys,&keypos,&status);
1206 if( status ) printerror( status );
1207
1208 // put keywords in a DVList object
1209 char keyname[LEN_KEYWORD]= "";
1210 char strval[FLEN_VALUE]= "";
1211 char dtype;
1212 char card[FLEN_CARD];
1213 char *comkey = "COMMENT";
[1143]1214 char comment[FLEN_COMMENT];
[1136]1215
1216 // shift with the number of mandatory keywords
[1143]1217 // int num= 8;
1218 int num= 0;
1219 // primary header
1220 if (hdunum == 1)
1221 {
1222 // read NAXIS
1223 int naxis=0;
1224 fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
1225 // number of mandatory keywords
1226 num = naxis+3;
1227 }
1228 // extensions
1229 else
1230 {
1231 if (hdutype == IMAGE_HDU)
1232 {
1233 // read NAXIS
1234 int naxis=0;
1235 fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
1236 // number of mandatory keywords
1237 num = naxis+5;
1238 }
1239 else
1240 if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
1241 {
1242 // number of mandatory keywords
1243 num = 8;
1244 }
1245 }
[1136]1246 int j;
1247 for(j = num+1; j <= nkeys; j++)
1248 {
1249 fits_read_keyn(fileptr,j,card,strval,NULL,&status);
1250 if(status) printerror(status);
1251
1252 strncpy(keyname,card,LEN_KEYWORD-1);
1253 if(strncmp(keyname,comkey,LEN_KEYWORD-1) != 0 && strlen(keyname) != 0
1254 && strlen(strval) != 0)
1255 {
1256 fits_get_keytype(strval,&dtype,&status);
1257 if(status) printerror(status);
1258
1259 strip(keyname, 'B',' ');
1260 strip(strval, 'B',' ');
1261 strip(strval, 'B','\'');
1262
1263 switch( dtype )
1264 {
1265 case 'C':
[1143]1266 fits_read_key(fileptr,TSTRING,keyname,strval,comment,&status);
1267 dvl[keyname]= strval;
1268 dvl.SetComment(keyname, comment);
[1136]1269 break;
1270 case 'I':
1271 int ival;
[1143]1272 fits_read_key(fileptr,TINT,keyname,&ival,comment,&status);
[1136]1273 dvl[keyname]= (int_4) ival; // Portage mac DY
[1143]1274 dvl.SetComment(keyname, comment);
[1136]1275 break;
1276 case 'L':
1277 int ilog;
[1143]1278 fits_read_key(fileptr,TLOGICAL,keyname,&ilog,comment,&status);
[1136]1279 dvl[keyname]= (int_4) ilog;
[1143]1280 dvl.SetComment(keyname, comment);
[1136]1281 break;
1282 case 'F':
1283 double dval;
[1143]1284 fits_read_key(fileptr,TDOUBLE,keyname,&dval,comment,&status);
[1136]1285 dvl[keyname]= dval;
[1143]1286 dvl.SetComment(keyname, comment);
[1136]1287 break;
1288 }
1289
1290 }
[839]1291 }
[1136]1292 // dvl_.Print();
1293}
1294
[1218]1295
1296/*!
1297 \class SOPHYA::FitsOutFile
[1371]1298 \ingroup FitsIOServer
[1218]1299 Class for loading SOPHYA objects from FITS Format Files (uses cfitsio lib)
1300*/
1301
[1136]1302FitsOutFile::FitsOutFile()
1303{
[1193]1304 InitNull();
[903]1305}
[839]1306
[1218]1307 /*! \fn SOPHYA::FitsOutFile::FitsOutFile(char flnm[], WriteMode wrm)
1308
1309\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)
1310
1311 */
[1231]1312
1313FitsOutFile::FitsOutFile(string const & flnm, WriteMode wrm)
[1136]1314{
[1231]1315 InitNull();
1316 openoutputfitsfile(flnm.c_str(), wrm);
1317}
[839]1318
[1231]1319FitsOutFile::FitsOutFile(const char * flnm, WriteMode wrm)
1320{
[1136]1321 InitNull();
[1231]1322 openoutputfitsfile(flnm, wrm);
1323}
1324
1325void FitsOutFile::openoutputfitsfile(const char * flnm, WriteMode wrm)
1326{
[1136]1327 int status = 0;
[839]1328
[1136]1329 // create new FITS file
[1183]1330 fits_create_file(&fptr_,flnm,&status);
1331 if( status )
[1136]1332 {
[1193]1333
1334 switch (wrm)
1335 {
[1183]1336 // si on veut ecrire a la fin de ce fichier
[1193]1337 case append :
[1183]1338 status = 0;
[1235]1339 fits_clear_errmsg();
[1183]1340 fits_open_file(&fptr_,flnm,READWRITE,&status);
1341 if( status )
1342 {
1343 cout << " error opening file: " << flnm << endl;
1344 printerror(status, "failure opening a file supposed to exist");
1345 }
1346 else cout << " file " << flnm << " opened, new objects will be appended " << endl;
1347 fits_get_num_hdus(fptr_, &hdunum_, &status);
1348 int hdutype;
1349 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1350 if( status ) printerror( status,":FitsFile::WriteF : erreur movabs");
[1193]1351 break;
1352
1353 case clear :
[1183]1354 {
1355 status = 0;
[1235]1356 fits_clear_errmsg();
[1183]1357 char* newname = new char[strlen(flnm)+1];
1358 //
1359 newname[0] = '!';
1360 newname[1] = '\0';
1361 strcat(newname, flnm);
1362 fits_create_file(&fptr_,newname,&status);
[1193]1363 delete [] newname;
[1183]1364 if (status)
1365 {
1366 cout << " error opening file: " << flnm << endl;
1367 printerror(status, "unable to open file, supposed to exist");
1368 }
[1193]1369 else cout << " WARNING : file " << flnm << " is overwritten " << endl;
1370 break;
[1183]1371 }
[1193]1372 case unknown :
1373 printerror(status, " file seems already to exist");
1374 break;
[1183]1375
[1193]1376 }
[1136]1377 }
1378}
1379
1380
1381
[1218]1382/*! \fn void SOPHYA::FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList &dvl)
1383
1384create an IMAGE header on FITS file.
1385\param <type> type of data (see method ColTypeFromFits)
1386\param <nbdim> number of dimensions : 1D, 2D, 3D etc. = NAXIS
1387\param <naxisn> array containind sizes of the different dimensions
1388*/
[1221]1389void FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList* ptr_dvl)
[1136]1390{
1391 int status = 0;
1392 long naxis = nbdim;
1393 long* naxes = new long[nbdim];
[1246]1394 bool hdunfirst= (hdunum_ == 0);
1395 if (hdunfirst)
[1136]1396 {
[1143]1397 if (imageOnPrimary_ == false)
1398 {
[1234]1399 hdunum_ = 1;
[1143]1400 fits_create_img(fptr_,FLOAT_IMG,0,naxes,&status);
[1246]1401 }
[1136]1402 }
1403 int k;
1404 for (k=0; k< nbdim; k++) naxes[k] = (long)naxisn[k];
1405 if (type == 'D')
1406 fits_create_img(fptr_,DOUBLE_IMG,naxis,naxes,&status);
1407 else
1408 if (type == 'E')
1409 fits_create_img(fptr_,FLOAT_IMG,naxis,naxes,&status);
1410 else
1411 if (type == 'I')
1412 fits_create_img(fptr_,LONG_IMG,naxis,naxes,&status);
1413 else
1414 {
1415 cout << " type of data: " << type << endl;
1416 throw PException("FitsFile:::makeHeaderImageOnFits:unprogrammed type of data ");
1417 }
[1353]1418
[1246]1419 // on ajoute eventuellement un dvlist prepare et la doc SOPHYA
[1136]1420 hdunum_++;
[1246]1421 if (hdunfirst)
1422 {
1423 addDVListOnPrimary();
1424 writeSignatureOnFits(1);
1425 }
[1143]1426
[1353]1427 // header format FITS
1428
1429 writeAppendedHeaderOnFits();
1430
1431 // write supplementary keywords (from SOPHYA)
[1143]1432 // dvl.Print();
[1221]1433 if (ptr_dvl != NULL) addKeywordsOfDVList(*ptr_dvl);
[1143]1434
[1136]1435 delete [] naxes;
1436 if( status ) printerror( status, "erreur creation HDU IMAGE" );
1437
1438}
[1218]1439
1440
1441/*! \fn void SOPHYA::FitsOutFile::PutImageToFits(int nbData, double* map) const
1442
1443write double data from array 'map'on an IMAGE extension
1444\param <nbData> number of data to be written
1445*/
[1379]1446void FitsOutFile::PutImageToFits(int nbData, r_8* map) const
[1136]1447{
1448 int status = 0;
1449 long npix= nbData;
1450 fits_write_img(fptr_,TDOUBLE,1,npix,map,&status);
[1209]1451 if( status ) printerror( status, "erreur ecriture PutImageToFits" );
[1136]1452}
1453
[1218]1454/*! \fn void SOPHYA::FitsOutFile::PutImageToFits(int nbData, float* map) const
1455
1456same as previous method with float data
1457*/
[1379]1458void FitsOutFile::PutImageToFits(int nbData, r_4* map) const
[1136]1459{
1460 int status = 0;
1461 long npix= nbData;
1462 fits_write_img(fptr_,TFLOAT,1,npix, map,&status);
[1209]1463 if( status ) printerror( status, "erreur ecriture PutImageToFits" );
[1136]1464
1465}
[1218]1466
1467 /*! \fn void SOPHYA::FitsOutFile::PutImageToFits( int nbData, int* map) const
1468
1469 same as previous method with int data */
[1379]1470void FitsOutFile::PutImageToFits( int nbData, int_4* map) const
[1136]1471{
1472 int status = 0;
1473
1474 long npix= nbData;
1475 fits_write_img(fptr_,TINT,1,npix,map,&status);
[1209]1476 if( status ) printerror( status, "erreur ecriture PutImageToFits" );
[1136]1477}
1478
1479
1480
[1218]1481/*! \fn void SOPHYA::FitsOutFile::makeHeaderBntblOnFits( string fieldType, vector<string> Noms, int nentries, int tfields, DVList &dvl, string extname, vector<int> taille_des_chaines)
1482
1483create an BINTABLE header on FITS file.
1484\param <fieldType> array conta
1485ining characters denoting types of the different column (see method ColTypeFromFits)
1486\param <Noms> array of the names of columns
1487\param <nentries> number of data of each column
1488\param <tfields> number of columns
1489\param <dvl> a SOPHYA DVList containing keywords to be appended
1490\param <extname> keyword EXTNAME for FITS file
1491\param <taille_des_chaines> vector containing the number of characters of data for each char* typed column, with order of appearance in 'fieldType'
1492*/
[1300]1493void FitsOutFile::makeHeaderBntblOnFits(string fieldType, vector<string> Noms, int nentries, int tfields, DVList* ptr_dvl, string extname, vector<int> taille_des_chaines)
[839]1494{
[1209]1495 int k;
[839]1496 int status = 0;
1497 long nrows;
[1300]1498 // verifications de coherences
[1209]1499
[1193]1500 if (fieldType.length() != tfields)
[839]1501 {
[1193]1502 cout << " nombre de champs :" << tfields << "nombre de types: " << fieldType.length() << endl;
[1136]1503 throw ParmError("FitsFile:: fields and types don't match");
[839]1504
1505 }
[1209]1506 if (tfields > Noms.size())
1507 {
1508 cout << " WARNING: FitsOutFile::makeHeaderBntblOnFits, length of vector of column names not equal to total number of columns" << endl;
1509 for (k=0; k<(tfields-Noms.size()); k++) Noms.push_back( string(" "));
1510 }
1511
1512 // nombre de variables "chaines de caracteres"
1513 int nbString = 0;
1514 for (k=0; k<tfields;k++) if (fieldType[k] == 'A') nbString++;
1515 // coherence de la longueur du vecteur des tailles
1516 if (nbString > taille_des_chaines.size())
1517 {
1518 cout << " WARNING: FitsOutFile::makeHeaderBntblOnFits, length of vector of string lengths not equal to total number of columns" << endl;
1519 int strSz=0;
1520 for (k=0; k<taille_des_chaines.size(); k++) if ( taille_des_chaines[k] > strSz) strSz = taille_des_chaines[k];
1521 for (k=0; k<(nbString-taille_des_chaines.size()); k++) taille_des_chaines.push_back(strSz);
1522 }
[839]1523 char ** ttype= new char*[tfields];
1524 char ** tform= new char*[tfields];
1525 char largeur[FLEN_VALUE];
1526 int noColString=0;
[971]1527 for (k=0; k<tfields;k++)
[839]1528 {
1529 char format[FLEN_VALUE];
1530
1531 if(nentries < 1024)
1532 {
1533 nrows= nentries;
1534 if (fieldType[k] == 'A')
1535 {
1536 sprintf(largeur,"%d",taille_des_chaines[noColString++]);
1537 strcpy(format,largeur);
1538 }
1539 else strcpy(format,"1");
1540 }
1541 else
1542 {
1543 nrows = nentries/1024;
1544 if(nentries%1024 != 0) nrows++;
1545 if (fieldType[k] == 'A')
1546 {
[1136]1547 char largaux[FLEN_VALUE];
1548 sprintf(largeur,"%d",taille_des_chaines[noColString]);
1549 sprintf(largaux,"%d",1024*taille_des_chaines[noColString]);
1550 noColString++;
1551 strcpy(format, largaux);
[839]1552 }
1553 else strcpy(format,"1024");
1554 }
1555 strncat(format,&fieldType[k],1);
1556 if (fieldType[k] == 'A')
1557 {
1558 strcat(format,largeur);
1559 }
[1193]1560 ttype[k] = const_cast<char*>(Noms[k].c_str());
[839]1561 tform[k]= new char[FLEN_VALUE];
1562 strcpy(tform[k],format);
1563 }
[1193]1564 char* extn = const_cast<char*>(extname.c_str());
[839]1565
1566 // create a new empty binary table onto the FITS file
1567 // physical units if they exist, are defined in the DVList object
1568 // so the NULL pointer is given for the tunit parameters.
1569 nrows=0;
1570 fits_create_tbl(fptr_,BINARY_TBL,nrows,tfields,ttype,tform,
1571 NULL,extn,&status);
1572 if( status ) printerror( status );
[1353]1573
1574 int ii;
1575 for(ii = 0; ii < tfields; ii++)
1576 {
1577 delete [] tform[ii];
1578 }
1579 delete [] ttype;
1580 delete [] tform;
1581
1582 // on ajoute eventuellement des mots-cles
1583
[1246]1584 if ( hdunum_ == 0 )
1585 {
1586 hdunum_ = 2;
1587 addDVListOnPrimary();
1588 writeSignatureOnFits(1);
1589 }
[1026]1590 else hdunum_++;
[1353]1591
1592 // header format FITS
1593
1594 writeAppendedHeaderOnFits();
1595
1596 // write SOPHYA keywords
[1221]1597 if (ptr_dvl != NULL) addKeywordsOfDVList(*ptr_dvl);
[839]1598}
1599
[1353]1600
1601
[1218]1602/*! \fn void SOPHYA::FitsOutFile::PutColToFits(int nocol, int nentries, double* donnees) const
1603
1604write double data from array 'donnees ' on column number 'nocol' of a BINTABLE extension.
1605\param <nentries> number of data to be written
1606*/
[1353]1607
[1379]1608void FitsOutFile::PutColToFits(int nocol, int nentries, r_8* donnees) const
[839]1609{
1610 int status = 0;
[971]1611 int hdutype;
[839]1612 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
[1209]1613 if( status ) printerror(status,"PutColToFits: le movabs a foire");
[839]1614 fits_get_hdu_type(fptr_, &hdutype, &status);
[867]1615 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1616 {
1617 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
[1209]1618 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
[867]1619 }
[839]1620 int code;
1621 long repeat, width;
1622 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1623 if( code != TDOUBLE)
1624 {
[1209]1625 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " to be written= DOUBLE " << endl;
[839]1626 }
[1499]1627 // cout << " 10 elements de colonne " << endl;
1628 // for (int toto=0; toto < 10; toto++) cout << donnees[toto] << endl;
[839]1629 fits_write_col(fptr_,TDOUBLE,nocol+1,1,1,nentries, donnees ,&status);
[1752]1630 if( status ) printerror( status,"erreur ecriture col. double, dans fichier fits" );
[839]1631}
[1218]1632
1633
1634
1635 /*! \fn void SOPHYA::FitsOutFile::PutColToFits(int nocol, int nentries, float* donnees) const
1636
1637same as previous method with float data
1638*/
[1379]1639void FitsOutFile::PutColToFits(int nocol, int nentries, r_4* donnees) const
[839]1640{
1641 int status = 0;
1642 int hdutype;
1643 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
[1209]1644 if( status ) printerror(status,"PutColToFits: le movabs a foire");
[839]1645 fits_get_hdu_type(fptr_, &hdutype, &status);
1646 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1647 {
1648 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
[1209]1649 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
[839]1650 }
1651 if(hdutype == ASCII_TBL && nocol>0)
1652 {
[1209]1653 throw IOExc("FitsFile::PutColToFits, this HDU is an ASCII table, nocol>0 forbidden");
[839]1654 }
1655 int code;
1656 long repeat, width;
1657 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1658 if( code != TFLOAT)
1659 {
[1209]1660 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " (FITS code), to be written= FLOAT " << endl;
[839]1661 }
1662 fits_write_col(fptr_,TFLOAT,nocol+1,1,1,nentries, donnees ,&status);
[1752]1663 if( status ) printerror( status,"erreur ecriture col. floats, dans fichier fits" );
[839]1664}
[1218]1665
1666
1667/*! \fn void FitsOutFile::PutColToFits(int nocol, int nentries, int* donnees) const
1668
1669same as previous method with int data
1670*/
[1379]1671void FitsOutFile::PutColToFits(int nocol, int nentries, int_4* donnees) const
[839]1672{
1673 int status = 0;
1674 int hdutype;
1675 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
[1209]1676 if( status ) printerror(status,"PutColToFits: le movabs a foire");
[839]1677 fits_get_hdu_type(fptr_, &hdutype, &status);
1678 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1679 {
1680 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
[1209]1681 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
[839]1682 }
1683 if(hdutype == ASCII_TBL && nocol>0)
1684 {
[1209]1685 throw IOExc("FitsFile::PutColToFits, this HDU is an ASCII table, nocol>0 forbidden");
[839]1686 }
1687 int code;
1688 long repeat, width;
1689 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
[1752]1690
1691
1692// la logique voudrait qu'on distingue TLONG et TINT. Mais si j'ecris
1693// et relis immediatement quelque chose en TLONG l'experience montre
1694// que ca foire. Donc, je fais tout en TINT, d'ailleurs cfitsio n'a pas
1695// (apparemment) d'entiers de longueur superieure a 32 bits.
1696// En fait, je n'y comprend rien. A suivre (GLM).
1697 if (code == TINT || code == TLONG )
[839]1698 {
[1752]1699 cout << " j'ecris des TINT" << endl;
1700 fits_write_col(fptr_,TINT,nocol+1,1,1,nentries, donnees ,&status);
1701 }
1702 else if (code == TSHORT)
1703 {
1704 cout << " j'ecris des TSHORT " << endl;
1705 fits_write_col(fptr_,TSHORT,nocol+1,1,1,nentries, donnees ,&status);
1706 }
1707 else
1708 {
1709 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " (FITS code), to be written= integers " << endl;
[839]1710 }
[1752]1711 if( status ) printerror( status,"erreur ecriture col. entiers, dans fichier fits" );
[839]1712}
[1218]1713
1714
1715/*! \fn void SOPHYA::FitsOutFile::PutColToFits(int nocol, int nentries, char** donnees) const
1716same as previous method with char* data
1717*/
[1209]1718void FitsOutFile::PutColToFits(int nocol, int nentries, char** donnees) const
[839]1719{
1720 int status = 0;
1721 int hdutype;
1722 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
[1209]1723 if( status ) printerror(status,"PutColToFits: le movabs a foire");
[839]1724 fits_get_hdu_type(fptr_, &hdutype, &status);
1725 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1726 {
1727 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
[1209]1728 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
[839]1729 }
1730 if(hdutype == ASCII_TBL && nocol>0)
1731 {
[1209]1732 throw IOExc("FitsFile::PutColToFits, this HDU is an ASCII table, nocol>0 forbidden");
[839]1733 }
1734 int code;
1735 long repeat, width;
1736 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1737 if( code != TSTRING)
1738 {
[1209]1739 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " (FITS code), to be written= char** " << endl;
[839]1740 }
1741 fits_write_col(fptr_,TSTRING,nocol+1,1,1,nentries, donnees ,&status);
[1752]1742 if( status ) printerror( status,"erreur ecriture col. chars, dans fichier fits" );
[839]1743}
1744
[1209]1745void FitsOutFile::PutBinTabLine(long NoLine, BnTblLine& ligne) const
[1193]1746{
[1209]1747 // on ne fait pas de verification de type, ni de dimension ici, pour
1748 // des raisons de performances
1749 int k;
[1193]1750 int status= 0;
1751 int anull;
[1209]1752 int ncol=0;
[1193]1753 long nels=1;
[1209]1754 // int nbcols;
1755 // fits_get_num_cols(fptr_, &nbcols,&status);
1756 for (k=0; k<ligne.ddata_.size(); k++, ncol++)
[1193]1757 {
[1209]1758 fits_write_col(fptr_,TDOUBLE,ncol+1,NoLine+1,1,1, &ligne.ddata_[k] ,&status);
1759 if( status ) printerror( status, "PutBinTabLine : erreur ecriture double" );
[1193]1760 }
[1209]1761 for (k=0; k<ligne.fdata_.size(); k++, ncol++)
1762 {
1763 fits_write_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1, &ligne.fdata_[k] ,&status);
1764 if( status ) printerror( status, "PutBinTabLine : erreur ecriture float" );
1765 }
1766 for (k=0; k<ligne.idata_.size(); k++, ncol++)
1767 {
1768 fits_write_col(fptr_,TINT,ncol+1,NoLine+1,1,1, &ligne.idata_[k] ,&status);
1769 if( status ) printerror( status, "PutBinTabLine : erreur ecriture entier" );
1770 }
[1359]1771 for (k=0; k<ligne.ldata_.size(); k++, ncol++)
1772 {
1773 fits_write_col(fptr_,TLONG,ncol+1,NoLine+1,1,1, &ligne.ldata_[k] ,&status);
1774 if( status ) printerror( status, "PutBinTabLine : erreur ecriture entier long" );
1775 }
1776 for (k=0; k<ligne.bdata_.size(); k++, ncol++)
1777 {
1778 fits_write_col(fptr_,TBYTE,ncol+1,NoLine+1,1,1, &ligne.bdata_[k] ,&status);
1779 if( status ) printerror( status, "PutBinTabLine : erreur ecriture byte" );
1780 }
[1209]1781
1782 for (k=0; k<ligne.cdata_.size(); k++, ncol++)
1783 {
[1220]1784 fits_write_col(fptr_,TSTRING,ncol+1,NoLine+1,1,1, (void*)ligne.cdata_[k].c_str() ,&status);
[1209]1785 if( status ) printerror( status, "PutBinTabLine : erreur ecriture caracteres" );
1786 }
[1193]1787}
1788
1789
[1218]1790/* \fn void SOPHYA::FitsOutFile::DVListIntoPrimaryHeader(DVList& dvl) const
1791
1792Put keywords from a DVList into the primary header of the fits-file
1793*/
[1246]1794void FitsOutFile::DVListIntoPrimaryHeader(DVList& dvl)
[1143]1795{
1796 int status = 0;
1797 int hdutype;
[1246]1798 if (hdunum_ == 0)
1799 {
1800 if (dvlToPrimary_ == NULL) dvlToPrimary_ = new DVList(dvl);
1801 else dvlToPrimary_->Merge(dvl);
1802 }
1803 else
1804 {
1805 fits_movabs_hdu(fptr_,1,&hdutype,&status);
1806 addKeywordsOfDVList(dvl);
1807 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1808 }
[1143]1809}
[839]1810
[1143]1811
[1246]1812void FitsOutFile::writeSignatureOnFits(int hdunum) const
[839]1813{
1814 int status = 0;
[1246]1815 int hdutype;
[839]1816 char keyname[LEN_KEYWORD];
1817 char strval[FLEN_VALUE];
1818 char comment[FLEN_COMMENT];
[1246]1819 if (hdunum_ == 0)
1820 {
1821 cerr << " WARNING : can't write keywords on non existing primary header" << endl;
1822 return;
1823 }
1824 fits_movabs_hdu(fptr_,1,&hdutype,&status);
1825 //
[971]1826 strncpy(keyname, "CREATOR", LEN_KEYWORD);
[1418]1827 keyname[7] = '\0';
[971]1828 strcpy(strval, "SOPHYA");
1829 strcpy(comment," SOPHYA Package - FITSIOServer ");
1830 fits_write_key(fptr_, TSTRING, keyname, &strval, comment, &status);
1831 if( status ) printerror( status );
[1143]1832 fits_write_date(fptr_, &status);
[971]1833 fits_write_comment(fptr_,"..............................................", &status);
1834 fits_write_comment(fptr_, " SOPHYA package - FITSIOSever ", &status);
1835 fits_write_comment(fptr_, " (C) LAL/IN2P3-CNRS Orsay, FRANCE 2000", &status);
1836 fits_write_comment(fptr_, " (C) DAPNIA/CEA Saclay, FRANCE 2000", &status);
1837 fits_write_comment(fptr_,"..............................................", &status);
[1045]1838 if( status ) printerror( status, "erreur writeSignatureOnFits" );
[1246]1839 //
1840 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
[839]1841}
1842
[903]1843
[1246]1844void FitsOutFile::addKeywordsOfDVList( DVList& dvl) const
[1143]1845{
1846 int status = 0;
1847 fits_write_comment(fptr_,"---------- keywords from SOPHYA ---------", &status);
1848 DVList::ValList::const_iterator it;
1849 for(it = dvl.Begin(); it != dvl.End(); it++)
1850 {
[1311]1851 MuTyV::MTVType keytype= (*it).second.elval.Type();
[1418]1852 char keyname[LEN_KEYWORD];
1853 strncpy(keyname,(*it).first.substr(0,64).c_str(),LEN_KEYWORD);
1854 int bout = ((*it).first.substr(0,64).length() < LEN_KEYWORD) ? (*it).first.substr(0,64).length() : LEN_KEYWORD-1;
1855 keyname[bout] = '\0';
1856 string key((*it).first.substr(0,64));
1857 // string key(keyname);
[1143]1858 char comment[FLEN_COMMENT];
1859 char strval[FLEN_VALUE]= "";
1860 char *comkey = "COMMENT";
[1353]1861 // fits_read_keyword(fptr_, keyname, strval, NULL, &status);
1862 // if (status != 0 || strncmp(keyname,comkey,LEN_KEYWORD-1) == 0 )
[1143]1863 {
[1183]1864 string coco = dvl.GetComment(key);
1865 coco.copy( comment, FLEN_COMMENT-1);
1866 int bout = (coco.length() < FLEN_COMMENT) ? coco.length() : FLEN_COMMENT-1;
1867 comment[bout]= '\0';
[1143]1868 status = 0;
1869 switch (keytype)
1870 {
[1311]1871 case MuTyV::MTVInteger :
[1143]1872 {
[1183]1873 int ival = (int)dvl.GetI(key);
1874 fits_write_key(fptr_,TINT,keyname,&ival, comment,&status);
[1143]1875 break;
1876 }
[1311]1877 case MuTyV::MTVFloat :
[1143]1878 {
[1183]1879 double dval= (double)dvl.GetD(key);
[1143]1880 fits_write_key(fptr_,TDOUBLE,keyname,&dval,comment,&status);
1881 break;
1882 }
[1311]1883 case MuTyV::MTVString :
[1143]1884 {
[1183]1885 char strvaleur[FLEN_VALUE]= "";
1886 string valChaine = dvl.GetS(key);
1887 valChaine.copy(strvaleur, FLEN_VALUE-1);
1888 int fin = (valChaine.length() < FLEN_VALUE) ? valChaine.length() : FLEN_VALUE-1;
1889 strvaleur[fin]= '\0';
1890
1891 fits_write_key(fptr_,TSTRING,keyname,&strvaleur,comment,&status);
[1143]1892 break;
1893 }
1894 }
1895 }
1896 if( status ) printerror( status,"fitsfile: probleme ecriture mot-cle du dvlist" );
1897 }
1898 fits_write_comment(fptr_,"--------------------------------------", &status);
1899}
[903]1900
1901
[1246]1902void FitsOutFile::addDVListOnPrimary()
1903 {
1904 int status = 0;
1905 int hdutype;
1906 if (hdunum_ == 0)
1907 {
1908 cerr << " WARNING : can't write keywords on non existing primary header" << endl;
1909 return;
1910 }
1911 if (dvlToPrimary_ != NULL)
1912 {
1913 fits_movabs_hdu(fptr_,1,&hdutype,&status);
1914 addKeywordsOfDVList(*dvlToPrimary_);
1915 delete dvlToPrimary_;
1916 dvlToPrimary_ = NULL;
1917 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1918 }
1919 }
[839]1920
[1353]1921
1922/*! \fn void FitsOutFile::appendInHeader(FitsInFile& infits, int hdunum)
1923
1924get a header from FitsInFile and append to the header beeing built
1925(shifting mandatory keywords)
1926*/
1927
1928void FitsOutFile::appendInputHeader(FitsInFile& infits, int hdunum)
1929{
1930 int status = 0;
1931 int hdutype;
1932 fitsfile* fptr=infits.fitsfilePtr();
1933 fits_movabs_hdu(fptr,hdunum,&hdutype,&status);
1934 if( status ) fits_report_error(stderr,status);
1935
1936 // get number of keywords
1937 int nkeys,keypos;
1938 fits_get_hdrpos(fptr,&nkeys,&keypos,&status);
1939 if( status ) fits_report_error(stderr,status);
1940 // shift with the number of mandatory keywords
1941 int num= 0;
1942 // if primary header
1943 if (hdunum == 1)
1944 {
1945 // read NAXIS
1946 int naxis=0;
1947 fits_read_key(fptr,TINT,"NAXIS",&naxis,NULL,&status);
1948 // number of mandatory keywords
1949 num = naxis+3;
1950 }
1951 // extensions
1952 else
1953 {
1954 if (hdutype == IMAGE_HDU)
1955 {
1956 // read NAXIS
1957 int naxis=0;
1958 fits_read_key(fptr,TINT,"NAXIS",&naxis,NULL,&status);
1959 // number of mandatory keywords
1960 num = naxis+5;
1961 }
1962 else
1963 if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
1964 {
1965 // number of mandatory keywords
1966 num = 8;
1967 }
1968 }
1969 int j;
1970 char keyname[LEN_KEYWORD];
1971 char value[FLEN_VALUE];
1972 char comment[FLEN_COMMENT];
1973 for(j = num+1; j <= nkeys; j++)
1974 {
1975 char dtype;
1976 fits_read_keyn(fptr,j,keyname,value,comment,&status);
1977 if(status)
1978 {
1979 fits_report_error(stderr,status);
1980 status=0;
1981 }
1982 string kn(keyname);
1983 string cm(comment);
1984 string val(value);
1985 FitsKeyword kw(kn, val, cm);
1986 mots_cles_.push_back(kw);
1987 }
1988}
1989void FitsOutFile::writeAppendedHeaderOnFits()
1990{
1991 for (list<FitsKeyword>::iterator it=mots_cles_.begin(); it !=mots_cles_.end(); it++)
1992 {
1993 (*it).writeOnFits(fptr_);
1994 }
1995 mots_cles_.clear();
1996}
1997
1998void FitsOutFile::insertKeywordOnHeader(string keyname, double value, string comment)
1999{
[1418]2000 char cvalue[16];
[1353]2001 sprintf(cvalue,"%e",value);
[1418]2002 FitsKeyword kw(keyname, string(cvalue), comment, 'F');
[1353]2003 mots_cles_.push_back(kw);
2004}
[1418]2005void FitsOutFile::insertKeywordOnHeader(string keyname, int value, string comment)
2006{
2007 char cvalue[16];
2008 sprintf(cvalue,"%d",value);
2009 FitsKeyword kw(keyname, string(cvalue), comment, 'I');
2010 mots_cles_.push_back(kw);
2011}
2012void FitsOutFile::insertKeywordOnHeader(string keyname, string value, string comment)
2013{
2014 FitsKeyword kw(keyname, value , comment, 'C');
2015 mots_cles_.push_back(kw);
2016}
[1353]2017
2018void FitsOutFile::insertCommentLineOnHeader(string comment)
2019{
2020 FitsKeyword kw(comment);
2021 mots_cles_.push_back(kw);
2022}
2023
2024void FitsOutFile::PrintHeaderToBeAppended()
2025{
2026 cout << " contenu du header en cours de fabrication " << endl;
2027 for (list<FitsKeyword>::iterator it=mots_cles_.begin(); it !=mots_cles_.end(); it++)
2028 {
2029 (*it).Print();
2030 }
2031}
2032
2033
2034FitsKeyword::FitsKeyword()
2035 {
2036 datatype_=' ';
2037 keyname_ = string("");
2038 dvalue_=0.;
2039 ivalue_=1;
2040 svalue_=string("");
2041 comment_=string("");
2042 }
2043
2044FitsKeyword::FitsKeyword(string comment)
2045 {
2046 datatype_=' ';
2047 keyname_=string("COMMENT");
2048 comment_=comment;
[1418]2049 }
[1353]2050
2051FitsKeyword::FitsKeyword(string keyname, string value, string comment) : keyname_(keyname), comment_(comment)
2052 {
2053 int status=0;
2054 char dtype;
2055 const char* val= value.c_str();
2056 char* valk = const_cast<char*>(val);
2057 fits_get_keytype(valk,&dtype,&status);
2058 if(status)
2059 {
2060 status=0;
[1354]2061 if (status == VALUE_UNDEFINED) cout << "WARNING (FitsKeyword) : undefined keyword value " << endl;
[1353]2062 datatype_=' ';
2063 }
2064 else datatype_=dtype;
2065
2066 switch( datatype_ )
2067 {
2068 case 'C':
2069 {
[1354]2070 strip(valk, 'B','\'');
2071 svalue_ = string(valk);
[1353]2072 break;
2073 }
2074 case 'I':
2075 {
2076 ivalue_ = atoi(val);
2077 break;
2078 }
2079 case 'L':
2080 {
[1354]2081 bool bb = value.c_str();
2082 ivalue_ = (int)bb;
[1353]2083 break;
2084 }
2085 case 'F':
2086 {
2087 dvalue_ = atof(val);
2088 break;
2089 }
2090 case 'X':
2091 {
2092 throw IOExc("FitsKeyword , complex keyword value not supported");
2093 }
2094 }
2095 }
2096
[1418]2097// constructeur pour les mots-cles maison (ne prvenant pas de la lecture d'un fichier fits)
2098FitsKeyword::FitsKeyword(string keyname, string value, string comment, char type) : keyname_(keyname), comment_(comment), datatype_(type)
2099 {
2100 char dtype;
2101 const char* val= value.c_str();
2102 char* valk = const_cast<char*>(val);
2103 switch( datatype_ )
2104 {
2105 case 'C':
2106 {
2107 strip(valk, 'B','\'');
2108 svalue_ = string(valk);
2109 break;
2110 }
2111 case 'I':
2112 {
2113 ivalue_ = atoi(val);
2114 break;
2115 }
2116 case 'L':
2117 {
2118 bool bb = value.c_str();
2119 ivalue_ = (int)bb;
2120 break;
2121 }
2122 case 'F':
2123 {
2124 dvalue_ = atof(val);
2125 break;
2126 }
2127 case 'X':
2128 {
2129 throw IOExc("FitsKeyword , complex keyword value not supported");
2130 }
2131 }
2132 }
2133
[1353]2134void FitsKeyword::writeOnFits(fitsfile* ptr)
2135 {
2136 int status=0;
[1418]2137 char keyname[LEN_KEYWORD];
2138 char comment[FLEN_COMMENT];
2139 keyname_.copy(keyname, LEN_KEYWORD);
2140 int bout = (keyname_.length() < LEN_KEYWORD) ? keyname_.length() : LEN_KEYWORD-1;
2141 keyname[bout] = '\0';
2142 comment_.copy( comment, FLEN_COMMENT);
2143 bout = (comment_.length() < FLEN_COMMENT) ? comment_.length() : FLEN_COMMENT-1;
2144 comment[bout]= '\0';
2145
[1353]2146 int nkeys,keypos;
2147 fits_get_hdrpos(ptr,&nkeys,&keypos,&status);
2148 switch( datatype_ )
2149 {
2150 case 'C':
2151 {
[1418]2152 char value[FLEN_VALUE]="";
2153 svalue_.copy(value, FLEN_VALUE-1);
2154 int fin = (svalue_.length() < FLEN_VALUE) ? svalue_.length() : FLEN_VALUE-1;
2155 value[fin]= '\0';
[1353]2156 fits_write_key(ptr,TSTRING,keyname,&value, comment,&status);
2157 fits_report_error(stderr,status);
2158 break;
2159 }
2160 case 'I':
2161 {
2162 fits_write_key(ptr,TINT,keyname,&ivalue_, comment,&status);
2163 fits_report_error(stderr,status);
2164 break;
2165 }
2166 case 'L':
2167 {
2168 fits_write_key(ptr,TLOGICAL,keyname,&ivalue_, comment,&status);
2169 fits_report_error(stderr,status);
2170 break;
2171 }
2172 case 'F':
2173 {
2174 fits_write_key(ptr,TDOUBLE,keyname,&dvalue_, comment,&status);
2175 fits_report_error(stderr,status);
2176 break;
2177 }
2178 case 'X':
2179 {
2180 cout << "FitsKeyword : complex keyword value not supported" << endl;;
2181 }
2182 default :
2183 {
2184 char *comkey = "COMMENT";
2185 if(strncmp(keyname,comkey,LEN_KEYWORD-1) == 0)
2186 {
2187 fits_write_comment(ptr,comment,&status);
2188 fits_report_error(stderr,status);
2189 }
2190 else
2191 {
2192 cout << " WARNING (FitsKeyword::writeOnFits) : unrecognized keyword : " << keyname_ << endl;
2193 }
2194 }
2195 }
2196 }
2197
2198void FitsKeyword::Print()
2199 {
2200 switch( datatype_ )
2201 {
2202 case 'C':
2203 {
2204 cout << " mot cle : " << keyname_ << " valeur : " << svalue_ << " commentaire : " << comment_ <<endl;
2205 break;
2206 }
2207 case 'I':
2208 {
2209 cout << " mot cle : " << keyname_ << " valeur : " << ivalue_ << " commentaire : " << comment_ <<endl;
2210 break;
2211 }
2212 case 'L':
2213 {
2214 cout << " mot cle : " << keyname_ << " valeur : " << ivalue_ << " commentaire : " << comment_ <<endl;
2215 break;
2216 }
2217 case 'F':
2218 {
2219 cout << " mot cle : " << keyname_ << " valeur : " << dvalue_ << " commentaire : " << comment_ <<endl;
2220 break;
2221 }
2222 case 'X':
2223 {
2224 cout << "FitsKeyword : complex keyword value not supported" << endl;;
2225 }
2226 default :
2227 {
2228 cout << " mot cle : " << keyname_ << " commentaire : " << comment_ <<endl;
2229 }
2230 }
2231 }
Note: See TracBrowser for help on using the repository browser.