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

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

mots-cles maison type string

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