0
$\begingroup$

Wikipedia cites this formula for calculating solar azimuth:

equation for 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));
};
$\endgroup$
3
  • $\begingroup$ Are you using degrees or hours for the arguments of the sines and cosines? They'll only work with radians ... $\endgroup$
    – Glorfindel
    Commented Feb 27, 2019 at 20:22
  • $\begingroup$ I'm using radians for everything here. $\endgroup$
    – nnyby
    Commented Feb 27, 2019 at 20:24
  • $\begingroup$ That formula should be derived straight from spherical trigonometry formulae, have you tried googling that and attempting to derive the equation yourself? $\endgroup$
    – Tosic
    Commented Feb 27, 2019 at 21:38

1 Answer 1

1
$\begingroup$

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;
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .