/
input_transf.py
73 lines (55 loc) · 1.95 KB
/
input_transf.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
'''
Converts cartesian co-ordinates to spherical co-ordinates and vice versa
'''
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
from util import read_data
from math import *
def cart_to_spher(data):
'''
Takes as an input a data set containing points in cartesian format (time, x, y, z) and returns the computed
spherical coordinates (time, azimuth, elevation, r)
Args:
data (numpy array): containing the cartesian coordinates in format of (time, x, y, z)
Returns:
numpy array: array of spherical coordinates in format of (time, azimuth, elevation, r)
'''
for i in range(0, len(data)):
x = data[i, 1]
y = data[i, 2]
z = data[i, 3]
r = sqrt(x**2 + y**2 + z**2)
elevation = atan2(z, sqrt(x**2 + y**2))
azimuth = atan2(y, x)
result = data
result[i, 1] = azimuth
result[i, 2] = elevation
result[i, 3] = r
return result
def spher_to_cart(data):
'''
Takes as an input a data set containing points in spherical format (time, azimuth, elevation, r) and
returns the computed cartesian coordinates (time, x, y, z).
Args:
data (numpy array): containing the spherical coordinates in format of (time, azimuth, elevation, r)
Returns:
numpy array: array of cartesian coordinates in format of (time, x, y, z)
'''
for i in range(0, len(data)):
elevation = data[i, 1]
azimuth = data[i, 2]
r = data[i, 3]
x = r * cos(elevation) * cos(azimuth)
y = r * cos(elevation) * sin(azimuth)
z = r * sin(elevation)
result = data
result[i, 1] = x
result[i, 2] = y
result[i, 3] = z
return result
if __name__ == "__main__":
data = read_data.load_data("../example_data/orbit.csv")
new_data = cart_to_spher(data)
same_data = spher_to_cart(new_data)
print(same_data == data)