geoana.spatial.cartesian_to_spherical#
- geoana.spatial.cartesian_to_spherical(grid, vec=None)#
Transform gridded locations or a set of vectors from Cartesian coordinates \((x, y, z)\) to spherical coordinates \((r, \phi, \theta)\). \(\phi\) and \(\theta\) are the azimuthal and polar angle, respectively. \(\phi\) and \(\theta\) are given in radians.
- Parameters:
- grid(…, 3) array_like
Gridded locations defined in Cartesian coordinates \((x, y, z)\).
- vec(…, 3) array_like, optional
Vectors defined in Cartesian coordinates \((v_x, v_y, v_z)\) at the gridded locations. Will also except a flattend array in column major order with the same number of elements.
- Returns:
- (…, 3) numpy.ndarray
If vec is
None
, this returns the transformed grid array, otherwise this is the transformed vec array.
Examples
Here, we convert a series of vectors in 3D space from Cartesian coordinates to spherical coordinates.
>>> from geoana.spatial import cartesian_to_spherical >>> import numpy as np
Construct original set of vectors in cartesian coordinates
>>> r = np.ones(9) >>> phi = np.linspace(0, 2*np.pi, 9) >>> theta = np.linspace(0., np.pi, 9) >>> x = r*np.sin(theta)*np.cos(phi) >>> y = r*np.sin(theta)*np.sin(phi) >>> z = r*np.cos(theta) >>> u = np.c_[x, y, z] >>> u array([[ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00], [ 2.70598050e-01, 2.70598050e-01, 9.23879533e-01], [ 4.32978028e-17, 7.07106781e-01, 7.07106781e-01], [-6.53281482e-01, 6.53281482e-01, 3.82683432e-01], [-1.00000000e+00, 1.22464680e-16, 6.12323400e-17], [-6.53281482e-01, -6.53281482e-01, -3.82683432e-01], [-1.29893408e-16, -7.07106781e-01, -7.07106781e-01], [ 2.70598050e-01, -2.70598050e-01, -9.23879533e-01], [ 1.22464680e-16, -2.99951957e-32, -1.00000000e+00]])
Compute equivalent set of vectors in spherical coordinates
>>> v = cartesian_to_spherical(u) >>> v array([[ 1.00000000e+00, 0.00000000e+00, 0.00000000e+00], [ 1.00000000e+00, 7.85398163e-01, 3.92699082e-01], [ 1.00000000e+00, 1.57079633e+00, 7.85398163e-01], [ 1.00000000e+00, 2.35619449e+00, 1.17809725e+00], [ 1.00000000e+00, 3.14159265e+00, 1.57079633e+00], [ 1.00000000e+00, -2.35619449e+00, 1.96349541e+00], [ 1.00000000e+00, -1.57079633e+00, 2.35619449e+00], [ 1.00000000e+00, -7.85398163e-01, 2.74889357e+00], [ 1.00000000e+00, -2.44929360e-16, 3.14159265e+00]])