sun_time 0.0.1

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/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2009-07-23
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,13 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/sun_time.rb
6
+ lib/sun_time/date_ext.rb
7
+ lib/sun_time/degree_trig.rb
8
+ script/console
9
+ script/destroy
10
+ script/generate
11
+ test/test_date_ext.rb
12
+ test/test_degree_trig.rb
13
+ test/test_sun_time.rb
data/README.rdoc ADDED
@@ -0,0 +1,90 @@
1
+ = sun_time
2
+
3
+ * http://github.com/elabs/sun_time
4
+
5
+ == DESCRIPTION:
6
+
7
+ Calculate the time of sunrise and sunset for location on a specific date.
8
+
9
+ == SYNOPSIS:
10
+
11
+ Create a new SunTime instance with a Date and latitude and longitude in decimal format as arguments. Then you can call the `sunrise` and `sunset` methods on that instance.
12
+
13
+ require 'sun_time'
14
+
15
+ date = Date.new(2009, 7, 23)
16
+ latitude = 57.7119 # N 57° 42' 42"
17
+ longitude = 11.9683 # E 11° 58' 5"
18
+
19
+ sun_time = SunTime.new(date, latitude, longitude)
20
+
21
+ sun_time.sunrise
22
+ # => Thu Jul 23 02:50:32 UTC 2009
23
+
24
+ sun_time.sunset
25
+ # => Thu Jul 23 19:48:56 UTC 2009
26
+
27
+ Note that the times returned are always in UTC, so you have to convert them to your local time yourself if you want that.
28
+
29
+ If you try to calculate sunrise or sunset for a location where the sun doesn't rise or set on the chosen date, nil is returned:
30
+
31
+ winter_in_sweden = SunTime.new(Date.new(2009,12,22), 67.8529, 20.2426)
32
+
33
+ winter_in_sweden.sunrise
34
+ # => nil
35
+
36
+ winter_in_sweden.sunset
37
+ # => nil
38
+
39
+ summer_in_sweden = SunTime.new(Date.new(2009,6,22), 67.8529, 20.2426)
40
+
41
+ summer_in_sweden.sunrise
42
+ # => nil
43
+
44
+ summer_in_sweden.sunset
45
+ # => nil
46
+
47
+
48
+ === Date Extension:
49
+
50
+ Optionally, you can use SunTime as an extension to the Date class. Here's how you do that:
51
+
52
+ require 'sun_time'
53
+ require 'sun_time/date_ext
54
+
55
+ date = Date.new(2009, 7, 23)
56
+
57
+ date.sunrise(57.7119, 11.9683)
58
+ # => Thu Jul 23 02:50:32 UTC 2009
59
+
60
+ date.sunset(57.7119, 11.9683)
61
+ # => Thu Jul 23 19:48:56 UTC 2009
62
+
63
+ == INSTALL:
64
+
65
+ sudo gem install sun_time
66
+
67
+ == LICENSE:
68
+
69
+ (The MIT License)
70
+
71
+ Copyright (c) 2009 Edithouse eLabs AB
72
+
73
+ Permission is hereby granted, free of charge, to any person obtaining
74
+ a copy of this software and associated documentation files (the
75
+ 'Software'), to deal in the Software without restriction, including
76
+ without limitation the rights to use, copy, modify, merge, publish,
77
+ distribute, sublicense, and/or sell copies of the Software, and to
78
+ permit persons to whom the Software is furnished to do so, subject to
79
+ the following conditions:
80
+
81
+ The above copyright notice and this permission notice shall be
82
+ included in all copies or substantial portions of the Software.
83
+
84
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
85
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
86
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
87
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
88
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
89
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
90
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/sun_time'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'sun_time' do
14
+ self.developer 'Carl-Johan Kihlbom', 'cj@elabs.se'
15
+ self.rubyforge_name = 'elabs'
16
+ end
17
+
18
+ require 'newgem/tasks'
19
+ Dir['tasks/**/*.rake'].each { |t| load t }
20
+
21
+ # TODO - want other tests/tasks run by default? Add them to the list
22
+ # remove_task :default
23
+ # task :default => [:spec, :features]
@@ -0,0 +1,9 @@
1
+ class Date
2
+ def sunrise(lat, lng)
3
+ SunTime.new(self, lat, lng).sunrise
4
+ end
5
+
6
+ def sunset(lat, lng)
7
+ SunTime.new(self, lat, lng).sunset
8
+ end
9
+ end
@@ -0,0 +1,35 @@
1
+ class SunTime
2
+ module DegreeTrig
3
+ def r2d (v)
4
+ 180/Math::PI * v
5
+ end
6
+
7
+ def d2r (v)
8
+ Math::PI/180 * v
9
+ end
10
+
11
+ def sin (d)
12
+ Math.sin(d2r(d))
13
+ end
14
+
15
+ def asin (d)
16
+ r2d Math.asin(d)
17
+ end
18
+
19
+ def cos (d)
20
+ Math.cos(d2r(d))
21
+ end
22
+
23
+ def acos (d)
24
+ r2d Math.acos(d)
25
+ end
26
+
27
+ def tan (d)
28
+ Math.tan(d2r(d))
29
+ end
30
+
31
+ def atan (d)
32
+ r2d Math.atan(d)
33
+ end
34
+ end
35
+ end
data/lib/sun_time.rb ADDED
@@ -0,0 +1,129 @@
1
+ require 'date'
2
+
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "sun_time/degree_trig"))
4
+
5
+ class SunTime
6
+ VERSION = '0.0.1'
7
+
8
+ class AlwaysDarkError < ::StandardError; end
9
+ class AlwaysLightError < ::StandardError; end
10
+
11
+ attr_reader :date, :lat, :lng
12
+
13
+ def initialize (date, lat, lng)
14
+ @date = date
15
+ @lat = lat
16
+ @lng = lng
17
+ @m = nil
18
+ end
19
+
20
+ def sunrise
21
+ jd2time j_rise
22
+ rescue AlwaysDarkError, AlwaysLightError
23
+ nil
24
+ end
25
+
26
+ def sunset
27
+ jd2time j_set
28
+ rescue AlwaysDarkError, AlwaysLightError
29
+ nil
30
+ end
31
+
32
+ private
33
+ include DegreeTrig
34
+
35
+ # Julian date
36
+ def j_date
37
+ date.jd
38
+ end
39
+
40
+ # West longitude
41
+ def lng_w
42
+ lng * -1
43
+ end
44
+
45
+ # North latitude
46
+ def lat_n
47
+ lat
48
+ end
49
+
50
+ # Mean solar anomaly
51
+ def m(_j_transit=j_star)
52
+ (357.5291 + 0.98560028 * (_j_transit - 2451545)) % 360
53
+ end
54
+
55
+ # Julian cycle since Jan 1, 2000
56
+ def n
57
+ ((j_date.to_f - 2451545 - 0.0009) - (lng_w/360)).round
58
+ end
59
+
60
+ # Equation of center
61
+ def c(_m=m)
62
+ 1.9148*sin(_m) + 0.0200*sin(2*_m) + 0.0003*sin(3*_m)
63
+ end
64
+
65
+ # Ecliptical longitude of the sun
66
+ def sun_l
67
+ _m = @m || m
68
+ c = c(_m)
69
+ (_m + 102.9372 + c + 180) % 360
70
+ end
71
+
72
+ # Declination of the sun
73
+ def sun_d
74
+ recalculate_m
75
+ asin(sin(sun_l) * sin(23.45))
76
+ end
77
+
78
+ # Hour angle (half the arc length of the sun)
79
+ def h
80
+ i = (sin(-0.83) - sin(lat_n) * sin(sun_d)) / (cos(lat_n) * cos(sun_d))
81
+
82
+ if i < -1.0
83
+ raise AlwaysLightError, "The sun is always up at this location on #{date}"
84
+ elsif i > 1.0
85
+ raise AlwaysDarkError, "The sun is always down at this location on #{date}"
86
+ end
87
+
88
+ acos( i )
89
+ end
90
+
91
+ # Julian date of solar noon on cycle n
92
+ # TODO: Iteratively recalculate m using j_transit until it stops changing
93
+ def j_transit(_m=m)
94
+ j_star + (0.0053 * sin(_m)) - (0.0069 * sin(2*sun_l))
95
+ end
96
+
97
+ def recalculate_m
98
+ current_m = m(j_transit)
99
+ last_m = nil
100
+ until current_m == last_m
101
+ last_m = current_m
102
+ current_m = m(j_transit(last_m))
103
+ end
104
+ @m = current_m
105
+ end
106
+
107
+ # Approximate Julian date of solar noon on cycle n
108
+ def j_star
109
+ 2451545 + 0.0009 + (lng_w/360) + n
110
+ end
111
+
112
+ # Julian date of sunrise on cycle n
113
+ def j_rise
114
+ j_transit - (j_set - j_transit)
115
+ end
116
+
117
+ # Julian date of sunset on cycle n
118
+ def j_set
119
+ @j_star = 2451545 + 0.0009 + ((h + lng_w) / 360) + n
120
+
121
+ @j_star + (0.0053 * sin(@m)) - (0.0069 * sin(2*sun_l))
122
+ end
123
+
124
+
125
+ def jd2time (jd)
126
+ dt = DateTime.jd(jd, 12)
127
+ Time.utc(dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec)
128
+ end
129
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/sun_time.rb'}"
9
+ puts "Loading sun_time gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,22 @@
1
+ require "test/unit"
2
+
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "../lib/sun_time/date_ext"))
4
+
5
+ class TestDate < Test::Unit::TestCase
6
+ def setup
7
+ @lat = 57.7119
8
+ @lng = 11.9683
9
+ @date = Date.new(2009, 7, 23)
10
+ end
11
+
12
+ def test_sunrise
13
+ sunrise = Time.mktime(2009, 7, 23, 4, 50, 32)
14
+ assert_equal(sunrise, @date.sunrise(@lat, @lng))
15
+ end
16
+
17
+ def test_sunset
18
+ sunset = Time.mktime(2009, 7, 23, 21, 48, 56)
19
+
20
+ assert_equal(sunset, @date.sunset(@lat, @lng))
21
+ end
22
+ end
@@ -0,0 +1,40 @@
1
+ require "test/unit"
2
+
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "../lib/sun_time/degree_trig"))
4
+
5
+ class TestDegreeTrig < Test::Unit::TestCase
6
+ include SunTime::DegreeTrig
7
+
8
+ def test_sin
9
+ assert_in_delta(0.017452406437284, sin(1), 2 ** -20)
10
+ end
11
+
12
+ def test_cos
13
+ assert_in_delta(0.999847695156391, cos(1), 2 ** -20)
14
+ end
15
+
16
+ def test_tan
17
+ assert_in_delta(0.017455064928218, tan(1), 2 ** -20)
18
+ end
19
+
20
+ def test_asin
21
+ assert_in_delta(r2d(1.57079633), asin(1), 2 ** -20)
22
+ end
23
+
24
+ def test_acos
25
+ assert_in_delta(r2d(0), acos(1), 2 ** -20)
26
+ assert_in_delta(r2d(1.04719755), acos(0.5), 2 ** -20)
27
+ end
28
+
29
+ def test_atan
30
+ assert_in_delta(r2d(0.785398163), atan(1), 2 ** -20)
31
+ end
32
+
33
+ def test_degrees_to_radians
34
+ assert_in_delta(0.0174532925, d2r(1), 2 ** -20)
35
+ end
36
+
37
+ def test_radians_to_degrees
38
+ assert_in_delta(57.2957795, r2d(1), 2 ** -20)
39
+ end
40
+ end
@@ -0,0 +1,82 @@
1
+ require "test/unit"
2
+
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "../lib/sun_time"))
4
+
5
+ class TestSunTime < Test::Unit::TestCase
6
+ def setup
7
+ @lat = 57.7119
8
+ @lng = 11.9683
9
+ @date = Date.new(2009, 7, 23)
10
+ @sun_time = SunTime.new(@date, @lat, @lng)
11
+ end
12
+
13
+ def test_sunrise
14
+ sunrise = Time.mktime(2009, 7, 23, 4, 50, 32)
15
+
16
+ assert_equal(sunrise, @sun_time.sunrise)
17
+ end
18
+
19
+ def test_sunset
20
+ sunset = Time.mktime(2009, 7, 23, 21, 48, 56)
21
+
22
+ assert_equal(sunset, @sun_time.sunset)
23
+ end
24
+
25
+ def test_always_light
26
+ summer_in_sweden = SunTime.new(Date.new(2009,6,22), 67.8529, 20.2426)
27
+ assert_nil(summer_in_sweden.sunrise)
28
+ assert_nil(summer_in_sweden.sunset)
29
+ end
30
+
31
+ def test_always_dark
32
+ winter_in_sweden = SunTime.new(Date.new(2009,12,22), 67.8529, 20.2426)
33
+ assert_nil(winter_in_sweden.sunrise)
34
+ assert_nil(winter_in_sweden.sunset)
35
+ end
36
+
37
+ # Private methods
38
+
39
+ def test_j_date
40
+ assert_equal(2455036, @sun_time.send(:j_date))
41
+ end
42
+
43
+ def test_n
44
+ assert_equal(3491, @sun_time.send(:n))
45
+ end
46
+
47
+ def test_j_star
48
+ assert_in_delta(2455035.9676547, @sun_time.send(:j_star), 2 ** -20)
49
+ end
50
+
51
+ def test_m
52
+ assert_in_delta(198.22779796522, @sun_time.send(:m), 2 ** -20)
53
+ end
54
+
55
+ def test_c
56
+ assert_in_delta(-0.58730215027404, @sun_time.send(:c), 2 ** -20)
57
+ end
58
+
59
+ def test_sun_l
60
+ assert_in_delta(120.57769581495, @sun_time.send(:sun_l), 2 ** -20)
61
+ end
62
+
63
+ def test_j_transit
64
+ assert_in_delta(2455035.9720408, @sun_time.send(:j_transit), 2 ** -20)
65
+ end
66
+
67
+ def test_sun_d
68
+ assert_in_delta(20.03506338786, @sun_time.send(:sun_d), 2 ** -20)
69
+ end
70
+
71
+ def test_h
72
+ assert_in_delta(127.29871041684, @sun_time.send(:h), 2 ** -20)
73
+ end
74
+
75
+ def test_j_set
76
+ assert_in_delta(2455036.3256485, @sun_time.send(:j_set), 2 ** -20)
77
+ end
78
+
79
+ def test_j_rise
80
+ assert_in_delta(2455035.6184334, @sun_time.send(:j_rise), 2 ** -20)
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sun_time
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Carl-Johan Kihlbom
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-23 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.2
24
+ version:
25
+ description: Calculate the time of sunrise and sunset for location on a specific date.
26
+ email:
27
+ - cj@elabs.se
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ files:
36
+ - History.txt
37
+ - Manifest.txt
38
+ - README.rdoc
39
+ - Rakefile
40
+ - lib/sun_time.rb
41
+ - lib/sun_time/date_ext.rb
42
+ - lib/sun_time/degree_trig.rb
43
+ - script/console
44
+ - script/destroy
45
+ - script/generate
46
+ - test/test_date_ext.rb
47
+ - test/test_degree_trig.rb
48
+ - test/test_sun_time.rb
49
+ has_rdoc: true
50
+ homepage: http://github.com/elabs/sun_time
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --main
54
+ - README.rdoc
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project: elabs
72
+ rubygems_version: 1.3.1
73
+ signing_key:
74
+ specification_version: 2
75
+ summary: Calculate the time of sunrise and sunset for location on a specific date.
76
+ test_files:
77
+ - test/test_date_ext.rb
78
+ - test/test_degree_trig.rb
79
+ - test/test_sun_time.rb