1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Module for transforming between different coordinate systems."""
24
25
26 from math import acos, atan2, cos, sin
27 from numpy import array, float64
28 from numpy.linalg import norm
29
30
32 """Convert the Cartesian vector [x, y, z] to spherical coordinates [r, theta, phi].
33
34 The parameter r is the radial distance, theta is the polar angle, and phi is the azimuth.
35
36
37 @param vector: The Cartesian vector [x, y, z].
38 @type vector: numpy rank-1, 3D array
39 @return: The spherical coordinate vector [r, theta, phi].
40 @rtype: numpy rank-1, 3D array
41 """
42
43
44 r = norm(vector)
45
46
47 unit = vector / r
48
49
50 theta = acos(unit[2])
51
52
53 phi = atan2(unit[1], unit[0])
54
55
56 return array([r, theta, phi], float64)
57
58
60 """Convert the spherical coordinate vector [r, theta, phi] to the Cartesian vector [x, y, z].
61
62 The parameter r is the radial distance, theta is the polar angle, and phi is the azimuth.
63
64
65 @param spherical_vect: The spherical coordinate vector [r, theta, phi].
66 @type spherical_vect: 3D array or list
67 @param cart_vect: The Cartesian vector [x, y, z].
68 @type cart_vect: 3D array or list
69 """
70
71
72 sin_theta = sin(spherical_vect[1])
73
74
75 cart_vect[0] = spherical_vect[0] * cos(spherical_vect[2]) * sin_theta
76 cart_vect[1] = spherical_vect[0] * sin(spherical_vect[2]) * sin_theta
77 cart_vect[2] = spherical_vect[0] * cos(spherical_vect[1])
78