From f38174097631931d4fb28232f2d9d77cc59f7b95 Mon Sep 17 00:00:00 2001 From: Don Cross Date: Mon, 8 Jul 2019 16:05:56 -0400 Subject: [PATCH] Python: Convert rise/set direction kinds to an enumerated type Direction. --- generate/template/astronomy.py | 10 ++++++---- generate/test.py | 14 +++++++------- source/python/astronomy.py | 10 ++++++---- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/generate/template/astronomy.py b/generate/template/astronomy.py index e2d81330..62aa511b 100644 --- a/generate/template/astronomy.py +++ b/generate/template/astronomy.py @@ -1637,8 +1637,10 @@ def SearchHourAngle(body, observer, hourAngle, startTime): delta_days = (delta_sidereal_hours / 24.0) * _SOLAR_DAYS_PER_SIDEREAL_DAY time = time.AddDays(delta_days) -DIRECTION_RISE = +1 -DIRECTION_SET = -1 +@unique +class Direction(IntEnum): + Rise = +1 + Set = -1 class _peak_altitude_context: def __init__(self, body, direction, observer, body_radius_au): @@ -1674,10 +1676,10 @@ def SearchRiseSet(body, observer, direction, startTime, limitDays): else: body_radius = 0.0 - if direction == DIRECTION_RISE: + if direction == Direction.Rise: ha_before = 12.0 # minimum altitude (bottom) happens BEFORE the body rises. ha_after = 0.0 # maximum altitude (culmination) happens AFTER the body rises. - elif direction == DIRECTION_SET: + elif direction == Direction.Set: ha_before = 0.0 # culmination happens BEFORE the body sets. ha_after = 12.0 # bottom happens AFTER the body sets. else: diff --git a/generate/test.py b/generate/test.py index bf3a020f..8ffa35f2 100755 --- a/generate/test.py +++ b/generate/test.py @@ -617,7 +617,7 @@ def Test_RiseSet(filename): minute = int(m.group(8)) kind = m.group(9) correct_time = astronomy.Time.Make(year, month, day, hour, minute, 0) - direction = astronomy.DIRECTION_RISE if kind == 'r' else astronomy.DIRECTION_SET + direction = astronomy.Direction.Rise if kind == 'r' else astronomy.Direction.Set body = astronomy.BodyCode(name) if body < 0: print('Test_RiseSet({} line {}): invalid body name "{}"'.format(filename, lnum, name)) @@ -638,11 +638,11 @@ def Test_RiseSet(filename): a_dir = b_dir b_evt = None else: - r_evt = astronomy.SearchRiseSet(body, observer, astronomy.DIRECTION_RISE, r_search_date, 366.0) + r_evt = astronomy.SearchRiseSet(body, observer, astronomy.Direction.Rise, r_search_date, 366.0) if r_evt is None: print('Test_RiseSet({} line {}): rise search failed'.format(filename, lnum)) return 1 - s_evt = astronomy.SearchRiseSet(body, observer, astronomy.DIRECTION_SET, s_search_date, 366.0) + s_evt = astronomy.SearchRiseSet(body, observer, astronomy.Direction.Set, s_search_date, 366.0) if s_evt is None: print('Test_RiseSet({} line {}): set search failed'.format(filename, lnum)) return 1 @@ -650,13 +650,13 @@ def Test_RiseSet(filename): if r_evt.tt < s_evt.tt: a_evt = r_evt b_evt = s_evt - a_dir = astronomy.DIRECTION_RISE - b_dir = astronomy.DIRECTION_SET + a_dir = astronomy.Direction.Rise + b_dir = astronomy.Direction.Set else: a_evt = s_evt b_evt = r_evt - a_dir = astronomy.DIRECTION_SET - b_dir = astronomy.DIRECTION_RISE + a_dir = astronomy.Direction.Set + b_dir = astronomy.Direction.Rise # Nudge the event times forward a tiny amount. r_search_date = r_evt.AddDays(nudge_days) s_search_date = s_evt.AddDays(nudge_days) diff --git a/source/python/astronomy.py b/source/python/astronomy.py index 828b3e22..c3301b27 100644 --- a/source/python/astronomy.py +++ b/source/python/astronomy.py @@ -3698,8 +3698,10 @@ def SearchHourAngle(body, observer, hourAngle, startTime): delta_days = (delta_sidereal_hours / 24.0) * _SOLAR_DAYS_PER_SIDEREAL_DAY time = time.AddDays(delta_days) -DIRECTION_RISE = +1 -DIRECTION_SET = -1 +@unique +class Direction(IntEnum): + Rise = +1 + Set = -1 class _peak_altitude_context: def __init__(self, body, direction, observer, body_radius_au): @@ -3735,10 +3737,10 @@ def SearchRiseSet(body, observer, direction, startTime, limitDays): else: body_radius = 0.0 - if direction == DIRECTION_RISE: + if direction == Direction.Rise: ha_before = 12.0 # minimum altitude (bottom) happens BEFORE the body rises. ha_after = 0.0 # maximum altitude (culmination) happens AFTER the body rises. - elif direction == DIRECTION_SET: + elif direction == Direction.Set: ha_before = 0.0 # culmination happens BEFORE the body sets. ha_after = 12.0 # bottom happens AFTER the body sets. else: