Module: SunTimes

Defined in:
lib/sun-times.rb,
lib/sun-times/version.rb

Constant Summary collapse

DEFAULT_ZENITH =
90.83333
KNOWN_EVENTS =
[:rise, :set]
DEGREES_PER_HOUR =
360.0 / 24.0
VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.calculate(event, date, latitude, longitude, options = {}) ⇒ Object

Calculates the sunrise or sunset time for a specific date and location

Parameters

  • event - One of :rise, :set.

  • date - An object that responds to yday.

  • latitude - The latitude of the location in degrees.

  • longitude - The longitude of the location in degrees.

  • options - Additional option is :zenith.

Example

SunTimes.calculate(:rise, Date.new(2010, 3, 8), 43.779, 11.432)
> Mon Mar 08 05:39:53 UTC 2010


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sun-times.rb', line 30

def self.calculate(event, date, latitude, longitude, options = {})
  raise "Unknown event '#{ event }'" if KNOWN_EVENTS.find_index(event).nil?
  zenith = options.delete(:zenith) || DEFAULT_ZENITH

  # lngHour
  longitude_hour = longitude / DEGREES_PER_HOUR

  # t
  base_time = event == :rise ? 6.0 : 18.0
  approximate_time = date.yday + (base_time - longitude_hour) / 24.0

  # M
  mean_sun_anomaly = (0.9856 * approximate_time) - 3.289

  # L
  sun_true_longitude = mean_sun_anomaly +
                      (1.916 * Math.sin(degrees_to_radians(mean_sun_anomaly))) +
                      (0.020 * Math.sin(2 * degrees_to_radians(mean_sun_anomaly))) +
                      282.634
  sun_true_longitude = coerce_degrees(sun_true_longitude)

  # RA
  tan_right_ascension = 0.91764 * Math.tan(degrees_to_radians(sun_true_longitude))
  sun_right_ascension = radians_to_degrees(Math.atan(tan_right_ascension))
  sun_right_ascension = coerce_degrees(sun_right_ascension)

  # right ascension value needs to be in the same quadrant as L
  sun_true_longitude_quadrant  = (sun_true_longitude  / 90.0).floor * 90.0
  sun_right_ascension_quadrant = (sun_right_ascension / 90.0).floor * 90.0
  sun_right_ascension += (sun_true_longitude_quadrant - sun_right_ascension_quadrant)

  # RA = RA / 15
  sun_right_ascension_hours = sun_right_ascension / DEGREES_PER_HOUR

  sin_declination = 0.39782 * Math.sin(degrees_to_radians(sun_true_longitude))
  cos_declination = Math.cos(Math.asin(sin_declination))

  cos_local_hour_angle =
    (Math.cos(degrees_to_radians(zenith)) - (sin_declination * Math.sin(degrees_to_radians(latitude)))) /
    (cos_declination * Math.cos(degrees_to_radians(latitude)))

  # the sun never rises on this location (on the specified date)
  return nil if cos_local_hour_angle > 1
  # the sun never sets on this location (on the specified date)
  return nil if cos_local_hour_angle < -1

  # H
  suns_local_hour =
    if event == :rise
      360 - radians_to_degrees(Math.acos(cos_local_hour_angle))
    else
      radians_to_degrees(Math.acos(cos_local_hour_angle))
    end

  # H = H / 15
  suns_local_hour_hours = suns_local_hour / DEGREES_PER_HOUR

  # T = H + RA - (0.06571 * t) - 6.622
  local_mean_time = suns_local_hour_hours + sun_right_ascension_hours - (0.06571 * approximate_time) - 6.622

  # UT = T - lngHour
  gmt_hours = local_mean_time - longitude_hour
  gmt_hours -= 24.0 if gmt_hours > 24
  gmt_hours += 24.0 if gmt_hours <  0

  hour = gmt_hours.floor
  hour_remainder = (gmt_hours.to_f - hour.to_f) * 60.0
  minute = hour_remainder.floor
  seconds = (hour_remainder - minute) * 60.0

  Time.gm(date.year, date.month, date.day, hour, minute, seconds)
end

.rise(date, latitude, longitude, options = {}) ⇒ Object

Helper method: calculates sunrise, with the same parameters as calculate



9
10
11
# File 'lib/sun-times.rb', line 9

def self.rise(date, latitude, longitude, options = {})
  calculate(:rise, date, latitude, longitude, options)
end

.set(date, latitude, longitude, options = {}) ⇒ Object

Helper method: calculates sunset, with the same parameters as calculate



14
15
16
# File 'lib/sun-times.rb', line 14

def self.set(date, latitude, longitude, options = {})
  calculate(:set, date, latitude, longitude, options)
end