/
AstronomicalTime.jl
268 lines (215 loc) · 6.26 KB
/
AstronomicalTime.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
module AstronomicalTime
__precompile__()
using Convertible
using EarthOrientation
using ERFA
using OptionalData
using RemoteFiles
using Unitful
import Base.Operators: +, -, ==
import Base: convert, isapprox, isless
import Unitful: Time
export Timescale, Epoch, second, seconds, minutes, hours, day, days, +, -,
julian, julian1, julian2, julian_strip, julian1_strip, julian2_strip,
mjd, jd2000, jd1950, in_seconds, in_days, in_centuries,
JULIAN_CENTURY, SEC_PER_DAY, SEC_PER_CENTURY, MJD, J2000, J1950,
@timescale
const second = u"s"
const seconds = 1.0second
const minutes = 60.0second
const hours = 3600.0second
const day = u"d"
const days = 1.0day
const JULIAN_CENTURY = 36525.0days
const SEC_PER_DAY = 86400.0seconds
const SEC_PER_CENTURY = second(JULIAN_CENTURY)
const MJD = 2400000.5days
const J2000 = Dates.datetime2julian(DateTime(2000, 1, 1, 12, 0, 0)) * days
const J1950 = Dates.datetime2julian(DateTime(1950, 1, 1, 12, 0, 0)) * days
"""
All timescales are subtypes of the abstract type `Timescale`.
The following timescales are defined:
* `UTC` — Coordinated Universal Time
* `UT1` — Universal Time
* `TAI` — International Atomic Time
* `TT` — Terrestrial Time
* `TCG` — Geocentric Coordinate Time
* `TCB` — Barycentric Coordinate Time
* `TDB` — Barycentric Dynamical Time
"""
abstract type Timescale end
Base.show{T<:Timescale}(io::IO, ::Type{T}) = print(io, T.name.name)
struct Epoch{T<:Timescale}
jd1::typeof(days)
jd2::typeof(days)
function Epoch{T}(jd1::Time, jd2::Time=0.0days) where T<:Timescale
new{T}(day(jd1), day(jd2))
end
end
function Base.show{T<:Timescale}(io::IO, ep::Epoch{T})
print(io, "$(Dates.format(DateTime(ep),
"yyyy-mm-ddTHH:MM:SS.sss")) $(T.name.name)")
end
"""
Epoch{T}(jd1, jd2=0.0) where T<:Timescale
Construct an `Epoch` with timescale `T` from a two-part Julian date.
# Example
```jldoctest
julia> Epoch{TT}(2.4578265e6, 0.30440190993249416)
2017-03-14T07:18:20.325 TT
```
"""
function Epoch{T}(jd1::Float64, jd2::Float64=0.0) where T<:Timescale
Epoch{T}(jd1 * days, jd2 * days)
end
"""
Epoch{T}(year, month, day,
hour=0, minute=0, seconds=0, milliseconds=0) where T<:Timescale
Construct an `Epoch` with timescale `T` at the given date and time.
# Example
```jldoctest
julia> Epoch{TT}(2017, 3, 14, 7, 18, 20, 325)
2017-03-14T07:18:20.325 TT
```
"""
function Epoch{T}(year, month, day,
hour=0, minute=0, seconds=0, milliseconds=0) where T<:Timescale
jd, jd1 = eraDtf2d(string(T.name.name),
year, month, day, hour, minute, seconds + milliseconds/1000)
Epoch{T}(jd, jd1)
end
"""
Epoch{T}(dt::DateTime) where T<:Timescale
Convert a `DateTime` object to an `Epoch` with timescale `T`.
# Example
```jldoctest
julia> Epoch{TT}(DateTime(2017, 3, 14, 7, 18, 20, 325))
2017-03-14T07:18:20.325 TT
```
"""
function Epoch{T}(dt::DateTime) where T<:Timescale
Epoch{T}(Dates.year(dt), Dates.month(dt), Dates.day(dt),
Dates.hour(dt), Dates.minute(dt),
Dates.second(dt) + Dates.millisecond(dt)/1000)
end
"""
DateTime{T<:Timescale}(ep::Epoch{T})
Convert an `Epoch` with timescale `T` to a `DateTime` object.
# Example
```jldoctest
julia> DateTime(Epoch{TT}(2017, 3, 14, 7, 18, 20, 325))
2017-03-14T07:18:20.325
```
"""
function Base.DateTime{T<:Timescale}(ep::Epoch{T})
dt = eraD2dtf(string(T.name.name), 3, julian1_strip(ep), julian2_strip(ep))
DateTime(dt...)
end
"""
Epoch{T}(ep::Epoch{S}) where {T<:Timescale, S<:Timescale}
Convert an `Epoch` with timescale `S` to an `Epoch` with timescale `T`.
# Example
```jldoctest
julia> Epoch{TT}(Epoch{TAI}(2000, 1, 1))
2000-01-01T00:00:32.184 TT
```
"""
function Epoch{T}(ep::Epoch{S}) where {T<:Timescale,S<:Timescale}
@convert convert(Epoch{T}, ep)
end
Epoch{T}(ep::Epoch{T}) where T<:Timescale = ep
"""
Epoch{T}(timestamp::AbstractString) where T<:Timescale
Construct an `Epoch` with timescale `T` from a timestamp.
# Example
```jldoctest
julia> Epoch{TT}("2017-03-14T07:18:20.325")
2017-03-14T07:18:20.325 TT
```
"""
Epoch{T}(str::AbstractString) where T<:Timescale = Epoch{T}(DateTime(str))
julian1(ep) = ep.jd1
julian2(ep) = ep.jd2
julian1_strip(ep) = ustrip(ep.jd1)
julian2_strip(ep) = ustrip(ep.jd2)
julian(ep) = julian1(ep) + julian2(ep)
julian_strip(ep) = julian1_strip(ep) + julian2_strip(ep)
mjd(ep) = julian(ep) - MJD
jd2000(ep) = julian(ep) - J2000
jd1950(ep) = julian(ep) - J1950
in_centuries(ep::Epoch, base=J2000) = (julian(ep) - base) / JULIAN_CENTURY
in_days(ep, base=J2000) = julian(ep) - base
in_seconds(ep, base=J2000) = second(julian(ep) - base)
dut1(ep::Epoch) = getΔUT1(julian_strip(ep))
function isapprox{T<:Timescale}(a::Epoch{T}, b::Epoch{T})
return julian(a) ≈ julian(b)
end
function (==){T<:Timescale}(a::Epoch{T}, b::Epoch{T})
return DateTime(a) == DateTime(b)
end
isless{T<:Timescale}(ep1::Epoch{T}, ep2::Epoch{T}) = julian(ep1) < julian(ep2)
function (+){T}(ep::Epoch{T}, dt::Unitful.Time)
if abs(dt) >= days
return Epoch{T}(ep.jd1 + day(dt), ep.jd2)
else
return Epoch{T}(ep.jd1, ep.jd2 + day(dt))
end
end
function (-){T}(ep::Epoch{T}, dt::Unitful.Time)
if abs(dt) >= days
return Epoch{T}(ep.jd1 - day(dt), ep.jd2)
else
return Epoch{T}(ep.jd1, ep.jd2 - day(dt))
end
end
function (-){T}(ep1::Epoch{T}, ep2::Epoch{T})
(ep1.jd1 - ep2.jd1) + (ep1.jd2 - ep2.jd2)
end
const scales = (
:TAI,
:TT,
:UTC,
:UT1,
:TCG,
:TCB,
:TDB,
)
for scale in scales
epoch = Symbol(scale, "Epoch")
@eval begin
immutable $scale <: Timescale end
@convertible const $epoch = Epoch{$scale}
export $scale, $epoch
end
end
"""
@timescale scale
Define a new timescale and the corresponding `Epoch` type alias.
# Example
```jldoctest
julia> @timescale Custom
julia> Custom <: Timescale
true
julia> CustomEpoch == Epoch{Custom}
true
```
"""
macro timescale(scale)
if !(scale isa Symbol)
error("Invalid time scale name.")
end
epoch = Symbol(scale, "Epoch")
return quote
immutable $(esc(scale)) <: Timescale end
@convertible const $(esc(epoch)) = Epoch{$(esc(scale))}
end
end
include("leapseconds.jl")
include("conversions.jl")
function update()
EarthOrientation.update()
download(LSK_FILE)
push!(LSK_DATA, path(LSK_FILE))
nothing
end
end # module