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

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

Modifs cosmetiques + introduction des operateurs << et >> sur

FitsOut/InFile - Reza 12/10/2000

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