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

Commit

Permalink
Merge 814d091 into 764d997
Browse files Browse the repository at this point in the history
  • Loading branch information
astrofrog committed Dec 7, 2014
2 parents 764d997 + 814d091 commit c8e0d75
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 221 deletions.
7 changes: 4 additions & 3 deletions wcsaxes/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from .transforms import (WCSPixel2WorldTransform, WCSWorld2PixelTransform,
CoordinateTransform)
from .coordinates_map import CoordinatesMap
from .utils import get_coordinate_frame, get_coord_meta
from .utils import get_coord_meta
from .wcs_utils import wcs_to_celestial_frame
from .frame import RectangularFrame
import numpy as np

Expand Down Expand Up @@ -242,8 +243,8 @@ def _get_transform_no_transdata(self, frame, equinox=None, obstime=None):

if isinstance(frame, WCS):

coord_in = get_coordinate_frame(self.wcs)
coord_out = get_coordinate_frame(frame)
coord_in = wcs_to_celestial_frame(self.wcs)
coord_out = wcs_to_celestial_frame(frame)

if coord_in == coord_out:

Expand Down
41 changes: 0 additions & 41 deletions wcsaxes/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,44 +69,3 @@ def test_coord_type_from_ctype():
assert coord_type_from_ctype('RA--') == ('longitude', None)
assert coord_type_from_ctype('DEC-') == ('latitude', None)
assert coord_type_from_ctype('spam') == ('scalar', None)


def test_get_coordinate_frame():

from ..utils import get_coordinate_frame, register_frame_identifier, reset_frame_identifiers
from astropy.coordinates import FK5, Galactic
from astropy.tests.helper import pytest

wcs = WCS(naxis=2)
wcs.wcs.ctype = ['RA---TAN', 'DEC--TAN']

assert get_coordinate_frame(wcs) is FK5

wcs = WCS(naxis=2)
wcs.wcs.ctype = ['GLON-CAR', 'GLAT-CAR']

assert get_coordinate_frame(wcs) is Galactic

wcs = WCS(naxis=2)
wcs.wcs.ctype = ['SOLARX', 'SOLARY']

with pytest.raises(ValueError) as exc:
get_coordinate_frame(wcs)
assert exc.value.args[0] == "Frame not supported: SOLARX/SOLARY"

class SolarXY(object):
pass

def identify_solar(wcs):
if wcs.wcs.ctype[0] == "SOLARX" and wcs.wcs.ctype[1] == "SOLARY":
return SolarXY

register_frame_identifier(identify_solar)

assert get_coordinate_frame(wcs) is SolarXY

reset_frame_identifiers()

with pytest.raises(ValueError) as exc:
get_coordinate_frame(wcs)
assert exc.value.args[0] == "Frame not supported: SOLARX/SOLARY"
187 changes: 60 additions & 127 deletions wcsaxes/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
# coordinates, but also world-to-world).

import abc

import numpy as np

from matplotlib.path import Path
from matplotlib.transforms import Transform

from astropy import units as u
from astropy.wcs import WCS
from astropy.extern import six
from .utils import get_coordinate_frame
from astropy.coordinates import (SkyCoord, frame_transform_graph,
SphericalRepresentation, UnitSphericalRepresentation)

from .wcs_utils import wcs_to_celestial_frame


@six.add_metaclass(abc.ABCMeta)
Expand Down Expand Up @@ -53,7 +59,6 @@ def inverted(self):


class WCSWorld2PixelTransform(CurvedTransform):

"""
WCS transformation from world to pixel coordinates
"""
Expand Down Expand Up @@ -102,7 +107,6 @@ def inverted(self):


class WCSPixel2WorldTransform(CurvedTransform):

"""
WCS transformation from pixel to world coordinates
"""
Expand Down Expand Up @@ -168,137 +172,66 @@ def inverted(self):
return WCSWorld2PixelTransform(self.wcs, slice=self.slice)


try:

from astropy.coordinates import (SkyCoord, frame_transform_graph,
SphericalRepresentation, UnitSphericalRepresentation)

class CoordinateTransform(CurvedTransform):
def __init__(self, input_system, output_system):
super(CoordinateTransform, self).__init__()
self._input_system_name = input_system
self._output_system_name = output_system

if isinstance(self._input_system_name, WCS):
self.input_system = get_coordinate_frame(self._input_system_name)
elif isinstance(self._input_system_name, six.string_types):
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))

if isinstance(self._output_system_name, WCS):
self.output_system = get_coordinate_frame(self._output_system_name)
elif isinstance(self._output_system_name, six.string_types):
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))

if self.output_system == self.input_system:
self.same_frames = True
else:
self.same_frames = False

@property
def same_frames(self):
return self._same_frames

@same_frames.setter
def same_frames(self, same_frames):
self._same_frames = same_frames

def transform(self, input_coords):
"""
Transform one set of coordinates to another
"""
if self.same_frames:
return input_coords

x_in, y_in = input_coords[:, 0], input_coords[:, 1]

c_in = SkyCoord(x_in, y_in, unit=(u.deg, u.deg), frame=self.input_system)

c_out = c_in.transform_to(self.output_system)

if (c_out.representation is SphericalRepresentation or
c_out.representation is UnitSphericalRepresentation):
lon = c_out.data.lon.deg
lat = c_out.data.lat.deg
else:
lon = c_out.spherical.lon.deg
lat = c_out.spherical.lat.deg

return np.concatenate((lon[:, np.newaxis], lat[:, np.newaxis]), axis=1)

transform_non_affine = transform

def inverted(self):
"""
Return the inverse of the transform
"""
return CoordinateTransform(self._output_system_name, self._input_system_name)

except ImportError:

class CoordinateTransform(CurvedTransform):
class CoordinateTransform(CurvedTransform):
def __init__(self, input_system, output_system):
super(CoordinateTransform, self).__init__()
self._input_system_name = input_system
self._output_system_name = output_system

if isinstance(self._input_system_name, WCS):
self.input_system = wcs_to_celestial_frame(self._input_system_name)
elif isinstance(self._input_system_name, six.string_types):
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))

if isinstance(self._output_system_name, WCS):
self.output_system = wcs_to_celestial_frame(self._output_system_name)
elif isinstance(self._output_system_name, six.string_types):
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))

if self.output_system == self.input_system:
self.same_frames = True
else:
self.same_frames = False

def __init__(self, input_system, output_system):
super(CoordinateTransform, self).__init__()
from astropy.coordinates import FK5, Galactic
self._input_system_name = input_system
self._output_system_name = output_system
@property
def same_frames(self):
return self._same_frames

if isinstance(self._input_system_name, WCS):
self.input_system = get_coordinate_frame(self._input_system_name)
elif isinstance(self._input_system_name, six.string_types):
if self._input_system_name == 'fk5':
self.input_system = FK5
elif self._input_system_name == 'galactic':
self.input_system = Galactic
else:
raise NotImplementedError("frame {0} not implemented".format(self._input_system_name))

if isinstance(self._output_system_name, WCS):
self.output_system = get_coordinate_frame(self._output_system_name)
elif isinstance(self._output_system_name, six.string_types):
if self._output_system_name == 'fk5':
self.output_system = FK5
elif self._output_system_name == 'galactic':
self.output_system = Galactic
else:
raise NotImplementedError("frame {0} not implemented".format(self._output_system_name))

if self.output_system == self.input_system:
self.same_frames = True
else:
self.same_frames = False
@same_frames.setter
def same_frames(self, same_frames):
self._same_frames = same_frames

@property
def same_frames(self):
return self._same_frames

@same_frames.setter
def same_frames(self, same_frames):
self._same_frames = same_frames
def transform(self, input_coords):
"""
Transform one set of coordinates to another
"""
if self.same_frames:
return input_coords

def transform(self, input_coords):
"""
Transform one set of coordinates to another
"""
if self.same_frames:
return input_coords
x_in, y_in = input_coords[:, 0], input_coords[:, 1]

x_in, y_in = input_coords[:, 0], input_coords[:, 1]
c_in = SkyCoord(x_in, y_in, unit=(u.deg, u.deg), frame=self.input_system)

c_in = self.input_system(x_in, y_in, unit=(u.deg, u.deg))
c_out = c_in.transform_to(self.output_system)

c_out = c_in.transform_to(self.output_system)
if (c_out.representation is SphericalRepresentation or
c_out.representation is UnitSphericalRepresentation):
lon = c_out.data.lon.deg
lat = c_out.data.lat.deg
else:
lon = c_out.spherical.lon.deg
lat = c_out.spherical.lat.deg

return np.concatenate((c_out.lonangle.deg[:, np.newaxis], c_out.latangle.deg[:, np.newaxis]), 1)
return np.concatenate((lon[:, np.newaxis], lat[:, np.newaxis]), axis=1)

transform_non_affine = transform
transform_non_affine = transform

def inverted(self):
"""
Return the inverse of the transform
"""
return CoordinateTransform(self._output_system_name, self._input_system_name)
def inverted(self):
"""
Return the inverse of the transform
"""
return CoordinateTransform(self._output_system_name, self._input_system_name)
50 changes: 0 additions & 50 deletions wcsaxes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,56 +93,6 @@ def select_step_scalar(dv):
return 10. ** (base + steps[imin])


FRAME_IDENTIFIERS = []


def register_frame_identifier(func):
"""
Register a function that can identify frames from WCS objects.
This should be a function that takes an :class:`~astropy.wcs.WCS` object
and returns an `astropy.coordinates`-compatible frame class, or `None` if
no match was found.
"""
FRAME_IDENTIFIERS.append(func)


def reset_frame_identifiers():
"""
Remove any registered frame identifiers.
"""
global FRAME_IDENTIFIERS
FRAME_IDENTIFIERS = []


def get_coordinate_frame(wcs):
"""
Given a WCS object for a pair of spherical coordinates, return the
corresponding astropy coordinate class.
"""

xcoord = wcs.wcs.ctype[0][0:4]
ycoord = wcs.wcs.ctype[1][0:4]

from astropy.coordinates import FK5, Galactic

if xcoord == 'RA--' and ycoord == 'DEC-':
coordinate_class = FK5
elif xcoord == 'GLON' and ycoord == 'GLAT':
coordinate_class = Galactic
else:
coordinate_class = None
for ident in FRAME_IDENTIFIERS:
coordinate_class = ident(wcs)
if coordinate_class is not None:
break
if coordinate_class is None:
raise ValueError("Frame not supported: {0}/{1}".format(wcs.wcs.ctype[0],
wcs.wcs.ctype[1]))

return coordinate_class


def get_coord_meta(frame):

coord_meta = {}
Expand Down
Loading

0 comments on commit c8e0d75

Please sign in to comment.