Skip to content

Commit

Permalink
Merge pull request #311 from mperrin/fix-302
Browse files Browse the repository at this point in the history
Fix issue 302, PSF normalization as expected by GriddedPSFModel
  • Loading branch information
mperrin committed Jul 23, 2019
2 parents d24e75d + 4aa908c commit 036dfc6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
7 changes: 7 additions & 0 deletions webbpsf/gridded_library.py
Expand Up @@ -264,6 +264,13 @@ def create_grid(self):
# Add PSF to 5D array
psf_arr[i, :, :] = psf_conv

# Normalize the output PSFs as expected by photutils.GriddedPSFModel:
# PSFs should be in surface brightness units, independent of oversampling.
# This is diferent than webbpsf/poppy's default in which PSFs usually sum to 1
# so the counts/pixel varies based on sampling. Apply the necessary conversion
# factor here. See issue #302.
psf_arr *= self.oversample**2

# Define meta data
meta = OrderedDict()

Expand Down
15 changes: 9 additions & 6 deletions webbpsf/tests/test_psfgrid.py
Expand Up @@ -40,10 +40,11 @@ def test_compare_to_calc_psf_oversampled():
calcpsf = fgs.calc_psf(oversample=oversample, fov_pixels=fov_pixels)["OVERDIST"].data
kernel = astropy.convolution.Box2DKernel(width=oversample)
convpsf = astropy.convolution.convolve(calcpsf, kernel)
scalefactor = oversample**2 # normalization as used internally in GriddedPSFModel; see #302

# Compare to make sure they are in fact the same PSF
assert gridpsf.shape == calcpsf.shape
assert np.array_equal(gridpsf, convpsf)
assert gridpsf.shape == calcpsf.shape, "Shape mismatch"
assert np.allclose(gridpsf, convpsf*scalefactor), "Data values not as expected"


def test_compare_to_calc_psf_detsampled():
Expand Down Expand Up @@ -80,7 +81,7 @@ def test_compare_to_calc_psf_detsampled():
assert np.array_equal(gridpsf, convpsf)


def test_all():
def test_all_detectors():
"""
Check that running all the detectors works (ie setting all_detectors=True). In
particular for NIRCam, test that the detectors pulled are correct
Expand Down Expand Up @@ -132,10 +133,12 @@ def test_one_psf():
calc = nis.calc_psf(add_distortion=True, oversample=2, fov_pixels=11)
kernel = astropy.convolution.Box2DKernel(width=oversample)
convpsf = astropy.convolution.convolve(calc["OVERDIST"].data, kernel)
scalefactor = oversample**2 # normalization as used internally in GriddedPSFModel; see #302


assert grid1.meta["grid_xypos"] == [(1023, 1023)] # the default is the center of the NIS aperture
assert grid2.meta["grid_xypos"] == [(10, 0)] # it's in (x,y)
assert np.array_equal(convpsf, grid2.data[0, :, :])
assert grid1.meta["grid_xypos"] == [(1023, 1023)], "Center position not as expected" # the default is the center of the NIS aperture
assert grid2.meta["grid_xypos"] == [(10, 0)], "Corner position not as expected" # it's in (x,y)
assert np.allclose(convpsf*scalefactor, grid2.data[0, :, :]), "PSF data values not as expected"


def test_nircam_errors():
Expand Down

0 comments on commit 036dfc6

Please sign in to comment.