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

Last change on this file since 2013 was 1978, checked in by lemeur, 23 years ago

suppression mots cles FITS de format, dans dvlist, en lecture

File size: 63.1 KB
Line 
1#include "machdefs.h"
2#include <stdlib.h>
3#include "fitsfile.h"
4#include "pexceptions.h"
5#include "strutil.h"
6#include "anydataobj.h"
7#include "fitsspherehealpix.h"
8
9/*!
10 \defgroup FitsIOServer FitsIOServer module
11 This module contains classes which handle FITS format I/O for
12 SOPHYA objects. This module uses cfitsio library.
13*/
14
15void BnTblLine::setFormat(int dc, int fc, int ic, int lc, int bc,int cc, vector<string> names)
16 {
17 int nbcols = dc + fc + ic + cc + lc + bc;
18 int maxName = names.size();
19 if (nbcols != maxName)
20 {
21 cout << " WARNING: BnTblLine:: length of vector of column names not equal to total number of columns" << endl;
22 maxName = nbcols < maxName ? nbcols : maxName;
23 }
24 ColName_ = vector<string>(nbcols);
25 for (int k=0; k < maxName; k++) ColName_[k] = names[k];
26 if (dc >0) ddata_ = vector<double>(dc);
27 if (fc >0) fdata_ = vector<float>(fc);
28 if (ic >0) idata_ = vector<int>(fc);
29 if (cc >0) cdata_ = vector<string>(fc);
30 if (lc >0) ldata_ = vector<long>(lc);
31 if (bc >0) bdata_ = vector<unsigned char>(bc);
32 }
33
34bool BnTblLine::sameFormat(const BnTblLine& btl) const
35 {
36 if (btl.ddata_.size() == ddata_.size() && btl.fdata_.size() == fdata_.size() && btl.idata_.size() == idata_.size() && btl.cdata_.size() == cdata_.size() && btl.ldata_.size() == ldata_.size() && btl.bdata_.size() == bdata_.size()) return true;
37 else return false;
38 }
39
40void BnTblLine::Print()
41 {
42 int k;
43 cout << " ********* ligne ************* " << endl;
44 cout << " *** noms de variables " << endl;
45 for (k=0; k < ColName_.size(); k++) cout << ColName_[k] << " ";
46 cout << endl;
47 cout << " *** variables doubles " << endl;
48 for (k=0; k < ddata_.size(); k++) cout << ddata_[k] << " ";
49 cout << endl;
50 cout << " *** variables float " << endl;
51 for (k=0; k < fdata_.size(); k++) cout << fdata_[k] << " ";
52 cout << endl;
53 cout << " *** variables int " << endl;
54 for (k=0; k < idata_.size(); k++) cout << idata_[k] << " ";
55 cout << endl;
56 cout << " *** variables string " << endl;
57 for (k=0; k < cdata_.size(); k++) cout << cdata_[k] << " ";
58 cout << endl;
59 cout << " *** variables long " << endl;
60 for (k=0; k < ldata_.size(); k++) cout << ldata_[k] << " ";
61 cout << endl;
62 cout << " *** variables byte " << endl;
63 for (k=0; k < bdata_.size(); k++) cout << (int)bdata_[k] << " ";
64 cout << endl;
65 cout << " ***************************** " << endl;
66 }
67
68
69
70/*!
71 \class SOPHYA::FitsIOHandler
72 \ingroup FitsIOServer
73The class structure is analogous to Sophya-PPersist system :
74Each SOPHYA object XXX is associated with a object of class FITS_XXX
75 (inheriting from FitsFileHandler), to which input/output operations with FITS
76 files are delegated (through a class Hierarchy : FitsFile (virtual),
77 FitsInFile, FitsOutFile) . A typical example of use is the following :
78
79\verbatim
80 int m=... ;
81 SphereHEALPix<r_8> sphere1(m); // definition of the SOPHYA object
82 .... fill the sphere ....
83
84 FITS_SphereHEALPix<r_8> fits_sph1(sphere1);
85 // delegated object
86 fits_sph.Write("myfile.fits"); // writing on FITS file
87
88 FITS_SphereHEALPix<r_8> fits_sph2("myfile.fits");
89 // load a delegated object
90 // from FITS file
91 SphereHEALPix<r_8> sphere2=(SphereHEALPix<r_8>)fits_sph2;
92 // casting the delegated object
93 // into a SOPHYA object
94\endverbatim
95
96
97*/
98
99/*! \fn void SOPHYA::FitsIOHandler::Read(char flnm[],int hdunum)
100
101this method is called from inherited objects :
102
103opens a file 'flnm'
104
105gets parameters in extension-header (hdunum)
106
107calls the method 'ReadFromFits' from the inherited object
108*/
109void FitsIOHandler::Read(char flnm[],int hdunum)
110{
111 FitsInFile ifts(flnm);
112 Read(ifts, hdunum);
113}
114
115 /*! \fn void SOPHYA::FitsIOHandler::Read(FitsInFile& is, int hdunum)
116Read the data on extension hdunum (or primary header, if hdunum=1) from FitsInFIle. If hdunum is not addressed, , one reads the next extension, with respect to the current position.
117 */
118void FitsIOHandler::Read(FitsInFile& is, int hdunum)
119{
120 is.ReadHeader(hdunum);
121 ReadFromFits(is);
122}
123
124
125/*! \fn void SOPHYA::FitsIOHandler::Write(char flnm[])
126this method is called from inherited objects.
127
128for writing a new object in a new fits-extension :
129
130\warning By convention, primary header may contain fits-image data.
131For switching off this convention (i.e. to make sure that all data will be on fits-extensions) use the method :
132
133firstImageOnPrimaryHeader() (see below)
134
135calls the method 'WriteToFits' from the inherited object
136
137*/
138void FitsIOHandler::Write(char flnm[])
139
140{
141 FitsOutFile of(flnm, FitsFile::unknown);
142 Write(of);
143}
144
145void FitsIOHandler::Write(FitsOutFile& os)
146{
147 WriteToFits(os);
148}
149
150
151
152
153FitsFile::~FitsFile()
154{
155 int status = 0;
156 if( fptr_ != NULL)
157 {
158 fits_close_file(fptr_,&status);
159 // je ne fais pas delete fptr_, c'est la lib. fitsio qui a fait
160 // new...
161 }
162 if( status ) printerror( status );
163}
164
165
166void FitsFile::printerror(int &status)
167 //*****************************************************/
168 //* Print out cfitsio error messages and exit program */
169 //*****************************************************/
170{
171 if( status )
172 {
173 fits_report_error(stderr,status);
174 throw IOExc("FitsFile:: error FITSIO status");
175 }
176 return;
177}
178
179void FitsFile::printerror(int& status, char* texte)
180 //*****************************************************/
181 //* Print out cfitsio error messages and exit program */
182 //*****************************************************/
183{
184 // print out cfitsio error messages and exit program
185 // print error report
186 fits_report_error(stderr, status);
187 cout << " erreur:: " << texte << endl;
188 throw IOExc("FitsFile:: error FITSIO status");
189}
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 case FitsDataType_byte :
790 {
791 unsigned char uschar = 0;
792 fits_read_col(fptr_,TBYTE,ncol+1,ligneALire,premierElement,1,&inull,&uschar, &anynul,&status);
793 idata[icount++] = (int)uschar;
794 }
795 break;
796 case FitsDataType_char :
797 fits_read_col(fptr_,TSTRING,ncol+1,ligneALire,premierElement,1,cnull,&cdata[ccount++],&anynul,&status);
798 break;
799 }
800 if (status)
801 {
802 ResetStatus(status);
803 break;
804 }
805 }
806}
807
808/*! \fn void SOPHYA::FitsInFile::GetBinTabLine(long NoLine, BnTblLine& ligne)
809Get the NoLine-th 'line' from the current BINTABLE extension on FITS file,
810*/
811void FitsInFile::GetBinTabLine(long NoLine, BnTblLine& ligne)
812{
813 int status= 0;
814 int anynul;
815 double dnull= dnull_;
816 float fnull= fnull_;
817 int inull= inull_;
818 char* cnull= const_cast<char*>(cnull_.c_str());
819 int dcount = 0.;
820 int fcount = 0.;
821 int icount = 0;
822 int ccount =0;
823 int ncol;
824 long nels=1;
825 for (ncol=0; ncol<nbcols_; ncol++)
826 {
827 switch (types_[ncol])
828 {
829 case FitsDataType_double :
830 fits_read_col(fptr_,TDOUBLE,ncol+1,NoLine+1,1,1,&dnull,&ligne.ddata_[dcount++],&anynul,&status);
831 break;
832 case FitsDataType_float :
833 fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&ligne.fdata_[fcount++],&anynul,&status);
834 break;
835 case FitsDataType_int :
836 fits_read_col(fptr_,TINT,ncol+1,NoLine+1,1,1,&inull,&ligne.idata_[icount++], &anynul,&status);
837 break;
838 case FitsDataType_long :
839 fits_read_col(fptr_,TLONG,ncol+1,NoLine+1,1,1,&inull,&ligne.ldata_[icount++], &anynul,&status);
840 break;
841 case FitsDataType_byte :
842 fits_read_col(fptr_,TBYTE,ncol+1,NoLine+1,1,1,&inull,&ligne.bdata_[icount++], &anynul,&status);
843 break;
844 case FitsDataType_char :
845 char* chaine = new char[taille_des_chaines_[ccount]];
846 fits_read_col(fptr_,TSTRING,ncol+1,NoLine+1,1,1,cnull,&chaine,&anynul,&status);
847 ligne.cdata_[ccount++] = string(chaine);
848 break;
849 }
850 if (status)
851 {
852 ResetStatus(status);
853 break;
854 }
855 }
856}
857
858/*! \fn void SOPHYA::FitsInFile::GetBinTabLine(int NoLine, float* fdata)
859
860Get the NoLine-th float 'line' from the current BINTABLE extension on FITS file,
861*/
862void FitsInFile::GetBinTabLine(int NoLine, float* fdata)
863{
864 int status= 0;
865 int anynul;
866 float fnull= fnull_;
867 long nels=1;
868 int ncol;
869 for (ncol=0; ncol<nbcols_; ncol++)
870 {
871 fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[ncol],&anynul,&status);
872 if (status)
873 {
874 ResetStatus(status);
875 break;
876 }
877 }
878}
879
880
881/*! \fn void SPOPHYA::FitsInFile::GetBinTabFCol(double* valeurs,int nentries, int NoCol) const
882
883fill the array 'valeurs' with double data from the current BINTABLE extension on FITS file, from column number 'NoCol'
884
885\param <nentries> number of data to be read
886*/
887void FitsInFile::GetBinTabFCol(r_8* valeurs,int nentries, int NoCol) const
888 {
889 int status= 0;
890 int DTYPE;
891 long repeat,width;
892 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
893 if( DTYPE != TDOUBLE)
894 {
895 if (DTYPE == TFLOAT) cout << " WARNING: reading double from float : conversion will be made by fitsio library" << endl;
896 else
897 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non double");
898 }
899 long nels=nentries;
900 int anynul;
901 double dnull= dnull_;
902
903 fits_read_col(fptr_,TDOUBLE,NoCol+1,1,1,nels,&dnull,valeurs,
904 &anynul,&status);
905 if( status )
906 {
907 printerrorAndContinue( status,"erreur lecture de colonne" );
908 }
909 }
910
911/*! \fn void SOPHYA::FitsInFile::GetBinTabFCol(float* valeurs,int nentries, int NoCol) const
912
913 same as previous method with float data
914*/
915void FitsInFile::GetBinTabFCol(r_4* valeurs,int nentries, int NoCol) const
916 {
917 int status= 0;
918 int DTYPE;
919 long repeat,width;
920 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
921 if( DTYPE != TFLOAT)
922 {
923 if (DTYPE == TDOUBLE) cout << " WARNING: reading float from double : conversion will be made by fitsio library" << endl;
924 else
925 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
926 }
927 long nels=nentries;
928 int anynul;
929 float fnull= fnull_;
930 fits_read_col(fptr_,TFLOAT,NoCol+1,1,1,nels,&fnull,valeurs,
931 &anynul,&status);
932 if( status ) printerrorAndContinue( status,"erreur lecture de colonne" );
933 }
934
935/*! \fn void SOPHYA::FitsInFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
936
937 same as previous method with int data
938*/
939
940void FitsInFile::GetBinTabFCol(int_4* valeurs,int nentries, int NoCol) const
941 {
942 int status= 0;
943 int DTYPE;
944 long repeat,width;
945 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
946 cout << " en lecture fits trouve le type " << DTYPE << endl;
947 if( DTYPE != TLONG && DTYPE != TINT)
948 {
949 throw IOExc("FitsFile::GetBinTabFCol, probleme de lecture d'entiers");
950 }
951 long nels=nentries;
952 int anynul;
953 int inull= inull_;
954
955
956
957 // voir commentaire dans putColToFits()
958 fits_read_col(fptr_,TINT,NoCol+1,1,1,nels,&inull,valeurs,
959 &anynul,&status);
960 if( status ) printerrorAndContinue( status,"erreur lecture de colonne" );
961 }
962
963/*! \fn void SOPHYA::FitsInFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
964
965 same as previous method with char* data
966*/
967
968void FitsInFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
969 {
970 int status= 0;
971 int DTYPE;
972 long repeat,width;
973 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
974 if( DTYPE != TSTRING && DTYPE != TBYTE)
975 {
976 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non string");
977 }
978 long nels=nentries;
979 int anynul;
980 char* cnull= const_cast<char*>(cnull_.c_str());
981 long frow=1;
982 long felem=1;
983 fits_read_col(fptr_,TSTRING,NoCol+1,frow,felem,nels,cnull,valeurs,
984 &anynul,&status);
985 if( status ) printerrorAndContinue( status,"erreur lecture de colonne" );
986 }
987
988/*! \fn void SOPHYA::FitsInFile::GetSingleColumn(double* map, int nentries) const
989fill the array 'map' with double data from the current extension on FITS file.
990If the extension is BINTABLE, the first column is provided.
991
992\param <nentries> number of data to be read
993*/
994void FitsInFile::GetSingleColumn(r_8* map, int nentries) const
995{
996 int status = 0;
997 if(hdutype_ == FitsExtensionType_IMAGE)
998 {
999
1000 if(imageDataType_ != FitsDataType_double)
1001 {
1002 cout << " The data type on fits file is not double...";
1003 cout << " Conversion to double achieved by cfitsio lib" << endl;
1004 }
1005
1006 int anynul;
1007 double dnull= dnull_;
1008
1009 long nels= nentries;
1010 fits_read_img(fptr_,TDOUBLE,1,nels,&dnull,map,&anynul,&status);
1011 if( status ) printerror( status );
1012 }
1013 else
1014 if(hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL)
1015 {
1016 GetBinTabFCol(map,nentries, 0);
1017 }
1018 else
1019 {
1020 cout << " hdutype= " << (int) hdutype_ << endl;
1021 throw IOExc("FitsFile::GetSingleColumn, this HDU is unknown");
1022 }
1023}
1024
1025/*! \fn void SOPHYA::FitsInFile::GetSingleColumn(float* map, int nentries) const
1026same as above with float data
1027*/
1028void FitsInFile::GetSingleColumn(r_4* map, int nentries) const
1029{
1030 int status = 0;
1031 if(hdutype_ == FitsExtensionType_IMAGE)
1032 {
1033 if(imageDataType_ != FitsDataType_float)
1034 {
1035 cout << " The data type on fits file is not float ";
1036 cout << " Conversion to float achieved by cfitsio lib" << endl;
1037 }
1038 int anynul;
1039 float fnull= fnull_;
1040
1041 long nels= nentries;
1042 fits_read_img(fptr_,TFLOAT,1,nels,&fnull, map,&anynul,&status);
1043 if( status ) printerror( status );
1044 }
1045 else
1046 if(hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL)
1047 {
1048 GetBinTabFCol(map,nentries, 0);
1049 }
1050 else
1051 {
1052 cout << " hdutype= " << (int) hdutype_ << endl;
1053 throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
1054 }
1055}
1056
1057/*! \fn void SOPHYA::FitsInFile::GetSingleColumn( int* map, int nentries) const
1058 same as above with int data
1059*/
1060void FitsInFile::GetSingleColumn( int_4* map, int nentries) const
1061{
1062 int status = 0;
1063 if(hdutype_ == FitsExtensionType_IMAGE)
1064 {
1065 if(imageDataType_ != FitsDataType_int)
1066 {
1067 cout << " The data type on fits file is not int ";
1068 cout << " Conversion to float achieved by cfitsio lib" << endl;
1069 }
1070 int anynul;
1071 float fnull= fnull_;
1072
1073 long nels= nentries;
1074 fits_read_img(fptr_,TINT,1,nels,&fnull,map,&anynul,&status);
1075 if( status ) printerror( status );
1076 }
1077 else
1078 if(hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL)
1079 {
1080 GetBinTabFCol(map,nentries, 0);
1081 }
1082 else
1083 {
1084 cout << " hdutype= " << (int) hdutype_ << endl;
1085 throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
1086 }
1087}
1088
1089void FitsInFile::GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
1090 vector<int>& repeat,
1091 vector<string>& noms,
1092 vector<FitsDataType>& types,
1093 vector<int>& taille_des_chaines)
1094{
1095 int status= 0;
1096 int hdunum=0;
1097 int hdutype=0;
1098 fits_get_hdu_num(fileptr,&hdunum);
1099 fits_get_hdu_type(fileptr, &hdutype, &status);
1100
1101 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1102 {
1103 throw IOExc("FitsFile::GetBinTabParameters this HDU is not an ASCII table nor a binary table");
1104 }
1105 // if(hdutype == ASCII_TBL)
1106 // cout << " Reading a FITS ascii table in HDU : " << hdunum << endl;
1107 // if(hdutype == BINARY_TBL)
1108 // cout << " Reading a FITS binary table in HDU : " << hdunum << endl;
1109
1110 // get the number of columns
1111 fits_get_num_cols(fileptr, &nbcols,&status);
1112 if( status ) printerror( status );
1113
1114 // get the number of rows
1115 long naxis2= 0;
1116 fits_get_num_rows(fileptr,&naxis2,&status);
1117 if( status ) printerror( status );
1118 nrows = (int)naxis2;
1119
1120 // get the datatype, names and the repeat count
1121 noms.clear();
1122 noms.reserve(nbcols);
1123 types.clear();
1124 types.reserve(nbcols);
1125 repeat.clear();
1126 repeat.reserve(nbcols);
1127 taille_des_chaines.clear();
1128 char **ttype = new char*[nbcols];
1129 int ii;
1130 //
1131 //
1132 for (ii=0; ii < nbcols; ii++) ttype[ii]=new char[FLEN_VALUE];
1133 int nfound;
1134 fits_read_keys_str(fileptr, "TTYPE",1,nbcols,ttype,&nfound, &status);
1135 if( status ) printerror( status,"erreur lecture des noms de colonne");
1136 int rept=0;
1137 if(hdutype == ASCII_TBL)
1138 {
1139 for(ii = 0; ii < nbcols; ii++)
1140 {
1141 int DTYPE;
1142 long width;
1143 long repete = 0;
1144 fits_get_coltype(fileptr,ii+1,&DTYPE,&repete,&width,&status);
1145 if( status ) printerror( status,"erreur lecture type de colonne");
1146 rept = repete;
1147 noms.push_back(string(ttype[ii]));
1148 switch (DTYPE)
1149 {
1150 case TDOUBLE :
1151 types.push_back(FitsDataType_double);
1152 break;
1153 case TFLOAT :
1154 types.push_back(FitsDataType_float);
1155 break;
1156 case TLONG :
1157 types.push_back(FitsDataType_long);
1158 break;
1159 case TSHORT :
1160 types.push_back(FitsDataType_int);
1161 break;
1162 case TSTRING :
1163 types.push_back(FitsDataType_char);
1164 taille_des_chaines.push_back(width);
1165 rept/=width;
1166 break;
1167 default :
1168 cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
1169 throw IOExc("FitsFile::GetBinTabParameters, unsupported data type of field, for ASCII table");
1170 }
1171 repeat.push_back(rept);
1172 }
1173 }
1174 else
1175 {
1176 for(ii = 0; ii < nbcols; ii++)
1177 {
1178 int DTYPE;
1179 long width;
1180 long repete = 0;
1181 fits_get_coltype(fileptr,ii+1,&DTYPE,&repete,&width,&status);
1182 if( status ) printerror( status,"erreur lecture type de colonne");
1183 rept = repete;
1184 noms.push_back(string(ttype[ii]));
1185 switch (DTYPE)
1186 {
1187 case TDOUBLE :
1188 types.push_back(FitsDataType_double);
1189 break;
1190 case TFLOAT :
1191 types.push_back(FitsDataType_float);
1192 break;
1193 case TLONG :
1194 types.push_back(FitsDataType_long);
1195 break;
1196 case TINT :
1197 types.push_back(FitsDataType_int);
1198 break;
1199 case TSHORT :
1200 types.push_back(FitsDataType_int);
1201 break;
1202 case TSTRING :
1203 types.push_back(FitsDataType_char);
1204 taille_des_chaines.push_back(width);
1205 rept/=width;
1206 break;
1207 case TBYTE :
1208 types.push_back(FitsDataType_byte);
1209 break;
1210 default :
1211 cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
1212 throw IOExc("FitsFile::GetBinTabParameters, unsupported data type of field, for BINTABLE");
1213 }
1214 repeat.push_back(rept);
1215 }
1216 }
1217 for (ii=0; ii < nbcols; ii++) delete [] ttype[ii];
1218 delete [] ttype;
1219}
1220
1221void FitsInFile::KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum)
1222{
1223 int status = 0;
1224 int hdutype;
1225 fits_movabs_hdu(fileptr,hdunum,&hdutype,&status);
1226 if( status ) printerror( status,":KeywordsIntoDVList : erreur movabs");
1227 // get number of keywords
1228 int nkeys,keypos;
1229 fits_get_hdrpos(fileptr,&nkeys,&keypos,&status);
1230 if( status ) printerror( status );
1231
1232 // put keywords in a DVList object
1233 char keyname[LEN_KEYWORD]= "";
1234 char strval[FLEN_VALUE]= "";
1235 char dtype;
1236 char card[FLEN_CARD];
1237 char *comkey = "COMMENT";
1238 char comment[FLEN_COMMENT];
1239
1240 // shift with the number of mandatory keywords
1241 // int num= 8;
1242 int num= 0;
1243 // primary header
1244 if (hdunum == 1)
1245 {
1246 // read NAXIS
1247 int naxis=0;
1248 fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
1249 // number of mandatory keywords
1250 num = naxis+3;
1251 }
1252 // extensions
1253 else
1254 {
1255 if (hdutype == IMAGE_HDU)
1256 {
1257 // read NAXIS
1258 int naxis=0;
1259 fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
1260 // number of mandatory keywords
1261 num = naxis+5;
1262 }
1263 else
1264 if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
1265 {
1266 // number of mandatory keywords
1267 num = 8;
1268 }
1269 }
1270 int j;
1271 for(j = num+1; j <= nkeys; j++)
1272 {
1273 fits_read_keyn(fileptr,j,card,strval,NULL,&status);
1274 if(status) printerror(status);
1275
1276 strncpy(keyname,card,LEN_KEYWORD-1);
1277 if(strncmp(keyname,comkey,LEN_KEYWORD-1) != 0 && strlen(keyname) != 0
1278 && strlen(strval) != 0)
1279 {
1280 fits_get_keytype(strval,&dtype,&status);
1281 if(status) printerror(status);
1282
1283 strip(keyname, 'B',' ');
1284 strip(strval, 'B',' ');
1285 strip(strval, 'B','\'');
1286
1287 switch( dtype )
1288 {
1289 case 'C':
1290 fits_read_key(fileptr,TSTRING,keyname,strval,comment,&status);
1291 if ( strncmp(keyname,"TTYPE",5) == 0 ||
1292 strncmp(keyname,"TFORM",5) == 0 ||
1293 strncmp(keyname,"TBCOL",5) == 0 ) break;
1294 dvl[keyname]= strval;
1295 dvl.SetComment(keyname, comment);
1296 break;
1297 case 'I':
1298 int ival;
1299 fits_read_key(fileptr,TINT,keyname,&ival,comment,&status);
1300 dvl[keyname]= (int_4) ival; // Portage mac DY
1301 dvl.SetComment(keyname, comment);
1302 break;
1303 case 'L':
1304 int ilog;
1305 fits_read_key(fileptr,TLOGICAL,keyname,&ilog,comment,&status);
1306 dvl[keyname]= (int_4) ilog;
1307 dvl.SetComment(keyname, comment);
1308 break;
1309 case 'F':
1310 double dval;
1311 fits_read_key(fileptr,TDOUBLE,keyname,&dval,comment,&status);
1312 dvl[keyname]= dval;
1313 dvl.SetComment(keyname, comment);
1314 break;
1315 }
1316
1317 }
1318 }
1319 // dvl.Print();
1320}
1321
1322
1323/*!
1324 \class SOPHYA::FitsOutFile
1325 \ingroup FitsIOServer
1326 Class for loading SOPHYA objects from FITS Format Files (uses cfitsio lib)
1327*/
1328
1329FitsOutFile::FitsOutFile()
1330{
1331 InitNull();
1332}
1333
1334 /*! \fn SOPHYA::FitsOutFile::FitsOutFile(char flnm[], WriteMode wrm)
1335
1336\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)
1337
1338 */
1339
1340FitsOutFile::FitsOutFile(string const & flnm, WriteMode wrm)
1341{
1342 InitNull();
1343 openoutputfitsfile(flnm.c_str(), wrm);
1344}
1345
1346FitsOutFile::FitsOutFile(const char * flnm, WriteMode wrm)
1347{
1348 InitNull();
1349 openoutputfitsfile(flnm, wrm);
1350}
1351
1352void FitsOutFile::openoutputfitsfile(const char * flnm, WriteMode wrm)
1353{
1354 int status = 0;
1355
1356 // create new FITS file
1357 fits_create_file(&fptr_,flnm,&status);
1358 if( status )
1359 {
1360
1361 switch (wrm)
1362 {
1363 // si on veut ecrire a la fin de ce fichier
1364 case append :
1365 status = 0;
1366 fits_clear_errmsg();
1367 fits_open_file(&fptr_,flnm,READWRITE,&status);
1368 if( status )
1369 {
1370 cout << " error opening file: " << flnm << endl;
1371 printerror(status, "failure opening a file supposed to exist");
1372 }
1373 else cout << " file " << flnm << " opened, new objects will be appended " << endl;
1374 fits_get_num_hdus(fptr_, &hdunum_, &status);
1375 int hdutype;
1376 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1377 if( status ) printerror( status,":FitsFile::WriteF : erreur movabs");
1378 break;
1379
1380 case clear :
1381 {
1382 status = 0;
1383 fits_clear_errmsg();
1384 char* newname = new char[strlen(flnm)+1];
1385 //
1386 newname[0] = '!';
1387 newname[1] = '\0';
1388 strcat(newname, flnm);
1389 fits_create_file(&fptr_,newname,&status);
1390 delete [] newname;
1391 if (status)
1392 {
1393 cout << " error opening file: " << flnm << endl;
1394 printerror(status, "unable to open file, supposed to exist");
1395 }
1396 else cout << " WARNING : file " << flnm << " is overwritten " << endl;
1397 break;
1398 }
1399 case unknown :
1400 printerror(status, " file seems already to exist");
1401 break;
1402
1403 }
1404 }
1405}
1406
1407
1408
1409/*! \fn void SOPHYA::FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList &dvl)
1410
1411create an IMAGE header on FITS file.
1412\param <type> type of data (see method ColTypeFromFits)
1413\param <nbdim> number of dimensions : 1D, 2D, 3D etc. = NAXIS
1414\param <naxisn> array containind sizes of the different dimensions
1415*/
1416void FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList* ptr_dvl)
1417{
1418 int status = 0;
1419 long naxis = nbdim;
1420 long* naxes = new long[nbdim];
1421 bool hdunfirst= (hdunum_ == 0);
1422 if (hdunfirst)
1423 {
1424 if (imageOnPrimary_ == false)
1425 {
1426 hdunum_ = 1;
1427 fits_create_img(fptr_,FLOAT_IMG,0,naxes,&status);
1428 }
1429 }
1430 int k;
1431 for (k=0; k< nbdim; k++) naxes[k] = (long)naxisn[k];
1432 if (type == 'D')
1433 fits_create_img(fptr_,DOUBLE_IMG,naxis,naxes,&status);
1434 else
1435 if (type == 'E')
1436 fits_create_img(fptr_,FLOAT_IMG,naxis,naxes,&status);
1437 else
1438 if (type == 'I')
1439 fits_create_img(fptr_,LONG_IMG,naxis,naxes,&status);
1440 else
1441 {
1442 cout << " type of data: " << type << endl;
1443 throw PException("FitsFile:::makeHeaderImageOnFits:unprogrammed type of data ");
1444 }
1445
1446 // on ajoute eventuellement un dvlist prepare et la doc SOPHYA
1447 hdunum_++;
1448 if (hdunfirst)
1449 {
1450 addDVListOnPrimary();
1451 writeSignatureOnFits(1);
1452 }
1453
1454 // header format FITS
1455
1456 writeAppendedHeaderOnFits();
1457
1458 // write supplementary keywords (from SOPHYA)
1459 // dvl.Print();
1460 if (ptr_dvl != NULL) addKeywordsOfDVList(*ptr_dvl);
1461
1462 delete [] naxes;
1463 if( status ) printerror( status, "erreur creation HDU IMAGE" );
1464
1465}
1466
1467
1468/*! \fn void SOPHYA::FitsOutFile::PutImageToFits(int nbData, double* map) const
1469
1470write double data from array 'map'on an IMAGE extension
1471\param <nbData> number of data to be written
1472*/
1473void FitsOutFile::PutImageToFits(int nbData, r_8* map) const
1474{
1475 int status = 0;
1476 long npix= nbData;
1477 fits_write_img(fptr_,TDOUBLE,1,npix,map,&status);
1478 if( status ) printerror( status, "erreur ecriture PutImageToFits" );
1479}
1480
1481/*! \fn void SOPHYA::FitsOutFile::PutImageToFits(int nbData, float* map) const
1482
1483same as previous method with float data
1484*/
1485void FitsOutFile::PutImageToFits(int nbData, r_4* map) const
1486{
1487 int status = 0;
1488 long npix= nbData;
1489 fits_write_img(fptr_,TFLOAT,1,npix, map,&status);
1490 if( status ) printerror( status, "erreur ecriture PutImageToFits" );
1491
1492}
1493
1494 /*! \fn void SOPHYA::FitsOutFile::PutImageToFits( int nbData, int* map) const
1495
1496 same as previous method with int data */
1497void FitsOutFile::PutImageToFits( int nbData, int_4* map) const
1498{
1499 int status = 0;
1500
1501 long npix= nbData;
1502 fits_write_img(fptr_,TINT,1,npix,map,&status);
1503 if( status ) printerror( status, "erreur ecriture PutImageToFits" );
1504}
1505
1506
1507
1508/*! \fn void SOPHYA::FitsOutFile::makeHeaderBntblOnFits( string fieldType, vector<string> Noms, int nentries, int tfields, DVList &dvl, string extname, vector<int> taille_des_chaines)
1509
1510create an BINTABLE header on FITS file.
1511\param <fieldType> array conta
1512ining characters denoting types of the different column (see method ColTypeFromFits)
1513\param <Noms> array of the names of columns
1514\param <nentries> number of data of each column
1515\param <tfields> number of columns
1516\param <dvl> a SOPHYA DVList containing keywords to be appended
1517\param <extname> keyword EXTNAME for FITS file
1518\param <taille_des_chaines> vector containing the number of characters of data for each char* typed column, with order of appearance in 'fieldType'
1519*/
1520void FitsOutFile::makeHeaderBntblOnFits(string fieldType, vector<string> Noms, int nentries, int tfields, DVList* ptr_dvl, string extname, vector<int> taille_des_chaines)
1521{
1522 int k;
1523 int status = 0;
1524 long nrows;
1525 // verifications de coherences
1526
1527 if (fieldType.length() != tfields)
1528 {
1529 cout << " nombre de champs :" << tfields << "nombre de types: " << fieldType.length() << endl;
1530 throw ParmError("FitsFile:: fields and types don't match");
1531
1532 }
1533 if (tfields > Noms.size())
1534 {
1535 cout << " WARNING: FitsOutFile::makeHeaderBntblOnFits, length of vector of column names not equal to total number of columns" << endl;
1536 for (k=0; k<(tfields-Noms.size()); k++) Noms.push_back( string(" "));
1537 }
1538
1539 // nombre de variables "chaines de caracteres"
1540 int nbString = 0;
1541 for (k=0; k<tfields;k++) if (fieldType[k] == 'A') nbString++;
1542 // coherence de la longueur du vecteur des tailles
1543 if (nbString > taille_des_chaines.size())
1544 {
1545 cout << " WARNING: FitsOutFile::makeHeaderBntblOnFits, length of vector of string lengths not equal to total number of columns" << endl;
1546 int strSz=0;
1547 for (k=0; k<taille_des_chaines.size(); k++) if ( taille_des_chaines[k] > strSz) strSz = taille_des_chaines[k];
1548 for (k=0; k<(nbString-taille_des_chaines.size()); k++) taille_des_chaines.push_back(strSz);
1549 }
1550 char ** ttype= new char*[tfields];
1551 char ** tform= new char*[tfields];
1552 char largeur[FLEN_VALUE];
1553 int noColString=0;
1554 for (k=0; k<tfields;k++)
1555 {
1556 char format[FLEN_VALUE];
1557
1558 if(nentries < 1024)
1559 {
1560 nrows= nentries;
1561 if (fieldType[k] == 'A')
1562 {
1563 sprintf(largeur,"%d",taille_des_chaines[noColString++]);
1564 strcpy(format,largeur);
1565 }
1566 else strcpy(format,"1");
1567 }
1568 else
1569 {
1570 nrows = nentries/1024;
1571 if(nentries%1024 != 0) nrows++;
1572 if (fieldType[k] == 'A')
1573 {
1574 char largaux[FLEN_VALUE];
1575 sprintf(largeur,"%d",taille_des_chaines[noColString]);
1576 sprintf(largaux,"%d",1024*taille_des_chaines[noColString]);
1577 noColString++;
1578 strcpy(format, largaux);
1579 }
1580 else strcpy(format,"1024");
1581 }
1582 strncat(format,&fieldType[k],1);
1583 if (fieldType[k] == 'A')
1584 {
1585 strcat(format,largeur);
1586 }
1587 ttype[k] = const_cast<char*>(Noms[k].c_str());
1588 tform[k]= new char[FLEN_VALUE];
1589 strcpy(tform[k],format);
1590 }
1591 char* extn = const_cast<char*>(extname.c_str());
1592
1593 // create a new empty binary table onto the FITS file
1594 // physical units if they exist, are defined in the DVList object
1595 // so the NULL pointer is given for the tunit parameters.
1596 nrows=0;
1597 fits_create_tbl(fptr_,BINARY_TBL,nrows,tfields,ttype,tform,
1598 NULL,extn,&status);
1599 if( status ) printerror( status );
1600
1601 int ii;
1602 for(ii = 0; ii < tfields; ii++)
1603 {
1604 delete [] tform[ii];
1605 }
1606 delete [] ttype;
1607 delete [] tform;
1608
1609 // on ajoute eventuellement des mots-cles
1610
1611 if ( hdunum_ == 0 )
1612 {
1613 hdunum_ = 2;
1614 addDVListOnPrimary();
1615 writeSignatureOnFits(1);
1616 }
1617 else hdunum_++;
1618
1619 // header format FITS
1620
1621 writeAppendedHeaderOnFits();
1622
1623 // write SOPHYA keywords
1624 if (ptr_dvl != NULL) addKeywordsOfDVList(*ptr_dvl);
1625}
1626
1627
1628
1629/*! \fn void SOPHYA::FitsOutFile::PutColToFits(int nocol, int nentries, double* donnees) const
1630
1631write double data from array 'donnees ' on column number 'nocol' of a BINTABLE extension.
1632\param <nentries> number of data to be written
1633*/
1634
1635void FitsOutFile::PutColToFits(int nocol, int nentries, r_8* donnees) const
1636{
1637 int status = 0;
1638 int hdutype;
1639 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1640 if( status ) printerror(status,"PutColToFits: le movabs a foire");
1641 fits_get_hdu_type(fptr_, &hdutype, &status);
1642 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1643 {
1644 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1645 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
1646 }
1647 int code;
1648 long repeat, width;
1649 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1650 if( code != TDOUBLE)
1651 {
1652 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " to be written= DOUBLE " << endl;
1653 }
1654 // cout << " 10 elements de colonne " << endl;
1655 // for (int toto=0; toto < 10; toto++) cout << donnees[toto] << endl;
1656 fits_write_col(fptr_,TDOUBLE,nocol+1,1,1,nentries, donnees ,&status);
1657 if( status ) printerror( status,"erreur ecriture col. double, dans fichier fits" );
1658}
1659
1660
1661
1662 /*! \fn void SOPHYA::FitsOutFile::PutColToFits(int nocol, int nentries, float* donnees) const
1663
1664same as previous method with float data
1665*/
1666void FitsOutFile::PutColToFits(int nocol, int nentries, r_4* donnees) const
1667{
1668 int status = 0;
1669 int hdutype;
1670 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1671 if( status ) printerror(status,"PutColToFits: le movabs a foire");
1672 fits_get_hdu_type(fptr_, &hdutype, &status);
1673 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1674 {
1675 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1676 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
1677 }
1678 if(hdutype == ASCII_TBL && nocol>0)
1679 {
1680 throw IOExc("FitsFile::PutColToFits, this HDU is an ASCII table, nocol>0 forbidden");
1681 }
1682 int code;
1683 long repeat, width;
1684 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1685 if( code != TFLOAT)
1686 {
1687 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " (FITS code), to be written= FLOAT " << endl;
1688 }
1689 fits_write_col(fptr_,TFLOAT,nocol+1,1,1,nentries, donnees ,&status);
1690 if( status ) printerror( status,"erreur ecriture col. floats, dans fichier fits" );
1691}
1692
1693
1694/*! \fn void FitsOutFile::PutColToFits(int nocol, int nentries, int* donnees) const
1695
1696same as previous method with int data
1697*/
1698void FitsOutFile::PutColToFits(int nocol, int nentries, int_4* donnees) const
1699{
1700 int status = 0;
1701 int hdutype;
1702 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1703 if( status ) printerror(status,"PutColToFits: le movabs a foire");
1704 fits_get_hdu_type(fptr_, &hdutype, &status);
1705 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1706 {
1707 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1708 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
1709 }
1710 if(hdutype == ASCII_TBL && nocol>0)
1711 {
1712 throw IOExc("FitsFile::PutColToFits, this HDU is an ASCII table, nocol>0 forbidden");
1713 }
1714 int code;
1715 long repeat, width;
1716 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1717
1718
1719// la logique voudrait qu'on distingue TLONG et TINT. Mais si j'ecris
1720// et relis immediatement quelque chose en TLONG l'experience montre
1721// que ca foire. Donc, je fais tout en TINT, d'ailleurs cfitsio n'a pas
1722// (apparemment) d'entiers de longueur superieure a 32 bits.
1723// En fait, je n'y comprend rien. A suivre (GLM).
1724 if (code == TINT || code == TLONG )
1725 {
1726 cout << " j'ecris des TINT" << endl;
1727 fits_write_col(fptr_,TINT,nocol+1,1,1,nentries, donnees ,&status);
1728 }
1729 else if (code == TSHORT)
1730 {
1731 cout << " j'ecris des TSHORT " << endl;
1732 fits_write_col(fptr_,TSHORT,nocol+1,1,1,nentries, donnees ,&status);
1733 }
1734 else
1735 {
1736 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " (FITS code), to be written= integers " << endl;
1737 }
1738 if( status ) printerror( status,"erreur ecriture col. entiers, dans fichier fits" );
1739}
1740
1741
1742/*! \fn void SOPHYA::FitsOutFile::PutColToFits(int nocol, int nentries, char** donnees) const
1743same as previous method with char* data
1744*/
1745void FitsOutFile::PutColToFits(int nocol, int nentries, char** donnees) const
1746{
1747 int status = 0;
1748 int hdutype;
1749 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1750 if( status ) printerror(status,"PutColToFits: le movabs a foire");
1751 fits_get_hdu_type(fptr_, &hdutype, &status);
1752 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1753 {
1754 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1755 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
1756 }
1757 if(hdutype == ASCII_TBL && nocol>0)
1758 {
1759 throw IOExc("FitsFile::PutColToFits, this HDU is an ASCII table, nocol>0 forbidden");
1760 }
1761 int code;
1762 long repeat, width;
1763 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1764 if( code != TSTRING)
1765 {
1766 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " (FITS code), to be written= char** " << endl;
1767 }
1768 fits_write_col(fptr_,TSTRING,nocol+1,1,1,nentries, donnees ,&status);
1769 if( status ) printerror( status,"erreur ecriture col. chars, dans fichier fits" );
1770}
1771
1772void FitsOutFile::PutBinTabLine(long NoLine, BnTblLine& ligne) const
1773{
1774 // on ne fait pas de verification de type, ni de dimension ici, pour
1775 // des raisons de performances
1776 int k;
1777 int status= 0;
1778 int anynul;
1779 int ncol=0;
1780 long nels=1;
1781 // int nbcols;
1782 // fits_get_num_cols(fptr_, &nbcols,&status);
1783 for (k=0; k<ligne.ddata_.size(); k++, ncol++)
1784 {
1785 fits_write_col(fptr_,TDOUBLE,ncol+1,NoLine+1,1,1, &ligne.ddata_[k] ,&status);
1786 if( status ) printerror( status, "PutBinTabLine : erreur ecriture double" );
1787 }
1788 for (k=0; k<ligne.fdata_.size(); k++, ncol++)
1789 {
1790 fits_write_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1, &ligne.fdata_[k] ,&status);
1791 if( status ) printerror( status, "PutBinTabLine : erreur ecriture float" );
1792 }
1793 for (k=0; k<ligne.idata_.size(); k++, ncol++)
1794 {
1795 fits_write_col(fptr_,TINT,ncol+1,NoLine+1,1,1, &ligne.idata_[k] ,&status);
1796 if( status ) printerror( status, "PutBinTabLine : erreur ecriture entier" );
1797 }
1798 for (k=0; k<ligne.ldata_.size(); k++, ncol++)
1799 {
1800 fits_write_col(fptr_,TLONG,ncol+1,NoLine+1,1,1, &ligne.ldata_[k] ,&status);
1801 if( status ) printerror( status, "PutBinTabLine : erreur ecriture entier long" );
1802 }
1803 for (k=0; k<ligne.bdata_.size(); k++, ncol++)
1804 {
1805 fits_write_col(fptr_,TBYTE,ncol+1,NoLine+1,1,1, &ligne.bdata_[k] ,&status);
1806 if( status ) printerror( status, "PutBinTabLine : erreur ecriture byte" );
1807 }
1808
1809 for (k=0; k<ligne.cdata_.size(); k++, ncol++)
1810 {
1811 fits_write_col(fptr_,TSTRING,ncol+1,NoLine+1,1,1, (void*)ligne.cdata_[k].c_str() ,&status);
1812 if( status ) printerror( status, "PutBinTabLine : erreur ecriture caracteres" );
1813 }
1814}
1815
1816
1817/* \fn void SOPHYA::FitsOutFile::DVListIntoPrimaryHeader(DVList& dvl) const
1818
1819Put keywords from a DVList into the primary header of the fits-file
1820*/
1821void FitsOutFile::DVListIntoPrimaryHeader(DVList& dvl)
1822{
1823 int status = 0;
1824 int hdutype;
1825 if (hdunum_ == 0)
1826 {
1827 if (dvlToPrimary_ == NULL) dvlToPrimary_ = new DVList(dvl);
1828 else dvlToPrimary_->Merge(dvl);
1829 }
1830 else
1831 {
1832 fits_movabs_hdu(fptr_,1,&hdutype,&status);
1833 addKeywordsOfDVList(dvl);
1834 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1835 }
1836}
1837
1838
1839void FitsOutFile::writeSignatureOnFits(int hdunum) const
1840{
1841 int status = 0;
1842 int hdutype;
1843 char keyname[LEN_KEYWORD];
1844 char strval[FLEN_VALUE];
1845 char comment[FLEN_COMMENT];
1846 if (hdunum_ == 0)
1847 {
1848 cerr << " WARNING : can't write keywords on non existing primary header" << endl;
1849 return;
1850 }
1851 fits_movabs_hdu(fptr_,1,&hdutype,&status);
1852 //
1853 strncpy(keyname, "CREATOR", LEN_KEYWORD);
1854 keyname[7] = '\0';
1855 strcpy(strval, "SOPHYA");
1856 strcpy(comment," SOPHYA Package - FITSIOServer ");
1857 fits_write_key(fptr_, TSTRING, keyname, &strval, comment, &status);
1858 if( status ) printerror( status );
1859 fits_write_date(fptr_, &status);
1860 fits_write_comment(fptr_,"..............................................", &status);
1861 fits_write_comment(fptr_, " SOPHYA package - FITSIOSever ", &status);
1862 fits_write_comment(fptr_, " (C) LAL/IN2P3-CNRS Orsay, FRANCE 2000", &status);
1863 fits_write_comment(fptr_, " (C) DAPNIA/CEA Saclay, FRANCE 2000", &status);
1864 fits_write_comment(fptr_,"..............................................", &status);
1865 if( status ) printerror( status, "erreur writeSignatureOnFits" );
1866 //
1867 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1868}
1869
1870
1871void FitsOutFile::addKeywordsOfDVList( DVList& dvl) const
1872{
1873 int status = 0;
1874 fits_write_comment(fptr_,"---------- keywords from SOPHYA ---------", &status);
1875 DVList::ValList::const_iterator it;
1876 for(it = dvl.Begin(); it != dvl.End(); it++)
1877 {
1878 MuTyV::MTVType keytype= (*it).second.elval.Type();
1879 char keyname[LEN_KEYWORD];
1880 strncpy(keyname,(*it).first.substr(0,64).c_str(),LEN_KEYWORD);
1881 int bout = ((*it).first.substr(0,64).length() < LEN_KEYWORD) ? (*it).first.substr(0,64).length() : LEN_KEYWORD-1;
1882 keyname[bout] = '\0';
1883 string key((*it).first.substr(0,64));
1884 // string key(keyname);
1885 char comment[FLEN_COMMENT];
1886 char strval[FLEN_VALUE]= "";
1887 char *comkey = "COMMENT";
1888 // fits_read_keyword(fptr_, keyname, strval, NULL, &status);
1889 // if (status != 0 || strncmp(keyname,comkey,LEN_KEYWORD-1) == 0 )
1890 {
1891 string coco = dvl.GetComment(key);
1892 coco.copy( comment, FLEN_COMMENT-1);
1893 int bout = (coco.length() < FLEN_COMMENT) ? coco.length() : FLEN_COMMENT-1;
1894 comment[bout]= '\0';
1895 status = 0;
1896 switch (keytype)
1897 {
1898 case MuTyV::MTVInteger :
1899 {
1900 int ival = (int)dvl.GetI(key);
1901 fits_write_key(fptr_,TINT,keyname,&ival, comment,&status);
1902 break;
1903 }
1904 case MuTyV::MTVFloat :
1905 {
1906 double dval= (double)dvl.GetD(key);
1907 fits_write_key(fptr_,TDOUBLE,keyname,&dval,comment,&status);
1908 break;
1909 }
1910 case MuTyV::MTVString :
1911 {
1912 char strvaleur[FLEN_VALUE]= "";
1913 string valChaine = dvl.GetS(key);
1914 valChaine.copy(strvaleur, FLEN_VALUE-1);
1915 int fin = (valChaine.length() < FLEN_VALUE) ? valChaine.length() : FLEN_VALUE-1;
1916 strvaleur[fin]= '\0';
1917
1918 fits_write_key(fptr_,TSTRING,keyname,&strvaleur,comment,&status);
1919 break;
1920 }
1921 }
1922 }
1923 if( status ) printerror( status,"fitsfile: probleme ecriture mot-cle du dvlist" );
1924 }
1925 fits_write_comment(fptr_,"--------------------------------------", &status);
1926}
1927
1928
1929void FitsOutFile::addDVListOnPrimary()
1930 {
1931 int status = 0;
1932 int hdutype;
1933 if (hdunum_ == 0)
1934 {
1935 cerr << " WARNING : can't write keywords on non existing primary header" << endl;
1936 return;
1937 }
1938 if (dvlToPrimary_ != NULL)
1939 {
1940 fits_movabs_hdu(fptr_,1,&hdutype,&status);
1941 addKeywordsOfDVList(*dvlToPrimary_);
1942 delete dvlToPrimary_;
1943 dvlToPrimary_ = NULL;
1944 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1945 }
1946 }
1947
1948
1949/*! \fn void FitsOutFile::appendInHeader(FitsInFile& infits, int hdunum)
1950
1951get a header from FitsInFile and append to the header beeing built
1952(shifting mandatory keywords)
1953*/
1954
1955void FitsOutFile::appendInputHeader(FitsInFile& infits, int hdunum)
1956{
1957
1958 infits.GetKeywordsFromHeader(hdunum, mots_cles_);
1959 /*
1960 int status = 0;
1961 int hdutype;
1962 fitsfile* fptr=infits.fitsfilePtr();
1963 fits_movabs_hdu(fptr,hdunum,&hdutype,&status);
1964 if( status ) fits_report_error(stderr,status);
1965
1966 // get number of keywords
1967 int nkeys,keypos;
1968 fits_get_hdrpos(fptr,&nkeys,&keypos,&status);
1969 if( status ) fits_report_error(stderr,status);
1970 // shift with the number of mandatory keywords
1971 int num= 0;
1972 // if primary header
1973 if (hdunum == 1)
1974 {
1975 // read NAXIS
1976 int naxis=0;
1977 fits_read_key(fptr,TINT,"NAXIS",&naxis,NULL,&status);
1978 // number of mandatory keywords
1979 num = naxis+3;
1980 }
1981 // extensions
1982 else
1983 {
1984 if (hdutype == IMAGE_HDU)
1985 {
1986 // read NAXIS
1987 int naxis=0;
1988 fits_read_key(fptr,TINT,"NAXIS",&naxis,NULL,&status);
1989 // number of mandatory keywords
1990 num = naxis+5;
1991 }
1992 else
1993 if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
1994 {
1995 // number of mandatory keywords
1996 num = 8;
1997 }
1998 }
1999 int j;
2000 char keyname[LEN_KEYWORD];
2001 char value[FLEN_VALUE];
2002 char comment[FLEN_COMMENT];
2003 for(j = num+1; j <= nkeys; j++)
2004 {
2005 char dtype;
2006 fits_read_keyn(fptr,j,keyname,value,comment,&status);
2007 if(status)
2008 {
2009 fits_report_error(stderr,status);
2010 status=0;
2011 }
2012 string kn(keyname);
2013 string cm(comment);
2014 string val(value);
2015 FitsKeyword kw(kn, val, cm);
2016 mots_cles_.push_back(kw);
2017 }
2018 */
2019}
2020void FitsOutFile::writeAppendedHeaderOnFits()
2021{
2022 for (list<FitsKeyword>::iterator it=mots_cles_.begin(); it !=mots_cles_.end(); it++)
2023 {
2024 (*it).writeOnFits(fptr_);
2025 }
2026 mots_cles_.clear();
2027}
2028
2029void FitsOutFile::insertKeywordOnHeader(string keyname, double value, string comment)
2030{
2031 char cvalue[16];
2032 sprintf(cvalue,"%e",value);
2033 FitsKeyword kw(keyname, string(cvalue), comment, 'F');
2034 mots_cles_.push_back(kw);
2035}
2036void FitsOutFile::insertKeywordOnHeader(string keyname, int value, string comment)
2037{
2038 char cvalue[16];
2039 sprintf(cvalue,"%d",value);
2040 FitsKeyword kw(keyname, string(cvalue), comment, 'I');
2041 mots_cles_.push_back(kw);
2042}
2043void FitsOutFile::insertKeywordOnHeader(string keyname, string value, string comment)
2044{
2045 FitsKeyword kw(keyname, value , comment, 'C');
2046 mots_cles_.push_back(kw);
2047}
2048
2049void FitsOutFile::insertCommentLineOnHeader(string comment)
2050{
2051 FitsKeyword kw(comment);
2052 mots_cles_.push_back(kw);
2053}
2054
2055void FitsOutFile::PrintHeaderToBeAppended()
2056{
2057 cout << " contenu du header en cours de fabrication " << endl;
2058 for (list<FitsKeyword>::iterator it=mots_cles_.begin(); it !=mots_cles_.end(); it++)
2059 {
2060 (*it).Print();
2061 }
2062}
2063
2064
2065FitsKeyword::FitsKeyword()
2066 {
2067 datatype_=' ';
2068 keyname_ = string("");
2069 dvalue_=0.;
2070 ivalue_=1;
2071 svalue_=string("");
2072 comment_=string("");
2073 }
2074
2075FitsKeyword::FitsKeyword(string comment)
2076 {
2077 datatype_=' ';
2078 keyname_=string("COMMENT");
2079 comment_=comment;
2080 }
2081
2082FitsKeyword::FitsKeyword(string keyname, string value, string comment) : keyname_(keyname), comment_(comment)
2083 {
2084 int status=0;
2085 char dtype;
2086 const char* val= value.c_str();
2087 char* valk = const_cast<char*>(val);
2088 fits_get_keytype(valk,&dtype,&status);
2089 if(status)
2090 {
2091 status=0;
2092 if (status == VALUE_UNDEFINED) cout << "WARNING (FitsKeyword) : undefined keyword value " << endl;
2093 datatype_=' ';
2094 }
2095 else datatype_=dtype;
2096
2097 switch( datatype_ )
2098 {
2099 case 'C':
2100 {
2101 strip(valk, 'B','\'');
2102 svalue_ = string(valk);
2103 break;
2104 }
2105 case 'I':
2106 {
2107 ivalue_ = atoi(val);
2108 break;
2109 }
2110 case 'L':
2111 {
2112 bool bb = value.c_str();
2113 ivalue_ = (int)bb;
2114 break;
2115 }
2116 case 'F':
2117 {
2118 dvalue_ = atof(val);
2119 break;
2120 }
2121 case 'X':
2122 {
2123 throw IOExc("FitsKeyword , complex keyword value not supported");
2124 }
2125 }
2126 }
2127
2128// constructeur pour les mots-cles maison (ne prvenant pas de la lecture d'un fichier fits)
2129FitsKeyword::FitsKeyword(string keyname, string value, string comment, char type) : keyname_(keyname), comment_(comment), datatype_(type)
2130 {
2131 char dtype;
2132 const char* val= value.c_str();
2133 char* valk = const_cast<char*>(val);
2134 switch( datatype_ )
2135 {
2136 case 'C':
2137 {
2138 strip(valk, 'B','\'');
2139 svalue_ = string(valk);
2140 break;
2141 }
2142 case 'I':
2143 {
2144 ivalue_ = atoi(val);
2145 break;
2146 }
2147 case 'L':
2148 {
2149 bool bb = value.c_str();
2150 ivalue_ = (int)bb;
2151 break;
2152 }
2153 case 'F':
2154 {
2155 dvalue_ = atof(val);
2156 break;
2157 }
2158 case 'X':
2159 {
2160 throw IOExc("FitsKeyword , complex keyword value not supported");
2161 }
2162 }
2163 }
2164
2165void FitsKeyword::writeOnFits(fitsfile* ptr)
2166 {
2167 int status=0;
2168 char keyname[LEN_KEYWORD];
2169 char comment[FLEN_COMMENT];
2170 keyname_.copy(keyname, LEN_KEYWORD);
2171 int bout = (keyname_.length() < LEN_KEYWORD) ? keyname_.length() : LEN_KEYWORD-1;
2172 keyname[bout] = '\0';
2173 comment_.copy( comment, FLEN_COMMENT);
2174 bout = (comment_.length() < FLEN_COMMENT) ? comment_.length() : FLEN_COMMENT-1;
2175 comment[bout]= '\0';
2176
2177 int nkeys,keypos;
2178 fits_get_hdrpos(ptr,&nkeys,&keypos,&status);
2179 switch( datatype_ )
2180 {
2181 case 'C':
2182 {
2183 char value[FLEN_VALUE]="";
2184 svalue_.copy(value, FLEN_VALUE-1);
2185 int fin = (svalue_.length() < FLEN_VALUE) ? svalue_.length() : FLEN_VALUE-1;
2186 value[fin]= '\0';
2187 fits_write_key(ptr,TSTRING,keyname,&value, comment,&status);
2188 fits_report_error(stderr,status);
2189 break;
2190 }
2191 case 'I':
2192 {
2193 fits_write_key(ptr,TINT,keyname,&ivalue_, comment,&status);
2194 fits_report_error(stderr,status);
2195 break;
2196 }
2197 case 'L':
2198 {
2199 fits_write_key(ptr,TLOGICAL,keyname,&ivalue_, comment,&status);
2200 fits_report_error(stderr,status);
2201 break;
2202 }
2203 case 'F':
2204 {
2205 fits_write_key(ptr,TDOUBLE,keyname,&dvalue_, comment,&status);
2206 fits_report_error(stderr,status);
2207 break;
2208 }
2209 case 'X':
2210 {
2211 cout << "FitsKeyword : complex keyword value not supported" << endl;;
2212 }
2213 default :
2214 {
2215 char *comkey = "COMMENT";
2216 if(strncmp(keyname,comkey,LEN_KEYWORD-1) == 0)
2217 {
2218 fits_write_comment(ptr,comment,&status);
2219 fits_report_error(stderr,status);
2220 }
2221 else
2222 {
2223 cout << " WARNING (FitsKeyword::writeOnFits) : unrecognized keyword : " << keyname_ << endl;
2224 }
2225 }
2226 }
2227 }
2228
2229void FitsKeyword::Print()
2230 {
2231 switch( datatype_ )
2232 {
2233 case 'C':
2234 {
2235 cout << " mot cle : " << keyname_ << " valeur : " << svalue_ << " commentaire : " << comment_ <<endl;
2236 break;
2237 }
2238 case 'I':
2239 {
2240 cout << " mot cle : " << keyname_ << " valeur : " << ivalue_ << " commentaire : " << comment_ <<endl;
2241 break;
2242 }
2243 case 'L':
2244 {
2245 cout << " mot cle : " << keyname_ << " valeur : " << ivalue_ << " commentaire : " << comment_ <<endl;
2246 break;
2247 }
2248 case 'F':
2249 {
2250 cout << " mot cle : " << keyname_ << " valeur : " << dvalue_ << " commentaire : " << comment_ <<endl;
2251 break;
2252 }
2253 case 'X':
2254 {
2255 cout << "FitsKeyword : complex keyword value not supported" << endl;;
2256 }
2257 default :
2258 {
2259 cout << " mot cle : " << keyname_ << " commentaire : " << comment_ <<endl;
2260 }
2261 }
2262 }
Note: See TracBrowser for help on using the repository browser.