Esempio n. 1
0
def match_to_gaia(imcat, refcat, product, output, searchrad=5.0):
    """Create a catalog with sources matched to GAIA sources
    
    Parameters
    ----------
    imcat : str or obj
        Filename or astropy.Table of source catalog written out as ECSV file
        
    refcat : str
        Filename of GAIA catalog files written out as ECSV file
        
    product : str
        Filename of drizzled product used to derive the source catalog
        
    output : str
        Rootname for matched catalog file to be written as an ECSV file 
    
    """
    if isinstance(imcat, str):
        imtab = Table.read(imcat, format='ascii.ecsv')
        imtab.rename_column('X-Center', 'x')
        imtab.rename_column('Y-Center', 'y')
    else:
        imtab = imcat
        if 'X-Center' in imtab.colnames:
            imtab.rename_column('X-Center', 'x')
            imtab.rename_column('Y-Center', 'y')
            
    
    reftab = Table.read(refcat, format='ascii.ecsv')
    
    # define WCS for matching
    tpwcs = tweakwcs.FITSWCS(HSTWCS(product, ext=1))
    
    # define matching parameters
    tpmatch = tweakwcs.TPMatch(searchrad=searchrad)
    
    # perform match
    ref_indx, im_indx = tpmatch(reftab, imtab, tpwcs)
    print('Found {} matches'.format(len(ref_indx)))
    
    # Obtain tangent plane positions for both image sources and reference sources
    im_x, im_y = tpwcs.det_to_tanp(imtab['x'][im_indx], imtab['y'][im_indx])
    ref_x, ref_y = tpwcs.world_to_tanp(reftab['RA'][ref_indx], reftab['DEC'][ref_indx])
    if 'RA' not in imtab.colnames:
        im_ra, im_dec = tpwcs.det_to_world(imtab['x'][im_indx], imtab['y'][im_indx])
    else:
        im_ra = imtab['RA'][im_indx]
        im_dec = imtab['DEC'][im_indx]
        

    # Compile match table
    match_tab = Table(data=[im_x, im_y, im_ra, im_dec, 
                            ref_x, ref_y, 
                            reftab['RA'][ref_indx], reftab['DEC'][ref_indx]],
                      names=['img_x','img_y', 'img_RA', 'img_DEC', 
                             'ref_x', 'ref_y', 'ref_RA', 'ref_DEC'])
    if not output.endswith('.ecsv'):
        output = '{}.ecsv'.format(output)                             
    match_tab.write(output, format='ascii.ecsv')
Esempio n. 2
0
    def test_generate_catalog(self,input_filenames, truth_file):
        """ Verify whether sources from astrometric catalogs can be extracted from images.

        Success Criteria
        -----------------
            * Initially, source catalog matches >80% of 'truth' catalog sources

        """
        self.input_loc = 'catalog_tests'
        self.curdir = os.getcwd()
        truth_path = [self.input_repo, self.tree, self.input_loc, *self.ref_loc]

        if not isinstance(input_filenames, list):
            input_filenames = [input_filenames]

        try:
            # Make local copies of input files
            local_files = []
            for infile in input_filenames:
                downloaded_files = self.get_input_file(infile, docopy=True)
                local_files.extend(downloaded_files)
            
            test_image = local_files[0]
            print("Testing with {}".format(test_image))
            imghdu = fits.open(test_image)
            instrume = imghdu[0].header['instrume'].lower()
            detector = imghdu[0].header['detector'].lower()
            instr_pars = detector_specific_params[instrume][detector]
            reference_wcs = amutils.build_reference_wcs(local_files)
            imcat = amutils.generate_sky_catalog(imghdu, reference_wcs, **instr_pars)
            imcat.rename_column('xcentroid', 'x')
            imcat.rename_column('ycentroid', 'y')

            # create FITS WCS corrector object
            wcs_corrector = tweakwcs.FITSWCS(reference_wcs)

            # get reference catalog as 'truth' files
            reference_catalog = get_bigdata(*truth_path, truth_file, docopy=True)
            if os.path.basename(reference_catalog).endswith('ecsv'):
                tab_format = 'ascii.ecsv'
            else:
                tab_format = 'ascii.fast_commented_header'
            reference_table = Table.read(reference_catalog, format=tab_format)
            num_expected = len(reference_table)

            # Perform matching
            match = tweakwcs.TPMatch(searchrad=200, separation=0.1, tolerance=5, use2dhist=True)
            ridx, iidx = match(reference_table, imcat, wcs_corrector)
            nmatches = len(ridx)

        except Exception:
            exc_type, exc_value, exc_tb = sys.exc_info()
            traceback.print_exception(exc_type, exc_value, exc_tb, file=sys.stdout)
            sys.exit()

        assert (nmatches > 0.8*num_expected)
def test_multichip_fitswcs_alignment():
    h1 = fits.Header.fromfile(get_pkg_data_filename('data/wfc3_uvis1.hdr'))
    w1 = wcs.WCS(h1)
    imcat1 = tweakwcs.FITSWCS(w1)
    imcat1.meta['catalog'] = table.Table.read(
        get_pkg_data_filename('data/wfc3_uvis1.cat'),
        format='ascii.csv',
        delimiter=' ',
        names=['x', 'y'])
    imcat1.meta['group_id'] = 1
    imcat1.meta['name'] = 'ext1'

    h2 = fits.Header.fromfile(get_pkg_data_filename('data/wfc3_uvis2.hdr'))
    w2 = wcs.WCS(h2)
    imcat2 = tweakwcs.FITSWCS(w2)
    imcat2.meta['catalog'] = table.Table.read(
        get_pkg_data_filename('data/wfc3_uvis2.cat'),
        format='ascii.csv',
        delimiter=' ',
        names=['x', 'y'])
    imcat2.meta['group_id'] = 1
    imcat2.meta['name'] = 'ext4'

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

    tweakwcs.align_wcs([imcat1, imcat2],
                       refcat,
                       match=_match,
                       nclip=None,
                       sigma=3,
                       fitgeom='general')

    fi1 = imcat1.meta['fit_info']
    fi2 = imcat2.meta['fit_info']

    w1 = imcat1.wcs
    w2 = imcat2.wcs

    assert np.allclose(w1.wcs.crval, (83.206917667519, -67.73275818507248),
                       rtol=0)
    assert np.allclose(
        w1.wcs.cd,
        np.array([[3.93222694902149e-06, -1.0106698270131359e-05],
                  [-1.0377001075437075e-05, -4.577945148472431e-06]]),
        atol=0.0,
        rtol=1e-8)

    assert np.allclose(w2.wcs.crval, (83.15167050722597, -67.74220306069903),
                       rtol=0)
    assert np.allclose(
        w2.wcs.cd,
        np.array([[3.834449806681195e-06, -9.996495217498745e-06],
                  [-1.0348147451241423e-05, -4.503496019301529e-06]]),
        atol=0.0,
        rtol=1e-8)

    assert np.allclose(fi1['<scale>'], 1.0025, rtol=0, atol=2e-8)
    assert np.allclose(fi2['<scale>'], 1.0025, rtol=0, atol=2e-8)

    assert fi1['rmse'] < 5e-5
    assert fi2['rmse'] < 5e-5

    cat1 = imcat1.meta['catalog']
    ra1, dec1 = w1.all_pix2world(cat1['x'], cat1['y'], 0)

    cat2 = imcat2.meta['catalog']
    ra2, dec2 = w2.all_pix2world(cat2['x'], cat2['y'], 0)

    ra = np.concatenate([ra1, ra2])
    dec = np.concatenate([dec1, dec2])

    rmse_ra = np.sqrt(np.mean((ra - refcat['RA'])**2))
    rmse_dec = np.sqrt(np.mean((dec - refcat['DEC'])**2))

    assert rmse_ra < 1e-9
    assert rmse_dec < 1e-9
def test_different_ref_tpwcs_fitswcs_alignment(wcsno, refscale, dra, ddec):
    # This test was designed to check that the results of alignment,
    # in particular and most importantly, the sky positions of sources in
    # aligned images do not depend on the tangent reference plane used
    # for alignment. [#125]
    h1 = fits.Header.fromfile(get_pkg_data_filename('data/wfc3_uvis1.hdr'))
    w1 = wcs.WCS(h1)
    imcat1 = tweakwcs.FITSWCS(w1)
    imcat1.meta['catalog'] = table.Table.read(
        get_pkg_data_filename('data/wfc3_uvis1.cat'),
        format='ascii.csv',
        delimiter=' ',
        names=['x', 'y'])
    imcat1.meta['group_id'] = 1
    imcat1.meta['name'] = 'ext1'

    h2 = fits.Header.fromfile(get_pkg_data_filename('data/wfc3_uvis2.hdr'))
    w2 = wcs.WCS(h2)
    imcat2 = tweakwcs.FITSWCS(w2)
    imcat2.meta['catalog'] = table.Table.read(
        get_pkg_data_filename('data/wfc3_uvis2.cat'),
        format='ascii.csv',
        delimiter=' ',
        names=['x', 'y'])
    imcat2.meta['group_id'] = 1
    imcat2.meta['name'] = 'ext4'

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

    refwcses = [wcs.WCS(h1), wcs.WCS(h2)]

    refwcs = refwcses[wcsno]

    # change pointing of the reference WCS (alignment tangent plane):
    refwcs.wcs.crval = refwcses[1 - wcsno].wcs.crval + np.asarray([dra, ddec])

    rotm = tweakwcs.linearfit.build_fit_matrix(*refscale)
    refwcs.wcs.cd = np.dot(refwcs.wcs.cd, rotm)
    refwcs.wcs.set()
    ref_tpwcs = tweakwcs.FITSWCS(refwcs)

    tweakwcs.align_wcs([imcat1, imcat2],
                       refcat,
                       ref_tpwcs=ref_tpwcs,
                       match=_match,
                       nclip=None,
                       sigma=3,
                       fitgeom='general')

    fi1 = imcat1.meta['fit_info']
    fi2 = imcat2.meta['fit_info']

    w1 = imcat1.wcs
    w2 = imcat2.wcs

    assert fi1['rmse'] < 1e-4
    assert fi2['rmse'] < 1e-4

    cat1 = imcat1.meta['catalog']
    ra1, dec1 = w1.all_pix2world(cat1['x'], cat1['y'], 0)

    cat2 = imcat2.meta['catalog']
    ra2, dec2 = w2.all_pix2world(cat2['x'], cat2['y'], 0)

    ra = np.concatenate([ra1, ra2])
    dec = np.concatenate([dec1, dec2])

    rmse_ra = np.sqrt(np.mean((ra - refcat['RA'])**2))
    rmse_dec = np.sqrt(np.mean((dec - refcat['DEC'])**2))

    assert rmse_ra < 5e-9
    assert rmse_dec < 5e-9