Skip to content

Commit

Permalink
fix: 🏷️ add strict typing for units
Browse files Browse the repository at this point in the history
  • Loading branch information
thkruz committed May 16, 2023
1 parent a9917c3 commit ff98aa0
Show file tree
Hide file tree
Showing 18 changed files with 3,764 additions and 3,678 deletions.
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "ootk",
"version": "1.8.1",
"version": "2.0.0",
"description": "Orbital Object Toolkit including SGP4 Propagator and Coordinate Transforms",
"main": "dist/ootk.js",
"scripts": {
"build": "node ./scripts/cleanup.js && tsc -p tsconfig.json && node --openssl-legacy-provider node_modules/webpack/bin/webpack.js --config ./scripts/webpack.js",
"build:legacy": "node ./scripts/cleanup.js && tsc && node node_modules/webpack/bin/webpack.js --config ./scripts/webpack.js",
"asbuild:debug": "asc asc/assembly/index.ts --target debug",
"asbuild:release": "asc asc/assembly/index.ts --target release",
"asbuild:debug": "asc src/sgp4/asc/assembly/index.ts --target debug",
"asbuild:release": "asc src/sgp4/asc/assembly/index.ts --target release",
"asbuild": "npm run asbuild:debug && npm run asbuild:release",
"lint": "eslint src",
"lint:fix": "eslint src --fix",
Expand Down Expand Up @@ -59,7 +59,7 @@
"@types/node-fetch": "^2.6.2",
"@typescript-eslint/eslint-plugin": "^5.54.0",
"@typescript-eslint/parser": "^5.54.0",
"assemblyscript": "^0.20.13",
"assemblyscript": "^0.20.19",
"auto-changelog": "^2.2.1",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.2.2",
Expand Down
10 changes: 5 additions & 5 deletions src/objects/base-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* to ground and space based objects.
*
* @license AGPL-3.0-or-later
* @Copyright (c) 2020-2022 Theodore Kruczek
* @Copyright (c) 2020-2023 Theodore Kruczek
*
* Orbital Object ToolKit is free software: you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free Software
Expand All @@ -21,7 +21,7 @@
* Orbital Object ToolKit. If not, see <http://www.gnu.org/licenses/>.
*/

import { EciVec3, SpaceObjectType } from '../types/types';
import { EciVec3, Kilometers, SpaceObjectType } from '../types/types';

interface ObjectInfo {
name?: string;
Expand All @@ -41,9 +41,9 @@ export class BaseObject {
this.name = info.name || 'Unknown';

this.position = info.position || {
x: 0,
y: 0,
z: 0,
x: <Kilometers>0,
y: <Kilometers>0,
z: <Kilometers>0,
}; // Default to the center of the earth until position is calculated

this.time = info.time || new Date();
Expand Down
2 changes: 1 addition & 1 deletion src/objects/detailed-sat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* properties for categorizing and filtering satellites.
*
* @license AGPL-3.0-or-later
* @Copyright (c) 2020-2022 Theodore Kruczek
* @Copyright (c) 2020-2023 Theodore Kruczek
*
* Orbital Object ToolKit is free software: you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free Software
Expand Down
35 changes: 28 additions & 7 deletions src/objects/sat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* to earth based sensors and other orbital objects.
*
* @license AGPL-3.0-or-later
* @Copyright (c) 2020-2022 Theodore Kruczek
* @Copyright (c) 2020-2023 Theodore Kruczek
*
* Orbital Object ToolKit is free software: you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free Software
Expand All @@ -26,6 +26,7 @@ import {
EcfVec3,
EciVec3,
GreenwichMeanSiderealTime,
Kilometers,
LlaVec3,
RaeVec3,
SatelliteRecord,
Expand Down Expand Up @@ -99,7 +100,11 @@ export class Sat extends SpaceObject {

public propagateTo(date: Date): Sat {
const { m } = Sat.calculateTimeVariables(date, this.satrec);
const eci = (Sgp4.propagate(this.satrec, m).position as EciVec3) || { x: 0, y: 0, z: 0 };
const eci = (Sgp4.propagate(this.satrec, m).position as EciVec3) || {
x: <Kilometers>0,
y: <Kilometers>0,
z: <Kilometers>0,
};

this.position = eci;
this.time = date;
Expand All @@ -125,9 +130,13 @@ export class Sat extends SpaceObject {
*/
public getEci(date: Date = this.time): EciVec3 {
const { m } = Sat.calculateTimeVariables(date, this.satrec);
const eci = Sgp4.propagate(this.satrec, m).position;
const eci = (Sgp4.propagate(this.satrec, m).position as EciVec3) || {
x: <Kilometers>0,
y: <Kilometers>0,
z: <Kilometers>0,
};

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

/**
Expand All @@ -137,7 +146,11 @@ export class Sat extends SpaceObject {
*/
public getEcf(date: Date = this.time): 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 };
const eci = (Sgp4.propagate(this.satrec, m).position as EciVec3) || {
x: <Kilometers>0,
y: <Kilometers>0,
z: <Kilometers>0,
};

return Transforms.eci2ecf(eci, gmst);
}
Expand All @@ -149,14 +162,22 @@ export class Sat extends SpaceObject {
*/
public getLla(date: Date = this.time): 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 };
const eci = (Sgp4.propagate(this.satrec, m).position as EciVec3) || {
x: <Kilometers>0,
y: <Kilometers>0,
z: <Kilometers>0,
};

return Transforms.eci2lla(eci, gmst);
}

public getRae(sensor: Sensor, date: Date = this.time): RaeVec3 {
const { m, gmst } = Sat.calculateTimeVariables(date, this.satrec);
const eci = (Sgp4.propagate(this.satrec, m).position as EciVec3) || { x: 0, y: 0, z: 0 };
const eci = (Sgp4.propagate(this.satrec, m).position as EciVec3) || {
x: <Kilometers>0,
y: <Kilometers>0,
z: <Kilometers>0,
};
const ecf = Transforms.eci2ecf(eci, gmst);

return Transforms.ecf2rae({ lat: sensor.lat, lon: sensor.lon, alt: sensor.alt }, ecf);
Expand Down
54 changes: 27 additions & 27 deletions src/objects/sensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @file The Sensor class is used for creating ground based observers.
*
* @license AGPL-3.0-or-later
* @Copyright (c) 2020-2022 Theodore Kruczek
* @Copyright (c) 2020-2023 Theodore Kruczek
*
* Orbital Object ToolKit is free software: you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free Software
Expand All @@ -20,7 +20,7 @@
* Orbital Object ToolKit. If not, see <http://www.gnu.org/licenses/>.
*/

import { Degrees, Kilometer, RaeVec3, SpaceObjectType } from '../types/types';
import { Degrees, Kilometers, Radians, RaeVec3, SpaceObjectType } from '../types/types';

import { BaseObject } from './base-object';
import { RAD2DEG } from '../utils/constants';
Expand All @@ -29,15 +29,15 @@ import { Sat } from './sat';
interface ObjectInfo {
name?: string;
type?: SpaceObjectType;
lat: number;
lon: number;
alt: number;
lat: Radians;
lon: Radians;
alt: Kilometers;
minAz?: Degrees;
maxAz?: Degrees;
minEl?: Degrees;
maxEl?: Degrees;
minRng?: Kilometer;
maxRng?: Kilometer;
minRng?: Kilometers;
maxRng?: Kilometers;
}

export enum PassType {
Expand All @@ -50,24 +50,24 @@ export enum PassType {
type Lookangles = {
type: PassType;
time: Date;
az: number;
el: number;
rng: number;
maxElPass?: number;
az: Degrees;
el: Degrees;
rng: Kilometers;
maxElPass?: Degrees;
};

const TAU = Math.PI * 2;

export class Sensor extends BaseObject {
public lat: number;
public lon: number;
public alt: number;
public lat: Radians;
public lon: Radians;
public alt: Kilometers;
public minAz: Degrees;
public maxAz: Degrees;
public minEl: Degrees;
public maxEl: Degrees;
public minRng: Kilometer;
public maxRng: Kilometer;
public minRng: Kilometers;
public maxRng: Kilometers;

/**
* * name: Name as a string - OPTIONAL
Expand Down Expand Up @@ -113,7 +113,7 @@ export class Sensor extends BaseObject {
this.minAz = info.minAz;
} else if (typeof info.minAz === 'undefined') {
// Default is a telescope
this.minAz = 0;
this.minAz = <Degrees>0;
} else {
throw new Error('Invalid minimum azimuth - must be between 0 and 360');
}
Expand All @@ -122,7 +122,7 @@ export class Sensor extends BaseObject {
this.maxAz = info.maxAz;
} else if (typeof info.maxAz === 'undefined') {
// Default is a telescope
this.maxAz = 360;
this.maxAz = <Degrees>360;
} else {
throw new Error('Invalid maximum azimuth - must be between 0 and 360');
}
Expand All @@ -131,7 +131,7 @@ export class Sensor extends BaseObject {
this.minEl = info.minEl;
} else if (typeof info.minEl === 'undefined') {
// Default is a telescope
this.minEl = 0;
this.minEl = <Degrees>0;
} else {
throw new Error('Invalid minimum elevation - must be between 0 and 90');
}
Expand All @@ -140,7 +140,7 @@ export class Sensor extends BaseObject {
this.maxEl = info.maxEl;
} else if (typeof info.maxEl === 'undefined') {
// Default is a telescope
this.maxEl = 90;
this.maxEl = <Degrees>90;
} else {
throw new Error('Invalid maximum elevation - must be between 0 and 180');
}
Expand All @@ -149,15 +149,15 @@ export class Sensor extends BaseObject {
this.minRng = info.minRng;
} else if (typeof info.minRng === 'undefined') {
// Default is a telescope
this.minRng = 0;
this.minRng = <Kilometers>0;
} else {
throw new Error('Invalid minimum range - must be greater than 0');
}
if (info.maxRng >= 0) {
this.maxRng = info.maxRng;
} else if (typeof info.maxRng === 'undefined') {
// Default is a telescope
this.maxRng = 50000; // arbitrary large number
this.maxRng = <Kilometers>50000; // arbitrary large number
} else {
throw new Error('Invalid maximum range - must be greater than 0');
}
Expand Down Expand Up @@ -221,7 +221,7 @@ export class Sensor extends BaseObject {

public calculatePasses(planningInterval: number, sat: Sat, date: Date = this.time) {
let isInViewLast = false;
let maxElThisPass = 0;
let maxElThisPass = <Degrees>0;
const msnPlanPasses: Lookangles[] = [];
const startTime = date.getTime();

Expand All @@ -230,8 +230,8 @@ export class Sensor extends BaseObject {
const rae = this.getRae(sat, curTime);

// Radians to Degrees
rae.az *= 360 / TAU;
rae.el *= 360 / TAU;
rae.az = <Degrees>((rae.az * 360) / TAU);
rae.el = <Degrees>((rae.el * 360) / TAU);

const isInView = this.isRaeInFov(rae);

Expand All @@ -244,7 +244,7 @@ export class Sensor extends BaseObject {

const type = Sensor.getPassType(isInView, isInViewLast);

maxElThisPass = Math.max(maxElThisPass, rae.el);
maxElThisPass = <Degrees>Math.max(maxElThisPass, rae.el);

if (type === PassType.ENTER || type === PassType.EXIT) {
const pass = <Lookangles>{
Expand All @@ -261,7 +261,7 @@ export class Sensor extends BaseObject {
}

msnPlanPasses.push(pass);
maxElThisPass = 0;
maxElThisPass = <Degrees>0;
}

isInViewLast = isInView;
Expand Down
2 changes: 1 addition & 1 deletion src/objects/space-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* do not apply to ground based objects.
*
* @license AGPL-3.0-or-later
* @Copyright (c) 2020-2022 Theodore Kruczek
* @Copyright (c) 2020-2023 Theodore Kruczek
*
* Orbital Object ToolKit is free software: you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free Software
Expand Down
Loading

0 comments on commit ff98aa0

Please sign in to comment.