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?
$$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
atan2function 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
atan2returns angles in $[-\pi, \pi)$,