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

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

lecture de spheres fits, sans NSIDE ni LASTPIX

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