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

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

extension bntblLineRW aux long et byte

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