UNPKG

astronomy-bundle

Version:

Bundle for astronomical calculations such as position of moon, sun and planets, sunrise, sunset or solar eclipses. Most of the calculations are based on Jean Meeus 'Astronomical Algorithms' book and the VSOP87 theory.

325 lines (324 loc) 11.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.shortYear2longYear = exports.getDeltaT = exports.getLocalHourAngle = exports.getLocalApparentSiderealTime = exports.getLocalMeanSiderealTime = exports.getGreenwichApparentSiderealTime = exports.getGreenwichMeanSiderealTime = exports.isLeapYear = exports.getDayOfWeek = exports.getDayOfYear = exports.getDecimalYear = exports.dayOfYear2time = exports.getEpochIntervalToJ2000 = exports.getEpochInterval = exports.julianMillenniaJ20002julianDay = exports.julianDay2julianMillenniaJ2000 = exports.julianCenturiesJ20002julianDay = exports.julianDay2julianCenturiesJ2000 = exports.julianDay2ModifiedJulianDay = exports.julianDay2julianDay0 = exports.julianDay2time = exports.time2julianDay = exports.sec2string = void 0; const epoch_1 = require("../../constants/epoch"); const calculations_1 = require("../../earth/calculations"); const angleCalc_1 = require("../../utils/angleCalc"); const math_1 = require("../../utils/math"); function sec2string(sec, short = false) { const sign = sec < 0 ? '-' : ''; sec = Math.abs(sec); const hour = Math.floor(sec / 3660); const min = Math.floor((sec - hour * 3600) / 60); const secPart = (0, math_1.round)(sec - hour * 3600 - min * 60, 2); if (short && hour === 0.0 && min === 0.0) { return sign + secPart + 's'; } if (short && hour === 0.0) { return sign + min + 'm ' + secPart + 's'; } return sign + hour + 'h ' + min + 'm ' + secPart + 's'; } exports.sec2string = sec2string; function time2julianDay(time) { const tmpYear = parseFloat(time.year + '.' + getDayOfYear(time)); let Y; let M; if (time.month > 2) { Y = time.year; M = time.month; } else { Y = time.year - 1; M = time.month + 12; } const D = time.day; const H = time.hour / 24 + time.min / 1440 + time.sec / 86400; let A; let B; if (tmpYear >= 1582.288) { A = Math.floor(Y / 100); B = 2 - A + Math.floor(A / 4); } else if (tmpYear <= 1582.277) { B = 0; } else { throw new Error('Date between 1582-10-04 and 1582-10-15 is not defined.'); } return Math.floor(365.25 * (Y + 4716)) + Math.floor(30.6001 * (M + 1)) + D + H + B - 1524.5; } exports.time2julianDay = time2julianDay; function julianDay2time(jd) { jd = jd + 0.5; const Z = Math.floor(jd); const F = jd - Z; let A = Z; if (Z >= 2299161) { const a = Math.floor((Z - 1867216.25) / 36524.25); A = Z + 1 + a - Math.floor(a / 4); } const B = A + 1524; const C = Math.floor((B - 122.1) / 365.25); const D = Math.floor(365.25 * C); const E = Math.floor((B - D) / 30.6001); const dayOnMonth = B - D - Math.floor(30.6001 * E) + F; const month = E < 14 ? E - 1 : E - 13; const year = month > 2 ? C - 4716 : C - 4715; const hour = (dayOnMonth - Math.floor(dayOnMonth)) * 24; const min = (hour - Math.floor(hour)) * 60; const sec = (min - Math.floor(min)) * 60; return { year: Math.floor(year), month: Math.floor(month), day: Math.floor(dayOnMonth), hour: Math.floor(hour), min: Math.floor(min), sec: Math.floor(sec), }; } exports.julianDay2time = julianDay2time; function julianDay2julianDay0(jd) { return Math.floor(jd + 0.5) - 0.5; } exports.julianDay2julianDay0 = julianDay2julianDay0; function julianDay2ModifiedJulianDay(jd) { return jd - 2400000.5; } exports.julianDay2ModifiedJulianDay = julianDay2ModifiedJulianDay; function julianDay2julianCenturiesJ2000(jd) { return (jd - 2451545.0) / 36525.0; } exports.julianDay2julianCenturiesJ2000 = julianDay2julianCenturiesJ2000; function julianCenturiesJ20002julianDay(T) { return T * 36525.0 + 2451545.0; } exports.julianCenturiesJ20002julianDay = julianCenturiesJ20002julianDay; function julianDay2julianMillenniaJ2000(jd) { const T = julianDay2julianCenturiesJ2000(jd); return T / 10; } exports.julianDay2julianMillenniaJ2000 = julianDay2julianMillenniaJ2000; function julianMillenniaJ20002julianDay(t) { const T = t * 10; return julianCenturiesJ20002julianDay(T); } exports.julianMillenniaJ20002julianDay = julianMillenniaJ20002julianDay; function getEpochInterval(jd, startingEpoch) { return (jd - startingEpoch) / 36525; } exports.getEpochInterval = getEpochInterval; function getEpochIntervalToJ2000(startingEpoch) { return getEpochInterval(startingEpoch, epoch_1.EPOCH_J2000); } exports.getEpochIntervalToJ2000 = getEpochIntervalToJ2000; function dayOfYear2time(year, dayOfYear) { const K = isLeapYear(year) ? 1 : 2; const month = dayOfYear < 32 ? 1 : Math.floor(9 * (K + dayOfYear) / 275 + 0.98); const day = Math.floor(dayOfYear - Math.floor(275 * month / 9) + K * Math.floor((month + 9) / 12) + 30); const hourFloat = 24 * (dayOfYear - Math.floor(dayOfYear)); const hour = Math.floor(hourFloat); const minFloat = 60 * (hourFloat - hour); const min = Math.floor(minFloat); const sec = (0, math_1.round)(60 * (minFloat - min)); return { year, month, day, hour, min, sec }; } exports.dayOfYear2time = dayOfYear2time; function getDecimalYear(time) { const daysInYear = isLeapYear(time.year) ? 366 : 365; const dayOfYear = getDayOfYear(time) - 1 + time.hour / 24 + time.min / 1440 + time.sec / 86400; return time.year + dayOfYear / daysInYear; } exports.getDecimalYear = getDecimalYear; function getDayOfYear(time) { const K = isLeapYear(time.year) ? 1 : 2; const M = time.month; const D = time.day; return Math.floor(275 * M / 9) - K * Math.floor((M + 9) / 12) + D - 30; } exports.getDayOfYear = getDayOfYear; function getDayOfWeek(time) { const jd = time2julianDay(time); return Math.floor((jd + 1.5) % 7); } exports.getDayOfWeek = getDayOfWeek; function isLeapYear(year) { if (year / 4 !== Math.floor(year / 4)) { return false; } else if (year / 100 !== Math.floor(year / 100)) { return true; } else if (year / 400 !== Math.floor(year / 400)) { return false; } else { return true; } } exports.isLeapYear = isLeapYear; function getGreenwichMeanSiderealTime(T) { const jd = julianCenturiesJ20002julianDay(T); const GMST = 280.46061837 + 360.98564736629 * (jd - 2451545) + 0.000387933 * Math.pow(T, 2) + Math.pow(T, 3) / 38710000; return (0, angleCalc_1.normalizeAngle)(GMST); } exports.getGreenwichMeanSiderealTime = getGreenwichMeanSiderealTime; function getGreenwichApparentSiderealTime(T) { const GMST = getGreenwichMeanSiderealTime(T); const p = calculations_1.earthCalc.getNutationInLongitude(T); const e = calculations_1.earthCalc.getTrueObliquityOfEcliptic(T); const eRad = (0, angleCalc_1.deg2rad)(e); return GMST + p * Math.cos(eRad); } exports.getGreenwichApparentSiderealTime = getGreenwichApparentSiderealTime; function getLocalMeanSiderealTime(T, lon) { const GMST = getGreenwichMeanSiderealTime(T); const LMST = GMST + lon; return (0, angleCalc_1.normalizeAngle)(LMST); } exports.getLocalMeanSiderealTime = getLocalMeanSiderealTime; function getLocalApparentSiderealTime(T, lon) { const GAST = getGreenwichApparentSiderealTime(T); return GAST + lon; } exports.getLocalApparentSiderealTime = getLocalApparentSiderealTime; function getLocalHourAngle(T, lon, rightAscension) { const LAST = getLocalApparentSiderealTime(T, lon); return (0, angleCalc_1.normalizeAngle)(LAST - rightAscension); } exports.getLocalHourAngle = getLocalHourAngle; function getDeltaT(year, month = 0) { const y = year + (month - 0.5) / 12; let t; let deltaT = 0; if (year < -500) { t = (y - 1820) / 100; deltaT = -20 + 32 * Math.pow(t, 2); } if (year >= -500 && year < 500) { t = y / 100; deltaT = 10583.6 - 1014.41 * t + 33.78311 * Math.pow(t, 2) - 5.952053 * Math.pow(t, 3) - 0.1798452 * Math.pow(t, 4) + 0.022174192 * Math.pow(t, 5) + 0.0090316521 * Math.pow(t, 6); } if (year >= 500 && year < 1600) { t = (y - 1000) / 100; deltaT = 1574.2 - 556.01 * t + 71.23472 * Math.pow(t, 2) + 0.319781 * Math.pow(t, 3) - 0.8503463 * Math.pow(t, 4) - 0.005050998 * Math.pow(t, 5) + 0.0083572073 * Math.pow(t, 6); } if (year >= 1600 && year < 1700) { t = y - 1600; deltaT = 120 - 0.9808 * t - 0.01532 * Math.pow(t, 2) + Math.pow(t, 3) / 7129; } if (year >= 1700 && year < 1800) { t = y - 1700; deltaT = 8.83 + 0.1603 * t - 0.0059285 * Math.pow(t, 2) + 0.00013336 * Math.pow(t, 3) - Math.pow(t, 4) / 1174000; } if (year >= 1800 && year < 1860) { t = y - 1800; deltaT = 13.72 - 0.332447 * t + 0.0068612 * Math.pow(t, 2) + 0.0041116 * Math.pow(t, 3) - 0.00037436 * Math.pow(t, 4) + 0.0000121272 * Math.pow(t, 5) - 0.0000001699 * Math.pow(t, 6) + 0.000000000875 * Math.pow(t, 7); } if (year >= 1860 && year < 1900) { t = y - 1860; deltaT = 7.62 + 0.5737 * t - 0.251754 * Math.pow(t, 2) + 0.01680668 * Math.pow(t, 3) - 0.0004473624 * Math.pow(t, 4) + Math.pow(t, 5) / 233174; } if (year >= 1900 && year < 1920) { t = y - 1900; deltaT = -2.79 + 1.494119 * t - 0.0598939 * Math.pow(t, 2) + 0.0061966 * Math.pow(t, 3) - 0.000197 * Math.pow(t, 4); } if (year >= 1920 && year < 1941) { t = y - 1920; deltaT = 21.20 + 0.84493 * t - 0.076100 * Math.pow(t, 2) + 0.0020936 * Math.pow(t, 3); } if (year >= 1941 && year < 1961) { t = y - 1950; deltaT = 29.07 + 0.407 * t - Math.pow(t, 2) / 233 + Math.pow(t, 3) / 2547; } if (year >= 1961 && year < 1986) { t = y - 1975; deltaT = 45.45 + 1.067 * t - Math.pow(t, 2) / 260 - Math.pow(t, 3) / 718; } if (year >= 1986 && year < 2005) { t = y - 2000; deltaT = 63.86 + 0.3345 * t - 0.060374 * Math.pow(t, 2) + 0.0017275 * Math.pow(t, 3) + 0.000651814 * Math.pow(t, 4) + 0.00002373599 * Math.pow(t, 5); } if (year >= 2005 && year < 2050) { t = y - 2000; deltaT = 62.92 + 0.32217 * t + 0.005589 * Math.pow(t, 2); } if (year >= 2050) { t = (y - 1820) / 100; deltaT = -20 + 32 * Math.pow(t, 2); } return deltaT; } exports.getDeltaT = getDeltaT; function shortYear2longYear(shortYearString) { const currentDate = new Date(Date.now()); const currentYear = currentDate.getFullYear(); const currentYearStr = currentYear.toString(); const currentYearFirstDigitsStr = currentYearStr.substr(0, currentYearStr.length - 2); const currentYearFirstDigits = parseInt(currentYearFirstDigitsStr); const year1Str = currentYearFirstDigits + shortYearString; const year1 = parseInt(year1Str); const year2Str = (currentYearFirstDigits - 1).toString() + shortYearString; const year2 = parseInt(year2Str); return year1 <= currentYear ? year1 : year2; } exports.shortYear2longYear = shortYear2longYear;