Module 12 · Lesson 2
Propagation and Pass Prediction
This lesson builds the core of the tracker with the open-source satellite.js library - SGP4 propagation, geodetic position, look angles, pass search, error handling and tests against known cases.
Learning objectives
After this lesson you will be able to:
- Use satellite.js to propagate an element set and convert the result to geodetic coordinates and look angles
- Find passes by stepping through time and refine rise and set times by bisection
- Handle propagation failures, malformed input and stale element sets, and test results against known cases
The library and its pipeline
Module 04 explained what SGP4 does and why its element sets cannot be used with any other model. This lesson uses an existing implementation. satellite.js is an open-source JavaScript library, released under the MIT license, whose SGP4 code follows the published reference implementation. The examples here were run against version 7.2.0. Check the function names against the version you install.
The pipeline has four steps.
- Build a satellite record.
twoline2satrec(line1, line2)parses a two-line element set and performs SGP4's one-time initialization. For element sets in OMM form, which the previous lesson recommended for new data,json2satrectakes an object of OMM fields and produces the same kind of record. Build the record once and reuse it. - Propagate.
propagate(satrec, date)returns position in kilometers and velocity in kilometers per second, ornullon failure. The library labels the frame ECI. More precisely it is TEME, the inertial frame SGP4 works in. - Rotate to the Earth.
gstime(date)returns Greenwich mean sidereal time in radians, the angle the Earth has turned.eciToEcfapplies that rotation, andeciToGeodeticreturns latitude and longitude in radians and height in kilometers. - Compute look angles.
ecfToLookAngles(observer, positionEcf)returns azimuth and elevation in radians and range in kilometers.
Step 3 is a simplification. A single rotation ignores polar motion and treats UTC as if it were UT1, which Module 03 distinguished. The resulting error is at most a few hundred meters, small beside the accuracy of the element sets. Work that needs better should use a vetted astrodynamics library with Earth orientation data.
import {
twoline2satrec, propagate, gstime, eciToGeodetic, eciToEcf, ecfToLookAngles,
degreesToRadians, radiansToDegrees, degreesLat, degreesLong,
} from 'satellite.js';
// ISS element set from the satellite.js README. Its epoch is 5 June 2019.
const LINE1 = '1 25544U 98067A 19156.50900463 .00003075 00000-0 59442-4 0 9992';
const LINE2 = '2 25544 51.6433 59.2583 0008217 16.4489 347.6017 15.51174618173442';
const satrec = twoline2satrec(LINE1, LINE2);
// Observer angles in radians, height in km.
const observer = {
latitude: degreesToRadians(36.9613),
longitude: degreesToRadians(-122.0308),
height: 0.37,
};
function observe(date: Date) {
const result = propagate(satrec, date); // position in km, TEME frame
if (result === null) throw new Error(`SGP4 failed with error code ${satrec.error}`);
const gmst = gstime(date); // sidereal time, radians
const geodetic = eciToGeodetic(result.position, gmst);
const look = ecfToLookAngles(observer, eciToEcf(result.position, gmst));
return {
latitudeDeg: degreesLat(geodetic.latitude),
longitudeDeg: degreesLong(geodetic.longitude),
altitudeKm: geodetic.height,
azimuthDeg: radiansToDegrees(look.azimuth),
elevationDeg: radiansToDegrees(look.elevation),
rangeKm: look.rangeSat,
};
}
console.log(observe(new Date('2019-06-05T18:00:00Z')));
The output, with values rounded:
{ latitudeDeg: -51.79, longitudeDeg: 165.19, altitudeKm: 432.67,
azimuthDeg: 218.94, elevationDeg: -53.21, rangeKm: 10709.41 }
The station is south of New Zealand and far below the horizon of the observer in California. One detail is worth noticing. The latitude is 51.79 degrees south, although the orbit's inclination is 51.64 degrees. That is not an error. The library returns geodetic latitude, measured against the ellipsoid, which can exceed the inclination by a fraction of a degree.
Finding passes
Module 05 worked through the look-angle geometry by hand and described the pass search in four stages: step, detect, refine and characterize. Here the library does the geometry, and the code below implements the first three stages, refining each crossing by bisection.
const elevationAt = (ms: number): number => observe(new Date(ms)).elevationDeg;
// lowMs and highMs bracket one horizon crossing. Bisect to within half a second.
function refineCrossing(lowMs: number, highMs: number): number {
const aboveAtLow = elevationAt(lowMs) > 0;
while (highMs - lowMs > 500) {
const midMs = (lowMs + highMs) / 2;
if ((elevationAt(midMs) > 0) === aboveAtLow) lowMs = midMs;
else highMs = midMs;
}
return (lowMs + highMs) / 2;
}
function findPasses(startMs: number, endMs: number, stepMs = 30_000) {
const passes: { rise: Date; set: Date; maxElevationDeg: number }[] = [];
let rise: Date | null = null;
let maxElevationDeg = 0;
let previous = elevationAt(startMs);
for (let t = startMs + stepMs; t <= endMs; t += stepMs) {
const current = elevationAt(t);
if (previous <= 0 && current > 0) rise = new Date(refineCrossing(t - stepMs, t));
if (rise !== null) maxElevationDeg = Math.max(maxElevationDeg, current);
if (previous > 0 && current <= 0 && rise !== null) {
passes.push({ rise, set: new Date(refineCrossing(t - stepMs, t)), maxElevationDeg });
rise = null;
maxElevationDeg = 0;
}
previous = current;
}
return passes;
}
const start = Date.parse('2019-06-05T18:00:00Z');
for (const pass of findPasses(start, start + 24 * 3600 * 1000)) {
console.log(pass.rise.toISOString(), pass.set.toISOString(), pass.maxElevationDeg.toFixed(1));
}
Over 24 hours it prints seven passes, as rise time, set time and maximum elevation in degrees:
2019-06-05T20:01:24.140Z 2019-06-05T20:10:06.796Z 11.1
2019-06-05T21:36:40.546Z 2019-06-05T21:47:23.203Z 60.9
2019-06-05T23:14:42.890Z 2019-06-05T23:23:47.109Z 11.9
2019-06-06T00:53:27.890Z 2019-06-06T01:01:00.234Z 6.2
2019-06-06T02:30:41.015Z 2019-06-06T02:39:46.640Z 11.8
2019-06-06T04:07:03.984Z 2019-06-06T04:17:52.265Z 59.0
2019-06-06T05:44:17.109Z 2019-06-06T05:53:13.359Z 11.8
The passes last between about 7.5 and 11 minutes, which is what a low orbit should produce. The code has limits that a finished predictor must remove:
- It uses the geometric horizon. Replace the comparisons with zero by comparisons with the observer's elevation mask.
- Maximum elevation is the highest sampled value. Rerunning with a one-second step changed these figures by less than 0.1 degree. A search around the peak would find the culmination time that the contract requires.
- A pass already in progress at the start is skipped, and a grazing pass shorter than one step can be missed.
- It says nothing about whether the satellite can be seen. That depends on the observer being in darkness while the satellite is in sunlight.
Errors and stale elements
Three behaviors of the library, observed in version 7.2.0, shape the error handling.
Numerical failure returns null. The reason is an error code in satrec.error. Propagating the 2019 element set above to the year 2060 returned null with code 1, which means the mean eccentricity went out of range. The type definitions declare the result as possibly null, so strict TypeScript refuses to compile code that skips the check. In the application, the throw in observe becomes the propagation-failed value from the previous lesson's contract.
Malformed input is not rejected. Passing two arbitrary strings to twoline2satrec raised no exception. Propagation then returned coordinates that were all not-a-number, with an error code of zero. The application must validate lines before use, by length, line number, matching catalog numbers and the checksum from Module 04, and must test outputs with Number.isFinite.
Staleness is invisible. SGP4 will propagate a month-old element set and return confident numbers. Module 04 gave typical error growth and worked through an incident caused by a nine-day-old element set. The check needs the epoch, which is available as a Julian date in satrec.jdsatepoch. The expression (satrec.jdsatepoch - 2440587.5) * 86400000 converts it to milliseconds since the Unix epoch, and for this element set gives 12:12:58 UTC on 5 June 2019.
Testing against known cases
- Pin reference outputs. The paper that defines the modern SGP4 reference code, Revisiting Spacetrack Report #3, was published with test element sets and expected results, and libraries are tested against them. The application's own tests should store a few outputs for a fixed element set and time, so that a library upgrade that changes results is noticed.
- Check invariants. Coordinates are finite. Altitude is within the expected band. Rise precedes set. Elevation at rise and set is near the mask. The latitude example above shows why each invariant needs thought.
- Compare independently. Run the same element set through another SGP4 implementation and compare pass times. Agreement within a second or two is realistic.
- Test the search separately. Give the pass finder a synthetic elevation function whose crossings are known exactly.
A practical example: the marker that vanished
One morning the map shows no satellite and the pass table is empty. No error has been logged.
The developer inspects the cached element set and finds a fragment of an error page where the two lines should be. The data source had answered the refresh job with an error status and a short message. The job ignored the status and stored the body. twoline2satrec accepted the text without complaint, every propagation returned not-a-number coordinates, and every comparison with not-a-number is false, so the pass finder never saw a rise.
Every function had returned successfully. This is the failure Module 11 warned about: bad data that passes through code with no defensive checks. The fix has four parts. The job treats any response other than success as a failure, keeps the previous element set and raises an alert, which is also what the source's usage policy asks for. Ingestion validates the element set before storing it. The propagator tests its outputs with Number.isFinite and returns propagation-failed. A stored copy of the bad response becomes a regression test.
The orbit code now produces positions and passes. The final lesson draws them, delivers the application and closes the course.
Key takeaways
- The pipeline has four steps - build a satellite record, propagate to a time for a position in the TEME frame, rotate by sidereal time to Earth-fixed and geodetic coordinates, and compute look angles from the observer.
- Passes are found by sampling elevation at a coarse step and bisecting each horizon crossing. This gives sub-second rise and set times for a few thousand propagations a day.
- The library reports numerical failure by returning null with an error code, but it does not reject malformed input and it cannot know that an element set is stale. Those checks belong to the application.
- Tests pin known outputs, check physical invariants and compare with an independent implementation. Some obvious invariants are wrong, such as latitude never exceeding inclination.
Knowledge check
Check your understanding. These questions are not graded and you can retry as often as you like.
0 of 3 questions answered.
Glossary
Satellite record
The object an SGP4 library builds from an element set, holding the parsed elements and the constants the model computes once at initialization. In satellite.js it is called a satrec.
TEME
True Equator, Mean Equinox. The Earth-centered inertial frame in which SGP4 produces position and velocity.
Greenwich mean sidereal time
The angle through which the Earth has rotated relative to the inertial frame at a given instant. It is used to rotate inertial coordinates into Earth-fixed ones. Abbreviated GMST.
Look angles
The direction and distance to a satellite from an observer, given as azimuth, elevation and range.
Bisection
A root-finding method that repeatedly halves an interval known to contain a crossing, keeping the half in which the crossing lies.
References and further reading
- satellite.js (opens in a new tab)satellite.js project (GitHub)
- Revisiting Spacetrack Report #3 (AIAA 2006-6753) (opens in a new tab)CelesTrak
- CelesTrak: A New Way to Obtain GP Data (opens in a new tab)CelesTrak
Finished this lesson?
Loading your progress…
Lesson version 1.0.0.