Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ported Cal2jd #29

Merged
merged 4 commits into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,37 @@ end

end

function cal2jd(iy, im, id)
EYEAR_ALLOWED = -4799
MON_LENGTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

if iy < EYEAR_ALLOWED
throw(ArgumentError("Year is outside of the representable range (< $EYEAR_ALLOWED)"))
end

if im < 1 || im > 12
throw(ArgumentError("Month is outside of the range (1,12)"))
end

ly = ((im == 2 ) && !Bool(iy % 4!=0) && (Bool(iy % 100!=0) || !Bool(iy % 400!=0)))? 1:0 #check if leap year

if ((id < 1) || (id > (MON_LENGTH[im] + ly)))
throw(ArgumentError("Day is outside of permissible range (1, $(MON_LENGTH[im]))"))
end

my = (im - 14) ÷ 12
iypmy = trunc(Int,(iy + my))
jd = MJD
jd1 = float((((1461 * (iypmy + 4800)) ÷ 4)
+ (367 * trunc(Int,(im - 2 - 12 * my))) ÷ 12
- (3 * ((iypmy + 4900) ÷ 100)) ÷ 4
+ trunc(Int,id) - 2432076))

jd, jd1
end



# TAI <-> UTC
@transform UTC TAI ep begin
jd1, jd2 = julian1(ep), julian2(ep)
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ AstroTime.update()
@test Epochs.diff_tdb_tt(jd, 0.5) ≈ Epochs.diff_tdb_tt(jd,0.5,0.0,0.0,0.0,0.0) atol=40e-6
end

@test Epochs.cal2jd(2000, 1, 1) == ERFA.cal2jd(2000, 1, 1)
@test Epochs.cal2jd(2016, 2, 29) == ERFA.cal2jd(2016, 2, 29)
@test TTEpoch(Epochs.cal2jd(2000, 1, 1)...) == TTEpoch(2000, 1, 1)
@test_throws ArgumentError Epochs.cal2jd(-4800, 1, 1)
@test_throws ArgumentError Epochs.cal2jd(2000, 15, 1)
@test_throws ArgumentError Epochs.cal2jd(2000, 1, 40)

@test Epochs.jd2cal(julian1(tt), julian2(tt)) == ERFA.jd2cal(julian1(tt), julian2(tt))
@test Epochs.jd2cal(julian2(tt), julian1(tt)) == ERFA.jd2cal(julian2(tt), julian1(tt))
end
Expand Down