Skip to content

Commit 76d893b

Browse files
committed
Some more tests
1 parent fc6b822 commit 76d893b

File tree

6 files changed

+243
-159
lines changed

6 files changed

+243
-159
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ available on the `main` branch.
1414

1515
### Fixed
1616

17-
- `tt2tdb()` Had a wrong scaling in sinusoidal period., resulting in an error of up to +/- 1.7 ms.
17+
- `tt2tdb()` Had a wrong scaling in sinusoidal period, resulting in an error of up to +/- 1.7 ms.
1818

1919
- `tdb2tt()` had the wrong time offset in NOVAS C 3.1. Its zero was defined at J2000, whereas it should have been
2020
1977 January 1, 0h 0m 0s TAI, corrssponding to JD<sub>TT</sub> = 2443144.5003725.

include/novas.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,7 @@ double novas_diff_time(const novas_timespec *t1, const novas_timespec *t2);
11051105

11061106
double novas_diff_coordinate_time(const novas_timespec *t1, const novas_timespec *t2);
11071107

1108-
int novas_increment_time(const novas_timespec *time, double seconds, novas_timespec *out);
1108+
int novas_offset_time(const novas_timespec *time, double seconds, novas_timespec *out);
11091109

11101110

11111111
// in frames.c

src/novas.c

+15-11
Original file line numberDiff line numberDiff line change
@@ -5349,6 +5349,10 @@ double get_ut1_to_tt(int leap_seconds, double dut1) {
53495349
* <ol>
53505350
* <li>Fairhead, L. & Bretagnon, P. (1990) Astron. & Astrophys. 229, 240.</li>
53515351
* <li>Kaplan, G. (2005), US Naval Observatory Circular 179.</li>
5352+
* <li><a href="https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/FORTRAN/req/time.html#The%20Relationship%20between%20TT%20and%20TDB">
5353+
* https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/FORTRAN/req/time.html</a></li>
5354+
* <li><a href="https://gssc.esa.int/navipedia/index.php/Transformations_between_Time_Systems">
5355+
* https://gssc.esa.int/navipedia/index.php/Transformations_between_Time_Systems</a></li>
53525356
* </ol>
53535357
*
53545358
* @param jd_tdb [day] Barycentric Dynamic Time (TDB) based Julian date
@@ -5368,6 +5372,11 @@ int tdb2tt(double jd_tdb, double *jd_tt, double *secdiff) {
53685372
+ 0.000005 * sin(606.9777 * t + 4.0212) + 0.000005 * sin(52.9691 * t + 0.4444) + 0.000002 * sin(21.3299 * t + 5.5431)
53695373
+ 0.000010 * t * sin(628.3076 * t + 4.2490);
53705374

5375+
// The simpler formula with a precision of ~30 us.
5376+
// const double t = (jd_tt - JD_J2000) / JULIAN_CENTURY_DAYS;
5377+
// const double g = 6.239996 + 630.0221385924 * t;
5378+
// const double d = 0.001657 * sin(g + 0.01671 * sin(g));
5379+
53715380
if(jd_tt)
53725381
*jd_tt = jd_tdb - d / DAY;
53735382
if(secdiff)
@@ -5379,17 +5388,18 @@ int tdb2tt(double jd_tdb, double *jd_tt, double *secdiff) {
53795388
/**
53805389
* Returns the TDB - TT time difference in seconds for a given TT date.
53815390
*
5382-
*
5383-
* Note, as of version 1.1, it uses the same calculation as the more precise original tdb2tt().
5391+
* Note, as of version 1.1, it uses the same calculation as the more precise original tdb2tt(). It thus has an acuracy of
5392+
* about 10 &mu;s vs around 30 &mu;s with the simpler formula from the references below.
53845393
*
53855394
*
53865395
* REFERENCES
53875396
* <ol>
5397+
* <li>Fairhead, L. & Bretagnon, P. (1990) Astron. & Astrophys. 229, 240.</li>
5398+
* <li>Kaplan, G. (2005), US Naval Observatory Circular 179.</li>
53885399
* <li><a href="https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/FORTRAN/req/time.html#The%20Relationship%20between%20TT%20and%20TDB">
5389-
* https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/FORTRAN/req/time.html</a>
5400+
* https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/FORTRAN/req/time.html</a></li>
53905401
* <li><a href="https://gssc.esa.int/navipedia/index.php/Transformations_between_Time_Systems">
5391-
* https://gssc.esa.int/navipedia/index.php/Transformations_between_Time_Systems</a>
5392-
* </li>
5402+
* https://gssc.esa.int/navipedia/index.php/Transformations_between_Time_Systems</a></li>
53935403
* </ol>
53945404
*
53955405
* @param jd_tt [day] Terrestrial Time (TT) based Julian date
@@ -5405,12 +5415,6 @@ double tt2tdb(double jd_tt) {
54055415

54065416
tdb2tt(jd_tt, NULL, &dt);
54075417
return dt;
5408-
5409-
/*
5410-
const double t = (jd_tt - JD_J2000) / JULIAN_CENTURY_DAYS;
5411-
const double g = 6.239996 + 630.0221385924 * t;
5412-
return 0.001657 * sin(g + 0.01671 * sin(g));
5413-
*/
54145418
}
54155419

54165420
/**

src/timescale.c

+11-9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
#define IDAY 86400 ///< [s] 1 day
2929

30+
#define IJD_J2000 2451545
31+
3032
#define UNIX_SECONDS_0UTC_1JAN2000 946684800 ///< [s] UNIX time at J2000.0
3133
#define UNIX_J2000 (UNIX_SECONDS_0UTC_1JAN2000 + (IDAY / 2))
3234

@@ -156,19 +158,19 @@ int novas_set_split_time(enum novas_timescale timescale, long ijd, double fjd, i
156158
* @since 1.1
157159
* @author Attila Kovacs
158160
*/
159-
int novas_increment_time(const novas_timespec *time, double seconds, novas_timespec *out) {
161+
int novas_offset_time(const novas_timespec *time, double seconds, novas_timespec *out) {
160162
int dd;
161163

162164
if(!time || !out)
163-
return novas_error(-1, EINVAL, "novas_increment_time", "NULL parameter: time=%p, out=%p", time, out);
165+
return novas_error(-1, EINVAL, "novas_offset_time", "NULL parameter: time=%p, out=%p", time, out);
164166

165167
if(out != time)
166168
*out = *time;
167169

168170
out->fjd_tt += seconds / DAY;
169171
dd = (int) floor(out->fjd_tt);
170172
if(dd) {
171-
out->fjd_tt -= dd * DAY;
173+
out->fjd_tt -= dd;
172174
out->ijd_tt += dd;
173175
}
174176

@@ -220,8 +222,6 @@ double novas_get_split_time(const novas_timespec *time, enum novas_timescale tim
220222
novas_error(-1, EINVAL, fn, "NULL input time specification");
221223
return NAN;
222224
}
223-
if(timescale < 0 || timescale > NOVAS_TIMESCALES)
224-
novas_error(-1, EINVAL, fn, "invalid timescale: %d", timescale);
225225

226226
if(ijd)
227227
*ijd = time->ijd_tt;
@@ -281,7 +281,7 @@ double novas_get_split_time(const novas_timespec *time, enum novas_timescale tim
281281
* will be set to EINVAL)
282282
*
283283
* @sa novas_set_time()
284-
* @sa novas_increment_time()
284+
* @sa novas_offset_time()
285285
* @sa novas_diff_coordinate_time()
286286
*
287287
* @since 1.1
@@ -341,12 +341,14 @@ int novas_set_unix_time(time_t unix_time, long nanos, int leap, double dut1, nov
341341

342342
// UTC based integer JD
343343
unix_time -= UNIX_J2000;
344-
jd = NOVAS_JD_J2000 + unix_time / IDAY;
344+
jd = IJD_J2000 + unix_time / IDAY;
345345

346346
// seconds of JD date
347347
sojd = unix_time % IDAY;
348-
if(sojd < 0)
348+
if(sojd < 0) {
349349
sojd += IDAY;
350+
jd--;
351+
}
350352

351353
return novas_set_split_time(NOVAS_UTC, jd, (sojd + 1e-9 * nanos) / DAY, leap, dut1, time);
352354
}
@@ -371,7 +373,7 @@ time_t novas_get_unix_time(const novas_timespec *time, long *nanos) {
371373

372374
sod = novas_get_split_time(time, NOVAS_UTC, &ijd) * DAY;
373375
isod = floor(sod);
374-
seconds = UNIX_J2000 + (ijd - NOVAS_JD_J2000) * DAY + isod;
376+
seconds = UNIX_J2000 + (ijd - IJD_J2000) * IDAY + isod;
375377

376378
if(nanos) {
377379
*nanos = round(1e9 * (sod - isod));

test/src/test-errors.c

+22
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,28 @@ static int test_obs_posvel() {
874874
return n;
875875
}
876876

877+
static int test_time() {
878+
novas_timespec time = {};
879+
int n = 0;
880+
881+
if(check("time:set:time", -1, novas_set_time(NOVAS_TT, NOVAS_JD_J2000, 37, 0.11, NULL))) n++;
882+
if(check("time:set:scale:-1", -1, novas_set_time(-1, NOVAS_JD_J2000, 37, 0.11, &time))) n++;
883+
if(check("time:set:scale:hi", -1, novas_set_time(NOVAS_TIMESCALES, NOVAS_JD_J2000, 37, 0.11, &time))) n++;
884+
885+
if(check("time:get:time", -1, novas_get_time(NULL, NOVAS_TT))) n++;
886+
if(check("time:get:scale:-1", -1, novas_get_time(&time, -1))) n++;
887+
if(check("time:get:scale:hi", -1, novas_get_time(&time, NOVAS_TIMESCALES))) n++;
888+
889+
if(check("time:offset:time", -1, novas_offset_time(NULL, 0.1, &time))) n++;
890+
if(check("time:offset:out", -1, novas_offset_time(&time, 0.1, NULL))) n++;
891+
if(check("time:offset:both", -1, novas_offset_time(NULL, 0.1, NULL))) n++;
892+
893+
if(check("time:diff:t1", -1, novas_diff_time(NULL, &time))) n++;
894+
if(check("time:diff:t2", -1, novas_diff_time(&time, NULL))) n++;
895+
if(check("time:diff:both", -1, novas_diff_time(NULL, NULL))) n++;
896+
897+
return n;
898+
}
877899

878900
int main() {
879901
int n = 0;

0 commit comments

Comments
 (0)