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

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

mises a jour pour ELDESTINO

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