Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fillval option to gwcs_blot #291

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/291.general.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added fillval option to ``gwcs_blot`` utility.
11 changes: 7 additions & 4 deletions src/stcal/outlier_detection/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def flag_resampled_crs(
return mask1_smoothed & mask2


def gwcs_blot(median_data, median_wcs, blot_shape, blot_wcs, pix_ratio):
def gwcs_blot(median_data, median_wcs, blot_shape, blot_wcs, pix_ratio, fillval=0.0):
"""
Resample the median data to recreate an input image based on
the blot wcs.
Expand All @@ -236,7 +236,7 @@ def gwcs_blot(median_data, median_wcs, blot_shape, blot_wcs, pix_ratio):
median_wcs : gwcs.wcs.WCS
The wcs for the median data.

blot_shape : list of int
blot_shape : tuple of int
The target blot data shape.

blot_wcs : gwcs.wcs.WCS
Expand All @@ -245,6 +245,9 @@ def gwcs_blot(median_data, median_wcs, blot_shape, blot_wcs, pix_ratio):
pix_ratio : float
Pixel ratio.

fillval : float, optional
Fill value for missing data.

Returns
-------
blotted : numpy.ndarray
Expand All @@ -259,15 +262,15 @@ def gwcs_blot(median_data, median_wcs, blot_shape, blot_wcs, pix_ratio):
log.debug("Sci shape: {}".format(blot_shape))
log.info('Blotting {} <-- {}'.format(blot_shape, median_data.shape))

outsci = np.zeros(blot_shape, dtype=np.float32)
outsci = np.full(blot_shape, fillval, dtype=np.float32)

# Currently tblot cannot handle nans in the pixmap, so we need to give some
# other value. -1 is not optimal and may have side effects. But this is
# what we've been doing up until now, so more investigation is needed
# before a change is made. Preferably, fix tblot in drizzle.
pixmap[np.isnan(pixmap)] = -1
tblot(median_data, pixmap, outsci, scale=pix_ratio, kscale=1.0,
interp='linear', exptime=1.0, misval=0.0, sinscl=1.0)
interp='linear', exptime=1.0, misval=fillval, sinscl=1.0)

return outsci

Expand Down
24 changes: 24 additions & 0 deletions tests/outlier_detection/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ def test_gwcs_blot():
np.testing.assert_equal(blotted, median_data[:blot_shape[0], :blot_shape[1]])


@pytest.mark.parametrize('fillval', [0.0, np.nan])
def test_gwcs_blot_fillval(fillval):
# set up a very simple wcs that scales by 1x
output_frame = gwcs.Frame2D(name="world")
forward_transform = models.Scale(1) & models.Scale(1)

median_shape = (10, 10)
median_data = np.arange(100, dtype=np.float32).reshape((10, 10))
median_wcs = gwcs.WCS(forward_transform, output_frame=output_frame)
blot_shape = (20, 20)
blot_wcs = gwcs.WCS(forward_transform, output_frame=output_frame)
pix_ratio = 1.0

blotted = gwcs_blot(median_data, median_wcs, blot_shape, blot_wcs,
pix_ratio, fillval=fillval)

# since the blot data is larger and the wcs are equivalent the blot
# will contain the median data + some fill values
assert blotted.shape == blot_shape
np.testing.assert_equal(blotted[:median_shape[0], :median_shape[1]], median_data)
np.testing.assert_equal(blotted[median_shape[0]:, :], fillval)
np.testing.assert_equal(blotted[:, median_shape[1]:], fillval)


def test_calc_gwcs_pixmap():
# generate 2 wcses with different scales
output_frame = gwcs.Frame2D(name="world")
Expand Down
Loading