Skip to content

Commit

Permalink
fix: 🐛 fix issues with jday calculations in Sgp4
Browse files Browse the repository at this point in the history
  • Loading branch information
thkruz committed Jul 29, 2022
1 parent d796c65 commit 7ceb301
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 70 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ test/performance
test/satellite/transform*

# Future assembly work
asc
asc
.asc
43 changes: 27 additions & 16 deletions src/sgp4/sgp4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3209,9 +3209,9 @@ class Sgp4 {
year = satrec.epochyr + 1900;
}

const { mon, day, hr, minute, sec } = Sgp4.days2mdhms(year, satrec.epochdays);
const { mon, day, hr, min, sec } = Sgp4.days2mdhms(year, satrec.epochdays);

const jdayRes = Sgp4.jday(year, mon, day, hr, minute, sec);
const jdayRes = Sgp4.jday(year, mon, day, hr, min, sec);

satrec.jdsatepoch = jdayRes.jd + jdayRes.jdFrac;

Expand Down Expand Up @@ -3861,20 +3861,31 @@ class Sgp4 {
* ---------------------------------------------------------------------------
*/
public static jday(
year: number,
mon: number,
day: number,
hr: number,
minute: number,
sec: number,
year: number | Date,
mon = 0,
day = 0,
hr = 0,
min = 0,
sec = 0,
ms = 0,
): { jd: number; jdFrac: number } {
if (year instanceof Date) {
mon = year.getUTCMonth() + 1;
day = year.getUTCDate();
hr = year.getUTCHours();
min = year.getUTCMinutes();
sec = year.getUTCSeconds();
ms = year.getUTCMilliseconds();
year = year.getUTCFullYear();
}

let jd =
367.0 * year -
Math.floor(7 * (year + Math.floor((mon + 9) / 12.0)) * 0.25) +
Math.floor((275 * mon) / 9.0) +
day +
1721013.5; // Use - 678987.0 to go to mjd directly
let jdFrac = (sec + minute * 60.0 + hr * 3600.0) / 86400.0;
let jdFrac = (ms / 1000 + sec + min * 60.0 + hr * 3600.0) / 86400.0;

// Check that the day and fractional day are correct
if (Math.abs(jdFrac) > 1.0) {
Expand Down Expand Up @@ -3933,7 +3944,7 @@ class Sgp4 {
mon: number;
day: number;
hr: number;
minute: number;
min: number;
sec: number;
} {
const lmonth = [31, year % 4 === 0 ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
Expand Down Expand Up @@ -3963,14 +3974,14 @@ class Sgp4 {
const hr = Math.floor(temp);

temp = (temp - hr) * 60.0;
const minute = Math.floor(temp);
const sec = (temp - minute) * 60.0;
const min = Math.floor(temp);
const sec = (temp - min) * 60.0;

return {
mon,
day,
hr,
minute,
min,
sec,
};
} // Days2mdhms
Expand Down Expand Up @@ -4020,7 +4031,7 @@ class Sgp4 {
public static invjday(
jd: number,
jdfrac: number,
): { year: number; mon: number; day: number; hr: number; minute: number; sec: number } {
): { year: number; mon: number; day: number; hr: number; min: number; sec: number } {
let leapyrs;
let days;

Expand Down Expand Up @@ -4055,14 +4066,14 @@ class Sgp4 {
}

/* ----------------- find remaining data ------------------------- */
const { mon, day, hr, minute, sec } = Sgp4.days2mdhms(year, days + jdfrac);
const { mon, day, hr, min, sec } = Sgp4.days2mdhms(year, days + jdfrac);

return {
year,
mon,
day,
hr,
minute,
min,
sec,
};
} // Invjday
Expand Down
11 changes: 11 additions & 0 deletions test/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"baseUrl": "../",
"paths": {
"@lib/*": ["lib/*"],
"@dist/*": ["dist/*"],
"@src/*": ["src/*"],
"@test/*": ["test/*"]
}
}
}
68 changes: 17 additions & 51 deletions test/sgp4/legacy/ext.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import { Sgp4 } from '@lib/ootk';
import { compareVectors } from '@test/lib/compareVectors';

describe.skip('Julian date / time', () => {
let now = new Date();
describe('Julian date / time', () => {
// Use number of milliseconds since epoch instead of local year, month, day, etc for consistency across machines
let now = new Date(1661400000000);

beforeAll(() => {
now = new Date();
now = new Date(1661400000000);
});

describe('jday & invjday', () => {
Expand Down Expand Up @@ -77,60 +78,25 @@ describe.skip('Julian date / time', () => {
expect(jday1).not.toEqual(jday2);
});

it('invjday gives the same result as date and array', () => {
const jd = Sgp4.jday(now);
const date = Sgp4.invjday(jd);
const dateArray = Sgp4.invjday(jd, true);

expect(date.getUTCFullYear()).toEqual(dateArray[0]);
expect(date.getUTCMonth() + 1).toEqual(dateArray[1]);
expect(date.getUTCDate()).toEqual(dateArray[2]);
expect(date.getUTCHours()).toEqual(dateArray[3]);
expect(date.getUTCMinutes()).toEqual(dateArray[4]);
expect(date.getUTCSeconds()).toEqual(dateArray[5]);
it('invjday gives different results with jdfrac', () => {
const { jd } = Sgp4.jday(now);
const jday = Sgp4.invjday(jd);
const jdayWFrac = Sgp4.invjday(jd, 10);

expect(jday).not.toEqual(jdayWFrac);
});

it('date to jday and inverse conversion', () => {
const jd = Sgp4.jday(now);
const { jd, jdFrac } = Sgp4.jday(now);
const expected = (now.getTime() - now.getMilliseconds()) / 1000;
// Allow a single millisecond margin of error
const time = Sgp4.invjday(jd, jdFrac);
const date = new Date();

expect(Sgp4.invjday(jd).getTime() / 1000).toBeCloseTo(expected, -1);
});
});
date.setUTCFullYear(time.year, time.mon - 1, time.day);
date.setUTCHours(time.hr, time.min, time.sec);

it('gstime gives the same result with different arguments describing the same time', () => {
expect(Sgp4.gstime(now)).toEqual(
Sgp4.gstime(
now.getUTCFullYear(),
now.getUTCMonth() + 1,
now.getUTCDate(),
now.getUTCHours(),
now.getUTCMinutes(),
now.getUTCSeconds(),
now.getUTCMilliseconds(),
),
);
});

it('propagation gives the same result with different arguments describing the same time', () => {
const date = new Date(2016, 7, 22);
const tleLine1 = '1 27424U 02022A 16235.86686911 .00000105 00000-0 33296-4 0 9990';
const tleLine2 = '2 27424 98.2022 175.3843 0001285 39.9183 23.2024 14.57119903760831';
const satrec = Sgp4.createSatrec(tleLine1, tleLine2);

const propagationByDate = Sgp4.propagate(satrec, date);
const propagationByDateItems = Sgp4.propagate(
satrec,
date.getUTCFullYear(),
date.getUTCMonth() + 1,
date.getUTCDate(),
date.getUTCHours(),
date.getUTCMinutes(),
date.getUTCSeconds(),
);

compareVectors(propagationByDate.position, propagationByDateItems.position);
compareVectors(propagationByDate.velocity, propagationByDateItems.velocity);
expect(date.getTime() / 1000).toBeCloseTo(expected, -1);
});
});
});
2 changes: 1 addition & 1 deletion test/sgp4/sgp4-full-cov.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe('Test vector equations in SGP4', () => {
expect(Sgp4.invjday(2450000, 0)).toEqual({
day: 9,
hr: 12,
minute: 0,
min: 0,
mon: 10,
sec: 0,
year: 1995,
Expand Down
10 changes: 9 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
"declaration": true,
// "noImplicitAny": true,
// "removeComments": true,
"baseUrl": ".",
"paths": {
"@lib/*": ["lib/*"],
"@dist/*": ["dist/*"],
"@src/*": ["src/*"],
"@test/*": ["test/*"]
},
"allowJs": true,
"experimentalDecorators": true,
"rootDir": "./src",
"sourceMap": true,
Expand All @@ -17,7 +25,7 @@
"noEmitOnError": true,
"types": []
},
"include": ["./src/**/*.ts", "src/objects/star.ts.ignore", "src/objects/.star.ts"],
"include": ["./src/**/*.ts"],
"filesGlob": ["./src/**/*.ts"],
"ignorePatterns": ["./asc/**/*.ts"]
}

0 comments on commit 7ceb301

Please sign in to comment.