Last active
August 29, 2015 14:00
-
-
Save taldcroft/56680d3e14429a892bbb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| # Licensed under a 3-clause BSD style license - see LICENSE.rst | |
| """ | |
| Tests for the SkyCoord class. Note that there are also SkyCoord tests in | |
| test_api_ape5.py | |
| """ | |
| from __future__ import (absolute_import, division, print_function, | |
| unicode_literals) | |
| import functools | |
| import numpy as np | |
| import time | |
| import astropy.units as u | |
| from astropy.tests.helper import pytest | |
| from astropy.coordinates import (ICRS, FK4, FK5, FK4NoETerms, Galactic, SkyCoord, Angle, | |
| SphericalRepresentation, CartesianRepresentation) | |
| from astropy.coordinates.sky_coordinate import CLASS_TO_NAME_MAP | |
| from astropy.time import Time | |
| from astropy.table import Table | |
| RA = 1.0 * u.deg | |
| DEC = 2.0 * u.deg | |
| C_ICRS = ICRS(RA, DEC) | |
| C_FK5 = C_ICRS.transform_to(FK5) | |
| J2001 = Time('J2001', scale='utc') | |
| allclose = functools.partial(np.allclose, rtol=0.0, atol=1e-8) | |
| # set up for parametrized test | |
| rt_sets = [] | |
| rt_frames = [ICRS, FK4, FK5, FK4NoETerms, Galactic] | |
| for rt_frame0 in rt_frames: | |
| for rt_frame1 in rt_frames: | |
| for equinox0 in (None, 'J1975.0'): | |
| for obstime0 in (None, 'J1980.0'): | |
| for equinox1 in (None, 'J1975.0'): | |
| for obstime1 in (None, 'J1980.0'): | |
| rt_sets.append([rt_frame0, rt_frame1, | |
| equinox0, equinox1, | |
| obstime0, obstime1]) | |
| def test_round_trip(frame0, frame1, equinox0, equinox1, obstime0, obstime1): | |
| """ | |
| Test round tripping out and back using transform_to in every combination. | |
| """ | |
| row = dict(frame0=CLASS_TO_NAME_MAP[frame0], frame1=CLASS_TO_NAME_MAP[frame1], | |
| equinox0=equinox0 or '...', equinox1=equinox1 or '...', | |
| obstime0=obstime0 or '...', obstime1=obstime1 or '...') | |
| print(row) | |
| attrs0 = {'equinox': equinox0, 'obstime': obstime0} | |
| attrs1 = {'equinox': equinox1, 'obstime': obstime1} | |
| # Remove None values | |
| attrs0 = dict((k, v) for k, v in attrs0.items() if v is not None) | |
| attrs1 = dict((k, v) for k, v in attrs1.items() if v is not None) | |
| try: | |
| # Go out and back | |
| t0 = time.time() | |
| row['attrs0'] = str({k: getattr(v, 'value', v) for k, v in attrs0.items()}) | |
| sc = SkyCoord(frame0, RA, DEC, **attrs0) | |
| t1 = time.time() | |
| dt = '{:8.3f}'.format((t1 - t0) * 1000) | |
| row['dt1'] = dt | |
| # Keep only frame attributes for frame1 | |
| attrs1 = dict((attr, val) for attr, val in attrs1.items() | |
| if attr in frame1.frame_attr_names) | |
| row['attrs1'] = str({k: getattr(v, 'value', v) for k, v in attrs1.items()}) | |
| t0 = time.time() | |
| sc2 = sc.transform_to(frame1(**attrs1)) | |
| t1 = time.time() | |
| dt = '{:8.3f}'.format((t1 - t0) * 1000) | |
| row['dt2'] = dt | |
| # When coming back only keep frame0 attributes for transform_to | |
| attrs0 = dict((attr, val) for attr, val in attrs0.items() | |
| if attr in frame0.frame_attr_names) | |
| # also, if any are None, fill in with defaults | |
| for attrnm in frame0.frame_attr_names: | |
| if attrs0.get(attrnm, None) is None: | |
| if attrnm=='obstime' and frame0.frame_attr_names[attrnm] is None: | |
| attrs0[attrnm] = attrs0['equinox'] | |
| else: | |
| attrs0[attrnm] = frame0.frame_attr_names[attrnm] | |
| row['attrs0rt'] = str({k: getattr(v, 'value', v) for k, v in attrs0.items()}) | |
| t0 = time.time() | |
| sc_rt = sc2.transform_to(frame0(**attrs0)) | |
| t1 = time.time() | |
| dt = '{:8.3f}'.format((t1 - t0) * 1000) | |
| row['dt3'] = dt | |
| row['OK'] = 'OK' | |
| if frame0 is Galactic: | |
| row['coord'] = str(allclose(sc.l, sc_rt.l) and allclose(sc.b, sc_rt.b)) | |
| else: | |
| row['coord'] = str(allclose(sc.ra, sc_rt.ra) and allclose(sc.dec, sc_rt.dec)) | |
| if equinox0: | |
| row['equinox'] = str(Time(sc.equinox) == Time(sc_rt.equinox)) | |
| else: | |
| row['equinox'] = '...' | |
| if obstime0: | |
| row['obstime'] = str(Time(sc.obstime) == Time(sc_rt.obstime)) | |
| else: | |
| row['obstime'] = '...' | |
| except: | |
| row['OK'] = 'FAIL' | |
| return row | |
| rows = [] | |
| names1 = 'frame0 frame1 equinox0 equinox1 obstime0 obstime1'.split() | |
| names2 = ['attrs0', 'attrs1', 'attrs0rt', 'dt1', 'dt2', 'dt3', 'OK', 'coord', 'equinox', 'obstime'] | |
| for rt_set in rt_sets: | |
| row = test_round_trip(*rt_set) | |
| for name in names2: | |
| if name not in row: | |
| row[name] = '---' | |
| rows.append(row) | |
| t = Table(rows, names=names1 + names2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment