Skip to content

Commit a6d970f

Browse files
committed
fix getOrbitTrack computing start times for previous and next orbits
1 parent faeeeea commit a6d970f

File tree

2 files changed

+66
-15
lines changed

2 files changed

+66
-15
lines changed

__tests__/sgp4.js

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ const tleStr = `ISS (ZARYA)
2929
1 25544U 98067A 17206.51418347 .00001345 00000-0 27503-4 0 9993
3030
2 25544 51.6396 207.2711 0006223 72.3525 71.7719 15.54224686 67715`;
3131

32+
const proxima2 = `PROXIMA II
33+
1 43696U 18088G 21129.25183023 .00001100 00000-0 45012-4 0 9990
34+
2 43696 85.0353 221.2351 0019671 158.2001 202.0083 15.22857070138409`;
35+
3236
const tleArr = tleStr.split("\n");
3337

3438
describe("getSatelliteInfo", () => {
@@ -190,8 +194,22 @@ describe("getGroundTracks", () => {
190194
const firstLng = coords[0][0][0];
191195
const lastLng = coords[0][coords[0].length - 1][0];
192196

193-
expect(firstLng).toBe(-179.95882804237493);
194-
expect(lastLng).toBe(179.96378140395484);
197+
expect(firstLng).toBe(-179.9996305056871);
198+
expect(lastLng).toBe(179.9939688862288);
199+
});
200+
201+
test("2", async () => {
202+
const timestamp = 1620583838732;
203+
const result = await getGroundTracks({
204+
tle: proxima2,
205+
startTimeMS: timestamp
206+
});
207+
expect(result[0][0][0]).toBeCloseTo(-179.65354);
208+
expect(result[0][0][1]).toBeCloseTo(84.57353);
209+
expect(result[1][0][0]).toBeCloseTo(-179.68200);
210+
expect(result[1][0][1]).toBeCloseTo(85.06215);
211+
expect(result[2][0][0]).toBeCloseTo(-179.89417);
212+
expect(result[2][0][1]).toBeCloseTo(84.63849);
195213
});
196214
});
197215

@@ -209,8 +227,22 @@ describe("getGroundTracksSync", () => {
209227

210228
const firstLng = coords[0][0][0];
211229
const lastLng = coords[0][coords[0].length - 1][0];
212-
expect(firstLng).toBe(-179.95882804237493);
213-
expect(lastLng).toBe(179.96378140395484);
230+
expect(firstLng).toBe(-179.9996305056871);
231+
expect(lastLng).toBe(179.9939688862288);
232+
});
233+
234+
test("2", async () => {
235+
const timestamp = 1620583838732;
236+
const result = await getGroundTracksSync({
237+
tle: proxima2,
238+
optionalTimeMS: timestamp
239+
});
240+
expect(result[0][0][0]).toBeCloseTo(-179.65354);
241+
expect(result[0][0][1]).toBeCloseTo(84.57353);
242+
expect(result[1][0][0]).toBeCloseTo(-179.68200);
243+
expect(result[1][0][1]).toBeCloseTo(85.06215);
244+
expect(result[2][0][0]).toBeCloseTo(-179.89417);
245+
expect(result[2][0][1]).toBeCloseTo(84.63849);
214246
});
215247
});
216248

@@ -236,6 +268,20 @@ describe("problematic TLES (geosync, decayed)", () => {
236268
expect(result).toEqual(expectedResult);
237269
});
238270

271+
test("getLastAntemeridianCrossingTimeMS 2", () => {
272+
const timestamp = 1620579956208;
273+
const result = getLastAntemeridianCrossingTimeMS(proxima2, timestamp);
274+
const expectedResult = -1;
275+
expect((timestamp - result) / 1000 / 60).toBeCloseTo(72.50);
276+
});
277+
278+
test("getLastAntemeridianCrossingTimeMS 3", () => {
279+
const timestamp = 1620581856788;
280+
const result = getLastAntemeridianCrossingTimeMS(proxima2, timestamp);
281+
const expectedResult = -1;
282+
expect((timestamp - result) / 1000 / 60).toBeCloseTo(8.976);
283+
});
284+
239285
test("getOrbitTrack", async () => {
240286
const timestamp = 1501039265000;
241287
const result = await getOrbitTrack({

src/sgp4.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ export function getLastAntemeridianCrossingTimeMS(tle, timeMS) {
233233

234234
const time = timeMS || Date.now();
235235

236-
let step = 1000 * 60 * 10;
236+
let step = 1000 * 60 * 3;
237237
let curLngLat = [];
238238
let lastLngLat = [];
239239
let curTimeMS = time;
@@ -247,10 +247,13 @@ export function getLastAntemeridianCrossingTimeMS(tle, timeMS) {
247247

248248
didCrossAntemeridian = _crossesAntemeridian(lastLngLat[0], curLng);
249249
if (didCrossAntemeridian) {
250-
// Back up a bit, then keep halving the step increment till we get close enough.
250+
// Back up to before we crossed the line.
251251
curTimeMS += step;
252-
step = step > 20000 ? 20000 : step / 2;
252+
253+
// Keep narrowing by halving increments.
254+
step = step / 2;
253255
} else {
256+
// Didn't cross yet, so keep incrementing.
254257
curTimeMS -= step;
255258
lastLngLat = curLngLat;
256259
}
@@ -543,17 +546,19 @@ export function getGroundTracks({
543546
]);
544547
}
545548

549+
/**
550+
* Buffer time that will be sure to place us well within the previous or next orbit.
551+
*/
552+
const bufferMS = orbitTimeMS / 5;
553+
546554
const lastOrbitStartMS = getLastAntemeridianCrossingTimeMS(
547555
parsedTLE,
548-
549-
// TODO: fix this magic math
550-
curOrbitStartMS - 10000
556+
curOrbitStartMS - bufferMS
551557
);
558+
552559
const nextOrbitStartMS = getLastAntemeridianCrossingTimeMS(
553560
parsedTLE,
554-
555-
// TODO: fix this magic math
556-
curOrbitStartMS + orbitTimeMS + 1000 * 60 * 30
561+
curOrbitStartMS + orbitTimeMS + bufferMS
557562
);
558563

559564
const groundTrackPromises = [
@@ -637,11 +642,11 @@ export function getGroundTracksSync({
637642

638643
const lastOrbitStartMS = getLastAntemeridianCrossingTimeMS(
639644
parsedTLE,
640-
curOrbitStartMS - 10000
645+
curOrbitStartMS - (orbitTimeMS / 5)
641646
);
642647
const nextOrbitStartMS = getLastAntemeridianCrossingTimeMS(
643648
parsedTLE,
644-
curOrbitStartMS + orbitTimeMS + 1000 * 60 * 30
649+
curOrbitStartMS + orbitTimeMS + (orbitTimeMS / 5)
645650
);
646651

647652
const orbitStartTimes = [

0 commit comments

Comments
 (0)