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

Last change on this file since 1352 was 1352, checked in by yvon, 25 years ago

les arguments pointeur de diverses fonctions sont passes de int a int_4 etc
+ quelques debogues associes

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