Skip to content

Commit

Permalink
test: ✅ fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thkruz committed Jan 11, 2024
1 parent 7f5d7c7 commit 4c2d701
Show file tree
Hide file tree
Showing 16 changed files with 616 additions and 482 deletions.
125 changes: 125 additions & 0 deletions examples/satellite-js-migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* eslint-disable max-len */
/* eslint-disable @typescript-eslint/no-unused-vars */
import {
calcGmst,
DEG2RAD,
Degrees,
GroundPosition,
Kilometers,
lla2eci,
Radians,
Satellite,
Sgp4,
TleLine1,
TleLine2,
} from '../src/ootk-core';

// Sample TLE
const tle1 = '1 25544U 98067A 19156.50900463 .00003075 00000-0 59442-4 0 9992' as TleLine1;
const tle2 = '2 25544 51.6433 59.2583 0008217 16.4489 347.6017 15.51174618173442' as TleLine2;

// Initialize a Satellite Object
const satellite = new Satellite({
tle1,
tle2,
});

// You can still propagate a satellite using time since epoch (in minutes), but it's not recommended.
const timeSinceTleEpochMinutes = 10;
const positionAndVelocity = Sgp4.propagate(satellite.satrec, timeSinceTleEpochMinutes);

// Use a Date object instead
const positionAndVelocity2 = satellite.eci(new Date(2024, 0, 1));
// Or use the current time
const positionAndVelocity3 = satellite.eci();

/*
* The position_velocity result is a key-value pair of ECI coordinates.
* These are the base results from which all other coordinates are derived.
*/
const positionEci = positionAndVelocity.position; // positionAndVelocity might be false
const velocityEci = positionAndVelocity.velocity; // typescript will error on this code

/*
* Unlike satellite.js using the eci method will ALWAYS return a result or throw
* an error if it can't propagate the satellite. No more checking for false and trying to handle
* a combined object and boolean result.
*/
const positionEci2 = satellite.eci().position; // This is correctly typed

// Set the Observer at 122.03 West by 36.96 North, in DEGREES (because who likes working in radians?)
const observer = new GroundPosition({
lon: -122.0308 as Degrees,
lat: 36.9613422 as Degrees,
alt: 0.37 as Kilometers,
});

/**
* You can still calculate GMST if you want to, but unlike satellite.js it's not required.
*/
const { gmst, j } = calcGmst(new Date());

// You can get ECF, Geodetic, Look Angles, and Doppler Factor.
const positionEcf = satellite.ecf();
const observerEcf = observer.ecf();
const positionGd = satellite.lla();
const lookAngles = satellite.rae(observer);
// This never worked in satellite.js, but it does now!
const dopplerFactor = satellite.dopplerFactor(observer);

/**
* The coordinates are all stored in strongly typed key-value pairs.
* ECI and ECF are accessed by `x`, `y`, `z` properties.
*
* satellite.js generates Property 'x' does not exist on type 'boolean | { x: number; y: number; z: number; }'.
*/
const position = satellite.eci().position;
const satelliteX = position.x; // This is typed as Kilometers
const satelliteY = position.y; // to prevent you from accidentally
const satelliteZ = position.z; // mixing Meters with Kilometers.

// Look Angles may be accessed by `azimuth`, `elevation`, `range` properties.
const azimuth = lookAngles.azimuth; // Typed as Degrees
const elevation = lookAngles.elevation; // Typed as Degrees
const rangeSat = lookAngles.range; // Typed as Kilometers

// Geodetic coords are accessed via `longitude`, `latitude`, `height`.
const longitude = positionGd.lon; // Longitude is in Degrees
const latitude = positionGd.lat; // Latitude is in Degrees
const height = positionGd.alt; // Height is in Kilometers

// Convert the DEGREES to RADIANS if you want.
const longitudeRad = longitude * DEG2RAD;
const latitudeRad = latitude * DEG2RAD;
/**
* In TypeScript you need to label your units.
* This will help prevent you from passing the wrong units into functions.
*/
const longitudeRad2 = (longitude * DEG2RAD) as Radians;
const latitudeRad2 = (latitude * DEG2RAD) as Radians;

// lla2eci(positionGd, gmst); // Throws an error: Argument of type 'LlaVec3<Degrees, Kilometers>' is not assignable to parameter of type 'LlaVec3<Radians, Kilometers>'.
lla2eci(observer.llaRad(), gmst); // This is correctly typed

// eslint-disable-next-line no-console
console.log(
positionEci2,
positionEcf,
observerEcf,
positionGd,
lookAngles,
dopplerFactor,
satelliteX,
satelliteY,
satelliteZ,
azimuth,
elevation,
rangeSat,
longitude,
latitude,
height,
longitudeRad,
latitudeRad,
longitudeRad2,
latitudeRad2,
);
69 changes: 0 additions & 69 deletions examples/sensor.ts

This file was deleted.

22 changes: 22 additions & 0 deletions examples/transforms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable no-console */
import { Degrees, ecf2rae, EcfVec3, GroundPosition, Kilometers, lla2ecf, lla2sez, RAD2DEG } from '../src/ootk-core';

const observer = new GroundPosition({
name: 'ground-position',
lat: (0.7287584767123405 * RAD2DEG) as Degrees,
lon: (-1.2311404365114507 * RAD2DEG) as Degrees,
alt: 0.060966 as Kilometers,
});

const ecf = {
x: 1838.5578358534067,
y: -4971.972919387344,
z: 4466.101983887215,
} as EcfVec3<Kilometers>;

const llaRad = observer.llaRad();

console.log(llaRad); // Good
console.log(lla2ecf(observer)); // Good
console.log(lla2sez(llaRad, ecf));
console.log(ecf2rae(observer, ecf));
52 changes: 26 additions & 26 deletions src/objects/Satellite.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { dopplerFactor } from './../utils/functions';
/**
* @author Theodore Kruczek.
* @description Orbital Object ToolKit (OOTK) is a collection of tools for working
Expand Down Expand Up @@ -57,10 +58,9 @@ import { Vector3D } from '../operations/Vector3D';
import { Sgp4 } from '../sgp4/sgp4';
import { EpochUTC } from '../time/EpochUTC';
import { Tle } from '../tle/tle';
import { ecf2rae, eci2ecf, eci2lla } from '../transforms';
import { Utils } from '../utils/utils';
import { ecf2rae, eci2ecf, eci2lla, jday } from '../transforms';
import { BaseObject } from './BaseObject';
import { Sensor } from './Sensor';
import { GroundPosition } from './GroundPosition';

/**
* TODO: Reduce unnecessary calls to calculateTimeVariables using optional
Expand Down Expand Up @@ -175,8 +175,8 @@ export class Satellite extends BaseObject {
*
* @optimized
*/
az(sensor: Sensor, date: Date = this.time): Degrees {
return (this.raeOpt(sensor, date).az * RAD2DEG) as Degrees;
az(observer: GroundPosition, date: Date = this.time): Degrees {
return (this.raeOpt(observer, date).az * RAD2DEG) as Degrees;
}

/**
Expand All @@ -185,8 +185,8 @@ export class Satellite extends BaseObject {
*
* @expanded
*/
rae(sensor: Sensor, date: Date = this.time): RAE {
const rae = this.raeOpt(sensor, date);
rae(observer: GroundPosition, date: Date = this.time): RAE {
const rae = this.raeOpt(observer, date);
const epoch = new EpochUTC(date.getTime());

return new RAE(epoch, rae.rng, (rae.az * DEG2RAD) as Radians, (rae.el * DEG2RAD) as Radians);
Expand All @@ -197,18 +197,18 @@ export class Satellite extends BaseObject {
*
* @optimized
*/
getEcf(date: Date = this.time): EcfVec3<Kilometers> {
ecf(date: Date = this.time): EcfVec3<Kilometers> {
const { gmst } = Satellite.calculateTimeVariables(date);

return eci2ecf(this.getEci(date).position, gmst);
return eci2ecf(this.eci(date).position, gmst);
}

/**
* Calculates ECI position at a given time.
*
* @optimized
*/
getEci(date: Date = this.time): PosVel<Kilometers> {
eci(date: Date = this.time): PosVel<Kilometers> {
const { m } = Satellite.calculateTimeVariables(date, this.satrec);
const pv = Sgp4.propagate(this.satrec, m);

Expand Down Expand Up @@ -249,16 +249,16 @@ export class Satellite extends BaseObject {
*
* @optimized
*/
el(sensor: Sensor, date: Date = this.time): Degrees {
return (this.raeOpt(sensor, date).el * RAD2DEG) as Degrees;
el(observer: GroundPosition, date: Date = this.time): Degrees {
return (this.raeOpt(observer, date).el * RAD2DEG) as Degrees;
}

/**
* Calculates LLA position at a given time.
*/
lla(date: Date = this.time): LlaVec3<Degrees, Kilometers> {
const { gmst } = Satellite.calculateTimeVariables(date, this.satrec);
const pos = this.getEci(date).position;
const pos = this.eci(date).position;

return eci2lla(pos, gmst);
}
Expand All @@ -280,27 +280,27 @@ export class Satellite extends BaseObject {
*
* @optimized
*/
raeOpt(sensor: Sensor, date: Date = this.time): RaeVec3<Kilometers, Degrees> {
raeOpt(observer: GroundPosition, date: Date = this.time): RaeVec3<Kilometers, Degrees> {
const { gmst } = Satellite.calculateTimeVariables(date, this.satrec);
const eci = this.getEci(date).position;
const eci = this.eci(date).position;
const ecf = eci2ecf(eci, gmst);

const lla = {
lat: (sensor.lat * DEG2RAD) as Radians,
lon: (sensor.lon * DEG2RAD) as Radians,
alt: sensor.alt,
};

return ecf2rae(lla, ecf);
return ecf2rae(observer, ecf);
}

/**
* Returns the range of the satellite from the given sensor at the specified time.
*
* @optimized
*/
range(sensor: Sensor, date: Date = this.time): Kilometers {
return this.raeOpt(sensor, date).rng;
range(observer: GroundPosition, date: Date = this.time): Kilometers {
return this.raeOpt(observer, date).rng;
}

dopplerFactor(observer: GroundPosition, date?: Date): number {
const position = this.eci(date);

return dopplerFactor(observer.eci(date), position.position, position.velocity);
}

/**
Expand All @@ -309,7 +309,7 @@ export class Satellite extends BaseObject {
* This method changes the position and time properties of the satellite object.
*/
propagateTo(date: Date): this {
const pv = this.getEci(date);
const pv = this.eci(date);

this.position = pv.position as EciVec3;
this.time = date;
Expand All @@ -328,7 +328,7 @@ export class Satellite extends BaseObject {
satrec?: SatelliteRecord,
): { gmst: GreenwichMeanSiderealTime; m: number; j: number } {
const j =
Utils.jday(
jday(
date.getUTCFullYear(),
date.getUTCMonth() + 1,
date.getUTCDate(),
Expand Down
5 changes: 2 additions & 3 deletions src/objects/Star.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ import { MILLISECONDS_TO_DAYS } from '../utils/constants';

import { Celestial } from '../body';
import { Sgp4 } from '../sgp4/sgp4';
import { ecf2eci, rae2ecf } from '../transforms/transforms';
import { Utils } from '../utils/utils';
import { ecf2eci, jday, rae2ecf } from '../transforms/transforms';
import { BaseObject } from './BaseObject';

export class Star extends BaseObject {
Expand Down Expand Up @@ -92,7 +91,7 @@ export class Star extends BaseObject {

private static calculateTimeVariables_(date: Date): { gmst: GreenwichMeanSiderealTime; j: number } {
const j =
Utils.jday(
jday(
date.getUTCFullYear(),
date.getUTCMonth() + 1,
date.getUTCDate(),
Expand Down
1 change: 1 addition & 0 deletions src/objects/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { BaseObject } from './BaseObject';
export { GroundPosition } from './GroundPosition';
export { Satellite } from './Satellite';
export { Sensor } from './Sensor';
export { Star } from './Star';
Loading

0 comments on commit 4c2d701

Please sign in to comment.