Class: Astronoby::TwilightCalculator
- Inherits:
-
Object
- Object
- Astronoby::TwilightCalculator
- Defined in:
- lib/astronoby/events/twilight_calculator.rb
Constant Summary collapse
- TWILIGHTS =
[ CIVIL = :civil, NAUTICAL = :nautical, ASTRONOMICAL = :astronomical ].freeze
- TWILIGHT_ANGLES =
{ CIVIL => Angle.from_degrees(96), NAUTICAL => Angle.from_degrees(102), ASTRONOMICAL => Angle.from_degrees(108) }.freeze
- PERIODS_OF_THE_DAY =
[ MORNING = :morning, EVENING = :evening ].freeze
Instance Method Summary collapse
- #event_on(date, utc_offset: 0) ⇒ Object
- #events_between(start_time, end_time) ⇒ Object
-
#initialize(observer:, ephem:) ⇒ TwilightCalculator
constructor
A new instance of TwilightCalculator.
- #time_for_zenith_angle(date:, period_of_the_day:, zenith_angle:, utc_offset: 0) ⇒ Object
Constructor Details
#initialize(observer:, ephem:) ⇒ TwilightCalculator
Returns a new instance of TwilightCalculator.
22 23 24 25 |
# File 'lib/astronoby/events/twilight_calculator.rb', line 22 def initialize(observer:, ephem:) @observer = observer @ephem = ephem end |
Instance Method Details
#event_on(date, utc_offset: 0) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/astronoby/events/twilight_calculator.rb', line 27 def event_on(date, utc_offset: 0) start_time = Time .new(date.year, date.month, date.day, 0, 0, 0, utc_offset) end_time = Time .new(date.year, date.month, date.day, 23, 59, 59, utc_offset) events = events_between(start_time, end_time) TwilightEvent.new( morning_civil_twilight_time: events.morning_civil_twilight_times.first, evening_civil_twilight_time: events.evening_civil_twilight_times.first, morning_nautical_twilight_time: events.morning_nautical_twilight_times.first, evening_nautical_twilight_time: events.evening_nautical_twilight_times.first, morning_astronomical_twilight_time: events.morning_astronomical_twilight_times.first, evening_astronomical_twilight_time: events.evening_astronomical_twilight_times.first ) end |
#events_between(start_time, end_time) ⇒ Object
50 51 52 53 54 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 |
# File 'lib/astronoby/events/twilight_calculator.rb', line 50 def events_between(start_time, end_time) rts_events = Astronoby::RiseTransitSetCalculator.new( body: Sun, observer: @observer, ephem: @ephem ).events_between(start_time, end_time) equatorial_by_time = {} (rts_events.rising_times + rts_events.setting_times) .compact .each do |event_time| rounded_time = event_time.round next if equatorial_by_time.key?(rounded_time) instant = Instant.from_time(rounded_time) sun_at_time = Sun.new(instant: instant, ephem: @ephem) equatorial_by_time[rounded_time] = sun_at_time.apparent.equatorial end morning_civil = [] evening_civil = [] morning_nautical = [] evening_nautical = [] morning_astronomical = [] evening_astronomical = [] arrays_by_period = { MORNING => { CIVIL => morning_civil, NAUTICAL => morning_nautical, ASTRONOMICAL => morning_astronomical }, EVENING => { CIVIL => evening_civil, NAUTICAL => evening_nautical, ASTRONOMICAL => evening_astronomical } } [ [rts_events.rising_times, MORNING], [rts_events.setting_times, EVENING] ].each do |times, period| times.each do |event_time| next unless event_time equatorial_coordinates = equatorial_by_time[event_time.round] TWILIGHT_ANGLES.each do |twilight, angle| arrays_by_period[period][twilight] << compute_twilight_time_from( period, angle, event_time, equatorial_coordinates ) end end end within_range = ->(time) { time && time >= start_time && time <= end_time } TwilightEvents.new( morning_civil.select(&within_range), evening_civil.select(&within_range), morning_nautical.select(&within_range), evening_nautical.select(&within_range), morning_astronomical.select(&within_range), evening_astronomical.select(&within_range) ) end |
#time_for_zenith_angle(date:, period_of_the_day:, zenith_angle:, utc_offset: 0) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/astronoby/events/twilight_calculator.rb', line 121 def time_for_zenith_angle( date:, period_of_the_day:, zenith_angle:, utc_offset: 0 ) unless PERIODS_OF_THE_DAY.include?(period_of_the_day) raise IncompatibleArgumentsError, "Only #{PERIODS_OF_THE_DAY.join(" or ")} are allowed as " \ "period_of_the_day, got #{period_of_the_day}" end observation_events = get_observation_events(date, utc_offset: utc_offset) midday_instant = create_midday_instant(date, utc_offset: utc_offset) sun_at_midday = Sun.new(instant: midday_instant, ephem: @ephem) equatorial_coordinates = sun_at_midday.apparent.equatorial compute_twilight_time( period_of_the_day, zenith_angle, observation_events, equatorial_coordinates ) end |