Celestial Programming : Latitude/Longitude to Geocentric XYZ

This takes geodedic coordinates (e.g. latitude and longitude from a GPS receiver) and converts it into rectangular coordinates (xyz). This is useful for computing topocentric coordinates. Before using it rotations need to be applied for Earth Rotation Angle (or GMST), and possibly precession, nutation, and polar motion depending on the accuracy required.

Explanatory Supplement to the Astronomical Almanac 7.131

$$ r= \begin{bmatrix} x\\ y\\ z \end{bmatrix} = \begin{bmatrix} \rho\cos\theta'\cos\lambda\\ \rho\cos\theta'\sin\lambda\\ \rho\sin\theta' \end{bmatrix} = \begin{bmatrix} (aC+h)\cos\theta\cos\lambda \\ (aC+h)\cos\theta\sin\lambda \\ (aS+h)\sin\theta \end{bmatrix} $$ $$ \begin{align*} C&=(\cos^2\theta + (1-f)^2\sin^2\theta)^{-\frac{1}{2}} \\ S&=(1-f)^2C \\ a&=6378136.6m\\ f&=1/298.25642\\ \end{align*} $$ \(\theta'\) Geocentric latitude
\(\theta\) Geodedic latitude
\(\lambda\) Longitude
\(\rho\) Geocentric distance
\(a\) Earth's equitorial radius
\(f\) Earth's flatening ratio
Components of \(r\) will be in the same units as \(a\), meters in this case.