Logo coherent WaveBurst  
Library Reference Guide
Logo
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
skymap.hh
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2019 Sergey Klimenko, Gabriele Vedovato
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 
19 //**********************************************************
20 // Wavelet Analysis Tool
21 // Sergey Klimenko, University of Florida
22 // sky map data container for network analysis
23 // used with DMT and ROOT
24 //**********************************************************
25 
26 #ifndef SKYMAP_HH
27 #define SKYMAP_HH
28 
29 #include <iostream>
30 #include <vector>
31 #include "complex"
32 #include "wavearray.hh"
33 #include "watfun.hh"
34 #include "TNamed.h"
35 #include "TFile.h"
36 #include "TString.h"
37 #ifdef _USE_HEALPIX
38 #include "alm.hh"
39 #endif
40 
41 #ifndef __CINT__
42 #ifdef _USE_HEALPIX
43 #include "xcomplex.h"
44 //#include "paramfile.h"
45 #include "healpix_data_io.h"
46 #include "alm.h"
47 #include "healpix_map.h"
48 #include "healpix_map_fitsio.h"
49 #include "alm_healpix_tools.h"
50 #include "alm_powspec_tools.h"
51 #include "fitshandle.h"
52 //#include "levels_facilities.h"
53 #include "lsconstants.h"
54 #endif
55 #endif
56 
57 typedef std::vector<double> vectorD;
58 
59 // GPS seconds of the J2000.0 epoch (2000 JAN 1 12h UTC).
60 #define EPOCH_J2000_0_GPS 630763213
61 
62 
63 class skymap : public TNamed
64 {
65  public:
66 
67  // constructors
68 
69  //: Default constructor
70  skymap();
71 
72  //: skymap constructor
73  //!param - step on phi and theta
74  //!param - theta begin
75  //!param - theta end
76  //!param - phi begin
77  //!param - phi end
78  skymap(double,double=0.,double=180.,double=0.,double=360.);
79 
80  //: skymap constructor
81  //!param - healpix order
82  skymap(int);
83 
84  //: skymap constructor
85  //!param - fits file
86  skymap(char*);
87 
88  //: skymap constructor
89  //!param ifile - root file name
90  //!param name - object name
91  skymap(TString ifile, TString name="skymap");
92 
93  //: Copy constructor
94  //!param: value - object to copy from
95  skymap(const skymap&);
96 
97  //: destructor
98  virtual ~skymap();
99 
100  // operators
101 
102  skymap& operator = (const skymap&);
103  skymap& operator += (const skymap&);
104  skymap& operator -= (const skymap&);
105  skymap& operator *= (const skymap&);
106  skymap& operator /= (const skymap&);
107  skymap& operator = (const double);
108  skymap& operator *= (const double);
109  skymap& operator += (const double);
110  char* operator >> (char* fname);
111 
112  // accessors
113 
114  //: get skymap value at index i
115  //: sets index nTheta and mPhi
116  //!param: sky index
117  double get(size_t i);
118 
119  //: set skymap value at index i,j
120  //!param: sky index
121  //!param: value to set
122  inline void set(size_t i, double a) {
123  if(int(i) != mIndex) this->get(i); // fill in sky index
124  if(mTheta>=0) value[mTheta][mPhi] = a;
125  }
126 
127  //: add skymap value at index i,j
128  //!param: sky index
129  //!param: value to add
130  inline void add(size_t i ,double a) {
131  if(int(i) != mIndex) this->get(i); // fill in sky index
132  if(mTheta>=0) value[mTheta][mPhi] += a;
133  }
134 
135  //: get skymap size
136  inline size_t size() {
137  size_t n = value.size();
138  size_t m = 0;
139  for(size_t i=0; i<n; i++) m += value[i].size();
140  return m;
141  }
142 
143  //: skymap sizes
144  inline size_t size(size_t k) {
145  if(!k) return value.size();
146  else if(k<=value.size()) return value[k-1].size();
147  else return 0;
148  }
149 
150  //: get sky index at theta,phi
151  //!param: theta
152  //!param: phi
153  size_t getSkyIndex(double th, double ph);
154 
155  //: get skymap value at theta,phi
156  //!param: theta
157  //!param: phi
158  inline double get(double th, double ph) {
159  mIndex = getSkyIndex(th,ph);
160  return get(mIndex);
161  }
162 
163  //: get phi value
164  inline double getPhi(size_t i) {
165  if(healpix!=NULL) {
166 #ifdef _USE_HEALPIX
167  pointing P = healpix->pix2ang(i);
168  return rad2deg*P.phi;
169 #else
170  return 0.;
171 #endif
172  } else {
173  if(int(i) != mIndex) this->get(i); // fill in sky index
174  size_t n = this->value[mTheta].size();
175  if(mTheta>=0 && n>1)
176  return phi_1+(mPhi+0.5)*(phi_2-phi_1)/double(n);
177  else return (phi_2-phi_1)/2.;
178  }
179  }
180 
181  //: get phi step
182  inline double getPhiStep(size_t i) {
183  if(int(i) != mIndex) this->get(i); // fill in sky index
184  size_t n = this->value[mTheta].size();
185  if(mTheta>=0 && n)
186  return (phi_2-phi_1)/double(n);
187  else return 0.;
188  }
189 
190  // get RA angle from EFEC phi angle in degrees
191  // Earth angular velocity defines duration of the siderial day
192  // 1 siderial day = 23h 56m 04.0905s
193  // (from http://www.maa.mhn.de/Scholar/times.html)
194  // phiRA function is implemented by Gabriele Vedovato and compared
195  // with LAL - agrees within 0.01 degrees
196 
197  static inline double phiRA(double ph, double gps, bool inverse=false) {
198  double sidereal_time; // sidereal time in sidereal seconds. (magic)
199  double t = (gps-EPOCH_J2000_0_GPS)/86400./36525.;
200 
201  sidereal_time = (-6.2e-6 * t + 0.093104) * t * t + 67310.54841;
202  sidereal_time += 8640184.812866*t + 3155760000.*t;
203 
204  double gmst = 360.*sidereal_time/86400.;
205  if(inverse) gmst=-gmst;
206 
207  double ra = fmod( ph + gmst, 360. );
208  return ra<0. ? ra+360 : ra;
209 
210  }
211 
212  inline double phi2RA(double ph, double gps) { return phiRA(ph,gps,false); }
213  inline double RA2phi(double ph, double gps) { return phiRA(ph,gps,true); }
214 
215  inline double getRA(size_t i) {
216  if(this->gps<=0.) return 0.;
217 // double omega = 7.292115090e-5; // http://hypertextbook.com/facts/2002/JasonAtkins.shtml
218 // double gps2000 = 630720013; // GPS time at 01/01/2000 00:00:00 UTC
219 // double GMST2000 = 24110.54841; // GMST at UT1=0 on Jan 1, 2000
220  return phi2RA(getPhi(i),this->gps);
221  }
222 
223  //: get theta value
224  inline double getTheta(size_t i) {
225  if(healpix!=NULL) {
226 #ifdef _USE_HEALPIX
227  pointing P = healpix->pix2ang(i);
228  return rad2deg*P.theta;
229 #else
230  return 0.;
231 #endif
232  } else {
233  size_t n = this->value.size()-1;
234  if(int(i) != mIndex) this->get(i); // fill in sky index
235  if(mTheta >= 0 && n)
236  return theta_1+mTheta*(theta_2-theta_1)/double(n);
237  else return (theta_2-theta_1)/2.;
238  }
239  }
240 
241  //: get theta step
242  inline double getThetaStep(size_t i) {
243  size_t n = this->value.size()-1;
244  if(int(i) != mIndex) this->get(i); // fill in sky index
245  if(mTheta >= 0 && n)
246  return (theta_2-theta_1)/double(n);
247  else return 0.;
248  }
249 
250  // get declination angle from EFEC theta angle in degrees
251  inline double getDEC(size_t i) { return -(getTheta(i)-90.); }
252 
253  //: find and return maximum value in skymap
254  //: set mTheta and mPhi to be theta and phi index for the maximum value
255  double max();
256 
257  //: find and return minimum value in skymap
258  //: set mTheta and mPhi to be theta and phi index for the minimum value
259  double min();
260 
261  //: find and return mean value over entire skymap
262  double mean();
263 
264  //: find for what fraction of the sky the statistic > threshold t
265  double fraction(double);
266 
267  //: normalize skymap
268  double norm(double=0.);
269 
270  //: downsample sky index array
271  //: input sky index array
272  //: down-sampling option: 2 or 4
273  void downsample(wavearray<short>&, size_t=4);
274 
275  //: dump skymap into binary file
276  //: parameter: file name
277  //: parameter: append mode
278  void DumpBinary(char*, int);
279 
280 #ifdef _USE_HEALPIX
281  //: dump skymap into fits file
282  //: file : output fits file name [ext : .fits or .fits.gz]
283  //: gps_obs : gps time of the observation
284  //: configur : software configuration used to process the data
285  //: TTYPE1 : label for field 1
286  //: TUNIT1 : physical unit of field 1
287  //: coordsys : Pixelisation coordinate system [c/C : select celestial]
288  void Dump2fits(const char* file, double gps_obs=0, const char configur[]="",
289  const char TTYPE1[]="", const char TUNIT1[]="", char coordsys='x'); // *MENU*
290 #endif
291 
292  //: dump skymap object into root file
293  void DumpObject(char*);
294 
295  // return 1 if healpix else 0
296  int getType() {if(healpix==NULL) return 0; else return 1;}
297 
298  // works only with the healpix scheme !!!
299 #ifdef _USE_HEALPIX
300  wavearray<int> neighbors(int index);
301  void median(double radius);
302  void smoothing(double fwhm, int nlmax=256, int num_iter=0);
303  void rotate(double psi, double theta, double phi, int nlmax=256, int num_iter=0);
304  void setAlm(wat::Alm alm);
305  wat::Alm getAlm(int nlmax, int num_iter=0);
306  void resample(int order);
307  int getRings();
308  int getRingPixels(int ring);
309  int getStartRingPixel(int ring);
310  int getEulerCharacteristic(double threshold);
311 #endif
312 
313  // return the healpix order (if=0 healpix skygrid is disabled)
314  int getOrder() {return healpix_order;}
315 
316 // data members
317 
318  std::vector<vectorD> value; // skymap map array
319  std::vector<int> index; // sample index array
320  double sms; // step on phi and theta
321  double theta_1; // theta range begin
322  double theta_2; // theta range end
323  double phi_1; // phi range begin
324  double phi_2; // phi range end
325  double gps; // gps time
326  int mTheta; // theta index
327  int mPhi; // phi index
328  int mIndex; // sky index
329 
330 private:
331  int healpix_order; // healpix order (if=0 healpix is disabled)
332 
333 #ifndef __CINT__
334 #ifdef _USE_HEALPIX
335  Healpix_Map<double>* healpix;//!
336 #else
337  bool* healpix;
338 #endif
339  double deg2rad;
340  double rad2deg;
341 #endif
342 
343  ClassDef(skymap,5)
344 
345 }; // class skymap
346 
347 using namespace std;
348 
349 namespace {
350 
351 template<typename Iterator> typename iterator_traits<Iterator>::value_type
352  median(Iterator first, Iterator last)
353  {
354  Iterator mid = first+(last-first-1)/2;
355  nth_element(first,mid,last);
356  if ((last-first)&1) return *mid;
357  return typename iterator_traits<Iterator>::value_type
358  (0.5*((*mid)+(*min_element(mid+1,last))));
359  }
360 
361 } // unnamed namespace
362 
363 #endif // SKYMAP_HH
364 
wavearray< double > t(hp.size())
double norm(double=0.)
Definition: skymap.cc:514
int mPhi
Definition: skymap.hh:327
#define EPOCH_J2000_0_GPS
Definition: skymap.hh:60
wavearray< double > a(hp.size())
int mTheta
Definition: skymap.hh:326
skymap & operator-=(const skymap &)
Definition: skymap.cc:317
par [0] name
int n
Definition: cwb_net.C:28
TString("c")
double gps
Definition: skymap.hh:325
float theta
double deg2rad
Definition: skymap.hh:339
double median(std::vector< double > &vec)
Definition: wavegraph.cc:485
TH2F * ph
skymap & operator*=(const skymap &)
Definition: skymap.cc:346
virtual ~skymap()
Definition: skymap.cc:250
double min()
Definition: skymap.cc:459
STL namespace.
double getTheta(size_t i)
Definition: skymap.hh:224
std::vector< int > index
Definition: skymap.hh:319
double getThetaStep(size_t i)
Definition: skymap.hh:242
double theta_1
Definition: skymap.hh:321
int m
Definition: cwb_net.C:28
double getPhiStep(size_t i)
Definition: skymap.hh:182
double max()
Definition: skymap.cc:438
void downsample(wavearray< short > &, size_t=4)
Definition: skymap.cc:536
i drho i
double theta_2
Definition: skymap.hh:322
double mean()
Definition: skymap.cc:480
skymap()
Definition: skymap.cc:47
double rad2deg
Definition: skymap.hh:340
size_t getSkyIndex(double th, double ph)
param: theta param: phi
Definition: skymap.cc:720
float phi
double ra
Definition: ConvertGWGC.C:46
std::vector< vectorD > value
Definition: skymap.hh:318
float psi
double fraction(double)
Definition: skymap.cc:497
skymap & operator+=(const skymap &)
Definition: skymap.cc:288
double getRA(size_t i)
Definition: skymap.hh:215
void DumpObject(char *)
Definition: skymap.cc:667
int getOrder()
Definition: skymap.hh:314
double sms
Definition: skymap.hh:320
double getDEC(size_t i)
Definition: skymap.hh:251
char fname[1024]
Class for storing spherical harmonic coefficients.
char * operator>>(char *fname)
Definition: skymap.cc:1110
int mIndex
Definition: skymap.hh:328
int k
double RA2phi(double ph, double gps)
Definition: skymap.hh:213
std::vector< double > vectorD
Definition: skymap.hh:57
Definition: skymap.hh:63
void DumpBinary(char *, int)
Definition: skymap.cc:675
TFile * ifile
double phi2RA(double ph, double gps)
Definition: skymap.hh:212
Definition: alm.hh:193
double getPhi(size_t i)
Definition: skymap.hh:164
int getType()
Definition: skymap.hh:296
skymap & operator=(const skymap &)
Definition: skymap.cc:256
int healpix_order
Definition: skymap.hh:331
size_t size(size_t k)
Definition: skymap.hh:144
double phi_2
Definition: skymap.hh:324
double phi_1
Definition: skymap.hh:323
bool * healpix
Definition: skymap.hh:337
size_t size()
Definition: skymap.hh:136
void add(size_t i, double a)
param: sky index param: value to add
Definition: skymap.hh:130
skymap & operator/=(const skymap &)
Definition: skymap.cc:373
static double phiRA(double ph, double gps, bool inverse=false)
Definition: skymap.hh:197