def test_miri_image_wcs(_bigdata): """ Regression test of creating a WCS object and doing pixel to sky transformation. """ suffix = 'assignwcsstep' output_file_base, output_file = add_suffix('miri_image_wcs_output.fits', suffix) try: os.remove(output_file) except: pass input_file = os.path.join( _bigdata, 'miri', 'test_wcs', 'image', 'jw00001001001_01101_00001_MIRIMAGE_ramp_fit.fits') ref_file = os.path.join( _bigdata, 'miri', 'test_wcs', 'image', 'jw00001001001_01101_00001_MIRIMAGE_assign_wcs.fits') AssignWcsStep.call(input_file, output_file=output_file_base, suffix=suffix) im = ImageModel(output_file) imref = ImageModel(ref_file) x, y = np.mgrid[:1031, :1024] ra, dec = im.meta.wcs(x, y) raref, decref = imref.meta.wcs(x, y) utils.assert_allclose(ra, raref) utils.assert_allclose(dec, decref)
def img_with_wcs(input): """ Open a JWST exposure and apply the distortion model. Parameters ---------- input : type Anything `jwst.datamodels.util.open` can accept for initialization. Returns ------- with_wcs : `jwst.datamodels.ImageModel` Image model with full `~gwcs` in `with_wcs.meta.wcs`. """ from jwst.datamodels import util from jwst.assign_wcs import AssignWcsStep # from jwst.stpipe import crds_client # from jwst.assign_wcs import assign_wcs # HDUList -> jwst.datamodels.ImageModel img = util.open(input) # AssignWcs to pupulate img.meta.wcsinfo step = AssignWcsStep() with_wcs = step.process(img) ## Above should be more robust to get all of the necessary ref files #dist_file = crds_client.get_reference_file(img, 'distortion') #reference_files = {'distortion': dist_file} #with_wcs = assign_wcs.load_wcs(img, reference_files=reference_files) return with_wcs
def create_tso_wcsimage(filtername="F277W", subarray=False): """Help create tsgrism GWCS object.""" if subarray: subarray = "SUBGRISM256" else: subarray = "FULL" hdul = create_hdul(exptype='NRC_TSGRISM', pupil='GRISMR', filtername=filtername, detector='NRCALONG', subarray=subarray, wcskeys=wcs_tso_kw) hdul['sci'].header['SUBSIZE1'] = NIRCAM_TSO_WIDTH if subarray: hdul['sci'].header['SUBSIZE2'] = 256 subsize = 256 else: hdul['sci'].header['SUBSIZE2'] = 2048 subsize = 2048 hdul['sci'].data = np.ones((2, subsize, NIRCAM_TSO_WIDTH)) im = CubeModel(hdul) im.meta.wcsinfo.siaf_xref_sci = 887.0 im.meta.wcsinfo.siaf_yref_sci = 35.0 aswcs = AssignWcsStep() return aswcs.process(im)
def test_miri_fixed_slit_wcs(_bigdata): """ Regression test of creating a WCS object and doing pixel to sky transformation. """ output_file_base, output_file = add_suffix('miri_fixed_wcs_output.fits', 'assignwcsstep') try: os.remove(output_file) except: pass input_file = os.path.join(_bigdata, 'miri', 'test_wcs', 'fixed', 'jw00035001001_01101_00001_mirimage_rate.fits') ref_file = os.path.join( _bigdata, 'miri', 'test_wcs', 'fixed', 'jw00035001001_01101_00001_mirimage_assign_wcs.fits') AssignWcsStep.call(input_file, output_file=output_file_base) im = ImageModel(output_file) imref = ImageModel(ref_file) y, x = np.mgrid[:1031, :1024] ra, dec, lam = im.meta.wcs(x, y) raref, decref, lamref = imref.meta.wcs(x, y) utils.assert_allclose(ra, raref) utils.assert_allclose(dec, decref) utils.assert_allclose(lam, lamref)
def test_miri_slitless_wcs(_bigdata): """ Regression test of creating a WCS object and doing pixel to sky transformation. """ suffix = 'assignwcsstep' output_file_base, output_file = add_suffix('miri_slitless_wcs_output.fits', suffix) try: os.remove(output_file) except: pass input_file = os.path.join( _bigdata, 'miri', 'test_wcs', 'slitless', 'jw80600012001_02101_00003_mirimage_rateints.fits') ref_file = os.path.join( _bigdata, 'miri', 'test_wcs', 'slitless', 'jw80600012001_02101_00003_mirimage_assign_wcs.fits') AssignWcsStep.call(input_file, output_file=output_file_base, suffix=suffix) im = CubeModel(output_file) imref = CubeModel(ref_file) x, y = np.mgrid[:1031, :1024] ra, dec, lam = im.meta.wcs(x, y) raref, decref, lamref = imref.meta.wcs(x, y) utils.assert_allclose(ra, raref) utils.assert_allclose(dec, decref) utils.assert_allclose(lam, lamref)
def create_reference_files(datamodel): refs = {} step = AssignWcsStep() for reftype in AssignWcsStep.reference_file_types: refs[reftype] = step.get_reference_file(datamodel, reftype) return refs
def test_nirspec_nrs1_wcs(_bigdata): """ Regression test of creating a WCS object and doing pixel to sky transformation. """ output_file_base, output_file = add_suffix('nrs1_ifu_wcs_output.fits', 'assignwcsstep') try: os.remove(output_file) except: pass input_file = os.path.join(_bigdata, 'nirspec', 'test_wcs', 'nrs1-ifu', 'jw00011001001_01120_00001_NRS1_rate.fits') ref_file = os.path.join(_bigdata, 'nirspec', 'test_wcs', 'nrs1-ifu', 'jw00011001001_01120_00001_NRS1_rate_assign_wcs.fits') AssignWcsStep.call(input_file, output_file=output_file_base, name='assignwcsstep' ) im = ImageModel(output_file) imref = ImageModel(ref_file) a_wcs = nirspec.nrs_wcs_set_input(im, 0) w = a_wcs y, x = np.mgrid[w.bounding_box[1][0]:w.bounding_box[1][1], w.bounding_box[0][0]: w.bounding_box[0][1]] ra, dec, lam = w(x, y) a_wcs_ref = nirspec.nrs_wcs_set_input(im, 0) wref = a_wcs_ref raref, decref, lamref = wref(x, y) # equal_nan is used here as many of the entries are nan. # The domain is defined but it is only a few entries in there that are valid # as it is a curved narrow slit. utils.assert_allclose(ra, raref, equal_nan=True) utils.assert_allclose(dec, decref, equal_nan=True) utils.assert_allclose(lam, lamref, equal_nan=True)
def miri_rate_pair(miri_rate_zero_crossing): im1 = miri_rate_zero_crossing # Create a nodded version im2 = im1.copy() im2.meta.wcsinfo.ra_ref = 0.00026308279776455 im2.meta.wcsinfo.dec_ref = -2.1860888891293e-05 im1 = AssignWcsStep.call(im1) im2 = AssignWcsStep.call(im2) return im1, im2
def test_miri_ifu_wcs(_bigdata): """ Regression test of creating a WCS object and doing pixel to sky transformation. """ try: os.remove("miri_ifu_wcs_output.fits") except: pass input_file = os.path.join( _bigdata, 'miri', 'test_wcs', 'ifu', 'jw00024001001_01101_00001_MIRIFUSHORT_uncal_MiriSloperPipeline.fits') ref_file = os.path.join( _bigdata, 'miri', 'test_wcs', 'ifu', 'jw00024001001_01101_00001_MIRIFUSHORT_assign_wcs.fits') AssignWcsStep.call(input_file, output_file='miri_ifu_wcs', suffix='output') im = ImageModel('miri_ifu_wcs_output.fits') imref = ImageModel(ref_file) # Get the region file region = RegionsModel(crds_client.get_reference_file(im, 'regions')) # inputs shape = region.regions.shape y, x = np.mgrid[:shape[0], :shape[1]] # Get indices where pixels == 0. These should be NaNs in the output. ind_zeros = region.regions == 0 ra, dec, lam = im.meta.wcs(x, y) raref, decref, lamref = imref.meta.wcs(x, y) utils.assert_allclose(ra, raref, equal_nan=True) utils.assert_allclose(dec, decref, equal_nan=True) utils.assert_allclose(lam, lamref, equal_nan=True) # Test that we got NaNs at ind_zero assert (np.isnan(ra).nonzero()[0] == ind_zeros.nonzero()[0]).all() assert (np.isnan(ra).nonzero()[1] == ind_zeros.nonzero()[1]).all() # Test the inverse transform x1, y1 = im.meta.wcs.backward_transform(ra, dec, lam) assert (np.isnan(x1).nonzero()[0] == ind_zeros.nonzero()[0]).all() assert (np.isnan(x1).nonzero()[1] == ind_zeros.nonzero()[1]).all() # Also run a smoke test with values outside the region. dec[100][200] = -80 ra[100][200] = 7 lam[100][200] = 15 x2, y2 = im.meta.wcs.backward_transform(ra, dec, lam) assert np.isnan(x2[100][200]) assert np.isnan(x2[100][200])
def img_with_wcs(input): """ Open a JWST exposure and apply the distortion model. Parameters ---------- input : type Anything `jwst.datamodels.util.open` can accept for initialization. Returns ------- with_wcs : `jwst.datamodels.ImageModel` Image model with full `~gwcs` in `with_wcs.meta.wcs`. """ from jwst.datamodels import util from jwst.assign_wcs import AssignWcsStep import astropy.io.fits as pyfits # from jwst.stpipe import crds_client # from jwst.assign_wcs import assign_wcs # HDUList -> jwst.datamodels.ImageModel # Generate WCS as image if isinstance(input, pyfits.HDUList): if input[0].header['INSTRUME'] == 'NIRISS': if input[0].header['FILTER'].startswith('GR'): input[0].header['FILTER'] = 'CLEAR' input[0].header['EXP_TYPE'] = 'NIS_IMAGE' #print(input[0].header) elif input[0].header['INSTRUME'] == 'NIRCAM': if input[0].header['PUPIL'].startswith('GR'): input[0].header['PUPIL'] = 'CLEAR' input[0].header['EXP_TYPE'] = 'NRC_IMAGE' #print(input[0].header) img = util.open(input) # AssignWcs to pupulate img.meta.wcsinfo step = AssignWcsStep() with_wcs = step.process(img) # Above should be more robust to get all of the necessary ref files #dist_file = crds_client.get_reference_file(img, 'distortion') #reference_files = {'distortion': dist_file} #with_wcs = assign_wcs.load_wcs(img, reference_files=reference_files) return with_wcs
def test_nis_wfss_background(filters, pupils, make_wfss_datamodel): """Test background subtraction for NIRISS WFSS modes.""" data = make_wfss_datamodel data.meta.instrument.filter = filters data.meta.instrument.pupil = pupils data.meta.instrument.detector = 'NIS' data.meta.instrument.name = 'NIRISS' data.meta.exposure.type = 'NIS_WFSS' wcs_corrected = AssignWcsStep.call(data) # Get References wavelenrange = Step().get_reference_file(wcs_corrected, "wavelengthrange") bkg_file = Step().get_reference_file(wcs_corrected, 'wfssbkg') mask = mask_from_source_cat(wcs_corrected, wavelenrange) with datamodels.open(bkg_file) as bkg_ref: bkg_ref = no_NaN(bkg_ref) # calculate backgrounds pipeline_data_mean = robust_mean(wcs_corrected.data[mask]) test_data_mean, _, _ = sigma_clipped_stats(wcs_corrected.data, sigma=2) pipeline_reference_mean = robust_mean(bkg_ref.data[mask]) test_reference_mean, _, _ = sigma_clipped_stats(bkg_ref.data, sigma=2) assert np.isclose([pipeline_data_mean], [test_data_mean], rtol=1e-3) assert np.isclose([pipeline_reference_mean], [test_reference_mean], rtol=1e-1)
def test_nirspec_ifu_wcs(_bigdata, test_id, input_file, truth_file): """ Regression test of creating a WCS object and doing pixel to sky transformation. """ del test_id input_file = os.path.join(_bigdata, 'nirspec', 'test_wcs', 'nrs1-ifu', input_file) truth_file = os.path.join(_bigdata, 'nirspec', 'test_wcs', 'nrs1-ifu', truth_file) result = AssignWcsStep.call(input_file, save_results=True, suffix='assign_wcs') result.close() im = ImageModel(result.meta.filename) imref = ImageModel(truth_file) w = nirspec.nrs_wcs_set_input(im, 0) grid = grid_from_bounding_box(w.bounding_box) ra, dec, lam = w(*grid) wref = nirspec.nrs_wcs_set_input(imref, 0) raref, decref, lamref = wref(*grid) # equal_nan is used here as many of the entries are nan. # The domain is defined but it is only a few entries in there that are valid # as it is a curved narrow slit. assert_allclose(ra, raref, equal_nan=True) assert_allclose(dec, decref, equal_nan=True) assert_allclose(lam, lamref, equal_nan=True)
def test_custom_wcs_resample_imaging(nircam_rate, ratio, rotation, crpix, crval, shape): im = AssignWcsStep.call(nircam_rate, sip_approx=False) im.data += 5 result = ResampleStep.call(im, output_shape=shape, crpix=crpix, crval=crval, rotation=rotation, pixel_scale_ratio=ratio) t = result.meta.wcs.forward_transform # test rotation pc = t['pc_rotation_matrix'].matrix.value orientation = np.rad2deg(np.arctan2(pc[0, 1], pc[1, 1])) assert np.allclose(rotation, orientation) # test CRPIX assert np.allclose((-t['crpix1'].offset.value, -t['crpix2'].offset.value), crpix) # test CRVAL assert np.allclose(t(*crpix), crval) # test output image shape assert result.data.shape == shape[::-1]
def test_resample_variance(nircam_rate, n_images): """Test that resampled variance and error arrays are computed properly""" err = 0.02429 var_rnoise = 0.00034 var_poisson = 0.00025 im = AssignWcsStep.call(nircam_rate) im.var_rnoise += var_rnoise im.var_poisson += var_poisson im.err += err im.meta.filename = "foo.fits" c = ModelContainer() for n in range(n_images): c.append(im.copy()) result = ResampleStep.call(c, blendheaders=False) # Verify that the combined uncertainty goes as 1 / sqrt(N) assert_allclose(result.err[5:-5, 5:-5].mean(), err / np.sqrt(n_images), atol=1e-5) assert_allclose(result.var_rnoise[5:-5, 5:-5].mean(), var_rnoise / n_images, atol=1e-7) assert_allclose(result.var_poisson[5:-5, 5:-5].mean(), var_poisson / n_images, atol=1e-7)
def test_master_background_userbg(_jail, user_background): """Verify data can run through the step with a user-supplied background""" image = datamodels.ImageModel((10, 10)) image.meta.instrument.name = 'MIRI' image.meta.instrument.detector = 'MIRIMAGE' image.meta.exposure.type = 'MIR_LRS-FIXEDSLIT' image.meta.observation.date = '2018-01-01' image.meta.observation.time = '00:00:00' image.meta.subarray.xstart = 1 image.meta.subarray.ystart = 1 image.meta.wcsinfo.v2_ref = 0 image.meta.wcsinfo.v3_ref = 0 image.meta.wcsinfo.roll_ref = 0 image.meta.wcsinfo.ra_ref = 0 image.meta.wcsinfo.dec_ref = 0 image = AssignWcsStep.call(image) # Run with a user-supplied background and verify this is recorded in header result = MasterBackgroundStep.call(image, user_background=user_background) collect_pipeline_cfgs('./config') result = MasterBackgroundStep.call( image, config_file='config/master_background.cfg', user_background=user_background, ) # For inputs that are not files, the following should be true assert type(image) is type(result) assert result is not image assert result.meta.cal_step.master_background == 'COMPLETE' assert result.meta.background.master_background_file == 'user_background.fits'
def test_create_box_gwcs(): """Make sure that a box is created around a source catalog object. This version allows use of the GWCS to translate the source location. This is currently expected to fail because of the distortion reference file. The settings and catalog used should produce first order trace boxes for the objects. """ source_catalog = get_file_path('step_SourceCatalogStep_cat.ecsv') hdul = create_hdul(exptype='NRC_WFSS', pupil='GRISMR', wcskeys=wcs_wfss_kw) im = ImageModel(hdul) aswcs = AssignWcsStep() imwcs = aswcs(im) imwcs.meta.source_catalog = source_catalog refs = get_reference_files(im) test_boxes = create_grism_bbox(imwcs, refs, mmag_extract=99.) assert len(test_boxes) >= 2 # the catalog has 4 objects for sid in [9, 19]: ids = [source for source in test_boxes if source.sid == sid] assert len(ids) == 1 assert ids[0].xcentroid > 0 assert ids[0].ycentroid > 0 if sid == 19: assert [1, 2] == list(ids[0].order_bounding.keys()) if sid == 9: assert [1] == list(ids[0].order_bounding.keys())
def test_weight_type(nircam_rate, _jail): """Check that weight_type of exptime and ivm work""" im1 = AssignWcsStep.call(nircam_rate, sip_approx=False) im1.var_rnoise[:] = 0 im2 = im1.copy() im3 = im1.copy() im1.data += 10 im2.data += 5 im3.data += 5 im1.var_rnoise += (1 / 10) im2.var_rnoise += (1 / 5) im3.var_rnoise += (1 / 5) im2.meta.observation.sequence_id = "2" im3.meta.observation.sequence_id = "3" c = ModelContainer([im1, im2, im3]) assert len(c.group_names) == 3 result1 = ResampleStep.call(c, weight_type="ivm", blendheaders=False, save_results=True) # assert_allclose(result1.data, result2.data) # assert_allclose(result1.wht, result2.wht) assert_allclose(result1.data[100:105, 100:105], 7.5, rtol=1e-2) assert_allclose(result1.wht[100:105, 100:105], 20, rtol=1e-2) result2 = ResampleStep.call(c, weight_type="exptime", blendheaders=False) assert_allclose(result2.data[100:105, 100:105], 7.5, rtol=1e-2) assert_allclose(result2.wht[100:105, 100:105], 20, rtol=1e-2)
def test_nirspec_nrs1_wcs(self): """ Regression test of creating a WCS object and doing pixel to sky transformation. """ input_file = self.get_data(*self.test_dir, 'jw00023001001_01101_00001_NRS1_ramp_fit.fits') ref_file = self.get_data(*self.ref_loc, 'jw00023001001_01101_00001_NRS1_ramp_fit_assign_wcs.fits') result = AssignWcsStep.call(input_file, save_results=True, suffix='assign_wcs') result.close() im = ImageModel(result.meta.filename) imref = ImageModel(ref_file) for slit in ['S200A1', 'S200A2', 'S400A1', 'S1600A1']: w = nirspec.nrs_wcs_set_input(im, slit) grid = grid_from_bounding_box(w.bounding_box) ra, dec, lam = w(*grid) wref = nirspec.nrs_wcs_set_input(imref, slit) raref, decref, lamref = wref(*grid) assert_allclose(ra, raref, equal_nan=True) assert_allclose(dec, decref, equal_nan=True) assert_allclose(lam, lamref, equal_nan=True)
def test_nirspec_ifu_wcs(envopt, _jail, test_id, input_file, truth_file): """ Regression test of creating a WCS object and doing pixel to sky transformation. """ del test_id input_file = get_bigdata('jwst-pipeline', envopt, 'nirspec', 'test_wcs', 'nrs1-ifu', input_file) truth_file = get_bigdata('jwst-pipeline', envopt, 'nirspec', 'test_wcs', 'nrs1-ifu', 'truth', truth_file) result = AssignWcsStep.call(input_file, save_results=True, suffix='assign_wcs') result.close() im = ImageModel(result.meta.filename) imref = ImageModel(truth_file) w = nirspec.nrs_wcs_set_input(im, 0) grid = grid_from_bounding_box(w.bounding_box) ra, dec, lam = w(*grid) wref = nirspec.nrs_wcs_set_input(imref, 0) raref, decref, lamref = wref(*grid) # equal_nan is used here as many of the entries are nan. # The domain is defined but it is only a few entries in there that are valid # as it is a curved narrow slit. assert_allclose(ra, raref, equal_nan=True) assert_allclose(dec, decref, equal_nan=True) assert_allclose(lam, lamref, equal_nan=True)
def test_step_neg_shift_no_refine_no_flip(wfs_association): path_asn, path1, path2 = wfs_association nircam_pixel_size = 0.031 / 3600. delta_pixel = -5 with datamodels.open(path2) as im2: im2.meta.wcsinfo = { 'dec_ref': 11.99875540218638, 'ra_ref': 22.02351763251896 + delta_pixel * nircam_pixel_size, 'roll_ref': 0.005076934167039675, 'v2_ref': 86.039011, 'v3_ref': -493.385704, 'v3yangle': -0.07385127, 'vparity': -1, 'wcsaxes': 2 } im2 = AssignWcsStep.call(im2, sip_approx=False) im2.save(path2) wfs = WfsCombineStep.call(path_asn, do_refine=False, flip_dithers=False, psf_size=50, blur_size=10, n_size=2) assert wfs[0].meta.wcsinfo.ra_ref == 22.02351763251896
def test_shift_order_no_refine_with_flip(wfs_association): # change the sign of the the dither and check that we get the same delta pix but with switched # order of images. path_asn, path1, path2 = wfs_association nircam_pixel_size = 0.031 / 3600. delta_pixel = -5 with datamodels.open(path2) as im2: im2.meta.wcsinfo = { 'dec_ref': 11.99875540218638, 'ra_ref': 22.02351763251896 + delta_pixel * nircam_pixel_size, 'roll_ref': 0.005076934167039675, 'v2_ref': 86.039011, 'v3_ref': -493.385704, 'v3yangle': -0.07385127, 'vparity': -1, 'wcsaxes': 2 } im2 = AssignWcsStep.call(im2, sip_approx=False) im2.save(path2) wfs = wfs_combine.DataSet(path1, path2, 'outfile.fits', do_refine=False, flip_dithers=True, psf_size=50, blur_size=10, n_size=2) wfs.do_all() wfs.input_1.close() wfs.input_2.close() # assert wfs.input_1.meta.observation.exposure_number == '2' # assert wfs.input_2.meta.observation.exposure_number == '1' assert wfs.off_x == -1 * delta_pixel
def test_nirspec_wcs(_jail, rtdata, test_id, input_file, truth_file): """ Test of the AssignWcs step on 4 different NIRSpec exposures: 1) IFU NRS1 exposure, 2) IFU NRS1 exposure with FILTER=OPAQUE, 3) IFU NRS2 exposure, and 4) FS NRS1 exposure with 4 slits. """ # Get the input and truth files rtdata.get_data('nirspec/test_wcs/' + input_file) rtdata.get_truth('truth/test_nirspec_wcs/' + truth_file) # Run the AssignWcs step result = AssignWcsStep.call(input_file, save_results=True, suffix='assign_wcs') result.close() # Open the output and truth files im = ImageModel(result.meta.filename) im_ref = ImageModel(truth_file) if result.meta.exposure.type == 'NRS_FIXEDSLIT': # Loop over the 4 slit instances for slit in ['S200A1', 'S200A2', 'S400A1', 'S1600A1']: # Create WCS objects for each image wcs = nirspec.nrs_wcs_set_input(im, slit) wcs_ref = nirspec.nrs_wcs_set_input(im_ref, slit) # Compute RA, Dec, lambda values for each image array grid = grid_from_bounding_box(wcs.bounding_box) ra, dec, lam = wcs(*grid) ra_ref, dec_ref, lam_ref = wcs_ref(*grid) # Compare the sky coordinates assert_allclose(ra, ra_ref, equal_nan=True) assert_allclose(dec, dec_ref, equal_nan=True) assert_allclose(lam, lam_ref, equal_nan=True) else: # Create WCS objects for each image wcs = nirspec.nrs_wcs_set_input(im, 0) wcs_ref = nirspec.nrs_wcs_set_input(im_ref, 0) # Compute RA, Dec, lambda values for each image array grid = grid_from_bounding_box(wcs.bounding_box) ra, dec, lam = wcs(*grid) ra_ref, dec_ref, lam_ref = wcs_ref(*grid) # Compare the sky coordinates # equal_nan is used, because many of the entries are NaN, # due to the bounding_box being rectilinear while the # defined spectral traces are curved assert_allclose(ra, ra_ref, equal_nan=True) assert_allclose(dec, dec_ref, equal_nan=True) assert_allclose(lam, lam_ref, equal_nan=True)
def create_wfss_image(pupil, filtername='F444W'): hdul = create_hdul(exptype='NRC_WFSS', filtername=filtername, pupil=pupil, wcskeys=wcs_wfss_kw) hdul['sci'].data = np.ones( (hdul[0].header['SUBSIZE1'], hdul[0].header['SUBSIZE2'])) im = ImageModel(hdul) return AssignWcsStep.call(im)
def test_pixel_scale_ratio_spec(miri_rate, ratio): im = AssignWcsStep.call(miri_rate, sip_approx=False) result1 = ResampleSpecStep.call(im) result2 = ResampleSpecStep.call(im, pixel_scale_ratio=ratio) assert_allclose(np.array(result1.data.shape), np.array(result2.data.shape) * ratio, rtol=1, atol=1)
def test_miri_wcs_roundtrip(miri_rate): im = AssignWcsStep.call(miri_rate) im = ResampleSpecStep.call(im) x, y = grid_from_bounding_box(im.meta.wcs.bounding_box) ra, dec, lam = im.meta.wcs(x, y) xp, yp = im.meta.wcs.invert(ra, dec, lam) assert_allclose(x, xp, atol=1e-8) assert_allclose(y, yp, atol=1e-8)
def test_nirspec_ifu_wcs(input_file, rtdata): """Test NIRSpec IFU wcs""" rtdata.get_data(f"nirspec/ifu/{input_file}") AssignWcsStep.call(input_file, save_results=True, suffix='assign_wcs') output = input_file.replace('rate.fits', 'assign_wcs.fits') rtdata.output = output rtdata.get_truth(f"truth/test_nirspec_wcs/{output}") with datamodels.open(rtdata.output) as im, datamodels.open( rtdata.truth) as im_truth: # Test several slices in the IFU, range(30) for slice_ in [0, 9, 16, 23, 29]: wcs = nirspec.nrs_wcs_set_input(im, slice_) wcs_truth = nirspec.nrs_wcs_set_input(im_truth, slice_) assert_wcs_grid_allclose(wcs, wcs_truth)
def test_image(asnfile): '''Test to single FITS input.''' with open(asnfile) as json_data: d = json.load(json_data) members = d['products'][0]['members'][0]['expname'] wcs = AssignWcsStep.call(members) resample = ResampleStep.call(wcs) resample.save(asnfile[:5] + '_singleimage_resample.fits')
def test_nirspec_fixedslit_wcs(rtdata): """Test NIRSpec fixed slit wcs""" input_file = 'jw00023001001_01101_00001_nrs1_rate.fits' rtdata.get_data(f"nirspec/fs/{input_file}") AssignWcsStep.call(input_file, save_results=True, suffix='assign_wcs') output = input_file.replace('rate', 'assign_wcs') rtdata.output = output rtdata.get_truth(f"truth/test_nirspec_wcs/{output}") with datamodels.open(rtdata.output) as im, datamodels.open( rtdata.truth) as im_truth: # Check the 4 science slits for slit in ['S200A1', 'S200A2', 'S400A1', 'S1600A1']: wcs = nirspec.nrs_wcs_set_input(im, slit) wcs_truth = nirspec.nrs_wcs_set_input(im_truth, slit) assert_wcs_grid_allclose(wcs, wcs_truth)
def test_resample_undefined_variance(nircam_rate, shape): """Test that resampled variance and error arrays are computed properly""" im = AssignWcsStep.call(nircam_rate) im.var_rnoise = np.ones(shape, dtype=im.var_rnoise.dtype.type) im.var_poisson = np.ones(shape, dtype=im.var_poisson.dtype.type) im.var_flat = np.ones(shape, dtype=im.var_flat.dtype.type) im.meta.filename = "foo.fits" c = ModelContainer([im]) ResampleStep.call(c, blendheaders=False)
def test_skipped(): """ Test all conditions that lead to skipping wavecorr.""" hdul = create_nirspec_fs_file(grating="G140H", filter="F100LP") im = datamodels.ImageModel(hdul) # test a non-valid exp_type2transform im.meta.exposure.type = 'NRS_IMAGE' out = WavecorrStep.call(im) assert out.meta.cal_step.wavecorr == "SKIPPED" # Test an error is raised if assign_wcs or extract_2d were not run. im.meta.exposure.type = 'NRS_FIXEDSLIT' with pytest.raises(AttributeError): WavecorrStep.call(im) outa = AssignWcsStep.call(im) oute = Extract2dStep.call(outa) outs = SourceTypeStep.call(oute) # Test step is skipped if no coverage in CRDS outs.slits[0].meta.observation.date = '2001-08-03' outw = WavecorrStep.call(outs) assert out.meta.cal_step.wavecorr == "SKIPPED" outs.meta.observation.date = '2017-08-03' outw = WavecorrStep.call(outs) # Primary name not set assert out.meta.cal_step.wavecorr == "SKIPPED" outs.meta.instrument.fixed_slit = "S400A1" # Test step is skipped if meta.dither is not populated outw = WavecorrStep.call(outs) assert out.meta.cal_step.wavecorr == "SKIPPED" dither = {'x_offset': 0.0, 'y_offset': 0.0} ind = np.nonzero([s.name == 'S400A1' for s in outs.slits])[0].item() outs.slits[ind].meta.dither = dither outs.slits[ind].meta.wcsinfo.v3yangle = 138.78 outs.slits[ind].meta.wcsinfo.vparity = -1 outs.slits[ind].meta.wcsinfo.v2_ref = 321.87 outs.slits[ind].meta.wcsinfo.v3_ref = -477.94 outs.slits[ind].meta.wcsinfo.roll_ref = 15.1234 # Test step is skipped if source is "EXTENDED" outw = WavecorrStep.call(outs) assert out.meta.cal_step.wavecorr == "SKIPPED" outs.slits[ind].source_type = 'POINT' outw = WavecorrStep.call(outs) source_pos = (0.004938526981283373, -0.02795306204991911) assert_allclose((outw.slits[ind].source_xpos, outw.slits[ind].source_ypos), source_pos)
def test_miri_mrs_1A(): #ref = {} ref = { 'distortion': '', 'regions': '', 'specwcs': '', 'v2v3': '', 'wavelengthrange': '' } im = ImageModel() im.meta.instrument.name = 'MIRI' im.meta.instrument.detector = 'MIRIFUSHORT' im.meta.instrument.channel = '12' im.meta.instrument.band = 'SHORT' im.meta.exposure.type = 'MIR_MRS' step = AssignWcsStep() for reftype in refs: ref[reftype] = step.get_reference_file(im, reftype) pipeline = miri.create_pipeline(im, ref) wcsobj = wcs.WCS(pipeline) for ch in im.meta.instrument.channel: ref_data = mrs_ref_data[ch + band_mapping[im.meta.instrument.band]] #ref_data = mrs_ref_data[im.meta.instrument.channel + band_mapping[im.meta.instrument.band]] #x = np.trunc(ref_data['x']).astype(np.int) #y = np.trunc(ref_data['y']).astype(np.int) for i, s in enumerate(ref_data['s']): sl = int(ch) * 100 + s detector_to_alpha_beta = wcsobj.get_transform( 'detector', 'alpha_beta') alpha, beta, lam = detector_to_alpha_beta.set_input(sl)( ref_data['x'][i], ref_data['y'][i]) ##xan, yan, lam = wcsobj.forward_transform.set_input(sl)(ref_data['x'][i], ref_data['y'][i]) utils.assert_allclose(alpha, ref_data['alpha'][i], atol=10**-5) utils.assert_allclose(beta, ref_data['beta'][i], atol=10**-5) utils.assert_allclose(lam, ref_data['lam'][i], atol=10**-5) detector_to_xan_yan = wcsobj.get_transform('detector', 'Xan_Yan') xan, yan, lam = wcsobj(ref_data['x'], ref_data['y']) utils.assert_allclose(xan, ref_data['v2'][i], atol=10**-5) utils.assert_allclose(yan, ref_data['v3'][i], atol=10**-5) utils.assert_allclose(lam, ref_data['lam'][i], atol=10**-5)
def test_miri_ifu_wcs(self): """ Regression test of creating a WCS object and doing pixel to sky transformation. """ input_file = self.get_data(self.test_dir, 'jw00024001001_01101_00001_MIRIFUSHORT_uncal_MiriSloperPipeline.fits') ref_file = self.get_data(os.path.join(*self.ref_loc), 'jw00024001001_01101_00001_MIRIFUSHORT_assign_wcs.fits') result = AssignWcsStep.call(input_file) output_file = result.meta.filename result.save(output_file) result.close() im = ImageModel(output_file) imref = ImageModel(ref_file) # Get the region file region = RegionsModel(crds_client.get_reference_file(im, 'regions')) # inputs shape = region.regions.shape y, x = np.mgrid[ : shape[0], : shape[1]] # Get indices where pixels == 0. These should be NaNs in the output. ind_zeros = region.regions == 0 ra, dec, lam = im.meta.wcs(x, y) raref, decref, lamref = imref.meta.wcs(x, y) utils.assert_allclose(ra, raref, equal_nan=True) utils.assert_allclose(dec, decref, equal_nan=True) utils.assert_allclose(lam, lamref, equal_nan=True) # Test that we got NaNs at ind_zero assert(np.isnan(ra).nonzero()[0] == ind_zeros.nonzero()[0]).all() assert(np.isnan(ra).nonzero()[1] == ind_zeros.nonzero()[1]).all() # Test the inverse transform x1, y1 = im.meta.wcs.backward_transform(ra, dec, lam) assert(np.isnan(x1).nonzero()[0] == ind_zeros.nonzero()[0]).all() assert (np.isnan(x1).nonzero()[1] == ind_zeros.nonzero()[1]).all() # Also run a smoke test with values outside the region. dec[100][200] = -80 ra[100][200] = 7 lam[100][200] = 15 x2, y2 = im.meta.wcs.backward_transform(ra, dec, lam) assert np.isnan(x2[100][200]) assert np.isnan(x2[100][200])
def test_miri_ifu_wcs(self): """ Regression test of creating a WCS object and doing pixel to sky transformation. """ input_file = self.get_data(self.test_dir, 'jw00024001001_01101_00001_MIRIFUSHORT_uncal_MiriSloperPipeline.fits') result = AssignWcsStep.call(input_file, save_results=True) # Get the region file region = RegionsModel(crds_client.get_reference_file(result, 'regions')) # inputs x, y = grid_from_bounding_box(result.meta.wcs.bounding_box) # Get indices where pixels == 0. These should be NaNs in the output. ind_zeros = region.regions == 0 cwd = os.path.abspath('.') os.makedirs('truth', exist_ok=True) os.chdir('truth') truth_file = self.get_data(*self.ref_loc, 'jw00024001001_01101_00001_MIRIFUSHORT_assign_wcs.fits') os.chdir(cwd) truth = ImageModel(truth_file) ra, dec, lam = result.meta.wcs(x, y) raref, decref, lamref = truth.meta.wcs(x, y) assert_allclose(ra, raref, equal_nan=True) assert_allclose(dec, decref, equal_nan=True) assert_allclose(lam, lamref, equal_nan=True) # Test that we got NaNs at ind_zero assert(np.isnan(ra).nonzero()[0] == ind_zeros.nonzero()[0]).all() assert(np.isnan(ra).nonzero()[1] == ind_zeros.nonzero()[1]).all() # Test the inverse transform x1, y1 = result.meta.wcs.backward_transform(ra, dec, lam) assert(np.isnan(x1).nonzero()[0] == ind_zeros.nonzero()[0]).all() assert (np.isnan(x1).nonzero()[1] == ind_zeros.nonzero()[1]).all() # Also run a smoke test with values outside the region. dec[100][200] = -80 ra[100][200] = 7 lam[100][200] = 15 x2, y2 = result.meta.wcs.backward_transform(ra, dec, lam) assert np.isnan(x2[100][200]) assert np.isnan(x2[100][200])
def test_miri_fixed_slit_wcs(self): """ Regression test of creating a WCS object and doing pixel to sky transformation. """ input_file = self.get_data(self.test_dir, 'jw00035001001_01101_00001_mirimage_rate.fits') result = AssignWcsStep.call(input_file, save_results=True) truth_file = self.get_data(os.path.join(*self.ref_loc), 'jw00035001001_01101_00001_mirimage_assign_wcs.fits') truth = ImageModel(truth_file) x, y = grid_from_bounding_box(result.meta.wcs.bounding_box) ra, dec, lam = result.meta.wcs(x, y) raref, decref, lamref = truth.meta.wcs(x, y) assert_allclose(ra, raref) assert_allclose(dec, decref) assert_allclose(lam, lamref)
def test_miri_slitless_wcs(self): """ Regression test of creating a WCS object and doing pixel to sky transformation. """ input_file = self.get_data(self.test_dir, "jw80600012001_02101_00003_mirimage_rateints.fits") result = AssignWcsStep.call(input_file, save_results=True) cwd = os.path.abspath('.') os.makedirs('truth', exist_ok=True) os.chdir('truth') truth_file = self.get_data(*self.ref_loc, "jw80600012001_02101_00003_mirimage_assignwcsstep.fits") os.chdir(cwd) truth = CubeModel(truth_file) x, y = grid_from_bounding_box(result.meta.wcs.bounding_box) ra, dec, lam = result.meta.wcs(x, y) raref, decref, lamref = truth.meta.wcs(x, y) assert_allclose(ra, raref) assert_allclose(dec, decref) assert_allclose(lam, lamref)
def test_miri_image_wcs(self): """ Regression test of creating a WCS object and doing pixel to sky transformation. """ input_file = self.get_data(self.test_dir, "jw00001001001_01101_00001_MIRIMAGE_ramp_fit.fits") result = AssignWcsStep.call(input_file, save_results=True) cwd = os.path.abspath('.') os.makedirs('truth', exist_ok=True) os.chdir('truth') truth_file = self.get_data(*self.ref_loc, "jw00001001001_01101_00001_MIRIMAGE_assign_wcs.fits") os.chdir(cwd) truth = ImageModel(truth_file) x, y = grid_from_bounding_box(result.meta.wcs.bounding_box) ra, dec = result.meta.wcs(x, y) raref, decref = truth.meta.wcs(x, y) assert_allclose(ra, raref) assert_allclose(dec, decref)
def test_miri_image_wcs(self): """ Regression test of creating a WCS object and doing pixel to sky transformation. """ input_file = self.get_data(self.test_dir, "jw00001001001_01101_00001_MIRIMAGE_ramp_fit.fits") ref_file = self.get_data(*self.ref_loc, "jw00001001001_01101_00001_MIRIMAGE_assign_wcs.fits") result = AssignWcsStep.call(input_file) output_file = result.meta.filename result.save(output_file) result.close() im = ImageModel(output_file) imref = ImageModel(ref_file) x, y = np.mgrid[:1031, :1024] ra, dec = im.meta.wcs(x, y) raref, decref = imref.meta.wcs(x, y) utils.assert_allclose(ra, raref) utils.assert_allclose(dec, decref)
def test_miri_fixed_slit_wcs(self): """ Regression test of creating a WCS object and doing pixel to sky transformation. """ input_file = self.get_data(self.test_dir, 'jw00035001001_01101_00001_mirimage_rate.fits') ref_file = self.get_data(os.path.join(*self.ref_loc), 'jw00035001001_01101_00001_mirimage_assign_wcs.fits') result = AssignWcsStep.call(input_file) output_file = result.meta.filename result.save(output_file) result.close() im = ImageModel(output_file) imref = ImageModel(ref_file) y, x = np.mgrid[:1031, :1024] ra, dec, lam = im.meta.wcs(x, y) raref, decref, lamref = imref.meta.wcs(x, y) utils.assert_allclose(ra, raref) utils.assert_allclose(dec, decref) utils.assert_allclose(lam, lamref)
def test_miri_slitless_wcs(self): """ Regression test of creating a WCS object and doing pixel to sky transformation. """ input_file = self.get_data(self.test_dir, "jw80600012001_02101_00003_mirimage_rateints.fits") ref_file = self.get_data(*self.ref_loc, "jw80600012001_02101_00003_mirimage_assign_wcs.fits") result = AssignWcsStep.call(input_file) output_file = result.meta.filename result.save(output_file) result.close() im = CubeModel(output_file) imref = CubeModel(ref_file) x, y = np.mgrid[:1031, :1024] ra, dec, lam = im.meta.wcs(x, y) raref, decref, lamref = imref.meta.wcs(x, y) utils.assert_allclose(ra, raref) utils.assert_allclose(dec, decref) utils.assert_allclose(lam, lamref)