diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 177c988..3017f07 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -17,7 +17,7 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest] - python-version: ['3.10', '3.11'] # There are still issues with Numpy <1.23 and 3.11. + python-version: ['3.10'] # There are still issues with Numpy <1.23 and 3.11. numpy-version: ['<1.23', '<1.24', '<2.0'] astropy-version: ['<5.1', '<6.0', '<7.0'] diff --git a/py/desiutil/names.py b/py/desiutil/names.py index a02d0bb..cd92f79 100644 --- a/py/desiutil/names.py +++ b/py/desiutil/names.py @@ -14,19 +14,19 @@ def radec_to_desiname(target_ra, target_dec): """Convert the right ascension and declination of a DESI target into the corresponding "DESINAME" for reference in publications. - Length of target_ra and target_dec must be the same if providing an + Length of `target_ra` and `target_dec` must be the same if providing an array or list. Note that these names are not unique for roughly one percent of DESI targets, so also including TARGETID in publications is highly recommended for uniqueness. Parameters ---------- - target_ra: array of :class:`float64` + target_ra: array of :class:`~numpy.float64` Right ascension in degrees of target object(s). Can be float, double, - or array/list of floats or doubles - target_dec: array of :class:`float64` + or array/list of floats or doubles. + target_dec: array of :class:`~numpy.float64` Declination in degrees of target object(s). Can be float, double, - or array/list of floats or doubles + or array/list of floats or doubles. Returns ------- @@ -34,10 +34,24 @@ def radec_to_desiname(target_ra, target_dec): The DESI names referring to the input target RA and DEC's. Array is the same length as the input arrays. + Raises + ------ + ValueError + If any input values are out of bounds. """ # Convert to numpy array in case inputs are scalars or lists target_ra, target_dec = np.atleast_1d(target_ra), np.atleast_1d(target_dec) + inputs = {'target_ra': target_ra, 'target_dec': target_dec} + tests = {'NaN values': np.isnan, + 'Infinite values': np.isinf, + 'RA not in range [0, 360)': lambda x: (x < 0) | (x >= 360), + 'Dec not in range [-90, 90]': lambda x: (x < -90) | (x > 90)} + for i in inputs: + for t in tests: + if (tests[t](inputs[i])).any(): + raise ValueError(f"{t} detected in {i}!") + # Number of decimal places in final naming convention precision = 4 diff --git a/py/desiutil/test/test_names.py b/py/desiutil/test/test_names.py index e4e1155..d7148db 100644 --- a/py/desiutil/test/test_names.py +++ b/py/desiutil/test/test_names.py @@ -4,6 +4,7 @@ """ import unittest import numpy as np +from ..names import radec_to_desiname class TestNames(unittest.TestCase): @@ -19,9 +20,8 @@ def tearDownClass(cls): pass def test_radec_to_desiname(self): - """Test MaskedArrayWithLimits + """Test computation of desiname. """ - from ..names import radec_to_desiname ras = [6.2457354547234, 23.914121939862518, 36.23454570972834, 235.25235223446, 99.9999999999999] decs = [29.974787585945496, -42.945872347904356, -0.9968423456, @@ -44,3 +44,15 @@ def test_radec_to_desiname(self): outnames = radec_to_desiname(np.array(ras), np.array(decs)) self.assertTrue(np.alltrue(outnames == correct_names)) + + def test_radec_to_desiname_bad_values(self): + """Test exceptions when running radec_to_desiname with bad values. + """ + ras = [6.2457354547234, 23.914121939862518, 36.23454570972834, + 235.25235223446, 99.9999999999999] + decs = [29.974787585945496, -42.945872347904356, -0.9968423456, + 8.45677345352345, 89.234958294953] + ras[2] = np.nan + with self.assertRaises(ValueError) as e: + outnames = radec_to_desiname(ras, decs) + self.assertEqual(str(e.exception), "NaN values detected in target_ra!")