@johnmorrisdotca/wakusei-planets 0.63.0 → 0.64.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.
- package/CHANGELOG.md +27 -0
- package/README.md +4 -3
- package/dist/asteroids.d.ts +15 -1
- package/dist/asteroids.js +28 -0
- package/dist/door/made.d.ts +4 -0
- package/dist/door/made.js +10 -0
- package/dist/door/own.d.ts +1 -4
- package/dist/door/own.js +1 -9
- package/dist/element.js +19 -22
- package/dist/scene/tumble.d.ts +13 -0
- package/dist/scene/tumble.js +34 -0
- package/dist/sky.d.ts +561 -0
- package/dist/sky.js +522 -0
- package/dist/space/asteroid-ids.d.ts +8 -0
- package/dist/space/asteroid-ids.js +19 -0
- package/dist/space/element.d.ts +63 -6
- package/dist/space/element.js +208 -26
- package/dist/space/scene.d.ts +32 -0
- package/dist/space/scene.js +36 -2
- package/dist/surface/element.d.ts +1 -1
- package/dist/surface/element.js +4 -2
- package/package.json +10 -1
package/dist/sky.js
ADDED
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
// Sky events (seasons, Moon phases, eclipses, rise/set/transit, calendar-day rise and set, alignments, peak magnitude, local solar eclipses, libration, lunar apsides and nodes, greatest elongation) through astronomy-engine, an OPTIONAL peer dependency: `pnpm add astronomy-engine` (2.1.19 or newer, below 3) beside this package.
|
|
2
|
+
// This is the only file under src/ that imports it, so the core, /suncalc and /sky-bodies never reach it (test/sky-events.test.ts holds that). A page that never imports `/sky` never needs the peer.
|
|
3
|
+
// A missing peer cannot be caught here: a static import fails before any code runs, with the runtime's own "cannot find package astronomy-engine", which names it. (A top-level await would let this file word its own error, but
|
|
4
|
+
// the package's size measure and any ES2020 bundler refuse top-level await; see the CHANGELOG, "default, for John to confirm".) Everything is experimental during 0.x. Pure: no DOM, no clock read, no three.js.
|
|
5
|
+
import { ApsisKind, Body, Equator, GeoMoon, GeoVector, Horizon, Ecliptic, KM_PER_AU, Libration, MakeTime, NextGlobalSolarEclipse, NextLunarApsis, NextLunarEclipse, NextMoonNode, NextMoonQuarter, NodeEventKind, Observer as EngineObserver, Search, SearchGlobalSolarEclipse, SearchHourAngle, SearchLocalSolarEclipse, SearchLunarApsis, SearchLunarEclipse, SearchMaxElongation, SearchMoonNode, SearchMoonPhase, SearchMoonQuarter, SearchPeakMagnitude, SearchRiseSet, Seasons } from "astronomy-engine";
|
|
6
|
+
import { moment } from "./when.js";
|
|
7
|
+
const DAY_MS = 86_400_000;
|
|
8
|
+
/** The two ends of a search interval as milliseconds, the start included and the end not; an end before the start throws a `RangeError` naming it. */
|
|
9
|
+
function interval(from, to) {
|
|
10
|
+
const start = moment(from, "from");
|
|
11
|
+
const end = moment(to, "to");
|
|
12
|
+
if (end < start)
|
|
13
|
+
throw new RangeError(`to must not be before from, got from ${new Date(start).toISOString()} and to ${new Date(end).toISOString()}`);
|
|
14
|
+
return [start, end];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The equinoxes and solstices of a calendar year (UTC), found by astronomy-engine. The year is an integer; anything else throws a `RangeError` naming it. The times are good to about a minute against the USNO table
|
|
18
|
+
* for 1900 to 2050 (the accuracy guide has the measured figures); the moments are UT, as a `Date` counts them.
|
|
19
|
+
*
|
|
20
|
+
* @param year - A calendar year, an integer (UTC).
|
|
21
|
+
* @returns The four moments, in order.
|
|
22
|
+
* @experimental
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { seasons } from "@johnmorrisdotca/wakusei-planets/sky";
|
|
26
|
+
*
|
|
27
|
+
* const { marchEquinox, decemberSolstice } = seasons(2026);
|
|
28
|
+
* console.log(marchEquinox.toISOString(), decemberSolstice.toISOString());
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export function seasons(year) {
|
|
32
|
+
if (!Number.isInteger(year))
|
|
33
|
+
throw new RangeError(`year must be an integer, got ${String(year)}`);
|
|
34
|
+
const found = Seasons(year);
|
|
35
|
+
return { marchEquinox: found.mar_equinox.date, juneSolstice: found.jun_solstice.date, septemberEquinox: found.sep_equinox.date, decemberSolstice: found.dec_solstice.date };
|
|
36
|
+
}
|
|
37
|
+
const PHASES = ["new", "firstQuarter", "full", "lastQuarter"];
|
|
38
|
+
/**
|
|
39
|
+
* Every new, first quarter, full and last quarter Moon from `from` (included) to `to` (not included), in order, as `Date`s. `from` and `to` are a `Date` or milliseconds; an end before the start throws a `RangeError`.
|
|
40
|
+
*
|
|
41
|
+
* @param from - The start of the interval, included.
|
|
42
|
+
* @param to - The end of the interval, not included.
|
|
43
|
+
* @returns The phases in time order; empty when the interval holds none.
|
|
44
|
+
* @experimental
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* import { moonPhases } from "@johnmorrisdotca/wakusei-planets/sky";
|
|
48
|
+
*
|
|
49
|
+
* const november = moonPhases(Date.UTC(2026, 10, 1), Date.UTC(2026, 11, 1));
|
|
50
|
+
* console.log(november.map((event) => `${event.phase} ${event.time.toISOString().slice(0, 10)}`).join(", "));
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export function moonPhases(from, to) {
|
|
54
|
+
const [start, end] = interval(from, to);
|
|
55
|
+
const found = [];
|
|
56
|
+
for (let quarter = SearchMoonQuarter(new Date(start)); quarter.time.date.getTime() < end; quarter = NextMoonQuarter(quarter)) {
|
|
57
|
+
found.push({ phase: PHASES[quarter.quarter], time: quarter.time.date });
|
|
58
|
+
}
|
|
59
|
+
return found;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The next time the Moon is in `phase` at or after `from`, as a `Date`, or `null` when it does not happen within `within.days` (35 days when not given, which always holds one of each phase).
|
|
63
|
+
*
|
|
64
|
+
* @param from - Where to start looking, included.
|
|
65
|
+
* @param options - `phase` is required; `within.days` bounds the search (a positive number).
|
|
66
|
+
* @returns The moment, or `null` when none falls inside the window.
|
|
67
|
+
* @experimental
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* import { nextMoonPhase } from "@johnmorrisdotca/wakusei-planets/sky";
|
|
71
|
+
*
|
|
72
|
+
* const full = nextMoonPhase(Date.UTC(2026, 10, 7), { phase: "full" });
|
|
73
|
+
* console.log(full?.toISOString());
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export function nextMoonPhase(from, options) {
|
|
77
|
+
const start = moment(from, "from");
|
|
78
|
+
const index = PHASES.indexOf(options.phase);
|
|
79
|
+
if (index < 0)
|
|
80
|
+
throw new RangeError(`phase must be one of ${PHASES.join(", ")}, got ${String(options.phase)}`);
|
|
81
|
+
const days = options.within?.days ?? 35;
|
|
82
|
+
if (!(days > 0) || !Number.isFinite(days))
|
|
83
|
+
throw new RangeError(`within.days must be a positive number, got ${String(days)}`);
|
|
84
|
+
const found = SearchMoonPhase(index * 90, new Date(start), days);
|
|
85
|
+
return found === null || found.date.getTime() >= start + days * DAY_MS ? null : found.date;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Every solar and lunar eclipse whose greatest moment falls from `from` (included) to `to` (not included), in time order. A solar eclipse is listed if it is seen anywhere on Earth. An end before the start throws a `RangeError`.
|
|
89
|
+
*
|
|
90
|
+
* @param from - The start of the interval, included.
|
|
91
|
+
* @param to - The end of the interval, not included.
|
|
92
|
+
* @returns The eclipses in time order; empty when the interval holds none.
|
|
93
|
+
* @experimental
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* import { findEclipses } from "@johnmorrisdotca/wakusei-planets/sky";
|
|
97
|
+
*
|
|
98
|
+
* for (const eclipse of findEclipses(Date.UTC(2026, 0, 1), Date.UTC(2027, 0, 1))) console.log(eclipse.type, eclipse.kind, eclipse.peak.toISOString().slice(0, 10));
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export function findEclipses(from, to) {
|
|
102
|
+
const [start, end] = interval(from, to);
|
|
103
|
+
const found = [];
|
|
104
|
+
// astronomy-engine searches from the full or new Moon after its start, which can fall just before an eclipse that peaks just after it: start a day early and keep what peaks inside the interval.
|
|
105
|
+
for (let lunar = SearchLunarEclipse(new Date(start - DAY_MS)); lunar.peak.date.getTime() < end; lunar = NextLunarEclipse(lunar.peak)) {
|
|
106
|
+
if (lunar.peak.date.getTime() < start)
|
|
107
|
+
continue;
|
|
108
|
+
found.push({ type: "lunar", kind: lunar.kind, peak: lunar.peak.date, magnitude: umbralMagnitude(lunar.peak) });
|
|
109
|
+
}
|
|
110
|
+
for (let solar = SearchGlobalSolarEclipse(new Date(start - DAY_MS)); solar.peak.date.getTime() < end; solar = NextGlobalSolarEclipse(solar.peak)) {
|
|
111
|
+
if (solar.peak.date.getTime() < start)
|
|
112
|
+
continue;
|
|
113
|
+
found.push({ type: "solar", kind: solar.kind, peak: solar.peak.date, ...(solar.obscuration === undefined ? {} : { obscuration: solar.obscuration }) });
|
|
114
|
+
}
|
|
115
|
+
return found.sort((a, b) => a.peak.getTime() - b.peak.getTime());
|
|
116
|
+
}
|
|
117
|
+
// The engine's own shadow geometry (astronomy-engine does not export it): the radii it uses for the Sun, the Moon and the Earth's shadow-making disc (6371 km plus 88 km of atmosphere).
|
|
118
|
+
const SUN_RADIUS_KM = 695_700;
|
|
119
|
+
const MOON_MEAN_RADIUS_KM = 1737.4;
|
|
120
|
+
const EARTH_ECLIPSE_RADIUS_KM = 6371 + 88;
|
|
121
|
+
/** The umbral magnitude of a lunar eclipse at its greatest moment: how far into the Earth's umbra the Moon's far edge reaches, in Moon diameters. The umbra's radius at the Moon is `k`, the Moon's centre is `r` from the shadow axis. */
|
|
122
|
+
function umbralMagnitude(peak) {
|
|
123
|
+
const sun = GeoVector(Body.Sun, peak, true);
|
|
124
|
+
const moon = GeoMoon(peak);
|
|
125
|
+
const axis = { x: -sun.x, y: -sun.y, z: -sun.z };
|
|
126
|
+
const u = (axis.x * moon.x + axis.y * moon.y + axis.z * moon.z) / (axis.x ** 2 + axis.y ** 2 + axis.z ** 2);
|
|
127
|
+
const r = KM_PER_AU * Math.hypot(u * axis.x - moon.x, u * axis.y - moon.y, u * axis.z - moon.z);
|
|
128
|
+
const k = SUN_RADIUS_KM - (1 + u) * (SUN_RADIUS_KM - EARTH_ECLIPSE_RADIUS_KM);
|
|
129
|
+
return (k + MOON_MEAN_RADIUS_KM - r) / (2 * MOON_MEAN_RADIUS_KM);
|
|
130
|
+
}
|
|
131
|
+
const BODIES = { sun: Body.Sun, moon: Body.Moon, mercury: Body.Mercury, venus: Body.Venus, mars: Body.Mars, jupiter: Body.Jupiter, saturn: Body.Saturn, uranus: Body.Uranus, neptune: Body.Neptune, pluto: Body.Pluto };
|
|
132
|
+
/** The engine's body for an id of this package, throwing a `RangeError` that names the ids there are. */
|
|
133
|
+
function bodyOf(id) {
|
|
134
|
+
const body = Object.hasOwn(BODIES, id) ? BODIES[id] : undefined;
|
|
135
|
+
if (body === undefined)
|
|
136
|
+
throw new RangeError(`body must be one of ${Object.keys(BODIES).join(", ")}, got ${String(id)}`);
|
|
137
|
+
return body;
|
|
138
|
+
}
|
|
139
|
+
/** The engine's observer for `{ latitude, longitude, heightM }` (degrees north, degrees east, metres above the ellipsoid, 0 when not given), throwing a `RangeError` with the value for anything out of range. */
|
|
140
|
+
function observerOf(observer) {
|
|
141
|
+
if (typeof observer !== "object" || observer === null)
|
|
142
|
+
throw new TypeError("observer must be { latitude, longitude, heightM }");
|
|
143
|
+
const { latitude, longitude, heightM = 0 } = observer;
|
|
144
|
+
if (!Number.isFinite(latitude) || latitude < -90 || latitude > 90)
|
|
145
|
+
throw new RangeError(`latitude must be between -90 and 90 degrees, got ${String(latitude)}`);
|
|
146
|
+
if (!Number.isFinite(longitude))
|
|
147
|
+
throw new RangeError(`longitude must be a finite number of degrees, got ${String(longitude)}`);
|
|
148
|
+
if (!Number.isFinite(heightM))
|
|
149
|
+
throw new RangeError(`heightM must be a finite number of metres, got ${String(heightM)}`);
|
|
150
|
+
return new EngineObserver(latitude, longitude, heightM);
|
|
151
|
+
}
|
|
152
|
+
/** The search window of `options.within` in days (2 when not given), throwing a `RangeError` for anything but a positive number. */
|
|
153
|
+
function windowDays(within) {
|
|
154
|
+
const days = within?.days ?? 2;
|
|
155
|
+
if (!(days > 0) || !Number.isFinite(days))
|
|
156
|
+
throw new RangeError(`within.days must be a positive number, got ${String(days)}`);
|
|
157
|
+
return days;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* The next time a body rises for an observer, at or after `from`, as a `Date`, or `null` when it does not rise within `within.days` (2 when not given; at a pole, or for a body that stays up or down, there is none).
|
|
161
|
+
* The Sun and the Moon rise when their upper limb touches a horizon lowered by 34 arc-minutes of refraction, the planets when their centre does; the height of the observer lowers it further.
|
|
162
|
+
*
|
|
163
|
+
* @param body - A body of this package: the Sun, the Moon or a planet (`"mars"`).
|
|
164
|
+
* @param from - Where to start looking, included (an event within a quarter of a second before it counts as at it: the search settles to about 0.1 s).
|
|
165
|
+
* @param options - The observer, and how far to look.
|
|
166
|
+
* @returns The moment, or `null` when there is none inside the window.
|
|
167
|
+
* @experimental
|
|
168
|
+
* @example
|
|
169
|
+
* ```ts
|
|
170
|
+
* import { nextRise } from "@johnmorrisdotca/wakusei-planets/sky";
|
|
171
|
+
*
|
|
172
|
+
* const tokyo = { latitude: 35.6895, longitude: 139.6917, heightM: 0 };
|
|
173
|
+
* console.log(nextRise("mars", Date.UTC(2026, 10, 8), { observer: tokyo })?.toISOString());
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
export function nextRise(body, from, options) {
|
|
177
|
+
return riseOrSet(body, from, options, 1);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* The next time a body sets for an observer, at or after `from`, as a `Date`, or `null` when it does not set within `within.days` (2 when not given). The horizon is the same as for `nextRise`.
|
|
181
|
+
*
|
|
182
|
+
* @param body - A body of this package: the Sun, the Moon or a planet.
|
|
183
|
+
* @param from - Where to start looking, included.
|
|
184
|
+
* @param options - The observer, and how far to look.
|
|
185
|
+
* @returns The moment, or `null` when there is none inside the window.
|
|
186
|
+
* @experimental
|
|
187
|
+
* @example
|
|
188
|
+
* ```ts
|
|
189
|
+
* import { nextSet } from "@johnmorrisdotca/wakusei-planets/sky";
|
|
190
|
+
*
|
|
191
|
+
* const tokyo = { latitude: 35.6895, longitude: 139.6917, heightM: 0 };
|
|
192
|
+
* console.log(nextSet("venus", Date.UTC(2026, 10, 8), { observer: tokyo })?.toISOString());
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
export function nextSet(body, from, options) {
|
|
196
|
+
return riseOrSet(body, from, options, -1);
|
|
197
|
+
}
|
|
198
|
+
function riseOrSet(body, from, options, direction) {
|
|
199
|
+
const start = moment(from, "from");
|
|
200
|
+
const engineBody = bodyOf(body);
|
|
201
|
+
const observer = observerOf(options.observer);
|
|
202
|
+
const days = windowDays(options.within);
|
|
203
|
+
const found = atOrAfter(start, (at) => SearchRiseSet(engineBody, observer, direction, at, days + 1 / 86_400)?.date ?? null);
|
|
204
|
+
return found !== null && found.getTime() < start + days * DAY_MS ? found : null;
|
|
205
|
+
}
|
|
206
|
+
/** The engine's searches settle to within about 0.1 second (measured: a rise of Mars comes out 37 and 91 ms from the same event for two starts), so an event this close before `start` counts as at `start`. */
|
|
207
|
+
const SEARCH_SLACK_MS = 250;
|
|
208
|
+
/** The first moment of a search at or after `start`: the engine finds events after its start, so look from a second earlier and, if that finds one before `start` by more than the search's slack, look again from `start`. */
|
|
209
|
+
function atOrAfter(start, find) {
|
|
210
|
+
const early = find(new Date(start - 1000));
|
|
211
|
+
return early !== null && early.getTime() < start - SEARCH_SLACK_MS ? find(new Date(start)) : early;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* The next time a body crosses the observer's meridian (its highest point in the sky, hour angle 0), at or after `from`, as a `Date`, or `null` when it does not within `within.days` (2 when not given).
|
|
215
|
+
*
|
|
216
|
+
* @param body - A body of this package: the Sun, the Moon or a planet.
|
|
217
|
+
* @param from - Where to start looking, included.
|
|
218
|
+
* @param options - The observer, and how far to look.
|
|
219
|
+
* @returns The moment, or `null` when there is none inside the window.
|
|
220
|
+
* @experimental
|
|
221
|
+
* @example
|
|
222
|
+
* ```ts
|
|
223
|
+
* import { nextTransit } from "@johnmorrisdotca/wakusei-planets/sky";
|
|
224
|
+
*
|
|
225
|
+
* const tokyo = { latitude: 35.6895, longitude: 139.6917, heightM: 0 };
|
|
226
|
+
* console.log(nextTransit("jupiter", Date.UTC(2026, 10, 8), { observer: tokyo })?.toISOString());
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
export function nextTransit(body, from, options) {
|
|
230
|
+
const start = moment(from, "from");
|
|
231
|
+
const engineBody = bodyOf(body);
|
|
232
|
+
const observer = observerOf(options.observer);
|
|
233
|
+
const days = windowDays(options.within);
|
|
234
|
+
const found = atOrAfter(start, (at) => SearchHourAngle(engineBody, observer, 0, at).time.date);
|
|
235
|
+
return found.getTime() < start + days * DAY_MS ? found : null;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* The Moon's libration at a moment, as seen from the centre of the Earth.
|
|
239
|
+
*
|
|
240
|
+
* @param when - A `Date` or milliseconds since 1970 (UTC).
|
|
241
|
+
* @returns The libration in longitude and latitude, the Moon's distance in kilometres and its angular diameter in degrees.
|
|
242
|
+
* @experimental
|
|
243
|
+
* @example
|
|
244
|
+
* ```ts
|
|
245
|
+
* import { moonLibration } from "@johnmorrisdotca/wakusei-planets/sky";
|
|
246
|
+
*
|
|
247
|
+
* const { longitude, latitude } = moonLibration(Date.UTC(2026, 10, 7, 12));
|
|
248
|
+
* console.log(longitude.toFixed(1), latitude.toFixed(1));
|
|
249
|
+
* ```
|
|
250
|
+
*/
|
|
251
|
+
export function moonLibration(when) {
|
|
252
|
+
const found = Libration(new Date(moment(when, "when")));
|
|
253
|
+
return { longitude: found.elon, latitude: found.elat, distanceKm: found.dist_km, angularDiameter: found.diam_deg };
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Every perigee and apogee of the Moon from `from` (included; the search settles to about 0.1 s, so one within a quarter of a second before it counts) to `to` (not included), in time order. An end before the start throws a `RangeError`.
|
|
257
|
+
*
|
|
258
|
+
* @param from - The start of the interval, included.
|
|
259
|
+
* @param to - The end of the interval, not included.
|
|
260
|
+
* @returns The apsides in time order; empty when the interval holds none.
|
|
261
|
+
* @experimental
|
|
262
|
+
* @example
|
|
263
|
+
* ```ts
|
|
264
|
+
* import { lunarApsides } from "@johnmorrisdotca/wakusei-planets/sky";
|
|
265
|
+
*
|
|
266
|
+
* for (const apsis of lunarApsides(Date.UTC(2026, 10, 1), Date.UTC(2026, 11, 1))) console.log(apsis.kind, apsis.time.toISOString(), Math.round(apsis.distanceKm));
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
export function lunarApsides(from, to) {
|
|
270
|
+
const [start, end] = interval(from, to);
|
|
271
|
+
const found = [];
|
|
272
|
+
for (let apsis = SearchLunarApsis(new Date(start - DAY_MS)); apsis.time.date.getTime() < end; apsis = NextLunarApsis(apsis)) {
|
|
273
|
+
if (apsis.time.date.getTime() >= start - SEARCH_SLACK_MS)
|
|
274
|
+
found.push({ kind: apsis.kind === ApsisKind.Pericenter ? "perigee" : "apogee", time: apsis.time.date, distanceKm: apsis.dist_km });
|
|
275
|
+
}
|
|
276
|
+
return found;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Every time the Moon's centre crosses the ecliptic plane from `from` (included; the search settles to about 0.1 s, so one within a quarter of a second before it counts) to `to` (not included), in time order. An end before the start throws a `RangeError`.
|
|
280
|
+
*
|
|
281
|
+
* @param from - The start of the interval, included.
|
|
282
|
+
* @param to - The end of the interval, not included.
|
|
283
|
+
* @returns The crossings in time order; empty when the interval holds none.
|
|
284
|
+
* @experimental
|
|
285
|
+
* @example
|
|
286
|
+
* ```ts
|
|
287
|
+
* import { lunarNodes } from "@johnmorrisdotca/wakusei-planets/sky";
|
|
288
|
+
*
|
|
289
|
+
* for (const node of lunarNodes(Date.UTC(2026, 10, 1), Date.UTC(2026, 11, 1))) console.log(node.kind, node.time.toISOString());
|
|
290
|
+
* ```
|
|
291
|
+
*/
|
|
292
|
+
export function lunarNodes(from, to) {
|
|
293
|
+
const [start, end] = interval(from, to);
|
|
294
|
+
const found = [];
|
|
295
|
+
for (let node = SearchMoonNode(new Date(start - DAY_MS)); node.time.date.getTime() < end; node = NextMoonNode(node)) {
|
|
296
|
+
if (node.time.date.getTime() >= start - SEARCH_SLACK_MS)
|
|
297
|
+
found.push({ kind: node.kind === NodeEventKind.Ascending ? "ascending" : "descending", time: node.time.date });
|
|
298
|
+
}
|
|
299
|
+
return found;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* The next greatest elongation of Mercury or Venus after `from`. Any other body throws a `RangeError` (a body outside Earth's orbit has no greatest elongation).
|
|
303
|
+
*
|
|
304
|
+
* @param body - `"mercury"` or `"venus"`.
|
|
305
|
+
* @param from - Where to start looking.
|
|
306
|
+
* @returns The moment, the elongation in degrees and the side of the Sun.
|
|
307
|
+
* @experimental
|
|
308
|
+
* @example
|
|
309
|
+
* ```ts
|
|
310
|
+
* import { nextGreatestElongation } from "@johnmorrisdotca/wakusei-planets/sky";
|
|
311
|
+
*
|
|
312
|
+
* const mercury = nextGreatestElongation("mercury", Date.UTC(2026, 0, 1));
|
|
313
|
+
* console.log(mercury.time.toISOString(), mercury.side);
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
export function nextGreatestElongation(body, from) {
|
|
317
|
+
const start = moment(from, "from");
|
|
318
|
+
if (body !== "mercury" && body !== "venus")
|
|
319
|
+
throw new RangeError(`body must be mercury or venus, got ${String(body)}`);
|
|
320
|
+
const found = SearchMaxElongation(bodyOf(body), new Date(start));
|
|
321
|
+
return { time: found.time.date, elongation: found.elongation, side: found.visibility === "evening" ? "east" : "west" };
|
|
322
|
+
}
|
|
323
|
+
const ZONE_FORMATS = new Map();
|
|
324
|
+
/** The offset of a time zone from UTC at a moment, in milliseconds (positive east of Greenwich). */
|
|
325
|
+
function zoneOffsetMs(timeZone, at) {
|
|
326
|
+
let format = ZONE_FORMATS.get(timeZone);
|
|
327
|
+
if (format === undefined) {
|
|
328
|
+
format = new Intl.DateTimeFormat("en-US", { timeZone, hourCycle: "h23", year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric" });
|
|
329
|
+
ZONE_FORMATS.set(timeZone, format);
|
|
330
|
+
}
|
|
331
|
+
const parts = format.formatToParts(new Date(at));
|
|
332
|
+
const get = (type) => Number(parts.find((part) => part.type === type).value);
|
|
333
|
+
return Date.UTC(get("year"), get("month") - 1, get("day"), get("hour"), get("minute"), get("second")) - Math.floor(at / 1000) * 1000;
|
|
334
|
+
}
|
|
335
|
+
/** The local calendar date of a moment in a zone as one number (year, month, day), to compare days. */
|
|
336
|
+
function dayKey(timeZone, at) {
|
|
337
|
+
const local = new Date(at + zoneOffsetMs(timeZone, at));
|
|
338
|
+
return local.getUTCFullYear() * 10_000 + (local.getUTCMonth() + 1) * 100 + local.getUTCDate();
|
|
339
|
+
}
|
|
340
|
+
/** The first moment (to the second) of the local calendar day with key `key` in a zone, found by halving, which also gives the right answer where a clock change skips midnight. `near` is any moment within a day and a half of it. */
|
|
341
|
+
function dayStart(timeZone, key, near) {
|
|
342
|
+
let low = Math.floor(near / 1000) * 1000 - 36 * 3_600_000, high = low + 72 * 3_600_000;
|
|
343
|
+
while (high - low > 1000) {
|
|
344
|
+
const middle = low + Math.floor((high - low) / 2000) * 1000;
|
|
345
|
+
if (dayKey(timeZone, middle) < key)
|
|
346
|
+
low = middle;
|
|
347
|
+
else
|
|
348
|
+
high = middle;
|
|
349
|
+
}
|
|
350
|
+
return high;
|
|
351
|
+
}
|
|
352
|
+
/** The first moment of the calendar day of a zone that holds `at`, and of the day after it, as milliseconds: 23, 24 or 25 hours apart, and 24 hours apart on all but the days when the clocks change. */
|
|
353
|
+
function calendarDay(timeZone, at) {
|
|
354
|
+
const local = new Date(at + zoneOffsetMs(timeZone, at));
|
|
355
|
+
const next = new Date(Date.UTC(local.getUTCFullYear(), local.getUTCMonth(), local.getUTCDate() + 1));
|
|
356
|
+
const nextKey = next.getUTCFullYear() * 10_000 + (next.getUTCMonth() + 1) * 100 + next.getUTCDate();
|
|
357
|
+
return [dayStart(timeZone, dayKey(timeZone, at), at - 12 * 3_600_000), dayStart(timeZone, nextKey, at + 12 * 3_600_000)];
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* The rise, meridian crossing and set of a body over one calendar day of a time zone, for an observer. `date` is any moment of the day wanted (a `Date` or milliseconds); the day is the one in `timeZone` that holds it, from local midnight to the next, so a day
|
|
361
|
+
* when the clocks change is 23 or 25 hours long. `timeZone` is required: a missing, empty or unknown name throws a `RangeError` naming it. The horizon is `nextRise`'s. An event is `null` when it does not happen inside the day (the Moon has no rise on one day in about 29), and
|
|
362
|
+
* `horizon` tells a body that is up all day (`"above"`, the Sun at a pole in midsummer) or down all day (`"below"`) from one that crosses the horizon.
|
|
363
|
+
*
|
|
364
|
+
* @param body - A body of this package: the Sun, the Moon or a planet.
|
|
365
|
+
* @param date - Any moment inside the day wanted.
|
|
366
|
+
* @param options - The observer and the IANA time zone.
|
|
367
|
+
* @returns The three events as `Date`s or `null`, and the horizon state.
|
|
368
|
+
* @experimental
|
|
369
|
+
* @example
|
|
370
|
+
* ```ts
|
|
371
|
+
* import { riseSetTimes } from "@johnmorrisdotca/wakusei-planets/sky";
|
|
372
|
+
*
|
|
373
|
+
* // Sunrise and sunset in New York on 1 November 2026, the day the clocks go back (a 25-hour day).
|
|
374
|
+
* const day = riseSetTimes("sun", Date.UTC(2026, 10, 1, 12), { observer: { latitude: 40.7128, longitude: -74.006 }, timeZone: "America/New_York" });
|
|
375
|
+
* console.log(day.rise?.toISOString(), day.set?.toISOString(), day.horizon);
|
|
376
|
+
* ```
|
|
377
|
+
*/
|
|
378
|
+
export function riseSetTimes(body, date, options) {
|
|
379
|
+
const at = moment(date, "date");
|
|
380
|
+
const engineBody = bodyOf(body);
|
|
381
|
+
const observer = observerOf(options.observer);
|
|
382
|
+
const timeZone = options.timeZone;
|
|
383
|
+
if (typeof timeZone !== "string" || timeZone === "")
|
|
384
|
+
throw new RangeError(`timeZone is required, an IANA name such as "Asia/Tokyo" or "UTC", got ${String(timeZone)}`);
|
|
385
|
+
try {
|
|
386
|
+
new Intl.DateTimeFormat("en-US", { timeZone });
|
|
387
|
+
}
|
|
388
|
+
catch {
|
|
389
|
+
throw new RangeError(`timeZone must be an IANA time zone name such as "Asia/Tokyo", got ${timeZone}`);
|
|
390
|
+
}
|
|
391
|
+
const [start, end] = calendarDay(timeZone, at);
|
|
392
|
+
const within = { days: (end - start) / DAY_MS };
|
|
393
|
+
const inside = (found) => (found !== null && found.getTime() < end ? found : null);
|
|
394
|
+
const rise = inside(nextRise(body, start, { observer: options.observer, within }));
|
|
395
|
+
const set = inside(nextSet(body, start, { observer: options.observer, within }));
|
|
396
|
+
const transit = inside(nextTransit(body, start, { observer: options.observer, within }));
|
|
397
|
+
if (rise !== null || set !== null)
|
|
398
|
+
return { rise, transit, set, horizon: "crosses" };
|
|
399
|
+
const middle = new Date((start + end) / 2);
|
|
400
|
+
const equator = Equator(engineBody, middle, observer, true, true);
|
|
401
|
+
return { rise, transit, set, horizon: Horizon(middle, observer, equator.ra, equator.dec, "normal").altitude > 0 ? "above" : "below" };
|
|
402
|
+
}
|
|
403
|
+
/** How far the longitude of a body is east of the Sun's, as seen from the Earth, from `target` degrees, taken to the interval from -180 up to 180. The longitudes are apparent (light time and aberration included, as the Astronomical Almanac and Horizons give them), which moves an opposition by about 15 minutes from the geometric one. */
|
|
404
|
+
function offsetFrom(body, target, at) {
|
|
405
|
+
const diff = Ecliptic(GeoVector(body, at, true)).elon - Ecliptic(GeoVector(Body.Sun, at, true)).elon - target;
|
|
406
|
+
return diff - 360 * Math.round(diff / 360);
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* The next time a body is in conjunction with the Sun, opposite it or at quadrature, by ecliptic longitude as seen from the Earth (the longitudes of the body and the Sun differ by 0, 180 or 90 or 270 degrees, the apparent ones, as the Astronomical Almanac and Horizons give them), at or after `from` (an event within a quarter of a second before it counts as at it), or `null` when none falls inside `within.days`
|
|
410
|
+
* (850 days when not given, enough for the slowest planet's cycle). It is the longitude alignment, which is why it is named so: it is not the closest approach of two bodies in the sky (`/nearest`). A planet at opposition is on the meridian at midnight, and its time is not quite
|
|
411
|
+
* that of its closest approach to the Earth or its brightest moment.
|
|
412
|
+
* Mercury and Venus have conjunctions only (inferior and superior, told apart by `inferior`); asking either for an opposition or a quadrature, or asking for the Sun, throws a `RangeError`, since they never happen. For the Moon, a conjunction is the new Moon, an opposition the full Moon and a quadrature a first or last quarter.
|
|
413
|
+
*
|
|
414
|
+
* @param body - A body of this package except the Sun.
|
|
415
|
+
* @param from - Where to start looking, included.
|
|
416
|
+
* @param options - `kind` is required; `within.days` bounds the search (a positive number).
|
|
417
|
+
* @returns The moment with the kind, or `null` when none falls inside the window.
|
|
418
|
+
* @experimental
|
|
419
|
+
* @example
|
|
420
|
+
* ```ts
|
|
421
|
+
* import { nextAlignment } from "@johnmorrisdotca/wakusei-planets/sky";
|
|
422
|
+
*
|
|
423
|
+
* const opposition = nextAlignment("jupiter", Date.UTC(2026, 0, 1), { kind: "opposition" });
|
|
424
|
+
* console.log(opposition?.time.toISOString());
|
|
425
|
+
* ```
|
|
426
|
+
*/
|
|
427
|
+
export function nextAlignment(body, from, options) {
|
|
428
|
+
const start = moment(from, "from");
|
|
429
|
+
const engineBody = bodyOf(body);
|
|
430
|
+
const { kind } = options;
|
|
431
|
+
if (kind !== "conjunction" && kind !== "opposition" && kind !== "quadrature")
|
|
432
|
+
throw new RangeError(`kind must be conjunction, opposition or quadrature, got ${String(kind)}`);
|
|
433
|
+
if (body === "sun")
|
|
434
|
+
throw new RangeError("body must not be the sun: an alignment is with the Sun");
|
|
435
|
+
const inner = body === "mercury" || body === "venus";
|
|
436
|
+
if (inner && kind !== "conjunction")
|
|
437
|
+
throw new RangeError(`${body} has no ${kind} (it never gets that far from the Sun); only conjunction`);
|
|
438
|
+
const days = options.within?.days ?? 850;
|
|
439
|
+
if (!(days > 0) || !Number.isFinite(days))
|
|
440
|
+
throw new RangeError(`within.days must be a positive number, got ${String(days)}`);
|
|
441
|
+
const targets = kind === "conjunction" ? [0] : kind === "opposition" ? [180] : [90, 270];
|
|
442
|
+
// Walk a day at a time and take the first change of sign of the offset from a target, then settle it with the engine's search (to a second).
|
|
443
|
+
const step = 1;
|
|
444
|
+
const t0 = new Date(start);
|
|
445
|
+
for (const target of targets)
|
|
446
|
+
if (offsetFrom(engineBody, target, t0) === 0)
|
|
447
|
+
return alignment(engineBody, kind, target, t0);
|
|
448
|
+
let before = targets.map((target) => offsetFrom(engineBody, target, t0));
|
|
449
|
+
for (let day = step; day <= days + step; day += step) {
|
|
450
|
+
const t = new Date(start + day * DAY_MS);
|
|
451
|
+
const now = targets.map((target) => offsetFrom(engineBody, target, MakeTime(t)));
|
|
452
|
+
for (let i = 0; i < targets.length; i += 1) {
|
|
453
|
+
const a = before[i], b = now[i];
|
|
454
|
+
if (Math.abs(a) < 90 && Math.abs(b) < 90 && (a < 0) !== (b < 0)) {
|
|
455
|
+
const sign = a < 0 ? 1 : -1;
|
|
456
|
+
const root = Search((time) => sign * offsetFrom(engineBody, targets[i], time), MakeTime(new Date(start + (day - step) * DAY_MS)), MakeTime(t), { dt_tolerance_seconds: 0.1 });
|
|
457
|
+
const found = root?.date ?? null;
|
|
458
|
+
return found !== null && found.getTime() < start + days * DAY_MS ? alignment(engineBody, kind, targets[i], found) : null;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
before = now;
|
|
462
|
+
}
|
|
463
|
+
return null;
|
|
464
|
+
}
|
|
465
|
+
function alignment(body, kind, target, time) {
|
|
466
|
+
const inner = body === Body.Mercury || body === Body.Venus;
|
|
467
|
+
return { time, kind, side: kind === "quadrature" ? (target === 90 ? "east" : "west") : null, inferior: inner ? GeoVector(body, time, true).Length() < GeoVector(Body.Sun, time, true).Length() : null };
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* The next time Venus is at its brightest as seen from the Earth, after `from`: the moment and its magnitude. Only Venus is supported, because astronomy-engine supports only Venus: Mercury's brightest moment is at superior conjunction, when it cannot be seen, and any other body throws a `RangeError`
|
|
471
|
+
* that says where to look. The other planets are brightest very near their opposition (`nextAlignment`), and the Moon near the full Moon (`nextMoonPhase`); their magnitudes at any moment are in `skyBody()`.
|
|
472
|
+
*
|
|
473
|
+
* @param body - `"venus"`.
|
|
474
|
+
* @param from - Where to start looking.
|
|
475
|
+
* @returns The moment and the magnitude.
|
|
476
|
+
* @experimental
|
|
477
|
+
* @example
|
|
478
|
+
* ```ts
|
|
479
|
+
* import { nextPeakMagnitude } from "@johnmorrisdotca/wakusei-planets/sky";
|
|
480
|
+
*
|
|
481
|
+
* const { time, magnitude } = nextPeakMagnitude("venus", Date.UTC(2026, 0, 1));
|
|
482
|
+
* console.log(time.toISOString(), magnitude.toFixed(2));
|
|
483
|
+
* ```
|
|
484
|
+
*/
|
|
485
|
+
export function nextPeakMagnitude(body, from) {
|
|
486
|
+
const start = moment(from, "from");
|
|
487
|
+
if (body !== "venus")
|
|
488
|
+
throw new RangeError(`nextPeakMagnitude supports venus only (astronomy-engine finds no other body's peak), got ${String(body)}; the outer planets are brightest near nextAlignment(..., { kind: "opposition" }) and the Moon near nextMoonPhase(..., { phase: "full" })`);
|
|
489
|
+
const found = SearchPeakMagnitude(Body.Venus, new Date(start));
|
|
490
|
+
return { time: found.time.date, magnitude: found.mag };
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* The next solar eclipse an observer can see at least partly in the sky's geometry (the Sun may be below the horizon for part of it, or all of it: read the altitudes), at or after `from`, with the times of its contacts and the Sun's altitude at each. `null` when none falls
|
|
494
|
+
* inside `within.days` (1826 days, five years, when not given). The altitude is the Sun's centre with standard refraction, and the contacts are for the observer's height.
|
|
495
|
+
*
|
|
496
|
+
* @param from - Where to start looking, included (an eclipse whose greatest moment is before it is not found).
|
|
497
|
+
* @param options - The observer (degrees north and east, metres), and `within.days`.
|
|
498
|
+
* @returns The eclipse or `null`.
|
|
499
|
+
* @experimental
|
|
500
|
+
* @example
|
|
501
|
+
* ```ts
|
|
502
|
+
* import { localSolarEclipse } from "@johnmorrisdotca/wakusei-planets/sky";
|
|
503
|
+
*
|
|
504
|
+
* const eclipse = localSolarEclipse(Date.UTC(2026, 6, 1), { observer: { latitude: 41.6, longitude: -4.7, heightM: 700 } });
|
|
505
|
+
* console.log(eclipse?.kind, eclipse?.partialBegin.time.toISOString(), eclipse?.partialEnd.time.toISOString());
|
|
506
|
+
* ```
|
|
507
|
+
*/
|
|
508
|
+
export function localSolarEclipse(from, options) {
|
|
509
|
+
const start = moment(from, "from");
|
|
510
|
+
const observer = observerOf(options.observer);
|
|
511
|
+
const days = options.within?.days ?? 1826;
|
|
512
|
+
if (!(days > 0) || !Number.isFinite(days))
|
|
513
|
+
throw new RangeError(`within.days must be a positive number, got ${String(days)}`);
|
|
514
|
+
const contact = (event) => (event === undefined ? null : { time: event.time.date, altitude: event.altitude });
|
|
515
|
+
// The engine finds the first eclipse whose greatest moment is after its start: look from a day early and drop one that peaked before `from`.
|
|
516
|
+
let found = SearchLocalSolarEclipse(new Date(start - DAY_MS), observer);
|
|
517
|
+
while (found.peak.time.date.getTime() < start)
|
|
518
|
+
found = SearchLocalSolarEclipse(found.peak.time.AddDays(1), observer);
|
|
519
|
+
if (found.peak.time.date.getTime() >= start + days * DAY_MS)
|
|
520
|
+
return null;
|
|
521
|
+
return { kind: found.kind, obscuration: found.obscuration, partialBegin: contact(found.partial_begin), centralBegin: contact(found.total_begin), peak: contact(found.peak), centralEnd: contact(found.total_end), partialEnd: contact(found.partial_end) };
|
|
522
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** The ids of the 104 bodies of `/asteroids`, in table order. */
|
|
2
|
+
export declare const ASTEROID_IDS: readonly string[];
|
|
3
|
+
/** The prefix of an asteroid's key in the view: two asteroids (`europa`, `dione`) have the id of a moon of the package, so the view keeps its own names for them apart by it. A page never sees it: events, `sight()` and `riding` say the plain id. */
|
|
4
|
+
export declare const ASTEROID_PREFIX = "asteroid:";
|
|
5
|
+
/** The view's key of an asteroid. */
|
|
6
|
+
export declare const asteroidKey: (id: string) => string;
|
|
7
|
+
/** The asteroid's plain id from the view's key, or `undefined` for a key that is not an asteroid's. */
|
|
8
|
+
export declare const asteroidIdOf: (key: string) => string | undefined;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// The ids of the asteroids of /asteroids, in table order: what the space view needs to tell an asteroid from a body (and to say so when the option `asteroids` is off) without loading the table, which is a chunk of its own
|
|
2
|
+
// that the view reaches only when the option is turned on. A test holds this list to the table. And how the view keys the asteroids among its own names (labels, places, the sight): two of them, `europa` and `dione`, have the id of a moon.
|
|
3
|
+
/** The ids of the 104 bodies of `/asteroids`, in table order. */
|
|
4
|
+
export const ASTEROID_IDS = [
|
|
5
|
+
"hygiea", "interamnia", "europa", "davida", "euphrosyne", "patientia", "sylvia", "juno", "cybele", "thisbe", "eunomia", "psyche", "bamberga", "doris", "ursula",
|
|
6
|
+
"camilla", "hermione", "lomia", "dione", "daphne", "aurora", "egeria", "eugenia", "fortuna", "iris", "chicago", "themis", "gaspra", "ida", "mathilde", "lutetia",
|
|
7
|
+
"steins", "annefrank", "braille", "dinkinesh", "donaldjohanson", "masursky", "apl", "eros", "itokawa", "ryugu", "bennu", "toutatis", "didymos", "arrokoth",
|
|
8
|
+
"apophis", "torifune", "1998-ky26", "2024-yr4", "phaethon", "icarus", "geographos", "toro", "cruithne", "kamooalewa", "1950-da", "florence", "ganymed", "apollo",
|
|
9
|
+
"aten", "amor", "atira", "hermes", "wilson-harrington", "duende", "nereus", "golevka", "adonis", "eurybates", "polymele", "leucus", "orus", "patroclus", "hektor",
|
|
10
|
+
"achilles", "agamemnon", "nestor", "priamus", "diomedes", "odysseus", "ajax", "menelaus", "aneas", "anchises", "chariklo", "nessus", "asbolus", "hylonome",
|
|
11
|
+
"echeclus", "bienor", "thereus", "salacia", "varuna", "ixion", "varda", "mani", "2014-uz224", "albion", "leleakuhonua", "huya", "chaos", "typhon", "2012-vp113",
|
|
12
|
+
"2018-ag37",
|
|
13
|
+
];
|
|
14
|
+
/** The prefix of an asteroid's key in the view: two asteroids (`europa`, `dione`) have the id of a moon of the package, so the view keeps its own names for them apart by it. A page never sees it: events, `sight()` and `riding` say the plain id. */
|
|
15
|
+
export const ASTEROID_PREFIX = "asteroid:";
|
|
16
|
+
/** The view's key of an asteroid. */
|
|
17
|
+
export const asteroidKey = (id) => ASTEROID_PREFIX + id;
|
|
18
|
+
/** The asteroid's plain id from the view's key, or `undefined` for a key that is not an asteroid's. */
|
|
19
|
+
export const asteroidIdOf = (key) => (key.startsWith(ASTEROID_PREFIX) ? key.slice(ASTEROID_PREFIX.length) : undefined);
|