Skip to content

A spy satellite simulator in your browser, except the data is real. Live open source spatial intelligence on a photorealistic 3D globe.

Merge branch 'main' into fix/tle-catalog-matching

Sameh Khamis155f5165d5ea
diff --cc src/sources/tle.js
index e46266244,f2b2ca3d7..61c409a2b
--- a/src/sources/tle.js
+++ b/src/sources/tle.js
@@@ -28,35 -21,47 +28,47 @@@ export function parseTleText(text)
return result;
}
+ // Alpha-5: a letter A–Z, skipping I and O, then exactly four digits.
+ const ALPHA5 = /^[A-HJ-NP-Z]\d{4}$/;
+ const DIGITS = /^\d+$/;
+
/**
- * The catalog number an Alpha-5 field stands for, or null for any other field.
+ * The catalog number a catalog field stands for, or null for any other field.
*
- * Since the catalog passed 99999 (July 2026) a TLE writes the numbers above it
- * in Alpha-5: a letter A–Z, skipping I and O, stands for 10–33 in the
- * ten-thousands place, so 100000 is "A0000" and 339999 is "Z9999".
+ * A field is either plain digits or Alpha-5, the TLE spelling of numbers above
+ * 99999: the letter stands for 10–33 in the ten-thousands place, skipping I
+ * and O, so 100000 is "A0000" and 339999 is "Z9999". Anything else (a sign, a
+ * decimal point, hex, an embedded space, a lowercase or trailing letter) is
+ * not a catalog number.
+ * @param {string} field the field with any leading spaces already removed
+ * @returns {number | null}
*/
- function alpha5CatalogNumber(field) {
+ function decodeCatalogField(field) {
+ if (DIGITS.test(field)) return Number(field);
+ if (!ALPHA5.test(field)) return null;
const letter = field[0];
- if (
- letter >= 'A' &&
- letter <= 'Z' &&
- letter !== 'I' &&
- letter !== 'O' &&
- /^\d{4}$/.test(field.slice(1))
- ) {
- let tens = letter.charCodeAt(0) - 65 + 10;
- if (letter > 'I') tens -= 1;
- if (letter > 'O') tens -= 1;
- return tens * 10000 + Number(field.slice(1));
- }
- return null;
+ let tens = letter.charCodeAt(0) - 65 + 10;
+ if (letter > 'I') tens -= 1;
+ if (letter > 'O') tens -= 1;
+ return tens * 10000 + Number(field.slice(1));
+ }
+
+ /**
+ * NORAD catalog number of a satrec as a number, or NaN.
+ *
+ * satellite.js keeps the TLE's five-character field verbatim in
+ * `satrec.satnum`, and `Number("A0000")` is NaN, so every Alpha-5 satellite
+ * would share one NaN key. Digits of any length (an OMM satrec carries the
+ * number unpadded) and Alpha-5 decode; anything else, blank included, is NaN.
+ * @param {string} satnum
+ * @returns {number}
+ */
+ export function noradIdFromSatnum(satnum) {
+ return decodeCatalogField(String(satnum ?? '').trim()) ?? Number.NaN;
}
- /** The NORAD catalog number from a TLE line, or null. */
-/** The NORAD catalog number from TLE line 1 (columns 3–7), or null. */
-export function tleCatalogNumber(line1) {
- const field = String(line1).slice(2, 7);
++/** The NORAD catalog number from TLE line 1 or 2 (columns 3–7), or null. */
+export function tleCatalogNumber(line) {
+ const field = String(line).slice(2, 7);
- const alpha5 = alpha5CatalogNumber(field);
- if (alpha5 !== null) return alpha5;
- const number = Number.parseInt(field, 10);
- return Number.isInteger(number) ? number : null;
+ return decodeCatalogField(field.replace(/^ +/, ''));
}
diff --cc src/tools/queries/space.test.mjs
index dcde81cba,d0f2ffca8..5b0fa5837
--- a/src/tools/queries/space.test.mjs
+++ b/src/tools/queries/space.test.mjs
@@@ -51,40 -56,76 +56,110 @@@ for (const missingLine of [1, 2])
});
}
+test('TLE parsing rejects pairs with mismatched catalog numbers and resumes', () => {
+ const text = `SAT A
+1 25544U 98067A 24001.50000000 .00016717 00000-0 30270-3 0 9994
+2 48274 41.4697 175.8913 0005643 312.8370 141.6893 15.61287398150000
+SAT B
+1 48274U 21035A 24001.50000000 .00020137 00000-0 24481-3 0 9993
+2 48274 41.4697 175.8913 0005643 312.8370 141.6893 15.61287398150000
+`;
+ const entries = parseTleText(text);
+ assert.deepEqual(
+ entries.map((entry) => entry.name),
+ ['SAT B'],
+ );
+ assert.equal(tleCatalogNumber(entries[0].line1), 48274);
+ assert.equal(tleCatalogNumber(entries[0].line2), 48274);
+});
+
+test('TLE parsing accepts matching Alpha-5 pairs and rejects mismatched Alpha-5 pairs', () => {
+ const text = `SAT MISMATCH
+1 A0000U 98067A 24001.50000000 .00016717 00000-0 30270-3 0 9994
+2 A0001 41.4697 175.8913 0005643 312.8370 141.6893 15.61287398150000
+SAT ALPHA5
+1 A0000U 98067A 24001.50000000 .00016717 00000-0 30270-3 0 9994
+2 A0000 41.4697 175.8913 0005643 312.8370 141.6893 15.61287398150000
+`;
+ const entries = parseTleText(text);
+ assert.deepEqual(
+ entries.map((entry) => entry.name),
+ ['SAT ALPHA5'],
+ );
+ assert.equal(tleCatalogNumber(entries[0].line1), 100000);
+ assert.equal(tleCatalogNumber(entries[0].line2), 100000);
+});
+
+ test('Alpha-5 catalog numbers decode, skipping I and O', () => {
+ for (const [field, id] of [
+ ['A0000', 100000],
+ ['A0404', 100404],
+ ['H9999', 179999],
+ ['J0000', 180000],
+ ['N9999', 229999],
+ ['P0000', 230000],
+ ['T0449', 270449],
+ ['Z9999', 339999],
+ ]) {
+ assert.equal(noradIdFromSatnum(field), id, field);
+ assert.equal(tleCatalogNumber(`1 ${field}U`), id, field);
+ }
+ });
+
+ test('a digit catalog field reads as it did before', () => {
+ assert.equal(noradIdFromSatnum('25544'), 25544);
+ assert.equal(noradIdFromSatnum('00404'), 404);
+ assert.equal(noradIdFromSatnum(' 404'), 404);
+ assert.equal(tleCatalogNumber('1 25544U'), 25544);
+ assert.equal(tleCatalogNumber('1 404U'), 404);
+ // An OMM satrec carries the number unpadded, past five digits.
+ assert.equal(noradIdFromSatnum('100000'), 100000);
+ });
+
+ test('a malformed catalog field is no number', () => {
+ for (const field of [
+ 'I0000',
+ 'O0000',
+ 'a0404',
+ 'A00',
+ '2554A',
+ '255 4',
+ '-1234',
+ '0x1F0',
+ '1.5',
+ '1e3',
+ '',
+ ' ',
+ ]) {
+ assert.ok(Number.isNaN(noradIdFromSatnum(field)), JSON.stringify(field));
+ assert.equal(
+ tleCatalogNumber(`1 ${field.padEnd(5)}U`),
+ null,
+ JSON.stringify(field),
+ );
+ }
+ assert.ok(Number.isNaN(noradIdFromSatnum(undefined)));
+ });
+
+ test('a satellite numbered above 99999 keeps its number and is found by it', async () => {
+ // The ISS set with its catalog field in Alpha-5: A0000 is 100000.
+ const text = TLE.replaceAll('25544', 'A0000');
+ const [iss] = parseTleText(text);
+ const { satnum } = twoline2satrec(iss.line1, iss.line2);
+ assert.ok(Number.isNaN(Number(satnum))); // what the catalog builders used
+ assert.equal(noradIdFromSatnum(satnum), 100000);
+ const catalog = composeCatalog({
+ tools: coreTools,
+ services: { satellites: satellites(text), clock: CLOCK },
+ });
+ const result = await catalog.call('next_satellite_pass', {
+ location: { lat: 40.7, lon: -74 },
+ satellite: '100000',
+ });
+ assert.equal(result.data.satellite, 'ISS (ZARYA)');
+ assert.equal(result.data.norad, 100000);
+ });
+
test('the next pass defaults to the ISS and reports times, peak and direction', async () => {
const catalog = composeCatalog({
tools: coreTools,