def test_ff_inv(rtdata, fitsdiff_default_kwargs): """Test flat field inversion""" with dm.open(rtdata.get_data('nirspec/imaging/usf_assign_wcs.fits')) as data: flatted = FlatFieldStep.call(data) unflatted = FlatFieldStep.call(flatted, inverse=True) assert np.allclose(data.data, unflatted.data), 'Inversion failed'
def test_ff_inv(rtdata, fitsdiff_default_kwargs): """Test flat field inversion""" data = dm.open(rtdata.get_data('nirspec/ifu/nrs_ifu_nrs1_assign_wcs.fits')) flatted = FlatFieldStep.call(data) unflatted = FlatFieldStep.call(flatted, inverse=True) assert np.allclose(data.data, unflatted.data), 'Inversion failed'
def test_ff_inv(rtdata, fitsdiff_default_kwargs): """Test flat field inversion""" with dm.open(rtdata.get_data('nirspec/mos/usf_wavecorr.fits')) as data: flatted = FlatFieldStep.call(data) unflatted = FlatFieldStep.call(flatted, inverse=True) bad_slits = [] for idx, slits in enumerate(zip(data.slits, unflatted.slits)): data_slit, unflatted_slit = slits if not np.allclose(data_slit.data, unflatted_slit.data): bad_slits.append(idx) assert not bad_slits, f'Inversion failed for slits {bad_slits}'
def test_flat_field_bots_interp_flat(rtdata, fitsdiff_default_kwargs): """Test the interpolated flat for a NRS BOTS exposure""" data = rtdata.get_data( 'nirspec/tso/jw93056001001_short_nrs1_wavecorr.fits') FlatFieldStep.call(data, save_interpolated_flat=True) rtdata.output = 'jw93056001001_short_nrs1_wavecorr_interpolatedflat.fits' rtdata.get_truth( 'truth/test_nirspec_brightobj_spec2/jw93056001001_short_nrs1_wavecorr_interpolatedflat.fits' ) diff = FITSDiff(rtdata.output, rtdata.truth, **fitsdiff_default_kwargs) assert diff.identical, diff.report()
def test_flatfield_step_interface(instrument, exptype): """Test that the basic inferface works for data requiring a FLAT reffile""" shape = (20, 20) data = datamodels.ImageModel(shape) data.meta.instrument.name = instrument data.meta.exposure.type = exptype data.meta.subarray.xstart = 1 data.meta.subarray.ystart = 1 data.meta.subarray.xsize = shape[1] data.meta.subarray.ysize = shape[0] flat = datamodels.FlatModel(shape) flat.meta.instrument.name = instrument flat.meta.subarray.xstart = 1 flat.meta.subarray.ystart = 1 flat.meta.subarray.xsize = shape[1] flat.meta.subarray.ysize = shape[0] flat.data += 1 flat.data[0, 0] = np.nan flat.err = np.random.random(shape) * 0.05 # override class attribute so only the `flat` type needs to be overriden # in the step call. Otherwise CRDS calls will be made for the other 3 # types of flat reference file not used in this test. FlatFieldStep.reference_file_types = ["flat"] result = FlatFieldStep.call(data, override_flat=flat) assert (result.data == data.data).all() assert result.var_flat.shape == shape assert result.meta.cal_step.flat_field == 'COMPLETE'
def test_nirspec_flatfield_step_interface(exptype): """Test that the interface works all NIRSpec types""" shape = (20, 20) data = datamodels.ImageModel(shape) data.meta.observation.date = "2019-01-01" data.meta.observation.time = "00:00:00" data.meta.instrument.name = "NIRSPEC" data.meta.instrument.detector = "NRS1" data.meta.instrument.filter = "CLEAR" data.meta.instrument.grating = "MIRROR" data.meta.exposure.type = exptype data.meta.subarray.xstart = 1 data.meta.subarray.ystart = 1 data.meta.subarray.xsize = shape[1] data.meta.subarray.ysize = shape[0] FlatFieldStep.call(data)
def test_flat_cases(cases, reffile): '''Test to check flat field step for different files.''' # get input data with fits.open(cases) as hduraw: in_data = hduraw['SCI'].data in_err = hduraw['ERR'].data in_dq = hduraw['DQ'].data[500, 500] # call flat field step outname = cases[:-5] + '_flat_field.fits' if reffile is not "None": print(reffile) default = FlatFieldStep.call(cases, override_flat=reffile, output_file=outname) else: print(reffile) default = FlatFieldStep.call(cases, output_file=outname) # grab reference file with fits.open(outname) as hduout: hdr = hduout['PRIMARY'].header['R_FLAT'] print('header', hdr) jwst = np.int(hdr.find('jwst')) print('jwst', jwst) if hdr[:4] == 'crds': reffile = '/grp/crds/cache/references/jwst/' + hdr[jwst:] else: reffile = hdr print(reffile) # get output data -- check SCI, ERR, and DQ extensions ref_data = fits.getdata(reffile, 1) out_data = hduout['SCI'].data out_err = hduout['ERR'].data out_dq = hduout['DQ'].data[500, 500] # SCI data should be SCI/REF, ERR data should be ERR/ref, DQout == DQin assert np.array_equal(out_data, in_data / ref_data) == True assert np.array_equal(out_err, in_err / ref_data) == True assert in_dq == out_dq
def test_flat_field_step_user_supplied_flat(rtdata, fitsdiff_default_kwargs): """Test providing a user-supplied flat field to the FlatFieldStep""" data = rtdata.get_data('nirspec/imaging/usf_assign_wcs.fits') user_supplied_flat = rtdata.get_data('nirspec/imaging/usf_flat.fits') data_flat_fielded = FlatFieldStep.call(data, user_supplied_flat=user_supplied_flat) rtdata.output = 'flat_fielded_step_user_supplied.fits' data_flat_fielded.write(rtdata.output) rtdata.get_truth('truth/test_nirspec_image2/flat_fielded_step_user_supplied.fits') diff = FITSDiff(rtdata.output, rtdata.truth, **fitsdiff_default_kwargs) assert diff.identical, diff.report()
def test_flat_field_step_user_supplied_flat(rtdata, fitsdiff_default_kwargs): """Test providing a user-supplied flat field to the FlatFieldStep""" data = rtdata.get_data('nirspec/ifu/nrs_ifu_nrs1_assign_wcs.fits') user_supplied_flat = rtdata.get_data('nirspec/ifu/nrs_ifu_nrs1_interpolated_flat.fits') data_flat_fielded = FlatFieldStep.call(data, user_supplied_flat=user_supplied_flat) rtdata.output = 'flat_fielded_step_user_supplied.fits' data_flat_fielded.write(rtdata.output) rtdata.get_truth(TRUTH_PATH + '/' + 'flat_fielded_step_user_supplied.fits') diff = FITSDiff(rtdata.output, rtdata.truth, **fitsdiff_default_kwargs) assert diff.identical, diff.report()
def test_flat_field_step_user_supplied_flat(rtdata, fitsdiff_default_kwargs): """Test providing a user-supplied flat field to the FlatFieldStep""" data = rtdata.get_data('nirspec/tso/nrs2_wavecorr.fits') user_supplied_flat = rtdata.get_data('nirspec/tso/nrs2_interpolatedflat.fits') data_flat_fielded = FlatFieldStep.call(data, user_supplied_flat=user_supplied_flat) rtdata.output = 'flat_fielded_step_user_supplied.fits' data_flat_fielded.write(rtdata.output) rtdata.get_truth('truth/test_nirspec_brightobj_spec2/flat_fielded_step_user_supplied.fits') diff = FITSDiff(rtdata.output, rtdata.truth, **fitsdiff_default_kwargs) assert diff.identical, diff.report()
def test_fake_data(): '''Test flat field step with fake data.''' # open ramp to get data shape and headers, fill it with fake data with fits.open('nrca1_47Tuc_subpix_dither1_newpos_rate.fits') as hduraw: nrows = 2048 ncols = 2048 new_data = np.zeros((nrows, ncols), dtype=np.float32) new_data[:, :] = 50 hduraw['SCI'].data = new_data hduraw['ERR'].data = new_data / 10. hduraw['DQ'].data[500, 500] = 50 hduraw.writeto("fake_test.fits", overwrite=True) # call flat field step FlatFieldStep.call('fake_test.fits', output_file='fake_test_flat.fits') # grab reference file with fits.open('fake_test_flat.fits') as hduout: hdr = hduout['PRIMARY'].header['R_FLAT'] print('header', hdr) jwst = np.int(hdr.find('jwst')) print('jwst', jwst) if hdr[:4] == 'crds': reffile = '/grp/crds/cache/references/jwst/' + hdr[jwst:] else: reffile = hdr print(reffile) # get output data -- check SCI, ERR, and DQ extensions refdata = fits.getdata(reffile, 1) scidata = hduout['SCI'].data errdata = hduout['ERR'].data dqdata = hduout['DQ'].data # SCI data should be SCI/REF, ERR data should be ERR/ref, DQout == DQin assert np.array_equal(scidata, hduraw['SCI'].data / refdata) == True assert np.array_equal(errdata, hduraw['ERR'].data / refdata) == True assert dqdata[500, 500] == hduraw['DQ'].data[500, 500]
def test_flat_field_step_user_supplied_flat(jail, rtdata_module, fitsdiff_default_kwargs): """Test providing a user-supplied flat field to the FlatFieldStep""" rtdata = rtdata_module data = rtdata.get_data('nirspec/mos/usf_wavecorr.fits') user_supplied_flat = rtdata.get_data('nirspec/mos/usf_flat.fits') data_flat_fielded = FlatFieldStep.call( data, user_supplied_flat=user_supplied_flat) rtdata.output = 'flat_fielded_step_user_supplied.fits' data_flat_fielded.write(rtdata.output) rtdata.get_truth( 'truth/test_nirspec_mos_spec2/flat_fielded_step_user_supplied.fits') diff = FITSDiff(rtdata.output, rtdata.truth, **fitsdiff_default_kwargs) assert diff.identical, diff.report()