8000 JP-4024: Set negative NIRSpec flat fields to NaN and DNU by hayescr · Pull Request #9522 · spacetelescope/jwst · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

JP-4024: Set negative NIRSpec flat fields to NaN and DNU #9522

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

Merged
merged 7 commits into from
Jun 13, 2025
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/9522.flatfield.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Set pixels with negative flat fields to NaN and flag as them DNU for NIRSpec data.
28 changes: 16 additions & 12 deletions jwst/flatfield/flat_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
HORIZONTAL = 1
VERTICAL = 2

BADFLAT = (
dqflags.pixel["NO_FLAT_FIELD"] | dqflags.pixel["DO_NOT_USE"] | dqflags.pixel["UNRELIABLE_FLAT"]
)


def do_correction(
input_model,
Expand Down Expand Up @@ -853,9 +857,10 @@
# Find all pixels in the flat that have a DQ value of DO_NOT_USE
flat_bad = np.bitwise_and(f_flat_dq, dqflags.pixel["DO_NOT_USE"])

# Reset the flat value of all bad pixels to 1.0, so that no
# correction is made
f_flat[np.where(flat_bad)] = 1.0
# Set the flat value of all bad pixels to nan. F-flats provide
# flux calibration scaling, so there is no safe default value for bad
# pixels.
f_flat[np.where(flat_bad)] = np.nan
return f_flat, f_flat_dq, f_flat_err


Expand Down Expand Up @@ -1070,9 +1075,6 @@
The 2D DQ array resulting from combining the input DQ arrays via
bitwise OR.
"""
badflat = dqflags.pixel["NO_FLAT_FIELD"] | dqflags.pixel["DO_NOT_USE"]
badflat = badflat | dqflags.pixel["UNRELIABLE_FLAT"]

dq_list = []
if f_flat_dq is not None:
dq_list.append(f_flat_dq)
Expand All @@ -1086,7 +1088,7 @@
# dq array with all BADFLAT bits set
flat_dq = np.zeros(default_shape, dtype=np.uint32)
if n_dq == 0:
flat_dq = np.bitwise_or(flat_dq, badflat)
flat_dq = np.bitwise_or(flat_dq, BADFLAT)

Check warning on line 1091 in jwst/flatfield/flat_field.py

View check run for this annotation

Codecov / codecov/patch

jwst/flatfield/flat_field.py#L1091

Added line #L1091 was not covered by tests
else:
for dq_component in dq_list:
flat_dq = np.bitwise_or(flat_dq, dq_component)
Expand All @@ -1098,7 +1100,7 @@
# Flag DO_NOT_USE, NO_FLAT_FIELD and UNRELIABLE_FLAT where some or all the
# flats had DO_NOT_USE set
iloc = np.where(np.bitwise_and(flat_dq, dqflags.pixel["DO_NOT_USE"]))
flat_dq[iloc] = np.bitwise_or(flat_dq[iloc], badflat)
flat_dq[iloc] = np.bitwise_or(flat_dq[iloc], BADFLAT)

return flat_dq

Expand Down Expand Up @@ -1720,12 +1722,12 @@
None,
None,
)
flat_2d[nan_flag] = 1.0
mask = flat_2d <= 0.0
nbad = mask.sum(dtype=np.intp)
if nbad > 0:
log.debug("%d flat-field values <= 0", nbad)
flat_2d[mask] = 1.0
flat_2d[mask] = np.nan
flat_dq_2d[mask] = np.bitwise_or(flat_dq_2d[mask], BADFLAT)

Check warning on line 1730 in jwst/flatfield/flat_field.py

View check run for this annotation

Codecov / codecov/patch

jwst/flatfield/flat_field.py#L1729-L1730

Added lines #L1729 - L1730 were not covered by tests
del mask

flat[ystart:ystop, xstart:xstop][good_flag] = flat_2d[good_flag]
Expand Down Expand Up @@ -1859,7 +1861,8 @@
nbad = mask.sum(dtype=np.intp)
if nbad > 0:
log.debug("%d flat-field values <= 0", nbad)
flat_2d[mask] = 1.0
flat_2d[mask] = np.nan
flat_dq_2d[mask] = np.bitwise_or(flat_dq_2d[mask], BADFLAT)

Check warning on line 1865 in jwst/flatfield/flat_field.py

View check run for this annotation

Codecov / codecov/patch

jwst/flatfield/flat_field.py#L1864-L1865

Added lines #L1864 - L1865 were not covered by tests
del mask

flat_dq_2d = flat_dq_2d.astype(output_model.dq.dtype)
Expand Down Expand Up @@ -1984,7 +1987,8 @@
nbad = mask.sum(dtype=np.intp)
if nbad > 0:
log.debug("%d flat-field values <= 0", nbad)
flat_2d[mask] = 1.0
flat_2d[mask] = np.nan
flat_dq_2d[mask] = np.bitwise_or(flat_dq_2d[mask], BADFLAT)
del mask

# Put the computed flat, flat_dq and flat_err into a datamodel
Expand Down
0