geoana.spatial.spherical_to_cartesian#
- geoana.spatial.spherical_to_cartesian(grid, vec=None)#
Transform gridded locations of a set of vectors from spherical coordinates \((r, \phi, \theta)\) to Cartesian coordinates \((x, y, z)\). \(\phi\) and \(\theta\) are the azimuthal and polar angles, respectively. \(\phi\) and \(\theta\) are given in radians.
- Parameters:
- grid(…, 3) array_like
Gridded locations defined in spherical coordinates \((r, \phi, \theta)\).
- vec(…, 3) array_like, optional
Vectors defined in spherical coordinates \((v_r, v_\phi, v_\theta)\) 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 spherical coordinates to Cartesian coordinates.
>>> from geoana.spatial import spherical_to_cartesian >>> import numpy as np
Construct original set of vectors in spherical coordinates
>>> r = np.ones(9) >>> phi = np.linspace(0, 2*np.pi, 9) >>> theta = np.linspace(0, np.pi, 9) >>> u = np.c_[r, phi, theta] >>> u array([[1. , 0. , 0. ], [1. , 0.78539816, 0.39269908], [1. , 1.57079633, 0.78539816], [1. , 2.35619449, 1.17809725], [1. , 3.14159265, 1.57079633], [1. , 3.92699082, 1.96349541], [1. , 4.71238898, 2.35619449], [1. , 5.49778714, 2.74889357], [1. , 6.28318531, 3.14159265]])
Create equivalent set of vectors in Cartesian coordinates
>>> v = spherical_to_cartesian(u) >>> v 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]])