Skip to content
This repository has been archived by the owner on Jun 16, 2018. It is now read-only.

Commit

Permalink
Allow metadata to be passed when passing transform instead of WCS to …
Browse files Browse the repository at this point in the history
…get_coords_overlay
  • Loading branch information
astrofrog committed Jun 30, 2014
1 parent 59b1df8 commit 91fa259
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
9 changes: 5 additions & 4 deletions wcsaxes/coordinate_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ def wrap_angle_at(values, coord_wrap):

class CoordinateHelper(object):

def __init__(self, parent_axes=None, transform=None, coord_index=None,
def __init__(self, parent_axes=None, parent_map=None, transform=None, coord_index=None,
coord_type='scalar',coord_unit=None, coord_wrap=None, frame=None):

# Keep a reference to the parent axes and the transform
self.parent_axes = parent_axes
self.parent_map = parent_map
self.transform = transform
self.coord_index = coord_index
self.coord_type = coord_type
Expand Down Expand Up @@ -336,7 +337,7 @@ def _update_ticks(self, renderer):
# to determine the rotations.

# Find the range of coordinates in all directions
coord_range = self.parent_axes.get_coord_range(self.transform)
coord_range = self.parent_map.get_coord_range()

# First find the ticks we want to show
tick_world_coordinates, spacing = self._formatter_locator.locator(*coord_range[self.coord_index])
Expand Down Expand Up @@ -480,7 +481,7 @@ def _update_grid_lines(self):
# the value in the slice). Here we basically assume that if the WCS
# had a third axis, it has been abstracted away in the transformation.

coord_range = self.parent_axes.get_coord_range(self.transform)
coord_range = self.parent_map.get_coord_range()

tick_world_coordinates, spacing = self._formatter_locator.locator(*coord_range[self.coord_index])
tick_world_coordinates_values = tick_world_coordinates.value
Expand Down Expand Up @@ -509,7 +510,7 @@ def _update_grid_contour(self):

X, Y, field = self.transform.get_coord_slices(xmin, xmax, ymin, ymax, 200, 200)

coord_range = self.parent_axes.get_coord_range(self.transform)
coord_range = self.parent_map.get_coord_range()

tick_world_coordinates, spacing = self._formatter_locator.locator(*coord_range[self.coord_index])

Expand Down
30 changes: 26 additions & 4 deletions wcsaxes/coordinates_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
from .transforms import WCSPixel2WorldTransform
from .utils import coord_type_from_ctype
from .frame import RectangularFrame
from .coordinate_range import find_coordinate_range

from . import six


class CoordinatesMap(object):

def __init__(self, axes, wcs, transform=None, slice=None):
def __init__(self, axes, wcs, transform=None, coord_meta=None, slice=None):

# Keep track of parent axes and WCS
self._axes = axes
Expand All @@ -28,9 +29,24 @@ def __init__(self, axes, wcs, transform=None, slice=None):

for coord_index in range(self._wcs.wcs.naxis):

coord_type, coord_wrap = coord_type_from_ctype(wcs.wcs.ctype[coord_index])
coord_unit = wcs.wcs.cunit[coord_index]
# Extract coordinate metadata from WCS object or transform
if transform is None:
coord_type, coord_wrap = coord_type_from_ctype(wcs.wcs.ctype[coord_index])
coord_unit = wcs.wcs.cunit[coord_index]
name = self._wcs.wcs.ctype[coord_index][:4].replace('-', '')
elif coord_meta is not None:
try:
coord_type = coord_meta['type'][coord_index]
coord_wrap = coord_meta['wrap'][coord_index]
coord_unit = coord_meta['unit'][coord_index]
name = coord_meta['name'][coord_index]
except IndexError:
raise ValueError("coord_meta items should have a length of {0}".format(len(self._wcs.wcs.naxis)))
else:
raise ValueError("coord_meta should be set")

self._coords.append(CoordinateHelper(parent_axes=axes,
parent_map=self,
transform=self._transform,
coord_index=coord_index,
coord_type=coord_type,
Expand All @@ -40,7 +56,6 @@ def __init__(self, axes, wcs, transform=None, slice=None):


# Set up aliases for coordinates
name = self._wcs.wcs.ctype[coord_index][:4].replace('-', '')
self._aliases[name.lower()] = coord_index

def __getitem__(self, item):
Expand Down Expand Up @@ -84,3 +99,10 @@ def grid(self, draw_grid=True, grid_type='lines', **kwargs):
"""
for coord in self:
coord.grid(draw_grid=draw_grid, grid_type=grid_type, **kwargs)

def get_coord_range(self):
xmin, xmax = self._axes.get_xlim()
ymin, ymax = self._axes.get_ylim()
return find_coordinate_range(self._transform,
[xmin, xmax, ymin, ymax],
[coord.coord_type for coord in self])
13 changes: 3 additions & 10 deletions wcsaxes/wcsaxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,6 @@ def reset_wcs(self, wcs, slices=None):
self.coords[coord_index].set_ticklabel_position('')
self.coords[coord_index].set_ticks_position('')

def get_coord_range(self, transform):
xmin, xmax = self.get_xlim()
ymin, ymax = self.get_ylim()
return find_coordinate_range(transform,
[xmin, xmax, ymin, ymax],
[coord.coord_type for coord in self.coords])

def draw(self, renderer, inframe=False):

super(WCSAxes, self).draw(renderer, inframe)
Expand Down Expand Up @@ -113,15 +106,15 @@ def get_xlabel(self):
def get_ylabel(self):
return self.coords[1].get_axislabel()

def get_coords_overlay(self, frame, equinox=None, obstime=None):
def get_coords_overlay(self, frame, equinox=None, obstime=None, coord_meta=None):

# Here we can't use get_transform because that deals with
# pixel-to-pixel transformations when passing a WCS object.
if isinstance(frame, WCS):
coords = CoordinatesMap(self, frame)
coords = CoordinatesMap(self, frame, coord_meta=coord_meta)
else:
transform = self._get_transform_no_transdata(frame, equinox=equinox, obstime=obstime)
coords = CoordinatesMap(self, self.wcs, transform=transform)
coords = CoordinatesMap(self, self.wcs, transform=transform, coord_meta=coord_meta)

self._all_coords.append(coords)

Expand Down

0 comments on commit 91fa259

Please sign in to comment.