def profilePlot(outpath, base_name, order_num, profile, peak, centroid,
                ext_range, sky_range_top, sky_range_bot, top_bg_mean,
                bot_bg_mean, gaussian, snr):

    pl.figure('spatial profile', facecolor='white')
    pl.cla()
    pl.title('spatial profile, ' + base_name + ', order ' + str(order_num),
             fontsize=14)

    pl.xlabel('relative row (pixels)')
    pl.ylabel('flux (counts)')

    # set axes limits
    yrange = profile.max() - profile.min()
    ymin = profile.min() - (0.1 * yrange)
    ymax = profile.max() + (0.1 * yrange)
    pl.ylim(ymin, ymax)

    # set ticks and grid
    pl.minorticks_on()
    pl.grid(False)

    # plot profile
    pl.plot(profile, "ko-", mfc='none', ms=3.0, linewidth=1)

    # plot vertical line to indicate location of centroid
    pl.plot([centroid, centroid], [ymin, profile.max()],
            "g-",
            linewidth=0.5,
            label='peak')

    # draw extraction window
    wvlh = 0.01 * yrange
    ewh = 0.05 * yrange

    pl.plot((ext_range[0], ext_range[-1]), (profile.max(), profile.max()),
            'r',
            linewidth=0.5,
            label='extraction window')
    pl.plot((ext_range[0], ext_range[0]),
            (profile.max() - wvlh, profile.max() + wvlh),
            'r',
            linewidth=1.5)
    pl.plot((ext_range[-1], ext_range[-1]),
            (profile.max() - wvlh, profile.max() + wvlh),
            'r',
            linewidth=1.5)

    # draw annotation showing centroid, width and SNR
    if gaussian is None:
        width = 'unknown'
    else:
        width = '{:.1f}'.format(abs(gaussian[2]))

    aStr = 'centroid = {:.1f} pixels'.format(centroid)
    aStr = aStr + '\nwidth = {} pixels'.format(width)
    if snr is not None:
        aStr = aStr + '\nSNR = {:.1f}'.format(snr)

    if peak > (len(profile) / 2):
        pl.annotate(aStr, (peak - (len(ext_range) / 2) - 20,
                           ((ymax - ymin) * 3 / 5) + ymin))
    else:
        pl.annotate(aStr, (peak + (len(ext_range) / 2) + 5,
                           ((ymax - ymin) * 3 / 5) + ymin))

    # draw sky windows

    if sky_range_top is not None and len(sky_range_top) > 0:

        pl.plot((sky_range_top[0], sky_range_top[-1]),
                (top_bg_mean, top_bg_mean),
                'b',
                linewidth=0.5,
                label='sky window')
        pl.plot((sky_range_top[0], sky_range_top[0]),
                (top_bg_mean - wvlh, top_bg_mean + wvlh),
                'b',
                linewidth=1.5)
        pl.plot((sky_range_top[-1], sky_range_top[-1]),
                (top_bg_mean - wvlh, top_bg_mean + wvlh),
                'b',
                linewidth=1.5)

    if sky_range_bot is not None and len(sky_range_bot) > 0:

        pl.plot((sky_range_bot[0], sky_range_bot[-1]),
                (bot_bg_mean, bot_bg_mean),
                'b',
                linewidth=0.5)
        pl.plot((sky_range_bot[0], sky_range_bot[0]),
                (bot_bg_mean - wvlh, bot_bg_mean + wvlh),
                'b',
                linewidth=1.5)
        pl.plot((sky_range_bot[-1], sky_range_bot[-1]),
                (bot_bg_mean - wvlh, bot_bg_mean + wvlh),
                'b',
                linewidth=1.5)

    # draw best fit Gaussian
    if gaussian is not None:
        min = np.amin(profile)
        if min < 0:
            offset = 0
        else:
            offset = min
        pl.plot(image_lib.gaussian(range(len(profile)), gaussian[0],
                                   gaussian[1], gaussian[2]) + offset,
                'k--',
                linewidth=0.5,
                label='Gaussian fit')

    pl.legend(loc='best', prop={'size': 8})

    fn = constructFileName(outpath, base_name, order_num, 'profile.png')
    savePreviewPlot(fn)
    pl.close()
    log_fn(fn)
    return
Ejemplo n.º 2
0
def profilePlot(outpath, base_name, order_num, profile, peak, centroid,
            ext_range, sky_range_top, sky_range_bot, top_bg_mean, bot_bg_mean, gaussian, snr):

    pl.figure('spatial profile', facecolor='white')
    pl.cla()
    pl.title('spatial profile, ' + base_name + ', order ' + str(order_num), fontsize=14)

    pl.xlabel('relative row (pixels)')
    pl.ylabel('flux (counts)')

    # set axes limits
    #pl.xlim(0, profile.shape[0])
    yrange = profile.max() - profile.min()
    ymin = profile.min() - (0.1 * yrange)
    ymax = profile.max() + (0.1 * yrange)
    pl.ylim(ymin, ymax)
    
    pl.minorticks_on()
    pl.grid(False)
    
    pl.plot(profile, "ko-", mfc='none', ms=3.0, linewidth=1)
    
#     pl.plot([centroid, centroid], [ymin, profile[peak]], "g-", linewidth=0.5, label='peak')
    pl.plot([centroid, centroid], [ymin, profile.max()], "g-", linewidth=0.5, label='peak')

    
    # draw extraction window
    wvlh = 0.01 * yrange;
    ewh = 0.05 * yrange
    
    pl.plot((ext_range[0], ext_range[-1]), (profile.max(), profile.max()), 
            'r', linewidth=0.5, label='extraction window')
    pl.plot((ext_range[0], ext_range[0]),
            (profile.max() - wvlh, profile.max() + wvlh), 
            'r', linewidth=1.5)
    pl.plot((ext_range[-1], ext_range[-1]),
            (profile.max() - wvlh, profile.max() + wvlh), 
            'r', linewidth=1.5)  
    
    if gaussian is None:
        width = 'unknown'
    else:
        width = '{:.1f}'.format(abs(gaussian[2]))
    if peak > (len(profile) / 2):
        pl.annotate('centroid = {:.1f} pixels\nwidth = {} pixels\nSNR = {:.1f}'.format(
                centroid, width, snr), 
                    (peak - (len(ext_range) / 2) - 20, ((ymax - ymin) * 3 / 5) + ymin))
    else:
        pl.annotate('centroid = {:.1f} pixels\nwidth = {} pixels\nSNR = {:.1f}'.format(
                centroid, width, snr), 
                    (peak + (len(ext_range) / 2) + 5, ((ymax - ymin) * 3 / 5) + ymin))
        
    # draw sky windows
 
    if (sky_range_top):

        pl.plot((sky_range_top[0], sky_range_top[-1]),
                (top_bg_mean, top_bg_mean), 
                'b', linewidth=0.5, label='sky window')  
        pl.plot((sky_range_top[0], sky_range_top[0]),
                (top_bg_mean - wvlh, top_bg_mean + wvlh), 
                'b', linewidth=1.5)
        pl.plot((sky_range_top[-1], sky_range_top[-1]),
                (top_bg_mean - wvlh, top_bg_mean + wvlh), 
                'b', linewidth=1.5)  
        
    if (sky_range_bot):
        pl.plot((sky_range_bot[0], sky_range_bot[-1]),
                (bot_bg_mean, bot_bg_mean), 
                'b', linewidth=0.5)   
        pl.plot((sky_range_bot[0], sky_range_bot[0]),
                (bot_bg_mean - wvlh, bot_bg_mean + wvlh), 
                'b', linewidth=1.5)
        pl.plot((sky_range_bot[-1], sky_range_bot[-1]),
                (bot_bg_mean - wvlh, bot_bg_mean + wvlh), 
                'b', linewidth=1.5)
        
    # draw best fit Gaussian
    if gaussian is not None:
        pl.plot(image_lib.gaussian(range(len(profile)), gaussian[0], gaussian[1], gaussian[2]) + np.amin(profile), 
                'k--', linewidth=0.5, label='Gaussian fit')
        
    pl.legend(loc='best', prop={'size': 8})
    
    fn = constructFileName(outpath, base_name, order_num, 'profile.png')
    pl.savefig(fn)
    pl.close()
    log_fn(fn)
    return