Skip to content

Commit af2ff57

Browse files
Bugfix for WCS handing non-coordinate inputs/outputs correctly (#706)
2 parents b90a1ea + 8440310 commit af2ff57

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

changes/706.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Bugfix for supporting non-coordinate inputs to transforms supporting quantities.

gwcs/coordinate_frames/_base.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import abc
2+
from itertools import zip_longest
23

34
import numpy as np
45
from astropy import units as u
@@ -136,8 +137,11 @@ def add_units(
136137
return u.Quantity(arrays, self.unit[0])
137138

138139
return tuple(
139-
u.Quantity(array, unit=unit)
140-
for array, unit in zip(arrays, self.unit, strict=True)
140+
array if unit is None else u.Quantity(array, unit=unit)
141+
# zip_longest is used here to support "non-coordinate" inputs/outputs
142+
# This implicitly assumes that the "non-coordinate" inputs/outputs
143+
# are tacked onto the end of the tuple of "coordinate" inputs/outputs.
144+
for array, unit in zip_longest(arrays, self.unit)
141145
)
142146

143147
def remove_units(
@@ -151,5 +155,8 @@ def remove_units(
151155

152156
return tuple(
153157
array.to_value(unit) if isinstance(array, u.Quantity) else array
154-
for array, unit in zip(arrays, self.unit, strict=True)
158+
# zip_longest is used here to support "non-coordinate" inputs/outputs
159+
# This implicitly assumes that the "non-coordinate" inputs/outputs
160+
# are tacked onto the end of the tuple of "coordinate" inputs/outputs.
161+
for array, unit in zip_longest(arrays, self.unit)
155162
)

gwcs/tests/test_wcs.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,3 +1925,39 @@ def test_fitswcs_transform(lon, lat):
19251925
assert_allclose(fwcs(0, 0), (lon, lat), atol=1e-14)
19261926
assert_allclose(fwcs(0, lat)[0], lon + 180, atol=1e-14)
19271927
assert_allclose(fwcs.inverse(lon, lat), (0, 0), atol=1e-14)
1928+
1929+
1930+
def test_non_coordinate_input():
1931+
"""Test that non-coordinate inputs are handled correctly."""
1932+
gw = wcs.WCS(
1933+
[
1934+
(
1935+
cf.Frame2D(
1936+
name="detector",
1937+
axes_names=("x", "y"),
1938+
unit=(u.deg, u.deg),
1939+
axes_order=(0, 1),
1940+
),
1941+
models.Identity(3),
1942+
),
1943+
(
1944+
cf.CompositeFrame(
1945+
[
1946+
cf.CelestialFrame(
1947+
name="icrs", axes_order=(0, 1), reference_frame=coord.ICRS()
1948+
),
1949+
cf.SpectralFrame(
1950+
name="spectral",
1951+
axes_order=(2,),
1952+
unit=(u.micron,),
1953+
axes_names=("wavelength",),
1954+
),
1955+
],
1956+
name="world",
1957+
),
1958+
None,
1959+
),
1960+
]
1961+
)
1962+
assert gw(1, 2, 3) == (1, 2, 3)
1963+
assert gw(1 * u.deg, 2 * u.deg, 3) == (1 * u.deg, 2 * u.deg, 3 * u.micron)

0 commit comments

Comments
 (0)