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

Last change on this file since 1235 was 1235, checked in by ansari, 25 years ago

nettoyage stack d'erreurs cfitsio

File size: 46.6 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
10void BnTblLine::setFormat(int dc, int fc, int ic, int cc, vector<string> names)
11 {
12 int nbcols = dc + fc + ic + cc;
13 int maxName = names.size();
14 if (nbcols != maxName)
15 {
16 cout << " WARNING: BnTblLine:: length of vector of column names not equal to total number of columns" << endl;
17 maxName = nbcols < maxName ? nbcols : maxName;
18 }
19 ColName_ = vector<string>(nbcols);
20 for (int k=0; k < maxName; k++) ColName_[k] = names[k];
21 if (dc >0) ddata_ = vector<double>(dc);
22 if (fc >0) fdata_ = vector<float>(fc);
23 if (ic >0) idata_ = vector<int>(fc);
24 if (cc >0) cdata_ = vector<string>(fc);
25 }
26
27bool BnTblLine::sameFormat(const BnTblLine& btl) const
28 {
29 if (btl.ddata_.size() == ddata_.size() && btl.fdata_.size() == fdata_.size() && btl.idata_.size() == idata_.size() && btl.cdata_.size() == cdata_.size()) return true;
30 else return false;
31 }
32
33void BnTblLine::Print()
34 {
35 int k;
36 cout << " ********* ligne ************* " << endl;
37 cout << " *** noms de variables " << endl;
38 for (k=0; k < ColName_.size(); k++) cout << ColName_[k] << " ";
39 cout << endl;
40 cout << " *** variables doubles " << endl;
41 for (k=0; k < ddata_.size(); k++) cout << ddata_[k] << " ";
42 cout << endl;
43 cout << " *** variables float " << endl;
44 for (k=0; k < fdata_.size(); k++) cout << fdata_[k] << " ";
45 cout << endl;
46 cout << " *** variables int " << endl;
47 for (k=0; k < idata_.size(); k++) cout << idata_[k] << " ";
48 cout << endl;
49 cout << " *** variables string " << endl;
50 for (k=0; k < cdata_.size(); k++) cout << cdata_[k] << " ";
51 cout << endl;
52 cout << " ***************************** " << endl;
53 }
54
55
56
57/*!
58 \class SOPHYA::FitsIOHandler
59The class structure is analogous to Sophya-PPersist system :
60Each SOPHYA object XXX is associated with a object of class FITS_XXX
61 (inheriting from FitsFileHandler), to which input/output operations with FITS
62 files are delegated (through a class Hierarchy : FitsFile (virtual),
63 FitsInFile, FitsOutFile) . A typical example of use is the following :
64
65\verbatim
66 int m=... ;
67 SphereHEALPix<r_8> sphere1(m); // definition of the SOPHYA object
68 .... fill the sphere ....
69
70 FITS_SphereHEALPix<r_8> fits_sph1(sphere1);
71 // delegated object
72 fits_sph.Write("myfile.fits"); // writing on FITS file
73
74 FITS_SphereHEALPix<r_8> fits_sph2("myfile.fits");
75 // load a delegated object
76 // from FITS file
77 SphereHEALPix<r_8> sphere2=(SphereHEALPix<r_8>)fits_sph2;
78 // casting the delegated object
79 // into a SOPHYA object
80\endverbatim
81
82
83*/
84
85/*! \fn void SOPHYA::FitsIOHandler::Read(char flnm[],int hdunum)
86
87this method is called from inherited objects :
88
89opens a file 'flnm'
90
91gets parameters in extension-header (hdunum)
92
93calls the method 'ReadFromFits' from the inherited object
94*/
95void FitsIOHandler::Read(char flnm[],int hdunum)
96{
97 FitsInFile ifts(flnm);
98 Read(ifts, hdunum);
99}
100
101 /*! \fn void SOPHYA::FitsIOHandler::Read(FitsInFile& is, int hdunum)
102Read 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.
103 */
104void FitsIOHandler::Read(FitsInFile& is, int hdunum)
105{
106 // if (hdunum == 0 ) is.moveToFollowingHeader();
107 // else is.ReadFInit(hdunum);
108 is.ReadFInit(hdunum);
109 ReadFromFits(is);
110}
111
112
113/*! \fn void SOPHYA::FitsIOHandler::Write(char flnm[])
114this method is called from inherited objects.
115
116for writing a new object in a new fits-extension :
117
118\warning By convention, primary header may contain fits-image data.
119For switching off this convention (i.e. to make sure that all data will be on fits-extensions) use the method :
120
121firstImageOnPrimaryHeader() (see below)
122
123calls the method 'WriteToFits' from the inherited object
124
125*/
126void FitsIOHandler::Write(char flnm[])
127
128{
129 FitsOutFile of(flnm, FitsFile::unknown);
130 Write(of);
131}
132
133void FitsIOHandler::Write(FitsOutFile& os)
134{
135 WriteToFits(os);
136}
137
138
139/*!
140 \class SOPHYA::FitsIOHandler
141Class (virtual) for managing FITS format files
142*/
143
144
145
146FitsFile::~FitsFile()
147{
148 int status = 0;
149 if( fptr_ != NULL)
150 {
151 fits_close_file(fptr_,&status);
152 // je ne fais pas delete fptr_, c'est la lib. fitsio qui a fait
153 // new...
154 }
155 if( status ) printerror( status );
156}
157
158
159void FitsFile::printerror(int &status)
160 //*****************************************************/
161 //* Print out cfitsio error messages and exit program */
162 //*****************************************************/
163{
164 if( status )
165 {
166 fits_report_error(stderr,status);
167 throw IOExc("FitsFile:: error FITSIO status");
168 }
169 return;
170}
171
172void FitsFile::printerror(int& status, char* texte)
173 //*****************************************************/
174 //* Print out cfitsio error messages and exit program */
175 //*****************************************************/
176{
177 // print out cfitsio error messages and exit program
178 // print error report
179 fits_report_error(stderr, status);
180 cout << " erreur:: " << texte << endl;
181 throw IOExc("FitsFile:: error FITSIO status");
182}
183
184void FitsFile::ResetStatus(int& status)
185{
186 fits_status_ = status;
187 status = 0;
188 fits_clear_errmsg();
189}
190
191string FitsFile::GetErrStatus(int status)
192{
193 char text[31];
194 fits_get_errstatus(status, text);
195 return string(text);
196}
197
198/*!
199 \class SOPHYA::FitsInFile
200
201class for saving SOPHYA objects on FITS Format Files (uses cfitsio lib)
202*/
203
204FitsInFile::FitsInFile()
205{
206 InitNull();
207}
208
209FitsInFile::FitsInFile(string const & flnm)
210{
211 InitNull();
212 int status = 0;
213 fits_open_file(&fptr_,flnm.c_str(),READONLY,&status);
214 if( status ) printerror( status );
215}
216
217FitsInFile::FitsInFile(const char * flnm)
218{
219 InitNull();
220 int status = 0;
221 fits_open_file(&fptr_,flnm,READONLY,&status);
222 if( status ) printerror( status );
223}
224
225
226void FitsInFile::InitNull()
227{
228
229 bitpix_ = 0;
230 naxis_ = 0;
231 nbData_ = 0;
232 nrows_ = 0;
233 nbcols_ = 0;
234 naxisn_.clear();
235 repeat_.clear();
236 noms_.clear();
237 taille_des_chaines_.clear();
238 dvl_.Clear();
239
240
241}
242
243//////////////////////////////////////////////////////////
244// methods with general purpose
245/////////////////////////////////////////////////////////
246
247int FitsInFile::NbBlocks(char flnm[])
248{
249 int status = 0;
250 int nbhdu = 0;
251 fitsfile* fileptr;
252 fits_open_file(&fileptr,flnm,READONLY,&status);
253 if( status ) printerror( status, "NbBlocks: erreur ouverture fichier" );
254 fits_get_num_hdus(fileptr, &nbhdu, &status);
255 fits_close_file(fileptr,&status);
256 return nbhdu;
257}
258
259void FitsInFile::GetBlockType(char flnm[], int hdunum, FitsExtensionType& typeOfExtension, int& naxis, vector<int>& naxisn, FitsDataType& dataType, DVList& dvl )
260{
261 int status = 0;
262 fitsfile* fileptr;
263 fits_open_file(&fileptr,flnm,READONLY,&status);
264 if( status ) printerror( status, "GetBlockType: erreur ouverture fichier" );
265 // move to the specified HDU number
266 int hdutype = 0;
267 fits_movabs_hdu(fileptr,hdunum,&hdutype,&status);
268 if( status ) printerror( status,"GetBlockType: erreur movabs");
269 if(hdutype == IMAGE_HDU)
270 {
271 typeOfExtension = FitsExtensionType_IMAGE;
272 int bitpix;
273 GetImageParameters (fileptr, bitpix, naxis, naxisn);
274 if(bitpix == DOUBLE_IMG) dataType = FitsDataType_double;
275 else
276 if(bitpix == FLOAT_IMG) dataType = FitsDataType_float;
277 else
278 if(bitpix == LONG_IMG || bitpix == SHORT_IMG ) dataType = FitsDataType_int;
279 else
280 {
281 cout << " bitpix= " << bitpix << endl;
282 throw PException(" FitsFile::GetBlockType : unsupprted FITS data type");
283 }
284
285 }
286 else
287 if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
288 {
289 int nrows = 0;
290 vector<string> noms;
291 vector<char> types;
292 vector<int> taille_des_chaines;
293 GetBinTabParameters(fileptr, naxis, nrows, naxisn, noms, types, taille_des_chaines);
294 int k;
295 for (k=0; k< naxisn.size(); k++) naxisn[k] *= nrows;
296 if(hdutype == ASCII_TBL)
297 {
298 typeOfExtension = FitsExtensionType_ASCII_TBL;
299 dataType = FitsDataType_ASCII;
300 }
301 else
302 {
303 typeOfExtension = FitsExtensionType_BINARY_TBL;
304 if(types[0] == 'D') dataType = FitsDataType_double;
305 else
306 if(types[0] == 'E') dataType = FitsDataType_float;
307 else
308 if(types[0] == 'I' ) dataType = FitsDataType_int;
309 else
310 if(types[0] == 'S' ) dataType = FitsDataType_char;
311 else
312 {
313 cout << " types[0]= " << types[0] << endl;
314 throw PException(" FitsFile::GetBlockType : unsupprted FITS data type");
315 }
316 }
317 }
318 else
319 {
320 cout << " hdutype= " << hdutype << endl;
321 throw IOExc("FitsFile::GetBlockType: this HDU type is unknown");
322 }
323
324 KeywordsIntoDVList(fileptr, dvl, hdunum);
325 fits_close_file(fileptr,&status);
326}
327
328
329void FitsInFile::ReadFInit(int hdunum)
330{
331 InitNull();
332 int status = 0;
333
334 if (hdunum != 0 ) hdunum_ = hdunum;
335
336 // si le numero de header non precise
337 else
338 {
339 // si c'est le premier objet a lire
340 if (hdunum_ == 0)
341 {
342 // on calcule le numero de header a lire
343 if (imageOnPrimary_ == true) hdunum_ = 1;
344 else hdunum_ = 2;
345 }
346 // sinon objet suivant
347 else hdunum_++;
348 }
349 getHeader();
350
351}
352
353
354void FitsInFile::GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn)
355{
356 int hdunum=0;
357 cout << " Reading a FITS image in HDU : " << fits_get_hdu_num(fileptr,&hdunum) << endl;
358 int status= 0;
359
360 // bits per pixels
361 fits_read_key(fileptr,TINT,"BITPIX",&bitpix,NULL,&status);
362 if( status ) printerror( status );
363
364 // number of dimensions in the FITS array
365 naxis= 0;
366 fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
367 if( status ) printerror( status );
368 // read the NAXISn keywords to get image size
369 long* naxes = new long[naxis] ;
370 int nfound;
371 fits_read_keys_lng(fileptr,"NAXIS",1,naxis,naxes,&nfound,&status);
372 if( status ) printerror( status );
373 if (nfound != naxis )
374 cout << " WARNING : " << nfound << " axes found, expected naxis= " << naxis << endl;
375 int k;
376 for (k=0; k<naxis; k++)
377 {
378 naxisn.push_back( (int)naxes[k] );
379 }
380 delete [] naxes;
381}
382
383
384
385
386 /*! \fn DVList SOPHYA::FitsInFile::DVListFromPrimaryHeader() const
387
388 \return the keywords of primary header in a DVList
389
390*/
391DVList FitsInFile::DVListFromPrimaryHeader() const
392 {
393 int status;
394 DVList dvl;
395 KeywordsIntoDVList(fptr_, dvl, 1);
396 int hdutype = 0;
397 if (hdunum_ > 0) fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
398 return dvl;
399 }
400
401void FitsInFile::getHeader()
402{
403 int status=0;
404 if (hdunum_ < 1) throw PException(" attempt to read hdunum < 1");
405 if (hdunum_ == 1)
406 {
407 // presence of image ?
408 int naxis= 0;
409 fits_read_key(fptr_,TINT,"NAXIS",&naxis,NULL,&status);
410 if( status ) printerror( status );
411 if (naxis > 0 ) // there is an image
412 {
413 hdutype_ = IMAGE_HDU;
414 GetImageParameters (fptr_, bitpix_, naxis_, naxisn_);
415 nbData_ = 1;
416 int k;
417 for (k=0; k<naxis_; k++) if (naxisn_[k] > 0) nbData_ *= naxisn_[k];
418 KeywordsIntoDVList(fptr_, dvl_,hdunum_);
419 }
420 else
421 {
422 throw PException(" first header : no image, probably error in hdunum");
423 }
424 }
425 else
426 {
427 int hdutype;
428 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
429 if( status ) printerror( status,":FitsInFile::getHeader : erreur movabs");
430 hdutype_= hdutype;
431 if(hdutype_ == IMAGE_HDU)
432 {
433 GetImageParameters (fptr_, bitpix_, naxis_, naxisn_);
434 nbData_ = 1;
435 int k;
436 for (k=0; k<naxis_; k++) if (naxisn_[k] > 0) nbData_ *= naxisn_[k];
437 KeywordsIntoDVList(fptr_, dvl_,hdunum_);
438 }
439 if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
440 {
441 GetBinTabParameters(fptr_,nbcols_, nrows_,repeat_, noms_, types_, taille_des_chaines_);
442 KeywordsIntoDVList(fptr_, dvl_, hdunum_);
443 }
444 }
445}
446
447
448void FitsInFile::moveToFollowingHeader()
449{
450 int status = 0;
451 hdunum_++;
452 getHeader();
453}
454
455
456
457
458
459/*! \fn int SOPHYA::FitsInFile::NbColsFromFits() const
460\return number of columns (return 1 if IMAGE)
461*/
462int FitsInFile::NbColsFromFits() const
463{
464 if(hdutype_ == BINARY_TBL) return nbcols_;
465 else
466 if(hdutype_ == ASCII_TBL || hdutype_ == IMAGE_HDU) return 1;
467 else
468 {
469 cout << " hdutype= " << hdutype_ << endl;
470 throw PException("FitsFile::NbColsFromFits, this HDU is unknown");
471 }
472}
473
474/*! \fn int SOPHYA::FitsInFile::NentriesFromFits(int nocol) const
475\return number of data in the current IMAGE extension on FITS file, or number
476 of data of column number 'nocol' of the current BINTABLE extension
477*/
478int FitsInFile::NentriesFromFits(int nocol) const
479{
480 if(hdutype_ == BINARY_TBL ) return nrows_*repeat_[nocol];
481 else
482 if(hdutype_ == ASCII_TBL) return nrows_;
483 else
484 if(hdutype_ == IMAGE_HDU) return nbData_;
485 else
486 {
487 cout << "hdutype= " << hdutype_ << endl;
488 throw PException("FitsFile::NentriesFromFits, this HDU is unknown");
489 }
490}
491
492/*! \fn char SOPHYA::FitsInFile::ColTypeFromFits(int nocol) const
493
494return a character denoting data type of column number 'nocol' in a BINTABLE :
495
496D : double
497
498E : float
499
500I : integer
501
502S : character string
503
504 */
505
506char FitsInFile::ColTypeFromFits(int nocol) const
507{
508 if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
509 {
510 throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
511 }
512 return types_[nocol];
513}
514
515
516/*! \fn string SOPHYA::FitsInFile::ColNameFromFits(int nocol) const
517
518\return name of the column number 'nocol' of the current BINTABLE extension
519 */
520
521string FitsInFile::ColNameFromFits(int nocol) const
522{
523 if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
524 {
525 throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
526 }
527 return noms_[nocol];
528}
529
530/*! \fn int DSOPHYA::FitsInFile::ColStringLengthFromFits(int nocol) const
531
532 \return number of characters of each data for the column number 'nocol' (if char* typed) of the current BINTABLE extension
533*/
534
535int FitsInFile::ColStringLengthFromFits(int nocol) const
536{
537 if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
538 {
539 throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
540 }
541 int index=-1;
542 int k;
543 for (k=0; k<=nocol; k++)
544 {
545 if (types_[k] == 'S') index++;
546 }
547 return taille_des_chaines_[index];
548}
549
550
551
552/*! \fn void SOPHYA::FitsInFile::GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char ** cdata)
553
554Get the NoLine-th 'line' from the current BINTABLE extension on FITS file,
555 */
556
557void FitsInFile::GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char ** cdata)
558{
559 int status= 0;
560 int anull;
561 double dnull= 0.;
562 float fnull= 0.;
563 int inull= 0;
564 char* cnull= "";
565 int dcount = 0.;
566 int fcount = 0.;
567 int icount = 0;
568 int ccount =0;
569 int ncol;
570 long nels=1;
571 for (ncol=0; ncol<nbcols_; ncol++)
572 {
573 switch (types_[ncol])
574 {
575 case 'D' :
576 fits_read_col(fptr_,TDOUBLE,ncol+1,NoLine+1,1,1,&dnull,&ddata[dcount++],&anull,&status);
577 break;
578 case 'E' :
579 fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[fcount++],&anull,&status);
580 break;
581 case 'I' :
582 fits_read_col(fptr_,TINT,ncol+1,NoLine+1,1,1,&inull,&idata[icount++],
583 &anull,&status);
584 break;
585 case 'S' :
586 fits_read_col(fptr_,TSTRING,ncol+1,NoLine+1,1,1,cnull,&cdata[ccount++],&anull,&status);
587 break;
588 }
589 if (status)
590 {
591 ResetStatus(status);
592 break;
593 }
594 }
595}
596
597/*! \fn void SOPHYA::FitsInFile::GetBinTabLine(long NoLine, BnTblLine& ligne)
598Get the NoLine-th 'line' from the current BINTABLE extension on FITS file,
599*/
600void FitsInFile::GetBinTabLine(long NoLine, BnTblLine& ligne)
601{
602 int status= 0;
603 int anull;
604 double dnull= 0.;
605 float fnull= 0.;
606 int inull= 0;
607 char* cnull= "";
608 int dcount = 0.;
609 int fcount = 0.;
610 int icount = 0;
611 int ccount =0;
612 int ncol;
613 long nels=1;
614 for (ncol=0; ncol<nbcols_; ncol++)
615 {
616 switch (types_[ncol])
617 {
618 case 'D' :
619 fits_read_col(fptr_,TDOUBLE,ncol+1,NoLine+1,1,1,&dnull,&ligne.ddata_[dcount++],&anull,&status);
620 break;
621 case 'E' :
622 fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&ligne.fdata_[fcount++],&anull,&status);
623 break;
624 case 'I' :
625 fits_read_col(fptr_,TINT,ncol+1,NoLine+1,1,1,&inull,&ligne.idata_[icount++],
626 &anull,&status);
627 break;
628 case 'S' :
629 char* chaine = new char[taille_des_chaines_[ccount]];
630 fits_read_col(fptr_,TSTRING,ncol+1,NoLine+1,1,1,cnull,&chaine,&anull,&status);
631 ligne.cdata_[ccount++] = string(chaine);
632 break;
633 }
634 if (status)
635 {
636 ResetStatus(status);
637 break;
638 }
639 }
640}
641
642/*! \fn void SOPHYA::FitsInFile::GetBinTabLine(int NoLine, float* fdata)
643
644Get the NoLine-th float 'line' from the current BINTABLE extension on FITS file,
645*/
646void FitsInFile::GetBinTabLine(int NoLine, float* fdata)
647{
648 int status= 0;
649 int anull;
650 float fnull= 0.;
651 long nels=1;
652 int ncol;
653 for (ncol=0; ncol<nbcols_; ncol++)
654 {
655 fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[ncol],&anull,&status);
656 if (status)
657 {
658 ResetStatus(status);
659 break;
660 }
661 }
662}
663
664
665/*! \fn void SPOPHYA::FitsInFile::GetBinTabFCol(double* valeurs,int nentries, int NoCol) const
666
667fill the array 'valeurs' with double data from the current BINTABLE extension on FITS file, from column number 'NoCol'
668
669\param <nentries> number of data to be read
670*/
671void FitsInFile::GetBinTabFCol(double* valeurs,int nentries, int NoCol) const
672 {
673 int status= 0;
674 int DTYPE;
675 long repeat,width;
676 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
677 if( DTYPE != TDOUBLE)
678 {
679 if (DTYPE == TFLOAT) cout << " WARNING: reading double from float : conversion will be made by fitsio library" << endl;
680 else
681 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non double");
682 }
683 long nels=nentries;
684 int anull;
685 // no checking for undefined pixels
686 double dnull= 0.;
687 // fits_read_key(fptr_,TDOUBLE,"BAD_DATA",&dnull,NULL,&status);
688 // if (status != 0)
689 // {
690 // dnull = -1.6375e30; // default value
691 // status = 0;
692 // }
693 if (nentries != nrows_*repeat)
694 {
695 cout << " found " << nentries << " pixels, expected: " << nrows_*repeat << endl;
696 throw PException(" FitsFile:::GetBinTabFCol ");
697 }
698 fits_read_col(fptr_,TDOUBLE,NoCol+1,1,1,nels,&dnull,valeurs,
699 &anull,&status);
700 if( status ) printerror( status,"erreur lecture de colonne" );
701
702 }
703
704/*! \fn void SOPHYA::FitsInFile::GetBinTabFCol(float* valeurs,int nentries, int NoCol) const
705
706 same as previous method with float data
707*/
708void FitsInFile::GetBinTabFCol(float* valeurs,int nentries, int NoCol) const
709 {
710 int status= 0;
711 int DTYPE;
712 long repeat,width;
713 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
714 if( DTYPE != TFLOAT)
715 {
716 if (DTYPE == TDOUBLE) cout << " WARNING: reading float from double : conversion will be made by fitsio library" << endl;
717 else
718 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
719 }
720 long nels=nentries;
721 int anull;
722 // no checking for undefined pixels
723 float fnull= 0.;
724 // fits_read_key(fptr_,TFLOAT,"BAD_DATA",&fnull,NULL,&status);
725 // if (status != 0)
726 // {
727 // fnull = -1.6375e30; // default value
728 // status = 0;
729 // }
730 if (nentries != nrows_*repeat)
731 {
732 cout << " found " << nentries << " pixels, expected: " << nrows_*repeat << endl;
733 throw PException(" FitsFile:::GetBinTabFCol ");
734 }
735 fits_read_col(fptr_,TFLOAT,NoCol+1,1,1,nels,&fnull,valeurs,
736 &anull,&status);
737 if( status ) printerror( status,"erreur lecture de colonne" );
738 }
739
740/*! \fn void SOPHYA::FitsInFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
741
742 same as previous method with int data
743*/
744
745void FitsInFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
746 {
747 int status= 0;
748 int DTYPE;
749 long repeat,width;
750 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
751 if( DTYPE != TLONG && DTYPE != TINT && DTYPE != TSHORT )
752 {
753 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non entier");
754 }
755 long nels=nentries;
756 // no checking for undefined pixels
757 int anull;
758 int inull= 0;
759 // fits_read_key(fptr_,TINT,"BAD_DATA",&inull,NULL,&status);
760 // if (status != 0)
761 // {
762 // inull = -999999; // default value
763 // status = 0;
764 // }
765 if (nentries != nrows_*repeat)
766 {
767 cout << " found " << nentries << " pixels, expected: " << nrows_*repeat << endl;
768 throw PException(" FitsFile:::GetBinTabFCol ");
769 }
770 fits_read_col(fptr_,TINT,NoCol+1,1,1,nels,&inull,valeurs,
771 &anull,&status);
772 if( status ) printerror( status,"erreur lecture de colonne" );
773 }
774
775/*! \fn void SOPHYA::FitsInFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
776
777 same as previous method with char* data
778*/
779
780void FitsInFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
781 {
782 int status= 0;
783 int DTYPE;
784 long repeat,width;
785 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
786 if( DTYPE != TSTRING )
787 {
788 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
789 }
790 long nels=nentries;
791 // no checking for undefined pixels
792 int anull;
793 char* cnull= "";
794 if (nentries != nrows_*repeat/width)
795 {
796 cout << " found " << nentries << " pixels, expected: " << nrows_*repeat/width << endl;
797 throw PException(" FitsFile:::GetBinTabFCol ");
798 }
799 long frow=1;
800 long felem=1;
801 fits_read_col(fptr_,TSTRING,NoCol+1,frow,felem,nels,cnull,valeurs,
802 &anull,&status);
803 if( status ) printerror( status,"erreur lecture de colonne" );
804 }
805
806/*! \fn void SOPHYA::FitsInFile::GetSingleColumn(double* map, int nentries) const
807fill the array 'map' with double data from the current extension on FITS file.
808If the extension is BINTABLE, the first column is provided.
809
810\param <nentries> number of data to be read
811*/
812void FitsInFile::GetSingleColumn(double* map, int nentries) const
813{
814 int status = 0;
815 if(hdutype_ == IMAGE_HDU)
816 {
817
818 if(bitpix_ != DOUBLE_IMG)
819 {
820 cout << " The data type on fits file is not double...";
821 cout << " Conversion to double achieved by cfitsio lib" << endl;
822 }
823
824 // no checking for undefined pixels
825 int anull;
826 double dnull= 0.;
827
828 long nels= nentries;
829 fits_read_img(fptr_,TDOUBLE,1,nels,&dnull,map,&anull,&status);
830 if( status ) printerror( status );
831 }
832 else
833 if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
834 {
835 GetBinTabFCol(map,nentries, 0);
836 }
837 else
838 {
839 cout << " hdutype= " << hdutype_ << endl;
840 throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
841 }
842}
843
844/*! \fn void SOPHYA::FitsInFile::GetSingleColumn(float* map, int nentries) const
845same as above with float data
846*/
847void FitsInFile::GetSingleColumn(float* map, int nentries) const
848{
849 int status = 0;
850 if(hdutype_ == IMAGE_HDU)
851 {
852 if(bitpix_ != FLOAT_IMG)
853 {
854 cout << " The data type on fits file is not float ";
855 cout << " Conversion to float achieved by cfitsio lib" << endl;
856 }
857 // no checking for undefined pixels
858 int anull;
859 float fnull= 0.;
860
861 long nels= nentries;
862 fits_read_img(fptr_,TFLOAT,1,nels,&fnull, map,&anull,&status);
863 if( status ) printerror( status );
864 }
865 else
866 if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
867 {
868 GetBinTabFCol(map,nentries, 0);
869 }
870 else
871 {
872 cout << " hdutype= " << hdutype_ << endl;
873 throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
874 }
875}
876
877/*! \fn void SOPHYA::FitsInFile::GetSingleColumn( int* map, int nentries) const
878 same as above with int data
879*/
880void FitsInFile::GetSingleColumn( int* map, int nentries) const
881{
882 int status = 0;
883 if(hdutype_ == IMAGE_HDU)
884 {
885 if(bitpix_ != LONG_IMG)
886 {
887 cout << " The data type on fits file is not int ";
888 cout << " Conversion to float achieved by cfitsio lib" << endl;
889 }
890 // no checking for undefined pixels
891 int anull;
892 float fnull= 0.;
893
894 long nels= nentries;
895 fits_read_img(fptr_,TINT,1,nels,&fnull,map,&anull,&status);
896 if( status ) printerror( status );
897 }
898 else
899 if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
900 {
901 GetBinTabFCol(map,nentries, 0);
902 }
903 else
904 {
905 cout << " hdutype= " << hdutype_ << endl;
906 throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
907 }
908}
909
910void FitsInFile::GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
911 vector<int>& repeat,
912 vector<string>& noms,
913 vector<char>& types,
914 vector<int>& taille_des_chaines)
915{
916 int status= 0;
917 int hdunum=0;
918 int hdutype=0;
919 fits_get_hdu_num(fileptr,&hdunum);
920 fits_get_hdu_type(fileptr, &hdutype, &status);
921
922 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
923 {
924 throw IOExc("FitsFile::GetBinTabParameters this HDU is not an ASCII table nor a binary table");
925 }
926 if(hdutype == ASCII_TBL)
927 cout << " Reading a FITS ascii table in HDU : " << hdunum << endl;
928 if(hdutype == BINARY_TBL)
929 cout << " Reading a FITS binary table in HDU : " << hdunum << endl;
930
931 // get the number of columns
932 fits_get_num_cols(fileptr, &nbcols,&status);
933 if( status ) printerror( status );
934
935 // get the number of rows
936 long naxis2= 0;
937 fits_get_num_rows(fileptr,&naxis2,&status);
938 if( status ) printerror( status );
939 nrows = (int)naxis2;
940
941 // get the datatype, names and the repeat count
942 noms.clear();
943 noms.reserve(nbcols);
944 types.clear();
945 types.reserve(nbcols);
946 repeat.clear();
947 repeat.reserve(nbcols);
948 taille_des_chaines.clear();
949 char **ttype = new char*[nbcols];
950 int ii;
951 //
952 //
953 for (ii=0; ii < nbcols; ii++) ttype[ii]=new char[FLEN_VALUE];
954 int nfound;
955 fits_read_keys_str(fileptr, "TTYPE",1,nbcols,ttype,&nfound, &status);
956 if( status ) printerror( status,"erreur lecture des noms de colonne");
957 int rept=0;
958 for(ii = 0; ii < nbcols; ii++)
959 {
960 int DTYPE;
961 long width;
962 long repete = 0;
963 fits_get_coltype(fileptr,ii+1,&DTYPE,&repete,&width,&status);
964 if( status ) printerror( status,"erreur lecture type de colonne");
965 rept = repete;
966 noms.push_back(string(ttype[ii]));
967 switch (DTYPE)
968 {
969 case TDOUBLE :
970 types.push_back('D');
971 break;
972 case TFLOAT :
973 types.push_back('E');
974 break;
975 case TLONG :
976 types.push_back('I');
977 break;
978 case TINT :
979 types.push_back('I');
980 break;
981 case TSHORT :
982 types.push_back('I');
983 break;
984 case TSTRING :
985 types.push_back('S');
986 taille_des_chaines.push_back(width);
987 rept/=width;
988 break;
989 default :
990 cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
991 throw IOExc("FitsFile:: unknown type of field");
992 }
993 repeat.push_back(rept);
994 }
995 for (ii=0; ii < nbcols; ii++) delete [] ttype[ii];
996 delete [] ttype;
997}
998
999void FitsInFile::KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum)
1000{
1001 int status = 0;
1002 int hdutype;
1003 fits_movabs_hdu(fileptr,hdunum,&hdutype,&status);
1004 if( status ) printerror( status,":KeywordsIntoDVList : erreur movabs");
1005 // get number of keywords
1006 int nkeys,keypos;
1007 fits_get_hdrpos(fileptr,&nkeys,&keypos,&status);
1008 if( status ) printerror( status );
1009
1010 // put keywords in a DVList object
1011 char keyname[LEN_KEYWORD]= "";
1012 char strval[FLEN_VALUE]= "";
1013 char dtype;
1014 char card[FLEN_CARD];
1015 char *comkey = "COMMENT";
1016 char comment[FLEN_COMMENT];
1017
1018 // shift with the number of mandatory keywords
1019 // int num= 8;
1020 int num= 0;
1021 // primary header
1022 if (hdunum == 1)
1023 {
1024 // read NAXIS
1025 int naxis=0;
1026 fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
1027 // number of mandatory keywords
1028 num = naxis+3;
1029 }
1030 // extensions
1031 else
1032 {
1033 if (hdutype == IMAGE_HDU)
1034 {
1035 // read NAXIS
1036 int naxis=0;
1037 fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
1038 // number of mandatory keywords
1039 num = naxis+5;
1040 }
1041 else
1042 if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
1043 {
1044 // number of mandatory keywords
1045 num = 8;
1046 }
1047 }
1048 int j;
1049 for(j = num+1; j <= nkeys; j++)
1050 {
1051 fits_read_keyn(fileptr,j,card,strval,NULL,&status);
1052 if(status) printerror(status);
1053
1054 strncpy(keyname,card,LEN_KEYWORD-1);
1055 if(strncmp(keyname,comkey,LEN_KEYWORD-1) != 0 && strlen(keyname) != 0
1056 && strlen(strval) != 0)
1057 {
1058 fits_get_keytype(strval,&dtype,&status);
1059 if(status) printerror(status);
1060
1061 strip(keyname, 'B',' ');
1062 strip(strval, 'B',' ');
1063 strip(strval, 'B','\'');
1064
1065 switch( dtype )
1066 {
1067 case 'C':
1068 fits_read_key(fileptr,TSTRING,keyname,strval,comment,&status);
1069 dvl[keyname]= strval;
1070 dvl.SetComment(keyname, comment);
1071 break;
1072 case 'I':
1073 int ival;
1074 fits_read_key(fileptr,TINT,keyname,&ival,comment,&status);
1075 dvl[keyname]= (int_4) ival; // Portage mac DY
1076 dvl.SetComment(keyname, comment);
1077 break;
1078 case 'L':
1079 int ilog;
1080 fits_read_key(fileptr,TLOGICAL,keyname,&ilog,comment,&status);
1081 dvl[keyname]= (int_4) ilog;
1082 dvl.SetComment(keyname, comment);
1083 break;
1084 case 'F':
1085 double dval;
1086 fits_read_key(fileptr,TDOUBLE,keyname,&dval,comment,&status);
1087 dvl[keyname]= dval;
1088 dvl.SetComment(keyname, comment);
1089 break;
1090 }
1091
1092 }
1093 }
1094 // dvl_.Print();
1095}
1096
1097
1098/*!
1099 \class SOPHYA::FitsOutFile
1100 Class for loading SOPHYA objects from FITS Format Files (uses cfitsio lib)
1101*/
1102
1103FitsOutFile::FitsOutFile()
1104{
1105 InitNull();
1106}
1107
1108 /*! \fn SOPHYA::FitsOutFile::FitsOutFile(char flnm[], WriteMode wrm)
1109
1110\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)
1111
1112 */
1113
1114FitsOutFile::FitsOutFile(string const & flnm, WriteMode wrm)
1115{
1116 InitNull();
1117 openoutputfitsfile(flnm.c_str(), wrm);
1118}
1119
1120FitsOutFile::FitsOutFile(const char * flnm, WriteMode wrm)
1121{
1122 InitNull();
1123 openoutputfitsfile(flnm, wrm);
1124}
1125
1126void FitsOutFile::openoutputfitsfile(const char * flnm, WriteMode wrm)
1127{
1128 int status = 0;
1129
1130 // create new FITS file
1131 fits_create_file(&fptr_,flnm,&status);
1132 if( status )
1133 {
1134
1135 switch (wrm)
1136 {
1137 // si on veut ecrire a la fin de ce fichier
1138 case append :
1139 status = 0;
1140 fits_clear_errmsg();
1141 fits_open_file(&fptr_,flnm,READWRITE,&status);
1142 if( status )
1143 {
1144 cout << " error opening file: " << flnm << endl;
1145 printerror(status, "failure opening a file supposed to exist");
1146 }
1147 else cout << " file " << flnm << " opened, new objects will be appended " << endl;
1148 fits_get_num_hdus(fptr_, &hdunum_, &status);
1149 int hdutype;
1150 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1151 if( status ) printerror( status,":FitsFile::WriteF : erreur movabs");
1152 break;
1153
1154 case clear :
1155 {
1156 status = 0;
1157 fits_clear_errmsg();
1158 char* newname = new char[strlen(flnm)+1];
1159 //
1160 newname[0] = '!';
1161 newname[1] = '\0';
1162 strcat(newname, flnm);
1163 fits_create_file(&fptr_,newname,&status);
1164 delete [] newname;
1165 if (status)
1166 {
1167 cout << " error opening file: " << flnm << endl;
1168 printerror(status, "unable to open file, supposed to exist");
1169 }
1170 else cout << " WARNING : file " << flnm << " is overwritten " << endl;
1171 break;
1172 }
1173 case unknown :
1174 printerror(status, " file seems already to exist");
1175 break;
1176
1177 }
1178 }
1179}
1180
1181
1182
1183/*! \fn void SOPHYA::FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList &dvl)
1184
1185create an IMAGE header on FITS file.
1186\param <type> type of data (see method ColTypeFromFits)
1187\param <nbdim> number of dimensions : 1D, 2D, 3D etc. = NAXIS
1188\param <naxisn> array containind sizes of the different dimensions
1189*/
1190void FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList* ptr_dvl)
1191{
1192 int status = 0;
1193 long naxis = nbdim;
1194 long* naxes = new long[nbdim];
1195 // if (hdunum_ == 1)
1196 if (hdunum_ == 0)
1197 {
1198 if (imageOnPrimary_ == false)
1199 {
1200 hdunum_ = 1;
1201 fits_create_img(fptr_,FLOAT_IMG,0,naxes,&status);
1202 writeSignatureOnFits(); }
1203 // else hdunum_--;
1204 }
1205 int k;
1206 for (k=0; k< nbdim; k++) naxes[k] = (long)naxisn[k];
1207 if (type == 'D')
1208 fits_create_img(fptr_,DOUBLE_IMG,naxis,naxes,&status);
1209 else
1210 if (type == 'E')
1211 fits_create_img(fptr_,FLOAT_IMG,naxis,naxes,&status);
1212 else
1213 if (type == 'I')
1214 fits_create_img(fptr_,LONG_IMG,naxis,naxes,&status);
1215 else
1216 {
1217 cout << " type of data: " << type << endl;
1218 throw PException("FitsFile:::makeHeaderImageOnFits:unprogrammed type of data ");
1219 }
1220
1221 hdunum_++;
1222
1223 // write supplementary keywords
1224 // dvl.Print();
1225 if (ptr_dvl != NULL) addKeywordsOfDVList(*ptr_dvl);
1226
1227 delete [] naxes;
1228 if( status ) printerror( status, "erreur creation HDU IMAGE" );
1229
1230}
1231
1232
1233/*! \fn void SOPHYA::FitsOutFile::PutImageToFits(int nbData, double* map) const
1234
1235write double data from array 'map'on an IMAGE extension
1236\param <nbData> number of data to be written
1237*/
1238void FitsOutFile::PutImageToFits(int nbData, double* map) const
1239{
1240 int status = 0;
1241 long npix= nbData;
1242 fits_write_img(fptr_,TDOUBLE,1,npix,map,&status);
1243 if( status ) printerror( status, "erreur ecriture PutImageToFits" );
1244 // writeSignatureOnFits();
1245}
1246
1247/*! \fn void SOPHYA::FitsOutFile::PutImageToFits(int nbData, float* map) const
1248
1249same as previous method with float data
1250*/
1251void FitsOutFile::PutImageToFits(int nbData, float* map) const
1252{
1253 int status = 0;
1254 long npix= nbData;
1255 fits_write_img(fptr_,TFLOAT,1,npix, map,&status);
1256 if( status ) printerror( status, "erreur ecriture PutImageToFits" );
1257 // writeSignatureOnFits();
1258
1259}
1260
1261 /*! \fn void SOPHYA::FitsOutFile::PutImageToFits( int nbData, int* map) const
1262
1263 same as previous method with int data */
1264void FitsOutFile::PutImageToFits( int nbData, int* map) const
1265{
1266 int status = 0;
1267
1268 long npix= nbData;
1269 fits_write_img(fptr_,TINT,1,npix,map,&status);
1270 if( status ) printerror( status, "erreur ecriture PutImageToFits" );
1271 // writeSignatureOnFits();
1272}
1273
1274
1275
1276/*! \fn void SOPHYA::FitsOutFile::makeHeaderBntblOnFits( string fieldType, vector<string> Noms, int nentries, int tfields, DVList &dvl, string extname, vector<int> taille_des_chaines)
1277
1278create an BINTABLE header on FITS file.
1279\param <fieldType> array conta
1280ining characters denoting types of the different column (see method ColTypeFromFits)
1281\param <Noms> array of the names of columns
1282\param <nentries> number of data of each column
1283\param <tfields> number of columns
1284\param <dvl> a SOPHYA DVList containing keywords to be appended
1285\param <extname> keyword EXTNAME for FITS file
1286\param <taille_des_chaines> vector containing the number of characters of data for each char* typed column, with order of appearance in 'fieldType'
1287*/
1288void FitsOutFile::makeHeaderBntblOnFits( string fieldType, vector<string> Noms, int nentries, int tfields, DVList* ptr_dvl, string extname, vector<int> taille_des_chaines)
1289{
1290 int k;
1291 int status = 0;
1292 long nrows;
1293 // verifications de coherence
1294
1295 if (fieldType.length() != tfields)
1296 {
1297 cout << " nombre de champs :" << tfields << "nombre de types: " << fieldType.length() << endl;
1298 throw ParmError("FitsFile:: fields and types don't match");
1299
1300 }
1301 if (tfields > Noms.size())
1302 {
1303 cout << " WARNING: FitsOutFile::makeHeaderBntblOnFits, length of vector of column names not equal to total number of columns" << endl;
1304 for (k=0; k<(tfields-Noms.size()); k++) Noms.push_back( string(" "));
1305 }
1306
1307 // nombre de variables "chaines de caracteres"
1308 int nbString = 0;
1309 for (k=0; k<tfields;k++) if (fieldType[k] == 'A') nbString++;
1310 // coherence de la longueur du vecteur des tailles
1311 if (nbString > taille_des_chaines.size())
1312 {
1313 cout << " WARNING: FitsOutFile::makeHeaderBntblOnFits, length of vector of string lengths not equal to total number of columns" << endl;
1314 int strSz=0;
1315 for (k=0; k<taille_des_chaines.size(); k++) if ( taille_des_chaines[k] > strSz) strSz = taille_des_chaines[k];
1316 for (k=0; k<(nbString-taille_des_chaines.size()); k++) taille_des_chaines.push_back(strSz);
1317 }
1318 char ** ttype= new char*[tfields];
1319 char ** tform= new char*[tfields];
1320 char largeur[FLEN_VALUE];
1321 int noColString=0;
1322 for (k=0; k<tfields;k++)
1323 {
1324 char format[FLEN_VALUE];
1325
1326 if(nentries < 1024)
1327 {
1328 nrows= nentries;
1329 if (fieldType[k] == 'A')
1330 {
1331 sprintf(largeur,"%d",taille_des_chaines[noColString++]);
1332 strcpy(format,largeur);
1333 }
1334 else strcpy(format,"1");
1335 }
1336 else
1337 {
1338 nrows = nentries/1024;
1339 if(nentries%1024 != 0) nrows++;
1340 if (fieldType[k] == 'A')
1341 {
1342 char largaux[FLEN_VALUE];
1343 sprintf(largeur,"%d",taille_des_chaines[noColString]);
1344 sprintf(largaux,"%d",1024*taille_des_chaines[noColString]);
1345 noColString++;
1346 strcpy(format, largaux);
1347 }
1348 else strcpy(format,"1024");
1349 }
1350 strncat(format,&fieldType[k],1);
1351 if (fieldType[k] == 'A')
1352 {
1353 strcat(format,largeur);
1354 }
1355 ttype[k] = const_cast<char*>(Noms[k].c_str());
1356 tform[k]= new char[FLEN_VALUE];
1357 strcpy(tform[k],format);
1358 }
1359 char* extn = const_cast<char*>(extname.c_str());
1360
1361 // create a new empty binary table onto the FITS file
1362 // physical units if they exist, are defined in the DVList object
1363 // so the NULL pointer is given for the tunit parameters.
1364 nrows=0;
1365 fits_create_tbl(fptr_,BINARY_TBL,nrows,tfields,ttype,tform,
1366 NULL,extn,&status);
1367 if( status ) printerror( status );
1368 if ( hdunum_ == 0 ) hdunum_ = 2;
1369 else hdunum_++;
1370 int ii;
1371 for(ii = 0; ii < tfields; ii++)
1372 {
1373 delete [] tform[ii];
1374 }
1375 delete [] ttype;
1376 delete [] tform;
1377 //
1378 // write supplementary keywords
1379 if (ptr_dvl != NULL) addKeywordsOfDVList(*ptr_dvl);
1380}
1381
1382/*! \fn void SOPHYA::FitsOutFile::PutColToFits(int nocol, int nentries, double* donnees) const
1383
1384write double data from array 'donnees ' on column number 'nocol' of a BINTABLE extension.
1385\param <nentries> number of data to be written
1386*/
1387void FitsOutFile::PutColToFits(int nocol, int nentries, double* donnees) const
1388{
1389 int status = 0;
1390 int hdutype;
1391 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1392 if( status ) printerror(status,"PutColToFits: le movabs a foire");
1393 fits_get_hdu_type(fptr_, &hdutype, &status);
1394 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1395 {
1396 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1397 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
1398 }
1399 int code;
1400 long repeat, width;
1401 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1402 if( code != TDOUBLE)
1403 {
1404 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " to be written= DOUBLE " << endl;
1405 }
1406 fits_write_col(fptr_,TDOUBLE,nocol+1,1,1,nentries, donnees ,&status);
1407 if( status ) printerror( status,"erreur ecriture du fichier fits" );
1408}
1409
1410
1411
1412 /*! \fn void SOPHYA::FitsOutFile::PutColToFits(int nocol, int nentries, float* donnees) const
1413
1414same as previous method with float data
1415*/
1416void FitsOutFile::PutColToFits(int nocol, int nentries, float* donnees) const
1417{
1418 int status = 0;
1419 int hdutype;
1420 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1421 if( status ) printerror(status,"PutColToFits: le movabs a foire");
1422 fits_get_hdu_type(fptr_, &hdutype, &status);
1423 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1424 {
1425 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1426 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
1427 }
1428 if(hdutype == ASCII_TBL && nocol>0)
1429 {
1430 throw IOExc("FitsFile::PutColToFits, this HDU is an ASCII table, nocol>0 forbidden");
1431 }
1432 int code;
1433 long repeat, width;
1434 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1435 if( code != TFLOAT)
1436 {
1437 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " (FITS code), to be written= FLOAT " << endl;
1438 }
1439 fits_write_col(fptr_,TFLOAT,nocol+1,1,1,nentries, donnees ,&status);
1440 if( status ) printerror( status,"erreur ecriture du fichier fits" );
1441}
1442
1443
1444/*! \fn void FitsOutFile::PutColToFits(int nocol, int nentries, int* donnees) const
1445
1446same as previous method with int data
1447*/
1448void FitsOutFile::PutColToFits(int nocol, int nentries, int* donnees) const
1449{
1450 int status = 0;
1451 int hdutype;
1452 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1453 if( status ) printerror(status,"PutColToFits: le movabs a foire");
1454 fits_get_hdu_type(fptr_, &hdutype, &status);
1455 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1456 {
1457 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1458 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
1459 }
1460 if(hdutype == ASCII_TBL && nocol>0)
1461 {
1462 throw IOExc("FitsFile::PutColToFits, this HDU is an ASCII table, nocol>0 forbidden");
1463 }
1464 int code;
1465 long repeat, width;
1466 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1467 if( code != TLONG && code != TINT && code != TSHORT )
1468 {
1469 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " (FITS code), to be written= INT " << endl;
1470 }
1471 fits_write_col(fptr_,TINT,nocol+1,1,1,nentries, donnees ,&status);
1472 if( status ) printerror( status," ecriture du fichier fits" );
1473}
1474
1475
1476/*! \fn void SOPHYA::FitsOutFile::PutColToFits(int nocol, int nentries, char** donnees) const
1477same as previous method with char* data
1478*/
1479void FitsOutFile::PutColToFits(int nocol, int nentries, char** donnees) const
1480{
1481 int status = 0;
1482 int hdutype;
1483 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1484 if( status ) printerror(status,"PutColToFits: le movabs a foire");
1485 fits_get_hdu_type(fptr_, &hdutype, &status);
1486 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1487 {
1488 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1489 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
1490 }
1491 if(hdutype == ASCII_TBL && nocol>0)
1492 {
1493 throw IOExc("FitsFile::PutColToFits, this HDU is an ASCII table, nocol>0 forbidden");
1494 }
1495 int code;
1496 long repeat, width;
1497 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1498 if( code != TSTRING)
1499 {
1500 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " (FITS code), to be written= char** " << endl;
1501 }
1502 fits_write_col(fptr_,TSTRING,nocol+1,1,1,nentries, donnees ,&status);
1503 if( status ) printerror( status,"erreur ecriture du fichier fits" );
1504}
1505
1506void FitsOutFile::PutBinTabLine(long NoLine, BnTblLine& ligne) const
1507{
1508 // on ne fait pas de verification de type, ni de dimension ici, pour
1509 // des raisons de performances
1510 int k;
1511 int status= 0;
1512 int anull;
1513 int ncol=0;
1514 long nels=1;
1515 // int nbcols;
1516 // fits_get_num_cols(fptr_, &nbcols,&status);
1517 for (k=0; k<ligne.ddata_.size(); k++, ncol++)
1518 {
1519 fits_write_col(fptr_,TDOUBLE,ncol+1,NoLine+1,1,1, &ligne.ddata_[k] ,&status);
1520 if( status ) printerror( status, "PutBinTabLine : erreur ecriture double" );
1521 }
1522 for (k=0; k<ligne.fdata_.size(); k++, ncol++)
1523 {
1524 fits_write_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1, &ligne.fdata_[k] ,&status);
1525 if( status ) printerror( status, "PutBinTabLine : erreur ecriture float" );
1526 }
1527 for (k=0; k<ligne.idata_.size(); k++, ncol++)
1528 {
1529 fits_write_col(fptr_,TINT,ncol+1,NoLine+1,1,1, &ligne.idata_[k] ,&status);
1530 if( status ) printerror( status, "PutBinTabLine : erreur ecriture entier" );
1531 }
1532
1533 for (k=0; k<ligne.cdata_.size(); k++, ncol++)
1534 {
1535 fits_write_col(fptr_,TSTRING,ncol+1,NoLine+1,1,1, (void*)ligne.cdata_[k].c_str() ,&status);
1536 if( status ) printerror( status, "PutBinTabLine : erreur ecriture caracteres" );
1537 }
1538}
1539
1540
1541/* \fn void SOPHYA::FitsOutFile::DVListIntoPrimaryHeader(DVList& dvl) const
1542
1543Put keywords from a DVList into the primary header of the fits-file
1544*/
1545void FitsOutFile::DVListIntoPrimaryHeader(DVList& dvl) const
1546{
1547 int status = 0;
1548 int hdutype;
1549 fits_movabs_hdu(fptr_,1,&hdutype,&status);
1550 addKeywordsOfDVList(dvl);
1551 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1552}
1553
1554
1555void FitsOutFile::writeSignatureOnFits() const
1556{
1557 int status = 0;
1558 char keyname[LEN_KEYWORD];
1559 char strval[FLEN_VALUE];
1560 char comment[FLEN_COMMENT];
1561 strncpy(keyname, "CREATOR", LEN_KEYWORD);
1562 keyname[LEN_KEYWORD-1] = '\0';
1563 strcpy(strval, "SOPHYA");
1564 strcpy(comment," SOPHYA Package - FITSIOServer ");
1565 fits_write_key(fptr_, TSTRING, keyname, &strval, comment, &status);
1566 if( status ) printerror( status );
1567 fits_write_date(fptr_, &status);
1568 fits_write_comment(fptr_,"..............................................", &status);
1569 fits_write_comment(fptr_, " SOPHYA package - FITSIOSever ", &status);
1570 fits_write_comment(fptr_, " (C) LAL/IN2P3-CNRS Orsay, FRANCE 2000", &status);
1571 fits_write_comment(fptr_, " (C) DAPNIA/CEA Saclay, FRANCE 2000", &status);
1572 fits_write_comment(fptr_,"..............................................", &status);
1573 if( status ) printerror( status, "erreur writeSignatureOnFits" );
1574}
1575
1576
1577void FitsOutFile::addKeywordsOfDVList(DVList& dvl) const
1578{
1579 int status = 0;
1580 fits_write_comment(fptr_,"---------- keywords from SOPHYA ---------", &status);
1581 if (hdunum_ == 1) writeSignatureOnFits();
1582 DVList::ValList::const_iterator it;
1583 for(it = dvl.Begin(); it != dvl.End(); it++)
1584 {
1585 char keytype= (*it).second.elval.typ;
1586 char keyname[10];
1587 strncpy(keyname,(*it).first.substr(0,64).c_str(),10);
1588 string key(keyname);
1589 char comment[FLEN_COMMENT];
1590 char strval[FLEN_VALUE]= "";
1591 char *comkey = "COMMENT";
1592 fits_read_keyword(fptr_, keyname, strval, NULL, &status);
1593 if (status != 0 || strncmp(keyname,comkey,LEN_KEYWORD-1) == 0 )
1594 {
1595 string coco = dvl.GetComment(key);
1596 coco.copy( comment, FLEN_COMMENT-1);
1597 int bout = (coco.length() < FLEN_COMMENT) ? coco.length() : FLEN_COMMENT-1;
1598 comment[bout]= '\0';
1599 status = 0;
1600 switch (keytype)
1601 {
1602 case 'I' :
1603 {
1604 int ival = (int)dvl.GetI(key);
1605 fits_write_key(fptr_,TINT,keyname,&ival, comment,&status);
1606 break;
1607 }
1608 case 'D' :
1609 {
1610 double dval= (double)dvl.GetD(key);
1611 fits_write_key(fptr_,TDOUBLE,keyname,&dval,comment,&status);
1612 break;
1613 }
1614 case 'S' :
1615 {
1616 char strvaleur[FLEN_VALUE]= "";
1617 string valChaine = dvl.GetS(key);
1618 valChaine.copy(strvaleur, FLEN_VALUE-1);
1619 int fin = (valChaine.length() < FLEN_VALUE) ? valChaine.length() : FLEN_VALUE-1;
1620 strvaleur[fin]= '\0';
1621
1622 fits_write_key(fptr_,TSTRING,keyname,&strvaleur,comment,&status);
1623 break;
1624 }
1625 }
1626 }
1627 if( status ) printerror( status,"fitsfile: probleme ecriture mot-cle du dvlist" );
1628 }
1629 fits_write_comment(fptr_,"--------------------------------------", &status);
1630}
1631
1632
1633
Note: See TracBrowser for help on using the repository browser.