Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observation events dedicated and centralized class #60

Merged
merged 6 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,31 +100,25 @@ observer = Astronoby::Observer.new(
longitude: Astronoby::Angle.from_degrees(-78)
)
sun = Astronoby::Sun.new(epoch: epoch)
observation_events = sun.observation_events(observer: observer)

sun.rising_time(observer: observer)
observation_events.rising_time
# => 2015-02-05 12:12:59 UTC

sun.rising_azimuth(observer: observer).str(:dms)
observation_events.rising_azimuth.str(:dms)
# => "+109° 46′ 43.1427″"

sun.transit_time(observer: observer)
observation_events.transit_time
# => 2015-02-05 17:25:59 UTC

sun.transit_altitude(observer: observer).str(:dms)
observation_events.transit_altitude.str(:dms)
# => "+36° 8′ 15.7669″"

sun.setting_time(observer: observer)
observation_events.setting_time
# => 2015-02-05 22:39:27 UTC

sun.setting_azimuth(observer: observer).str(:dms)
observation_events.setting_azimuth.str(:dms)
# => "+250° 23′ 33.6177″"

# Also available in more compact forms:
sun.rise_transit_set_times(observer: observer)
# => [..., ..., ...]

sun.rise_set_azimuths(observer: observer)
# => [..., ...]
```

### Solstice and Equinox times
Expand Down
2 changes: 1 addition & 1 deletion lib/astronoby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
require "astronoby/aberration"
require "astronoby/equinox_solstice"
require "astronoby/errors"
require "astronoby/events/observation_events"
require "astronoby/geocentric_parallax"
require "astronoby/mean_obliquity"
require "astronoby/nutation"
require "astronoby/observer"
require "astronoby/precession"
require "astronoby/refraction"
require "astronoby/rise_transit_set"
require "astronoby/time/greenwich_sidereal_time"
require "astronoby/time/local_sidereal_time"
require "astronoby/util/astrodynamics"
Expand Down
107 changes: 29 additions & 78 deletions lib/astronoby/bodies/sun.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,55 +92,36 @@ def horizontal_coordinates(latitude:, longitude:)
.to_horizontal(time: time, latitude: latitude, longitude: longitude)
end

# @param observer [Astronoby::Observer] Observer of the events
# @return [Array<Time, Time, Time>, nil] Sunrise, transit and sunset times
def rise_transit_set_times(observer:)
rise_transit_set(observer).times
end

# @param observer [Astronoby::Observer] Observer of the events
# @return [Array<Astronoby::Angle, Astronoby::Angle>] Azimuths of sunrise
# and sunset
def rise_set_azimuths(observer:)
rise_transit_set(observer).azimuths
end

# @param observer [Astronoby::Observer] Observer of the event
# @return [Time] Time of sunrise
def rising_time(observer:)
rise_transit_set(observer).times[0]
end

# @param observer [Astronoby::Observer] Observer of the event
# @return [Astronoby::Angle, nil] Azimuth of sunrise
def rising_azimuth(observer:)
rise_transit_set(observer).azimuths[0]
end

# @param observer [Astronoby::Observer] Observer of the event
# @return [Time] Time of sunset
def setting_time(observer:)
rise_transit_set(observer).times[2]
end

# @param observer [Astronoby::Observer] Observer of the event
# @return [Astronoby::Angle, nil] Azimuth of sunset
def setting_azimuth(observer:)
rise_transit_set(observer).azimuths[1]
end

def transit_time(observer:)
rise_transit_set(observer).times[1]
end

# @param observer [Astronoby::Observer] Observer of the event
# @return [Astronoby::Angle] Altitude at transit
def transit_altitude(observer:)
rise_transit_set(observer).transit_altitude
# @return [Astronoby::Events::ObservationEvents] Sun's observation events
def observation_events(observer:)
date = Epoch.to_utc(@epoch).to_date
yesterday_epoch = Epoch.from_time(date.prev_day)
tomorrow_epoch = Epoch.from_time(date.next_day)

coordinates_of_the_previous_day = self.class
.new(epoch: yesterday_epoch)
.apparent_ecliptic_coordinates
.to_apparent_equatorial(epoch: yesterday_epoch)
coordinates_of_the_day =
apparent_ecliptic_coordinates.to_apparent_equatorial(epoch: @epoch)
coordinates_of_the_next_day = self.class
.new(epoch: tomorrow_epoch)
.apparent_ecliptic_coordinates
.to_apparent_equatorial(epoch: tomorrow_epoch)

Events::ObservationEvents.new(
observer: observer,
date: date,
coordinates_of_the_previous_day: coordinates_of_the_previous_day,
coordinates_of_the_day: coordinates_of_the_day,
coordinates_of_the_next_day: coordinates_of_the_next_day,
additional_altitude: Angle.from_degrees(angular_size.degrees / 2)
)
end

# @param observer [Astronoby::Observer] Observer of the event
# @return [Time] Time the morning civil twilight starts
# @return [Time, nil] Time the morning civil twilight starts
def morning_civil_twilight_time(observer:)
twilight_time(CIVIL, MORNING, observer)
end
Expand Down Expand Up @@ -256,47 +237,17 @@ def current_date
Epoch.to_utc(@epoch).to_date
end

def rise_transit_set(observer)
@rise_transit_set ||= {}
return @rise_transit_set[observer] if @rise_transit_set.key?(observer)

@rise_transit_set[observer] = begin
date = Epoch.to_utc(@epoch).to_date
yesterday_epoch = Epoch.from_time(date.prev_day)
tomorrow_epoch = Epoch.from_time(date.next_day)

coordinates_of_the_previous_day = self.class
.new(epoch: yesterday_epoch)
.apparent_ecliptic_coordinates
.to_apparent_equatorial(epoch: yesterday_epoch)
coordinates_of_the_day =
apparent_ecliptic_coordinates.to_apparent_equatorial(epoch: @epoch)
coordinates_of_the_next_day = self.class
.new(epoch: tomorrow_epoch)
.apparent_ecliptic_coordinates
.to_apparent_equatorial(epoch: tomorrow_epoch)

RiseTransitSet.new(
observer: observer,
date: date,
coordinates_of_the_previous_day: coordinates_of_the_previous_day,
coordinates_of_the_day: coordinates_of_the_day,
coordinates_of_the_next_day: coordinates_of_the_next_day,
additional_altitude: Angle.from_degrees(angular_size.degrees / 2)
)
end
end

# Source:
# Title: Practical Astronomy with your Calculator or Spreadsheet
# Authors: Peter Duffett-Smith and Jonathan Zwart
# Edition: Cambridge University Press
# Chapter: 50 - Twilight
def twilight_time(twilight, period_of_the_day, observer)
observation_events = observation_events(observer: observer)
period_time = if period_of_the_day == MORNING
rising_time(observer: observer)
observation_events.rising_time
else
setting_time(observer: observer)
observation_events.setting_time
end

hour_angle_at_period = equatorial_coordinates_at_midday
Expand Down
Loading