How to determine the angles of a particular point in a special spherical coordinate system? Consider a point $(x,y,z) \in \mathbb{R}^3$ whose coordinates are defined in the following way:
$x=R\cos\varphi\cos\vartheta, \\
y=R\sin\varphi, \\
z=-R\cos\varphi\sin\vartheta,$
where $R \ge 0$, $\vartheta \in [0, \pi)$, and $\varphi \in [0, 2\pi)$.
My goal is to express $(R,\vartheta,\varphi)$ from $(x,y,z)$. Could you please help me how to determine $\vartheta$ and $\varphi$ with their proper phase in $[0,\pi)$ and $[0,2\pi)$, respectively?
 A: $$R = \sqrt{x^2 + y^2 + z^2}$$
If $R = 0$ then set $\varphi = \vartheta = 0$.
Let $\vartheta' = \text{atan2}(x, -z)$, and let $\varphi' = \text{atan2}(\sqrt{x^2 + z^2}, y)$. I will assume your atan2 function returns angles in $[0,2\pi)$ and has the horizontal coordinate first, vertical coordinate second (check - there are different conventions). Because the first coordinate is $\ge 0$, either $\varphi' \in \left[0, \frac \pi 2\right]$ or $\varphi' \in \left[\frac {3\pi} 2, 2\pi\right)$.

*

*If $\vartheta' < \pi$, then set $\vartheta = \vartheta', \varphi = \varphi'$.

*If $\vartheta' \ge\pi$, then set $\vartheta = \vartheta' - \pi$.

*

*If $\varphi' \in \left[0, \frac \pi 2\right]$, set $\varphi = \pi - \varphi'$

*If $\varphi' \in \left[\frac {3\pi} 2, 2\pi\right)$, set $\varphi = 3\pi - \varphi'$
If atan2 returns angles in $[-\pi, \pi)$,

*

*If $\vartheta' \ge 0$, then set $\vartheta = \vartheta'$.

*

*If $\varphi' \ge 0$, set $\varphi = \varphi'$.

*If $\varphi' < 0$, set $\varphi = 2\pi + \varphi'$



*If $\vartheta' < 0$, then set $\vartheta = \vartheta' + \pi$ and $\varphi = \pi - \varphi'$.

