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.
162 lines (161 loc) • 8.12 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSet = exports.getRise = exports.getTransit = void 0;
const coordinateCalc_1 = require("../coordinates/calculations/coordinateCalc");
const time_1 = require("../time");
const timeCalc_1 = require("../time/calculations/timeCalc");
const angleCalc_1 = require("./angleCalc");
const interpolationCalc_1 = require("./interpolationCalc");
function getTransit(ObjConstructor, location, jd0) {
return __awaiter(this, void 0, void 0, function* () {
let m0 = yield _getApproximatedMTransit(ObjConstructor, location, jd0);
let dm = 0;
let cnt = 0;
do {
dm = yield _getCorrectionsTransit(ObjConstructor, location, jd0, m0);
m0 += dm;
if (cnt++ > 100) {
throw new Error(`While loop overflow. Astronomical object has no transit on given day ${jd0}.`);
}
} while (Math.abs(dm) > 0.00001);
if (m0 < 0 || m0 >= 1) {
throw new Error(`Astronomical object has no transit on given day ${jd0}.`);
}
return jd0 + m0;
});
}
exports.getTransit = getTransit;
function getRise(ObjConstructor, location, jd0, h0) {
return __awaiter(this, void 0, void 0, function* () {
const m0 = yield _getApproximatedMTransit(ObjConstructor, location, jd0);
const mH = yield _getApproximatedMRiseSet(ObjConstructor, location, jd0, h0);
if (isNaN(mH)) {
throw new Error(`Astronomical object cannot rise on given day ${jd0}.`);
}
let m1 = (0, angleCalc_1.normalizeAngle)(m0 - mH, 1);
let dm = 0;
let cnt = 0;
do {
dm = yield _getCorrectionsRiseSet(ObjConstructor, location, jd0, h0, m1);
m1 += dm;
if (cnt++ > 100) {
throw new Error(`While loop overflow. Astronomical object cannot rise on given day ${jd0}.`);
}
} while (Math.abs(dm) > 0.00001);
if (m1 < 0) {
throw new Error(`Astronomical object cannot rise on given day ${jd0}. Rise happens the day before.`);
}
if (m1 >= 1) {
throw new Error(`Astronomical object cannot rise on given day ${jd0}. Rise happens the next day.`);
}
return jd0 + m1;
});
}
exports.getRise = getRise;
function getSet(ObjConstructor, location, jd0, h0) {
return __awaiter(this, void 0, void 0, function* () {
const m0 = yield _getApproximatedMTransit(ObjConstructor, location, jd0);
const mH = yield _getApproximatedMRiseSet(ObjConstructor, location, jd0, h0);
if (isNaN(mH)) {
throw new Error(`Astronomical object cannot set on given day ${jd0}.`);
}
let m2 = (0, angleCalc_1.normalizeAngle)(m0 + mH, 1);
let dm = 0;
let cnt = 0;
do {
dm = yield _getCorrectionsRiseSet(ObjConstructor, location, jd0, h0, m2);
m2 += dm;
if (cnt++ > 100) {
throw new Error(`While loop overflow. Astronomical object cannot set on given day ${jd0}.`);
}
} while (Math.abs(dm) > 0.00001);
if (m2 < 0) {
throw new Error(`Astronomical object cannot set on given day ${jd0}. Set happens the day before.`);
}
if (m2 >= 1) {
throw new Error(`Astronomical object cannot set on given day ${jd0}. Set happens the next day.`);
}
return jd0 + m2;
});
}
exports.getSet = getSet;
function _getApproximatedMTransit(ObjConstructor, location, jd0) {
return __awaiter(this, void 0, void 0, function* () {
const object = _createAstronomicalObject(ObjConstructor, jd0);
const { rightAscension } = yield object.getApparentGeocentricEquatorialSphericalCoordinates();
const T = (0, timeCalc_1.julianDay2julianCenturiesJ2000)(jd0);
const GAST = (0, timeCalc_1.getGreenwichApparentSiderealTime)(T);
const m0 = (rightAscension - location.lon - GAST) / 360;
return (0, angleCalc_1.normalizeAngle)(m0, 1);
});
}
function _getApproximatedMRiseSet(ObjConstructor, location, jd0, h0) {
return __awaiter(this, void 0, void 0, function* () {
const H0 = yield _getH0(ObjConstructor, location, jd0, h0);
return H0 / 360;
});
}
function _getCorrectionsTransit(ObjConstructor, location, jd0, m) {
return __awaiter(this, void 0, void 0, function* () {
const rightAscensionArray = yield (0, interpolationCalc_1.getRightAscensionInterpolationArray)(ObjConstructor, jd0, 1);
const n0 = _getN0(jd0, m);
const raInterpolated = (0, angleCalc_1.normalizeAngle)((0, interpolationCalc_1.tabularInterpolation3)(rightAscensionArray, n0));
const H = _getLocalHourAngle(raInterpolated, location.lon, jd0, m);
return -H / 360;
});
}
function _getCorrectionsRiseSet(ObjConstructor, location, jd0, h0, m) {
return __awaiter(this, void 0, void 0, function* () {
const { lat } = location;
const latRad = (0, angleCalc_1.deg2rad)(lat);
const rightAscensionArray = yield (0, interpolationCalc_1.getRightAscensionInterpolationArray)(ObjConstructor, jd0, 1);
const declinationArray = yield (0, interpolationCalc_1.getDeclinationInterpolationArray)(ObjConstructor, jd0, 1);
const n0 = _getN0(jd0, m);
const raInterpolated = (0, angleCalc_1.normalizeAngle)((0, interpolationCalc_1.tabularInterpolation3)(rightAscensionArray, n0));
const decInterpolated = (0, interpolationCalc_1.tabularInterpolation3)(declinationArray, n0);
const dRad = (0, angleCalc_1.deg2rad)(decInterpolated);
const H = _getLocalHourAngle(raInterpolated, location.lon, jd0, m);
const HRad = (0, angleCalc_1.deg2rad)(H);
const { altitude } = (0, coordinateCalc_1.equatorialSpherical2topocentricHorizontalByLocalHourAngle)(H, decInterpolated, lat);
return (altitude - h0) / (360 * Math.cos(dRad) * Math.cos(latRad) * Math.sin(HRad));
});
}
function _getLocalHourAngle(raInterpolated, longitude, jd0, m) {
const T = (0, timeCalc_1.julianDay2julianCenturiesJ2000)(jd0);
const GAST = (0, timeCalc_1.getGreenwichApparentSiderealTime)(T);
const theta0 = GAST + 360.985647 * m;
const H = theta0 + longitude - raInterpolated;
return (0, angleCalc_1.normalizeAngle)(H + 180) - 180;
}
function _getH0(ObjConstructor, location, jd0, h0) {
return __awaiter(this, void 0, void 0, function* () {
const object = _createAstronomicalObject(ObjConstructor, jd0);
const { declination } = yield object.getApparentGeocentricEquatorialSphericalCoordinates();
const latRad = (0, angleCalc_1.deg2rad)(location.lat);
const dRad = (0, angleCalc_1.deg2rad)(declination);
const h0Rad = (0, angleCalc_1.deg2rad)(h0);
const cosH0 = (Math.sin(h0Rad) - Math.sin(latRad) * Math.sin(dRad)) / (Math.cos(latRad) * Math.cos(dRad));
if (Math.abs(cosH0) > 1 && Math.abs(cosH0) <= 1.1) {
return 0;
}
return (0, angleCalc_1.rad2deg)(Math.acos(cosH0));
});
}
function _getN0(jd, m) {
const { year, month } = (0, timeCalc_1.julianDay2time)(jd);
const dT = (0, timeCalc_1.getDeltaT)(year, month);
return m + dT / 86400;
}
function _createAstronomicalObject(ObjConstructor, jd) {
const toi = time_1.createTimeOfInterest.fromJulianDay(jd);
return new ObjConstructor(toi);
}