Astronomia is a Javascript Library used for calculating various astronomical phenomena, such as the positions of the planets. It achieves this by implementing the celestial mechanics algorithms of the Belgian astronomer Jean Meeus as they are described in his great work on this subject Astronomical Algorithms.
These algorithms are only modelled approximations to our solar system and will never produce a result that precisely agrees with observation – though 2-3 significant figures correct can hardly be called a failure!
As it happens, there is not much documentation regarding Astronomia, though the code itself is at least reasonable well commented.
So here’s something to get you up and running quickly:
Step 1: After initialising a new node.js project in the usual way download Astronomia using npm:
npm install astronomia
Step 2: Modify package.json to accept module imports
{
"name": "astronomia-intro",
"version": "1.0.0",
"main": "main.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"astronomia": "^4.1.1"
}
}
Step 3: Make a new Javascript file and add a function like this:
import { julian, planetposition, elliptic, solar } from "astronomia";
import planetData from "astronomia/data";
/**
* Computes the current positions of the planets in equatorial coordinates at this moment in time using the Astronomia library
* @param {*} currentDate argument should be the object returned from javascript new Date() function
* @returns Returns array of form {PlanetName: [ra, dec]}
*/
export function GetPlanetaryPositions(currentDate) {
const currentJDE = julian.CalendarGregorianToJD(
currentDate.getFullYear(),
currentDate.getMonth() + 1,
currentDate.getDate() +
currentDate.getHours() / 24 +
currentDate.getMinutes() / 1440 +
currentDate.getSeconds() / 86400
); // Julian Date
const earth = new planetposition.Planet(planetData.vsop87Dearth);
const mercury = new planetposition.Planet(planetData.vsop87Dmercury);
const venus = new planetposition.Planet(planetData.vsop87Dvenus);
const mars = new planetposition.Planet(planetData.vsop87Dmars);
const mercuryPosition = elliptic.position(mercury, earth, currentJDE);
const venusPosition = elliptic.position(venus, earth, currentJDE);
const marsPosition = elliptic.position(mars, earth, currentJDE);
const solarPosition = solar.apparentEquatorial(currentJDE);
const planetaryPositions = {
Mercury: [mercuryPosition.ra, mercuryPosition.dec],
Venus: [venusPosition.ra, venusPosition.dec],
Mars: [marsPosition.ra, marsPosition.dec],
Sol: [solarPosition._ra, solarPosition._dec],
};
return planetaryPositions;
}
Step 4: Make a main.js file and call the function using the object returned from the Date() method:
import { GetPlanetaryPositions } from "./planet_positions.js";
const currentDate = new Date();
const planets = GetPlanetaryPositions(currentDate);
console.log(currentDate);
console.log(planets);
You should get some output like the following:
2024-11-30T01:53:47.019Z
{
Mercury: [ 4.546437945963073, -0.4143493117942818 ],
Venus: [ 5.128013793508959, -0.42082567506301716 ],
Mars: [ 2.2496232318071048, 0.37069470379916947 ],
Sol: [ -1.979117878118154, -0.3786921629022522 ]
}
Ready for use in any Javascript based project.
Git repo: https://github.com/WhiteLightningGun/astronomia_example1