22Test functions for NIRSPEC WCS - all modes.
33"""
44import functools
5- import os .path
65import shutil
76from math import cos , sin
87
1413from astropy .modeling import models as astmodels
1514from astropy import table
1615from astropy import wcs as astwcs
16+ from astropy .utils .data import get_pkg_data_filename
1717from gwcs import wcs , wcstools
1818from numpy .testing import assert_allclose
1919
2020from stdatamodels .jwst import datamodels
2121from stdatamodels .jwst .transforms import models as trmodels
2222
2323from jwst .assign_wcs import nirspec , assign_wcs_step
24- from jwst .assign_wcs .tests import data
2524from jwst .assign_wcs .util import MSAFileError , in_ifu_slice
2625
27-
28- data_path = os .path .split (os .path .abspath (data .__file__ ))[0 ]
29-
30-
3126wcs_kw = {'wcsaxes' : 2 , 'ra_ref' : 165 , 'dec_ref' : 54 ,
3227 'v2_ref' : - 8.3942412 , 'v3_ref' : - 5.3123744 , 'roll_ref' : 37 ,
3328 'crpix1' : 1024 , 'crpix2' : 1024 ,
3631 'pc1_1' : 1 , 'pc1_2' : 0 , 'pc2_1' : 0 , 'pc2_2' : 1
3732 }
3833
39-
4034slit_fields_num = ["shutter_id" , "dither_position" , "xcen" , "ycen" ,
4135 "ymin" , "ymax" , "quadrant" , "source_id" ,
4236 "stellarity" , "source_xpos" , "source_ypos" ,
4337 "slit_xscale" , "slit_yscale" ]
4438
45-
4639slit_fields_str = ["name" , "shutter_state" , "source_name" , "source_alias" ]
4740
4841
@@ -53,13 +46,6 @@ def _compare_slits(s1, s2):
5346 assert getattr (s1 , f ) == getattr (s2 , f )
5447
5548
56- def get_file_path (filename ):
57- """
58- Construct an absolute path.
59- """
60- return os .path .join (data_path , filename )
61-
62-
6349def create_hdul (detector = 'NRS1' ):
6450 """
6551 Create a fits HDUList instance.
@@ -109,7 +95,8 @@ def create_nirspec_mos_file(grating='G235M', filt='F170LP'):
10995 image [0 ].header ['grating' ] = grating
11096 image [0 ].header ['PATT_NUM' ] = 1
11197
112- msa_status_file = get_file_path ('SPCB-GD-A.msa.fits.gz' )
98+ msa_status_file = get_pkg_data_filename (
99+ "data/SPCB-GD-A.msa.fits.gz" , package = "jwst.assign_wcs.tests" )
113100 image [0 ].header ['MSAMETFL' ] = msa_status_file
114101 return image
115102
@@ -203,7 +190,7 @@ def test_nirspec_ifu_against_esa(wcs_ifu_grating):
203190 """
204191 Test Nirspec IFU mode using CV3 reference files.
205192 """
206- with fits .open (get_file_path ( ' Trace_IFU_Slice_00_SMOS-MOD-G1M-17-5344175105_30192_JLAB88.fits' )) as ref :
193+ with fits .open (get_pkg_data_filename ( "data/ Trace_IFU_Slice_00_SMOS-MOD-G1M-17-5344175105_30192_JLAB88.fits" , package = "jwst.assign_wcs.tests" )) as ref :
207194 # Test NRS1
208195 pyw = astwcs .WCS (ref ['SLITY1' ].header )
209196 # Test evaluating the WCS (slice 0)
@@ -245,7 +232,7 @@ def test_nirspec_fs_esa():
245232 # Test evaluating the WCS
246233 w1 = nirspec .nrs_wcs_set_input (im , "S200A1" )
247234
248- ref = fits .open (get_file_path ( ' Trace_SLIT_A_200_1_V84600010001P0000000002101_39547_JLAB88.fits' ))
235+ ref = fits .open (get_pkg_data_filename ( "data/ Trace_SLIT_A_200_1_V84600010001P0000000002101_39547_JLAB88.fits" , package = "jwst.assign_wcs.tests" ))
249236 pyw = astwcs .WCS (ref [1 ].header )
250237
251238 # get positions within the slit and the corresponding lambda
@@ -314,7 +301,8 @@ def test_msa_configuration_normal():
314301 # Test 1: Reasonably normal as well
315302 prog_id = '1234'
316303 msa_meta_id = 12
317- msaconfl = get_file_path ('msa_configuration.fits' )
304+ msaconfl = get_pkg_data_filename (
305+ "data/msa_configuration.fits" , package = "jwst.assign_wcs.tests" )
318306 dither_position = 1
319307 slitlet_info = nirspec .get_open_msa_slits (prog_id , msaconfl , msa_meta_id , dither_position ,
320308 slit_y_range = [- .5 , .5 ])
@@ -327,7 +315,8 @@ def test_msa_configuration_normal():
327315def test_msa_configuration_slit_scales ():
328316 prog_id = '1234'
329317 msa_meta_id = 12
330- msaconfl = get_file_path ('msa_configuration.fits' )
318+ msaconfl = get_pkg_data_filename (
319+ "data/msa_configuration.fits" , package = "jwst.assign_wcs.tests" )
331320 dither_position = 1
332321
333322 # mock slit scale for quadrant 4
@@ -346,7 +335,8 @@ def test_msa_configuration_no_background():
346335 # Test 2: Two main shutters, not allowed and should fail
347336 prog_id = '1234'
348337 msa_meta_id = 13
349- msaconfl = get_file_path ('msa_configuration.fits' )
338+ msaconfl = get_pkg_data_filename (
339+ "data/msa_configuration.fits" , package = "jwst.assign_wcs.tests" )
350340 dither_position = 1
351341 with pytest .raises (MSAFileError ):
352342 nirspec .get_open_msa_slits (prog_id , msaconfl , msa_meta_id , dither_position ,
@@ -361,7 +351,8 @@ def test_msa_configuration_all_background():
361351 # Test 3: No non-background, not acceptable.
362352 prog_id = '1234'
363353 msa_meta_id = 14
364- msaconfl = get_file_path ('msa_configuration.fits' )
354+ msaconfl = get_pkg_data_filename (
355+ "data/msa_configuration.fits" , package = "jwst.assign_wcs.tests" )
365356 dither_position = 1
366357 slitlet_info = nirspec .get_open_msa_slits (prog_id , msaconfl , msa_meta_id , dither_position ,
367358 slit_y_range = [- .5 , .5 ])
@@ -378,7 +369,8 @@ def test_msa_configuration_row_skipped():
378369 # Test 4: One row is skipped, should be acceptable.
379370 prog_id = '1234'
380371 msa_meta_id = 15
381- msaconfl = get_file_path ('msa_configuration.fits' )
372+ msaconfl = get_pkg_data_filename (
373+ "data/msa_configuration.fits" , package = "jwst.assign_wcs.tests" )
382374 dither_position = 1
383375 slitlet_info = nirspec .get_open_msa_slits (prog_id , msaconfl , msa_meta_id , dither_position ,
384376 slit_y_range = [- .5 , .5 ])
@@ -395,7 +387,8 @@ def test_msa_configuration_multiple_returns():
395387 # Test 4: One row is skipped, should be acceptable.
396388 prog_id = '1234'
397389 msa_meta_id = 16
398- msaconfl = get_file_path ('msa_configuration.fits' )
390+ msaconfl = get_pkg_data_filename (
391+ "data/msa_configuration.fits" , package = "jwst.assign_wcs.tests" )
399392 dither_position = 1
400393 slitlet_info = nirspec .get_open_msa_slits (prog_id , msaconfl , msa_meta_id , dither_position ,
401394 slit_y_range = [- .5 , .5 ])
@@ -415,7 +408,8 @@ def test_msa_fs_configuration():
415408 """
416409 prog_id = '1234'
417410 msa_meta_id = 12
418- msaconfl = get_file_path ('msa_fs_configuration.fits' )
411+ msaconfl = get_pkg_data_filename (
412+ "data/msa_fs_configuration.fits" , package = "jwst.assign_wcs.tests" )
419413 dither_position = 1
420414 slitlet_info = nirspec .get_open_msa_slits (
421415 prog_id , msaconfl , msa_meta_id , dither_position , slit_y_range = [- .5 , .5 ])
@@ -448,7 +442,8 @@ def test_msa_fs_configuration_unsupported(tmp_path):
448442 Test the get_open_msa_slits function with unsupported FS defined.
449443 """
450444 # modify an existing MSA file to add a bad row
451- msaconfl = get_file_path ('msa_fs_configuration.fits' )
445+ msaconfl = get_pkg_data_filename (
446+ "data/msa_fs_configuration.fits" , package = "jwst.assign_wcs.tests" )
452447 bad_confl = str (tmp_path / 'bad_msa_fs_configuration.fits' )
453448 shutil .copy (msaconfl , bad_confl )
454449
@@ -472,7 +467,8 @@ def test_msa_missing_source(tmp_path):
472467 Test the get_open_msa_slits function with missing source information.
473468 """
474469 # modify an existing MSA file to remove source info
475- msaconfl = get_file_path ('msa_fs_configuration.fits' )
470+ msaconfl = get_pkg_data_filename (
471+ "data/msa_fs_configuration.fits" , package = "jwst.assign_wcs.tests" )
476472 bad_confl = str (tmp_path / 'bad_msa_fs_configuration.fits' )
477473 shutil .copy (msaconfl , bad_confl )
478474
@@ -510,7 +506,8 @@ def test_msa_nan_source_posn(tmp_path):
510506 Test the get_open_msa_slits function with nan values for source position.
511507 """
512508 # modify an existing MSA file to remove source info
513- msaconfl = get_file_path ('msa_fs_configuration.fits' )
509+ msaconfl = get_pkg_data_filename (
510+ "data/msa_fs_configuration.fits" , package = "jwst.assign_wcs.tests" )
514511 bad_confl = str (tmp_path / 'nan_msa_fs_configuration.fits' )
515512 shutil .copy (msaconfl , bad_confl )
516513
@@ -599,7 +596,8 @@ def test_open_slits():
599596 """
600597 image = create_nirspec_mos_file ()
601598 model = datamodels .ImageModel (image )
602- msaconfl = get_file_path ('msa_configuration.fits' )
599+ msaconfl = get_pkg_data_filename (
600+ "data/msa_configuration.fits" , package = "jwst.assign_wcs.tests" )
603601
604602 model .meta .instrument .msa_metadata_file = msaconfl
605603 model .meta .instrument .msa_metadata_id = 12
@@ -614,7 +612,8 @@ def test_shutter_size_on_sky():
614612 """
615613 image = create_nirspec_mos_file ()
616614 model = datamodels .ImageModel (image )
617- msaconfl = get_file_path ('msa_configuration.fits' )
615+ msaconfl = get_pkg_data_filename (
616+ "data/msa_configuration.fits" , package = "jwst.assign_wcs.tests" )
618617
619618 model .meta .instrument .msa_metadata_file = msaconfl
620619 model .meta .instrument .msa_metadata_id = 12
@@ -676,7 +675,8 @@ def test_functional_fs_msa(mode):
676675 im .meta .wcs = w
677676 slit_wcs = nirspec .nrs_wcs_set_input (im , 1 )
678677
679- ins_file = get_file_path (model_file )
678+ ins_file = get_pkg_data_filename (
679+ f"data/{ model_file } " , package = "jwst.assign_wcs.tests" )
680680 ins_tab = table .Table .read (ins_file , format = 'ascii' )
681681
682682 # Setup the test
@@ -791,7 +791,8 @@ def test_functional_ifu_grating(wcs_ifu_grating):
791791 im , refs = wcs_ifu_grating ('G395H' , 'F290LP' , gwa_xtil = 0.35986012 , gwa_ytil = 0.13448857 )
792792
793793 slit_wcs = nirspec .nrs_wcs_set_input (im , 0 ) # use slice 0
794- ins_file = get_file_path (model_file )
794+ ins_file = get_pkg_data_filename (
795+ f"data/{ model_file } " , package = "jwst.assign_wcs.tests" )
795796 ins_tab = table .Table .read (ins_file , format = 'ascii' )
796797 slitx = [0 ] * 5
797798 slity = [- .5 , - .25 , 0 , .25 , .5 ]
@@ -952,7 +953,8 @@ def test_functional_ifu_prism():
952953 w = wcs .WCS (pipeline )
953954 im .meta .wcs = w
954955 slit_wcs = nirspec .nrs_wcs_set_input (im , 0 ) # use slice 0
955- ins_file = get_file_path (model_file )
956+ ins_file = get_pkg_data_filename (
957+ f"data/{ model_file } " , package = "jwst.assign_wcs.tests" )
956958 ins_tab = table .Table .read (ins_file , format = 'ascii' )
957959 slitx = [0 ] * 5
958960 slity = [- .5 , - .25 , 0 , .25 , .5 ]
0 commit comments