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

Last change on this file since 2173 was 2129, checked in by lemeur, 23 years ago

rajout d'un break disparu intempestivement

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