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

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

suppression delete fptr_ , fait par cfitsio

File size: 32.3 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[], bool OldFile)
24
25{
26
27 FitsOutFile of(flnm, OldFile);
28 Write(of);
29}
30
31void FitsIOHandler::Write(FitsOutFile& os)
32{
33 WriteToFits(os);
34}
35
36
37
38
39FitsFile::~FitsFile()
40{
41 int status = 0;
42 if( fptr_ != NULL)
43 {
44 fits_close_file(fptr_,&status);
45 // je ne fais pas delete fptr_, c'est la lib. fitsio qui a fait
46 // new...
47 }
48 if( status ) printerror( status );
49}
50
51
52void FitsFile::printerror(int &status)
53 //*****************************************************/
54 //* Print out cfitsio error messages and exit program */
55 //*****************************************************/
56{
57 if( status )
58 {
59 fits_report_error(stderr,status);
60 throw IOExc("FitsFile:: error FITSIO status");
61 }
62 return;
63}
64
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{
97 InitNull();
98 int status = 0;
99 fits_open_file(&fptr_,flnm,READONLY,&status);
100 if( status ) printerror( status );
101}
102
103
104void FitsInFile::InitNull()
105{
106
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();
117
118
119}
120
121
122int FitsInFile::NbBlocks(char flnm[])
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
134void FitsInFile::getBlockType(char flnm[], int hdunum, string& typeOfExtension, int& naxis, vector<int>& naxisn, string& dataType, DVList& dvl )
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;
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;
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
199 KeywordsIntoDVList(fileptr, dvl, hdunum);
200 fits_close_file(fileptr,&status);
201}
202
203void FitsInFile::GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn)
204{
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;
230}
231
232
233void FitsInFile::ReadFInit(int hdunum)
234{
235 InitNull();
236 // int status = 0;
237
238 // fits_open_file(&fptr_,flnm,READONLY,&status);
239 // if( status ) printerror( status );
240
241 int status = 0;
242
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
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 }
284
285
286void FitsInFile::moveToFollowingHeader()
287{
288 int status = 0;
289 int hdutype;
290 InitNull();
291 fits_movrel_hdu(fptr_, 1,&hdutype,&status);
292 if( status ) printerror( status," lecture du header suivant" );
293 hdunum_++;
294 hdutype_= hdutype;
295 if(hdutype_ == IMAGE_HDU)
296 {
297 GetImageParameters (fptr_, bitpix_, naxis_, naxisn_);
298 nbData_ = 1;
299 int k;
300 for (k=0; k<naxis_; k++) if (naxisn_[k] > 0) nbData_ *= naxisn_[k];
301 KeywordsIntoDVList(fptr_, dvl_,hdunum_);
302 }
303 if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
304 {
305 GetBinTabParameters(fptr_,nbcols_, nrows_,repeat_, noms_, types_, taille_des_chaines_);
306 KeywordsIntoDVList(fptr_, dvl_, hdunum_);
307 }
308}
309
310
311int FitsInFile::NbColsFromFits() const
312{
313 if(hdutype_ == BINARY_TBL) return nbcols_;
314 else
315 if(hdutype_ == ASCII_TBL || hdutype_ == IMAGE_HDU) return 1;
316 else
317 {
318 cout << " hdutype= " << hdutype_ << endl;
319 throw PException("FitsFile::NbColsFromFits, this HDU is unknown");
320 }
321}
322
323int FitsInFile::NentriesFromFits(int nocol) const
324{
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
331 {
332 cout << "hdutype= " << hdutype_ << endl;
333 throw PException("FitsFile::NentriesFromFits, this HDU is unknown");
334 }
335}
336
337char FitsInFile::ColTypeFromFits(int nocol) const
338{
339 if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
340 {
341 throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
342 }
343 return types_[nocol];
344}
345string FitsInFile::ColNameFromFits(int nocol) const
346{
347 if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
348 {
349 throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
350 }
351 return noms_[nocol];
352}
353
354int FitsInFile::ColStringLengthFromFits(int nocol) const
355{
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];
367}
368void FitsInFile::GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char ** cdata)
369{
370 int status= 0;
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++)
383 {
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 }
405 }
406}
407
408void FitsInFile::GetBinTabLine(int NoLine, float* fdata)
409{
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++)
416 {
417 fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[ncol],&anull,&status);
418 if (status)
419 {
420 ResetStatus(status);
421 break;
422 }
423 }
424}
425
426
427void FitsInFile::GetBinTabFCol(double* valeurs,int nentries, int NoCol) const
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 {
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");
438 }
439 long nels=nentries;
440 int anull;
441 // no checking for undefined pixels
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 }
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" );
457
458 }
459
460void FitsInFile::GetBinTabFCol(float* valeurs,int nentries, int NoCol) const
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 {
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");
471 }
472 long nels=nentries;
473 int anull;
474 // no checking for undefined pixels
475 float fnull= 0.;
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 }
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 }
491
492void FitsInFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
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;
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 }
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 }
521
522void FitsInFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
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;
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 }
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 }
547
548void FitsInFile::GetSingleColumn(double* map, int nentries) const
549{
550 int status = 0;
551 if(hdutype_ == IMAGE_HDU)
552 {
553
554 if(bitpix_ != DOUBLE_IMG)
555 {
556 cout << " The data type on fits file is not double...";
557 cout << " Conversion to double achieved by cfitsio lib" << endl;
558 }
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 );
567 }
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 }
578}
579
580void FitsInFile::GetSingleColumn(float* map, int nentries) const
581{
582 int status = 0;
583 if(hdutype_ == IMAGE_HDU)
584 {
585 if(bitpix_ != FLOAT_IMG)
586 {
587 cout << " The data type on fits file is not float ";
588 cout << " Conversion to float achieved by cfitsio lib" << endl;
589 }
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 );
597 }
598 else
599 if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
600 {
601 GetBinTabFCol(map,nentries, 0);
602 }
603 else
604 {
605 cout << " hdutype= " << hdutype_ << endl;
606 throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
607 }
608}
609
610void FitsInFile::GetSingleColumn( int* map, int nentries) const
611{
612 int status = 0;
613 if(hdutype_ == IMAGE_HDU)
614 {
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 );
627 }
628 else
629 if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
630 {
631 GetBinTabFCol(map,nentries, 0);
632 }
633 else
634 {
635 cout << " hdutype= " << hdutype_ << endl;
636 throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
637 }
638}
639
640void FitsInFile::GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
641 vector<int>& repeat,
642 vector<string>& noms,
643 vector<char>& types,
644 vector<int>& taille_des_chaines)
645{
646 int status= 0;
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)
653 {
654 throw IOExc("FitsFile::GetBinTabParameters this HDU is not an ASCII table nor a binary table");
655 }
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;
660
661 // get the number of columns
662 fits_get_num_cols(fileptr, &nbcols,&status);
663 if( status ) printerror( status );
664
665 // get the number of rows
666 long naxis2= 0;
667 fits_get_num_rows(fileptr,&naxis2,&status);
668 if( status ) printerror( status );
669 nrows = (int)naxis2;
670
671 // get the datatype, names and the repeat count
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();
679 char **ttype = new char*[nbcols];
680 int ii;
681 //
682 //
683 for (ii=0; ii < nbcols; ii++) ttype[ii]=new char[FLEN_VALUE];
684 int nfound;
685 fits_read_keys_str(fileptr, "TTYPE",1,nbcols,ttype,&nfound, &status);
686 if( status ) printerror( status,"erreur lecture des noms de colonne");
687 int rept=0;
688 for(ii = 0; ii < nbcols; ii++)
689 {
690 int DTYPE;
691 long width;
692 long repete = 0;
693 fits_get_coltype(fileptr,ii+1,&DTYPE,&repete,&width,&status);
694 if( status ) printerror( status,"erreur lecture type de colonne");
695 rept = repete;
696 noms.push_back(string(ttype[ii]));
697 switch (DTYPE)
698 {
699 case TDOUBLE :
700 types.push_back('D');
701 break;
702 case TFLOAT :
703 types.push_back('E');
704 break;
705 case TLONG :
706 types.push_back('I');
707 break;
708 case TINT :
709 types.push_back('I');
710 break;
711 case TSHORT :
712 types.push_back('I');
713 break;
714 case TSTRING :
715 types.push_back('S');
716 taille_des_chaines.push_back(width);
717 rept/=width;
718 break;
719 default :
720 cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
721 throw IOExc("FitsFile:: unknown type of field");
722 }
723 repeat.push_back(rept);
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";
746 char comment[FLEN_COMMENT];
747
748 // shift with the number of mandatory keywords
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 }
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
786 if(strncmp(keyname,comkey,LEN_KEYWORD-1) != 0 && strlen(keyname) != 0
787 && strlen(strval) != 0)
788 {
789 fits_get_keytype(strval,&dtype,&status);
790 if(status) printerror(status);
791
792 strip(keyname, 'B',' ');
793 strip(strval, 'B',' ');
794 strip(strval, 'B','\'');
795
796 switch( dtype )
797 {
798 case 'C':
799 fits_read_key(fileptr,TSTRING,keyname,strval,comment,&status);
800 dvl[keyname]= strval;
801 dvl.SetComment(keyname, comment);
802 break;
803 case 'I':
804 int ival;
805 fits_read_key(fileptr,TINT,keyname,&ival,comment,&status);
806 dvl[keyname]= (int_4) ival; // Portage mac DY
807 dvl.SetComment(keyname, comment);
808 break;
809 case 'L':
810 int ilog;
811 fits_read_key(fileptr,TLOGICAL,keyname,&ilog,comment,&status);
812 dvl[keyname]= (int_4) ilog;
813 dvl.SetComment(keyname, comment);
814 break;
815 case 'F':
816 double dval;
817 fits_read_key(fileptr,TDOUBLE,keyname,&dval,comment,&status);
818 dvl[keyname]= dval;
819 dvl.SetComment(keyname, comment);
820 break;
821 }
822
823 }
824 }
825 // dvl_.Print();
826}
827
828FitsOutFile::FitsOutFile()
829{
830 InitNull();
831}
832
833FitsOutFile::FitsOutFile(char flnm[], bool OldFile)
834{
835
836 InitNull();
837 int status = 0;
838
839 // create new FITS file
840 if (!OldFile)
841 {
842 fits_create_file(&fptr_,flnm,&status);
843 if( status ) printerror(status,"file already exists");
844 }
845 else
846 {
847 fits_open_file(&fptr_,flnm,READWRITE,&status);
848 if( status ) printerror(status,"file does not exist");
849 fits_get_num_hdus(fptr_, &hdunum_, &status);
850 int hdutype;
851 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
852 if( status ) printerror( status,":FitsFile::WriteF : erreur movabs");
853
854 }
855
856}
857
858
859
860void FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList &dvl)
861{
862 int status = 0;
863 long naxis = nbdim;
864 long* naxes = new long[nbdim];
865 if (hdunum_ == 1)
866 {
867 if (imageOnPrimary_ == false)
868 {
869 fits_create_img(fptr_,FLOAT_IMG,0,naxes,&status);
870 writeSignatureOnFits(); }
871 else hdunum_--;
872 }
873 int k;
874 for (k=0; k< nbdim; k++) naxes[k] = (long)naxisn[k];
875 if (type == 'D')
876 fits_create_img(fptr_,DOUBLE_IMG,naxis,naxes,&status);
877 else
878 if (type == 'E')
879 fits_create_img(fptr_,FLOAT_IMG,naxis,naxes,&status);
880 else
881 if (type == 'I')
882 fits_create_img(fptr_,LONG_IMG,naxis,naxes,&status);
883 else
884 {
885 cout << " type of data: " << type << endl;
886 throw PException("FitsFile:::makeHeaderImageOnFits:unprogrammed type of data ");
887 }
888
889 hdunum_++;
890
891 // write supplementary keywords
892 // dvl.Print();
893 addKeywordsOfDVList(dvl);
894
895 delete [] naxes;
896 if( status ) printerror( status, "erreur creation HDU IMAGE" );
897
898}
899void FitsOutFile::putImageToFits(int nbData, double* map) const
900{
901 int status = 0;
902 long npix= nbData;
903 fits_write_img(fptr_,TDOUBLE,1,npix,map,&status);
904 if( status ) printerror( status, "erreur ecriture putImageToFits" );
905 // writeSignatureOnFits();
906}
907
908void FitsOutFile::putImageToFits(int nbData, float* map) const
909{
910 int status = 0;
911 long npix= nbData;
912 fits_write_img(fptr_,TFLOAT,1,npix, map,&status);
913 if( status ) printerror( status, "erreur ecriture putImageToFits" );
914 // writeSignatureOnFits();
915
916}
917void FitsOutFile::putImageToFits( int nbData, int* map) const
918{
919 int status = 0;
920
921 long npix= nbData;
922 fits_write_img(fptr_,TINT,1,npix,map,&status);
923 if( status ) printerror( status, "erreur ecriture putImageToFits" );
924 // writeSignatureOnFits();
925}
926
927
928
929void FitsOutFile::makeHeaderBntblOnFits( char* fieldType, char** Noms, int nentries, int tfields, DVList &dvl, char* extname, vector<int> taille_des_chaines)
930{
931 int status = 0;
932 long nrows;
933 if (strlen(fieldType) != tfields)
934 {
935 cout << " nombre de champs :" << tfields << "nombre de types: " << strlen(fieldType) << endl;
936 throw ParmError("FitsFile:: fields and types don't match");
937
938 }
939 char ** ttype= new char*[tfields];
940 char ** tform= new char*[tfields];
941 char largeur[FLEN_VALUE];
942 int noColString=0;
943 int k;
944 for (k=0; k<tfields;k++)
945 {
946 char format[FLEN_VALUE];
947
948 if(nentries < 1024)
949 {
950 nrows= nentries;
951 if (fieldType[k] == 'A')
952 {
953 sprintf(largeur,"%d",taille_des_chaines[noColString++]);
954 strcpy(format,largeur);
955 }
956 else strcpy(format,"1");
957 }
958 else
959 {
960 nrows = nentries/1024;
961 if(nentries%1024 != 0) nrows++;
962 if (fieldType[k] == 'A')
963 {
964 char largaux[FLEN_VALUE];
965 sprintf(largeur,"%d",taille_des_chaines[noColString]);
966 sprintf(largaux,"%d",1024*taille_des_chaines[noColString]);
967 noColString++;
968 strcpy(format, largaux);
969 }
970 else strcpy(format,"1024");
971 }
972 strncat(format,&fieldType[k],1);
973 if (fieldType[k] == 'A')
974 {
975 strcat(format,largeur);
976 }
977 ttype[k]= new char[FLEN_VALUE];
978 strcpy(ttype[k],Noms[k]);
979 tform[k]= new char[FLEN_VALUE];
980 strcpy(tform[k],format);
981 }
982 // value of the EXTNAME keyword
983 char extn[FLEN_VALUE];
984 strncpy(extn,extname,FLEN_VALUE);
985
986 // create a new empty binary table onto the FITS file
987 // physical units if they exist, are defined in the DVList object
988 // so the NULL pointer is given for the tunit parameters.
989 nrows=0;
990 fits_create_tbl(fptr_,BINARY_TBL,nrows,tfields,ttype,tform,
991 NULL,extn,&status);
992 if( status ) printerror( status );
993 if ( hdunum_ == 0 ) hdunum_ = 2;
994 else hdunum_++;
995 int ii;
996 for(ii = 0; ii < tfields; ii++)
997 {
998 delete [] ttype[ii];
999 delete [] tform[ii];
1000 }
1001 delete [] ttype;
1002 delete [] tform;
1003 //
1004 // write supplementary keywords
1005 addKeywordsOfDVList(dvl);
1006
1007}
1008
1009void FitsOutFile::putColToFits(int nocol, int nentries, double* donnees) const
1010{
1011 int status = 0;
1012 int hdutype;
1013 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1014 if( status ) printerror(status,"putColToFits: le movabs a foire");
1015 fits_get_hdu_type(fptr_, &hdutype, &status);
1016 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1017 {
1018 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1019 throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
1020 }
1021 int code;
1022 long repeat, width;
1023 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1024 if( code != TDOUBLE)
1025 {
1026 cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " to be written= DOUBLE " << endl;
1027 }
1028 fits_write_col(fptr_,TDOUBLE,nocol+1,1,1,nentries, donnees ,&status);
1029 if( status ) printerror( status,"erreur ecriture du fichier fits" );
1030}
1031void FitsOutFile::putColToFits(int nocol, int nentries, float* donnees) const
1032{
1033 int status = 0;
1034 int hdutype;
1035 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1036 if( status ) printerror(status,"putColToFits: le movabs a foire");
1037 fits_get_hdu_type(fptr_, &hdutype, &status);
1038 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1039 {
1040 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1041 throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
1042 }
1043 if(hdutype == ASCII_TBL && nocol>0)
1044 {
1045 throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
1046 }
1047 int code;
1048 long repeat, width;
1049 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1050 if( code != TFLOAT)
1051 {
1052 cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= FLOAT " << endl;
1053 }
1054 fits_write_col(fptr_,TFLOAT,nocol+1,1,1,nentries, donnees ,&status);
1055 if( status ) printerror( status,"erreur ecriture du fichier fits" );
1056}
1057void FitsOutFile::putColToFits(int nocol, int nentries, int* donnees) const
1058{
1059 int status = 0;
1060 int hdutype;
1061 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1062 if( status ) printerror(status,"putColToFits: le movabs a foire");
1063 fits_get_hdu_type(fptr_, &hdutype, &status);
1064 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1065 {
1066 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1067 throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
1068 }
1069 if(hdutype == ASCII_TBL && nocol>0)
1070 {
1071 throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
1072 }
1073 int code;
1074 long repeat, width;
1075 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1076 if( code != TLONG && code != TINT && code != TSHORT )
1077 {
1078 cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= INT " << endl;
1079 }
1080 fits_write_col(fptr_,TINT,nocol+1,1,1,nentries, donnees ,&status);
1081 if( status ) printerror( status," ecriture du fichier fits" );
1082}
1083void FitsOutFile::putColToFits(int nocol, int nentries, char** donnees) const
1084{
1085 int status = 0;
1086 int hdutype;
1087 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1088 if( status ) printerror(status,"putColToFits: le movabs a foire");
1089 fits_get_hdu_type(fptr_, &hdutype, &status);
1090 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1091 {
1092 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1093 throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
1094 }
1095 if(hdutype == ASCII_TBL && nocol>0)
1096 {
1097 throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
1098 }
1099 int code;
1100 long repeat, width;
1101 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1102 if( code != TSTRING)
1103 {
1104 cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= char** " << endl;
1105 }
1106 fits_write_col(fptr_,TSTRING,nocol+1,1,1,nentries, donnees ,&status);
1107 if( status ) printerror( status,"erreur ecriture du fichier fits" );
1108}
1109
1110void FitsOutFile::DVListIntoPrimaryHeader(DVList& dvl) const
1111{
1112 int status = 0;
1113 int hdutype;
1114 fits_movabs_hdu(fptr_,1,&hdutype,&status);
1115 addKeywordsOfDVList(dvl);
1116 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1117}
1118
1119
1120void FitsOutFile::writeSignatureOnFits() const
1121{
1122 int status = 0;
1123 char keyname[LEN_KEYWORD];
1124 char strval[FLEN_VALUE];
1125 char comment[FLEN_COMMENT];
1126 strncpy(keyname, "CREATOR", LEN_KEYWORD);
1127 keyname[LEN_KEYWORD-1] = '\0';
1128 strcpy(strval, "SOPHYA");
1129 strcpy(comment," SOPHYA Package - FITSIOServer ");
1130 fits_write_key(fptr_, TSTRING, keyname, &strval, comment, &status);
1131 if( status ) printerror( status );
1132 fits_write_date(fptr_, &status);
1133 fits_write_comment(fptr_,"..............................................", &status);
1134 fits_write_comment(fptr_, " SOPHYA package - FITSIOSever ", &status);
1135 fits_write_comment(fptr_, " (C) LAL/IN2P3-CNRS Orsay, FRANCE 2000", &status);
1136 fits_write_comment(fptr_, " (C) DAPNIA/CEA Saclay, FRANCE 2000", &status);
1137 fits_write_comment(fptr_,"..............................................", &status);
1138 if( status ) printerror( status, "erreur writeSignatureOnFits" );
1139}
1140
1141
1142void FitsOutFile::addKeywordsOfDVList(DVList& dvl) const
1143{
1144 int status = 0;
1145 fits_write_comment(fptr_,"---------- keywords from SOPHYA ---------", &status);
1146 if (hdunum_ == 1) writeSignatureOnFits();
1147 DVList::ValList::const_iterator it;
1148 for(it = dvl.Begin(); it != dvl.End(); it++)
1149 {
1150 char keytype= (*it).second.elval.typ;
1151 char keyname[10];
1152 strncpy(keyname,(*it).first.substr(0,64).c_str(),10);
1153 char comment[FLEN_COMMENT];
1154 char strval[FLEN_VALUE]= "";
1155 char *comkey = "COMMENT";
1156 fits_read_keyword(fptr_, keyname, strval, NULL, &status);
1157 if (status != 0 || strncmp(keyname,comkey,LEN_KEYWORD-1) == 0 )
1158 {
1159 status = 0;
1160 switch (keytype)
1161 {
1162 case 'I' :
1163 {
1164 int ival=(*it).second.elval.iv;
1165 strncpy(comment,(*it).second.elcomm.c_str(),FLEN_COMMENT );
1166 fits_write_key(fptr_,TINT,keyname,&ival,comment,&status);
1167 break;
1168 }
1169 case 'D' :
1170 {
1171 double dval=(*it).second.elval.dv;
1172 strncpy(comment,(*it).second.elcomm.c_str(),FLEN_COMMENT );
1173 fits_write_key(fptr_,TDOUBLE,keyname,&dval,comment,&status);
1174 break;
1175 }
1176 case 'S' :
1177 {
1178 char strval[128];
1179 strncpy(strval,(*it).second.elval.strv->c_str(),127);
1180 strncpy(comment,(*it).second.elcomm.c_str(),FLEN_COMMENT );
1181 fits_write_key(fptr_,TSTRING,keyname,&strval,comment,&status);
1182 break;
1183 }
1184 }
1185 }
1186 if( status ) printerror( status,"fitsfile: probleme ecriture mot-cle du dvlist" );
1187 }
1188 fits_write_comment(fptr_,"--------------------------------------", &status);
1189}
1190
1191
1192
Note: See TracBrowser for help on using the repository browser.