Logo Coherent WaveBurst  
Reference Guide
Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
skymap.cc
Go to the documentation of this file.
1 // ++++++++++++++++++++++++++++++++++++++++++++++
2 // S. Klimenko, University of Florida
3 // WAT sky map class
4 // ++++++++++++++++++++++++++++++++++++++++++++++
5 
6 #define SKYMAP_CC
7 #include "skymap.hh"
8 #include "wavearray.hh"
9 #include "TObjArray.h"
10 #include "TObjString.h"
11 #include "TSystem.h"
12 #include "time.hh"
13 
14 using namespace std;
15 
16 #ifdef _USE_HEALPIX
17 unsigned int healpix_repcount (tsize npix)
18  {
19  if (npix<1024) return 1;
20  if ((npix%1024)==0) return 1024;
21  return isqrt (npix/12);
22  }
23 #endif
24 
25 ClassImp(skymap)
26 
27 // constructors
28 
30 
31  deg2rad = PI/180.;
32  rad2deg = 180./PI;
33  healpix = NULL;
34  healpix_order = 0;
35 }
36 
37 skymap::skymap(double sms,double t1,double t2,double p1,double p2)
38 {
39  int i,j;
40  int ntheta, nphi;
41  double c,s,p,d;
42  double a = PI/180.;
43 
44  if(t1<0 || t1>180. || t2<0 || t2>180. || t2<t1) {
45  cout<<"skymap(): invalid theta parameters"<<endl;
46  return;
47  }
48 
49  if(p1<0 || p1>360. || p2<0 || p2>360. || p2<p1) {
50  cout<<"skymap(): invalid phi parameters"<<endl;
51  return;
52  }
53 
54  this->sms = sms; // step on phi and theta
55  theta_1 = t1; // theta range
56  theta_2 = t2; // set theta range
57  phi_1 = p1; // phi range
58  phi_2 = p2; // phi range
59 
60  ntheta = 2*size_t((t2-t1)/sms/2.)+1; // divisions on theta
61  d = ntheta>1 ? (t2-t1)/(ntheta-1) : sms;
62 
63  this->index.reserve(ntheta); // reserve index array
64  this->value.reserve(ntheta); // reserve value array
65 
66  for(i=-ntheta/2; i<=ntheta/2; i++) {
67  c = cos(((t2+t1)/2.+i*d)*a);
68  s = sin(((t2+t1)/2.+i*d)*a);
69  c = s>0. ? (cos(sms*a)-c*c)/s/s : 2.;
70  p = c>0&&c<1. ? acos(c)/a : 361.; // step on phi
71  nphi = 2*size_t((p2-p1)/p/2.)+1; // divisions on phi
72 
73  std::vector<double> v;
74 
75  v.reserve(nphi);
76  for(j=0; j<nphi; j++) v.push_back(0.);
77  index.push_back(nphi); // initialize index array
78  value.push_back(v); // initialize value array
79  }
80  mIndex = mTheta = mPhi = -1;
81  gps = -1.;
82 
83  deg2rad = PI/180.;
84  rad2deg = 180./PI;
85  healpix = NULL;
86  healpix_order = 0;
87 
88 }
89 
91 {
92 #ifdef _USE_HEALPIX
93  int i,j;
94  int ntheta, nphi;
95 
96  sms = 0.0; // step on phi and theta
97  theta_1 = 0.0; // theta range
98  theta_2 = 180.0; // set theta range
99  phi_1 = 0.0; // phi range
100  phi_2 = 360.0; // phi range
101 
102  healpix = new Healpix_Map<double>(); //Healpix MAP
103  read_Healpix_map_from_fits(ifile,*healpix,1,2);
104  this->healpix_order = healpix->Order();
105 
106  ntheta = 1; // divisions on theta
107  for(i=0;i<=healpix->Order();i++) ntheta=2*ntheta+1;
108 
109  this->index.reserve(ntheta); // reserve index array
110  this->value.reserve(ntheta); // reserve value array
111  for(i=1; i<=ntheta; i++) {
112 
113  int ring=i; int startpix; int ringpix; double costheta; double sintheta; bool shifted=false;
114  healpix->get_ring_info(ring, startpix, ringpix, costheta, sintheta, shifted);
115  nphi=ringpix; // divisions on phi
116 
117  std::vector<double> v;
118 
119  v.reserve(nphi);
120  for(j=0; j<nphi; j++) v.push_back(0.);
121  index.push_back(nphi); // initialize index array
122  value.push_back(v); // initialize value array
123  }
124  mIndex = mTheta = mPhi = -1;
125  gps = -1.;
126 
127  // fill skymap
128  int L = healpix->Npix();
129  for(int l=0;l<L;l++) this->set(l,(*healpix)[l]);
130 
131  deg2rad = PI/180.;
132  rad2deg = 180./PI;
133 #else
134  cout << "skymap::skymap(char* ifile) : not available - healpix not enabled" << endl;
135  exit(1);
136 #endif
137 }
138 
139 skymap::skymap(int healpix_order)
140 {
141 #ifdef _USE_HEALPIX
142  int i,j;
143  int ntheta, nphi;
144 
145  sms = 0.0; // step on phi and theta
146  theta_1 = 0.0; // theta range
147  theta_2 = 180.0; // set theta range
148  phi_1 = 0.0; // phi range
149  phi_2 = 360.0; // phi range
150 
151  ntheta = 1; // divisions on theta
152  for(i=0;i<=healpix_order;i++) ntheta=2*ntheta+1;
153 
154  healpix = new Healpix_Map<double>(healpix_order,RING); //Healpix MAP
155  int L = healpix->Npix();
156  this->healpix_order = healpix->Order();
157 
158 // TEST
159 /*
160  char pixDir[8][3] = {"SW", "W", "NW", "N", "NE", "E", "SE", "S"};
161  int ring=0; int startpix; int ringpix; double costheta; double sintheta; bool shifted=false;
162  for(int i=1;i<=ntheta;i++) {
163  ring=i;
164  healpix->get_ring_info(ring, startpix, ringpix, costheta, sintheta, shifted);
165  cout << "ring : " << ring << " startpix : " << startpix << " ringpix : " << ringpix << endl;
166  fix_arr< int, 8 > result;
167  for(int j=startpix;j<startpix+ringpix;j++) {
168  healpix->neighbors (j, result);
169  for(int k=0;k<8;k++) cout << "pixel : " << j << " pixDir : " << pixDir[k] << " " << result[k] << endl;
170  cout << endl;
171  }
172  }
173 */
174 // END TEST
175 
176  this->index.reserve(ntheta); // reserve index array
177  this->value.reserve(ntheta); // reserve value array
178  for(i=1; i<=ntheta; i++) {
179 
180  int ring=i; int startpix; int ringpix; double costheta; double sintheta; bool shifted=false;
181  healpix->get_ring_info(ring, startpix, ringpix, costheta, sintheta, shifted);
182  nphi=ringpix; // divisions on phi
183 
184  std::vector<double> v;
185 
186  v.reserve(nphi);
187  for(j=0; j<nphi; j++) v.push_back(0.);
188  index.push_back(nphi); // initialize index array
189  value.push_back(v); // initialize value array
190  }
191  mIndex = mTheta = mPhi = -1;
192  gps = -1.;
193 
194  deg2rad = PI/180.;
195  rad2deg = 180./PI;
196 #else
197  cout << "skymap::skymap(int healpix_order) : not available - healpix not enabled" << endl;
198  exit(1);
199 #endif
200 }
201 
203 {
204  healpix = NULL;
205  healpix_order = 0;
206 
207  TFile *rfile = TFile::Open(ifile);
208  if (rfile==NULL) {
209  cout << "skymap::skymap - Error : No " << ifile.Data() << " file !!!" << endl;
210  exit(1);
211  }
212  // read skymap object
213  if(rfile->Get(name)!=NULL) {
214  *this = *(skymap*)rfile->Get(name);
215  } else {
216  cout << "skymap::skymap - Error : skymap is not contained in root file " << ifile.Data() << endl;
217  exit(1);
218  }
219  rfile->Close();
220 }
221 
223 {
224  healpix = NULL;
225  healpix_order = 0;
226 
227  *this = value;
228 }
229 
230 // destructor
231 
233  if(healpix!=NULL) delete healpix;
234 }
235 
236 //: operators
237 
239 {
240  if(healpix!=NULL) {delete healpix;healpix=NULL;}
241 
242  if(a.healpix!=NULL) {
243 #ifdef _USE_HEALPIX
244  healpix = new Healpix_Map<double>(a.healpix->Order(),a.healpix->Scheme());
245  this->healpix_order = healpix->Order();
246 #endif
247  } else {
248  healpix = NULL;
249  this->healpix_order = 0;
250  }
251 
252  sms = a.sms; // step on phi and theta
253  theta_1 = a.theta_1; // theta range
254  theta_2 = a.theta_2; // set theta range
255  phi_1 = a.phi_1; // phi range
256  phi_2 = a.phi_2; // phi range
257  value = a.value;
258  index = a.index;
259  mTheta = a.mTheta;
260  mPhi = a.mPhi;
261  mIndex = a.mIndex;
262  gps = a.gps;
263 
264  deg2rad = PI/180.;
265  rad2deg = 180./PI;
266 
267  return *this;
268 }
269 
271 {
272  size_t i,j;
273  size_t n = value.size();
274  size_t m;
275 
276  if( a.theta_1 != theta_1 ||
277  a.theta_2 != theta_2 ||
278  a.phi_1 != phi_1 ||
279  a.phi_2 != phi_2 ||
280  a.sms != sms ||
281  a.value.size() != n )
282  {
283  cout<<"skymap::operator+ - incompatible skymaps"<<endl;
284  }
285 
286 
287  for(i=0; i<n; i++) {
288  m = value[i].size();
289  if(m != a.value[i].size()) {
290  cout<<"skymap::operator+ - incompatible skymaps"<<endl;
291  break;
292  }
293  for(j=0; j<m; j++) { value[i][j] += a.value[i][j]; }
294  }
295 
296  return *this;
297 }
298 
300 {
301  size_t i,j;
302  size_t n = value.size();
303  size_t m;
304 
305  if( a.theta_1 != theta_1 ||
306  a.theta_2 != theta_2 ||
307  a.phi_1 != phi_1 ||
308  a.phi_2 != phi_2 ||
309  a.sms != sms ||
310  a.value.size() != n )
311  {
312  cout<<"skymap::operator- - incompatible skymaps"<<endl;
313  }
314 
315 
316  for(i=0; i<n; i++) {
317  m = value[i].size();
318  if(m != a.value[i].size()) {
319  cout<<"skymap::operator- - incompatible skymaps"<<endl;
320  break;
321  }
322  for(j=0; j<m; j++) { value[i][j] -= a.value[i][j]; }
323  }
324 
325  return *this;
326 }
327 
329 {
330  size_t i,j,m;
331  size_t n = value.size();
332 
333  if( a.theta_1 != theta_1 ||
334  a.theta_2 != theta_2 ||
335  a.phi_1 != phi_1 ||
336  a.phi_2 != phi_2 ||
337  a.sms != sms ||
338  a.value.size() != n )
339  {
340  cout<<"skymap::operator* - incompatible skymaps"<<endl;
341  }
342 
343 
344  for(i=0; i<n; i++) {
345  m = value[i].size();
346  if(m != a.value[i].size()) {
347  cout<<"skymap::operator* - incompatible skymaps"<<endl;
348  break;
349  }
350  for(j=0; j<m; j++) { value[i][j] *= a.value[i][j]; }
351  }
352  return *this;
353 }
354 
356 {
357  size_t i,j,m;
358  size_t n = value.size();
359 
360  if( a.theta_1 != theta_1 ||
361  a.theta_2 != theta_2 ||
362  a.phi_1 != phi_1 ||
363  a.phi_2 != phi_2 ||
364  a.sms != sms ||
365  a.value.size() != n )
366  {
367  cout<<"skymap::operator/ - incompatible skymaps"<<endl;
368  }
369 
370 
371  for(i=0; i<n; i++) {
372  m = value[i].size();
373  if(m != a.value[i].size()) {
374  cout<<"skymap::operator/ - incompatible skymaps"<<endl;
375  break;
376  }
377  for(j=0; j<m; j++) { value[i][j] /= a.value[i][j]!=0. ? a.value[i][j] : 1.; }
378  }
379  return *this;
380 }
381 
382 skymap& skymap::operator=(const double a)
383 {
384  size_t i,j;
385  size_t n = value.size();
386  size_t m;
387 
388  for(i=0; i<n; i++) {
389  m = value[i].size();
390  for(j=0; j<m; j++) { this->value[i][j] = a; }
391  }
392  return *this;
393 }
394 
396 {
397  size_t i,j;
398  size_t n = value.size();
399  size_t m;
400 
401  for(i=0; i<n; i++) {
402  m = value[i].size();
403  for(j=0; j<m; j++) { this->value[i][j] *= a; }
404  }
405  return *this;
406 }
407 
409 {
410  size_t i,j,m;
411  size_t n = value.size();
412 
413  for(i=0; i<n; i++) {
414  m = value[i].size();
415  for(j=0; j<m; j++) { this->value[i][j] += a; }
416  }
417  return *this;
418 }
419 
420 double skymap::max()
421 {
422  size_t i,j,m;
423  size_t k = 0;
424  size_t n = value.size();
425  double a = -1.e100;
426  double x;
427 
428  mTheta = mPhi = mIndex = -1;
429 
430  for(i=0; i<n; i++) {
431  m = value[i].size();
432  for(j=0; j<m; j++) {
433  x = this->value[i][j];
434  if(x>a) { a=x; mTheta=i; mPhi=j; mIndex=k; }
435  k++;
436  }
437  }
438  return a;
439 }
440 
441 double skymap::min()
442 {
443  size_t i,j,m;
444  size_t k = 0;
445  size_t n = value.size();
446  double a = 1.e100;
447  double x;
448 
449  mTheta = mPhi = mIndex = -1;
450 
451  for(i=0; i<n; i++) {
452  m = value[i].size();
453  for(j=0; j<m; j++) {
454  x = this->value[i][j];
455  if(x<a) { a=x; mTheta=i; mPhi=j; mIndex=k; }
456  k++;
457  }
458  }
459  return a;
460 }
461 
462 double skymap::mean()
463 {
464  size_t i,j,m;
465  size_t n = value.size();
466  size_t k = 0;
467  double a = 0.;
468 
469  for(i=0; i<n; i++) {
470  m = value[i].size();
471  k += m;
472  for(j=0; j<m; j++) {
473  a += this->value[i][j];
474  }
475  }
476  return a/double(k);
477 }
478 
479 double skymap::fraction(double t)
480 {
481  size_t i,j,m;
482  size_t n = value.size();
483  size_t k = 0;
484  double a = 0.;
485 
486  for(i=0; i<n; i++) {
487  m = value[i].size();
488  k += m;
489  for(j=0; j<m; j++) {
490  if(this->value[i][j]>t) a += 1.;
491  }
492  }
493  return a/double(k);
494 }
495 
496 double skymap::norm(double a)
497 {
498  size_t i,j,m;
499  size_t n = value.size();
500  double s = 0;
501  double x;
502 
503  (*this) += 1.-this->max();
504  if(a==0.) return a;
505 
506  for(i=0; i<n; i++) {
507  m = value[i].size();
508  for(j=0; j<m; j++) {
509  x = exp((this->value[i][j]-1.)*fabs(a));
510  s += x;
511  if(a>0.) this->value[i][j] = x;
512  }
513  }
514  if(a>0.) (*this) *= 1./s;
515  return s;
516 }
517 
519 {
520  size_t i,j;
521  size_t n = value.size();
522  size_t m = this->size();
523  size_t l = 0;
524 
525  if(index.size() != m) index.resize(m);
526  index = 0;
527 
528  for(i=0; i<n; i++) {
529  m = value[i].size();
530  for(j=0; j<m; j++) {
531  if(i&1) index.data[l] = 1;
532  if(j&1 && k==4) index.data[l] = 1;
533  l++;
534  }
535  }
536  return;
537 }
538 
539 #ifdef _USE_HEALPIX
540 //: dump skymap into fits file
541 void skymap::Dump2fits(const char* file, double gps_obs, const char* configur,
542  const char* TTYPE1, const char* TUNIT1, char coordsys)
543 {
544 
545  if(healpix!=NULL) {
546  int L = healpix->Npix();
547  for(int l=0;l<L;l++) (*healpix)[l]=this->get(l);
548  try {
549 
550  TString fName = file;
551  if(fName.EndsWith(".gz")) fName.Resize(fName.Sizeof()-4);
552 
553  if(!fName.EndsWith(".fits")) {
554  cout << "skymap::Dump2fits Error : wrong file extension" << endl;
555  cout << "fits file must ends with '.fits' or '.fits.gz'" << endl;
556  exit(1);
557  }
558 
559  // if already exist the delete file
560  Long_t id,size,flags,mt;
561  int estat = gSystem->GetPathInfo(fName.Data(),&id,&size,&flags,&mt);
562  if (estat==0) {
563  char cmd[1024];
564  sprintf(cmd,"rm %s",fName.Data());
565  cout << cmd << endl;
566  int err=gSystem->Exec(cmd);
567  if(err) {
568  cout << "skymap::Dump2fits Error : failed to remove file" << endl;
569  exit(1);
570  }
571  }
572 
573  // write_Healpix_map_to_fits(file,*healpix,PLANCK_FLOAT32);
574  fitshandle out;
575  out.create(fName.Data());
576  PDT datatype = PLANCK_FLOAT32;
577 
578  // prepare_Healpix_fitsmap (out, *healpix, datatype, colname);
579  arr<string> colname(1);
580  colname[0] = (TString(TTYPE1)!="") ? TTYPE1 : "unknown ";
581  string tunit1 = (TString(TUNIT1)!="") ? TUNIT1 : "unknown ";
582  vector<fitscolumn> cols;
583  int repcount = healpix_repcount (healpix->Npix());
584  for (tsize m=0; m<colname.size(); ++m)
585  cols.push_back (fitscolumn (colname[m],tunit1,repcount, datatype));
586  out.insert_bintab(cols);
587  out.set_key ("PIXTYPE",string("HEALPIX"),"HEALPIX pixelisation");
588  string ordering = (healpix->Scheme()==RING) ? "RING" : "NESTED";
589  out.set_key ("ORDERING",ordering, "Pixel ordering scheme, either RING or NESTED");
590  out.set_key ("NSIDE",healpix->Nside(),"Resolution parameter for HEALPIX");
591  out.set_key ("FIRSTPIX",0,"First pixel # (0 based)");
592  out.set_key ("LASTPIX",healpix->Npix()-1,"Last pixel # (0 based)");
593  out.set_key ("INDXSCHM",string("IMPLICIT"), "Indexing: IMPLICIT or EXPLICIT");
594  if(coordsys=='c' || coordsys=='C')
595  out.set_key ("COORDSYS",string("C "),"Pixelisation coordinate system");
596  out.set_key ("CREATOR",string("CWB "),"Program that created this file");
597  if(TString(configur)!="")
598  out.set_key ("CONFIGUR",string(configur),"software configuration used to process the data");
599 
600  // convert gps_obs to (YYYY-MM-DDThh:mm:ss.xx UT)
601  if(gps_obs>0) {
602  wat::Time date(gps_obs);
603  TString sdate = date.GetDateString();
604  sdate.Resize(19);
605  sdate.ReplaceAll(" ","T");
606  char date_obs[32];
607  sprintf(date_obs,"%s.%d",sdate.Data(),int(100*(date.GetNSec()/1000000000.)));
608  out.set_key ("DATE-OBS",string(date_obs),"UTC date of the observation");
609  char mjd_obs[32];
610  sprintf(mjd_obs,"%.9f",date.GetModJulianDate());
611  out.set_key ("MJD-OBS",string(mjd_obs),"modified Julian date of the observation");
612  }
613 
614  // UTC date of file creation
615  wat::Time date("now");
616  TString sdate = date.GetDateString();
617  sdate.Resize(19);
618  sdate.ReplaceAll(" ","T");
619  char date_obs[32];
620  sprintf(date_obs,"%s.%d",sdate.Data(),int(100*(date.GetNSec()/1000000000.)));
621  out.set_key ("DATE",string(date_obs),"UTC date of file creation");
622 
623  out.write_column(1,healpix->Map());
624 
625  out.close();
626 
627  // if file ends with .gz then the file zipped
628  if(TString(file).EndsWith(".gz")) {
629  char cmd[1024];
630  sprintf(cmd,"gzip -f %s",fName.Data());
631  cout << cmd << endl;
632  int err=gSystem->Exec(cmd);
633  if(err) {
634  cout << "skymap::Dump2fits Error : gzip error" << endl;
635  exit(1);
636  }
637  }
638 
639  } catch(...) {}
640  return;
641  } else {
642  cout << "skymap::Dump2fits Error : healpix not initialized" << endl;
643  exit(1);
644  }
645 }
646 #endif
647 
648 //: dump skymap into root file
649 void skymap::DumpObject(char* file)
650 {
651  TFile* rfile = new TFile(file, "RECREATE");
652  this->Write("skymap"); // write this object
653  rfile->Close();
654 }
655 
656 //: dump skymap into binary file
657 void skymap::DumpBinary(char* file, int mode)
658 {
659  size_t i,j,m;
660  size_t n = value.size();
661  size_t k = 0;
662 
663  for(i=0; i<n; i++) k += value[i].size();
664  wavearray<float> x(k);
665 
666  k = 0;
667  for(i=0; i<n; i++) {
668  m = value[i].size();
669  for(j=0; j<m; j++) {
670  x.data[k++] = this->value[i][j];
671  }
672  }
673 
674  x.DumpBinary(file,mode);
675  return;
676 }
677 
678 //: get skymap value at index l (access as a linear array)
679 // and fill in mTheta, mPhi and mIndex fields
680 //!param: sky index
681 double skymap::get(size_t l) {
682  size_t i,m;
683  size_t n = value.size();
684  size_t k = 0;
685 
686  mIndex = l;
687  for(i=0; i<n; i++) {
688  m = value[i].size();
689  k += m;
690  if(k <= l) continue;
691  mTheta = i;
692  mPhi = m-int(k-l);
693  return this->value[mTheta][mPhi];
694  }
695  mTheta = mPhi = mIndex = -1;
696  return 0.;
697 }
698 
699 //: get sky index at theta,phi
700 //!param: theta
701 //!param: phi
702 size_t skymap::getSkyIndex(double th, double ph) {
703  size_t k;
704 
705  if(healpix!=NULL) {
706 #ifdef _USE_HEALPIX
707  pointing P(th*deg2rad,ph*deg2rad);
708  k = healpix->ang2pix(P);
709  this->get(k);
710  return mIndex;
711 #endif
712  }
713 
714  double g;
715  g = (value.size()-1)/(theta_2-theta_1);
716  if(g>0.) {
717  if(th<theta_1) th = theta_1;
718  if(th>theta_2) th = theta_2;
719  mTheta = int(g*(th-theta_1)+0.5);
720  }
721  else { mTheta = 0; }
722 
723  g = value[mTheta].size()/(phi_2-phi_1);
724  if(ph< phi_1) ph = phi_2 - 0.5/g;
725  if(ph>=phi_2) ph = phi_1 + 0.5/g;
726  mPhi = int(g*(ph-phi_1));
727 
728  k = mPhi;
729  for(int i=0; i<mTheta; i++) k += value[i].size();
730  mIndex = k;
731  return k;
732 }
733 
734 #ifdef _USE_HEALPIX
735 //:
736 wavearray<int> skymap::neighbors(int index)
737 {
738 /*! Returns the neighboring pixels of pixel index in neighbors.
739  On exit, neighbors contains (in this order)
740  the pixel numbers of the SW, W, NW, N, NE, E, SE and S neighbor
741  of the pixel index. If a neighbor does not exist (this can only be the case
742  for the W, N, E and S neighbors), its entry is set to -1.
743 */
744 
745  wavearray<int> neighbors(8);
746 
747  if(healpix!=NULL) {
748  fix_arr< int, 8 > result;
749  healpix->neighbors (index, result);
750  for(int k=0;k<8;k++) neighbors[k]=result[k];
751  }
752 
753  return neighbors;
754 }
755 #endif
756 
757 #ifdef _USE_HEALPIX
758 void skymap::median(double radius) {
759 //
760 // Applies a median filter with the desired radius
761 //
762 // Input: radius - the radius (in degrees) of the disc
763 
764  if(healpix==NULL) {
765  cout << "skymap::median Error : healpix not initialized" << endl;
766  exit(1);
767  }
768 
769  radius*=deg2rad;
770 
771  // fill healpix map with skymap values
772  int L = healpix->Npix();
773  for(int l=0;l<L;l++) (*healpix)[l]=this->get(l);
774 
775  // This method works in both RING and NEST schemes, but is
776  // considerably faster in the RING scheme.
777  if (healpix->Scheme()==NEST) healpix->swap_scheme();
778 
779  vector<int> list;
780  vector<float> list2;
781  for (int l=0; l<L; ++l) {
782  // Returns the numbers of all pixels that lie at least partially within
783  // radius of healpix->pix2ang(l) in list. It may also return a few pixels
784  // which do not lie in the disk at all
785  healpix->query_disc(healpix->pix2ang(l),radius,list);
786  list2.resize(list.size());
787  for (tsize i=0; i<list.size(); ++i) list2[i] = (*healpix)[list[i]];
788  double x=::median(list2.begin(),list2.end());
789  // fill skymap with healpix map values
790  this->set(l,x);
791  }
792 
793  return;
794 }
795 #endif
796 
797 #ifdef _USE_HEALPIX
798 void skymap::smoothing(double fwhm, int nlmax, int num_iter) {
799 //
800 // Applies a convolution with a Gaussian beam with an FWHM of fwhm_arcmin arcmin to alm.
801 //
802 // Input: fwhm - Gaussian beam with an FWHM (degrees)
803 // note : If fwhm<0, a deconvolution with -fwhm is performed
804 // nlmax - l max of spherical harmonic coefficients.
805 // num_iter - the number of iterations (default=0 is identical to map2alm())
806 
807  if (fwhm<0)
808  cout << "NOTE: negative FWHM supplied, doing a deconvolution..." << endl;
809 
810  if(healpix==NULL) {
811  cout << "skymap::smoothing Error : healpix not initialized" << endl;
812  exit(1);
813  }
814 
815  int L = healpix->Npix();
816 
817  // fill healpix map with skymap values
818  for(int l=0;l<L;l++) (*healpix)[l]=this->get(l);
819  double avg=healpix->average();
820  healpix->Add(double(-avg));
821 
822  // get alm coefficients
823  wat::Alm alm = getAlm(nlmax, num_iter);
824  // applies gaussian smoothing to alm
825  alm.smoothWithGauss(fwhm);
826  // converts a set of alm to a HEALPix map.
827  setAlm(alm);
828 
829  // fill skymap with healpix map values
830  healpix->Add(double(avg));
831  for(int l=0;l<L;l++) this->set(l,(*healpix)[l]);
832 
833  return;
834 }
835 #endif
836 
837 #ifdef _USE_HEALPIX
838 void skymap::rotate(double psi, double theta, double phi, int nlmax, int num_iter) {
839 //
840 // Rotates alm through the Euler angles psi, theta and phi.
841 // The Euler angle convention is right handed, rotations are active.
842 // - psi is the first rotation about the z-axis (vertical)
843 // - then theta about the ORIGINAL (unrotated) y-axis
844 // - then phi about the ORIGINAL (unrotated) z-axis (vertical)
845 // relates Alm */
846 
847  if(healpix==NULL) {
848  cout << "skymap::smoothing Error : healpix not initialized" << endl;
849  exit(1);
850  }
851 
852  // get alm coefficients
853  wat::Alm alm = getAlm(nlmax, num_iter);
854  // applies gaussian smoothing to alm
855  alm.rotate(psi, theta, phi);
856  // converts a set of alm to a HEALPix map.
857  setAlm(alm);
858 
859  return;
860 }
861 #endif
862 
863 #ifdef _USE_HEALPIX
864 void skymap::setAlm(wat::Alm alm) {
865 //
866 // Converts a set of a_lm to a HEALPix map.
867 //
868 // Input : alm - spherical harmonic coefficients.
869 
870  if(healpix==NULL) {
871  cout << "skymap::setAlm Error : healpix not initialized" << endl;
872  exit(1);
873  }
874 
875  // comvert wat::alm to alm
876  Alm<xcomplex<double> > _alm(alm.Lmax(),alm.Mmax());
877  for(int l=0;l<=_alm.Lmax();l++) {
878  int limit = TMath::Min(l,_alm.Mmax());
879  for (int m=0; m<=limit; m++)
880  _alm(l,m)=complex<double>(alm(l,m).real(),alm(l,m).imag());
881  }
882  // converts a set of alm to a HEALPix map.
883  alm2map(_alm,*healpix);
884 
885  // fill skymap with healpix map values
886  int L = healpix->Npix();
887  for(int l=0;l<L;l++) this->set(l,(*healpix)[l]);
888 
889  return;
890 }
891 #endif
892 
893 #ifdef _USE_HEALPIX
894 wat::Alm
895 skymap::getAlm(int nlmax, int num_iter) {
896 //
897 // Converts a Healpix map to a set of a_lms, using an iterative scheme
898 // which is more accurate than plain map2alm().
899 //
900 // Input: nlmax - l max of spherical harmonic coefficients.
901 // num_iter - the number of iterations (default=0 is identical to map2alm())
902 
903 
904  if(healpix==NULL) {
905  cout << "skymap::getAlm Error : healpix not initialized" << endl;
906  exit(1);
907  }
908 
909  if(nlmax<0) nlmax=0;
910 
911  // fill healpix map with skymap values
912  int L = healpix->Npix();
913  for(int l=0;l<L;l++) (*healpix)[l]=this->get(l);
914 
915  // create weight array and set to 1
916  // weight array containing the weights for the individual rings of the map
917  arr<double> weight(2*healpix->Nside());
918  for(int i=0;i<2*healpix->Nside();i++) weight[i]=1.0;
919 
920  // get alm coefficients
921  Alm<xcomplex<double> > alm(nlmax,nlmax);
922  if (healpix->Scheme()==NEST) healpix->swap_scheme();
923  // Converts a Healpix map to a set of a_lms, using an iterative scheme
924  map2alm_iter(*healpix,alm,num_iter,weight);
925 
926  // comverts alm to wat::alm
927  wat::Alm _alm = alm;
928 
929  return _alm;
930 }
931 #endif
932 
933 #ifdef _USE_HEALPIX
934 void
935 skymap::resample(int order) {
936 //
937 // Resample the Healpix map to the healpix order
938 // the resampling is done using the spherical harmonic coefficients
939 //
940 // Input: order - healpix order of the resampled skymap
941 
942  if(healpix==NULL) {
943  cout << "skymap::resample Error : healpix not initialized" << endl;
944  exit(1);
945  }
946 
947  if(order == getOrder()) return; // nothing to do
948 
949  // get alm coefficients
950  int nlmax = 2*healpix->Nside();
951  wat::Alm alm = getAlm(nlmax);
952 
953  // skymap with new healpix order
954  *this = skymap(order);
955 
956  int _nlmax = 2*healpix->Nside();
957  wat::Alm _alm(_nlmax,_nlmax);
958 
959  // fill new _alm with original alm
960  int min_nlmax = TMath::Min(nlmax,_nlmax);
961  for(int l=0;l<=min_nlmax;l++) {
962  int limit = TMath::Min(l,alm.Mmax());
963  for (int m=0; m<=limit; m++) _alm(l,m)=alm(l,m);
964  }
965 
966  // set new _alm coefficients
967  setAlm(_alm);
968 
969  return;
970 }
971 #endif
972 
973 #ifdef _USE_HEALPIX
974 int
975 skymap::getRings() {
976 //
977 // return the number of division in theta
978 
979  if(healpix==NULL) {
980  cout << "skymap::getRings Error : healpix not initialized" << endl;
981  exit(1);
982  }
983 
984  int nrings = 1; // divisions on theta
985  for(int i=0;i<=healpix->Order();i++) nrings=2*nrings+1;
986 
987  return nrings;
988 }
989 #endif
990 
991 #ifdef _USE_HEALPIX
992 int
993 skymap::getRingPixels(int ring) {
994 //
995 // return the number of pixels in the ring
996 
997  if(healpix==NULL) {
998  cout << "skymap::getRingPixels Error : healpix not initialized" << endl;
999  exit(1);
1000  }
1001 
1002  if(ring<1 || ring>getRings()) {
1003  cout << "skymap::getRingPixels Error : ring index " << ring
1004  << " not allowed [1:" << getRings() << "]" << endl;
1005  exit(1);
1006  }
1007 
1008  int startpix; int ringpix; double costheta; double sintheta; bool shifted=false;
1009  healpix->get_ring_info(ring, startpix, ringpix, costheta, sintheta, shifted);
1010  return ringpix;
1011 }
1012 #endif
1013 
1014 #ifdef _USE_HEALPIX
1015 int
1016 skymap::getStartRingPixel(int ring) {
1017 //
1018 // return the start pixel index in the ring
1019 
1020  if(healpix==NULL) {
1021  cout << "skymap::getStartRingPixel Error : healpix not initialized" << endl;
1022  exit(1);
1023  }
1024 
1025  if(ring<1 || ring>getRings()) {
1026  cout << "skymap::getRingPixels Error : ring index " << ring
1027  << " not allowed [1:" << getRings() << "]" << endl;
1028  exit(1);
1029  }
1030 
1031  int startpix; int ringpix; double costheta; double sintheta; bool shifted=false;
1032  healpix->get_ring_info(ring, startpix, ringpix, costheta, sintheta, shifted);
1033  return startpix;
1034 }
1035 #endif
1036 
1037 #ifdef _USE_HEALPIX
1038 int
1039 skymap::getEulerCharacteristic(double threshold) {
1040 //
1041 // return the get the Euler characteristic for pixels value > threshold
1042 
1043  if(healpix==NULL) {
1044  cout << "skymap::getEulerCharacteristic Error : healpix not initialized" << endl;
1045  exit(1);
1046  }
1047 
1048 
1049  int nV=0; // number of vertices (= number of pixels)
1050  int nE=0; // number of edges
1051  int nF=0; // number of faces
1052 
1053  int F1=0;
1054  int F2=0;
1055  int F3=0;
1056  int F4=0;
1057 
1059 
1060  double a;
1061  for(int l=0;l<size();l++) {
1062  a=get(l);
1063  if(a>threshold){
1064  nV++;
1065  index = neighbors(l);
1066  a=get(index[0]);
1067  if(a>threshold) {nE++;F1++;F4++;}
1068  a=get(index[1]);
1069  if(a>threshold) {F1++;}
1070  a=get(index[2]);
1071  if(a>threshold) {nE++;F1++;F2++;}
1072  a=get(index[3]);
1073  if(a>threshold) {F2++;}
1074  a=get(index[4]);
1075  if(a>threshold) {nE++;F2++;F3++;}
1076  a=get(index[5]);
1077  if(a>threshold) {F3++;}
1078  a=get(index[6]);
1079  if(a>threshold) {nE++;F3++;F4++;}
1080  a=get(index[7]);
1081  if(a>threshold) {F4++;}
1082  nF+=F1/3+F2/3+F3/3+F4/3;
1083  F1=0; F2=0; F3=0; F4=0;
1084  }
1085  }
1086  nE/=2;nF/=4;
1087 
1088  return nV-nE+nF;
1089 }
1090 #endif
1091 
1093 
1094  TString name = fname;
1095  name.ReplaceAll(" ","");
1096  TObjArray* token = TString(fname).Tokenize(TString("."));
1097  TObjString* ext_tok = (TObjString*)token->At(token->GetEntries()-1);
1098  TString ext = ext_tok->GetString();
1099  if(ext=="dat") {
1100  //: dump skymap into binary file
1101  DumpBinary(fname,0);
1102  } else
1103 #ifdef _USE_HEALPIX
1104  if(ext=="fits") {
1105  //: dump skymap into fits file
1106  Dump2fits(fname);
1107  } else
1108 #endif
1109  if(ext=="root") {
1110  //: dump skymap object into root file
1111  DumpObject(fname);
1112  } else {
1113  cout << "skymap operator (>>) : file type " << ext.Data() << " not supported" << endl;
1114  }
1115  return fname;
1116 }
1117 
1118 //______________________________________________________________________________
1119 void skymap::Streamer(TBuffer &R__b)
1120 {
1121  // Stream an object of class skymap.
1122 
1123  UInt_t R__s, R__c;
1124  if (R__b.IsReading()) {
1125  Version_t R__v = R__b.ReadVersion(&R__s, &R__c); if (R__v) { }
1126  TNamed::Streamer(R__b);
1127  {
1128  vector<vectorD> &R__stl = value;
1129  R__stl.clear();
1130  TClass *R__tcl1 = TBuffer::GetClass(typeid(vector<double,allocator<double> >));
1131  if (R__tcl1==0) {
1132  Error("value streamer","Missing the TClass object for vector<double,allocator<double> >!");
1133  return;
1134  }
1135  int R__i, R__n;
1136  R__b >> R__n;
1137  R__stl.reserve(R__n);
1138  for (R__i = 0; R__i < R__n; R__i++) {
1139  vector<double,allocator<double> > R__t;
1140  R__b.StreamObject(&R__t,R__tcl1);
1141  R__stl.push_back(R__t);
1142  }
1143  }
1144  {
1145  vector<int> &R__stl = index;
1146  R__stl.clear();
1147  int R__i, R__n;
1148  R__b >> R__n;
1149  R__stl.reserve(R__n);
1150  for (R__i = 0; R__i < R__n; R__i++) {
1151  int R__t;
1152  R__b >> R__t;
1153  R__stl.push_back(R__t);
1154  }
1155  }
1156  if(R__v > 2) R__b >> sms;
1157  R__b >> theta_1;
1158  R__b >> theta_2;
1159  R__b >> phi_1;
1160  R__b >> phi_2;
1161  R__b >> gps;
1162  R__b >> mTheta;
1163  R__b >> mPhi;
1164  R__b >> mIndex;
1165  if(R__v > 1) {
1166  R__b >> healpix_order;
1167  if(healpix_order > 0) {
1168 #ifdef _USE_HEALPIX
1169  healpix = new Healpix_Map<double>(healpix_order,RING);
1170 #endif
1171  } else {
1172  healpix = NULL;
1173  healpix_order = 0;
1174  }
1175  }
1176  R__b.CheckByteCount(R__s, R__c, skymap::IsA());
1177  } else {
1178  R__c = R__b.WriteVersion(skymap::IsA(), kTRUE);
1179  TNamed::Streamer(R__b);
1180  {
1181  vector<vectorD> &R__stl = value;
1182  int R__n=(&R__stl) ? int(R__stl.size()) : 0;
1183  R__b << R__n;
1184  if(R__n) {
1185  TClass *R__tcl1 = TBuffer::GetClass(typeid(vector<double,allocator<double> >));
1186  if (R__tcl1==0) {
1187  Error("value streamer","Missing the TClass object for vector<double,allocator<double> >!");
1188  return;
1189  }
1190  vector<vectorD>::iterator R__k;
1191  for (R__k = R__stl.begin(); R__k != R__stl.end(); ++R__k) {
1192  R__b.StreamObject((vector<double,allocator<double> >*)&(*R__k),R__tcl1);
1193  }
1194  }
1195  }
1196  {
1197  vector<int> &R__stl = index;
1198  int R__n=(&R__stl) ? int(R__stl.size()) : 0;
1199  R__b << R__n;
1200  if(R__n) {
1201  vector<int>::iterator R__k;
1202  for (R__k = R__stl.begin(); R__k != R__stl.end(); ++R__k) {
1203  R__b << (*R__k);
1204  }
1205  }
1206  }
1207  R__b << sms;
1208  R__b << theta_1;
1209  R__b << theta_2;
1210  R__b << phi_1;
1211  R__b << phi_2;
1212  R__b << gps;
1213  R__b << mTheta;
1214  R__b << mPhi;
1215  R__b << mIndex;
1216  R__b << healpix_order;
1217  R__b.SetByteCount(R__c, kTRUE);
1218  }
1219 }
1220 
wavearray< double > t(hp.size())
void smoothWithGauss(double fwhm)
Definition: alm.hh:189
#define F1
Definition: Regression_H1.C:13
virtual size_t size() const
Definition: wavearray.hh:127
double norm(double=0.)
Definition: skymap.cc:496
static double g(double e)
Definition: GNGen.cc:98
par[0] value
int mPhi
Definition: skymap.hh:309
char cmd[1024]
wavearray< double > a(hp.size())
int mTheta
Definition: skymap.hh:308
int Lmax() const
Returns the maximum l.
Definition: alm.hh:52
WSeries< float > v[nIFO]
Definition: cwb_net.C:62
skymap & operator-=(const skymap &)
Definition: skymap.cc:299
par[0] name
int n
Definition: cwb_net.C:10
TString("c")
double gps
Definition: skymap.hh:307
cout<< endl;cout<< "ts size = "<< ts.size()<< " ts rate = "<< ts.rate()<< endl;tf.Forward(ts, wdm);int levels=tf.getLevel();cout<< "tf size = "<< tf.size()<< endl;double dF=tf.resolution();double dT=1./(2 *dF);cout<< "rate(hz) : "<< RATE<< "\t layers : "<< nLAYERS<< "\t dF(hz) : "<< dF<< "\t dT(ms) : "<< dT *1000.<< endl;int itime=TIME_PIXEL_INDEX;int ifreq=FREQ_PIXEL_INDEX;int index=(levels+1)*itime+ifreq;double time=itime *dT;double freq=(ifreq >0)?ifreq *dF:dF/4;cout<< endl;cout<< "PIXEL TIME = "<< time<< " sec "<< endl;cout<< "PIXEL FREQ = "<< freq<< " Hz "<< endl;cout<< endl;wavearray< double > x
float theta
double median(std::vector< double > &vec)
Definition: wavegraph.cc:467
delete[] radius
TH2F * ph
skymap & operator*=(const skymap &)
Definition: skymap.cc:328
TRandom3 P
Definition: compare_bkg.C:296
virtual ~skymap()
Definition: skymap.cc:232
double min()
Definition: skymap.cc:441
Long_t flags
STL namespace.
std::vector< int > index
Definition: skymap.hh:301
double theta_1
Definition: skymap.hh:303
Long_t size
watplot p1(const_cast< char * >("TFMap1"))
int m
Definition: cwb_net.C:10
double max()
Definition: skymap.cc:420
void downsample(wavearray< short > &, size_t=4)
Definition: skymap.cc:518
int j
Definition: cwb_net.C:10
i drho i
TList * list
double theta_2
Definition: skymap.hh:304
double mean()
Definition: skymap.cc:462
skymap()
Definition: skymap.cc:29
#define PI
Definition: watfun.hh:14
size_t mode
size_t getSkyIndex(double th, double ph)
param: theta param: phi
Definition: skymap.cc:702
ofstream out
Definition: cwb_merge.C:196
pTF[i] DumpBinary(file)
tlive_fix Write()
float phi
std::vector< vectorD > value
Definition: skymap.hh:300
float psi
#define F2
Definition: Regression_H1.C:14
int Mmax() const
Returns the maximum m.
Definition: alm.hh:54
double deg2rad
double fraction(double)
Definition: skymap.cc:479
skymap & operator+=(const skymap &)
Definition: skymap.cc:270
i() int(T_cor *100))
void DumpObject(char *)
Definition: skymap.cc:649
double sms
Definition: skymap.hh:302
char fname[1024]
char * operator>>(char *fname)
Definition: skymap.cc:1092
int mIndex
Definition: skymap.hh:310
int k
TObjArray * token
Definition: skymap.hh:45
void DumpBinary(char *, int)
Definition: skymap.cc:657
TFile * ifile
int npix
virtual void DumpBinary(const char *, int=0)
Definition: wavearray.cc:335
Definition: alm.hh:175
s s
Definition: cwb_net.C:137
double rad2deg
double gps
void rotate(double psi, double theta, double phi)
Definition: alm.hh:213
watplot p2(const_cast< char * >("TFMap2"))
wavearray< int > index
double fabs(const Complex &x)
Definition: numpy.cc:37
string file
Definition: cwb_online.py:385
skymap & operator=(const skymap &)
Definition: skymap.cc:238
int estat
int l
Definition: cbc_plots.C:434
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
Long_t mt
double phi_2
Definition: skymap.hh:306
DataType_t * data
Definition: wavearray.hh:301
sm set(30, 10)
Long_t id
TH1 * t1
double phi_1
Definition: skymap.hh:305
double get(size_t i)
param: sky index
Definition: skymap.cc:681
bool * healpix
Definition: skymap.hh:319
char fName[256]
virtual void resize(unsigned int)
Definition: wavearray.cc:445
skymap & operator/=(const skymap &)
Definition: skymap.cc:355
exit(0)
size_t healpix