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

Last change on this file since 1371 was 1371, checked in by ansari, 25 years ago

MAJ documentation, Makefile, ... - Reza 5/1/2001

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