UNPKG

@sffjunkie/astral

Version:

calculations for the position of the sun and the moon

67 lines (66 loc) 2.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var index_1 = require("./index"); var sun_1 = require("./sun"); function proper_angle(value) { if (value > 0.0) { value /= 360.0; return (value - Math.floor(value)) * 360.0; } else { var tmp = Math.ceil(Math.abs(value / 360.0)); return value + tmp * 360.0; } } function _phase_asfloat(date) { var jd = sun_1.julianDayNumber(date); var DT = Math.pow(jd - 2382148, 2) / (41048480 * 86400); var T = (jd + DT - 2451545.0) / 36525; var T2 = Math.pow(T, 2); var T3 = Math.pow(T, 3); var D = 297.85 + 445267.1115 * T - 0.00163 * T2 + T3 / 545868; D = _toRadians(proper_angle(D)); var M = 357.53 + 35999.0503 * T; M = _toRadians(proper_angle(M)); var M1 = 134.96 + 477198.8676 * T + 0.008997 * T2 + T3 / 69699; M1 = _toRadians(proper_angle(M1)); var elong = _toDegrees(D) + 6.29 * Math.sin(M1); elong -= 2.1 * Math.sin(M); elong += 1.27 * Math.sin(2 * D - M1); elong += 0.66 * Math.sin(2 * D); elong = proper_angle(elong); elong = Math.round(elong); var moon = ((elong + 6.43) / 360) * 28; return moon; } /** * Calculates the phase of the moon on the specified date. * * ============ ============== * 0 .. 6.99 New moon * 7 .. 13.99 First quarter * 14 .. 20.99 Full moon * 21 .. 27.99 Last quarter * ============ ============== * * @param date - The date to calculate the phase for. Dates are always in the UTC timezone. * If not specified then today's date is used. * @returns A number designating the phase. */ function phase(date) { if (!date) { date = index_1.today(); } var moon = _phase_asfloat(date); if (moon >= 28.0) { moon -= 28.0; } return moon; } exports.phase = phase; function _toRadians(degrees) { return degrees * (Math.PI / 180.0); } function _toDegrees(radians) { return radians * (180.0 / Math.PI); }