Beispiel #1
0
    def process(self, input):

        catalog_format = self.catalog_format
        kernel_fwhm = self.kernel_fwhm
        snr_threshold = self.snr_threshold

        model = ModelContainer(input)

        for image_model in model:
            catalog = make_tweakreg_catalog(image_model, kernel_fwhm,
                                            snr_threshold)
            filename = image_model.meta.filename
            self.log.info('Detected {0} sources in {1}.'.
                          format(len(catalog), filename))

            catalog_filename = filename.replace('.fits', '_cat.{0}'.
                                                format(catalog_format))
            if catalog_format == 'ecsv':
                fmt = 'ascii.ecsv'
            elif catalog_format == 'fits':
                # NOTE: The catalog must not contain any 'None' values.
                #       FITS will also not clobber existing files.
                fmt = 'fits'
            else:
                raise ValueError('catalog_format must be "ecsv" or "fits".')
            catalog.write(catalog_filename, format=fmt)
            self.log.info('Wrote source catalog: {0}'.
                          format(catalog_filename))
            image_model.meta.tweakreg_catalog.filename = catalog_filename
            image_model.catalog = catalog

        model.close()
        return model
Beispiel #2
0
def test_container_structure():
    """Test for container usage."""

    # Setup input
    inputs = [MultiSlitModel(f) for f in helpers.INPUT_FILES]
    container = ModelContainer(inputs)

    # Make the source-based containers
    outputs = multislit_to_container(container)

    # See what we got.
    assert len(container) == 3
    assert len(outputs) == 5
    for i, model in enumerate(container):
        for slit in model.slits:
            exposure = outputs[str(slit.source_id)][i]
            assert (exposure.data == slit.data).all()
            assert np.array_equal(exposure.data, slit.data)
            assert exposure.meta.filename == model.meta.filename
            assert exposure.meta.wcs.pipeline == slit.meta.wcs.pipeline

    # Closeout
    container.close()
    for model in inputs:
        model.close()
Beispiel #3
0
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)
Beispiel #4
0
    def process(self, *args):
        container = ModelContainer()
        model1 = ImageModel(args[0]).copy()
        model2 = ImageModel(args[0]).copy()
        model1.meta.filename = 'swc_model1.fits'
        model2.meta.filename = 'swc_model2.fits'
        container.append(model1)
        container.append(model2)

        return container
Beispiel #5
0
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 create_models(infiles=INFILES, plot=True):
    allmodels = []
    allmodels_wcs = []
    if plot is True:
            fig = plt.figure(figsize=(12,10))
    infiles.sort()
    for i,item in enumerate(infiles):
        if plot is True:
            # Plot dithered images
            data = fits.getdata(item)
            ax = fig.add_subplot(2,2,i+1)
            ax.set_adjustable('box-forced')
            #pos = re.search("pos\d", item)
            ax.set_title(os.path.basename(item))
            ax.imshow(data, origin="lower", vmin=-0.5, vmax=0.5)

        # Add WCS to the files
        model = image.ImageModel(item)
        allmodels.append(model)
        awcs = assign_wcs.AssignWcsStep()
        model_wcs = awcs(model)
        allmodels_wcs.append(model_wcs)
    
    if plot is True:
        figname = OUTROOT+"_dithered.png"
        plt.savefig(figname)
    model_container = ModelContainer(allmodels_wcs) 
    model_container.meta.filename = RESAMPLED_MOSAIC
    return model_container
Beispiel #7
0
def test_default_input_with_container(mk_tmp_dirs):
    """Test default input name from a ModelContainer"""

    model_path = t_path('data/flat.fits')
    with ModelContainer([model_path]) as container:
        step = StepWithModel()
        step.run(container)

        assert step._input_filename is None
Beispiel #8
0
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)
Beispiel #9
0
    def to_container(self):
        """Convert to a ModelContainer of ImageModels for each plane"""
        from jwst.datamodels import ImageModel, ModelContainer

        container = ModelContainer()
        for plane in range(self.shape[0]):
            image = ImageModel()
            for attribute in [
                    'data', 'dq', 'err', 'zeroframe', 'area', 'var_poisson',
                    'var_rnoise', 'var_flat'
            ]:
                try:
                    setattr(image, attribute,
                            self.getarray_noinit(attribute)[plane])
                except AttributeError:
                    pass
            image.update(self)
            try:
                image.meta.wcs = self.meta.wcs
            except AttributeError:
                pass
            container.append(image)
        return container
Beispiel #10
0
def container():
    warnings.simplefilter("ignore")
    asn_file_path, asn_file_name = op.split(ASN_FILE)
    with pushdir(asn_file_path):
        with ModelContainer(asn_file_name) as c:
            for m in c:
                m.meta.observation.program_number = '0001'
                m.meta.observation.observation_number = '1'
                m.meta.observation.visit_number = '1'
                m.meta.observation.visit_group = '1'
                m.meta.observation.sequence_id = '01'
                m.meta.observation.activity_id = '1'
                m.meta.observation.exposure_number = '1'
                m.meta.instrument.name = 'NIRCAM'
                m.meta.instrument.channel = 'SHORT'
        yield c
Beispiel #11
0
def test_build_interpolated_output_wcs(miri_rate_pair):
    im1, im2 = miri_rate_pair

    driz = ResampleSpecData(ModelContainer([im1, im2]))
    output_wcs = driz.build_interpolated_output_wcs()

    # Make sure that all RA, Dec values in the input image have a location in
    # the output frame
    grid = grid_from_bounding_box(im2.meta.wcs.bounding_box)
    ra, dec, lam = im2.meta.wcs(*grid)
    x, y = output_wcs.invert(ra, dec, lam)

    # This currently fails, as we see a slight offset
    # assert (x > 0).all()

    # Make sure the output slit size is larger than the input slit size
    # for this nodded data
    assert driz.data_size[1] > ra.shape[1]
Beispiel #12
0
def xxx(header):
    """
    """
    ra, dec = 53.18118642, -27.79096316
    hdu = utils.make_wcsheader(ra=ra,
                               dec=dec,
                               size=10,
                               pixscale=0.06,
                               get_hdu=True)
    out = grizli.jwst.hdu_to_imagemodel(hdu)

    from jwst.datamodels import ModelContainer, DrizProductModel

    product = DrizProductModel(out.data.shape)
    product.meta.wcs = out.meta.wcs

    from jwst.resample import gwcs_blot, gwcs_drizzle
    driz = gwcs_drizzle.GWCSDrizzle(product)  #, outwcs=out.meta.wcs)

    driz.add_image(blot_data,
                   wcs_model.meta.wcs,
                   xmax=out.data.shape[1],
                   ymax=out.data.shape[0])

    from jwst.resample import resample_utils
    from drizzle import util

    input_wcs = wcs_model.meta.wcs
    output_wcs = out.meta.wcs

    fillval = 'INDEF'
    insci = blot_data
    inwht = None
    xmin = xmax = ymin = ymax = 0
    uniqid = 1
    outsci = driz.outsci * 1
    outwht = driz.outwht * 1
    outcon = driz.outcon * 1
    in_units = 'cps'

    from jwst.resample import resample

    groups = ModelContainer([wcs_model])
    sampler = resample.ResampleData(groups, output=driz)
Beispiel #13
0
def data_model_list():
    pytest.importorskip("jwst")
    from jwst.datamodels import DataModel, ModelContainer

    models = []
    for k in range(6):
        m = DataModel()
        m.meta.observation.program_number = '0001'
        m.meta.observation.observation_number = '1'
        m.meta.observation.visit_number = '1'
        m.meta.observation.visit_group = '1'
        m.meta.observation.sequence_id = '01'
        m.meta.observation.activity_id = '1'
        m.meta.observation.exposure_number = '1'
        m.meta.instrument.name = 'NIRCAM'
        m.meta.instrument.channel = 'SHORT'
        m.meta.filename = 'file{:d}.fits'.format(k)
        models.append(m)

    models[-3].meta.observation.observation_number = '2'
    models[-2].meta.observation.observation_number = '3'
    models[-1].meta.observation.observation_number = '3'

    return ModelContainer(models)
Beispiel #14
0
def test_multichip_alignment_step(monkeypatch):
    monkeypatch.setattr(tweakreg_step, 'align_wcs', _align_wcs)
    monkeypatch.setattr(tweakreg_step, 'make_tweakreg_catalog',
                        _make_tweakreg_catalog)

    # image 1
    w1 = _make_gwcs_wcs('data/wfc3_uvis1.hdr')

    m1 = ImageModel(np.zeros((100, 100)))
    m1.meta.filename = 'ext1'
    m1.meta.observation.observation_number = '1'
    m1.meta.observation.program_number = '1'
    m1.meta.observation.visit_number = '1'
    m1.meta.observation.visit_group = '1'
    m1.meta.observation.sequence_id = '1'
    m1.meta.observation.activity_id = '1'
    m1.meta.observation.exposure_number = '1'

    m1.meta.wcsinfo.v2_ref = 0
    m1.meta.wcsinfo.v3_ref = 0
    m1.meta.wcsinfo.roll_ref = 0
    m1.meta.wcs = w1

    imcat1 = table.Table.read(get_pkg_data_filename('data/wfc3_uvis1.cat'),
                              format='ascii.csv',
                              delimiter=' ',
                              names=['x', 'y'])
    imcat1['x'] += 1
    imcat1['y'] += 1
    m1.tweakreg_catalog = imcat1

    # image 2
    w2 = _make_gwcs_wcs('data/wfc3_uvis2.hdr')

    m2 = ImageModel(np.zeros((100, 100)))
    m2.meta.filename = 'ext4'

    m2.meta.observation.observation_number = '1'
    m2.meta.observation.program_number = '1'
    m2.meta.observation.visit_number = '1'
    m2.meta.observation.visit_group = '1'
    m2.meta.observation.sequence_id = '1'
    m2.meta.observation.activity_id = '1'
    m2.meta.observation.exposure_number = '1'

    m2.meta.wcsinfo.v2_ref = 0
    m2.meta.wcsinfo.v3_ref = 0
    m2.meta.wcsinfo.roll_ref = 0
    m2.meta.wcs = w2

    imcat2 = table.Table.read(get_pkg_data_filename('data/wfc3_uvis2.cat'),
                              format='ascii.csv',
                              delimiter=' ',
                              names=['x', 'y'])
    imcat2['x'] += 1
    imcat2['y'] += 1
    m2.tweakreg_catalog = imcat2

    # refcat
    wr = _make_reference_gwcs_wcs('data/wfc3_uvis1.hdr')

    mr = ImageModel(np.zeros((100, 100)))
    mr.meta.filename = 'refcat'
    mr.meta.observation.observation_number = '0'
    mr.meta.observation.program_number = '0'
    mr.meta.observation.visit_number = '0'
    mr.meta.observation.visit_group = '0'
    mr.meta.observation.sequence_id = '0'
    mr.meta.observation.activity_id = '0'
    mr.meta.observation.exposure_number = '0'

    mr.meta.wcsinfo.v2_ref = 0
    mr.meta.wcsinfo.v3_ref = 0
    mr.meta.wcsinfo.roll_ref = 0
    mr.meta.wcs = wr

    refcat = table.Table.read(get_pkg_data_filename('data/ref.cat'),
                              format='ascii.csv',
                              delimiter=' ',
                              names=['RA', 'DEC'])
    x, y = wr.world_to_pixel(refcat['RA'], refcat['DEC'])
    refcat['x'] = x
    refcat['y'] = y
    mr.tweakreg_catalog = refcat

    # update bounding box of the reference WCS to include all test sources:
    mr.meta.wcs.bounding_box = ((x.min() - 0.5, x.max() + 0.5),
                                (y.min() - 0.5, y.max() + 0.5))

    mc = ModelContainer([mr, m1, m2])
    mc.models_grouped

    step = tweakreg_step.TweakRegStep()
    step.fitgeometry = 'general'
    step.nclip = 0
    # Increase matching tolerance to pass 'fit_quality_is_good' test.
    # This test would detect large corrections and therefore
    # would flag the quality of the fit as "bad" and therefore, it will not
    # apply computed corrections ('fit_quality_is_good' test was designed by
    # Warren for evaluating "quality of fit" for HAP).
    step.tolerance = 2
    # Alternatively, disable this 'fit_quality_is_good' test:
    # step.fit_quality_is_good = lambda x, y: True

    mr, m1, m2 = step.process(mc)

    wc1 = m1.meta.wcs
    wc2 = m2.meta.wcs

    ra1, dec1 = wc1(imcat1['x'], imcat1['y'])
    ra2, dec2 = wc2(imcat2['x'], imcat2['y'])
    ra = np.concatenate([ra1, ra2])
    dec = np.concatenate([dec1, dec2])
    rra = refcat['RA']
    rdec = refcat['DEC']
    rmse_ra = np.sqrt(np.mean((ra - rra)**2))
    rmse_dec = np.sqrt(np.mean((dec - rdec)**2))

    assert rmse_ra < _REF_RMSE_RA
    assert rmse_dec < _REF_RMSE_DEC
Beispiel #15
0
def run_multislit_to_container():
    inputs = ModelContainer([MultiSlitModel(f) for f in helpers.INPUT_FILES])
    outputs = multislit_to_container(inputs)
    return inputs, outputs