Skip to content

Commit 81f38ef

Browse files
authored
Ensure at least one pixel in header (#586)
2 parents f628605 + d85c45f commit 81f38ef

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

Diff for: scopesim/optics/image_plane_utils.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -202,24 +202,31 @@ def create_wcs_from_points(points: np.ndarray,
202202
f"pix, got '{naxis.unit}' instead.")
203203
naxis = naxis.value
204204

205+
if (naxis == 0).any():
206+
# Ensure at least one pixel in each axis.
207+
logger.debug("NAXISn == 0, using minimum of 1.")
208+
naxis[naxis == 0] = 1
209+
205210
crpix = (naxis + 1) / 2
206211
crval = (points.min(axis=0) + points.max(axis=0)) / 2
207212

208-
ctype = "LINEAR" if wcs_suffix in "DX" else "RA---TAN"
213+
# Cannot do `in "DX"` here because that would also match the empty string.
214+
linsuff = {"D", "X"}
215+
lintype = ["LINEAR", "LINEAR"]
216+
# skytype = ["RA---TAN", "DEC--TAN"] # ScopeSim can't handle the truth yet
217+
skytype = ["LINEAR", "LINEAR"]
218+
ctype = lintype if wcs_suffix in linsuff else skytype
209219

210220
if isinstance(points, u.Quantity):
211221
cunit = points.unit
212222
else:
213-
if wcs_suffix == "D":
214-
cunit = "mm"
215-
else:
216-
cunit = "deg"
223+
cunit = "mm" if wcs_suffix == "D" else "deg"
217224

218225
if isinstance(pixel_scale, u.Quantity):
219226
pixel_scale = pixel_scale.value
220227

221228
new_wcs = WCS(key=wcs_suffix)
222-
new_wcs.wcs.ctype = 2 * [ctype]
229+
new_wcs.wcs.ctype = ctype
223230
new_wcs.wcs.cunit = 2 * [cunit]
224231
new_wcs.wcs.cdelt = np.array(2 * [pixel_scale])
225232
new_wcs.wcs.crval = crval
@@ -916,6 +923,8 @@ def calc_footprint(header, wcs_suffix="", new_unit: str = None):
916923
xy0 = np.array([[0, 0], [0, y_ext], [x_ext, y_ext], [x_ext, 0]])
917924
xy1 = coords.wcs_pix2world(xy0, 0)
918925

926+
# FIXME: Catch case of unit mismatch in CUNIT1 and CUNIT2, there should be
927+
# a function for this somewhere but I forgot.
919928
if (cunit := coords.wcs.cunit[0]) == "deg":
920929
xy1 = _fix_360(xy1)
921930

Diff for: scopesim/tests/tests_optics/test_ImagePlaneUtils.py

+23
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,26 @@ def test_wcs_roundtrip(self):
347347
# Compare header representations because of missing NAXIS info in new
348348
assert new_wcs.to_header() == sky_wcs.to_header()
349349
assert all(nax == (hdu.header["NAXIS1"], hdu.header["NAXIS2"]))
350+
351+
352+
class TestWCSFromMinimalPoints:
353+
def test_all_zero_points(self):
354+
pnts = np.array([[0, 0]])
355+
wcs, naxis = imp_utils.create_wcs_from_points(pnts, 5)
356+
np.testing.assert_array_equal(naxis, [1, 1])
357+
np.testing.assert_array_equal(wcs.wcs.crpix, [1, 1])
358+
np.testing.assert_array_equal(wcs.wcs.crval, [0, 0])
359+
360+
def test_all_within_one_pixel(self):
361+
pnts = np.array([[-1, -1], [-1, 1], [1, -1], [1, 1]])
362+
wcs, naxis = imp_utils.create_wcs_from_points(pnts, 5)
363+
np.testing.assert_array_equal(naxis, [1, 1])
364+
np.testing.assert_array_equal(wcs.wcs.crpix, [1, 1])
365+
np.testing.assert_array_equal(wcs.wcs.crval, [0, 0])
366+
367+
def test_all_within_one_pixel_along_one_axis(self):
368+
pnts = np.array([[-3, 0], [-2, 0], [0, 0], [2, 0]])
369+
wcs, naxis = imp_utils.create_wcs_from_points(pnts, 1)
370+
np.testing.assert_array_equal(naxis, [5, 1])
371+
np.testing.assert_array_equal(wcs.wcs.crpix, [3, 1])
372+
np.testing.assert_array_equal(wcs.wcs.crval, [-0.5, 0])

0 commit comments

Comments
 (0)