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

JP-2013 NIRSpec leakcal and nodded exposures #7426

Merged
merged 8 commits into from
Jan 18, 2023
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
11 changes: 11 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ documentation

- Remove references to pub server [#7421]

calwebb_spec2
-------------

- Subtract leakcal image from science and backgrounds and then background subtract [#7426]

imprint
-------

- Add matching leakcal image and science/background image by using dither.position_number [#7426]


1.9.1 (2023-01-03)
==================

Expand Down
4 changes: 2 additions & 2 deletions docs/jwst/imprint/description.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ MOS and IFU exposures by the MSA structure. This is accomplished by
subtracting a dedicated exposure taken with all MSA shutters closed and the
IFU entrance aperture blocked.

The step has two input parameters: the target exposure and the imprint
exposure. These arguments can be provided as either a file name
The step has two input parameters: the target exposure and a list of imprint
exposures. These arguments can be provided as either a file name
or a JWST data model.

The SCI data array of the imprint exposure is subtracted from the SCI array
Expand Down
8 changes: 4 additions & 4 deletions docs/jwst/pipeline/calwebb_spec2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ TSO exposures. The instrument mode abbreviations used in the table are as follow
+==========================================================+=====+=====+=====+=====+=====+=====+======+======+========+=====+
| :ref:`assign_wcs <assign_wcs_step>` | |c| | |c| | |c| | |c| | |c| | |c| | |c| | |c| | |c| | |c| |
+----------------------------------------------------------+-----+-----+-----+-----+-----+-----+------+------+--------+-----+
| :ref:`background <background_step>` | |c| | |c| | |c| | |c| | | |c| | |c| | |c| | |c| | |
+----------------------------------------------------------+-----+-----+-----+-----+-----+-----+------+------+--------+-----+
| :ref:`imprint <imprint_step>` | | |c| | |c| | | | | | | | |
+----------------------------------------------------------+-----+-----+-----+-----+-----+-----+------+------+--------+-----+
| :ref:`background <background_step>` | |c| | |c| | |c| | |c| | | |c| | |c| | |c| | |c| | |
+----------------------------------------------------------+-----+-----+-----+-----+-----+-----+------+------+--------+-----+
| :ref:`msaflagopen <msaflagopen_step>` | | |c| | |c| | | | | | | | |
+----------------------------------------------------------+-----+-----+-----+-----+-----+-----+------+------+--------+-----+
| :ref:`extract_2d <extract_2d_step>`\ :sup:`1` | |c| | |c| | | | | | | |c| | |c| | |c| |
Expand Down Expand Up @@ -172,10 +172,10 @@ abbreviations used in the table are as follows:
+=======================================+============+==============+=================+==============+
| :ref:`assign_wcs <assign_wcs_step>` | ALL | ALL | ALL | ALL |
+---------------------------------------+------------+--------------+-----------------+--------------+
| :ref:`background <background_step>` | NONE | NONE | NONE | NONE |
+---------------------------------------+------------+--------------+-----------------+--------------+
| :ref:`imprint <imprint_step>` | NONE | IFU | NONE | NONE |
+---------------------------------------+------------+--------------+-----------------+--------------+
| :ref:`background <background_step>` | NONE | NONE | NONE | NONE |
+---------------------------------------+------------+--------------+-----------------+--------------+
| :ref:`msaflagopen <msaflagopen_step>` | MOS, IFU | MOS, IFU | MOS, IFU | MOS |
+---------------------------------------+------------+--------------+-----------------+--------------+
| :ref:`extract_2d <extract_2d_step>` | MOS, FS | MOS, FS | MOS, FS | MOS |
Expand Down
42 changes: 34 additions & 8 deletions jwst/imprint/imprint_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,43 @@ class ImprintStep(Step):

def process(self, input, imprint):

# Open the input data model
with datamodels.open(input) as input_model:

# Open the imprint exposure data model
imprint_model = datamodels.open(imprint)

# Do the imprint exposure subtraction
# subtract leakcal (imprint) image
# If there is only one imprint image is in the association use for all the data.
# If there is more than one imprint image in the association then select
# the imprint image to subtract based on position number (DataModel.meta.dither.position_number).

# Open the input data model and get position number of image
input_model = datamodels.open(input)
pos_no = input_model.meta.dither.position_number

# find imprint that goes with input image - if there is only 1 imprint image - use it.
num_imprint = len(imprint)
match = None
if num_imprint == 1:
match = 0
else:
i = 0
while match is None and i < num_imprint:
# open imprint images and read in pos_no to find match
imprint_model = datamodels.open(imprint[i])
imprint_pos_no = imprint_model.meta.dither.position_number
imprint_model.close()

if pos_no == imprint_pos_no:
match = i
i = i + 1

# Initialize result to be input (just in case no matching imprint image was found)
result = input_model.copy()

if match is not None:
imprint_model = datamodels.open(imprint[match])
result = subtract_images.subtract(input_model, imprint_model)

# Update the step status and close the imprint model
result.meta.cal_step.imprint = 'COMPLETE'
input_model.close()
imprint_model.close()

else:
self.log.info(f'No imprint image was found for {input}')
return result
4 changes: 3 additions & 1 deletion jwst/imprint/tests/test_imprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ def test_step(make_imagemodel):
"""Assert that the results should be all zeros.
"""
im = make_imagemodel(10, 10)
result = ImprintStep.call(im, im)
imprint = []
imprint.append(im)
result = ImprintStep.call(im, imprint)

assert result.meta.cal_step.imprint == 'COMPLETE'
assert result.data.sum() == 0
Expand Down
27 changes: 21 additions & 6 deletions jwst/pipeline/calwebb_spec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ def process_exposure_product(

# Decide on what steps can actually be accomplished based on the
# provided input.
self._step_verification(exp_type, members_by_type, multi_int)

self._step_verification(exp_type, science, members_by_type, multi_int)

# Start processing the individual steps.
# `assign_wcs` is the critical step. Without it, processing
Expand Down Expand Up @@ -237,8 +238,21 @@ def process_exposure_product(
raise RuntimeError('Cannot determine WCS.')

# Steps whose order is the same for all types of input.
calibrated = self.bkg_subtract(calibrated, members_by_type['background'])

# Leakcal subtraction (imprint) occurs before background subtraction on a per-exposure basis.
# If there is only one `imprint` member, this imprint exposure is subtracted from all the
# science and background exposures. Otherwise, there will be as many `imprint` members as
# there are science plus background members.

calibrated = self.imprint_subtract(calibrated, members_by_type['imprint'])

# for each background image subtract an associated leak cal
for i, bkg_file in enumerate(members_by_type['background']):
hbushouse marked this conversation as resolved.
Show resolved Hide resolved
bkg_imprint_sub = self.imprint_subtract(bkg_file, members_by_type['imprint'])
members_by_type['background'][i] = bkg_imprint_sub

calibrated = self.bkg_subtract(calibrated, members_by_type['background'])

calibrated = self.msa_flagging(calibrated)

# The order of the next few steps is tricky, depending on mode:
Expand Down Expand Up @@ -320,7 +334,7 @@ def process_exposure_product(

return calibrated

def _step_verification(self, exp_type, members_by_type, multi_int):
def _step_verification(self, exp_type, input, members_by_type, multi_int):
"""Verify whether requested steps can operate on the given data

Though ideally this would all be controlled through the pipeline
Expand Down Expand Up @@ -350,14 +364,15 @@ def _step_verification(self, exp_type, members_by_type, multi_int):
self.log.debug('Science data does not allow direct background subtraction. Skipping "bkg_subtract".')
self.bkg_subtract.skip = True

# Check for imprint subtraction
# Check for imprint subtraction. If we have a background then we could have an imprint image associated with the
# background.
imprint = members_by_type['imprint']
if not self.imprint_subtract.skip:
if len(imprint) > 0 and (exp_type in ['NRS_MSASPEC', 'NRS_IFU']
or is_nrs_ifu_flatlamp(input)):
if len(imprint) > 1:
if len(imprint) > 1 and (exp_type in ['NRS_MSASPEC'] or is_nrs_ifu_flatlamp(input)):
self.log.warning('Wrong number of imprint members')
members_by_type['imprint'] = imprint[0]
members_by_type['imprint'] = imprint[0]
else:
self.log.debug('Science data does not allow imprint processing. Skipping "imprint_subtraction".')
self.imprint_subtract.skip = True
Expand Down