Skip to content

Commit

Permalink
add high level api functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nden committed Jun 21, 2018
1 parent 42f38e3 commit 2b1c096
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 42 deletions.
20 changes: 13 additions & 7 deletions gwcs/coordinate_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,10 @@ def coordinates(self, *args):
args : float
inputs to wcs.input_frame
"""
# Reorder axes if necessary.
return coord.SkyCoord(*args, unit=self.unit, frame=self._reference_frame)
if isinstance(args[0], coord.SkyCoord):
return args[0].transform_to(self.reference_frame)
else:
return coord.SkyCoord(*args, unit=self.unit, frame=self.reference_frame)

def coordinate_to_quantity(self, *coords):
if len(coords) == 2:
Expand Down Expand Up @@ -277,9 +279,9 @@ def __init__(self, axes_order=(0,), reference_frame=None, unit=None,
unit=unit, name=name,
reference_position=reference_position)

def coordinates(self, *args):
def coordinates(self, *args, equivalencies=[]):
if hasattr(args[0], 'unit'):
return args[0]
return args[0].to(self.unit[0], equivalencies=equivalencies)
if np.isscalar(args):
return args * self.unit[0]
else:
Expand Down Expand Up @@ -400,9 +402,13 @@ def __repr__(self):

def coordinates(self, *args):
coo = []
for frame in self.frames:
fargs = [args[i] for i in frame.axes_order]
coo.append(frame.coordinates(*fargs))
if len(args) == len(self.frames):
for frame, arg in zip(self.frames, args):
coo.append(frame.coordinates(arg))
else:
for frame in self.frames:
fargs = [args[i] for i in frame.axes_order]
coo.append(frame.coordinates(*fargs))
return coo

def coordinate_to_quantity(self, *coords):
Expand Down
40 changes: 36 additions & 4 deletions gwcs/tests/test_wcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
(icrs, None)
]

# Create some data.
nx, ny = (5, 2)
x = np.linspace(0, 1, nx)
y = np.linspace(0, 1, ny)
xv, yv = np.meshgrid(x, y)

# Test initializing a WCS

def test_create_wcs():
Expand Down Expand Up @@ -295,6 +301,28 @@ def test_available_frames():
assert w.available_frames == ['detector', 'focal', 'icrs']


def test_high_level_api():
"""
Test WCS high level API.
"""
output_frame = cf.CompositeFrame(frames=[icrs, spec])
transform = m1 & models.Scale(1.5)
w = wcs.WCS(forward_transform=transform, output_frame=output_frame)

r, d, l = w(xv, yv, xv)
world_coord = w.pixel_to_world(xv, yv, xv)
assert isinstance(world_coord[0], coord.SkyCoord)
assert isinstance(world_coord[1], u.Quantity)
assert_allclose(world_coord[0].data.lon.value, r)
assert_allclose(world_coord[0].data.lat.value, d)
assert_allclose(world_coord[1].value, l)

x1, y1, z1 = w.world_to_pixel(*world_coord)
assert_allclose(x1, xv)
assert_allclose(y1, yv)
assert_allclose(z1, xv)


class TestImaging(object):
def setup_class(self):
hdr = fits.Header.fromtextfile(get_pkg_data_filename("data/acs.hdr"), endcard=False)
Expand Down Expand Up @@ -333,10 +361,7 @@ def setup_class(self):
self.wcs = wcs.WCS(input_frame=det,
output_frame=sky_cs,
forward_transform=pipeline)
nx, ny = (5, 2)
x = np.linspace(0, 1, nx)
y = np.linspace(0, 1, ny)
self.xv, self.yv = np.meshgrid(x, y)
self.xv, self.yv = xv, yv

@pytest.mark.filterwarnings('ignore')
def test_distortion(self):
Expand Down Expand Up @@ -389,3 +414,10 @@ def test_get_transform(self):
with pytest.raises(wcs.CoordinateFrameError):
assert(self.wcs.get_transform('x_translation', 'sky_rotation').submodel_names ==
self.wcs.forward_transform[1:].submodel_names)

def test_pixel_to_world(self):
sky_coord = self.wcs.pixel_to_world(self.xv, self.yv)
ra, dec = self.fitsw.all_pix2world(self.xv, self.yv, 1)
assert isinstance(sky_coord, coord.SkyCoord)
assert_allclose(sky_coord.data.lon.value, ra)
assert_allclose(sky_coord.data.lat.value, dec)
53 changes: 22 additions & 31 deletions gwcs/wcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,62 +550,53 @@ def footprint(self, bounding_box=None, center=False):

# APE 14 implementation

@property
def pixel_n_dim(self):
return self.input_frame.naxes

@property
def world_n_dim(self):
return self.output_frame.naxes

@property
def pixel_shape(self):
return None

@property
def pixel_bounds(self):
return self.bounding_box

@property
def world_axis_physical_types(self):
# Need to match with UCD type
return self.output_frame.axes_types
return self.output_frame.axes_type

@property
def world_axis_units(self):
return self.output_frame.unit
return [unit.to_string() for unit in self.output_frame.unit]

@property
def axis_correlation_matrix(self):
return is_separable(self.forward_transform)

def pixel_to_world_values(self, *pixel_arrays):
# need **kwargs
return self.__call__(*pixel_arrays, **kwargs)

def world_to_pixel_values(self, *world_arrays):
# needs **kwargs
return self.invert(*world_arrays, **kwargs)
from astropy.modeling import separable
return separable.is_separable(self.forward_transform)

def pixel_to_world(self, *pixel_arrays):
"""
Convert pixel values to world coordinates.
"""
pass
if not self.forward_transform.uses_quantity:
kwargs = {'with_units': True}
return self.__call__(*pixel_arrays, **kwargs)

def world_to_pixel(self, *world_objects):
"""
Convert pixel values to world coordinates.
Convert world coordinates to pixel values.
"""
pass

#@property
#def world_axis_object_components(self):
#components = []
#outframe = self.output_frame
#if isinstance(outframe, cf.CompoundFrame):
#for frame in outframe.frames:
#components.append(self.output_frame.axes_names[axis])
#return components

#@property
#def world_axis_object_classes(self):
#classes = {}
#for axis in self.output_frame.axes_order:
#classes[self.output_frame.axes_names[axis]] = ()
#return classes

args = self.output_frame.coordinates(*world_objects)

if len(world_objects) == 1:
args = [args]
if not self.forward_transform.uses_quantity:
args = self.output_frame.coordinate_to_quantity(*args)
args = utils.get_values(self.output_frame.unit, *args)
return self.invert(*args)

0 comments on commit 2b1c096

Please sign in to comment.