Skip to content

Commit d4660de

Browse files
committed
JS: Ecliptic function returns ECT instead of ECL.
1 parent 47ce0ac commit d4660de

19 files changed

+1338
-1325
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ of complexity. So I decided to create Astronomy Engine with the following engine
187187
- Support JavaScript, C, C#, and Python with the same algorithms, and verify them to produce identical results.
188188
(Kotlin support was added in 2022.)
189189
- No external dependencies! The code must not require anything outside the standard library for each language.
190-
- Minified JavaScript code less than 120K. (The current size is <!--MINIFIED_SIZE-->117416 bytes.)
190+
- Minified JavaScript code less than 120K. (The current size is <!--MINIFIED_SIZE-->117533 bytes.)
191191
- Accuracy always within 1 arcminute of results from NOVAS.
192192
- It would be well documented, relatively easy to use, and support a wide variety of common use cases.
193193

Diff for: demo/browser/astronomy.browser.js

+27-27
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,6 @@ function MassProduct(body) {
190190
}
191191
}
192192
exports.MassProduct = MassProduct;
193-
let ob2000; // lazy-evaluated mean obliquity of the ecliptic at J2000, in radians
194-
let cos_ob2000;
195-
let sin_ob2000;
196193
function VerifyBoolean(b) {
197194
if (b !== true && b !== false) {
198195
console.trace();
@@ -2753,27 +2750,31 @@ function RotateEquatorialToEcliptic(equ, cos_ob, sin_ob) {
27532750
return new EclipticCoordinates(ecl, elat, elon);
27542751
}
27552752
/**
2756-
* @brief Converts equatorial Cartesian coordinates to ecliptic Cartesian and angular coordinates.
2753+
* @brief Converts a J2000 mean equator (EQJ) vector to a true ecliptic of date (ETC) vector and angles.
27572754
*
2758-
* Given J2000 equatorial Cartesian coordinates,
2759-
* returns J2000 ecliptic latitude, longitude, and cartesian coordinates.
2760-
* You can call {@link GeoVector} and pass the resulting vector to this function.
2755+
* Given coordinates relative to the Earth's equator at J2000 (the instant of noon UTC
2756+
* on 1 January 2000), this function converts those coordinates to true ecliptic coordinates
2757+
* that are relative to the plane of the Earth's orbit around the Sun on that date.
27612758
*
2762-
* @param {Vector} equ
2763-
* A vector in the J2000 equatorial coordinate system.
2759+
* @param {Vector} eqj
2760+
* Equatorial coordinates in the EQJ frame of reference.
2761+
* You can call {@link GeoVector} to obtain suitable equatorial coordinates.
27642762
*
27652763
* @returns {EclipticCoordinates}
27662764
*/
2767-
function Ecliptic(equ) {
2768-
// Based on NOVAS functions equ2ecl() and equ2ecl_vec().
2769-
if (ob2000 === undefined) {
2770-
// Lazy-evaluate and keep the mean obliquity of the ecliptic at J2000.
2771-
// This way we don't need to crunch the numbers more than once.
2772-
ob2000 = exports.DEG2RAD * e_tilt(MakeTime(J2000)).mobl;
2773-
cos_ob2000 = Math.cos(ob2000);
2774-
sin_ob2000 = Math.sin(ob2000);
2775-
}
2776-
return RotateEquatorialToEcliptic(equ, cos_ob2000, sin_ob2000);
2765+
function Ecliptic(eqj) {
2766+
// Calculate nutation and obliquity for this time.
2767+
// As an optimization, the nutation angles are cached in `time`,
2768+
// and reused below when the `nutation` function is called.
2769+
const et = e_tilt(eqj.t);
2770+
// Convert mean J2000 equator (EQJ) to true equator of date (EQD).
2771+
const eqj_pos = [eqj.x, eqj.y, eqj.z];
2772+
const mean_pos = precession(eqj_pos, eqj.t, PrecessDirection.From2000);
2773+
const [x, y, z] = nutation(mean_pos, eqj.t, PrecessDirection.From2000);
2774+
const eqd = new Vector(x, y, z, eqj.t);
2775+
// Rotate from EQD to true ecliptic of date (ECT).
2776+
const tobl = et.tobl * exports.DEG2RAD;
2777+
return RotateEquatorialToEcliptic(eqd, Math.cos(tobl), Math.sin(tobl));
27772778
}
27782779
exports.Ecliptic = Ecliptic;
27792780
/**
@@ -4345,21 +4346,20 @@ function AngleFromSun(body, date) {
43454346
}
43464347
exports.AngleFromSun = AngleFromSun;
43474348
/**
4348-
* @brief Calculates heliocentric ecliptic longitude based on the J2000 equinox.
4349+
* @brief Calculates heliocentric ecliptic longitude of a body.
4350+
*
4351+
* This function calculates the angle around the plane of the Earth's orbit
4352+
* of a celestial body, as seen from the center of the Sun.
4353+
* The angle is measured prograde (in the direction of the Earth's orbit around the Sun)
4354+
* in degrees from the true equinox of date. The ecliptic longitude is always in the range [0, 360).
43494355
*
43504356
* @param {Body} body
4351-
* The name of a celestial body other than the Sun.
4357+
* A body other than the Sun.
43524358
*
43534359
* @param {FlexibleDateTime} date
43544360
* The date and time for which to calculate the ecliptic longitude.
43554361
*
43564362
* @returns {number}
4357-
* The ecliptic longitude angle of the body in degrees measured counterclockwise around the mean
4358-
* plane of the Earth's orbit, as seen from above the Sun's north pole.
4359-
* Ecliptic longitude starts at 0 at the J2000
4360-
* <a href="https://en.wikipedia.org/wiki/Equinox_(celestial_coordinates)">equinox</a> and
4361-
* increases in the same direction the Earth orbits the Sun.
4362-
* The returned value is always in the range [0, 360).
43634363
*/
43644364
function EclipticLongitude(body, date) {
43654365
if (body === Body.Sun)

Diff for: demo/nodejs/astronomy.js

+27-27
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,6 @@ function MassProduct(body) {
189189
}
190190
}
191191
exports.MassProduct = MassProduct;
192-
let ob2000; // lazy-evaluated mean obliquity of the ecliptic at J2000, in radians
193-
let cos_ob2000;
194-
let sin_ob2000;
195192
function VerifyBoolean(b) {
196193
if (b !== true && b !== false) {
197194
console.trace();
@@ -2752,27 +2749,31 @@ function RotateEquatorialToEcliptic(equ, cos_ob, sin_ob) {
27522749
return new EclipticCoordinates(ecl, elat, elon);
27532750
}
27542751
/**
2755-
* @brief Converts equatorial Cartesian coordinates to ecliptic Cartesian and angular coordinates.
2752+
* @brief Converts a J2000 mean equator (EQJ) vector to a true ecliptic of date (ETC) vector and angles.
27562753
*
2757-
* Given J2000 equatorial Cartesian coordinates,
2758-
* returns J2000 ecliptic latitude, longitude, and cartesian coordinates.
2759-
* You can call {@link GeoVector} and pass the resulting vector to this function.
2754+
* Given coordinates relative to the Earth's equator at J2000 (the instant of noon UTC
2755+
* on 1 January 2000), this function converts those coordinates to true ecliptic coordinates
2756+
* that are relative to the plane of the Earth's orbit around the Sun on that date.
27602757
*
2761-
* @param {Vector} equ
2762-
* A vector in the J2000 equatorial coordinate system.
2758+
* @param {Vector} eqj
2759+
* Equatorial coordinates in the EQJ frame of reference.
2760+
* You can call {@link GeoVector} to obtain suitable equatorial coordinates.
27632761
*
27642762
* @returns {EclipticCoordinates}
27652763
*/
2766-
function Ecliptic(equ) {
2767-
// Based on NOVAS functions equ2ecl() and equ2ecl_vec().
2768-
if (ob2000 === undefined) {
2769-
// Lazy-evaluate and keep the mean obliquity of the ecliptic at J2000.
2770-
// This way we don't need to crunch the numbers more than once.
2771-
ob2000 = exports.DEG2RAD * e_tilt(MakeTime(J2000)).mobl;
2772-
cos_ob2000 = Math.cos(ob2000);
2773-
sin_ob2000 = Math.sin(ob2000);
2774-
}
2775-
return RotateEquatorialToEcliptic(equ, cos_ob2000, sin_ob2000);
2764+
function Ecliptic(eqj) {
2765+
// Calculate nutation and obliquity for this time.
2766+
// As an optimization, the nutation angles are cached in `time`,
2767+
// and reused below when the `nutation` function is called.
2768+
const et = e_tilt(eqj.t);
2769+
// Convert mean J2000 equator (EQJ) to true equator of date (EQD).
2770+
const eqj_pos = [eqj.x, eqj.y, eqj.z];
2771+
const mean_pos = precession(eqj_pos, eqj.t, PrecessDirection.From2000);
2772+
const [x, y, z] = nutation(mean_pos, eqj.t, PrecessDirection.From2000);
2773+
const eqd = new Vector(x, y, z, eqj.t);
2774+
// Rotate from EQD to true ecliptic of date (ECT).
2775+
const tobl = et.tobl * exports.DEG2RAD;
2776+
return RotateEquatorialToEcliptic(eqd, Math.cos(tobl), Math.sin(tobl));
27762777
}
27772778
exports.Ecliptic = Ecliptic;
27782779
/**
@@ -4344,21 +4345,20 @@ function AngleFromSun(body, date) {
43444345
}
43454346
exports.AngleFromSun = AngleFromSun;
43464347
/**
4347-
* @brief Calculates heliocentric ecliptic longitude based on the J2000 equinox.
4348+
* @brief Calculates heliocentric ecliptic longitude of a body.
4349+
*
4350+
* This function calculates the angle around the plane of the Earth's orbit
4351+
* of a celestial body, as seen from the center of the Sun.
4352+
* The angle is measured prograde (in the direction of the Earth's orbit around the Sun)
4353+
* in degrees from the true equinox of date. The ecliptic longitude is always in the range [0, 360).
43484354
*
43494355
* @param {Body} body
4350-
* The name of a celestial body other than the Sun.
4356+
* A body other than the Sun.
43514357
*
43524358
* @param {FlexibleDateTime} date
43534359
* The date and time for which to calculate the ecliptic longitude.
43544360
*
43554361
* @returns {number}
4356-
* The ecliptic longitude angle of the body in degrees measured counterclockwise around the mean
4357-
* plane of the Earth's orbit, as seen from above the Sun's north pole.
4358-
* Ecliptic longitude starts at 0 at the J2000
4359-
* <a href="https://en.wikipedia.org/wiki/Equinox_(celestial_coordinates)">equinox</a> and
4360-
* increases in the same direction the Earth orbits the Sun.
4361-
* The returned value is always in the range [0, 360).
43624362
*/
43634363
function EclipticLongitude(body, date) {
43644364
if (body === Body.Sun)

Diff for: demo/nodejs/calendar/astronomy.ts

+29-28
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,6 @@ export function MassProduct(body: Body): number {
216216
}
217217

218218

219-
let ob2000: number; // lazy-evaluated mean obliquity of the ecliptic at J2000, in radians
220-
let cos_ob2000: number;
221-
let sin_ob2000: number;
222-
223219
function VerifyBoolean(b: boolean): boolean {
224220
if (b !== true && b !== false) {
225221
console.trace();
@@ -2999,27 +2995,33 @@ function RotateEquatorialToEcliptic(equ: Vector, cos_ob: number, sin_ob: number)
29992995
}
30002996

30012997
/**
3002-
* @brief Converts equatorial Cartesian coordinates to ecliptic Cartesian and angular coordinates.
2998+
* @brief Converts a J2000 mean equator (EQJ) vector to a true ecliptic of date (ETC) vector and angles.
30032999
*
3004-
* Given J2000 equatorial Cartesian coordinates,
3005-
* returns J2000 ecliptic latitude, longitude, and cartesian coordinates.
3006-
* You can call {@link GeoVector} and pass the resulting vector to this function.
3000+
* Given coordinates relative to the Earth's equator at J2000 (the instant of noon UTC
3001+
* on 1 January 2000), this function converts those coordinates to true ecliptic coordinates
3002+
* that are relative to the plane of the Earth's orbit around the Sun on that date.
30073003
*
3008-
* @param {Vector} equ
3009-
* A vector in the J2000 equatorial coordinate system.
3004+
* @param {Vector} eqj
3005+
* Equatorial coordinates in the EQJ frame of reference.
3006+
* You can call {@link GeoVector} to obtain suitable equatorial coordinates.
30103007
*
30113008
* @returns {EclipticCoordinates}
30123009
*/
3013-
export function Ecliptic(equ: Vector): EclipticCoordinates {
3014-
// Based on NOVAS functions equ2ecl() and equ2ecl_vec().
3015-
if (ob2000 === undefined) {
3016-
// Lazy-evaluate and keep the mean obliquity of the ecliptic at J2000.
3017-
// This way we don't need to crunch the numbers more than once.
3018-
ob2000 = DEG2RAD * e_tilt(MakeTime(J2000)).mobl;
3019-
cos_ob2000 = Math.cos(ob2000);
3020-
sin_ob2000 = Math.sin(ob2000);
3021-
}
3022-
return RotateEquatorialToEcliptic(equ, cos_ob2000, sin_ob2000);
3010+
export function Ecliptic(eqj: Vector): EclipticCoordinates {
3011+
// Calculate nutation and obliquity for this time.
3012+
// As an optimization, the nutation angles are cached in `time`,
3013+
// and reused below when the `nutation` function is called.
3014+
const et = e_tilt(eqj.t);
3015+
3016+
// Convert mean J2000 equator (EQJ) to true equator of date (EQD).
3017+
const eqj_pos: ArrayVector = [eqj.x, eqj.y, eqj.z];
3018+
const mean_pos = precession(eqj_pos, eqj.t, PrecessDirection.From2000);
3019+
const [x, y, z] = nutation(mean_pos, eqj.t, PrecessDirection.From2000);
3020+
const eqd = new Vector(x, y, z, eqj.t)
3021+
3022+
// Rotate from EQD to true ecliptic of date (ECT).
3023+
const tobl = et.tobl * DEG2RAD;
3024+
return RotateEquatorialToEcliptic(eqd, Math.cos(tobl), Math.sin(tobl));
30233025
}
30243026

30253027
/**
@@ -4887,21 +4889,20 @@ export function AngleFromSun(body: Body, date: FlexibleDateTime): number {
48874889
}
48884890

48894891
/**
4890-
* @brief Calculates heliocentric ecliptic longitude based on the J2000 equinox.
4892+
* @brief Calculates heliocentric ecliptic longitude of a body.
4893+
*
4894+
* This function calculates the angle around the plane of the Earth's orbit
4895+
* of a celestial body, as seen from the center of the Sun.
4896+
* The angle is measured prograde (in the direction of the Earth's orbit around the Sun)
4897+
* in degrees from the true equinox of date. The ecliptic longitude is always in the range [0, 360).
48914898
*
48924899
* @param {Body} body
4893-
* The name of a celestial body other than the Sun.
4900+
* A body other than the Sun.
48944901
*
48954902
* @param {FlexibleDateTime} date
48964903
* The date and time for which to calculate the ecliptic longitude.
48974904
*
48984905
* @returns {number}
4899-
* The ecliptic longitude angle of the body in degrees measured counterclockwise around the mean
4900-
* plane of the Earth's orbit, as seen from above the Sun's north pole.
4901-
* Ecliptic longitude starts at 0 at the J2000
4902-
* <a href="https://en.wikipedia.org/wiki/Equinox_(celestial_coordinates)">equinox</a> and
4903-
* increases in the same direction the Earth orbits the Sun.
4904-
* The returned value is always in the range [0, 360).
49054906
*/
49064907
export function EclipticLongitude(body: Body, date: FlexibleDateTime): number {
49074908
if (body === Body.Sun)

0 commit comments

Comments
 (0)