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

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

dvlist dans primary header de fits

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