Skip to content

Commit e9dd9c7

Browse files
feat: amend convertEquatorialToHorizontal to accept topocentric altitude corrections in @observerly/astrometry
feat: amend convertEquatorialToHorizontal to accept topocentric altitude corrections in @observerly/astrometry
1 parent de7bdfd commit e9dd9c7

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/coordinates.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
GeographicCoordinate,
1616
HorizontalCoordinate
1717
} from './common'
18+
import { EARTH_RADIUS } from './constants'
1819

1920
import { convertRadiansToDegrees as degrees, convertDegreesToRadians as radians } from './utilities'
2021

@@ -123,12 +124,14 @@ export const convertGalacticToEquatorial = (target: GalacticCoordinate): Equator
123124
export const convertEquatorialToHorizontal = (
124125
datetime: Date,
125126
observer: GeographicCoordinate,
126-
target: EquatorialCoordinate
127+
target: EquatorialCoordinate & { distance?: number }
127128
): HorizontalCoordinate => {
128-
const { latitude, longitude } = observer
129+
const { latitude, longitude, elevation = 0 } = observer
129130

130131
const declination = radians(target.dec)
131132

133+
const R = EARTH_RADIUS
134+
132135
// Divide-by-zero errors can occur when we have cos(90), and sin(0)/sin(180) etc
133136
// cosine: multiples of π/2
134137
// sine: 0, and multiples of π.
@@ -167,9 +170,25 @@ export const convertEquatorialToHorizontal = (
167170
)
168171
)
169172

173+
let p = 0
174+
175+
if (target.distance !== undefined && target.distance > 0) {
176+
// For nearby objects, horizontal parallax (p) is approximated as R/target.distance (in radians)
177+
p = R / target.distance
178+
}
179+
180+
// Calculate the topocentric correction for the altitude of the target:
181+
const topocentricCorrection: HorizontalCoordinate = {
182+
alt: p * Math.cos(altitude) * Math.cos(azimuth),
183+
az: (-p * Math.sin(azimuth)) / Math.cos(altitude)
184+
}
185+
170186
return {
171-
alt: degrees(altitude),
172-
az: Math.sin(ha) > 0 ? 360 - degrees(azimuth) : degrees(azimuth)
187+
alt: degrees(altitude + Math.sqrt((2 * elevation) / R)) + topocentricCorrection.alt,
188+
az:
189+
Math.sin(ha) > 0
190+
? 360 - degrees(azimuth + topocentricCorrection.az)
191+
: degrees(azimuth + topocentricCorrection.az)
173192
}
174193
}
175194

tests/coordinates.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ describe('convertEquatorialToHorizontal', () => {
8181
// The target should be at the observer's meridian:
8282
expect(target.az).toBe(270)
8383
})
84+
85+
it('should return the correct horizontal coordinate for the star Betelgeuse with an observer elevation greater than 0', () => {
86+
const { alt, az } = convertEquatorialToHorizontal(datetime, { latitude, longitude, elevation: 100 }, betelgeuse)
87+
expect(alt).toBe(73.10623604640716)
88+
expect(az).toBe(134.44877920325155)
89+
})
8490
})
8591

8692
/*****************************************************************************************************************/

0 commit comments

Comments
 (0)