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

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

Pb compil sur IRIX64-CC, re-decl for(int ii, ...) FitsIOServer - Reza 6/12/99

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