Sky: one sidereal transform for sun, moon and stars; low-precision moon ephemeris and phase

sun.js exports daysSinceJ2000, gmstRad, sunEquatorial and equatorialToHorizontal (sunPosition is now their composition, unchanged in value). moon.js adds the Astronomical Almanac low-precision moon, phase from elongation (Meeus 48.3), topocentric position. Tests against Meeus ex. 47.a and USNO phases.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-20 20:32:24 +01:00
commit 18ad84aff5
3 changed files with 166 additions and 7 deletions

88
src/sky/moon.js Normal file
View file

@ -0,0 +1,88 @@
// The moon for a place and a moment. Dependency-free ES module beside sun.js, whose sidereal transform it shares.
//
// moonEquatorial(time) -> { ra, dec, distanceKm, lambda, beta } radians, of date
// moonPhase(time) -> { elongationDeg, phaseAngleDeg, illuminated, waxing }
// moonPosition(time, lat, lon) -> { elevation, azimuth } degrees, topocentric, no refraction
//
// Position is the low-precision series of the Astronomical Almanac (section D, "Low precision formulae for the Moon", T in Julian
// centuries from J2000.0): about 0.3 degrees in longitude and 0.2 in latitude, a few hundred km in distance, for 1950..2050.
// Phase is the elongation from the sun (sun.js) and the phase angle of Meeus, "Astronomical Algorithms", 2nd ed., eq. 48.3.
import { daysSinceJ2000, equatorialToHorizontal, sunEquatorial } from './sun.js';
const RAD = Math.PI / 180;
const EARTH_RADIUS_KM = 6378.14;
const AU_KM = 149597870.7;
const sinD = (deg) => Math.sin(deg * RAD);
const cosD = (deg) => Math.cos(deg * RAD);
/** The moon's ecliptic longitude and latitude (radians), horizontal parallax (radians) and distance (km), of date. */
function moonEcliptic(time) {
const T = daysSinceJ2000(time) / 36525;
const lambda =
218.32 +
481267.881 * T +
6.29 * sinD(135.0 + 477198.87 * T) -
1.27 * sinD(259.3 - 413335.36 * T) +
0.66 * sinD(235.7 + 890534.22 * T) +
0.21 * sinD(269.9 + 954397.74 * T) -
0.19 * sinD(357.5 + 35999.05 * T) -
0.11 * sinD(186.5 + 966404.03 * T);
const beta =
5.13 * sinD(93.3 + 483202.02 * T) +
0.28 * sinD(228.2 + 960400.89 * T) -
0.28 * sinD(318.3 + 6003.15 * T) -
0.17 * sinD(217.6 - 407332.21 * T);
const parallax =
0.9508 +
0.0518 * cosD(135.0 + 477198.87 * T) +
0.0095 * cosD(259.3 - 413335.36 * T) +
0.0078 * cosD(235.7 + 890534.22 * T) +
0.0028 * cosD(269.9 + 954397.74 * T);
return {
lambda: lambda * RAD,
beta: beta * RAD,
parallax: parallax * RAD,
distanceKm: EARTH_RADIUS_KM / Math.sin(parallax * RAD),
};
}
/** Right ascension, declination (radians, of date), distance (km) and ecliptic coordinates (radians) of the moon at a UTC moment. */
export function moonEquatorial(time) {
const { lambda, beta, distanceKm } = moonEcliptic(time);
const l = Math.cos(beta) * Math.cos(lambda);
const m = 0.9175 * Math.cos(beta) * Math.sin(lambda) - 0.3978 * Math.sin(beta);
const n = 0.3978 * Math.cos(beta) * Math.sin(lambda) + 0.9175 * Math.sin(beta);
return { ra: Math.atan2(m, l), dec: Math.asin(n), distanceKm, lambda, beta };
}
/**
* The phase at a UTC moment. `elongationDeg` is the geocentric angle sun-earth-moon side: 0 new, 90 first quarter, 180 full, 270 last quarter
* (by the longitude difference, so it runs the whole circle). `phaseAngleDeg` is the angle sun-moon-earth: 0 full, 180 new. `illuminated` is
* the lit fraction of the disc, (1 + cos i) / 2.
*/
export function moonPhase(time) {
const moon = moonEquatorial(time);
const sun = sunEquatorial(time);
const sunDistanceKm = AU_KM;
const cosPsi = Math.cos(moon.beta) * Math.cos(moon.lambda - sun.lambda);
const psi = Math.acos(Math.min(1, Math.max(-1, cosPsi)));
const i = Math.atan2(sunDistanceKm * Math.sin(psi), moon.distanceKm - sunDistanceKm * Math.cos(psi));
const elongationDeg = ((((moon.lambda - sun.lambda) / RAD) % 360) + 360) % 360;
return {
elongationDeg,
phaseAngleDeg: i / RAD,
illuminated: (1 + Math.cos(i)) / 2,
waxing: elongationDeg < 180,
distanceKm: moon.distanceKm,
};
}
/** Elevation and azimuth of the moon in degrees at a UTC moment and place, topocentric (the parallax lowers it up to a degree), no refraction. Azimuth clockwise from north. */
export function moonPosition(time, lat, lon) {
const m = moonEquatorial(time);
const { elevation, azimuth } = equatorialToHorizontal(m.ra, m.dec, time, lat, lon);
const parallaxDeg = Math.asin(EARTH_RADIUS_KM / m.distanceKm) / RAD;
return { elevation: elevation - parallaxDeg * cosD(elevation), azimuth };
}

50
src/sky/moon.test.ts Normal file
View file

@ -0,0 +1,50 @@
import { describe, expect, it } from 'vitest';
import { moonEquatorial, moonPhase, moonPosition } from './moon.js';
import { sunPosition } from './sun.js';
const DEG = 180 / Math.PI;
describe('moon position', () => {
it('matches Meeus, Astronomical Algorithms ex. 47.a (1992 April 12, 0h TD) within the series\' 0.3 degrees', () => {
// Meeus: ra 134.688470, dec +13.768368, distance 368409.7 km (full series).
const m = moonEquatorial(Date.UTC(1992, 3, 12, 0, 0, 0));
expect(Math.abs(m.ra * DEG - 134.68847)).toBeLessThan(0.3);
expect(Math.abs(m.dec * DEG - 13.768368)).toBeLessThan(0.3);
expect(Math.abs(m.distanceKm - 368409.7)).toBeLessThan(1500);
});
it('is opposite the sun at full moon, with the parallax of a few tenths of a degree at most', () => {
// Total lunar eclipse of 2026-03-03, greatest eclipse 11:33 UTC: the moon sits opposite the sun.
const t = '2026-03-03T11:33:00Z';
const moon = moonPosition(t, 48.19, 16.35);
const sun = sunPosition(t, 48.19, 16.35);
expect(Math.abs(moon.elevation + sun.elevation)).toBeLessThan(1.5);
const dAz = Math.abs(((moon.azimuth - sun.azimuth + 360) % 360) - 180);
expect(dAz).toBeLessThan(2);
});
});
describe('moon phase', () => {
// US Naval Observatory phases of January 2000 and the lunar eclipse of March 2026 (UTC); the series is good to ~0.5 degree of
// elongation, which is an hour of the moon's 12.2 degrees a day relative to the sun, so these hold to a degree and a half.
const phases: [string, number][] = [
['2000-01-06T18:14:00Z', 0],
['2000-01-14T13:34:00Z', 90],
['2000-01-21T04:40:00Z', 180],
['2000-01-28T07:57:00Z', 270],
['2026-03-03T11:38:00Z', 180],
];
it.each(phases)('elongation at %s is %d degrees', (t, e) => {
const d = ((moonPhase(t).elongationDeg - e + 540) % 360) - 180;
expect(Math.abs(d)).toBeLessThan(1.5);
});
it('lit fraction: dark at new, half at the quarters, full at full; waxing between new and full', () => {
expect(moonPhase('2000-01-06T18:14:00Z').illuminated).toBeLessThan(0.01);
expect(moonPhase('2000-01-14T13:34:00Z').illuminated).toBeCloseTo(0.5, 1);
expect(moonPhase('2000-01-21T04:40:00Z').illuminated).toBeGreaterThan(0.99);
expect(moonPhase('2000-01-10T00:00:00Z').waxing).toBe(true);
expect(moonPhase('2000-01-25T00:00:00Z').waxing).toBe(false);
expect(moonPhase('2000-01-21T04:40:00Z').phaseAngleDeg).toBeLessThan(2);
});
});

View file

@ -29,17 +29,32 @@ const mix = (a, b, t) => a + (b - a) * t;
const toMs = (time) => (time instanceof Date ? time.getTime() : typeof time === 'string' ? Date.parse(time) : Number(time));
/** Sun elevation and azimuth in degrees at a UTC moment and place. Azimuth runs clockwise from north. */
export function sunPosition(time, lat, lon) {
const d = toMs(time) / 86400000 - 10957.5; // days since J2000.0 (2000-01-01 12:00 UTC)
/** Days since J2000.0 (2000-01-01 12:00 UTC) at a UTC moment. */
export const daysSinceJ2000 = (time) => toMs(time) / 86400000 - 10957.5;
/** Greenwich mean sidereal time in radians (unreduced) at `d` days since J2000.0. */
export const gmstRad = (d) => (280.46061837 + 360.98564736629 * d) * RAD;
/** The sun's right ascension and declination of date, in radians, and its ecliptic longitude `lambda`, at a UTC moment. */
export function sunEquatorial(time) {
const d = daysSinceJ2000(time);
const L = (280.46 + 0.9856474 * d) * RAD; // mean longitude
const g = (357.528 + 0.9856003 * d) * RAD; // mean anomaly
const lambda = L + (1.915 * Math.sin(g) + 0.02 * Math.sin(2 * g)) * RAD; // ecliptic longitude
const eps = (23.439 - 0.0000004 * d) * RAD; // obliquity
const dec = Math.asin(Math.sin(eps) * Math.sin(lambda));
const ra = Math.atan2(Math.cos(eps) * Math.sin(lambda), Math.cos(lambda));
const gmst = (280.46061837 + 360.98564736629 * d) * RAD;
const hourAngle = gmst + lon * RAD - ra;
return {
dec: Math.asin(Math.sin(eps) * Math.sin(lambda)),
ra: Math.atan2(Math.cos(eps) * Math.sin(lambda), Math.cos(lambda)),
lambda,
};
}
/**
* Elevation and azimuth in degrees of a point of the sky at a UTC moment and place, from its right ascension and declination of date
* (radians): the one sidereal transform of the sun, the moon and the stars. Azimuth runs clockwise from north; no refraction.
*/
export function equatorialToHorizontal(ra, dec, time, lat, lon) {
const hourAngle = gmstRad(daysSinceJ2000(time)) + lon * RAD - ra;
const phi = lat * RAD;
const sinEl = Math.sin(phi) * Math.sin(dec) + Math.cos(phi) * Math.cos(dec) * Math.cos(hourAngle);
// Hour angle is westward-positive, so atan2 gives azimuth from south, westward; +180 turns it into
@ -51,6 +66,12 @@ export function sunPosition(time, lat, lon) {
};
}
/** Sun elevation and azimuth in degrees at a UTC moment and place. Azimuth runs clockwise from north. */
export function sunPosition(time, lat, lon) {
const { ra, dec } = sunEquatorial(time);
return equatorialToHorizontal(ra, dec, time, lat, lon);
}
/** Sun elevation in degrees at a UTC moment and place. */
export const sunElevation = (time, lat, lon) => sunPosition(time, lat, lon).elevation;