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

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

structuration a la ppersist+ convention sur hdu

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