Skip to content

Commit 13cdcae

Browse files
committed
Add global registry of representations and default names and units
Each representation is automatically registered when the class is defined. That provides a mapping between the alias name and the class. Each representation now defines the default names (e.g. ra, dec, distance) and units (e.g. u.deg, u.deg, None) for that representation. These are only used in frames but here seems like the most logical place to store this information. Otherwise there would need to be a secondary data structure, and authors of new representations would need to have a way to add their frame to that secondary structure.
1 parent a14904e commit 13cdcae

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

‎astropy/coordinates/representation.py‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
"SphericalRepresentation", "UnitSphericalRepresentation",
2222
"PhysicsSphericalRepresentation", "CylindricalRepresentation"]
2323

24+
# Module-level dict mapping representation string alias names to class.
25+
# This is populated by the metaclass init so all representation classes
26+
# get registered automatically.
27+
REPRESENTATION_CLASSES = {}
28+
2429

2530
def broadcast_quantity(*args, **kwargs):
2631
"""
@@ -33,7 +38,18 @@ def broadcast_quantity(*args, **kwargs):
3338
return tuple(new_quantities)
3439

3540

36-
@six.add_metaclass(abc.ABCMeta)
41+
class MetaBaseRepresentation(type):
42+
def __init__(cls, name, bases, dct):
43+
super(MetaBaseRepresentation, cls).__init__(name, bases, dct)
44+
45+
# Register representation name (except for BaseRepresentation)
46+
if cls.__name__ == 'BaseRepresentation':
47+
return
48+
49+
REPRESENTATION_CLASSES[cls.get_name()] = cls
50+
51+
52+
@six.add_metaclass(MetaBaseRepresentation)
3753
class BaseRepresentation(object):
3854
"""
3955
Base Representation object, for representing a point in a 3D coordinate
@@ -75,6 +91,13 @@ def components(self):
7591
"""Return tuple with the names of the coordinate components"""
7692
raise NotImplementedError()
7793

94+
@classmethod
95+
def get_name(cls):
96+
name = cls.__name__.lower()
97+
if name.endswith('representation'):
98+
name = name[:-14]
99+
return name
100+
78101
@classmethod
79102
def attr_dict(cls, names, units):
80103
"""
@@ -208,6 +231,8 @@ class CartesianRepresentation(BaseRepresentation):
208231
_attr_classes = OrderedDict([('x', u.Quantity),
209232
('y', u.Quantity),
210233
('z', u.Quantity)])
234+
_default_names = ('x', 'y', 'z')
235+
_default_units = (None, None, None)
211236

212237
def __init__(self, x, y=None, z=None, copy=True):
213238

@@ -303,6 +328,8 @@ class SphericalRepresentation(BaseRepresentation):
303328
_attr_classes = OrderedDict([('lon', Longitude),
304329
('lat', Latitude),
305330
('distance', u.Quantity)])
331+
_default_names = ('ra', 'dec', 'distance')
332+
_default_units = (u.deg, u.deg, None)
306333

307334
def __init__(self, lon, lat, distance, copy=True):
308335

@@ -418,6 +445,8 @@ class UnitSphericalRepresentation(BaseRepresentation):
418445

419446
_attr_classes = OrderedDict([('lon', Longitude),
420447
('lat', Latitude)])
448+
_default_names = ('ra', 'dec')
449+
_default_units = (u.deg, u.deg)
421450

422451
def __init__(self, lon, lat, copy=True):
423452

@@ -524,6 +553,8 @@ class PhysicsSphericalRepresentation(BaseRepresentation):
524553
_attr_classes = OrderedDict([('phi', Angle),
525554
('theta', Angle),
526555
('r', u.Quantity)])
556+
_default_names = ('phi', 'theta', 'r')
557+
_default_units = (u.deg, u.deg, None)
527558

528559
def __init__(self, phi, theta, r, copy=True):
529560

@@ -655,6 +686,8 @@ class CylindricalRepresentation(BaseRepresentation):
655686
_attr_classes = OrderedDict([('rho', u.Quantity),
656687
('phi', Angle),
657688
('z', u.Quantity)])
689+
_default_names = ('rho', 'phi', 'z')
690+
_default_units = (None, u.deg, None)
658691

659692
def __init__(self, rho, phi, z, copy=True):
660693

0 commit comments

Comments
 (0)