Module: SunTimes
- Defined in:
- lib/sun_times.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
-
.calculate(event, date, latitude, longitude, options = {}) ⇒ Object
Calculates the sunrise or sunset time for a specific date and location.
-
.rise(date, latitude, longitude, options = {}) ⇒ Object
Helper method: calculates sunrise, with the same parameters as calculate.
-
.set(date, latitude, longitude, options = {}) ⇒ Object
Helper method: calculates sunset, with the same parameters as calculate.
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
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/sun_times.rb', line 64 def SunTimes.calculate(event, date, latitude, longitude, = {}) raise "Unknown event '#{ event }'" if KNOWN_EVENTS.find_index(event).nil? zenith = .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 case when date.respond_to?( :offset ) offset_hours = date.offset * 24 when date.respond_to?( :gmt_offset ) offset_hours = date.gmt_offset / 3600 else offset_hours = nil end if ! offset_hours.nil? if gmt_hours + offset_hours < 0 next_day = Date.new(date.year, date.month, date.day + 1) return calculate(event, next_day, latitude, longitude, = {}) end if gmt_hours + offset_hours > 24 previous_day = Date.new(date.year, date.month, date.day + 1) return calculate(event, previous_day, latitude, longitude, = {}) end 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(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
43 44 45 |
# File 'lib/sun_times.rb', line 43 def SunTimes.rise(date, latitude, longitude, = {}) calculate(:rise, date, latitude, longitude, ) end |
.set(date, latitude, longitude, options = {}) ⇒ Object
Helper method: calculates sunset, with the same parameters as calculate
48 49 50 |
# File 'lib/sun_times.rb', line 48 def SunTimes.set(date, latitude, longitude, = {}) calculate(:set, date, latitude, longitude, ) end |