Skip to content

Commit

Permalink
Using converter functions everywhere
Browse files Browse the repository at this point in the history
Signed-off-by: Cédric Foellmi <[email protected]>
  • Loading branch information
onekiloparsec committed Nov 2, 2023
1 parent a4ab31d commit d221999
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 118 deletions.
38 changes: 19 additions & 19 deletions src/coordinates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ import {
Radian,
TopocentricCoordinates
} from './types'
import { DEG2H, DEG2RAD, ECLIPTIC_OBLIQUITY_J2000_0, H2DEG, J2000, JULIAN_DAY_B1950_0, RAD2DEG } from '@/constants'
import { ECLIPTIC_OBLIQUITY_J2000_0, J2000, JULIAN_DAY_B1950_0 } from '@/constants'
import { getLocalSiderealTime } from '@/juliandays'
import { precessEquatorialCoordinates } from '@/precession'
import { getFlatteningCorrections } from '@/earth/coordinates'
import { fmod24, fmod360, fmod90 } from '@/utils'

// Degree-based trigonometric functions for easier debugging with AA.
const sin = (deg: Degree | number): Radian => new Decimal(deg).mul(DEG2RAD).sin()
const cos = (deg: Degree | number): Radian => new Decimal(deg).mul(DEG2RAD).cos()
const tan = (deg: Degree | number): Radian => new Decimal(deg).mul(DEG2RAD).tan()
const asin = (val: Degree | number): Degree => new Decimal(val).asin().mul(RAD2DEG)
const atan = (val: Degree | number): Degree => new Decimal(val).atan().mul(RAD2DEG)
const atan2 = (y: Degree | number, x: Degree | number): Degree => Decimal.atan2(y, x).mul(RAD2DEG)
const sin = (deg: Degree | number): Radian => new Decimal(deg).degreesToRadians().sin()
const cos = (deg: Degree | number): Radian => new Decimal(deg).degreesToRadians().cos()
const tan = (deg: Degree | number): Radian => new Decimal(deg).degreesToRadians().tan()
const asin = (val: Degree | number): Degree => new Decimal(val).asin().radiansToDegrees()
const atan = (val: Degree | number): Degree => new Decimal(val).atan().radiansToDegrees()
const atan2 = (y: Degree | number, x: Degree | number): Degree => Decimal.atan2(y, x).radiansToDegrees()


/**
Expand All @@ -39,7 +39,7 @@ const atan2 = (y: Degree | number, x: Degree | number): Degree => Decimal.atan2(
* @returns {Hour}
*/
export function getRightAscensionFromEcliptic (l: Degree | number, b: Degree | number, epsilon: Degree | number = ECLIPTIC_OBLIQUITY_J2000_0): Hour {
return fmod24(atan2(sin(l).mul(cos(epsilon)).minus(tan(b).mul(sin(epsilon))), cos(l)).mul(DEG2H))
return fmod24(atan2(sin(l).mul(cos(epsilon)).minus(tan(b).mul(sin(epsilon))), cos(l)).degreesToHours())
}

/**
Expand Down Expand Up @@ -78,7 +78,7 @@ export function transformEclipticToEquatorial (l: Degree | number, b: Degree | n
* @returns {Degree}
*/
export function getEclipticLongitudeFromEquatorial (ra: Hour | number, dec: Degree | number, epsilon: Degree | number = ECLIPTIC_OBLIQUITY_J2000_0): Degree {
const degRa = new Decimal(ra).mul(H2DEG)
const degRa = new Decimal(ra).hoursToDegrees()
return fmod360(atan2(sin(degRa).mul(cos(epsilon)).plus(tan(dec).mul(sin(epsilon))), cos(degRa)))
}

Expand All @@ -90,7 +90,7 @@ export function getEclipticLongitudeFromEquatorial (ra: Hour | number, dec: Degr
* @returns {Degree}
*/
export function getEclipticLatitudeFromEquatorial (ra: Hour | number, dec: Degree | number, epsilon: Degree | number = ECLIPTIC_OBLIQUITY_J2000_0): Degree {
const degRa = new Decimal(ra).mul(H2DEG)
const degRa = new Decimal(ra).hoursToDegrees()
return fmod90(asin(sin(dec).mul(cos(epsilon)).minus(cos(dec).mul(sin(epsilon).mul(sin(degRa))))))
}

Expand Down Expand Up @@ -122,7 +122,7 @@ export function transformEquatorialToEcliptic (ra: Hour | number, dec: Degree |
*/
export function getGalacticLongitudeFromEquatorial (ra: Hour | number, dec: Degree | number, epoch: JulianDay | number = J2000): Degree {
const equCoordsB1950 = precessEquatorialCoordinates(ra, dec, epoch, JULIAN_DAY_B1950_0)
const degRa = equCoordsB1950.rightAscension.mul(H2DEG)
const degRa = equCoordsB1950.rightAscension.hoursToDegrees()
const y = sin(new Decimal(192.25).minus(degRa))
const x = cos(new Decimal(192.25).minus(degRa)).mul(sin(27.4)).minus(tan(equCoordsB1950.declination).mul(cos(27.4)))
return fmod360(new Decimal(303).minus(atan2(y, x)))
Expand All @@ -137,7 +137,7 @@ export function getGalacticLongitudeFromEquatorial (ra: Hour | number, dec: Degr
*/
export function getGalacticLatitudeFromEquatorial (ra: Hour | number, dec: Degree | number, epoch: JulianDay | number = J2000): Degree {
const equCoordsB1950 = precessEquatorialCoordinates(ra, dec, epoch, JULIAN_DAY_B1950_0)
const degRa = equCoordsB1950.rightAscension.mul(H2DEG)
const degRa = equCoordsB1950.rightAscension.hoursToDegrees()
return fmod360(
sin(equCoordsB1950.declination).mul(sin(27.4))
.plus(cos(equCoordsB1950.declination).mul(cos(27.4)).mul(new Decimal(192.25).minus(degRa)))
Expand Down Expand Up @@ -168,7 +168,7 @@ export function getEquatorialRightAscensionB1950FromGalactic (l: Degree | number
const lprime = new Decimal(l).minus(123)
const y = sin(lprime)
const x = cos(lprime).mul(sin(27.4)).minus(tan(b).mul(cos(27.4)))
return fmod24(new Decimal(12.15).plus(atan2(y, x)).mul(DEG2H))
return fmod24(new Decimal(12.15).plus(atan2(y, x)).degreesToHours())
}

/**
Expand Down Expand Up @@ -209,7 +209,7 @@ export function transformGalacticToEquatorial (l: Degree | number, b: Degree | n
*/
export function getHorizontalAltitude (jd: JulianDay | number, lng: Degree | number, lat: Degree | number, ra: Hour | number, dec: Degree | number): Degree {
const lmst = getLocalSiderealTime(jd, lng)
const hourAngle = lmst.minus(ra).mul(H2DEG)
const hourAngle = lmst.minus(ra).hoursToDegrees()
return fmod90(asin(sin(lat).mul(sin(dec)).plus(cos(lat).mul(cos(dec)).mul(cos(hourAngle)))))
}

Expand All @@ -224,7 +224,7 @@ export function getHorizontalAltitude (jd: JulianDay | number, lng: Degree | num
*/
export function getHorizontalAzimuth (jd: JulianDay | number, lng: Degree | number, lat: Degree | number, ra: Hour | number, dec: Degree | number): Degree {
const lmst = getLocalSiderealTime(jd, lng)
const hourAngle = lmst.minus(ra).mul(H2DEG)
const hourAngle = lmst.minus(ra).hoursToDegrees()
return fmod360(atan2(sin(hourAngle), cos(hourAngle).mul(sin(lat)).minus(tan(dec).mul(cos(lat)))))
}

Expand Down Expand Up @@ -258,7 +258,7 @@ export function getRightAscensionFromHorizontal (jd: JulianDay | number, alt: De
const lmst = getLocalSiderealTime(jd, lng)
const y = sin(az)
const x = cos(az).mul(sin(lat)).plus(tan(alt).mul(cos(lat)))
return fmod24(lmst.minus(atan2(y, x).mul(DEG2H)))
return fmod24(lmst.minus(atan2(y, x).degreesToHours()))
}

/**
Expand Down Expand Up @@ -301,7 +301,7 @@ export function transformEquatorialToTopocentric (jd: JulianDay | number, coords
const corrections = getFlatteningCorrections(geoCoords.height, geoCoords.latitude)
const sinpi: Radian = sin(new Decimal(8.794).dividedBy(3600)).dividedBy(distance)
const theta0: Degree = getLocalSiderealTime(jd, 0)
const H: Degree = fmod24(theta0.plus(new Decimal(geoCoords.longitude).mul(DEG2H)).minus(coords.rightAscension)).mul(H2DEG)
const H: Degree = fmod24(theta0.plus(new Decimal(geoCoords.longitude).degreesToHours()).minus(coords.rightAscension)).hoursToDegrees()

const numeratorAlpha = new Decimal(-1).mul(corrections.rhocosphi).mul(sinpi).mul(sin(H))
const denominatorAlpha = cos(coords.declination).minus(corrections.rhocosphi.mul(sinpi).mul(cos(H)))
Expand All @@ -313,7 +313,7 @@ export function transformEquatorialToTopocentric (jd: JulianDay | number, coords
const tanDeltaPrime = numeratorDelta.dividedBy(denominatorDelta)

return {
rightAscension: new Decimal(coords.rightAscension).plus(atan(tanDeltaAlpha).mul(DEG2H)),
rightAscension: new Decimal(coords.rightAscension).plus(atan(tanDeltaAlpha).degreesToHours()),
declination: atan(tanDeltaPrime)
}
}
Expand Down Expand Up @@ -364,7 +364,7 @@ export function transformHorizontalToPoint (alt: Degree | number, az: Degree | n
*/
export function getParallacticAngle (jd: JulianDay | number, ra: Hour | number, dec: Degree | number, lng: Degree | number, lat: Degree | number): Degree {
const lmst = getLocalSiderealTime(jd, lng)
const HA = lmst.minus(ra).mul(H2DEG)
const HA = lmst.minus(ra).hoursToDegrees()

let angle = undefined
const cosdec = cos(dec)
Expand Down
4 changes: 2 additions & 2 deletions src/decimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare module 'decimal.js' {
interface Decimal {
degreesToRadians (): Radian;

degreeToHours (): Hour;
degreesToHours (): Hour;

hoursToRadians (): Radian;

Expand All @@ -22,7 +22,7 @@ Decimal.prototype.degreesToRadians = function (): Radian {
return this.mul(DEG2RAD)
}

Decimal.prototype.degreeToHours = function (): Hour {
Decimal.prototype.degreesToHours = function (): Hour {
return this.mul(DEG2H)
}

Expand Down
8 changes: 4 additions & 4 deletions src/earth/aberration/aberration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
JulianDay,
Radian
} from '@/types'
import { CONSTANT_OF_ABERRATION, DEG2RAD, H2RAD, MINUSONE, RAD2DEG, RAD2H, ZERO } from '@/constants'
import { CONSTANT_OF_ABERRATION, MINUSONE, ZERO } from '@/constants'
import { Sun } from '@/sun'
import { getJulianCentury } from '@/juliandays'
import { getEccentricity, getLongitudeOfPerihelion } from '../coordinates'
Expand Down Expand Up @@ -78,8 +78,8 @@ export function getEarthVelocity (jd: JulianDay | number): Coordinates3D {
* @return {Coordinates2D}
*/
export function getAccurateAnnualEquatorialAberration (jd: JulianDay | number, Alpha: Hour | number, Delta: Degree | number): EquatorialCoordinatesCorrection {
const ra = new Decimal(Alpha).mul(H2RAD)
const dec = new Decimal(Delta).mul(DEG2RAD)
const ra = new Decimal(Alpha).hoursToRadians()
const dec = new Decimal(Delta).degreesToRadians()

const cosAlpha = ra.cos()
const sinAlpha = ra.sin()
Expand All @@ -96,7 +96,7 @@ export function getAccurateAnnualEquatorialAberration (jd: JulianDay | number, A
const Y1 = velocity.Z.mul(cosDelta)
const Y = (Y0.mul(sinDelta).minus(Y1)).dividedBy(c)

return { DeltaRightAscension: X.mul(RAD2H), DeltaDeclination: MINUSONE.mul(Y).mul(RAD2DEG) }
return { DeltaRightAscension: X.radiansToHours(), DeltaDeclination: MINUSONE.mul(Y).radiansToDegrees() }
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/earth/coordinates.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Decimal from '@/decimal'
import { AstronomicalUnit, Degree, EclipticCoordinates, Equinox, JulianDay, Meter, Radian } from '@/types'
import { DEG2RAD, EARTH_EQUATORIAL_RADIUS, EARTH_RADIUS_FLATTENING_FACTOR, RAD2DEG } from '@/constants'
import { EARTH_EQUATORIAL_RADIUS, EARTH_RADIUS_FLATTENING_FACTOR } from '@/constants'
import { getJulianCentury, getJulianMillenium } from '@/juliandays'
import { fmod360, fmod90 } from '@/utils'
import { Sun } from '@/sun'
Expand Down Expand Up @@ -55,7 +55,7 @@ function getEclipticLongitudeValue (jd: JulianDay | number, equinox: Equinox = E
.plus(L5.mul(tau.pow(5)))
).dividedBy(1e8)

return value.mul(RAD2DEG)
return value.radiansToDegrees()
}

/**
Expand Down Expand Up @@ -109,7 +109,7 @@ export function getEclipticLatitude (jd: JulianDay | number, equinox: Equinox =
.plus(B4.mul(tau.pow(4)))
).dividedBy(1e8)

return fmod90(value.mul(RAD2DEG))
return fmod90(value.radiansToDegrees())
}

/**
Expand Down Expand Up @@ -192,7 +192,7 @@ export function getLongitudeOfPerihelion (jd: JulianDay | number): Degree {
export function getFlatteningCorrections (height: Meter | number, lat: Degree | number) {
const earthRadius: Meter = EARTH_EQUATORIAL_RADIUS.mul(1000)
const b_a: Decimal = new Decimal(1).minus(EARTH_RADIUS_FLATTENING_FACTOR)
const radLat: Radian = new Decimal(lat).mul(DEG2RAD)
const radLat: Radian = new Decimal(lat).degreesToRadians()
const u: Radian = Decimal.atan(b_a.mul(radLat.tan())) // Radians
const h = new Decimal(height).dividedBy(earthRadius)
return {
Expand Down
13 changes: 5 additions & 8 deletions src/exoplanets.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import Decimal from '@/decimal'
import { AstronomicalUnit, Day, Degree, Hour, JulianDay, JupiterRadius, Kilometer, Radian, SolarRadius } from '@/types'
import {
DEG2RAD,
H2RAD,
ONE,
ONE_JUPITER_RADIUS_IN_KILOMETERS,
ONE_SOLAR_RADIUS_IN_KILOMETERS,
ONE_UA_IN_KILOMETERS,
PI,
RAD2DEG,
TWO
} from '@/constants'
import { getLocalSiderealTime } from '@/juliandays'
Expand Down Expand Up @@ -46,7 +43,7 @@ export function getExoplanetTransitDetails (orbitalPeriod: Day | number,
radius: JupiterRadius | number,
semiMajorAxis: AstronomicalUnit | number,
parentStarRadius: SolarRadius | number) {
let f = (PI.dividedBy(2)).minus(new Decimal(lambdaAngle).mul(DEG2RAD))
let f = (PI.dividedBy(2)).minus(new Decimal(lambdaAngle).degreesToRadians())
const e = new Decimal(eccentricity)
const P = new Decimal(orbitalPeriod)
const E = TWO.mul(Decimal.atan(Decimal.sqrt((ONE.minus(e)).dividedBy(ONE.plus(e))).mul(Decimal.tan(f.dividedBy(2)))))
Expand Down Expand Up @@ -81,11 +78,11 @@ export function getTransitAltitude (ra: Hour | number, dec: Degree | number, lng
let cosH = new Decimal(1)
if (transitJD !== undefined && transitJD !== null) {
const lmst = getLocalSiderealTime(transitJD, lng)
cosH = Decimal.cos((lmst.minus(ra)).mul(H2RAD))
cosH = Decimal.cos((lmst.minus(ra)).hoursToRadians())
}
const dlat = new Decimal(lat).mul(DEG2RAD)
const ddec = new Decimal(lat).mul(DEG2RAD)
const dlat = new Decimal(lat).degreesToRadians()
const ddec = new Decimal(lat).degreesToRadians()
return Decimal.asin(
dlat.sin().mul(ddec.sin()).plus(dlat.cos().mul(ddec.cos()).mul(cosH))
).mul(RAD2DEG)
).radiansToDegrees()
}
25 changes: 12 additions & 13 deletions src/fk5.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import Decimal from '@/decimal'
import { Degree, JulianCentury, JulianDay, Radian } from '@/types'
import { DEG2RAD } from '@/constants'
import { getJulianCentury } from '@/juliandays'

export function getCorrectionInLongitude (jd: JulianDay | number, lng: Degree | number, lat: Degree | number): Degree {
const T = getJulianCentury(jd)
let Ldash: Radian = new Decimal(lng)
.minus(new Decimal(1.397).mul(T))
.minus(new Decimal(0.00031).mul(T.pow(2)))
.mul(DEG2RAD)
const value = new Decimal(-0.09033)
.plus(new Decimal(0.03916)
.minus(new Decimal('1.397').mul(T))
.minus(new Decimal('0.000_31').mul(T.pow(2)))
.degreesToRadians()
const value = new Decimal('-0.090_33')
.plus(new Decimal('0.039_16')
.mul(Ldash.cos().plus(Ldash.sin()))
.mul(new Decimal(lat).mul(DEG2RAD).tan()))
return value.dividedBy(3600)
.mul(new Decimal(lat).degreesToRadians().tan()))
return value.dividedBy('3600')
}

export function getCorrectionInLatitude (jd: JulianDay | number, lng: Degree | number): Degree {
const T: JulianCentury = getJulianCentury(jd)
let Ldash: Radian = new Decimal(lng)
.minus(new Decimal(1.397).mul(T))
.minus(new Decimal(0.00031).mul(T.pow(2)))
.mul(DEG2RAD)
const value = new Decimal(0.03916).mul(Ldash.cos().minus(Ldash.sin()))
return value.dividedBy(3600)
.minus(new Decimal('1.397').mul(T))
.minus(new Decimal('0.000_31').mul(T.pow(2)))
.degreesToRadians()
const value = new Decimal('0.039_16').mul(Ldash.cos().minus(Ldash.sin()))
return value.dividedBy('3600')
}
6 changes: 3 additions & 3 deletions src/juliandays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import dayjs from 'dayjs'
import Decimal from '@/decimal'
import { DAYMS, DEG2H, J1970, MJD_START, ONE_DAY_IN_SECONDS } from './constants'
import { DAYMS, J1970, MJD_START, ONE_DAY_IN_SECONDS } from './constants'
import { ArcSecond, Degree, Hour, JulianCentury, JulianDay, JulianMillenium, Radian } from './types'
import { fmod24, fmod360, isNumber } from './utils'
import { Earth } from '@/earth'
Expand Down Expand Up @@ -66,7 +66,7 @@ export function getLocalSiderealTime (jd: JulianDay | number, lng: Degree | numb
.plus(new Decimal(0.000387933).mul(T.pow(2)))
.minus(T.pow(3).dividedBy(38710000))

return fmod24(fmod360(gmst).plus(lng).mul(DEG2H))
return fmod24(fmod360(gmst).plus(lng).degreesToHours())
}

/**
Expand All @@ -79,7 +79,7 @@ export function getLocalSiderealTime (jd: JulianDay | number, lng: Degree | numb
export function getApparentLocalSiderealTime (jd: JulianDay | number, lng: Degree | number): Hour {
const epsilon: Radian = Earth.getTrueObliquityOfEcliptic(jd).degreesToRadians()
const deltaPsi: ArcSecond = Earth.getNutationInLongitude(jd)
return getLocalSiderealTime(jd, lng).plus(deltaPsi.mul(epsilon.cos()).degreeToHours().dividedBy(ONE_DAY_IN_SECONDS))
return getLocalSiderealTime(jd, lng).plus(deltaPsi.mul(epsilon.cos()).degreesToHours().dividedBy(ONE_DAY_IN_SECONDS))
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/planets/elliptical.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Decimal from '@/decimal'
import { DEG2RAD, ONE, PI, RAD2DEG, THREE, TWO } from '@/constants'
import { ONE, PI, THREE, TWO } from '@/constants'
import {
AstronomicalUnit,
Day,
Expand Down Expand Up @@ -38,8 +38,8 @@ export function getPlanetDistanceDetailsFromEarth (jd: JulianDay | number,
radiusVectorFunc: QuantityInAstronomicalUnitAtJulianDayFunction): EllipticalDistance {
// Calculate the position of the Earth first
const earthCoords = {
L: earthGetEclipticLongitude(jd).mul(DEG2RAD),
B: earthGetEclipticLatitude(jd).mul(DEG2RAD),
L: earthGetEclipticLongitude(jd).degreesToRadians(),
B: earthGetEclipticLatitude(jd).degreesToRadians(),
R: earthGetRadiusVector(jd)
}

Expand All @@ -63,8 +63,8 @@ export function getPlanetDistanceDetailsFromEarth (jd: JulianDay | number,
let JD0 = new Decimal(jd)
while (continueIterations) {
coords = {
L: eclipticLongitudeFunc(JD0).mul(DEG2RAD),
B: eclipticLatitudeFunc(JD0).mul(DEG2RAD),
L: eclipticLongitudeFunc(JD0).degreesToRadians(),
B: eclipticLatitudeFunc(JD0).degreesToRadians(),
R: radiusVectorFunc(JD0)
}

Expand Down Expand Up @@ -124,8 +124,8 @@ export function getPlanetGeocentricEclipticCoordinates (jd: JulianDay | number,
const details = getPlanetDistanceDetailsFromEarth(jd, eclipticLongitudeFunc, eclipticLatitudeFunc, radiusVectorFunc)

return {
longitude: fmod360(Decimal.atan2(details.y, details.x).mul(RAD2DEG)),
latitude: fmod90(Decimal.atan2(details.z, Decimal.sqrt(details.x.pow(2).plus(details.y.pow(2)))).mul(RAD2DEG))
longitude: fmod360(Decimal.atan2(details.y, details.x).radiansToDegrees()),
latitude: fmod90(Decimal.atan2(details.z, Decimal.sqrt(details.x.pow(2).plus(details.y.pow(2)))).radiansToDegrees())
}
}

Expand Down
Loading

0 comments on commit d221999

Please sign in to comment.