Skip to content

Commit

Permalink
Merge d1efdf6 into 93aca73
Browse files Browse the repository at this point in the history
  • Loading branch information
akremin committed Nov 28, 2023
2 parents 93aca73 + d1efdf6 commit b38be01
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
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 -*-
"""
============
desiutil.names
============
This package contains functions for naming 'things' in DESI
or decoding those names
"""
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))

0 comments on commit b38be01

Please sign in to comment.