clouddrift.sphere.cartesian_to_spherical

clouddrift.sphere.cartesian_to_spherical#

clouddrift.sphere.cartesian_to_spherical(x: float | ndarray | DataArray, y: float | ndarray | DataArray, z: float | ndarray | DataArray) tuple[ndarray, ndarray][source]#

Converts Cartesian three-dimensional coordinates to latitude and longitude on a spherical body.

The Cartesian coordinate system is a right-handed system whose origin lies at the center of the sphere. It is oriented with the Z-axis passing through the poles and the X-axis passing through the point lon = 0, lat = 0. This function is inverted by spherical_to_cartesian.

Parameters#

xfloat or array-like

x-coordinates in 3D.

yfloat or array-like

y-coordinates in 3D.

zfloat or array-like

z-coordinates in 3D.

Returns#

lonfloat or array-like

An N-d array of longitudes in degrees in range [-180, 180].

latfloat or array-like

An N-d array of latitudes in degrees.

Examples#

>>> x = EARTH_RADIUS_METERS * np.cos(np.deg2rad(45))
>>> y = EARTH_RADIUS_METERS * np.cos(np.deg2rad(45))
>>> z = 0 * x
>>> cartesian_to_spherical(x, y, z)
(44.99999999999985, 0.0)

cartesian_to_spherical is inverted by spherical_to_cartesian:

>>> x, y, z = spherical_to_cartesian(np.array([45]),np.array(0))
>>> cartesian_to_spherical(x, y, z)
(array([45.]), array([0.]))

Raises#

AttributeError

If x, y, and z are not NumPy arrays.

See Also#

spherical_to_cartesian()