Logo coherent WaveBurst  
Library Reference Guide
Logo
skycoord.hh
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2019 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  * Package: sky coordinates (extract from the lal library)
21  * File name: skycoord.hh
22  * Author: Gabriele Vedovato (vedovato@lnl.infn.it)
23  * Release Date: 01/27/2012
24  * Version: 1.00
25  **********************************************************/
26 
27 #ifndef SKYCOORD_HH
28 #define SKYCOORD_HH
29 
30 #define LAL_DELTAGAL (0.473477302)
31 #define LAL_ALPHAGAL (3.366032942)
32 #define LAL_LGAL (0.576)
33 #define LAL_IEARTH 0.409092600600582871467239393761915655 // Earth inclination (2000),radians : LALConstants.h
34 
35 #define LAL_REARTH_SI 6378136.6 // Earth equatorial radius : LALConstants.h
36 #define LAL_EARTHFLAT (0.00335281)
37 #define LAL_HSERIES (0.0001) // value H below which we expand sqrt(1-H)
38 
39 #include <iostream>
40 #include <fstream>
41 #include <stdlib.h>
42 #include "skymap.hh"
43 
44 using namespace std;
45 
46 inline void
48 { // see LAL CelestialCoordinates.c
49  double deg2rad = TMath::Pi()/180.;
50  ilongitude*=deg2rad;
51  ilatitude*=deg2rad;
52 
53  double sinDGal = sin( LAL_DELTAGAL ); /* sin(delta_NGP) */
54  double cosDGal = cos( LAL_DELTAGAL ); /* cos(delta_NGP) */
55  double l = -LAL_LGAL; /* will be l-l(ascend) */
56  double sinB, cosB, sinL, cosL; /* sin and cos of b and l */
57  double sinD, sinA, cosA; /* sin and cos of delta and alpha */
58 
59  /* Compute intermediates. */
60  l += ilongitude;
61  sinB = sin( ilatitude );
62  cosB = cos( ilatitude );
63  sinL = sin( l );
64  cosL = cos( l );
65 
66  /* Compute components. */
67  sinD = cosB*cosDGal*sinL + sinB*sinDGal;
68  sinA = cosB*cosL;
69  cosA = sinB*cosDGal - cosB*sinL*sinDGal;
70 
71  /* Compute final results. */
72  olatitude = asin( sinD );
73  l = atan2( sinA, cosA ) + LAL_ALPHAGAL;
74 
75  /* Optional phase correction. */
76  if ( l < 0.0 )
77  l += TMath::TwoPi();
78  if ( l > 360. )
79  l -= TMath::TwoPi();
80  olongitude = l;
81 
82  double rad2deg = 180./TMath::Pi();
83  olongitude*=rad2deg;
84  olatitude*=rad2deg;
85 
86  return;
87 }
88 
89 inline void
91 { // see LAL CelestialCoordinates.c
92  double deg2rad = TMath::Pi()/180.;
93  ilongitude*=deg2rad;
94  ilatitude*=deg2rad;
95 
96  double sinDGal = sin( LAL_DELTAGAL ); /* sin(delta_NGP) */
97  double cosDGal = cos( LAL_DELTAGAL ); /* cos(delta_NGP) */
98  double a = -LAL_ALPHAGAL; /* will be alpha-alpha_NGP */
99  double sinD, cosD, sinA, cosA; /* sin and cos of delta and alpha */
100  double sinB, sinL, cosL; /* sin and cos of b and l */
101 
102  /* Compute intermediates. */
103  a += ilongitude;
104  sinD = sin( ilatitude );
105  cosD = cos( ilatitude );
106  sinA = sin( a );
107  cosA = cos( a );
108 
109  /* Compute components. */
110  sinB = cosD*cosDGal*cosA + sinD*sinDGal;
111  sinL = sinD*cosDGal - cosD*cosA*sinDGal;
112  cosL = cosD*sinA;
113 
114  /* Compute final results. */
115  olatitude = asin( sinB );
116  a = atan2( sinL, cosL ) + LAL_LGAL;
117 
118  /* Optional phase correction. */
119  if ( a < 0.0 )
120  a += TMath::TwoPi();
121  if ( a > 360. )
122  a -= TMath::TwoPi();
123  olongitude = a;
124 
125  double rad2deg = 180./TMath::Pi();
126  olongitude*=rad2deg;
127  olatitude*=rad2deg;
128 
129  return;
130 }
131 
132 inline void
134 { // see LAL CelestialCoordinates.c
135  double deg2rad = TMath::Pi()/180.;
136  ilongitude*=deg2rad;
137  ilatitude*=deg2rad;
138 
139  double sinE = sin( LAL_IEARTH ); /* sin(epsilon) */
140  double cosE = cos( LAL_IEARTH ); /* cos(epsilon) */
141  double sinB, cosB, sinL, cosL; /* sin and cos of b and l */
142  double sinD, sinA, cosA; /* sin and cos of delta and alpha */
143 
144  /* Compute intermediates. */
145  sinB = sin( ilatitude );
146  cosB = cos( ilatitude );
147  sinL = sin( ilongitude );
148  cosL = cos( ilongitude );
149 
150  /* Compute components. */
151  sinD = cosB*sinL*sinE + sinB*cosE;
152  sinA = cosB*sinL*cosE - sinB*sinE;
153  cosA = cosB*cosL;
154 
155  /* Compute final results. */
156  olatitude = asin( sinD );
157  olongitude = atan2( sinA, cosA );
158 
159  /* Optional phase correction. */
160  if ( olongitude < 0.0 )
161  olongitude += TMath::TwoPi();
162  if ( olongitude > 360. )
163  olongitude -= TMath::TwoPi();
164 
165  double rad2deg = 180./TMath::Pi();
166  olongitude*=rad2deg;
167  olatitude*=rad2deg;
168  olatitude=-olatitude;
169 
170  return;
171 }
172 
173 inline void
175 { // see LAL CelestialCoordinates.c
176  double deg2rad = TMath::Pi()/180.;
177  ilongitude*=deg2rad;
178  ilatitude*=deg2rad;
179 
180  double sinE = sin( LAL_IEARTH ); /* sin(epsilon) */
181  double cosE = cos( LAL_IEARTH ); /* cos(epsilon) */
182  double sinD, cosD, sinA, cosA; /* sin and cos of delta and alpha */
183  double sinB, sinL, cosL; /* sin and cos of b and l */
184 
185  /* Compute intermediates. */
186  sinD = sin( ilatitude );
187  cosD = cos( ilatitude );
188  sinA = sin( ilongitude );
189  cosA = cos( ilongitude );
190 
191  /* Compute components. */
192  sinB = sinD*cosE - cosD*sinA*sinE;
193  sinL = cosD*sinA*cosE + sinD*sinE;
194  cosL = cosD*cosA;
195 
196  /* Compute final results. */
197  olatitude = asin( sinB );
198  olongitude = atan2( sinL, cosL );
199 
200  /* Optional phase correction. */
201  if ( olongitude < 0.0 )
202  olongitude += TMath::TwoPi();
203  if ( olongitude > 360. )
204  olongitude -= TMath::TwoPi();
205 
206  double rad2deg = 180./TMath::Pi();
207  olongitude*=rad2deg;
208  olatitude*=rad2deg;
209  olatitude=-olatitude;
210 
211  return;
212 }
213 
214 inline void
215 GeodeticToGeocentric(double latitude, double longitude, double elevation, double& X, double& Y, double& Z) {
216 
217  double c, s; /* position components in and orthogonal to the equator */
218  double cosP, sinP; /* cosine and sine of latitude */
219  double fFac; /* ( 1 - f )^2 */
220  double x, y; /* Cartesian coordinates */
221  double maxComp, r; /* max{x,y,z}, and sqrt(x^2+y^2+z^2) */
222 
223 
224  /* Compute intermediates. */
225  fFac = 1.0 - LAL_EARTHFLAT;
226  fFac *= fFac;
227  cosP = cos( latitude );
228  sinP = sin( latitude );
229  c = sqrt( 1.0 / ( cosP*cosP + fFac*sinP*sinP ) );
230  s = fFac*c;
231  c = ( LAL_REARTH_SI*c + elevation )*cosP;
232  s = ( LAL_REARTH_SI*s + elevation )*sinP;
233 
234  /* Compute Cartesian coordinates. */
235  X = x = c*cos( longitude );
236  Y = y = c*sin( longitude );
237  Z = s;
238 
239  /* Compute the radius. */
240  maxComp = x;
241  if ( y > maxComp )
242  maxComp = y;
243  if ( s > maxComp )
244  maxComp = s;
245  x /= maxComp;
246  y /= maxComp;
247  s /= maxComp;
248  r = sqrt( x*x + y*y + s*s );
249 /*
250  // Compute the spherical coordinates, and exit.
251  location->radius = maxComp*r;
252  location->geocentric.longitude = longitude;
253  location->geocentric.latitude = asin( s / r );
254 */
255 }
256 
257 inline void
258 HeapSort( double* data, double length) {
259 
260  int i;
261  int j;
262  int k;
263  int n;
264  double temp;
265 
266  n=length;
267 
268  /* A vector of length 0 or 1 is already sorted. */
269  if(n<2)
270  {
271  cout << "A vector of length 0 or 1 is already sorted" << endl;
272  }
273 
274  /* Here is the heapsort algorithm. */
275  j=n-1;
276  n >>= 1;
277 
278  while(1){
279  if(n>0)
280  temp=data[--n];
281  else{
282  temp=data[j];
283  data[j]=data[0];
284  if(--j==0){
285  data[0]=temp;
286  break;
287  }
288  }
289  i=n;
290  k=(n << 1)+1;
291  while(k<=j){
292  if(k<j && data[k]<data[k+1])
293  k++;
294  if(temp<data[k]){
295  data[i]=data[k];
296  i=k;
297  k<<=1;
298  k++;
299  }else
300  k=j+1;
301  }
302  data[i]=temp;
303  }
304 }
305 
306 inline void
307 GeocentricToGeodetic(double X, double Y, double Z, double& latitude, double& longitude, double& elevation) {
308 
309  double x, y, z; /* normalized geocentric coordinates */
310  double pi; /* axial distance */
311 
312  /* Declare some local constants. */
313  const double rInv = 1.0 / LAL_REARTH_SI;
314  const double f1 = 1.0 - LAL_EARTHFLAT;
315  const double f2 = LAL_EARTHFLAT*( 2.0 - LAL_EARTHFLAT );
316 
317  /* See if we've been given a special set of coordinates. */
318  x = rInv*X;
319  y = rInv*Y;
320  z = rInv*Z;
321  pi = sqrt( x*x + y*y );
322  if ( pi == 0.0 ) {
323  longitude = atan2( y, x );
324  if ( z >= 0.0 ) {
325  latitude = TMath::PiOver2();
326  elevation = z - f1;
327  } else {
328  latitude = -TMath::PiOver2();
329  elevation = f1 - z;
330  }
331  elevation *= LAL_REARTH_SI;
332  }
333 
334  /* Do the general transformation even if z=0. */
335  else {
336  double za, e, f, p, q, d, v, w, g, h, t, phi, tanP;
337 
338  /* Compute intermediates variables. */
339  za = f1*fabs( z );
340  e = za - f2;
341  f = za + f2;
342  p = ( 4.0/3.0 )*( pi*pi + za*za - f2*f2 );
343  q = 8.0*f2*za;
344  d = p*p*p + pi*pi*q*q;
345  if ( d >= 0.0 ) {
346  v = pow( sqrt( d ) + pi*q, 1.0/3.0 );
347  v -= pow( sqrt( d ) - pi*q, 1.0/3.0 );
348  } else {
349  v = 2.0*sqrt( -p )*cos( acos( pi*q/( p*sqrt( -p ) ) )/3.0 );
350  }
351  w = sqrt( e*e + v*pi );
352  g = 0.5*( e + w );
353  h = pi*( f*pi - v*g )/( g*g*w );
354 
355  /* Compute t, expanding the square root if necessary. */
356  if ( fabs( h ) < LAL_HSERIES )
357  t = g*( 0.5*h + 0.375*h*h + 0.3125*h*h*h );
358  else
359  t = g*( sqrt( 1.0 + h ) - 1.0 );
360 
361  /* Compute and sort the factors in the arctangent. */
362  {
363  double tanPFac[4]; /* factors of tanP */
364  tanPFac[0] = pi - t;
365  tanPFac[1] = pi + t;
366  tanPFac[2] = 1.0/pi;
367  tanPFac[3] = 1.0/t;
368  double length = 4;
369  HeapSort( tanPFac, length );
370  tanP = tanPFac[0]*tanPFac[3];
371  tanP *= tanPFac[1]*tanPFac[2];
372  tanP /= 2.0*f1;
373  }
374 
375  /* Compute latitude, longitude, and elevation. */
376  phi = atan( tanP );
377  latitude = phi;
378  if ( z < 0.0 ) latitude *= -1.0;
379  longitude = atan2( y, x );
380  elevation = ( pi - t/pi )*cos( phi );
381  elevation += ( fabs( z ) - f1 )*sin( phi );
382  elevation *= LAL_REARTH_SI;
383  }
384 }
385 
386 inline void
387 GetCartesianComponents( double u[3], double Alt, double Az, double Lat, double Lon) {
388 
389  double cosAlt=cos(Alt); double sinAlt=sin(Alt);
390  double cosAz=cos(Az); double sinAz=sin(Az);
391  double cosLat=cos(Lat); double sinLat=sin(Lat);
392  double cosLon=cos(Lon); double sinLon=sin(Lon);
393 
394  double uNorth = cosAlt * cosAz;
395  double uEast = cosAlt * sinAz;
396  /* uUp == sinAlt */
397  double uRho = - sinLat * uNorth + cosLat * sinAlt;
398  /* uLambda == uEast */
399 
400  u[0] = cosLon * uRho - sinLon * uEast;
401  u[1] = sinLon * uRho + cosLon * uEast;
402  u[2] = cosLat * uNorth + sinLat * sinAlt;
403 
404  return;
405 }
406 
407 // ************************************
408 // cWB Coordinate System
409 // theta=0 : North Pole
410 // phi=0 : Greenwich meridian
411 // ************************************
412 
413 inline void
414 CwbToGeographic(double ilongitude, double ilatitude, double& olongitude, double& olatitude)
415 {
416  olongitude = ilongitude>180 ? ilongitude-360 : ilongitude;
417  olatitude=-(ilatitude-90);
418 }
419 
420 inline void
421 GeographicToCwb(double ilongitude, double ilatitude, double& olongitude, double& olatitude)
422 {
423  olongitude = ilongitude<0 ? ilongitude+360 : ilongitude;
424  olatitude=-(ilatitude-90);
425 }
426 
427 inline void
428 CwbToCelestial(double ilongitude, double ilatitude, double& olongitude, double& olatitude, double gps=0)
429 {
430  skymap sm;
431  olongitude = gps>0 ? sm.phi2RA(ilongitude, gps) : ilongitude;
432  olongitude = 360-olongitude;
433  olatitude=-(ilatitude-90);
434 }
435 
436 inline void
437 CelestialToCwb(double ilongitude, double ilatitude, double& olongitude, double& olatitude, double gps=0)
438 {
439  skymap sm;
440  olongitude = gps>0 ? sm.RA2phi(ilongitude, gps) : ilongitude;
441  olongitude = 360-olongitude;
442  olatitude=-(ilatitude-90);
443 }
444 
445 #endif
wavearray< double > t(hp.size())
void EquatorialToEcliptic(double ilongitude, double ilatitude, double &olongitude, double &olatitude)
Definition: skycoord.hh:174
static double g(double e)
Definition: GNGen.cc:116
void CwbToCelestial(double ilongitude, double ilatitude, double &olongitude, double &olatitude, double gps=0)
Definition: skycoord.hh:428
wavearray< double > a(hp.size())
WSeries< float > v[nIFO]
Definition: cwb_net.C:80
int n
Definition: cwb_net.C:28
#define LAL_EARTHFLAT
Definition: skycoord.hh:36
wavearray< double > z
Definition: Test10.C:32
double olatitude
#define LAL_LGAL
Definition: skycoord.hh:32
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<< "\ layers : "<< nLAYERS<< "\ dF(hz) : "<< dF<< "\ 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
#define LAL_IEARTH
Definition: skycoord.hh:33
double ilatitude
STL namespace.
double olongitude
int j
Definition: cwb_net.C:28
i drho i
cout<< "SNR "<< xsnr<< endl;wavearray< double > f
Definition: ComputeSNR.C:75
double pi
Definition: TestChirp.C:18
wavearray< double > w
Definition: Test1.C:27
#define LAL_HSERIES
Definition: skycoord.hh:37
float phi
wavearray< double > h
Definition: Regression_H1.C:25
double deg2rad
TF1 * f2
void GeocentricToGeodetic(double X, double Y, double Z, double &latitude, double &longitude, double &elevation)
Definition: skycoord.hh:307
double Pi
int l
int k
double RA2phi(double ph, double gps)
Definition: skymap.hh:213
void EclipticToEquatorial(double ilongitude, double ilatitude, double &olongitude, double &olatitude)
Definition: skycoord.hh:133
Definition: skymap.hh:63
double e
void EquatorialToGalactic(double ilongitude, double ilatitude, double &olongitude, double &olatitude)
Definition: skycoord.hh:90
double phi2RA(double ph, double gps)
Definition: skymap.hh:212
regression r
Definition: Regression_H1.C:44
s s
Definition: cwb_net.C:155
#define LAL_REARTH_SI
Definition: skycoord.hh:35
double rad2deg
void CelestialToCwb(double ilongitude, double ilatitude, double &olongitude, double &olatitude, double gps=0)
Definition: skycoord.hh:437
double gps
double ilongitude
void GeodeticToGeocentric(double latitude, double longitude, double elevation, double &X, double &Y, double &Z)
Definition: skycoord.hh:215
double fabs(const Complex &x)
Definition: numpy.cc:55
#define LAL_ALPHAGAL
Definition: skycoord.hh:31
void CwbToGeographic(double ilongitude, double ilatitude, double &olongitude, double &olatitude)
Definition: skycoord.hh:414
void HeapSort(double *data, double length)
Definition: skycoord.hh:258
void GalacticToEquatorial(double ilongitude, double ilatitude, double &olongitude, double &olatitude)
Definition: skycoord.hh:47
void GeographicToCwb(double ilongitude, double ilatitude, double &olongitude, double &olatitude)
Definition: skycoord.hh:421
void GetCartesianComponents(double u[3], double Alt, double Az, double Lat, double Lon)
Definition: skycoord.hh:387
double length
Definition: TestBandPass.C:18
wavearray< double > y
Definition: Test10.C:31
#define LAL_DELTAGAL
Definition: skycoord.hh:30