Esempio n. 1
0
def onthesky_image(req, zoom=None, calid=None):
    from astrometry.net.views.onthesky import plot_aitoff_wcs_outline
    from astrometry.net.views.onthesky import plot_wcs_outline
    from astrometry.util import util as anutil
    #
    cal = get_object_or_404(Calibration, pk=calid)
    wcsfn = cal.get_wcs_file()
    plotfn = get_temp_file()
    #
    wcs = anutil.Tan(wcsfn, 0)
    zoom = int(zoom)
    if zoom == 0:
        zoom = wcs.radius() < 15.
        plot_aitoff_wcs_outline(wcsfn, plotfn, zoom=zoom)
    elif zoom == 1:
        zoom = wcs.radius() < 1.5
        plot_wcs_outline(wcsfn, plotfn, zoom=zoom)
    elif zoom == 2:
        zoom = wcs.radius() < 0.15
        plot_wcs_outline(wcsfn, plotfn, width=3.6, grid=1, zoom=zoom,
                         zoomwidth=0.36, hd=True, hd_labels=False,
                         tycho2=False)
        # hd=True is too cluttered at this level
    elif zoom == 3:
        plot_wcs_outline(wcsfn, plotfn, width=0.36, grid=0.1, zoom=False,
                         hd=True, hd_labels=True, tycho2=True)
    else:
        return HttpResponse('invalid zoom')
    f = open(plotfn)
    res = HttpResponse(f)
    res['Content-Type'] = 'image/png'
    return res
Esempio n. 2
0
 def to_tanwcs(self):
     from astrometry.util import util as anutil
     tan = anutil.Tan(self.crval1, self.crval2, self.crpix1, self.crpix2,
                      self.cd11, self.cd12, self.cd21, self.cd22,
                      self.imagew, self.imageh)
     return tan
     '''
Esempio n. 3
0
def write_wcs_file(req, wcsfn):
    from astrometry.util import util as anutil
    wcsparams = []
    wcs = req.json['wcs']
    for name in ['crval1', 'crval2', 'crpix1', 'crpix2',
                 'cd11', 'cd12', 'cd21', 'cd22', 'imagew', 'imageh']:
        wcsparams.append(wcs[name])
    wcs = anutil.Tan(*wcsparams)
    wcs.write_to(wcsfn)
Esempio n. 4
0
def LensPlaneWCS(pos):
    '''
      The "local" WCS -- useful when you need to work on the sky, but in
      small offsets from RA, Dec in arcsec. Initialisation is with the
      coordinates of the central pixel, which is set to be the origin of
      the "pixel coordinate" system. Return a WCS object.
      '''

    onearcsec = 1.0 / 3600.0

    return tractor.FitsWcs(
        util.Tan(pos.ra, pos.dec, 1.0, 1.0, -onearcsec, 0.0, 0.0, onearcsec, 0,
                 0))
Esempio n. 5
0
 def overlay_plot(self, service, outfn, wcsfn, wcsext=0):
     from astrometry.util import util as anutil
     wcs = anutil.Tan(wcsfn, wcsext)
     params = dict(crval1 = wcs.crval[0], crval2 = wcs.crval[1],
                   crpix1 = wcs.crpix[0], crpix2 = wcs.crpix[1],
                   cd11 = wcs.cd[0], cd12 = wcs.cd[1],
                   cd21 = wcs.cd[2], cd22 = wcs.cd[3],
                   imagew = wcs.imagew, imageh = wcs.imageh)
     result = self.send_request(service, {'wcs':params})
     #print('Result status:', result['status'])
     plotdata = result['plot']
     plotdata = base64.b64decode(plotdata)
     open(outfn, 'wb').write(plotdata)
Esempio n. 6
0
def PS1WCS(hdr):
    '''
      Return a WCS object initialised from a PS1 file header.
      WARNING: PC matrix not being used, determinant may be wrong sign...
      Need to check with literature image of H1413
      '''
    t = util.Tan()
    t.set_crpix(hdr['CRPIX1'], hdr['CRPIX2'])
    t.set_crval(hdr['CRVAL1'], hdr['CRVAL2'])
    t.set_cd(hdr['CDELT1'], 0., 0., hdr['CDELT2'])
    t.set_imagesize(hdr['NAXIS1'], hdr['NAXIS2'])

    return tractor.FitsWcs(t)
Esempio n. 7
0
    def overlay_plot(self, service, outfn, wcsfn, wcsext=0):
        from astrometry.util import util as anutil

        wcs = anutil.Tan(wcsfn, wcsext)
        params = dict(
            crval1=wcs.crval[0],
            crval2=wcs.crval[1],
            crpix1=wcs.crpix[0],
            crpix2=wcs.crpix[1],
            cd11=wcs.cd[0],
            cd12=wcs.cd[1],
            cd21=wcs.cd[2],
            cd22=wcs.cd[3],
            imagew=wcs.imagew,
            imageh=wcs.imageh,
        )
        result = self.send_request(service, {"wcs": params})
        print("Result status:", result["status"])
        plotdata = result["plot"]
        plotdata = base64.b64decode(plotdata)
        open(outfn, "wb").write(plotdata)
        print("Wrote", outfn)
Esempio n. 8
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)
Esempio n. 9
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)
Esempio n. 10
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)
Esempio n. 11
0
        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)


if __name__ == '__main__':
    import logging
    from astrometry.util import util as anutil

    logging.basicConfig(format='%(message)s', level=logging.DEBUG)

    wcsfn = 'wcs.fits'
    outfn = 'sdss.png'

    if True:
        wcs = anutil.Tan(wcsfn)
        scale = 640. / wcs.get_width()
        print('Scale', scale)

    from astrometry.util.plotutils import *

    ps = PlotSequence('sdss')

    plot_sdss_image(wcsfn, outfn, image_scale=scale, debug_ps=ps)