diff --git a/python/lsst/pipe/tasks/associationUtils.py b/python/lsst/pipe/tasks/associationUtils.py index 39d6377ee..164828a89 100644 --- a/python/lsst/pipe/tasks/associationUtils.py +++ b/python/lsst/pipe/tasks/associationUtils.py @@ -195,7 +195,7 @@ def query_disc(nside, ra, dec, max_rad, min_rad=0): return pixels -def obj_id_to_ss_object_id(objID, flags=0): +def obj_id_to_ss_object_id(objID): """Convert from Minor Planet Center packed provisional object ID to Rubin ssObjectID. @@ -204,10 +204,6 @@ def obj_id_to_ss_object_id(objID, flags=0): objID : `str` Minor Planet Center packed provisional designation for a small solar system object. Must be fewer than eight characters. - flags : `int`, optional - Eight free bits to enable future decoupling between Minor Planet Center - and Rubin. Zero by default, should not be changed unless we need to - move away from a 1:1 mapping with the MPC. Must be within [0, 255]. Returns ------- @@ -217,20 +213,18 @@ def obj_id_to_ss_object_id(objID, flags=0): Raises ------ ValueError - Raised if either objID is longer than 7 characters or flags is greater - than 255 or less than 0. + Raised if either objID is shorter than 7 or longer than 8 characters or contains + illegal objID characters """ - if len(objID) > 7: - raise ValueError(f'objID longer than 7 characters: "{objID}"') + if len(objID) > 8: + raise ValueError(f'objID longer than 8 characters: "{objID}"') if len(objID) < 7: raise ValueError(f'objID shorter than 7 characters: "{objID}"') - if flags < 0 or flags > 255: - raise ValueError(f'Flags ({flags}) outside [0, 255].') if any([ord(c) > 255 for c in objID]): raise ValueError(f'{[c for c in objID if ord(c) > 255]} not legal objID characters (ascii [1, 255])') - ssObjectID = flags - for character in objID: + ssObjectID = ord(objID[0]) + for character in objID[1:]: ssObjectID <<= 8 ssObjectID += ord(character) return ssObjectID @@ -250,14 +244,12 @@ def ss_object_id_to_obj_id(ssObjectID): objID : `str` Minor Planet Center packed provisional designation. - flags : `int` - Rubin flags (not yet defined, but usable in case we decouple from MPC). - Raises ------ """ if ssObjectID < 0 or ssObjectID >= (1 << 64): raise ValueError(f'ssObjectID ({ssObjectID}) outside [0, 2^64 - 1].') - objID = ''.join([chr((ssObjectID >> (8 * i)) % 256) for i in reversed(range(0, 7))]) - return objID, ssObjectID >> (8 * 7) % 256 + objID = ''.join([chr((ssObjectID >> (8 * i)) % 256) for i in reversed(range(0, 8))]) + objID = objID.replace('\x00', '') + return objID diff --git a/tests/test_associationUtils.py b/tests/test_associationUtils.py index e13a84017..7f8b0e6c0 100644 --- a/tests/test_associationUtils.py +++ b/tests/test_associationUtils.py @@ -60,44 +60,24 @@ def test_conversions_between_obj_id_and_ss_object_id(self): """ allowed_strings = ['J95X00A', 'J95X01L', 'J95F13B', 'J98SA8Q', 'J98SC7V', 'J98SG2S'] \ + ['K99AJ3Z', 'K08Aa0A', 'K07Tf8A', 'PLS2040', 'T1S3138', 'T2S1010', 'T3S4101'] \ - + [' ', '\x00\x00\x00\x00\x00\x00\x00'] - allowed_flags = [i for i in range(0, 256)] - allowed_ssObjectIDs = [0, 1 << 64 - 1] + [1 << n for n in range(64)] + + [' ', 'PJ48Q010', 'AAAAAAAA'] for allowed_string in allowed_strings: - for allowed_flag in allowed_flags: - returned_string, returned_flag = ss_object_id_to_obj_id( - obj_id_to_ss_object_id(allowed_string, allowed_flag)) - self.assertEqual((allowed_string, allowed_flag), (returned_string, returned_flag)) - for allowed_ssObjectID in allowed_ssObjectIDs: - returned_ssObjectID = obj_id_to_ss_object_id(*ss_object_id_to_obj_id(allowed_ssObjectID)) - self.assertEqual(allowed_ssObjectID, returned_ssObjectID) + returned_string = ss_object_id_to_obj_id(obj_id_to_ss_object_id(allowed_string)) + self.assertEqual(allowed_string, returned_string) def test_invalid_conversions_between_obj_id_and_ss_object_id(self): """Convert between ssObjectIDs and MPC packed designations """ - allowed_strings = ['J95X00A', 'J95X01L', 'J95F13B', 'J98SA8Q', 'J98SC7V', 'J98SG2S'] \ - + ['K99AJ3Z', 'K08Aa0A', 'K07Tf8A', 'PLS2040', 'T1S3138', 'T2S1010', 'T3S4101'] - allowed_flags = [i for i in range(0, 256)] - disallowed_flags = [-999999999, -512, -256, -255, -1, 256, 512, 99999999] disallowed_strings = [''] + [ch for ch in 'ABCDEFGHIJKMNOPQRSTUVWXYZ0123456789 -'] \ + ['A' * i for i in range(2, 7)] + ['Z' * i for i in range(2, 7)] \ - + ['Ā', '🔭', 'A' * 8, ' ' * 8, 'A' * 128] + + ['Ā', '🔭', 'A' * 9, ' ' * 9, 'A' * 128] disallowed_ssObjectIDs = [-1, 1 << 64, 1 << 64 + 1, 2 << 65] - for allowed_string in allowed_strings: - for disallowed_flag in disallowed_flags: - with self.assertRaises(ValueError): - obj_id_to_ss_object_id(allowed_string, disallowed_flag) - for disallowed_string in disallowed_strings: - for allowed_flag in allowed_flags: - with self.assertRaises(ValueError): - obj_id_to_ss_object_id(disallowed_string, allowed_flag) - for disallowed_string in disallowed_strings: - for disallowed_flag in disallowed_flags: - with self.assertRaises(ValueError): - obj_id_to_ss_object_id(disallowed_string, disallowed_flag) for disallowed_ssObjectID in disallowed_ssObjectIDs: with self.assertRaises(ValueError): ss_object_id_to_obj_id(disallowed_ssObjectID) + for disallowed_string in disallowed_strings: + with self.assertRaises(ValueError): + obj_id_to_ss_object_id(disallowed_string) class MemoryTestCase(lsst.utils.tests.MemoryTestCase):