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

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

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

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