Skip to content

Commit

Permalink
Merge 6448e11 into 070360a
Browse files Browse the repository at this point in the history
  • Loading branch information
mwcraig committed Dec 12, 2019
2 parents 070360a + 6448e11 commit db8a912
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
12 changes: 11 additions & 1 deletion ccdproc/core.py
Expand Up @@ -1306,7 +1306,8 @@ def cosmicray_lacosmic(ccd, sigclip=4.5, sigfrac=0.3,
satlevel=65535.0, pssl=0.0, niter=4,
sepmed=True, cleantype='meanmask', fsmode='median',
psfmodel='gauss', psffwhm=2.5, psfsize=7,
psfk=None, psfbeta=4.765, verbose=False):
psfk=None, psfbeta=4.765, verbose=False,
gain_apply=True):
r"""
Identify cosmic rays through the L.A. Cosmic technique. The L.A. Cosmic
technique identifies cosmic rays by identifying pixels based on a variation
Expand Down Expand Up @@ -1342,6 +1343,10 @@ def cosmicray_lacosmic(ccd, sigclip=4.5, sigfrac=0.3,
Gain of the image (electrons / ADU). We always need to work in
electrons for cosmic ray detection. Default: 1.0
gain_apply : bool, optional
If ``True``, return gain-corrected data, otherwise do not gain-correct
the data. Default is ``True`` to preserve backwards compatibility.
readnoise : float, optional
Read noise of the image (electrons). Used to generate the noise model
of the image. Default: 6.5.
Expand Down Expand Up @@ -1472,6 +1477,8 @@ def cosmicray_lacosmic(ccd, sigclip=4.5, sigfrac=0.3,
psfsize=psfsize, psfk=psfk, psfbeta=psfbeta,
verbose=verbose)

if not gain_apply and gain != 1.0:
cleanarr = cleanarr / gain
return cleanarr, crmask

elif isinstance(ccd, CCDData):
Expand All @@ -1486,6 +1493,9 @@ def cosmicray_lacosmic(ccd, sigclip=4.5, sigfrac=0.3,

# create the new ccd data object
nccd = ccd.copy()
if not gain_apply and gain != 1.0:
cleanarr = cleanarr / gain

nccd.data = cleanarr
if nccd.mask is None:
nccd.mask = crmask
Expand Down
36 changes: 36 additions & 0 deletions ccdproc/tests/test_cosmicray.py
Expand Up @@ -64,6 +64,42 @@ def test_cosmicray_lacosmic_check_data():
cosmicray_lacosmic(10, noise)


@pytest.mark.parametrize('array_input', [True, False])
@pytest.mark.parametrize('gain_correct_data', [True, False])
def test_cosmicray_gain_correct(array_input, gain_correct_data):
# Add regression check for #705 and for the new gain_correct
# argument.
# The issue is that cosmicray_lacosmic gain-corrects the
# data and returns that gain corrected data. That is not the
# intent...
ccd_data = ccd_data_func(data_scale=DATA_SCALE)
threshold = 5
add_cosmicrays(ccd_data, DATA_SCALE, threshold, ncrays=NCRAYS)
noise = DATA_SCALE * np.ones_like(ccd_data.data)
ccd_data.uncertainty = noise
# This may need units at some point.
gain = 2.0
if array_input:
new_data, cr_mask = cosmicray_lacosmic(ccd_data.data,
gain=gain,
gain_apply=gain_correct_data)
else:
new_ccd = cosmicray_lacosmic(ccd_data,
gain=gain,
gain_apply=gain_correct_data)
new_data = new_ccd.data
cr_mask = new_ccd.mask
# Fill masked locations with 0 since there is no simple relationship
# between the original value and the corrected value.
orig_data = np.ma.array(ccd_data.data, mask=cr_mask).filled(0)
new_data = np.ma.array(new_data, mask=cr_mask).filled(0)
if gain_correct_data:
gain_for_test = gain
else:
gain_for_test = 1.0
np.testing.assert_allclose(gain_for_test * orig_data, new_data)


def test_cosmicray_median_check_data():
with pytest.raises(TypeError):
ndata, crarr = cosmicray_median(10, thresh=5, mbox=11,
Expand Down

0 comments on commit db8a912

Please sign in to comment.