Skip to content

Commit

Permalink
refactor: ♻️ standardize rng az el syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
thkruz committed Jan 15, 2024
1 parent f27b230 commit 59f7f53
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 97 deletions.
142 changes: 73 additions & 69 deletions src/observation/RAE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,15 @@ import { angularDistance } from '../utils/functions';
export class RAE {
constructor(
public epoch: EpochUTC,
public range: Kilometers,
public azimuth: Radians,
public elevation: Radians,
/**
* The range rate of the satellite relative to the observer in kilometers
* per second.
*/
public rangeRate?: number,
/**
* The azimuth rate of the satellite relative to the observer in radians per
* second.
*/
public azimuthRate?: number,
/**
* The elevation rate of the satellite relative to the observer in radians
* per second.
*/
public elevationRate?: number,
public rng: Kilometers,
public azRad: Radians,
public elRad: Radians,
/** The range rate of the satellite relative to the observer in kilometers per second. */
public rngRate?: number,
/** The azimuth rate of the satellite relative to the observer in radians per second. */
public azRateRad?: number,
/** The elevation rate of the satellite relative to the observer in radians per second. */
public elRateRad?: number,
) {
// Do nothing
}
Expand All @@ -61,23 +52,23 @@ export class RAE {
static fromDegrees(
epoch: EpochUTC,
range: Kilometers,
azimuthDegrees: Degrees,
elevationDegrees: Degrees,
azimuth: Degrees,
elevation: Degrees,
rangeRate?: number,
azimuthRateDegrees?: number,
elevationRateDegrees?: number,
azimuthRate?: number,
elevationRate?: number,
): RAE {
const azimuthRate = azimuthRateDegrees ? azimuthRateDegrees * DEG2RAD : undefined;
const elevationRate = elevationRateDegrees ? elevationRateDegrees * DEG2RAD : undefined;
const azimuthRateRad = azimuthRate ? azimuthRate * DEG2RAD : undefined;
const elevationRateRad = elevationRate ? elevationRate * DEG2RAD : undefined;

return new RAE(
epoch,
range,
(azimuthDegrees * DEG2RAD) as Radians,
(elevationDegrees * DEG2RAD) as Radians,
(azimuth * DEG2RAD) as Radians,
(elevation * DEG2RAD) as Radians,
rangeRate,
azimuthRate,
elevationRate,
azimuthRateRad,
elevationRateRad,
);
}

Expand Down Expand Up @@ -126,33 +117,46 @@ export class RAE {
elevationRate,
);
}
// / Azimuth _(°)_.
get azimuthDegrees(): number {
return this.azimuth * RAD2DEG;

/**
* Gets the azimuth in degrees.
* @returns The azimuth in degrees.
*/
get az(): Degrees {
return this.azRad * RAD2DEG as Degrees;
}

// / Elevation _(°)_.
get elevationDegrees(): number {
return this.elevation * RAD2DEG;
/**
* Gets the elevation angle in degrees.
* @returns The elevation angle in degrees.
*/
get el(): Degrees {
return this.elRad * RAD2DEG as Degrees;
}

// / Azimuth rate _(°/s)_.
get azimuthRateDegrees(): number | undefined {
return this.azimuthRate ? this.azimuthRate * RAD2DEG : undefined;
/**
* Gets the azimuth rate in degrees per second.
* @returns The azimuth rate in degrees per second, or undefined if it is not available.
*/
get azRate(): number | undefined {
return this.azRateRad ? this.azRateRad * RAD2DEG : undefined;
}

// / Elevation rate _(°/s)_.
get elevationRateDegrees(): number | undefined {
return this.elevationRate ? this.elevationRate * RAD2DEG : undefined;
/**
* Gets the elevation rate in degrees per second.
* @returns The elevation rate in degrees per second, or undefined if the elevation rate is not set.
*/
get elRate(): number | undefined {
return this.elRateRad ? this.elRateRad * RAD2DEG : undefined;
}

toString(): string {
return [
'[RazEl]',
` Epoch: ${this.epoch}`,
` Azimuth: ${this.azimuthDegrees.toFixed(4)}°`,
` Elevation: ${this.elevationDegrees.toFixed(4)}°`,
` Range: ${this.range.toFixed(3)} km`,
` Azimuth: ${this.az.toFixed(4)}°`,
` Elevation: ${this.el.toFixed(4)}°`,
` Range: ${this.rng.toFixed(3)} km`,
].join('\n');
}

Expand All @@ -162,24 +166,24 @@ export class RAE {
* An optional azimuth [az] _(rad)_ and elevation [el] _(rad)_ value can be
* passed to override the values contained in this observation.
* @param site The observer [site].
* @param az Azimuth _(rad)_.
* @param el Elevation _(rad)_.
* @param azRad Azimuth _(rad)_.
* @param elRad Elevation _(rad)_.
* @returns A [Vector3D] object.
*/
position(site: J2000, az?: Radians, el?: Radians): Vector3D<Kilometers> {
position(site: J2000, azRad?: Radians, elRad?: Radians): Vector3D<Kilometers> {
const ecef = site.toITRF();
const geo = ecef.toGeodetic();
const po2 = halfPi;
const newAz = az ?? this.azimuth;
const newEl = el ?? this.elevation;
const newAz = azRad ?? this.azRad;
const newEl = elRad ?? this.elRad;
const sAz = Math.sin(newAz);
const cAz = Math.cos(newAz);
const sEl = Math.sin(newEl);
const cEl = Math.cos(newEl);
const pSez = new Vector3D<Kilometers>(
(-this.range * cEl * cAz) as Kilometers,
(this.range * cEl * sAz) as Kilometers,
(this.range * sEl) as Kilometers,
(-this.rng * cEl * cAz) as Kilometers,
(this.rng * cEl * sAz) as Kilometers,
(this.rng * sEl) as Kilometers,
);
const rEcef = pSez
.rotY(-(po2 - geo.lat) as Radians)
Expand All @@ -199,30 +203,30 @@ export class RAE {
*/
toStateVector(site: J2000): J2000 {
// If the rates are not defined then assume stationary
this.rangeRate ??= 0;
this.elevationRate ??= 0;
this.azimuthRate ??= 0;
this.rngRate ??= 0;
this.elRateRad ??= 0;
this.azRateRad ??= 0;

const ecef = site.toITRF();
const geo = ecef.toGeodetic();
const po2 = halfPi;
const sAz = Math.sin(this.azimuth);
const cAz = Math.cos(this.azimuth);
const sEl = Math.sin(this.elevation);
const cEl = Math.cos(this.elevation);
const sAz = Math.sin(this.azRad);
const cAz = Math.cos(this.azRad);
const sEl = Math.sin(this.elRad);
const cEl = Math.cos(this.elRad);
const pSez = new Vector3D<Kilometers>(
(-this.range * cEl * cAz) as Kilometers,
(this.range * cEl * sAz) as Kilometers,
(this.range * sEl) as Kilometers,
(-this.rng * cEl * cAz) as Kilometers,
(this.rng * cEl * sAz) as Kilometers,
(this.rng * sEl) as Kilometers,
);
const pDotSez = new Vector3D<Kilometers>(
(-this.rangeRate * cEl * cAz +
this.range * sEl * cAz * this.elevationRate +
this.range * cEl * sAz * this.azimuthRate) as Kilometers,
(this.rangeRate * cEl * sAz -
this.range * sEl * sAz * this.elevationRate +
this.range * cEl * cAz * this.azimuthRate) as Kilometers,
(this.rangeRate * sEl + this.range * cEl * this.elevationRate) as Kilometers,
(-this.rngRate * cEl * cAz +
this.rng * sEl * cAz * this.elRateRad +
this.rng * cEl * sAz * this.azRateRad) as Kilometers,
(this.rngRate * cEl * sAz -
this.rng * sEl * sAz * this.elRateRad +
this.rng * cEl * cAz * this.azRateRad) as Kilometers,
(this.rngRate * sEl + this.rng * cEl * this.elRateRad) as Kilometers,
);
const pEcef = pSez.rotY(-(po2 - geo.lat) as Radians).rotZ(-geo.lon as Radians);
const pDotEcef = pDotSez.rotY(-(po2 - geo.lat) as Radians).rotZ(-geo.lon as Radians);
Expand All @@ -239,7 +243,7 @@ export class RAE {
* @returns The angular distance _(rad)_.
*/
angle(razel: RAE, method: AngularDistanceMethod = AngularDistanceMethod.Cosine): number {
return angularDistance(this.azimuth, this.elevation, razel.azimuth, razel.elevation, method);
return angularDistance(this.azRad, this.elRad, razel.azRad, razel.elRad, method);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions test/objects/__snapshots__/Satellite.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ Object {

exports[`Satellite should calculate and return RAE coordinates 1`] = `
RAE {
"azimuth": 1.3489093210949852,
"azimuthRate": 0.031044709157512784,
"elevation": -1.2837885129141846,
"elevationRate": -0.03103994353789119,
"azRad": 1.3489093210949852,
"azRateRad": 0.031044709157512784,
"elRad": -1.2837885129141846,
"elRateRad": -0.03103994353789119,
"epoch": EpochUTC {
"posix": 1705109326817,
},
"range": 12650.301530537545,
"rangeRate": 1.8799097918581538,
"rng": 12650.301530537545,
"rngRate": 1.8799097918581538,
}
`;

Expand Down
8 changes: 4 additions & 4 deletions test/observation/RAE.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ describe('RAE', () => {

// azimuthDegrees
it('should return the azimuth in degrees', () => {
expect(rae.azimuthDegrees).toMatchSnapshot();
expect(rae.az).toMatchSnapshot();
});

// elevationDegrees
it('should return the elevation in degrees', () => {
expect(rae.elevationDegrees).toMatchSnapshot();
expect(rae.el).toMatchSnapshot();
});

// azimuthRateDegrees
it('should return the azimuth rate in degrees', () => {
expect(rae.azimuthRateDegrees).toMatchSnapshot();
expect(rae.azRate).toMatchSnapshot();
});

// elevationRateDegrees
it('should return the elevation rate in degrees', () => {
expect(rae.elevationRateDegrees).toMatchSnapshot();
expect(rae.elRate).toMatchSnapshot();
});

// toString
Expand Down
36 changes: 18 additions & 18 deletions test/observation/__snapshots__/RAE.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,43 @@

exports[`RAE should be constructable 1`] = `
RAE {
"azimuth": 0.17453292519943295,
"azimuthRate": undefined,
"elevation": 0.3490658503988659,
"elevationRate": undefined,
"azRad": 0.17453292519943295,
"azRateRad": undefined,
"elRad": 0.3490658503988659,
"elRateRad": undefined,
"epoch": EpochUTC {
"posix": 1705109326.817,
},
"range": 1000,
"rangeRate": undefined,
"rng": 1000,
"rngRate": undefined,
}
`;

exports[`RAE should be constructable from degrees 1`] = `
RAE {
"azimuth": 0.17453292519943295,
"azimuthRate": undefined,
"elevation": 0.3490658503988659,
"elevationRate": undefined,
"azRad": 0.17453292519943295,
"azRateRad": undefined,
"elRad": 0.3490658503988659,
"elRateRad": undefined,
"epoch": EpochUTC {
"posix": 1705109326.817,
},
"range": 1000,
"rangeRate": undefined,
"rng": 1000,
"rngRate": undefined,
}
`;

exports[`RAE should return a RAE object from a state vector 1`] = `
RAE {
"azimuth": 4.202021284209623,
"azimuthRate": 0.00043952100964508644,
"elevation": 0.5130090849662382,
"elevationRate": -0.0003354532635408476,
"azRad": 4.202021284209623,
"azRateRad": 0.00043952100964508644,
"elRad": 0.5130090849662382,
"elRateRad": -0.0003354532635408476,
"epoch": EpochUTC {
"posix": 1705109326.817,
},
"range": 999.9999999999991,
"rangeRate": 0.5104272486229879,
"rng": 999.9999999999991,
"rngRate": 0.5104272486229879,
}
`;

Expand Down

0 comments on commit 59f7f53

Please sign in to comment.