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

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

revu l'ouverture des fichiers

File size: 33.3 KB
RevLine 
[949]1#include "machdefs.h"
2#include <stdlib.h>
[839]3#include "fitsfile.h"
4#include "pexceptions.h"
5#include "strutil.h"
[903]6#include "anydataobj.h"
7#include "fitsspherehealpix.h"
[1136]8
9
10void FitsIOHandler::Read(char flnm[],int hdunum)
[839]11{
[1136]12 FitsInFile ifts(flnm);
13 Read(ifts, hdunum);
[839]14}
[1136]15void FitsIOHandler::Read(FitsInFile& is, int hdunum)
16{
[1175]17 if (hdunum == 0) is.moveToFollowingHeader();
18 else is.ReadFInit(hdunum);
[1136]19 ReadFromFits(is);
20}
21
22
[1183]23void FitsIOHandler::Write(char flnm[], string WriteMode)
[1136]24
25{
26
[1183]27 FitsOutFile of(flnm, WriteMode);
[1136]28 Write(of);
29}
30
31void FitsIOHandler::Write(FitsOutFile& os)
32{
33 WriteToFits(os);
34}
35
36
37
[839]38
39FitsFile::~FitsFile()
40{
41 int status = 0;
[1175]42 if( fptr_ != NULL)
[903]43 {
44 fits_close_file(fptr_,&status);
[1175]45 // je ne fais pas delete fptr_, c'est la lib. fitsio qui a fait
46 // new...
[903]47 }
[1175]48 if( status ) printerror( status );
[839]49}
[903]50
[1136]51
52void FitsFile::printerror(int &status)
53 //*****************************************************/
54 //* Print out cfitsio error messages and exit program */
55 //*****************************************************/
[1045]56{
[1136]57 if( status )
58 {
59 fits_report_error(stderr,status);
60 throw IOExc("FitsFile:: error FITSIO status");
61 }
62 return;
63}
[903]64
[1136]65void FitsFile::printerror(int& status, char* texte)
66 //*****************************************************/
67 //* Print out cfitsio error messages and exit program */
68 //*****************************************************/
69{
70 // print out cfitsio error messages and exit program
71 // print error report
72 fits_report_error(stderr, status);
73 cout << " erreur:: " << texte << endl;
74 throw IOExc("FitsFile:: error FITSIO status");
75}
76
77void FitsFile::ResetStatus(int& status)
78{
79 fits_status_ = status;
80 status = 0;
81}
82
83string FitsFile::getErrStatus(int status)
84{
85 char text[31];
86 fits_get_errstatus(status, text);
87 return string(text);
88}
89
90FitsInFile::FitsInFile()
91{
92 InitNull();
93}
94//FitsInFile::FitsInFile(char flnm[], int hdunum)
95FitsInFile::FitsInFile(char flnm[])
96{
[1175]97 InitNull();
98 int status = 0;
99 fits_open_file(&fptr_,flnm,READONLY,&status);
100 if( status ) printerror( status );
[1136]101}
102
103
104void FitsInFile::InitNull()
105{
[1175]106
[1045]107 bitpix_ = 0;
108 naxis_ = 0;
109 nbData_ = 0;
110 nrows_ = 0;
111 nbcols_ = 0;
112 naxisn_.clear();
113 repeat_.clear();
114 noms_.clear();
115 taille_des_chaines_.clear();
116 dvl_.Clear();
[1175]117
118
[1045]119}
120
121
[1136]122int FitsInFile::NbBlocks(char flnm[])
[903]123{
124 int status = 0;
125 int nbhdu = 0;
126 fitsfile* fileptr;
127 fits_open_file(&fileptr,flnm,READONLY,&status);
128 if( status ) printerror( status, "NbBlocks: erreur ouverture fichier" );
129 fits_get_num_hdus(fileptr, &nbhdu, &status);
130 fits_close_file(fileptr,&status);
131 return nbhdu;
132}
133
[1136]134void FitsInFile::getBlockType(char flnm[], int hdunum, string& typeOfExtension, int& naxis, vector<int>& naxisn, string& dataType, DVList& dvl )
[903]135{
136 int status = 0;
137 fitsfile* fileptr;
138 fits_open_file(&fileptr,flnm,READONLY,&status);
139 if( status ) printerror( status, "getBlockType: erreur ouverture fichier" );
140 // move to the specified HDU number
141 int hdutype = 0;
142 fits_movabs_hdu(fileptr,hdunum,&hdutype,&status);
143 if( status ) printerror( status,"getBlockType: erreur movabs");
144 if(hdutype == IMAGE_HDU)
145 {
146 typeOfExtension = "IMAGE";
147 int bitpix;
148 GetImageParameters (fileptr, bitpix, naxis, naxisn);
149 if(bitpix == DOUBLE_IMG) dataType = "double";
150 else
151 if(bitpix == FLOAT_IMG) dataType = "float";
152 else
153 if(bitpix == LONG_IMG || bitpix == SHORT_IMG ) dataType = "int";
154 else
155 {
156 cout << " bitpix= " << bitpix << endl;
157 throw PException(" FitsFile::getBlockType : unsupprted FITS data type");
158 }
159
160 }
161 else
162 if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
163 {
164 int nrows = 0;
165 vector<string> noms;
166 vector<char> types;
167 vector<int> taille_des_chaines;
[971]168 GetBinTabParameters(fileptr, naxis, nrows, naxisn, noms, types, taille_des_chaines);
169 int k;
170 for (k=0; k< naxisn.size(); k++) naxisn[k] *= nrows;
[903]171 if(hdutype == ASCII_TBL)
172 {
173 typeOfExtension = "ASCII_TBL";
174 dataType = "ASCII";
175 }
176 else
177 {
178 typeOfExtension = "BINARY_TBL";
179 if(types[0] == 'D') dataType = "double";
180 else
181 if(types[0] == 'E') dataType = "float";
182 else
183 if(types[0] == 'I' ) dataType = "int";
184 else
185 if(types[0] == 'S' ) dataType = "char*";
186 else
187 {
188 cout << " types[0]= " << types[0] << endl;
189 throw PException(" FitsFile::getBlockType : unsupprted FITS data type");
190 }
191 }
192 }
193 else
194 {
195 cout << " hdutype= " << hdutype << endl;
196 throw IOExc("FitsFile::getBlockType: this HDU type is unknown");
197 }
198
[971]199 KeywordsIntoDVList(fileptr, dvl, hdunum);
[903]200 fits_close_file(fileptr,&status);
201}
202
[1136]203void FitsInFile::GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn)
[839]204{
[1136]205 int hdunum=0;
206 cout << " Reading a FITS image in HDU : " << fits_get_hdu_num(fileptr,&hdunum) << endl;
207 int status= 0;
208
209 // bits per pixels
210 fits_read_key(fileptr,TINT,"BITPIX",&bitpix,NULL,&status);
211 if( status ) printerror( status );
212
213 // number of dimensions in the FITS array
214 naxis= 0;
215 fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
216 if( status ) printerror( status );
217 // read the NAXISn keywords to get image size
218 long* naxes = new long[naxis] ;
219 int nfound;
220 fits_read_keys_lng(fileptr,"NAXIS",1,naxis,naxes,&nfound,&status);
221 if( status ) printerror( status );
222 if (nfound != naxis )
223 cout << " WARNING : " << nfound << " axes found, expected naxis= " << naxis << endl;
224 int k;
225 for (k=0; k<naxis; k++)
226 {
227 naxisn.push_back( (int)naxes[k] );
228 }
229 delete [] naxes;
[971]230}
[839]231
[971]232
[1136]233void FitsInFile::ReadFInit(int hdunum)
[1045]234{
[1143]235 InitNull();
[1136]236 // int status = 0;
237
238 // fits_open_file(&fptr_,flnm,READONLY,&status);
239 // if( status ) printerror( status );
240
[1045]241 int status = 0;
[1136]242
[1045]243 if (hdunum <= 1)
244 {
245 hdunum_ = 1;
246 // presence of image ?
247 int naxis= 0;
248 fits_read_key(fptr_,TINT,"NAXIS",&naxis,NULL,&status);
249 if( status ) printerror( status );
250 if (naxis > 0 ) // there is an image
251 {
252 hdutype_ = IMAGE_HDU;
253 GetImageParameters (fptr_, bitpix_, naxis_, naxisn_);
254 nbData_ = 1;
255 int k;
256 for (k=0; k<naxis_; k++) if (naxisn_[k] > 0) nbData_ *= naxisn_[k];
257 KeywordsIntoDVList(fptr_, dvl_,hdunum_);
258 }
259 else
260 {
261 throw PException(" first header : no image, probably error in hdunum");
262 }
263 //
264 }
265 else
266 {
267 hdunum_ = hdunum-1;
268 int hdutype;
269 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
270 if( status ) printerror( status,":FitsFile::ReadF : erreur movabs");
271 moveToFollowingHeader();
272 }
273}
274
[1143]275DVList FitsInFile::DVListFromPrimaryHeader() const
276 {
277 int status;
278 DVList dvl;
279 KeywordsIntoDVList(fptr_, dvl, 1);
280 int hdutype = 0;
281 if (hdunum_ > 0) fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
282 return dvl;
283 }
[1136]284
285
286void FitsInFile::moveToFollowingHeader()
[971]287{
288 int status = 0;
[839]289 int hdutype;
[1143]290 InitNull();
[971]291 fits_movrel_hdu(fptr_, 1,&hdutype,&status);
292 if( status ) printerror( status," lecture du header suivant" );
293 hdunum_++;
[839]294 hdutype_= hdutype;
295 if(hdutype_ == IMAGE_HDU)
296 {
[903]297 GetImageParameters (fptr_, bitpix_, naxis_, naxisn_);
298 nbData_ = 1;
[971]299 int k;
300 for (k=0; k<naxis_; k++) if (naxisn_[k] > 0) nbData_ *= naxisn_[k];
301 KeywordsIntoDVList(fptr_, dvl_,hdunum_);
[839]302 }
303 if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
304 {
[903]305 GetBinTabParameters(fptr_,nbcols_, nrows_,repeat_, noms_, types_, taille_des_chaines_);
[971]306 KeywordsIntoDVList(fptr_, dvl_, hdunum_);
[839]307 }
[971]308}
[839]309
[1136]310
311int FitsInFile::NbColsFromFits() const
[839]312{
[1136]313 if(hdutype_ == BINARY_TBL) return nbcols_;
[971]314 else
[1136]315 if(hdutype_ == ASCII_TBL || hdutype_ == IMAGE_HDU) return 1;
[839]316 else
317 {
[1136]318 cout << " hdutype= " << hdutype_ << endl;
319 throw PException("FitsFile::NbColsFromFits, this HDU is unknown");
[839]320 }
321}
322
[1136]323int FitsInFile::NentriesFromFits(int nocol) const
[839]324{
[1136]325 if(hdutype_ == BINARY_TBL ) return nrows_*repeat_[nocol];
326 else
327 if(hdutype_ == ASCII_TBL) return nrows_;
328 else
329 if(hdutype_ == IMAGE_HDU) return nbData_;
330 else
[839]331 {
[1136]332 cout << "hdutype= " << hdutype_ << endl;
333 throw PException("FitsFile::NentriesFromFits, this HDU is unknown");
[839]334 }
335}
336
[1136]337char FitsInFile::ColTypeFromFits(int nocol) const
[839]338{
[1136]339 if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
[839]340 {
[1136]341 throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
[839]342 }
[1136]343 return types_[nocol];
[839]344}
[1136]345string FitsInFile::ColNameFromFits(int nocol) const
[839]346{
[1136]347 if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
[1045]348 {
[1136]349 throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
[1045]350 }
[1136]351 return noms_[nocol];
[839]352}
353
[1136]354int FitsInFile::ColStringLengthFromFits(int nocol) const
[839]355{
[1136]356 if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
357 {
358 throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
359 }
360 int index=-1;
361 int k;
362 for (k=0; k<=nocol; k++)
363 {
364 if (types_[k] == 'S') index++;
365 }
366 return taille_des_chaines_[index];
[839]367}
[1136]368void FitsInFile::GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char ** cdata)
[839]369{
370 int status= 0;
[1136]371 int anull;
372 double dnull= 0.;
373 float fnull= 0.;
374 int inull= 0;
375 char* cnull= "";
376 int dcount = 0.;
377 int fcount = 0.;
378 int icount = 0;
379 int ccount =0;
380 int ncol;
381 long nels=1;
382 for (ncol=0; ncol<nbcols_; ncol++)
[861]383 {
[1136]384 switch (types_[ncol])
385 {
386 case 'D' :
387 fits_read_col(fptr_,TDOUBLE,ncol+1,NoLine+1,1,1,&dnull,&ddata[dcount++],&anull,&status);
388 break;
389 case 'E' :
390 fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[fcount++],&anull,&status);
391 break;
392 case 'I' :
393 fits_read_col(fptr_,TINT,ncol+1,NoLine+1,1,1,&inull,&idata[icount++],
394 &anull,&status);
395 break;
396 case 'S' :
397 fits_read_col(fptr_,TSTRING,ncol+1,NoLine+1,1,1,cnull,&cdata[ccount++],&anull,&status);
398 break;
399 }
400 if (status)
401 {
402 ResetStatus(status);
403 break;
404 }
[861]405 }
[903]406}
[839]407
[1136]408void FitsInFile::GetBinTabLine(int NoLine, float* fdata)
[903]409{
[1136]410 int status= 0;
411 int anull;
412 float fnull= 0.;
413 long nels=1;
414 int ncol;
415 for (ncol=0; ncol<nbcols_; ncol++)
[861]416 {
[1136]417 fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[ncol],&anull,&status);
418 if (status)
[903]419 {
[1136]420 ResetStatus(status);
421 break;
[903]422 }
[1136]423 }
424}
[839]425
[903]426
[1136]427void FitsInFile::GetBinTabFCol(double* valeurs,int nentries, int NoCol) const
[839]428 {
429 int status= 0;
430 int DTYPE;
431 long repeat,width;
432 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
433 if( DTYPE != TDOUBLE)
434 {
[1045]435 if (DTYPE == TFLOAT) cout << " WARNING: reading double from float : conversion will be made by fitsio library" << endl;
436 else
437 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non double");
[839]438 }
439 long nels=nentries;
[971]440 int anull;
[839]441 // no checking for undefined pixels
[971]442 double dnull= 0.;
443 // fits_read_key(fptr_,TDOUBLE,"BAD_DATA",&dnull,NULL,&status);
444 // if (status != 0)
445 // {
446 // dnull = -1.6375e30; // default value
447 // status = 0;
448 // }
449 if (nentries != nrows_*repeat)
450 {
451 cout << " found " << nentries << " pixels, expected: " << nrows_*repeat << endl;
452 throw PException(" FitsFile:::GetBinTabFCol ");
453 }
[839]454 fits_read_col(fptr_,TDOUBLE,NoCol+1,1,1,nels,&dnull,valeurs,
455 &anull,&status);
456 if( status ) printerror( status,"erreur lecture de colonne" );
[971]457
[839]458 }
459
[1136]460void FitsInFile::GetBinTabFCol(float* valeurs,int nentries, int NoCol) const
[839]461 {
462 int status= 0;
463 int DTYPE;
464 long repeat,width;
465 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
466 if( DTYPE != TFLOAT)
467 {
[1045]468 if (DTYPE == TDOUBLE) cout << " WARNING: reading float from double : conversion will be made by fitsio library" << endl;
469 else
470 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
[839]471 }
472 long nels=nentries;
[971]473 int anull;
[839]474 // no checking for undefined pixels
475 float fnull= 0.;
[971]476 // fits_read_key(fptr_,TFLOAT,"BAD_DATA",&fnull,NULL,&status);
477 // if (status != 0)
478 // {
479 // fnull = -1.6375e30; // default value
480 // status = 0;
481 // }
482 if (nentries != nrows_*repeat)
483 {
484 cout << " found " << nentries << " pixels, expected: " << nrows_*repeat << endl;
485 throw PException(" FitsFile:::GetBinTabFCol ");
486 }
[839]487 fits_read_col(fptr_,TFLOAT,NoCol+1,1,1,nels,&fnull,valeurs,
488 &anull,&status);
489 if( status ) printerror( status,"erreur lecture de colonne" );
490 }
[1136]491
492void FitsInFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
[839]493 {
494 int status= 0;
495 int DTYPE;
496 long repeat,width;
497 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
498 if( DTYPE != TLONG && DTYPE != TINT && DTYPE != TSHORT )
499 {
500 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non entier");
501 }
502 long nels=nentries;
503 // no checking for undefined pixels
504 int anull;
505 int inull= 0;
[971]506 // fits_read_key(fptr_,TINT,"BAD_DATA",&inull,NULL,&status);
507 // if (status != 0)
508 // {
509 // inull = -999999; // default value
510 // status = 0;
511 // }
512 if (nentries != nrows_*repeat)
513 {
514 cout << " found " << nentries << " pixels, expected: " << nrows_*repeat << endl;
515 throw PException(" FitsFile:::GetBinTabFCol ");
516 }
[839]517 fits_read_col(fptr_,TINT,NoCol+1,1,1,nels,&inull,valeurs,
518 &anull,&status);
519 if( status ) printerror( status,"erreur lecture de colonne" );
520 }
[1136]521
522void FitsInFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
[839]523 {
524 int status= 0;
525 int DTYPE;
526 long repeat,width;
527 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
528 if( DTYPE != TSTRING )
529 {
530 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
531 }
532 long nels=nentries;
533 // no checking for undefined pixels
534 int anull;
[971]535 char* cnull= "";
536 if (nentries != nrows_*repeat/width)
537 {
538 cout << " found " << nentries << " pixels, expected: " << nrows_*repeat/width << endl;
539 throw PException(" FitsFile:::GetBinTabFCol ");
540 }
[839]541 long frow=1;
542 long felem=1;
543 fits_read_col(fptr_,TSTRING,NoCol+1,frow,felem,nels,cnull,valeurs,
544 &anull,&status);
545 if( status ) printerror( status,"erreur lecture de colonne" );
546 }
[1045]547
[1136]548void FitsInFile::GetSingleColumn(double* map, int nentries) const
549{
550 int status = 0;
551 if(hdutype_ == IMAGE_HDU)
[1045]552 {
[1136]553
554 if(bitpix_ != DOUBLE_IMG)
[1047]555 {
[1136]556 cout << " The data type on fits file is not double...";
557 cout << " Conversion to double achieved by cfitsio lib" << endl;
[1047]558 }
[1136]559
560 // no checking for undefined pixels
561 int anull;
562 double dnull= 0.;
563
564 long nels= nentries;
565 fits_read_img(fptr_,TDOUBLE,1,nels,&dnull,map,&anull,&status);
566 if( status ) printerror( status );
[1045]567 }
[1136]568 else
569 if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
570 {
571 GetBinTabFCol(map,nentries, 0);
572 }
573 else
574 {
575 cout << " hdutype= " << hdutype_ << endl;
576 throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
577 }
[1045]578}
579
[1136]580void FitsInFile::GetSingleColumn(float* map, int nentries) const
[1045]581{
[1136]582 int status = 0;
583 if(hdutype_ == IMAGE_HDU)
[1045]584 {
[1136]585 if(bitpix_ != FLOAT_IMG)
[1047]586 {
[1136]587 cout << " The data type on fits file is not float ";
588 cout << " Conversion to float achieved by cfitsio lib" << endl;
[1047]589 }
[1136]590 // no checking for undefined pixels
591 int anull;
592 float fnull= 0.;
593
594 long nels= nentries;
595 fits_read_img(fptr_,TFLOAT,1,nels,&fnull, map,&anull,&status);
596 if( status ) printerror( status );
[1045]597 }
[839]598 else
[1136]599 if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
600 {
601 GetBinTabFCol(map,nentries, 0);
602 }
[839]603 else
604 {
[1136]605 cout << " hdutype= " << hdutype_ << endl;
606 throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
[839]607 }
608}
609
[1136]610void FitsInFile::GetSingleColumn( int* map, int nentries) const
[839]611{
[1136]612 int status = 0;
613 if(hdutype_ == IMAGE_HDU)
[839]614 {
[1136]615 if(bitpix_ != LONG_IMG)
616 {
617 cout << " The data type on fits file is not int ";
618 cout << " Conversion to float achieved by cfitsio lib" << endl;
619 }
620 // no checking for undefined pixels
621 int anull;
622 float fnull= 0.;
623
624 long nels= nentries;
625 fits_read_img(fptr_,TINT,1,nels,&fnull,map,&anull,&status);
626 if( status ) printerror( status );
[839]627 }
628 else
[1136]629 if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
630 {
631 GetBinTabFCol(map,nentries, 0);
632 }
[839]633 else
[1136]634 {
635 cout << " hdutype= " << hdutype_ << endl;
636 throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
637 }
[839]638}
639
[1136]640void FitsInFile::GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
[903]641 vector<int>& repeat,
642 vector<string>& noms,
643 vector<char>& types,
644 vector<int>& taille_des_chaines)
[839]645{
646 int status= 0;
[903]647 int hdunum=0;
648 int hdutype=0;
649 fits_get_hdu_num(fileptr,&hdunum);
650 fits_get_hdu_type(fileptr, &hdutype, &status);
651
652 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
[839]653 {
[903]654 throw IOExc("FitsFile::GetBinTabParameters this HDU is not an ASCII table nor a binary table");
[839]655 }
[903]656 if(hdutype == ASCII_TBL)
657 cout << " Reading a FITS ascii table in HDU : " << hdunum << endl;
658 if(hdutype == BINARY_TBL)
659 cout << " Reading a FITS binary table in HDU : " << hdunum << endl;
[839]660
661 // get the number of columns
[903]662 fits_get_num_cols(fileptr, &nbcols,&status);
[839]663 if( status ) printerror( status );
664
665 // get the number of rows
666 long naxis2= 0;
[903]667 fits_get_num_rows(fileptr,&naxis2,&status);
[839]668 if( status ) printerror( status );
[903]669 nrows = (int)naxis2;
[839]670
671 // get the datatype, names and the repeat count
[903]672 noms.clear();
673 noms.reserve(nbcols);
674 types.clear();
675 types.reserve(nbcols);
676 repeat.clear();
677 repeat.reserve(nbcols);
678 taille_des_chaines.clear();
[839]679 char **ttype = new char*[nbcols];
[923]680 int ii;
[1175]681 //
682 //
[923]683 for (ii=0; ii < nbcols; ii++) ttype[ii]=new char[FLEN_VALUE];
[839]684 int nfound;
[903]685 fits_read_keys_str(fileptr, "TTYPE",1,nbcols,ttype,&nfound, &status);
[839]686 if( status ) printerror( status,"erreur lecture des noms de colonne");
687 int rept=0;
[923]688 for(ii = 0; ii < nbcols; ii++)
[839]689 {
690 int DTYPE;
691 long width;
[903]692 long repete = 0;
693 fits_get_coltype(fileptr,ii+1,&DTYPE,&repete,&width,&status);
[839]694 if( status ) printerror( status,"erreur lecture type de colonne");
[903]695 rept = repete;
696 noms.push_back(string(ttype[ii]));
[839]697 switch (DTYPE)
698 {
699 case TDOUBLE :
[903]700 types.push_back('D');
[839]701 break;
702 case TFLOAT :
[903]703 types.push_back('E');
[839]704 break;
705 case TLONG :
[903]706 types.push_back('I');
[839]707 break;
708 case TINT :
[903]709 types.push_back('I');
[839]710 break;
711 case TSHORT :
[903]712 types.push_back('I');
[839]713 break;
714 case TSTRING :
[903]715 types.push_back('S');
716 taille_des_chaines.push_back(width);
[839]717 rept/=width;
718 break;
719 default :
720 cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
721 throw IOExc("FitsFile:: unknown type of field");
722 }
[903]723 repeat.push_back(rept);
[1136]724 }
725 for (ii=0; ii < nbcols; ii++) delete [] ttype[ii];
726 delete [] ttype;
727}
728
729void FitsInFile::KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum)
730{
731 int status = 0;
732 int hdutype;
733 fits_movabs_hdu(fileptr,hdunum,&hdutype,&status);
734 if( status ) printerror( status,":KeywordsIntoDVList : erreur movabs");
735 // get number of keywords
736 int nkeys,keypos;
737 fits_get_hdrpos(fileptr,&nkeys,&keypos,&status);
738 if( status ) printerror( status );
739
740 // put keywords in a DVList object
741 char keyname[LEN_KEYWORD]= "";
742 char strval[FLEN_VALUE]= "";
743 char dtype;
744 char card[FLEN_CARD];
745 char *comkey = "COMMENT";
[1143]746 char comment[FLEN_COMMENT];
[1136]747
748 // shift with the number of mandatory keywords
[1143]749 // int num= 8;
750 int num= 0;
751 // primary header
752 if (hdunum == 1)
753 {
754 // read NAXIS
755 int naxis=0;
756 fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
757 // number of mandatory keywords
758 num = naxis+3;
759 }
760 // extensions
761 else
762 {
763 if (hdutype == IMAGE_HDU)
764 {
765 // read NAXIS
766 int naxis=0;
767 fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
768 // number of mandatory keywords
769 num = naxis+5;
770 }
771 else
772 if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
773 {
774 // number of mandatory keywords
775 num = 8;
776 }
777 }
[1136]778 int j;
779 for(j = num+1; j <= nkeys; j++)
780 {
781 fits_read_keyn(fileptr,j,card,strval,NULL,&status);
782 if(status) printerror(status);
783
784 strncpy(keyname,card,LEN_KEYWORD-1);
785 if(strncmp(keyname,comkey,LEN_KEYWORD-1) != 0 && strlen(keyname) != 0
786 && strlen(strval) != 0)
787 {
788 fits_get_keytype(strval,&dtype,&status);
789 if(status) printerror(status);
790
791 strip(keyname, 'B',' ');
792 strip(strval, 'B',' ');
793 strip(strval, 'B','\'');
794
795 switch( dtype )
796 {
797 case 'C':
[1143]798 fits_read_key(fileptr,TSTRING,keyname,strval,comment,&status);
799 dvl[keyname]= strval;
800 dvl.SetComment(keyname, comment);
[1136]801 break;
802 case 'I':
803 int ival;
[1143]804 fits_read_key(fileptr,TINT,keyname,&ival,comment,&status);
[1136]805 dvl[keyname]= (int_4) ival; // Portage mac DY
[1143]806 dvl.SetComment(keyname, comment);
[1136]807 break;
808 case 'L':
809 int ilog;
[1143]810 fits_read_key(fileptr,TLOGICAL,keyname,&ilog,comment,&status);
[1136]811 dvl[keyname]= (int_4) ilog;
[1143]812 dvl.SetComment(keyname, comment);
[1136]813 break;
814 case 'F':
815 double dval;
[1143]816 fits_read_key(fileptr,TDOUBLE,keyname,&dval,comment,&status);
[1136]817 dvl[keyname]= dval;
[1143]818 dvl.SetComment(keyname, comment);
[1136]819 break;
820 }
821
822 }
[839]823 }
[1136]824 // dvl_.Print();
825}
826
827FitsOutFile::FitsOutFile()
828{
829 InitNull();
[903]830}
[839]831
[1183]832FitsOutFile::FitsOutFile(char flnm[], string WriteMode)
[1136]833{
[839]834
[1136]835 InitNull();
836 int status = 0;
[839]837
[1136]838 // create new FITS file
[1183]839
840 fits_create_file(&fptr_,flnm,&status);
841 if( status )
[1136]842 {
[1183]843 // si on veut ecrire a la fin de ce fichier
844 if (WriteMode == string("append"))
845 {
846 status = 0;
847 fits_open_file(&fptr_,flnm,READWRITE,&status);
848 if( status )
849 {
850 cout << " error opening file: " << flnm << endl;
851 printerror(status, "failure opening a file supposed to exist");
852 }
853 else cout << " file " << flnm << " opened, new objects will be appended " << endl;
854 fits_get_num_hdus(fptr_, &hdunum_, &status);
855 int hdutype;
856 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
857 if( status ) printerror( status,":FitsFile::WriteF : erreur movabs");
858 }
859 else
860 if (WriteMode == string("clear"))
861 {
862 status = 0;
863 char* newname = new char[strlen(flnm)+1];
864 //
865 newname[0] = '!';
866 newname[1] = '\0';
867 strcat(newname, flnm);
868 fits_create_file(&fptr_,newname,&status);
869 if (status)
870 {
871 cout << " error opening file: " << flnm << endl;
872 printerror(status, "unable to open file, supposed to exist");
873 }
874 else cout << " file " << flnm << " will be overwrited " << endl;
875
876 }
877 else
878 if (WriteMode == string("unknown")) printerror(status, " file seems already to exist");
[1136]879
[1183]880 else printerror(status, "open file failed");
881
[1136]882 }
883}
884
885
886
[1143]887void FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList &dvl)
[1136]888{
889 int status = 0;
890 long naxis = nbdim;
891 long* naxes = new long[nbdim];
892 if (hdunum_ == 1)
893 {
[1143]894 if (imageOnPrimary_ == false)
895 {
896 fits_create_img(fptr_,FLOAT_IMG,0,naxes,&status);
897 writeSignatureOnFits(); }
[1136]898 else hdunum_--;
899 }
900 int k;
901 for (k=0; k< nbdim; k++) naxes[k] = (long)naxisn[k];
902 if (type == 'D')
903 fits_create_img(fptr_,DOUBLE_IMG,naxis,naxes,&status);
904 else
905 if (type == 'E')
906 fits_create_img(fptr_,FLOAT_IMG,naxis,naxes,&status);
907 else
908 if (type == 'I')
909 fits_create_img(fptr_,LONG_IMG,naxis,naxes,&status);
910 else
911 {
912 cout << " type of data: " << type << endl;
913 throw PException("FitsFile:::makeHeaderImageOnFits:unprogrammed type of data ");
914 }
915
916 hdunum_++;
[1143]917
918 // write supplementary keywords
919 // dvl.Print();
920 addKeywordsOfDVList(dvl);
921
[1136]922 delete [] naxes;
923 if( status ) printerror( status, "erreur creation HDU IMAGE" );
924
925}
926void FitsOutFile::putImageToFits(int nbData, double* map) const
927{
928 int status = 0;
929 long npix= nbData;
930 fits_write_img(fptr_,TDOUBLE,1,npix,map,&status);
931 if( status ) printerror( status, "erreur ecriture putImageToFits" );
[1143]932 // writeSignatureOnFits();
[1136]933}
934
935void FitsOutFile::putImageToFits(int nbData, float* map) const
936{
937 int status = 0;
938 long npix= nbData;
939 fits_write_img(fptr_,TFLOAT,1,npix, map,&status);
940 if( status ) printerror( status, "erreur ecriture putImageToFits" );
[1143]941 // writeSignatureOnFits();
[1136]942
943}
944void FitsOutFile::putImageToFits( int nbData, int* map) const
945{
946 int status = 0;
947
948 long npix= nbData;
949 fits_write_img(fptr_,TINT,1,npix,map,&status);
950 if( status ) printerror( status, "erreur ecriture putImageToFits" );
[1143]951 // writeSignatureOnFits();
[1136]952}
953
954
955
956void FitsOutFile::makeHeaderBntblOnFits( char* fieldType, char** Noms, int nentries, int tfields, DVList &dvl, char* extname, vector<int> taille_des_chaines)
[839]957{
958 int status = 0;
959 long nrows;
960 if (strlen(fieldType) != tfields)
961 {
962 cout << " nombre de champs :" << tfields << "nombre de types: " << strlen(fieldType) << endl;
[1136]963 throw ParmError("FitsFile:: fields and types don't match");
[839]964
965 }
966 char ** ttype= new char*[tfields];
967 char ** tform= new char*[tfields];
968 char largeur[FLEN_VALUE];
969 int noColString=0;
[971]970 int k;
971 for (k=0; k<tfields;k++)
[839]972 {
973 char format[FLEN_VALUE];
974
975 if(nentries < 1024)
976 {
977 nrows= nentries;
978 if (fieldType[k] == 'A')
979 {
980 sprintf(largeur,"%d",taille_des_chaines[noColString++]);
981 strcpy(format,largeur);
982 }
983 else strcpy(format,"1");
984 }
985 else
986 {
987 nrows = nentries/1024;
988 if(nentries%1024 != 0) nrows++;
989 if (fieldType[k] == 'A')
990 {
[1136]991 char largaux[FLEN_VALUE];
992 sprintf(largeur,"%d",taille_des_chaines[noColString]);
993 sprintf(largaux,"%d",1024*taille_des_chaines[noColString]);
994 noColString++;
995 strcpy(format, largaux);
[839]996 }
997 else strcpy(format,"1024");
998 }
999 strncat(format,&fieldType[k],1);
1000 if (fieldType[k] == 'A')
1001 {
1002 strcat(format,largeur);
1003 }
1004 ttype[k]= new char[FLEN_VALUE];
1005 strcpy(ttype[k],Noms[k]);
1006 tform[k]= new char[FLEN_VALUE];
1007 strcpy(tform[k],format);
1008 }
1009 // value of the EXTNAME keyword
1010 char extn[FLEN_VALUE];
1011 strncpy(extn,extname,FLEN_VALUE);
1012
1013 // create a new empty binary table onto the FITS file
1014 // physical units if they exist, are defined in the DVList object
1015 // so the NULL pointer is given for the tunit parameters.
1016 nrows=0;
1017 fits_create_tbl(fptr_,BINARY_TBL,nrows,tfields,ttype,tform,
1018 NULL,extn,&status);
1019 if( status ) printerror( status );
[1026]1020 if ( hdunum_ == 0 ) hdunum_ = 2;
1021 else hdunum_++;
[971]1022 int ii;
1023 for(ii = 0; ii < tfields; ii++)
[839]1024 {
1025 delete [] ttype[ii];
1026 delete [] tform[ii];
1027 }
1028 delete [] ttype;
1029 delete [] tform;
1030 //
1031 // write supplementary keywords
[1143]1032 addKeywordsOfDVList(dvl);
[839]1033
1034}
1035
[1136]1036void FitsOutFile::putColToFits(int nocol, int nentries, double* donnees) const
[839]1037{
1038 int status = 0;
[971]1039 int hdutype;
[839]1040 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1041 if( status ) printerror(status,"putColToFits: le movabs a foire");
1042 fits_get_hdu_type(fptr_, &hdutype, &status);
[867]1043 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1044 {
1045 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1046 throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
1047 }
[839]1048 int code;
1049 long repeat, width;
1050 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1051 if( code != TDOUBLE)
1052 {
1053 cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " to be written= DOUBLE " << endl;
1054 }
1055 fits_write_col(fptr_,TDOUBLE,nocol+1,1,1,nentries, donnees ,&status);
1056 if( status ) printerror( status,"erreur ecriture du fichier fits" );
1057}
[1136]1058void FitsOutFile::putColToFits(int nocol, int nentries, float* donnees) const
[839]1059{
1060 int status = 0;
1061 int hdutype;
1062 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1063 if( status ) printerror(status,"putColToFits: le movabs a foire");
1064 fits_get_hdu_type(fptr_, &hdutype, &status);
1065 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1066 {
1067 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1068 throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
1069 }
1070 if(hdutype == ASCII_TBL && nocol>0)
1071 {
1072 throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
1073 }
1074 int code;
1075 long repeat, width;
1076 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1077 if( code != TFLOAT)
1078 {
1079 cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= FLOAT " << endl;
1080 }
1081 fits_write_col(fptr_,TFLOAT,nocol+1,1,1,nentries, donnees ,&status);
1082 if( status ) printerror( status,"erreur ecriture du fichier fits" );
1083}
[1136]1084void FitsOutFile::putColToFits(int nocol, int nentries, int* donnees) const
[839]1085{
1086 int status = 0;
1087 int hdutype;
1088 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1089 if( status ) printerror(status,"putColToFits: le movabs a foire");
1090 fits_get_hdu_type(fptr_, &hdutype, &status);
1091 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1092 {
1093 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1094 throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
1095 }
1096 if(hdutype == ASCII_TBL && nocol>0)
1097 {
1098 throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
1099 }
1100 int code;
1101 long repeat, width;
1102 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1103 if( code != TLONG && code != TINT && code != TSHORT )
1104 {
[971]1105 cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= INT " << endl;
[839]1106 }
1107 fits_write_col(fptr_,TINT,nocol+1,1,1,nentries, donnees ,&status);
[971]1108 if( status ) printerror( status," ecriture du fichier fits" );
[839]1109}
[1136]1110void FitsOutFile::putColToFits(int nocol, int nentries, char** donnees) const
[839]1111{
1112 int status = 0;
1113 int hdutype;
1114 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1115 if( status ) printerror(status,"putColToFits: le movabs a foire");
1116 fits_get_hdu_type(fptr_, &hdutype, &status);
1117 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1118 {
1119 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1120 throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
1121 }
1122 if(hdutype == ASCII_TBL && nocol>0)
1123 {
1124 throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
1125 }
1126 int code;
1127 long repeat, width;
1128 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1129 if( code != TSTRING)
1130 {
1131 cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= char** " << endl;
1132 }
1133 fits_write_col(fptr_,TSTRING,nocol+1,1,1,nentries, donnees ,&status);
1134 if( status ) printerror( status,"erreur ecriture du fichier fits" );
1135}
1136
[1143]1137void FitsOutFile::DVListIntoPrimaryHeader(DVList& dvl) const
1138{
1139 int status = 0;
1140 int hdutype;
1141 fits_movabs_hdu(fptr_,1,&hdutype,&status);
1142 addKeywordsOfDVList(dvl);
1143 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1144}
[839]1145
[1143]1146
[1136]1147void FitsOutFile::writeSignatureOnFits() const
[839]1148{
1149 int status = 0;
1150 char keyname[LEN_KEYWORD];
1151 char strval[FLEN_VALUE];
1152 char comment[FLEN_COMMENT];
[971]1153 strncpy(keyname, "CREATOR", LEN_KEYWORD);
1154 keyname[LEN_KEYWORD-1] = '\0';
1155 strcpy(strval, "SOPHYA");
1156 strcpy(comment," SOPHYA Package - FITSIOServer ");
1157 fits_write_key(fptr_, TSTRING, keyname, &strval, comment, &status);
1158 if( status ) printerror( status );
[1143]1159 fits_write_date(fptr_, &status);
[971]1160 fits_write_comment(fptr_,"..............................................", &status);
1161 fits_write_comment(fptr_, " SOPHYA package - FITSIOSever ", &status);
1162 fits_write_comment(fptr_, " (C) LAL/IN2P3-CNRS Orsay, FRANCE 2000", &status);
1163 fits_write_comment(fptr_, " (C) DAPNIA/CEA Saclay, FRANCE 2000", &status);
1164 fits_write_comment(fptr_,"..............................................", &status);
[1045]1165 if( status ) printerror( status, "erreur writeSignatureOnFits" );
[839]1166}
1167
[903]1168
[1143]1169void FitsOutFile::addKeywordsOfDVList(DVList& dvl) const
1170{
1171 int status = 0;
1172 fits_write_comment(fptr_,"---------- keywords from SOPHYA ---------", &status);
1173 if (hdunum_ == 1) writeSignatureOnFits();
1174 DVList::ValList::const_iterator it;
1175 for(it = dvl.Begin(); it != dvl.End(); it++)
1176 {
1177 char keytype= (*it).second.elval.typ;
1178 char keyname[10];
1179 strncpy(keyname,(*it).first.substr(0,64).c_str(),10);
[1183]1180 string key(keyname);
[1143]1181 char comment[FLEN_COMMENT];
1182 char strval[FLEN_VALUE]= "";
1183 char *comkey = "COMMENT";
1184 fits_read_keyword(fptr_, keyname, strval, NULL, &status);
1185 if (status != 0 || strncmp(keyname,comkey,LEN_KEYWORD-1) == 0 )
1186 {
[1183]1187 string coco = dvl.GetComment(key);
1188 coco.copy( comment, FLEN_COMMENT-1);
1189 int bout = (coco.length() < FLEN_COMMENT) ? coco.length() : FLEN_COMMENT-1;
1190 comment[bout]= '\0';
[1143]1191 status = 0;
1192 switch (keytype)
1193 {
1194 case 'I' :
1195 {
[1183]1196 int ival = (int)dvl.GetI(key);
1197 fits_write_key(fptr_,TINT,keyname,&ival, comment,&status);
[1143]1198 break;
1199 }
1200 case 'D' :
1201 {
[1183]1202 double dval= (double)dvl.GetD(key);
[1143]1203 fits_write_key(fptr_,TDOUBLE,keyname,&dval,comment,&status);
1204 break;
1205 }
1206 case 'S' :
1207 {
[1183]1208 char strvaleur[FLEN_VALUE]= "";
1209 string valChaine = dvl.GetS(key);
1210 valChaine.copy(strvaleur, FLEN_VALUE-1);
1211 int fin = (valChaine.length() < FLEN_VALUE) ? valChaine.length() : FLEN_VALUE-1;
1212 strvaleur[fin]= '\0';
1213
1214 fits_write_key(fptr_,TSTRING,keyname,&strvaleur,comment,&status);
[1143]1215 break;
1216 }
1217 }
1218 }
1219 if( status ) printerror( status,"fitsfile: probleme ecriture mot-cle du dvlist" );
1220 }
1221 fits_write_comment(fptr_,"--------------------------------------", &status);
1222}
[903]1223
1224
[839]1225
Note: See TracBrowser for help on using the repository browser.