示例#1
0
def ast_diff_plot(ast, Rinds1s, Rinds2s, inds, **kwargs):
    ast_file_ext = kwargs.get('ast_file_ext', '')
    filters = ('F275W', 'F336W')
    ax1s = graphics.setup_five_panel_plot(fig=1)
    [ax.set_xlabel('$%s$ Recovered' % filters[0]) for ax in ax1s]
    ax1s[0].set_ylabel('$%s$ Recovered-Input' % filters[0])

    mag1in = ast['mag1in']
    mag1out = ast['mag1out']
    mag2in = ast['mag2in']
    mag2out = ast['mag2out']
    for j in range(len(Rinds1s)):
        ax1s[j].plot(mag1out[inds[j]],
                     mag1out[inds[j]] - mag1in[inds[j]],
                     '.',
                     color='black')
        ax1s[j].plot(mag1out[Rinds1s[j]],
                     mag1out[Rinds1s[j]] - mag1in[Rinds1s[j]],
                     '.',
                     color='blue')
        ax1s[j].xaxis.set_major_locator(MultipleLocator(2))
        ax1s[j].set_xlim(19, 27)
        ax1s[j].set_ylim(-2, 2)
    plt.savefig('ast_diff_mag1%s%s' % EXT % (ast_file_ext))
    plt.close()

    ax2s = graphics.setup_five_panel_plot(fig=2)
    [ax.set_xlabel('$%s$ Recovered' % filters[1]) for ax in ax2s]
    ax2s[0].set_ylabel('$%s$ Recovered-Input' % filters[1])
    for j in range(len(Rinds2s)):
        ax2s[j].plot(mag2out[inds[j]],
                     mag2out[inds[j]] - mag2in[inds[j]],
                     '.',
                     color='black')
        ax2s[j].plot(mag2out[Rinds2s[j]],
                     mag2out[Rinds2s[j]] - mag2in[Rinds2s[j]],
                     '.',
                     color='blue')
        ax2s[j].xaxis.set_major_locator(MultipleLocator(2))
        ax2s[j].set_xlim(19, 27)
        ax2s[j].set_ylim(-2, 2)

    plt.savefig('ast_diff_mag2%s%s' % EXT % (ast_file_ext))
    plt.close()
    return
示例#2
0
def red_star(phot, rad_inds, cut_inds, **kwargs):
    axs = graphics.setup_five_panel_plot()
    flux1, flux2 = [], []
    Filter1 = kwargs.get('Filter1')
    Filter2 = kwargs.get('Filter2')
    for i in range(len(rad_inds)):
        inds = list(set(rad_inds[i]) & set(cut_inds))
        mag1 = phot['mag1'][inds]
        mag2 = phot['mag2'][inds]
        color = mag1 - mag2
        red = np.nonzero((color > 0.8) & (mag2 < 19))[0]
        flux2.append(
            np.sum(Astronomy.vegamag2flux(mag2[red], Filter2, 'wfc3uvis')))
        flux1.append(
            np.sum(Astronomy.vegamag2flux(mag1[red], Filter1, 'wfc3uvis')))
        ax = axs[i]
        ax.plot(color, mag2, ',', color='black')
        ax.plot(mag1[red] - mag2[red], mag2[red], ',', color='red')
    return flux1, flux2
def main():
    # /astro/users/ben/ad2/ben/WFC3/12058_M31-B01-F040510111617-UVIS/proc/
    fits_file = os.path.join(
        PHAT_DATA_BASE, '12058_M31-B01-F040510111617-UVIS_F275W_F336W.st.fits')
    exposure_reg = os.path.join(
        PHAT_DATA_BASE,
        '12058_exposure_coverage_UVIS_F336W_chipswap_bulge.reg')

    v3_contours = PHAT_DIR_EXTRA3 + 'contours_v3.reg'

    f = pyfits.open(fits_file)
    f_data = f[1].data
    f.close()

    Filter1 = 'F275W'
    Filter2 = 'F336W'

    mag1 = f_data.field('MAG1_UVIS')
    mag2 = f_data.field('MAG2_UVIS')
    color = mag1 - mag2
    ra = f_data.field('RA')
    dec = f_data.field('DEC')
    contour_dict = reg_to_array(v3_contours)

    verts, verts2, verts2 = get_overlap_regions()

    radec = np.column_stack((ra, dec))
    ind = np.nonzero(nxutils.points_inside_poly(radec, verts))[0]
    ind2 = np.nonzero(nxutils.points_inside_poly(radec, verts2))[0]
    ind3 = np.nonzero(nxutils.points_inside_poly(radec, verts3))[0]

    inds = calcs.points_inside_ds9_polygon(v3_contours, ra, dec)

    axs = graphics.setup_five_panel_plot(fig=2)
    for i in range(len(inds)):
        bam = list(set(inds[i]) & set(ind3))
        axs[i].plot(color[bam], mag2[bam], '.')
        axs[i].set_ylim(27, 19.5)
示例#4
0
def lf_bin_comparison(phot, inds_cut1, inds_cut2, rad_inds, **kwargs):
    lf_data = load_saved_lf(
        '/Users/phil/research/PHAT/code/UVBrights/diy_final/lf_data.dat')
    sup_phot = kwargs.get('sup_phot')
    rif_36 = sup_phot['rif_36']
    comp_file = kwargs.get('comp_file')
    comp_data = kwargs.get('comp_data')
    frac1s, frac2s, magbins = file_io.load_completeness_corrections(comp_file)
    comp50mag1, comp50mag2 = file_io.get_comp50mags(comp_data)
    ylab = r'$\rm{Number}/10^7 M_\odot$'
    xlab1 = '$%s$' % kwargs.get('Filter1')
    xlab2 = '$%s$' % kwargs.get('Filter2')
    cols = kwargs.get('cols')
    filter1 = kwargs.get('Filter1')
    filter2 = kwargs.get('Filter2')
    ax1s = graphics.setup_five_panel_plot(fig=2)
    for i in range(len(rad_inds)):
        ax1 = ax1s[i]
        ax1.set_xlim(26, 19)
        ax1.xaxis.set_major_locator(MultipleLocator(2))
        if i == 0: ax1.set_ylabel(ylab)
        ax1.set_xlabel(xlab1)
        # M/L ratio 1e7 scaling is for AXIS
        norm = rif_36[i + 1] / 2.315e-9 * 0.95 * 1e-7
        inds1 = list(set(inds_cut1) & set(rad_inds[i]))
        hist1, bins = np.histogram(phot['mag1'][inds1], bins=magbins)
        known = np.nonzero(bins[1:] <= comp50mag1[i])[0]
        poor = np.nonzero(bins[1:] >= comp50mag1[i])[0]
        vals = hist1[known] * frac1s[i][known] / norm
        errlow, errhigh = np.transpose([cdf_bitches(v) for v in vals])
        ax1.fill_between(bins[1:][known],
                         errlow,
                         errhigh,
                         color=cols[i],
                         alpha=0.2)
        ax1.plot(bins[1:],
                 hist1 * frac1s[i] / norm,
                 drawstyle='steps',
                 color=cols[i],
                 lw=1)
        ax1.plot(bins[1:][known],
                 hist1[known] * frac1s[i][known] / norm,
                 drawstyle='steps',
                 color=cols[i],
                 lw=2)
        lf_hist, lf_bins = lf_data[filter1][str(i)].values()
        ax1.plot(lf_bins, lf_hist, drawstyle='steps', color='black', lw=2)
        ax1.set_yscale('log')
        ax1.set_ylim(.01, 10)
    plt.savefig('radialLF_cdf_%s%s' % EXT % kwargs.get('Filter1'))
    plt.close()

    ax2s = graphics.setup_five_panel_plot(fig=1)
    #ax1,ax2 = graphics.two_panel_plot(11,5,xlab1,xlab2,ylab)
    for i in range(len(rad_inds)):
        ax2 = ax2s[i]
        ax2.set_xlim(26, 19)
        #ax2.set_ylim(0,8)
        ax2.xaxis.set_major_locator(MultipleLocator(2))
        if i == 0: ax2.set_ylabel(ylab)
        ax2.set_xlabel(xlab2)
        # M/L ratio 1e7 scaling is for AXIS
        norm = rif_36[i + 1] / 2.315e-9 * 0.95 * 1e-7
        inds2 = list(set(inds_cut2) & set(rad_inds[i]))
        hist2, bins = np.histogram(phot['mag2'][inds2], bins=magbins)
        known = np.nonzero(bins[1:] <= comp50mag2[i])[0]
        poor = np.nonzero(bins[1:] >= comp50mag2[i])[0]
        vals = hist2[known] * frac2s[i][known] / norm
        errlow, errhigh = np.transpose([cdf_bitches(v) for v in vals])
        ax2.fill_between(bins[1:][known],
                         errlow,
                         errhigh,
                         color=cols[i],
                         alpha=0.2)
        ax2.plot(bins[1:],
                 hist2 * frac2s[i] / norm,
                 drawstyle='steps',
                 color=cols[i],
                 lw=1)
        ax2.plot(bins[1:][known],
                 hist2[known] * frac2s[i][known] / norm,
                 drawstyle='steps',
                 color=cols[i],
                 lw=2)
        lf_hist, lf_bins = lf_data[filter2][str(i)].values()
        ax2.plot(lf_bins, lf_hist, drawstyle='steps', color='black', lw=2)
        ax2.set_yscale('log')
        ax2.set_ylim(.01, 10)
    plt.savefig('radialLF_cdf_%s%s' % EXT % kwargs.get('Filter2'))
    plt.close()
    return
示例#5
0
def disk_cont(phot, rad_inds, cut_inds, **kwargs):
    ftsize = kwargs.get('ftsize')
    disk_fit = kwargs.get('disk_fit')
    mag1_cut = kwargs.get('mag1_cut')
    mag2_cut = kwargs.get('mag2_cut')
    # exculding innermost region
    areas = kwargs.get('areas')[1:]
    distances = kwargs.get('distances')[1:]

    d1 = pyfits.open(disk_fit)[1].data
    dmag1 = d1.field('mag1')
    dmag2 = d1.field('mag2')
    dqc_inds = np.nonzero((dmag1 < mag1_cut) & (dmag2 < mag2_cut))
    dmag = dmag2[dqc_inds]
    dcolor = dmag1[dqc_inds] - dmag

    mag_bot = 23.
    mag_top = 22.
    # outline of MS between 22-23 - done roughly by eye/plot
    col_bl = 0.013
    col_tl = -0.13
    col_br = col_bl + 0.15
    col_tr = col_tl + 0.15

    test_col = [col_tl, col_tr, col_tr, col_tl, col_tl]
    test_mag = [mag_top, mag_top, mag_bot, mag_bot, mag_top]
    test_verts = np.column_stack((test_col, test_mag))
    # outline of NOT MS between 22-23
    col_r = -0.35
    col_l = -0.50
    control_col = [col_l, col_r, col_r, col_l, col_l]
    control_mag = test_mag  # just to be clear, same mag range...
    control_verts = np.column_stack((control_col, control_mag))
    axs = graphics.setup_five_panel_plot()
    Ncontrol, Ntest = [], []
    control, test = [], []

    for i in range(len(rad_inds)):
        inds = list(set(rad_inds[i]) & set(cut_inds))
        mag1 = phot['mag1'][inds]
        mag2 = phot['mag2'][inds]
        color = mag1 - mag2
        colmag = np.column_stack((color, mag2))
        control = np.nonzero(nxutils.points_inside_poly(colmag,
                                                        control_verts))[0]
        test = np.nonzero(nxutils.points_inside_poly(colmag, test_verts))[0]
        Ncontrol.append(float(len(control)))
        Ntest.append(float(len(test)))
        ax = axs[i]
        ax.plot(dcolor, dmag, ',', color='grey', mec='grey')
        ax.plot(color[control], mag2[control], '.', color='black')
        ax.plot(control_verts[:, 0], control_verts[:, 1], lw=2, color='blue')
        ax.plot(test_verts[:, 0], test_verts[:, 1], lw=2, color='white')
        ax.plot(color[test], mag2[test], '.', color='red')

    Ncfit = np.polyfit(distances, Ncontrol, 1)
    Ntfit = np.polyfit(distances, Ntest, 1)
    plt.savefig('disk_cont.png')
    print 'wrote disk_cont.png'
    plt.close()

    rel_errorNt = np.sqrt(Ntest) / Ntest
    rel_errorNc = np.sqrt(Ncontrol) / Ncontrol
    pct_err = [
        rt**2 + rc**2 + 2 * rt * rc for rt, rc in zip(rel_errorNt, rel_errorNc)
    ]
    ratio = np.array(Ntest) / np.array(Ncontrol)
    ax1, ax2, ax3 = graphics.set_up_three_panel_plot()
    ax3.errorbar(distances, ratio, yerr=ratio * pct_err, color='black', lw=2)
    ax2.plot(dcolor, dmag, ',', color='grey')
    for i in range(len(rad_inds)):
        inds = list(set(rad_inds[i]) & set(cut_inds))
        mag1 = phot['mag1'][inds]
        mag2 = phot['mag2'][inds]
        color = mag1 - mag2
        ax1.plot(color, mag2, ',', color='black')

    for ax in [ax1, ax2]:
        ax.plot(control_verts[:, 0],
                control_verts[:, 1],
                '--',
                lw=2,
                color='red')
        ax.plot(test_verts[:, 0], test_verts[:, 1], lw=2, color='darkblue')
        ax.set_ylim(24.1, 19)
        ax.set_xlim(-0.75, 2)
        ax.set_xlabel(r'$F275W-F336W$', fontsize=ftsize)

    ax2.yaxis.set_major_formatter(nullfmt)
    ax1.set_ylabel(r'$F336W$', fontsize=ftsize)
    ax3.set_xlabel(r'$\rm{Radius\ (pc)}$', fontsize=ftsize)
    ax3.set_ylabel(r'$\rm{Number\ Ratio Bluer/Redder}$', fontsize=ftsize)
    plt.savefig('nratio_cont.png')
    print 'wrote nratio_cont.png'
    plt.close()

    plt.errorbar(distances,
                 Ntest / areas,
                 yerr=np.sqrt(np.array(Ntest / areas)),
                 color='red',
                 lw=2,
                 label='Possible cont')
    plt.errorbar(distances,
                 Ncontrol / areas,
                 np.sqrt(np.array(Ncontrol / areas)),
                 color='black',
                 lw=2,
                 label='No cont')
    plt.xlabel('Region pc')
    plt.ylabel('Number in box/arcsec$^2$')
    plt.legend(loc=2)
    plt.savefig('density_cont.png')
    print 'wrote density_cont.png'
    plt.close()

    plt.figure()
    plt.plot(distances, Ntest, color='red', lw=2, label='Possible cont')
    plt.plot(distances, Ncontrol, color='black', lw=2, label='No cont')
    plt.xlabel('Region Out -- to -- In')
    plt.ylabel('Number in box')
    plt.legend(loc=0)
    plt.savefig('number_cont.png')
    print 'wrote number_cont.png'

    return
示例#6
0
def new_mag_cut(**kwargs):
    '''
    this can run by a call to ASTs, not just by running main
    set the magnitude cutoff to be the faintest recovered magnitude of a 
    star whose input magnitude was at the 50% completeness limit
    '''
    ast_file = kwargs.get('ast_file')
    comp_data = kwargs.get('comp_data')

    ast = load_ast_file(ast_file)
    comp = file_io.load_comp_data(comp_data)
    pm = 0.05

    # within dmag range
    rec1 = recovered_asts(ast['mag1in'], ast['mag1out'])
    rec2 = recovered_asts(ast['mag2in'], ast['mag2out'])

    # quality cut on all asts
    qc1 = quality_cut(ast['sharp1'],
                      ast['snr1'],
                      ast['crowd1'],
                      ast['round1'],
                      loud=True,
                      **kwargs)

    qc2 = quality_cut(ast['sharp2'],
                      ast['snr2'],
                      ast['crowd2'],
                      ast['round2'],
                      loud=True,
                      **kwargs)
    # each radial bin
    from params import PHAT_DIR_EXTRA3
    contour = PHAT_DIR_EXTRA3 + 'contours_v3.reg'
    inds = points_inside_ds9_polygon(contour, ast['ra'], ast['dec'])
    filters = ('F275W', 'F336W')
    ax1s = graphics.setup_five_panel_plot(fig=1)
    [ax.set_xlabel('$%s$ Recovered' % filters[0]) for ax in ax1s]
    ax1s[0].set_ylabel('$%s$ Recovered-Input' % filters[0])
    cols = kwargs.get('cols')
    axs = graphics.two_panel_plot_vert_bsquat()
    axs[0].set_xlabel('F275W Recovered')
    axs[1].set_ylabel('standard deviation')
    axs[0].set_ylabel('residual')
    for i in range(len(inds)):
        Rinds1 = list(set(rec1) & set(qc1) & set(inds[i]))
        #Rinds1 = list(set(rec1) & set(inds[i]))
        #Rinds1 = list(set(rec1) & set(qc1) & set(qc2) & set(inds[i]))
        within1 = np.nonzero(
            (ast['mag1in'][Rinds1] <= comp['comp50mag1'][i] + pm)
            & (ast['mag1in'][Rinds1] >= comp['comp50mag1'][i] - pm))[0]
        mag1_cut = np.max(ast['mag1out'][Rinds1][within1])
        print mag1_cut
        ast_diff_test(ast['mag1out'][Rinds1],
                      ast['mag1in'][Rinds1],
                      axs=axs,
                      color=cols[i],
                      spacing=0.4)
        axs[0].set_ylim(-0.01, 0.1)
        ax1s[i].plot(ast['mag1out'][inds[i]],
                     ast['mag1out'][inds[i]] - ast['mag1in'][inds[i]],
                     '.',
                     color='black')
        ax1s[i].plot(ast['mag1out'][Rinds1],
                     ast['mag1out'][Rinds1] - ast['mag1in'][Rinds1],
                     '.',
                     color='blue')
        #ax1s[i].plot(ast['mag1in'][inds[i]],ast['mag1out'][inds[i]]-ast['mag1in'][inds[i]],'.',color='black')
        #ax1s[i].plot(ast['mag1in'][Rinds1],ast['mag1out'][Rinds1]-ast['mag1in'][Rinds1],'.',color='blue')
        ax1s[i].plot(np.zeros(100) + mag1_cut,
                     np.linspace(-2, 10, 100),
                     color='green')
        ax1s[i].plot(np.zeros(100) + comp['comp50mag1'][i],
                     np.linspace(-2, 10, 100),
                     color='red')
        ax1s[i].xaxis.set_major_locator(MultipleLocator(2))
        ax1s[i].set_xlim(19, 27)
        ax1s[i].set_ylim(-2, 2)
    plt.savefig('ast_diff_mag1%s' % EXT)

    ax2s = graphics.setup_five_panel_plot(fig=2)
    [ax.set_xlabel('$%s$ Recovered' % filters[1]) for ax in ax2s]
    ax2s[0].set_ylabel('$%s$ Recovered-Input' % filters[1])
    axs = graphics.two_panel_plot_vert_bsquat()
    axs[0].set_xlabel('F336W Recovered')
    axs[1].set_ylabel('standard deviation')
    axs[0].set_ylabel('residual')
    for i in range(len(inds)):
        Rinds2 = list(set(rec2) & set(qc2) & set(inds[i]))
        #Rinds2 = list(set(rec2) & set(qc1) & set(qc2) & set(inds[i]))
        within2 = np.nonzero(
            (ast['mag2in'][Rinds2] <= comp['comp50mag2'][i] + pm)
            & (ast['mag2in'][Rinds2] >= comp['comp50mag2'][i] - pm))[0]
        mag2_cut = np.max(ast['mag2out'][Rinds2][within2])
        print mag2_cut
        ast_diff_test(ast['mag2out'][Rinds2],
                      ast['mag2in'][Rinds2],
                      axs=axs,
                      color=cols[i],
                      spacing=0.4)
        axs[0].set_ylim(-0.01, 0.1)
        ax2s[i].plot(ast['mag2out'][inds[i]],
                     ast['mag2out'][inds[i]] - ast['mag2in'][inds[i]],
                     '.',
                     color='black')
        ax2s[i].plot(ast['mag2out'][Rinds2],
                     ast['mag2out'][Rinds2] - ast['mag2in'][Rinds2],
                     '.',
                     color='blue')
        ax2s[i].plot(np.zeros(100) + mag2_cut,
                     np.linspace(-2, 10, 100),
                     color='green')
        ax2s[i].plot(np.zeros(100) + comp['comp50mag2'][i],
                     np.linspace(-2, 10, 100),
                     color='red')
        ax2s[i].xaxis.set_major_locator(MultipleLocator(2))
        ax2s[i].set_xlim(19, 27)
        ax2s[i].set_ylim(-2, 2)
    plt.savefig('ast_diff_mag2%s' % EXT)
    return
示例#7
0
def ast_LF(ast, **kwargs):
    cont_file = kwargs.get('cont_file')
    one_plot = kwargs.get('one_plot', True)
    powerlaw = lambda x, amp, index: amp * (x**index)
    import CrowdTest
    cols = kwargs.get('cols')
    double_plot = kwargs.get('double_plot', False)
    two_panel = kwargs.get('two_panel', True)
    lf_o = CrowdTest.load_saved_lf(
        filename=
        '/Users/phil/research/PHAT/code/UVBrights/diy_final/lf_data.dat')
    comp_data = kwargs['comp_data']
    mag1_cut = file_io.get_comp90mags(comp_data)[0][0]
    mag2_cut = file_io.get_comp90mags(comp_data)[1][0]
    print '# mag1_cut, mag2_cut', mag1_cut, mag2_cut
    if cont_file != None:
        cf = open(cont_file, 'w')
        cf.write('# mag1_cut, mag2_cut %.1f %.1f\n' % (mag1_cut, mag2_cut))
    from params import PHAT_DIR_EXTRA3
    contour = PHAT_DIR_EXTRA3 + 'contours_v3.reg'
    inds = calcs.points_inside_ds9_polygon(contour, ast['ra'], ast['dec'])
    filters = ('F275W', 'F336W')

    rec1 = recovered_asts(ast['mag1in'], ast['mag1out'])
    rec2 = recovered_asts(ast['mag2in'], ast['mag2out'])

    # quality cut on all asts
    qc1 = quality_cut(ast['sharp1'],
                      ast['snr1'],
                      ast['crowd1'],
                      ast['round1'],
                      loud=True,
                      **kwargs)

    qc2 = quality_cut(ast['sharp2'],
                      ast['snr2'],
                      ast['crowd2'],
                      ast['round2'],
                      loud=True,
                      **kwargs)
    spacing = .05
    if two_panel == True:
        ax1, ax2 = graphics.two_panel_plot(11, 5, 'x', 'x', '$\#$')
        axs = [ax1, ax2, ax1, ax2, ax1]
    for j, filter in enumerate(filters):
        fcolor = 'blue'
        if double_plot == True:
            axs, baxs = graphics.setup_ten_panel_plot(fig=j + 1, noy=True)
        elif two_panel == False:
            fig = j + 1
            if one_plot == True: fig = 1
            axs = graphics.setup_five_panel_plot(fig=fig, noy=True)
            if j != 0: fcolor = 'green'
        sortkeys = np.sort(lf_o[filter].keys())
        for k, region in enumerate(sortkeys):
            if filter == 'F336W':
                qc = qc2
                ast_mag_in = ast['mag2in']
                ast_mag_out = ast['mag2out']
                mag = mag2_cut
                if two_panel == True: axs[k] = ax2
                axs[k].set_xlim(26.1, 16)
                index = 16.943523372280001
                amp = 1e-17
            if filter == 'F275W':
                qc = qc1
                ast_mag_in = ast['mag1in']
                ast_mag_out = ast['mag1out']
                mag = mag1_cut
                if two_panel == True: axs[k] = ax1
                axs[k].set_xlim(25.9, 16)
                index = 19.246755161139998
                amp = 1e-20

            Rinds = list(set(rec1) & set(qc) & set(inds[k]))
            bins = np.arange(np.min(ast_mag_in[Rinds]),
                             np.max(ast_mag_in[Rinds]), spacing)
            hist = powerlaw(bins, 1e-17, 15)
            #hist = powerlaw(bins,amp,index)
            # paste tests here
            axs[k].plot(bins, hist, ls='steps', color='black')
            hist_o, bins_o = lf_o[filter][region].values()
            #axs[k].plot(bins_o,np.array(hist_o)*1e2,ls='steps',color=cols[k])
            # bin up input mag
            in1s = np.digitize(ast_mag_in[Rinds], bins)
            new_mags = np.array([])
            for i in range(len(bins)):
                if hist[i] == 0: continue
                if len(np.array(in1s == i).nonzero()[0]) <= 1: continue
                # output mag corresponding to the input mag bin
                ast_mag = ast_mag_out[Rinds][np.array(in1s == i).nonzero()[0]]
                # make cumulative distribution
                sort_ast = np.sort(ast_mag)
                yvals = np.arange(len(sort_ast)) / float(len(sort_ast))
                # interpolate it from 0,1
                f = scipy.interpolate.interp1d(yvals,
                                               sort_ast,
                                               bounds_error=False)
                # pick randomly from distribution the same number as in the current histogram
                new_mags = np.append(new_mags, f(np.random.random(hist[i])))
            # find new LF
            new_hist, lixo = np.histogram(new_mags, bins=bins)
            #print sum(new_hist), sum(hist)
            axs[k].plot(bins[1:], new_hist, ls='steps', color=fcolor, lw=2)
            ofst = 0.1
            #mag = closest_match(bins[1:],(new_hist-hist[1:])/hist[1:],.2)
            g = scipy.interpolate.interp1d(bins[1:],
                                           (new_hist - hist[1:]) / hist[1:])
            print filter, k, g(mag)
            if cont_file != None:
                cf.write('%s %i %f\n' % (filter, k, g(mag)))
            if double_plot == True:
                baxs[k].plot(np.zeros(10) + mag,
                             np.linspace(baxs[k].get_ylim()[0],
                                         baxs[k].get_ylim()[1], 10),
                             '--',
                             color='black',
                             label='%.2f' % g(mag))
                baxs[k].set_xlim(
                    np.max(ast_mag_in) - ofst,
                    np.min(ast_mag_in) + ofst)
                baxs[k].legend(loc=0, frameon=False)
                baxs[k].set_xlabel('$%s$' % filter)
                baxs[k].plot(bins[1:], (new_hist - hist[1:]) / hist[1:],
                             color='black',
                             lw=2,
                             ls='steps')
            else:
                if one_plot == True:
                    axs[k].set_xlabel(r'$\rm{mag}$')
                    if k != 0: axs[k].yaxis.set_major_formatter(nullfmt)
                else:
                    axs[k].set_xlabel('$%s$' % filter)
            axs[k].xaxis.set_major_locator(MultipleLocator(2))
            axs[k].xaxis.set_minor_locator(MultipleLocator(1))
            axs[k].set_ylim(1e1, 1e5)
            axs[k].plot(np.zeros(3) + mag,
                        np.logspace(1, 5, 3),
                        '--',
                        lw=2,
                        color=fcolor,
                        label='%.2f' % g(mag))
            axs[k].set_yscale('log')
        axs[0].set_ylabel('$\#$')
        axs[k].annotate(r'$F275W$', xy=(22, 5e4), fontsize=20, color='blue')
        axs[k].annotate(r'$F336W$', xy=(22, 3e4), fontsize=20, color='green')
        [ax.set_ylim(1e2, 1e5) for ax in axs]
        [ax.set_xlim(26, 19) for ax in axs]
        if double_plot == True: baxs[0].set_ylabel('(input-obs)/input')
        if one_plot != True:
            plt.figure(j + 1)
            plt.savefig('ast_lf_test_%s%s' % EXT % filter)
        else:
            plt.savefig('ast_lf_test%s' % EXT)
    if cont_file != None: cf.close()
    return axs