Skip to content

Commit

Permalink
Use observer object for geocentric parallax (#79)
Browse files Browse the repository at this point in the history
Before, `Astronoby::GeocentricParallax::for_equatorial_coordinates` used
to require the `latitude`, `longitude` and `elevation` of the observer.

Now, for consistency and ease of use, it requires a
`Astronoby::Observer` object instead.
  • Loading branch information
rhannequin authored May 30, 2024
1 parent 0577b48 commit 881488a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 45 deletions.
4 changes: 1 addition & 3 deletions lib/astronoby/bodies/moon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ def apparent_equatorial_coordinates
def horizontal_coordinates(observer:)
apparent_topocentric_equatorial_coordinates =
Astronoby::GeocentricParallax.for_equatorial_coordinates(
latitude: observer.latitude,
longitude: observer.longitude,
elevation: observer.elevation,
observer: observer,
time: @time,
coordinates: apparent_equatorial_coordinates,
distance: distance
Expand Down
42 changes: 18 additions & 24 deletions lib/astronoby/geocentric_parallax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ def self.angle(distance:)
end

# Correct equatorial coordinates with the equatorial horizontal parallax
# @param latitude [Astronoby::Angle] Observer's latitude
# @param longitude [Astronoby::Angle] Observer's longitude
# @param elevation [Astronoby::Distance] Observer's elevation above sea level
# @param observer [Astronoby::Observer] Observer
# @param time [Time] Date-time of the observation
# @param coordinates [Astronoby::Coordinates::Equatorial]
# Equatorial coordinates of the observed body
Expand All @@ -28,52 +26,42 @@ def self.angle(distance:)
# @return [Astronoby::Coordinates::Equatorial] Apparent equatorial
# coordinates with equatorial horizontal parallax
def self.for_equatorial_coordinates(
latitude:,
longitude:,
elevation:,
observer:,
time:,
coordinates:,
distance:
)
new(
latitude,
longitude,
elevation,
observer,
time,
coordinates,
distance
).apply
end

# @param latitude [Astronoby::Angle] Observer's latitude
# @param longitude [Astronoby::Angle] Observer's longitude
# @param elevation [Astronoby::Distance] Observer's elevation above sea level
# @param observer [Astronoby::Observer] Observer
# @param time [Time] Date-time of the observation
# @param coordinates [Astronoby::Coordinates::Equatorial] Equatorial
# coordinates of the observed body
# @param distance [Astronoby::Distance] Distance of the observed body from
# the center of the Earth
def initialize(
latitude,
longitude,
elevation,
observer,
time,
coordinates,
distance
)
@latitude = latitude
@longitude = longitude
@elevation = elevation
@observer = observer
@time = time
@coordinates = coordinates
@distance = distance
end

def apply
term1 = Angle.atan(Constants::EARTH_FLATTENING_CORRECTION * @latitude.tan)
quantity1 = term1.cos + elevation_ratio * @latitude.cos
term1 = Angle.atan(Constants::EARTH_FLATTENING_CORRECTION * latitude.tan)
quantity1 = term1.cos + elevation_ratio * latitude.cos
quantity2 = Constants::EARTH_FLATTENING_CORRECTION * term1.sin +
elevation_ratio * @latitude.sin
elevation_ratio * latitude.sin

term1 = -quantity1 * equatorial_horizontal_parallax.sin * hour_angle.sin
term2 = declination.cos - quantity1 * equatorial_horizontal_parallax.sin * hour_angle.cos
Expand All @@ -92,6 +80,10 @@ def apply

private

def latitude
@observer.latitude
end

def right_ascension
@coordinates.right_ascension
end
Expand All @@ -101,12 +93,14 @@ def declination
end

def hour_angle
@_hour_angle ||=
@coordinates.compute_hour_angle(time: @time, longitude: @longitude)
@_hour_angle ||= @coordinates.compute_hour_angle(
time: @time,
longitude: @observer.longitude
)
end

def elevation_ratio
@elevation.meters / Constants::EARTH_EQUATORIAL_RADIUS_IN_METERS.to_f
@observer.elevation.meters / Constants::EARTH_EQUATORIAL_RADIUS_IN_METERS.to_f
end

def equatorial_horizontal_parallax
Expand Down
36 changes: 18 additions & 18 deletions spec/astronoby/geocentric_parallax_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@

describe "::for_equatorial_coordinates" do
it "returns equatorial coordinates" do
elevation = Astronoby::Distance.zero
latitude = Astronoby::Angle.zero
longitude = Astronoby::Angle.zero
observer = Astronoby::Observer.new(
latitude: Astronoby::Angle.zero,
longitude: Astronoby::Angle.zero,
elevation: Astronoby::Distance.zero
)
time = Time.new
true_coordinates = Astronoby::Coordinates::Equatorial.new(
right_ascension: Astronoby::Angle.zero,
Expand All @@ -51,9 +53,7 @@
distance = Astronoby::Distance.from_meters(100_000_000)

apparent_coordinates = described_class.for_equatorial_coordinates(
latitude: latitude,
longitude: longitude,
elevation: elevation,
observer: observer,
time: time,
coordinates: true_coordinates,
distance: distance
Expand All @@ -68,9 +68,11 @@
# Edition: Cambridge University Press
# Chapter: 39 - Calculating correction for parallax
it "returns the corrected equatorial coordinates for the Moon" do
latitude = Astronoby::Angle.from_degrees(50)
longitude = Astronoby::Angle.from_degrees(-100)
elevation = Astronoby::Distance.from_meters(60)
observer = Astronoby::Observer.new(
latitude: Astronoby::Angle.from_degrees(50),
longitude: Astronoby::Angle.from_degrees(-100),
elevation: Astronoby::Distance.from_meters(60)
)
time = Time.utc(1979, 2, 26, 16, 45)
true_coordinates = Astronoby::Coordinates::Equatorial.new(
right_ascension: Astronoby::Angle.from_hms(22, 35, 19),
Expand All @@ -81,9 +83,7 @@
)

apparent_coordinates = described_class.for_equatorial_coordinates(
latitude: latitude,
longitude: longitude,
elevation: elevation,
observer: observer,
time: time,
coordinates: true_coordinates,
distance: distance
Expand All @@ -101,9 +101,11 @@
# Edition: Cambridge University Press
# Chapter: 39 - Calculating correction for parallax
it "returns the corrected equatorial coordinates for the Sun" do
latitude = Astronoby::Angle.from_degrees(50)
longitude = Astronoby::Angle.from_degrees(-100)
elevation = Astronoby::Distance.from_meters(60)
observer = Astronoby::Observer.new(
latitude: Astronoby::Angle.from_degrees(50),
longitude: Astronoby::Angle.from_degrees(-100),
elevation: Astronoby::Distance.from_meters(60)
)
time = Time.utc(1979, 2, 26, 16, 45)
true_coordinates = Astronoby::Coordinates::Equatorial.new(
right_ascension: Astronoby::Angle.from_hms(22, 36, 44),
Expand All @@ -112,9 +114,7 @@
distance = Astronoby::Distance.from_au(0.9901)

apparent_coordinates = described_class.for_equatorial_coordinates(
latitude: latitude,
longitude: longitude,
elevation: elevation,
observer: observer,
time: time,
coordinates: true_coordinates,
distance: distance
Expand Down

0 comments on commit 881488a

Please sign in to comment.