MCPcopy
hub / github.com/mourner/suncalc / getMoonIllumination

Function getMoonIllumination

index.js:418–441  ·  view source on GitHub ↗
(date = new Date())

Source from the content-addressed store, hash-verified

416
417// moon illumination parameters, Meeus ch. 48 (and idlastro's mphase.pro)
418export function getMoonIllumination(date = new Date()) {
419 const d = toDaysTT(toDays(date));
420 const s = sunCoords(d);
421 const m = moonCoords(d);
422 const sdist = 149598000; // distance from Earth to Sun in km
423 const phi = acos(sin(s.dec) * sin(m.dec) + cos(s.dec) * cos(m.dec) * cos(s.ra - m.ra));
424 const inc = atan(sdist * sin(phi), m.dist - sdist * cos(phi));
425 const angle = atan(cos(s.dec) * sin(s.ra - m.ra), sin(s.dec) * cos(m.dec) -
426 cos(s.dec) * sin(m.dec) * cos(s.ra - m.ra));
427
428 const waxing = angle < 0; // bright limb leads → illuminated fraction is growing (new → full)
429
430 return {
431 // illuminated fraction, 0 (new) → 1 (full); reaches the exact extrema only at perfect
432 // syzygy (eclipses), so a "full" moon typically peaks a hair under 1 — this is correct
433 fraction: (1 + cos(inc)) / 2,
434 // phase, 0 → 1: 0 new, 0.25 first quarter, 0.5 full, 0.75 last quarter (waxing for phase < 0.5)
435 phase: 0.5 + 0.5 * inc * (waxing ? -1 : 1) / PI,
436 // position angle of the bright limb, degrees (v2: all angles emitted in degrees) — subtract
437 // getMoonPosition().parallacticAngle, also degrees, to get the zenith-relative tilt
438 angle: angle / rad,
439 waxing // true while the Moon is waxing (new → full), false while waning (full → new)
440 };
441}
442
443function hoursLater(date, h) {
444 return new Date(date.valueOf() + h * dayMs / 24);

Callers

nothing calls this directly

Calls 4

toDaysTTFunction · 0.85
toDaysFunction · 0.85
sunCoordsFunction · 0.85
moonCoordsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…