Skip to content

Commit

Permalink
test: ✅ add tests for J2000
Browse files Browse the repository at this point in the history
  • Loading branch information
thkruz committed Jan 14, 2024
1 parent 49501a3 commit f77390c
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/coordinate/J2000.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,23 @@ import { ITRF } from './ITRF';
import { StateVector } from './StateVector';
import { TEME } from './TEME';

/**
* Represents a position and velocity in the J2000 coordinate system. This is an
* Earth-centered inertial (ECI) coordinate system.
*
* Commonly used ECI frame is defined with the Earth's Mean Equator and Mean
* Equinox (MEME) at 12:00 Terrestrial Time on 1 January 2000. It can be
* referred to as J2K, J2000 or EME2000. The x-axis is aligned with the mean
* vernal equinox. The z-axis is aligned with the Earth's rotation axis (or
* equivalently, the celestial North Pole) as it was at that time. The y-axis is
* rotated by 90° East about the celestial equator.
*
* @see https://en.wikipedia.org/wiki/Earth-centered_inertial
*/
export class J2000 extends StateVector {
/**
* Creates a J2000 instance from classical elements.
* Creates a J2000 coordinate from classical elements.
* @param elements The classical elements. @returns The J2000 coordinate.
*/
static fromClassicalElements(elements: ClassicalElements): J2000 {
const rv = elements.toPositionVelocity();
Expand All @@ -17,13 +31,16 @@ export class J2000 extends StateVector {

/**
* Gets the name of the coordinate system.
* @returns The name of the coordinate system.
*/
get name(): string {
return 'J2000';
}

/**
* Gets a value indicating whether the coordinate system is inertial.
* @returns A boolean value indicating whether the coordinate system is
* inertial.
*/
get inertial(): boolean {
return true;
Expand All @@ -34,6 +51,8 @@ export class J2000 extends StateVector {
* Reference Frame (ITRF).
*
* This is an ECI to ECF transformation.
*
* @returns The ITRF coordinates.
*/
toITRF(): ITRF {
const p = Earth.precession(this.epoch);
Expand All @@ -49,6 +68,11 @@ export class J2000 extends StateVector {
return new ITRF(this.epoch, rPEF, vPEF);
}

/**
* Converts the J2000 coordinate to the TEME coordinate.
*
* @returns The TEME coordinate.
*/
toTEME(): TEME {
const p = Earth.precession(this.epoch);
const n = Earth.nutation(this.epoch);
Expand Down
70 changes: 70 additions & 0 deletions test/coordinate/J2000.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Generated by CodiumAI

import { EpochUTC, ClassicalElements, Kilometers, Radians, J2000, Vector3D } from '../../src/main';
import { exampleDate } from '../lib/mockData';

describe('J2000', () => {
const epoch = EpochUTC.fromDateTime(exampleDate);

// can be created from classical elements
it('should create a J2000 coordinate from classical elements', () => {
const elements = new ClassicalElements({
epoch,
semimajorAxis: 6943.547853722985 as Kilometers,
eccentricity: 0.0011235968124658146,
inclination: 0.7509087232045765 as Radians,
rightAscension: 0.028239555738616327 as Radians,
argPerigee: 2.5386411901807353 as Radians,
trueAnomaly: 0.5931399364974058 as Radians,
});
const j2000 = J2000.fromClassicalElements(elements);

expect(j2000.epoch).toEqual(elements.epoch);
expect(j2000.position).toEqual(elements.toPositionVelocity().position);
expect(j2000.velocity).toEqual(elements.toPositionVelocity().velocity);
});

// can get the name of the coordinate system
it('should return the name of the coordinate system as "J2000"', () => {
const j2000 = new J2000(
epoch,
new Vector3D<Kilometers>(5000 as Kilometers, 10000 as Kilometers, 2100 as Kilometers),
new Vector3D<Kilometers>(7 as Kilometers, 4 as Kilometers, 2 as Kilometers),
);

expect(j2000.name).toBe('J2000');
});

// can get a value indicating whether the coordinate system is inertial
it('should return true for the inertial property', () => {
const j2000 = new J2000(
epoch,
new Vector3D<Kilometers>(5000 as Kilometers, 10000 as Kilometers, 2100 as Kilometers),
new Vector3D<Kilometers>(7 as Kilometers, 4 as Kilometers, 2 as Kilometers),
);

expect(j2000.inertial).toBe(true);
});

// Check conversion to and from
it('should convert to and from other coordinate systems', () => {
const j2000 = new J2000(
epoch,
new Vector3D<Kilometers>(5000 as Kilometers, 10000 as Kilometers, 2100 as Kilometers),
new Vector3D<Kilometers>(7 as Kilometers, 4 as Kilometers, 2 as Kilometers),
);
const itrf = j2000.toITRF();
const j200FromItf = itrf.toJ2000();

expect(j200FromItf.position.x).toBeCloseTo(j2000.position.x, 8);
expect(j200FromItf.position.y).toBeCloseTo(j2000.position.y, 8);
expect(j200FromItf.position.z).toBeCloseTo(j2000.position.z, 8);

const teme = j2000.toTEME();
const j200FromTeme = teme.toJ2000();

expect(j200FromTeme.position.x).toBeCloseTo(j2000.position.x, 8);
expect(j200FromTeme.position.y).toBeCloseTo(j2000.position.y, 8);
expect(j200FromTeme.position.z).toBeCloseTo(j2000.position.z, 8);
});
});

0 comments on commit f77390c

Please sign in to comment.