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

Last change on this file since 2130 was 2129, checked in by lemeur, 23 years ago

rajout d'un break disparu intempestivement

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