Beispiel #1
0
def extraction_image(req, job_id=None, size='full'):
    from astrometry.plot.plotstuff import (Plotstuff,
                                           PLOTSTUFF_FORMAT_PNG,
                                           PLOTSTUFF_FORMAT_PPM)

    job = get_object_or_404(Job, pk=job_id)
    ui = job.user_image
    sub = ui.submission
    img = ui.image
    if size == 'display':
        scale = float(img.get_display_image().width)/img.width
        img = img.get_display_image()
    else:
        scale = 1.0

    axyfn = job.get_axy_file()
    pnmfn = img.get_pnm_path()
    exfn = get_temp_file()

    try:
        plot = Plotstuff()
        plot.size = [img.width, img.height]
        plot.outformat = PLOTSTUFF_FORMAT_PNG
        plot.outfn = exfn

        # plot image
        pimg = plot.image
        pimg.set_file(str(pnmfn))
        pimg.format = PLOTSTUFF_FORMAT_PPM
        plot.plot('image')

        # plot sources
        xy = plot.xy
        if hasattr(img, 'sourcelist'):
            # set xy offsets for source lists
            fits = img.sourcelist.get_fits_table()
            #xy.xoff = int(fits.x.min())
            #xy.yoff = int(fits.y.min())
            xy.xoff = xy.yoff = 1.

        if sub.use_sextractor:
            xy.xcol = 'X_IMAGE'
            xy.ycol = 'Y_IMAGE'

        xy.set_filename(str(axyfn))
        xy.scale = scale
        plot.color = 'red'
        # plot 50 brightest
        xy.firstobj = 0
        xy.nobjs = 50
        plot.lw = 2.
        plot.markersize = 6
        plot.plot('xy')
        # plot 200 other next brightest sources
        xy.firstobj = 50
        xy.nobjs = 250
        plot.alpha = 0.9
        plot.lw = 1.
        plot.markersize = 4
        plot.plot('xy')
        # plot 250 other next brightest sources
        xy.firstobj = 250
        xy.nobjs = 500
        plot.alpha = 0.5
        plot.lw = 1.
        plot.markersize = 3
        plot.plot('xy')
        plot.write()
    except:
        import traceback
        traceback.print_exc()
        return HttpResponse("plot failed")

    f = open(exfn, 'rb')
    res = HttpResponse(f)
    res['Content-Type'] = 'image/png'
    return res
Beispiel #2
0
def grid_image(req, jobid=None, size='full'):
    from astrometry.plot.plotstuff import (Plotstuff,
                                           PLOTSTUFF_FORMAT_JPG,
                                           PLOTSTUFF_FORMAT_PPM,
                                           plotstuff_set_size_wcs,
    )
    job = get_object_or_404(Job, pk=jobid)
    ui = job.user_image
    img = ui.image
    if size == 'display':
        scale = float(img.get_display_image().width)/img.width
        img = img.get_display_image()
    else:
        scale = 1.0

    wcsfn = job.get_wcs_file()
    pnmfn = img.get_pnm_path()
    outfn = get_temp_file()

    plot = Plotstuff()
    plot.wcs_file = wcsfn
    plot.outformat = PLOTSTUFF_FORMAT_JPG
    plot.outfn = outfn
    plot.scale_wcs(scale)
    plotstuff_set_size_wcs(plot.pargs)

    # plot image
    pimg = plot.image
    pimg.set_file(str(pnmfn))
    pimg.format = PLOTSTUFF_FORMAT_PPM
    plot.plot('image')

    grid = plot.grid
    ra,dec,radius = job.calibration.get_center_radecradius()
    steps = np.array([ 0.02, 0.05, 0.1, 0.2, 0.5,
                       1., 2., 5., 10., 15.,  30., 60. ])
    istep = np.argmin(np.abs(np.log(radius) - np.log(steps)))
    grid.declabelstep = steps[istep]
    nd = plot.count_dec_labels()
    if nd < 2:
        istep = max(istep-1, 0)
        grid.declabelstep = steps[istep]
    grid.decstep = grid.declabelstep
    plot.alpha = 1.
    plot.plot('grid')

    plot.alpha = 0.7
    grid.declabelstep = 0
    grid.decstep /= 2.
    plot.plot('grid')
    grid.decstep = 0

    # RA
    cosdec = np.cos(np.deg2rad(dec))
    istep = np.argmin(np.abs(np.log(radius/cosdec) - np.log(steps)))
    grid.ralabelstep = steps[istep] #min(istep+1, len(steps)-1)]
    nra = plot.count_ra_labels()
    if nra < 2:
        istep = max(istep-1, 0)
        grid.ralabelstep = steps[istep]
    grid.rastep = grid.ralabelstep
    plot.alpha = 1.
    plot.plot('grid')

    plot.alpha = 0.7
    grid.ralabelstep = 0
    grid.rastep /= 2.
    plot.plot('grid')

    plot.write()
    f = open(outfn, 'rb')
    res = HttpResponse(f)
    res['Content-Type'] = 'image/jpeg'
    return res
Beispiel #3
0
def red_green_image(req, job_id=None, size='full'):
    from astrometry.plot.plotstuff import (Plotstuff,
                                           PLOTSTUFF_FORMAT_PNG,
                                           PLOTSTUFF_FORMAT_PPM,
                                           #plotstuff_set_size_wcs,
    )

    job = get_object_or_404(Job, pk=job_id)
    ui = job.user_image
    sub = ui.submission
    img = ui.image
    if size == 'display':
        scale = float(img.get_display_image().width)/img.width
        img = img.get_display_image()
    else:
        scale = 1.0

    axyfn = job.get_axy_file()
    wcsfn = job.get_wcs_file()
    rdlsfn = job.get_rdls_file()
    pnmfn = img.get_pnm_path()
    exfn = get_temp_file()

    try:
        plot = Plotstuff()
        plot.wcs_file = wcsfn
        plot.outformat = PLOTSTUFF_FORMAT_PNG
        plot.outfn = exfn
        plot.scale_wcs(scale)
        plot.set_size_from_wcs()
        #plotstuff_set_size_wcs(plot.pargs)

        # plot image
        pimg = plot.image
        pimg.set_file(str(pnmfn))
        pimg.format = PLOTSTUFF_FORMAT_PPM
        plot.color = 'white'
        plot.alpha = 1.
        if sub.use_sextractor:
            xy = plot.xy
            xy.xcol = 'X_IMAGE'
            xy.ycol = 'Y_IMAGE'
        plot.plot('image')

        # plot red
        xy = plot.xy
        if hasattr(img, 'sourcelist'):
            # set xy offsets for source lists
            fits = img.sourcelist.get_fits_table()
            #xy.xoff = int(fits.x.min())
            #xy.yoff = int(fits.y.min())
            xy.xoff = 0.
            xy.yoff = 0.

        xy.set_filename(str(axyfn))
        xy.scale = scale
        plot.color = 'red'
        xy.nobjs = 200
        plot.lw = 2.
        plot.markersize = 6
        plot.plot('xy')

        # plot green
        rd = plot.radec
        rd.set_filename(str(rdlsfn))
        plot.color = 'green'
        plot.markersize = 4
        plot.plot('radec')

        plot.write()
    except:
        return HttpResponse("plot failed")

    f = open(exfn, 'rb')
    res = StreamingHttpResponse(f)
    res['Content-Type'] = 'image/png'
    return res
Beispiel #4
0
def plot_index(
    fits_path,
    anet_bin_prefix = '',
):
    from astrometry.plot.plotstuff import Plotstuff
    import shutil
    import tempfile

    # Sourcefind ...

    try:
        info = source_extract_fits(fits_path)
    except Exception as e:
        raise Exception(f'could not extract sources from `{fits_path}`') from e

    work_dir = tempfile.mkdtemp()
    objects_fits = os.path.join(work_dir, f'objects.fits')
    info.wcs_objects.write(objects_fits, format='fits', overwrite=True)
    index_fits = os.path.join(work_dir, f'index.fits')
    index_log = os.path.join(work_dir, f'build-index.log')

    # Index ...

    try:
        index_extracted_image(
            objects_fits,
            index_fits,
            index_log = index_log,
            extraction_info = info,
            index_unique_key = '0',
            anet_bin_prefix = anet_bin_prefix,
        )
    except Exception as e:
        shutil.rmtree(work_dir)
        raise Exception(f'could not index the sources from `{fits_path}') from e

    # Plot!

    with fits.open(fits_path) as hdul:
        hdu = hdul[0]  # haack
        wcs = WCS(hdu.header)
        height, width = hdu.shape[-2:]
        midx = width // 2
        midy = height // 2
        coords = wcs.pixel_to_world(
            [midx, 0, width],
            [midy, 0, height],
        )
        rdw = (coords[0].ra.deg, coords[0].dec.deg, coords[1].separation(coords[2]).deg)

    plot = Plotstuff(outformat='png', size=(800, 800), rdw=rdw)
    ind = plot.index
    plot.color = 'white'
    ind.add_file(index_fits)
    ind.stars = True
    ind.quads = True
    ind.fill = False
    plot.plot('index')
    ind.stars = False
    ind.fill = True
    plot.alpha = 0.1
    plot.plot('index')
    plot.alpha = 0.5
    plot.plot_grid(0.02, 0.02, 0.02, 0.02)

    img_path = os.path.splitext(fits_path)[0] + '_index.png'
    plot.write(img_path)
    logger.debug('saved index image to `%s`', img_path)