Exemple #1
0
def fname(tmpdir, fname_template):
    """Copy the Fe file into a temporary directory and return the file name"""
    fe_name = py.path.local(prefix_filename(fname_template, "Fe"))
    fe_name.copy(tmpdir)

    fe_name = tmpdir.join(fe_name.basename)

    return str(fe_name)
Exemple #2
0
def reffiles(fname_template):
    """Subtracted and sky reference file name"""
    skysub_ref = prefix_filename(fname_template, 'FeS')
    sky_ref = prefix_filename(fname_template, 'FeSky')
    return skysub_ref, sky_ref
Exemple #3
0
def outfiles(fname):
    """Subtracted and sky output file name"""
    skysub_fname = prefix_filename(fname, 'S')
    sky_fname = prefix_filename(fname, 'Sky')
    return skysub_fname, sky_fname
Exemple #4
0
def get_image(ra, dec, pa, size, ifu_centers, yflip, outdir):
    """Create the SDSS image around the given coordinates

    .. todo::
        ``filename``: is hard coded. Instead of the output directory pass the
        full output name in the function arguments

        Rename the function: probably ``plot_image`` would be more appropriate

        ``box``, ``size_cut_out``: they should not be hard coded

        html size should not be hard code

    Parameters
    ----------
    ra, dec : float
        coordinates of the plot
    pa : ?
        ??
    size : float
        ??
    ifu_centers : list
        list of ifu coordinates?
    yflip : bool
        flip the image around the y axis
    outdir : string
        output directory

    Returns
    -------
    outfile : string
        full name of the saved plot
    """
    filename = "quicklook_sky_%f_%f_%f.jpeg" % (ra, dec, pa)
    filename = os.path.join(outdir, filename)

    # Size in degrees
    imarray, CD, url, img_src = retrieve_image(ra, dec, size, yflip)

    size_pix = len(imarray)
    scale = pyhwcs.deg2pix(size, scale=size_pix)

    fig = plt.figure()
    ax = fig.add_axes([0, 0, 1, 1], frameon=False)

    coll = plotFocalPlaneQuicklook(0,
                                   0,
                                   pa,
                                   scale,
                                   ifu_centers,
                                   ra,
                                   dec,
                                   CD,
                                   size_pix,
                                   color='green')
    ax.add_collection(coll)
    ax.imshow(imarray, origin='lower', cmap='gray', interpolation="nearest")
    ax.axis('off')
    tempfile = ft.prefix_filename(filename, "temp_")
    fig.savefig(tempfile, bbox_inches='tight')
    plt.close(fig)

    # Convert array to Image object
    img = Image.open(tempfile)

    # Cut out the 600x600 image part of the plot
    box = (42, 8, 642, 608)
    imgCrop = img.crop(box)
    size_cut_out = 600.0

    # Set size to get 25 arcseconds per pixel - hardcoded scale of the HTML
    # interface
    rescale = int(size_cut_out * 25.0 / scale)
    imgCrop.resize((rescale, rescale))

    # Match position angle
    rotImg = imgCrop.rotate(-1.0 * pa)

    # Finally trim the image to be the correct dimensions for the 478 width by
    # 586 height html div
    xsize, ysize = rotImg.size
    left = (xsize - 478) / 2
    right = xsize - (xsize - 478) / 2
    top = (ysize - 586) / 2
    bottom = ysize - (ysize - 586) / 2

    rotImgCrop = rotImg.crop((left, top, right, bottom))
    finalImg = rotImgCrop.filter(ImageFilter.SMOOTH)
    finalImg.save(filename, "JPEG")
    os.remove(tempfile)

    return filename
Exemple #5
0
def fe_sky_subtraction(fname,
                       sig=2.5,
                       iters=None,
                       wmin=None,
                       wmax=None,
                       width=20,
                       prefix='S',
                       skyprefix='Sky',
                       output_both=True):
    """Perform the sky subtraction from a fiber extracted frame

    Parameters
    ----------
    fname : string
        name of the fits file to process
    sig : float, optional
        number of standard deviations to use for the clipping limit
    iters  : int or `None`, optional
        number of iterations to perform clipping for, or `None` to clip
        until convergence is achieved (i.e. continue until the last
        iteration clips nothing).
    wmin, wmax : floats, optional
        maximum and minimum wavelength in Armstrong to use for the sigma
        clipping. Converted to indices using the 'CRVAL1' and 'CDELT1' keyword
        in the header of the fits file. If None the minimum and/or maximum of
        the range used
    width : int, optional
        width of the moving window used to estimate the sky background
    prefix : string, optional
        prefix of the sky subtracted file
    skyprefix : string, optional
        prefix of the sky file
    output_both : bool, optional
        save both sky and sky subtracted frames, instead of the latter alone
    """

    with fits.open(fname) as hdulist:
        header = hdulist[0].header
        data = hdulist[0].data

        # get the index of the input wavelengths
        imin = wavelength_to_index(header, wmin)
        imax = wavelength_to_index(header, wmax)

        # take the median along the fibers within the give wavelength range
        median_data = np.median(data[:, imin:imax], axis=1)
        # and get the inverse of the mask from the sigma clipping
        clip_mask = np.logical_not(
            sigma_clip(median_data,
                       sigma=sig,
                       iters=iters,
                       cenfunc=np.ma.median).mask)

        # construct the sky frame
        sky = np.empty_like(data)
        for i, _ in enumerate(clip_mask):
            win_mask = moving_window(clip_mask, i, width=width)
            sky[i, :] = np.median(data[win_mask, :], axis=0)

        if parse_version(astropy.__version__) < parse_version('1.3'):
            writeto_kwars = {'clobber': True}
        else:
            writeto_kwars = {'overwrite': True}

        # save the sky subtracted file
        hdulist[0].data = data - sky
        hdulist.writeto(prefix_filename(fname, prefix), **writeto_kwars)

        if output_both:  # save the sky file
            hdulist[0].data = sky
            hdulist.writeto(prefix_filename(fname, skyprefix), **writeto_kwars)
Exemple #6
0
def test_prefix_filename(infname, prefix, outfname):
    "test the insertion of a prefix in front of the file name"
    fout = ft.prefix_filename(infname, prefix)
    assert fout == outfname