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

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

revu l'ouverture des fichiers

File size: 33.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[], string WriteMode)
24
25{
26
27 FitsOutFile of(flnm, WriteMode);
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 if(strncmp(keyname,comkey,LEN_KEYWORD-1) != 0 && strlen(keyname) != 0
786 && strlen(strval) != 0)
787 {
788 fits_get_keytype(strval,&dtype,&status);
789 if(status) printerror(status);
790
791 strip(keyname, 'B',' ');
792 strip(strval, 'B',' ');
793 strip(strval, 'B','\'');
794
795 switch( dtype )
796 {
797 case 'C':
798 fits_read_key(fileptr,TSTRING,keyname,strval,comment,&status);
799 dvl[keyname]= strval;
800 dvl.SetComment(keyname, comment);
801 break;
802 case 'I':
803 int ival;
804 fits_read_key(fileptr,TINT,keyname,&ival,comment,&status);
805 dvl[keyname]= (int_4) ival; // Portage mac DY
806 dvl.SetComment(keyname, comment);
807 break;
808 case 'L':
809 int ilog;
810 fits_read_key(fileptr,TLOGICAL,keyname,&ilog,comment,&status);
811 dvl[keyname]= (int_4) ilog;
812 dvl.SetComment(keyname, comment);
813 break;
814 case 'F':
815 double dval;
816 fits_read_key(fileptr,TDOUBLE,keyname,&dval,comment,&status);
817 dvl[keyname]= dval;
818 dvl.SetComment(keyname, comment);
819 break;
820 }
821
822 }
823 }
824 // dvl_.Print();
825}
826
827FitsOutFile::FitsOutFile()
828{
829 InitNull();
830}
831
832FitsOutFile::FitsOutFile(char flnm[], string WriteMode)
833{
834
835 InitNull();
836 int status = 0;
837
838 // create new FITS file
839
840 fits_create_file(&fptr_,flnm,&status);
841 if( status )
842 {
843 // si on veut ecrire a la fin de ce fichier
844 if (WriteMode == string("append"))
845 {
846 status = 0;
847 fits_open_file(&fptr_,flnm,READWRITE,&status);
848 if( status )
849 {
850 cout << " error opening file: " << flnm << endl;
851 printerror(status, "failure opening a file supposed to exist");
852 }
853 else cout << " file " << flnm << " opened, new objects will be appended " << endl;
854 fits_get_num_hdus(fptr_, &hdunum_, &status);
855 int hdutype;
856 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
857 if( status ) printerror( status,":FitsFile::WriteF : erreur movabs");
858 }
859 else
860 if (WriteMode == string("clear"))
861 {
862 status = 0;
863 char* newname = new char[strlen(flnm)+1];
864 //
865 newname[0] = '!';
866 newname[1] = '\0';
867 strcat(newname, flnm);
868 fits_create_file(&fptr_,newname,&status);
869 if (status)
870 {
871 cout << " error opening file: " << flnm << endl;
872 printerror(status, "unable to open file, supposed to exist");
873 }
874 else cout << " file " << flnm << " will be overwrited " << endl;
875
876 }
877 else
878 if (WriteMode == string("unknown")) printerror(status, " file seems already to exist");
879
880 else printerror(status, "open file failed");
881
882 }
883}
884
885
886
887void FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList &dvl)
888{
889 int status = 0;
890 long naxis = nbdim;
891 long* naxes = new long[nbdim];
892 if (hdunum_ == 1)
893 {
894 if (imageOnPrimary_ == false)
895 {
896 fits_create_img(fptr_,FLOAT_IMG,0,naxes,&status);
897 writeSignatureOnFits(); }
898 else hdunum_--;
899 }
900 int k;
901 for (k=0; k< nbdim; k++) naxes[k] = (long)naxisn[k];
902 if (type == 'D')
903 fits_create_img(fptr_,DOUBLE_IMG,naxis,naxes,&status);
904 else
905 if (type == 'E')
906 fits_create_img(fptr_,FLOAT_IMG,naxis,naxes,&status);
907 else
908 if (type == 'I')
909 fits_create_img(fptr_,LONG_IMG,naxis,naxes,&status);
910 else
911 {
912 cout << " type of data: " << type << endl;
913 throw PException("FitsFile:::makeHeaderImageOnFits:unprogrammed type of data ");
914 }
915
916 hdunum_++;
917
918 // write supplementary keywords
919 // dvl.Print();
920 addKeywordsOfDVList(dvl);
921
922 delete [] naxes;
923 if( status ) printerror( status, "erreur creation HDU IMAGE" );
924
925}
926void FitsOutFile::putImageToFits(int nbData, double* map) const
927{
928 int status = 0;
929 long npix= nbData;
930 fits_write_img(fptr_,TDOUBLE,1,npix,map,&status);
931 if( status ) printerror( status, "erreur ecriture putImageToFits" );
932 // writeSignatureOnFits();
933}
934
935void FitsOutFile::putImageToFits(int nbData, float* map) const
936{
937 int status = 0;
938 long npix= nbData;
939 fits_write_img(fptr_,TFLOAT,1,npix, map,&status);
940 if( status ) printerror( status, "erreur ecriture putImageToFits" );
941 // writeSignatureOnFits();
942
943}
944void FitsOutFile::putImageToFits( int nbData, int* map) const
945{
946 int status = 0;
947
948 long npix= nbData;
949 fits_write_img(fptr_,TINT,1,npix,map,&status);
950 if( status ) printerror( status, "erreur ecriture putImageToFits" );
951 // writeSignatureOnFits();
952}
953
954
955
956void FitsOutFile::makeHeaderBntblOnFits( char* fieldType, char** Noms, int nentries, int tfields, DVList &dvl, char* extname, vector<int> taille_des_chaines)
957{
958 int status = 0;
959 long nrows;
960 if (strlen(fieldType) != tfields)
961 {
962 cout << " nombre de champs :" << tfields << "nombre de types: " << strlen(fieldType) << endl;
963 throw ParmError("FitsFile:: fields and types don't match");
964
965 }
966 char ** ttype= new char*[tfields];
967 char ** tform= new char*[tfields];
968 char largeur[FLEN_VALUE];
969 int noColString=0;
970 int k;
971 for (k=0; k<tfields;k++)
972 {
973 char format[FLEN_VALUE];
974
975 if(nentries < 1024)
976 {
977 nrows= nentries;
978 if (fieldType[k] == 'A')
979 {
980 sprintf(largeur,"%d",taille_des_chaines[noColString++]);
981 strcpy(format,largeur);
982 }
983 else strcpy(format,"1");
984 }
985 else
986 {
987 nrows = nentries/1024;
988 if(nentries%1024 != 0) nrows++;
989 if (fieldType[k] == 'A')
990 {
991 char largaux[FLEN_VALUE];
992 sprintf(largeur,"%d",taille_des_chaines[noColString]);
993 sprintf(largaux,"%d",1024*taille_des_chaines[noColString]);
994 noColString++;
995 strcpy(format, largaux);
996 }
997 else strcpy(format,"1024");
998 }
999 strncat(format,&fieldType[k],1);
1000 if (fieldType[k] == 'A')
1001 {
1002 strcat(format,largeur);
1003 }
1004 ttype[k]= new char[FLEN_VALUE];
1005 strcpy(ttype[k],Noms[k]);
1006 tform[k]= new char[FLEN_VALUE];
1007 strcpy(tform[k],format);
1008 }
1009 // value of the EXTNAME keyword
1010 char extn[FLEN_VALUE];
1011 strncpy(extn,extname,FLEN_VALUE);
1012
1013 // create a new empty binary table onto the FITS file
1014 // physical units if they exist, are defined in the DVList object
1015 // so the NULL pointer is given for the tunit parameters.
1016 nrows=0;
1017 fits_create_tbl(fptr_,BINARY_TBL,nrows,tfields,ttype,tform,
1018 NULL,extn,&status);
1019 if( status ) printerror( status );
1020 if ( hdunum_ == 0 ) hdunum_ = 2;
1021 else hdunum_++;
1022 int ii;
1023 for(ii = 0; ii < tfields; ii++)
1024 {
1025 delete [] ttype[ii];
1026 delete [] tform[ii];
1027 }
1028 delete [] ttype;
1029 delete [] tform;
1030 //
1031 // write supplementary keywords
1032 addKeywordsOfDVList(dvl);
1033
1034}
1035
1036void FitsOutFile::putColToFits(int nocol, int nentries, double* donnees) const
1037{
1038 int status = 0;
1039 int hdutype;
1040 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1041 if( status ) printerror(status,"putColToFits: le movabs a foire");
1042 fits_get_hdu_type(fptr_, &hdutype, &status);
1043 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1044 {
1045 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1046 throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
1047 }
1048 int code;
1049 long repeat, width;
1050 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1051 if( code != TDOUBLE)
1052 {
1053 cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " to be written= DOUBLE " << endl;
1054 }
1055 fits_write_col(fptr_,TDOUBLE,nocol+1,1,1,nentries, donnees ,&status);
1056 if( status ) printerror( status,"erreur ecriture du fichier fits" );
1057}
1058void FitsOutFile::putColToFits(int nocol, int nentries, float* donnees) const
1059{
1060 int status = 0;
1061 int hdutype;
1062 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1063 if( status ) printerror(status,"putColToFits: le movabs a foire");
1064 fits_get_hdu_type(fptr_, &hdutype, &status);
1065 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1066 {
1067 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1068 throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
1069 }
1070 if(hdutype == ASCII_TBL && nocol>0)
1071 {
1072 throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
1073 }
1074 int code;
1075 long repeat, width;
1076 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1077 if( code != TFLOAT)
1078 {
1079 cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= FLOAT " << endl;
1080 }
1081 fits_write_col(fptr_,TFLOAT,nocol+1,1,1,nentries, donnees ,&status);
1082 if( status ) printerror( status,"erreur ecriture du fichier fits" );
1083}
1084void FitsOutFile::putColToFits(int nocol, int nentries, int* donnees) const
1085{
1086 int status = 0;
1087 int hdutype;
1088 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1089 if( status ) printerror(status,"putColToFits: le movabs a foire");
1090 fits_get_hdu_type(fptr_, &hdutype, &status);
1091 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1092 {
1093 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1094 throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
1095 }
1096 if(hdutype == ASCII_TBL && nocol>0)
1097 {
1098 throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
1099 }
1100 int code;
1101 long repeat, width;
1102 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1103 if( code != TLONG && code != TINT && code != TSHORT )
1104 {
1105 cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= INT " << endl;
1106 }
1107 fits_write_col(fptr_,TINT,nocol+1,1,1,nentries, donnees ,&status);
1108 if( status ) printerror( status," ecriture du fichier fits" );
1109}
1110void FitsOutFile::putColToFits(int nocol, int nentries, char** donnees) const
1111{
1112 int status = 0;
1113 int hdutype;
1114 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1115 if( status ) printerror(status,"putColToFits: le movabs a foire");
1116 fits_get_hdu_type(fptr_, &hdutype, &status);
1117 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
1118 {
1119 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
1120 throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
1121 }
1122 if(hdutype == ASCII_TBL && nocol>0)
1123 {
1124 throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
1125 }
1126 int code;
1127 long repeat, width;
1128 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
1129 if( code != TSTRING)
1130 {
1131 cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= char** " << endl;
1132 }
1133 fits_write_col(fptr_,TSTRING,nocol+1,1,1,nentries, donnees ,&status);
1134 if( status ) printerror( status,"erreur ecriture du fichier fits" );
1135}
1136
1137void FitsOutFile::DVListIntoPrimaryHeader(DVList& dvl) const
1138{
1139 int status = 0;
1140 int hdutype;
1141 fits_movabs_hdu(fptr_,1,&hdutype,&status);
1142 addKeywordsOfDVList(dvl);
1143 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
1144}
1145
1146
1147void FitsOutFile::writeSignatureOnFits() const
1148{
1149 int status = 0;
1150 char keyname[LEN_KEYWORD];
1151 char strval[FLEN_VALUE];
1152 char comment[FLEN_COMMENT];
1153 strncpy(keyname, "CREATOR", LEN_KEYWORD);
1154 keyname[LEN_KEYWORD-1] = '\0';
1155 strcpy(strval, "SOPHYA");
1156 strcpy(comment," SOPHYA Package - FITSIOServer ");
1157 fits_write_key(fptr_, TSTRING, keyname, &strval, comment, &status);
1158 if( status ) printerror( status );
1159 fits_write_date(fptr_, &status);
1160 fits_write_comment(fptr_,"..............................................", &status);
1161 fits_write_comment(fptr_, " SOPHYA package - FITSIOSever ", &status);
1162 fits_write_comment(fptr_, " (C) LAL/IN2P3-CNRS Orsay, FRANCE 2000", &status);
1163 fits_write_comment(fptr_, " (C) DAPNIA/CEA Saclay, FRANCE 2000", &status);
1164 fits_write_comment(fptr_,"..............................................", &status);
1165 if( status ) printerror( status, "erreur writeSignatureOnFits" );
1166}
1167
1168
1169void FitsOutFile::addKeywordsOfDVList(DVList& dvl) const
1170{
1171 int status = 0;
1172 fits_write_comment(fptr_,"---------- keywords from SOPHYA ---------", &status);
1173 if (hdunum_ == 1) writeSignatureOnFits();
1174 DVList::ValList::const_iterator it;
1175 for(it = dvl.Begin(); it != dvl.End(); it++)
1176 {
1177 char keytype= (*it).second.elval.typ;
1178 char keyname[10];
1179 strncpy(keyname,(*it).first.substr(0,64).c_str(),10);
1180 string key(keyname);
1181 char comment[FLEN_COMMENT];
1182 char strval[FLEN_VALUE]= "";
1183 char *comkey = "COMMENT";
1184 fits_read_keyword(fptr_, keyname, strval, NULL, &status);
1185 if (status != 0 || strncmp(keyname,comkey,LEN_KEYWORD-1) == 0 )
1186 {
1187 string coco = dvl.GetComment(key);
1188 coco.copy( comment, FLEN_COMMENT-1);
1189 int bout = (coco.length() < FLEN_COMMENT) ? coco.length() : FLEN_COMMENT-1;
1190 comment[bout]= '\0';
1191 status = 0;
1192 switch (keytype)
1193 {
1194 case 'I' :
1195 {
1196 int ival = (int)dvl.GetI(key);
1197 fits_write_key(fptr_,TINT,keyname,&ival, comment,&status);
1198 break;
1199 }
1200 case 'D' :
1201 {
1202 double dval= (double)dvl.GetD(key);
1203 fits_write_key(fptr_,TDOUBLE,keyname,&dval,comment,&status);
1204 break;
1205 }
1206 case 'S' :
1207 {
1208 char strvaleur[FLEN_VALUE]= "";
1209 string valChaine = dvl.GetS(key);
1210 valChaine.copy(strvaleur, FLEN_VALUE-1);
1211 int fin = (valChaine.length() < FLEN_VALUE) ? valChaine.length() : FLEN_VALUE-1;
1212 strvaleur[fin]= '\0';
1213
1214 fits_write_key(fptr_,TSTRING,keyname,&strvaleur,comment,&status);
1215 break;
1216 }
1217 }
1218 }
1219 if( status ) printerror( status,"fitsfile: probleme ecriture mot-cle du dvlist" );
1220 }
1221 fits_write_comment(fptr_,"--------------------------------------", &status);
1222}
1223
1224
1225
Note: See TracBrowser for help on using the repository browser.