Skip to content

Commit

Permalink
fix: 🚑 fix rae error using milliseconds instead of seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
thkruz committed Jan 16, 2024
1 parent 36896c4 commit 9553288
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/body/Earth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ export class Earth {
);

// / Calculate mean motion _(rad/s)_ from a given [semimajorAxis] _(km)_.
static smaToMeanMotion(semimajorAxis: number): number {
return Math.sqrt(Earth.mu / (semimajorAxis * semimajorAxis * semimajorAxis));
static smaToMeanMotion(semimajorAxis: Kilometers): RadiansPerSecond {
return Math.sqrt(Earth.mu / (semimajorAxis * semimajorAxis * semimajorAxis)) as RadiansPerSecond;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/objects/Satellite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
Radians,
RaeVec3,
SatelliteRecord,
Seconds,
TleLine1,
TleLine2,
} from '../types/types';
Expand Down Expand Up @@ -187,7 +188,7 @@ export class Satellite extends BaseObject {
toRae(observer: GroundObject, date: Date = new Date()): RAE {
const rae = this.rae(observer, date);
const rae2 = this.rae(observer, new Date(date.getTime() + 1000));
const epoch = new EpochUTC(date.getTime());
const epoch = new EpochUTC(date.getTime() / 1000 as Seconds);
const rangeRate = rae2.rng - rae.rng;
const azimuthRate = rae2.az - rae.az;
const elevationRate = rae2.el - rae.el;
Expand Down Expand Up @@ -257,7 +258,7 @@ export class Satellite extends BaseObject {
const p = pv.position as EciVec3;
const v = pv.velocity as EciVec3<KilometersPerSecond>;

const epoch = new EpochUTC(date.getTime());
const epoch = new EpochUTC(date.getTime() / 1000 as Seconds);
const pos = new Vector3D(p.x, p.y, p.z);
const vel = new Vector3D(v.x, v.y, v.z);

Expand Down
4 changes: 2 additions & 2 deletions src/time/Epoch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* SOFTWARE.
*/

import { Seconds } from 'src/main';
import { Seconds } from '../main';
import { secondsPerDay } from '../utils/constants';

// / Base class for [Epoch] data.
Expand All @@ -30,7 +30,7 @@ export class Epoch {
* Create a new [Epoch] object given the number of seconds elapsed since the
* [posix] epoch _(`1970-01-01T00:00:00.000`)_ in the [Epoch] time scale.
*/
constructor(public posix: number) {
constructor(public posix: Seconds) {
if (posix < 0) {
throw new Error('Epoch cannot be negative');
}
Expand Down
24 changes: 12 additions & 12 deletions src/time/EpochUTC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,25 @@ type DateToPosixParams = {

export class EpochUTC extends Epoch {
static now() {
return new EpochUTC(new Date().getTime() / 1000);
return new EpochUTC(new Date().getTime() / 1000 as Seconds);
}

static fromDate({ year, month, day, hour = 0, minute = 0, second = 0 }: FromDateParams) {
return new EpochUTC(EpochUTC.dateToPosix_({ year, month, day, hour, minute, second }));
}

static fromDateTime(dt: Date) {
return new EpochUTC(dt.getTime() / 1000);
return new EpochUTC(dt.getTime() / 1000 as Seconds);
}

static fromDateTimeString(dateTimeString: string): EpochUTC {
const dts = dateTimeString.trim().toUpperCase().endsWith('Z') ? dateTimeString : `${dateTimeString}Z`;

return new EpochUTC(new Date(dts).getTime() / 1000);
return new EpochUTC(new Date(dts).getTime() / 1000 as Seconds);
}

static fromJ2000TTSeconds(seconds: Seconds): EpochUTC {
const tInit = new EpochUTC(seconds + 946728000);
const tInit = new EpochUTC(seconds + 946728000 as Seconds);
const ls = DataHandler.getInstance().getLeapSeconds(tInit.toJulianDate());

return tInit.roll(-32.184 - ls as Seconds);
Expand All @@ -85,11 +85,11 @@ export class EpochUTC extends Epoch {
// Add day - 1 days in milliseconds to the epoch.
const dts = new Date(`${year}-01-01T${timeField}Z`).getTime() + (day - 1) * MS_PER_DAY;

return new EpochUTC(dts / 1000);
return new EpochUTC(dts / 1000 as Seconds);
}

roll(seconds: Seconds): EpochUTC {
return new EpochUTC(this.posix + seconds);
return new EpochUTC(this.posix + seconds as Seconds);
}

toMjd(): number {
Expand All @@ -103,20 +103,20 @@ export class EpochUTC extends Epoch {
toTAI(): EpochTAI {
const ls = DataHandler.getInstance().getLeapSeconds(this.toJulianDate());

return new EpochTAI(this.posix + ls);
return new EpochTAI(this.posix + ls as Seconds);
}

toTT(): EpochTT {
return new EpochTT(this.toTAI().posix + 32.184);
return new EpochTT(this.toTAI().posix + 32.184 as Seconds);
}

toTDB(): EpochTDB {
const tt = this.toTT();
const tTT = tt.toJulianCenturies();
const mEarth = (357.5277233 + 35999.05034 * tTT) * DEG2RAD;
const seconds = 0.001658 * Math.sin(mEarth) + 0.00001385 * Math.sin(2 * mEarth);
const seconds = 0.001658 * Math.sin(mEarth) + 0.00001385 * Math.sin(2 * mEarth) as Seconds;

return new EpochTDB(tt.posix + seconds);
return new EpochTDB(tt.posix + seconds as Seconds);
}

toGPS(): EpochGPS {
Expand Down Expand Up @@ -168,7 +168,7 @@ export class EpochUTC extends Epoch {
return EpochUTC.dayOfYearLookup_[dex][month - 1] + day - 1;
}

private static dateToPosix_({ year, month, day, hour, minute, second }: DateToPosixParams): number {
private static dateToPosix_({ year, month, day, hour, minute, second }: DateToPosixParams): Seconds {
const days = EpochUTC.dayOfYear_(year, month, day);
const yearMod = year - 1900;

Expand All @@ -181,6 +181,6 @@ export class EpochUTC extends Epoch {
Math.floor((yearMod - 1) / 100) * 86400 +
Math.floor((yearMod + 299) / 400) * 86400 +
second
);
) as Seconds;
}
}
22 changes: 11 additions & 11 deletions test/objects/__snapshots__/Satellite.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ Object {

exports[`Satellite should calculate and return Geodetic coordinates when given a date 1`] = `
Geodetic {
"alt": 405.17244322847,
"lat": 0.1099327125671112,
"lon": 0.2809557331563047,
"alt": 409.48725329355693,
"lat": -0.48221646502840027,
"lon": 1.2198848056557416,
}
`;

exports[`Satellite should calculate and return ITRF coordinates when called 1`] = `
ITRF {
"epoch": EpochUTC {
"posix": 1705109326817,
"posix": 1705109326.817,
},
"position": Vector3D {
"x": 6478.244529235784,
"y": 1869.552512878403,
"z": 739.5500168212324,
"x": 2068.607191044621,
"y": 5650.9777806539605,
"z": -3130.036328555523,
},
"velocity": Vector3D {
"x": 2.366559347644857,
"y": -7.704390508005883,
"z": -1.227328372730736,
"x": -5.491548321368525,
"y": -0.6798748760128108,
"z": -4.863052489363518,
},
}
`;
Expand All @@ -64,7 +64,7 @@ RAE {
"elRad": -1.2837885129141846,
"elRateRad": -0.03103994353789119,
"epoch": EpochUTC {
"posix": 1705109326817,
"posix": 1705109326.817,
},
"rng": 12650.301530537545,
"rngRate": 1.8799097918581538,
Expand Down

0 comments on commit 9553288

Please sign in to comment.