@johnmorrisdotca/wakusei-planets 0.34.0 → 0.35.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 (203) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/NOTICE.md +48 -0
  3. package/README.md +21 -3
  4. package/dist/approaches.d.ts +276 -0
  5. package/dist/approaches.js +255 -0
  6. package/dist/asteroids.d.ts +397 -0
  7. package/dist/asteroids.js +338 -0
  8. package/dist/comets.d.ts +316 -0
  9. package/dist/comets.js +404 -0
  10. package/dist/craft/bepicolombo.d.ts +1 -0
  11. package/dist/craft/bepicolombo.js +923 -0
  12. package/dist/craft/europaclipper.d.ts +1 -0
  13. package/dist/craft/europaclipper.js +342 -0
  14. package/dist/craft/hayabusa2.d.ts +1 -0
  15. package/dist/craft/hayabusa2.js +73 -0
  16. package/dist/craft/juice.d.ts +1 -0
  17. package/dist/craft/juice.js +40 -0
  18. package/dist/craft/lucy.d.ts +1 -0
  19. package/dist/craft/lucy.js +42 -0
  20. package/dist/craft/osirisapex.d.ts +1 -0
  21. package/dist/craft/osirisapex.js +69 -0
  22. package/dist/craft/psyche.d.ts +1 -0
  23. package/dist/craft/psyche.js +30 -0
  24. package/dist/craft/solarorbiter.d.ts +1 -0
  25. package/dist/craft/solarorbiter.js +94 -0
  26. package/dist/data/approaches.d.ts +17 -0
  27. package/dist/data/approaches.js +4192 -0
  28. package/dist/data/asteroids.d.ts +3 -0
  29. package/dist/data/asteroids.js +107 -0
  30. package/dist/data/comets.d.ts +2 -0
  31. package/dist/data/comets.js +36 -0
  32. package/dist/data/places-67p.d.ts +27 -0
  33. package/dist/data/places-67p.js +2 -0
  34. package/dist/data/places-ariel.d.ts +602 -0
  35. package/dist/data/places-ariel.js +27 -0
  36. package/dist/data/places-bennu.d.ts +25 -0
  37. package/dist/data/places-bennu.js +2 -0
  38. package/dist/data/places-callisto.d.ts +3547 -0
  39. package/dist/data/places-callisto.js +155 -0
  40. package/dist/data/places-ceres.d.ts +3436 -0
  41. package/dist/data/places-ceres.js +150 -0
  42. package/dist/data/places-charon.d.ts +349 -0
  43. package/dist/data/places-charon.js +16 -0
  44. package/dist/data/places-dimorphos.d.ts +23 -0
  45. package/dist/data/places-dimorphos.js +2 -0
  46. package/dist/data/places-dione.d.ts +2258 -0
  47. package/dist/data/places-dione.js +99 -0
  48. package/dist/data/places-enceladus.d.ts +2009 -0
  49. package/dist/data/places-enceladus.js +88 -0
  50. package/dist/data/places-eros.d.ts +26 -0
  51. package/dist/data/places-eros.js +2 -0
  52. package/dist/data/places-europa.d.ts +2977 -0
  53. package/dist/data/places-europa.js +130 -0
  54. package/dist/data/places-ganymede.d.ts +4518 -0
  55. package/dist/data/places-ganymede.js +197 -0
  56. package/dist/data/places-iapetus.d.ts +1616 -0
  57. package/dist/data/places-iapetus.js +71 -0
  58. package/dist/data/places-io.d.ts +6012 -0
  59. package/dist/data/places-io.js +262 -0
  60. package/dist/data/places-jupiter.d.ts +46 -0
  61. package/dist/data/places-jupiter.js +3 -0
  62. package/dist/data/places-mars.d.ts +20701 -0
  63. package/dist/data/places-mars.js +942 -0
  64. package/dist/data/places-mercury.d.ts +9256 -0
  65. package/dist/data/places-mercury.js +403 -0
  66. package/dist/data/places-mimas.d.ts +968 -0
  67. package/dist/data/places-mimas.js +43 -0
  68. package/dist/data/places-miranda.d.ts +419 -0
  69. package/dist/data/places-miranda.js +19 -0
  70. package/dist/data/places-moon.d.ts +18849 -0
  71. package/dist/data/places-moon.js +861 -0
  72. package/dist/data/places-oberon.d.ts +235 -0
  73. package/dist/data/places-oberon.js +11 -0
  74. package/dist/data/places-pluto.d.ts +1687 -0
  75. package/dist/data/places-pluto.js +74 -0
  76. package/dist/data/places-rhea.d.ts +3317 -0
  77. package/dist/data/places-rhea.js +145 -0
  78. package/dist/data/places-ryugu.d.ts +87 -0
  79. package/dist/data/places-ryugu.js +5 -0
  80. package/dist/data/places-saturn.d.ts +25 -0
  81. package/dist/data/places-saturn.js +2 -0
  82. package/dist/data/places-tempel-1.d.ts +25 -0
  83. package/dist/data/places-tempel-1.js +2 -0
  84. package/dist/data/places-tethys.d.ts +1222 -0
  85. package/dist/data/places-tethys.js +54 -0
  86. package/dist/data/places-titan.d.ts +7031 -0
  87. package/dist/data/places-titan.js +306 -0
  88. package/dist/data/places-titania.d.ts +418 -0
  89. package/dist/data/places-titania.js +19 -0
  90. package/dist/data/places-triton.d.ts +1457 -0
  91. package/dist/data/places-triton.js +64 -0
  92. package/dist/data/places-umbriel.d.ts +303 -0
  93. package/dist/data/places-umbriel.js +14 -0
  94. package/dist/data/places-venus.d.ts +9290 -0
  95. package/dist/data/places-venus.js +405 -0
  96. package/dist/data/places-vesta.d.ts +2444 -0
  97. package/dist/data/places-vesta.js +107 -0
  98. package/dist/data/roadster.d.ts +8 -0
  99. package/dist/data/roadster.js +686 -0
  100. package/dist/element.js +17 -3
  101. package/dist/places/67p.d.ts +13 -0
  102. package/dist/places/67p.js +14 -0
  103. package/dist/places/ariel.d.ts +13 -0
  104. package/dist/places/ariel.js +14 -0
  105. package/dist/places/bennu.d.ts +13 -0
  106. package/dist/places/bennu.js +14 -0
  107. package/dist/places/callisto.d.ts +13 -0
  108. package/dist/places/callisto.js +14 -0
  109. package/dist/places/ceres.d.ts +13 -0
  110. package/dist/places/ceres.js +14 -0
  111. package/dist/places/charon.d.ts +13 -0
  112. package/dist/places/charon.js +14 -0
  113. package/dist/places/dimorphos.d.ts +13 -0
  114. package/dist/places/dimorphos.js +14 -0
  115. package/dist/places/dione.d.ts +13 -0
  116. package/dist/places/dione.js +14 -0
  117. package/dist/places/enceladus.d.ts +13 -0
  118. package/dist/places/enceladus.js +14 -0
  119. package/dist/places/eros.d.ts +13 -0
  120. package/dist/places/eros.js +14 -0
  121. package/dist/places/europa.d.ts +13 -0
  122. package/dist/places/europa.js +14 -0
  123. package/dist/places/ganymede.d.ts +13 -0
  124. package/dist/places/ganymede.js +14 -0
  125. package/dist/places/iapetus.d.ts +13 -0
  126. package/dist/places/iapetus.js +14 -0
  127. package/dist/places/io.d.ts +13 -0
  128. package/dist/places/io.js +14 -0
  129. package/dist/places/jupiter.d.ts +13 -0
  130. package/dist/places/jupiter.js +14 -0
  131. package/dist/places/mars.d.ts +12 -0
  132. package/dist/places/mars.js +13 -0
  133. package/dist/places/mercury.d.ts +13 -0
  134. package/dist/places/mercury.js +14 -0
  135. package/dist/places/mimas.d.ts +13 -0
  136. package/dist/places/mimas.js +14 -0
  137. package/dist/places/miranda.d.ts +13 -0
  138. package/dist/places/miranda.js +14 -0
  139. package/dist/places/moon.d.ts +12 -0
  140. package/dist/places/moon.js +13 -0
  141. package/dist/places/oberon.d.ts +13 -0
  142. package/dist/places/oberon.js +14 -0
  143. package/dist/places/pluto.d.ts +13 -0
  144. package/dist/places/pluto.js +14 -0
  145. package/dist/places/rhea.d.ts +13 -0
  146. package/dist/places/rhea.js +14 -0
  147. package/dist/places/ryugu.d.ts +13 -0
  148. package/dist/places/ryugu.js +14 -0
  149. package/dist/places/saturn.d.ts +13 -0
  150. package/dist/places/saturn.js +14 -0
  151. package/dist/places/tempel-1.d.ts +13 -0
  152. package/dist/places/tempel-1.js +14 -0
  153. package/dist/places/tethys.d.ts +13 -0
  154. package/dist/places/tethys.js +14 -0
  155. package/dist/places/titan.d.ts +13 -0
  156. package/dist/places/titan.js +14 -0
  157. package/dist/places/titania.d.ts +13 -0
  158. package/dist/places/titania.js +14 -0
  159. package/dist/places/triton.d.ts +13 -0
  160. package/dist/places/triton.js +14 -0
  161. package/dist/places/umbriel.d.ts +13 -0
  162. package/dist/places/umbriel.js +14 -0
  163. package/dist/places/venus.d.ts +13 -0
  164. package/dist/places/venus.js +14 -0
  165. package/dist/places/vesta.d.ts +13 -0
  166. package/dist/places/vesta.js +14 -0
  167. package/dist/places.d.ts +354 -0
  168. package/dist/places.js +357 -0
  169. package/dist/plan/element.d.ts +52 -0
  170. package/dist/plan/element.js +258 -0
  171. package/dist/plan/index.d.ts +2 -0
  172. package/dist/plan/index.js +2 -0
  173. package/dist/plan/layout.d.ts +230 -0
  174. package/dist/plan/layout.js +229 -0
  175. package/dist/plan/svg.d.ts +125 -0
  176. package/dist/plan/svg.js +185 -0
  177. package/dist/roadster.d.ts +415 -0
  178. package/dist/roadster.js +496 -0
  179. package/dist/scene/body.js +10 -9
  180. package/dist/scene/painter.d.ts +1 -1
  181. package/dist/scene/painter.js +23 -7
  182. package/dist/scene/textures.d.ts +2 -2
  183. package/dist/space/beside.d.ts +4 -1
  184. package/dist/space/beside.js +47 -6
  185. package/dist/space/covered.d.ts +102 -0
  186. package/dist/space/covered.js +98 -0
  187. package/dist/space/element.d.ts +21 -1
  188. package/dist/space/element.js +126 -11
  189. package/dist/space/index.d.ts +1 -0
  190. package/dist/space/index.js +1 -0
  191. package/dist/space/scale.d.ts +2 -1
  192. package/dist/space/scale.js +13 -2
  193. package/dist/space/scene.d.ts +10 -0
  194. package/dist/space/scene.js +45 -22
  195. package/dist/spacecraft-facts.d.ts +4 -4
  196. package/dist/spacecraft-facts.js +158 -3
  197. package/dist/spacecraft.d.ts +69 -2
  198. package/dist/spacecraft.js +45 -2
  199. package/dist/textures/mimas-1024.webp +0 -0
  200. package/dist/textures/mimas-512.webp +0 -0
  201. package/dist/version.d.ts +1 -1
  202. package/dist/version.js +1 -1
  203. package/package.json +203 -1
package/dist/comets.js ADDED
@@ -0,0 +1,404 @@
1
+ // Comets: a set of well-known ones with their orbits, where each is at a date and how far from the Sun and the Earth, the line of its orbit, and a door to add a newly found one. The orbits are the elements of the NASA/JPL
2
+ // Small-Body Database (scripts/make-comet-data.py, src/data/comets.ts), run as two-body motion round the Sun, and only this entry point imports the table. Positions are heliocentric, in astronomical units, in the J2000
3
+ // ecliptic frame, like `heliocentric` (which gives the planets in the same axes). Pure: no DOM, no clock reads, no three.js.
4
+ import { COMET_TABLE } from "./data/comets.js";
5
+ import { DAY_MS, UNIX_EPOCH_JD, toTerrestrialTime, ttMinusUtc } from "./clock.js";
6
+ import { heliocentric } from "./ephemeris.js";
7
+ import { length, RAD, sub } from "./vec.js";
8
+ import { moment } from "./when.js";
9
+ /** The Gaussian gravitational constant in radians a day: the Sun's pull for an orbit measured in astronomical units and days (the constant JPL's elements are written with). */
10
+ const GAUSS_K = 0.017_202_098_95;
11
+ const YEAR_DAYS = 365.25;
12
+ /** The distance, in days from an orbit's epoch, within which the position was measured against Horizons (see `COMET_ACCURACY`). */
13
+ const TRUSTED_DAYS = 365;
14
+ /**
15
+ * How far to trust a comet's position, stated. The elements are one osculating orbit: the planets pull on a comet, and a comet's own jets push on it, so the real orbit drifts from the ellipse, a little in a year and
16
+ * more the further from the epoch. A comet that has not been near a planet is good to the numbers below within a year of its epoch; a long-period comet's position is good only near its epoch, and a periodic comet's
17
+ * period is the osculating one, which is not the mean (Halley's comes out a year off).
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * import { COMET_ACCURACY } from "@johnmorrisdotca/wakusei-planets/comets";
22
+ *
23
+ * console.log(COMET_ACCURACY.read, COMET_ACCURACY.note);
24
+ * ```
25
+ */
26
+ export const COMET_ACCURACY = {
27
+ /** The date the elements were read from the Small-Body Database, `YYYY-MM-DD`. */
28
+ read: "2026-11-11",
29
+ /** The source of the elements. */
30
+ source: "NASA/JPL Small-Body Database, https://ssd-api.jpl.nasa.gov/sbdb.api",
31
+ /** The days from an epoch within which `cometTrust` says a position is inside what was measured. */
32
+ trustedDays: TRUSTED_DAYS,
33
+ /** In words, what the numbers leave out. */
34
+ note: "The orbit is two-body motion on the elements of one epoch: the planets' pull and a comet's own jets move the real orbit away from it, so a position is good near the epoch and drifts away from it, by days or years later a comet's closest approach to the Sun may be off. A long-period comet is good only near its epoch; a periodic comet's last and next perihelion are JPL's own dates inside the span they cover and are repeated from the osculating period outside it. For a close look at one date use JPL Horizons.",
35
+ };
36
+ const RECORDS = COMET_TABLE;
37
+ const BY_KEY = new Map();
38
+ for (const record of RECORDS) {
39
+ BY_KEY.set(record.id, record);
40
+ BY_KEY.set(record.designation.toLowerCase(), record);
41
+ BY_KEY.set(record.name.en.toLowerCase(), record);
42
+ }
43
+ /**
44
+ * The well-known comets of the set, in the order of the table (the periodic ones by number, then the long-period ones and the interstellar ones). Give a kind to list only that kind.
45
+ * The comet `halley` is the same one `heliocentric("halley", ...)` draws; the table here is the plain two-body orbit of the Small-Body Database, and inside 1800 to 2200 `heliocentric` has the more exact one (see `COMET_ACCURACY`).
46
+ *
47
+ * @param kind - `periodic`, `long-period` or `interstellar`; leave it out for all.
48
+ * @returns A new array of the comets.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * import { listComets } from "@johnmorrisdotca/wakusei-planets/comets";
53
+ *
54
+ * console.log(listComets("periodic").map((comet) => comet.designation).join(" "));
55
+ * ```
56
+ */
57
+ export function listComets(kind) {
58
+ return kind === undefined ? [...RECORDS] : RECORDS.filter((comet) => comet.kind === kind);
59
+ }
60
+ /**
61
+ * The comet with an id (`halley`), a designation (`1P`, `C/1995 O1`) or an English name (`Halley`), or `undefined` when the set has none. Case does not matter.
62
+ *
63
+ * @param key - The id, designation or English name.
64
+ * @returns The comet, or `undefined`.
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * import { findComet } from "@johnmorrisdotca/wakusei-planets/comets";
69
+ *
70
+ * const comet = findComet("c/1995 o1");
71
+ * console.log(comet?.name.en, comet?.name.ja);
72
+ * ```
73
+ */
74
+ export function findComet(key) {
75
+ return typeof key === "string" ? BY_KEY.get(key.trim().toLowerCase()) : undefined;
76
+ }
77
+ function resolve(comet) {
78
+ if (typeof comet !== "string") {
79
+ if (comet === null || typeof comet !== "object" || !Number.isFinite(comet.e) || !Number.isFinite(comet.q) || !Number.isFinite(comet.tp))
80
+ throw new TypeError("comet must be a comet of this set, one made by cometFromElements, or the id of one");
81
+ return comet;
82
+ }
83
+ const found = findComet(comet);
84
+ if (found === undefined)
85
+ throw new RangeError(`no comet "${comet}" in the set; the ids are ${RECORDS.map((one) => one.id).join(", ")}`);
86
+ return found;
87
+ }
88
+ // Stumpff's functions C(z) and S(z) of the universal Kepler equation.
89
+ function stumpff(z) {
90
+ if (Math.abs(z) < 0.1) {
91
+ const c = 1 / 2 - z / 24 + (z * z) / 720 - (z * z * z) / 40_320 + (z * z * z * z) / 3_628_800 - (z * z * z * z * z) / 479_001_600;
92
+ const s = 1 / 6 - z / 120 + (z * z) / 5_040 - (z * z * z) / 362_880 + (z * z * z * z) / 39_916_800 - (z * z * z * z * z) / 6_227_020_800;
93
+ return [c, s];
94
+ }
95
+ if (z > 0) {
96
+ const r = Math.sqrt(z);
97
+ return [(1 - Math.cos(r)) / z, (r - Math.sin(r)) / (r * r * r)];
98
+ }
99
+ const r = Math.sqrt(-z);
100
+ return [(Math.cosh(r) - 1) / -z, (Math.sinh(r) - r) / (r * r * r)];
101
+ }
102
+ /** The position in the orbit's own plane (x toward perihelion), in au, `days` after perihelion, from the universal form of Kepler's equation: elliptic, parabolic and open orbits all in one. */
103
+ function inPlane(e, q, days) {
104
+ let dt = days;
105
+ const alpha = (1 - e) / q;
106
+ if (e < 1) {
107
+ const a = q / (1 - e);
108
+ const period = (2 * Math.PI * Math.sqrt(a * a * a)) / GAUSS_K;
109
+ dt -= period * Math.round(dt / period);
110
+ }
111
+ if (dt === 0)
112
+ return [q, 0];
113
+ const sign = dt < 0 ? -1 : 1;
114
+ const target = GAUSS_K * Math.abs(dt);
115
+ const f = (chi) => {
116
+ const [, s] = stumpff(alpha * chi * chi);
117
+ return q * chi + e * chi * chi * chi * s;
118
+ };
119
+ // the start: Barker's cubic, a parabola's solution, found by Cardano's formula; then Newton's method held inside a bracket that bisection falls back on
120
+ let chi = target / q;
121
+ if (e > 1e-3) {
122
+ const w = Math.cbrt((3 * target) / e + Math.sqrt(9 * (target / e) ** 2 + 8 * (q / e) ** 3));
123
+ if (Number.isFinite(w) && w > 0)
124
+ chi = w - (2 * q) / (e * w);
125
+ }
126
+ let lo = 0;
127
+ let hi = Math.max(chi * 2, 1e-9);
128
+ for (let guard = 0; f(hi) < target && guard < 200; guard += 1)
129
+ hi *= 2;
130
+ for (let round = 0; round < 100; round += 1) {
131
+ const z = alpha * chi * chi;
132
+ const [c, s] = stumpff(z);
133
+ const value = q * chi + e * chi * chi * chi * s - target;
134
+ if (value > 0)
135
+ hi = Math.min(hi, chi);
136
+ else
137
+ lo = Math.max(lo, chi);
138
+ if (Math.abs(value) < 1e-14 * Math.max(1, target))
139
+ break;
140
+ const r = chi * chi * c + q * (1 - z * c);
141
+ let next = chi - value / r;
142
+ if (!(next > lo && next < hi))
143
+ next = (lo + hi) / 2;
144
+ if (next === chi)
145
+ break;
146
+ chi = next;
147
+ }
148
+ const z = alpha * chi * chi;
149
+ const [c, s] = stumpff(z);
150
+ const fx = 1 - (chi * chi * c) / q;
151
+ const gx = Math.abs(dt) - (chi * chi * chi * s) / GAUSS_K;
152
+ const speed = GAUSS_K * Math.sqrt((1 + e) / q);
153
+ return [fx * q, sign * gx * speed];
154
+ }
155
+ function rotate(x, y, comet) {
156
+ const node = comet.node * RAD;
157
+ const w = comet.w * RAD;
158
+ const inc = comet.i * RAD;
159
+ const cw = Math.cos(w);
160
+ const sw = Math.sin(w);
161
+ const cn = Math.cos(node);
162
+ const sn = Math.sin(node);
163
+ const ci = Math.cos(inc);
164
+ const si = Math.sin(inc);
165
+ return [(cw * cn - sw * sn * ci) * x + (-sw * cn - cw * sn * ci) * y, (cw * sn + sw * cn * ci) * x + (-sw * sn + cw * cn * ci) * y, sw * si * x + cw * si * y];
166
+ }
167
+ /** A Julian date in TDB as milliseconds since 1970 in UTC. */
168
+ function utcOfJulianDate(jd) {
169
+ const tt = (jd - UNIX_EPOCH_JD) * DAY_MS;
170
+ return tt - ttMinusUtc(tt) * 1000;
171
+ }
172
+ /**
173
+ * Where a comet is at a moment: x, y, z in astronomical units from the Sun, in the J2000 ecliptic frame (the axes of `heliocentric`), from two-body motion on the comet's elements. Closed, parabolic and open orbits all work.
174
+ * A date far from the comet's epoch is accepted and answered, but the real comet is not where two-body motion puts it, see `COMET_ACCURACY` and `cometTrust`. A comet that is not in the set throws a `RangeError`, a `when`
175
+ * that is not a date or a finite number a `TypeError` or `RangeError`.
176
+ *
177
+ * @param comet - A comet of the set, one from `cometFromElements`, or the id, designation or name of one in the set.
178
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
179
+ * @returns The position in astronomical units.
180
+ *
181
+ * @example
182
+ * ```ts
183
+ * import { cometPosition } from "@johnmorrisdotca/wakusei-planets/comets";
184
+ *
185
+ * const [x, y, z] = cometPosition("67P", Date.UTC(2021, 10, 2));
186
+ * console.log(Math.hypot(x, y, z).toFixed(3), "au from the Sun");
187
+ * ```
188
+ */
189
+ export function cometPosition(comet, when) {
190
+ const record = resolve(comet);
191
+ const ms = moment(when);
192
+ const days = (toTerrestrialTime(ms) / DAY_MS + UNIX_EPOCH_JD) - record.tp;
193
+ const [x, y] = inPlane(record.e, record.q, days);
194
+ const out = rotate(x, y, record);
195
+ if (!Number.isFinite(out[0]) || !Number.isFinite(out[1]) || !Number.isFinite(out[2]))
196
+ throw new RangeError(`${record.designation} has no position at ${new Date(ms).toISOString()} on two-body motion: too far from its perihelion`);
197
+ return out;
198
+ }
199
+ /**
200
+ * How far a comet is from the Sun and from the Earth at a moment, in astronomical units (the Earth from `heliocentric`).
201
+ *
202
+ * @param comet - A comet of the set, one from `cometFromElements`, or the id, designation or name of one in the set.
203
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
204
+ * @returns `sunAu` and `earthAu`.
205
+ *
206
+ * @example
207
+ * ```ts
208
+ * import { cometDistance } from "@johnmorrisdotca/wakusei-planets/comets";
209
+ *
210
+ * const d = cometDistance("hale-bopp", Date.UTC(1997, 2, 22));
211
+ * console.log(`${d.sunAu.toFixed(2)} au from the Sun, ${d.earthAu.toFixed(2)} au from the Earth`);
212
+ * ```
213
+ */
214
+ export function cometDistance(comet, when) {
215
+ const ms = moment(when);
216
+ const position = cometPosition(comet, ms);
217
+ return { sunAu: length(position), earthAu: length(sub(position, heliocentric("earth", ms))) };
218
+ }
219
+ /**
220
+ * A comet's orbit as points to draw a line through: x, y, z in astronomical units from the Sun, in the J2000 ecliptic frame, three numbers a point. A closed orbit whose far end is within `maxAu` is the whole
221
+ * ellipse, with its first point repeated at the end so a line closes; any other orbit is the arc through perihelion from where it comes in to where it goes out, up to `maxAu` from the Sun. Points are closer
222
+ * together near the Sun, where the orbit bends.
223
+ *
224
+ * @param comet - A comet of the set, one from `cometFromElements`, or the id, designation or name of one in the set.
225
+ * @param points - How many points, from 8 (default 180).
226
+ * @param maxAu - The furthest distance from the Sun to draw, in astronomical units (default 40: past Neptune).
227
+ * @returns The points, `3 * points` numbers (one more point for a closed ellipse).
228
+ *
229
+ * @example
230
+ * ```ts
231
+ * import { cometOrbit } from "@johnmorrisdotca/wakusei-planets/comets";
232
+ *
233
+ * const line = cometOrbit("halley", 120);
234
+ * console.log(line.length / 3, "points, the first at", line[0]!.toFixed(2), line[1]!.toFixed(2), line[2]!.toFixed(2));
235
+ * ```
236
+ */
237
+ export function cometOrbit(comet, points = 180, maxAu = 40) {
238
+ const record = resolve(comet);
239
+ if (!Number.isInteger(points) || points < 8 || points > 100_000)
240
+ throw new RangeError(`points must be a whole number from 8 to 100000, got ${String(points)}`);
241
+ if (!(maxAu > record.q) || !Number.isFinite(maxAu))
242
+ throw new RangeError(`maxAu must be a finite number beyond the perihelion distance ${record.q}, got ${String(maxAu)}`);
243
+ const { e, q } = record;
244
+ const closed = e < 1 && (q * (1 + e)) / (1 - e) <= maxAu;
245
+ const count = closed ? points + 1 : points;
246
+ const out = new Float32Array(count * 3);
247
+ const put = (k, x, y) => {
248
+ const p = rotate(x, y, record);
249
+ out[k * 3] = p[0];
250
+ out[k * 3 + 1] = p[1];
251
+ out[k * 3 + 2] = p[2];
252
+ };
253
+ if (closed) {
254
+ const a = q / (1 - e);
255
+ const b = a * Math.sqrt(1 - e * e);
256
+ for (let k = 0; k < count; k += 1) {
257
+ const eccentric = (k / points) * 2 * Math.PI;
258
+ put(k, a * (Math.cos(eccentric) - e), b * Math.sin(eccentric));
259
+ }
260
+ return out;
261
+ }
262
+ const p = q * (1 + e);
263
+ const cosMax = e === 0 ? -1 : Math.max(-1, Math.min(1, (p / maxAu - 1) / e));
264
+ const reach = Math.acos(Math.max(cosMax, e >= 1 ? -1 / e + 1e-9 : -1));
265
+ for (let k = 0; k < count; k += 1) {
266
+ const u = (2 * k) / (count - 1) - 1;
267
+ const nu = reach * Math.sign(u) * u * u;
268
+ const r = p / (1 + e * Math.cos(nu));
269
+ put(k, r * Math.cos(nu), r * Math.sin(nu));
270
+ }
271
+ return out;
272
+ }
273
+ /**
274
+ * The last and the next time a comet is at perihelion, relative to a moment, in milliseconds since 1970 (UTC). For a periodic comet of the set between the two dates JPL gives (`CometRecord.perihelion`: the last, observed or predicted
275
+ * from JPL's solution, and the next, JPL's prediction by integrating that orbit) they are those, `basis` is `"jpl"`, and `lastObserved` is what JPL's orbit solution records (an observation of the solution within 150 days of the last passage), not whether the comet was seen. Outside that span the passages are repeated from the
276
+ * nearest JPL date with the osculating period and `basis` is `"estimated"` (good to months for a short orbit and to a year or more for a long one). A comet of the set on a parabola or an open orbit, or on an ellipse with a period over 10,000 years (Hyakutake's is about 70,000), has one perihelion, JPL's, which is the last when
277
+ * it is behind and the next when it is ahead (the other is `null`: a repetition 70,000 years on would be a date in the year 99940). A comet from `cometFromElements` has the date it was given (`"elements"`) or the repetition of it (`"estimated"`).
278
+ *
279
+ * @param comet - A comet of the set, one from `cometFromElements`, or the id, designation or name of one in the set.
280
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
281
+ * @returns `last` and `next`, in milliseconds since 1970 (UTC), `null` where there is none; `basis`; and `lastObserved`.
282
+ *
283
+ * @example
284
+ * ```ts
285
+ * import { cometPerihelia } from "@johnmorrisdotca/wakusei-planets/comets";
286
+ *
287
+ * const { last, next, basis } = cometPerihelia("1P", Date.UTC(2026, 10, 11));
288
+ * console.log(new Date(last!).toISOString().slice(0, 10), new Date(next!).toISOString().slice(0, 10), basis);
289
+ * ```
290
+ */
291
+ export function cometPerihelia(comet, when) {
292
+ const record = resolve(comet);
293
+ const ms = moment(when);
294
+ const jpl = record.perihelion;
295
+ if (jpl !== undefined) {
296
+ const last = utcOfJulianDate(jpl.last);
297
+ const next = utcOfJulianDate(jpl.next);
298
+ if (last <= ms && ms < next)
299
+ return { last, next, basis: "jpl", lastObserved: jpl.lastObserved };
300
+ }
301
+ const tp = utcOfJulianDate(record.tp);
302
+ const periodYears = record.e < 1 ? (2 * Math.PI * Math.sqrt((record.q / (1 - record.e)) ** 3)) / GAUSS_K / 365.25 : Infinity;
303
+ if (!(record.e < 1) || periodYears > 10_000) {
304
+ const basis = record.spkid === null ? "elements" : "jpl";
305
+ return tp <= ms ? { last: tp, next: null, basis, lastObserved: false } : { last: null, next: tp, basis, lastObserved: false };
306
+ }
307
+ const period = (2 * Math.PI * Math.sqrt((record.q / (1 - record.e)) ** 3)) / GAUSS_K;
308
+ const anchor = jpl === undefined ? record.tp : ms < utcOfJulianDate(jpl.last) ? jpl.last : jpl.next;
309
+ const turns = Math.floor((toTerrestrialTime(ms) / DAY_MS + UNIX_EPOCH_JD - anchor) / period);
310
+ return { last: utcOfJulianDate(anchor + turns * period), next: utcOfJulianDate(anchor + (turns + 1) * period), basis: jpl === undefined && (turns === 0 || turns === -1) ? "elements" : "estimated", lastObserved: false };
311
+ }
312
+ /**
313
+ * How far a moment is from the comet's epoch, and whether it is inside the year around it that the position was measured against Horizons for (`COMET_ACCURACY.trustedDays`).
314
+ *
315
+ * @param comet - A comet of the set, one from `cometFromElements`, or the id, designation or name of one in the set.
316
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
317
+ * @returns `epoch` (milliseconds since 1970, UTC), `daysFromEpoch` (negative before it) and `withinYear`.
318
+ *
319
+ * @example
320
+ * ```ts
321
+ * import { cometTrust } from "@johnmorrisdotca/wakusei-planets/comets";
322
+ *
323
+ * const trust = cometTrust("neowise", Date.UTC(2026, 0, 1));
324
+ * console.log(trust.daysFromEpoch.toFixed(0), "days from the epoch;", trust.withinYear ? "near it" : "far from it: two-body motion drifts");
325
+ * ```
326
+ */
327
+ export function cometTrust(comet, when) {
328
+ const record = resolve(comet);
329
+ const epoch = utcOfJulianDate(record.epoch);
330
+ const daysFromEpoch = (moment(when) - epoch) / DAY_MS;
331
+ return { epoch, daysFromEpoch, withinYear: Math.abs(daysFromEpoch) <= TRUSTED_DAYS };
332
+ }
333
+ /**
334
+ * A comet made from elements a page supplies, to use with every function here, so a newly found comet needs no release. The comet is checked (a bad number throws a `TypeError` or a `RangeError` that names it) and the
335
+ * angles are folded into 0 to 360. Fields the elements do not give are `null` or empty.
336
+ *
337
+ * @param input - The elements.
338
+ * @returns The comet.
339
+ *
340
+ * @example
341
+ * ```ts
342
+ * import { cometFromElements, cometPosition } from "@johnmorrisdotca/wakusei-planets/comets";
343
+ *
344
+ * const comet = cometFromElements({ designation: "C/2099 A1", e: 0.99, q: 0.8, i: 45, node: 120, w: 30, perihelion: Date.UTC(2099, 5, 1) });
345
+ * console.log(cometPosition(comet, Date.UTC(2099, 5, 1)).map((v) => v.toFixed(3)).join(" "));
346
+ * ```
347
+ */
348
+ export function cometFromElements(input) {
349
+ if (input === null || typeof input !== "object")
350
+ throw new TypeError("cometFromElements needs an object of elements");
351
+ const named = (field, value) => {
352
+ if (typeof value !== "number" || !Number.isFinite(value))
353
+ throw new TypeError(`${field} must be a finite number, got ${String(value)}`);
354
+ return value;
355
+ };
356
+ if (typeof input.designation !== "string" || input.designation.trim() === "")
357
+ throw new TypeError("designation must be a non-empty string");
358
+ const e = named("e", input.e);
359
+ const q = named("q", input.q);
360
+ const inclination = named("i", input.i);
361
+ if (e < 0)
362
+ throw new RangeError(`e must be 0 or more, got ${e}`);
363
+ if (!(q > 0))
364
+ throw new RangeError(`q must be more than 0, got ${q}`);
365
+ if (inclination < 0 || inclination > 180)
366
+ throw new RangeError(`i must be from 0 to 180 degrees, got ${inclination}`);
367
+ if ((input.tp === undefined) === (input.perihelion === undefined))
368
+ throw new TypeError("give the time of perihelion as `tp` (a Julian date in TDB) or as `perihelion` (a Date or milliseconds), one of the two");
369
+ const tp = input.tp !== undefined ? named("tp", input.tp) : (() => {
370
+ const tt = toTerrestrialTime(moment(input.perihelion, "perihelion"));
371
+ return tt / DAY_MS + UNIX_EPOCH_JD;
372
+ })();
373
+ const epoch = input.epoch === undefined ? tp : named("epoch", input.epoch);
374
+ const fold = (field, value) => ((named(field, value) % 360) + 360) % 360;
375
+ const periodYears = e < 1 ? (2 * Math.PI * Math.sqrt((q / (1 - e)) ** 3)) / GAUSS_K / YEAR_DAYS : null;
376
+ const kind = input.kind ?? (periodYears !== null && periodYears < 200 ? "periodic" : "long-period");
377
+ if (kind !== "periodic" && kind !== "long-period" && kind !== "interstellar")
378
+ throw new RangeError(`kind must be periodic, long-period or interstellar, got ${String(kind)}`);
379
+ const designation = input.designation.trim();
380
+ return {
381
+ id: input.id ?? designation.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, ""),
382
+ designation,
383
+ fullName: input.name?.en === undefined ? designation : `${designation} (${input.name.en})`,
384
+ name: { en: input.name?.en ?? designation, ja: input.name?.ja ?? "" },
385
+ kind,
386
+ e,
387
+ q,
388
+ i: inclination,
389
+ node: fold("node", input.node),
390
+ w: fold("w", input.w),
391
+ tp,
392
+ epoch,
393
+ a: e < 1 ? q / (1 - e) : null,
394
+ periodYears,
395
+ condition: null,
396
+ observations: null,
397
+ arcDays: null,
398
+ solution: null,
399
+ nucleusKm: null,
400
+ nucleusExtentKm: null,
401
+ showers: [],
402
+ spkid: null,
403
+ };
404
+ }
@@ -0,0 +1 @@
1
+ export {};