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.
105 lines (104 loc) • 4.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sec2deg = exports.normalizeAngle = exports.time2deg = exports.deg2time = exports.angle2deg = exports.decimal2degreeMinutesSeconds = exports.decimal2degreeMinutes = exports.deg2angle = exports.rad2deg = exports.deg2rad = void 0;
const math_1 = require("./math");
function deg2rad(degrees) {
return degrees * (Math.PI / 180);
}
exports.deg2rad = deg2rad;
function rad2deg(radians) {
return radians * (180 / Math.PI);
}
exports.rad2deg = rad2deg;
function deg2angle(deg, short = false) {
return decimal2degreeMinutesSeconds(deg, short);
}
exports.deg2angle = deg2angle;
function decimal2degreeMinutes(decimal, short = false, prefixes) {
const sign = getSignPrefix(decimal, prefixes);
decimal = Math.abs(decimal);
const degPart = Math.floor(decimal);
const min = (0, math_1.round)((decimal - degPart) * 60, 5);
const degString = degPart + '° ';
const minString = (0, math_1.pad)(min, 2) + '\'';
if (short && degPart === 0.0) {
return sign + minString;
}
return sign + degString + minString;
}
exports.decimal2degreeMinutes = decimal2degreeMinutes;
function decimal2degreeMinutesSeconds(decimal, short = false, prefixes) {
const sign = getSignPrefix(decimal, prefixes);
decimal = Math.abs(decimal);
const degPart = Math.floor(decimal);
const min = Math.floor((decimal - degPart) * 60);
const sec = (0, math_1.round)((decimal - degPart - min / 60) * 3600, 3);
const secParts = sec.toString().split('.');
const degString = degPart + '° ';
const minString = (0, math_1.pad)(min, 2) + '\' ';
const secString = (secParts.length === 1 ? (0, math_1.pad)(sec, 2) : (0, math_1.pad)(secParts[0], 2) + '.' + secParts[1]) + '"';
if (short && degPart === 0.0 && min === 0.0) {
return sign + secString;
}
if (short && degPart === 0.0) {
return sign + minString + secString;
}
return sign + degString + minString + secString;
}
exports.decimal2degreeMinutesSeconds = decimal2degreeMinutesSeconds;
function angle2deg(angle) {
const matches = angle.match(/(-?)(\d+)°.*?(\d+)'.*?([\d.]+)"/);
if (!matches) {
throw new Error('false angle format');
}
const sign = matches[1].trim() === '-' ? -1 : 1;
const deg = parseInt(matches[2]);
const min = parseInt(matches[3]);
const sec = parseFloat(matches[4]);
return sign * (deg + min / 60 + sec / 3600);
}
exports.angle2deg = angle2deg;
function deg2time(angle) {
const sign = angle < 0 ? '-' : '';
const time = Math.abs(angle / 15);
const hour = Math.floor(time);
const min = Math.floor((time - hour) * 60);
const sec = (0, math_1.round)((time - hour - min / 60) * 3600, 3);
const secParts = sec.toString().split('.');
const hourString = sign + hour;
const minString = (0, math_1.pad)(min, 2);
const secString = secParts.length === 1 ? (0, math_1.pad)(sec, 2) : (0, math_1.pad)(secParts[0], 2) + '.' + secParts[1];
return `${hourString}h ${minString}m ${secString}s`;
}
exports.deg2time = deg2time;
function time2deg(timeAngle) {
const matches = timeAngle.match(/(-?)(\d+)h.*?(\d+)m.*?([\d.]+)s/);
if (!matches) {
throw new Error('false time angle format');
}
const sign = matches[1].trim() === '-' ? -1 : 1;
const deg = parseInt(matches[2]);
const min = parseInt(matches[3]);
const sec = parseFloat(matches[4]);
const angleDeg = sign * (deg + min / 60 + sec / 3600);
return angleDeg * 15;
}
exports.time2deg = time2deg;
function normalizeAngle(degrees, baseAngle = 360.0) {
let angle = degrees % baseAngle;
if (angle < 0) {
angle = angle + baseAngle;
}
return angle;
}
exports.normalizeAngle = normalizeAngle;
function sec2deg(seconds) {
return seconds / 3600;
}
exports.sec2deg = sec2deg;
function getSignPrefix(decimal, prefixes) {
if (prefixes) {
return decimal < 0 ? prefixes.negativePrefix : prefixes.positivePrefix;
}
return decimal < 0 ? '-' : '';
}