Skip to content

Commit

Permalink
improve time
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagohm committed Dec 26, 2024
1 parent f1389cd commit 68c4a84
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 23 deletions.
14 changes: 7 additions & 7 deletions scripts/astropy/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
def time(scale: str, hasLocation: bool):
location = EarthLocation.from_geodetic('-45d', '-23d', 890 * u.m, 'WGS84') if hasLocation else None
t = Time('2020-10-07T12:00:00', format='isot', scale=scale, location=location)
print('expect(ut1(time)).toMatchTime([{0}, {1:.18f}, Timescale.UT1])'.format(t.ut1.jd1, t.ut1.jd2))
print('expect(utc(time)).toMatchTime([{0}, {1:.18f}, Timescale.UTC])'.format(t.utc.jd1, t.utc.jd2))
print('expect(tai(time)).toMatchTime([{0}, {1:.18f}, Timescale.TAI])'.format(t.tai.jd1, t.tai.jd2))
print('expect(tt(time)).toMatchTime([{0}, {1:.18f}, Timescale.TT])'.format(t.tt.jd1, t.tt.jd2))
print('expect(tcg(time)).toMatchTime([{0}, {1:.18f}, Timescale.TCG])'.format(t.tcg.jd1, t.tcg.jd2))
print('expect(tdb(time)).toMatchTime([{0}, {1:.18f}, Timescale.TDB])'.format(t.tdb.jd1, t.tdb.jd2))
print('expect(tcb(time)).toMatchTime([{0}, {1:.18f}, Timescale.TCB])'.format(t.tcb.jd1, t.tcb.jd2))
print('expect(ut1(t)).toMatchTime(time({0:.0f}, {1:.18f}, Timescale.UT1, false))'.format(t.ut1.jd1, t.ut1.jd2))
print('expect(utc(t)).toMatchTime(time({0:.0f}, {1:.18f}, Timescale.UTC, false))'.format(t.utc.jd1, t.utc.jd2))
print('expect(tai(t)).toMatchTime(time({0:.0f}, {1:.18f}, Timescale.TAI, false))'.format(t.tai.jd1, t.tai.jd2))
print('expect(tt(t)).toMatchTime(time({0:.0f}, {1:.18f}, Timescale.TT, false))'.format(t.tt.jd1, t.tt.jd2))
print('expect(tcg(t)).toMatchTime(time({0:.0f}, {1:.18f}, Timescale.TCG, false))'.format(t.tcg.jd1, t.tcg.jd2))
print('expect(tdb(t)).toMatchTime(time({0:.0f}, {1:.18f}, Timescale.TDB, false))'.format(t.tdb.jd1, t.tdb.jd2))
print('expect(tcb(t)).toMatchTime(time({0:.0f}, {1:.18f}, Timescale.TCB, false))'.format(t.tcb.jd1, t.tcb.jd2))

if location != None:
x, y, z = location.to_geocentric()
Expand Down
31 changes: 21 additions & 10 deletions src/time.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ test('ut1', () => {
expect(t.fraction).toBe(0)

expect(ut1(t)).toMatchTime(time(2459130, 0, Timescale.UT1, false))
expect(utc(t)).toMatchTime(time(2459130, 0.000001988640612458, Timescale.UTC, false), 1e-13)
expect(tai(t)).toMatchTime(time(2459130, 0.000430229381353175, Timescale.TAI, false), 1e-13)
expect(tt(t)).toMatchTime(time(2459130, 0.000802729381353175, Timescale.TT, false), 1e-13)
expect(tcg(t)).toMatchTime(time(2459130, 0.000813870140404485, Timescale.TCG, false), 1e-13)
expect(tdb(t)).toMatchTime(time(2459130, 0.000802709826729233, Timescale.TDB, false), 1e-13)
expect(tcb(t)).toMatchTime(time(2459130, 0.001050568932858317, Timescale.TCB, false), 1e-13)
expect(utc(t)).toMatchTime(time(2459130, 0.000001988640612458, Timescale.UTC, false))
expect(tai(t)).toMatchTime(time(2459130, 0.000430229381353175, Timescale.TAI, false))
expect(tt(t)).toMatchTime(time(2459130, 0.000802729381353175, Timescale.TT, false))
expect(tcg(t)).toMatchTime(time(2459130, 0.000813870140404485, Timescale.TCG, false))
expect(tdb(t)).toMatchTime(time(2459130, 0.000802709826729233, Timescale.TDB, false))
expect(tcb(t)).toMatchTime(time(2459130, 0.001050568932858317, Timescale.TCB, false))
})

test('utc', () => {
Expand Down Expand Up @@ -211,16 +211,27 @@ test('location', () => {
expect(t.day).toBe(2459130)
expect(t.fraction).toBe(0)

expect(tt(t)).toMatchTime(time(2459130, 0.000000019543382764, Timescale.TT, false), 13)
expect(ut1(t).location).toBe(t.location)
expect(ut1(t)).toMatchTime(time(2459130, -0.000802709843032912, Timescale.UT1, false))
expect(utc(t)).toMatchTime(time(2459130, -0.000800721197357957, Timescale.UTC, false))
expect(tai(t)).toMatchTime(time(2459130, -0.000372480456617236, Timescale.TAI, false))
expect(tt(t)).toMatchTime(time(2459130, 0.000000019543382764, Timescale.TT, false))
expect(tcg(t)).toMatchTime(time(2459130, 0.000011160301874642, Timescale.TCG, false))
expect(tdb(t)).toMatchTime(time(2459130, 0, Timescale.TDB, false))
expect(tcb(t)).toMatchTime(time(2459130, 0.00024785909368291, Timescale.TCB, false))

t = timeYMDHMS(2020, 10, 7, 12, 0, 0, Timescale.TT)
t = timeYMDHMS(2020, 10, 7, 12, 0, 0, Timescale.UT1)
t.location = location(deg(-45), deg(-23), meter(890), GeoId.WGS84)

expect(t.day).toBe(2459130)
expect(t.fraction).toBe(0)

expect(tdb(t)).toMatchTime(time(2459130, -0.000000019543382764, Timescale.TDB, false), 13)
expect(ut1(t)).toMatchTime(time(2459130, 0, Timescale.UT1, false))
expect(utc(t)).toMatchTime(time(2459130, 0.000001988640612458, Timescale.UTC, false))
expect(tai(t)).toMatchTime(time(2459130, 0.000430229381353175, Timescale.TAI, false))
expect(tt(t)).toMatchTime(time(2459130, 0.000802729381353175, Timescale.TT, false))
expect(tcg(t)).toMatchTime(time(2459130, 0.000813870140404485, Timescale.TCG, false))
expect(tdb(t)).toMatchTime(time(2459130, 0.000802709837905048, Timescale.TDB, false))
expect(tcb(t)).toMatchTime(time(2459130, 0.001050568944034133, Timescale.TCB, false))
})

test('extra', () => {
Expand Down
38 changes: 32 additions & 6 deletions src/time.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Angle } from './angle'
import { DAYSEC, DAYSPERJC, DAYSPERJY, DTY, J2000, MJD0, TTMINUSTAI } from './constants'
import { DAYSEC, DAYSPERJC, DAYSPERJY, DTY, J2000, MJD0 } from './constants'
import { eraCalToJd, eraDat, eraDtDb, eraEra00, eraGmst06, eraGst06a, eraJdToCal, eraNut06a, eraObl06, eraPmat06, eraPnm06a, eraPom00, eraSp00, eraTaiTt, eraTaiUt1, eraTaiUtc, eraTcbTdb, eraTcgTt, eraTdbTcb, eraTdbTt, eraTtTai, eraTtTcg, eraTtTdb, eraUt1Tai, eraUt1Utc, eraUtcTai, eraUtcUt1 } from './erfa'
import { iersab } from './iers'
import { delta } from './iers'
import { itrs } from './itrs'
import { type Location } from './location'
import { twoProduct, twoSum } from './math'
Expand Down Expand Up @@ -48,6 +48,7 @@ export interface Time {
tdbMinusTt?: TimeDelta
// taiMinusUtc?: TimeDelta
ut1MinusTai?: TimeDelta
ut1MinusUtc?: TimeDelta

location?: Location
extra?: TimeExtra
Expand Down Expand Up @@ -233,7 +234,7 @@ export function ut1(time: Time): Time {
let ret: Time

if (scale === Timescale.TAI) ret = makeTime(eraTaiUt1(day, fraction, (time.ut1MinusTai ?? ut1MinusTai)(time)), time, Timescale.UT1)
else if (scale === Timescale.UTC) ret = makeTime(eraUtcUt1(day, fraction, iersab.delta(time)), time, Timescale.UT1)
else if (scale === Timescale.UTC) ret = makeTime(eraUtcUt1(day, fraction, (time.ut1MinusUtc ?? ut1MinusUtc)(time)), time, Timescale.UT1)
else ret = ut1(utc(time))

extra(ret, time)
Expand All @@ -250,7 +251,7 @@ export function utc(time: Time): Time {

let ret: Time

if (scale === Timescale.UT1) ret = makeTime(eraUt1Utc(day, fraction, iersab.delta(time)), time, Timescale.UTC)
if (scale === Timescale.UT1) ret = makeTime(eraUt1Utc(day, fraction, (time.ut1MinusUtc ?? ut1MinusUtc)(time)), time, Timescale.UTC)
else if (scale === Timescale.TAI) ret = makeTime(eraTaiUtc(day, fraction), time, Timescale.UTC)
else ret = utc(tai(time))

Expand Down Expand Up @@ -435,12 +436,37 @@ export function equationOfOrigins(time: Time): Mat3 {
return equationOfOrigins
}

// Computes UT1 - UTC in seconds at time.
export const ut1MinusUtc: TimeDelta = (time) => {
// https://github.com/astropy/astropy/blob/71a2eafd6c09f1992f8b4132e6e40ba68a675bde/astropy/time/core.py#L2554
// Interpolate UT1-UTC in IERS table
const d = delta(time)

// If we interpolated using UT1, we may be off by one
// second near leap seconds (and very slightly off elsewhere)
if (time.scale === Timescale.UT1) {
const a = eraUt1Utc(time.day, time.fraction, d)
// Calculate a better estimate using the nearly correct UTC
return delta({ day: a[0], fraction: a[1], scale: Timescale.UTC })
}

return d
}

// Computes TDB - TT in seconds at time.
export const tdbMinusTt: TimeDelta = (time) => {
const { day, fraction, scale } = time

if (scale === Timescale.TDB || scale === Timescale.TT) {
const ut = normalize(day - 0.5, fraction - TTMINUSTAI / DAYSEC).fraction
// First go from the current input time (which is either
// TDB or TT) to an approximate UT1. Since TT and TDB are
// pretty close (few msec?), assume TT. Similarly, since the
// UT1 terms are very small, use UTC instead of UT1.
// https://github.com/astropy/astropy/blob/71a2eafd6c09f1992f8b4132e6e40ba68a675bde/astropy/time/core.py#L2597
const a = eraTaiUtc(...eraTtTai(day, fraction))

// Subtract 0.5, so UT is fraction of the day from midnight
const ut = normalize(a[0] - 0.5, a[1]).fraction

if (time.location) {
const [x, y, z] = itrs(time.location)
Expand Down Expand Up @@ -478,6 +504,6 @@ export const taiMinusUtc: TimeDelta = (time) => {
export const ut1MinusTai: TimeDelta = (time) => {
const cal = eraJdToCal(time.day, time.fraction)
const dat = eraDat(cal[0], cal[1], cal[2], cal[3])
const dut1 = iersab.delta(time)
const dut1 = (time.ut1MinusUtc ?? ut1MinusUtc)(time)
return dut1 - dat
}

0 comments on commit 68c4a84

Please sign in to comment.