| 1 |
import math |
| 2 |
from juliandate.__version__ import __version__ |
| 3 |
|
| 4 |
# Algorithms from: |
| 5 |
# |
| 6 |
# Urban, Sean E., and P. Kenneth Seidelmann, eds. Explanatory |
| 7 |
# Supplement to the Astronomical Almanac. 3. ed. Mill Valley, Calif: |
| 8 |
# University Science Books, 2012. |
| 9 |
|
| 10 |
|
| 11 |
def to_julian(J): |
| 12 |
"""Return a Julian calendar date for a Julian Day.""" |
| 13 |
# return __jd_to_date(int(J + 0.5) + 1401) + __h_m_s(J) |
| 14 |
return __jd_to_date(J) + __h_m_s(J) |
| 15 |
|
| 16 |
|
| 17 |
def to_gregorian(J): |
| 18 |
"""Return a Gregorian calendar date for a Julian Day.""" |
| 19 |
j = 1401 |
| 20 |
B = 274277 |
| 21 |
C = -38 |
| 22 |
|
| 23 |
return __jd_to_date(J, True) + __h_m_s(J) |
| 24 |
|
| 25 |
|
| 26 |
def __param_f(J, is_gregorian): |
| 27 |
"""Calculate parameter f for conversion algorithm depending on whether the conversion is to Julian or Gregorian calendar.""" |
| 28 |
# Urban and Seidelmann, Algorithm 4 |
| 29 |
|
| 30 |
# Values from Table 15.14 |
| 31 |
j = 1401 |
| 32 |
B = 274277 |
| 33 |
C = -38 |
| 34 |
|
| 35 |
if is_gregorian: |
| 36 |
# Formulas 1 & 1a |
| 37 |
return J + j + int((int((4 * J + B) / 146_097) * 3) / 4) + C |
| 38 |
|
| 39 |
# Formula 1 |
| 40 |
return J + j |
| 41 |
|
| 42 |
|
| 43 |
def from_gregorian(Y, M, D, H=0, m=0, sec=0, ms=0): |
| 44 |
"""Return a Julian day for a Gregorian calendar date.""" |
| 45 |
return ( |
| 46 |
int((1461 * (Y + 4800 + int((M - 14) / 12))) / 4) |
| 47 |
+ int((367 * (M - 2 - 12 * int((M - 14) / 12))) / 12) |
| 48 |
- int((3 * int((Y + 4900 + int((M - 14) / 12)) / 100)) / 4) |
| 49 |
+ D |
| 50 |
- 32075 |
| 51 |
) + __day_pct(H, m, sec, ms) |
| 52 |
|
| 53 |
|
| 54 |
def from_julian(Y, M, D, H=0, m=0, sec=0, ms=0): |
| 55 |
"""Return a Julian day for a Julian calendar date.""" |
| 56 |
return ( |
| 57 |
367 * Y |
| 58 |
- int((7 * (Y + 5001 + int((M - 9) / 7))) / 4) |
| 59 |
+ int((275 * M) / 9) |
| 60 |
+ D |
| 61 |
+ 1729777 |
| 62 |
+ __day_pct(H, m, sec, ms) |
| 63 |
) |
| 64 |
|
| 65 |
|
| 66 |
def __jd_to_date(jdn, is_gregorian=False): |
| 67 |
"""Base calculation for converting from Julian day. |
| 68 |
|
| 69 |
Urban and Seidelmann, Explanatory Supplement to the Astronomical Almanac. 617–619 |
| 70 |
""" |
| 71 |
if jdn < 0: |
| 72 |
raise ValueError("JDN to convert must greater than 0") |
| 73 |
|
| 74 |
# Round JDN to integer Julian day |
| 75 |
J = int(jdn + 0.5) |
| 76 |
|
| 77 |
# Values from Table 15.14 |
| 78 |
m = 2 |
| 79 |
n = 12 |
| 80 |
p = 1461 |
| 81 |
r = 4 |
| 82 |
s = 153 |
| 83 |
u = 5 |
| 84 |
v = 3 |
| 85 |
w = 2 |
| 86 |
y = 4716 |
| 87 |
|
| 88 |
f = __param_f(J, is_gregorian) |
| 89 |
e = r * f + v |
| 90 |
g = int((e % p) / r) |
| 91 |
h = u * g + w |
| 92 |
|
| 93 |
D = int((h % s) / u) + 1 |
| 94 |
M = ((int(h / s) + m) % n) + 1 |
| 95 |
Y = e / p - y + (n + m - M) / n |
| 96 |
|
| 97 |
Y = int(e / p) - y + int((n + m - M) / n) |
| 98 |
|
| 99 |
return (Y, M, D) |
| 100 |
|
| 101 |
|
| 102 |
def __h_m_s(t): |
| 103 |
"""Convert decimal fraction to hours, minutes, (fractional) seconds.""" |
| 104 |
pct = t - int(t) |
| 105 |
(hour, r) = __tdiv(pct, 24) |
| 106 |
(minutes, r) = __tdiv(r, 60) |
| 107 |
(seconds, r) = __tdiv(r, 60) |
| 108 |
return ((hour + 12) % 24, minutes, seconds, math.floor(r * 1_000_000 + 0.5)) |
| 109 |
|
| 110 |
|
| 111 |
def __tdiv(t, d): |
| 112 |
return (int(d * t), d * t - int(d * t)) |
| 113 |
|
| 114 |
|
| 115 |
def __day_pct(h, m, sec, ms): |
| 116 |
s = sec + (ms / 1_000_000) |
| 117 |
return ((h * 3600 + m * 60 + s) / 86400) - 0.5 |
| 118 |
|
| 119 |
|
| 120 |
def version(): |
| 121 |
return __version__ |