Skip to content

Commit

Permalink
Fix test for custom WCS for imaging
Browse files Browse the repository at this point in the history
  • Loading branch information
mcara committed Dec 18, 2024
1 parent 5a914c6 commit 0144479
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions jwst/resample/tests/test_resample_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,15 +881,17 @@ def test_custom_refwcs_resample_imaging(nircam_rate, output_shape2, match,
rng = np.random.default_rng(seed=77)
im.data[:, :] = rng.random(im.data.shape)

crpix = (600, 550)
crval = (22.04, 11.98)
if output_shape2 is None:
crpix = None
else:
crpix = (output_shape2[-1] // 2, output_shape2[-2] // 2)
crval = tuple(np.mean(im.meta.wcs.footprint(), axis=0))
rotation = 15
ratio = 0.7

# first pass - create a reference output WCS:
result = ResampleStep.call(
im,
output_shape=(1205, 1100),
output_shape=output_shape2,
crpix=crpix,
crval=crval,
rotation=rotation,
Expand All @@ -898,11 +900,15 @@ def test_custom_refwcs_resample_imaging(nircam_rate, output_shape2, match,

# make sure results are nontrivial
data1 = result.data
weight1 = result.wht

assert not np.all(np.isnan(data1))

if crpix is not None:
assert np.allclose(result.meta.wcs(*crpix), crval, rtol=1e-12, atol=0)

refwcs = str(tmp_path / "resample_refwcs.asdf")
result.meta.wcs.bounding_box = [(-0.5, 1204.5), (-0.5, 1099.5)]
asdf.AsdfFile({"wcs": result.meta.wcs}).write_to(refwcs)
asdf.AsdfFile({"wcs": result.meta.wcs, "array_shape": data1.shape}).write_to(refwcs)

result = ResampleStep.call(
im,
Expand All @@ -911,6 +917,7 @@ def test_custom_refwcs_resample_imaging(nircam_rate, output_shape2, match,
)

data2 = result.data
weight2 = result.wht
assert not np.all(np.isnan(data2))

if output_shape2 is not None:
Expand All @@ -919,17 +926,21 @@ def test_custom_refwcs_resample_imaging(nircam_rate, output_shape2, match,
if match:
# test output image shape
assert data1.shape == data2.shape
assert np.allclose(data1, data2, equal_nan=True)
assert np.allclose(data1, data2, equal_nan=True, rtol=1.0e-7, atol=1e-7)

# make sure pixel values are similar, accounting for scale factor
# (assuming inputs are in surface brightness units)
iscale = np.sqrt(im.meta.photometry.pixelarea_steradians
/ compute_image_pixel_area(im.meta.wcs))
iscale2 = (
im.meta.photometry.pixelarea_steradians /
compute_image_pixel_area(im.meta.wcs)
)

input_mean = np.nanmean(im.data)
output_mean_1 = np.nanmean(data1)
output_mean_2 = np.nanmean(data2)
assert np.isclose(input_mean * iscale**2, output_mean_1, atol=1e-4)
assert np.isclose(input_mean * iscale**2, output_mean_2, atol=1e-4)
total_weight = np.sum(weight1)
output_mean_1 = np.nansum(data1 * weight1) / total_weight
output_mean_2 = np.nansum(data2 * weight2) / total_weight
assert np.isclose(input_mean * iscale2, output_mean_1)
assert np.isclose(input_mean * iscale2, output_mean_2)

im.close()
result.close()
Expand Down

0 comments on commit 0144479

Please sign in to comment.