# How do I calculate the sun's azimuth based on zenith, hour angle, declination, and latitude?

Wikipedia cites this formula for calculating solar azimuth: Where:

• $$\phi_s$$ is the solar azimuth angle
• $$\theta_s$$ is the solar zenith angle
• $$h$$ is the hour angle, in the local solar time
• $$\delta$$ is the current sun declination
• $$\Phi$$ is the local latitude

I'm implementing this equation in JavaScript, but it doesn't seem correct. Can someone here help me confirm whether or not this equation is correct?

Here's my JS implementation. The only strange thing is at the end: limiting the acos domain to [-1, 1] to prevent NaN values.

const getSolarAzimuth = function(
zenithAngle, hourAngle, declination, latitude
) {
const cos = Math.cos;
const sin = Math.sin;

const cos_phi = (
sin(declination) * cos(latitude) - cos(hourAngle) * cos(declination) *
sin(latitude)) /
sin(zenithAngle);

return Math.acos(Math.min(Math.max(cos_phi, -1), 1));
};

• Are you using degrees or hours for the arguments of the sines and cosines? They'll only work with radians ... Feb 27 '19 at 20:22
• I'm using radians for everything here. Feb 27 '19 at 20:24
• That formula should be derived straight from spherical trigonometry formulae, have you tried googling that and attempting to derive the equation yourself? Feb 27 '19 at 21:38

I've confirmed that this formula is in fact correct. I did have the wrong units in the code, which @Glorfindel suggested: sun's hour angle was in hours instead of radians.

I did need to adjust the azimuth's angle offset based on the hour angle:

     // The angle offset needs to be adjusted based on whether the hour angle
// is in the morning or the evening.
//   https://en.wikipedia.org/wiki/Solar_azimuth_angle#Formulas
if (hourAngle < 0 || hourAngle > Math.PI) {
return az;
}

return (Math.PI * 2) - az;