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

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

amelioration lecture ligne a ligne

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