diff --git a/generate/template/astronomy.py b/generate/template/astronomy.py index a00308cc..bf5ed816 100644 --- a/generate/template/astronomy.py +++ b/generate/template/astronomy.py @@ -111,7 +111,7 @@ class Vector: return math.sqrt(self.x**2 + self.y**2 + self.z**2) @enum.unique -class Body(enum.IntEnum): +class Body(enum.Enum): """The celestial bodies supported by Astronomy Engine calculations. Values @@ -246,11 +246,11 @@ class NoConvergeError(Error): def _SynodicPeriod(body): if body == Body.Earth: raise EarthNotAllowedError() - if body < 0 or body >= len(_PlanetOrbitalPeriod): + if body.value < 0 or body.value >= len(_PlanetOrbitalPeriod): raise InvalidBodyError() if body == Body.Moon: return _MEAN_SYNODIC_MONTH - return abs(_EARTH_ORBITAL_PERIOD / (_EARTH_ORBITAL_PERIOD/_PlanetOrbitalPeriod[body] - 1.0)) + return abs(_EARTH_ORBITAL_PERIOD / (_EARTH_ORBITAL_PERIOD/_PlanetOrbitalPeriod[body.value] - 1.0)) def _AngleBetween(a, b): r = a.Length() * b.Length() @@ -1017,7 +1017,7 @@ def _VsopHelioDistance(model, time): return _VsopFormula(model[2], time.tt / 365250.0) def _CalcEarth(time): - return _CalcVsop(_vsop[Body.Earth], time) + return _CalcVsop(_vsop[Body.Earth.value], time) # END VSOP #---------------------------------------------------------------------------- @@ -1242,7 +1242,7 @@ def Search(func, context, t1, t2, dt_tolerance_seconds): def _AdjustBarycenter(ssb, time, body, pmass): shift = pmass / (pmass + _SUN_MASS) - planet = _CalcVsop(_vsop[body], time) + planet = _CalcVsop(_vsop[body.value], time) ssb.x += shift * planet.x ssb.y += shift * planet.y ssb.z += shift * planet.z @@ -1286,8 +1286,8 @@ def HelioVector(body, time): if body == Body.Pluto: return _CalcChebyshev(_pluto, time) - if 0 <= body < len(_vsop): - return _CalcVsop(_vsop[body], time) + if 0 <= body.value < len(_vsop): + return _CalcVsop(_vsop[body.value], time) if body == Body.Sun: return Vector(0.0, 0.0, 0.0, time) @@ -1334,8 +1334,8 @@ def HelioDistance(body, time): if body == Body.Sun: return 0.0 - if 0 <= body < len(_vsop): - return _VsopHelioDistance(_vsop[body], time) + if 0 <= body.value < len(_vsop): + return _VsopHelioDistance(_vsop[body.value], time) return HelioVector(body, time).Length() @@ -3199,7 +3199,7 @@ def SearchPlanetApsis(body, startTime): return _SearchNeptuneApsis(startTime) positive_slope = (+1.0, body) negative_slope = (-1.0, body) - orbit_period_days = _PlanetOrbitalPeriod[body] + orbit_period_days = _PlanetOrbitalPeriod[body.value] increment = orbit_period_days / 6.0 t1 = startTime m1 = _planet_distance_slope(positive_slope, t1) @@ -3259,7 +3259,7 @@ def NextPlanetApsis(body, apsis): if apsis.kind not in [ApsisKind.Apocenter, ApsisKind.Pericenter]: raise Error('Parameter "apsis" contains an invalid "kind" value.') # Skip 1/4 of an orbit before starting search again. - skip = 0.25 * _PlanetOrbitalPeriod[body] + skip = 0.25 * _PlanetOrbitalPeriod[body.value] time = apsis.time.AddDays(skip) next = SearchPlanetApsis(body, time) # Verify that we found the opposite apsis from the previous one. @@ -3268,7 +3268,7 @@ def NextPlanetApsis(body, apsis): return next def _NeptuneHelioDistance(time): - return _VsopHelioDistance(_vsop[Body.Neptune], time) + return _VsopHelioDistance(_vsop[Body.Neptune.value], time) def _NeptuneExtreme(kind, start_time, dayspan): direction = +1.0 if (kind == ApsisKind.Apocenter) else -1.0 diff --git a/generate/test.py b/generate/test.py index 167f4e71..a55d8b52 100755 --- a/generate/test.py +++ b/generate/test.py @@ -291,7 +291,7 @@ def TestElongFile(filename, targetRelLon): minute = int(m.group(5)) name = m.group(6) body = astronomy.BodyCode(name) - if body < 0: + if body.value == astronomy.Body.Invalid: print('TestElongFile({} line {}): invalid body name "{}"'.format(filename, lnum, name)) return 1 search_time = astronomy.Time.Make(year, 1, 1, 0, 0, 0) @@ -641,7 +641,7 @@ def Test_RiseSet(filename): correct_time = astronomy.Time.Make(year, month, day, hour, minute, 0) direction = astronomy.Direction.Rise if kind == 'r' else astronomy.Direction.Set body = astronomy.BodyCode(name) - if body < 0: + if body == astronomy.Body.Invalid: print('Test_RiseSet({} line {}): invalid body name "{}"'.format(filename, lnum, name)) return 1 @@ -1040,10 +1040,10 @@ def Test_PlanetApsis(): start_time = astronomy.Time.Make(1700, 1, 1, 0, 0, 0) found_bad_planet = False body = astronomy.Body.Mercury - while body <= astronomy.Body.Pluto: + while body.value <= astronomy.Body.Pluto.value: count = 1 - period = astronomy._PlanetOrbitalPeriod[body] - filename = os.path.join('apsides', 'apsis_{}.txt'.format(int(body))) + period = astronomy._PlanetOrbitalPeriod[body.value] + filename = os.path.join('apsides', 'apsis_{}.txt'.format(body.value)) min_interval = -1.0 max_diff_days = 0.0 max_dist_ratio = 0.0 @@ -1097,7 +1097,7 @@ def Test_PlanetApsis(): (max_diff_days / period) * 360.0, max_dist_ratio )) - body = astronomy.Body(body + 1) + body = astronomy.Body(body.value + 1) if found_bad_planet: print('Test_PlanetApsis: FAIL - planet(s) exceeded angular threshold ({} degrees)'.format(degree_threshold)) diff --git a/source/python/astronomy.py b/source/python/astronomy.py index 4009c040..b44067e0 100644 --- a/source/python/astronomy.py +++ b/source/python/astronomy.py @@ -111,7 +111,7 @@ class Vector: return math.sqrt(self.x**2 + self.y**2 + self.z**2) @enum.unique -class Body(enum.IntEnum): +class Body(enum.Enum): """The celestial bodies supported by Astronomy Engine calculations. Values @@ -246,11 +246,11 @@ class NoConvergeError(Error): def _SynodicPeriod(body): if body == Body.Earth: raise EarthNotAllowedError() - if body < 0 or body >= len(_PlanetOrbitalPeriod): + if body.value < 0 or body.value >= len(_PlanetOrbitalPeriod): raise InvalidBodyError() if body == Body.Moon: return _MEAN_SYNODIC_MONTH - return abs(_EARTH_ORBITAL_PERIOD / (_EARTH_ORBITAL_PERIOD/_PlanetOrbitalPeriod[body] - 1.0)) + return abs(_EARTH_ORBITAL_PERIOD / (_EARTH_ORBITAL_PERIOD/_PlanetOrbitalPeriod[body.value] - 1.0)) def _AngleBetween(a, b): r = a.Length() * b.Length() @@ -2959,7 +2959,7 @@ def _VsopHelioDistance(model, time): return _VsopFormula(model[2], time.tt / 365250.0) def _CalcEarth(time): - return _CalcVsop(_vsop[Body.Earth], time) + return _CalcVsop(_vsop[Body.Earth.value], time) # END VSOP #---------------------------------------------------------------------------- @@ -3352,7 +3352,7 @@ def Search(func, context, t1, t2, dt_tolerance_seconds): def _AdjustBarycenter(ssb, time, body, pmass): shift = pmass / (pmass + _SUN_MASS) - planet = _CalcVsop(_vsop[body], time) + planet = _CalcVsop(_vsop[body.value], time) ssb.x += shift * planet.x ssb.y += shift * planet.y ssb.z += shift * planet.z @@ -3396,8 +3396,8 @@ def HelioVector(body, time): if body == Body.Pluto: return _CalcChebyshev(_pluto, time) - if 0 <= body < len(_vsop): - return _CalcVsop(_vsop[body], time) + if 0 <= body.value < len(_vsop): + return _CalcVsop(_vsop[body.value], time) if body == Body.Sun: return Vector(0.0, 0.0, 0.0, time) @@ -3444,8 +3444,8 @@ def HelioDistance(body, time): if body == Body.Sun: return 0.0 - if 0 <= body < len(_vsop): - return _VsopHelioDistance(_vsop[body], time) + if 0 <= body.value < len(_vsop): + return _VsopHelioDistance(_vsop[body.value], time) return HelioVector(body, time).Length() @@ -5309,7 +5309,7 @@ def SearchPlanetApsis(body, startTime): return _SearchNeptuneApsis(startTime) positive_slope = (+1.0, body) negative_slope = (-1.0, body) - orbit_period_days = _PlanetOrbitalPeriod[body] + orbit_period_days = _PlanetOrbitalPeriod[body.value] increment = orbit_period_days / 6.0 t1 = startTime m1 = _planet_distance_slope(positive_slope, t1) @@ -5369,7 +5369,7 @@ def NextPlanetApsis(body, apsis): if apsis.kind not in [ApsisKind.Apocenter, ApsisKind.Pericenter]: raise Error('Parameter "apsis" contains an invalid "kind" value.') # Skip 1/4 of an orbit before starting search again. - skip = 0.25 * _PlanetOrbitalPeriod[body] + skip = 0.25 * _PlanetOrbitalPeriod[body.value] time = apsis.time.AddDays(skip) next = SearchPlanetApsis(body, time) # Verify that we found the opposite apsis from the previous one. @@ -5378,7 +5378,7 @@ def NextPlanetApsis(body, apsis): return next def _NeptuneHelioDistance(time): - return _VsopHelioDistance(_vsop[Body.Neptune], time) + return _VsopHelioDistance(_vsop[Body.Neptune.value], time) def _NeptuneExtreme(kind, start_time, dayspan): direction = +1.0 if (kind == ApsisKind.Apocenter) else -1.0