Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add radec_to_desiname function #203

Merged
merged 12 commits into from
Nov 29, 2023
62 changes: 62 additions & 0 deletions py/desiutil/names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-
"""
============
akremin marked this conversation as resolved.
Show resolved Hide resolved
desiutil.names
============

This package contains functions for naming 'things' in DESI
or decoding those names
akremin marked this conversation as resolved.
Show resolved Hide resolved
"""
import numpy as np


def radec_to_desiname(target_ra, target_dec):
"""Convert the right ascension and declination of a DESI target
into the corresponding "DESI name" for reference in publications.
Length of target_ra and target_dec must be the same if providing an
array or list.

Parameters
----------
target_ra: array of :class:`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`
Declination in degrees of target object(s). Can be float, double,
or array/list of floats or doubles

Returns
-------
array of :class:`str`
The DESI names referring to the input target RA and DEC's. Array is
the same length as the input arrays.

"""
# 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)

# Number of decimal places in final naming convention
precision = 4

# Truncate decimals to the given precision
ratrunc = np.trunc((10 ** precision) * target_ra).astype(int).astype(str)
dectrunc = np.trunc((10 ** precision) * target_dec).astype(int).astype(str)

# Loop over input values and create DESINAME as: DESI JXXX.XXXX+/-YY.YYYY
# Here J refers to J2000, which isn't strictly correct but is the closest
# IAU compliant term
desinames = []
for ra, dec in zip(ratrunc, dectrunc):
desiname = 'DESI J' + ra[:-precision].zfill(3) + '.' + ra[-precision:]
# Positive numbers need an explicit "+" while negative numbers
# already have a "-".
# zfill works properly with '-' but counts it in number of characters
# so need one more
if dec.startswith('-'):
desiname += dec[:-precision].zfill(3) + '.' + dec[-precision:]
else:
desiname += '+' + dec[:-precision].zfill(2) + '.' + dec[-precision:]
desinames.append(desiname)

return np.array(desinames)
46 changes: 46 additions & 0 deletions py/desiutil/test/test_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-
"""Test desiutil.names.
"""
import unittest
import numpy as np


class TestNames(unittest.TestCase):
"""Test desiutil.names
"""

@classmethod
def setUpClass(cls):
pass

@classmethod
def tearDownClass(cls):
pass

def test_radec_to_desiname(self):
"""Test MaskedArrayWithLimits
"""
from ..names import radec_to_desiname
ras = [6.2457354547234, 23.914121939862518, 36.23454570972834,
235.25235223446, 99.9999999999999]
decs = [29.974787585945496, -42.945872347904356, -0.9968423456,
8.45677345352345, 89.234958294953]
correct_names = np.array(['DESI J006.2457+29.9747',
'DESI J023.9141-42.9458',
'DESI J036.2345-00.9968',
'DESI J235.2523+08.4567',
'DESI J099.9999+89.2349'])
# Test scalar conversion
for ra, dec, correct_name in zip(ras, decs, correct_names):
outname = radec_to_desiname(ra, dec)
self.assertEqual(outname, correct_name)

# Test list conversion
outnames = radec_to_desiname(ras, decs)
self.assertTrue(np.alltrue(outnames == correct_names))

# Test array conversion
outnames = radec_to_desiname(np.array(ras),
np.array(decs))
self.assertTrue(np.alltrue(outnames == correct_names))