-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpore_mollweide.py
177 lines (132 loc) · 6.17 KB
/
expore_mollweide.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import numpy as np
import healpy as hp
from astropy.coordinates import SkyCoord, Galactic
import astropy.units as u
import matplotlib.pyplot as plt
NSIDE = 64
def verify_mollweide_conversion():
test_coordinates = {'bulge': {'l': 2.216, 'b': -3.14, 'symbol': 'D'},
'smc': {'l': 302.8084, 'b': -44.3277, 'symbol': '+'},
'lmc': {'l': 280.4652, 'b': -32.888443, 'symbol': 'x'}}
# Forward conversion from Galactic coordinates to Mollweide cartesian
for name, coords in test_coordinates.items():
# First convert from galactic coordinates to RA, Dec J2000:
(coords['ra'],coords['dec']) = galacticToRADec(coords['l'],coords['b'])
# Now calculate radial coordinates phi and lambda:
(coords['colatitude'],coords['colongitude']) = icrsToHPRadialCoords(coords['ra'],coords['dec'])
# Convert Healpy radial coordinates to Mollweide radial coordinates:
(coords['latitude'],coords['longitude']) = HPRadialToMollweideRadialCoords(coords['colatitude'],coords['colongitude'])
# Convert to Mollweide cartesian coordinates, x,y:
(coords['x'],coords['y']) = radialToMollweideCoords(coords['latitude'],coords['longitude'])
# Scale Mollweide coordinates to match Healpy convention:
#(coords['xprime'],coords['yprime']) = scaleMollweideCoords(coords['x'],coords['y'])
# Reverse the transformation:
(coords['latitude2'],coords['longitude2']) = MollweideToRadialCoords(coords['x'],coords['y'])
(coords['colatitude2'],coords['colongitude2']) = MollweideRadialToHPRadialCooords(coords['latitude2'],coords['longitude2'])
(coords['ra2'],coords['dec2']) = HPRadialCoordsToICRS(coords['colatitude2'],coords['colongitude2'])
# Check we have reacquired the original coordinates:
np.testing.assert_almost_equal(coords['ra'],coords['ra2'],1e-4)
np.testing.assert_almost_equal(coords['dec'],coords['dec2'],1e-4)
print('\n')
# Plot Mollweide cartesian positions:
plot_true_moll = True
fig = plt.figure(1,(10,5))
for name, coords in test_coordinates.items():
if plot_true_moll:
plt.plot(coords['x'],coords['y'],'r', marker=coords['symbol'], markersize=10)
else:
plt.plot(coords['xprime'],coords['yprime'],'r', marker=coords['symbol'], markersize=10)
plt.xlabel('Mollweide x')
plt.ylabel('Mollweide y')
if plot_true_moll:
xmin = -2.0*np.sqrt(2.0)
xmax = 2.0*np.sqrt(2.0)
ymin = -np.sqrt(2.0)
ymax = np.sqrt(2.0)
else:
xmin = 0.0
xmax = 800.0
ymin = 0.0
ymax = 400.0
#plt.axis([xmin,xmax,ymin,ymax])
print('Mollweide plot ranges: ',xmin,xmax,ymin,ymax)
plt.savefig('mollweide_forward_conversion.png')
plt.close(1)
def galacticToRADec(l,b):
"""Convert from galactic coordinates to RA, Dec J2000"""
s = SkyCoord(l, b, unit=(u.deg,u.deg), frame=Galactic())
s = s.transform_to('icrs')
ra = s.ra.degree
dec = s.dec.degree
print('Galactic l,b: ',l,b,' -> RA, Dec: ',ra, dec)
return ra, dec
def icrsToHPRadialCoords(ra,dec,verbose=False):
"""Function assumes input coordinates in decimal degrees, output in radians"""
colongitude = np.deg2rad(ra)
colatitude = (np.pi/2.0) - np.deg2rad(dec)
if verbose:
print('RA,Dec: ',ra,dec,
' -> radial coords co-latitude, co-longitude: ',
colatitude,colongitude)
return colatitude, colongitude
def HPRadialCoordsToICRS(colatitude, colongitude):
"""Function input is in radians, output in decimal degrees"""
ra = np.rad2deg(colongitude)
dec = np.rad2deg( (np.pi/2.0) - colatitude )
print('HP Co-latitude, co-longitude: ',colatitude, colongitude,' -> RA, Dec: ',ra,dec)
return ra,dec
def HPRadialToMollweideRadialCoords(colatitude,colongitude,verbose=False):
latitude = (np.pi/2.0) - colatitude
longitude = colongitude
if verbose:
print('HP theta, phi: ',colatitude,colongitude,
' Mollweide phi,lambda: ',latitude, longitude)
return latitude, longitude
def MollweideRadialToHPRadialCooords(latitude, longitude, verbose=False):
colatitude = (np.pi/2.0) - latitude
colongitude = longitude
if verbose:
print('Mollweide phi,lambda: ',latitude, longitude,
' HP theta, phi: ',colatitude,colongitude)
return colatitude, colongitude
def calcTheta(phi):
"""Newton-Raphson approximation of intermediate angle theta"""
# Catch to avoid division by zero:
if abs(phi) == np.pi/2:
return phi
theta = phi
stop_criterion = 1e-6
delta = 1e6
while delta > stop_criterion:
newTheta = theta - ( ( (2.0*theta) + (np.sin(2.0*theta)) - (np.pi*np.sin(phi)) ) / (2.0 + 2.0*np.cos(2.0*theta)) )
delta = abs(theta-newTheta)
theta = newTheta
return theta
def radialToMollweideCoords(latitude,longitude,verbose=False):
theta = calcTheta(latitude)
if verbose: print('Theta: ',theta)
R = 1.0 # Radius of projected globe
longitude0 = 0.0 # Longitude of central meridian
x = (R * (2.0 * np.sqrt(2.0)) * (longitude-longitude0) * np.cos(theta))/np.pi
y = R * np.sqrt(2.0) * np.sin(theta)
if verbose:
print('Radial phi,lambda: ',latitude,longitude,' -> Mollweide x,y:',x,y)
return x, y
def scaleMollweideCoords(x,y):
"""Scale the Mollweide coordinates following the Healpy convention
of xmax=800"""
R = 1.0 # Radius of projected globe
xprime = (800.0/(4.0*R*np.sqrt(2.0)))*x + 400
yprime = (400.0/(2.0*R*np.sqrt(2.0)))*y + 200
print('Mollweide x,y: ',x,y,' -> Scaled Mollweide xprime,yprime:',xprime,yprime)
return xprime,yprime
def MollweideToRadialCoords(x,y):
R = 1.0 # Radius of projected globe
longitude0 = 0.0 # Longitude of central meridian
theta = np.arcsin( y/(R*np.sqrt(2.0)) )
latitude = np.arcsin( (2.0*theta + np.sin(2.0*theta)) / np.pi )
longitude = longitude0 + ( (np.pi*x) / (2.0*R*np.sqrt(2.0)*np.cos(theta)) )
print('Reverse Mollweide x,y: ',x,y,' -> radial latitude, longitude: ',latitude, longitude)
return latitude, longitude
if __name__ == '__main__':
verify_mollweide_conversion()