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

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

quelques const et autres delete[] 21-OCT-99 GLM

File size: 35.9 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 <iostream.h>
13#include <list>
14
15#include "fitsioserver.h"
16#include "strutil.h"
17
18void FitsIoServer::load(TMatrix<double>& mat,char flnm[])
19{
20 int nbrows=0;
21 int nbcols=0;
22 FITS_tab_typ_ = TDOUBLE;
23 long naxis;
24 int n1, n2, n3;
25 DVList dvl;
26 planck_read_img(flnm, naxis, n1, n2, n3, dvl);
27
28 nbrows=n1;
29 nbcols=n2;
30 if (naxis == 1) nbcols=1;
31 if (naxis > 2)
32 {
33 cout<<" FitsIOServer : le fichier fits n'est pas une matrice, naxis= " <<naxis<< endl;
34 }
35
36 // number of components
37 if (mat.NRows() != nbrows || mat.NCols() != nbcols )
38 {
39 cout << " found " << nbrows << " rows ";
40 cout << " expected " << mat.NRows() << endl;
41 cout << " found " << nbcols << " columns " ;
42 cout << " expected " << mat.NCols() << endl;
43 mat.ReSize(nbrows,nbcols);
44 cout << " resize the vector to nbrows= " << nbrows << " nbcols= " << nbcols << endl;
45 }
46 int ij=0;
47 for (int j=0; j< nbcols; j++)
48 for (int i = 0; i < nbrows; i++) mat(i,j) = (double)r_8tab_[ij++];
49}
50
51void FitsIoServer::load(NTuple& ntpl,char flnm[],int hdunum)
52
53 //********************************************************/
54 //* move to the HDU which has the specified number hdunum*/
55 //* in the FITS file, perform read operations and write */
56 //* the elements in an NTuple. */
57 //********************************************************/
58{
59
60 // pointer to the FITS file, defined in fitsio.h
61 fitsfile *fptr;
62 int status = 0;
63 if( fits_open_file(&fptr,flnm,READONLY,&status) )
64 printerror( status );
65
66 // move to the HDU
67 int hdutype;
68 if( fits_movabs_hdu(fptr,hdunum,&hdutype,&status) )
69 printerror( status );
70
71 // get number of keywords
72 int nkeys,keypos;
73 if( fits_get_hdrpos(fptr,&nkeys,&keypos,&status) )
74 printerror( status );
75
76
77 if (hdutype == BINARY_TBL)
78 printf("\nReading binary table in HDU %d:\n",hdunum);
79 else
80 {
81 printf("Error:: this HDU is not a binary table\n");
82 exit ( status );
83 }
84
85 // number of columns
86 int tfields;
87 if( fits_get_num_cols(fptr,&tfields,&status) )
88 printerror( status );
89
90 // to get table size
91 long naxis[2];
92 int nfound;
93 if( fits_read_keys_lng(fptr, "NAXIS", 1, 2, naxis, &nfound, &status) )
94 printerror( status );
95 int nrows= naxis[1];
96
97 //Information about each column
98 char **ttype, **tform;
99 ttype= new char*[tfields];
100 tform= new char*[tfields];
101 for( int ii = 0; ii < tfields; ii++)
102 {
103 ttype[ii]= new char[FLEN_VALUE];
104 tform[ii]= new char[FLEN_VALUE];
105 }
106
107 // number of TTYPEn,TFORMn and TUNITn keywords found
108 int num= 0;
109
110 // read the column names from the TTYPEn keywords
111 fits_read_keys_str(fptr,"TTYPE",1,tfields,ttype,&nfound,&status);
112 num += nfound;
113 // read the column names from the TFORMn keywords
114 fits_read_keys_str(fptr,"TFORM",1,tfields,tform,&nfound,&status);
115 num += nfound;
116
117 //for(int ii = 0; ii < tfields; ii++)
118 // printf("\nColumn name & Format %8s %8s", ttype[ii],tform[ii]);
119
120 // select the columns with float data values
121 int typecode;
122 long repeat,width;
123 list<int> column;
124 for( int ii = 0; ii < tfields; ii++)
125 {
126 fits_binary_tform(tform[ii],&typecode, &repeat, &width, &status);
127 //printf("\n%4s %3d %2ld %2ld", tform[ii], typecode, repeat, width);
128 if(typecode == TFLOAT)
129 column.push_back(ii+1);
130 }
131
132 // get input elements to create the NTuple
133 char **clname;
134 clname= new char*[column.size()];
135 for(int ii = 0; ii < column.size(); ii++)
136 clname[ii]= new char[FLEN_VALUE];
137
138 list<int>::iterator itr;
139 int index= 0;
140 for(itr= column.begin(); itr != column.end(); itr++)
141 strcpy(clname[index++],ttype[*itr-1]);
142
143 for( int ii = 0; ii < tfields; ii++)
144 {
145 delete [] ttype[ii];
146 delete [] tform[ii];
147 }
148 delete [] ttype;
149 delete [] tform;
150
151
152 // check if the specified keyword BLK exists
153 int blk= 512;
154 if(check_keyword(fptr,nkeys,"BLK"))
155 {
156 if( fits_read_key(fptr,TINT,"BLK",&blk,NULL,&status) )
157 printerror( status );
158 }
159 // create a NTuple
160 NTuple nt0(column.size(),clname,blk);
161
162 for(int ii = 0; ii < column.size(); ii++)
163 delete [] clname[ii];
164 delete [] clname;
165
166 float value[1];
167 long felem = 1;
168 char strnull[10];
169 strcpy(strnull, " ");
170 int anynull= 0;
171 long longnull = 0;
172
173 // value to represent undefined array elements
174 float floatnull = FLOATNULLVALUE;
175
176 // read elements from columns and fill NTuple
177 for (int k = 0; k < nrows; k++)
178 {
179 int j= 0;
180 float *xnt= new float[column.size()];
181 list<int>::iterator itr;
182 for(itr= column.begin(); itr != column.end(); itr++)
183 {
184 fits_read_col(fptr,TFLOAT,*itr,k+1,felem,1,&floatnull,value,
185 &anynull, &status);
186 xnt[j++]= value[0];
187 }
188 nt0.Fill(xnt);
189 delete[] xnt;
190 }
191
192 // the TUNITn keywords are optional, if they exist they are put
193 // in the DVLIst object
194 char keyname[LEN_KEYWORD]= "";
195 char strval[FLEN_VALUE];
196 for(int ii = 0; ii < tfields; ii++)
197 {
198 fits_make_keyn("TUNIT",ii+1,keyname,&status);
199 if(check_keyword(fptr,nkeys,keyname))
200 {
201 num++;
202 if( fits_read_key_str(fptr,keyname,strval,NULL,&status) )
203 printerror(status);
204 strip (keyname, 'B',' ');
205 strip(strval, 'B',' ');
206 nt0.Info()[keyname]= strval;
207 }
208 }
209
210 // add the number of mandatory keywords of a binary table
211 num += 8;
212
213 // put names and values of other reserved keywords in a DVList object
214 char comment[FLEN_COMMENT];
215 char dtype;
216 for(int j = num+1; j <= nkeys; j++)
217 {
218 fits_read_keyn(fptr,j,keyname,strval,comment,&status);
219 fits_get_keytype(strval,&dtype,&status);
220 strip (keyname, 'B',' ');
221 strip(strval, 'B',' ');
222
223 switch( dtype )
224 {
225 case 'C':
226 nt0.Info()[keyname]= strval;
227 break;
228 case 'I':
229 int ival;
230 ctoi(strval,&ival);
231 nt0.Info()[keyname]= (int_4)ival;
232 break;
233 case 'F':
234 double dval;
235 ctof(strval,&dval);
236 nt0.Info()[keyname]= dval;
237 break;
238 }
239 }
240
241 // copy in the input NTuple
242 ntpl= nt0;
243
244 if( fits_close_file(fptr, &status) )
245 printerror(status);
246
247 printf("\n");
248 return;
249}
250
251
252void FitsIoServer::load(SphericalMap<double>& sph, char flnm[])
253{
254 int npixels=0;
255 int nside=0;
256 long naxis;
257 int n1, n2, n3;
258
259 FITS_tab_typ_ = TDOUBLE;
260
261 DVList dvl;
262 planck_read_img(flnm, naxis, n1, n2, n3, dvl);
263 if (naxis != 1)
264 {
265 cout << " le fichier fits n'est pas une sphere, naxis= " << naxis << endl;
266 }
267 npixels=n1;
268 nside= dvl.GetI("NSIDE");
269
270 // number of pixels in the sphere
271 if (sph.NbPixels() != npixels)
272 {
273 cout << " found " << npixels << " pixels" << endl;
274 cout << " expected " << sph.NbPixels() << endl;
275 if (nside <= 0 )
276 {
277 cout<<" FITSIOSERVER: no resolution parameter on fits file "<<endl;
278 exit(0);
279 }
280 if (nside != sph.SizeIndex())
281 {
282 sph.Resize(nside);
283 cout << " resizing the sphere to nside= " << nside << endl;
284 }
285 else
286 {
287 cout << " FITSIOSERVER : same resolution, surprising ! " << endl;
288 exit(0);
289 }
290 }
291 for (int j = 0; j < sph.NbPixels(); j++) sph(j)= (double)r_8tab_[j];
292}
293void FitsIoServer::load(SphericalMap<float>& sph, char flnm[])
294{
295 int npixels=0;
296 int nside=0;
297 long naxis;
298 int n1, n2, n3;
299 DVList dvl;
300
301 FITS_tab_typ_ = TFLOAT;
302
303 planck_read_img(flnm, naxis, n1, n2, n3, dvl);
304 if (naxis != 1)
305 {
306 cout << " le fichier fits n'est pas une sphere, naxis= " << naxis << endl;
307 }
308 npixels=n1;
309 nside= dvl.GetI("NSIDE");
310
311 // number of pixels in the sphere
312 if (sph.NbPixels() != npixels)
313 {
314 cout << " found " << npixels << " pixels" << endl;
315 cout << " expected " << sph.NbPixels() << endl;
316 if (nside <= 0 )
317 {
318 cout<<" FITSIOSERVER: no resolution parameter on fits file "<<endl;
319 exit(0);
320 }
321 if (nside != sph.SizeIndex())
322 {
323 sph.Resize(nside);
324 cout << " resizing the sphere to nside= " << nside << endl;
325 }
326 else
327 {
328 cout << " FITSIOSERVER : same resolution, surprising ! " << endl;
329 exit(0);
330 }
331 }
332 for (int j = 0; j < sph.NbPixels(); j++) sph(j)= (float)r_4tab_[j];
333}
334
335void FitsIoServer::load(LocalMap<double>& lcm, char flnm[])
336{
337 int nbrows=0;
338 int nbcols=0;
339 FITS_tab_typ_ = TDOUBLE;
340 long naxis;
341 int n1, n2, n3;
342 DVList dvl;
343 planck_read_img(flnm, naxis, n1, n2, n3, dvl);
344
345 nbrows=n1;
346 nbcols=n2;
347 if (naxis != 2)
348 {
349 cout<<" FitsIOServer : le fichier fits n'est pas une localmap, naxis= "<<naxis<<endl;
350 }
351 float theta0 = dvl.GetD("THETA0");
352 float phi0 = dvl.GetD("PHI0");
353 int x0 = dvl.GetI("X0");
354 int y0 = dvl.GetI("Y0");
355 int xo = dvl.GetI("XO");
356 int yo = dvl.GetI("YO");
357 float anglex=dvl.GetD("ANGLEX");
358 float angley=dvl.GetD("ANGLEY");
359 float angle = dvl.GetD("ANGLE");
360
361 // number of components
362
363 if (lcm.Size_x() != nbrows || lcm.Size_y() != nbcols )
364 {
365 cout << " found " << nbrows << " x-pixels ";
366 cout << " expected " << lcm.Size_x() << endl;
367 cout << " found " << nbcols << " y-pixels " ;
368 cout << " expected " << lcm.Size_y() << endl;
369 lcm.ReSize(nbrows,nbcols);
370 cout << " resizing the map to x-size= " << nbrows << " y-size= " << nbcols << endl;
371 }
372 lcm.SetSize(anglex, angley);
373 lcm.SetOrigin(theta0, phi0, x0, y0, angle);
374 int ij=0;
375 for (int j=0; j< nbcols; j++)
376 for (int i = 0; i < nbrows; i++) lcm(i,j) = (double)r_8tab_[ij++];
377}
378
379void FitsIoServer::load(ImageR4& DpcImg,char flnm[])
380{
381 FITS_tab_typ_ = TFLOAT;
382 long naxis;
383 int siz_x;
384 int siz_y;
385 int n3;
386 DVList dvl;
387
388 planck_read_img(flnm, naxis, siz_x, siz_y, n3, dvl);
389
390
391 if (naxis != 2)
392 {
393 cout << " FitsIOServer : naxis= " << naxis << " not equal to 2 ?" << endl;
394 }
395
396 DpcImg.Allocate(siz_x, siz_y, 0., 0);
397 float* pixelPtr=DpcImg.ImagePtr();
398 // verifications de type
399 PBaseDataTypes dataT=DataType((r_4)0);
400 int TypeDonnees=dvl.GetI("DATATYPE");
401 if (int(dataT) != TypeDonnees)
402 {
403 cout << " FitsIOServer : parameter DATATYPE on file " << flnm << " is not float " << endl;
404 cout << " eventual conversion to float is achieved by cfitsio lib " << endl;
405 }
406
407 memcpy(pixelPtr, r_4tab_, siz_x*siz_y*DataSize(dataT));
408 const char* nom=dvl.GetS("NAME").c_str();
409 DpcImg.SetNameId(dvl.GetI("IDENT"), nom);
410 DpcImg.SetOrg(dvl.GetI("ORG_X"), dvl.GetI("ORG_Y"));
411 DpcImg.SetPxSize(dvl.GetD("PIXSZ_X"), dvl.GetD("PIXSZ_Y"));
412 DpcImg.SetAtt(dvl.GetI("NBNUL"), dvl.GetI("NBSAT"),
413 dvl.GetD("MINPIX"), dvl.GetD("MAXPIX"), dvl.GetD("MOYPIX"),
414 dvl.GetD("SIGPIX"),dvl.GetD("FOND"), dvl.GetD("SIGFON"));
415
416}
417
418
419void FitsIoServer::load(ImageI4& DpcImg,char flnm[])
420{
421 FITS_tab_typ_ = TINT;
422 long naxis;
423 int siz_x;
424 int siz_y;
425 int n3;
426 DVList dvl;
427
428 // pointer to the FITS file, defined in fitsio.h
429 //fitsfile *fptr;
430 // initialize status before calling fitsio routines
431 //int status = 0;
432
433 //fits_open_file(&fptr, flnm, READONLY, &status);
434 //if( status ) printerror( status );
435 planck_read_img(flnm, naxis, siz_x, siz_y, n3, dvl);
436 // close the file
437 //fits_close_file(fptr, &status);
438 //if( status ) printerror( status );
439
440 if (naxis != 2)
441 {
442 cout << " FitsIOServer : naxis= " << naxis << " not equal to 2 ?" << endl;
443 }
444
445 DpcImg.Allocate(siz_x, siz_y, 0., 0);
446 int_4* pixelPtr=DpcImg.ImagePtr();
447
448
449 // verifications de type
450 PBaseDataTypes dataT=DataType((int_4)0);
451 int TypeDonnees=dvl.GetI("DATATYPE");
452 if (int(dataT) != TypeDonnees)
453 {
454 cout << " FitsIOServer : parameter DATATYPE on file " << flnm << " is not int_4 " << endl;
455 cout << " eventual conversion to int_4 is achieved by cfitsio lib " << endl;
456 }
457 memcpy(pixelPtr, i_4tab_, siz_x*siz_y*DataSize(dataT));
458 const char* nom=dvl.GetS("NAME").c_str();
459 DpcImg.SetNameId(dvl.GetI("IDENT"), nom);
460 DpcImg.SetOrg(dvl.GetI("ORG_X"), dvl.GetI("ORG_Y"));
461 DpcImg.SetPxSize(dvl.GetD("PIXSZ_X"), dvl.GetD("PIXSZ_Y"));
462 DpcImg.SetAtt(dvl.GetI("NBNUL"), dvl.GetI("NBSAT"),
463 dvl.GetD("MINPIX"), dvl.GetD("MAXPIX"), dvl.GetD("MOYPIX"),
464 dvl.GetD("SIGPIX"),dvl.GetD("FOND"), dvl.GetD("SIGFON"));
465}
466
467
468void FitsIoServer::save( TMatrix<double>& mat, char filename[])
469{
470 int nbrows = mat.NRows();
471 int nbcols = mat.NCols();
472 long naxis = nbcols > 1 ? 2 : 1;
473 cout << " nombre de lignes : " << nbrows << " colonnes " << nbcols << endl;
474 FITS_tab_typ_ = TDOUBLE;
475 if (r_8tab_ != NULL ) delete[] r_8tab_;
476 r_8tab_=new r_8[nbrows*nbcols];
477
478
479 int ij=0;
480 for (int j=0; j< nbcols; j++)
481 for (int i = 0; i < nbrows; i++) r_8tab_[ij++]= (r_8)mat(i,j);
482
483 DVList dvl;
484
485 planck_write_img(filename, naxis, nbrows, nbcols, 0, dvl);
486 delete[] r_8tab_;
487}
488
489void FitsIoServer::save(NTuple& ntpl,char flnm[])
490
491 //****************************************************/
492 //* read the elements of the NTuple ntpl, and create */
493 //* a FITS file with a binary table extension */
494 //****************************************************/
495{
496
497 // delete old file if it already exists
498 remove(flnm);
499
500 // pointer to the FITS file, defined in fitsio.h
501 fitsfile *fptr;
502 int status = 0;
503 if( fits_create_file(&fptr, flnm, &status) )
504 printerror( status );
505
506 // table will have tfields columns
507 int tfields= ntpl.NVar();
508
509 // table will have nrows rows
510 int nrows= ntpl.NEntry();
511
512 // extension name
513 char extname[] = "NTuple_Binary_tbl";
514
515 // define the name, and the datatype for the tfields columns
516 char **ttype, **tform;
517 ttype= new char*[tfields];
518 tform= new char*[tfields];
519 for(int i = 0; i < tfields; i++)
520 {
521 ttype[i]= new char[FLEN_VALUE];
522 strcpy(ttype[i], ntpl.NomIndex(i));
523 tform[i]= new char[FLEN_VALUE];
524 strcpy(tform[i], "1E");
525 }
526
527 // create a new empty binary table onto the FITS file
528 // physical units if they exist, are defined in the DVList object
529 // so the null pointer is given for the tunit parameters.
530 if ( fits_create_tbl(fptr,BINARY_TBL,nrows,tfields,ttype,tform,
531 NULL,extname,&status) )
532 printerror( status );
533
534 for( int ii = 0; ii < tfields; ii++)
535 {
536 delete [] ttype[ii];
537 delete [] tform[ii];
538 }
539 delete [] ttype;
540 delete [] tform;
541
542
543 // first row in table to write
544 int firstrow = 1;
545
546 // first element in row (ignored in ASCII tables)
547 int firstelem = 1;
548
549 for(int i = 0; i < tfields; i++)
550 {
551 float *dens= new float[nrows];
552 for(int j = 0; j < nrows; j++)
553 {
554 dens[j]= ntpl.GetVal(j,i);
555 }
556 fits_write_col(fptr,TFLOAT,i+1,firstrow,firstelem,nrows,dens,&status);
557 delete []dens;
558 }
559
560 // number of blocks per event
561 int blk= ntpl.BLock();
562 fits_write_key(fptr,TINT,"BLK",&blk,"number of blocks per evt",&status);
563
564 // get names and values from the join DVList object
565 DVList dvl= ntpl.Info();
566 dvl.Print();
567 DVList::ValList::const_iterator it;
568 for(it = dvl.Begin(); it != dvl.End(); it++)
569 {
570 char keytype= (*it).second.typ;
571 char keyname[9]="";
572 strncpy(keyname,(*it).first.substr(0,64).c_str(),8);
573 char comment[FLEN_COMMENT]="";
574
575 switch (keytype)
576 {
577 case 'I' :
578 {
579 int ival=(*it).second.mtv.iv;
580 strcpy(comment,"I entier");
581 fits_write_key(fptr,TINT,keyname,&ival,comment,&status);
582 break;
583 }
584 case 'D' :
585 {
586 double dval=(*it).second.mtv.dv;
587 strcpy(comment,"D double");
588 fits_write_key(fptr,TDOUBLE,keyname,&dval,comment,&status);
589 break;
590 }
591 case 'S' :
592 {
593 char strval[]="";
594 strcpy(strval,(*it).second.mtv.strv);
595 strcpy(comment,"S character string");
596 fits_write_key(fptr,TSTRING,keyname,&strval,comment,&status);
597 break;
598 }
599 }
600 }
601
602 //close the FITS file
603 if ( fits_close_file(fptr, &status) )
604 printerror( status );
605 return;
606}
607
608
609
610
611//void FitsIoServer::save(SphericalMap<double>& sph, char filename[])
612void FitsIoServer::save(SphericalMap<double>& sph, char filename[])
613{
614 int npixels = sph.NbPixels();
615 FITS_tab_typ_ = TDOUBLE;
616 if (r_8tab_ != NULL ) delete[] r_8tab_;
617 r_8tab_=new r_8[npixels];
618
619
620 for (int j = 0; j < npixels; j++) r_8tab_[j]= (r_8)sph(j);
621 DVList dvl;
622 dvl["NSIDE"] = sph.SizeIndex();
623 dvl["ORDERING"]=sph.TypeOfMap();
624
625 planck_write_img(filename, 1, npixels, 0, 0, dvl);
626
627 // decider ulterieurement ce qu'on fait de ce qui suit, specifique
628 // pour l'instant, aux spheres gorski.
629
630 /*
631 // write supplementary keywords
632 fits_write_comment(fptr, " ", &status);
633 if( status ) printerror( status );
634
635
636 strcpy(comment,"HEALPIX Pixelisation");
637 strcpy(svalue, "HEALPIX");
638 fits_write_key(fptr, TSTRING, "PIXTYPE", svalue, comment, &status);
639 if( status ) printerror( status );
640
641
642 strcpy(comment,"pixel ordering scheme, either RING or NESTED");
643 strcpy(svalue, "RING");
644 fits_write_key(fptr, TSTRING, "ORDERING", svalue, comment, &status);
645 if( status ) printerror( status );
646
647
648 strcpy(comment,"Random generator seed");
649 int iseed= sph.iseed();
650 fits_write_key(fptr, TINT, "RANDSEED", &iseed, comment, &status);
651 if( status ) printerror( status );
652
653
654 strcpy(comment,"--------------------------------------------");
655 fits_write_comment(fptr, comment, &status);
656 if( status ) printerror( status );
657
658 strcpy(comment," Above keywords are still likely to change !");
659 fits_write_comment(fptr, comment, &status);
660 if( status ) printerror( status );
661
662 strcpy(comment,"--------------------------------------------");
663 fits_write_comment(fptr, comment, &status);
664 if( status ) printerror( status );
665
666 */
667
668 delete[] r_8tab_;
669}
670
671void FitsIoServer::save(SphericalMap<float>& sph, char filename[])
672{
673 int npixels = sph.NbPixels();
674 FITS_tab_typ_ = TFLOAT;
675 if (r_4tab_ != NULL ) delete[] r_4tab_;
676 r_4tab_=new r_4[npixels];
677
678
679 for (int j = 0; j < npixels; j++) r_4tab_[j]= (r_4)sph(j);
680 DVList dvl;
681 dvl["NSIDE"] = sph.SizeIndex();
682 dvl["ORDERING"]=sph.TypeOfMap();
683
684 planck_write_img(filename, 1, npixels, 0, 0, dvl);
685 delete[] r_4tab_;
686
687}
688
689void FitsIoServer::save(LocalMap<double>& locm, char filename[])
690{
691 int nbrows = locm.Size_x();
692 int nbcols = locm.Size_y();
693 long naxis = 2;
694 cout << " nombre de pts en x : " << nbrows << " en y " << nbcols << endl;
695 FITS_tab_typ_ = TDOUBLE;
696 if (r_8tab_ != NULL ) delete[] r_8tab_;
697 r_8tab_=new r_8[nbrows*nbcols];
698
699 int ij=0;
700 for (int j=0; j< nbcols; j++)
701 for (int i = 0; i < nbrows; i++) r_8tab_[ij++]= (r_8)locm(i,j);
702
703 DVList dvl;
704 dvl["NSIDE"] = locm.SizeIndex();
705 dvl["ORDERING"]=locm.TypeOfMap();
706 double theta0;
707 double phi0;
708 int x0;
709 int y0;
710 double angle;
711 locm.Origin(theta0,phi0,x0,y0,angle);
712 double anglex;
713 double angley;
714 locm.Aperture(anglex,angley);
715 dvl["THETA0"] = theta0;
716 dvl["PHI0"] = phi0;
717 dvl["X0"] = (int_4)x0;
718 dvl["Y0"] = (int_4)y0;
719 dvl["ANGLE"] = angle;
720 dvl["ANGLEX"] = anglex;
721 dvl["ANGLEY"] = angley;
722 planck_write_img(filename, naxis, nbrows, nbcols, 0, dvl);
723 delete[] r_8tab_;}
724
725
726void FitsIoServer::save(const ImageR4& DpcImg,char flnm[])
727{
728 long naxis=2;
729 int siz_x = DpcImg.XSize();
730 int siz_y = DpcImg.YSize();
731 int nbpixels = siz_x*siz_y;
732 FITS_tab_typ_ = TFLOAT;
733
734
735
736 // write FITS image
737 DVList dvl;
738
739 dvl["DATATYPE"] = (int_4)DpcImg.PixelType();
740 dvl["ORG_X"] = DpcImg.XOrg();
741 dvl["ORG_Y"] = DpcImg.YOrg();
742 dvl["PIXSZ_X"] = DpcImg.XPxSize();
743 dvl["PIXSZ_Y"] = DpcImg.YPxSize();
744 dvl["IDENT"] = DpcImg.Ident();
745 dvl["NAME"] = DpcImg.Nom();
746 dvl["NBSAT"] = DpcImg.nbSat;
747 dvl["NBNUL"] = DpcImg.nbNul;
748 dvl["MINPIX"] = DpcImg.minPix;
749 dvl["MAXPIX"] = DpcImg.maxPix;
750 dvl["MOYPIX"] = DpcImg.moyPix;
751 dvl["SIGPIX"] = DpcImg.sigPix;
752 dvl["FOND"] = DpcImg.fond;
753 dvl["SIGFON"] = DpcImg.sigmaFond;
754
755 // get the values of the DpcImage
756 if (r_4tab_ != NULL) delete [] r_4tab_;
757 r_4tab_=new r_4[siz_x*siz_y];
758 PBaseDataTypes dataT=DataType((r_4)0);
759 memcpy( r_4tab_, DpcImg.ImagePtr(), siz_x*siz_y*DataSize(dataT));
760 planck_write_img(flnm, naxis, siz_x, siz_y, 0, dvl);
761
762
763 delete [] r_4tab_;
764}
765
766
767void FitsIoServer::save(const ImageI4& DpcImg,char flnm[])
768{
769 long naxis=2;
770 int siz_x = DpcImg.XSize();
771 int siz_y = DpcImg.YSize();
772 int nbpixels = siz_x*siz_y;
773 FITS_tab_typ_ = TINT;
774
775 // pointer to the FITS file, defined in fitsio.h
776 //fitsfile *fptr;
777
778 // initialize status before calling fitsio routines
779 //int status = 0;
780
781 // delete old file if it already exists
782 //remove(flnm);
783
784 // create new FITS file
785 //fits_create_file(&fptr, flnm, &status);
786 //if( status ) printerror( status );
787
788
789 // write FITS image
790 DVList dvl;
791
792 dvl["DATATYPE"] = (int_4)DpcImg.PixelType();
793 dvl["ORG_X"] = DpcImg.XOrg();
794 dvl["ORG_Y"] = DpcImg.YOrg();
795 dvl["PIXSZ_X"] = DpcImg.XPxSize();
796 dvl["PIXSZ_Y"] = DpcImg.YPxSize();
797 dvl["IDENT"] = DpcImg.Ident();
798 dvl["NAME"] = DpcImg.Nom();
799 dvl["NBSAT"] = DpcImg.nbSat;
800 dvl["NBNUL"] = DpcImg.nbNul;
801 dvl["MINPIX"] = DpcImg.minPix;
802 dvl["MAXPIX"] = DpcImg.maxPix;
803 dvl["MOYPIX"] = DpcImg.moyPix;
804 dvl["SIGPIX"] = DpcImg.sigPix;
805 dvl["FOND"] = DpcImg.fond;
806 dvl["SIGFON"] = DpcImg.sigmaFond;
807
808 // get the values of the DpcImage
809 if (i_4tab_ != NULL) delete [] i_4tab_;
810 i_4tab_=new int_4[siz_x*siz_y];
811 PBaseDataTypes dataT=DataType((int_4)0);
812 memcpy( i_4tab_, DpcImg.ImagePtr(), siz_x*siz_y*DataSize(dataT));
813
814
815 planck_write_img(flnm, naxis, siz_x, siz_y, 0, dvl);
816
817 // close the file
818 //fits_close_file(fptr, &status);
819 //if( status ) printerror( status );
820
821 delete [] i_4tab_;
822}
823
824
825
826
827void FitsIoServer::planck_write_img(char flnm[], int naxis,int n1, int n2, int n3, DVList& dvl)
828{
829
830
831 // pointer to the FITS file, defined in fitsio.h
832 fitsfile *fptr;
833
834 // initialize status before calling fitsio routines
835 int status = 0;
836
837 // delete old file if it already exists
838 remove(flnm);
839
840 // create new FITS file
841 fits_create_file(&fptr, flnm, &status);
842 if( status ) printerror( status );
843
844 int bitpix=0;
845 if ( FITS_tab_typ_ == TDOUBLE) bitpix = DOUBLE_IMG;
846 if ( FITS_tab_typ_ == TFLOAT) bitpix = FLOAT_IMG;
847 if ( FITS_tab_typ_ == TINT) bitpix = LONG_IMG;
848 long naxes[3];
849 naxes[0] = n1;
850 naxes[1] = n2;
851 naxes[2] = n3;
852 if (n2 > 0 && naxis < 2) cout << " FitsIoServer:: n2 = " << n2 << " seems to be incompatible with naxis = " << naxis << endl;
853 if (n3 > 0 && naxis < 3) cout << " FitsIoServer:: n3 = " << n3 << " seems to be incompatible with naxis = " << naxis << endl;
854 fits_create_img(fptr, bitpix, naxis, naxes, &status);
855 if( status ) printerror( status );
856
857
858
859 long nelements= naxes[0];
860 if (naxis >=2) nelements*=naxes[1];
861 if (naxis == 3) nelements*=naxes[2];
862 switch (FITS_tab_typ_)
863 {
864 case TDOUBLE :
865 fits_write_img(fptr, TDOUBLE, 1, nelements, r_8tab_, &status);
866 if( status ) printerror( status );
867 break;
868 case TFLOAT :
869 fits_write_img(fptr, TFLOAT, 1, nelements, r_4tab_, &status);
870 if( status ) printerror( status );
871 break;
872 case TINT :
873 fits_write_img(fptr, TINT, 1, nelements, i_4tab_, &status);
874 if( status ) printerror( status );
875 break;
876 default :
877 cout << " FitsIOServer : type de tableau non traite en ecriture " << endl;
878 break;
879 }
880
881 // write the current date
882 fits_write_date(fptr, &status);
883 if( status ) printerror( status );
884 // on convient d ecrire apres la date, les mots cles utilisateur.
885 // la date servira de repere pour relire ces mots cles.
886
887 //dvl.Print();
888 DVList::ValList::const_iterator it;
889 for(it = dvl.Begin(); it != dvl.End(); it++)
890 {
891 int datatype= key_type_PL2FITS( (*it).second.typ);
892 char keyname[]="";
893 int flen_keyword = 9;
894 // FLEN_KEYWORD est la longueur max d'un mot-cle. Il doit y avoir une
895 // erreur dans la librairie fits qui donne FLEN_KEYWORD=72
896 // contrairement a la notice qui donne FLEN_KEYWORD=9 (ch. 5, p.39)
897 strncpy(keyname, (*it).first.substr(0,64).c_str(),flen_keyword-1);
898 char comment[FLEN_COMMENT]="";
899 int ival=0;
900 double dval=0.;
901 char strval[]="";
902 switch (datatype)
903 {
904 case TINT :
905 ival=(*it).second.mtv.iv;
906 strcpy(comment,"I entier");
907 fits_write_key(fptr, datatype, keyname, &ival, comment, &status);
908 break;
909 case TDOUBLE :
910 dval=(*it).second.mtv.dv;
911 strcpy(comment,"D double");
912 fits_write_key(fptr, datatype, keyname, &dval, comment, &status);
913 break;
914 case TSTRING :
915 strcpy(strval, (*it).second.mtv.strv);
916 strcpy(comment,"S character string");
917 fits_write_key(fptr, datatype, keyname, &strval, comment, &status);
918 break;
919 default :
920 cout << " FitsIOServer : probleme dans type mot cle optionnel" << endl;
921 break;
922 }
923 if( status ) printerror( status );
924 }
925 // close the file
926 fits_close_file(fptr, &status);
927 if( status ) printerror( status );
928}
929
930void FitsIoServer::planck_read_img(char flnm[], long& naxis,int& n1, int& n2, int& n3, DVList& dvl)
931{
932 int status=0;
933 long bitpix;
934 long naxes[3]={0,0,0};
935 char* comment=NULL;
936
937 // pointer to the FITS file, defined in fitsio.h
938 fitsfile *fptr;
939 // initialize status before calling fitsio routines
940 fits_open_file(&fptr, flnm, READONLY, &status);
941 if( status ) printerror( status );
942
943
944 fits_read_key_lng(fptr, "BITPIX", &bitpix, comment, &status);
945 if( status ) printerror( status );
946 fits_read_key_lng(fptr, "NAXIS", &naxis, comment, &status);
947 if( status ) printerror( status );
948 int nfound;
949 int nkeys=(int)naxis;
950 fits_read_keys_lng(fptr, "NAXIS", 1, nkeys, naxes, &nfound, &status);
951 if( status ) printerror( status );
952
953 n1 = naxes[0] ;
954 n2 = naxes[1] ;
955 n3 = naxes[2] ;
956
957
958 long nelements= naxes[0];
959 if (naxis >=2) nelements*=naxes[1];
960 if (naxis == 3) nelements*=naxes[2];
961 int anynull;
962 r_8 dnullval=0.;
963 r_4 fnullval=0.;
964 int_4 inullval=0;
965 // on laisse a fits le soin de convertir le type du tableau lu vers
966 // le type suppose par l'utilisateur de fitsioserver
967 //
968 switch ( FITS_tab_typ_)
969 {
970 case TDOUBLE :
971 if (bitpix != DOUBLE_IMG)
972 {
973 cout << " FitsIOServer : the data type on fits file " << flnm << " is not double, "
974 << " conversion to double will be achieved by cfitsio lib " << endl;
975 }
976 if (r_8tab_ != NULL) delete [] r_8tab_;
977 r_8tab_=new r_8[nelements];
978 fits_read_img(fptr, TDOUBLE, 1, nelements, &dnullval, r_8tab_,
979 &anynull, &status);
980 if( status ) printerror( status );
981 break;
982 case TFLOAT :
983 if (bitpix != FLOAT_IMG)
984 {
985 cout << " FitsIOServer : the data type on fits file " << flnm << " is not float, "
986 << " conversion to float will be achieved by cfitsio lib " << endl;
987 }
988 if (r_4tab_ != NULL) delete [] r_4tab_;
989 r_4tab_=new r_4[nelements];
990 fits_read_img(fptr, TFLOAT, 1, nelements, &fnullval, r_4tab_,
991 &anynull, &status);
992 if( status ) printerror( status );
993 break;
994
995
996 case TINT :
997 if (bitpix != LONG_IMG)
998 {
999 cout << " FitsIOServer : the data type on fits file " << flnm << " is not long, "
1000 << " conversion to long will be achieved by cfitsio lib " << endl;
1001 }
1002 if (i_4tab_ != NULL) delete [] i_4tab_;
1003 i_4tab_=new int_4[nelements];
1004 fits_read_img(fptr, TINT, 1, nelements, &inullval, i_4tab_,
1005 &anynull, &status);
1006 if( status ) printerror( status );
1007 break;
1008
1009
1010 default :
1011 cout << " FitsIOServer::read_img : type non traite: " << FITS_tab_typ_ << endl;
1012 break;
1013 }
1014 status = 0;
1015 char card[FLEN_CARD];
1016 int num = 0;
1017 char comment2[FLEN_COMMENT] = "x";
1018 char keyname[]= "";
1019 char datekey[]= "DATE";
1020 char endkey[] = "END";
1021 char typ='x';
1022 int ival;
1023 double dval;
1024 char strval[]="";
1025 // on a convenu que les mots cles utilisateur sont apres le mot cle DATE
1026 // on va jusqu'au mot cle DATE
1027 int flen_keyword = 9;
1028 // FLEN_KEYWORD est la longueur max d'un mot-cle. Il doit y avoir une
1029 // erreur dans la librairie fits qui donne FLEN_KEYWORD=72
1030 // contrairement a la notice qui donne FLEN_KEYWORD=9 (ch. 5, p.39)
1031 while (status == 0 && strncmp(keyname, datekey,4) != 0 )
1032 {
1033 num++;
1034 fits_read_record(fptr, num, card, &status);
1035 strncpy(keyname,card,flen_keyword-1);
1036 }
1037 if (status != 0 )
1038 {
1039 cout << " fitsio::planck_read_img : erreur, mot cle DATE absent " << endl;
1040 }
1041 // on recupere la liste des mots-cles utilisateurs
1042 while (status == 0)
1043 {
1044 num++;
1045 // on lit un record pour recuperer le nom du mot-cle
1046 fits_read_record(fptr, num, card, &status);
1047 strncpy(keyname,card,flen_keyword-1);
1048 char value[FLEN_VALUE];
1049 // on recupere le premier caractere du commentaire, qui contient
1050 // le code du type de la valeur
1051 // (tant que ce n est pas le mot cle END)
1052 fits_read_keyword(fptr, keyname, value, comment2, &status);
1053 if ( strncmp(keyname, endkey,flen_keyword-1) != 0)
1054 {
1055 typ = comment2[0];
1056 // quand le type est connu, on lit la valeur
1057 switch (typ)
1058 {
1059 case 'I' :
1060 fits_read_key(fptr, TINT, keyname, &ival, comment2, &status);
1061 if( status ) printerror( status );
1062 strip (keyname, 'B',' ');
1063 dvl[keyname] = (int_4)ival;
1064 break;
1065 case 'D' :
1066 fits_read_key(fptr, TDOUBLE, keyname, &dval, comment2, &status);
1067 if( status ) printerror( status );
1068 strip (keyname, 'B',' ');
1069 dvl[keyname] = dval;
1070 break;
1071 case 'S' :
1072 fits_read_key(fptr, TSTRING, keyname, strval, comment2, &status);
1073 if( status ) printerror( status );
1074 strip (keyname, 'B',' ');
1075 strip(strval, 'B',' ');
1076 dvl[keyname]=strval;
1077 break;
1078 default :
1079 cout << " FITSIOSERVER::planck_read_img : type de donnee non prevu " << endl;
1080 break;
1081 }
1082 }
1083 }
1084
1085
1086 // close the file
1087 status=0;
1088 fits_close_file(fptr, &status);
1089 if( status ) printerror( status );
1090}
1091
1092
1093
1094// projects a SphericalMap<double>, according sinus-method, and saves onto
1095// a FITS-file
1096void FitsIoServer::sinus_picture_projection(SphericalMap<double>& sph, char filename[])
1097{
1098
1099 long naxes[2]={600, 300};
1100 float* map =new float[ 600*300 ];
1101 int npixels= naxes[0]*naxes[1];
1102
1103 cout << " image FITS en projection SINUS" << endl;
1104 // table will have npixels rows
1105 for(int j=0; j < npixels; j++) map[j]=0.;
1106 for(int j=0; j<naxes[1]; j++)
1107 {
1108 double yd = (j+0.5)/naxes[1]-0.5;
1109 double theta = (0.5-yd)*Pi;
1110 double facteur=1./sin(theta);
1111 for(int i=0; i<naxes[0]; i++)
1112 {
1113 double xa = (i+0.5)/naxes[0]-0.5;
1114 double phi = 2.*Pi*xa*facteur+Pi;
1115 float th=float(theta);
1116 float ff=float(phi);
1117 if (phi<2*Pi && phi>= 0)
1118 {
1119 map[j*naxes[0]+i] = sph.PixValSph(th, ff);
1120 }
1121 }
1122 }
1123
1124 write_picture(naxes, map, filename);
1125 delete [] map;
1126}
1127
1128// projects a SphericalMap<double>, according sinus-method, and saves onto
1129// a FITS-file
1130void FitsIoServer::sinus_picture_projection(SphericalMap<float>& sph, char filename[])
1131{
1132 // le code de cete methode duplique celui de la precedente, la seule
1133 //difference etant le type de sphere en entree. Ces methodes de projection
1134 // sont provisoires, et ne servent que pour les tests. C est pourquoi je
1135 // ne me suis pas casse la tete, pour l instant
1136
1137 long naxes[2]={600, 300};
1138 float* map = new float[ 600*300 ];
1139 int npixels= naxes[0]*naxes[1];
1140
1141 cout << " image FITS en projection SINUS" << endl;
1142 // table will have npixels rows
1143 for(int j=0; j < npixels; j++) map[j]=0.;
1144 for(int j=0; j<naxes[1]; j++)
1145 {
1146 double yd = (j+0.5)/naxes[1]-0.5;
1147 double theta = (0.5-yd)*Pi;
1148 double facteur=1./sin(theta);
1149 for(int i=0; i<naxes[0]; i++)
1150 {
1151 double xa = (i+0.5)/naxes[0]-0.5;
1152 double phi = 2.*Pi*xa*facteur+Pi;
1153 float th=float(theta);
1154 float ff=float(phi);
1155 if (phi<2*Pi && phi>= 0)
1156 {
1157 map[j*naxes[0]+i] = sph.PixValSph(th, ff);
1158 }
1159 }
1160 }
1161
1162 write_picture(naxes, map, filename);
1163 delete [] map;
1164
1165}
1166
1167// saves a (LocalMap<double> on a FITS-file in order to be visualized
1168// (for tests)
1169void FitsIoServer::picture(LocalMap<double>& lcm, char filename[])
1170{
1171
1172 long naxes[2];
1173 naxes[0] = lcm.Size_x();
1174 naxes[1] = lcm.Size_y();
1175 int npixels= naxes[0]*naxes[1];
1176 float* map = new float[npixels];
1177
1178 // table will have npixels rows
1179 for(int j=0; j < npixels; j++) map[j]=0.;
1180 for(int j=0; j<naxes[1]; j++)
1181 {
1182 for(int i=0; i<naxes[0]; i++)
1183 {
1184 map[j*naxes[0]+i] = lcm(i, j);
1185 }
1186 }
1187
1188 write_picture(naxes, map, filename);
1189 delete [] map;
1190}
1191
1192
1193
1194void FitsIoServer::write_picture(long* naxes, float* map, char* filename) const
1195{
1196
1197 int bitpix = FLOAT_IMG;
1198 long naxis = 2;
1199
1200 //pointer to the FITS file, defined in fitsio.h
1201 fitsfile *fptr;
1202 // delete old file if it already exists
1203 remove(filename);
1204 // initialize status before calling fitsio routines
1205 int status = 0;
1206
1207 // create new FITS file
1208 fits_create_file(&fptr, filename, &status);
1209 if( status ) printerror( status );
1210
1211 // write the required header keywords
1212 fits_create_img(fptr, bitpix, naxis, naxes, &status);
1213 if( status ) printerror( status );
1214
1215 // write the current date
1216 fits_write_date(fptr, &status);
1217 if( status ) printerror( status );
1218
1219
1220 // first row in table to write
1221 long firstrow = 1;
1222 // first element in row
1223 long firstelem = 1;
1224 int colnum = 1;
1225 int nelements=naxes[0]*naxes[1];
1226 fits_write_img(fptr, TFLOAT, firstelem, nelements, map, &status);
1227 if( status ) printerror( status );
1228
1229 // close the file
1230 fits_close_file(fptr, &status);
1231 if( status ) printerror( status );
1232}
1233
1234
1235bool FitsIoServer::check_keyword(fitsfile *fptr,int nkeys,char keyword[])
1236
1237 //*****************************************************/
1238 //* check if the specified keyword exits in the CHU */
1239 //*****************************************************/
1240{
1241
1242 bool KEY_EXIST = false;
1243 int status = 0;
1244 char strbide[FLEN_VALUE];
1245 char keybide[LEN_KEYWORD]= "";
1246 for(int jj = 1; jj <= nkeys; jj++)
1247 {
1248 if( fits_read_keyn(fptr,jj,keybide,strbide,NULL,&status) )
1249 printerror( status );
1250 if( !strcmp(keybide,keyword) )
1251 {
1252 KEY_EXIST= true;
1253 break;
1254 }
1255 }
1256 return(KEY_EXIST);
1257}
1258
1259void FitsIoServer::readheader ( char filename[] )
1260
1261 //**********************************************************************/
1262 //* Print out all the header keywords in all extensions of a FITS file */
1263 //**********************************************************************/
1264{
1265
1266 // standard string lengths defined in fitsioc.h
1267 char card[FLEN_CARD];
1268
1269 // pointer to the FITS file, defined in fitsio.h
1270 fitsfile *fptr;
1271
1272 int status = 0;
1273 if ( fits_open_file(&fptr, filename, READONLY, &status) )
1274 printerror( status );
1275
1276 // attempt to move to next HDU, until we get an EOF error
1277 int hdutype;
1278 for (int ii = 1; !(fits_movabs_hdu(fptr,ii,&hdutype,&status));ii++)
1279 {
1280 if (hdutype == ASCII_TBL)
1281 printf("\nReading ASCII table in HDU %d:\n", ii);
1282 else if (hdutype == BINARY_TBL)
1283 printf("\nReading binary table in HDU %d:\n", ii);
1284 else if (hdutype == IMAGE_HDU)
1285 printf("\nReading FITS image in HDU %d:\n", ii);
1286 else
1287 {
1288 printf("Error: unknown type of this HDU \n");
1289 printerror( status );
1290 }
1291
1292 // get the number of keywords
1293 int nkeys, keypos;
1294 if ( fits_get_hdrpos(fptr, &nkeys, &keypos, &status) )
1295 printerror( status );
1296
1297 printf("Header listing for HDU #%d:\n", ii);
1298 for (int jj = 1; jj <= nkeys; jj++)
1299 {
1300 if ( fits_read_record(fptr, jj, card, &status) )
1301 printerror( status );
1302
1303 // print the keyword card
1304 printf("%s\n", card);
1305 }
1306 printf("END\n\n");
1307 }
1308
1309 // got the expected EOF error; reset = 0
1310 if (status == END_OF_FILE)
1311 status = 0;
1312 else
1313 printerror( status );
1314
1315 if ( fits_close_file(fptr, &status) )
1316 printerror( status );
1317
1318 return;
1319}
1320
1321void FitsIoServer::printerror(int status) const
1322
1323 //*****************************************************/
1324 //* Print out cfitsio error messages and exit program */
1325 //*****************************************************/
1326{
1327
1328 // print out cfitsio error messages and exit program
1329 if( status )
1330 {
1331 // print error report
1332 fits_report_error(stderr, status);
1333 // terminate the program, returning error status
1334 exit( status );
1335 }
1336 return;
1337}
1338
1339
1340
Note: See TracBrowser for help on using the repository browser.