Module: SunTimes

Defined in:
lib/sun_times.rb,
lib/sun_times/version.rb

Defined Under Namespace

Modules: VERSION

Constant Summary collapse

DEFAULT_ZENITH =
90.83333
KNOWN_EVENTS =
[:rise, :set]
DEGREES_PER_HOUR =
360.0 / 24.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 :to_datetime.

  • 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


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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/sun_times.rb', line 55

def self.calculate(event, date, latitude, longitude, options = {})
  datetime = date.to_datetime
  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 = datetime.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

  offset_hours = datetime.offset * 24.0

  if gmt_hours + offset_hours < 0
    next_day = datetime.next_day
    return calculate(event, next_day.new_offset, latitude, longitude, options = {})
  end
  if gmt_hours + offset_hours > 24
    previous_day = datetime.prev_day
    return calculate(event, previous_day.new_offset, latitude, longitude, options = {})
  end

  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(datetime.year, datetime.month, datetime.day, hour, minute, seconds)
end

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

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



34
35
36
# File 'lib/sun_times.rb', line 34

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



39
40
41
# File 'lib/sun_times.rb', line 39

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