diff --git a/ssapy/utils.py b/ssapy/utils.py index 1a45d1b8..13a0cb7b 100644 --- a/ssapy/utils.py +++ b/ssapy/utils.py @@ -1866,17 +1866,16 @@ def rightascension_to_hourangle(right_ascension, local_time): rightascension_to_hourangle("10:30:00", "12:45:00") -> "02:15:00" rightascension_to_hourangle(157.5, 191.25) -> "02:15:00" """ - if type(right_ascension) is not str: - right_ascension = dd_to_hms(right_ascension) - if type(local_time) is not str: - local_time = dd_to_dms(local_time) - _ra = float(right_ascension.split(':')[0]) - _lt = float(local_time.split(':')[0]) - if _ra > _lt: - __ltm, __lts = local_time.split(':')[1:] - local_time = f'{24 + _lt}:{__ltm}:{__lts}' - - return dd_to_dms(hms_to_dd(local_time) - hms_to_dd(right_ascension)) + if isinstance(right_ascension, str): + right_ascension = hms_to_dd(right_ascension) + else: + right_ascension = float(right_ascension) + if isinstance(local_time, str): + local_time = hms_to_dd(local_time) + else: + local_time = float(local_time) + hour_angle = (local_time - right_ascension) % 360.0 + return dd_to_hms(hour_angle) def equatorial_to_horizontal(observer_latitude, declination, right_ascension=None, hour_angle=None, local_time=None, hms=False): @@ -1907,9 +1906,7 @@ def equatorial_to_horizontal(observer_latitude, declination, right_ascension=Non if hms: hour_angle = hms_to_dd(hour_angle) elif right_ascension is not None: - hour_angle = rightascension_to_hourangle(right_ascension, local_time) - if hms: - hour_angle = hms_to_dd(hour_angle) + hour_angle = hms_to_dd(rightascension_to_hourangle(right_ascension, local_time)) elif hour_angle is not None: if hms: hour_angle = hms_to_dd(hour_angle) diff --git a/tests/test_hour_angle_regressions.py b/tests/test_hour_angle_regressions.py new file mode 100644 index 00000000..88ccc424 --- /dev/null +++ b/tests/test_hour_angle_regressions.py @@ -0,0 +1,33 @@ +import numpy as np +import pytest + +from ssapy import utils + + +def test_rightascension_to_hourangle_matches_documented_examples(): + for right_ascension, local_time in [ + ("10:30:00", "12:45:00"), + (157.5, 191.25), + ]: + hour_angle = utils.rightascension_to_hourangle(right_ascension, local_time) + assert utils.hms_to_dd(hour_angle) == pytest.approx(33.75) + + +def test_rightascension_to_hourangle_wraps_through_midnight(): + hour_angle = utils.rightascension_to_hourangle("02:00:00", "01:00:00") + assert utils.hms_to_dd(hour_angle) == pytest.approx(345.0) + + +def test_equatorial_to_horizontal_accepts_numeric_ra_and_local_time(): + from_ra = utils.equatorial_to_horizontal( + observer_latitude=35.0, + declination=20.0, + right_ascension=157.5, + local_time=191.25, + ) + from_hour_angle = utils.equatorial_to_horizontal( + observer_latitude=35.0, + declination=20.0, + hour_angle=33.75, + ) + np.testing.assert_allclose(from_ra, from_hour_angle, atol=1e-12) diff --git a/tests/test_utils.py b/tests/test_utils.py index 816d2ae3..6acf9524 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -184,8 +184,8 @@ def test_small_coordinate_and_angle_helpers(capsys): assert utils.zenithangle_to_altitude(60.0) == 30.0 assert np.isclose(utils.altitude_to_zenithangle(np.pi / 6, deg=False), np.pi / 3) assert np.isclose(utils.zenithangle_to_altitude(np.pi / 3, deg=False), np.pi / 6) - assert utils.rightascension_to_hourangle(30.0, 2.0) == "0:0:0" - assert utils.rightascension_to_hourangle("02:00:00", "01:00:00").count(":") == 2 + assert utils.hms_to_dd(utils.rightascension_to_hourangle(30.0, 2.0)) == pytest.approx(332.0) + assert utils.hms_to_dd(utils.rightascension_to_hourangle("02:00:00", "01:00:00")) == pytest.approx(345.0) assert utils.dms_to_dd("12:30:00") == 12.5 assert utils.dms_to_dd(["12:30:00", "-12:30:00"]) == [12.5, -12.5]