@johnmorrisdotca/wakusei-planets 0.19.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/NOTICE.md +5 -3
  3. package/README.md +8 -5
  4. package/dist/calendar.d.ts +119 -0
  5. package/dist/calendar.js +154 -0
  6. package/dist/clock.d.ts +244 -0
  7. package/dist/clock.js +326 -0
  8. package/dist/coordinates.d.ts +177 -0
  9. package/dist/coordinates.js +169 -0
  10. package/dist/data/lunar.d.ts +4 -0
  11. package/dist/data/lunar.js +548 -0
  12. package/dist/data/pluto.d.ts +6 -0
  13. package/dist/data/pluto.js +29 -0
  14. package/dist/data/tables.d.ts +41 -0
  15. package/dist/data/tables.js +37 -0
  16. package/dist/data/vsop.d.ts +19 -1
  17. package/dist/data/vsop.js +25 -8
  18. package/dist/element.d.ts +3 -2
  19. package/dist/element.js +1 -0
  20. package/dist/ephemeris.d.ts +3 -15
  21. package/dist/ephemeris.js +66 -49
  22. package/dist/mars.d.ts +41 -0
  23. package/dist/mars.js +52 -0
  24. package/dist/scales.d.ts +46 -0
  25. package/dist/scales.js +75 -0
  26. package/dist/sidereal.d.ts +66 -0
  27. package/dist/sidereal.js +88 -0
  28. package/dist/sky/engine.d.ts +46 -0
  29. package/dist/sky/engine.js +145 -0
  30. package/dist/sky/events.d.ts +37 -0
  31. package/dist/sky/events.js +133 -0
  32. package/dist/suncalc-legacy.d.ts +187 -0
  33. package/dist/suncalc-legacy.js +168 -0
  34. package/dist/suncalc.d.ts +209 -0
  35. package/dist/suncalc.js +173 -0
  36. package/dist/tables/all.d.ts +2 -0
  37. package/dist/tables/all.js +2 -0
  38. package/dist/tables/ceres.d.ts +1 -0
  39. package/dist/tables/ceres.js +57 -0
  40. package/dist/tables/dwarfs.d.ts +1 -0
  41. package/dist/tables/dwarfs.js +59 -0
  42. package/dist/theory.d.ts +20 -0
  43. package/dist/theory.js +98 -0
  44. package/dist/time.d.ts +6 -108
  45. package/dist/time.js +7 -180
  46. package/dist/version.d.ts +1 -1
  47. package/dist/version.js +1 -1
  48. package/dist/when.d.ts +16 -0
  49. package/dist/when.js +18 -0
  50. package/package.json +45 -3
package/dist/scales.js ADDED
@@ -0,0 +1,75 @@
1
+ // Time scales: UTC (the clock, as JavaScript counts it, no leap seconds), UT1 (the Earth's turn), TAI (atomic time), GPS, TT (terrestrial time) and TDB (barycentric dynamical time), with the leap seconds from 1972 (IERS Bulletin C)
2
+ // and delta T before. Every moment is a number of milliseconds since 1970 read on the named scale's own clock, so `convertTimeScale` adds or takes off the difference. Pure: no DOM, no three.js.
3
+ import { J2000_JD, TT_MINUS_TAI_SECONDS, taiMinusUtc, toJulianDate } from "./clock.js";
4
+ import { moment } from "./when.js";
5
+ const GPS_MINUS_TAI_SECONDS = -19;
6
+ /**
7
+ * TDB minus TT in seconds: a term that follows the Earth round the Sun and is never more than 1.7 milliseconds (Meeus's two terms, 0.001657 sin g and 0.000014 sin 2g, good to about 30 microseconds).
8
+ * @param when - A `Date` or milliseconds since 1970 (read as TT).
9
+ * @returns Seconds.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { tdbMinusTt } from "@johnmorrisdotca/wakusei-planets/time";
14
+ *
15
+ * console.log(Math.abs(tdbMinusTt(Date.UTC(2026, 0, 3))) < 0.002);
16
+ * ```
17
+ */
18
+ export function tdbMinusTt(when) {
19
+ const g = ((357.53 + 0.98560028 * (toJulianDate(when) - J2000_JD)) * Math.PI) / 180;
20
+ return 0.001657 * Math.sin(g) + 0.000014 * Math.sin(2 * g);
21
+ }
22
+ function toUtc(ms, from, dut1) {
23
+ if (from === "utc")
24
+ return ms;
25
+ if (from === "ut1")
26
+ return ms - dut1 * 1000;
27
+ let tai = ms;
28
+ if (from === "gps")
29
+ tai = ms - GPS_MINUS_TAI_SECONDS * 1000;
30
+ else if (from === "tt")
31
+ tai = ms - TT_MINUS_TAI_SECONDS * 1000;
32
+ else if (from === "tdb")
33
+ tai = ms - tdbMinusTt(ms) * 1000 - TT_MINUS_TAI_SECONDS * 1000;
34
+ let utc = tai;
35
+ for (let i = 0; i < 3; i += 1)
36
+ utc = tai - taiMinusUtc(utc) * 1000;
37
+ return utc;
38
+ }
39
+ function fromUtc(utc, to, dut1) {
40
+ if (to === "utc")
41
+ return utc;
42
+ if (to === "ut1")
43
+ return utc + dut1 * 1000;
44
+ const tai = utc + taiMinusUtc(utc) * 1000;
45
+ if (to === "tai")
46
+ return tai;
47
+ if (to === "gps")
48
+ return tai + GPS_MINUS_TAI_SECONDS * 1000;
49
+ const tt = tai + TT_MINUS_TAI_SECONDS * 1000;
50
+ return to === "tt" ? tt : tt + tdbMinusTt(tt) * 1000;
51
+ }
52
+ /**
53
+ * A moment on one time scale as the same moment on another, in milliseconds since 1970 read on the target scale's own clock. UTC to TT is the leap seconds plus 32.184 s from 1972 and delta T before;
54
+ * UT1 needs `dut1` (UT1 minus UTC, from the IERS) and is UTC when it is left out, which is within 0.9 s; the TDB term is under 2 ms.
55
+ * @param when - A `Date` or a number of milliseconds since 1970, on the `from` scale.
56
+ * @param from - The scale it is on.
57
+ * @param to - The scale wanted.
58
+ * @param options - `dut1`, UT1 minus UTC in seconds, default 0.
59
+ * @returns Milliseconds since 1970 on the `to` scale.
60
+ *
61
+ * @example
62
+ * ```ts
63
+ * import { convertTimeScale } from "@johnmorrisdotca/wakusei-planets/time";
64
+ *
65
+ * const utc = Date.UTC(2026, 10, 7, 12);
66
+ * console.log((convertTimeScale(utc, "utc", "tt") - utc) / 1000, (convertTimeScale(utc, "utc", "gps") - utc) / 1000);
67
+ * ```
68
+ */
69
+ export function convertTimeScale(when, from, to, options = {}) {
70
+ const dut1 = options.dut1 ?? 0;
71
+ if (!Number.isFinite(dut1))
72
+ throw new RangeError(`dut1 must be a finite number of seconds, got ${dut1}`);
73
+ const ms = moment(when);
74
+ return from === to ? ms : fromUtc(toUtc(ms, from, dut1), to, dut1);
75
+ }
@@ -0,0 +1,66 @@
1
+ import { type When } from "./when.ts";
2
+ /** Nutation in longitude and in obliquity, and the mean obliquity of date, in radians, at `T` Julian centuries of terrestrial time from J2000 (Meeus ch. 22, accurate to 0.5 arc-seconds). */
3
+ export declare function nutation(T: number): {
4
+ longitude: number;
5
+ obliquity: number;
6
+ meanObliquity: number;
7
+ };
8
+ /**
9
+ * The Greenwich mean sidereal time in degrees, 0 to 360 (divide by 15 for hours).
10
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
11
+ * @param dut1 - UT1 minus UTC in seconds, from the IERS; default 0 (within 0.9 s).
12
+ * @returns Degrees.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * import { greenwichMeanSiderealTime } from "@johnmorrisdotca/wakusei-planets/time";
17
+ *
18
+ * console.log((greenwichMeanSiderealTime(Date.UTC(1987, 3, 10)) / 15).toFixed(4));
19
+ * ```
20
+ */
21
+ export declare function greenwichMeanSiderealTime(when: When, dut1?: number): number;
22
+ /**
23
+ * The equation of the equinoxes in degrees: the nutation in longitude times the cosine of the obliquity, what apparent sidereal time has over mean (about 1 second of time at most).
24
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
25
+ * @returns Degrees, between about -0.0013 and 0.0013.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * import { equationOfTheEquinoxes } from "@johnmorrisdotca/wakusei-planets/time";
30
+ *
31
+ * console.log((equationOfTheEquinoxes(Date.UTC(1987, 3, 10)) * 240).toFixed(4));
32
+ * ```
33
+ */
34
+ export declare function equationOfTheEquinoxes(when: When): number;
35
+ /**
36
+ * The Greenwich apparent sidereal time in degrees, 0 to 360: the mean time with the equation of the equinoxes added, which is what the hour angle of a body is measured against.
37
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
38
+ * @param dut1 - UT1 minus UTC in seconds, from the IERS; default 0.
39
+ * @returns Degrees.
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * import { greenwichApparentSiderealTime } from "@johnmorrisdotca/wakusei-planets/time";
44
+ *
45
+ * console.log((greenwichApparentSiderealTime(Date.UTC(1987, 3, 10)) / 15).toFixed(4));
46
+ * ```
47
+ */
48
+ export declare function greenwichApparentSiderealTime(when: When, dut1?: number): number;
49
+ /**
50
+ * The local sidereal time in degrees, 0 to 360, at a longitude east of Greenwich (west negative): the right ascension on the meridian.
51
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
52
+ * @param longitude - Degrees east.
53
+ * @param options - `apparent` (default `true`: with the equation of the equinoxes) and `dut1`, UT1 minus UTC in seconds.
54
+ * @returns Degrees.
55
+ *
56
+ * @example
57
+ * ```ts
58
+ * import { localSiderealTime } from "@johnmorrisdotca/wakusei-planets/time";
59
+ *
60
+ * console.log(localSiderealTime(Date.UTC(2026, 10, 7, 12), -123.1).toFixed(2));
61
+ * ```
62
+ */
63
+ export declare function localSiderealTime(when: When, longitude: number, options?: {
64
+ apparent?: boolean;
65
+ dut1?: number;
66
+ }): number;
@@ -0,0 +1,88 @@
1
+ // Sidereal time: the Earth's turn against the stars. Greenwich mean sidereal time (IAU 1982, Meeus ch. 12) from UT1, the equation of the equinoxes from the nutation (IAU 1980, its largest terms, 0.5 arc-seconds),
2
+ // the apparent time that adds it, and the local time at a longitude. Angles are degrees, 0 to 360 (15 degrees an hour). UT1 is UTC unless `dut1` is given, which is within 0.9 s of the Earth's turn (0.004 degrees). Pure: no DOM, no three.js.
3
+ import { DAY_MS, J2000_JD, J2000_MS, toJulianDate, toTerrestrialTime } from "./clock.js";
4
+ import { moment } from "./when.js";
5
+ const RAD = Math.PI / 180;
6
+ const ARCSEC = Math.PI / 648000;
7
+ /** Nutation in longitude and in obliquity, and the mean obliquity of date, in radians, at `T` Julian centuries of terrestrial time from J2000 (Meeus ch. 22, accurate to 0.5 arc-seconds). */
8
+ export function nutation(T) {
9
+ const omega = (125.04452 - 1934.136261 * T) * RAD;
10
+ const sunL = (280.4665 + 36000.7698 * T) * RAD;
11
+ const moonL = (218.3165 + 481267.8813 * T) * RAD;
12
+ const longitude = (-17.2 * Math.sin(omega) - 1.32 * Math.sin(2 * sunL) - 0.23 * Math.sin(2 * moonL) + 0.21 * Math.sin(2 * omega)) * ARCSEC;
13
+ const obliquity = (9.2 * Math.cos(omega) + 0.57 * Math.cos(2 * sunL) + 0.1 * Math.cos(2 * moonL) - 0.09 * Math.cos(2 * omega)) * ARCSEC;
14
+ const meanObliquity = (84381.448 - 46.815 * T - 0.00059 * T * T + 0.001813 * T * T * T) * ARCSEC;
15
+ return { longitude, obliquity, meanObliquity };
16
+ }
17
+ const wrap = (degrees) => ((degrees % 360) + 360) % 360;
18
+ /**
19
+ * The Greenwich mean sidereal time in degrees, 0 to 360 (divide by 15 for hours).
20
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
21
+ * @param dut1 - UT1 minus UTC in seconds, from the IERS; default 0 (within 0.9 s).
22
+ * @returns Degrees.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * import { greenwichMeanSiderealTime } from "@johnmorrisdotca/wakusei-planets/time";
27
+ *
28
+ * console.log((greenwichMeanSiderealTime(Date.UTC(1987, 3, 10)) / 15).toFixed(4));
29
+ * ```
30
+ */
31
+ export function greenwichMeanSiderealTime(when, dut1 = 0) {
32
+ const jd = toJulianDate(moment(when) + dut1 * 1000);
33
+ const T = (jd - J2000_JD) / 36525;
34
+ return wrap(280.46061837 + 360.98564736629 * (jd - J2000_JD) + 0.000387933 * T * T - (T * T * T) / 38710000);
35
+ }
36
+ /**
37
+ * The equation of the equinoxes in degrees: the nutation in longitude times the cosine of the obliquity, what apparent sidereal time has over mean (about 1 second of time at most).
38
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
39
+ * @returns Degrees, between about -0.0013 and 0.0013.
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * import { equationOfTheEquinoxes } from "@johnmorrisdotca/wakusei-planets/time";
44
+ *
45
+ * console.log((equationOfTheEquinoxes(Date.UTC(1987, 3, 10)) * 240).toFixed(4));
46
+ * ```
47
+ */
48
+ export function equationOfTheEquinoxes(when) {
49
+ const T = (toTerrestrialTime(when) - J2000_MS) / (36525 * DAY_MS);
50
+ const n = nutation(T);
51
+ return (n.longitude * Math.cos(n.meanObliquity + n.obliquity)) / RAD;
52
+ }
53
+ /**
54
+ * The Greenwich apparent sidereal time in degrees, 0 to 360: the mean time with the equation of the equinoxes added, which is what the hour angle of a body is measured against.
55
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
56
+ * @param dut1 - UT1 minus UTC in seconds, from the IERS; default 0.
57
+ * @returns Degrees.
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * import { greenwichApparentSiderealTime } from "@johnmorrisdotca/wakusei-planets/time";
62
+ *
63
+ * console.log((greenwichApparentSiderealTime(Date.UTC(1987, 3, 10)) / 15).toFixed(4));
64
+ * ```
65
+ */
66
+ export function greenwichApparentSiderealTime(when, dut1 = 0) {
67
+ return wrap(greenwichMeanSiderealTime(when, dut1) + equationOfTheEquinoxes(when));
68
+ }
69
+ /**
70
+ * The local sidereal time in degrees, 0 to 360, at a longitude east of Greenwich (west negative): the right ascension on the meridian.
71
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
72
+ * @param longitude - Degrees east.
73
+ * @param options - `apparent` (default `true`: with the equation of the equinoxes) and `dut1`, UT1 minus UTC in seconds.
74
+ * @returns Degrees.
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * import { localSiderealTime } from "@johnmorrisdotca/wakusei-planets/time";
79
+ *
80
+ * console.log(localSiderealTime(Date.UTC(2026, 10, 7, 12), -123.1).toFixed(2));
81
+ * ```
82
+ */
83
+ export function localSiderealTime(when, longitude, options = {}) {
84
+ if (!Number.isFinite(longitude))
85
+ throw new RangeError(`longitude must be a finite number of degrees east, got ${longitude}`);
86
+ const greenwich = options.apparent === false ? greenwichMeanSiderealTime(when, options.dut1) : greenwichApparentSiderealTime(when, options.dut1);
87
+ return wrap(greenwich + longitude);
88
+ }
@@ -0,0 +1,46 @@
1
+ /** A place on the Earth: degrees north, degrees east (west negative) and metres above the ellipsoid. */
2
+ export interface Observer {
3
+ latitude: number;
4
+ longitude: number;
5
+ heightM?: number;
6
+ }
7
+ /** A body's place on the sky of date, as the Earth's centre sees it (apparent: light time, aberration for the Sun, nutation): angles in degrees, distance in kilometres. */
8
+ export interface Equatorial {
9
+ /** Right ascension of date, 0 to 360. */
10
+ rightAscension: number;
11
+ /** Declination of date, -90 to 90. */
12
+ declination: number;
13
+ distanceKm: number;
14
+ }
15
+ /** Where a body is in a place's sky: azimuth 0 to 360 from north through east, altitude, the hour angle (0 on the meridian, positive west) and the parallactic angle, all in degrees. */
16
+ export interface Horizontal {
17
+ azimuth: number;
18
+ altitude: number;
19
+ hourAngle: number;
20
+ parallacticAngle: number;
21
+ /** The distance from the place to the body in kilometres. */
22
+ distanceKm: number;
23
+ }
24
+ /** The Sun as seen from the Earth's centre: the apparent place of date, with the annual aberration (the Earth's speed over the speed of light, about 20 arc-seconds) and nutation. */
25
+ export declare function sunEquatorial(ms: number): Equatorial;
26
+ /** The Moon as seen from the Earth's centre: the apparent place of date, with the light time (the Moon is about 1.3 seconds away) and nutation. */
27
+ export declare function moonEquatorial(ms: number): Equatorial;
28
+ /** The apparent sidereal time at Greenwich in degrees, from the UTC instant taken as UT1. */
29
+ export declare const greenwichSiderealTime: (ms: number) => number;
30
+ /**
31
+ * Altitude, azimuth, hour angle and parallactic angle of a body for an observer: topocentric (parallax taken out of the Earth's centre to the place) and geometric, with no refraction.
32
+ * `ms` is the instant the observer's sky turns for; the body's place `equatorial` is its apparent place at that instant.
33
+ */
34
+ export declare function horizontalOf(equatorial: Equatorial, ms: number, observer: Observer): Horizontal;
35
+ /**
36
+ * The refraction of the atmosphere in degrees at a geometric altitude in degrees (Saemundsson's formula, standard air at 10 degrees C and 1010 hPa): it lifts a body toward the zenith.
37
+ * It is smooth through the horizon and fades to nothing below -2 degrees.
38
+ */
39
+ export declare function refraction(altitude: number): number;
40
+ /** The Moon's phase: the illuminated fraction 0 to 1, the phase 0 to 1 (0 new, 0.25 first quarter, 0.5 full, 0.75 last quarter), the bright limb's position angle in degrees eastward from north, and whether it is waxing. */
41
+ export declare function moonPhase(ms: number): {
42
+ fraction: number;
43
+ phase: number;
44
+ angle: number;
45
+ waxing: boolean;
46
+ };
@@ -0,0 +1,145 @@
1
+ // What the Sun and the Moon look like from a place on the Earth at a moment: apparent right ascension and declination of date, parallax, refraction, altitude and azimuth, the phase and the bright limb.
2
+ // Written from the standard methods (Meeus, "Astronomical Algorithms", chapters 12, 21, 22, 40, 48; the IAU 1976 precession; IAU 1980 nutation, its largest terms) on this package's own positions: the Sun from the Earth's VSOP87B place,
3
+ // the Moon from ELP2000-82B. Pure: no DOM, no three.js. Time is UTC in milliseconds; UT1 is taken as UTC (up to 0.9 s, 0.004 degrees of turn of the Earth); the theories run in terrestrial time.
4
+ import { earthPosition, moonGeocentric } from "../theory.js";
5
+ import { toTerrestrialTime } from "../clock.js";
6
+ import { greenwichApparentSiderealTime, nutation } from "../sidereal.js";
7
+ const RAD = Math.PI / 180;
8
+ const DEG = 180 / Math.PI;
9
+ const ARCSEC = Math.PI / 648000;
10
+ const DAY_MS = 86_400_000;
11
+ const J2000_MS = Date.UTC(2000, 0, 1, 12);
12
+ const AU_KM = 149597870.7;
13
+ const LIGHT_KM_S = 299792.458;
14
+ /** The mean obliquity of the ecliptic at J2000 in the IAU 1976 value the VSOP87 and ELP frames use (84381.448 arc-seconds). */
15
+ const OBLIQUITY_J2000 = 84381.448 * ARCSEC;
16
+ /** The Earth's equatorial radius in kilometres and its flattening (WGS 84). */
17
+ const EARTH_RADIUS_KM = 6378.137;
18
+ const EARTH_FLATTENING = 1 / 298.257223563;
19
+ const matrixTimes = (a, b) => [0, 1, 2].map((i) => [0, 1, 2].map((j) => a[i][0] * b[0][j] + a[i][1] * b[1][j] + a[i][2] * b[2][j]));
20
+ const apply = (m, v) => [m[0][0] * v[0] + m[0][1] * v[1] + m[0][2] * v[2], m[1][0] * v[0] + m[1][1] * v[1] + m[1][2] * v[2], m[2][0] * v[0] + m[2][1] * v[1] + m[2][2] * v[2]];
21
+ /** The matrix that re-expresses a vector in axes turned by `a` radians about x, y or z (a frame turn, not a vector turn). */
22
+ const frameTurn = (axis, a) => {
23
+ const c = Math.cos(a);
24
+ const s = Math.sin(a);
25
+ if (axis === 1)
26
+ return [[1, 0, 0], [0, c, s], [0, -s, c]];
27
+ if (axis === 2)
28
+ return [[c, 0, -s], [0, 1, 0], [s, 0, c]];
29
+ return [[c, s, 0], [-s, c, 0], [0, 0, 1]];
30
+ };
31
+ /** Julian centuries of terrestrial time from J2000 for a UTC instant in milliseconds. */
32
+ const centuries = (ms) => (toTerrestrialTime(ms) - J2000_MS) / (36525 * DAY_MS);
33
+ /** A J2000 mean-ecliptic vector turned into the true equator and equinox of date: obliquity of J2000, IAU 1976 precession, then nutation. */
34
+ function trueOfDate(ecliptic, T) {
35
+ const equatorial = apply(frameTurn(1, -OBLIQUITY_J2000), ecliptic);
36
+ const zeta = (2306.2181 * T + 0.30188 * T * T + 0.017998 * T * T * T) * ARCSEC;
37
+ const z = (2306.2181 * T + 1.09468 * T * T + 0.018203 * T * T * T) * ARCSEC;
38
+ const theta = (2004.3109 * T - 0.42665 * T * T - 0.041833 * T * T * T) * ARCSEC;
39
+ const precession = matrixTimes(frameTurn(3, -z), matrixTimes(frameTurn(2, theta), frameTurn(3, -zeta)));
40
+ const n = nutation(T);
41
+ const nutate = matrixTimes(frameTurn(1, -(n.meanObliquity + n.obliquity)), matrixTimes(frameTurn(3, -n.longitude), frameTurn(1, n.meanObliquity)));
42
+ return apply(nutate, apply(precession, equatorial));
43
+ }
44
+ const toEquatorial = (v, distanceKm) => ({
45
+ rightAscension: ((Math.atan2(v[1], v[0]) * DEG) % 360 + 360) % 360,
46
+ declination: Math.asin(Math.max(-1, Math.min(1, v[2] / Math.hypot(v[0], v[1], v[2])))) * DEG,
47
+ distanceKm,
48
+ });
49
+ const fromEquatorial = (e) => {
50
+ const ra = e.rightAscension * RAD;
51
+ const dec = e.declination * RAD;
52
+ return [e.distanceKm * Math.cos(dec) * Math.cos(ra), e.distanceKm * Math.cos(dec) * Math.sin(ra), e.distanceKm * Math.sin(dec)];
53
+ };
54
+ /** The Sun as seen from the Earth's centre: the apparent place of date, with the annual aberration (the Earth's speed over the speed of light, about 20 arc-seconds) and nutation. */
55
+ export function sunEquatorial(ms) {
56
+ const T = centuries(ms);
57
+ const earth = earthPosition(ms);
58
+ const later = earthPosition(ms + 60_000);
59
+ const sun = [-earth[0], -earth[1], -earth[2]];
60
+ const distanceAu = Math.hypot(sun[0], sun[1], sun[2]);
61
+ // the Earth's velocity in au per second, over the speed of light in au per second, moves the apparent direction toward where the Earth is heading
62
+ const beta = [(later[0] - earth[0]) / 60 / (LIGHT_KM_S / AU_KM), (later[1] - earth[1]) / 60 / (LIGHT_KM_S / AU_KM), (later[2] - earth[2]) / 60 / (LIGHT_KM_S / AU_KM)];
63
+ const aberrated = [sun[0] / distanceAu + beta[0], sun[1] / distanceAu + beta[1], sun[2] / distanceAu + beta[2]];
64
+ return toEquatorial(trueOfDate(aberrated, T), distanceAu * AU_KM);
65
+ }
66
+ /** The Moon as seen from the Earth's centre: the apparent place of date, with the light time (the Moon is about 1.3 seconds away) and nutation. */
67
+ export function moonEquatorial(ms) {
68
+ const T = centuries(ms);
69
+ const first = moonGeocentric(ms);
70
+ const distance = Math.hypot(first[0], first[1], first[2]);
71
+ const geometric = moonGeocentric(ms - (distance / LIGHT_KM_S) * 1000);
72
+ return toEquatorial(trueOfDate(geometric, T), Math.hypot(geometric[0], geometric[1], geometric[2]));
73
+ }
74
+ /** The apparent sidereal time at Greenwich in degrees, from the UTC instant taken as UT1. */
75
+ export const greenwichSiderealTime = (ms) => greenwichApparentSiderealTime(ms);
76
+ /** The observer's place from the Earth's centre in the true equator of date, in kilometres, with the turn of the Earth (Meeus ch. 11). */
77
+ function observerVector(observer, ms) {
78
+ const lat = observer.latitude * RAD;
79
+ const height = (observer.heightM ?? 0) / 1000;
80
+ const u = Math.atan((1 - EARTH_FLATTENING) * Math.tan(lat));
81
+ const rhoSin = (1 - EARTH_FLATTENING) * Math.sin(u) + (height / EARTH_RADIUS_KM) * Math.sin(lat);
82
+ const rhoCos = Math.cos(u) + (height / EARTH_RADIUS_KM) * Math.cos(lat);
83
+ const theta = (greenwichSiderealTime(ms) + observer.longitude) * RAD;
84
+ return [EARTH_RADIUS_KM * rhoCos * Math.cos(theta), EARTH_RADIUS_KM * rhoCos * Math.sin(theta), EARTH_RADIUS_KM * rhoSin];
85
+ }
86
+ /**
87
+ * Altitude, azimuth, hour angle and parallactic angle of a body for an observer: topocentric (parallax taken out of the Earth's centre to the place) and geometric, with no refraction.
88
+ * `ms` is the instant the observer's sky turns for; the body's place `equatorial` is its apparent place at that instant.
89
+ */
90
+ export function horizontalOf(equatorial, ms, observer) {
91
+ const body = fromEquatorial(equatorial);
92
+ const place = observerVector(observer, ms);
93
+ const topo = [body[0] - place[0], body[1] - place[1], body[2] - place[2]];
94
+ const distanceKm = Math.hypot(topo[0], topo[1], topo[2]);
95
+ const ra = Math.atan2(topo[1], topo[0]);
96
+ const dec = Math.asin(topo[2] / distanceKm);
97
+ const lat = observer.latitude * RAD;
98
+ const H = (greenwichSiderealTime(ms) + observer.longitude) * RAD - ra;
99
+ const sinAlt = Math.sin(lat) * Math.sin(dec) + Math.cos(lat) * Math.cos(dec) * Math.cos(H);
100
+ const altitude = Math.asin(Math.max(-1, Math.min(1, sinAlt)));
101
+ const azimuth = Math.atan2(-Math.cos(dec) * Math.sin(H), Math.sin(dec) * Math.cos(lat) - Math.cos(dec) * Math.sin(lat) * Math.cos(H));
102
+ const parallactic = Math.atan2(Math.sin(H), Math.tan(lat) * Math.cos(dec) - Math.sin(dec) * Math.cos(H));
103
+ return {
104
+ azimuth: (((azimuth * DEG) % 360) + 360) % 360,
105
+ altitude: altitude * DEG,
106
+ hourAngle: ((((H * DEG) % 360) + 540) % 360) - 180,
107
+ parallacticAngle: parallactic * DEG,
108
+ distanceKm,
109
+ };
110
+ }
111
+ /**
112
+ * The refraction of the atmosphere in degrees at a geometric altitude in degrees (Saemundsson's formula, standard air at 10 degrees C and 1010 hPa): it lifts a body toward the zenith.
113
+ * It is smooth through the horizon and fades to nothing below -2 degrees.
114
+ */
115
+ export function refraction(altitude) {
116
+ if (altitude < -2)
117
+ return 0;
118
+ const h = Math.max(altitude, -1);
119
+ const r = 1.02 / Math.tan((h + 10.3 / (h + 5.11)) * RAD) / 60;
120
+ return altitude < -1 ? r * (altitude + 2) : r;
121
+ }
122
+ /** The Moon's phase: the illuminated fraction 0 to 1, the phase 0 to 1 (0 new, 0.25 first quarter, 0.5 full, 0.75 last quarter), the bright limb's position angle in degrees eastward from north, and whether it is waxing. */
123
+ export function moonPhase(ms) {
124
+ const sun = sunEquatorial(ms);
125
+ const moon = moonEquatorial(ms);
126
+ const sunV = fromEquatorial(sun);
127
+ const moonV = fromEquatorial(moon);
128
+ const psi = Math.acos(Math.max(-1, Math.min(1, (sunV[0] * moonV[0] + sunV[1] * moonV[1] + sunV[2] * moonV[2]) / (sun.distanceKm * moon.distanceKm))));
129
+ // the angle at the Moon between the Sun and the Earth (Meeus ch. 48)
130
+ const inc = Math.atan2(sun.distanceKm * Math.sin(psi), moon.distanceKm - sun.distanceKm * Math.cos(psi));
131
+ const fraction = (1 + Math.cos(inc)) / 2;
132
+ const a0 = sun.rightAscension * RAD;
133
+ const d0 = sun.declination * RAD;
134
+ const a = moon.rightAscension * RAD;
135
+ const d = moon.declination * RAD;
136
+ const angle = Math.atan2(Math.cos(d0) * Math.sin(a0 - a), Math.sin(d0) * Math.cos(d) - Math.cos(d0) * Math.sin(d) * Math.cos(a0 - a)) * DEG;
137
+ // waxing while the Moon is east of the Sun along the ecliptic: its longitude is ahead by under half a turn
138
+ const n = nutation(centuries(ms));
139
+ const epsilon = n.meanObliquity + n.obliquity;
140
+ const longitudeOf = (v) => Math.atan2(v[1] * Math.cos(epsilon) + v[2] * Math.sin(epsilon), v[0]) * DEG;
141
+ const ahead = (((longitudeOf(moonV) - longitudeOf(sunV)) % 360) + 360) % 360;
142
+ const waxing = ahead < 180;
143
+ const phase = waxing ? 0.5 - inc / (2 * Math.PI) : 0.5 + inc / (2 * Math.PI);
144
+ return { fraction, phase: phase >= 1 ? phase - 1 : phase, angle: ((angle % 360) + 360) % 360, waxing };
145
+ }
@@ -0,0 +1,37 @@
1
+ import { type Observer } from "./engine.ts";
2
+ /** A date or milliseconds since 1970 as milliseconds, throwing on anything that is not a moment. */
3
+ export declare function moment(date: Date | number, name?: string): number;
4
+ export declare function place(latitude: number, longitude: number, heightM?: number): Observer;
5
+ /** The window of a day: the local calendar day when a UTC offset is given, otherwise the solar day from one solar midnight to the next that holds the moment. Milliseconds, the end not included. */
6
+ export declare function dayWindow(ms: number, observer: Observer, utcOffset: number | undefined): [number, number];
7
+ /** The Sun's hour angle in degrees from -180 to 180 for an observer (positive west, 0 on the meridian). */
8
+ export declare function sunHourAngle(ms: number, observer: Observer): number;
9
+ /** The instant nearest to `ms` when the Sun's hour angle is `target` degrees (0 noon, 180 midnight), by Newton steps on the Sun's turn of about 360 degrees a day. */
10
+ export declare function nearestHourAngle(ms: number, observer: Observer, target: number): number;
11
+ /** The zero crossings of `f` in [start, end), as [instant, true when f goes up], found by sampling every `step` milliseconds and bisecting each bracket to about a tenth of a second. */
12
+ export declare function crossings(f: (ms: number) => number, start: number, end: number, step: number): Array<[number, boolean]>;
13
+ /** The height of the horizon below level for an observer `height` metres up, in degrees (the dip, with the refraction along it). */
14
+ export declare const dip: (heightM: number) => number;
15
+ /** The Sun's times of a day: when it crosses each row's altitude going up and going down, with solar noon and the solar midnight that leads into it. */
16
+ export interface SunDay {
17
+ noon: number;
18
+ nadir: number;
19
+ /** For each row `[angle, morningName, eveningName]`: the instants, or null for one that does not happen in the window. */
20
+ events: Map<string, number | null>;
21
+ alwaysUp: boolean;
22
+ alwaysDown: boolean;
23
+ }
24
+ /**
25
+ * The Sun's day at a place: `utcOffset` in minutes east of UTC picks the local calendar day holding `ms`, and `undefined` the solar day (one solar midnight to the next).
26
+ * A row at the horizon (between -1 and 0 degrees) is lowered by the dip of the horizon for an observer `heightM` metres up.
27
+ */
28
+ export declare function sunDay(ms: number, observer: Observer, utcOffset: number | undefined, rows: ReadonlyArray<readonly [number, string, string]>): SunDay;
29
+ /** The Moon between two instants at a place: rise and set (the upper limb at the horizon through 34 arc-minutes of refraction), the upper and lower meridian crossings, and whether it stays up or down. */
30
+ export declare function moonDay(start: number, end: number, observer: Observer): {
31
+ rise: number | null;
32
+ set: number | null;
33
+ transit: number | null;
34
+ lowerTransit: number | null;
35
+ alwaysUp: boolean;
36
+ alwaysDown: boolean;
37
+ };
@@ -0,0 +1,133 @@
1
+ // The searches behind the sun and moon times: the day a time belongs to, the instants when a body crosses an altitude or the meridian, found by sampling and bisection on this package's own positions.
2
+ // Shared by `suncalc` and `suncalc/legacy`. Pure: no DOM, no three.js.
3
+ import { horizontalOf, moonEquatorial, sunEquatorial } from "./engine.js";
4
+ const DAY_MS = 86_400_000;
5
+ const MOON_RADIUS_KM = 1737.4;
6
+ const SAMPLE_MS = 6 * 60_000;
7
+ /** A date or milliseconds since 1970 as milliseconds, throwing on anything that is not a moment. */
8
+ export function moment(date, name = "date") {
9
+ const ms = typeof date === "number" ? date : date.getTime();
10
+ if (!Number.isFinite(ms))
11
+ throw new RangeError(`${name} must be a valid Date or a finite number of milliseconds, got ${String(date)}`);
12
+ return ms;
13
+ }
14
+ export function place(latitude, longitude, heightM = 0) {
15
+ if (!Number.isFinite(latitude) || latitude < -90 || latitude > 90)
16
+ throw new RangeError(`lat must be between -90 and 90 degrees, got ${latitude}`);
17
+ if (!Number.isFinite(longitude))
18
+ throw new RangeError(`lng must be a finite number of degrees, got ${longitude}`);
19
+ if (!Number.isFinite(heightM))
20
+ throw new RangeError(`height must be a finite number of metres, got ${heightM}`);
21
+ return { latitude, longitude, heightM };
22
+ }
23
+ /** The window of a day: the local calendar day when a UTC offset is given, otherwise the solar day from one solar midnight to the next that holds the moment. Milliseconds, the end not included. */
24
+ export function dayWindow(ms, observer, utcOffset) {
25
+ if (utcOffset !== undefined) {
26
+ if (!Number.isFinite(utcOffset))
27
+ throw new RangeError(`utcOffset must be a finite number of minutes east of UTC, got ${utcOffset}`);
28
+ const start = Math.floor((ms + utcOffset * 60_000) / DAY_MS) * DAY_MS - utcOffset * 60_000;
29
+ return [start, start + DAY_MS];
30
+ }
31
+ const nadir = nearestHourAngle(ms, observer, 180);
32
+ const previous = nadir <= ms ? nadir : nearestHourAngle(nadir - DAY_MS, observer, 180);
33
+ const next = nearestHourAngle(previous + DAY_MS, observer, 180);
34
+ return [previous, next];
35
+ }
36
+ /** The Sun's hour angle in degrees from -180 to 180 for an observer (positive west, 0 on the meridian). */
37
+ export function sunHourAngle(ms, observer) {
38
+ return horizontalOf(sunEquatorial(ms), ms, observer).hourAngle;
39
+ }
40
+ /** The instant nearest to `ms` when the Sun's hour angle is `target` degrees (0 noon, 180 midnight), by Newton steps on the Sun's turn of about 360 degrees a day. */
41
+ export function nearestHourAngle(ms, observer, target) {
42
+ let t = ms;
43
+ for (let i = 0; i < 8; i += 1) {
44
+ const difference = ((((sunHourAngle(t, observer) - target) % 360) + 540) % 360) - 180;
45
+ t -= (difference / 360) * DAY_MS;
46
+ if (Math.abs(difference) < 1e-6)
47
+ break;
48
+ }
49
+ return t;
50
+ }
51
+ /** The zero crossings of `f` in [start, end), as [instant, true when f goes up], found by sampling every `step` milliseconds and bisecting each bracket to about a tenth of a second. */
52
+ export function crossings(f, start, end, step) {
53
+ const found = [];
54
+ let t0 = start;
55
+ let f0 = f(t0);
56
+ while (t0 < end) {
57
+ const t1 = Math.min(t0 + step, end);
58
+ const f1 = f(t1);
59
+ if ((f0 < 0 && f1 >= 0) || (f0 >= 0 && f1 < 0)) {
60
+ let lo = t0;
61
+ let hi = t1;
62
+ let flo = f0;
63
+ for (let i = 0; i < 26; i += 1) {
64
+ const mid = (lo + hi) / 2;
65
+ const fm = f(mid);
66
+ if ((fm < 0) === (flo < 0)) {
67
+ lo = mid;
68
+ flo = fm;
69
+ }
70
+ else
71
+ hi = mid;
72
+ }
73
+ const at = (lo + hi) / 2;
74
+ if (at < end)
75
+ found.push([at, f0 < 0]);
76
+ }
77
+ t0 = t1;
78
+ f0 = f1;
79
+ }
80
+ return found;
81
+ }
82
+ /** The height of the horizon below level for an observer `height` metres up, in degrees (the dip, with the refraction along it). */
83
+ export const dip = (heightM) => 0.0347 * Math.sqrt(Math.max(0, heightM));
84
+ /**
85
+ * The Sun's day at a place: `utcOffset` in minutes east of UTC picks the local calendar day holding `ms`, and `undefined` the solar day (one solar midnight to the next).
86
+ * A row at the horizon (between -1 and 0 degrees) is lowered by the dip of the horizon for an observer `heightM` metres up.
87
+ */
88
+ export function sunDay(ms, observer, utcOffset, rows) {
89
+ const [start, end] = dayWindow(ms, observer, utcOffset);
90
+ const altitude = (t) => horizontalOf(sunEquatorial(t), t, observer).altitude;
91
+ const solarNoon = nearestHourAngle(start + 12 * 3_600_000, observer, 0);
92
+ const noon = solarNoon < start - 1000 ? nearestHourAngle(solarNoon + DAY_MS, observer, 0) : solarNoon;
93
+ // the solar midnight that leads into this day's solar noon, as suncalc does: it can fall a few minutes before a calendar day begins
94
+ const nadir = nearestHourAngle(noon - DAY_MS / 2, observer, 180);
95
+ const cache = new Map();
96
+ const sampled = (t) => {
97
+ let v = cache.get(t);
98
+ if (v === undefined) {
99
+ v = altitude(t);
100
+ cache.set(t, v);
101
+ }
102
+ return v;
103
+ };
104
+ const events = new Map();
105
+ const height = observer.heightM ?? 0;
106
+ for (const [angle, morning, evening] of rows) {
107
+ const level = angle - (angle > -1 && angle < 0 ? dip(height) : 0);
108
+ const found = crossings((t) => sampled(t) - level, start, end, 10 * 60_000);
109
+ events.set(morning, found.find(([, rising]) => rising)?.[0] ?? null);
110
+ events.set(evening, found.find(([, rising]) => !rising)?.[0] ?? null);
111
+ }
112
+ const sunrise = events.get("sunrise");
113
+ const sunset = events.get("sunset");
114
+ const none = sunrise === null && sunset === null;
115
+ const high = none && altitude(noon) > -0.833 - dip(height);
116
+ return { noon, nadir, events, alwaysUp: high, alwaysDown: none && !high };
117
+ }
118
+ /** The Moon between two instants at a place: rise and set (the upper limb at the horizon through 34 arc-minutes of refraction), the upper and lower meridian crossings, and whether it stays up or down. */
119
+ export function moonDay(start, end, observer) {
120
+ // the centre's geometric altitude plus the refraction and the radius of the disc is zero at the rise and the set
121
+ const above = (t) => {
122
+ const h = horizontalOf(moonEquatorial(t), t, observer);
123
+ return h.altitude + 34 / 60 + (Math.asin(MOON_RADIUS_KM / h.distanceKm) * 180) / Math.PI;
124
+ };
125
+ const meridian = (t) => Math.sin((horizontalOf(moonEquatorial(t), t, observer).hourAngle * Math.PI) / 180);
126
+ const edges = crossings(above, start, end, SAMPLE_MS);
127
+ const turns = crossings(meridian, start, end, SAMPLE_MS);
128
+ const rise = edges.find(([, rising]) => rising)?.[0] ?? null;
129
+ const set = edges.find(([, rising]) => !rising)?.[0] ?? null;
130
+ const none = rise === null && set === null;
131
+ const up = none && above(start) > 0;
132
+ return { rise, set, transit: turns.find(([, rising]) => rising)?.[0] ?? null, lowerTransit: turns.find(([, rising]) => !rising)?.[0] ?? null, alwaysUp: up, alwaysDown: none && !up };
133
+ }