@johnmorrisdotca/chikyu-earth 0.21.0 → 0.22.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 +6 -0
- package/NOTICE.md +13 -0
- package/README.md +32 -0
- package/dist/passes/element.d.ts +47 -0
- package/dist/passes/element.js +409 -0
- package/dist/passes/index.d.ts +3 -0
- package/dist/passes/index.js +4 -0
- package/dist/passes/magnitudes.d.ts +62 -0
- package/dist/passes/magnitudes.js +80 -0
- package/dist/passes/passes.d.ts +399 -0
- package/dist/passes/passes.js +469 -0
- package/dist/passes/sky.d.ts +82 -0
- package/dist/passes/sky.js +104 -0
- package/package.json +13 -1
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
// When a satellite crosses the sky over a place. SGP4 (satellite.js, an optional peer) puts the satellite in the sky, this package's own Sun position says whether the watcher's sky is dark and whether
|
|
2
|
+
// the satellite is in sunlight, and the brightness follows from the standard magnitude. Pure: no DOM, no clock (the window is given), no state; it imports on a server.
|
|
3
|
+
import { ecfToLookAngles, eciToEcf, eciToGeodetic, geodeticToEcf, gstime, json2satrec, sgp4 } from "satellite.js";
|
|
4
|
+
import { subsolarPoint } from "../realistic/sun.js";
|
|
5
|
+
import { DEFAULT_DIM_AFTER_DAYS, DEFAULT_MAX_AGE_DAYS } from "../satellites/limits.js";
|
|
6
|
+
import { magnitude, STANDARD_MAGNITUDES } from "./magnitudes.js";
|
|
7
|
+
const RAD = Math.PI / 180;
|
|
8
|
+
const DAY_MS = 86_400_000;
|
|
9
|
+
/** The Earth's equatorial radius, kilometres (WGS84): the radius of the shadow's cylinder. */
|
|
10
|
+
const EARTH_RADIUS_KM = 6378.137;
|
|
11
|
+
/** The Sun's altitude at sunrise and sunset, degrees: its upper edge on the horizon, with the usual refraction. Below it begins civil twilight. */
|
|
12
|
+
const SUNSET = -0.833;
|
|
13
|
+
const SCAN_MS = 30_000;
|
|
14
|
+
const SAMPLE_MS = 2000;
|
|
15
|
+
const COMPASS = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"];
|
|
16
|
+
/**
|
|
17
|
+
* The compass word for a direction.
|
|
18
|
+
*
|
|
19
|
+
* @param azimuthDeg - Degrees clockwise from north: 0 is north, 90 east, 180 south, 270 west.
|
|
20
|
+
* @returns One of sixteen words, "N" to "NNW".
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { compassOf } from "@johnmorrisdotca/chikyu-earth/passes";
|
|
25
|
+
*
|
|
26
|
+
* console.log(compassOf(200));
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function compassOf(azimuthDeg) {
|
|
30
|
+
return COMPASS[Math.round((((azimuthDeg % 360) + 360) % 360) / 22.5) % 16];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The name of the sky for a Sun altitude.
|
|
34
|
+
*
|
|
35
|
+
* @param sunAltitudeDeg - The Sun's altitude in degrees: above 0 it is up.
|
|
36
|
+
* @returns `day` above -0.833 degrees (sunrise and sunset), then `civil`, `nautical`, `astronomical` and `night` at -6, -12 and -18.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* import { skyAt } from "@johnmorrisdotca/chikyu-earth/passes";
|
|
41
|
+
*
|
|
42
|
+
* console.log(skyAt(-8));
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export function skyAt(sunAltitudeDeg) {
|
|
46
|
+
if (sunAltitudeDeg > SUNSET)
|
|
47
|
+
return "day";
|
|
48
|
+
if (sunAltitudeDeg > -6)
|
|
49
|
+
return "civil";
|
|
50
|
+
if (sunAltitudeDeg > -12)
|
|
51
|
+
return "nautical";
|
|
52
|
+
if (sunAltitudeDeg > -18)
|
|
53
|
+
return "astronomical";
|
|
54
|
+
return "night";
|
|
55
|
+
}
|
|
56
|
+
/** The Sun's altitude (degrees) that ends each sky: the Sun must be at or below it for the sky to be that dark. */
|
|
57
|
+
const SKY_LIMIT = { civil: SUNSET, nautical: -6, astronomical: -12, night: -18 };
|
|
58
|
+
/** The direction of the Sun in the Earth-fixed frame, a unit vector (x to longitude 0 on the equator, y east, z north). */
|
|
59
|
+
function sunVector(ms) {
|
|
60
|
+
const sun = subsolarPoint(ms);
|
|
61
|
+
const lat = sun.lat * RAD;
|
|
62
|
+
const lon = sun.lon * RAD;
|
|
63
|
+
return [Math.cos(lat) * Math.cos(lon), Math.cos(lat) * Math.sin(lon), Math.sin(lat)];
|
|
64
|
+
}
|
|
65
|
+
const upVector = (observer) => {
|
|
66
|
+
const lat = observer.latitude * RAD;
|
|
67
|
+
const lon = observer.longitude * RAD;
|
|
68
|
+
return [Math.cos(lat) * Math.cos(lon), Math.cos(lat) * Math.sin(lon), Math.sin(lat)];
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* The Sun's altitude over a place at a moment, in degrees (above 0 the Sun is up). It uses the package's own low-precision Sun, good to about 0.02 degree, and does not add refraction.
|
|
72
|
+
*
|
|
73
|
+
* @param ms - A moment, in milliseconds since the Unix epoch.
|
|
74
|
+
* @param observer - Where the watcher stands.
|
|
75
|
+
* @returns The altitude of the Sun's centre, in degrees.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* import { sunAltitude } from "@johnmorrisdotca/chikyu-earth/passes";
|
|
80
|
+
*
|
|
81
|
+
* // Tokyo at 9 in the evening local time (12:00 UTC) on 9 November 2026
|
|
82
|
+
* console.log(sunAltitude(Date.UTC(2026, 10, 9, 12), { latitude: 35.68, longitude: 139.69 }).toFixed(0));
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export function sunAltitude(ms, observer) {
|
|
86
|
+
const s = sunVector(ms);
|
|
87
|
+
const up = upVector(observer);
|
|
88
|
+
return Math.asin(Math.max(-1, Math.min(1, s[0] * up[0] + s[1] * up[1] + s[2] * up[2]))) / RAD;
|
|
89
|
+
}
|
|
90
|
+
const epochOf = (text) => Date.parse(/[zZ]|[+-]\d\d:?\d\d$/.test(text) ? text : `${text}Z`);
|
|
91
|
+
/** One satellite seen from one place: the elements, started once, and every question asked of them. */
|
|
92
|
+
function watcher(set, observer) {
|
|
93
|
+
const rec = json2satrec(set);
|
|
94
|
+
const epoch = epochOf(set.EPOCH);
|
|
95
|
+
const here = { latitude: observer.latitude * RAD, longitude: observer.longitude * RAD, height: (observer.heightM ?? 0) / 1000 };
|
|
96
|
+
const site = geodeticToEcf(here);
|
|
97
|
+
const up = upVector(observer);
|
|
98
|
+
/** Altitude, azimuth (degrees), range (km), and the satellite's Earth-fixed position, or undefined where SGP4 gives none. */
|
|
99
|
+
const look = (ms) => {
|
|
100
|
+
const sample = sgp4(rec, (ms - epoch) / 60_000);
|
|
101
|
+
if (!sample || !sample.position || !Number.isFinite(sample.position.x))
|
|
102
|
+
return undefined;
|
|
103
|
+
const ecf = eciToEcf(sample.position, gstime(new Date(ms)));
|
|
104
|
+
const angles = ecfToLookAngles(here, ecf);
|
|
105
|
+
return { altitude: angles.elevation / RAD, azimuth: angles.azimuth / RAD, rangeKm: angles.rangeSat, ecf };
|
|
106
|
+
};
|
|
107
|
+
return { epoch, look, site, up };
|
|
108
|
+
}
|
|
109
|
+
/** Everything about one satellite at one moment, seen from the watcher's place; undefined where SGP4 gives no place. */
|
|
110
|
+
function condition(w, ms, standard) {
|
|
111
|
+
const at = w.look(ms);
|
|
112
|
+
if (!at)
|
|
113
|
+
return undefined;
|
|
114
|
+
const s = sunVector(ms);
|
|
115
|
+
const r = at.ecf;
|
|
116
|
+
const along = r.x * s[0] + r.y * s[1] + r.z * s[2];
|
|
117
|
+
// in sunlight unless it is on the night side of the Earth and inside the cylinder of shadow behind it
|
|
118
|
+
const sunlit = along >= 0 || r.x * r.x + r.y * r.y + r.z * r.z - along * along > EARTH_RADIUS_KM * EARTH_RADIUS_KM;
|
|
119
|
+
const sunAltitude = Math.asin(Math.max(-1, Math.min(1, s[0] * w.up[0] + s[1] * w.up[1] + s[2] * w.up[2]))) / RAD;
|
|
120
|
+
let mag;
|
|
121
|
+
if (standard !== undefined && sunlit) {
|
|
122
|
+
// the angle at the satellite between the Sun and the watcher
|
|
123
|
+
const dx = w.site.x - r.x;
|
|
124
|
+
const dy = w.site.y - r.y;
|
|
125
|
+
const dz = w.site.z - r.z;
|
|
126
|
+
const cos = (dx * s[0] + dy * s[1] + dz * s[2]) / Math.hypot(dx, dy, dz);
|
|
127
|
+
mag = magnitude(standard, at.rangeKm, Math.acos(Math.max(-1, Math.min(1, cos))) / RAD);
|
|
128
|
+
}
|
|
129
|
+
return { azimuth: at.azimuth, altitude: at.altitude, rangeKm: at.rangeKm, sunlit, sunAltitude, magnitude: mag };
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Where one satellite is in the sky over a place at one moment, and what the light is like: its direction and height, its distance, whether the Sun lights it, how high the Sun is, and how bright it looks.
|
|
133
|
+
*
|
|
134
|
+
* @param set - The satellite's elements.
|
|
135
|
+
* @param observer - Where the watcher stands.
|
|
136
|
+
* @param ms - The moment, milliseconds since the Unix epoch.
|
|
137
|
+
* @param options - `magnitudes` for your own standard magnitudes (the ISS, Tianhe and Hubble are known).
|
|
138
|
+
* @returns Its place in the sky (`altitude` below 0 means below the horizon), `sunlit`, the Sun's altitude and the magnitude (undefined when it is in shadow or its standard magnitude is not known); undefined where SGP4 gives no place.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* import { satelliteAt } from "@johnmorrisdotca/chikyu-earth/passes";
|
|
143
|
+
*
|
|
144
|
+
* const iss = {
|
|
145
|
+
* OBJECT_NAME: "ISS (ZARYA)",
|
|
146
|
+
* EPOCH: "2026-11-02T10:25:21.580320",
|
|
147
|
+
* MEAN_MOTION: 15.49150046,
|
|
148
|
+
* ECCENTRICITY: 0.0007139,
|
|
149
|
+
* INCLINATION: 51.6332,
|
|
150
|
+
* RA_OF_ASC_NODE: 335.9757,
|
|
151
|
+
* ARG_OF_PERICENTER: 339.3739,
|
|
152
|
+
* MEAN_ANOMALY: 20.6961,
|
|
153
|
+
* NORAD_CAT_ID: 25544,
|
|
154
|
+
* BSTAR: 0.00012,
|
|
155
|
+
* MEAN_MOTION_DOT: 0.0001,
|
|
156
|
+
* MEAN_MOTION_DDOT: 0,
|
|
157
|
+
* };
|
|
158
|
+
* const where = satelliteAt(iss, { latitude: 51.5, longitude: -0.13 }, Date.UTC(2026, 10, 3, 12));
|
|
159
|
+
* console.log(where?.compass, where?.altitude.toFixed(0));
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
export function satelliteAt(set, observer, ms, options = {}) {
|
|
163
|
+
const here = condition(watcher(set, observer), ms, { ...STANDARD_MAGNITUDES, ...options.magnitudes }[set.NORAD_CAT_ID]);
|
|
164
|
+
if (!here)
|
|
165
|
+
return undefined;
|
|
166
|
+
return { ms, azimuth: here.azimuth, compass: compassOf(here.azimuth), altitude: here.altitude, rangeKm: here.rangeKm, sunlit: here.sunlit, sunAltitude: here.sunAltitude, magnitude: here.magnitude };
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Every pass of the satellites over a place in a span of time, the ones a person can see or all of them. For each satellite the search steps through the span every 30 seconds, then narrows each rise, highest point and
|
|
170
|
+
* set to a tenth of a second; for each pass it works out where the satellite is lit by the Sun, how dark the watcher's sky is, and how bright the satellite looks. It needs `satellite.js` (MIT, an optional peer dependency) and
|
|
171
|
+
* imports in a browser or on a server. It is for satellites that rise and set within hours (low orbits); one that stays above the horizon (a geostationary one) has no passes.
|
|
172
|
+
*
|
|
173
|
+
* Passes depend on fresh elements: SGP4's error grows by about a second of time a day or two for the ISS, and a reboost (the ISS has one every month or two) moves the station by more than that all at once, so a pass is as good as the
|
|
174
|
+
* elements are new. Each pass says how old they are (`elementAgeDays`) and flags `stale` after two weeks.
|
|
175
|
+
*
|
|
176
|
+
* @param sets - One element set or several, in CelesTrak's JSON format (`ElementSet`), fetched by your page.
|
|
177
|
+
* @param observer - Where the watcher stands.
|
|
178
|
+
* @param window - The span to look in, `from` to `to` in milliseconds since the Unix epoch; a pass is listed when its highest point is in it.
|
|
179
|
+
* @param options - The lowest altitude, the lightest sky, whether to list the passes that cannot be seen, the limiting magnitude and your own standard magnitudes.
|
|
180
|
+
* @returns The passes, earliest first, each with its start, highest point and end, the part that can be seen and why not when none can.
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```ts
|
|
184
|
+
* import { findPasses } from "@johnmorrisdotca/chikyu-earth/passes";
|
|
185
|
+
*
|
|
186
|
+
* const iss = {
|
|
187
|
+
* OBJECT_NAME: "ISS (ZARYA)",
|
|
188
|
+
* OBJECT_ID: "1998-067A",
|
|
189
|
+
* EPOCH: "2026-11-02T10:25:21.580320",
|
|
190
|
+
* MEAN_MOTION: 15.49150046,
|
|
191
|
+
* ECCENTRICITY: 0.0007139,
|
|
192
|
+
* INCLINATION: 51.6332,
|
|
193
|
+
* RA_OF_ASC_NODE: 335.9757,
|
|
194
|
+
* ARG_OF_PERICENTER: 339.3739,
|
|
195
|
+
* MEAN_ANOMALY: 20.6961,
|
|
196
|
+
* NORAD_CAT_ID: 25544,
|
|
197
|
+
* BSTAR: 0.00012,
|
|
198
|
+
* MEAN_MOTION_DOT: 0.0001,
|
|
199
|
+
* MEAN_MOTION_DDOT: 0,
|
|
200
|
+
* };
|
|
201
|
+
* const day = Date.UTC(2026, 10, 3);
|
|
202
|
+
* const passes = findPasses(iss, { latitude: 51.5, longitude: -0.13 }, { from: day, to: day + 5 * 86_400_000 }, { all: true });
|
|
203
|
+
* console.log(passes.length > 0, passes[0]?.highest.compass);
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
export function findPasses(sets, observer, window, options = {}) {
|
|
207
|
+
const list = Array.isArray(sets) ? sets : [sets];
|
|
208
|
+
const minAltitude = options.minAltitude ?? 10;
|
|
209
|
+
const darkest = SKY_LIMIT[options.sky ?? "civil"];
|
|
210
|
+
const limit = options.limitingMagnitude ?? 4;
|
|
211
|
+
const maxAgeMs = (options.maxAgeDays ?? DEFAULT_MAX_AGE_DAYS) * DAY_MS;
|
|
212
|
+
const standards = { ...STANDARD_MAGNITUDES, ...options.magnitudes };
|
|
213
|
+
const found = [];
|
|
214
|
+
for (const set of list) {
|
|
215
|
+
// a satellite that goes round more slowly than once in about 7 hours (up past the GPS ring) does not rise and set in a way a pass table can use
|
|
216
|
+
if (!(set.MEAN_MOTION >= 3.4))
|
|
217
|
+
continue;
|
|
218
|
+
let w;
|
|
219
|
+
try {
|
|
220
|
+
w = watcher(set, observer);
|
|
221
|
+
}
|
|
222
|
+
catch {
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
if (!Number.isFinite(w.epoch) || w.look(w.epoch) === undefined)
|
|
226
|
+
continue;
|
|
227
|
+
const standard = standards[set.NORAD_CAT_ID];
|
|
228
|
+
const altitudeAt = (ms) => w.look(ms)?.altitude ?? Number.NEGATIVE_INFINITY;
|
|
229
|
+
/** The moment the altitude passes `level` between `inside` (above it) and `outside` (below it), to 0.01 s. */
|
|
230
|
+
const crossing = (inside, outside) => {
|
|
231
|
+
let a = inside;
|
|
232
|
+
let b = outside;
|
|
233
|
+
for (let i = 0; i < 40 && Math.abs(b - a) > 10; i += 1) {
|
|
234
|
+
const m = (a + b) / 2;
|
|
235
|
+
if (altitudeAt(m) >= minAltitude)
|
|
236
|
+
a = m;
|
|
237
|
+
else
|
|
238
|
+
b = m;
|
|
239
|
+
}
|
|
240
|
+
return (a + b) / 2;
|
|
241
|
+
};
|
|
242
|
+
const first = window.from - 20 * 60_000;
|
|
243
|
+
const steps = Math.ceil((window.to + 20 * 60_000 - first) / SCAN_MS);
|
|
244
|
+
const alt = [];
|
|
245
|
+
for (let i = 0; i <= steps; i += 1)
|
|
246
|
+
alt.push(altitudeAt(first + i * SCAN_MS));
|
|
247
|
+
for (let i = 1; i < steps; i += 1) {
|
|
248
|
+
// a rough top: a sample no lower than the one before and higher than the one after
|
|
249
|
+
if (!(alt[i] >= alt[i - 1] && alt[i] > alt[i + 1] && alt[i] > minAltitude - 3))
|
|
250
|
+
continue;
|
|
251
|
+
// the exact top, by narrowing a ternary search between the two neighbours
|
|
252
|
+
let lo = first + (i - 1) * SCAN_MS;
|
|
253
|
+
let hi = first + (i + 1) * SCAN_MS;
|
|
254
|
+
for (let k = 0; k < 48 && hi - lo > 10; k += 1) {
|
|
255
|
+
const a = lo + (hi - lo) / 3;
|
|
256
|
+
const b = hi - (hi - lo) / 3;
|
|
257
|
+
if (altitudeAt(a) < altitudeAt(b))
|
|
258
|
+
lo = a;
|
|
259
|
+
else
|
|
260
|
+
hi = b;
|
|
261
|
+
}
|
|
262
|
+
const top = (lo + hi) / 2;
|
|
263
|
+
if (top < window.from || top > window.to || altitudeAt(top) < minAltitude)
|
|
264
|
+
continue;
|
|
265
|
+
// the rise and the set: step out until the satellite is below the lowest altitude, then narrow
|
|
266
|
+
let before = top;
|
|
267
|
+
while (altitudeAt(before - 10_000) >= minAltitude && top - before < 3_600_000)
|
|
268
|
+
before -= 10_000;
|
|
269
|
+
let after = top;
|
|
270
|
+
while (altitudeAt(after + 10_000) >= minAltitude && after - top < 3_600_000)
|
|
271
|
+
after += 10_000;
|
|
272
|
+
if (top - before >= 3_600_000 || after - top >= 3_600_000)
|
|
273
|
+
continue;
|
|
274
|
+
const start = crossing(before, before - 10_000);
|
|
275
|
+
const end = crossing(after, after + 10_000);
|
|
276
|
+
if (Math.abs(top - w.epoch) > maxAgeMs)
|
|
277
|
+
continue;
|
|
278
|
+
const point = (ms) => {
|
|
279
|
+
const at = w.look(ms);
|
|
280
|
+
return { ms, azimuth: at.azimuth, compass: compassOf(at.azimuth), altitude: at.altitude, rangeKm: at.rangeKm };
|
|
281
|
+
};
|
|
282
|
+
/** What the watcher can tell at one moment of the pass: is the satellite in sunlight, how dark is the sky, how bright is the satellite. */
|
|
283
|
+
const state = (ms) => {
|
|
284
|
+
const at = condition(w, ms, standard);
|
|
285
|
+
const dark = at.sunAltitude <= darkest;
|
|
286
|
+
const bright = at.magnitude === undefined || at.magnitude <= limit;
|
|
287
|
+
return { lit: at.sunlit, sun: at.sunAltitude, mag: at.magnitude, dark, bright, seen: at.sunlit && dark && bright && at.altitude >= minAltitude };
|
|
288
|
+
};
|
|
289
|
+
// the part that can be seen, found at 2 s and narrowed at each end
|
|
290
|
+
const seenAt = (ms) => w.look(ms) !== undefined && state(ms).seen;
|
|
291
|
+
const edge = (inside, outside) => {
|
|
292
|
+
let a = inside;
|
|
293
|
+
let b = outside;
|
|
294
|
+
for (let k = 0; k < 40 && Math.abs(b - a) > 10; k += 1) {
|
|
295
|
+
const m = (a + b) / 2;
|
|
296
|
+
if (seenAt(m))
|
|
297
|
+
a = m;
|
|
298
|
+
else
|
|
299
|
+
b = m;
|
|
300
|
+
}
|
|
301
|
+
return (a + b) / 2;
|
|
302
|
+
};
|
|
303
|
+
let runStart = -1;
|
|
304
|
+
let best;
|
|
305
|
+
let brightest;
|
|
306
|
+
let brightestLit;
|
|
307
|
+
let anyDark = false;
|
|
308
|
+
let anyLit = false;
|
|
309
|
+
let anySunlit = false;
|
|
310
|
+
for (let ms = start; ms <= end + SAMPLE_MS; ms += SAMPLE_MS) {
|
|
311
|
+
const t = Math.min(ms, end);
|
|
312
|
+
const one = state(t);
|
|
313
|
+
anyDark ||= one.dark;
|
|
314
|
+
if (one.dark && one.lit)
|
|
315
|
+
anyLit = true;
|
|
316
|
+
if (one.lit)
|
|
317
|
+
anySunlit = true;
|
|
318
|
+
if (one.lit && one.mag !== undefined && (brightestLit === undefined || one.mag < brightestLit))
|
|
319
|
+
brightestLit = one.mag;
|
|
320
|
+
if (one.seen) {
|
|
321
|
+
if (one.mag !== undefined && (brightest === undefined || one.mag < brightest))
|
|
322
|
+
brightest = one.mag;
|
|
323
|
+
if (runStart < 0)
|
|
324
|
+
runStart = t;
|
|
325
|
+
if (best === undefined || t - runStart > best[1] - best[0])
|
|
326
|
+
best = [runStart, t];
|
|
327
|
+
}
|
|
328
|
+
else
|
|
329
|
+
runStart = -1;
|
|
330
|
+
if (t === end)
|
|
331
|
+
break;
|
|
332
|
+
}
|
|
333
|
+
let visible;
|
|
334
|
+
if (best !== undefined) {
|
|
335
|
+
let vs = best[0];
|
|
336
|
+
let ve = best[1];
|
|
337
|
+
// narrow the ends where they are inside the pass (the run began or ended because of the light, not the pass's own ends)
|
|
338
|
+
if (vs > start + 1)
|
|
339
|
+
vs = edge(vs, vs - SAMPLE_MS);
|
|
340
|
+
if (ve < end - 1)
|
|
341
|
+
ve = edge(ve, ve + SAMPLE_MS);
|
|
342
|
+
visible = { start: point(vs), highest: point(Math.min(ve, Math.max(vs, top))), end: point(ve) };
|
|
343
|
+
}
|
|
344
|
+
const mid = visible ? visible.highest.ms : top;
|
|
345
|
+
const here = state(mid);
|
|
346
|
+
const reason = visible ? "visible" : !anyDark ? "daylight" : !anyLit ? "shadow" : "faint";
|
|
347
|
+
const pass = {
|
|
348
|
+
noradId: set.NORAD_CAT_ID,
|
|
349
|
+
name: set.OBJECT_NAME,
|
|
350
|
+
start: point(start),
|
|
351
|
+
highest: point(top),
|
|
352
|
+
end: point(end),
|
|
353
|
+
durationSeconds: (end - start) / 1000,
|
|
354
|
+
visible,
|
|
355
|
+
reason,
|
|
356
|
+
sky: skyAt(here.sun),
|
|
357
|
+
sunAltitude: here.sun,
|
|
358
|
+
sunlit: anySunlit,
|
|
359
|
+
magnitude: visible ? brightest : brightestLit,
|
|
360
|
+
elementEpochMs: w.epoch,
|
|
361
|
+
elementAgeDays: (top - w.epoch) / DAY_MS,
|
|
362
|
+
stale: Math.abs(top - w.epoch) > DEFAULT_DIM_AFTER_DAYS * DAY_MS,
|
|
363
|
+
};
|
|
364
|
+
if (options.all || visible)
|
|
365
|
+
found.push(pass);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return found.sort((a, b) => a.highest.ms - b.highest.ms);
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* The satellite's track across the sky for a pass: a point every `stepSeconds` from the pass's start to its end, with the first and last on the start and the end. Draw it with `skySvg`.
|
|
372
|
+
*
|
|
373
|
+
* @param set - The satellite's elements (the one the pass was found from).
|
|
374
|
+
* @param observer - Where the watcher stands.
|
|
375
|
+
* @param pass - A pass from `findPasses`.
|
|
376
|
+
* @param stepSeconds - Seconds between points (5 by default).
|
|
377
|
+
* @returns The points, in time order.
|
|
378
|
+
*
|
|
379
|
+
* @example
|
|
380
|
+
* ```ts
|
|
381
|
+
* import { findPasses, passPath } from "@johnmorrisdotca/chikyu-earth/passes";
|
|
382
|
+
*
|
|
383
|
+
* const iss = {
|
|
384
|
+
* OBJECT_NAME: "ISS (ZARYA)",
|
|
385
|
+
* EPOCH: "2026-11-02T10:25:21.580320",
|
|
386
|
+
* MEAN_MOTION: 15.49150046,
|
|
387
|
+
* ECCENTRICITY: 0.0007139,
|
|
388
|
+
* INCLINATION: 51.6332,
|
|
389
|
+
* RA_OF_ASC_NODE: 335.9757,
|
|
390
|
+
* ARG_OF_PERICENTER: 339.3739,
|
|
391
|
+
* MEAN_ANOMALY: 20.6961,
|
|
392
|
+
* NORAD_CAT_ID: 25544,
|
|
393
|
+
* BSTAR: 0.00012,
|
|
394
|
+
* MEAN_MOTION_DOT: 0.0001,
|
|
395
|
+
* MEAN_MOTION_DDOT: 0,
|
|
396
|
+
* };
|
|
397
|
+
* const place = { latitude: 51.5, longitude: -0.13 };
|
|
398
|
+
* const day = Date.UTC(2026, 10, 3);
|
|
399
|
+
* const [first] = findPasses(iss, place, { from: day, to: day + 3 * 86_400_000 }, { all: true });
|
|
400
|
+
* console.log(first ? passPath(iss, place, first).length : 0);
|
|
401
|
+
* ```
|
|
402
|
+
*/
|
|
403
|
+
export function passPath(set, observer, pass, stepSeconds = 5) {
|
|
404
|
+
const w = watcher(set, observer);
|
|
405
|
+
const out = [];
|
|
406
|
+
const step = Math.max(1, stepSeconds) * 1000;
|
|
407
|
+
for (let ms = pass.start.ms;; ms += step) {
|
|
408
|
+
const t = Math.min(ms, pass.end.ms);
|
|
409
|
+
const at = w.look(t);
|
|
410
|
+
if (at) {
|
|
411
|
+
const s = sunVector(t);
|
|
412
|
+
const r = at.ecf;
|
|
413
|
+
const along = r.x * s[0] + r.y * s[1] + r.z * s[2];
|
|
414
|
+
out.push({ ms: t, azimuth: at.azimuth, altitude: at.altitude, sunlit: along >= 0 || r.x * r.x + r.y * r.y + r.z * r.z - along * along > EARTH_RADIUS_KM * EARTH_RADIUS_KM });
|
|
415
|
+
}
|
|
416
|
+
if (t >= pass.end.ms)
|
|
417
|
+
break;
|
|
418
|
+
}
|
|
419
|
+
return out;
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* The places straight under a satellite from one moment to another: its ground track. Use a pass's `start.ms` and `end.ms` for the track of one pass, to draw on a globe.
|
|
423
|
+
*
|
|
424
|
+
* @param set - The satellite's elements.
|
|
425
|
+
* @param fromMs - The first moment, milliseconds since the Unix epoch.
|
|
426
|
+
* @param toMs - The last moment.
|
|
427
|
+
* @param stepSeconds - Seconds between points (10 by default).
|
|
428
|
+
* @returns The points, in time order; a moment SGP4 gives no place for is left out.
|
|
429
|
+
*
|
|
430
|
+
* @example
|
|
431
|
+
* ```ts
|
|
432
|
+
* import { groundTrack } from "@johnmorrisdotca/chikyu-earth/passes";
|
|
433
|
+
*
|
|
434
|
+
* const iss = {
|
|
435
|
+
* OBJECT_NAME: "ISS (ZARYA)",
|
|
436
|
+
* EPOCH: "2026-11-02T10:25:21.580320",
|
|
437
|
+
* MEAN_MOTION: 15.49150046,
|
|
438
|
+
* ECCENTRICITY: 0.0007139,
|
|
439
|
+
* INCLINATION: 51.6332,
|
|
440
|
+
* RA_OF_ASC_NODE: 335.9757,
|
|
441
|
+
* ARG_OF_PERICENTER: 339.3739,
|
|
442
|
+
* MEAN_ANOMALY: 20.6961,
|
|
443
|
+
* NORAD_CAT_ID: 25544,
|
|
444
|
+
* BSTAR: 0.00012,
|
|
445
|
+
* MEAN_MOTION_DOT: 0.0001,
|
|
446
|
+
* MEAN_MOTION_DDOT: 0,
|
|
447
|
+
* };
|
|
448
|
+
* const noon = Date.UTC(2026, 10, 3, 12);
|
|
449
|
+
* const track = groundTrack(iss, noon, noon + 10 * 60_000);
|
|
450
|
+
* console.log(track.length, Math.abs(track[0]!.lat) < 52);
|
|
451
|
+
* ```
|
|
452
|
+
*/
|
|
453
|
+
export function groundTrack(set, fromMs, toMs, stepSeconds = 10) {
|
|
454
|
+
const rec = json2satrec(set);
|
|
455
|
+
const epoch = epochOf(set.EPOCH);
|
|
456
|
+
const out = [];
|
|
457
|
+
const step = Math.max(1, stepSeconds) * 1000;
|
|
458
|
+
for (let ms = fromMs;; ms += step) {
|
|
459
|
+
const t = Math.min(ms, toMs);
|
|
460
|
+
const sample = sgp4(rec, (t - epoch) / 60_000);
|
|
461
|
+
if (sample && sample.position && Number.isFinite(sample.position.x)) {
|
|
462
|
+
const place = eciToGeodetic(sample.position, gstime(new Date(t)));
|
|
463
|
+
out.push({ ms: t, lat: place.latitude / RAD, lon: ((((place.longitude / RAD) % 360) + 540) % 360) - 180, altitudeKm: place.height });
|
|
464
|
+
}
|
|
465
|
+
if (t >= toMs)
|
|
466
|
+
break;
|
|
467
|
+
}
|
|
468
|
+
return out;
|
|
469
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { PathPoint, SatellitePass } from "./passes.ts";
|
|
2
|
+
/**
|
|
3
|
+
* The words on a sky dome: the four directions and the three marks on the track. English by default; pass your own (the demo passes Japanese).
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import type { SkyLabels } from "@johnmorrisdotca/chikyu-earth/passes";
|
|
8
|
+
*
|
|
9
|
+
* const japanese: SkyLabels = { north: "北", east: "東", south: "南", west: "西", start: "出", highest: "最高", end: "入", zenith: "真上" };
|
|
10
|
+
* console.log(japanese.north);
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export interface SkyLabels {
|
|
14
|
+
north: string;
|
|
15
|
+
east: string;
|
|
16
|
+
south: string;
|
|
17
|
+
west: string;
|
|
18
|
+
start: string;
|
|
19
|
+
highest: string;
|
|
20
|
+
end: string;
|
|
21
|
+
zenith: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* What `skySvg` may be asked.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import type { SkyOptions } from "@johnmorrisdotca/chikyu-earth/passes";
|
|
29
|
+
*
|
|
30
|
+
* const options: SkyOptions = { size: 320, times: { start: "18:21", highest: "18:24", end: "18:27" } };
|
|
31
|
+
* console.log(options.size);
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export interface SkyOptions {
|
|
35
|
+
/** Width and height of the SVG in pixels (320 by default). */
|
|
36
|
+
size?: number;
|
|
37
|
+
/** The words, any of them (English where missing). */
|
|
38
|
+
labels?: Partial<SkyLabels>;
|
|
39
|
+
/** A time to write beside each mark, already in the place's own time. */
|
|
40
|
+
times?: {
|
|
41
|
+
start?: string;
|
|
42
|
+
highest?: string;
|
|
43
|
+
end?: string;
|
|
44
|
+
};
|
|
45
|
+
/** The description for a screen reader (a plain sentence is made from the pass when missing). */
|
|
46
|
+
description?: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* A pass drawn on the sky as an SVG string: the horizon as a circle, the zenith (straight overhead) in the middle with rings at 30 and 60 degrees, north up and east on the left (the way a star chart is held over your head), and
|
|
50
|
+
* the satellite's track as a line that is solid and warm where the satellite is in sunlight and dashed where it is in the Earth's shadow, with its start, highest point and end marked. The dome is a dark sky whatever the page's
|
|
51
|
+
* colours; the four directions are written in the page's text colour. It is pure: the same arguments give the same markup.
|
|
52
|
+
*
|
|
53
|
+
* @param pass - A pass from `findPasses`.
|
|
54
|
+
* @param path - Its track from `passPath`.
|
|
55
|
+
* @param options - The size, the words and the times to write beside the marks.
|
|
56
|
+
* @returns One `<svg>` element as text.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* import { findPasses, passPath, skySvg } from "@johnmorrisdotca/chikyu-earth/passes";
|
|
61
|
+
*
|
|
62
|
+
* const iss = {
|
|
63
|
+
* OBJECT_NAME: "ISS (ZARYA)",
|
|
64
|
+
* EPOCH: "2026-11-02T10:25:21.580320",
|
|
65
|
+
* MEAN_MOTION: 15.49150046,
|
|
66
|
+
* ECCENTRICITY: 0.0007139,
|
|
67
|
+
* INCLINATION: 51.6332,
|
|
68
|
+
* RA_OF_ASC_NODE: 335.9757,
|
|
69
|
+
* ARG_OF_PERICENTER: 339.3739,
|
|
70
|
+
* MEAN_ANOMALY: 20.6961,
|
|
71
|
+
* NORAD_CAT_ID: 25544,
|
|
72
|
+
* BSTAR: 0.00012,
|
|
73
|
+
* MEAN_MOTION_DOT: 0.0001,
|
|
74
|
+
* MEAN_MOTION_DDOT: 0,
|
|
75
|
+
* };
|
|
76
|
+
* const place = { latitude: 51.5, longitude: -0.13 };
|
|
77
|
+
* const day = Date.UTC(2026, 10, 3);
|
|
78
|
+
* const [first] = findPasses(iss, place, { from: day, to: day + 3 * 86_400_000 }, { all: true });
|
|
79
|
+
* if (first) console.log(skySvg(first, passPath(iss, place, first), { size: 240 }).startsWith("<svg"));
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export declare function skySvg(pass: SatellitePass, path: readonly PathPoint[], options?: SkyOptions): string;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
const RAD = Math.PI / 180;
|
|
2
|
+
const ENGLISH = { north: "N", east: "E", south: "S", west: "W", start: "start", highest: "highest", end: "end", zenith: "overhead" };
|
|
3
|
+
const escape = (text) => text.replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]);
|
|
4
|
+
const round = (n) => (Math.round(n * 10) / 10).toString();
|
|
5
|
+
/**
|
|
6
|
+
* A pass drawn on the sky as an SVG string: the horizon as a circle, the zenith (straight overhead) in the middle with rings at 30 and 60 degrees, north up and east on the left (the way a star chart is held over your head), and
|
|
7
|
+
* the satellite's track as a line that is solid and warm where the satellite is in sunlight and dashed where it is in the Earth's shadow, with its start, highest point and end marked. The dome is a dark sky whatever the page's
|
|
8
|
+
* colours; the four directions are written in the page's text colour. It is pure: the same arguments give the same markup.
|
|
9
|
+
*
|
|
10
|
+
* @param pass - A pass from `findPasses`.
|
|
11
|
+
* @param path - Its track from `passPath`.
|
|
12
|
+
* @param options - The size, the words and the times to write beside the marks.
|
|
13
|
+
* @returns One `<svg>` element as text.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { findPasses, passPath, skySvg } from "@johnmorrisdotca/chikyu-earth/passes";
|
|
18
|
+
*
|
|
19
|
+
* const iss = {
|
|
20
|
+
* OBJECT_NAME: "ISS (ZARYA)",
|
|
21
|
+
* EPOCH: "2026-11-02T10:25:21.580320",
|
|
22
|
+
* MEAN_MOTION: 15.49150046,
|
|
23
|
+
* ECCENTRICITY: 0.0007139,
|
|
24
|
+
* INCLINATION: 51.6332,
|
|
25
|
+
* RA_OF_ASC_NODE: 335.9757,
|
|
26
|
+
* ARG_OF_PERICENTER: 339.3739,
|
|
27
|
+
* MEAN_ANOMALY: 20.6961,
|
|
28
|
+
* NORAD_CAT_ID: 25544,
|
|
29
|
+
* BSTAR: 0.00012,
|
|
30
|
+
* MEAN_MOTION_DOT: 0.0001,
|
|
31
|
+
* MEAN_MOTION_DDOT: 0,
|
|
32
|
+
* };
|
|
33
|
+
* const place = { latitude: 51.5, longitude: -0.13 };
|
|
34
|
+
* const day = Date.UTC(2026, 10, 3);
|
|
35
|
+
* const [first] = findPasses(iss, place, { from: day, to: day + 3 * 86_400_000 }, { all: true });
|
|
36
|
+
* if (first) console.log(skySvg(first, passPath(iss, place, first), { size: 240 }).startsWith("<svg"));
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export function skySvg(pass, path, options = {}) {
|
|
40
|
+
const size = options.size ?? 320;
|
|
41
|
+
const words = { ...ENGLISH, ...options.labels };
|
|
42
|
+
const margin = size * 0.09;
|
|
43
|
+
const radius = size / 2 - margin;
|
|
44
|
+
const c = size / 2;
|
|
45
|
+
const at = (azimuth, altitude) => {
|
|
46
|
+
const r = (radius * (90 - Math.max(0, altitude))) / 90;
|
|
47
|
+
return [c - r * Math.sin(azimuth * RAD), c - r * Math.cos(azimuth * RAD)];
|
|
48
|
+
};
|
|
49
|
+
const font = Math.max(10, size * 0.045);
|
|
50
|
+
const parts = [];
|
|
51
|
+
parts.push(`<circle cx="${c}" cy="${c}" r="${round(radius)}" style="fill:var(--chikyu-sky,#10192b);stroke:currentColor" stroke-width="1.5"/>`);
|
|
52
|
+
for (const ring of [30, 60])
|
|
53
|
+
parts.push(`<circle cx="${c}" cy="${c}" r="${round((radius * (90 - ring)) / 90)}" fill="none" stroke="#8fa1b8" stroke-opacity="0.45" stroke-width="1" stroke-dasharray="2 4"/>`);
|
|
54
|
+
parts.push(`<path d="M${round(c - radius)} ${c}H${round(c + radius)}M${c} ${round(c - radius)}V${round(c + radius)}" stroke="#8fa1b8" stroke-opacity="0.35" stroke-width="1"/>`);
|
|
55
|
+
const edge = radius + font * 0.95;
|
|
56
|
+
const letters = [[words.north, 0], [words.east, 90], [words.south, 180], [words.west, 270]];
|
|
57
|
+
for (const [text, azimuth] of letters) {
|
|
58
|
+
const x = c - edge * Math.sin(azimuth * RAD);
|
|
59
|
+
const y = c - edge * Math.cos(azimuth * RAD) + font * 0.35;
|
|
60
|
+
parts.push(`<text x="${round(x)}" y="${round(y)}" text-anchor="middle" font-size="${round(font)}" fill="currentColor" font-weight="600">${escape(text)}</text>`);
|
|
61
|
+
}
|
|
62
|
+
parts.push(`<circle cx="${c}" cy="${c}" r="2" fill="#8fa1b8"/>`);
|
|
63
|
+
// the track: runs of lit and unlit points, each its own line, the shared point joining them
|
|
64
|
+
const runs = [];
|
|
65
|
+
for (const point of path) {
|
|
66
|
+
const xy = at(point.azimuth, point.altitude);
|
|
67
|
+
const last = runs[runs.length - 1];
|
|
68
|
+
if (last && last.lit === point.sunlit)
|
|
69
|
+
last.points.push(xy);
|
|
70
|
+
else {
|
|
71
|
+
const join = last?.points[last.points.length - 1];
|
|
72
|
+
runs.push({ lit: point.sunlit, points: join ? [join, xy] : [xy] });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
for (const run of runs) {
|
|
76
|
+
if (run.points.length < 2)
|
|
77
|
+
continue;
|
|
78
|
+
const d = run.points.map(([x, y], i) => `${i === 0 ? "M" : "L"}${round(x)} ${round(y)}`).join("");
|
|
79
|
+
parts.push(run.lit
|
|
80
|
+
? `<path d="${d}" fill="none" style="stroke:var(--chikyu-pass-lit,#ffb454)" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" data-lit="true"/>`
|
|
81
|
+
: `<path d="${d}" fill="none" stroke="#8fa1b8" stroke-width="2" stroke-dasharray="4 5" stroke-linecap="round" stroke-linejoin="round" data-lit="false"/>`);
|
|
82
|
+
}
|
|
83
|
+
// the marks: start, highest, end, each with its word and (when given) its time, set inward from the mark so they stay inside the dome
|
|
84
|
+
const mark = (kind, azimuth, altitude) => {
|
|
85
|
+
const [x, y] = at(azimuth, altitude);
|
|
86
|
+
const inward = kind === "highest" ? [0, -1] : [(c - x) / Math.max(1, Math.hypot(c - x, c - y)), (c - y) / Math.max(1, Math.hypot(c - x, c - y))];
|
|
87
|
+
const shape = kind === "highest"
|
|
88
|
+
? `<circle cx="${round(x)}" cy="${round(y)}" r="5" fill="#ffffff" stroke="#10192b" stroke-width="1.5"/>`
|
|
89
|
+
: kind === "start"
|
|
90
|
+
? `<circle cx="${round(x)}" cy="${round(y)}" r="4.5" fill="#6cd0ff" stroke="#10192b" stroke-width="1.5"/>`
|
|
91
|
+
: `<rect x="${round(x - 4.5)}" y="${round(y - 4.5)}" width="9" height="9" fill="#ff8a65" stroke="#10192b" stroke-width="1.5"/>`;
|
|
92
|
+
const tx = x + inward[0] * (font * 1.7);
|
|
93
|
+
const ty = y + inward[1] * (font * 1.7) + font * 0.35;
|
|
94
|
+
const time = options.times?.[kind];
|
|
95
|
+
parts.push(`<g data-mark="${kind}">${shape}<text x="${round(tx)}" y="${round(ty)}" text-anchor="middle" font-size="${round(font * 0.85)}" fill="#e6edf7" stroke="#10192b" stroke-width="2.5" paint-order="stroke">${escape(words[kind])}${time ? ` ${escape(time)}` : ""}</text></g>`);
|
|
96
|
+
};
|
|
97
|
+
mark("start", pass.start.azimuth, pass.start.altitude);
|
|
98
|
+
mark("highest", pass.highest.azimuth, pass.highest.altitude);
|
|
99
|
+
mark("end", pass.end.azimuth, pass.end.altitude);
|
|
100
|
+
const description = options.description ??
|
|
101
|
+
`${pass.name} crosses the sky from ${pass.start.compass} to ${pass.end.compass}, highest ${Math.round(pass.highest.altitude)} degrees up in the ${pass.highest.compass}.`;
|
|
102
|
+
return (`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${size} ${size}" width="${size}" height="${size}" role="img" aria-label="${escape(description)}" ` +
|
|
103
|
+
`font-family="system-ui, sans-serif">${parts.join("")}</svg>`);
|
|
104
|
+
}
|