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

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

lecture xntuple par blocs et complement de types

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