コード例 #1
0
ファイル: test_vizier.py プロジェクト: cdeil/astroquery
def test_simple():
    query = {}
    query["-source"] = "VII/258/vv10"
    query["-out"] = ["Name", "Sp", "Vmag"]
    query["Vmag"] = "5.0..11.0"
    table1 = vizier.vizquery(query)
    
    #Find sources in 2MASS matching the AGNs positions to within 2 arcsec
    query = {}
    query["-source"] = "II/246/out"
    query["-out"] = ["RAJ2000", "DEJ2000", "2MASS", "Kmag"]
    query["-c.rs"] = "2"
    query["-c"] = table1
    table2 = vizier.vizquery(query)
    
    print(table1)
    print(table2)
コード例 #2
0
ファイル: shifts.py プロジェクト: themiyan/threedhst
def align_to_reference(ROOT_DIRECT,
                       ALIGN_IMAGE,
                       fitgeometry="shift",
                       clean=True,
                       verbose=False,
                       ALIGN_EXTENSION=0,
                       toler=3,
                       skip_swarp=False,
                       align_sdss_ds9=False,
                       catalog=None):
    """
xshift, yshift, rot, scale, xrms, yrms = align_to_reference()
    """
    import os
    import glob
    import shutil

    from pyraf import iraf
    from iraf import stsdas, dither

    import threedhst
    from threedhst import catIO

    no = iraf.no
    yes = iraf.yes
    INDEF = iraf.INDEF

    #### Clean slate
    rmfiles = [
        'SCI.fits', 'WHT.fits', 'align.cat', 'direct.cat'
        'align.map', 'align.match', 'align.reg', 'align.xy', 'direct.reg',
        'direct.xy', 'ds9_align.tsv'
    ]

    for file in rmfiles:
        try:
            os.remove(file)
        except:
            pass

    if catalog is not None:
        align_sdss_ds9 = True

    #### Get only images that overlap from the ALIGN_IMAGE list
    if not align_sdss_ds9:
        align_img_list = find_align_images_that_overlap(
            ROOT_DIRECT + '_drz.fits',
            ALIGN_IMAGE,
            ALIGN_EXTENSION=ALIGN_EXTENSION)
        if not align_img_list:
            print 'threedhst.shifts.align_to_reference: no alignment images overlap.'
            return 0, 0

    #### Use swarp to combine the alignment images to the same image
    #### dimensions as the direct mosaic
    if (not skip_swarp) & (not align_sdss_ds9):
        try:
            os.remove(ROOT_DIRECT + '_align.fits')
        except:
            pass
        matchImagePixels(input=align_img_list,
                         matchImage=ROOT_DIRECT + '_drz.fits',
                         output=ROOT_DIRECT + '_align.fits',
                         match_extension=1,
                         input_extension=ALIGN_EXTENSION)

    #### Run SExtractor on the direct image, with the WHT
    #### extension as a weight image
    se = threedhst.sex.SExtractor()
    se.aXeParams()
    se.copyConvFile()
    se.overwrite = True
    se.options['CHECKIMAGE_TYPE'] = 'NONE'
    se.options['WEIGHT_TYPE'] = 'MAP_WEIGHT'
    se.options['WEIGHT_IMAGE'] = 'WHT.fits'
    se.options['FILTER'] = 'Y'
    ## Detect thresholds (default = 1.5)
    THRESH = 10
    if align_sdss_ds9:
        if 'Vizier' not in REFERENCE_CATALOG:
            THRESH = 20

    se.options['DETECT_THRESH'] = '%d' % (THRESH)
    se.options['ANALYSIS_THRESH'] = '%d' % (THRESH)
    se.options['MAG_ZEROPOINT'] = str(threedhst.options['MAG_ZEROPOINT'])

    #### Run SExtractor on direct and alignment images
    ## direct image
    se.options['CATALOG_NAME'] = 'direct.cat'
    iraf.imcopy(ROOT_DIRECT + '_drz.fits[1]', "SCI.fits")
    iraf.imcopy(ROOT_DIRECT + '_drz.fits[2]', "WHT.fits")
    status = se.sextractImage('SCI.fits')

    ## Read the catalog
    directCat = threedhst.sex.mySexCat('direct.cat')

    if align_sdss_ds9:
        ### Use ds9 SDSS catalog to refine alignment
        import threedhst.dq
        import pywcs
        import threedhst.catIO as catIO

        wcs = pywcs.WCS(pyfits.getheader('SCI.fits', 0))
        #wcs = pywcs.WCS(pyfits.getheader('Q0821+3107-F140W_drz.fits', 1))

        if 'Vizier' in REFERENCE_CATALOG:
            #### Use (unstable) astroquery Vizier search
            #### CFHTLS-Deep: 'Vizier.II/317'
            VIZIER_CAT = REFERENCE_CATALOG.split('Vizier.')[1]
            print 'Align to Vizier catalog: http://vizier.u-strasbg.fr/viz-bin/VizieR?-source=%s' % (
                VIZIER_CAT)

            import astroquery
            if astroquery.__version__ < '0.0.dev1078':
                from astroquery import vizier

                query = {}
                query["-source"] = VIZIER_CAT
                #query["-out"] = ["_r", "CFHTLS", "rmag"]
                query["-out"] = ["_RAJ2000", "_DEJ2000"]  ### Just RA/Dec.

                #### Center position and query radius
                r0, d0 = wcs.wcs_pix2sky([[wcs.naxis1 / 2., wcs.naxis2 / 2.]],
                                         1)[0]
                rll, dll = wcs.wcs_pix2sky([[0, 0]], 1)[0]
                corner_radius = np.sqrt(
                    (r0 - rll)**2 * np.cos(d0 / 360. * 2 * np.pi)**2 +
                    (d0 - dll)**2) * 60. * 1.5
                h = query["-c"] = "%.6f %.6f" % (r0, d0)
                query["-c.rm"] = "%.3f" % (corner_radius
                                           )  ### xxx check image size

                #### Run the query
                vt = vizier.vizquery(query)
            else:
                #### Newer astroquery
                from astroquery.vizier import Vizier
                import astropy.coordinates as coord
                import astropy.units as u

                Vizier.ROW_LIMIT = -1

                r0, d0 = wcs.wcs_pix2sky([[wcs.naxis1 / 2., wcs.naxis2 / 2.]],
                                         1)[0]
                rll, dll = wcs.wcs_pix2sky([[0, 0]], 1)[0]
                corner_radius = np.sqrt(
                    (r0 - rll)**2 * np.cos(d0 / 360. * 2 * np.pi)**2 +
                    (d0 - dll)**2) * 60. * 1.5
                #
                c = coord.ICRSCoordinates(ra=r0, dec=d0, unit=(u.deg, u.deg))
                #### something with astropy.coordinates
                # c.icrs.ra.degree = c.icrs.ra.degrees
                # c.icrs.dec.degree = c.icrs.dec.degrees
                #
                vt = Vizier.query_region(c,
                                         width=u.Quantity(
                                             corner_radius, u.arcminute),
                                         catalog=[VIZIER_CAT])[0]

            #### Make a region file
            ra_list, dec_list = vt['RAJ2000'], vt['DEJ2000']
            print 'Vizier, found %d objects.' % (len(ra_list))
            fp = open('%s.vizier.reg' % (ROOT_DIRECT), 'w')
            fp.write('# %s, r=%.1f\'\nfk5\n' % (VIZIER_CAT, corner_radius))
            for ra, dec in zip(ra_list, dec_list):
                fp.write('circle(%.6f, %.6f, 0.5")\n' % (ra, dec))
            #
            fp.close()
        else:
            #### Use DS9 catalog
            ds9 = threedhst.dq.myDS9()
            ds9.set('file SCI.fits')
            #ds9.set('file Q0821+3107-F140W_drz.fits')
            ds9.set('catalog %s' % (REFERENCE_CATALOG))
            ### Can't find XPA access point for "copy to regions"
            ds9.set('catalog export tsv ds9_align.tsv')
            lines = open('ds9_align.tsv').readlines()
            ra_list, dec_list = [], []
            for line in lines[1:]:
                spl = line.split()
                ra, dec = float(spl[0]), float(spl[1])
                ra_list.append(ra)
                dec_list.append(dec)
            #
            del (ds9)

        x_image, y_image = [], []
        for ra, dec in zip(ra_list, dec_list):
            x, y = wcs.wcs_sky2pix([[ra, dec]], 1)[0]
            x_image.append(x)
            y_image.append(y)

        alignCat = catIO.EmptyCat()
        alignCat['X_IMAGE'] = np.array(x_image)
        alignCat['Y_IMAGE'] = np.array(y_image)

    else:
        ## alignment image
        se.options['CATALOG_NAME'] = 'align.cat'
        status = se.sextractImage(ROOT_DIRECT + '_align.fits')
        alignCat = threedhst.sex.mySexCat('align.cat')

    xshift = 0
    yshift = 0
    rot = 0
    scale = 1.

    xrms = 2
    yrms = 2

    NITER = 5
    IT = 0
    while (IT < NITER):
        IT = IT + 1

        #### Get x,y coordinates of detected objects
        ## direct image
        fp = open('direct.xy', 'w')
        for i in range(len(directCat.X_IMAGE)):
            fp.write('%s  %s\n' % (directCat.X_IMAGE[i], directCat.Y_IMAGE[i]))
        fp.close()

        ## alignment image
        fp = open('align.xy', 'w')
        for i in range(len(alignCat.X_IMAGE)):
            fp.write('%s  %s\n' % (np.float(alignCat.X_IMAGE[i]) + xshift,
                                   np.float(alignCat.Y_IMAGE[i]) + yshift))
        fp.close()

        iraf.flpr()
        iraf.flpr()
        iraf.flpr()
        #### iraf.xyxymatch to find matches between the two catalogs
        pow = toler * 1.
        try:
            os.remove('align.match')
        except:
            pass

        status1 = iraf.xyxymatch(input="direct.xy",
                                 reference="align.xy",
                                 output="align.match",
                                 tolerance=2**pow,
                                 separation=0,
                                 verbose=yes,
                                 Stdout=1)

        nmatch = 0
        while status1[-1].startswith('0') | (nmatch < 10):
            pow += 1
            os.remove('align.match')
            status1 = iraf.xyxymatch(input="direct.xy",
                                     reference="align.xy",
                                     output="align.match",
                                     tolerance=2**pow,
                                     separation=0,
                                     verbose=yes,
                                     Stdout=1)
            #
            nmatch = 0
            for line in open('align.match').xreadlines():
                nmatch += 1

        if verbose:
            for line in status1:
                print line

        #### Compute shifts with iraf.geomap
        iraf.flpr()
        iraf.flpr()
        iraf.flpr()
        try:
            os.remove("align.map")
        except:
            pass

        status2 = iraf.geomap(input="align.match",
                              database="align.map",
                              fitgeometry=fitgeometry,
                              interactive=no,
                              xmin=INDEF,
                              xmax=INDEF,
                              ymin=INDEF,
                              ymax=INDEF,
                              maxiter=10,
                              reject=2.0,
                              Stdout=1)
        if verbose:
            for line in status2:
                print line

        #fp = open(root+'.iraf.log','a')
        #fp.writelines(status1)
        #fp.writelines(status2)
        #fp.close()

        #### Parse geomap.output
        fp = open("align.map", "r")
        for line in fp.readlines():
            spl = line.split()
            if spl[0].startswith('xshift'):
                xshift += float(spl[1])
            if spl[0].startswith('yshift'):
                yshift += float(spl[1])
            if spl[0].startswith('xrotation'):
                rot = float(spl[1])
            if spl[0].startswith('xmag'):
                scale = float(spl[1])
            if spl[0].startswith('xrms'):
                xrms = float(spl[1])
            if spl[0].startswith('yrms'):
                yrms = float(spl[1])

        fp.close()

        #os.system('wc align.match')
        print 'Shift iteration #%d, xshift=%f, yshift=%f, rot=%f, scl=%f (rms: %5.2f,%5.2f)' % (
            IT, xshift, yshift, rot, scale, xrms, yrms)

    im = pyfits.open('SCI.fits')

    shutil.copy('align.map', ROOT_DIRECT + '_align.map')
    shutil.copy('align.match', ROOT_DIRECT + '_align.match')

    #### Cleanup
    if clean:
        rmfiles = [
            'SCI.fits', 'WHT.fits', 'align.cat', 'align.map', 'align.match',
            'align.reg', 'align.xy', 'direct.cat', 'direct.reg', 'direct.xy',
            'drz_sci.fits', 'drz_wht.fits', 'bg.fits'
        ]

        for file in rmfiles:
            try:
                os.remove(file)
            except:
                pass

    return xshift, yshift, rot, scale, xrms, yrms
コード例 #3
0
ファイル: shifts.py プロジェクト: kewhitaker/threedhst
def align_to_reference(
    ROOT_DIRECT,
    ALIGN_IMAGE,
    fitgeometry="shift",
    clean=True,
    verbose=False,
    ALIGN_EXTENSION=0,
    toler=3,
    skip_swarp=False,
    align_sdss_ds9=False,
    catalog=None,
):
    """
xshift, yshift, rot, scale, xrms, yrms = align_to_reference()
    """
    import os
    import glob
    import shutil

    from pyraf import iraf
    from iraf import stsdas, dither

    import threedhst
    from threedhst import catIO

    no = iraf.no
    yes = iraf.yes
    INDEF = iraf.INDEF

    #### Clean slate
    rmfiles = [
        "SCI.fits",
        "WHT.fits",
        "align.cat",
        "direct.cat" "align.map",
        "align.match",
        "align.reg",
        "align.xy",
        "direct.reg",
        "direct.xy",
        "ds9_align.tsv",
    ]

    for file in rmfiles:
        try:
            os.remove(file)
        except:
            pass

    if catalog is not None:
        align_sdss_ds9 = True

    #### Get only images that overlap from the ALIGN_IMAGE list
    if not align_sdss_ds9:
        align_img_list = find_align_images_that_overlap(
            ROOT_DIRECT + "_drz.fits", ALIGN_IMAGE, ALIGN_EXTENSION=ALIGN_EXTENSION
        )
        if not align_img_list:
            print "threedhst.shifts.align_to_reference: no alignment images overlap."
            return 0, 0

    #### Use swarp to combine the alignment images to the same image
    #### dimensions as the direct mosaic
    if (not skip_swarp) & (not align_sdss_ds9):
        try:
            os.remove(ROOT_DIRECT + "_align.fits")
        except:
            pass
        matchImagePixels(
            input=align_img_list,
            matchImage=ROOT_DIRECT + "_drz.fits",
            output=ROOT_DIRECT + "_align.fits",
            match_extension=1,
            input_extension=ALIGN_EXTENSION,
        )

    #### Run SExtractor on the direct image, with the WHT
    #### extension as a weight image
    se = threedhst.sex.SExtractor()
    se.aXeParams()
    se.copyConvFile()
    se.overwrite = True
    se.options["CHECKIMAGE_TYPE"] = "NONE"
    se.options["WEIGHT_TYPE"] = "MAP_WEIGHT"
    se.options["WEIGHT_IMAGE"] = "WHT.fits"
    se.options["FILTER"] = "Y"
    ## Detect thresholds (default = 1.5)
    THRESH = 10
    if align_sdss_ds9:
        if "Vizier" not in REFERENCE_CATALOG:
            THRESH = 20

    se.options["DETECT_THRESH"] = "%d" % (THRESH)
    se.options["ANALYSIS_THRESH"] = "%d" % (THRESH)
    se.options["MAG_ZEROPOINT"] = str(threedhst.options["MAG_ZEROPOINT"])

    #### Run SExtractor on direct and alignment images
    ## direct image
    se.options["CATALOG_NAME"] = "direct.cat"
    iraf.imcopy(ROOT_DIRECT + "_drz.fits[1]", "SCI.fits")
    iraf.imcopy(ROOT_DIRECT + "_drz.fits[2]", "WHT.fits")
    status = se.sextractImage("SCI.fits")

    ## Read the catalog
    directCat = threedhst.sex.mySexCat("direct.cat")

    if align_sdss_ds9:
        ### Use ds9 SDSS catalog to refine alignment
        import threedhst.dq
        import pywcs
        import threedhst.catIO as catIO

        wcs = pywcs.WCS(pyfits.getheader("SCI.fits", 0))
        # wcs = pywcs.WCS(pyfits.getheader('Q0821+3107-F140W_drz.fits', 1))

        if "Vizier" in REFERENCE_CATALOG:
            #### Use (unstable) astroquery Vizier search
            #### CFHTLS-Deep: 'Vizier.II/317'
            VIZIER_CAT = REFERENCE_CATALOG.split("Vizier.")[1]
            print "Align to Vizier catalog: http://vizier.u-strasbg.fr/viz-bin/VizieR?-source=%s" % (VIZIER_CAT)

            import astroquery

            if astroquery.__version__ < "0.0.dev1078":
                from astroquery import vizier

                query = {}
                query["-source"] = VIZIER_CAT
                # query["-out"] = ["_r", "CFHTLS", "rmag"]
                query["-out"] = ["_RAJ2000", "_DEJ2000"]  ### Just RA/Dec.

                #### Center position and query radius
                r0, d0 = wcs.wcs_pix2sky([[wcs.naxis1 / 2.0, wcs.naxis2 / 2.0]], 1)[0]
                rll, dll = wcs.wcs_pix2sky([[0, 0]], 1)[0]
                corner_radius = (
                    np.sqrt((r0 - rll) ** 2 * np.cos(d0 / 360.0 * 2 * np.pi) ** 2 + (d0 - dll) ** 2) * 60.0 * 1.5
                )
                h = query["-c"] = "%.6f %.6f" % (r0, d0)
                query["-c.rm"] = "%.3f" % (corner_radius)  ### xxx check image size

                #### Run the query
                vt = vizier.vizquery(query)
            else:
                #### Newer astroquery
                from astroquery.vizier import Vizier
                import astropy.coordinates as coord
                import astropy.units as u

                Vizier.ROW_LIMIT = -1

                r0, d0 = wcs.wcs_pix2sky([[wcs.naxis1 / 2.0, wcs.naxis2 / 2.0]], 1)[0]
                rll, dll = wcs.wcs_pix2sky([[0, 0]], 1)[0]
                corner_radius = (
                    np.sqrt((r0 - rll) ** 2 * np.cos(d0 / 360.0 * 2 * np.pi) ** 2 + (d0 - dll) ** 2) * 60.0 * 1.5
                )
                #
                c = coord.ICRSCoordinates(ra=r0, dec=d0, unit=(u.deg, u.deg))
                #### something with astropy.coordinates
                # c.icrs.ra.degree = c.icrs.ra.degrees
                # c.icrs.dec.degree = c.icrs.dec.degrees
                #
                vt = Vizier.query_region(c, width=u.Quantity(corner_radius, u.arcminute), catalog=[VIZIER_CAT])[0]

            #### Make a region file
            ra_list, dec_list = vt["RAJ2000"], vt["DEJ2000"]
            print "Vizier, found %d objects." % (len(ra_list))
            fp = open("%s.vizier.reg" % (ROOT_DIRECT), "w")
            fp.write("# %s, r=%.1f'\nfk5\n" % (VIZIER_CAT, corner_radius))
            for ra, dec in zip(ra_list, dec_list):
                fp.write('circle(%.6f, %.6f, 0.5")\n' % (ra, dec))
            #
            fp.close()
        else:
            #### Use DS9 catalog
            ds9 = threedhst.dq.myDS9()
            ds9.set("file SCI.fits")
            # ds9.set('file Q0821+3107-F140W_drz.fits')
            ds9.set("catalog %s" % (REFERENCE_CATALOG))
            ### Can't find XPA access point for "copy to regions"
            ds9.set("catalog export tsv ds9_align.tsv")
            lines = open("ds9_align.tsv").readlines()
            ra_list, dec_list = [], []
            for line in lines[1:]:
                spl = line.split()
                ra, dec = float(spl[0]), float(spl[1])
                ra_list.append(ra)
                dec_list.append(dec)
            #
            del (ds9)

        x_image, y_image = [], []
        for ra, dec in zip(ra_list, dec_list):
            x, y = wcs.wcs_sky2pix([[ra, dec]], 1)[0]
            x_image.append(x)
            y_image.append(y)

        alignCat = catIO.EmptyCat()
        alignCat["X_IMAGE"] = np.array(x_image)
        alignCat["Y_IMAGE"] = np.array(y_image)

    else:
        ## alignment image
        se.options["CATALOG_NAME"] = "align.cat"
        status = se.sextractImage(ROOT_DIRECT + "_align.fits")
        alignCat = threedhst.sex.mySexCat("align.cat")

    xshift = 0
    yshift = 0
    rot = 0
    scale = 1.0

    xrms = 2
    yrms = 2

    NITER = 5
    IT = 0
    while IT < NITER:
        IT = IT + 1

        #### Get x,y coordinates of detected objects
        ## direct image
        fp = open("direct.xy", "w")
        for i in range(len(directCat.X_IMAGE)):
            fp.write("%s  %s\n" % (directCat.X_IMAGE[i], directCat.Y_IMAGE[i]))
        fp.close()

        ## alignment image
        fp = open("align.xy", "w")
        for i in range(len(alignCat.X_IMAGE)):
            fp.write("%s  %s\n" % (np.float(alignCat.X_IMAGE[i]) + xshift, np.float(alignCat.Y_IMAGE[i]) + yshift))
        fp.close()

        iraf.flpr()
        iraf.flpr()
        iraf.flpr()
        #### iraf.xyxymatch to find matches between the two catalogs
        pow = toler * 1.0
        try:
            os.remove("align.match")
        except:
            pass

        status1 = iraf.xyxymatch(
            input="direct.xy",
            reference="align.xy",
            output="align.match",
            tolerance=2 ** pow,
            separation=0,
            verbose=yes,
            Stdout=1,
        )

        nmatch = 0
        while status1[-1].startswith("0") | (nmatch < 10):
            pow += 1
            os.remove("align.match")
            status1 = iraf.xyxymatch(
                input="direct.xy",
                reference="align.xy",
                output="align.match",
                tolerance=2 ** pow,
                separation=0,
                verbose=yes,
                Stdout=1,
            )
            #
            nmatch = 0
            for line in open("align.match").xreadlines():
                nmatch += 1

        if verbose:
            for line in status1:
                print line

        #### Compute shifts with iraf.geomap
        iraf.flpr()
        iraf.flpr()
        iraf.flpr()
        try:
            os.remove("align.map")
        except:
            pass

        status2 = iraf.geomap(
            input="align.match",
            database="align.map",
            fitgeometry=fitgeometry,
            interactive=no,
            xmin=INDEF,
            xmax=INDEF,
            ymin=INDEF,
            ymax=INDEF,
            maxiter=10,
            reject=2.0,
            Stdout=1,
        )
        if verbose:
            for line in status2:
                print line

        # fp = open(root+'.iraf.log','a')
        # fp.writelines(status1)
        # fp.writelines(status2)
        # fp.close()

        #### Parse geomap.output
        fp = open("align.map", "r")
        for line in fp.readlines():
            spl = line.split()
            if spl[0].startswith("xshift"):
                xshift += float(spl[1])
            if spl[0].startswith("yshift"):
                yshift += float(spl[1])
            if spl[0].startswith("xrotation"):
                rot = float(spl[1])
            if spl[0].startswith("xmag"):
                scale = float(spl[1])
            if spl[0].startswith("xrms"):
                xrms = float(spl[1])
            if spl[0].startswith("yrms"):
                yrms = float(spl[1])

        fp.close()

        # os.system('wc align.match')
        print "Shift iteration #%d, xshift=%f, yshift=%f, rot=%f, scl=%f (rms: %5.2f,%5.2f)" % (
            IT,
            xshift,
            yshift,
            rot,
            scale,
            xrms,
            yrms,
        )

    im = pyfits.open("SCI.fits")

    shutil.copy("align.map", ROOT_DIRECT + "_align.map")
    shutil.copy("align.match", ROOT_DIRECT + "_align.match")

    #### Cleanup
    if clean:
        rmfiles = [
            "SCI.fits",
            "WHT.fits",
            "align.cat",
            "align.map",
            "align.match",
            "align.reg",
            "align.xy",
            "direct.cat",
            "direct.reg",
            "direct.xy",
            "drz_sci.fits",
            "drz_wht.fits",
            "bg.fits",
        ]

        for file in rmfiles:
            try:
                os.remove(file)
            except:
                pass

    return xshift, yshift, rot, scale, xrms, yrms