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

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

Corrections bug divers ds FitsIO - Reza 21/11/99

File size: 59.3 KB
RevLine 
[482]1//************************************************************************
2// Class for loadind and saving from FITS-formatted file to DPC objects
[488]3// (G. Le Meur ; Francois Touze) OCT. 99
4//
[482]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" .
[471]9
[482]10//************************************************************************
11
[605]12#include "machdefs.h"
[471]13#include <iostream.h>
[477]14#include <list>
[605]15#include <string>
[477]16
[471]17#include "fitsioserver.h"
[605]18#include "pexceptions.h"
[471]19#include "strutil.h"
20
[477]21void FitsIoServer::load(TMatrix<double>& mat,char flnm[])
[471]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;
[482]29 planck_read_img(flnm, naxis, n1, n2, n3, dvl);
[471]30
31 nbrows=n1;
32 nbcols=n2;
33 if (naxis == 1) nbcols=1;
34 if (naxis > 2)
35 {
[477]36 cout<<" FitsIOServer : le fichier fits n'est pas une matrice, naxis= " <<naxis<< endl;
[471]37 }
[477]38
[471]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++)
[488]51 for (int i = 0; i < nbrows; i++) mat(i,j) = (double)r_8tab_[ij++];
[471]52}
53
[605]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
[482]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");
[605]151 throw IOExc("FitsIoServer::load(NTuple& ," + (string)flnm + ") Error Not a bin table !");
152// exit ( status );
[482]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
[488]187 //for(int ii = 0; ii < tfields; ii++)
188 // printf("\nColumn name & Format %8s %8s", ttype[ii],tform[ii]);
[482]189
[488]190 // select the columns with float data values
[482]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);
[488]198 if(typecode == TFLOAT)
[482]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
[488]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
[482]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
[488]232 for(int ii = 0; ii < column.size(); ii++)
233 delete [] clname[ii];
234 delete [] clname;
235
[482]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
[471]322void FitsIoServer::load(SphericalMap<double>& sph, char flnm[])
323{
324 int npixels=0;
325 int nside=0;
[488]326 long naxis;
327 int n1, n2, n3;
328
[471]329 FITS_tab_typ_ = TDOUBLE;
[477]330
[488]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
[471]340 // number of pixels in the sphere
341 if (sph.NbPixels() != npixels)
342 {
[609]343 //DBG cout << " found " << npixels << " pixels" << endl;
344 //DBG cout << " expected " << sph.NbPixels() << endl;
[471]345 if (nside <= 0 )
346 {
[477]347 cout<<" FITSIOSERVER: no resolution parameter on fits file "<<endl;
[605]348 throw IOExc("FitsIoServer::load(SphericalMap<double>& ," + (string)flnm + ") Error NSide<0 !");
349// exit(0);
[471]350 }
351 if (nside != sph.SizeIndex())
352 {
353 sph.Resize(nside);
[609]354 cout << "FitsIoServer::load(SphereGorski<double> ...) ReSizing to NSide= " << nside << endl;
[471]355 }
[609]356 // else
357 // {
358 // cout << " FITSIOSERVER : same resolution, surprising ! " << endl;
[605]359// exit(0); $CHECK$ Ca peut etre OK , non ?? Reza 20/11/99
[609]360 // }
[471]361 }
[488]362 for (int j = 0; j < sph.NbPixels(); j++) sph(j)= (double)r_8tab_[j];
[471]363}
[564]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);
[570]375 //dvl.Print();
[564]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 {
[609]388 //DBG cout << " found " << npixels << " pixels" << endl;
389 //DBG cout << " expected " << sph.NbPixels() << endl;
[564]390 if (nside <= 0 )
391 {
392 cout<<" FITSIOSERVER: no resolution parameter on fits file "<<endl;
[605]393 throw IOExc("FitsIoServer::load(SphericalMap<float>& ," + (string)flnm + ", ) Error No resolution parameter !");
394// exit(0);
[564]395 }
396 if (nside != sph.SizeIndex())
397 {
398 sph.Resize(nside);
[609]399 cout << "FitsIoServer::load(SphereGorski<float> ...) ReSizing to NSide= " << nside << endl;
[564]400 }
[609]401 // else
402 // {
403 // cout << " FITSIOSERVER : same resolution, surprising ! " << endl;
[605]404// exit(0); $CHECK$ - Il ne faut pas sortir, me semble-t-il , Reza 20/11/99
[609]405 // }
[564]406 }
[603]407 // cout << " fin relecture debut transfert" << endl;
[564]408 for (int j = 0; j < sph.NbPixels(); j++) sph(j)= (float)r_4tab_[j];
409}
[603]410void FitsIoServer::load(SphereGorski<double>& sph, char flnm[], int hdunum)
411{
412 int npixels=0;
413 int nside=0;
[564]414
[603]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;
[605]437 throw IOExc("FitsIoServer::load(SphereGorski<double>& ," + (string)flnm + ", ) No resol parameter !");
438// exit(0);
[603]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;
[605]448// exit(0); $CHECK$ - ne pas sortir , Reza 20/11/99
[603]449 }
450 }
451 for (int j = 0; j < sph.NbPixels(); j++) sph(j)= (double)r_8tab_[j];
452}
453
[471]454void FitsIoServer::load(SphericalMap<float>& sph, char flnm[])
455{
456 int npixels=0;
457 int nside=0;
[488]458 long naxis;
459 int n1, n2, n3;
460 DVList dvl;
461
[471]462 FITS_tab_typ_ = TFLOAT;
[488]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
[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 {
[477]479 cout<<" FITSIOSERVER: no resolution parameter on fits file "<<endl;
[605]480 throw IOExc("FitsIoServer::load(SphericalMap<float>& ," + (string)flnm + ") No resolution param !");
481// exit(0);
[471]482 }
483 if (nside != sph.SizeIndex())
484 {
485 sph.Resize(nside);
[477]486 cout << " resizing the sphere to nside= " << nside << endl;
[471]487 }
488 else
489 {
490 cout << " FITSIOSERVER : same resolution, surprising ! " << endl;
[605]491// exit(0); $CHECK$ - Ne pas sortir , Reza 20/11/99
[471]492 }
493 }
[488]494 for (int j = 0; j < sph.NbPixels(); j++) sph(j)= (float)r_4tab_[j];
[471]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;
[482]505 planck_read_img(flnm, naxis, n1, n2, n3, dvl);
[471]506
507 nbrows=n1;
508 nbcols=n2;
509 if (naxis != 2)
510 {
[477]511 cout<<" FitsIOServer : le fichier fits n'est pas une localmap, naxis= "<<naxis<<endl;
[471]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);
[477]532 cout << " resizing the map to x-size= " << nbrows << " y-size= " << nbcols << endl;
[471]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++)
[488]538 for (int i = 0; i < nbrows; i++) lcm(i,j) = (double)r_8tab_[ij++];
[471]539}
540
[482]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;
[477]549
[482]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 {
[488]565 cout << " FitsIOServer : parameter DATATYPE on file " << flnm << " is not float " << endl;
[482]566 cout << " eventual conversion to float is achieved by cfitsio lib " << endl;
567 }
568
[488]569 memcpy(pixelPtr, r_4tab_, siz_x*siz_y*DataSize(dataT));
[482]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 {
[488]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;
[482]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
[471]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;
[477]635 cout << " nombre de lignes : " << nbrows << " colonnes " << nbcols << endl;
[471]636 FITS_tab_typ_ = TDOUBLE;
[605]637 if (r_8tab_ != NULL ) { delete[] r_8tab_; r_8tab_ = NULL; }
[488]638 r_8tab_=new r_8[nbrows*nbcols];
[471]639
640
641 int ij=0;
642 for (int j=0; j< nbcols; j++)
[488]643 for (int i = 0; i < nbrows; i++) r_8tab_[ij++]= (r_8)mat(i,j);
[471]644
645 DVList dvl;
646
[482]647 planck_write_img(filename, naxis, nbrows, nbcols, 0, dvl);
[605]648 delete[] r_8tab_; r_8tab_ = NULL;
[482]649}
[471]650
[605]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
[482]694void FitsIoServer::save(NTuple& ntpl,char flnm[])
[471]695
[482]696 //****************************************************/
697 //* read the elements of the NTuple ntpl, and create */
698 //* a FITS file with a binary table extension */
699 //****************************************************/
700{
[471]701
[482]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
[609]718 char * extname = "NTuple_Binary_tbl";
[482]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
[488]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
[482]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);
[609]762 delete[] dens;
[482]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;
[609]776 char keyname[10];
[482]777 strncpy(keyname,(*it).first.substr(0,64).c_str(),8);
[609]778 char comment[FLEN_COMMENT];
[482]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 {
[609]798 char strval[128];
799 strncpy(strval,(*it).second.mtv.strv,127);
[482]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
[471]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;
[605]821 if (r_8tab_ != NULL ) { delete[] r_8tab_; r_8tab_ = NULL; }
[488]822 r_8tab_=new r_8[npixels];
[471]823
824
[488]825 for (int j = 0; j < npixels; j++) r_8tab_[j]= (r_8)sph(j);
[471]826 DVList dvl;
[531]827 dvl["NSIDE"] = (int_4) sph.SizeIndex();
[471]828 dvl["ORDERING"]=sph.TypeOfMap();
829
[482]830 planck_write_img(filename, 1, npixels, 0, 0, dvl);
[471]831
832 // decider ulterieurement ce qu'on fait de ce qui suit, specifique
[482]833 // pour l'instant, aux spheres gorski.
834
[471]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
[605]873 delete[] r_8tab_; r_8tab_ = NULL;
[471]874}
875
876void FitsIoServer::save(SphericalMap<float>& sph, char filename[])
877{
878 int npixels = sph.NbPixels();
879 FITS_tab_typ_ = TFLOAT;
[605]880 if (r_4tab_ != NULL ) { delete[] r_4tab_; r_4tab_ = NULL; }
[488]881 r_4tab_=new r_4[npixels];
[471]882
883
[488]884 for (int j = 0; j < npixels; j++) r_4tab_[j]= (r_4)sph(j);
[471]885 DVList dvl;
[531]886 dvl["NSIDE"] = (int_4)sph.SizeIndex();
[471]887 dvl["ORDERING"]=sph.TypeOfMap();
888
[482]889 planck_write_img(filename, 1, npixels, 0, 0, dvl);
[605]890 delete[] r_4tab_; r_4tab_ = NULL;
[471]891
892}
[603]893void FitsIoServer::save(SphereGorski<float>& sph, char filename[])
894{
895 int npixels = sph.NbPixels();
896 FITS_tab_typ_ = TFLOAT;
[605]897 if (r_4tab_ != NULL ) { delete[] r_4tab_; r_4tab_ = NULL; }
[603]898 r_4tab_=new r_4[npixels];
[471]899
[603]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);
[605]912 delete[] r_4tab_; r_4tab_ = NULL;
[603]913
914}
915void FitsIoServer::save(SphereGorski<double>& sph, char filename[])
916{
917 int npixels = sph.NbPixels();
918 FITS_tab_typ_ = TDOUBLE;
[605]919 if (r_8tab_ != NULL ) { delete[] r_8tab_; r_8tab_ = NULL; }
[603]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);
[605]934 delete[] r_8tab_; r_8tab_ = NULL;
[603]935
936}
937
[471]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;
[605]945 if (r_8tab_ != NULL ) { delete[] r_8tab_; r_8tab_ = NULL; }
[488]946 r_8tab_=new r_8[nbrows*nbcols];
[471]947
948 int ij=0;
949 for (int j=0; j< nbcols; j++)
[488]950 for (int i = 0; i < nbrows; i++) r_8tab_[ij++]= (r_8)locm(i,j);
[471]951
952 DVList dvl;
[531]953 dvl["NSIDE"] = (int_4) locm.SizeIndex();
[471]954 dvl["ORDERING"]=locm.TypeOfMap();
[477]955 double theta0;
956 double phi0;
[471]957 int x0;
958 int y0;
[477]959 double angle;
960 locm.Origin(theta0,phi0,x0,y0,angle);
961 double anglex;
962 double angley;
963 locm.Aperture(anglex,angley);
[471]964 dvl["THETA0"] = theta0;
965 dvl["PHI0"] = phi0;
[479]966 dvl["X0"] = (int_4)x0;
967 dvl["Y0"] = (int_4)y0;
[471]968 dvl["ANGLE"] = angle;
969 dvl["ANGLEX"] = anglex;
970 dvl["ANGLEY"] = angley;
[482]971 planck_write_img(filename, naxis, nbrows, nbcols, 0, dvl);
[605]972 delete[] r_8tab_; r_8tab_ = NULL;
973}
[471]974
975
[488]976void FitsIoServer::save(const ImageR4& DpcImg,char flnm[])
[482]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();
[530]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
[482]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
[488]1013 // get the values of the DpcImage
[605]1014 if (r_4tab_ != NULL) { delete [] r_4tab_; r_4tab_ = NULL; }
[488]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));
[482]1018 planck_write_img(flnm, naxis, siz_x, siz_y, 0, dvl);
1019
1020
[605]1021 delete [] r_4tab_; r_4tab_ = NULL;
[482]1022}
1023
1024
[488]1025void FitsIoServer::save(const ImageI4& DpcImg,char flnm[])
[482]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();
[530]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
[482]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
[488]1073 // get the values of the DpcImage
[605]1074 if (i_4tab_ != NULL) { delete [] i_4tab_; i_4tab_ = NULL; }
[488]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));
[482]1078
[488]1079
[482]1080 planck_write_img(flnm, naxis, siz_x, siz_y, 0, dvl);
1081
[471]1082 // close the file
[482]1083 //fits_close_file(fptr, &status);
1084 //if( status ) printerror( status );
[471]1085
[605]1086 delete [] i_4tab_; i_4tab_ = NULL;
[471]1087}
[482]1088
1089
1090
1091
1092void FitsIoServer::planck_write_img(char flnm[], int naxis,int n1, int n2, int n3, DVList& dvl)
[471]1093{
1094
[482]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
[471]1109 int bitpix=0;
1110 if ( FITS_tab_typ_ == TDOUBLE) bitpix = DOUBLE_IMG;
1111 if ( FITS_tab_typ_ == TFLOAT) bitpix = FLOAT_IMG;
[477]1112 if ( FITS_tab_typ_ == TINT) bitpix = LONG_IMG;
[471]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 :
[488]1130 fits_write_img(fptr, TDOUBLE, 1, nelements, r_8tab_, &status);
[471]1131 if( status ) printerror( status );
1132 break;
1133 case TFLOAT :
[488]1134 fits_write_img(fptr, TFLOAT, 1, nelements, r_4tab_, &status);
[471]1135 if( status ) printerror( status );
1136 break;
[477]1137 case TINT :
1138 fits_write_img(fptr, TINT, 1, nelements, i_4tab_, &status);
1139 if( status ) printerror( status );
1140 break;
[471]1141 default :
[477]1142 cout << " FitsIOServer : type de tableau non traite en ecriture " << endl;
[471]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
[477]1152 //dvl.Print();
[609]1153 char keyname[16];
1154 int flen_keyword = 9;
1155 char comment[FLEN_COMMENT];
1156 char strval[128];
1157
[477]1158 DVList::ValList::const_iterator it;
[471]1159 for(it = dvl.Begin(); it != dvl.End(); it++)
1160 {
1161 int datatype= key_type_PL2FITS( (*it).second.typ);
1162 // FLEN_KEYWORD est la longueur max d'un mot-cle. Il doit y avoir une
1163 // erreur dans la librairie fits qui donne FLEN_KEYWORD=72
1164 // contrairement a la notice qui donne FLEN_KEYWORD=9 (ch. 5, p.39)
[609]1165 //$CHECK$ Reza 20/11/99
1166 // strncpy(keyname, (*it).first.substr(0,64).c_str(),flen_keyword-1);
1167 strncpy(keyname, (*it).first.substr(0,64).c_str(),flen_keyword);
1168 keyname[flen_keyword] = '\0';
[471]1169 int ival=0;
1170 double dval=0.;
1171 switch (datatype)
1172 {
1173 case TINT :
1174 ival=(*it).second.mtv.iv;
1175 strcpy(comment,"I entier");
[609]1176//DBG cerr << " Writing I " << (string)keyname << " = " << ival << endl;
[471]1177 fits_write_key(fptr, datatype, keyname, &ival, comment, &status);
1178 break;
1179 case TDOUBLE :
1180 dval=(*it).second.mtv.dv;
1181 strcpy(comment,"D double");
[609]1182//DBG cerr << " Writing D " << (string)keyname << " = " << dval << endl;
[471]1183 fits_write_key(fptr, datatype, keyname, &dval, comment, &status);
1184 break;
1185 case TSTRING :
[609]1186 strncpy(strval, (*it).second.mtv.strv, 128); strval[127] = '\0';
[471]1187 strcpy(comment,"S character string");
[609]1188//DBG cerr << " Writing S " << (string)keyname << " = " << (string)strval << endl;
[471]1189 fits_write_key(fptr, datatype, keyname, &strval, comment, &status);
1190 break;
1191 default :
[477]1192 cout << " FitsIOServer : probleme dans type mot cle optionnel" << endl;
[471]1193 break;
1194 }
1195 if( status ) printerror( status );
1196 }
[609]1197
1198 strncpy(keyname, "CREATOR",flen_keyword); keyname[flen_keyword] = '\0';
1199 strcpy(strval, "SOPHYA");
1200 strcpy(comment," SOPHYA Package - FITSIOServer ");
1201 fits_write_key(fptr, TSTRING, keyname, &strval, comment, &status);
1202 if( status ) printerror( status );
1203
1204 fits_write_comment(fptr,"..............................................", &status);
1205 fits_write_comment(fptr, " SOPHYA package - FITSIOSever ", &status);
1206 fits_write_comment(fptr, " (C) LAL/IN2P3-CNRS Orsay, FRANCE 1999", &status);
1207 fits_write_comment(fptr, " (C) DAPNIA/CEA Saclay, FRANCE 1999", &status);
1208 fits_write_comment(fptr,"..............................................", &status);
1209 if( status ) printerror( status );
1210
[482]1211 // close the file
1212 fits_close_file(fptr, &status);
1213 if( status ) printerror( status );
[471]1214}
[477]1215
[603]1216void FitsIoServer::planck_write_bntbl(char flnm[], int npixels, char typeOfContent[], char extname[], char comment1[], DVList& dvl)
1217{
1218
1219
1220 // pointer to the FITS file, defined in fitsio.h
1221 fitsfile *fptr;
1222
1223 // initialize status before calling fitsio routines
1224 int status = 0;
1225
1226 // delete old file if it already exists
1227 remove(flnm);
1228
1229 // create new FITS file
[609]1230 fits_create_file(&fptr, flnm, &status);
1231 //DBG cerr << " DBG - Apres fits_create_file status = " << status << endl;
[603]1232 if( status ) printerror( status );
1233
1234 // primary header
1235 int bitpix=LONG_IMG;
1236 int naxis=0;
1237 fits_create_img(fptr, bitpix, naxis, NULL, &status);
1238 // write the current date
1239 fits_write_date(fptr, &status);
[609]1240 //DBG cerr << " DBG - Apres write_date status = " << status << endl;
[603]1241 if( status ) printerror( status );
1242
1243
1244 long nrows=npixels/1024;
1245 if (nrows==0) nrows=1;
1246 if (npixels%1024 !=0) nrows++;
1247 int tfields=1;
1248 char **ttype, **tform;
1249 ttype= new char*[tfields];
1250 tform= new char*[tfields];
1251 char* format;
1252 if ( FITS_tab_typ_ == TDOUBLE) format="1024D";
1253 if ( FITS_tab_typ_ == TFLOAT) format="1024E";
1254 if ( FITS_tab_typ_ == TINT) format="1024I";
1255 for(int i = 0; i < tfields; i++)
1256 {
1257 ttype[i]= new char[FLEN_VALUE];
1258 strcpy(ttype[i], typeOfContent);
1259 tform[i]= new char[FLEN_VALUE];
1260 strcpy(tform[i], format);
1261 }
1262 // create a new empty binary table onto the FITS file
1263 // physical units if they exist, are defined in the DVList object
1264 // so the null pointer is given for the tunit parameters.
1265 fits_create_tbl(fptr,BINARY_TBL,nrows,tfields,ttype,tform,
1266 NULL,extname,&status);
[609]1267 //DBG cerr << " DBG - Apres create_tbl status = " << status << endl;
[603]1268 if( status ) printerror( status );
1269 for( int ii = 0; ii < tfields; ii++)
1270 {
1271 delete [] ttype[ii];
1272 delete [] tform[ii];
1273 }
1274 delete [] ttype;
1275 delete [] tform;
1276 long nelements= npixels;
1277 switch (FITS_tab_typ_)
1278 {
1279 case TDOUBLE :
1280 fits_write_col(fptr, TDOUBLE, 1, 1, 1, nelements, r_8tab_, &status);
[609]1281 if( status )
1282 printerror( status, "planck_write_bntbl: Error writing double table" );
[603]1283 break;
1284
1285 case TFLOAT :
[609]1286 //DBG!!! $CHECK$ Reza for (int kk=0; kk<10; kk++) cout << r_4tab_[kk] << endl;
[603]1287 fits_write_col(fptr, TFLOAT, 1, 1, 1, nelements, r_4tab_, &status);
[609]1288 if( status )
1289 printerror( status, "planck_write_bntbl: Error writing float table" );
[603]1290 break;
1291 case TINT :
1292 fits_write_col(fptr, TINT, 1, 1, 1, nelements, i_4tab_, &status);
[609]1293 if( status )
1294 printerror( status, "planck_write_bntbl: Error writing int table");
[603]1295 break;
1296 default :
1297 cout << " FitsIOServer : type de tableau non traite en ecriture " << endl;
1298 break;
1299 }
1300 // write supplementary keywords
1301 fits_write_comment(fptr, " ", &status);
1302 fits_write_comment(fptr, " ", &status);
[609]1303 //DBG cerr << " DBG - Apres write_comment1 status = " << status << endl;
[603]1304 if( status ) printerror( status );
1305 fits_write_comment(fptr,"--------------------------------------------", &status);
1306 fits_write_comment(fptr, comment1, &status);
1307 fits_write_comment(fptr,"--------------------------------------------", &status);
[609]1308 //DBG cerr << " DBG - Apres write_comment2 status = " << status << endl;
[603]1309 if( status ) printerror( status );
1310
1311
1312 int flen_keyword = 9;
1313 // FLEN_KEYWORD est la longueur max d'un mot-cle. Il doit y avoir une
1314 // erreur dans la librairie fits qui donne FLEN_KEYWORD=72
1315 // contrairement a la notice qui donne FLEN_KEYWORD=9 (ch. 5, p.39)
1316 DVList::ValList::const_iterator it;
1317 for(it = dvl.Begin(); it != dvl.End(); it++)
1318 {
1319 int datatype= key_type_PL2FITS( (*it).second.typ);
[609]1320 char keyname[16];
1321 strncpy(keyname, (*it).first.substr(0,64).c_str(),flen_keyword);
1322 keyname[flen_keyword] = '\0';
1323 char comment[FLEN_COMMENT];
[603]1324 int ival=0;
1325 double dval=0.;
[609]1326 char strval[128];
[603]1327 switch (datatype)
1328 {
1329 case TINT :
1330 ival=(*it).second.mtv.iv;
1331 strcpy(comment," ");
[609]1332//DBG cerr << " Writing I " << (string)keyname << " = " << ival << endl;
[603]1333 fits_write_key(fptr, datatype, keyname, &ival, comment, &status);
1334 break;
1335 case TDOUBLE :
1336 dval=(*it).second.mtv.dv;
1337 strcpy(comment," ");
[609]1338//DBG cerr << " Writing D " << (string)keyname << " = " << dval << endl;
[603]1339 fits_write_key(fptr, datatype, keyname, &dval, comment, &status);
1340 break;
1341 case TSTRING :
[609]1342 strncpy(strval, (*it).second.mtv.strv, 128); strval[127] = '\0';
[603]1343 strcpy(comment," ");
[609]1344//DBG cerr << " Writing S " << (string)keyname << " = " << (string)strval << endl;
[603]1345 fits_write_key(fptr, datatype, keyname, &strval, comment, &status);
1346 break;
1347 default :
1348 cout << " FitsIOServer : probleme dans type mot cle optionnel" << endl;
1349 break;
1350 }
1351 if( status ) printerror( status );
1352 }
[609]1353 char keyname[16];
1354 char strval[32];
1355 char comment[FLEN_COMMENT];
1356 strncpy(keyname, "CREATOR",flen_keyword); keyname[flen_keyword] = '\0';
1357 strcpy(strval, "SOPHYA");
1358 strcpy(comment," SOPHYA Package - FITSIOServer ");
1359 fits_write_key(fptr, TSTRING, keyname, &strval, comment, &status);
1360 if( status ) printerror( status );
[603]1361 // close the file
1362 fits_close_file(fptr, &status);
[609]1363 //DBG cerr << " DBG - Apres close status = " << status << endl;
[603]1364 if( status ) printerror( status );
1365}
1366
[482]1367void FitsIoServer::planck_read_img(char flnm[], long& naxis,int& n1, int& n2, int& n3, DVList& dvl)
[471]1368{
1369 int status=0;
1370 long bitpix;
1371 long naxes[3]={0,0,0};
1372 char* comment=NULL;
1373
[482]1374 // pointer to the FITS file, defined in fitsio.h
1375 fitsfile *fptr;
1376 // initialize status before calling fitsio routines
1377 fits_open_file(&fptr, flnm, READONLY, &status);
1378 if( status ) printerror( status );
1379
1380
[471]1381 fits_read_key_lng(fptr, "BITPIX", &bitpix, comment, &status);
1382 if( status ) printerror( status );
1383 fits_read_key_lng(fptr, "NAXIS", &naxis, comment, &status);
1384 if( status ) printerror( status );
1385 int nfound;
1386 int nkeys=(int)naxis;
1387 fits_read_keys_lng(fptr, "NAXIS", 1, nkeys, naxes, &nfound, &status);
1388 if( status ) printerror( status );
1389
1390 n1 = naxes[0] ;
1391 n2 = naxes[1] ;
1392 n3 = naxes[2] ;
1393
1394
1395 long nelements= naxes[0];
1396 if (naxis >=2) nelements*=naxes[1];
1397 if (naxis == 3) nelements*=naxes[2];
1398 int anynull;
[488]1399 r_8 dnullval=0.;
1400 r_4 fnullval=0.;
1401 int_4 inullval=0;
[471]1402 // on laisse a fits le soin de convertir le type du tableau lu vers
1403 // le type suppose par l'utilisateur de fitsioserver
1404 //
1405 switch ( FITS_tab_typ_)
1406 {
1407 case TDOUBLE :
[477]1408 if (bitpix != DOUBLE_IMG)
1409 {
[488]1410 cout << " FitsIOServer : the data type on fits file " << flnm << " is not double, "
[477]1411 << " conversion to double will be achieved by cfitsio lib " << endl;
1412 }
[605]1413 if (r_8tab_ != NULL) { delete [] r_8tab_; r_8tab_ = NULL; }
[488]1414 r_8tab_=new r_8[nelements];
1415 fits_read_img(fptr, TDOUBLE, 1, nelements, &dnullval, r_8tab_,
[471]1416 &anynull, &status);
1417 if( status ) printerror( status );
1418 break;
1419 case TFLOAT :
[477]1420 if (bitpix != FLOAT_IMG)
1421 {
[488]1422 cout << " FitsIOServer : the data type on fits file " << flnm << " is not float, "
[477]1423 << " conversion to float will be achieved by cfitsio lib " << endl;
1424 }
[605]1425 if (r_4tab_ != NULL) { delete [] r_4tab_; r_4tab_ = NULL; }
[488]1426 r_4tab_=new r_4[nelements];
1427 fits_read_img(fptr, TFLOAT, 1, nelements, &fnullval, r_4tab_,
[471]1428 &anynull, &status);
1429 if( status ) printerror( status );
1430 break;
[477]1431
1432
1433 case TINT :
1434 if (bitpix != LONG_IMG)
1435 {
[488]1436 cout << " FitsIOServer : the data type on fits file " << flnm << " is not long, "
[477]1437 << " conversion to long will be achieved by cfitsio lib " << endl;
1438 }
[605]1439 if (i_4tab_ != NULL) { delete [] i_4tab_; i_4tab_ = NULL; }
[477]1440 i_4tab_=new int_4[nelements];
[488]1441 fits_read_img(fptr, TINT, 1, nelements, &inullval, i_4tab_,
[477]1442 &anynull, &status);
1443 if( status ) printerror( status );
1444 break;
1445
1446
[471]1447 default :
[477]1448 cout << " FitsIOServer::read_img : type non traite: " << FITS_tab_typ_ << endl;
[471]1449 break;
1450 }
1451 status = 0;
1452 char card[FLEN_CARD];
1453 int num = 0;
1454 char comment2[FLEN_COMMENT] = "x";
[609]1455 char keyname[16];
1456 char * datekey= "DATE";
1457 char * endkey= "END";
[471]1458 char typ='x';
1459 int ival;
1460 double dval;
[609]1461 char strval[90];
[471]1462 // on a convenu que les mots cles utilisateur sont apres le mot cle DATE
1463 // on va jusqu'au mot cle DATE
1464 int flen_keyword = 9;
1465 // FLEN_KEYWORD est la longueur max d'un mot-cle. Il doit y avoir une
1466 // erreur dans la librairie fits qui donne FLEN_KEYWORD=72
1467 // contrairement a la notice qui donne FLEN_KEYWORD=9 (ch. 5, p.39)
1468 while (status == 0 && strncmp(keyname, datekey,4) != 0 )
1469 {
1470 num++;
1471 fits_read_record(fptr, num, card, &status);
[609]1472 strncpy(keyname,card,flen_keyword-1); keyname[10] = '\0';
[471]1473 }
1474 if (status != 0 )
1475 {
1476 cout << " fitsio::planck_read_img : erreur, mot cle DATE absent " << endl;
1477 }
1478 // on recupere la liste des mots-cles utilisateurs
1479 while (status == 0)
1480 {
1481 num++;
1482 // on lit un record pour recuperer le nom du mot-cle
1483 fits_read_record(fptr, num, card, &status);
[609]1484 strncpy(keyname,card,flen_keyword-1); keyname[10] = '\0';
[471]1485 char value[FLEN_VALUE];
1486 // on recupere le premier caractere du commentaire, qui contient
1487 // le code du type de la valeur
1488 // (tant que ce n est pas le mot cle END)
1489 fits_read_keyword(fptr, keyname, value, comment2, &status);
1490 if ( strncmp(keyname, endkey,flen_keyword-1) != 0)
1491 {
1492 typ = comment2[0];
1493 // quand le type est connu, on lit la valeur
1494 switch (typ)
1495 {
1496 case 'I' :
1497 fits_read_key(fptr, TINT, keyname, &ival, comment2, &status);
1498 if( status ) printerror( status );
1499 strip (keyname, 'B',' ');
[482]1500 dvl[keyname] = (int_4)ival;
[471]1501 break;
1502 case 'D' :
1503 fits_read_key(fptr, TDOUBLE, keyname, &dval, comment2, &status);
1504 if( status ) printerror( status );
1505 strip (keyname, 'B',' ');
1506 dvl[keyname] = dval;
1507 break;
1508 case 'S' :
1509 fits_read_key(fptr, TSTRING, keyname, strval, comment2, &status);
1510 if( status ) printerror( status );
1511 strip (keyname, 'B',' ');
1512 strip(strval, 'B',' ');
1513 dvl[keyname]=strval;
1514 break;
1515 default :
[609]1516 // cout << " FITSIOSERVER::planck_read_img -> DVList Type ? for key "
1517 // << (string)keyname << endl;
[471]1518 break;
1519 }
1520 }
1521 }
1522
[488]1523
[482]1524 // close the file
1525 status=0;
1526 fits_close_file(fptr, &status);
1527 if( status ) printerror( status );
[471]1528}
1529
1530
[564]1531void FitsIoServer::planck_read_bntbl(char flnm[], int hdunum, int& npixels, DVList& dvl)
1532{
1533 int status=0;
1534 int nkeys,keypos;
1535 int hdutype;
1536 int tfields;
1537 int datype;
1538 long lastpix;
1539 long repeat, width;
1540 long nrows;
1541 long extend;
1542 char* comment=NULL;
[482]1543
[564]1544 // pointer to the FITS file, defined in fitsio.h
1545 fitsfile *fptr;
1546 // initialize status before calling fitsio routines
1547 fits_open_file(&fptr, flnm, READONLY, &status);
1548 if( status ) printerror( status );
1549 fits_read_key_lng(fptr, "EXTEND", &extend, comment, &status);
1550 if( status ) printerror( status );
1551 if (extend!=1)
1552 {
1553 cout << "FitsIoServer:: le fichier fits ne contient pas d'extension binary table" << endl;
[605]1554 throw IOExc("FitsIoServer::planck_read_bntbl(" + (string)flnm + ") Error No bin table extension !");
1555// return;
[564]1556 }
1557 fits_movabs_hdu(fptr, hdunum,&hdutype,&status);
1558 if( status ) printerror( status );
1559 if (hdutype!=BINARY_TBL)
1560 {
1561 cout << "FitsIoServer:: this HDU is not a binary table " << endl;
[605]1562 throw IOExc("FitsIoServer::planck_read_bntbl(" + (string)flnm + ") Error Not a bin table (1) !");
1563// exit(status);
[564]1564 }
1565 char xtension[FLEN_VALUE];
1566 fits_read_key_str(fptr,"XTENSION",xtension,NULL,&status);
1567 if( status ) printerror( status );
1568
[609]1569 char * binta = "BINTABLE";
[564]1570 if ( strncmp(xtension, binta,8) != 0)
1571 // if (xtension !="BINTABLE")
1572 {
1573 cout << "FitsIoServer:: not a binary table " << endl;
[605]1574 throw IOExc("FitsIoServer::planck_read_bntbl(" + (string)flnm + ") Error Not a bin table (2) !");
1575// exit(status);
[564]1576 }
1577 fits_get_hdrpos(fptr,&nkeys,&keypos,&status);
1578 if( status ) printerror( status );
1579 //cout << " nombre de mots-cles : " << nkeys << endl;
1580 fits_get_num_cols(fptr, &tfields, &status);
1581 if (tfields != 1)
1582 {
1583 cout << "FitsIoServer:: il y a plus d'une colonne" << endl;
[605]1584 throw IOExc("FitsIoServer::planck_read_bntbl(" + (string)flnm + ") Error >1 column !");
1585// return;
[564]1586 }
1587 fits_get_num_rows(fptr, &nrows, &status);
1588 //cout << "nblignes= " << nrows << endl;
1589 fits_get_coltype(fptr, 1, &datype, &repeat, &width, &status);
1590 if( status ) printerror( status );
1591 //cout << " type de donnees : " << datype << endl;
1592 //cout << " repeat : " << repeat << endl;
1593 //cout << " width : " << width << endl;
1594 fits_read_key_lng(fptr, "LASTPIX", &lastpix, comment, &status);
[603]1595 if( status ) printerror( status," mot cle LASTPIX" );
[564]1596
1597 long nelements= nrows*repeat;
1598 if (nelements!=lastpix+1)
1599 {
1600 cout << " erreur sur longueur du vecteur " << endl;
1601 cout << " nelements= " << nelements << " lastpix+1=" << lastpix+1 << endl;
1602 }
1603 npixels=nelements;
1604 int anynull;
1605 r_8 dnullval=0.;
1606 r_4 fnullval=0.;
1607 int_4 inullval=0;
1608 // on laisse a fits le soin de convertir le type du tableau lu vers
1609 // le type suppose par l'utilisateur de fitsioserver
1610 //
1611 switch ( FITS_tab_typ_)
1612 {
1613 case TDOUBLE :
1614 if (datype != TDOUBLE)
1615 {
1616 cout << " FitsIOServer : the data type on fits file " << flnm << " is not double, "
1617 << " conversion to double will be achieved by cfitsio lib " << endl;
1618 }
[605]1619 if (r_8tab_ != NULL) { delete [] r_8tab_; r_8tab_ = NULL; }
[564]1620 r_8tab_=new r_8[ npixels];
1621 fits_read_col(fptr, TDOUBLE, 1, 1, 1, nelements, &dnullval,
1622 r_8tab_,
1623 &anynull, &status);
[603]1624 if( status ) printerror( status, "probleme dans lecture du tableau de doubles" );
[564]1625 break;
1626 case TFLOAT :
1627 if (datype != TFLOAT)
1628 {
1629
1630 cout << " FitsIOServer : the data type on fits file " << flnm << " is not float, "
1631 << " conversion to float will be achieved by cfitsio lib " << endl;
1632 }
[605]1633 if (r_4tab_ != NULL) { delete [] r_4tab_; r_4tab_ = NULL; }
[564]1634 r_4tab_=new r_4[nelements];
1635 fits_read_col(fptr, TFLOAT, 1, 1, 1, nelements, &fnullval,
1636 r_4tab_, &anynull, &status);
[603]1637 if( status ) printerror( status,"probleme dans lecture du tableau de floats" );
[564]1638 break;
1639
1640
1641 case TINT :
1642 if (datype != TLONG)
1643 {
1644 cout << " FitsIOServer : the data type on fits file " << flnm << " is not long, "
1645 << " conversion to long will be achieved by cfitsio lib " << endl;
1646 }
[605]1647 if (i_4tab_ != NULL) { delete [] i_4tab_; i_4tab_ = NULL; }
[564]1648 i_4tab_=new int_4[nelements];
1649 fits_read_col(fptr, TLONG, 1, 1, 1, nelements, &inullval,
1650 i_4tab_, &anynull, &status);
1651 //fits_read_img(fptr, TINT, 1, nelements, &inullval, i_4tab_,
1652 // &anynull, &status);
[603]1653 if( status ) printerror( status,"probleme dans lecture du tableau de ints" );
[564]1654 break;
1655
1656
1657 default :
[570]1658 cout << " FitsIOServer::read_bntbl : type non traite: " << FITS_tab_typ_ << endl;
[564]1659 break;
1660 }
1661 char card[FLEN_CARD];
1662 char keyname[LEN_KEYWORD]= "";
1663 char strval[FLEN_VALUE];
1664 char comment1[FLEN_COMMENT];
1665 char dtype;
1666 //char bidon[LEN_KEYWORD];
[609]1667 char * comkey = "COMMENT";
[564]1668
1669 for(int j = 1; j <= nkeys; j++)
1670 {
1671 // fits_read_record(fptr, j, card, &status);
1672 // strncpy(keyname,card,LEN_KEYWORD-1);
1673 // cout << " bidon= " << keyname << endl;
1674 // if ( strncmp(keyname,comkey ,LEN_KEYWORD-1) != 0)
1675 fits_read_keyn(fptr,j,card,strval,comment1,&status);
1676 strncpy(keyname,card,LEN_KEYWORD-1);
1677
1678 if ( strncmp(keyname,comkey ,LEN_KEYWORD-1) != 0)
1679 {
1680 fits_get_keytype(strval,&dtype,&status);
[603]1681 // cout<<" keyname= "<< keyname <<" dtype= "<<dtype <<endl;
[564]1682 strip (keyname, 'B',' ');
1683 strip(strval, 'B',' ');
1684 switch( dtype )
1685 {
1686 case 'C':
1687 dvl[keyname]= strval;
1688 break;
1689 case 'I':
1690 int ival;
1691 ctoi(strval,&ival);
1692 dvl[keyname]= (int_4)ival;
1693 break;
1694 case 'F':
1695 double dval;
1696 ctof(strval,&dval);
1697 dvl[keyname]= dval;
1698 break;
1699 default :
[603]1700 cout << " FitsIOServer : mot-cle bizarre " << endl;
[564]1701 break;
1702 }
1703 }
1704 }
1705
1706 // close the file
1707 status=0;
1708 fits_close_file(fptr, &status);
1709 if( status ) printerror( status );
1710}
1711
1712
[482]1713// projects a SphericalMap<double>, according sinus-method, and saves onto
1714// a FITS-file
[471]1715void FitsIoServer::sinus_picture_projection(SphericalMap<double>& sph, char filename[])
1716{
1717
1718 long naxes[2]={600, 300};
[482]1719 float* map =new float[ 600*300 ];
[471]1720 int npixels= naxes[0]*naxes[1];
1721
1722 cout << " image FITS en projection SINUS" << endl;
1723 // table will have npixels rows
1724 for(int j=0; j < npixels; j++) map[j]=0.;
1725 for(int j=0; j<naxes[1]; j++)
1726 {
1727 double yd = (j+0.5)/naxes[1]-0.5;
1728 double theta = (0.5-yd)*Pi;
1729 double facteur=1./sin(theta);
1730 for(int i=0; i<naxes[0]; i++)
1731 {
1732 double xa = (i+0.5)/naxes[0]-0.5;
1733 double phi = 2.*Pi*xa*facteur+Pi;
1734 float th=float(theta);
1735 float ff=float(phi);
1736 if (phi<2*Pi && phi>= 0)
1737 {
1738 map[j*naxes[0]+i] = sph.PixValSph(th, ff);
1739 }
1740 }
1741 }
1742
1743 write_picture(naxes, map, filename);
[482]1744 delete [] map;
1745}
[471]1746
[482]1747// projects a SphericalMap<double>, according sinus-method, and saves onto
1748// a FITS-file
[471]1749void FitsIoServer::sinus_picture_projection(SphericalMap<float>& sph, char filename[])
1750{
1751 // le code de cete methode duplique celui de la precedente, la seule
1752 //difference etant le type de sphere en entree. Ces methodes de projection
1753 // sont provisoires, et ne servent que pour les tests. C est pourquoi je
1754 // ne me suis pas casse la tete, pour l instant
1755
1756 long naxes[2]={600, 300};
[482]1757 float* map = new float[ 600*300 ];
[471]1758 int npixels= naxes[0]*naxes[1];
1759
1760 cout << " image FITS en projection SINUS" << endl;
1761 // table will have npixels rows
1762 for(int j=0; j < npixels; j++) map[j]=0.;
1763 for(int j=0; j<naxes[1]; j++)
1764 {
1765 double yd = (j+0.5)/naxes[1]-0.5;
1766 double theta = (0.5-yd)*Pi;
1767 double facteur=1./sin(theta);
1768 for(int i=0; i<naxes[0]; i++)
1769 {
1770 double xa = (i+0.5)/naxes[0]-0.5;
1771 double phi = 2.*Pi*xa*facteur+Pi;
1772 float th=float(theta);
1773 float ff=float(phi);
1774 if (phi<2*Pi && phi>= 0)
1775 {
1776 map[j*naxes[0]+i] = sph.PixValSph(th, ff);
1777 }
1778 }
1779 }
1780
1781 write_picture(naxes, map, filename);
[482]1782 delete [] map;
[471]1783
1784}
1785
[603]1786// projects a SphericalMap<float>, according Mollweide-method, and saves onto
[564]1787// a FITS-file
1788void FitsIoServer::Mollweide_picture_projection(SphericalMap<float>& sph, char filename[])
1789{
1790 // le code de cete methode duplique celui de la precedente, la seule
1791 //difference etant le type de sphere en entree. Ces methodes de projection
1792 // sont provisoires, et ne servent que pour les tests. C est pourquoi je
1793 // ne me suis pas casse la tete, pour l instant
1794
1795 long naxes[2]={600, 300};
1796 float* map = new float[ 600*300 ];
1797 int npixels= naxes[0]*naxes[1];
1798
1799 cout << " image FITS en projection MOLLWEIDE" << endl;
1800 // table will have npixels rows
1801 for(int j=0; j < npixels; j++) map[j]=0.;
1802 for(int j=0; j<naxes[1]; j++)
1803 {
1804 double yd = (j+0.5)/naxes[1]-0.5;
1805 double facteur=2.*Pi/sin(acos(yd*2));
1806 double theta = (0.5-yd)*Pi;
1807 for(int i=0; i<naxes[0]; i++)
1808 {
1809 double xa = (i+0.5)/naxes[0]-0.5;
1810 double phi = xa*facteur+Pi;
1811 float th=float(theta);
1812 float ff=float(phi);
1813 if (phi<2*Pi && phi>= 0)
1814 {
1815 map[j*naxes[0]+i] = sph.PixValSph(th, ff);
1816 }
1817 }
1818 }
1819
1820 write_picture(naxes, map, filename);
1821 delete [] map;
1822
1823}
[603]1824// projects a SphericalMap<double>, according Mollweide-method, and saves onto
1825// a FITS-file
1826void FitsIoServer::Mollweide_picture_projection(SphericalMap<double>& sph, char filename[])
1827{
1828 // le code de cete methode duplique celui de la precedente, la seule
1829 //difference etant le type de sphere en entree. Ces methodes de projection
1830 // sont provisoires, et ne servent que pour les tests. C est pourquoi je
1831 // ne me suis pas casse la tete, pour l instant
1832
1833 long naxes[2]={600, 300};
1834 float* map = new float[ 600*300 ];
1835 int npixels= naxes[0]*naxes[1];
[564]1836
[603]1837 cout << " image FITS en projection MOLLWEIDE" << endl;
1838 // table will have npixels rows
1839 for(int j=0; j < npixels; j++) map[j]=0.;
1840 for(int j=0; j<naxes[1]; j++)
1841 {
1842 double yd = (j+0.5)/naxes[1]-0.5;
1843 double facteur=2.*Pi/sin(acos(yd*2));
1844 double theta = (0.5-yd)*Pi;
1845 for(int i=0; i<naxes[0]; i++)
1846 {
1847 double xa = (i+0.5)/naxes[0]-0.5;
1848 double phi = xa*facteur+Pi;
1849 float th=float(theta);
1850 float ff=float(phi);
1851 if (phi<2*Pi && phi>= 0)
1852 {
1853 map[j*naxes[0]+i] = sph.PixValSph(th, ff);
1854 }
1855 }
1856 }
1857
1858 write_picture(naxes, map, filename);
1859 delete [] map;
1860
1861}
[564]1862
1863
[603]1864
[482]1865// saves a (LocalMap<double> on a FITS-file in order to be visualized
1866// (for tests)
[471]1867void FitsIoServer::picture(LocalMap<double>& lcm, char filename[])
1868{
1869
1870 long naxes[2];
1871 naxes[0] = lcm.Size_x();
1872 naxes[1] = lcm.Size_y();
1873 int npixels= naxes[0]*naxes[1];
1874 float* map = new float[npixels];
1875
1876 // table will have npixels rows
1877 for(int j=0; j < npixels; j++) map[j]=0.;
1878 for(int j=0; j<naxes[1]; j++)
1879 {
1880 for(int i=0; i<naxes[0]; i++)
1881 {
1882 map[j*naxes[0]+i] = lcm(i, j);
1883 }
1884 }
1885
1886 write_picture(naxes, map, filename);
1887 delete [] map;
1888}
1889
1890
1891
1892void FitsIoServer::write_picture(long* naxes, float* map, char* filename) const
1893{
[482]1894
[471]1895 int bitpix = FLOAT_IMG;
[477]1896 long naxis = 2;
[471]1897
[482]1898 //pointer to the FITS file, defined in fitsio.h
1899 fitsfile *fptr;
[471]1900 // delete old file if it already exists
1901 remove(filename);
[482]1902 // initialize status before calling fitsio routines
[471]1903 int status = 0;
1904
1905 // create new FITS file
1906 fits_create_file(&fptr, filename, &status);
1907 if( status ) printerror( status );
1908
1909 // write the required header keywords
1910 fits_create_img(fptr, bitpix, naxis, naxes, &status);
1911 if( status ) printerror( status );
1912
1913 // write the current date
1914 fits_write_date(fptr, &status);
1915 if( status ) printerror( status );
1916
1917
1918 // first row in table to write
1919 long firstrow = 1;
1920 // first element in row
1921 long firstelem = 1;
1922 int colnum = 1;
1923 int nelements=naxes[0]*naxes[1];
1924 fits_write_img(fptr, TFLOAT, firstelem, nelements, map, &status);
1925 if( status ) printerror( status );
1926
1927 // close the file
1928 fits_close_file(fptr, &status);
1929 if( status ) printerror( status );
1930}
1931
1932
[477]1933bool FitsIoServer::check_keyword(fitsfile *fptr,int nkeys,char keyword[])
1934
1935 //*****************************************************/
1936 //* check if the specified keyword exits in the CHU */
1937 //*****************************************************/
1938{
1939
1940 bool KEY_EXIST = false;
1941 int status = 0;
1942 char strbide[FLEN_VALUE];
1943 char keybide[LEN_KEYWORD]= "";
1944 for(int jj = 1; jj <= nkeys; jj++)
1945 {
1946 if( fits_read_keyn(fptr,jj,keybide,strbide,NULL,&status) )
1947 printerror( status );
1948 if( !strcmp(keybide,keyword) )
1949 {
1950 KEY_EXIST= true;
1951 break;
1952 }
1953 }
1954 return(KEY_EXIST);
1955}
1956
1957void FitsIoServer::readheader ( char filename[] )
1958
1959 //**********************************************************************/
1960 //* Print out all the header keywords in all extensions of a FITS file */
1961 //**********************************************************************/
1962{
1963
1964 // standard string lengths defined in fitsioc.h
1965 char card[FLEN_CARD];
1966
1967 // pointer to the FITS file, defined in fitsio.h
1968 fitsfile *fptr;
1969
1970 int status = 0;
1971 if ( fits_open_file(&fptr, filename, READONLY, &status) )
1972 printerror( status );
1973
1974 // attempt to move to next HDU, until we get an EOF error
1975 int hdutype;
1976 for (int ii = 1; !(fits_movabs_hdu(fptr,ii,&hdutype,&status));ii++)
1977 {
1978 if (hdutype == ASCII_TBL)
1979 printf("\nReading ASCII table in HDU %d:\n", ii);
1980 else if (hdutype == BINARY_TBL)
1981 printf("\nReading binary table in HDU %d:\n", ii);
1982 else if (hdutype == IMAGE_HDU)
1983 printf("\nReading FITS image in HDU %d:\n", ii);
1984 else
1985 {
1986 printf("Error: unknown type of this HDU \n");
1987 printerror( status );
1988 }
1989
1990 // get the number of keywords
1991 int nkeys, keypos;
1992 if ( fits_get_hdrpos(fptr, &nkeys, &keypos, &status) )
1993 printerror( status );
1994
1995 printf("Header listing for HDU #%d:\n", ii);
1996 for (int jj = 1; jj <= nkeys; jj++)
1997 {
1998 if ( fits_read_record(fptr, jj, card, &status) )
1999 printerror( status );
2000
2001 // print the keyword card
2002 printf("%s\n", card);
2003 }
2004 printf("END\n\n");
2005 }
2006
2007 // got the expected EOF error; reset = 0
2008 if (status == END_OF_FILE)
2009 status = 0;
2010 else
2011 printerror( status );
2012
2013 if ( fits_close_file(fptr, &status) )
2014 printerror( status );
2015
2016 return;
2017}
2018
[603]2019void FitsIoServer::printerror(int& status) const
[477]2020
2021 //*****************************************************/
2022 //* Print out cfitsio error messages and exit program */
2023 //*****************************************************/
2024{
2025
2026 // print out cfitsio error messages and exit program
2027 // print error report
2028 fits_report_error(stderr, status);
2029 // terminate the program, returning error status
[603]2030 // exit( status );
2031 status=0;
[477]2032}
[603]2033void FitsIoServer::printerror(int& status, char* texte) const
[477]2034
[603]2035 //*****************************************************/
2036 //* Print out cfitsio error messages and exit program */
2037 //*****************************************************/
2038{
[477]2039
[603]2040 // print out cfitsio error messages and exit program
2041 // print error report
2042 fits_report_error(stderr, status);
2043 cout << " erreur : " << texte << endl;
2044 status=0;
2045}
[477]2046
[603]2047
2048
Note: See TracBrowser for help on using the repository browser.