Skip to content

Commit

Permalink
feat: ✨ add Sat class
Browse files Browse the repository at this point in the history
  • Loading branch information
thkruz committed Jul 26, 2022
1 parent 19b70f8 commit 281a9c5
Show file tree
Hide file tree
Showing 8 changed files with 1,612 additions and 1,114 deletions.
8 changes: 4 additions & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"keyword-spacing": "error",
"line-comment-position": "off",
"linebreak-style": ["error", "unix"],
"lines-around-comment": "error",
"lines-around-comment": "off",
"lines-around-directive": "error",
"lines-between-class-members": "error",
"max-classes-per-file": "error",
Expand Down Expand Up @@ -119,7 +119,7 @@
"no-lone-blocks": "error",
"no-lonely-if": "error",
"no-loop-func": "error",
"no-loss-of-precision": "error",
"no-loss-of-precision": "off",
"no-magic-numbers": "off",
"no-mixed-operators": "off",
"no-mixed-requires": "error",
Expand All @@ -128,7 +128,7 @@
"no-multi-str": "error",
"no-multiple-empty-lines": "error",
"no-native-reassign": "error",
"no-negated-condition": "error",
"no-negated-condition": "off",
"no-negated-in-lhs": "error",
"no-nested-ternary": "error",
"no-new": "error",
Expand All @@ -140,7 +140,7 @@
"no-octal-escape": "error",
"no-param-reassign": "error",
"no-path-concat": "error",
"no-plusplus": "error",
"no-plusplus": "off",
"no-process-env": "error",
"no-process-exit": "error",
"no-promise-executor-return": "error",
Expand Down
1 change: 1 addition & 0 deletions src/ootk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ export { Sgp4 } from './sgp4';
export { Transforms } from './transforms';
export { Utils } from './utils';
export { Tle } from './tle';
export { Sat } from './sat';
export * from './types';
138 changes: 138 additions & 0 deletions src/sat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { EcfVec3, EciVec3, LlaVec3, SatelliteRecord, StateVector, TleLine1, TleLine2 } from './types';
import { MILLISECONDS_PER_DAY, MINUTES_PER_DAY } from './utils/constants';
import { Sgp4 } from './sgp4';
import { Tle } from './tle';
import { Transforms } from './transforms';
import { Utils } from './utils';

type GreenwichMeanSiderealTime = number;

export class Sat {
public satNum: number;

public satrec: SatelliteRecord;

public options: any;

public intlDes: string;

public epochYear: number;

public epochDay: number;

public meanMoDev1: number;

public meanMoDev2: number;

public bstar: number;

public inclination: number;

public raan: number;

public eccentricity: number;

public argOfPerigee: number;

public meanAnomaly: number;

public meanMotion: number;

public period: number;

constructor(tle1: TleLine1, tle2: TleLine2, options) {
const tleData = Tle.parseTle(tle1, tle2);

this.satNum = tleData.satNum;
this.intlDes = tleData.intlDes;
this.epochYear = tleData.epochYear;
this.epochDay = tleData.epochDay;
this.meanMoDev1 = tleData.meanMoDev1;
this.meanMoDev2 = tleData.meanMoDev2;
this.bstar = tleData.bstar;
this.inclination = tleData.inclination;
this.raan = tleData.raan;
this.eccentricity = tleData.eccentricity;
this.argOfPerigee = tleData.argOfPerigee;
this.meanAnomaly = tleData.meanAnomaly;
this.meanMotion = tleData.meanMotion;
this.period = 1440 / this.meanMotion;

this.satrec = Sgp4.createSatrec(tle1, tle2);
this.options = options;
}

/**
* Calculates position and velocity in ECI coordinates at a given time.
* @param {Date} date Date to calculate the state vector for
* @returns {StateVector} State vector for the given date
*/
public getStateVec(date: Date): StateVector {
const { m } = Sat.calculateTimeVariables(date, this.satrec);

return Sgp4.propagate(this.satrec, m);
}

/**
* Calculates ECI position at a given time.
* @param {Date} date Date to calculate
* @returns {EciVec3} ECI position vector
*/
public getEci(date: Date): EciVec3 {
const { m } = Sat.calculateTimeVariables(date, this.satrec);
const eci = Sgp4.propagate(this.satrec, m).position;

return eci ? (eci as EciVec3) : { x: 0, y: 0, z: 0 };
}

/**
* Calculates ECF position at a given time.
* @param {Date} date Date to calculate
* @returns {EcfVec3} ECF position vector
*/
public getEcf(date: Date): EcfVec3 {
const { m, gmst } = Sat.calculateTimeVariables(date, this.satrec);
const eci = (Sgp4.propagate(this.satrec, m).position as EciVec3) || { x: 0, y: 0, z: 0 };

return Transforms.eci2ecf(eci, gmst);
}

/**
* Calculates LLA position at a given time.
* @param {Date} date Date to calculate
* @returns {LlaVec3} LLA position vector
*/
public getLla(date: Date): LlaVec3 {
const { m, gmst } = Sat.calculateTimeVariables(date, this.satrec);
const eci = (Sgp4.propagate(this.satrec, m).position as EciVec3) || { x: 0, y: 0, z: 0 };

return Transforms.eci2lla(eci, gmst);
}

/**
* Calculates the time variables for a given date relative to the TLE epoch.
* @param {Date} date Date to calculate
* @param {SatelliteRecord} satrec Satellite orbital information
* @returns {{m: number, gmst: GreenwichMeanSiderealTime, j: number}} Time variables
*/
private static calculateTimeVariables(
date: Date,
satrec?: SatelliteRecord,
): { gmst: GreenwichMeanSiderealTime; m: number; j: number } {
const j =
Utils.jday(
date.getUTCFullYear(),
date.getUTCMonth() + 1,
date.getUTCDate(),
date.getUTCHours(),
date.getUTCMinutes(),
date.getUTCSeconds(),
) +
date.getUTCMilliseconds() * MILLISECONDS_PER_DAY;
const gmst = Sgp4.gstime(j);

const m = satrec ? (j - satrec.jdsatepoch) * MINUTES_PER_DAY : null;

return { gmst, m, j };
}
}
Loading

0 comments on commit 281a9c5

Please sign in to comment.