module SunCalc::Helpers

Public Instance Methods

altitude(h, phi, dec) click to toggle source
# File lib/sun_calc/helpers.rb, line 36
def altitude(h, phi, dec)
  Math.asin(Math.sin(phi) * Math.sin(dec) +
            Math.cos(phi) * Math.cos(dec) * Math.cos(h))
end
approx_transit(ht, lw, n) click to toggle source
# File lib/sun_calc/helpers.rb, line 77
def approx_transit(ht, lw, n)
  J0 + (ht + lw) / (2 * Math::PI) + n
end
astro_refraction(h) click to toggle source
# File lib/sun_calc/helpers.rb, line 45
def astro_refraction(h)
  # The following formula works for positive altitudes only.
  h = 0 if h < 0
  # Based on forumla 16.4 of "Astronomical Algorithms" 2nd edition by Jean
  # Meeus (Willmann-Bell, Richmond) 1998.
  0.0002967 / Math.tan(h + 0.00312536 / (h + 0.08901179))
end
azimuth(h, phi, dec) click to toggle source
# File lib/sun_calc/helpers.rb, line 31
def azimuth(h, phi, dec)
  Math.atan2(Math.sin(h),
             Math.cos(h) * Math.sin(phi) - Math.tan(dec) * Math.cos(phi))
end
declination(l, b) click to toggle source
# File lib/sun_calc/helpers.rb, line 26
def declination(l, b)
  Math.asin(Math.sin(b) * Math.cos(OBLIQUITY_OF_THE_EARTH) +
    Math.cos(b) * Math.sin(OBLIQUITY_OF_THE_EARTH) * Math.sin(l))
end
ecliptic_longitude(m) click to toggle source
# File lib/sun_calc/helpers.rb, line 57
def ecliptic_longitude(m)
  # Equation of center.
  c = ONE_RADIAN * (1.9148 * Math.sin(m) +
                    0.02 * Math.sin(2 * m) + 0.0003 * Math.sin(3 * m))
  # Perihelion of Earth.
  p = ONE_RADIAN * 102.9372
  m + c + p + Math::PI
end
from_julian(j) click to toggle source
# File lib/sun_calc/helpers.rb, line 10
def from_julian(j)
  Time.at((j + 0.5 - J1970) * ONE_DAY_IN_SECONDS).utc
end
get_set_j(h0, lw, phi, dec, n, m, l) click to toggle source
# File lib/sun_calc/helpers.rb, line 92
def get_set_j(h0, lw, phi, dec, n, m, l)
  w = hour_angle(h0, phi, dec)
  a = approx_transit(w, lw, n)
  solar_transit_j(a, m, l)
end
hour_angle(h0, phi, dec) click to toggle source
# File lib/sun_calc/helpers.rb, line 85
def hour_angle(h0, phi, dec)
  Math.acos(
    (Math.sin(h0) - Math.sin(phi) * Math.sin(dec)) /
    (Math.cos(phi) * Math.cos(dec))
  )
end
hours_later(date, h) click to toggle source
# File lib/sun_calc/helpers.rb, line 111
def hours_later(date, h)
  Time.at(date.to_f + h * ONE_DAY_IN_SECONDS / 24).utc
end
julian_cycle(d, lw) click to toggle source
# File lib/sun_calc/helpers.rb, line 73
def julian_cycle(d, lw)
  (d - J0 - lw / (2 * Math::PI)).round
end
moon_coords(d) click to toggle source
# File lib/sun_calc/helpers.rb, line 98
def moon_coords(d)
  # Geocentric ecliptic coordinates of the moon
  l  = ONE_RADIAN * (218.316 + 13.176396 * d) # ecliptic longitude
  m  = ONE_RADIAN * (134.963 + 13.064993 * d) # mean anomaly
  f  = ONE_RADIAN * (93.272 + 13.229350 * d)  # mean distance
  l += ONE_RADIAN * 6.289 * Math.sin(m) # longitude
  b  = ONE_RADIAN * 5.128 * Math.sin(f) # latitude
  dt = 385_001 - 20_905 * Math.cos(m) # distance to the moon in km
  { ra: right_ascension(l, b),
    dec: declination(l, b),
    dist: dt }
end
right_ascension(l, b) click to toggle source
# File lib/sun_calc/helpers.rb, line 18
def right_ascension(l, b)
  Math.atan2(
    Math.sin(l) * Math.cos(OBLIQUITY_OF_THE_EARTH) -
      Math.tan(b) * Math.sin(OBLIQUITY_OF_THE_EARTH),
    Math.cos(l)
  )
end
sidereal_time(d, lw) click to toggle source
# File lib/sun_calc/helpers.rb, line 41
def sidereal_time(d, lw)
  ONE_RADIAN * (280.16 + 360.9856235 * d) - lw
end
solar_mean_anomaly(d) click to toggle source
# File lib/sun_calc/helpers.rb, line 53
def solar_mean_anomaly(d)
  ONE_RADIAN * (357.5291 + 0.98560028 * d)
end
solar_transit_j(ds, m, l) click to toggle source
# File lib/sun_calc/helpers.rb, line 81
def solar_transit_j(ds, m, l)
  J2000 + ds + 0.0053 * Math.sin(m) - 0.0069 * Math.sin(2 * l)
end
sun_coords(d) click to toggle source
# File lib/sun_calc/helpers.rb, line 66
def sun_coords(d)
  m = solar_mean_anomaly(d)
  l = ecliptic_longitude(m)
  { dec: declination(l, 0),
    ra: right_ascension(l, 0) }
end
to_days(date) click to toggle source
# File lib/sun_calc/helpers.rb, line 14
def to_days(date)
  to_julian(date) - J2000
end
to_julian(date) click to toggle source
# File lib/sun_calc/helpers.rb, line 6
def to_julian(date)
  date.to_f / ONE_DAY_IN_SECONDS - 0.5 + J1970
end