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

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

modifs returns de autoreader

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