From 476283a6e5024bc557fb6455f8cb9b36da13cb0f Mon Sep 17 00:00:00 2001 From: Tyler Pauly Date: Thu, 24 Oct 2024 15:42:40 -0400 Subject: [PATCH] JP-3102 follow-up: Catch source_pos nans from msa file (#8874) --- changes/8874.assign_wcs.rst | 1 + jwst/assign_wcs/nirspec.py | 9 ++++---- jwst/assign_wcs/tests/test_nirspec.py | 32 +++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 changes/8874.assign_wcs.rst diff --git a/changes/8874.assign_wcs.rst b/changes/8874.assign_wcs.rst new file mode 100644 index 0000000000..d27218f8d4 --- /dev/null +++ b/changes/8874.assign_wcs.rst @@ -0,0 +1 @@ +Catch NaN values in msa tables for source positions in slit and replace with slit center. \ No newline at end of file diff --git a/jwst/assign_wcs/nirspec.py b/jwst/assign_wcs/nirspec.py index 2168804bee..7902d6c3bc 100644 --- a/jwst/assign_wcs/nirspec.py +++ b/jwst/assign_wcs/nirspec.py @@ -674,8 +674,9 @@ def get_open_msa_slits(prog_id, msa_file, msa_metadata_id, dither_position, if n_main_shutter == 1: # Source is marked primary source_id = slitlet['source_id'] - source_xpos = slitlet['estimated_source_in_shutter_x'] - source_ypos = slitlet['estimated_source_in_shutter_y'] + source_xpos = np.nan_to_num(slitlet['estimated_source_in_shutter_x'], nan=0.5) + source_ypos = np.nan_to_num(slitlet['estimated_source_in_shutter_y'], nan=0.5) + log.info(f'Found fixed slit {slitlet_id} with source_id = {source_id}.') # Get source info for this slitlet: @@ -746,8 +747,8 @@ def get_open_msa_slits(prog_id, msa_file, msa_metadata_id, dither_position, elif n_main_shutter == 1: xcen, ycen, quadrant, source_xpos, source_ypos = [ (s['shutter_row'], s['shutter_column'], s['shutter_quadrant'], - s['estimated_source_in_shutter_x'], - s['estimated_source_in_shutter_y']) + np.nan_to_num(s['estimated_source_in_shutter_x'], nan=0.5), + np.nan_to_num(s['estimated_source_in_shutter_y'], nan=0.5)) for s in slitlet_rows if s['background'] == 'N'][0] shutter_id = xcen + (ycen - 1) * 365 # shutter numbers in MSA file are 1-indexed diff --git a/jwst/assign_wcs/tests/test_nirspec.py b/jwst/assign_wcs/tests/test_nirspec.py index 3c6d3591f5..fc7a7032c3 100644 --- a/jwst/assign_wcs/tests/test_nirspec.py +++ b/jwst/assign_wcs/tests/test_nirspec.py @@ -446,6 +446,38 @@ def test_msa_missing_source(tmp_path): _compare_slits(slitlet_info[1], ref_slit) +def test_msa_nan_source_posn(tmp_path): + """ + Test the get_open_msa_slits function with nan values for source position. + """ + # modify an existing MSA file to remove source info + msaconfl = get_file_path('msa_fs_configuration.fits') + bad_confl = str(tmp_path / 'nan_msa_fs_configuration.fits') + shutil.copy(msaconfl, bad_confl) + + with fits.open(bad_confl) as msa_hdu_list: + shutter_table = table.Table(msa_hdu_list['SHUTTER_INFO'].data) + shutter_table[-5:]['estimated_source_in_shutter_x'] = np.nan + msa_hdu_list['SHUTTER_INFO'] = fits.table_to_hdu(shutter_table) + msa_hdu_list[2].name = 'SHUTTER_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(name='S200A1', shutter_id=0, dither_position=1, xcen=0, ycen=0, + ymin=-0.5, ymax=0.5, quadrant=5, source_id=3, shutter_state='x', + source_name='95065_3', source_alias='3', stellarity=1.0, + source_xpos=0.0, source_ypos=-0.2290000021457672, + source_ra=53.139904, source_dec=-27.805002) + _compare_slits(slitlet_info[1], 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"]