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

Last change on this file since 1824 was 1771, checked in by lemeur, 24 years ago

mises a jour pour ELDESTINO

File size: 64.4 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
[1371]9/*!
10 \defgroup FitsIOServer FitsIOServer module
11 This module contains classes which handle FITS format I/O for
12 SOPHYA objects. This module uses cfitsio library.
13*/
[1136]14
[1359]15void BnTblLine::setFormat(int dc, int fc, int ic, int lc, int bc,int cc, vector<string> names)
[1218]16 {
[1359]17 int nbcols = dc + fc + ic + cc + lc + bc;
[1218]18 int maxName = names.size();
19 if (nbcols != maxName)
20 {
21 cout << " WARNING: BnTblLine:: length of vector of column names not equal to total number of columns" << endl;
22 maxName = nbcols < maxName ? nbcols : maxName;
23 }
24 ColName_ = vector<string>(nbcols);
25 for (int k=0; k < maxName; k++) ColName_[k] = names[k];
26 if (dc >0) ddata_ = vector<double>(dc);
27 if (fc >0) fdata_ = vector<float>(fc);
28 if (ic >0) idata_ = vector<int>(fc);
29 if (cc >0) cdata_ = vector<string>(fc);
[1359]30 if (lc >0) ldata_ = vector<long>(lc);
31 if (bc >0) bdata_ = vector<unsigned char>(bc);
[1218]32 }
33
34bool BnTblLine::sameFormat(const BnTblLine& btl) const
35 {
[1359]36 if (btl.ddata_.size() == ddata_.size() && btl.fdata_.size() == fdata_.size() && btl.idata_.size() == idata_.size() && btl.cdata_.size() == cdata_.size() && btl.ldata_.size() == ldata_.size() && btl.bdata_.size() == bdata_.size()) return true;
[1218]37 else return false;
38 }
39
40void BnTblLine::Print()
41 {
42 int k;
43 cout << " ********* ligne ************* " << endl;
44 cout << " *** noms de variables " << endl;
45 for (k=0; k < ColName_.size(); k++) cout << ColName_[k] << " ";
46 cout << endl;
47 cout << " *** variables doubles " << endl;
48 for (k=0; k < ddata_.size(); k++) cout << ddata_[k] << " ";
49 cout << endl;
50 cout << " *** variables float " << endl;
51 for (k=0; k < fdata_.size(); k++) cout << fdata_[k] << " ";
52 cout << endl;
53 cout << " *** variables int " << endl;
54 for (k=0; k < idata_.size(); k++) cout << idata_[k] << " ";
55 cout << endl;
56 cout << " *** variables string " << endl;
57 for (k=0; k < cdata_.size(); k++) cout << cdata_[k] << " ";
58 cout << endl;
[1359]59 cout << " *** variables long " << endl;
60 for (k=0; k < ldata_.size(); k++) cout << ldata_[k] << " ";
61 cout << endl;
62 cout << " *** variables byte " << endl;
63 for (k=0; k < bdata_.size(); k++) cout << (int)bdata_[k] << " ";
64 cout << endl;
[1218]65 cout << " ***************************** " << endl;
66 }
67
68
69
70/*!
71 \class SOPHYA::FitsIOHandler
[1371]72 \ingroup FitsIOServer
[1218]73The class structure is analogous to Sophya-PPersist system :
74Each SOPHYA object XXX is associated with a object of class FITS_XXX
75 (inheriting from FitsFileHandler), to which input/output operations with FITS
76 files are delegated (through a class Hierarchy : FitsFile (virtual),
77 FitsInFile, FitsOutFile) . A typical example of use is the following :
78
79\verbatim
80 int m=... ;
81 SphereHEALPix<r_8> sphere1(m); // definition of the SOPHYA object
82 .... fill the sphere ....
83
84 FITS_SphereHEALPix<r_8> fits_sph1(sphere1);
85 // delegated object
86 fits_sph.Write("myfile.fits"); // writing on FITS file
87
88 FITS_SphereHEALPix<r_8> fits_sph2("myfile.fits");
89 // load a delegated object
90 // from FITS file
91 SphereHEALPix<r_8> sphere2=(SphereHEALPix<r_8>)fits_sph2;
92 // casting the delegated object
93 // into a SOPHYA object
94\endverbatim
95
96
97*/
98
99/*! \fn void SOPHYA::FitsIOHandler::Read(char flnm[],int hdunum)
100
101this method is called from inherited objects :
102
103opens a file 'flnm'
104
105gets parameters in extension-header (hdunum)
106
107calls the method 'ReadFromFits' from the inherited object
108*/
[1136]109void FitsIOHandler::Read(char flnm[],int hdunum)
[839]110{
[1136]111 FitsInFile ifts(flnm);
112 Read(ifts, hdunum);
[839]113}
[1218]114
115 /*! \fn void SOPHYA::FitsIOHandler::Read(FitsInFile& is, int hdunum)
116Read 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.
117 */
[1136]118void FitsIOHandler::Read(FitsInFile& is, int hdunum)
119{
[1300]120 is.ReadHeader(hdunum);
[1136]121 ReadFromFits(is);
122}
123
124
[1218]125/*! \fn void SOPHYA::FitsIOHandler::Write(char flnm[])
126this method is called from inherited objects.
127
128for writing a new object in a new fits-extension :
129
[1234]130\warning By convention, primary header may contain fits-image data.
131For switching off this convention (i.e. to make sure that all data will be on fits-extensions) use the method :
[1218]132
133firstImageOnPrimaryHeader() (see below)
134
135calls the method 'WriteToFits' from the inherited object
136
137*/
[1193]138void FitsIOHandler::Write(char flnm[])
[1136]139
140{
[1231]141 FitsOutFile of(flnm, FitsFile::unknown);
[1136]142 Write(of);
143}
144
145void FitsIOHandler::Write(FitsOutFile& os)
146{
147 WriteToFits(os);
148}
149
150
151
[839]152
153FitsFile::~FitsFile()
154{
155 int status = 0;
[1175]156 if( fptr_ != NULL)
[903]157 {
158 fits_close_file(fptr_,&status);
[1175]159 // je ne fais pas delete fptr_, c'est la lib. fitsio qui a fait
160 // new...
[903]161 }
[1175]162 if( status ) printerror( status );
[839]163}
[903]164
[1136]165
166void FitsFile::printerror(int &status)
167 //*****************************************************/
168 //* Print out cfitsio error messages and exit program */
169 //*****************************************************/
[1045]170{
[1136]171 if( status )
172 {
173 fits_report_error(stderr,status);
174 throw IOExc("FitsFile:: error FITSIO status");
175 }
176 return;
177}
[903]178
[1136]179void FitsFile::printerror(int& status, char* texte)
180 //*****************************************************/
181 //* Print out cfitsio error messages and exit program */
182 //*****************************************************/
183{
184 // print out cfitsio error messages and exit program
185 // print error report
[1235]186 fits_report_error(stderr, status);
[1136]187 cout << " erreur:: " << texte << endl;
188 throw IOExc("FitsFile:: error FITSIO status");
189}
[1703]190void FitsFile::printerrorAndContinue(int& status, char* texte)
191 //*****************************************************/
192 //* Print out cfitsio error messages and exit program */
193 //*****************************************************/
194{
195 // print out cfitsio error messages and exit program
196 // print error report
197 fits_report_error(stderr, status);
198 cout << " erreur:: " << texte << endl;
199 // throw IOExc("FitsFile:: error FITSIO status");
200}
[1136]201
202void FitsFile::ResetStatus(int& status)
203{
204 fits_status_ = status;
205 status = 0;
[1235]206 fits_clear_errmsg();
[1136]207}
208
[1209]209string FitsFile::GetErrStatus(int status)
[1136]210{
211 char text[31];
212 fits_get_errstatus(status, text);
213 return string(text);
214}
215
[1218]216/*!
217 \class SOPHYA::FitsInFile
[1371]218 \ingroup FitsIOServer
[1246]219class for reading SOPHYA objects from FITS Format Files (uses cfitsio lib)
[1218]220*/
221
[1136]222FitsInFile::FitsInFile()
223{
224 InitNull();
225}
[1218]226
[1231]227FitsInFile::FitsInFile(string const & flnm)
[1136]228{
[1175]229 InitNull();
230 int status = 0;
[1231]231 fits_open_file(&fptr_,flnm.c_str(),READONLY,&status);
232 if( status ) printerror( status );
233}
234
235FitsInFile::FitsInFile(const char * flnm)
236{
237 InitNull();
238 int status = 0;
[1175]239 fits_open_file(&fptr_,flnm,READONLY,&status);
240 if( status ) printerror( status );
[1136]241}
242
243
244void FitsInFile::InitNull()
245{
[1300]246 imageDataType_ = FitsDataType_NULL;
[1045]247 naxis_ = 0;
248 nbData_ = 0;
249 nrows_ = 0;
250 nbcols_ = 0;
251 naxisn_.clear();
252 repeat_.clear();
253 noms_.clear();
254 taille_des_chaines_.clear();
255 dvl_.Clear();
[1175]256
[1045]257}
258
[1218]259//////////////////////////////////////////////////////////
260// methods with general purpose
261/////////////////////////////////////////////////////////
[1045]262
[1136]263int FitsInFile::NbBlocks(char flnm[])
[903]264{
265 int status = 0;
266 int nbhdu = 0;
267 fitsfile* fileptr;
268 fits_open_file(&fileptr,flnm,READONLY,&status);
269 if( status ) printerror( status, "NbBlocks: erreur ouverture fichier" );
270 fits_get_num_hdus(fileptr, &nbhdu, &status);
271 fits_close_file(fileptr,&status);
272 return nbhdu;
273}
[1334]274int FitsInFile::NbBlocks()
275{
276 int status = 0;
277 int nbhdu = 0;
278 fits_get_num_hdus(fptr_, &nbhdu, &status);
279 return nbhdu;
280}
[903]281
[1231]282void FitsInFile::GetBlockType(char flnm[], int hdunum, FitsExtensionType& typeOfExtension, int& naxis, vector<int>& naxisn, FitsDataType& dataType, DVList& dvl )
[903]283{
284 int status = 0;
285 fitsfile* fileptr;
286 fits_open_file(&fileptr,flnm,READONLY,&status);
[1209]287 if( status ) printerror( status, "GetBlockType: erreur ouverture fichier" );
[903]288 // move to the specified HDU number
289 int hdutype = 0;
290 fits_movabs_hdu(fileptr,hdunum,&hdutype,&status);
[1209]291 if( status ) printerror( status,"GetBlockType: erreur movabs");
[903]292 if(hdutype == IMAGE_HDU)
293 {
[1231]294 typeOfExtension = FitsExtensionType_IMAGE;
[1300]295 GetImageParameters (fileptr, dataType, naxis, naxisn);
[903]296 }
297 else
298 if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
299 {
300 int nrows = 0;
301 vector<string> noms;
[1300]302 vector<FitsDataType> types;
[903]303 vector<int> taille_des_chaines;
[971]304 GetBinTabParameters(fileptr, naxis, nrows, naxisn, noms, types, taille_des_chaines);
305 int k;
306 for (k=0; k< naxisn.size(); k++) naxisn[k] *= nrows;
[903]307 if(hdutype == ASCII_TBL)
308 {
[1231]309 typeOfExtension = FitsExtensionType_ASCII_TBL;
310 dataType = FitsDataType_ASCII;
[903]311 }
312 else
313 {
[1231]314 typeOfExtension = FitsExtensionType_BINARY_TBL;
[1300]315 dataType = types[0];
[903]316 }
317 }
318 else
319 {
320 cout << " hdutype= " << hdutype << endl;
[1209]321 throw IOExc("FitsFile::GetBlockType: this HDU type is unknown");
[903]322 }
323
[971]324 KeywordsIntoDVList(fileptr, dvl, hdunum);
[903]325 fits_close_file(fileptr,&status);
326}
327
[1136]328
[1300]329void FitsInFile::ReadHeader(int hdunum)
[1045]330{
[1300]331 // InitNull();
[1045]332 int status = 0;
[1334]333 if (hdunum<0)
334 {
[1703]335 throw PException(" FitsInFile::ReadHeader : hdu number must be not negative");
[1334]336 }
[1234]337 if (hdunum != 0 ) hdunum_ = hdunum;
338
339 // si le numero de header non precise
340 else
[1045]341 {
[1234]342 // si c'est le premier objet a lire
343 if (hdunum_ == 0)
[1045]344 {
[1234]345 // on calcule le numero de header a lire
[1246]346 if (imageOnPrimary_ == true ) hdunum_ = 1;
[1234]347 else hdunum_ = 2;
[1045]348 }
[1234]349 // sinon objet suivant
350 else hdunum_++;
[1045]351 }
[1771]352 getHeaderWithSophyaObject();
[1334]353 if ( hdutype_ == FitsExtensionType_NULL )
354 {
355 if (hdunum == 0 && hdunum_ == 1)
356 {
357 hdunum_++;
[1771]358 getHeaderWithSophyaObject();
[1334]359 }
360 else
361 {
362 cout << " WARNING (FitsInFile::ReadHeader) : no SOPHYA object on HDU number : " << hdunum_ << endl;
363 }
364 }
[1418]365 if ( hdutype_ == FitsExtensionType_EOF )
366 {
367 throw PException("FitsFile::ReadHeader, attempt to read through EOF");
368 }
[1045]369}
370
[1354]371string FitsInFile::getStringKeyword(int hdunum, string keyw, int& retStatus)
372{
373 string s;
374 retStatus = 0;
375 int status = 0;
376 if (hdunum != hdunum_ )
377 {
378 int hdutype;
379 fits_movabs_hdu(fptr_,hdunum,&hdutype,&status);
380 }
381
382 char value[FLEN_VALUE];
383 char* keyname= const_cast<char*>(keyw.c_str());
384 fits_read_key_str(fptr_,keyname,value,NULL,&status);
385 if (status == 0)
386 s = string(value);
387 else retStatus = status;
388 if (hdunum != hdunum_ )
389 {
390 int hdutype;
391 if (hdunum_ != 0)
392 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
393 else fits_movabs_hdu(fptr_,1,&hdutype,&status);
394
395 }
396 return s;
397}
398bool FitsInFile::hasKeyword(int hdunum, string keyw)
[1353]399 {
400 bool has=false;
401 int status = 0;
402 if (hdunum != hdunum_ )
403 {
404 int hdutype;
405 fits_movabs_hdu(fptr_,hdunum,&hdutype,&status);
406 }
[1218]407
[1353]408 char value[FLEN_VALUE];
409 char* keyname= const_cast<char*>(keyw.c_str());
410 fits_read_keyword(fptr_,keyname,value,NULL,&status);
411 if (status == 0)
412 has = true;
413 else
414 if (status == KEY_NO_EXIST ) status =0;
415 else fits_report_error(stderr,status);
416 if (hdunum != hdunum_ )
417 {
418 int hdutype;
419 if (hdunum_ != 0)
420 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
421 else fits_movabs_hdu(fptr_,1,&hdutype,&status);
422
423 }
424 return has;
425 }
426
[1771]427void FitsInFile::GetKeywordsFromHeader (int hdunum, list<FitsKeyword>& mots_cles) const
428{
429 int status = 0;
430 int hdutype;
431 fits_movabs_hdu(fptr_,hdunum,&hdutype,&status);
432 if( status ) fits_report_error(stderr,status);
433
434 // get number of keywords
435 int nkeys,keypos;
436 fits_get_hdrpos(fptr_, &nkeys, &keypos,&status);
437 if( status ) fits_report_error(stderr,status);
438 // shift with the number of mandatory keywords
439 int num= 0;
440 // if primary header
441 if (hdunum == 1)
442 {
443 // read NAXIS
444 int naxis=0;
445 fits_read_key(fptr_,TINT,"NAXIS",&naxis,NULL,&status);
446 // number of mandatory keywords
447 num = naxis+3;
448 }
449 // extensions
450 else
451 {
452 if (hdutype == IMAGE_HDU)
453 {
454 // read NAXIS
455 int naxis=0;
456 fits_read_key(fptr_,TINT,"NAXIS",&naxis,NULL,&status);
457 // number of mandatory keywords
458 num = naxis+5;
459 }
460 else
461 if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
462 {
463 // number of mandatory keywords
464 num = 8;
465 }
466 }
467 int j;
468 char keyname[LEN_KEYWORD];
469 char value[FLEN_VALUE];
470 char comment[FLEN_COMMENT];
471 for(j = num+1; j <= nkeys; j++)
472 {
473 char dtype;
474 fits_read_keyn(fptr_,j,keyname,value,comment,&status);
475 if(status)
476 {
477 fits_report_error(stderr,status);
478 status=0;
479 }
480 string kn(keyname);
481 string cm(comment);
482 string val(value);
483 FitsKeyword kw(kn, val, cm);
484 mots_cles.push_back(kw);
485 }
486 if (hdunum_ > 0) fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
487
488}
[1300]489void FitsInFile::GetImageParameters (fitsfile* fileptr,FitsDataType& dataType,int& naxis,vector<int>& naxisn)
[1218]490{
491 int hdunum=0;
[1334]492 // cout << " Reading a FITS image in HDU : " << fits_get_hdu_num(fileptr,&hdunum) << endl;
[1218]493 int status= 0;
494
495 // bits per pixels
[1300]496 int bitpix=0;
[1218]497 fits_read_key(fileptr,TINT,"BITPIX",&bitpix,NULL,&status);
498 if( status ) printerror( status );
[1300]499 if(bitpix == DOUBLE_IMG) dataType = FitsDataType_double;
500 else if(bitpix == FLOAT_IMG) dataType = FitsDataType_float;
501 else if(bitpix == LONG_IMG || bitpix == SHORT_IMG ) dataType = FitsDataType_int;
502 else if (bitpix == BYTE_IMG) dataType = FitsDataType_char;
503 else
504 {
505 cout << " bitpix= " << bitpix << endl;
506 throw PException(" FitsFile::GetImageParameters : unsupported FITS data type");
507 }
[1218]508
509 // number of dimensions in the FITS array
510 naxis= 0;
511 fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
512 if( status ) printerror( status );
513 // read the NAXISn keywords to get image size
514 long* naxes = new long[naxis] ;
515 int nfound;
516 fits_read_keys_lng(fileptr,"NAXIS",1,naxis,naxes,&nfound,&status);
517 if( status ) printerror( status );
518 if (nfound != naxis )
519 cout << " WARNING : " << nfound << " axes found, expected naxis= " << naxis << endl;
520 int k;
521 for (k=0; k<naxis; k++)
522 {
523 naxisn.push_back( (int)naxes[k] );
524 }
525 delete [] naxes;
526}
527
528
529
530
531 /*! \fn DVList SOPHYA::FitsInFile::DVListFromPrimaryHeader() const
532
533 \return the keywords of primary header in a DVList
534
535*/
[1143]536DVList FitsInFile::DVListFromPrimaryHeader() const
537 {
538 int status;
539 DVList dvl;
540 KeywordsIntoDVList(fptr_, dvl, 1);
541 int hdutype = 0;
542 if (hdunum_ > 0) fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
543 return dvl;
544 }
[1136]545
[1771]546void FitsInFile::getHeaderWithSophyaObject()
[971]547{
[1300]548 // si hdunum_ > 1 lit le header correspondant
549 // si hdunum_ = 1 se positionne au (et lit le) premier header qui
550 // contient reellement un objet
[1234]551 int status=0;
552 if (hdunum_ < 1) throw PException(" attempt to read hdunum < 1");
[1300]553 InitNull();
[1234]554 if (hdunum_ == 1)
[839]555 {
[1234]556 // presence of image ?
557 int naxis= 0;
558 fits_read_key(fptr_,TINT,"NAXIS",&naxis,NULL,&status);
559 if( status ) printerror( status );
560 if (naxis > 0 ) // there is an image
561 {
[1334]562 hdutype_ = FitsExtensionType_IMAGE;
[1300]563 GetImageParameters (fptr_, imageDataType_, naxis_, naxisn_);
[1234]564 nbData_ = 1;
565 int k;
566 for (k=0; k<naxis_; k++) if (naxisn_[k] > 0) nbData_ *= naxisn_[k];
567 KeywordsIntoDVList(fptr_, dvl_,hdunum_);
568 }
569 else
570 {
[1334]571 hdutype_ = FitsExtensionType_NULL;
572 KeywordsIntoDVList(fptr_, dvl_,hdunum_);
[1234]573 }
[839]574 }
[1234]575 else
[839]576 {
[1234]577 int hdutype;
578 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
[1334]579
580 if( status )
[1234]581 {
[1334]582 if (status == END_OF_FILE)
583 {
584 hdutype_= FitsExtensionType_EOF;
585 status =0;
586 return;
587 }
588 else
589 {
[1771]590 cout << "WARNING (FitsInFile::getHeaderWithSophyaObject) : error during movabs" << endl;
[1334]591 hdutype_= FitsExtensionType_ERROR;
592 status =0;
593 return;
594 }
595 // printerror( status,":FitsInFile::getHeader : erreur movabs");
596 }
597 if(hdutype == IMAGE_HDU)
598 {
599 hdutype_= FitsExtensionType_IMAGE;
[1300]600 GetImageParameters (fptr_, imageDataType_, naxis_, naxisn_);
[1234]601 nbData_ = 1;
602 int k;
603 for (k=0; k<naxis_; k++) if (naxisn_[k] > 0) nbData_ *= naxisn_[k];
604 KeywordsIntoDVList(fptr_, dvl_,hdunum_);
605 }
[1334]606 else if(hdutype == ASCII_TBL)
[1234]607 {
[1334]608 hdutype_= FitsExtensionType_ASCII_TBL;
[1234]609 GetBinTabParameters(fptr_,nbcols_, nrows_,repeat_, noms_, types_, taille_des_chaines_);
610 KeywordsIntoDVList(fptr_, dvl_, hdunum_);
611 }
[1334]612 else if(hdutype == BINARY_TBL)
613 {
614 hdutype_= FitsExtensionType_BINARY_TBL;
615 GetBinTabParameters(fptr_,nbcols_, nrows_,repeat_, noms_, types_, taille_des_chaines_);
616 KeywordsIntoDVList(fptr_, dvl_, hdunum_);
617 }
618 else
619 {
620 hdutype_= FitsExtensionType_NULL;
621 KeywordsIntoDVList(fptr_, dvl_, hdunum_);
622 }
[839]623 }
[971]624}
[839]625
[1136]626
[1234]627void FitsInFile::moveToFollowingHeader()
628{
629 int status = 0;
630 hdunum_++;
[1771]631 getHeaderWithSophyaObject();
[1334]632 if ( hdutype_ == FitsExtensionType_NULL )
633 {
634 cout << " WARNING (FitsInFile::ReadHeader) : no SOPHYA object on HDU number : " << hdunum_ << endl;
635
636 }
[1234]637}
[1218]638
639
640
[1234]641
642
[1218]643/*! \fn int SOPHYA::FitsInFile::NbColsFromFits() const
644\return number of columns (return 1 if IMAGE)
645*/
[1136]646int FitsInFile::NbColsFromFits() const
[839]647{
[1334]648 if(hdutype_ == FitsExtensionType_BINARY_TBL) return nbcols_;
[971]649 else
[1334]650 if(hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_IMAGE) return 1;
[839]651 else
652 {
[1379]653 cout << " hdutype= " << (int) hdutype_ << endl;
[1334]654 throw PException("FitsFile::NbColsFromFits, HDU not supported");
[839]655 }
656}
657
[1218]658/*! \fn int SOPHYA::FitsInFile::NentriesFromFits(int nocol) const
659\return number of data in the current IMAGE extension on FITS file, or number
660 of data of column number 'nocol' of the current BINTABLE extension
661*/
[1136]662int FitsInFile::NentriesFromFits(int nocol) const
[839]663{
[1334]664 if(hdutype_ == FitsExtensionType_BINARY_TBL) return nrows_*repeat_[nocol];
[1136]665 else
[1334]666 if(hdutype_ == FitsExtensionType_ASCII_TBL) return nrows_;
[1136]667 else
[1334]668 if(hdutype_ == FitsExtensionType_IMAGE) return nbData_;
[1136]669 else
[839]670 {
[1379]671 cout << "hdutype= " << (int) hdutype_ << endl;
[1334]672 throw PException("FitsFile::NentriesFromFits, this HDU is not supported");
[839]673 }
674}
675
[1218]676/*! \fn char SOPHYA::FitsInFile::ColTypeFromFits(int nocol) const
677
678return a character denoting data type of column number 'nocol' in a BINTABLE :
679
680D : double
681
682E : float
683
684I : integer
685
686S : character string
687
688 */
689
[1300]690FitsFile::FitsDataType FitsInFile::ColTypeFromFits(int nocol) const
[839]691{
[1334]692 if(hdutype_ != FitsExtensionType_ASCII_TBL && hdutype_ != FitsExtensionType_BINARY_TBL)
[839]693 {
[1136]694 throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
[839]695 }
[1136]696 return types_[nocol];
[839]697}
[1218]698
699
700/*! \fn string SOPHYA::FitsInFile::ColNameFromFits(int nocol) const
701
702\return name of the column number 'nocol' of the current BINTABLE extension
703 */
704
[1136]705string FitsInFile::ColNameFromFits(int nocol) const
[839]706{
[1334]707 if(hdutype_ != FitsExtensionType_ASCII_TBL && hdutype_ != FitsExtensionType_BINARY_TBL)
[1045]708 {
[1136]709 throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
[1045]710 }
[1136]711 return noms_[nocol];
[839]712}
713
[1218]714/*! \fn int DSOPHYA::FitsInFile::ColStringLengthFromFits(int nocol) const
715
716 \return number of characters of each data for the column number 'nocol' (if char* typed) of the current BINTABLE extension
717*/
718
[1136]719int FitsInFile::ColStringLengthFromFits(int nocol) const
[839]720{
[1334]721 if(hdutype_ != FitsExtensionType_ASCII_TBL && hdutype_ != FitsExtensionType_BINARY_TBL)
[1136]722 {
723 throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
724 }
725 int index=-1;
726 int k;
727 for (k=0; k<=nocol; k++)
728 {
[1300]729 if (types_[k] == FitsDataType_char) index++;
[1136]730 }
731 return taille_des_chaines_[index];
[839]732}
[1218]733
734
735
736/*! \fn void SOPHYA::FitsInFile::GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char ** cdata)
737
738Get the NoLine-th 'line' from the current BINTABLE extension on FITS file,
739 */
740
[1193]741void FitsInFile::GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char ** cdata)
[839]742{
743 int status= 0;
[1136]744 int anull;
745 double dnull= 0.;
746 float fnull= 0.;
747 int inull= 0;
748 char* cnull= "";
749 int dcount = 0.;
750 int fcount = 0.;
751 int icount = 0;
752 int ccount =0;
753 int ncol;
754 long nels=1;
[1499]755 int ligneAsolue = NoLine+1;
[1136]756 for (ncol=0; ncol<nbcols_; ncol++)
[861]757 {
[1499]758 int repetition =repeat_[ncol];
759 int ligneALire = ligneAsolue/repetition;
760 int premierElement = ligneAsolue-ligneALire*repetition;
761 if (premierElement != 0 )
762 {
763 ligneALire++;
764 }
765 else premierElement = repetition;
766
[1136]767 switch (types_[ncol])
768 {
[1300]769 case FitsDataType_double :
[1499]770 {
771 fits_read_col(fptr_,TDOUBLE,ncol+1,ligneALire,premierElement,1,&dnull,&ddata[dcount++],&anull,&status);
[1136]772 break;
[1499]773 }
774 case FitsDataType_float :
775 fits_read_col(fptr_,TFLOAT,ncol+1,ligneALire,premierElement,1,&fnull,&fdata[fcount++],&anull,&status);
[1136]776 break;
[1300]777 case FitsDataType_int :
[1499]778 fits_read_col(fptr_,TINT,ncol+1,ligneALire,premierElement,1,&inull,&idata[icount++],
[1136]779 &anull,&status);
780 break;
[1499]781 case FitsDataType_long :
782 fits_read_col(fptr_,TLONG,ncol+1,ligneALire,premierElement,1,&inull,&idata[icount++], &anull,&status);
783 case FitsDataType_byte :
784 {
785 unsigned char uschar = 0;
786 fits_read_col(fptr_,TBYTE,ncol+1,ligneALire,premierElement,1,&inull,&uschar, &anull,&status);
787 idata[icount++] = (int)uschar;
788 }
789 break;
[1300]790 case FitsDataType_char :
[1499]791 fits_read_col(fptr_,TSTRING,ncol+1,ligneALire,premierElement,1,cnull,&cdata[ccount++],&anull,&status);
[1136]792 break;
793 }
794 if (status)
795 {
796 ResetStatus(status);
797 break;
798 }
[861]799 }
[903]800}
[839]801
[1218]802/*! \fn void SOPHYA::FitsInFile::GetBinTabLine(long NoLine, BnTblLine& ligne)
803Get the NoLine-th 'line' from the current BINTABLE extension on FITS file,
804*/
[1193]805void FitsInFile::GetBinTabLine(long NoLine, BnTblLine& ligne)
806{
807 int status= 0;
808 int anull;
809 double dnull= 0.;
810 float fnull= 0.;
811 int inull= 0;
812 char* cnull= "";
813 int dcount = 0.;
814 int fcount = 0.;
815 int icount = 0;
816 int ccount =0;
817 int ncol;
818 long nels=1;
819 for (ncol=0; ncol<nbcols_; ncol++)
820 {
821 switch (types_[ncol])
822 {
[1300]823 case FitsDataType_double :
[1193]824 fits_read_col(fptr_,TDOUBLE,ncol+1,NoLine+1,1,1,&dnull,&ligne.ddata_[dcount++],&anull,&status);
[1300]825 break;
826 case FitsDataType_float :
[1193]827 fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&ligne.fdata_[fcount++],&anull,&status);
828 break;
[1300]829 case FitsDataType_int :
[1359]830 fits_read_col(fptr_,TINT,ncol+1,NoLine+1,1,1,&inull,&ligne.idata_[icount++], &anull,&status);
[1193]831 break;
[1359]832 case FitsDataType_long :
833 fits_read_col(fptr_,TLONG,ncol+1,NoLine+1,1,1,&inull,&ligne.ldata_[icount++], &anull,&status);
834 break;
835 case FitsDataType_byte :
836 fits_read_col(fptr_,TBYTE,ncol+1,NoLine+1,1,1,&inull,&ligne.bdata_[icount++], &anull,&status);
837 break;
[1300]838 case FitsDataType_char :
[1193]839 char* chaine = new char[taille_des_chaines_[ccount]];
840 fits_read_col(fptr_,TSTRING,ncol+1,NoLine+1,1,1,cnull,&chaine,&anull,&status);
841 ligne.cdata_[ccount++] = string(chaine);
842 break;
843 }
844 if (status)
845 {
846 ResetStatus(status);
847 break;
848 }
849 }
850}
851
[1218]852/*! \fn void SOPHYA::FitsInFile::GetBinTabLine(int NoLine, float* fdata)
853
854Get the NoLine-th float 'line' from the current BINTABLE extension on FITS file,
855*/
[1136]856void FitsInFile::GetBinTabLine(int NoLine, float* fdata)
[903]857{
[1136]858 int status= 0;
859 int anull;
860 float fnull= 0.;
861 long nels=1;
862 int ncol;
863 for (ncol=0; ncol<nbcols_; ncol++)
[861]864 {
[1136]865 fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[ncol],&anull,&status);
866 if (status)
[903]867 {
[1136]868 ResetStatus(status);
869 break;
[903]870 }
[1136]871 }
872}
[839]873
[903]874
[1218]875/*! \fn void SPOPHYA::FitsInFile::GetBinTabFCol(double* valeurs,int nentries, int NoCol) const
876
877fill the array 'valeurs' with double data from the current BINTABLE extension on FITS file, from column number 'NoCol'
878
879\param <nentries> number of data to be read
880*/
[1379]881void FitsInFile::GetBinTabFCol(r_8* valeurs,int nentries, int NoCol) const
[839]882 {
883 int status= 0;
884 int DTYPE;
885 long repeat,width;
886 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
887 if( DTYPE != TDOUBLE)
888 {
[1045]889 if (DTYPE == TFLOAT) cout << " WARNING: reading double from float : conversion will be made by fitsio library" << endl;
890 else
891 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non double");
[839]892 }
893 long nels=nentries;
[971]894 int anull;
[839]895 // no checking for undefined pixels
[971]896 double dnull= 0.;
897 // fits_read_key(fptr_,TDOUBLE,"BAD_DATA",&dnull,NULL,&status);
898 // if (status != 0)
899 // {
900 // dnull = -1.6375e30; // default value
901 // status = 0;
902 // }
[1752]903 // if (nentries != nrows_*repeat)
904 // {
905 // cout << " found " << nentries << " pixels, expected: " << nrows_*repeat << endl;
906 // throw PException(" FitsFile:::GetBinTabFCol ");
907 // }
[839]908 fits_read_col(fptr_,TDOUBLE,NoCol+1,1,1,nels,&dnull,valeurs,
909 &anull,&status);
[1703]910 if( status )
911 {
912 printerrorAndContinue( status,"erreur lecture de colonne" );
913 }
[839]914 }
915
[1218]916/*! \fn void SOPHYA::FitsInFile::GetBinTabFCol(float* valeurs,int nentries, int NoCol) const
917
918 same as previous method with float data
919*/
[1379]920void FitsInFile::GetBinTabFCol(r_4* valeurs,int nentries, int NoCol) const
[839]921 {
922 int status= 0;
923 int DTYPE;
924 long repeat,width;
925 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
926 if( DTYPE != TFLOAT)
927 {
[1045]928 if (DTYPE == TDOUBLE) cout << " WARNING: reading float from double : conversion will be made by fitsio library" << endl;
929 else
930 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
[839]931 }
932 long nels=nentries;
[971]933 int anull;
[839]934 // no checking for undefined pixels
935 float fnull= 0.;
[971]936 // fits_read_key(fptr_,TFLOAT,"BAD_DATA",&fnull,NULL,&status);
937 // if (status != 0)
938 // {
939 // fnull = -1.6375e30; // default value
940 // status = 0;
941 // }
[1752]942 // if (nentries != nrows_*repeat)
943 // {
944 // cout << " found " << nentries << " pixels, expected: " << nrows_*repeat << endl;
945 // throw PException(" FitsFile:::GetBinTabFCol ");
946 // }
[839]947 fits_read_col(fptr_,TFLOAT,NoCol+1,1,1,nels,&fnull,valeurs,
948 &anull,&status);
[1703]949 if( status ) printerrorAndContinue( status,"erreur lecture de colonne" );
[839]950 }
[1136]951
[1218]952/*! \fn void SOPHYA::FitsInFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
953
954 same as previous method with int data
955*/
956
[1379]957void FitsInFile::GetBinTabFCol(int_4* valeurs,int nentries, int NoCol) const
[839]958 {
959 int status= 0;
960 int DTYPE;
961 long repeat,width;
962 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
[1752]963 cout << " en lecture fits trouve le type " << DTYPE << endl;
964 if( DTYPE != TLONG && DTYPE != TINT)
[839]965 {
[1752]966 throw IOExc("FitsFile::GetBinTabFCol, probleme de lecture d'entiers");
[839]967 }
968 long nels=nentries;
969 // no checking for undefined pixels
970 int anull;
971 int inull= 0;
[971]972 // fits_read_key(fptr_,TINT,"BAD_DATA",&inull,NULL,&status);
973 // if (status != 0)
974 // {
975 // inull = -999999; // default value
976 // status = 0;
977 // }
[1752]978 // if (nentries != nrows_*repeat)
979 // {
980 // cout << " found " << nentries << " pixels, expected: " << nrows_*repeat << endl;
981 // throw PException(" FitsFile:::GetBinTabFCol ");
982 // }
983
984
985
986 // voir commentaire dans putColToFits()
[839]987 fits_read_col(fptr_,TINT,NoCol+1,1,1,nels,&inull,valeurs,
988 &anull,&status);
[1703]989 if( status ) printerrorAndContinue( status,"erreur lecture de colonne" );
[839]990 }
[1136]991
[1218]992/*! \fn void SOPHYA::FitsInFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
993
994 same as previous method with char* data
995*/
996
[1136]997void FitsInFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
[839]998 {
999 int status= 0;
1000 int DTYPE;
1001 long repeat,width;
1002 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
[1300]1003 if( DTYPE != TSTRING && DTYPE != TBYTE)
[839]1004 {
[1300]1005 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non string");
[839]1006 }
1007 long nels=nentries;
1008 // no checking for undefined pixels
1009 int anull;
[971]1010 char* cnull= "";
[1752]1011 // if (nentries != nrows_*repeat/width)
1012 // {
1013 // cout << " found " << nentries << " pixels, expected: " << nrows_*repeat/width << endl;
1014 // throw PException(" FitsFile:::GetBinTabFCol ");
1015 // }
[839]1016 long frow=1;
1017 long felem=1;
1018 fits_read_col(fptr_,TSTRING,NoCol+1,frow,felem,nels,cnull,valeurs,
1019 &anull,&status);
[1703]1020 if( status ) printerrorAndContinue( status,"erreur lecture de colonne" );
[839]1021 }
[1045]1022
[1218]1023/*! \fn void SOPHYA::FitsInFile::GetSingleColumn(double* map, int nentries) const
1024fill the array 'map' with double data from the current extension on FITS file.
1025If the extension is BINTABLE, the first column is provided.
1026
1027\param <nentries> number of data to be read
1028*/
[1379]1029void FitsInFile::GetSingleColumn(r_8* map, int nentries) const
[1136]1030{
1031 int status = 0;
[1334]1032 if(hdutype_ == FitsExtensionType_IMAGE)
[1045]1033 {
[1136]1034
[1300]1035 if(imageDataType_ != FitsDataType_double)
[1047]1036 {
[1136]1037 cout << " The data type on fits file is not double...";
1038 cout << " Conversion to double achieved by cfitsio lib" << endl;
[1047]1039 }
[1136]1040
1041 // no checking for undefined pixels
1042 int anull;
1043 double dnull= 0.;
1044
1045 long nels= nentries;
1046 fits_read_img(fptr_,TDOUBLE,1,nels,&dnull,map,&anull,&status);
1047 if( status ) printerror( status );
[1045]1048 }
[1136]1049 else
[1334]1050 if(hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL)
[1136]1051 {
1052 GetBinTabFCol(map,nentries, 0);
1053 }
1054 else
1055 {
[1379]1056 cout << " hdutype= " << (int) hdutype_ << endl;
[1334]1057 throw IOExc("FitsFile::GetSingleColumn, this HDU is unknown");
[1136]1058 }
[1045]1059}
1060
[1218]1061/*! \fn void SOPHYA::FitsInFile::GetSingleColumn(float* map, int nentries) const
1062same as above with float data
1063*/
[1379]1064void FitsInFile::GetSingleColumn(r_4* map, int nentries) const
[1045]1065{
[1136]1066 int status = 0;
[1334]1067 if(hdutype_ == FitsExtensionType_IMAGE)
[1045]1068 {
[1300]1069 if(imageDataType_ != FitsDataType_float)
[1047]1070 {
[1136]1071 cout << " The data type on fits file is not float ";
1072 cout << " Conversion to float achieved by cfitsio lib" << endl;
[1047]1073 }
[1136]1074 // no checking for undefined pixels
1075 int anull;
1076 float fnull= 0.;
1077
1078 long nels= nentries;
1079 fits_read_img(fptr_,TFLOAT,1,nels,&fnull, map,&anull,&status);
1080 if( status ) printerror( status );
[1045]1081 }
[839]1082 else
[1334]1083 if(hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL)
[1136]1084 {
1085 GetBinTabFCol(map,nentries, 0);
1086 }
[839]1087 else
1088 {
[1379]1089 cout << " hdutype= " << (int) hdutype_ << endl;
[1136]1090 throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
[839]1091 }
1092}
1093
[1218]1094/*! \fn void SOPHYA::FitsInFile::GetSingleColumn( int* map, int nentries) const
1095 same as above with int data
1096*/
[1379]1097void FitsInFile::GetSingleColumn( int_4* map, int nentries) const
[839]1098{
[1136]1099 int status = 0;
[1334]1100 if(hdutype_ == FitsExtensionType_IMAGE)
[839]1101 {
[1300]1102 if(imageDataType_ != FitsDataType_int)
[1136]1103 {
1104 cout << " The data type on fits file is not int ";
1105 cout << " Conversion to float achieved by cfitsio lib" << endl;
1106 }
1107 // no checking for undefined pixels
1108 int anull;
1109 float fnull= 0.;
1110
1111 long nels= nentries;
1112 fits_read_img(fptr_,TINT,1,nels,&fnull,map,&anull,&status);
1113 if( status ) printerror( status );
[839]1114 }
1115 else
[1334]1116 if(hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL)
[1136]1117 {
1118 GetBinTabFCol(map,nentries, 0);
1119 }
[839]1120 else
[1136]1121 {
[1379]1122 cout << " hdutype= " << (int) hdutype_ << endl;
[1136]1123 throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
1124 }
[839]1125}
1126
[1136]1127void FitsInFile::GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
[903]1128 vector<int>& repeat,
1129 vector<string>& noms,
[1300]1130 vector<FitsDataType>& types,
[903]1131 vector<int>& taille_des_chaines)
[839]1132{
1133 int status= 0;
[903]1134 int hdunum=0;
1135 int hdutype=0;
1136 fits_get_hdu_num(fileptr,&hdunum);
1137 fits_get_hdu_type(fileptr, &hdutype, &status);
1138
1139 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
[839]1140 {
[903]1141 throw IOExc("FitsFile::GetBinTabParameters this HDU is not an ASCII table nor a binary table");
[839]1142 }
[1334]1143 // if(hdutype == ASCII_TBL)
1144 // cout << " Reading a FITS ascii table in HDU : " << hdunum << endl;
1145 // if(hdutype == BINARY_TBL)
1146 // cout << " Reading a FITS binary table in HDU : " << hdunum << endl;
[839]1147
1148 // get the number of columns
[903]1149 fits_get_num_cols(fileptr, &nbcols,&status);
[839]1150 if( status ) printerror( status );
1151
1152 // get the number of rows
1153 long naxis2= 0;
[903]1154 fits_get_num_rows(fileptr,&naxis2,&status);
[839]1155 if( status ) printerror( status );
[903]1156 nrows = (int)naxis2;
[839]1157
1158 // get the datatype, names and the repeat count
[903]1159 noms.clear();
1160 noms.reserve(nbcols);
1161 types.clear();
1162 types.reserve(nbcols);
1163 repeat.clear();
1164 repeat.reserve(nbcols);
1165 taille_des_chaines.clear();
[839]1166 char **ttype = new char*[nbcols];
[923]1167 int ii;
[1175]1168 //
1169 //
[923]1170 for (ii=0; ii < nbcols; ii++) ttype[ii]=new char[FLEN_VALUE];
[839]1171 int nfound;
[903]1172 fits_read_keys_str(fileptr, "TTYPE",1,nbcols,ttype,&nfound, &status);
[839]1173 if( status ) printerror( status,"erreur lecture des noms de colonne");
1174 int rept=0;
[1300]1175 if(hdutype == ASCII_TBL)
[839]1176 {
[1300]1177 for(ii = 0; ii < nbcols; ii++)
[839]1178 {
[1300]1179 int DTYPE;
1180 long width;
1181 long repete = 0;
1182 fits_get_coltype(fileptr,ii+1,&DTYPE,&repete,&width,&status);
1183 if( status ) printerror( status,"erreur lecture type de colonne");
1184 rept = repete;
1185 noms.push_back(string(ttype[ii]));
1186 switch (DTYPE)
1187 {
1188 case TDOUBLE :
1189 types.push_back(FitsDataType_double);
1190 break;
1191 case TFLOAT :
1192 types.push_back(FitsDataType_float);
1193 break;
1194 case TLONG :
[1359]1195 types.push_back(FitsDataType_long);
[1300]1196 break;
1197 case TSHORT :
1198 types.push_back(FitsDataType_int);
1199 break;
1200 case TSTRING :
1201 types.push_back(FitsDataType_char);
1202 taille_des_chaines.push_back(width);
1203 rept/=width;
1204 break;
1205 default :
1206 cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
1207 throw IOExc("FitsFile::GetBinTabParameters, unsupported data type of field, for ASCII table");
1208 }
1209 repeat.push_back(rept);
[839]1210 }
[1136]1211 }
[1300]1212 else
1213 {
1214 for(ii = 0; ii < nbcols; ii++)
1215 {
1216 int DTYPE;
1217 long width;
1218 long repete = 0;
1219 fits_get_coltype(fileptr,ii+1,&DTYPE,&repete,&width,&status);
1220 if( status ) printerror( status,"erreur lecture type de colonne");
1221 rept = repete;
1222 noms.push_back(string(ttype[ii]));
1223 switch (DTYPE)
1224 {
1225 case TDOUBLE :
1226 types.push_back(FitsDataType_double);
1227 break;
1228 case TFLOAT :
1229 types.push_back(FitsDataType_float);
1230 break;
1231 case TLONG :
[1359]1232 types.push_back(FitsDataType_long);
[1300]1233 break;
1234 case TINT :
1235 types.push_back(FitsDataType_int);
1236 break;
1237 case TSHORT :
1238 types.push_back(FitsDataType_int);
1239 break;
1240 case TSTRING :
1241 types.push_back(FitsDataType_char);
1242 taille_des_chaines.push_back(width);
1243 rept/=width;
1244 break;
1245 case TBYTE :
[1359]1246 types.push_back(FitsDataType_byte);
[1300]1247 break;
1248 default :
1249 cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
1250 throw IOExc("FitsFile::GetBinTabParameters, unsupported data type of field, for BINTABLE");
1251 }
1252 repeat.push_back(rept);
1253 }
1254 }
[1136]1255 for (ii=0; ii < nbcols; ii++) delete [] ttype[ii];
1256 delete [] ttype;
1257}
1258
1259void FitsInFile::KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum)
1260{
1261 int status = 0;
1262 int hdutype;
1263 fits_movabs_hdu(fileptr,hdunum,&hdutype,&status);
1264 if( status ) printerror( status,":KeywordsIntoDVList : erreur movabs");
1265 // get number of keywords
1266 int nkeys,keypos;
1267 fits_get_hdrpos(fileptr,&nkeys,&keypos,&status);
1268 if( status ) printerror( status );
1269
1270 // put keywords in a DVList object
1271 char keyname[LEN_KEYWORD]= "";
1272 char strval[FLEN_VALUE]= "";
1273 char dtype;
1274 char card[FLEN_CARD];
1275 char *comkey = "COMMENT";
[1143]1276 char comment[FLEN_COMMENT];
[1136]1277
1278 // shift with the number of mandatory keywords
[1143]1279 // int num= 8;
1280 int num= 0;
1281 // primary header
1282 if (hdunum == 1)
1283 {
1284 // read NAXIS
1285 int naxis=0;
1286 fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
1287 // number of mandatory keywords
1288 num = naxis+3;
1289 }
1290 // extensions
1291 else
1292 {
1293 if (hdutype == IMAGE_HDU)
1294 {
1295 // read NAXIS
1296 int naxis=0;
1297 fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
1298 // number of mandatory keywords
1299 num = naxis+5;
1300 }
1301 else
1302 if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
1303 {
1304 // number of mandatory keywords
1305 num = 8;
1306 }
1307 }
[1136]1308 int j;
1309 for(j = num+1; j <= nkeys; j++)
1310 {
1311 fits_read_keyn(fileptr,j,card,strval,NULL,&status);
1312 if(status) printerror(status);
1313
1314 strncpy(keyname,card,LEN_KEYWORD-1);
1315 if(strncmp(keyname,comkey,LEN_KEYWORD-1) != 0 && strlen(keyname) != 0
1316 && strlen(strval) != 0)
1317 {
1318 fits_get_keytype(strval,&dtype,&status);
1319 if(status) printerror(status);
1320
1321 strip(keyname, 'B',' ');
1322 strip(strval, 'B',' ');
1323 strip(strval, 'B','\'');
1324
1325 switch( dtype )
1326 {
1327 case 'C':
[1143]1328 fits_read_key(fileptr,TSTRING,keyname,strval,comment,&status);
1329 dvl[keyname]= strval;
1330 dvl.SetComment(keyname, comment);
[1136]1331 break;
1332 case 'I':
1333 int ival;
[1143]1334 fits_read_key(fileptr,TINT,keyname,&ival,comment,&status);
[1136]1335 dvl[keyname]= (int_4) ival; // Portage mac DY
[1143]1336 dvl.SetComment(keyname, comment);
[1136]1337 break;
1338 case 'L':
1339 int ilog;
[1143]1340 fits_read_key(fileptr,TLOGICAL,keyname,&ilog,comment,&status);
[1136]1341 dvl[keyname]= (int_4) ilog;
[1143]1342 dvl.SetComment(keyname, comment);
[1136]1343 break;
1344 case 'F':
1345 double dval;
[1143]1346 fits_read_key(fileptr,TDOUBLE,keyname,&dval,comment,&status);
[1136]1347 dvl[keyname]= dval;
[1143]1348 dvl.SetComment(keyname, comment);
[1136]1349 break;
1350 }
1351
1352 }
[839]1353 }
[1136]1354 // dvl_.Print();
1355}
1356
[1218]1357
1358/*!
1359 \class SOPHYA::FitsOutFile
[1371]1360 \ingroup FitsIOServer
[1218]1361 Class for loading SOPHYA objects from FITS Format Files (uses cfitsio lib)
1362*/
1363
[1136]1364FitsOutFile::FitsOutFile()
1365{
[1193]1366 InitNull();
[903]1367}
[839]1368
[1218]1369 /*! \fn SOPHYA::FitsOutFile::FitsOutFile(char flnm[], WriteMode wrm)
1370
1371\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)
1372
1373 */
[1231]1374
1375FitsOutFile::FitsOutFile(string const & flnm, WriteMode wrm)
[1136]1376{
[1231]1377 InitNull();
1378 openoutputfitsfile(flnm.c_str(), wrm);
1379}
[839]1380
[1231]1381FitsOutFile::FitsOutFile(const char * flnm, WriteMode wrm)
1382{
[1136]1383 InitNull();
[1231]1384 openoutputfitsfile(flnm, wrm);
1385}
1386
1387void FitsOutFile::openoutputfitsfile(const char * flnm, WriteMode wrm)
1388{
[1136]1389 int status = 0;
[839]1390
[1136]1391 // create new FITS file
[1183]1392 fits_create_file(&fptr_,flnm,&status);
1393 if( status )
[1136]1394 {
[1193]1395
1396 switch (wrm)
1397 {
[1183]1398 // si on veut ecrire a la fin de ce fichier
[1193]1399 case append :
[1183]1400 status = 0;
[1235]1401 fits_clear_errmsg();
[1183]1402 fits_open_file(&fptr_,flnm,READWRITE,&status);
1403 if( status )
1404 {
1405 cout << " error opening file: " << flnm << endl;
1406 printerror(status, "failure opening a file supposed to exist");
1407 }
1408 else cout << " file " << flnm << " opened, new objects will be appended " << endl;
1409 fits_get_num_hdus(fptr_, &hdunum_, &status);
1410 int hdutype;
1411 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1412 if( status ) printerror( status,":FitsFile::WriteF : erreur movabs");
[1193]1413 break;
1414
1415 case clear :
[1183]1416 {
1417 status = 0;
[1235]1418 fits_clear_errmsg();
[1183]1419 char* newname = new char[strlen(flnm)+1];
1420 //
1421 newname[0] = '!';
1422 newname[1] = '\0';
1423 strcat(newname, flnm);
1424 fits_create_file(&fptr_,newname,&status);
[1193]1425 delete [] newname;
[1183]1426 if (status)
1427 {
1428 cout << " error opening file: " << flnm << endl;
1429 printerror(status, "unable to open file, supposed to exist");
1430 }
[1193]1431 else cout << " WARNING : file " << flnm << " is overwritten " << endl;
1432 break;
[1183]1433 }
[1193]1434 case unknown :
1435 printerror(status, " file seems already to exist");
1436 break;
[1183]1437
[1193]1438 }
[1136]1439 }
1440}
1441
1442
1443
[1218]1444/*! \fn void SOPHYA::FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList &dvl)
1445
1446create an IMAGE header on FITS file.
1447\param <type> type of data (see method ColTypeFromFits)
1448\param <nbdim> number of dimensions : 1D, 2D, 3D etc. = NAXIS
1449\param <naxisn> array containind sizes of the different dimensions
1450*/
[1221]1451void FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList* ptr_dvl)
[1136]1452{
1453 int status = 0;
1454 long naxis = nbdim;
1455 long* naxes = new long[nbdim];
[1246]1456 bool hdunfirst= (hdunum_ == 0);
1457 if (hdunfirst)
[1136]1458 {
[1143]1459 if (imageOnPrimary_ == false)
1460 {
[1234]1461 hdunum_ = 1;
[1143]1462 fits_create_img(fptr_,FLOAT_IMG,0,naxes,&status);
[1246]1463 }
[1136]1464 }
1465 int k;
1466 for (k=0; k< nbdim; k++) naxes[k] = (long)naxisn[k];
1467 if (type == 'D')
1468 fits_create_img(fptr_,DOUBLE_IMG,naxis,naxes,&status);
1469 else
1470 if (type == 'E')
1471 fits_create_img(fptr_,FLOAT_IMG,naxis,naxes,&status);
1472 else
1473 if (type == 'I')
1474 fits_create_img(fptr_,LONG_IMG,naxis,naxes,&status);
1475 else
1476 {
1477 cout << " type of data: " << type << endl;
1478 throw PException("FitsFile:::makeHeaderImageOnFits:unprogrammed type of data ");
1479 }
[1353]1480
[1246]1481 // on ajoute eventuellement un dvlist prepare et la doc SOPHYA
[1136]1482 hdunum_++;
[1246]1483 if (hdunfirst)
1484 {
1485 addDVListOnPrimary();
1486 writeSignatureOnFits(1);
1487 }
[1143]1488
[1353]1489 // header format FITS
1490
1491 writeAppendedHeaderOnFits();
1492
1493 // write supplementary keywords (from SOPHYA)
[1143]1494 // dvl.Print();
[1221]1495 if (ptr_dvl != NULL) addKeywordsOfDVList(*ptr_dvl);
[1143]1496
[1136]1497 delete [] naxes;
1498 if( status ) printerror( status, "erreur creation HDU IMAGE" );
1499
1500}
[1218]1501
1502
1503/*! \fn void SOPHYA::FitsOutFile::PutImageToFits(int nbData, double* map) const
1504
1505write double data from array 'map'on an IMAGE extension
1506\param <nbData> number of data to be written
1507*/
[1379]1508void FitsOutFile::PutImageToFits(int nbData, r_8* map) const
[1136]1509{
1510 int status = 0;
1511 long npix= nbData;
1512 fits_write_img(fptr_,TDOUBLE,1,npix,map,&status);
[1209]1513 if( status ) printerror( status, "erreur ecriture PutImageToFits" );
[1136]1514}
1515
[1218]1516/*! \fn void SOPHYA::FitsOutFile::PutImageToFits(int nbData, float* map) const
1517
1518same as previous method with float data
1519*/
[1379]1520void FitsOutFile::PutImageToFits(int nbData, r_4* map) const
[1136]1521{
1522 int status = 0;
1523 long npix= nbData;
1524 fits_write_img(fptr_,TFLOAT,1,npix, map,&status);
[1209]1525 if( status ) printerror( status, "erreur ecriture PutImageToFits" );
[1136]1526
1527}
[1218]1528
1529 /*! \fn void SOPHYA::FitsOutFile::PutImageToFits( int nbData, int* map) const
1530
1531 same as previous method with int data */
[1379]1532void FitsOutFile::PutImageToFits( int nbData, int_4* map) const
[1136]1533{
1534 int status = 0;
1535
1536 long npix= nbData;
1537 fits_write_img(fptr_,TINT,1,npix,map,&status);
[1209]1538 if( status ) printerror( status, "erreur ecriture PutImageToFits" );
[1136]1539}
1540
1541
1542
[1218]1543/*! \fn void SOPHYA::FitsOutFile::makeHeaderBntblOnFits( string fieldType, vector<string> Noms, int nentries, int tfields, DVList &dvl, string extname, vector<int> taille_des_chaines)
1544
1545create an BINTABLE header on FITS file.
1546\param <fieldType> array conta
1547ining characters denoting types of the different column (see method ColTypeFromFits)
1548\param <Noms> array of the names of columns
1549\param <nentries> number of data of each column
1550\param <tfields> number of columns
1551\param <dvl> a SOPHYA DVList containing keywords to be appended
1552\param <extname> keyword EXTNAME for FITS file
1553\param <taille_des_chaines> vector containing the number of characters of data for each char* typed column, with order of appearance in 'fieldType'
1554*/
[1300]1555void FitsOutFile::makeHeaderBntblOnFits(string fieldType, vector<string> Noms, int nentries, int tfields, DVList* ptr_dvl, string extname, vector<int> taille_des_chaines)
[839]1556{
[1209]1557 int k;
[839]1558 int status = 0;
1559 long nrows;
[1300]1560 // verifications de coherences
[1209]1561
[1193]1562 if (fieldType.length() != tfields)
[839]1563 {
[1193]1564 cout << " nombre de champs :" << tfields << "nombre de types: " << fieldType.length() << endl;
[1136]1565 throw ParmError("FitsFile:: fields and types don't match");
[839]1566
1567 }
[1209]1568 if (tfields > Noms.size())
1569 {
1570 cout << " WARNING: FitsOutFile::makeHeaderBntblOnFits, length of vector of column names not equal to total number of columns" << endl;
1571 for (k=0; k<(tfields-Noms.size()); k++) Noms.push_back( string(" "));
1572 }
1573
1574 // nombre de variables "chaines de caracteres"
1575 int nbString = 0;
1576 for (k=0; k<tfields;k++) if (fieldType[k] == 'A') nbString++;
1577 // coherence de la longueur du vecteur des tailles
1578 if (nbString > taille_des_chaines.size())
1579 {
1580 cout << " WARNING: FitsOutFile::makeHeaderBntblOnFits, length of vector of string lengths not equal to total number of columns" << endl;
1581 int strSz=0;
1582 for (k=0; k<taille_des_chaines.size(); k++) if ( taille_des_chaines[k] > strSz) strSz = taille_des_chaines[k];
1583 for (k=0; k<(nbString-taille_des_chaines.size()); k++) taille_des_chaines.push_back(strSz);
1584 }
[839]1585 char ** ttype= new char*[tfields];
1586 char ** tform= new char*[tfields];
1587 char largeur[FLEN_VALUE];
1588 int noColString=0;
[971]1589 for (k=0; k<tfields;k++)
[839]1590 {
1591 char format[FLEN_VALUE];
1592
1593 if(nentries < 1024)
1594 {
1595 nrows= nentries;
1596 if (fieldType[k] == 'A')
1597 {
1598 sprintf(largeur,"%d",taille_des_chaines[noColString++]);
1599 strcpy(format,largeur);
1600 }
1601 else strcpy(format,"1");
1602 }
1603 else
1604 {
1605 nrows = nentries/1024;
1606 if(nentries%1024 != 0) nrows++;
1607 if (fieldType[k] == 'A')
1608 {
[1136]1609 char largaux[FLEN_VALUE];
1610 sprintf(largeur,"%d",taille_des_chaines[noColString]);
1611 sprintf(largaux,"%d",1024*taille_des_chaines[noColString]);
1612 noColString++;
1613 strcpy(format, largaux);
[839]1614 }
1615 else strcpy(format,"1024");
1616 }
1617 strncat(format,&fieldType[k],1);
1618 if (fieldType[k] == 'A')
1619 {
1620 strcat(format,largeur);
1621 }
[1193]1622 ttype[k] = const_cast<char*>(Noms[k].c_str());
[839]1623 tform[k]= new char[FLEN_VALUE];
1624 strcpy(tform[k],format);
1625 }
[1193]1626 char* extn = const_cast<char*>(extname.c_str());
[839]1627
1628 // create a new empty binary table onto the FITS file
1629 // physical units if they exist, are defined in the DVList object
1630 // so the NULL pointer is given for the tunit parameters.
1631 nrows=0;
1632 fits_create_tbl(fptr_,BINARY_TBL,nrows,tfields,ttype,tform,
1633 NULL,extn,&status);
1634 if( status ) printerror( status );
[1353]1635
1636 int ii;
1637 for(ii = 0; ii < tfields; ii++)
1638 {
1639 delete [] tform[ii];
1640 }
1641 delete [] ttype;
1642 delete [] tform;
1643
1644 // on ajoute eventuellement des mots-cles
1645
[1246]1646 if ( hdunum_ == 0 )
1647 {
1648 hdunum_ = 2;
1649 addDVListOnPrimary();
1650 writeSignatureOnFits(1);
1651 }
[1026]1652 else hdunum_++;
[1353]1653
1654 // header format FITS
1655
1656 writeAppendedHeaderOnFits();
1657
1658 // write SOPHYA keywords
[1221]1659 if (ptr_dvl != NULL) addKeywordsOfDVList(*ptr_dvl);
[839]1660}
1661
[1353]1662
1663
[1218]1664/*! \fn void SOPHYA::FitsOutFile::PutColToFits(int nocol, int nentries, double* donnees) const
1665
1666write double data from array 'donnees ' on column number 'nocol' of a BINTABLE extension.
1667\param <nentries> number of data to be written
1668*/
[1353]1669
[1379]1670void FitsOutFile::PutColToFits(int nocol, int nentries, r_8* donnees) const
[839]1671{
1672 int status = 0;
[971]1673 int hdutype;
[839]1674 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
[1209]1675 if( status ) printerror(status,"PutColToFits: le movabs a foire");
[839]1676 fits_get_hdu_type(fptr_, &hdutype, &status);
[867]1677 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1678 {
1679 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
[1209]1680 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
[867]1681 }
[839]1682 int code;
1683 long repeat, width;
1684 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1685 if( code != TDOUBLE)
1686 {
[1209]1687 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " to be written= DOUBLE " << endl;
[839]1688 }
[1499]1689 // cout << " 10 elements de colonne " << endl;
1690 // for (int toto=0; toto < 10; toto++) cout << donnees[toto] << endl;
[839]1691 fits_write_col(fptr_,TDOUBLE,nocol+1,1,1,nentries, donnees ,&status);
[1752]1692 if( status ) printerror( status,"erreur ecriture col. double, dans fichier fits" );
[839]1693}
[1218]1694
1695
1696
1697 /*! \fn void SOPHYA::FitsOutFile::PutColToFits(int nocol, int nentries, float* donnees) const
1698
1699same as previous method with float data
1700*/
[1379]1701void FitsOutFile::PutColToFits(int nocol, int nentries, r_4* donnees) const
[839]1702{
1703 int status = 0;
1704 int hdutype;
1705 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
[1209]1706 if( status ) printerror(status,"PutColToFits: le movabs a foire");
[839]1707 fits_get_hdu_type(fptr_, &hdutype, &status);
1708 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1709 {
1710 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
[1209]1711 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
[839]1712 }
1713 if(hdutype == ASCII_TBL && nocol>0)
1714 {
[1209]1715 throw IOExc("FitsFile::PutColToFits, this HDU is an ASCII table, nocol>0 forbidden");
[839]1716 }
1717 int code;
1718 long repeat, width;
1719 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1720 if( code != TFLOAT)
1721 {
[1209]1722 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " (FITS code), to be written= FLOAT " << endl;
[839]1723 }
1724 fits_write_col(fptr_,TFLOAT,nocol+1,1,1,nentries, donnees ,&status);
[1752]1725 if( status ) printerror( status,"erreur ecriture col. floats, dans fichier fits" );
[839]1726}
[1218]1727
1728
1729/*! \fn void FitsOutFile::PutColToFits(int nocol, int nentries, int* donnees) const
1730
1731same as previous method with int data
1732*/
[1379]1733void FitsOutFile::PutColToFits(int nocol, int nentries, int_4* donnees) const
[839]1734{
1735 int status = 0;
1736 int hdutype;
1737 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
[1209]1738 if( status ) printerror(status,"PutColToFits: le movabs a foire");
[839]1739 fits_get_hdu_type(fptr_, &hdutype, &status);
1740 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1741 {
1742 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
[1209]1743 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
[839]1744 }
1745 if(hdutype == ASCII_TBL && nocol>0)
1746 {
[1209]1747 throw IOExc("FitsFile::PutColToFits, this HDU is an ASCII table, nocol>0 forbidden");
[839]1748 }
1749 int code;
1750 long repeat, width;
1751 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
[1752]1752
1753
1754// la logique voudrait qu'on distingue TLONG et TINT. Mais si j'ecris
1755// et relis immediatement quelque chose en TLONG l'experience montre
1756// que ca foire. Donc, je fais tout en TINT, d'ailleurs cfitsio n'a pas
1757// (apparemment) d'entiers de longueur superieure a 32 bits.
1758// En fait, je n'y comprend rien. A suivre (GLM).
1759 if (code == TINT || code == TLONG )
[839]1760 {
[1752]1761 cout << " j'ecris des TINT" << endl;
1762 fits_write_col(fptr_,TINT,nocol+1,1,1,nentries, donnees ,&status);
1763 }
1764 else if (code == TSHORT)
1765 {
1766 cout << " j'ecris des TSHORT " << endl;
1767 fits_write_col(fptr_,TSHORT,nocol+1,1,1,nentries, donnees ,&status);
1768 }
1769 else
1770 {
1771 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " (FITS code), to be written= integers " << endl;
[839]1772 }
[1752]1773 if( status ) printerror( status,"erreur ecriture col. entiers, dans fichier fits" );
[839]1774}
[1218]1775
1776
1777/*! \fn void SOPHYA::FitsOutFile::PutColToFits(int nocol, int nentries, char** donnees) const
1778same as previous method with char* data
1779*/
[1209]1780void FitsOutFile::PutColToFits(int nocol, int nentries, char** donnees) const
[839]1781{
1782 int status = 0;
1783 int hdutype;
1784 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
[1209]1785 if( status ) printerror(status,"PutColToFits: le movabs a foire");
[839]1786 fits_get_hdu_type(fptr_, &hdutype, &status);
1787 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1788 {
1789 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
[1209]1790 throw IOExc("FitsFile::PutColToFits, this HDU is not an ASCII table nor a binary table");
[839]1791 }
1792 if(hdutype == ASCII_TBL && nocol>0)
1793 {
[1209]1794 throw IOExc("FitsFile::PutColToFits, this HDU is an ASCII table, nocol>0 forbidden");
[839]1795 }
1796 int code;
1797 long repeat, width;
1798 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1799 if( code != TSTRING)
1800 {
[1209]1801 cout << " WARNING : types don't match (PutColToFits) : on fits file= " << code << " (FITS code), to be written= char** " << endl;
[839]1802 }
1803 fits_write_col(fptr_,TSTRING,nocol+1,1,1,nentries, donnees ,&status);
[1752]1804 if( status ) printerror( status,"erreur ecriture col. chars, dans fichier fits" );
[839]1805}
1806
[1209]1807void FitsOutFile::PutBinTabLine(long NoLine, BnTblLine& ligne) const
[1193]1808{
[1209]1809 // on ne fait pas de verification de type, ni de dimension ici, pour
1810 // des raisons de performances
1811 int k;
[1193]1812 int status= 0;
1813 int anull;
[1209]1814 int ncol=0;
[1193]1815 long nels=1;
[1209]1816 // int nbcols;
1817 // fits_get_num_cols(fptr_, &nbcols,&status);
1818 for (k=0; k<ligne.ddata_.size(); k++, ncol++)
[1193]1819 {
[1209]1820 fits_write_col(fptr_,TDOUBLE,ncol+1,NoLine+1,1,1, &ligne.ddata_[k] ,&status);
1821 if( status ) printerror( status, "PutBinTabLine : erreur ecriture double" );
[1193]1822 }
[1209]1823 for (k=0; k<ligne.fdata_.size(); k++, ncol++)
1824 {
1825 fits_write_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1, &ligne.fdata_[k] ,&status);
1826 if( status ) printerror( status, "PutBinTabLine : erreur ecriture float" );
1827 }
1828 for (k=0; k<ligne.idata_.size(); k++, ncol++)
1829 {
1830 fits_write_col(fptr_,TINT,ncol+1,NoLine+1,1,1, &ligne.idata_[k] ,&status);
1831 if( status ) printerror( status, "PutBinTabLine : erreur ecriture entier" );
1832 }
[1359]1833 for (k=0; k<ligne.ldata_.size(); k++, ncol++)
1834 {
1835 fits_write_col(fptr_,TLONG,ncol+1,NoLine+1,1,1, &ligne.ldata_[k] ,&status);
1836 if( status ) printerror( status, "PutBinTabLine : erreur ecriture entier long" );
1837 }
1838 for (k=0; k<ligne.bdata_.size(); k++, ncol++)
1839 {
1840 fits_write_col(fptr_,TBYTE,ncol+1,NoLine+1,1,1, &ligne.bdata_[k] ,&status);
1841 if( status ) printerror( status, "PutBinTabLine : erreur ecriture byte" );
1842 }
[1209]1843
1844 for (k=0; k<ligne.cdata_.size(); k++, ncol++)
1845 {
[1220]1846 fits_write_col(fptr_,TSTRING,ncol+1,NoLine+1,1,1, (void*)ligne.cdata_[k].c_str() ,&status);
[1209]1847 if( status ) printerror( status, "PutBinTabLine : erreur ecriture caracteres" );
1848 }
[1193]1849}
1850
1851
[1218]1852/* \fn void SOPHYA::FitsOutFile::DVListIntoPrimaryHeader(DVList& dvl) const
1853
1854Put keywords from a DVList into the primary header of the fits-file
1855*/
[1246]1856void FitsOutFile::DVListIntoPrimaryHeader(DVList& dvl)
[1143]1857{
1858 int status = 0;
1859 int hdutype;
[1246]1860 if (hdunum_ == 0)
1861 {
1862 if (dvlToPrimary_ == NULL) dvlToPrimary_ = new DVList(dvl);
1863 else dvlToPrimary_->Merge(dvl);
1864 }
1865 else
1866 {
1867 fits_movabs_hdu(fptr_,1,&hdutype,&status);
1868 addKeywordsOfDVList(dvl);
1869 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1870 }
[1143]1871}
[839]1872
[1143]1873
[1246]1874void FitsOutFile::writeSignatureOnFits(int hdunum) const
[839]1875{
1876 int status = 0;
[1246]1877 int hdutype;
[839]1878 char keyname[LEN_KEYWORD];
1879 char strval[FLEN_VALUE];
1880 char comment[FLEN_COMMENT];
[1246]1881 if (hdunum_ == 0)
1882 {
1883 cerr << " WARNING : can't write keywords on non existing primary header" << endl;
1884 return;
1885 }
1886 fits_movabs_hdu(fptr_,1,&hdutype,&status);
1887 //
[971]1888 strncpy(keyname, "CREATOR", LEN_KEYWORD);
[1418]1889 keyname[7] = '\0';
[971]1890 strcpy(strval, "SOPHYA");
1891 strcpy(comment," SOPHYA Package - FITSIOServer ");
1892 fits_write_key(fptr_, TSTRING, keyname, &strval, comment, &status);
1893 if( status ) printerror( status );
[1143]1894 fits_write_date(fptr_, &status);
[971]1895 fits_write_comment(fptr_,"..............................................", &status);
1896 fits_write_comment(fptr_, " SOPHYA package - FITSIOSever ", &status);
1897 fits_write_comment(fptr_, " (C) LAL/IN2P3-CNRS Orsay, FRANCE 2000", &status);
1898 fits_write_comment(fptr_, " (C) DAPNIA/CEA Saclay, FRANCE 2000", &status);
1899 fits_write_comment(fptr_,"..............................................", &status);
[1045]1900 if( status ) printerror( status, "erreur writeSignatureOnFits" );
[1246]1901 //
1902 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
[839]1903}
1904
[903]1905
[1246]1906void FitsOutFile::addKeywordsOfDVList( DVList& dvl) const
[1143]1907{
1908 int status = 0;
1909 fits_write_comment(fptr_,"---------- keywords from SOPHYA ---------", &status);
1910 DVList::ValList::const_iterator it;
1911 for(it = dvl.Begin(); it != dvl.End(); it++)
1912 {
[1311]1913 MuTyV::MTVType keytype= (*it).second.elval.Type();
[1418]1914 char keyname[LEN_KEYWORD];
1915 strncpy(keyname,(*it).first.substr(0,64).c_str(),LEN_KEYWORD);
1916 int bout = ((*it).first.substr(0,64).length() < LEN_KEYWORD) ? (*it).first.substr(0,64).length() : LEN_KEYWORD-1;
1917 keyname[bout] = '\0';
1918 string key((*it).first.substr(0,64));
1919 // string key(keyname);
[1143]1920 char comment[FLEN_COMMENT];
1921 char strval[FLEN_VALUE]= "";
1922 char *comkey = "COMMENT";
[1353]1923 // fits_read_keyword(fptr_, keyname, strval, NULL, &status);
1924 // if (status != 0 || strncmp(keyname,comkey,LEN_KEYWORD-1) == 0 )
[1143]1925 {
[1183]1926 string coco = dvl.GetComment(key);
1927 coco.copy( comment, FLEN_COMMENT-1);
1928 int bout = (coco.length() < FLEN_COMMENT) ? coco.length() : FLEN_COMMENT-1;
1929 comment[bout]= '\0';
[1143]1930 status = 0;
1931 switch (keytype)
1932 {
[1311]1933 case MuTyV::MTVInteger :
[1143]1934 {
[1183]1935 int ival = (int)dvl.GetI(key);
1936 fits_write_key(fptr_,TINT,keyname,&ival, comment,&status);
[1143]1937 break;
1938 }
[1311]1939 case MuTyV::MTVFloat :
[1143]1940 {
[1183]1941 double dval= (double)dvl.GetD(key);
[1143]1942 fits_write_key(fptr_,TDOUBLE,keyname,&dval,comment,&status);
1943 break;
1944 }
[1311]1945 case MuTyV::MTVString :
[1143]1946 {
[1183]1947 char strvaleur[FLEN_VALUE]= "";
1948 string valChaine = dvl.GetS(key);
1949 valChaine.copy(strvaleur, FLEN_VALUE-1);
1950 int fin = (valChaine.length() < FLEN_VALUE) ? valChaine.length() : FLEN_VALUE-1;
1951 strvaleur[fin]= '\0';
1952
1953 fits_write_key(fptr_,TSTRING,keyname,&strvaleur,comment,&status);
[1143]1954 break;
1955 }
1956 }
1957 }
1958 if( status ) printerror( status,"fitsfile: probleme ecriture mot-cle du dvlist" );
1959 }
1960 fits_write_comment(fptr_,"--------------------------------------", &status);
1961}
[903]1962
1963
[1246]1964void FitsOutFile::addDVListOnPrimary()
1965 {
1966 int status = 0;
1967 int hdutype;
1968 if (hdunum_ == 0)
1969 {
1970 cerr << " WARNING : can't write keywords on non existing primary header" << endl;
1971 return;
1972 }
1973 if (dvlToPrimary_ != NULL)
1974 {
1975 fits_movabs_hdu(fptr_,1,&hdutype,&status);
1976 addKeywordsOfDVList(*dvlToPrimary_);
1977 delete dvlToPrimary_;
1978 dvlToPrimary_ = NULL;
1979 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1980 }
1981 }
[839]1982
[1353]1983
1984/*! \fn void FitsOutFile::appendInHeader(FitsInFile& infits, int hdunum)
1985
1986get a header from FitsInFile and append to the header beeing built
1987(shifting mandatory keywords)
1988*/
1989
1990void FitsOutFile::appendInputHeader(FitsInFile& infits, int hdunum)
1991{
[1771]1992
1993 infits.GetKeywordsFromHeader(hdunum, mots_cles_);
1994 /*
[1353]1995 int status = 0;
1996 int hdutype;
1997 fitsfile* fptr=infits.fitsfilePtr();
1998 fits_movabs_hdu(fptr,hdunum,&hdutype,&status);
1999 if( status ) fits_report_error(stderr,status);
2000
2001 // get number of keywords
2002 int nkeys,keypos;
2003 fits_get_hdrpos(fptr,&nkeys,&keypos,&status);
2004 if( status ) fits_report_error(stderr,status);
2005 // shift with the number of mandatory keywords
2006 int num= 0;
2007 // if primary header
2008 if (hdunum == 1)
2009 {
2010 // read NAXIS
2011 int naxis=0;
2012 fits_read_key(fptr,TINT,"NAXIS",&naxis,NULL,&status);
2013 // number of mandatory keywords
2014 num = naxis+3;
2015 }
2016 // extensions
2017 else
2018 {
2019 if (hdutype == IMAGE_HDU)
2020 {
2021 // read NAXIS
2022 int naxis=0;
2023 fits_read_key(fptr,TINT,"NAXIS",&naxis,NULL,&status);
2024 // number of mandatory keywords
2025 num = naxis+5;
2026 }
2027 else
2028 if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
2029 {
2030 // number of mandatory keywords
2031 num = 8;
2032 }
2033 }
2034 int j;
2035 char keyname[LEN_KEYWORD];
2036 char value[FLEN_VALUE];
2037 char comment[FLEN_COMMENT];
2038 for(j = num+1; j <= nkeys; j++)
2039 {
2040 char dtype;
2041 fits_read_keyn(fptr,j,keyname,value,comment,&status);
2042 if(status)
2043 {
2044 fits_report_error(stderr,status);
2045 status=0;
2046 }
2047 string kn(keyname);
2048 string cm(comment);
2049 string val(value);
2050 FitsKeyword kw(kn, val, cm);
2051 mots_cles_.push_back(kw);
2052 }
[1771]2053 */
[1353]2054}
2055void FitsOutFile::writeAppendedHeaderOnFits()
2056{
2057 for (list<FitsKeyword>::iterator it=mots_cles_.begin(); it !=mots_cles_.end(); it++)
2058 {
2059 (*it).writeOnFits(fptr_);
2060 }
2061 mots_cles_.clear();
2062}
2063
2064void FitsOutFile::insertKeywordOnHeader(string keyname, double value, string comment)
2065{
[1418]2066 char cvalue[16];
[1353]2067 sprintf(cvalue,"%e",value);
[1418]2068 FitsKeyword kw(keyname, string(cvalue), comment, 'F');
[1353]2069 mots_cles_.push_back(kw);
2070}
[1418]2071void FitsOutFile::insertKeywordOnHeader(string keyname, int value, string comment)
2072{
2073 char cvalue[16];
2074 sprintf(cvalue,"%d",value);
2075 FitsKeyword kw(keyname, string(cvalue), comment, 'I');
2076 mots_cles_.push_back(kw);
2077}
2078void FitsOutFile::insertKeywordOnHeader(string keyname, string value, string comment)
2079{
2080 FitsKeyword kw(keyname, value , comment, 'C');
2081 mots_cles_.push_back(kw);
2082}
[1353]2083
2084void FitsOutFile::insertCommentLineOnHeader(string comment)
2085{
2086 FitsKeyword kw(comment);
2087 mots_cles_.push_back(kw);
2088}
2089
2090void FitsOutFile::PrintHeaderToBeAppended()
2091{
2092 cout << " contenu du header en cours de fabrication " << endl;
2093 for (list<FitsKeyword>::iterator it=mots_cles_.begin(); it !=mots_cles_.end(); it++)
2094 {
2095 (*it).Print();
2096 }
2097}
2098
2099
2100FitsKeyword::FitsKeyword()
2101 {
2102 datatype_=' ';
2103 keyname_ = string("");
2104 dvalue_=0.;
2105 ivalue_=1;
2106 svalue_=string("");
2107 comment_=string("");
2108 }
2109
2110FitsKeyword::FitsKeyword(string comment)
2111 {
2112 datatype_=' ';
2113 keyname_=string("COMMENT");
2114 comment_=comment;
[1418]2115 }
[1353]2116
2117FitsKeyword::FitsKeyword(string keyname, string value, string comment) : keyname_(keyname), comment_(comment)
2118 {
2119 int status=0;
2120 char dtype;
2121 const char* val= value.c_str();
2122 char* valk = const_cast<char*>(val);
2123 fits_get_keytype(valk,&dtype,&status);
2124 if(status)
2125 {
2126 status=0;
[1354]2127 if (status == VALUE_UNDEFINED) cout << "WARNING (FitsKeyword) : undefined keyword value " << endl;
[1353]2128 datatype_=' ';
2129 }
2130 else datatype_=dtype;
2131
2132 switch( datatype_ )
2133 {
2134 case 'C':
2135 {
[1354]2136 strip(valk, 'B','\'');
2137 svalue_ = string(valk);
[1353]2138 break;
2139 }
2140 case 'I':
2141 {
2142 ivalue_ = atoi(val);
2143 break;
2144 }
2145 case 'L':
2146 {
[1354]2147 bool bb = value.c_str();
2148 ivalue_ = (int)bb;
[1353]2149 break;
2150 }
2151 case 'F':
2152 {
2153 dvalue_ = atof(val);
2154 break;
2155 }
2156 case 'X':
2157 {
2158 throw IOExc("FitsKeyword , complex keyword value not supported");
2159 }
2160 }
2161 }
2162
[1418]2163// constructeur pour les mots-cles maison (ne prvenant pas de la lecture d'un fichier fits)
2164FitsKeyword::FitsKeyword(string keyname, string value, string comment, char type) : keyname_(keyname), comment_(comment), datatype_(type)
2165 {
2166 char dtype;
2167 const char* val= value.c_str();
2168 char* valk = const_cast<char*>(val);
2169 switch( datatype_ )
2170 {
2171 case 'C':
2172 {
2173 strip(valk, 'B','\'');
2174 svalue_ = string(valk);
2175 break;
2176 }
2177 case 'I':
2178 {
2179 ivalue_ = atoi(val);
2180 break;
2181 }
2182 case 'L':
2183 {
2184 bool bb = value.c_str();
2185 ivalue_ = (int)bb;
2186 break;
2187 }
2188 case 'F':
2189 {
2190 dvalue_ = atof(val);
2191 break;
2192 }
2193 case 'X':
2194 {
2195 throw IOExc("FitsKeyword , complex keyword value not supported");
2196 }
2197 }
2198 }
2199
[1353]2200void FitsKeyword::writeOnFits(fitsfile* ptr)
2201 {
2202 int status=0;
[1418]2203 char keyname[LEN_KEYWORD];
2204 char comment[FLEN_COMMENT];
2205 keyname_.copy(keyname, LEN_KEYWORD);
2206 int bout = (keyname_.length() < LEN_KEYWORD) ? keyname_.length() : LEN_KEYWORD-1;
2207 keyname[bout] = '\0';
2208 comment_.copy( comment, FLEN_COMMENT);
2209 bout = (comment_.length() < FLEN_COMMENT) ? comment_.length() : FLEN_COMMENT-1;
2210 comment[bout]= '\0';
2211
[1353]2212 int nkeys,keypos;
2213 fits_get_hdrpos(ptr,&nkeys,&keypos,&status);
2214 switch( datatype_ )
2215 {
2216 case 'C':
2217 {
[1418]2218 char value[FLEN_VALUE]="";
2219 svalue_.copy(value, FLEN_VALUE-1);
2220 int fin = (svalue_.length() < FLEN_VALUE) ? svalue_.length() : FLEN_VALUE-1;
2221 value[fin]= '\0';
[1353]2222 fits_write_key(ptr,TSTRING,keyname,&value, comment,&status);
2223 fits_report_error(stderr,status);
2224 break;
2225 }
2226 case 'I':
2227 {
2228 fits_write_key(ptr,TINT,keyname,&ivalue_, comment,&status);
2229 fits_report_error(stderr,status);
2230 break;
2231 }
2232 case 'L':
2233 {
2234 fits_write_key(ptr,TLOGICAL,keyname,&ivalue_, comment,&status);
2235 fits_report_error(stderr,status);
2236 break;
2237 }
2238 case 'F':
2239 {
2240 fits_write_key(ptr,TDOUBLE,keyname,&dvalue_, comment,&status);
2241 fits_report_error(stderr,status);
2242 break;
2243 }
2244 case 'X':
2245 {
2246 cout << "FitsKeyword : complex keyword value not supported" << endl;;
2247 }
2248 default :
2249 {
2250 char *comkey = "COMMENT";
2251 if(strncmp(keyname,comkey,LEN_KEYWORD-1) == 0)
2252 {
2253 fits_write_comment(ptr,comment,&status);
2254 fits_report_error(stderr,status);
2255 }
2256 else
2257 {
2258 cout << " WARNING (FitsKeyword::writeOnFits) : unrecognized keyword : " << keyname_ << endl;
2259 }
2260 }
2261 }
2262 }
2263
2264void FitsKeyword::Print()
2265 {
2266 switch( datatype_ )
2267 {
2268 case 'C':
2269 {
2270 cout << " mot cle : " << keyname_ << " valeur : " << svalue_ << " commentaire : " << comment_ <<endl;
2271 break;
2272 }
2273 case 'I':
2274 {
2275 cout << " mot cle : " << keyname_ << " valeur : " << ivalue_ << " commentaire : " << comment_ <<endl;
2276 break;
2277 }
2278 case 'L':
2279 {
2280 cout << " mot cle : " << keyname_ << " valeur : " << ivalue_ << " commentaire : " << comment_ <<endl;
2281 break;
2282 }
2283 case 'F':
2284 {
2285 cout << " mot cle : " << keyname_ << " valeur : " << dvalue_ << " commentaire : " << comment_ <<endl;
2286 break;
2287 }
2288 case 'X':
2289 {
2290 cout << "FitsKeyword : complex keyword value not supported" << endl;;
2291 }
2292 default :
2293 {
2294 cout << " mot cle : " << keyname_ << " commentaire : " << comment_ <<endl;
2295 }
2296 }
2297 }
Note: See TracBrowser for help on using the repository browser.