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

Last change on this file since 2875 was 2860, checked in by ansari, 20 years ago

Ajout constructeur de copie (pas parfait) pour FitsInOutFile + modifs classes FitsFile/FitsInFile/FitsOutFile pour heriter de FitsInOutFile avec constructeur a partir de FitsInOutFile - Reza 20/12/2005

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