source: Sophya/trunk/SophyaExt/FitsIOServer/fitsioserver.cc@ 605

Last change on this file since 605 was 605, checked in by ansari, 26 years ago

Extension + Bugs FitsIO - Reza 20/11/99

File size: 57.0 KB
Line 
1//************************************************************************
2// Class for loadind and saving from FITS-formatted file to DPC objects
3// (G. Le Meur ; Francois Touze) OCT. 99
4//
5// methods 'load(X& x, char f[])' get from FITS file "f" a DPC object x
6// from DPC class X.
7// methods 'save(X& x, char f[])' save a DPC object x from DPC // class X
8// onto a FITS file "f" .
9
10//************************************************************************
11
12#include "machdefs.h"
13#include <iostream.h>
14#include <list>
15#include <string>
16
17#include "fitsioserver.h"
18#include "pexceptions.h"
19#include "strutil.h"
20
21void FitsIoServer::load(TMatrix<double>& mat,char flnm[])
22{
23 int nbrows=0;
24 int nbcols=0;
25 FITS_tab_typ_ = TDOUBLE;
26 long naxis;
27 int n1, n2, n3;
28 DVList dvl;
29 planck_read_img(flnm, naxis, n1, n2, n3, dvl);
30
31 nbrows=n1;
32 nbcols=n2;
33 if (naxis == 1) nbcols=1;
34 if (naxis > 2)
35 {
36 cout<<" FitsIOServer : le fichier fits n'est pas une matrice, naxis= " <<naxis<< endl;
37 }
38
39 // number of components
40 if (mat.NRows() != nbrows || mat.NCols() != nbcols )
41 {
42 cout << " found " << nbrows << " rows ";
43 cout << " expected " << mat.NRows() << endl;
44 cout << " found " << nbcols << " columns " ;
45 cout << " expected " << mat.NCols() << endl;
46 mat.ReSize(nbrows,nbcols);
47 cout << " resize the vector to nbrows= " << nbrows << " nbcols= " << nbcols << endl;
48 }
49 int ij=0;
50 for (int j=0; j< nbcols; j++)
51 for (int i = 0; i < nbrows; i++) mat(i,j) = (double)r_8tab_[ij++];
52}
53
54void FitsIoServer::load(TMatrix<float>& mat,char flnm[])
55{
56 int nbrows=0;
57 int nbcols=0;
58 FITS_tab_typ_ = TFLOAT;
59 long naxis;
60 int n1, n2, n3;
61 DVList dvl;
62 planck_read_img(flnm, naxis, n1, n2, n3, dvl);
63
64 nbrows=n1;
65 nbcols=n2;
66 if (naxis == 1) nbcols=1;
67 if (naxis > 2)
68 {
69 cout<<" FitsIOServer : le fichier fits n'est pas une matrice, naxis= " <<naxis<< endl;
70 }
71
72 // number of components
73 if (mat.NRows() != nbrows || mat.NCols() != nbcols )
74 {
75 cout << " found " << nbrows << " rows ";
76 cout << " expected " << mat.NRows() << endl;
77 cout << " found " << nbcols << " columns " ;
78 cout << " expected " << mat.NCols() << endl;
79 mat.ReSize(nbrows,nbcols);
80 cout << " resize the vector to nbrows= " << nbrows << " nbcols= " << nbcols << endl;
81 }
82 int ij=0;
83 for (int j=0; j< nbcols; j++)
84 for (int i = 0; i < nbrows; i++) mat(i,j) = (float)r_4tab_[ij++];
85}
86
87void FitsIoServer::load(TMatrix<int_4>& mat,char flnm[])
88{
89 int nbrows=0;
90 int nbcols=0;
91 FITS_tab_typ_ = TINT;
92 long naxis;
93 int n1, n2, n3;
94 DVList dvl;
95 planck_read_img(flnm, naxis, n1, n2, n3, dvl);
96
97 nbrows=n1;
98 nbcols=n2;
99 if (naxis == 1) nbcols=1;
100 if (naxis > 2)
101 {
102 cout<<" FitsIOServer : le fichier fits n'est pas une matrice, naxis= " <<naxis<< endl;
103 }
104
105 // number of components
106 if (mat.NRows() != nbrows || mat.NCols() != nbcols )
107 {
108 cout << " found " << nbrows << " rows ";
109 cout << " expected " << mat.NRows() << endl;
110 cout << " found " << nbcols << " columns " ;
111 cout << " expected " << mat.NCols() << endl;
112 mat.ReSize(nbrows,nbcols);
113 cout << " resize the vector to nbrows= " << nbrows << " nbcols= " << nbcols << endl;
114 }
115 int ij=0;
116 for (int j=0; j< nbcols; j++)
117 for (int i = 0; i < nbrows; i++) mat(i,j) = (int_4)i_4tab_[ij++];
118}
119
120void FitsIoServer::load(NTuple& ntpl,char flnm[],int hdunum)
121
122 //********************************************************/
123 //* move to the HDU which has the specified number hdunum*/
124 //* in the FITS file, perform read operations and write */
125 //* the elements in an NTuple. */
126 //********************************************************/
127{
128
129 // pointer to the FITS file, defined in fitsio.h
130 fitsfile *fptr;
131 int status = 0;
132 if( fits_open_file(&fptr,flnm,READONLY,&status) )
133 printerror( status );
134
135 // move to the HDU
136 int hdutype;
137 if( fits_movabs_hdu(fptr,hdunum,&hdutype,&status) )
138 printerror( status );
139
140 // get number of keywords
141 int nkeys,keypos;
142 if( fits_get_hdrpos(fptr,&nkeys,&keypos,&status) )
143 printerror( status );
144
145
146 if (hdutype == BINARY_TBL)
147 printf("\nReading binary table in HDU %d:\n",hdunum);
148 else
149 {
150 printf("Error:: this HDU is not a binary table\n");
151 throw IOExc("FitsIoServer::load(NTuple& ," + (string)flnm + ") Error Not a bin table !");
152// exit ( status );
153 }
154
155 // number of columns
156 int tfields;
157 if( fits_get_num_cols(fptr,&tfields,&status) )
158 printerror( status );
159
160 // to get table size
161 long naxis[2];
162 int nfound;
163 if( fits_read_keys_lng(fptr, "NAXIS", 1, 2, naxis, &nfound, &status) )
164 printerror( status );
165 int nrows= naxis[1];
166
167 //Information about each column
168 char **ttype, **tform;
169 ttype= new char*[tfields];
170 tform= new char*[tfields];
171 for( int ii = 0; ii < tfields; ii++)
172 {
173 ttype[ii]= new char[FLEN_VALUE];
174 tform[ii]= new char[FLEN_VALUE];
175 }
176
177 // number of TTYPEn,TFORMn and TUNITn keywords found
178 int num= 0;
179
180 // read the column names from the TTYPEn keywords
181 fits_read_keys_str(fptr,"TTYPE",1,tfields,ttype,&nfound,&status);
182 num += nfound;
183 // read the column names from the TFORMn keywords
184 fits_read_keys_str(fptr,"TFORM",1,tfields,tform,&nfound,&status);
185 num += nfound;
186
187 //for(int ii = 0; ii < tfields; ii++)
188 // printf("\nColumn name & Format %8s %8s", ttype[ii],tform[ii]);
189
190 // select the columns with float data values
191 int typecode;
192 long repeat,width;
193 list<int> column;
194 for( int ii = 0; ii < tfields; ii++)
195 {
196 fits_binary_tform(tform[ii],&typecode, &repeat, &width, &status);
197 //printf("\n%4s %3d %2ld %2ld", tform[ii], typecode, repeat, width);
198 if(typecode == TFLOAT)
199 column.push_back(ii+1);
200 }
201
202 // get input elements to create the NTuple
203 char **clname;
204 clname= new char*[column.size()];
205 for(int ii = 0; ii < column.size(); ii++)
206 clname[ii]= new char[FLEN_VALUE];
207
208 list<int>::iterator itr;
209 int index= 0;
210 for(itr= column.begin(); itr != column.end(); itr++)
211 strcpy(clname[index++],ttype[*itr-1]);
212
213 for( int ii = 0; ii < tfields; ii++)
214 {
215 delete [] ttype[ii];
216 delete [] tform[ii];
217 }
218 delete [] ttype;
219 delete [] tform;
220
221
222 // check if the specified keyword BLK exists
223 int blk= 512;
224 if(check_keyword(fptr,nkeys,"BLK"))
225 {
226 if( fits_read_key(fptr,TINT,"BLK",&blk,NULL,&status) )
227 printerror( status );
228 }
229 // create a NTuple
230 NTuple nt0(column.size(),clname,blk);
231
232 for(int ii = 0; ii < column.size(); ii++)
233 delete [] clname[ii];
234 delete [] clname;
235
236 float value[1];
237 long felem = 1;
238 char strnull[10];
239 strcpy(strnull, " ");
240 int anynull= 0;
241 long longnull = 0;
242
243 // value to represent undefined array elements
244 float floatnull = FLOATNULLVALUE;
245
246 // read elements from columns and fill NTuple
247 for (int k = 0; k < nrows; k++)
248 {
249 int j= 0;
250 float *xnt= new float[column.size()];
251 list<int>::iterator itr;
252 for(itr= column.begin(); itr != column.end(); itr++)
253 {
254 fits_read_col(fptr,TFLOAT,*itr,k+1,felem,1,&floatnull,value,
255 &anynull, &status);
256 xnt[j++]= value[0];
257 }
258 nt0.Fill(xnt);
259 delete[] xnt;
260 }
261
262 // the TUNITn keywords are optional, if they exist they are put
263 // in the DVLIst object
264 char keyname[LEN_KEYWORD]= "";
265 char strval[FLEN_VALUE];
266 for(int ii = 0; ii < tfields; ii++)
267 {
268 fits_make_keyn("TUNIT",ii+1,keyname,&status);
269 if(check_keyword(fptr,nkeys,keyname))
270 {
271 num++;
272 if( fits_read_key_str(fptr,keyname,strval,NULL,&status) )
273 printerror(status);
274 strip (keyname, 'B',' ');
275 strip(strval, 'B',' ');
276 nt0.Info()[keyname]= strval;
277 }
278 }
279
280 // add the number of mandatory keywords of a binary table
281 num += 8;
282
283 // put names and values of other reserved keywords in a DVList object
284 char comment[FLEN_COMMENT];
285 char dtype;
286 for(int j = num+1; j <= nkeys; j++)
287 {
288 fits_read_keyn(fptr,j,keyname,strval,comment,&status);
289 fits_get_keytype(strval,&dtype,&status);
290 strip (keyname, 'B',' ');
291 strip(strval, 'B',' ');
292
293 switch( dtype )
294 {
295 case 'C':
296 nt0.Info()[keyname]= strval;
297 break;
298 case 'I':
299 int ival;
300 ctoi(strval,&ival);
301 nt0.Info()[keyname]= (int_4)ival;
302 break;
303 case 'F':
304 double dval;
305 ctof(strval,&dval);
306 nt0.Info()[keyname]= dval;
307 break;
308 }
309 }
310
311 // copy in the input NTuple
312 ntpl= nt0;
313
314 if( fits_close_file(fptr, &status) )
315 printerror(status);
316
317 printf("\n");
318 return;
319}
320
321
322void FitsIoServer::load(SphericalMap<double>& sph, char flnm[])
323{
324 int npixels=0;
325 int nside=0;
326 long naxis;
327 int n1, n2, n3;
328
329 FITS_tab_typ_ = TDOUBLE;
330
331 DVList dvl;
332 planck_read_img(flnm, naxis, n1, n2, n3, dvl);
333 if (naxis != 1)
334 {
335 cout << " le fichier fits n'est pas une sphere, naxis= " << naxis << endl;
336 }
337 npixels=n1;
338 nside= dvl.GetI("NSIDE");
339
340 // number of pixels in the sphere
341 if (sph.NbPixels() != npixels)
342 {
343 cout << " found " << npixels << " pixels" << endl;
344 cout << " expected " << sph.NbPixels() << endl;
345 if (nside <= 0 )
346 {
347 cout<<" FITSIOSERVER: no resolution parameter on fits file "<<endl;
348 throw IOExc("FitsIoServer::load(SphericalMap<double>& ," + (string)flnm + ") Error NSide<0 !");
349// exit(0);
350 }
351 if (nside != sph.SizeIndex())
352 {
353 sph.Resize(nside);
354 cout << " resizing the sphere to nside= " << nside << endl;
355 }
356 else
357 {
358 cout << " FITSIOSERVER : same resolution, surprising ! " << endl;
359// exit(0); $CHECK$ Ca peut etre OK , non ?? Reza 20/11/99
360 }
361 }
362 for (int j = 0; j < sph.NbPixels(); j++) sph(j)= (double)r_8tab_[j];
363}
364
365
366void FitsIoServer::load(SphereGorski<float>& sph, char flnm[], int hdunum)
367{
368 int npixels=0;
369 int nside=0;
370
371 FITS_tab_typ_ = TFLOAT;
372
373 DVList dvl;
374 planck_read_bntbl(flnm, hdunum, npixels, dvl);
375 //dvl.Print();
376 nside= dvl.GetI("NSIDE");
377 const char* ordering= dvl.GetS("ORDERING").c_str();
378 char* ring = "'RING";
379 if ( strncmp(ordering, ring,3) != 0)
380
381 // if (ordering!="RING ")
382 {
383 cout << " numerotation non RING" << endl;
384 }
385 // number of pixels in the sphere
386 if (sph.NbPixels() != npixels)
387 {
388 cout << " found " << npixels << " pixels" << endl;
389 cout << " expected " << sph.NbPixels() << endl;
390 if (nside <= 0 )
391 {
392 cout<<" FITSIOSERVER: no resolution parameter on fits file "<<endl;
393 throw IOExc("FitsIoServer::load(SphericalMap<float>& ," + (string)flnm + ", ) Error No resolution parameter !");
394// exit(0);
395 }
396 if (nside != sph.SizeIndex())
397 {
398 sph.Resize(nside);
399 cout << " resizing the sphere to nside= " << nside << endl;
400 }
401 else
402 {
403 cout << " FITSIOSERVER : same resolution, surprising ! " << endl;
404// exit(0); $CHECK$ - Il ne faut pas sortir, me semble-t-il , Reza 20/11/99
405 }
406 }
407 // cout << " fin relecture debut transfert" << endl;
408 for (int j = 0; j < sph.NbPixels(); j++) sph(j)= (float)r_4tab_[j];
409}
410void FitsIoServer::load(SphereGorski<double>& sph, char flnm[], int hdunum)
411{
412 int npixels=0;
413 int nside=0;
414
415 FITS_tab_typ_ = TDOUBLE;
416
417 DVList dvl;
418 planck_read_bntbl(flnm, hdunum, npixels, dvl);
419 //dvl.Print();
420 nside= dvl.GetI("NSIDE");
421 const char* ordering= dvl.GetS("ORDERING").c_str();
422 char* ring = "'RING";
423 if ( strncmp(ordering, ring,3) != 0)
424
425 // if (ordering!="RING ")
426 {
427 cout << " numerotation non RING" << endl;
428 }
429 // number of pixels in the sphere
430 if (sph.NbPixels() != npixels)
431 {
432 cout << " found " << npixels << " pixels" << endl;
433 cout << " expected " << sph.NbPixels() << endl;
434 if (nside <= 0 )
435 {
436 cout<<" FITSIOSERVER: no resolution parameter on fits file "<<endl;
437 throw IOExc("FitsIoServer::load(SphereGorski<double>& ," + (string)flnm + ", ) No resol parameter !");
438// exit(0);
439 }
440 if (nside != sph.SizeIndex())
441 {
442 sph.Resize(nside);
443 cout << " resizing the sphere to nside= " << nside << endl;
444 }
445 else
446 {
447 cout << " FITSIOSERVER : same resolution, surprising ! " << endl;
448// exit(0); $CHECK$ - ne pas sortir , Reza 20/11/99
449 }
450 }
451 for (int j = 0; j < sph.NbPixels(); j++) sph(j)= (double)r_8tab_[j];
452}
453
454void FitsIoServer::load(SphericalMap<float>& sph, char flnm[])
455{
456 int npixels=0;
457 int nside=0;
458 long naxis;
459 int n1, n2, n3;
460 DVList dvl;
461
462 FITS_tab_typ_ = TFLOAT;
463
464 planck_read_img(flnm, naxis, n1, n2, n3, dvl);
465 if (naxis != 1)
466 {
467 cout << " le fichier fits n'est pas une sphere, naxis= " << naxis << endl;
468 }
469 npixels=n1;
470 nside= dvl.GetI("NSIDE");
471
472 // number of pixels in the sphere
473 if (sph.NbPixels() != npixels)
474 {
475 cout << " found " << npixels << " pixels" << endl;
476 cout << " expected " << sph.NbPixels() << endl;
477 if (nside <= 0 )
478 {
479 cout<<" FITSIOSERVER: no resolution parameter on fits file "<<endl;
480 throw IOExc("FitsIoServer::load(SphericalMap<float>& ," + (string)flnm + ") No resolution param !");
481// exit(0);
482 }
483 if (nside != sph.SizeIndex())
484 {
485 sph.Resize(nside);
486 cout << " resizing the sphere to nside= " << nside << endl;
487 }
488 else
489 {
490 cout << " FITSIOSERVER : same resolution, surprising ! " << endl;
491// exit(0); $CHECK$ - Ne pas sortir , Reza 20/11/99
492 }
493 }
494 for (int j = 0; j < sph.NbPixels(); j++) sph(j)= (float)r_4tab_[j];
495}
496
497void FitsIoServer::load(LocalMap<double>& lcm, char flnm[])
498{
499 int nbrows=0;
500 int nbcols=0;
501 FITS_tab_typ_ = TDOUBLE;
502 long naxis;
503 int n1, n2, n3;
504 DVList dvl;
505 planck_read_img(flnm, naxis, n1, n2, n3, dvl);
506
507 nbrows=n1;
508 nbcols=n2;
509 if (naxis != 2)
510 {
511 cout<<" FitsIOServer : le fichier fits n'est pas une localmap, naxis= "<<naxis<<endl;
512 }
513 float theta0 = dvl.GetD("THETA0");
514 float phi0 = dvl.GetD("PHI0");
515 int x0 = dvl.GetI("X0");
516 int y0 = dvl.GetI("Y0");
517 int xo = dvl.GetI("XO");
518 int yo = dvl.GetI("YO");
519 float anglex=dvl.GetD("ANGLEX");
520 float angley=dvl.GetD("ANGLEY");
521 float angle = dvl.GetD("ANGLE");
522
523 // number of components
524
525 if (lcm.Size_x() != nbrows || lcm.Size_y() != nbcols )
526 {
527 cout << " found " << nbrows << " x-pixels ";
528 cout << " expected " << lcm.Size_x() << endl;
529 cout << " found " << nbcols << " y-pixels " ;
530 cout << " expected " << lcm.Size_y() << endl;
531 lcm.ReSize(nbrows,nbcols);
532 cout << " resizing the map to x-size= " << nbrows << " y-size= " << nbcols << endl;
533 }
534 lcm.SetSize(anglex, angley);
535 lcm.SetOrigin(theta0, phi0, x0, y0, angle);
536 int ij=0;
537 for (int j=0; j< nbcols; j++)
538 for (int i = 0; i < nbrows; i++) lcm(i,j) = (double)r_8tab_[ij++];
539}
540
541void FitsIoServer::load(ImageR4& DpcImg,char flnm[])
542{
543 FITS_tab_typ_ = TFLOAT;
544 long naxis;
545 int siz_x;
546 int siz_y;
547 int n3;
548 DVList dvl;
549
550 planck_read_img(flnm, naxis, siz_x, siz_y, n3, dvl);
551
552
553 if (naxis != 2)
554 {
555 cout << " FitsIOServer : naxis= " << naxis << " not equal to 2 ?" << endl;
556 }
557
558 DpcImg.Allocate(siz_x, siz_y, 0., 0);
559 float* pixelPtr=DpcImg.ImagePtr();
560 // verifications de type
561 PBaseDataTypes dataT=DataType((r_4)0);
562 int TypeDonnees=dvl.GetI("DATATYPE");
563 if (int(dataT) != TypeDonnees)
564 {
565 cout << " FitsIOServer : parameter DATATYPE on file " << flnm << " is not float " << endl;
566 cout << " eventual conversion to float is achieved by cfitsio lib " << endl;
567 }
568
569 memcpy(pixelPtr, r_4tab_, siz_x*siz_y*DataSize(dataT));
570 const char* nom=dvl.GetS("NAME").c_str();
571 DpcImg.SetNameId(dvl.GetI("IDENT"), nom);
572 DpcImg.SetOrg(dvl.GetI("ORG_X"), dvl.GetI("ORG_Y"));
573 DpcImg.SetPxSize(dvl.GetD("PIXSZ_X"), dvl.GetD("PIXSZ_Y"));
574 DpcImg.SetAtt(dvl.GetI("NBNUL"), dvl.GetI("NBSAT"),
575 dvl.GetD("MINPIX"), dvl.GetD("MAXPIX"), dvl.GetD("MOYPIX"),
576 dvl.GetD("SIGPIX"),dvl.GetD("FOND"), dvl.GetD("SIGFON"));
577
578}
579
580
581void FitsIoServer::load(ImageI4& DpcImg,char flnm[])
582{
583 FITS_tab_typ_ = TINT;
584 long naxis;
585 int siz_x;
586 int siz_y;
587 int n3;
588 DVList dvl;
589
590 // pointer to the FITS file, defined in fitsio.h
591 //fitsfile *fptr;
592 // initialize status before calling fitsio routines
593 //int status = 0;
594
595 //fits_open_file(&fptr, flnm, READONLY, &status);
596 //if( status ) printerror( status );
597 planck_read_img(flnm, naxis, siz_x, siz_y, n3, dvl);
598 // close the file
599 //fits_close_file(fptr, &status);
600 //if( status ) printerror( status );
601
602 if (naxis != 2)
603 {
604 cout << " FitsIOServer : naxis= " << naxis << " not equal to 2 ?" << endl;
605 }
606
607 DpcImg.Allocate(siz_x, siz_y, 0., 0);
608 int_4* pixelPtr=DpcImg.ImagePtr();
609
610
611 // verifications de type
612 PBaseDataTypes dataT=DataType((int_4)0);
613 int TypeDonnees=dvl.GetI("DATATYPE");
614 if (int(dataT) != TypeDonnees)
615 {
616 cout << " FitsIOServer : parameter DATATYPE on file " << flnm << " is not int_4 " << endl;
617 cout << " eventual conversion to int_4 is achieved by cfitsio lib " << endl;
618 }
619 memcpy(pixelPtr, i_4tab_, siz_x*siz_y*DataSize(dataT));
620 const char* nom=dvl.GetS("NAME").c_str();
621 DpcImg.SetNameId(dvl.GetI("IDENT"), nom);
622 DpcImg.SetOrg(dvl.GetI("ORG_X"), dvl.GetI("ORG_Y"));
623 DpcImg.SetPxSize(dvl.GetD("PIXSZ_X"), dvl.GetD("PIXSZ_Y"));
624 DpcImg.SetAtt(dvl.GetI("NBNUL"), dvl.GetI("NBSAT"),
625 dvl.GetD("MINPIX"), dvl.GetD("MAXPIX"), dvl.GetD("MOYPIX"),
626 dvl.GetD("SIGPIX"),dvl.GetD("FOND"), dvl.GetD("SIGFON"));
627}
628
629
630void FitsIoServer::save( TMatrix<double>& mat, char filename[])
631{
632 int nbrows = mat.NRows();
633 int nbcols = mat.NCols();
634 long naxis = nbcols > 1 ? 2 : 1;
635 cout << " nombre de lignes : " << nbrows << " colonnes " << nbcols << endl;
636 FITS_tab_typ_ = TDOUBLE;
637 if (r_8tab_ != NULL ) { delete[] r_8tab_; r_8tab_ = NULL; }
638 r_8tab_=new r_8[nbrows*nbcols];
639
640
641 int ij=0;
642 for (int j=0; j< nbcols; j++)
643 for (int i = 0; i < nbrows; i++) r_8tab_[ij++]= (r_8)mat(i,j);
644
645 DVList dvl;
646
647 planck_write_img(filename, naxis, nbrows, nbcols, 0, dvl);
648 delete[] r_8tab_; r_8tab_ = NULL;
649}
650
651void FitsIoServer::save( TMatrix<float>& mat, char filename[])
652{
653 int nbrows = mat.NRows();
654 int nbcols = mat.NCols();
655 long naxis = nbcols > 1 ? 2 : 1;
656 cout << " nombre de lignes : " << nbrows << " colonnes " << nbcols << endl;
657 FITS_tab_typ_ = TFLOAT;
658 if (r_4tab_ != NULL ) { delete[] r_4tab_; r_4tab_ = NULL; }
659 r_4tab_=new r_4[nbrows*nbcols];
660
661
662 int ij=0;
663 for (int j=0; j< nbcols; j++)
664 for (int i = 0; i < nbrows; i++) r_4tab_[ij++]= (r_4)mat(i,j);
665
666 DVList dvl;
667
668 planck_write_img(filename, naxis, nbrows, nbcols, 0, dvl);
669 delete[] r_4tab_; r_4tab_ = NULL;
670}
671
672void FitsIoServer::save( TMatrix<int_4>& mat, char filename[])
673{
674 int nbrows = mat.NRows();
675 int nbcols = mat.NCols();
676 long naxis = nbcols > 1 ? 2 : 1;
677 cout << " nombre de lignes : " << nbrows << " colonnes " << nbcols << endl;
678 FITS_tab_typ_ = TINT;
679 if (i_4tab_ != NULL ) { delete[] i_4tab_; i_4tab_ = NULL; }
680 i_4tab_=new int_4[nbrows*nbcols];
681
682
683 int ij=0;
684 for (int j=0; j< nbcols; j++)
685 for (int i = 0; i < nbrows; i++) i_4tab_[ij++]= (int_4)mat(i,j);
686
687 DVList dvl;
688
689 planck_write_img(filename, naxis, nbrows, nbcols, 0, dvl);
690 delete[] i_4tab_; i_4tab_ = NULL;
691}
692
693
694void FitsIoServer::save(NTuple& ntpl,char flnm[])
695
696 //****************************************************/
697 //* read the elements of the NTuple ntpl, and create */
698 //* a FITS file with a binary table extension */
699 //****************************************************/
700{
701
702 // delete old file if it already exists
703 remove(flnm);
704
705 // pointer to the FITS file, defined in fitsio.h
706 fitsfile *fptr;
707 int status = 0;
708 if( fits_create_file(&fptr, flnm, &status) )
709 printerror( status );
710
711 // table will have tfields columns
712 int tfields= ntpl.NVar();
713
714 // table will have nrows rows
715 int nrows= ntpl.NEntry();
716
717 // extension name
718 char extname[] = "NTuple_Binary_tbl";
719
720 // define the name, and the datatype for the tfields columns
721 char **ttype, **tform;
722 ttype= new char*[tfields];
723 tform= new char*[tfields];
724 for(int i = 0; i < tfields; i++)
725 {
726 ttype[i]= new char[FLEN_VALUE];
727 strcpy(ttype[i], ntpl.NomIndex(i));
728 tform[i]= new char[FLEN_VALUE];
729 strcpy(tform[i], "1E");
730 }
731
732 // create a new empty binary table onto the FITS file
733 // physical units if they exist, are defined in the DVList object
734 // so the null pointer is given for the tunit parameters.
735 if ( fits_create_tbl(fptr,BINARY_TBL,nrows,tfields,ttype,tform,
736 NULL,extname,&status) )
737 printerror( status );
738
739 for( int ii = 0; ii < tfields; ii++)
740 {
741 delete [] ttype[ii];
742 delete [] tform[ii];
743 }
744 delete [] ttype;
745 delete [] tform;
746
747
748 // first row in table to write
749 int firstrow = 1;
750
751 // first element in row (ignored in ASCII tables)
752 int firstelem = 1;
753
754 for(int i = 0; i < tfields; i++)
755 {
756 float *dens= new float[nrows];
757 for(int j = 0; j < nrows; j++)
758 {
759 dens[j]= ntpl.GetVal(j,i);
760 }
761 fits_write_col(fptr,TFLOAT,i+1,firstrow,firstelem,nrows,dens,&status);
762 delete []dens;
763 }
764
765 // number of blocks per event
766 int blk= ntpl.BLock();
767 fits_write_key(fptr,TINT,"BLK",&blk,"number of blocks per evt",&status);
768
769 // get names and values from the join DVList object
770 DVList dvl= ntpl.Info();
771 dvl.Print();
772 DVList::ValList::const_iterator it;
773 for(it = dvl.Begin(); it != dvl.End(); it++)
774 {
775 char keytype= (*it).second.typ;
776 char keyname[9]="";
777 strncpy(keyname,(*it).first.substr(0,64).c_str(),8);
778 char comment[FLEN_COMMENT]="";
779
780 switch (keytype)
781 {
782 case 'I' :
783 {
784 int ival=(*it).second.mtv.iv;
785 strcpy(comment,"I entier");
786 fits_write_key(fptr,TINT,keyname,&ival,comment,&status);
787 break;
788 }
789 case 'D' :
790 {
791 double dval=(*it).second.mtv.dv;
792 strcpy(comment,"D double");
793 fits_write_key(fptr,TDOUBLE,keyname,&dval,comment,&status);
794 break;
795 }
796 case 'S' :
797 {
798 char strval[]="";
799 strcpy(strval,(*it).second.mtv.strv);
800 strcpy(comment,"S character string");
801 fits_write_key(fptr,TSTRING,keyname,&strval,comment,&status);
802 break;
803 }
804 }
805 }
806
807 //close the FITS file
808 if ( fits_close_file(fptr, &status) )
809 printerror( status );
810 return;
811}
812
813
814
815
816//void FitsIoServer::save(SphericalMap<double>& sph, char filename[])
817void FitsIoServer::save(SphericalMap<double>& sph, char filename[])
818{
819 int npixels = sph.NbPixels();
820 FITS_tab_typ_ = TDOUBLE;
821 if (r_8tab_ != NULL ) { delete[] r_8tab_; r_8tab_ = NULL; }
822 r_8tab_=new r_8[npixels];
823
824
825 for (int j = 0; j < npixels; j++) r_8tab_[j]= (r_8)sph(j);
826 DVList dvl;
827 dvl["NSIDE"] = (int_4) sph.SizeIndex();
828 dvl["ORDERING"]=sph.TypeOfMap();
829
830 planck_write_img(filename, 1, npixels, 0, 0, dvl);
831
832 // decider ulterieurement ce qu'on fait de ce qui suit, specifique
833 // pour l'instant, aux spheres gorski.
834
835 /*
836 // write supplementary keywords
837 fits_write_comment(fptr, " ", &status);
838 if( status ) printerror( status );
839
840
841 strcpy(comment,"HEALPIX Pixelisation");
842 strcpy(svalue, "HEALPIX");
843 fits_write_key(fptr, TSTRING, "PIXTYPE", svalue, comment, &status);
844 if( status ) printerror( status );
845
846
847 strcpy(comment,"pixel ordering scheme, either RING or NESTED");
848 strcpy(svalue, "RING");
849 fits_write_key(fptr, TSTRING, "ORDERING", svalue, comment, &status);
850 if( status ) printerror( status );
851
852
853 strcpy(comment,"Random generator seed");
854 int iseed= sph.iseed();
855 fits_write_key(fptr, TINT, "RANDSEED", &iseed, comment, &status);
856 if( status ) printerror( status );
857
858
859 strcpy(comment,"--------------------------------------------");
860 fits_write_comment(fptr, comment, &status);
861 if( status ) printerror( status );
862
863 strcpy(comment," Above keywords are still likely to change !");
864 fits_write_comment(fptr, comment, &status);
865 if( status ) printerror( status );
866
867 strcpy(comment,"--------------------------------------------");
868 fits_write_comment(fptr, comment, &status);
869 if( status ) printerror( status );
870
871 */
872
873 delete[] r_8tab_; r_8tab_ = NULL;
874}
875
876void FitsIoServer::save(SphericalMap<float>& sph, char filename[])
877{
878 int npixels = sph.NbPixels();
879 FITS_tab_typ_ = TFLOAT;
880 if (r_4tab_ != NULL ) { delete[] r_4tab_; r_4tab_ = NULL; }
881 r_4tab_=new r_4[npixels];
882
883
884 for (int j = 0; j < npixels; j++) r_4tab_[j]= (r_4)sph(j);
885 DVList dvl;
886 dvl["NSIDE"] = (int_4)sph.SizeIndex();
887 dvl["ORDERING"]=sph.TypeOfMap();
888
889 planck_write_img(filename, 1, npixels, 0, 0, dvl);
890 delete[] r_4tab_; r_4tab_ = NULL;
891
892}
893void FitsIoServer::save(SphereGorski<float>& sph, char filename[])
894{
895 int npixels = sph.NbPixels();
896 FITS_tab_typ_ = TFLOAT;
897 if (r_4tab_ != NULL ) { delete[] r_4tab_; r_4tab_ = NULL; }
898 r_4tab_=new r_4[npixels];
899
900
901 for (int j = 0; j < npixels; j++) r_4tab_[j]= (r_4)sph(j);
902 DVList dvl;
903 dvl.SetS("PIXTYPE","HEALPIX ");
904 dvl["ORDERING"]=sph.TypeOfMap();
905 dvl["NSIDE"] = (int_4)sph.SizeIndex();
906 dvl["FIRSTPIX"]=0;
907 dvl["LASTPIX"]=npixels-1;
908 char* typeOfContent="TEMPERATURE";
909 char* extname="SIMULATION";
910 char* comment1=" Sky Map Pixelisation Specific Keywords";
911 planck_write_bntbl(filename, npixels, typeOfContent, extname, comment1, dvl);
912 delete[] r_4tab_; r_4tab_ = NULL;
913
914}
915void FitsIoServer::save(SphereGorski<double>& sph, char filename[])
916{
917 int npixels = sph.NbPixels();
918 FITS_tab_typ_ = TDOUBLE;
919 if (r_8tab_ != NULL ) { delete[] r_8tab_; r_8tab_ = NULL; }
920 r_8tab_=new r_8[npixels];
921
922
923 for (int j = 0; j < npixels; j++) r_8tab_[j]= (r_8)sph(j);
924 DVList dvl;
925 dvl.SetS("PIXTYPE","HEALPIX ");
926 dvl["ORDERING"]=sph.TypeOfMap();
927 dvl["NSIDE"] = (int_4)sph.SizeIndex();
928 dvl["FIRSTPIX"]=0;
929 dvl["LASTPIX"]=npixels-1;
930 char* typeOfContent="TEMPERATURE";
931 char* extname="SIMULATION";
932 char* comment1=" Sky Map Pixelisation Specific Keywords";
933 planck_write_bntbl(filename, npixels, typeOfContent, extname, comment1, dvl);
934 delete[] r_8tab_; r_8tab_ = NULL;
935
936}
937
938void FitsIoServer::save(LocalMap<double>& locm, char filename[])
939{
940 int nbrows = locm.Size_x();
941 int nbcols = locm.Size_y();
942 long naxis = 2;
943 cout << " nombre de pts en x : " << nbrows << " en y " << nbcols << endl;
944 FITS_tab_typ_ = TDOUBLE;
945 if (r_8tab_ != NULL ) { delete[] r_8tab_; r_8tab_ = NULL; }
946 r_8tab_=new r_8[nbrows*nbcols];
947
948 int ij=0;
949 for (int j=0; j< nbcols; j++)
950 for (int i = 0; i < nbrows; i++) r_8tab_[ij++]= (r_8)locm(i,j);
951
952 DVList dvl;
953 dvl["NSIDE"] = (int_4) locm.SizeIndex();
954 dvl["ORDERING"]=locm.TypeOfMap();
955 double theta0;
956 double phi0;
957 int x0;
958 int y0;
959 double angle;
960 locm.Origin(theta0,phi0,x0,y0,angle);
961 double anglex;
962 double angley;
963 locm.Aperture(anglex,angley);
964 dvl["THETA0"] = theta0;
965 dvl["PHI0"] = phi0;
966 dvl["X0"] = (int_4)x0;
967 dvl["Y0"] = (int_4)y0;
968 dvl["ANGLE"] = angle;
969 dvl["ANGLEX"] = anglex;
970 dvl["ANGLEY"] = angley;
971 planck_write_img(filename, naxis, nbrows, nbcols, 0, dvl);
972 delete[] r_8tab_; r_8tab_ = NULL;
973}
974
975
976void FitsIoServer::save(const ImageR4& DpcImg,char flnm[])
977{
978 long naxis=2;
979 int siz_x = DpcImg.XSize();
980 int siz_y = DpcImg.YSize();
981 int nbpixels = siz_x*siz_y;
982 FITS_tab_typ_ = TFLOAT;
983
984
985
986 // write FITS image
987 DVList dvl;
988
989 dvl["DATATYPE"] = (int_4)DpcImg.PixelType();
990 dvl["ORG_X"] = DpcImg.XOrg();
991 dvl["ORG_Y"] = DpcImg.YOrg();
992 dvl["PIXSZ_X"] = DpcImg.XPxSize();
993 dvl["PIXSZ_Y"] = DpcImg.YPxSize();
994 dvl["IDENT"] = DpcImg.Ident();
995
996 //dvl["NAME"] = DpcImg.Nom();
997
998 // j utilise la methode SetS parce que ses parametres sont const et
999 // que l'argument DpcImg est const dans la presente method.
1000 // (dans dvlist, l'operateur surcharge [] renvoie MuTyV&, ce qui
1001 // est non const et provoque un warning sur mac (CodeWarrior)
1002 dvl.SetS("NAME",DpcImg.Nom());
1003
1004 dvl["NBSAT"] = DpcImg.nbSat;
1005 dvl["NBNUL"] = DpcImg.nbNul;
1006 dvl["MINPIX"] = DpcImg.minPix;
1007 dvl["MAXPIX"] = DpcImg.maxPix;
1008 dvl["MOYPIX"] = DpcImg.moyPix;
1009 dvl["SIGPIX"] = DpcImg.sigPix;
1010 dvl["FOND"] = DpcImg.fond;
1011 dvl["SIGFON"] = DpcImg.sigmaFond;
1012
1013 // get the values of the DpcImage
1014 if (r_4tab_ != NULL) { delete [] r_4tab_; r_4tab_ = NULL; }
1015 r_4tab_=new r_4[siz_x*siz_y];
1016 PBaseDataTypes dataT=DataType((r_4)0);
1017 memcpy( r_4tab_, DpcImg.ImagePtr(), siz_x*siz_y*DataSize(dataT));
1018 planck_write_img(flnm, naxis, siz_x, siz_y, 0, dvl);
1019
1020
1021 delete [] r_4tab_; r_4tab_ = NULL;
1022}
1023
1024
1025void FitsIoServer::save(const ImageI4& DpcImg,char flnm[])
1026{
1027 long naxis=2;
1028 int siz_x = DpcImg.XSize();
1029 int siz_y = DpcImg.YSize();
1030 int nbpixels = siz_x*siz_y;
1031 FITS_tab_typ_ = TINT;
1032
1033 // pointer to the FITS file, defined in fitsio.h
1034 //fitsfile *fptr;
1035
1036 // initialize status before calling fitsio routines
1037 //int status = 0;
1038
1039 // delete old file if it already exists
1040 //remove(flnm);
1041
1042 // create new FITS file
1043 //fits_create_file(&fptr, flnm, &status);
1044 //if( status ) printerror( status );
1045
1046
1047 // write FITS image
1048 DVList dvl;
1049
1050 dvl["DATATYPE"] = (int_4)DpcImg.PixelType();
1051 dvl["ORG_X"] = DpcImg.XOrg();
1052 dvl["ORG_Y"] = DpcImg.YOrg();
1053 dvl["PIXSZ_X"] = DpcImg.XPxSize();
1054 dvl["PIXSZ_Y"] = DpcImg.YPxSize();
1055 dvl["IDENT"] = DpcImg.Ident();
1056
1057 //dvl["NAME"] = DpcImg.Nom();
1058 // j utilise la methode SetS parce que ses parametres sont const et
1059 // que l'argument DpcImg est const dans la presente method.
1060 // (dans dvlist, l'operateur surcharge [] renvoie MuTyV&, ce qui
1061 // est non const et provoque un warning sur mac (CodeWarrior)
1062 dvl.SetS("NAME",DpcImg.Nom());
1063
1064 dvl["NBSAT"] = DpcImg.nbSat;
1065 dvl["NBNUL"] = DpcImg.nbNul;
1066 dvl["MINPIX"] = DpcImg.minPix;
1067 dvl["MAXPIX"] = DpcImg.maxPix;
1068 dvl["MOYPIX"] = DpcImg.moyPix;
1069 dvl["SIGPIX"] = DpcImg.sigPix;
1070 dvl["FOND"] = DpcImg.fond;
1071 dvl["SIGFON"] = DpcImg.sigmaFond;
1072
1073 // get the values of the DpcImage
1074 if (i_4tab_ != NULL) { delete [] i_4tab_; i_4tab_ = NULL; }
1075 i_4tab_=new int_4[siz_x*siz_y];
1076 PBaseDataTypes dataT=DataType((int_4)0);
1077 memcpy( i_4tab_, DpcImg.ImagePtr(), siz_x*siz_y*DataSize(dataT));
1078
1079
1080 planck_write_img(flnm, naxis, siz_x, siz_y, 0, dvl);
1081
1082 // close the file
1083 //fits_close_file(fptr, &status);
1084 //if( status ) printerror( status );
1085
1086 delete [] i_4tab_; i_4tab_ = NULL;
1087}
1088
1089
1090
1091
1092void FitsIoServer::planck_write_img(char flnm[], int naxis,int n1, int n2, int n3, DVList& dvl)
1093{
1094
1095
1096 // pointer to the FITS file, defined in fitsio.h
1097 fitsfile *fptr;
1098
1099 // initialize status before calling fitsio routines
1100 int status = 0;
1101
1102 // delete old file if it already exists
1103 remove(flnm);
1104
1105 // create new FITS file
1106 fits_create_file(&fptr, flnm, &status);
1107 if( status ) printerror( status );
1108
1109 int bitpix=0;
1110 if ( FITS_tab_typ_ == TDOUBLE) bitpix = DOUBLE_IMG;
1111 if ( FITS_tab_typ_ == TFLOAT) bitpix = FLOAT_IMG;
1112 if ( FITS_tab_typ_ == TINT) bitpix = LONG_IMG;
1113 long naxes[3];
1114 naxes[0] = n1;
1115 naxes[1] = n2;
1116 naxes[2] = n3;
1117 if (n2 > 0 && naxis < 2) cout << " FitsIoServer:: n2 = " << n2 << " seems to be incompatible with naxis = " << naxis << endl;
1118 if (n3 > 0 && naxis < 3) cout << " FitsIoServer:: n3 = " << n3 << " seems to be incompatible with naxis = " << naxis << endl;
1119 fits_create_img(fptr, bitpix, naxis, naxes, &status);
1120 if( status ) printerror( status );
1121
1122
1123
1124 long nelements= naxes[0];
1125 if (naxis >=2) nelements*=naxes[1];
1126 if (naxis == 3) nelements*=naxes[2];
1127 switch (FITS_tab_typ_)
1128 {
1129 case TDOUBLE :
1130 fits_write_img(fptr, TDOUBLE, 1, nelements, r_8tab_, &status);
1131 if( status ) printerror( status );
1132 break;
1133 case TFLOAT :
1134 fits_write_img(fptr, TFLOAT, 1, nelements, r_4tab_, &status);
1135 if( status ) printerror( status );
1136 break;
1137 case TINT :
1138 fits_write_img(fptr, TINT, 1, nelements, i_4tab_, &status);
1139 if( status ) printerror( status );
1140 break;
1141 default :
1142 cout << " FitsIOServer : type de tableau non traite en ecriture " << endl;
1143 break;
1144 }
1145
1146 // write the current date
1147 fits_write_date(fptr, &status);
1148 if( status ) printerror( status );
1149 // on convient d ecrire apres la date, les mots cles utilisateur.
1150 // la date servira de repere pour relire ces mots cles.
1151
1152 //dvl.Print();
1153 DVList::ValList::const_iterator it;
1154 for(it = dvl.Begin(); it != dvl.End(); it++)
1155 {
1156 int datatype= key_type_PL2FITS( (*it).second.typ);
1157 char keyname[]="";
1158 int flen_keyword = 9;
1159 // FLEN_KEYWORD est la longueur max d'un mot-cle. Il doit y avoir une
1160 // erreur dans la librairie fits qui donne FLEN_KEYWORD=72
1161 // contrairement a la notice qui donne FLEN_KEYWORD=9 (ch. 5, p.39)
1162 strncpy(keyname, (*it).first.substr(0,64).c_str(),flen_keyword-1);
1163 char comment[FLEN_COMMENT]="";
1164 int ival=0;
1165 double dval=0.;
1166 char strval[]="";
1167 switch (datatype)
1168 {
1169 case TINT :
1170 ival=(*it).second.mtv.iv;
1171 strcpy(comment,"I entier");
1172 fits_write_key(fptr, datatype, keyname, &ival, comment, &status);
1173 break;
1174 case TDOUBLE :
1175 dval=(*it).second.mtv.dv;
1176 strcpy(comment,"D double");
1177 fits_write_key(fptr, datatype, keyname, &dval, comment, &status);
1178 break;
1179 case TSTRING :
1180 strcpy(strval, (*it).second.mtv.strv);
1181 strcpy(comment,"S character string");
1182 fits_write_key(fptr, datatype, keyname, &strval, comment, &status);
1183 break;
1184 default :
1185 cout << " FitsIOServer : probleme dans type mot cle optionnel" << endl;
1186 break;
1187 }
1188 if( status ) printerror( status );
1189 }
1190 // close the file
1191 fits_close_file(fptr, &status);
1192 if( status ) printerror( status );
1193}
1194
1195void FitsIoServer::planck_write_bntbl(char flnm[], int npixels, char typeOfContent[], char extname[], char comment1[], DVList& dvl)
1196{
1197
1198
1199 // pointer to the FITS file, defined in fitsio.h
1200 fitsfile *fptr;
1201
1202 // initialize status before calling fitsio routines
1203 int status = 0;
1204
1205 // delete old file if it already exists
1206 remove(flnm);
1207
1208 // create new FITS file
1209 fits_create_file(&fptr, flnm, &status);
1210 if( status ) printerror( status );
1211
1212 // primary header
1213 int bitpix=LONG_IMG;
1214 int naxis=0;
1215 fits_create_img(fptr, bitpix, naxis, NULL, &status);
1216 // write the current date
1217 fits_write_date(fptr, &status);
1218 if( status ) printerror( status );
1219
1220
1221 long nrows=npixels/1024;
1222 if (nrows==0) nrows=1;
1223 if (npixels%1024 !=0) nrows++;
1224 int tfields=1;
1225 char **ttype, **tform;
1226 ttype= new char*[tfields];
1227 tform= new char*[tfields];
1228 char* format;
1229 if ( FITS_tab_typ_ == TDOUBLE) format="1024D";
1230 if ( FITS_tab_typ_ == TFLOAT) format="1024E";
1231 if ( FITS_tab_typ_ == TINT) format="1024I";
1232 for(int i = 0; i < tfields; i++)
1233 {
1234 ttype[i]= new char[FLEN_VALUE];
1235 strcpy(ttype[i], typeOfContent);
1236 tform[i]= new char[FLEN_VALUE];
1237 strcpy(tform[i], format);
1238 }
1239 // create a new empty binary table onto the FITS file
1240 // physical units if they exist, are defined in the DVList object
1241 // so the null pointer is given for the tunit parameters.
1242 fits_create_tbl(fptr,BINARY_TBL,nrows,tfields,ttype,tform,
1243 NULL,extname,&status);
1244 if( status ) printerror( status );
1245 for( int ii = 0; ii < tfields; ii++)
1246 {
1247 delete [] ttype[ii];
1248 delete [] tform[ii];
1249 }
1250 delete [] ttype;
1251 delete [] tform;
1252 long nelements= npixels;
1253 switch (FITS_tab_typ_)
1254 {
1255 case TDOUBLE :
1256 fits_write_col(fptr, TDOUBLE, 1, 1, 1, nelements, r_8tab_, &status);
1257 if( status ) printerror( status, "probleme dans ecriture du tableau de doubles" );
1258 break;
1259
1260
1261 case TFLOAT :
1262 for (int kk=0; kk<10; kk++) cout << r_4tab_[kk] << endl;
1263 fits_write_col(fptr, TFLOAT, 1, 1, 1, nelements, r_4tab_, &status);
1264 if( status ) printerror( status );
1265 break;
1266 case TINT :
1267 fits_write_col(fptr, TINT, 1, 1, 1, nelements, i_4tab_, &status);
1268 if( status ) printerror( status );
1269 break;
1270 default :
1271 cout << " FitsIOServer : type de tableau non traite en ecriture " << endl;
1272 break;
1273 }
1274 // write supplementary keywords
1275 fits_write_comment(fptr, " ", &status);
1276 fits_write_comment(fptr, " ", &status);
1277 if( status ) printerror( status );
1278 fits_write_comment(fptr,"--------------------------------------------", &status);
1279 fits_write_comment(fptr, comment1, &status);
1280 fits_write_comment(fptr,"--------------------------------------------", &status);
1281 if( status ) printerror( status );
1282
1283
1284 int flen_keyword = 9;
1285 // FLEN_KEYWORD est la longueur max d'un mot-cle. Il doit y avoir une
1286 // erreur dans la librairie fits qui donne FLEN_KEYWORD=72
1287 // contrairement a la notice qui donne FLEN_KEYWORD=9 (ch. 5, p.39)
1288 DVList::ValList::const_iterator it;
1289 for(it = dvl.Begin(); it != dvl.End(); it++)
1290 {
1291 int datatype= key_type_PL2FITS( (*it).second.typ);
1292 char keyname[]="";
1293 strncpy(keyname, (*it).first.substr(0,64).c_str(),flen_keyword-1);
1294 char comment[FLEN_COMMENT]="";
1295 int ival=0;
1296 double dval=0.;
1297 char strval[]="";
1298 switch (datatype)
1299 {
1300 case TINT :
1301 ival=(*it).second.mtv.iv;
1302 strcpy(comment," ");
1303 fits_write_key(fptr, datatype, keyname, &ival, comment, &status);
1304 break;
1305 case TDOUBLE :
1306 dval=(*it).second.mtv.dv;
1307 strcpy(comment," ");
1308 fits_write_key(fptr, datatype, keyname, &dval, comment, &status);
1309 break;
1310 case TSTRING :
1311 strcpy(strval, (*it).second.mtv.strv);
1312 strcpy(comment," ");
1313 fits_write_key(fptr, datatype, keyname, &strval, comment, &status);
1314 break;
1315 default :
1316 cout << " FitsIOServer : probleme dans type mot cle optionnel" << endl;
1317 break;
1318 }
1319 if( status ) printerror( status );
1320 }
1321 //char keyname[]="";
1322 //char strval[]="";
1323 //char comment[FLEN_COMMENT]="";
1324 //strncpy(keyname, "CREATOR",flen_keyword-1);
1325 //strcpy(strval, "SOPHYA");
1326 //strcpy(comment," Orsay");
1327 //fits_write_key(fptr, TSTRING, keyname, &strval, comment, &status);
1328 //if( status ) printerror( status );
1329 // close the file
1330 fits_close_file(fptr, &status);
1331 if( status ) printerror( status );
1332}
1333
1334void FitsIoServer::planck_read_img(char flnm[], long& naxis,int& n1, int& n2, int& n3, DVList& dvl)
1335{
1336 int status=0;
1337 long bitpix;
1338 long naxes[3]={0,0,0};
1339 char* comment=NULL;
1340
1341 // pointer to the FITS file, defined in fitsio.h
1342 fitsfile *fptr;
1343 // initialize status before calling fitsio routines
1344 fits_open_file(&fptr, flnm, READONLY, &status);
1345 if( status ) printerror( status );
1346
1347
1348 fits_read_key_lng(fptr, "BITPIX", &bitpix, comment, &status);
1349 if( status ) printerror( status );
1350 fits_read_key_lng(fptr, "NAXIS", &naxis, comment, &status);
1351 if( status ) printerror( status );
1352 int nfound;
1353 int nkeys=(int)naxis;
1354 fits_read_keys_lng(fptr, "NAXIS", 1, nkeys, naxes, &nfound, &status);
1355 if( status ) printerror( status );
1356
1357 n1 = naxes[0] ;
1358 n2 = naxes[1] ;
1359 n3 = naxes[2] ;
1360
1361
1362 long nelements= naxes[0];
1363 if (naxis >=2) nelements*=naxes[1];
1364 if (naxis == 3) nelements*=naxes[2];
1365 int anynull;
1366 r_8 dnullval=0.;
1367 r_4 fnullval=0.;
1368 int_4 inullval=0;
1369 // on laisse a fits le soin de convertir le type du tableau lu vers
1370 // le type suppose par l'utilisateur de fitsioserver
1371 //
1372 switch ( FITS_tab_typ_)
1373 {
1374 case TDOUBLE :
1375 if (bitpix != DOUBLE_IMG)
1376 {
1377 cout << " FitsIOServer : the data type on fits file " << flnm << " is not double, "
1378 << " conversion to double will be achieved by cfitsio lib " << endl;
1379 }
1380 if (r_8tab_ != NULL) { delete [] r_8tab_; r_8tab_ = NULL; }
1381 r_8tab_=new r_8[nelements];
1382 fits_read_img(fptr, TDOUBLE, 1, nelements, &dnullval, r_8tab_,
1383 &anynull, &status);
1384 if( status ) printerror( status );
1385 break;
1386 case TFLOAT :
1387 if (bitpix != FLOAT_IMG)
1388 {
1389 cout << " FitsIOServer : the data type on fits file " << flnm << " is not float, "
1390 << " conversion to float will be achieved by cfitsio lib " << endl;
1391 }
1392 if (r_4tab_ != NULL) { delete [] r_4tab_; r_4tab_ = NULL; }
1393 r_4tab_=new r_4[nelements];
1394 fits_read_img(fptr, TFLOAT, 1, nelements, &fnullval, r_4tab_,
1395 &anynull, &status);
1396 if( status ) printerror( status );
1397 break;
1398
1399
1400 case TINT :
1401 if (bitpix != LONG_IMG)
1402 {
1403 cout << " FitsIOServer : the data type on fits file " << flnm << " is not long, "
1404 << " conversion to long will be achieved by cfitsio lib " << endl;
1405 }
1406 if (i_4tab_ != NULL) { delete [] i_4tab_; i_4tab_ = NULL; }
1407 i_4tab_=new int_4[nelements];
1408 fits_read_img(fptr, TINT, 1, nelements, &inullval, i_4tab_,
1409 &anynull, &status);
1410 if( status ) printerror( status );
1411 break;
1412
1413
1414 default :
1415 cout << " FitsIOServer::read_img : type non traite: " << FITS_tab_typ_ << endl;
1416 break;
1417 }
1418 status = 0;
1419 char card[FLEN_CARD];
1420 int num = 0;
1421 char comment2[FLEN_COMMENT] = "x";
1422 char keyname[]= "";
1423 char datekey[]= "DATE";
1424 char endkey[] = "END";
1425 char typ='x';
1426 int ival;
1427 double dval;
1428 char strval[]="";
1429 // on a convenu que les mots cles utilisateur sont apres le mot cle DATE
1430 // on va jusqu'au mot cle DATE
1431 int flen_keyword = 9;
1432 // FLEN_KEYWORD est la longueur max d'un mot-cle. Il doit y avoir une
1433 // erreur dans la librairie fits qui donne FLEN_KEYWORD=72
1434 // contrairement a la notice qui donne FLEN_KEYWORD=9 (ch. 5, p.39)
1435 while (status == 0 && strncmp(keyname, datekey,4) != 0 )
1436 {
1437 num++;
1438 fits_read_record(fptr, num, card, &status);
1439 strncpy(keyname,card,flen_keyword-1);
1440 }
1441 if (status != 0 )
1442 {
1443 cout << " fitsio::planck_read_img : erreur, mot cle DATE absent " << endl;
1444 }
1445 // on recupere la liste des mots-cles utilisateurs
1446 while (status == 0)
1447 {
1448 num++;
1449 // on lit un record pour recuperer le nom du mot-cle
1450 fits_read_record(fptr, num, card, &status);
1451 strncpy(keyname,card,flen_keyword-1);
1452 char value[FLEN_VALUE];
1453 // on recupere le premier caractere du commentaire, qui contient
1454 // le code du type de la valeur
1455 // (tant que ce n est pas le mot cle END)
1456 fits_read_keyword(fptr, keyname, value, comment2, &status);
1457 if ( strncmp(keyname, endkey,flen_keyword-1) != 0)
1458 {
1459 typ = comment2[0];
1460 // quand le type est connu, on lit la valeur
1461 switch (typ)
1462 {
1463 case 'I' :
1464 fits_read_key(fptr, TINT, keyname, &ival, comment2, &status);
1465 if( status ) printerror( status );
1466 strip (keyname, 'B',' ');
1467 dvl[keyname] = (int_4)ival;
1468 break;
1469 case 'D' :
1470 fits_read_key(fptr, TDOUBLE, keyname, &dval, comment2, &status);
1471 if( status ) printerror( status );
1472 strip (keyname, 'B',' ');
1473 dvl[keyname] = dval;
1474 break;
1475 case 'S' :
1476 fits_read_key(fptr, TSTRING, keyname, strval, comment2, &status);
1477 if( status ) printerror( status );
1478 strip (keyname, 'B',' ');
1479 strip(strval, 'B',' ');
1480 dvl[keyname]=strval;
1481 break;
1482 default :
1483 cout << " FITSIOSERVER::planck_read_img : type de donnee non prevu " << endl;
1484 break;
1485 }
1486 }
1487 }
1488
1489
1490 // close the file
1491 status=0;
1492 fits_close_file(fptr, &status);
1493 if( status ) printerror( status );
1494}
1495
1496
1497void FitsIoServer::planck_read_bntbl(char flnm[], int hdunum, int& npixels, DVList& dvl)
1498{
1499 int status=0;
1500 int nkeys,keypos;
1501 int hdutype;
1502 int tfields;
1503 int datype;
1504 long lastpix;
1505 long repeat, width;
1506 long nrows;
1507 long extend;
1508 char* comment=NULL;
1509
1510 // pointer to the FITS file, defined in fitsio.h
1511 fitsfile *fptr;
1512 // initialize status before calling fitsio routines
1513 fits_open_file(&fptr, flnm, READONLY, &status);
1514 if( status ) printerror( status );
1515 fits_read_key_lng(fptr, "EXTEND", &extend, comment, &status);
1516 if( status ) printerror( status );
1517 if (extend!=1)
1518 {
1519 cout << "FitsIoServer:: le fichier fits ne contient pas d'extension binary table" << endl;
1520 throw IOExc("FitsIoServer::planck_read_bntbl(" + (string)flnm + ") Error No bin table extension !");
1521// return;
1522 }
1523 fits_movabs_hdu(fptr, hdunum,&hdutype,&status);
1524 if( status ) printerror( status );
1525 if (hdutype!=BINARY_TBL)
1526 {
1527 cout << "FitsIoServer:: this HDU is not a binary table " << endl;
1528 throw IOExc("FitsIoServer::planck_read_bntbl(" + (string)flnm + ") Error Not a bin table (1) !");
1529// exit(status);
1530 }
1531 char xtension[FLEN_VALUE];
1532 fits_read_key_str(fptr,"XTENSION",xtension,NULL,&status);
1533 if( status ) printerror( status );
1534
1535 char binta[] = "BINTABLE";
1536 if ( strncmp(xtension, binta,8) != 0)
1537 // if (xtension !="BINTABLE")
1538 {
1539 cout << "FitsIoServer:: not a binary table " << endl;
1540 throw IOExc("FitsIoServer::planck_read_bntbl(" + (string)flnm + ") Error Not a bin table (2) !");
1541// exit(status);
1542 }
1543 fits_get_hdrpos(fptr,&nkeys,&keypos,&status);
1544 if( status ) printerror( status );
1545 //cout << " nombre de mots-cles : " << nkeys << endl;
1546 fits_get_num_cols(fptr, &tfields, &status);
1547 if (tfields != 1)
1548 {
1549 cout << "FitsIoServer:: il y a plus d'une colonne" << endl;
1550 throw IOExc("FitsIoServer::planck_read_bntbl(" + (string)flnm + ") Error >1 column !");
1551// return;
1552 }
1553 fits_get_num_rows(fptr, &nrows, &status);
1554 //cout << "nblignes= " << nrows << endl;
1555 fits_get_coltype(fptr, 1, &datype, &repeat, &width, &status);
1556 if( status ) printerror( status );
1557 //cout << " type de donnees : " << datype << endl;
1558 //cout << " repeat : " << repeat << endl;
1559 //cout << " width : " << width << endl;
1560 fits_read_key_lng(fptr, "LASTPIX", &lastpix, comment, &status);
1561 if( status ) printerror( status," mot cle LASTPIX" );
1562
1563 long nelements= nrows*repeat;
1564 if (nelements!=lastpix+1)
1565 {
1566 cout << " erreur sur longueur du vecteur " << endl;
1567 cout << " nelements= " << nelements << " lastpix+1=" << lastpix+1 << endl;
1568 }
1569 npixels=nelements;
1570 int anynull;
1571 r_8 dnullval=0.;
1572 r_4 fnullval=0.;
1573 int_4 inullval=0;
1574 // on laisse a fits le soin de convertir le type du tableau lu vers
1575 // le type suppose par l'utilisateur de fitsioserver
1576 //
1577 switch ( FITS_tab_typ_)
1578 {
1579 case TDOUBLE :
1580 if (datype != TDOUBLE)
1581 {
1582 cout << " FitsIOServer : the data type on fits file " << flnm << " is not double, "
1583 << " conversion to double will be achieved by cfitsio lib " << endl;
1584 }
1585 if (r_8tab_ != NULL) { delete [] r_8tab_; r_8tab_ = NULL; }
1586 r_8tab_=new r_8[ npixels];
1587 fits_read_col(fptr, TDOUBLE, 1, 1, 1, nelements, &dnullval,
1588 r_8tab_,
1589 &anynull, &status);
1590 if( status ) printerror( status, "probleme dans lecture du tableau de doubles" );
1591 break;
1592 case TFLOAT :
1593 if (datype != TFLOAT)
1594 {
1595
1596 cout << " FitsIOServer : the data type on fits file " << flnm << " is not float, "
1597 << " conversion to float will be achieved by cfitsio lib " << endl;
1598 }
1599 if (r_4tab_ != NULL) { delete [] r_4tab_; r_4tab_ = NULL; }
1600 r_4tab_=new r_4[nelements];
1601 fits_read_col(fptr, TFLOAT, 1, 1, 1, nelements, &fnullval,
1602 r_4tab_, &anynull, &status);
1603 if( status ) printerror( status,"probleme dans lecture du tableau de floats" );
1604 break;
1605
1606
1607 case TINT :
1608 if (datype != TLONG)
1609 {
1610 cout << " FitsIOServer : the data type on fits file " << flnm << " is not long, "
1611 << " conversion to long will be achieved by cfitsio lib " << endl;
1612 }
1613 if (i_4tab_ != NULL) { delete [] i_4tab_; i_4tab_ = NULL; }
1614 i_4tab_=new int_4[nelements];
1615 fits_read_col(fptr, TLONG, 1, 1, 1, nelements, &inullval,
1616 i_4tab_, &anynull, &status);
1617 //fits_read_img(fptr, TINT, 1, nelements, &inullval, i_4tab_,
1618 // &anynull, &status);
1619 if( status ) printerror( status,"probleme dans lecture du tableau de ints" );
1620 break;
1621
1622
1623 default :
1624 cout << " FitsIOServer::read_bntbl : type non traite: " << FITS_tab_typ_ << endl;
1625 break;
1626 }
1627 char card[FLEN_CARD];
1628 char keyname[LEN_KEYWORD]= "";
1629 char strval[FLEN_VALUE];
1630 char comment1[FLEN_COMMENT];
1631 char dtype;
1632 //char bidon[LEN_KEYWORD];
1633 char comkey[] = "COMMENT";
1634
1635 for(int j = 1; j <= nkeys; j++)
1636 {
1637 // fits_read_record(fptr, j, card, &status);
1638 // strncpy(keyname,card,LEN_KEYWORD-1);
1639 // cout << " bidon= " << keyname << endl;
1640 // if ( strncmp(keyname,comkey ,LEN_KEYWORD-1) != 0)
1641 fits_read_keyn(fptr,j,card,strval,comment1,&status);
1642 strncpy(keyname,card,LEN_KEYWORD-1);
1643
1644 if ( strncmp(keyname,comkey ,LEN_KEYWORD-1) != 0)
1645 {
1646 fits_get_keytype(strval,&dtype,&status);
1647 // cout<<" keyname= "<< keyname <<" dtype= "<<dtype <<endl;
1648 strip (keyname, 'B',' ');
1649 strip(strval, 'B',' ');
1650 switch( dtype )
1651 {
1652 case 'C':
1653 dvl[keyname]= strval;
1654 break;
1655 case 'I':
1656 int ival;
1657 ctoi(strval,&ival);
1658 dvl[keyname]= (int_4)ival;
1659 break;
1660 case 'F':
1661 double dval;
1662 ctof(strval,&dval);
1663 dvl[keyname]= dval;
1664 break;
1665 default :
1666 cout << " FitsIOServer : mot-cle bizarre " << endl;
1667 break;
1668 }
1669 }
1670 }
1671
1672 // close the file
1673 status=0;
1674 fits_close_file(fptr, &status);
1675 if( status ) printerror( status );
1676}
1677
1678
1679// projects a SphericalMap<double>, according sinus-method, and saves onto
1680// a FITS-file
1681void FitsIoServer::sinus_picture_projection(SphericalMap<double>& sph, char filename[])
1682{
1683
1684 long naxes[2]={600, 300};
1685 float* map =new float[ 600*300 ];
1686 int npixels= naxes[0]*naxes[1];
1687
1688 cout << " image FITS en projection SINUS" << endl;
1689 // table will have npixels rows
1690 for(int j=0; j < npixels; j++) map[j]=0.;
1691 for(int j=0; j<naxes[1]; j++)
1692 {
1693 double yd = (j+0.5)/naxes[1]-0.5;
1694 double theta = (0.5-yd)*Pi;
1695 double facteur=1./sin(theta);
1696 for(int i=0; i<naxes[0]; i++)
1697 {
1698 double xa = (i+0.5)/naxes[0]-0.5;
1699 double phi = 2.*Pi*xa*facteur+Pi;
1700 float th=float(theta);
1701 float ff=float(phi);
1702 if (phi<2*Pi && phi>= 0)
1703 {
1704 map[j*naxes[0]+i] = sph.PixValSph(th, ff);
1705 }
1706 }
1707 }
1708
1709 write_picture(naxes, map, filename);
1710 delete [] map;
1711}
1712
1713// projects a SphericalMap<double>, according sinus-method, and saves onto
1714// a FITS-file
1715void FitsIoServer::sinus_picture_projection(SphericalMap<float>& sph, char filename[])
1716{
1717 // le code de cete methode duplique celui de la precedente, la seule
1718 //difference etant le type de sphere en entree. Ces methodes de projection
1719 // sont provisoires, et ne servent que pour les tests. C est pourquoi je
1720 // ne me suis pas casse la tete, pour l instant
1721
1722 long naxes[2]={600, 300};
1723 float* map = new float[ 600*300 ];
1724 int npixels= naxes[0]*naxes[1];
1725
1726 cout << " image FITS en projection SINUS" << endl;
1727 // table will have npixels rows
1728 for(int j=0; j < npixels; j++) map[j]=0.;
1729 for(int j=0; j<naxes[1]; j++)
1730 {
1731 double yd = (j+0.5)/naxes[1]-0.5;
1732 double theta = (0.5-yd)*Pi;
1733 double facteur=1./sin(theta);
1734 for(int i=0; i<naxes[0]; i++)
1735 {
1736 double xa = (i+0.5)/naxes[0]-0.5;
1737 double phi = 2.*Pi*xa*facteur+Pi;
1738 float th=float(theta);
1739 float ff=float(phi);
1740 if (phi<2*Pi && phi>= 0)
1741 {
1742 map[j*naxes[0]+i] = sph.PixValSph(th, ff);
1743 }
1744 }
1745 }
1746
1747 write_picture(naxes, map, filename);
1748 delete [] map;
1749
1750}
1751
1752// projects a SphericalMap<float>, according Mollweide-method, and saves onto
1753// a FITS-file
1754void FitsIoServer::Mollweide_picture_projection(SphericalMap<float>& sph, char filename[])
1755{
1756 // le code de cete methode duplique celui de la precedente, la seule
1757 //difference etant le type de sphere en entree. Ces methodes de projection
1758 // sont provisoires, et ne servent que pour les tests. C est pourquoi je
1759 // ne me suis pas casse la tete, pour l instant
1760
1761 long naxes[2]={600, 300};
1762 float* map = new float[ 600*300 ];
1763 int npixels= naxes[0]*naxes[1];
1764
1765 cout << " image FITS en projection MOLLWEIDE" << endl;
1766 // table will have npixels rows
1767 for(int j=0; j < npixels; j++) map[j]=0.;
1768 for(int j=0; j<naxes[1]; j++)
1769 {
1770 double yd = (j+0.5)/naxes[1]-0.5;
1771 double facteur=2.*Pi/sin(acos(yd*2));
1772 double theta = (0.5-yd)*Pi;
1773 for(int i=0; i<naxes[0]; i++)
1774 {
1775 double xa = (i+0.5)/naxes[0]-0.5;
1776 double phi = xa*facteur+Pi;
1777 float th=float(theta);
1778 float ff=float(phi);
1779 if (phi<2*Pi && phi>= 0)
1780 {
1781 map[j*naxes[0]+i] = sph.PixValSph(th, ff);
1782 }
1783 }
1784 }
1785
1786 write_picture(naxes, map, filename);
1787 delete [] map;
1788
1789}
1790// projects a SphericalMap<double>, according Mollweide-method, and saves onto
1791// a FITS-file
1792void FitsIoServer::Mollweide_picture_projection(SphericalMap<double>& sph, char filename[])
1793{
1794 // le code de cete methode duplique celui de la precedente, la seule
1795 //difference etant le type de sphere en entree. Ces methodes de projection
1796 // sont provisoires, et ne servent que pour les tests. C est pourquoi je
1797 // ne me suis pas casse la tete, pour l instant
1798
1799 long naxes[2]={600, 300};
1800 float* map = new float[ 600*300 ];
1801 int npixels= naxes[0]*naxes[1];
1802
1803 cout << " image FITS en projection MOLLWEIDE" << endl;
1804 // table will have npixels rows
1805 for(int j=0; j < npixels; j++) map[j]=0.;
1806 for(int j=0; j<naxes[1]; j++)
1807 {
1808 double yd = (j+0.5)/naxes[1]-0.5;
1809 double facteur=2.*Pi/sin(acos(yd*2));
1810 double theta = (0.5-yd)*Pi;
1811 for(int i=0; i<naxes[0]; i++)
1812 {
1813 double xa = (i+0.5)/naxes[0]-0.5;
1814 double phi = xa*facteur+Pi;
1815 float th=float(theta);
1816 float ff=float(phi);
1817 if (phi<2*Pi && phi>= 0)
1818 {
1819 map[j*naxes[0]+i] = sph.PixValSph(th, ff);
1820 }
1821 }
1822 }
1823
1824 write_picture(naxes, map, filename);
1825 delete [] map;
1826
1827}
1828
1829
1830
1831// saves a (LocalMap<double> on a FITS-file in order to be visualized
1832// (for tests)
1833void FitsIoServer::picture(LocalMap<double>& lcm, char filename[])
1834{
1835
1836 long naxes[2];
1837 naxes[0] = lcm.Size_x();
1838 naxes[1] = lcm.Size_y();
1839 int npixels= naxes[0]*naxes[1];
1840 float* map = new float[npixels];
1841
1842 // table will have npixels rows
1843 for(int j=0; j < npixels; j++) map[j]=0.;
1844 for(int j=0; j<naxes[1]; j++)
1845 {
1846 for(int i=0; i<naxes[0]; i++)
1847 {
1848 map[j*naxes[0]+i] = lcm(i, j);
1849 }
1850 }
1851
1852 write_picture(naxes, map, filename);
1853 delete [] map;
1854}
1855
1856
1857
1858void FitsIoServer::write_picture(long* naxes, float* map, char* filename) const
1859{
1860
1861 int bitpix = FLOAT_IMG;
1862 long naxis = 2;
1863
1864 //pointer to the FITS file, defined in fitsio.h
1865 fitsfile *fptr;
1866 // delete old file if it already exists
1867 remove(filename);
1868 // initialize status before calling fitsio routines
1869 int status = 0;
1870
1871 // create new FITS file
1872 fits_create_file(&fptr, filename, &status);
1873 if( status ) printerror( status );
1874
1875 // write the required header keywords
1876 fits_create_img(fptr, bitpix, naxis, naxes, &status);
1877 if( status ) printerror( status );
1878
1879 // write the current date
1880 fits_write_date(fptr, &status);
1881 if( status ) printerror( status );
1882
1883
1884 // first row in table to write
1885 long firstrow = 1;
1886 // first element in row
1887 long firstelem = 1;
1888 int colnum = 1;
1889 int nelements=naxes[0]*naxes[1];
1890 fits_write_img(fptr, TFLOAT, firstelem, nelements, map, &status);
1891 if( status ) printerror( status );
1892
1893 // close the file
1894 fits_close_file(fptr, &status);
1895 if( status ) printerror( status );
1896}
1897
1898
1899bool FitsIoServer::check_keyword(fitsfile *fptr,int nkeys,char keyword[])
1900
1901 //*****************************************************/
1902 //* check if the specified keyword exits in the CHU */
1903 //*****************************************************/
1904{
1905
1906 bool KEY_EXIST = false;
1907 int status = 0;
1908 char strbide[FLEN_VALUE];
1909 char keybide[LEN_KEYWORD]= "";
1910 for(int jj = 1; jj <= nkeys; jj++)
1911 {
1912 if( fits_read_keyn(fptr,jj,keybide,strbide,NULL,&status) )
1913 printerror( status );
1914 if( !strcmp(keybide,keyword) )
1915 {
1916 KEY_EXIST= true;
1917 break;
1918 }
1919 }
1920 return(KEY_EXIST);
1921}
1922
1923void FitsIoServer::readheader ( char filename[] )
1924
1925 //**********************************************************************/
1926 //* Print out all the header keywords in all extensions of a FITS file */
1927 //**********************************************************************/
1928{
1929
1930 // standard string lengths defined in fitsioc.h
1931 char card[FLEN_CARD];
1932
1933 // pointer to the FITS file, defined in fitsio.h
1934 fitsfile *fptr;
1935
1936 int status = 0;
1937 if ( fits_open_file(&fptr, filename, READONLY, &status) )
1938 printerror( status );
1939
1940 // attempt to move to next HDU, until we get an EOF error
1941 int hdutype;
1942 for (int ii = 1; !(fits_movabs_hdu(fptr,ii,&hdutype,&status));ii++)
1943 {
1944 if (hdutype == ASCII_TBL)
1945 printf("\nReading ASCII table in HDU %d:\n", ii);
1946 else if (hdutype == BINARY_TBL)
1947 printf("\nReading binary table in HDU %d:\n", ii);
1948 else if (hdutype == IMAGE_HDU)
1949 printf("\nReading FITS image in HDU %d:\n", ii);
1950 else
1951 {
1952 printf("Error: unknown type of this HDU \n");
1953 printerror( status );
1954 }
1955
1956 // get the number of keywords
1957 int nkeys, keypos;
1958 if ( fits_get_hdrpos(fptr, &nkeys, &keypos, &status) )
1959 printerror( status );
1960
1961 printf("Header listing for HDU #%d:\n", ii);
1962 for (int jj = 1; jj <= nkeys; jj++)
1963 {
1964 if ( fits_read_record(fptr, jj, card, &status) )
1965 printerror( status );
1966
1967 // print the keyword card
1968 printf("%s\n", card);
1969 }
1970 printf("END\n\n");
1971 }
1972
1973 // got the expected EOF error; reset = 0
1974 if (status == END_OF_FILE)
1975 status = 0;
1976 else
1977 printerror( status );
1978
1979 if ( fits_close_file(fptr, &status) )
1980 printerror( status );
1981
1982 return;
1983}
1984
1985void FitsIoServer::printerror(int& status) const
1986
1987 //*****************************************************/
1988 //* Print out cfitsio error messages and exit program */
1989 //*****************************************************/
1990{
1991
1992 // print out cfitsio error messages and exit program
1993 // print error report
1994 fits_report_error(stderr, status);
1995 // terminate the program, returning error status
1996 // exit( status );
1997 status=0;
1998}
1999void FitsIoServer::printerror(int& status, char* texte) const
2000
2001 //*****************************************************/
2002 //* Print out cfitsio error messages and exit program */
2003 //*****************************************************/
2004{
2005
2006 // print out cfitsio error messages and exit program
2007 // print error report
2008 fits_report_error(stderr, status);
2009 cout << " erreur : " << texte << endl;
2010 status=0;
2011}
2012
2013
2014
Note: See TracBrowser for help on using the repository browser.