tranzito_utils 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 91051949d5689eb724426c9dc4c233f2b6f71eb0d411d63ca0037676b8338cea
4
- data.tar.gz: 9380a70b1a4e798be1ee05d4b183d03f939abc194d0ea0e1d536fd7efe2bf851
3
+ metadata.gz: f9200b266283acc5bca16516873cfa19fd2df8f3da6ebdf3c00678224f797ff5
4
+ data.tar.gz: 3bb4b8c641003c58dbca7925b82fce1e9e9d536a8127bf545587de01a11611a8
5
5
  SHA512:
6
- metadata.gz: a618433b4284b261862e73f4bde3ef116012f1dfb2cbc415837027f4c07abb4569b92de1d75dec6fc678d6f0244c0d467c1044bcdde524e674ee0ea2268e5d53
7
- data.tar.gz: e2e8649aeadce46d8885a3d401021694a7eaae01ffe418ef0358b450f45c423789a59446e4cf16e5d06e764eb8cba8d4bea93d154f934137443bcf4d5881e70f
6
+ metadata.gz: a3bab2fe8fa1c67a8fe1aaee70d8ff6737ab22429cd9392012cf403ee7f519ac8ec18569e54ee335a4a7a16d1976fcebe7e1d733cbb0098e66b5085aa29b4bc9
7
+ data.tar.gz: 6586213cd8e7dabfcf5eb483c41ec57167eacca52e51d5d5de1d54d4b3b5730eb2908e64f54b5dee68e80c1ff3f593c2564fe56159f23afe2be5436c7559298e
@@ -0,0 +1,85 @@
1
+ class SolarPositionCalculator
2
+ LATITUDE_RANGE = (-90..90)
3
+ LONGITUDE_RANGE = (-180..180)
4
+
5
+ attr_reader :latitude, :longitude, :timezone
6
+
7
+ def initialize(latitude, longitude, timezone)
8
+ validate_latitude(latitude)
9
+ validate_longitude(longitude)
10
+ @latitude = latitude
11
+ @longitude = longitude
12
+ @timezone = timezone
13
+ end
14
+
15
+ def sunrise_time(date)
16
+ calculate_sun_time(date, :sunrise)
17
+ end
18
+
19
+ def sunset_time(date)
20
+ calculate_sun_time(date, :sunset)
21
+ end
22
+
23
+ private
24
+
25
+ def calculate_sun_time(date, type)
26
+ validate_date(date)
27
+ utc_time = calculate_utc_time(date, type)
28
+ utc_to_timezone(date, utc_time)
29
+ end
30
+
31
+ def calculate_utc_time(date, type)
32
+ time = Time.new(date.year, date.month, date.day, 12, 0, 0, timezone)
33
+ gamma = calculate_gamma(time)
34
+ eqtime = calculate_eqtime(gamma)
35
+
36
+ decl = calculate_decl(gamma)
37
+ ha = calculate_ha(decl)
38
+ calculate_sun_time_utc(ha, eqtime, type)
39
+ end
40
+
41
+ def calculate_sun_time_utc(ha, eqtime, type)
42
+ if type == :sunrise
43
+ 720 - 4 * (longitude + ha * 180 / Math::PI) - eqtime
44
+ else
45
+ 720 - 4 * (longitude - ha * 180 / Math::PI) - eqtime
46
+ end
47
+ end
48
+
49
+ def calculate_gamma(time)
50
+ 2 * Math::PI / (leap_year?(time.year) ? 366 : 365) * (time.yday - 1 + (time.hour - 12) / 24.0)
51
+ end
52
+
53
+ def calculate_eqtime(gamma)
54
+ 229.18 * (0.000075 + 0.001868 * Math.cos(gamma) - 0.032077 * Math.sin(gamma) - 0.014615 * Math.cos(2 * gamma) - 0.040849 * Math.sin(2 * gamma))
55
+ end
56
+
57
+ def calculate_decl(gamma)
58
+ 0.006918 - 0.399912 * Math.cos(gamma) + 0.070257 * Math.sin(gamma) - 0.006758 * Math.cos(2 * gamma) + 0.000907 * Math.sin(2 * gamma) - 0.002697 * Math.cos(3 * gamma) + 0.00148 * Math.sin(3 * gamma)
59
+ end
60
+
61
+ def calculate_ha(decl)
62
+ Math.acos(Math.cos(Math::PI / 180 * 90.833) / (Math.cos(Math::PI / 180 * latitude) * Math.cos(decl)) - Math.tan(Math::PI / 180 * latitude) * Math.tan(decl))
63
+ end
64
+
65
+ def utc_to_timezone(date, utc_time)
66
+ response = Time.parse("#{date} #{Time.at(utc_time * 60).utc.strftime("%H:%M:%S")}") + timezone * 3600
67
+ response.strftime("%H:%M:%S")
68
+ end
69
+
70
+ def leap_year?(year)
71
+ year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
72
+ end
73
+
74
+ def validate_latitude(latitude)
75
+ raise ArgumentError, "Latitude should be between -90 and 90 degrees" unless LATITUDE_RANGE.include?(latitude)
76
+ end
77
+
78
+ def validate_longitude(longitude)
79
+ raise ArgumentError, "Longitude should be between -180 and 180 degrees" unless LONGITUDE_RANGE.include?(longitude)
80
+ end
81
+
82
+ def validate_date(date)
83
+ raise ArgumentError, "Invalid date" unless date.is_a?(Date)
84
+ end
85
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TranzitoUtils
4
- VERSION = "1.2.1"
4
+ VERSION = "1.3.0"
5
5
  end
@@ -21,4 +21,5 @@ require "tranzito_utils/helpers/sortable_helper"
21
21
  require "tranzito_utils/helpers/helpers"
22
22
  require "tranzito_utils/services/time_parser"
23
23
  require "tranzito_utils/services/normalize"
24
+ require "tranzito_utils/services/solar_position_calculator"
24
25
  require "tranzito_utils/gem"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tranzito_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - willbarrettdev
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2023-03-07 00:00:00.000000000 Z
13
+ date: 2023-06-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -112,6 +112,7 @@ files:
112
112
  - lib/tranzito_utils/helpers/helpers.rb
113
113
  - lib/tranzito_utils/helpers/sortable_helper.rb
114
114
  - lib/tranzito_utils/services/normalize.rb
115
+ - lib/tranzito_utils/services/solar_position_calculator.rb
115
116
  - lib/tranzito_utils/services/time_parser.rb
116
117
  - lib/tranzito_utils/version.rb
117
118
  homepage: https://github.com/Tranzito/tranzito_utils