def plot_into_wcs(wcsfn, plotfn, wcsext=0, basedir='.', scale=1.0):
    wcs = Tan(wcsfn, wcsext)
    ra, dec = wcs.radec_center()
    radius = wcs.radius()

    # The "index.fits" table has RA,Dec center and file paths.
    T = fits_table(os.path.join(basedir, 'index.fits'))
    # MAGIC 1: the GALEX fields are all smaller than 1 degree (~0.95) in radius,
    # so add that to the search
    r = radius + 1.
    # find rows in "T" that are within range.
    J = points_within_radius(ra, dec, r, T.ra, T.dec)
    T = T[J]
    debug(len(T), 'GALEX fields within range of RA,Dec = ', ra, dec, 'radius',
          radius)

    size = [int(scale * wcs.imagew), int(scale * wcs.imageh)]

    plot = ps.Plotstuff(outformat='png', wcsfn=wcsfn, wcsext=wcsext, size=size)
    plot.scale_wcs(scale)

    #debug('WCS:', str(plot.wcs))
    #plot.wcs.write_to('/tmp/wcs.fits')

    img = plot.image
    img.format = ps.PLOTSTUFF_FORMAT_JPG
    img.resample = 1
    if len(T):
        paths = [fn.strip() for fn in T.path]
    else:
        paths = []
        plot.color = 'black'
        plot.plot('fill')
    for jpegfn in paths:
        jpegfn = os.path.join(basedir, jpegfn)
        imwcsfn = jpegfn.replace('.jpg', '.wcs')
        debug('  Plotting GALEX fields', jpegfn, imwcsfn)
        #debug('jpeg "%s"' % jpegfn)
        #debug('wcs  "%s"' % imwcsfn)
        img.set_wcs_file(imwcsfn, 0)
        img.set_file(jpegfn)
        # convert black to transparent
        ps.plot_image_read(plot.pargs, img)
        ps.plot_image_make_color_transparent(img, 0, 0, 0)
        plot.plot('image')
    plot.write(plotfn)
    debug('wrote', plotfn)
Beispiel #2
0
def plot_aitoff_wcs_outline(wcsfn, plotfn, W=256, zoom=True):
    #anutil.log_init(3)
    H = int(W // 2)
    # Create Hammer-Aitoff WCS of the appropriate size.
    wcs = anutil.anwcs_create_allsky_hammer_aitoff(0., 0., W, H)

    plot = ps.Plotstuff(outformat='png', size=(W, H))
    plot.wcs = wcs
    plot.linestep = 1.
    plot.color = 'verydarkblue'
    plot.apply_settings()
    plot.line_constant_ra(180, -90, 90)
    plot.line_constant_ra(-180, 90, -90)
    plot.fill()

    #plot.plot_grid(60, 30, 60, 30)
    plot.fontsize = 12
    ras = [-180, -120, -60, 0, 60, 120, 180]
    decs = [-60, -30, 0, 30, 60]
    # dark gray
    plot.rgb = (0.3, 0.3, 0.3)
    plot.apply_settings()
    for ra in ras:
        plot.line_constant_ra(ra, -90, 90)
        plot.stroke()
    for dec in decs:
        plot.line_constant_dec(dec, -180, 180)
        plot.stroke()

    plot.color = 'gray'
    plot.apply_settings()
    for ra in ras:
        plot.move_to_radec(ra, 0)
        plot.text_radec(ra, 0, '%i' % ((ra + 360) % 360))
        plot.stroke()
    for dec in decs:
        if dec != 0:
            plot.move_to_radec(0, dec)
            plot.text_radec(0, dec, '%+i' % dec)
            plot.stroke()

    plot.color = 'white'
    plot.lw = 3
    out = plot.outline
    #out.fill = 1
    out.wcs_file = wcsfn
    anutil.anwcs_print_stdout(out.wcs)
    plot.plot('outline')

    # Not helpful to add constellations in this view
    #ann = plot.annotations
    #ann.NGC = ann.bright = ann.HD = 0
    #ann.constellations = 1
    #plot.plot('annotations')

    if zoom:
        owcs = anutil.Tan(wcsfn, 0)
        # MAGIC 15 degrees radius
        #if owcs.radius() < 15.:
        if True:
            ra, dec = owcs.radec_center()
            # MAGIC 36-degree width zoom-in
            # MAGIC width, height are arbitrary
            zoomwcs = anutil.anwcs_create_box(ra, dec, 36, 1000, 1000)
            out.wcs = zoomwcs
            #plot.color = 'gray'
            plot.lw = 1
            plot.dashed(3)
            plot.plot('outline')

    plot.write(plotfn)
Beispiel #3
0
def plot_wcs_outline(wcsfn,
                     plotfn,
                     W=256,
                     H=256,
                     width=36,
                     zoom=True,
                     zoomwidth=3.6,
                     grid=10,
                     hd=False,
                     hd_labels=False,
                     tycho2=False):
    anutil.log_init(3)
    #anutil.log_set_level(3)

    wcs = anutil.Tan(wcsfn, 0)
    ra, dec = wcs.radec_center()

    plot = ps.Plotstuff(outformat='png', size=(W, H), rdw=(ra, dec, width))
    plot.linestep = 1.
    plot.color = 'verydarkblue'
    plot.plot('fill')

    plot.fontsize = 12
    #plot.color = 'gray'
    # dark gray
    plot.rgb = (0.3, 0.3, 0.3)
    if grid is not None:
        plot.plot_grid(*([grid] * 4))

    plot.rgb = (0.4, 0.6, 0.4)
    ann = plot.annotations
    ann.NGC = ann.bright = ann.HD = 0
    ann.constellations = 1
    ann.constellation_labels = 1
    ann.constellation_labels_long = 1
    plot.plot('annotations')
    plot.stroke()
    ann.constellation_labels = 0
    ann.constellation_labels_long = 0
    ann.constellation_lines = 0

    ann.constellation_markers = 1
    plot.markersize = 3
    plot.rgb = (0.4, 0.6, 0.4)
    plot.plot('annotations')
    plot.fill()
    ann.constellation_markers = 0

    ann.bright_labels = False
    ann.bright = True
    plot.markersize = 2
    if zoom >= 2:
        ann.bright_labels = True
    plot.plot('annotations')
    ann.bright = False
    ann.bright_labels = False
    plot.fill()

    if hd:
        ann.HD = True
        ann.HD_labels = hd_labels
        ps.plot_annotations_set_hd_catalog(ann, settings.HENRY_DRAPER_CAT)
        plot.plot('annotations')
        plot.stroke()
        ann.HD = False
        ann.HD_labels = False

    if tycho2 and settings.TYCHO2_KD:
        from astrometry.libkd.spherematch import tree_open, tree_close, tree_build_radec, tree_free, trees_match
        from astrometry.libkd import spherematch_c
        from astrometry.util.starutil_numpy import deg2dist, xyztoradec
        import numpy as np
        import sys
        kd = tree_open(settings.TYCHO2_KD)
        # this is a bit silly: build a tree with a single point, then do match()
        kd2 = tree_build_radec(np.array([ra]), np.array([dec]))
        r = deg2dist(width * np.sqrt(2.) / 2.)
        #r = deg2dist(wcs.radius())
        I, nil, nil = trees_match(kd, kd2, r, permuted=False)
        del nil
        #print 'Matched', len(I)
        xyz = spherematch_c.kdtree_get_positions(kd, I)
        del I
        tree_free(kd2)
        tree_close(kd)
        #print >>sys.stderr, 'Got', xyz.shape, xyz
        tra, tdec = xyztoradec(xyz)
        #print >>sys.stderr, 'RA,Dec', ra,dec
        plot.apply_settings()
        for r, d in zip(tra, tdec):
            plot.marker_radec(r, d)
        plot.fill()

    ann.NGC = 1
    plot.plot('annotations')
    ann.NGC = 0

    plot.color = 'white'
    plot.lw = 3
    out = plot.outline
    out.wcs_file = wcsfn
    plot.plot('outline')

    if zoom:
        # MAGIC width, height are arbitrary
        zoomwcs = anutil.anwcs_create_box(ra, dec, zoomwidth, 1000, 1000)
        out.wcs = zoomwcs
        plot.lw = 1
        plot.dashed(3)
        plot.plot('outline')

    plot.write(plotfn)
Beispiel #4
0
def plot_sdss_image(wcsfn, plotfn, image_scale=1.0, debug_ps=None):
    from astrometry.util import util as anutil
    from astrometry.blind import plotstuff as ps
    # Parse the wcs.fits file
    wcs = anutil.Tan(wcsfn, 0)
    # grab SDSS tiles with about the same resolution as this image.
    pixscale = wcs.pixel_scale()
    pixscale = pixscale / image_scale
    logmsg('Original image scale is', wcs.pixel_scale(), 'arcsec/pix; scaled',
           image_scale, '->', pixscale)
    # size of SDSS image tiles to request, in pixels
    sdsssize = 512
    scale = sdsssize * pixscale / 60.
    # healpix-vs-north-up rotation
    nside = anutil.healpix_nside_for_side_length_arcmin(scale / math.sqrt(2.))
    nside = 2**int(math.ceil(math.log(nside) / math.log(2.)))
    logmsg('Next power-of-2 nside:', nside)
    ra, dec = wcs.radec_center()
    logmsg('Image center is RA,Dec', ra, dec)

    dirnm = os.path.join(settings.SDSS_TILE_DIR, 'nside%i' % nside)
    if not os.path.exists(dirnm):
        os.makedirs(dirnm)

    #hp = anutil.radecdegtohealpix(ra, dec, nside)
    #logmsg('Healpix of center:', hp)
    radius = wcs.radius()
    hps = anutil.healpix_rangesearch_radec(ra, dec, radius, nside)
    logmsg('Healpixes in range:', len(hps), ': ', hps)

    scale = math.sqrt(2.) * anutil.healpix_side_length_arcmin(
        nside) * 60. / float(sdsssize)
    logmsg('Grabbing SDSS tile with scale', scale, 'arcsec/pix')

    size = [int(image_scale * wcs.imagew), int(image_scale * wcs.imageh)]

    plot = ps.Plotstuff(outformat='png', wcsfn=wcsfn, size=size)
    plot.scale_wcs(image_scale)
    img = plot.image
    img.format = ps.PLOTSTUFF_FORMAT_JPG
    img.resample = 1

    for hp in hps:
        fn = os.path.join(dirnm, '%i.jpg' % hp)
        logmsg('Checking for filename', fn)
        if not os.path.exists(fn):
            ra, dec = anutil.healpix_to_radecdeg(hp, nside, 0.5, 0.5)
            logmsg('Healpix center is RA,Dec', ra, dec)
            url = (
                'http://skyservice.pha.jhu.edu/DR8/ImgCutout/getjpeg.aspx?' +
                'ra=%f&dec=%f&scale=%f&opt=&width=%i&height=%i' %
                (ra, dec, scale, sdsssize, sdsssize))
            urlretrieve(url, fn)  #nosec
            logmsg('Wrote', fn)
        swcsfn = os.path.join(dirnm, '%i.wcs' % hp)
        logmsg('Checking for WCS', swcsfn)
        if not os.path.exists(swcsfn):
            # Create WCS header
            cd = scale / 3600.
            swcs = anutil.Tan(ra, dec, sdsssize / 2 + 0.5, sdsssize / 2 + 0.5,
                              -cd, 0, 0, -cd, sdsssize, sdsssize)
            swcs.write_to(swcsfn)
            logmsg('Wrote WCS to', swcsfn)

        img.set_wcs_file(swcsfn, 0)
        img.set_file(fn)
        plot.plot('image')

        if debug_ps is not None:
            fn = debug_ps.getnext()
            plot.write(fn)
            print('Wrote', fn)

    if debug_ps is not None:
        out = plot.outline
        plot.color = 'white'
        plot.alpha = 0.25
        for hp in hps:
            swcsfn = os.path.join(dirnm, '%i.wcs' % hp)
            ps.plot_outline_set_wcs_file(out, swcsfn, 0)
            plot.plot('outline')
        plot.write(fn)
        print('Wrote', fn)

    plot.write(plotfn)