From 7f0a369027053de97caf18fa8f3a5dad2b90694c Mon Sep 17 00:00:00 2001 From: Don Cross Date: Sun, 23 Jun 2019 19:46:28 -0400 Subject: [PATCH] Python: reworked names and made Time more object-oriented. --- generate/template/astronomy.py | 31 ++++++++++++++++--------------- generate/test.py | 8 ++++---- source/python/astronomy.py | 31 ++++++++++++++++--------------- 3 files changed, 36 insertions(+), 34 deletions(-) diff --git a/generate/template/astronomy.py b/generate/template/astronomy.py index e5b73f68..362a5d80 100644 --- a/generate/template/astronomy.py +++ b/generate/template/astronomy.py @@ -3,6 +3,7 @@ import math import datetime +_EPOCH = datetime.datetime(2000, 1, 1, 12) _T0 = 2451545.0 _MJD_BASIS = 2400000.5 _Y2000_IN_MJD = _T0 - _MJD_BASIS @@ -155,13 +156,26 @@ def _DeltaT(mjd): def _TerrestrialTime(ut): return ut + _DeltaT(ut + _Y2000_IN_MJD) / 86400.0 -class astro_time_t: +class Time: def __init__(self, ut): self.ut = ut self.tt = _TerrestrialTime(ut) + @staticmethod + def Make(year, month, day, hour, minute, second): + 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 Time(ut) + + @staticmethod + def Now(): + ut = (datetime.datetime.utcnow() - _EPOCH).total_seconds() / 86400.0 + return Time(ut) + def AddDays(self, days): - return astro_time_t(self.ut + days) + return Time(self.ut + days) def __str__(self): millis = round(self.ut * 86400000.0) @@ -170,16 +184,3 @@ class astro_time_t: def Utc(self): return _EPOCH + datetime.timedelta(days=self.ut) - -_EPOCH = datetime.datetime(2000, 1, 1, 12) - -def CurrentTime(): - ut = (datetime.datetime.utcnow() - _EPOCH).total_seconds() / 86400.0 - return astro_time_t(ut) - -def MakeTime(year, month, day, hour, minute, second): - 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 index 6d0ba263..b824ea37 100644 --- a/generate/test.py +++ b/generate/test.py @@ -7,7 +7,7 @@ import astronomy def Test_AstroTime(): expected_ut = 6910.270978506945 expected_tt = 6910.271779431480 - time = astronomy.MakeTime(2018, 12, 2, 18, 30, 12.543) + time = astronomy.Time.Make(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)) @@ -20,17 +20,17 @@ def Test_AstroTime(): if s != '2018-12-02 18:30:12.543000': print('Test_AstroTime: Utc() returned incorrect string "{}"'.format(s)) sys.exit(1) - time = astronomy.MakeTime(2018, 12, 31, 23, 59, 59.9994) + time = astronomy.Time.Make(2018, 12, 31, 23, 59, 59.9994) s = str(time) if s != '2018-12-31T23:59:59.999Z': print('Test_AstroTime: expected 2018-12-31T23:59:59.999Z but found {}'.format(s)) sys.exit(1) - time = astronomy.MakeTime(2018, 12, 31, 23, 59, 59.9995) + time = astronomy.Time.Make(2018, 12, 31, 23, 59, 59.9995) s = str(time) if s != '2019-01-01T00:00:00.000Z': print('Test_AstroTime: expected 2019-01-01T00:00:00.000Z but found {}'.format(s)) sys.exit(1) - print('Current time =', astronomy.CurrentTime()) + print('Current time =', astronomy.Time.Now()) if len(sys.argv) == 2: if sys.argv[1] == 'time': diff --git a/source/python/astronomy.py b/source/python/astronomy.py index 5b840e9a..8332f8e9 100644 --- a/source/python/astronomy.py +++ b/source/python/astronomy.py @@ -3,6 +3,7 @@ import math import datetime +_EPOCH = datetime.datetime(2000, 1, 1, 12) _T0 = 2451545.0 _MJD_BASIS = 2400000.5 _Y2000_IN_MJD = _T0 - _MJD_BASIS @@ -246,13 +247,26 @@ def _DeltaT(mjd): def _TerrestrialTime(ut): return ut + _DeltaT(ut + _Y2000_IN_MJD) / 86400.0 -class astro_time_t: +class Time: def __init__(self, ut): self.ut = ut self.tt = _TerrestrialTime(ut) + @staticmethod + def Make(year, month, day, hour, minute, second): + 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 Time(ut) + + @staticmethod + def Now(): + ut = (datetime.datetime.utcnow() - _EPOCH).total_seconds() / 86400.0 + return Time(ut) + def AddDays(self, days): - return astro_time_t(self.ut + days) + return Time(self.ut + days) def __str__(self): millis = round(self.ut * 86400000.0) @@ -261,16 +275,3 @@ class astro_time_t: def Utc(self): return _EPOCH + datetime.timedelta(days=self.ut) - -_EPOCH = datetime.datetime(2000, 1, 1, 12) - -def CurrentTime(): - ut = (datetime.datetime.utcnow() - _EPOCH).total_seconds() / 86400.0 - return astro_time_t(ut) - -def MakeTime(year, month, day, hour, minute, second): - 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)