Skip to content

Commit d7e815f

Browse files
JP-4075: Raise error for no slits selected (#9794)
1 parent 5fa1452 commit d7e815f

File tree

5 files changed

+46
-17
lines changed

5 files changed

+46
-17
lines changed

changes/9794.extract_2d.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
If the ``slit_names`` or ``source_ids`` are specified, but none of the slits are valid, a ``NoDataOnDetectorError`` exception is raised.

docs/jwst/extract_2d/main.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ modes. For NIRSpec observations there is one applicable argument:
306306
of None will cause all known slits for the instrument to be extracted.
307307

308308
``slit_names`` and ``source_ids`` can be used at the same time, duplicates will be filtered out.
309+
If either argument is specified, but no valid slits are identified, an error will be
310+
raised and the step will exit.
309311

310312
There are several arguments available for Wide-Field Slitless Spectroscopy (WFSS) and
311313
Time-Series (TSO) grism spectroscopy:

jwst/extract_2d/extract_2d_step.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,16 @@ def process(self, input_model):
3939
output_model : DataModel
4040
The resulting DataModel of the extract_2d step
4141
"""
42-
reference_file_names = {}
43-
if input_model.meta.exposure.type in extract_2d.slitless_modes:
44-
# The wavelengthrange file is used only by the WFSS modes.
45-
# If retrieved by a Nirspec mode, it would override the name of
46-
# the file in meta.ref_file if a custom file was used.
47-
for reftype in self.reference_file_types:
48-
reffile = self.get_reference_file(input_model, reftype)
49-
reference_file_names[reftype] = reffile if reffile else ""
5042
with datamodels.open(input_model) as dm:
43+
reference_file_names = {}
44+
if dm.meta.exposure.type in extract_2d.slitless_modes:
45+
# The wavelengthrange file is used only by the WFSS modes.
46+
# If retrieved by a Nirspec mode, it would override the name of
47+
# the file in meta.ref_file if a custom file was used.
48+
for reftype in self.reference_file_types:
49+
reffile = self.get_reference_file(input_model, reftype)
50+
reference_file_names[reftype] = reffile if reffile else ""
51+
5152
output_model = extract_2d.extract2d(
5253
dm,
5354
self.slit_names,

jwst/extract_2d/nirspec.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ def nrs_extract2d(input_model, slit_names=None, source_ids=None):
6565
open_slits = input_model.meta.wcs.get_transform("gwa", "slit_frame").slits
6666

6767
# Select the slits to process
68-
open_slits = select_slits(open_slits, slit_names, source_ids)
68+
if slit_names is not None or source_ids is not None:
69+
open_slits = select_slits(open_slits, slit_names, source_ids)
6970

7071
# NIRSpec BRIGHTOBJ (S1600A1 TSO) mode
7172
if exp_type == "NRS_BRIGHTOBJ":
@@ -146,6 +147,11 @@ def select_slits(open_slits, slit_names, source_ids):
146147
-------
147148
selected_open_slits : list
148149
List of slits selected by slit_name or source_id
150+
151+
Raises
152+
------
153+
`~jwst.assign_wcs.util.NoDataOnDetectorError`
154+
If no valid slits are selected.
149155
"""
150156
open_slit_names = [str(x.name) for x in open_slits]
151157
open_slit_source_ids = [str(x.source_id) for x in open_slits]
@@ -156,7 +162,7 @@ def select_slits(open_slits, slit_names, source_ids):
156162
if this_slit in open_slit_names:
157163
matched_slits.append(this_slit)
158164
else:
159-
log.warn(f"Slit {this_slit} is not in the list of open slits.")
165+
log.warning(f"Slit {this_slit} is not in the list of open slits.")
160166
for sub in open_slits:
161167
if str(sub.name) in matched_slits:
162168
selected_open_slits.append(sub)
@@ -166,7 +172,7 @@ def select_slits(open_slits, slit_names, source_ids):
166172
if this_id in open_slit_source_ids:
167173
matched_sources.append(this_id)
168174
else:
169-
log.warn(f"Source id {this_id} is not in the list of open slits.")
175+
log.warning(f"Source id {this_id} is not in the list of open slits.")
170176
for sub in open_slits:
171177
if str(sub.source_id) in matched_sources:
172178
if sub not in selected_open_slits:
@@ -179,8 +185,9 @@ def select_slits(open_slits, slit_names, source_ids):
179185
log.info(f"Name: {this_slit.name}, source_id: {this_slit.source_id}")
180186
return selected_open_slits
181187
else:
182-
log.info("All slits selected")
183-
return open_slits
188+
log_message = "No valid slits selected."
189+
log.critical(log_message)
190+
raise util.NoDataOnDetectorError(log_message)
184191

185192

186193
def process_slit(input_model, slit):
@@ -363,7 +370,7 @@ class DitherMetadataError(Exception):
363370
Handle DitherMetadataError exception.
364371
365372
This exception is raised if a Slit object doesn't have the required
366-
dither attribute, or the x_ and y_ offsets aren't numeric.
373+
dither attribute, or the x and y offsets aren't numeric.
367374
"""
368375

369376
pass

jwst/extract_2d/tests/test_nirspec.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from stdatamodels.jwst.transforms.models import Slit
77

88
from jwst.assign_wcs import AssignWcsStep
9+
from jwst.assign_wcs.util import NoDataOnDetectorError
910
from jwst.extract_2d.extract_2d_step import Extract2dStep
1011
from jwst.extract_2d.nirspec import select_slits
1112

@@ -273,26 +274,43 @@ def test_extract_2d_nirspec_bots(nirspec_bots_rateints):
273274

274275
def test_select_slits(nirspec_slit_list):
275276
slit_list = nirspec_slit_list
276-
# Choose all slits
277-
all_slits = select_slits(slit_list, None, None)
278-
assert all_slits == slit_list
277+
278+
# No slits selected
279+
with pytest.raises(NoDataOnDetectorError, match="No valid slits selected"):
280+
select_slits(slit_list, None, None)
281+
with pytest.raises(NoDataOnDetectorError, match="No valid slits selected"):
282+
select_slits(slit_list, [], [])
283+
with pytest.raises(NoDataOnDetectorError, match="No valid slits selected"):
284+
select_slits(slit_list, ["-99"], [-9999])
285+
279286
# Just slit with name=='3'
280287
single_name = select_slits(slit_list, ["3"], None)
281288
assert single_name[0].name == "3"
282289
assert single_name[0].source_id == "3000"
290+
283291
# Just slit with source_id == '3000'
284292
single_id = select_slits(slit_list, None, ["2000"])
285293
assert single_id[0].name == "2"
286294
assert single_id[0].source_id == "2000"
295+
287296
# Slits with name == '4' and source_id == '4000' are duplicates
288297
duplicates = select_slits(slit_list, ["1", "4"], ["2000", "4000"])
289298
assert Slit(name="1", source_id="1000") in duplicates
290299
assert Slit(name="2", source_id="2000") in duplicates
291300
assert Slit(name="4", source_id="4000") in duplicates
301+
292302
# Select slit with a non-integer name
293303
non_integer = select_slits(slit_list, ["S200A1"], None)
294304
assert non_integer[0] == Slit(name="S200A1", source_id="2222")
305+
295306
# Select slits with mix of integer and string names
296307
with_integer = select_slits(slit_list, [2, "5"], None)
297308
assert Slit(name="2", source_id="2000") in with_integer
298309
assert Slit(name="5", source_id="5000") in with_integer
310+
311+
# Select some valid, some invalid slits
312+
some_valid = select_slits(slit_list, ["1", "4", "-99"], ["2000", "4000", "-9999"])
313+
assert len(some_valid) == 3
314+
assert Slit(name="1", source_id="1000") in some_valid
315+
assert Slit(name="2", source_id="2000") in some_valid
316+
assert Slit(name="4", source_id="4000") in some_valid

0 commit comments

Comments
 (0)