Skip to content

Instantly share code, notes, and snippets.

@rhannequin
Last active July 3, 2022 21:58
Show Gist options
  • Save rhannequin/8ab3166aabe9310ca97be54328e2f493 to your computer and use it in GitHub Desktop.
Save rhannequin/8ab3166aabe9310ca97be54328e2f493 to your computer and use it in GitHub Desktop.
Ruby script to convert equatorial to horizon coordinates
require "bigdecimal"
require "date"
time = Time.new(2022, 6, 26, 3, 10, 5, "+02:00")
latitude = BigDecimal("49.70911954641343")
longitude = BigDecimal("0.20271537957527094")
right_ascension_hour = 21
right_ascension_minute = 49
right_ascension_second = 8.6
declination_degree = -14
declination_minute = 26
declination_second = 57.4
## Local civil time to universal time
universal_time = time.utc
## Universal time to Greenwich sidereal time
jd = universal_time.to_date.ajd.to_f
jd0 = Time.utc(universal_time.year, 1, 1, 0, 0, 0).to_date.ajd - 1
days = jd - jd0
t = (jd0 - BigDecimal("2415020")) / BigDecimal("36525")
r = BigDecimal("6.6460656") + BigDecimal("2400.051262") * t + BigDecimal("0.00002581") * t * t
b = 24 - r + 24 * (universal_time.year - 1900)
t0 = BigDecimal("0.0657098") * days - b
ut = universal_time.hour + universal_time.min / BigDecimal("60") + universal_time.sec / BigDecimal("3600")
gst = t0 + BigDecimal("1.002738") * ut
gst += 24 if gst.negative?
gst -= 24 if gst > 24
## Greenwich sidereal time to local sidereal time
adjustment = longitude / 15
lst = (gst + adjustment)
## Right ascension to hour angle
hour_angle = lst - (right_ascension_hour + right_ascension_minute / 60r + right_ascension_second / 3600r)
hour_angle += 24 if hour_angle.negative?
hour_angle -= 24 if hour_angle > 24
## Equatorial to horizon coordinates
to_radians = Math::PI / 180
to_degrees = 180 / Math::PI
hour_angle_degree = hour_angle * 15
hour_angle_radians = hour_angle_degree * to_radians
declination_decimal = (declination_degree + declination_minute / 60.0 + declination_second / 3600.0).to_f
declination_radians = declination_decimal * to_radians
latitude_radians = latitude * to_radians
t0 = Math.sin(declination_radians) * Math.sin(latitude_radians) + Math.cos(declination_radians) * Math.cos(latitude_radians) * Math.cos(hour_angle_radians)
h = Math.asin(t0) * to_degrees
h_radians = h * to_radians
t1 = Math.sin(declination_radians) - Math.sin(latitude_radians) * Math.sin(h_radians)
t2 = t1 / (Math.cos(latitude_radians) * Math.cos(h_radians))
a = Math.acos(t2) * to_degrees
sin_hour_angle = Math.sin(hour_angle_radians)
a = 360 - a if sin_hour_angle.positive?
pp h.to_f
pp a.to_f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment