Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toDays, moonCoords and sunCoords now public from SunCalc #30

Open
wants to merge 3 commits into
base: moontimes
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 76 additions & 14 deletions suncalc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ var dayMs = 1000 * 60 * 60 * 24,
J1970 = 2440588,
J2000 = 2451545;

var SunCalc = {};

function toJulian(date) { return date.valueOf() / dayMs - 0.5 + J1970; }
function fromJulian(j) { return new Date((j + 0.5 - J1970) * dayMs); }
function toDays(date) { return toJulian(date) - J2000; }
SunCalc.toDays = function (date) { return toJulian(date) - J2000; }


// general calculations for position
Expand Down Expand Up @@ -56,7 +58,7 @@ function eclipticLongitude(M) {
return M + C + P + PI;
}

function sunCoords(d) {
SunCalc.sunCoords = function (d) {

var M = solarMeanAnomaly(d),
L = eclipticLongitude(M);
Expand All @@ -68,18 +70,15 @@ function sunCoords(d) {
}


var SunCalc = {};


// calculates sun position for a given date and latitude/longitude

SunCalc.getPosition = function (date, lat, lng) {

var lw = rad * -lng,
phi = rad * lat,
d = toDays(date),
d = SunCalc.toDays(date),

c = sunCoords(d),
c = SunCalc.sunCoords(d),
H = siderealTime(d, lw) - c.ra;

return {
Expand Down Expand Up @@ -134,7 +133,7 @@ SunCalc.getTimes = function (date, lat, lng) {
var lw = rad * -lng,
phi = rad * lat,

d = toDays(date),
d = SunCalc.toDays(date),
n = julianCycle(d, lw),
ds = approxTransit(0, lw, n),

Expand Down Expand Up @@ -168,7 +167,7 @@ SunCalc.getTimes = function (date, lat, lng) {

// moon calculations, based on http://aa.quae.nl/en/reken/hemelpositie.html formulas

function moonCoords(d) { // geocentric ecliptic coordinates of the moon
SunCalc.moonCoords = function (d) { // geocentric ecliptic coordinates of the moon

var L = rad * (218.316 + 13.176396 * d), // ecliptic longitude
M = rad * (134.963 + 13.064993 * d), // mean anomaly
Expand All @@ -189,9 +188,9 @@ SunCalc.getMoonPosition = function (date, lat, lng) {

var lw = rad * -lng,
phi = rad * lat,
d = toDays(date),
d = SunCalc.toDays(date),

c = moonCoords(d),
c = SunCalc.moonCoords(d),
H = siderealTime(d, lw) - c.ra,
h = altitude(H, phi, c.dec);

Expand All @@ -212,9 +211,9 @@ SunCalc.getMoonPosition = function (date, lat, lng) {

SunCalc.getMoonIllumination = function (date) {

var d = toDays(date),
s = sunCoords(d),
m = moonCoords(d),
var d = SunCalc.toDays(date),
s = SunCalc.sunCoords(d),
m = SunCalc.moonCoords(d),

sdist = 149598000, // distance from Earth to Sun in km

Expand All @@ -231,6 +230,69 @@ SunCalc.getMoonIllumination = function (date) {
};


function hoursLater(date, h) {
return new Date(date.valueOf() + h * dayMs / 24);
}

// calculations for moon rise/set times are based on http://www.stargazing.net/kepler/moonrise.html article

SunCalc.getMoonTimes = function (date, lat, lng) {
var t = new Date(date);
t.setHours(0);
t.setMinutes(0);
t.setSeconds(0);
t.setMilliseconds(0);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t.setHours(0, 0, 0, 0) instead


var hc = 0.133 * rad,
h0 = SunCalc.getMoonPosition(t, lat, lng).altitude - hc,
h1, h2, rise, set, a, b, xe, ye, d, roots, x1, x2, dx;

// go in 2-hour chunks, each time seeing if a 3-point quadratic curve crosses zero (which means rise or set)
for (var i = 1; i <= 24; i += 2) {
h1 = SunCalc.getMoonPosition(hoursLater(t, i), lat, lng).altitude - hc;
h2 = SunCalc.getMoonPosition(hoursLater(t, i + 1), lat, lng).altitude - hc;

a = (h0 + h2) / 2 - h1;
b = (h2 - h0) / 2;
xe = -b / (2 * a);
ye = (a * xe + b) * xe + h1;
d = b * b - 4 * a * h1;
roots = 0;

if (d >= 0) {
dx = Math.sqrt(d) / (Math.abs(a) * 2);
x1 = xe - dx;
x2 = xe + dx;
if (Math.abs(x1) <= 1) roots++;
if (Math.abs(x2) <= 1) roots++;
if (x1 < -1) x1 = x2;
}

if (roots === 1) {
if (h0 < 0) rise = i + x1;
else set = i + x1;

} else if (roots === 2) {
rise = i + (ye < 0 ? x2 : x1);
set = i + (ye < 0 ? x1 : x2);
}

if (rise && set) break;

h0 = h2;
}

var result = {};

if (rise) result.rise = hoursLater(t, rise);
if (set) result.set = hoursLater(t, set);

if (!rise && !set) result[ye > 0 ? 'alwaysUp' : 'alwaysDown'] = true;

return result;
};


// export as AMD module / Node module / browser variable
if (typeof define === 'function' && define.amd) define(SunCalc);
else if (typeof module !== 'undefined') module.exports = SunCalc;
Expand Down