Skip to content

Commit 7596394

Browse files
committed
Add more concise way of defined represenation attr names and units
1 parent d1fdead commit 7596394

2 files changed

Lines changed: 79 additions & 111 deletions

File tree

‎astropy/coordinates/builtin_frames.py‎

Lines changed: 44 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,12 @@ class ICRS(BaseCoordinateFrame):
5656
The Distance for this object along the line-of-sight.
5757
(``representation`` must be None).
5858
"""
59-
_representations = {SphericalRepresentation:
60-
{'attr_names': OrderedDict([('ra', 'lon'), ('dec', 'lat'),
61-
('distance', 'distance')]),
62-
'attr_units': {'ra': u.degree, 'dec': u.degree}},
63-
UnitSphericalRepresentation:
64-
{'attr_names': OrderedDict([('ra', 'lon'), ('dec', 'lat')]),
65-
'attr_units': {'ra': u.degree, 'dec': u.degree}},
66-
CartesianRepresentation:
67-
{'attr_names': OrderedDict([('x', 'x'), ('y', 'y'), ('z', 'z')]),
68-
'attr_units': {}}
69-
}
70-
71-
_preferred_representation = SphericalRepresentation
72-
_preferred_attr_names = _representations[_preferred_representation]['attr_names']
73-
_preferred_attr_units = _representations[_preferred_representation]['attr_units']
59+
representation_attrs = dict(
60+
[SphericalRepresentation.attr_dict(('ra', 'dec', 'distance'), (u.deg, u.deg, None)),
61+
UnitSphericalRepresentation.attr_dict(('ra', 'dec'), (u.deg, u.deg)),
62+
CartesianRepresentation.attr_dict(('x', 'y', 'z'), (None, None, None))])
63+
64+
_representation = SphericalRepresentation
7465

7566
frame_attr_names = {} # not necessary if empty, but this makes it clearer
7667

@@ -117,21 +108,12 @@ class FK5(BaseCoordinateFrame):
117108
The equinox of this frame.
118109
"""
119110

120-
_representations = {SphericalRepresentation:
121-
{'attr_names': OrderedDict([('ra', 'lon'), ('dec', 'lat'),
122-
('distance', 'distance')]),
123-
'attr_units': {'ra': u.degree, 'dec': u.degree}},
124-
UnitSphericalRepresentation:
125-
{'attr_names': OrderedDict([('ra', 'lon'), ('dec', 'lat')]),
126-
'attr_units': {'ra': u.degree, 'dec': u.degree}},
127-
CartesianRepresentation:
128-
{'attr_names': OrderedDict([('x', 'x'), ('y', 'y'), ('z', 'z')]),
129-
'attr_units': {}}
130-
}
131-
132-
_preferred_representation = SphericalRepresentation
133-
_preferred_attr_names = _representations[_preferred_representation]['attr_names']
134-
_preferred_attr_units = _representations[_preferred_representation]['attr_units']
111+
representation_attrs = dict(
112+
[SphericalRepresentation.attr_dict(('ra', 'dec', 'distance'), (u.deg, u.deg, None)),
113+
UnitSphericalRepresentation.attr_dict(('ra', 'dec'), (u.deg, u.deg)),
114+
CartesianRepresentation.attr_dict(('x', 'y', 'z'), (None, None, None))])
115+
116+
_representation = SphericalRepresentation
135117

136118
frame_attr_names = {'equinox': _EQUINOX_J2000}
137119

@@ -188,21 +170,12 @@ class FK4(BaseCoordinateFrame):
188170
``equinox``.
189171
"""
190172

191-
_representations = {SphericalRepresentation:
192-
{'attr_names': OrderedDict([('ra', 'lon'), ('dec', 'lat'),
193-
('distance', 'distance')]),
194-
'attr_units': {'ra': u.degree, 'dec': u.degree}},
195-
UnitSphericalRepresentation:
196-
{'attr_names': OrderedDict([('ra', 'lon'), ('dec', 'lat')]),
197-
'attr_units': {'ra': u.degree, 'dec': u.degree}},
198-
CartesianRepresentation:
199-
{'attr_names': OrderedDict([('x', 'x'), ('y', 'y'), ('z', 'z')]),
200-
'attr_units': {}}
201-
}
202-
203-
_preferred_representation = SphericalRepresentation
204-
_preferred_attr_names = _representations[_preferred_representation]['attr_names']
205-
_preferred_attr_units = _representations[_preferred_representation]['attr_units']
173+
representation_attrs = dict(
174+
[SphericalRepresentation.attr_dict(('ra', 'dec', 'distance'), (u.deg, u.deg, None)),
175+
UnitSphericalRepresentation.attr_dict(('ra', 'dec'), (u.deg, u.deg)),
176+
CartesianRepresentation.attr_dict(('x', 'y', 'z'), (None, None, None))])
177+
178+
_representation = SphericalRepresentation
206179

207180
frame_attr_names = {'equinox': _EQUINOX_B1950, 'obstime': None}
208181

@@ -246,21 +219,12 @@ class FK4NoETerms(BaseCoordinateFrame):
246219
``equinox``.
247220
"""
248221

249-
_representations = {SphericalRepresentation:
250-
{'attr_names': OrderedDict([('ra', 'lon'), ('dec', 'lat'),
251-
('distance', 'distance')]),
252-
'attr_units': {'ra': u.degree, 'dec': u.degree}},
253-
UnitSphericalRepresentation:
254-
{'attr_names': OrderedDict([('ra', 'lon'), ('dec', 'lat')]),
255-
'attr_units': {'ra': u.degree, 'dec': u.degree}},
256-
CartesianRepresentation:
257-
{'attr_names': OrderedDict([('x', 'x'), ('y', 'y'), ('z', 'z')]),
258-
'attr_units': {}}
259-
}
260-
261-
_preferred_representation = SphericalRepresentation
262-
_preferred_attr_names = _representations[_preferred_representation]['attr_names']
263-
_preferred_attr_units = _representations[_preferred_representation]['attr_units']
222+
representation_attrs = dict(
223+
[SphericalRepresentation.attr_dict(('ra', 'dec', 'distance'), (u.deg, u.deg, None)),
224+
UnitSphericalRepresentation.attr_dict(('ra', 'dec'), (u.deg, u.deg)),
225+
CartesianRepresentation.attr_dict(('x', 'y', 'z'), (None, None, None))])
226+
227+
_representation = SphericalRepresentation
264228

265229
frame_attr_names = {'equinox': _EQUINOX_B1950, 'obstime': None}
266230

@@ -327,21 +291,12 @@ class Galactic(BaseCoordinateFrame):
327291
The Distance for this object along the line-of-sight.
328292
"""
329293

330-
_representations = {SphericalRepresentation:
331-
{'attr_names': OrderedDict([('l', 'lon'), ('b', 'lat'),
332-
('distance', 'distance')]),
333-
'attr_units': {'l': u.degree, 'b': u.degree}},
334-
UnitSphericalRepresentation:
335-
{'attr_names': OrderedDict([('l', 'lon'), ('b', 'lat')]),
336-
'attr_units': {'l': u.degree, 'b': u.degree}},
337-
CartesianRepresentation:
338-
{'attr_names': OrderedDict([('x', 'x'), ('y', 'y'), ('z', 'z')]),
339-
'attr_units': {}}
340-
}
341-
342-
_preferred_representation = SphericalRepresentation
343-
_preferred_attr_names = _representations[_preferred_representation]['attr_names']
344-
_preferred_attr_units = _representations[_preferred_representation]['attr_units']
294+
representation_attrs = dict(
295+
[SphericalRepresentation.attr_dict(('l', 'b', 'distance'), (u.deg, u.deg, None)),
296+
UnitSphericalRepresentation.attr_dict(('l', 'b'), (u.deg, u.deg)),
297+
CartesianRepresentation.attr_dict(('x', 'y', 'z'), (None, None, None))])
298+
299+
_representation = SphericalRepresentation
345300

346301
frame_attr_names = {}
347302

@@ -373,26 +328,13 @@ class AltAz(BaseCoordinateFrame):
373328
The Distance for this object along the line-of-sight.
374329
"""
375330

376-
_representations = {SphericalRepresentation:
377-
{'attr_names': OrderedDict([('az', 'lon'), ('alt', 'lat'),
378-
('distance', 'distance')]),
379-
'attr_units': {'az': u.degree, 'alt': u.degree}},
380-
UnitSphericalRepresentation:
381-
{'attr_names': OrderedDict([('az', 'lon'), ('alt', 'lat')]),
382-
'attr_units': {'az': u.degree, 'alt': u.degree}},
383-
CartesianRepresentation:
384-
{'attr_names': OrderedDict([('x', 'x'), ('y', 'y'), ('z', 'z')]),
385-
'attr_units': {}}
386-
}
387-
388-
_preferred_representation = SphericalRepresentation
389-
_preferred_attr_names = _representations[_preferred_representation]['attr_names']
390-
_preferred_attr_units = _representations[_preferred_representation]['attr_units']
391-
392-
preferred_representation = SphericalRepresentation
393-
preferred_attr_names = OrderedDict([('az', 'lon'), ('alt', 'lat'),
394-
('distance', 'distance')])
395-
preferred_attr_units = {'az': u.degree, 'alt': u.degree}
331+
representation_attrs = dict(
332+
[SphericalRepresentation.attr_dict(('az', 'alt', 'distance'), (u.deg, u.deg, None)),
333+
UnitSphericalRepresentation.attr_dict(('az', 'alt'), (u.deg, u.deg)),
334+
CartesianRepresentation.attr_dict(('x', 'y', 'z'), (None, None, None))])
335+
336+
_representation = SphericalRepresentation
337+
396338
frame_attr_names = {}
397339

398340

@@ -406,26 +348,17 @@ class NoFrame(BaseCoordinateFrame):
406348
407349
"""
408350

409-
_representations = {SphericalRepresentation:
410-
{'attr_names': OrderedDict([('ra', 'lon'), ('dec', 'lat'),
411-
('distance', 'distance')]),
412-
'attr_units': {'ra': u.degree, 'dec': u.degree}},
413-
UnitSphericalRepresentation:
414-
{'attr_names': OrderedDict([('ra', 'lon'), ('dec', 'lat')]),
415-
'attr_units': {'ra': u.degree, 'dec': u.degree}},
416-
CartesianRepresentation:
417-
{'attr_names': OrderedDict([('x', 'x'), ('y', 'y'), ('z', 'z')]),
418-
'attr_units': {}}
419-
}
420-
421-
_preferred_representation = SphericalRepresentation
422-
_preferred_attr_names = _representations[_preferred_representation]['attr_names']
423-
_preferred_attr_units = _representations[_preferred_representation]['attr_units']
351+
representation_attrs = dict(
352+
[SphericalRepresentation.attr_dict(('ra', 'dec', 'distance'), (u.deg, u.deg, None)),
353+
UnitSphericalRepresentation.attr_dict(('ra', 'dec'), (u.deg, u.deg)),
354+
CartesianRepresentation.attr_dict(('x', 'y', 'z'), (None, None, None))])
355+
356+
_representation = SphericalRepresentation
424357

425358
frame_attr_names = {}
426359

427360

428-
#<--------------------------------transformations------------------------------>
361+
# <--------------------------------transformations------------------------------>
429362
# Transformations are defined here instead of in the classes themselves, because
430363
# we need references to the various objects to give to the decorators.
431364

‎astropy/coordinates/representation.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,41 @@ def components(self):
7575
"""Return tuple with the names of the coordinate components"""
7676
raise NotImplementedError()
7777

78+
@classmethod
79+
def attr_dict(cls, names, units):
80+
"""
81+
Return a tuple in the form (cls, {'attr_names': {'ra': 'lon', ...},
82+
'attr_units': {'ra': u.deg, ...}}
83+
This is a convenience method to make frame class definitions more
84+
concise and readable. The output is intended to use the tuple to
85+
initialize the frame representation_attrs dict which maps cls to
86+
the above nested dict.
87+
88+
Parameters
89+
----------
90+
names : tuple of strings
91+
Names of attributes in frame corresponding to representation
92+
attributes, in order.
93+
units : tuple of `~astropy.units.Unit` or None values
94+
Units of attributes in frame corresponding to representation
95+
attributes, in order. None implies no unit.
96+
97+
98+
"""
99+
attr_names = OrderedDict()
100+
attr_units = OrderedDict()
101+
# cls._attr_classes gives the representation attribute names in order
102+
if len(names) != len(cls._attr_classes):
103+
raise ValueError('Must have same number of names as representation attributes')
104+
105+
for name, repr_name in zip(names, cls._attr_classes):
106+
attr_names[name] = repr_name
107+
for name, unit in zip(names, units):
108+
if unit is not None:
109+
attr_units[name] = unit
110+
111+
return (cls, {'attr_names': attr_names, 'attr_units': attr_units})
112+
78113
def __getitem__(self, view):
79114
return self.__class__(*[getattr(self, component)[view]
80115
for component in self.components])

0 commit comments

Comments
 (0)