コード例 #1
0
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.stpipe import crds_client
    from jwst.assign_wcs import assign_wcs

    img = util.open(input)
    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
コード例 #2
0
ファイル: test_miri_ifu_wcs.py プロジェクト: gkanarek/jwst
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])
コード例 #3
0
    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'))
        # Choose the same plane as in the miri.py file (hardcoded for now).
        regions = region.regions[7, :, :]

        # 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 = 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])
コード例 #4
0
    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])
コード例 #5
0
    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])
コード例 #6
0
    def plot_ramps_pre_post_correction(self, pixel=[500, 500]):
        """
        We want to plot a test ramp pre and post dark correction and the dark itself.
        Also print the properties of the dark file to check that they match.

        Parameters
        ----------
        pixel : list
            the pixel to plot the ramp for
        """
        in_dm = datamodels.MIRIRampModel(self.pre_dark_file)
        out_dm = datamodels.MIRIRampModel(self.post_dark_file)

        nints, ngroups, nrows, ncols = in_dm.data.shape
        group = range(1, ngroups + 1)

        fig, axs = plt.subplots(2, 1, figsize=(10, 8), sharex=True)

        axs[0].plot(group,
                    in_dm.data[0, :, pixel[1], pixel[0]],
                    c='r',
                    marker='^',
                    markersize=0,
                    linestyle='-',
                    linewidth=1,
                    label='pre-dark')
        axs[0].plot(group,
                    out_dm.data[0, :, pixel[1], pixel[0]],
                    c='b',
                    marker='o',
                    markersize=0,
                    linestyle='-',
                    linewidth=1,
                    label='post-dark')
        axs[0].set_title('dark current correction', fontsize=15)
        axs[0].set_ylabel('DN', fontsize=15)
        axs[0].set_xlim(-1, max(group) + 1)
        axs[0].legend(prop={'size': 12}, loc=0)

        # get CDP
        cdp = self._get_dark_cdp()

        # get CRDS
        crds_filename = crds_client.get_reference_file(in_dm, 'dark')
        crds = datamodels.DarkMIRIModel(crds_filename)

        axs[1].plot(group,
                    in_dm.data[0, :, pixel[1], pixel[0]] -
                    out_dm.data[0, :, pixel[1], pixel[0]],
                    c='g',
                    marker='s',
                    markersize=0,
                    linestyle='-',
                    linewidth=1,
                    label='difference')
        axs[1].plot(group,
                    cdp.data[0, 0:ngroups, pixel[1], pixel[0]],
                    c='orange',
                    marker='o',
                    markersize=5,
                    linestyle='-',
                    linewidth=0,
                    label='CDP')
        axs[1].plot(group,
                    crds.data[0, 0:ngroups, pixel[1], pixel[0]],
                    c='red',
                    marker='x',
                    markersize=9,
                    linestyle='-',
                    linewidth=0,
                    label='CRDS')
        axs[1].set_ylabel('DN', fontsize=15)
        axs[1].set_xlabel('group', fontsize=15)
        axs[1].legend(prop={'size': 12}, loc=0)

        plt.tight_layout(h_pad=0)

        plot_name = os.path.join(self.output_dir, 'dark_correction.pdf')
        try:
            os.remove(plot_name)
        except:
            pass

        fig.savefig(plot_name, dpi=100)