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