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-2922: Minor fixes for MSA source names #8533

Merged
merged 4 commits into from
Jun 5, 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
9 changes: 8 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ assign_wcs

- Updated the routines that load NIRSpec MOS slit and source data from the MSA meta
data file to properly handle background and virtual slits, and assign appropriate
meta data to them for use downstream. [#8442]
meta data to them for use downstream. [#8442, #8533]

associations
------------
Expand Down Expand Up @@ -103,6 +103,13 @@ general

- Increase minimum required scipy. [#8441]

master_background_mos
---------------------

- Updated check for NIRSpec MOS background slits to use new naming convention:
``slit.source_name`` now contains the string "BKG" instead of
"background". [#8533]

outlier_detection
-----------------

Expand Down
8 changes: 7 additions & 1 deletion jwst/assign_wcs/nirspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,13 @@ def get_open_msa_slits(prog_id, msa_file, msa_metadata_id, dither_position,
(s['source_name'], s['alias'], s['stellarity'], s['ra'], s['dec'])
for s in msa_source if s['source_id'] == source_id][0]
except IndexError:
log.warning("Could not retrieve source info from MSA file")
source_name = f"{prog_id}_VRT{slitlet_id}"
source_alias = "VRT{}".format(slitlet_id)
stellarity = 0.0
source_ra = 0.0
source_dec = 0.0
log.warning(f"Could not retrieve source info from MSA file; "
f"assigning virtual source_name={source_name}")

if source_id < 0:
log.info(f'Slitlet {slitlet_id} contains virtual source, with source_id={source_id}')
Expand Down
30 changes: 30 additions & 0 deletions jwst/assign_wcs/tests/test_nirspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import functools
from math import cos, sin
import os.path
import shutil

import pytest
import numpy as np
Expand Down Expand Up @@ -355,6 +356,35 @@ def test_msa_configuration_multiple_returns():
_compare_slits(slitlet_info[1], ref_slit2)


def test_msa_missing_source(tmp_path):
"""
Test the get_open_msa_slits function with missing source information.
"""
# modify an existing MSA file to remove source info
msaconfl = get_file_path('msa_configuration.fits')
bad_confl = str(tmp_path / 'bad_msa_configuration.fits')
shutil.copy(msaconfl, bad_confl)

with fits.open(bad_confl) as msa_hdu_list:
source_table = table.Table(msa_hdu_list['SOURCE_INFO'].data)
source_table.remove_rows(slice(0, -1))
msa_hdu_list['SOURCE_INFO'] = fits.table_to_hdu(source_table)
msa_hdu_list[3].name = 'SOURCE_INFO'
msa_hdu_list.writeto(bad_confl, overwrite=True)

prog_id = '1234'
msa_meta_id = 12
dither_position = 1

slitlet_info = nirspec.get_open_msa_slits(
prog_id, bad_confl, msa_meta_id, dither_position, slit_y_range=[-.5, .5])

# MSA slit: virtual source name assigned
ref_slit = trmodels.Slit(55, 9376, 1, 251, 26, -5.6, 1.0, 4, 1, '1111x',
'1234_VRT55', 'VRT55', 0.0,
-0.31716078999999997, -0.18092266, 0.0, 0.0)
_compare_slits(slitlet_info[0], ref_slit)

open_shutters = [[24], [23, 24], [22, 23, 25, 27], [22, 23, 25, 27, 28]]
main_shutter = [24, 23, 25, 28]
result = ["x", "x1", "110x01", "110101x"]
Expand Down
2 changes: 1 addition & 1 deletion jwst/master_background/master_background_mos_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
# count how many are background vs source.
num_bkg = num_src = 0
for slit in data.slits:
if "background" in slit.source_name:
if nirspec_utils.is_background_msa_slit(slit):

Check warning on line 194 in jwst/master_background/master_background_mos_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/master_background/master_background_mos_step.py#L194

Added line #L194 was not covered by tests
num_bkg += 1
else:
num_src += 1
Expand Down
22 changes: 21 additions & 1 deletion jwst/master_background/nirspec_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
bkg_model.update(input_model)
slits = []
for slit in input_model.slits:
if "background" in slit.source_name:
if is_background_msa_slit(slit):

Check warning on line 104 in jwst/master_background/nirspec_utils.py

View check run for this annotation

Codecov / codecov/patch

jwst/master_background/nirspec_utils.py#L104

Added line #L104 was not covered by tests
log.info(f'Using background slitlet {slit.source_name}')
slits.append(slit)

Expand Down Expand Up @@ -242,3 +242,23 @@
input_model.data *= (pl_uniform / pl_point)

return input_model


def is_background_msa_slit(slit):
"""
Check if an MSA slitlet is a background source.

Parameters
----------
slit : `~jwst.datamodels.SlitModel`
The slit model to check.

Returns
-------
bool
True if the slit is background; False if it is not.
"""
if "BKG" in str(slit.source_name).upper():
return True
else:
return False
15 changes: 14 additions & 1 deletion jwst/master_background/tests/test_nirspec_corrections.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
Unit tests for master background NIRSpec corrections
"""
import numpy as np
import pytest

from stdatamodels.jwst import datamodels

from jwst.master_background.nirspec_utils import (
correct_nrs_ifu_bkg, correct_nrs_fs_bkg
correct_nrs_ifu_bkg, correct_nrs_fs_bkg, is_background_msa_slit
)


Expand Down Expand Up @@ -54,3 +55,15 @@ def test_fs_correction():
result = correct_nrs_fs_bkg(input, primary_slit=True)

assert np.allclose(corrected, result.data, rtol=1.e-7)


@pytest.mark.parametrize('name,status',
[('BKG101', True), ('bkg101', True),
('background_101', False), ('101', False),
(None, False)])
def test_is_background(name, status):
"""Test check for background slit."""
slit = datamodels.SlitModel()
if name is not None:
slit.source_name = name
assert is_background_msa_slit(slit) == status