Skip to content

Commit

Permalink
fixed bug on calculation of astronomical refraction
Browse files Browse the repository at this point in the history
the formula 16.4 of "Astronomical Algorithms" 2nd edition is now
translated correctly to radians. additionally the calculation works
also for negative altitudes now.
  • Loading branch information
Fabiz committed Jan 8, 2016
1 parent 1d058d6 commit dc171ad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
11 changes: 9 additions & 2 deletions suncalc.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ function altitude(H, phi, dec) { return asin(sin(phi) * sin(dec) + cos(phi) * co

function siderealTime(d, lw) { return rad * (280.16 + 360.9856235 * d) - lw; }

function astroRefraction(h) {
if (h < 0) // the following formula works for positive altitudes only.
h = 0; // if h = -0.08901179 a div/0 would occur.

// formula 16.4 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
// 1.02 / tan(h + 10.26 / (h + 5.10)) h in degrees, result in arc minutes -> converted to rad:
return 0.0002967 / Math.tan(h + 0.00312536 / (h + 0.08901179));
}

// general sun calculations

Expand Down Expand Up @@ -197,8 +205,7 @@ SunCalc.getMoonPosition = function (date, lat, lng) {
// formula 14.1 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
pa = atan(sin(H), tan(phi) * cos(c.dec) - sin(c.dec) * cos(H));

// altitude correction for refraction
h = h + rad * 0.017 / tan(h + rad * 10.26 / (h + rad * 5.10));
h = h + astroRefraction(h); // altitude correction for refraction

return {
azimuth: azimuth(H, phi, c.dec),
Expand Down
6 changes: 3 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ t.test('getMoonPosition returns moon position data given time and location', fun
var moonPos = SunCalc.getMoonPosition(date, lat, lng);

t.ok(near(moonPos.azimuth, -0.9783999522438226), 'azimuth');
t.ok(near(moonPos.altitude, 0.006969727754891917), 'altitude');
t.ok(near(moonPos.altitude, 0.014551482243892251), 'altitude');
t.ok(near(moonPos.distance, 364121.37256256194), 'distance');
t.end();
});
Expand All @@ -65,8 +65,8 @@ t.test('getMoonIllumination returns fraction and angle of moon\'s illuminated li
t.test('getMoonTimes returns moon rise and set times', function (t) {
var moonTimes = SunCalc.getMoonTimes(new Date('2013-03-04UTC'), lat, lng, true);

t.equal(moonTimes.rise.toUTCString(), 'Mon, 04 Mar 2013 23:57:55 GMT');
t.equal(moonTimes.set.toUTCString(), 'Mon, 04 Mar 2013 07:28:41 GMT');
t.equal(moonTimes.rise.toUTCString(), 'Mon, 04 Mar 2013 23:54:29 GMT');
t.equal(moonTimes.set.toUTCString(), 'Mon, 04 Mar 2013 07:47:58 GMT');

t.end();
});

0 comments on commit dc171ad

Please sign in to comment.