Skip to content

Commit

Permalink
MNT: Remove deprecated dms_to_degrees and hms_to_hours
Browse files Browse the repository at this point in the history
and associated functions. Angle with tuple now throws
NotImplementedError.
  • Loading branch information
pllim committed Aug 23, 2023
1 parent 46df1bb commit d996820
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 175 deletions.
110 changes: 1 addition & 109 deletions astropy/coordinates/angle_formats.py
Expand Up @@ -22,8 +22,7 @@
import numpy as np

from astropy import units as u
from astropy.utils import format_exception, parsing
from astropy.utils.decorators import deprecated
from astropy.utils import parsing

from .errors import (
IllegalHourError,
Expand Down Expand Up @@ -413,113 +412,6 @@ def degrees_to_dms(d):
return np.floor(sign * d), sign * np.floor(m), sign * s


@deprecated(
since="5.1",
message=(
"dms_to_degrees (or creating an Angle with a tuple) has ambiguous "
"behavior when the degree value is 0. Use {alternative}."
),
alternative=(
"another way of creating angles instead (e.g. a less "
"ambiguous string like '-0d1m2.3s')"
),
)
def dms_to_degrees(d, m, s=None):
"""
Convert degrees, arcminute, arcsecond to a float degrees value.
"""
_check_minute_range(m)
_check_second_range(s)

# determine sign
sign = np.copysign(1.0, d)

try:
d = np.floor(np.abs(d))
if s is None:
m = np.abs(m)
s = 0
else:
m = np.floor(np.abs(m))
s = np.abs(s)
except ValueError as err:
raise ValueError(
format_exception(
"{func}: dms values ({1[0]},{2[1]},{3[2]}) could not be "
"converted to numbers.",
d,
m,
s,
)
) from err

return sign * (d + m / 60.0 + s / 3600.0)


@deprecated(
since="5.1",
message=(
"hms_to_hours (or creating an Angle with a tuple) has ambiguous "
"behavior when the hour value is 0. Use {alternative}."
),
alternative=(
"another way of creating angles instead (e.g. a less "
"ambiguous string like '-0h1m2.3s')"
),
)
def hms_to_hours(h, m, s=None):
"""
Convert hour, minute, second to a float hour value.
"""
check_hms_ranges(h, m, s)

# determine sign
sign = np.copysign(1.0, h)

try:
h = np.floor(np.abs(h))
if s is None:
m = np.abs(m)
s = 0
else:
m = np.floor(np.abs(m))
s = np.abs(s)
except ValueError as err:
raise ValueError(
format_exception(
"{func}: HMS values ({1[0]},{2[1]},{3[2]}) could not be "
"converted to numbers.",
h,
m,
s,
)
) from err

return sign * (h + m / 60.0 + s / 3600.0)


def hms_to_degrees(h, m, s):
"""
Convert hour, minute, second to a float degrees value.
"""
return hms_to_hours(h, m, s) * 15.0


def hms_to_radians(h, m, s):
"""
Convert hour, minute, second to a float radians value.
"""
return u.degree.to(u.radian, hms_to_degrees(h, m, s))


def hms_to_dms(h, m, s):
"""
Convert degrees, arcminutes, arcseconds to an ``(hour, minute, second)``
tuple.
"""
return degrees_to_dms(hms_to_degrees(h, m, s))


def hours_to_decimal(h):
"""
Convert any parseable hour value into a float value.
Expand Down
19 changes: 4 additions & 15 deletions astropy/coordinates/angles.py
Expand Up @@ -112,7 +112,10 @@ def __new__(cls, angle, unit=None, dtype=np.inexact, copy=True, **kwargs):
unit = cls._convert_unit_to_angle_unit(u.Unit(unit))

if isinstance(angle, tuple):
angle = cls._tuple_to_float(angle, unit)
raise NotImplementedError(
"Creating an Angle with a tuple has ambiguous behavior "
"and is no longer supported."
)

elif isinstance(angle, str):
angle, angle_unit = form.parse_angle(angle, unit)
Expand Down Expand Up @@ -141,20 +144,6 @@ def __new__(cls, angle, unit=None, dtype=np.inexact, copy=True, **kwargs):

return super().__new__(cls, angle, unit, dtype=dtype, copy=copy, **kwargs)

@staticmethod
def _tuple_to_float(angle, unit):
"""
Converts an angle represented as a 3-tuple or 2-tuple into a floating
point number in the given unit.
"""
# TODO: Numpy array of tuples?
if unit == u.hourangle:
return form.hms_to_hours(*angle)
elif unit == u.degree:
return form.dms_to_degrees(*angle)
else:
raise u.UnitsError(f"Can not parse '{angle}' as unit '{unit}'")

@staticmethod
def _convert_unit_to_angle_unit(unit):
return u.hourangle if unit == u.hour else unit
Expand Down
71 changes: 31 additions & 40 deletions astropy/coordinates/tests/test_angles.py
Expand Up @@ -17,7 +17,6 @@
IllegalSecondError,
IllegalSecondWarning,
)
from astropy.utils.exceptions import AstropyDeprecationWarning


def test_create_angles():
Expand Down Expand Up @@ -46,11 +45,10 @@ def test_create_angles():

a10 = Angle(3.60827466667, unit=u.hour)
a11 = Angle("3:36:29.7888000120", unit=u.hour)
with pytest.warns(AstropyDeprecationWarning, match="hms_to_hour"):
a12 = Angle((3, 36, 29.7888000120), unit=u.hour) # *must* be a tuple
with pytest.warns(AstropyDeprecationWarning, match="hms_to_hour"):
# Regression test for #5001
a13 = Angle((3, 36, 29.7888000120), unit="hour")
with pytest.raises(NotImplementedError, match="no longer supported"):
Angle((3, 36, 29.7888000120), unit=u.hour)
with pytest.raises(NotImplementedError, match="no longer supported"):
Angle((3, 36, 29.7888000120), unit="hour")

Angle(0.944644098745, unit=u.radian)

Expand Down Expand Up @@ -93,34 +91,32 @@ def test_create_angles():
assert_allclose(a5.radian, a6.radian)

assert_allclose(a10.degree, a11.degree)
assert a11 == a12 == a13 == a14
assert a11 == a14
assert a21 == a22
assert a23 == -a24
assert a24 == a25

# check for illegal ranges / values
with pytest.raises(IllegalSecondError):
a = Angle("12 32 99", unit=u.degree)
Angle("12 32 99", unit=u.degree)

with pytest.raises(IllegalMinuteError):
a = Angle("12 99 23", unit=u.degree)
Angle("12 99 23", unit=u.degree)

with pytest.raises(IllegalSecondError):
a = Angle("12 32 99", unit=u.hour)
Angle("12 32 99", unit=u.hour)

with pytest.raises(IllegalMinuteError):
a = Angle("12 99 23", unit=u.hour)
Angle("12 99 23", unit=u.hour)

with pytest.raises(IllegalHourError):
a = Angle("99 25 51.0", unit=u.hour)
Angle("99 25 51.0", unit=u.hour)

with pytest.raises(ValueError):
a = Angle("12 25 51.0xxx", unit=u.hour)
Angle("12 25 51.0xxx", unit=u.hour)

with pytest.raises(ValueError):
a = Angle("12h34321m32.2s")

assert a1 is not None
Angle("12h34321m32.2s")


def test_angle_from_view():
Expand Down Expand Up @@ -425,9 +421,9 @@ def test_radec():
"""

with pytest.raises(u.UnitsError):
ra = Longitude("4:08:15.162342") # error - hours or degrees?
Longitude("4:08:15.162342") # error - hours or degrees?
with pytest.raises(u.UnitsError):
ra = Longitude("-4:08:15.162342")
Longitude("-4:08:15.162342")

# the "smart" initializer allows >24 to automatically do degrees, but the
# Angle-based one does not
Expand All @@ -436,29 +432,27 @@ def test_radec():
# ra = Longitude("26:34:15.345634") # unambiguous b/c hours don't go past 24
# assert_allclose(ra.degree, 26.570929342)
with pytest.raises(u.UnitsError):
ra = Longitude("26:34:15.345634")
Longitude("26:34:15.345634")

# ra = Longitude(68)
with pytest.raises(u.UnitsError):
ra = Longitude(68)
Longitude(68)

with pytest.raises(u.UnitsError):
ra = Longitude(12)
Longitude(12)

with pytest.raises(ValueError):
ra = Longitude("garbage containing a d and no units")
Longitude("garbage containing a d and no units")

ra = Longitude("12h43m23s")
assert_allclose(ra.hour, 12.7230555556)

# TODO: again, fix based on >24 behavior
# ra = Longitude((56,14,52.52))
with pytest.raises(u.UnitsError):
ra = Longitude((56, 14, 52.52))
with pytest.raises(u.UnitsError):
ra = Longitude((12, 14, 52)) # ambiguous w/o units
with pytest.warns(AstropyDeprecationWarning, match="hms_to_hours"):
ra = Longitude((12, 14, 52), unit=u.hour)
with pytest.raises(NotImplementedError, match="no longer supported"):
Longitude((56, 14, 52.52))
with pytest.raises(NotImplementedError, match="no longer supported"):
Longitude((12, 14, 52)) # ambiguous w/o units
with pytest.raises(NotImplementedError, match="no longer supported"):
Longitude((12, 14, 52), unit=u.hour)

# Units can be specified
ra = Longitude("4:08:15.162342", unit=u.hour)
Expand All @@ -468,7 +462,7 @@ def test_radec():
# nearly always specified in degrees, so this is the default.
# dec = Latitude("-41:08:15.162342")
with pytest.raises(u.UnitsError):
dec = Latitude("-41:08:15.162342")
Latitude("-41:08:15.162342")
dec = Latitude("-41:08:15.162342", unit=u.degree) # same as above


Expand Down Expand Up @@ -688,9 +682,9 @@ def test_wrap_at_inplace():

def test_latitude():
with pytest.raises(ValueError):
lat = Latitude(["91d", "89d"])
Latitude(["91d", "89d"])
with pytest.raises(ValueError):
lat = Latitude("-91d")
Latitude("-91d")

lat = Latitude(["90d", "89d"])
# check that one can get items
Expand Down Expand Up @@ -735,7 +729,7 @@ def test_latitude():
TypeError, match="A Latitude angle cannot be created from a Longitude angle"
):
lon = Longitude(10, "deg")
lat = Latitude(lon)
Latitude(lon)

with pytest.raises(
TypeError, match="A Longitude angle cannot be assigned to a Latitude angle"
Expand Down Expand Up @@ -838,7 +832,7 @@ def test_longitude():
TypeError, match="A Longitude angle cannot be created from a Latitude angle"
):
lat = Latitude(10, "deg")
lon = Longitude(lat)
Longitude(lat)

with pytest.raises(
TypeError, match="A Latitude angle cannot be assigned to a Longitude angle"
Expand Down Expand Up @@ -929,12 +923,9 @@ def test_empty_sep():
def test_create_tuple():
"""
Tests creation of an angle with an (h,m,s) tuple
(d, m, s) tuples are not tested because of sign ambiguity issues (#13162)
"""
with pytest.warns(AstropyDeprecationWarning, match="hms_to_hours"):
a1 = Angle((1, 30, 0), unit=u.hourangle)
assert a1.value == 1.5
with pytest.raises(NotImplementedError, match="no longer supported"):
Angle((1, 30, 0), unit=u.hourangle)


def test_list_of_quantities():
Expand Down
19 changes: 8 additions & 11 deletions astropy/coordinates/tests/test_arrays.py
Expand Up @@ -18,7 +18,6 @@
from astropy.tests.helper import assert_quantity_allclose as assert_allclose
from astropy.time import Time
from astropy.utils.compat import NUMPY_LT_1_24
from astropy.utils.exceptions import AstropyDeprecationWarning


def test_angle_arrays():
Expand Down Expand Up @@ -56,7 +55,7 @@ def test_angle_arrays():
else:
stack.enter_context(pytest.raises(ValueError))

a7 = Angle([a1, a2, a3], unit=u.degree)
Angle([a1, a2, a3], unit=u.degree)

a8 = Angle(["04:02:02", "03:02:01", "06:02:01"], unit=u.degree)
npt.assert_almost_equal(a8.value, [4.03388889, 3.03361111, 6.03361111])
Expand All @@ -65,7 +64,7 @@ def test_angle_arrays():
npt.assert_almost_equal(a9.value, a8.value)

with pytest.raises(u.UnitsError):
a10 = Angle(["04:02:02", "03:02:01", "06:02:01"])
Angle(["04:02:02", "03:02:01", "06:02:01"])


def test_dms():
Expand All @@ -87,10 +86,8 @@ def test_hms():
hours = hms[0] + hms[1] / 60.0 + hms[2] / 3600.0
npt.assert_almost_equal(a1.hour, hours)

with pytest.warns(AstropyDeprecationWarning, match="hms_to_hours"):
a2 = Angle(hms, unit=u.hour)

npt.assert_almost_equal(a2.radian, a1.radian)
with pytest.raises(NotImplementedError, match="no longer supported"):
Angle(hms, unit=u.hour)


def test_array_coordinates_creation():
Expand All @@ -101,9 +98,9 @@ def test_array_coordinates_creation():
assert not c.ra.isscalar

with pytest.raises(ValueError):
c = ICRS(np.array([1, 2]) * u.deg, np.array([3, 4, 5]) * u.deg)
ICRS(np.array([1, 2]) * u.deg, np.array([3, 4, 5]) * u.deg)
with pytest.raises(ValueError):
c = ICRS(np.array([1, 2, 4, 5]) * u.deg, np.array([[3, 4], [5, 6]]) * u.deg)
ICRS(np.array([1, 2, 4, 5]) * u.deg, np.array([[3, 4], [5, 6]]) * u.deg)

# make sure cartesian initialization also works
cart = CartesianRepresentation(
Expand All @@ -116,9 +113,9 @@ def test_array_coordinates_creation():

# but invalid strings cannot
with pytest.raises(ValueError):
c = SkyCoord(Angle(["10m0s", "2h02m00.3s"]), Angle(["3d", "4d"]))
SkyCoord(Angle(["10m0s", "2h02m00.3s"]), Angle(["3d", "4d"]))
with pytest.raises(ValueError):
c = SkyCoord(Angle(["1d0m0s", "2h02m00.3s"]), Angle(["3x", "4d"]))
SkyCoord(Angle(["1d0m0s", "2h02m00.3s"]), Angle(["3x", "4d"]))


def test_array_coordinates_distances():
Expand Down
3 changes: 3 additions & 0 deletions docs/changes/coordinates/15205.api.rst
@@ -0,0 +1,3 @@
Removed deprecated ``dms_to_degrees``, ``hms_to_hours``, ``hms_to_degrees``, ``hms_to_radians``,
and ``hms_to_dms`` in ``astropy.coordinates.angle_formats``. Initializing ``Angle`` and ``Longitude``
with a tuple is no longer supported and will now throw ``NotImplementedError``.

0 comments on commit d996820

Please sign in to comment.