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

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

lecture xntuple par blocs et complement de types

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