when_sun 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +21 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/when_sun.rb +124 -0
- data/test/helper.rb +15 -0
- data/test/test_when_sun.rb +39 -0
- data/when_sun.gemspec +55 -0
- metadata +122 -0
data/.document
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Enphase Energy
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
= when_sun
|
2
|
+
|
3
|
+
WhenSun is a RubyGem for calculating the sunrise and sunset for locations based on the date, latitude and longitude.
|
4
|
+
|
5
|
+
This gem is based on https://github.com/joeyates/ruby-sun-times implementation of http://williams.best.vwh.net/sunrise_sunset_algorithm.htm
|
6
|
+
|
7
|
+
== Contributing to when_sun
|
8
|
+
|
9
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
10
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
11
|
+
* Fork the project
|
12
|
+
* Start a feature/bugfix branch
|
13
|
+
* Commit and push until you are happy with your contribution
|
14
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
15
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
16
|
+
|
17
|
+
== Copyright
|
18
|
+
|
19
|
+
Copyright (c) 2011 Enphase Energy. See LICENSE.txt for
|
20
|
+
further details.
|
21
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "when_sun"
|
18
|
+
gem.homepage = "http://github.com/cracell/when_sun"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{WhenSun is a RubyGem for calculating the sunrise and sunset for locations based on the date, latitude and longitude.}
|
21
|
+
gem.description = %Q{WhenSun is a RubyGem for calculating the sunrise and sunset for locations based on the date, latitude and longitude.}
|
22
|
+
gem.email = "ecranston@enphaseenergy.com"
|
23
|
+
gem.authors = ["Eric Cranston"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
test.rcov_opts << '--exclude "gems/*"'
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "when_sun #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/when_sun.rb
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
class WhenSun
|
4
|
+
DEFAULT_ZENITH = 90.83333
|
5
|
+
KNOWN_EVENTS = [:rise, :set]
|
6
|
+
|
7
|
+
# Helper method: calculates sunrise, with the same parameters as calculate
|
8
|
+
def self.rise(date, latitude, longitude, options = {})
|
9
|
+
calculate(:rise, date, latitude, longitude, options)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Helper method: calculates sunset, with the same parameters as calculate
|
13
|
+
def self.set(date, latitude, longitude, options = {})
|
14
|
+
calculate(:set, date, latitude, longitude, options)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Calculates the sunrise or sunset time for a specific date and location
|
18
|
+
#
|
19
|
+
# ==== Parameters
|
20
|
+
# * +event+ - One of :rise, :set.
|
21
|
+
# * +date+ - An object that responds to yday.
|
22
|
+
# * +latitude+ - The latitude of the location in degrees.
|
23
|
+
# * +longitude+ - The longitude of the location in degrees.
|
24
|
+
# * +options+ - Additional option is <tt>:zenith</tt>.
|
25
|
+
#
|
26
|
+
# ==== Example
|
27
|
+
# SunTimes.calculate(:rise, Date.new(2010, 3, 8), 43.779, 11.432)
|
28
|
+
# > Mon Mar 08 05:39:53 UTC 2010
|
29
|
+
def self.calculate(event, date, latitude, longitude, options = {})
|
30
|
+
raise "Unknown event '#{ event }'" unless KNOWN_EVENTS.include?(event)
|
31
|
+
zenith = options.delete(:zenith) || DEFAULT_ZENITH
|
32
|
+
|
33
|
+
# lngHour
|
34
|
+
longitude_hour = longitude / 15.0
|
35
|
+
|
36
|
+
# t
|
37
|
+
base_time = event == :rise ? 6.0 : 18.0
|
38
|
+
approximate_time = date.yday + (base_time - longitude_hour) / 24.0
|
39
|
+
|
40
|
+
# M
|
41
|
+
mean_sun_anomaly = (0.9856 * approximate_time) - 3.289
|
42
|
+
|
43
|
+
# L
|
44
|
+
sun_true_longitude = mean_sun_anomaly +
|
45
|
+
(1.916 * Math.sin(degrees_to_radians(mean_sun_anomaly))) +
|
46
|
+
(0.020 * Math.sin(2 * degrees_to_radians(mean_sun_anomaly))) +
|
47
|
+
282.634
|
48
|
+
sun_true_longitude = coerce_degrees(sun_true_longitude)
|
49
|
+
|
50
|
+
# RA
|
51
|
+
tan_right_ascension = 0.91764 * Math.tan(degrees_to_radians(sun_true_longitude))
|
52
|
+
sun_right_ascension = radians_to_degrees(Math.atan(tan_right_ascension))
|
53
|
+
sun_right_ascension = coerce_degrees(sun_right_ascension)
|
54
|
+
|
55
|
+
# right ascension value needs to be in the same quadrant as L
|
56
|
+
sun_true_longitude_quadrant = (sun_true_longitude / 90.0).floor * 90.0
|
57
|
+
sun_right_ascension_quadrant = (sun_right_ascension / 90.0).floor * 90.0
|
58
|
+
sun_right_ascension += (sun_true_longitude_quadrant - sun_right_ascension_quadrant)
|
59
|
+
|
60
|
+
# RA = RA / 15
|
61
|
+
sun_right_ascension_hours = sun_right_ascension / 15.0
|
62
|
+
|
63
|
+
sin_declination = 0.39782 * Math.sin(degrees_to_radians(sun_true_longitude))
|
64
|
+
cos_declination = Math.cos(Math.asin(sin_declination))
|
65
|
+
|
66
|
+
cos_local_hour_angle =
|
67
|
+
(Math.cos(degrees_to_radians(zenith)) - (sin_declination * Math.sin(degrees_to_radians(latitude)))) /
|
68
|
+
(cos_declination * Math.cos(degrees_to_radians(latitude)))
|
69
|
+
|
70
|
+
# the sun never rises on this location (on the specified date)
|
71
|
+
return nil if cos_local_hour_angle > 1
|
72
|
+
# the sun never sets on this location (on the specified date)
|
73
|
+
return nil if cos_local_hour_angle < -1
|
74
|
+
|
75
|
+
# H
|
76
|
+
suns_local_hour =
|
77
|
+
if event == :rise
|
78
|
+
360 - radians_to_degrees(Math.acos(cos_local_hour_angle))
|
79
|
+
else
|
80
|
+
radians_to_degrees(Math.acos(cos_local_hour_angle))
|
81
|
+
end
|
82
|
+
|
83
|
+
# H = H / 15
|
84
|
+
suns_local_hour_hours = suns_local_hour / 15.0
|
85
|
+
|
86
|
+
# T = H + RA - (0.06571 * t) - 6.622
|
87
|
+
local_mean_time = suns_local_hour_hours + sun_right_ascension_hours - (0.06571 * approximate_time) - 6.622
|
88
|
+
|
89
|
+
# UT = T - lngHour
|
90
|
+
gmt_hours = local_mean_time - longitude_hour
|
91
|
+
gmt_hours -= 24.0 if gmt_hours > 24
|
92
|
+
gmt_hours += 24.0 if gmt_hours < 0
|
93
|
+
|
94
|
+
hour = gmt_hours.floor
|
95
|
+
hour_remainder = (gmt_hours.to_f - hour.to_f) * 60.0
|
96
|
+
minute = hour_remainder.floor
|
97
|
+
seconds = (hour_remainder - minute) * 60.0
|
98
|
+
|
99
|
+
Time.gm(date.year, date.month, date.day, hour, minute, seconds)
|
100
|
+
end
|
101
|
+
|
102
|
+
private
|
103
|
+
|
104
|
+
def self.degrees_to_radians(d)
|
105
|
+
d.to_f / 360.0 * 2.0 * Math::PI
|
106
|
+
end
|
107
|
+
|
108
|
+
def self.radians_to_degrees(r)
|
109
|
+
r.to_f * 360.0 / (2.0 * Math::PI)
|
110
|
+
end
|
111
|
+
|
112
|
+
def self.coerce_degrees(d)
|
113
|
+
if d < 0
|
114
|
+
d += 360
|
115
|
+
return coerce_degrees(d)
|
116
|
+
end
|
117
|
+
if d >= 360
|
118
|
+
d -= 360
|
119
|
+
return coerce_degrees(d)
|
120
|
+
end
|
121
|
+
d
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
|
12
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
13
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
14
|
+
require 'when_sun'
|
15
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
3
|
+
require 'test/unit'
|
4
|
+
require 'when_sun'
|
5
|
+
|
6
|
+
class TestWhenSun < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def test_rise_20100308_pontassieve
|
9
|
+
rise = WhenSun.calculate(:rise, Date.new(2010, 3, 8), 43.779, 11.432)
|
10
|
+
assert_equal(rise.to_i, 1268026793)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_set_20100308_pontassieve
|
14
|
+
rise = WhenSun.calculate(:set, Date.new(2010, 3, 8), 43.779, 11.432)
|
15
|
+
assert_equal(rise.to_i, 1268068276)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_rise_helper
|
19
|
+
rise = WhenSun.rise(Date.new(2010, 3, 8), 43.779, 11.432)
|
20
|
+
assert_equal(rise.to_i, 1268026793)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_set_helper
|
24
|
+
rise = WhenSun.set(Date.new(2010, 3, 8), 43.779, 11.432)
|
25
|
+
assert_equal(rise.to_i, 1268068276)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_midnight_sun_on_20100621_north_cape
|
29
|
+
rise = WhenSun.calculate(:rise, Date.new(2010, 6, 21), 71.170219, 25.785556)
|
30
|
+
assert_nil(rise)
|
31
|
+
set = WhenSun.calculate(:set, Date.new(2010, 6, 21), 71.170219, 25.785556)
|
32
|
+
assert_nil(set)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_unknown_event
|
36
|
+
assert_raise(RuntimeError) { WhenSun.calculate(:foo, Date.new(2010, 3, 8), 43.779, 11.432) }
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
data/when_sun.gemspec
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{when_sun}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Eric Cranston"]
|
12
|
+
s.date = %q{2011-08-18}
|
13
|
+
s.description = %q{WhenSun is a RubyGem for calculating the sunrise and sunset for locations based on the date, latitude and longitude.}
|
14
|
+
s.email = %q{ecranston@enphaseenergy.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"LICENSE.txt",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/when_sun.rb",
|
27
|
+
"test/helper.rb",
|
28
|
+
"test/test_when_sun.rb",
|
29
|
+
"when_sun.gemspec"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/cracell/when_sun}
|
32
|
+
s.licenses = ["MIT"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.4.2}
|
35
|
+
s.summary = %q{WhenSun is a RubyGem for calculating the sunrise and sunset for locations based on the date, latitude and longitude.}
|
36
|
+
|
37
|
+
if s.respond_to? :specification_version then
|
38
|
+
s.specification_version = 3
|
39
|
+
|
40
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
41
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
42
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
43
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
44
|
+
else
|
45
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
46
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
47
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
48
|
+
end
|
49
|
+
else
|
50
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
51
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
52
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: when_sun
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Eric Cranston
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-08-18 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
type: :development
|
23
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 23
|
29
|
+
segments:
|
30
|
+
- 1
|
31
|
+
- 0
|
32
|
+
- 0
|
33
|
+
version: 1.0.0
|
34
|
+
requirement: *id001
|
35
|
+
prerelease: false
|
36
|
+
name: bundler
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
type: :development
|
39
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 7
|
45
|
+
segments:
|
46
|
+
- 1
|
47
|
+
- 6
|
48
|
+
- 4
|
49
|
+
version: 1.6.4
|
50
|
+
requirement: *id002
|
51
|
+
prerelease: false
|
52
|
+
name: jeweler
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
type: :development
|
55
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
requirement: *id003
|
65
|
+
prerelease: false
|
66
|
+
name: rcov
|
67
|
+
description: WhenSun is a RubyGem for calculating the sunrise and sunset for locations based on the date, latitude and longitude.
|
68
|
+
email: ecranston@enphaseenergy.com
|
69
|
+
executables: []
|
70
|
+
|
71
|
+
extensions: []
|
72
|
+
|
73
|
+
extra_rdoc_files:
|
74
|
+
- LICENSE.txt
|
75
|
+
- README.rdoc
|
76
|
+
files:
|
77
|
+
- .document
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.rdoc
|
81
|
+
- Rakefile
|
82
|
+
- VERSION
|
83
|
+
- lib/when_sun.rb
|
84
|
+
- test/helper.rb
|
85
|
+
- test/test_when_sun.rb
|
86
|
+
- when_sun.gemspec
|
87
|
+
has_rdoc: true
|
88
|
+
homepage: http://github.com/cracell/when_sun
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
hash: 3
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
hash: 3
|
111
|
+
segments:
|
112
|
+
- 0
|
113
|
+
version: "0"
|
114
|
+
requirements: []
|
115
|
+
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 1.4.2
|
118
|
+
signing_key:
|
119
|
+
specification_version: 3
|
120
|
+
summary: WhenSun is a RubyGem for calculating the sunrise and sunset for locations based on the date, latitude and longitude.
|
121
|
+
test_files: []
|
122
|
+
|