Skip to content

Instantly share code, notes, and snippets.

@cosimo
Created November 20, 2022 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosimo/b8b0ad22f6f6961f20b06095327fe2a6 to your computer and use it in GitHub Desktop.
Save cosimo/b8b0ad22f6f6961f20b06095327fe2a6 to your computer and use it in GitHub Desktop.
Calculate position of the Sun and Moon from the Earth frame of reference (equatorial coordinates)
// https://stackoverflow.com/a/11760121/11303
function julian_days() {
const date = new Date();
//const jd = Math.floor((date / 86400000) - (date.getTimezoneOffset() / 1440) + 2440587.5);
const jd = (date / 86400000) - (date.getTimezoneOffset() / 1440) + 2440587.5;
console.log("jd=" + jd);
return jd;
}
// https://en.wikipedia.org/wiki/Position_of_the_Sun
function sun_position() {
const JD = julian_days();
// Days since J2000
const n = JD - 2451545.0;
// Mean longitude of the Sun
const L = 280.460 + 0.9856474 * n;
// Mean anomaly
const g = 357.528 + 0.9856003 * n;
const L_mod_360 = L % 360;
const g_mod_360 = g % 360;
const ecliptic_longitude = L + 1.915 * Math.sin(g) + 0.020 * Math.sin(2 * g);
const ecliptic_latitude = 0.0;
const distance_from_earth = 1.00014 - 0.01671 * Math.cos(g) - 0.00014 * Math.cos(2 * g);
const obliquity_of_ecliptic = 23.439 - 0.0000004 * n;
const right_ascension = Math.atan2(Math.cos(obliquity_of_ecliptic) * Math.sin(ecliptic_longitude), Math.cos(ecliptic_longitude));
const declination = Math.asin(Math.sin(obliquity_of_ecliptic) * Math.sin(ecliptic_longitude));
console.log("ra=" + right_ascension + " dec=" + declination);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment