<p>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.</p>

<p>I did need to adjust the azimuth's angle offset based on the hour angle:</p>

<pre><code>     // 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 &lt; 0 || hourAngle &gt; Math.PI) {
         return az;
     }

     return (Math.PI * 2) - az;
</code></pre>

CC BY-SA 4.0
2019-02-27T22:02:11.227Z
2019-02-27T22:02:11.227Z
nnyby
<p>Wikipedia cites this formula for calculating solar azimuth:</p>

<p><a href="https://i.sstatic.net/JN6jQ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/JN6jQ.png" alt="equation for solar azimuth"></a></p>

<p>Where:</p>

<ul>
<li><span class="math-container">$\phi_s$</span> is the solar azimuth angle</li>
<li><span class="math-container">$\theta_s$</span> is the solar zenith angle</li>
<li><span class="math-container">$h$</span> is the hour angle, in the local solar time</li>
<li><span class="math-container">$\delta$</span> is the current sun declination</li>
<li><span class="math-container">$\Phi$</span> is the local latitude</li>
</ul>

<p>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?</p>

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

<pre><code>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));
};
</code></pre>

CC BY-SA 4.0
2019-02-27T20:22:32.283Z
Are you using degrees or hours for the arguments of the sines and cosines? They'll only work with radians ...
Glorfindel
CC BY-SA 4.0
2019-02-27T20:24:05.563Z
I'm using radians for everything here.
nnyby
CC BY-SA 4.0
2019-02-27T21:38:12.420Z
That formula should be derived straight from spherical trigonometry formulae, have you tried googling that and attempting to derive the equation yourself?
Tosic
<azimuth>
How do I calculate the sun's azimuth based on zenith, hour angle, declination, and latitude?
nnyby
# How do I calculate the sun's azimuth based on zenith, hour angle, declination, and latitude?

(Asked by: username_0 on 2019-02-27)

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));
};

**Tags:** azimuth

> **Comments:**
> **username_1**: Are you using degrees or hours for the arguments of the sines and cosines? They'll only work with radians ...  
> **username_0**: I'm using radians for everything here.  
> **username_2**: That formula should be derived straight from spherical trigonometry formulae, have you tried googling that and attempting to derive the equation yourself?  

---

## 1 answers

### Answer by username_0 on 2019-02-27. Score: 1

I've confirmed that this formula is in fact correct. I did have the wrong units in the code, which @username_1 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;


---

