Skip to content

Commit

Permalink
Fix units in waoc
Browse files Browse the repository at this point in the history
fixes #495
  • Loading branch information
Cadair committed Sep 26, 2024
1 parent 506745b commit a5d7642
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 31 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

- Fix an issue with units in ``wcs_from_points``. [#507]

- Fix incorrect units being returned in the low level WCS API. [#512]

0.21.0 (2024-03-10)
-------------------

Expand Down
6 changes: 3 additions & 3 deletions gwcs/coordinate_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ def _world_axis_object_classes(self):

@property
def _world_axis_object_components(self):
return [('celestial', 0, 'spherical.lon'),
('celestial', 1, 'spherical.lat')]
return [('celestial', 0, lambda sc: sc.spherical.lon.to_value(self.unit[0])),
('celestial', 1, lambda sc: sc.spherical.lat.to_value(self.unit[1]))]

def coordinates(self, *args):
"""
Expand Down Expand Up @@ -457,7 +457,7 @@ def _world_axis_object_classes(self):

@property
def _world_axis_object_components(self):
return [('spectral', 0, 'value')]
return [('spectral', 0, lambda sc: sc.to_value(self.unit[0]))]

def coordinates(self, *args):
# using SpectralCoord
Expand Down
3 changes: 2 additions & 1 deletion gwcs/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ def gwcs_3d_identity_units():
models.Multiply(1 * u.nm / u.pixel))
sky_frame = cf.CelestialFrame(axes_order=(0, 1), name='icrs',
reference_frame=coord.ICRS(),
axes_names=("longitude", "latitude"))
axes_names=("longitude", "latitude"),
unit=(u.arcsec, u.arcsec))
wave_frame = cf.SpectralFrame(axes_order=(2, ), unit=u.nm, axes_names=("wavelength",))

frame = cf.CompositeFrame([sky_frame, wave_frame])
Expand Down
36 changes: 29 additions & 7 deletions gwcs/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ def test_array_index_to_world_values(gwcs_2d_spatial_shift, x, y):

def test_world_axis_object_components_2d(gwcs_2d_spatial_shift):
waoc = gwcs_2d_spatial_shift.world_axis_object_components
assert waoc == [('celestial', 0, 'spherical.lon'),
('celestial', 1, 'spherical.lat')]
assert waoc[0][:2] == ('celestial', 0)
assert callable(waoc[0][2])
assert waoc[1][:2] == ('celestial', 1)
assert callable(waoc[1][2])


def test_world_axis_object_components_2d_generic(gwcs_2d_quantity_shift):
Expand All @@ -177,15 +179,19 @@ def test_world_axis_object_components_2d_generic(gwcs_2d_quantity_shift):

def test_world_axis_object_components_1d(gwcs_1d_freq):
waoc = gwcs_1d_freq.world_axis_object_components
assert waoc == [('spectral', 0, 'value')]
assert [c[:2] for c in waoc] == [('spectral', 0)]
assert callable(waoc[0][2])


def test_world_axis_object_components_4d(gwcs_4d_identity_units):
waoc = gwcs_4d_identity_units.world_axis_object_components
assert waoc[0:3] == [('celestial', 0, 'spherical.lon'),
('celestial', 1, 'spherical.lat'),
('spectral', 0, 'value')]
assert waoc[3][0:2] == ('temporal', 0)
first_two = [c[:2] for c in waoc]
last_one = [c[2] for c in waoc]
assert first_two == [('celestial', 0),
('celestial', 1),
('spectral', 0),
('temporal', 0)]
assert all([callable(l) for l in last_one])


def test_world_axis_object_classes_2d(gwcs_2d_spatial_shift):
Expand Down Expand Up @@ -521,3 +527,19 @@ def test_coordinate_frame_api():

pixel2 = wcs.invert(world)
assert u.allclose(pixel2, 0*u.pix)


def test_world_axis_object_components_units(gwcs_3d_identity_units):
from astropy.wcs.wcsapi.high_level_api import high_level_objects_to_values

wcs = gwcs_3d_identity_units
world = wcs.pixel_to_world(1, 1, 1)

values = high_level_objects_to_values(*world, low_level_wcs=wcs)

expected_values = [world[0].spherical.lon.to_value(wcs.output_frame.unit[0]),
world[0].spherical.lon.to_value(wcs.output_frame.unit[1]),
world[1].to_value(wcs.output_frame.unit[2])]

assert not any([isinstance(o, u.Quantity) for o in values])
np.testing.assert_allclose(values, expected_values)
68 changes: 48 additions & 20 deletions gwcs/tests/test_api_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ def test_ellipsis(gwcs_3d_galactic_spectral):

assert_equal(wcs.axis_correlation_matrix, [[True, False, True], [False, True, False], [True, False, True]])

assert wcs.world_axis_object_components == [('celestial', 1, 'spherical.lat'),
('spectral', 0, 'value'),
('celestial', 0, 'spherical.lon')]
first_two = [c[:2] for c in wcs.world_axis_object_components]
last_one = [c[2] for c in wcs.world_axis_object_components]
assert first_two == [('celestial', 1),
('spectral', 0),
('celestial', 0)]

assert all([callable(l) for l in last_one])

assert wcs.world_axis_object_classes['celestial'][0] is SkyCoord
assert wcs.world_axis_object_classes['celestial'][1] == ()
Expand Down Expand Up @@ -106,8 +110,12 @@ def test_spectral_slice(gwcs_3d_galactic_spectral):

assert_equal(wcs.axis_correlation_matrix, [[True, True], [True, True]])

assert wcs.world_axis_object_components == [('celestial', 1, 'spherical.lat'),
('celestial', 0, 'spherical.lon')]
first_two = [c[:2] for c in wcs.world_axis_object_components]
last_one = [c[2] for c in wcs.world_axis_object_components]
assert first_two == [('celestial', 1),
('celestial', 0)]

assert all([callable(l) for l in last_one])

assert wcs.world_axis_object_classes['celestial'][0] is SkyCoord
assert wcs.world_axis_object_classes['celestial'][1] == ()
Expand Down Expand Up @@ -166,9 +174,13 @@ def test_spectral_range(gwcs_3d_galactic_spectral):

assert_equal(wcs.axis_correlation_matrix, [[True, False, True], [False, True, False], [True, False, True]])

assert wcs.world_axis_object_components == [('celestial', 1, 'spherical.lat'),
('spectral', 0, 'value'),
('celestial', 0, 'spherical.lon')]
first_two = [c[:2] for c in wcs.world_axis_object_components]
last_one = [c[2] for c in wcs.world_axis_object_components]
assert first_two == [('celestial', 1),
('spectral', 0),
('celestial', 0)]

assert all([callable(l) for l in last_one])

assert wcs.world_axis_object_classes['celestial'][0] is SkyCoord
assert wcs.world_axis_object_classes['celestial'][1] == ()
Expand Down Expand Up @@ -230,9 +242,13 @@ def test_celestial_slice(gwcs_3d_galactic_spectral):

assert_equal(wcs.axis_correlation_matrix, [[False, True], [True, False], [False, True]])

assert wcs.world_axis_object_components == [('celestial', 1, 'spherical.lat'),
('spectral', 0, 'value'),
('celestial', 0, 'spherical.lon')]
first_two = [c[:2] for c in wcs.world_axis_object_components]
last_one = [c[2] for c in wcs.world_axis_object_components]
assert first_two == [('celestial', 1),
('spectral', 0),
('celestial', 0)]

assert all([callable(l) for l in last_one])

assert wcs.world_axis_object_classes['celestial'][0] is SkyCoord
assert wcs.world_axis_object_classes['celestial'][1] == ()
Expand Down Expand Up @@ -295,9 +311,13 @@ def test_celestial_range(gwcs_3d_galactic_spectral):

assert_equal(wcs.axis_correlation_matrix, [[True, False, True], [False, True, False], [True, False, True]])

assert wcs.world_axis_object_components == [('celestial', 1, 'spherical.lat'),
('spectral', 0, 'value'),
('celestial', 0, 'spherical.lon')]
first_two = [c[:2] for c in wcs.world_axis_object_components]
last_one = [c[2] for c in wcs.world_axis_object_components]
assert first_two == [('celestial', 1),
('spectral', 0),
('celestial', 0)]

assert all([callable(l) for l in last_one])

assert wcs.world_axis_object_classes['celestial'][0] is SkyCoord
assert wcs.world_axis_object_classes['celestial'][1] == ()
Expand Down Expand Up @@ -363,9 +383,13 @@ def test_no_array_shape(gwcs_3d_galactic_spectral):

assert_equal(wcs.axis_correlation_matrix, [[True, False, True], [False, True, False], [True, False, True]])

assert wcs.world_axis_object_components == [('celestial', 1, 'spherical.lat'),
('spectral', 0, 'value'),
('celestial', 0, 'spherical.lon')]
first_two = [c[:2] for c in wcs.world_axis_object_components]
last_one = [c[2] for c in wcs.world_axis_object_components]
assert first_two == [('celestial', 1),
('spectral', 0),
('celestial', 0)]

assert all([callable(l) for l in last_one])

assert wcs.world_axis_object_classes['celestial'][0] is SkyCoord
assert wcs.world_axis_object_classes['celestial'][1] == ()
Expand Down Expand Up @@ -431,9 +455,13 @@ def test_ellipsis_none_types(gwcs_3d_galactic_spectral):
assert_equal(wcs.axis_correlation_matrix, [[True, False, True],
[False, True, False], [True, False, True]])

assert wcs.world_axis_object_components == [('celestial', 1, 'spherical.lat'),
('spectral', 0, 'value'),
('celestial', 0, 'spherical.lon')]
first_two = [c[:2] for c in wcs.world_axis_object_components]
last_one = [c[2] for c in wcs.world_axis_object_components]
assert first_two == [('celestial', 1),
('spectral', 0),
('celestial', 0)]

assert all([callable(l) for l in last_one])

assert wcs.world_axis_object_classes['celestial'][0] is SkyCoord
assert wcs.world_axis_object_classes['celestial'][1] == ()
Expand Down

0 comments on commit a5d7642

Please sign in to comment.