From b2eaf0dbf910cd92a938475709b005b286b14750 Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Tue, 16 Dec 2014 00:48:40 +0100 Subject: [PATCH] Remove unused keywords and allow frame instances to be used for ``get_transform`` --- wcsaxes/core.py | 11 +++--- .../tests/test_display_world_coordinates.py | 36 +++++++++++++++++++ wcsaxes/transforms.py | 13 ++++++- wcsaxes/utils.py | 6 +++- 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/wcsaxes/core.py b/wcsaxes/core.py index 9b0eada..05ed7bf 100644 --- a/wcsaxes/core.py +++ b/wcsaxes/core.py @@ -177,7 +177,7 @@ def get_xlabel(self): def get_ylabel(self): return self.coords[self._y_index].get_axislabel() - def get_coords_overlay(self, frame, equinox=None, obstime=None, coord_meta=None): + def get_coords_overlay(self, frame, coord_meta=None): # Here we can't use get_transform because that deals with # pixel-to-pixel transformations when passing a WCS object. @@ -186,7 +186,7 @@ def get_coords_overlay(self, frame, equinox=None, obstime=None, coord_meta=None) else: if coord_meta is None: coord_meta = get_coord_meta(frame) - transform = self._get_transform_no_transdata(frame, equinox=equinox, obstime=obstime) + transform = self._get_transform_no_transdata(frame) coords = CoordinatesMap(self, transform=transform, coord_meta=coord_meta, frame_class=self.frame_class) self._all_coords.append(coords) @@ -201,7 +201,7 @@ def get_coords_overlay(self, frame, equinox=None, obstime=None, coord_meta=None) return coords - def get_transform(self, frame, equinox=None, obstime=None): + def get_transform(self, frame): """ Return a transform from the specified frame to display coordinates. @@ -230,10 +230,11 @@ def get_transform(self, frame, equinox=None, obstime=None): ``WCSAxes`` instance). * ``'fk5'`` or ``'galactic'``: return a transformation from the specified frame to the pixel/data coordinates. + * :class:`~astropy.coordinates.BaseCoordinateFrame` instance. """ - return self._get_transform_no_transdata(frame, equinox=equinox, obstime=obstime).inverted() + self.transData + return self._get_transform_no_transdata(frame).inverted() + self.transData - def _get_transform_no_transdata(self, frame, equinox=None, obstime=None): + def _get_transform_no_transdata(self, frame): """ Return a transform from data to the specified frame """ diff --git a/wcsaxes/tests/test_display_world_coordinates.py b/wcsaxes/tests/test_display_world_coordinates.py index a673db2..a8686b9 100644 --- a/wcsaxes/tests/test_display_world_coordinates.py +++ b/wcsaxes/tests/test_display_world_coordinates.py @@ -6,6 +6,8 @@ from astropy.wcs import WCS from astropy.extern import six from astropy.tests.helper import pytest +from astropy.coordinates import FK5 +from astropy.time import Time from .test_images import BaseImageTests @@ -58,6 +60,40 @@ def test_overlay_coords(self, tmpdir): assert string_world3 == six.u('267.176 -28\xb045\'56" (world, overlay 1)') + overlay = ax.get_coords_overlay(FK5()) + + # Regression test for bug that caused format to always be taken from + # main world coordinates. + overlay[0].set_major_formatter('d.ddd') + + # On some systems, fig.canvas.draw is not enough to force a draw, so we + # save to a temporary file. + fig.savefig(tmpdir.join('test3.png').strpath) + + event5 = KeyEvent('test_pixel_coords', canvas, 'w') + fig.canvas.key_press_event(event4.key, guiEvent=event4) + # Test that it displays the overlay world coordinates + string_world4 = ax._display_world_coords(0.523412, 0.518311) + + assert string_world4 == six.u('267.176 -28\xb045\'56" (world, overlay 2)') + + overlay = ax.get_coords_overlay(FK5(equinox=Time("J2030"))) + + # Regression test for bug that caused format to always be taken from + # main world coordinates. + overlay[0].set_major_formatter('d.ddd') + + # On some systems, fig.canvas.draw is not enough to force a draw, so we + # save to a temporary file. + fig.savefig(tmpdir.join('test4.png').strpath) + + event6 = KeyEvent('test_pixel_coords', canvas, 'w') + fig.canvas.key_press_event(event5.key, guiEvent=event4) + # Test that it displays the overlay world coordinates + string_world5 = ax._display_world_coords(0.523412, 0.518311) + + assert string_world5 == six.u('267.652 -28\xb046\'23" (world, overlay 3)') + def test_cube_coords(self, tmpdir): wcs = WCS(self.cube_header) diff --git a/wcsaxes/transforms.py b/wcsaxes/transforms.py index 53f9293..38d9b13 100644 --- a/wcsaxes/transforms.py +++ b/wcsaxes/transforms.py @@ -16,7 +16,9 @@ from astropy.wcs import WCS from astropy.extern import six from astropy.coordinates import (SkyCoord, frame_transform_graph, - SphericalRepresentation, UnitSphericalRepresentation) + SphericalRepresentation, + UnitSphericalRepresentation, + BaseCoordinateFrame) from .wcs_utils import wcs_to_celestial_frame @@ -173,6 +175,7 @@ def inverted(self): class CoordinateTransform(CurvedTransform): + def __init__(self, input_system, output_system): super(CoordinateTransform, self).__init__() self._input_system_name = input_system @@ -184,6 +187,10 @@ def __init__(self, input_system, output_system): self.input_system = frame_transform_graph.lookup_name(self._input_system_name) if self.input_system is None: raise ValueError("Frame {0} not found".format(self._input_system_name)) + elif isinstance(self._input_system_name, BaseCoordinateFrame): + self.input_system = self._input_system_name + else: + raise TypeError("input_system should be a WCS instance, string, or a coordinate frame instance") if isinstance(self._output_system_name, WCS): self.output_system = wcs_to_celestial_frame(self._output_system_name) @@ -191,6 +198,10 @@ def __init__(self, input_system, output_system): self.output_system = frame_transform_graph.lookup_name(self._output_system_name) if self.output_system is None: raise ValueError("Frame {0} not found".format(self._output_system_name)) + elif isinstance(self._output_system_name, BaseCoordinateFrame): + self.output_system = self._output_system_name + else: + raise TypeError("output_system should be a WCS instance, string, or a coordinate frame instance") if self.output_system == self.input_system: self.same_frames = True diff --git a/wcsaxes/utils.py b/wcsaxes/utils.py index 0f39b8a..4ee7028 100644 --- a/wcsaxes/utils.py +++ b/wcsaxes/utils.py @@ -3,6 +3,7 @@ from astropy import units as u from astropy.extern import six +from astropy.coordinates import BaseCoordinateFrame # Modified from axis_artist, supports astropy.units @@ -107,7 +108,10 @@ def get_coord_meta(frame): if isinstance(frame, six.string_types): frame = frame_transform_graph.lookup_name(frame) - names = list(frame().representation_component_names.keys()) + if not isinstance(frame, BaseCoordinateFrame): + frame = frame() + + names = list(frame.representation_component_names.keys()) coord_meta['name'] = names[:2] except ImportError: