Module: Astronoby::Util::Time

Defined in:
lib/astronoby/util/time.rb

Class Method Summary collapse

Class Method Details

.local_sidereal_time(time:, longitude:) ⇒ Object



76
77
78
# File 'lib/astronoby/util/time.rb', line 76

def local_sidereal_time(time:, longitude:)
  ut_to_gmst(time.utc) + longitude.hours
end

.lst_to_gmst(lst:, longitude:) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/astronoby/util/time.rb', line 80

def lst_to_gmst(lst:, longitude:)
  gmst = lst - longitude.hours

  # If gmst negative, add 24 hours to the date
  # If gmst is greater than 24, subtract 24 hours from the date
  gmst += 24 if gmst.negative?
  gmst -= 24 if gmst > 24

  gmst
end

.lst_to_ut(date:, longitude:, lst:) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
# File 'lib/astronoby/util/time.rb', line 35

def lst_to_ut(date:, longitude:, lst:)
  gmst = lst_to_gmst(lst: lst, longitude: longitude)
  julian_day = date.ajd
  jd0 = ::DateTime.new(date.year, 1, 1, 0, 0, 0).ajd - 1
  days_into_the_year = julian_day - jd0

  t = (jd0 - Epoch::J1900) / Epoch::DAYS_PER_JULIAN_CENTURY
  r = BigDecimal("6.6460656") +
    BigDecimal("2400.051262") * t +
    BigDecimal("0.00002581") * t * t
  b = 24 - r + 24 * (date.year - 1900)

  # If t0 negative, add 24 hours to the date
  # If t0 is greater than 24, subtract 24 hours from the date
  t0 = BigDecimal("0.0657098") * days_into_the_year - b
  t0 += 24 if t0.negative?

  a = gmst - t0
  a += 24 if a.negative?

  ut = BigDecimal("0.99727") * a

  absolute_hour = ut.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)
  ).round

  ::Time.utc(
    date.year,
    date.month,
    date.day,
    hour,
    minute,
    second
  )
end

.ut_to_gmst(universal_time) ⇒ Object

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)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/astronoby/util/time.rb', line 12

def ut_to_gmst(universal_time)
  julian_day = universal_time.to_date.ajd
  t = (julian_day - Epoch::J2000) / Epoch::DAYS_PER_JULIAN_CENTURY
  t0 = (
    (BigDecimal("6.697374558") +
    (BigDecimal("2400.051336") * t) +
    (BigDecimal("0.000025862") * t * t)) % 24
  ).abs

  ut_in_hours = universal_time.hour +
    universal_time.min / 60.0 +
    (universal_time.sec + universal_time.subsec) / 3600.0

  gmst = BigDecimal("1.002737909") * ut_in_hours + t0

  # If gmst negative, add 24 hours to the date
  # If gmst is greater than 24, subtract 24 hours from the date
  gmst += 24 if gmst.negative?
  gmst -= 24 if gmst > 24

  gmst
end