-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GreenwichSiderealTime and LocalSiderealTime (#36)
Time conversion between local civil time (LCT), coordinated universal time (UTC), Greenwich sidereal time (GST) and local sidereal time (LST) were confusing. They were made out of utility functions with a blurry public API due to a mix of exposure and refactoring. This introduces `GreenwichSiderealTime` and `LocalSiderealTime`, objects that can hold information about time related to the sidereal time. With a clear public API, it makes it easier to convert times and understand what is manipulated. ```rb utc_time = Time.utc(2024, 3, 14, 1, 2, 3) longitude = Astronoby::Angle.as_degrees(2.3522) lst = Astronoby::GreenwichSiderealTime .from_utc(utc_time) .to_lst(longitude: longitude) lst.time.to_f # => 12.667470435200839 utc = Astronoby::LocalSiderealTime.new( date: utc_time.to_date, time: lst.time, longitude: longitude ).to_gst.to_utc utc.round # => 2024-03-14 01:02:03 UTC ```
- Loading branch information
1 parent
591fb76
commit 3ca960b
Showing
12 changed files
with
353 additions
and
339 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
|
||
module Astronoby | ||
class GreenwichSiderealTime | ||
JULIAN_CENTURIES_EXPONENTS = [ | ||
BigDecimal("6.697374558"), | ||
BigDecimal("2400.051336"), | ||
BigDecimal("0.000025862") | ||
].freeze | ||
|
||
attr_reader :date, :time | ||
|
||
# Source: | ||
# Title: Practical Astronomy with your Calculator or Spreadsheet | ||
# Authors: Peter Duffett-Smith and Jonathan Zwart | ||
# Edition: Cambridge University Press | ||
# Chapter: 12 - Conversion of UT to Greenwich sidereal time (GST) | ||
def self.from_utc(utc) | ||
date = utc.to_date | ||
julian_day = utc.to_date.ajd | ||
t = (julian_day - Epoch::J2000) / Epoch::DAYS_PER_JULIAN_CENTURY | ||
t0 = ( | ||
(JULIAN_CENTURIES_EXPONENTS[0] + | ||
(JULIAN_CENTURIES_EXPONENTS[1] * t) + | ||
(JULIAN_CENTURIES_EXPONENTS[2] * t * t)) % 24 | ||
).abs | ||
|
||
ut_in_hours = utc.hour + | ||
utc.min / 60.0 + | ||
(utc.sec + utc.subsec) / 3600.0 | ||
|
||
gmst = BigDecimal("1.002737909") * ut_in_hours + t0 | ||
gmst += 24 if gmst.negative? | ||
gmst -= 24 if gmst > 24 | ||
|
||
new(date: date, time: gmst) | ||
end | ||
|
||
def initialize(date:, time:) | ||
@date = date | ||
@time = time | ||
end | ||
|
||
# Source: | ||
# Title: Practical Astronomy with your Calculator or Spreadsheet | ||
# Authors: Peter Duffett-Smith and Jonathan Zwart | ||
# Edition: Cambridge University Press | ||
# Chapter: 13 - Conversion of GST to UT | ||
def to_utc | ||
date = @date | ||
julian_day = @date.ajd | ||
t = (julian_day - Epoch::J2000) / Epoch::DAYS_PER_JULIAN_CENTURY | ||
|
||
t0 = ( | ||
(JULIAN_CENTURIES_EXPONENTS[0] + | ||
(JULIAN_CENTURIES_EXPONENTS[1] * t) + | ||
(JULIAN_CENTURIES_EXPONENTS[2] * t * t)) % 24 | ||
).abs | ||
|
||
a = @time - t0 | ||
a += 24 if a.negative? | ||
a -= 24 if a > 24 | ||
|
||
utc = BigDecimal("0.9972695663") * a | ||
|
||
decimal_hour_to_time(date, utc) | ||
end | ||
|
||
def to_lst(longitude:) | ||
LocalSiderealTime.from_gst(gst: self, longitude: longitude) | ||
end | ||
|
||
private | ||
|
||
def decimal_hour_to_time(date, decimal) | ||
absolute_hour = decimal.abs | ||
hour = absolute_hour.floor | ||
decimal_minute = 60 * (absolute_hour - hour) | ||
absolute_decimal_minute = (60 * (absolute_hour - hour)).abs | ||
minute = decimal_minute.floor | ||
second = 60 * (absolute_decimal_minute - absolute_decimal_minute.floor) | ||
|
||
::Time.utc(date.year, date.month, date.day, hour, minute, second) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
module Astronoby | ||
class LocalSiderealTime | ||
attr_reader :date, :time, :longitude | ||
|
||
# Source: | ||
# Title: Practical Astronomy with your Calculator or Spreadsheet | ||
# Authors: Peter Duffett-Smith and Jonathan Zwart | ||
# Edition: Cambridge University Press | ||
# Chapter: 14 - Local sidereal time (LST) | ||
def self.from_gst(gst:, longitude:) | ||
date = gst.date | ||
time = gst.time + longitude.hours | ||
time += 24 if time.negative? | ||
time -= 24 if time > 24 | ||
|
||
new(date: date, time: time, longitude: longitude) | ||
end | ||
|
||
def initialize(date:, time:, longitude:) | ||
@date = date | ||
@time = time | ||
@longitude = longitude | ||
end | ||
|
||
# Source: | ||
# Title: Practical Astronomy with your Calculator or Spreadsheet | ||
# Authors: Peter Duffett-Smith and Jonathan Zwart | ||
# Edition: Cambridge University Press | ||
# Chapter: 15 - Converting LST to GST | ||
def to_gst | ||
date = @date | ||
time = @time - @longitude.hours | ||
time += 24 if time.negative? | ||
time -= 24 if time > 24 | ||
|
||
GreenwichSiderealTime.new(date: date, time: time) | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.