From 35017483cb2d02e4d59a1dec44c7ad459ad5b5ed Mon Sep 17 00:00:00 2001 From: Don Cross Date: Sun, 23 Jun 2019 17:10:58 -0400 Subject: [PATCH] Fixed problem in Python MakeTime() function.y --- .gitignore | 1 + generate/template/astronomy.py | 18 +++++------------- generate/test.py | 28 ++++++++++++++++++++++++++++ generate/unit_test_python | 10 ++++++++++ source/python/astronomy.py | 18 +++++------------- 5 files changed, 49 insertions(+), 26 deletions(-) create mode 100644 generate/test.py create mode 100755 generate/unit_test_python diff --git a/.gitignore b/.gitignore index 722d5e71..9f94e5dd 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .vscode +__pycache__ diff --git a/generate/template/astronomy.py b/generate/template/astronomy.py index 3d25a289..ee703934 100644 --- a/generate/template/astronomy.py +++ b/generate/template/astronomy.py @@ -166,20 +166,12 @@ class astro_time_t: _EPOCH = datetime.datetime(2000, 1, 1, 12) def CurrentTime(): - now = datetime.datetime.utcnow() - dt = now - _EPOCH - ut = dt.total_seconds() / 86400.0 + ut = (datetime.datetime.utcnow() - _EPOCH).total_seconds() / 86400.0 return astro_time_t(ut) def MakeTime(year, month, day, hour, minute, second): - # This formula is adapted from NOVAS C 3.1 function julian_date(). - jd12h = ( - day - 32075 + 1461 * (year + 4800 - + (month - 14) / 12) / 4 - + 367 * (month - 2 - (month - 14) / 12 * 12) - / 12 - 3 * ((year + 4900 + (month - 14) / 12) - / 100) / 4) - y2000 = jd12h - 2451545 - ut = y2000 - 0.5 + (hour / 24.0) + (minute / (24.0 * 60.0)) + (second / (24.0 * 3600.0)) + micro = round((second % 1) * 1000000) + second = math.floor(second - micro/1000000) + d = datetime.datetime(year, month, day, hour, minute, second, micro) + ut = (d - _EPOCH).total_seconds() / 86400 return astro_time_t(ut) - diff --git a/generate/test.py b/generate/test.py new file mode 100644 index 00000000..110ede36 --- /dev/null +++ b/generate/test.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +import sys +sys.path.append('../source/python') +import astronomy + + +def Test_AstroTime(): + expected_ut = 6910.270978506945 + expected_tt = 6910.271779431480 + time = astronomy.MakeTime(2018, 12, 2, 18, 30, 12.543) + diff = time.ut - expected_ut + if abs(diff) > 1.0e-12: + print('Test_AstroTime: excessive UT error {}'.format(diff)) + sys.exit(1) + + diff = time.tt - expected_tt + if abs(diff) > 1.0e-12: + print('Test_AstroTime: excessive TT error {}'.format(diff)) + sys.exit(1) + + +if len(sys.argv) == 2: + if sys.argv[1] == 'time': + Test_AstroTime() + sys.exit(0) + +print('test.py: Invalid command line arguments.') +sys.exit(1) diff --git a/generate/unit_test_python b/generate/unit_test_python new file mode 100755 index 00000000..8b1f602e --- /dev/null +++ b/generate/unit_test_python @@ -0,0 +1,10 @@ +#!/bin/bash +Fail() +{ + echo "ERROR($0): $1" + exit 1 +} + +python3 --version || Fail "Cannot print python version" +python3 test.py time || Fail "Failure reported by test.py" +exit 0 diff --git a/source/python/astronomy.py b/source/python/astronomy.py index 651cdcf8..5f624a69 100644 --- a/source/python/astronomy.py +++ b/source/python/astronomy.py @@ -257,20 +257,12 @@ class astro_time_t: _EPOCH = datetime.datetime(2000, 1, 1, 12) def CurrentTime(): - now = datetime.datetime.utcnow() - dt = now - _EPOCH - ut = dt.total_seconds() / 86400.0 + ut = (datetime.datetime.utcnow() - _EPOCH).total_seconds() / 86400.0 return astro_time_t(ut) def MakeTime(year, month, day, hour, minute, second): - # This formula is adapted from NOVAS C 3.1 function julian_date(). - jd12h = ( - day - 32075 + 1461 * (year + 4800 - + (month - 14) / 12) / 4 - + 367 * (month - 2 - (month - 14) / 12 * 12) - / 12 - 3 * ((year + 4900 + (month - 14) / 12) - / 100) / 4) - y2000 = jd12h - 2451545 - ut = y2000 - 0.5 + (hour / 24.0) + (minute / (24.0 * 60.0)) + (second / (24.0 * 3600.0)) + micro = round((second % 1) * 1000000) + second = math.floor(second - micro/1000000) + d = datetime.datetime(year, month, day, hour, minute, second, micro) + ut = (d - _EPOCH).total_seconds() / 86400 return astro_time_t(ut) -