-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoordinateTransformations.py
253 lines (206 loc) · 7.97 KB
/
CoordinateTransformations.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import numpy as np
from scipy.optimize import root
def point_transform(
input_points: np.ndarray,
input_coordinates: str,
output_coordinates: str
) -> np.ndarray:
if input_coordinates == "geographic":
if output_coordinates == "Cartesian":
output_points = geographic_to_Cartesian_point(input_points)
elif output_coordinates == "Mollweide":
output_points = geographic_to_Mollweide_point(input_points)
else:
output_points = input_points.copy()
elif input_coordinates == "Cartesian":
if output_coordinates == "geographic":
output_points = Cartesian_to_geographic_point(input_points)
else:
output_points = input_points.copy()
elif input_coordinates == "Mollweide":
if output_coordinates == "geographic":
output_points = Mollweide_to_geographic_point(input_points)
else:
output_points = input_points.copy()
else:
output_points = input_points.copy()
return output_points
def geographic_to_Cartesian_point(points):
"""
Transform points on the unit sphere from geographic
coords (ra,dec) to Cartesian coords (x,y,z)
INPUTS
------
points: numpy array
The coords ra and dec in radians.
Either a single point [shape=(2,)], or
a list of points [shape=(Npoints,2)].
RETURNS
-------
new_points: numpy array
The coords (x,y,z) with x^2+y^2+z^=1.
Either a single point [shape=(3,)], or
a list of points [shape=(Npoints,3)].
"""
new_points = np.zeros((len(points), 3))
theta = np.pi/2 - points[... , 1]
phi = points[... , 0]
new_points[...,0] = np.sin(theta) * np.cos(phi)
new_points[...,1] = np.sin(theta) * np.sin(phi)
new_points[...,2] = np.cos(theta)
if len(points.shape) == 1:
return new_points[0]
else:
return new_points
def Cartesian_to_geographic_point(points_Cartesian):
"""
Transform points on the unit sphere from Cartesian
coords (x,y,z) to geographic coords (ra,dec)
INPUTS
------
points: numpy array
The Cartesian coords (x,y,z).
Either a single point [shape=(3,)], or
a list of points [shape=(Npoints,3)].
RETURNS
-------
new_points: numpy array
The coords (ra,dec) in radians.
Either a single point [shape=(2,)], or
a list of points [shape=(Npoints,3)].
"""
shape_geographic = list(points_Cartesian.shape)
shape_geographic[-1] = 2
points_geographic = np.zeros(shape=shape_geographic)
theta = np.arccos(points_Cartesian[..., 2] / np.linalg.norm(points_Cartesian, axis=-1))
phi = np.arctan2(points_Cartesian[..., 1], points_Cartesian[..., 0])
phi[phi<0] += 2*np.pi
points_geographic[...,0] = phi
points_geographic[...,1] = np.pi/2 - theta
return points_geographic
def geographic_to_Mollweide_point(
points_geographic: np.ndarray
) -> np.ndarray:
"""
Transform points on the unit sphere from geographic coordinates (ra,dec)
to Mollweide projection coordiantes (x,y).
INPUTS
------
points_geographic: numpy array
The geographic coords (ra,dec).
Either a single point [shape=(2,)], or
a list of points [shape=(Npoints,2)].
RETURNS
-------
points_Mollweide: numpy array
The Mollweide projection coords (x,y).
Either a single point [shape=(2,)], or
a list of points [shape=(Npoints,2)].
"""
final_shape_Mollweide = list(points_geographic.shape)
points_geographic = points_geographic.reshape(-1, points_geographic.shape[-1])
points_Mollweide = np.zeros(shape=points_geographic.shape,
dtype=points_geographic.dtype)
alpha_tol = 1.e-6
def alpha_eq(x):
return np.where(np.pi/2 - np.abs(points_geographic[...,1]) < alpha_tol, points_geographic[...,1], 2 * x + np.sin(2 * x) - np.pi * np.sin(points_geographic[...,1]))
alpha = root(fun=alpha_eq, x0=points_geographic[...,1], method='krylov', tol=1.e-10)
points_Mollweide[...,0] = 2 * np.sqrt(2) * (points_geographic[...,0] - np.pi) * np.cos(alpha.x) / np.pi
points_Mollweide[...,1] = np.sqrt(2) * np.sin(alpha.x)
points_Mollweide = points_Mollweide.reshape(final_shape_Mollweide)
return points_Mollweide
def Mollweide_to_geographic_point(
points_Mollweide: np.ndarray
) -> np.ndarray:
"""
Transform points on the unit sphere from Mollweide projection coordiantes (x,y)
to geographic coordinates (ra,dec).
INPUTS
------
points_Mollweide: numpy array
The Mollweide projection coords (x,y).
Either a single point [shape=(2,)], or
a list of points [shape=(Npoints,2)].
RETURNS
-------
points_geographic: numpy array
The geographic coords (ra,dec).
Either a single point [shape=(2,)], or
a list of points [shape=(Npoints,2)].
"""
if points_Mollweide.ndim == 1:
points_geographic = np.zeros((2), dtype=points_Mollweide.dtype)
else:
points_geographic = np.zeros((points_Mollweide.shape[0], 2), dtype=points_Mollweide.dtype)
alpha = np.arcsin(points_Mollweide[...,1]/np.sqrt(2))
points_geographic[...,0] = np.pi + (np.pi * points_Mollweide[...,0]) / (2*np.sqrt(2)*np.cos(alpha))
points_geographic[...,1] = np.arcsin((2*alpha + np.sin(2*alpha))/np.pi)
return points_geographic
def Cartesian_to_geographic_vector(points, dpoints):
"""
Transform vectors in the tangent plane of the unit sphere from
Cartesian coords (d_x,d_y,d_z) to geographic coords (d_ra,d_dec).
INPUTS
------
points: numpy array
The Cartesian coords (x,y,z).
Either a single point [shape=(3,)], or
a list of points [shape=(Npoints,3)].
dpoints: numpy array
The Cartesian coords (d_x,d_y,d_z) which
satisfy x*d_x+y*d_y+z*d_z = 0.
Either a single point or many with shape
matching points.
RETURNS
-------
tangent_vector: numpy array
The coords (d_ra,d_dec) in radians.
Either a single point [shape=(2,)], or
a list of points [shape=(Npoints,2)].
"""
if points.ndim == 1:
tangent_vector = np.zeros((2), dtype=dpoints.dtype)
else:
tangent_vector = np.zeros((len(points), 2), dtype=dpoints.dtype)
x = points[... , 0]
y = points[... , 1]
z = points[... , 2]
dx = dpoints[... , 0]
dy = dpoints[... , 1]
dz = dpoints[... , 2]
tangent_vector[... , 0] = ( x*dy-y*dx ) / ( x**2+y**2 )
tangent_vector[... , 1] = dz / ( np.sqrt( 1-z**2 ) )
return tangent_vector
def geographic_to_Cartesian_vector(points, dpoints):
"""
Transform vectors in the tangent plane of the unit sphere from
geographic coords (d_ra,d_dec) to Cartesian coords (d_x,d_y,d_z).
INPUTS
------
points: numpy array
The geographic coords (ra,dec).
Either a single point [shape=(2,)], or
a list of points [shape=(Npoints,2)].
dpoints: numpy array
The geographic coords (d_ra,d_dec).
Either a single point or many with shape
matching points.
RETURNS
-------
tangent_vector: numpy array
The coords (d_x,d_y,d_z).
Either a single point [shape=(3,)], or
a list of points [shape=(Npoints,3)].
"""
if points.ndim == 1:
tangent_vector = np.zeros((3), dtype=dpoints.dtype)
else:
tangent_vector = np.zeros((len(points), 3), dtype=dpoints.dtype)
theta = np.pi / 2 - points[... , 1]
phi = points[... , 0]
dtheta = - dpoints[... , 1]
dphi = dpoints[... , 0]
tangent_vector[...,0] = np.cos(theta) * np.cos(phi) * dtheta - np.sin(theta) * np.sin(phi) * dphi
tangent_vector[...,1] = np.cos(theta) * np.sin(phi) * dtheta + np.sin(theta) * np.cos(phi) * dphi
tangent_vector[...,2] = - np.sin(theta) * dtheta
return tangent_vector