def hst_wfc3_pos_error():
    # Load up observed data:
    wd1_data = '/u/jlu/data/Wd1/hst/from_jay/EXPORT_WEST1.2012.02.04/wd1_catalog.fits'
    data = atpy.Table(wd1_data)

    # Determine median astrometric and photometric error as a function of
    # magnitude for both F814W and F125W. Use Y as a proxy for astrometric error.
    magBinSize = 0.25
    magBinCenter = np.arange(12, 28, magBinSize)
    yerr160w = np.zeros(len(magBinCenter), dtype=float)
    yerr125w = np.zeros(len(magBinCenter), dtype=float)
    merr160w = np.zeros(len(magBinCenter), dtype=float)
    merr125w = np.zeros(len(magBinCenter), dtype=float)

    for ii in range(len(magBinCenter)):
        mlo = magBinCenter[ii] - (magBinSize/2.0)
        mhi = magBinCenter[ii] + (magBinSize/2.0)

        idx160 = np.where((data.mag160 >= mlo) & (data.mag160 < mhi) &
                          (np.isnan(data.y2005_e) == False))[0]
        idx125 = np.where((data.mag125 >= mlo) & (data.mag125 < mhi) &
                          (np.isnan(data.y2010_e) == False))[0]

        if len(idx160) > 1:
            yerr160w[ii] = statsIter.mean(data.y2005_e[idx160], hsigma=3, lsigma=5, iter=10)
            merr160w[ii] = statsIter.mean(data.mag160_e[idx160], hsigma=3, lsigma=5, iter=10)
        else:
            yerr160w[ii] = np.nan
            merr160w[ii] = np.nan

        if len(idx125) > 1:
            yerr125w[ii] = statsIter.mean(data.y2010_e[idx125], hsigma=3, lsigma=5, iter=10)
            merr125w[ii] = statsIter.mean(data.mag125_e[idx125], hsigma=3, lsigma=5, iter=10)
        else:
            yerr125w[ii] = np.nan
            merr125w[ii] = np.nan



            
    py.clf()
    py.plot(magBinCenter, yerr160w*scale, 'b.', ms=10)
    py.xlabel('Magnitude')
    py.ylabel('F160W Positional Error (mas)')
    py.ylim(0, 5)
    py.xlim(12, 20)
    py.savefig(plotDir + 'avg_poserr_f160w.png')
def hst_wfc3_pos_error():
    # Load up observed data:
    wd1_data = "/u/jlu/data/Wd1/hst/from_jay/EXPORT_WEST1.2012.02.04/wd1_catalog.fits"
    data = atpy.Table(wd1_data)

    # Determine median astrometric and photometric error as a function of
    # magnitude for both F814W and F125W. Use Y as a proxy for astrometric error.
    magBinSize = 0.25
    magBinCenter = np.arange(12, 28, magBinSize)
    yerr160w = np.zeros(len(magBinCenter), dtype=float)
    yerr125w = np.zeros(len(magBinCenter), dtype=float)
    merr160w = np.zeros(len(magBinCenter), dtype=float)
    merr125w = np.zeros(len(magBinCenter), dtype=float)

    for ii in range(len(magBinCenter)):
        mlo = magBinCenter[ii] - (magBinSize / 2.0)
        mhi = magBinCenter[ii] + (magBinSize / 2.0)

        idx160 = np.where((data.mag160 >= mlo) & (data.mag160 < mhi) & (np.isnan(data.y2005_e) == False))[0]
        idx125 = np.where((data.mag125 >= mlo) & (data.mag125 < mhi) & (np.isnan(data.y2010_e) == False))[0]

        if len(idx160) > 1:
            yerr160w[ii] = statsIter.mean(data.y2005_e[idx160], hsigma=3, lsigma=5, iter=10)
            merr160w[ii] = statsIter.mean(data.mag160_e[idx160], hsigma=3, lsigma=5, iter=10)
        else:
            yerr160w[ii] = np.nan
            merr160w[ii] = np.nan

        if len(idx125) > 1:
            yerr125w[ii] = statsIter.mean(data.y2010_e[idx125], hsigma=3, lsigma=5, iter=10)
            merr125w[ii] = statsIter.mean(data.mag125_e[idx125], hsigma=3, lsigma=5, iter=10)
        else:
            yerr125w[ii] = np.nan
            merr125w[ii] = np.nan

    py.clf()
    py.plot(magBinCenter, yerr160w * scale, "b.", ms=10)
    py.xlabel("Magnitude")
    py.ylabel("F160W Positional Error (mas)")
    py.ylim(0, 5)
    py.xlim(12, 20)
    py.savefig(plotDir + "avg_poserr_f160w.png")
def map_of_errors():
    t = atpy.Table(workDir + '20.KS2_PMA/wd1_catalog.fits')

    xbins = np.arange(0, 4251, 250)
    ybins = np.arange(0, 4200, 250)

    xb2d, yb2d = np.meshgrid(xbins, ybins)

    xe_mean = np.zeros(xb2d.shape, dtype=float)
    ye_mean = np.zeros(yb2d.shape, dtype=float)
    me_mean = np.zeros(yb2d.shape, dtype=float)
    xe_std = np.zeros(xb2d.shape, dtype=float)
    ye_std = np.zeros(yb2d.shape, dtype=float)
    me_std = np.zeros(yb2d.shape, dtype=float)

    for xx in range(len(xbins)-1):
        for yy in range(len(ybins)-1):
            idx = np.where((t.x_160 > xbins[xx]) & (t.x_160 <= xbins[xx+1]) &
                           (t.y_160 > ybins[yy]) & (t.y_160 <= ybins[yy+1]) &
                           (t.xe_160 < 0.2) & (t.ye_160 < 0.2))[0]

            if len(idx) > 0:
                xe_mean[yy, xx] = statsIter.mean(t.xe_160[idx], hsigma=3, iter=5)
                ye_mean[yy, xx] = statsIter.mean(t.ye_160[idx], hsigma=3, iter=5)
                me_mean[yy, xx] = statsIter.mean(t.me_160[idx], hsigma=3, iter=5)
                xe_std[yy, xx] = statsIter.std(t.xe_160[idx], hsigma=3, iter=5)
                ye_std[yy, xx] = statsIter.std(t.ye_160[idx], hsigma=3, iter=5)
                me_std[yy, xx] = statsIter.std(t.me_160[idx], hsigma=3, iter=5)


    py.close('all')
    py.figure(1, figsize=(12, 6))
    py.clf()
    py.subplots_adjust(left=0.05, bottom=0.05)
    py.subplot(1, 2, 1)
    py.imshow(xe_mean, extent=[xbins[0], xbins[-1], ybins[0], ybins[-1]],
              vmin=0.01, vmax=0.07)
    py.colorbar(orientation='horizontal')
    py.title('X Error Mean')

    py.subplot(1, 2, 2)
    py.imshow(xe_std, extent=[xbins[0], xbins[-1], ybins[0], ybins[-1]],
              vmin=0.01, vmax=0.07)
    py.colorbar(orientation='horizontal')
    py.title('X Error Std')


    py.figure(2, figsize=(12, 6))
    py.clf()
    py.subplots_adjust(left=0.05, bottom=0.05)
    py.subplot(1, 2, 1)
    py.imshow(ye_mean, extent=[xbins[0], xbins[-1], ybins[0], ybins[-1]],
              vmin=0.01, vmax=0.07)
    py.colorbar(orientation='horizontal')
    py.title('Y Error Mean')

    py.subplot(1, 2, 2)
    py.imshow(ye_std, extent=[xbins[0], xbins[-1], ybins[0], ybins[-1]],
              vmin=0.01, vmax=0.07)
    py.colorbar(orientation='horizontal')
    py.title('Y Error Std')




    py.figure(3, figsize=(12, 6))
    py.clf()
    py.subplots_adjust(left=0.05, bottom=0.05)
    py.subplot(1, 2, 1)
    py.imshow(me_mean, extent=[xbins[0], xbins[-1], ybins[0], ybins[-1]],
              vmin=0.01, vmax=0.07)
    py.colorbar(orientation='horizontal')
    py.title('M Error Mean')

    py.subplot(1, 2, 2)
    py.imshow(me_std, extent=[xbins[0], xbins[-1], ybins[0], ybins[-1]],
              vmin=0.01, vmax=0.07)
    py.colorbar(orientation='horizontal')
    py.title('M Error Std')
def map_of_errors():
    t = atpy.Table(workDir + '20.KS2_PMA/wd1_catalog.fits')

    xbins = np.arange(0, 4251, 250)
    ybins = np.arange(0, 4200, 250)

    xb2d, yb2d = np.meshgrid(xbins, ybins)

    xe_mean = np.zeros(xb2d.shape, dtype=float)
    ye_mean = np.zeros(yb2d.shape, dtype=float)
    me_mean = np.zeros(yb2d.shape, dtype=float)
    xe_std = np.zeros(xb2d.shape, dtype=float)
    ye_std = np.zeros(yb2d.shape, dtype=float)
    me_std = np.zeros(yb2d.shape, dtype=float)

    for xx in range(len(xbins)-1):
        for yy in range(len(ybins)-1):
            idx = np.where((t.x_160 > xbins[xx]) & (t.x_160 <= xbins[xx+1]) &
                           (t.y_160 > ybins[yy]) & (t.y_160 <= ybins[yy+1]) &
                           (t.xe_160 < 0.2) & (t.ye_160 < 0.2))[0]

            if len(idx) > 0:
                xe_mean[yy, xx] = statsIter.mean(t.xe_160[idx], hsigma=3, iter=5)
                ye_mean[yy, xx] = statsIter.mean(t.ye_160[idx], hsigma=3, iter=5)
                me_mean[yy, xx] = statsIter.mean(t.me_160[idx], hsigma=3, iter=5)
                xe_std[yy, xx] = statsIter.std(t.xe_160[idx], hsigma=3, iter=5)
                ye_std[yy, xx] = statsIter.std(t.ye_160[idx], hsigma=3, iter=5)
                me_std[yy, xx] = statsIter.std(t.me_160[idx], hsigma=3, iter=5)


    py.close('all')
    py.figure(1, figsize=(12, 6))
    py.clf()
    py.subplots_adjust(left=0.05, bottom=0.05)
    py.subplot(1, 2, 1)
    py.imshow(xe_mean, extent=[xbins[0], xbins[-1], ybins[0], ybins[-1]],
              vmin=0.01, vmax=0.07)
    py.colorbar(orientation='horizontal')
    py.title('X Error Mean')

    py.subplot(1, 2, 2)
    py.imshow(xe_std, extent=[xbins[0], xbins[-1], ybins[0], ybins[-1]],
              vmin=0.01, vmax=0.07)
    py.colorbar(orientation='horizontal')
    py.title('X Error Std')


    py.figure(2, figsize=(12, 6))
    py.clf()
    py.subplots_adjust(left=0.05, bottom=0.05)
    py.subplot(1, 2, 1)
    py.imshow(ye_mean, extent=[xbins[0], xbins[-1], ybins[0], ybins[-1]],
              vmin=0.01, vmax=0.07)
    py.colorbar(orientation='horizontal')
    py.title('Y Error Mean')

    py.subplot(1, 2, 2)
    py.imshow(ye_std, extent=[xbins[0], xbins[-1], ybins[0], ybins[-1]],
              vmin=0.01, vmax=0.07)
    py.colorbar(orientation='horizontal')
    py.title('Y Error Std')




    py.figure(3, figsize=(12, 6))
    py.clf()
    py.subplots_adjust(left=0.05, bottom=0.05)
    py.subplot(1, 2, 1)
    py.imshow(me_mean, extent=[xbins[0], xbins[-1], ybins[0], ybins[-1]],
              vmin=0.01, vmax=0.07)
    py.colorbar(orientation='horizontal')
    py.title('M Error Mean')

    py.subplot(1, 2, 2)
    py.imshow(me_std, extent=[xbins[0], xbins[-1], ybins[0], ybins[-1]],
              vmin=0.01, vmax=0.07)
    py.colorbar(orientation='horizontal')
    py.title('M Error Std')
Esempio n. 5
0
def mass_luminosity():
    # Load up observed data:
    wd1_data = '/u/jlu/data/Wd1/hst/from_jay/EXPORT_WEST1.2012.02.04/wd1_catalog.fits'
    data = atpy.Table(wd1_data)

    iso = load_isochrone()

    # Determine median astrometric and photometric error as a function of
    # magnitude for both F814W and F125W. Use Y as a proxy for astrometric error.
    magBinSize = 0.25
    magBinCenter = np.arange(12, 28, magBinSize)
    yerr814w = np.zeros(len(magBinCenter), dtype=float)
    yerr125w = np.zeros(len(magBinCenter), dtype=float)
    merr814w = np.zeros(len(magBinCenter), dtype=float)
    merr125w = np.zeros(len(magBinCenter), dtype=float)

    for ii in range(len(magBinCenter)):
        mlo = magBinCenter[ii] - (magBinSize/2.0)
        mhi = magBinCenter[ii] + (magBinSize/2.0)

        idx814 = np.where((data.mag814 >= mlo) & (data.mag814 < mhi) &
                          (np.isnan(data.y2005_e) == False))[0]
        idx125 = np.where((data.mag125 >= mlo) & (data.mag125 < mhi) &
                          (np.isnan(data.y2010_e) == False))[0]

        if len(idx814) > 1:
            yerr814w[ii] = statsIter.mean(data.y2005_e[idx814], hsigma=3, lsigma=5, iter=10)
            merr814w[ii] = statsIter.mean(data.mag814_e[idx814], hsigma=3, lsigma=5, iter=10)
        else:
            yerr814w[ii] = np.nan
            merr814w[ii] = np.nan

        if len(idx125) > 1:
            yerr125w[ii] = statsIter.mean(data.y2010_e[idx125], hsigma=3, lsigma=5, iter=10)
            merr125w[ii] = statsIter.mean(data.mag125_e[idx125], hsigma=3, lsigma=5, iter=10)
        else:
            yerr125w[ii] = np.nan
            merr125w[ii] = np.nan

    # Assume we get an additional sqrt(3) by combining the 3 filters together.
    yerr125w /= math.sqrt(3.0)
            
    py.clf()
    py.plot(magBinCenter, yerr814w*scale, 'b.', ms=10,
            label='ACS-WFC F814W')
    py.plot(magBinCenter, yerr125w*scale, 'r.', ms=10,
            label='WFC3-IR F125W')
    py.xlabel('Magnitude')
    py.ylabel('Positional Error (mas)')
    py.legend(numpoints=1, loc='upper left')
    py.ylim(0, 5)
    py.savefig(plotDir + 'avg_poserr_f814w_vs_f125w.png')

    py.clf()
    py.plot(magBinCenter, merr814w, 'b.', ms=10,
            label='ACS-WFC F814W')
    py.plot(magBinCenter, merr125w, 'r.', ms=10,
            label='WFC3-IR F125W')
    py.xlabel('Magnitude')
    py.ylabel('Photometric Error (mag)')
    py.legend(numpoints=1, loc='upper left')
    py.ylim(0, 0.1)
    py.savefig(plotDir + 'avg_magerr_f814w_vs_f125w.png')

    idx814 = np.where((yerr814w*scale < 2) & (merr814w < 0.04))[0]
    idx125 = np.where((yerr125w*scale < 2) & (merr125w < 0.04))[0]

    lim814 = magBinCenter[idx814[-1]]
    lim125 = magBinCenter[idx125[-1]]

    print 'Limit for F814W data (pos err < 2 mas, mag err < 0.04: %5.2f' % \
        lim814
    print 'Limit for F125W data (pos err < 2 mas, mag err < 0.04: %5.2f' % \
        lim125

    ii814 = np.abs(iso.mag814w - lim814).argmin()
    ii125 = np.abs(iso.mag125w - lim125).argmin()

    # massLim814 = iso.M[ii814]
    # magLim814 = iso.mag814w[ii814]
    massLim814 = 0.85
    magLim814 = 23.5
    massLim125 = iso.M[ii125]
    magLim125 = iso.mag125w[ii125]

    py.clf()
    py.semilogx(iso.M, iso.mag814w, 'b-', ms=2, label='ACS-WFC F814W',
                linewidth=2)
    py.semilogx(iso.M, iso.mag125w, 'r-', ms=2, label='WFC3-IR F125W',
                linewidth=2)
    py.gca().invert_yaxis()
    py.plot([massLim814, massLim814], [magLim814-2, magLim814+2], 'b-',
            linewidth=4)
    py.plot([massLim125, massLim125], [magLim125-2, magLim125+2], 'r-',
            linewidth=4)
    ar1 = py.Arrow(massLim814, magLim814+0.1, 1.0, 0, color='blue')
    ar2 = py.Arrow(massLim125, magLim125+0.1, 0.15, 0, color='red')
    py.gca().add_patch(ar1)
    py.gca().add_patch(ar2)
    py.xlabel('Mass (Msun)')
    py.ylabel('Magnitude (F814W or F125W)')
    py.legend(loc='upper left')
    py.savefig(plotDir + 'mass_magnitude_with_limits.png')

    # Also plot a color-magnitude diagram and show the two mass limits
    py.clf()
    py.plot(data.mag814 - data.mag160, data.mag814, 'k.', ms=2)
    py.plot(iso.mag814w - iso.mag160w, iso.mag814w, 'b-', ms=2,
            linewidth=2)
    py.plot(iso.mag814w[ii814] - iso.mag160w[ii814], iso.mag814w[ii814],
            'b^', ms=10)
    py.plot(iso.mag814w[ii125] - iso.mag160w[ii125], iso.mag814w[ii125],
            'r^', ms=10)
    py.gca().invert_yaxis()
    py.xlim(0, 10)
    py.ylim(28, 13)
    py.xlabel('F814W - F160W')
    py.ylabel('F814W')
    py.savefig(plotDir + 'cmd_optical_iso.png')

    py.clf()
    py.plot(data.mag125 - data.mag160, data.mag125, 'k.', ms=2)
    py.plot(iso.mag125w - iso.mag160w, iso.mag125w, 'r-', ms=2,
            linewidth=2)
    py.plot(iso.mag125w[ii814] - iso.mag160w[ii814], iso.mag125w[ii814],
            'b^', ms=10)
    py.plot(iso.mag125w[ii125] - iso.mag160w[ii125], iso.mag125w[ii125],
            'r^', ms=10)
    py.gca().invert_yaxis()
    py.xlim(0, 3)
    py.ylim(24, 10)
    py.xlabel('F125W - F160W')
    py.ylabel('F125W')
    py.savefig(plotDir + 'cmd_infrared_iso.png')
Esempio n. 6
0
def cmd():
    # Load up observed data:
    wd1_data = '/u/jlu/data/Wd1/hst/from_jay/EXPORT_WEST1.2012.02.04/wd1_catalog.fits'
    data = atpy.Table(wd1_data)

    # Load up model isochrone
    iso = load_isochrone()

    # Select a region in NIR CMD for defining cluster center and
    # velocity dispersion.
    magLo = 15.0
    magHi = 16.5
    colorLo = 0.8
    colorHi = 0.88
    
    # Pull out a few key masses along the isochrone
    idxM1 = np.abs(iso.M - 1.0).argmin()
    idxM01 = np.abs(iso.M - 0.1).argmin()

    colM1 = iso.mag125w[idxM1] - iso.mag160w[idxM1]
    magM1 = iso.mag125w[idxM1]
    colM01 = iso.mag125w[idxM01] - iso.mag160w[idxM01]
    magM01 = iso.mag125w[idxM01]

    # Plot up the complete near-infrared color magnitude diagram, select out
    # a color region to show cluster members in the VPD.
    py.clf()
    py.subplots_adjust(left=0.11)
    py.plot(data.mag125 - data.mag160, data.mag125, 'k.', ms=2)
    py.plot(iso.mag125w - iso.mag160w, iso.mag125w, 'r-', ms=2,
            color='red', linewidth=2)
    py.plot([colM1], [magM1], 'r*', ms=25)
    py.plot([colM01], [magM01],'r*', ms=25)
    py.text(colM1+0.1, magM1, r'1 M$_\odot$', color='red', 
            fontweight='normal', fontsize=28)
    py.text(colM01+0.1, magM01, r'0.1 M$_\odot$', color='red',
            fontweight='normal', fontsize=28)
    py.gca().invert_yaxis()

    py.plot([colorLo, colorHi, colorHi, colorLo, colorLo,],
            [magLo, magLo, magHi, magHi, magLo],
            'k-', color='lightgreen', linewidth=4)

    py.xlim(0, 2)
    py.ylim(23, 12)
    py.xlabel('F125W - F160W')
    py.ylabel('F125W')
    py.savefig(plotDir + 'cmd_infrared_vpd_box.png')

    # Plot up the VPD with the CMD selected highlighted.
    # Also trim out anything without good astrometric and
    # photometric errors.
    idx = np.where((data.x2005_e*scale < 2) & (data.y2005_e*scale < 2) &
                   (data.x2010_e*scale < 2) & (data.y2010_e*scale < 2) &
                   (data.mag814_e < 0.04) & (data.mag125_e < 0.04))[0]

    mag = data.mag125[idx]
    color = data.mag125[idx] - data.mag160[idx]
    tdx = np.where((mag > magLo) & (mag < magHi) & 
                   (color > colorLo) & (color < colorHi))[0]

    dx_mean = statsIter.mean(data.dx[idx[tdx]], hsigma=5, lsigma=5, iter=5)
    dy_mean = statsIter.mean(data.dy[idx[tdx]], hsigma=5, lsigma=5, iter=5)
    dx_std = statsIter.std(data.dx[idx[tdx]], hsigma=5, lsigma=5, iter=5)
    dy_std = statsIter.std(data.dy[idx[tdx]], hsigma=5, lsigma=5, iter=5)

    print 'Cluster Center: '
    print '   x = %5.1f +/- %5.1f mas' % (dx_mean, dx_std)
    print '   y = %5.1f +/- %5.1f mas' % (dy_mean, dy_std)

    # Select out cluster members
    dr = np.hypot(data.dx[idx] - dx_mean, data.dy[idx] - dy_mean)
    dr_cluster = np.max([dx_std, dy_std])
    inCluster = np.where(dr <= dr_cluster)[0]

    dt = 2010.6521 - 2005.4846

    py.clf()
    py.subplots_adjust(left=0.13)
    py.plot(data.dx[idx]/dt, data.dy[idx]/dt, 'k.', ms=4)
    py.plot(data.dx[idx[tdx]]/dt, data.dy[idx[tdx]]/dt,
            'b.', color='lightgreen', ms=10)
    circ = py.Circle([dx_mean/dt, dy_mean/dt], radius=dr_cluster/dt,
                     fc='none', ec='yellow', linewidth=4, zorder=10)
    py.gca().add_patch(circ)
    lim = 20
    py.axis([-lim/dt, lim/dt, -lim/dt, lim/dt])
    py.xlabel('X Proper Motion (mas/yr)')
    py.ylabel('Y Proper Motion (mas/yr)')
    py.title('2010 F814W - 2005 F125W')
    py.savefig(plotDir + 'vpd_cmd_selected.png')
    
    # Plot up the complete near-infrared color magnitude diagram, 
    # and overplot cluster members as determined from proper motions
    # from optical/IR data set.
    py.clf()
    py.subplots_adjust(left=0.11)
    py.plot(data.mag125 - data.mag160, data.mag125, 'k.', ms=2)
    py.plot(data.mag125[idx[inCluster]] - data.mag160[idx[inCluster]],
            data.mag125[idx[inCluster]], 'y.', ms=5)
    py.plot(iso.mag125w - iso.mag160w, iso.mag125w, 'r-', ms=2,
            linewidth=2)
    py.plot([colM1], [magM1], 'r*', ms=25)
    py.plot([colM01], [magM01],'r*', ms=25)
    py.text(colM1+0.1, magM1, r'1 M$_\odot$', color='red', 
            fontweight='normal', fontsize=28)
    py.text(colM01+0.1, magM01, r'0.1 M$_\odot$', color='red',
            fontweight='normal', fontsize=28)
    py.gca().invert_yaxis()
    py.xlim(0, 2)
    py.ylim(23, 12)
    py.xlabel('F125W - F160W')
    py.ylabel('F125W')
    py.savefig(plotDir + 'cmd_infrared_members.png')
Esempio n. 7
0
def plot_final_synthesis():
    data = atpy.Table(workDir + 'gal1_master.txt', type='ascii')
    img = pyfits.getdata(workDir + 'gal1_img.fits')

    imgBkg = statsIter.mean(img.flatten(), hsigma=5, iter=1, verbose=True)
    imgStd = statsIter.std(img.flatten(), hsigma=5, iter=1, verbose=True)
    print 'Image mean = %5.1f  std = %5.1f' % (imgBkg, imgStd)

    fluxBins = np.arange(0, 500, 8)
    velBins = np.arange(-500, 801, 50)
    n_bins = np.arange(10, 60, 2)

    npix, fluxEdges = np.histogram(img.flatten(), bins=fluxBins)
    nstars, binEdges = np.histogram(data.vel, bins=velBins)
    n, b = np.histogram(nstars, bins=n_bins)

    bkg = np.mean(nstars)
    noise = np.std(nstars)
    print 'First Iteration:  mean = %5.1f, std = %5.1f' % (bkg, noise)

    # Do one round of rejection
    idx = np.where(nstars < (bkg + (3.0 * noise)))[0]
    bkg = np.mean(nstars[idx])
    noise = np.std(nstars[idx])
    print 'Second Iteration: mean = %5.1f, std = %5.1f' % (bkg, noise)

    gaussianCDF = scipy.stats.norm.cdf(b, loc=bkg, scale=noise)
    gaussianPDF = np.diff(gaussianCDF) * n.sum()
    gaussianBinCenters = n_bins[0:-1] + np.diff(n_bins) / 2.0

    fluxBinsModel = np.arange(0, 500, 1)
    fluxNormCDF = scipy.stats.norm.cdf(fluxBinsModel, loc=imgBkg, scale=imgStd)
    fluxNormPDF = np.diff(fluxNormCDF)
    fluxNormPDF *= npix.max() / fluxNormPDF.max()
    fluxBinCenters = fluxBinsModel[0:-1] + np.diff(fluxBinsModel) / 2.0

    # Images
    py.figure(1)
    py.clf()
    py.subplots_adjust(left=0.22)
    py.subplot(2, 1, 1)
    py.hist(img.flatten(), histtype='step', bins=fluxBins)
    py.plot(fluxBinCenters, fluxNormPDF, 'g-')
    py.title('Field #1')
    py.xlim(0, 200)

    py.subplot(2, 1, 2)
    py.hist(img.flatten(), histtype='step', bins=fluxBins)
    py.plot(fluxBinCenters, fluxNormPDF, 'g-')
    py.ylim(0, 1000)
    py.xlim(0, 200)
    py.xlabel('Flux')
    py.savefig(workDir + 'final_syn_gal1_flux_hist.png')

    py.figure(1)
    py.subplots_adjust(left=0.15)
    py.clf()
    py.hist(data.vel, bins=velBins, histtype='step')
    py.xlim(-500, 800)
    py.xlabel('Velocity (km/s)')
    py.ylabel('Number of Stars')
    py.savefig(workDir + 'final_syn_gal1_vel_hist_no_lines.png')

    py.axhline(bkg, linestyle='-', linewidth=3, color='black')
    py.axhline(bkg + noise, linestyle='--', linewidth=3, color='black')
    py.axhline(bkg - noise, linestyle='--', linewidth=3, color='black')
    py.savefig(workDir + 'final_syn_gal1_vel_hist.png')

    py.figure(2)
    py.clf()
    py.subplots_adjust(left=0.15)
    (n, b, p) = py.hist(nstars, histtype='step', bins=n_bins, label='Data')
    py.ylim(0, n.max() * 1.3)
    py.xlim(b.min(), b.max())
    py.xlabel('Number of Stars per Velocity Bin')
    py.ylabel('Number of Velocity Bins')

    print gaussianPDF.max(), n.max()

    py.plot(gaussianBinCenters, gaussianPDF, label='Gaussian Fit')
    axLim = py.axis()
    py.axvline(bkg, linestyle='-', color='black', linewidth=3)
    py.arrow(bkg + 1 * noise,
             axLim[3],
             0,
             -0.5,
             color='black',
             linewidth=3,
             head_width=1,
             head_length=0.1)
    py.arrow(bkg + 2 * noise,
             axLim[3],
             0,
             -0.5,
             color='black',
             linewidth=3,
             head_width=1,
             head_length=0.1)
    py.arrow(bkg + 3 * noise,
             axLim[3],
             0,
             -0.5,
             color='black',
             linewidth=3,
             head_width=1,
             head_length=0.1)

    py.text(bkg + 1 * noise,
            axLim[3] + 0.15,
            r'$1\sigma$',
            horizontalalignment='center',
            verticalalignment='bottom')
    py.text(bkg + 2 * noise,
            axLim[3] + 0.15,
            r'$2\sigma$',
            horizontalalignment='center',
            verticalalignment='bottom')
    py.text(bkg + 3 * noise,
            axLim[3] + 0.15,
            r'$3\sigma$',
            horizontalalignment='center',
            verticalalignment='bottom')

    py.savefig(workDir + 'final_syn_gal1_vel_hist_hist.png')
def plot_vector_diff(undersample):
    """
    Make a vector plot of the differences between the input positions
    and the output positions after running starfinder.
    """
    # Dimensions of new grid
    npsf_side = get_npsf_side(undersample)
    suffix = '%d_%d' % (npsf_side, npsf_side)

    s = starset.StarSet('grid_' + suffix + '/align_' + suffix)

    cnt = s.getArray('velCnt')
    mag = s.getArray('mag')
    idx = np.where((cnt == 2) & (mag < 15))[0]
    print 'Using {0} stars out of {1}'.format(len(idx), len(cnt))

    newStars = [s.stars[ii] for ii in idx]
    s.stars = newStars

    x0 = s.getArrayFromEpoch(0, 'xorig')
    y0 = s.getArrayFromEpoch(0, 'yorig')
    x1 = s.getArrayFromEpoch(1, 'xorig')
    y1 = s.getArrayFromEpoch(1, 'yorig')

    dx = x1 - x0
    dy = y1 - y0
    dr = np.hypot(dx, dy)

    # Boundaries
    t = atpy.Table('svpar_' + suffix + '.txt', type='ascii')
    lx = t.col1
    ux = t.col2
    ly = t.col3
    uy = t.col4
    xedges = np.unique(np.append(lx, ux))
    yedges = np.unique(np.append(ly, uy))

    py.clf()
    q = py.quiver(x0, y0, dx, dy, scale=0.2)
    py.xlim(0, 1200)
    py.ylim(0, 1200)
    py.quiverkey(q,
                 0.5,
                 0.97,
                 0.02,
                 '0.02 pixel (0.2 mas)',
                 color='red',
                 coordinates='axes')

    for xx in xedges:
        py.axvline(xx, linestyle='--')
    for yy in yedges:
        py.axhline(yy, linestyle='--')

    py.savefig('plots/vec_diff_' + suffix + '.png')

    xmean = statsIter.mean(dx, hsigma=5, lsigma=5, iter=5)
    ymean = statsIter.mean(dy, hsigma=5, lsigma=5, iter=5)
    rmean = statsIter.mean(dr, hsigma=5, lsigma=5, iter=5)
    xstd = statsIter.std(dx, hsigma=5, lsigma=5, iter=5)
    ystd = statsIter.std(dy, hsigma=5, lsigma=5, iter=5)
    rstd = statsIter.std(dr, hsigma=5, lsigma=5, iter=5)
    print('Mean Delta-X: {0:9.5f} +/- {1:9.5f} pixels'.format(xmean, xstd))
    print('Mean Delta-Y: {0:9.5f} +/- {1:9.5f} pixels'.format(ymean, ystd))
    print('Mean Delta-R: {0:9.5f} +/- {1:9.5f} pixels'.format(rmean, rstd))
    f_name = 'diff_stats_' + suffix + '.txt'
    f_stat = open(f_name, 'w')
    hdr = '#{0:>5s} {1:>10s} {2:>10s} {3:>10s} {4:>10s} {5:>10s} {6:>10s}\n'
    fmt = '{0:6d} {1:10.5f} {2:10.5f} {3:10.5f} {4:10.5f} {5:10.5f} {6:10.5f}\n'
    f_stat.write(
        hdr.format('Npsf', 'Xmean', 'Ymean', 'Rmean', 'Xstd', 'Ystd', 'Rstd'))
    f_stat.write(fmt.format(npsf_side, xmean, ymean, rmean, xstd, ystd, rstd))
    f_stat.close()

    return f_name
Esempio n. 9
0
def plot_comparison(tableSuffix,
                    posLim=0.15,
                    magLimits=[-13, -1],
                    errLim=0.04,
                    quiverScale=2):
    """
    Load up starlists produced by matchup_ks2_pass1() and plot some parameters
    of interest. Just pass in the filter suffix (e.g. "_f0"). Plots include:

    delta-x vs. m
    delta-y vs. m
    delta-m vs. m
    uncertainties vs. m for both lists
    me vs. m for stars in ks2 vs. stars not in ks2
    """
    stars = atpy.Table('stars_ks2_pass1%s.fits' % tableSuffix)
    other = atpy.Table('stars_pass1_only%s.fits' % tableSuffix)

    print '%6d stars in ks2' % len(stars)
    print '%6d stars not found in ks2' % len(other)

    #####
    # Plot delta-x vs. m
    #####
    dx = stars.x_ks2 - stars.x_pass1
    dxe = stars.xe_ks2

    #py.close('all')

    py.figure(1)
    py.clf()
    ax1 = py.subplot(2, 1, 1)
    py.plot(stars.m_pass1, dx, 'k.', ms=2)
    py.ylabel('dx (pix)')
    py.ylim(-posLim, posLim)
    py.axhline(y=0, color='r', linewidth=2)
    py.title('KS2 - One Pass')

    ax2 = py.subplot(2, 1, 2, sharex=ax1)
    py.plot(stars.m_pass1, dx / dxe, 'k.', ms=2)
    py.ylabel('dx/dxe (sigma)')
    py.ylim(-3, 3)
    py.axhline(y=0, color='r', linewidth=2)
    py.xlabel('Magnitude')
    py.xlim(magLimits[0], magLimits[1])
    py.savefig('plot_dx_m%s.png' % tableSuffix)

    #####
    # Plot delta-y vs. m
    #####
    dy = stars.y_ks2 - stars.y_pass1
    dye = stars.ye_ks2

    py.figure(2)
    py.clf()
    ax1 = py.subplot(2, 1, 1)
    py.plot(stars.m_pass1, dy, 'k.', ms=2)
    py.ylabel('dy (pix)')
    py.ylim(-posLim, posLim)
    py.axhline(y=0, color='r', linewidth=2)
    py.title('KS2 - One Pass')

    ax2 = py.subplot(2, 1, 2, sharex=ax1)
    py.plot(stars.m_pass1, dy / dye, 'k.', ms=2)
    py.ylabel('dy/dye (sigma)')
    py.ylim(-3, 3)
    py.axhline(y=0, color='r', linewidth=2)
    py.xlabel('Magnitude')
    py.xlim(magLimits[0], magLimits[1])

    py.savefig('plot_dy_m%s.png' % tableSuffix)

    #####
    # Plot delta-m vs. m
    #####
    dm = stars.m_ks2 - stars.m_pass1
    dme = stars.me_ks2

    py.figure(3)
    py.clf()
    ax1 = py.subplot(2, 1, 1)
    py.plot(stars.m_pass1, dm, 'k.', ms=2)
    py.ylabel('dm (mag)')
    py.ylim(-0.1, 0.1)
    py.axhline(y=0, color='r', linewidth=2)
    py.title('KS2 - One Pass')

    ax2 = py.subplot(2, 1, 2, sharex=ax1)
    py.plot(stars.m_pass1, dm / dme, 'k.', ms=2)
    py.ylabel('dm/dme (sigma)')
    py.ylim(-3, 3)
    py.axhline(y=0, color='r', linewidth=2)
    py.xlabel('Magnitude')
    py.xlim(magLimits[0], magLimits[1])

    py.savefig('plot_dm_m%s.png' % tableSuffix)

    #####
    # Plot differences in errors vs. m
    #####
    dxerr = stars.xe_ks2 - stars.xe_pass1
    dyerr = stars.ye_ks2 - stars.ye_pass1
    dmerr = stars.me_ks2 - stars.me_pass1

    py.close(4)
    py.figure(4, figsize=(9, 12))
    py.clf()
    py.subplots_adjust(left=0.12, bottom=0.07, top=0.95)
    ax1 = py.subplot(3, 1, 1)
    py.plot(stars.m_ks2, dxerr, 'k.', ms=2)
    py.ylabel('xe diff')
    py.ylim(-0.05, 0.05)
    py.axhline(y=0, color='r', linewidth=2)
    py.title('KS2 - One Pass')

    ax2 = py.subplot(3, 1, 2, sharex=ax1)
    py.plot(stars.m_ks2, dyerr, 'k.', ms=2)
    py.ylabel('ye diff')
    py.ylim(-0.05, 0.05)
    py.axhline(y=0, color='r', linewidth=2)

    ax3 = py.subplot(3, 1, 3, sharex=ax1)
    py.plot(stars.m_ks2, dmerr, 'k.', ms=2)
    py.ylabel('me diff')
    py.ylim(-0.05, 0.05)
    py.axhline(y=0, color='r', linewidth=2)

    py.xlabel('Magnitude')
    py.xlim(magLimits[0], magLimits[1])

    py.savefig('plot_compare_errors%s.png' % tableSuffix)

    #####
    # Plot stars that are NOT in KS2 vs. those that are.
    #####
    py.figure(5)
    py.clf()

    py.semilogy(stars.m_pass1, stars.me_pass1, 'k.', label='In KS2', ms=2)
    py.semilogy(other.m, other.me, 'r.', label='Not In KS2', ms=3)
    py.legend(numpoints=1, loc='upper left')
    py.xlabel('Magnitude')
    py.xlim(-23, -1)
    py.ylabel('Magnitude Error')
    py.ylim(0.004, 0.1)
    py.savefig('plot_m_me_others%s.png' % tableSuffix)

    #####
    # Plot vector point diagram of positional differences
    #####
    idx = np.where((stars.m_ks2 < -5) & (np.abs(dx) < posLim)
                   & (np.abs(dy) < posLim) & (stars.xe_pass1 < errLim)
                   & (stars.ye_pass1 < errLim))[0]

    py.figure(6)
    py.clf()

    py.plot(dx[idx], dy[idx], 'k.', ms=2)
    py.xlabel('X KS2 - One Pass')
    py.ylabel('Y KS2 - One Pass')
    py.axis([-posLim, posLim, -posLim, posLim])
    py.savefig('plot_dxdy_vpd%s.png' % tableSuffix)

    #####
    # Plot vector field of positional differences
    #####
    py.figure(7)
    py.clf()

    q = py.quiver(stars.x_pass1[idx],
                  stars.y_pass1[idx],
                  dx[idx],
                  dy[idx],
                  scale=quiverScale)
    py.quiverkey(q, 0.5, 0.95, 0.05, '0.05 pix', color='red')
    py.title('KS2 - One Pass')
    py.xlabel('X (pixels)')
    py.xlabel('Y (pixels)')
    py.savefig('plot_dxdy_quiver%s.png' % tableSuffix)

    #####
    # Print out some statistics
    #####
    idx = np.where(stars.m_ks2 < -5)[0]
    print 'Mean dx = %7.3f +/- %7.3f (pix)' % \
        (statsIter.mean(dx[idx], lsigma=4, hsigma=4, iter=5),
         statsIter.std(dx[idx], lsigma=4, hsigma=4, iter=5))
    print 'Mean dy = %7.3f +/- %7.3f (pix)' % \
        (statsIter.mean(dy[idx], lsigma=4, hsigma=4, iter=5),
         statsIter.std(dy[idx], lsigma=4, hsigma=4, iter=5))
    print 'Mean dxerr = %7.3f +/- %7.3f (pix)' % \
        (statsIter.mean(dxerr[idx], lsigma=4, hsigma=4, iter=5),
         statsIter.std(dxerr[idx], lsigma=4, hsigma=4, iter=5))
    print 'Mean dyerr = %7.3f +/- %7.3f (pix)' % \
        (statsIter.mean(dyerr[idx], lsigma=4, hsigma=4, iter=5),
         statsIter.std(dyerr[idx], lsigma=4, hsigma=4, iter=5))
Esempio n. 10
0
def plot_comparison(tableSuffix, posLim=0.15, magLimits=[-13,-1], errLim=0.04,
                    quiverScale=2):
    """
    Load up starlists produced by matchup_ks2_pass1() and plot some parameters
    of interest. Just pass in the filter suffix (e.g. "_f0"). Plots include:

    delta-x vs. m
    delta-y vs. m
    delta-m vs. m
    uncertainties vs. m for both lists
    me vs. m for stars in ks2 vs. stars not in ks2
    """
    stars = atpy.Table('stars_ks2_pass1%s.fits' % tableSuffix)
    other = atpy.Table('stars_pass1_only%s.fits' % tableSuffix)

    print '%6d stars in ks2' % len(stars)
    print '%6d stars not found in ks2' % len(other)

    #####
    # Plot delta-x vs. m
    #####
    dx = stars.x_ks2 - stars.x_pass1
    dxe = stars.xe_ks2

    #py.close('all')

    py.figure(1)
    py.clf()
    ax1 = py.subplot(2, 1, 1)
    py.plot(stars.m_pass1, dx, 'k.', ms=2)
    py.ylabel('dx (pix)')
    py.ylim(-posLim, posLim)
    py.axhline(y=0, color='r', linewidth=2)
    py.title('KS2 - One Pass')

    ax2 = py.subplot(2, 1, 2, sharex=ax1)
    py.plot(stars.m_pass1, dx/dxe, 'k.', ms=2)
    py.ylabel('dx/dxe (sigma)')
    py.ylim(-3, 3)
    py.axhline(y=0, color='r', linewidth=2)
    py.xlabel('Magnitude')
    py.xlim(magLimits[0], magLimits[1])
    py.savefig('plot_dx_m%s.png' % tableSuffix)

    
    #####
    # Plot delta-y vs. m
    #####
    dy = stars.y_ks2 - stars.y_pass1
    dye = stars.ye_ks2

    py.figure(2)
    py.clf()
    ax1 = py.subplot(2, 1, 1)
    py.plot(stars.m_pass1, dy, 'k.', ms=2)
    py.ylabel('dy (pix)')
    py.ylim(-posLim, posLim)
    py.axhline(y=0, color='r', linewidth=2)
    py.title('KS2 - One Pass')

    ax2 = py.subplot(2, 1, 2, sharex=ax1)
    py.plot(stars.m_pass1, dy/dye, 'k.', ms=2)
    py.ylabel('dy/dye (sigma)')
    py.ylim(-3, 3)
    py.axhline(y=0, color='r', linewidth=2)
    py.xlabel('Magnitude')
    py.xlim(magLimits[0], magLimits[1])

    py.savefig('plot_dy_m%s.png' % tableSuffix)
    
    #####
    # Plot delta-m vs. m
    #####
    dm = stars.m_ks2 - stars.m_pass1
    dme = stars.me_ks2

    py.figure(3)
    py.clf()
    ax1 = py.subplot(2, 1, 1)
    py.plot(stars.m_pass1, dm, 'k.', ms=2)
    py.ylabel('dm (mag)')
    py.ylim(-0.1, 0.1)
    py.axhline(y=0, color='r', linewidth=2)
    py.title('KS2 - One Pass')

    ax2 = py.subplot(2, 1, 2, sharex=ax1)
    py.plot(stars.m_pass1, dm/dme, 'k.', ms=2)
    py.ylabel('dm/dme (sigma)')
    py.ylim(-3, 3)
    py.axhline(y=0, color='r', linewidth=2)
    py.xlabel('Magnitude')
    py.xlim(magLimits[0], magLimits[1])

    py.savefig('plot_dm_m%s.png' % tableSuffix)


    #####
    # Plot differences in errors vs. m
    #####
    dxerr = stars.xe_ks2 - stars.xe_pass1
    dyerr = stars.ye_ks2 - stars.ye_pass1
    dmerr = stars.me_ks2 - stars.me_pass1
    
    py.close(4)
    py.figure(4, figsize=(9,12))
    py.clf()
    py.subplots_adjust(left=0.12, bottom=0.07, top=0.95)
    ax1 = py.subplot(3, 1, 1)
    py.plot(stars.m_ks2, dxerr, 'k.', ms=2)
    py.ylabel('xe diff')
    py.ylim(-0.05, 0.05)
    py.axhline(y=0, color='r', linewidth=2)
    py.title('KS2 - One Pass')

    ax2 = py.subplot(3, 1, 2, sharex=ax1)
    py.plot(stars.m_ks2, dyerr, 'k.', ms=2)
    py.ylabel('ye diff')
    py.ylim(-0.05, 0.05)
    py.axhline(y=0, color='r', linewidth=2)

    ax3 = py.subplot(3, 1, 3, sharex=ax1)
    py.plot(stars.m_ks2, dmerr, 'k.', ms=2)
    py.ylabel('me diff')
    py.ylim(-0.05, 0.05)
    py.axhline(y=0, color='r', linewidth=2)

    py.xlabel('Magnitude')
    py.xlim(magLimits[0], magLimits[1])

    py.savefig('plot_compare_errors%s.png' % tableSuffix)
    
    #####
    # Plot stars that are NOT in KS2 vs. those that are.
    #####
    py.figure(5)
    py.clf()

    py.semilogy(stars.m_pass1, stars.me_pass1, 'k.', label='In KS2', ms=2)
    py.semilogy(other.m, other.me, 'r.', label='Not In KS2', ms=3)
    py.legend(numpoints=1, loc='upper left')
    py.xlabel('Magnitude')
    py.xlim(-23, -1)
    py.ylabel('Magnitude Error')
    py.ylim(0.004, 0.1)
    py.savefig('plot_m_me_others%s.png' % tableSuffix)


    #####
    # Plot vector point diagram of positional differences
    #####
    idx = np.where((stars.m_ks2 < -5) & (np.abs(dx) < posLim) & (np.abs(dy) < posLim) &
                   (stars.xe_pass1 < errLim) & (stars.ye_pass1 < errLim))[0]

    py.figure(6)
    py.clf()

    py.plot(dx[idx], dy[idx], 'k.', ms=2)
    py.xlabel('X KS2 - One Pass')
    py.xlabel('Y KS2 - One Pass')
    py.axis([-posLim, posLim, -posLim, posLim])
    py.savefig('plot_dxdy_vpd%s.png' % tableSuffix)


    #####
    # Plot vector field of positional differences
    #####
    py.figure(7)
    py.clf()

    q = py.quiver(stars.x_pass1[idx], stars.y_pass1[idx], dx[idx], dy[idx], scale=quiverScale)
    py.quiverkey(q, 0.5, 0.95, 0.05, '0.05 pix', color='red')
    py.title('KS2 - One Pass')
    py.xlabel('X (pixels)')
    py.xlabel('Y (pixels)')
    py.savefig('plot_dxdy_quiver%s.png' % tableSuffix)

    #####
    # Print out some statistics
    #####
    idx = np.where(stars.m_ks2 < -5)[0]
    print 'Mean dx = %7.3f +/- %7.3f (pix)' % \
        (statsIter.mean(dx[idx], lsigma=4, hsigma=4, iter=5),
         statsIter.std(dx[idx], lsigma=4, hsigma=4, iter=5))
    print 'Mean dy = %7.3f +/- %7.3f (pix)' % \
        (statsIter.mean(dy[idx], lsigma=4, hsigma=4, iter=5),
         statsIter.std(dy[idx], lsigma=4, hsigma=4, iter=5))
    print 'Mean dxerr = %7.3f +/- %7.3f (pix)' % \
        (statsIter.mean(dxerr[idx], lsigma=4, hsigma=4, iter=5),
         statsIter.std(dxerr[idx], lsigma=4, hsigma=4, iter=5))
    print 'Mean dyerr = %7.3f +/- %7.3f (pix)' % \
        (statsIter.mean(dyerr[idx], lsigma=4, hsigma=4, iter=5),
         statsIter.std(dyerr[idx], lsigma=4, hsigma=4, iter=5))
Esempio n. 11
0
def plot_spie_figure3():
    """
    Pass in a starlist (stack of starlists) and calculate the
    astrometric and photometric errors per star. Then plot them.
    """
    align_dir = '/g/lu/data/gsaoi/commission/reduce/ngc1851/align_2013_03_04_G2/'
    plot_dir = '/u/jlu/doc/papers/proceed_2014_spie/'
    
    from jlu.gsaoi import stars as stars_obj
    stars2 = stars_obj.Starlist(align_dir + 'align_a2')
    stars3 = stars_obj.Starlist(align_dir + 'align_a3')
    stars4 = stars_obj.Starlist(align_dir + 'align_a4')

    stars_all = [stars2, stars3, stars4]

    py.close('all')
    f, ax_all = py.subplots(1, 3, sharex=True, sharey=True, figsize=(12, 4))
    f.subplots_adjust(left=0.10, bottom=0.18, wspace=0.01)

    for ii in range(len(stars_all)):
        stars = stars_all[ii]
        ax = ax_all[ii]
        
        Nstars = stars.dxm.shape[1]
        Nimages = stars.dxm.shape[0]
    
        mavg = stars.mm.mean(axis=0)

        # Error on the mean
        xerr = stars.dxm.std(axis=0) / math.sqrt(Nimages)
        yerr = stars.dym.std(axis=0) / math.sqrt(Nimages)
        merr = stars.mm.std(axis=0) / math.sqrt(Nimages)

        # Convert to mas
        xerr *= 1e3
        yerr *= 1e3

        rerr = np.hypot(xerr, yerr)

        xerr_med = np.median(xerr)
        yerr_med = np.median(yerr)
        err_med = np.median([xerr_med, yerr_med])
        rerr_med = np.median(np.hypot(xerr, yerr))

        # Make a curve of the median error
        magBinCent = np.arange(10, 13.5, 0.05)
        magBinSize = 0.5
        rerrMed = np.zeros(len(magBinCent), dtype=float)
        rerrAvg = np.zeros(len(magBinCent), dtype=float)
        rerrStd = np.zeros(len(magBinCent), dtype=float)
        for ii in range(len(magBinCent)):
            magLo = magBinCent[ii] - (magBinSize / 2.0)
            magHi = magBinCent[ii] + (magBinSize / 2.0)
            idx = np.where((mavg > magLo) & (mavg <= magHi))[0]
            rerrAvg[ii] = statsIter.mean(rerr[idx], hsigma=2, lsigma=5, iter=10)
            rerrStd[ii] = statsIter.std(rerr[idx], hsigma=2, lsigma=5, iter=10)
            rerrMed[ii] = np.median(rerr[idx])

        # Smooth the average curve
        n_window = 20
        window = np.hanning(n_window)
        s = np.r_[rerrAvg[n_window-1:0:-1], rerrAvg, rerrAvg[-1:-n_window:-1]]
        rerrAvg_smooth = np.convolve(window/window.sum(), s, mode='valid')
        rerrAvg_smooth = rerrAvg_smooth[(n_window/2)-1:-(n_window/2)]
        
        ax.plot(mavg, rerr, 'k.', alpha=0.4)
        #ax.plot(magBinCent, rerrMed, 'k-')
        #ax.plot(magBinCent, rerrAvg, 'r-', linewidth=2)
        ax.plot(magBinCent, rerrAvg_smooth, 'r-', linewidth=2)
        # ax.plot(magBinCent, rerrAvg+rerrStd, 'r--')
        # ax.plot(magBinCent, rerrAvg-rerrStd, 'r--')
        ax.set_xlabel('K Magnitude')
        ax.set_ylim(0, 1)
        ax.set_xlim(9.1, 14)

    ax_all[0].set_ylabel('Astrometric Error (mas)')
    py.savefig(plot_dir + 'gems_ngc1851_pos_err.png')
Esempio n. 12
0
def image_noise_properites():
    print 'READ NOISE:'
    hdr = pyfits.getheader(workDir + 'mag10maylgs_kp.fits')
    itime = hdr['ITIME']
    coadds = hdr['COADDS']
    nsamp = hdr['MULTISAM']
    gain = float(hdr['GAIN'])

    readnoise = (16.0 / math.sqrt(nsamp)) * math.sqrt(coadds) # in DN
    readnoise *= gain # convert to e-
    
    print '    MCDS with %d Fowler samples and %d coadds' % (nsamp, coadds)
    print '    noise = %.1f DN (%.1f e-)' % (readnoise/gain, readnoise)
    print ''


    # Figure out the dark current in a 2.8 sec x 10 coadd image.
    darkImg = pyfits.getdata(workDir + 'dark_2.8s_10ca.fits')
    darkImgFlat = darkImg.flatten()
    darkCurrent = istats.mean(darkImg, lsigma=5, hsigma=5, iter=10)*gain
    darkNoise = istats.std(darkImg, lsigma=5, hsigma=5, iter=10)*gain
    print 'DARK:'
    print '    Dark Current = %.1f DN (%.1f e-)' % \
        (darkCurrent/gain, darkCurrent)
    print '    Dark Noise = %.1f DN (%.1f e-)' % \
        (darkNoise/gain, darkNoise)
    print '    !! Dark noise does not match expected readnoise.'
    print ''
    
    py.clf()
    bins = np.arange(-100, 100, 1)
    fit = stats.norm.pdf(bins, loc=darkCurrent/gain, scale=darkNoise/gain) 
    loCut = (darkCurrent - 3.0 * darkNoise) / gain
    hiCut = (darkCurrent + 3.0 * darkNoise) / gain
    idx = np.where((darkImgFlat > loCut) & (darkImgFlat < hiCut))[0]
    fit *= len(idx)
    py.hist(darkImgFlat, bins=bins, histtype='step', label='Observed')
    py.plot(bins, fit, 'k-', linewidth=2, label='Gaussian with Mean/STD')
    py.xlabel('Counts from Dark Image (DN)')
    py.ylabel('Number of Pixels')
    py.legend()
    py.title('Mean Current = %.1f DN, Stddev = %.1f DN' % \
                 (darkCurrent/gain, darkNoise/gain))

    # Reset so that readnoise = darkNoise
#     print 'SETTING READ NOISE = DARK NOISE'
#     readnoise = darkNoise

    # Sky background flux
    skyImg = pyfits.getdata(workDir + 'mag10maylgs_sky_kp.fits')
    skyCount = 10.0
    skyLevel = istats.mean(skyImg, lsigma=5, hsigma=5, iter=5) * gain
    skyStd = istats.std(skyImg, lsigma=5, hsigma=5, iter=5) * gain
    skyNoise = math.sqrt((skyLevel + readnoise**2) / skyCount)
    print 'SKY:'
    print '    Sky Level = %.1f DN (%.1f e-)' % (skyLevel/gain, skyLevel)
    print '    Sky Stddev = %.1f DN (%.1f e-)' % (skyStd/gain, skyStd)
    print '    Assuming %d sky exposures' % skyCount
    print '    Sky Noise = %.1f DN (%.1f e-)' % (skyNoise/gain, skyNoise)
    print ''

    # GC background flux
    backLevelDC = 90.0 * gain   # Subtracted DC offset
    backNoiseDC = math.sqrt(backLevelDC)
    backImg = pyfits.getdata(workDir + 'mag10maylgs_kp_back.fits')
    backLevel = istats.mean(backImg, lsigma=5, hsigma=5, iter=5) * gain
    backNoise = math.sqrt(backLevel)
    backStd = istats.std(backImg, lsigma=5, hsigma=5, iter=5) * gain
    print 'GC BACKGROUND:'
    print '    GC Background subtracted off is 90 DN (360 e-)'
    print '    Background Level = %.1f DN (%.1f e-)' % \
        (backLevel/gain, backLevel)
    print '    Background Stddev = %.1f DN (%.1f e-)' % \
        (backStd/gain, backStd)
    print '    Background Photon Noise = %.1f DN (%.1f e-)' % \
        (backNoise/gain, backNoise)
    print '    Actual Photon Noise = %.1f DN (%.1f e-)' % \
        (backNoiseDC/gain, backNoiseDC)
    print ''

    # Final Noise Estimate
    skyFact = 1.0 + (1.0 / skyCount)
    gcCount = 150
    gcNoise = skyFact * (skyLevel + readnoise**2)
    gcNoise += backLevelDC
    gcNoise /= gcCount
    gcNoise = math.sqrt(gcNoise)
    print 'FINAL GC NOISE (excluding photon noise from stars):'
    print '    %.1f DN (%.1f e-)' % (gcNoise/gain, gcNoise)
    print ''

    gcNoise = math.sqrt((skyNoise**2 + readnoise**2) / gcCount)
    print 'GC NOISE added to sky/DC/star photon noise (from sky subtraction and readnoise):'
    print '    %.1f DN (%.1f e-)' % (gcNoise/gain, gcNoise)
    print ''

    return skyLevel, backLevelDC, gcNoise, gain
Esempio n. 13
0
def plot_final_synthesis():
    data = atpy.Table(workDir + 'gal1_master.txt', type='ascii')
    img = pyfits.getdata(workDir + 'gal1_img.fits')

    imgBkg = statsIter.mean(img.flatten(), hsigma=5, iter=1, verbose=True)
    imgStd = statsIter.std(img.flatten(), hsigma=5, iter=1, verbose=True)
    print 'Image mean = %5.1f  std = %5.1f' % (imgBkg, imgStd)

    fluxBins = np.arange(0, 500, 8)
    velBins = np.arange(-500, 801, 50)
    n_bins = np.arange(10, 60, 2)

    npix, fluxEdges = np.histogram(img.flatten(), bins=fluxBins)
    nstars, binEdges = np.histogram(data.vel, bins=velBins)
    n, b = np.histogram(nstars, bins=n_bins)

    bkg = np.mean(nstars)
    noise = np.std(nstars)
    print 'First Iteration:  mean = %5.1f, std = %5.1f' % (bkg, noise)

    # Do one round of rejection
    idx = np.where(nstars < (bkg + (3.0 * noise)))[0]
    bkg = np.mean(nstars[idx])
    noise = np.std(nstars[idx])
    print 'Second Iteration: mean = %5.1f, std = %5.1f' % (bkg, noise)
    
    gaussianCDF = scipy.stats.norm.cdf(b, loc=bkg, scale=noise)
    gaussianPDF = np.diff(gaussianCDF) * n.sum()
    gaussianBinCenters = n_bins[0:-1] + np.diff(n_bins) / 2.0

    fluxBinsModel = np.arange(0, 500, 1)
    fluxNormCDF = scipy.stats.norm.cdf(fluxBinsModel, loc=imgBkg, scale=imgStd)
    fluxNormPDF = np.diff(fluxNormCDF)
    fluxNormPDF *= npix.max() / fluxNormPDF.max()
    fluxBinCenters = fluxBinsModel[0:-1] + np.diff(fluxBinsModel) / 2.0

    # Images
    py.figure(1)
    py.clf()
    py.subplots_adjust(left=0.22)
    py.subplot(2, 1, 1)
    py.hist(img.flatten(), histtype='step', bins=fluxBins)
    py.plot(fluxBinCenters, fluxNormPDF, 'g-')
    py.title('Field #1')
    py.xlim(0, 200)

    py.subplot(2, 1, 2)
    py.hist(img.flatten(), histtype='step', bins=fluxBins)
    py.plot(fluxBinCenters, fluxNormPDF, 'g-')
    py.ylim(0, 1000)
    py.xlim(0, 200)
    py.xlabel('Flux')
    py.savefig(workDir + 'final_syn_gal1_flux_hist.png')

    py.figure(1)
    py.subplots_adjust(left=0.15)
    py.clf()
    py.hist(data.vel, bins=velBins, histtype='step')
    py.xlim(-500, 800)
    py.xlabel('Velocity (km/s)')
    py.ylabel('Number of Stars')
    py.savefig(workDir + 'final_syn_gal1_vel_hist_no_lines.png')

    py.axhline(bkg, linestyle='-', linewidth=3, color='black')
    py.axhline(bkg+noise, linestyle='--', linewidth=3, color='black')
    py.axhline(bkg-noise, linestyle='--', linewidth=3, color='black')
    py.savefig(workDir + 'final_syn_gal1_vel_hist.png')
    
    py.figure(2)
    py.clf()
    py.subplots_adjust(left=0.15)
    (n, b, p) = py.hist(nstars, histtype='step', bins=n_bins, label='Data')
    py.ylim(0, n.max()*1.3)
    py.xlim(b.min(), b.max())
    py.xlabel('Number of Stars per Velocity Bin')
    py.ylabel('Number of Velocity Bins')


    print gaussianPDF.max(), n.max()
    
    py.plot(gaussianBinCenters, gaussianPDF, label='Gaussian Fit')
    axLim = py.axis()
    py.axvline(bkg, linestyle='-', color='black', linewidth=3)
    py.arrow(bkg+1*noise, axLim[3], 0, -0.5,
             color='black', linewidth=3, head_width=1, head_length=0.1)
    py.arrow(bkg+2*noise, axLim[3], 0, -0.5,
             color='black', linewidth=3, head_width=1, head_length=0.1)
    py.arrow(bkg+3*noise, axLim[3], 0, -0.5,
             color='black', linewidth=3, head_width=1, head_length=0.1)

    py.text(bkg+1*noise, axLim[3]+0.15, r'$1\sigma$',
            horizontalalignment='center', verticalalignment='bottom')
    py.text(bkg+2*noise, axLim[3]+0.15, r'$2\sigma$',
            horizontalalignment='center', verticalalignment='bottom')
    py.text(bkg+3*noise, axLim[3]+0.15, r'$3\sigma$',
            horizontalalignment='center', verticalalignment='bottom')
    
    py.savefig(workDir + 'final_syn_gal1_vel_hist_hist.png')
def plot_spie_figure3():
    """
    Pass in a starlist (stack of starlists) and calculate the
    astrometric and photometric errors per star. Then plot them.
    """
    align_dir = '/g/lu/data/gsaoi/commission/reduce/ngc1851/align_2013_03_04_G2/'
    plot_dir = '/u/jlu/doc/papers/proceed_2014_spie/'

    from jlu.gsaoi import stars as stars_obj
    stars2 = stars_obj.Starlist(align_dir + 'align_a2')
    stars3 = stars_obj.Starlist(align_dir + 'align_a3')
    stars4 = stars_obj.Starlist(align_dir + 'align_a4')

    stars_all = [stars2, stars3, stars4]

    py.close('all')
    f, ax_all = py.subplots(1, 3, sharex=True, sharey=True, figsize=(12, 4))
    f.subplots_adjust(left=0.10, bottom=0.18, wspace=0.01)

    for ii in range(len(stars_all)):
        stars = stars_all[ii]
        ax = ax_all[ii]

        Nstars = stars.dxm.shape[1]
        Nimages = stars.dxm.shape[0]

        mavg = stars.mm.mean(axis=0)

        # Error on the mean
        xerr = stars.dxm.std(axis=0) / math.sqrt(Nimages)
        yerr = stars.dym.std(axis=0) / math.sqrt(Nimages)
        merr = stars.mm.std(axis=0) / math.sqrt(Nimages)

        # Convert to mas
        xerr *= 1e3
        yerr *= 1e3

        rerr = np.hypot(xerr, yerr)

        xerr_med = np.median(xerr)
        yerr_med = np.median(yerr)
        err_med = np.median([xerr_med, yerr_med])
        rerr_med = np.median(np.hypot(xerr, yerr))

        # Make a curve of the median error
        magBinCent = np.arange(10, 13.5, 0.05)
        magBinSize = 0.5
        rerrMed = np.zeros(len(magBinCent), dtype=float)
        rerrAvg = np.zeros(len(magBinCent), dtype=float)
        rerrStd = np.zeros(len(magBinCent), dtype=float)
        for ii in range(len(magBinCent)):
            magLo = magBinCent[ii] - (magBinSize / 2.0)
            magHi = magBinCent[ii] + (magBinSize / 2.0)
            idx = np.where((mavg > magLo) & (mavg <= magHi))[0]
            rerrAvg[ii] = statsIter.mean(rerr[idx],
                                         hsigma=2,
                                         lsigma=5,
                                         iter=10)
            rerrStd[ii] = statsIter.std(rerr[idx], hsigma=2, lsigma=5, iter=10)
            rerrMed[ii] = np.median(rerr[idx])

        # Smooth the average curve
        n_window = 20
        window = np.hanning(n_window)
        s = np.r_[rerrAvg[n_window - 1:0:-1], rerrAvg,
                  rerrAvg[-1:-n_window:-1]]
        rerrAvg_smooth = np.convolve(window / window.sum(), s, mode='valid')
        rerrAvg_smooth = rerrAvg_smooth[(n_window / 2) - 1:-(n_window / 2)]

        ax.plot(mavg, rerr, 'k.', alpha=0.4)
        #ax.plot(magBinCent, rerrMed, 'k-')
        #ax.plot(magBinCent, rerrAvg, 'r-', linewidth=2)
        ax.plot(magBinCent, rerrAvg_smooth, 'r-', linewidth=2)
        # ax.plot(magBinCent, rerrAvg+rerrStd, 'r--')
        # ax.plot(magBinCent, rerrAvg-rerrStd, 'r--')
        ax.set_xlabel('K Magnitude')
        ax.set_ylim(0, 1)
        ax.set_xlim(9.1, 14)

    ax_all[0].set_ylabel('Astrometric Error (mas)')
    py.savefig(plot_dir + 'gems_ngc1851_pos_err.png')
Esempio n. 15
0
def image_noise_properites():
    print 'READ NOISE:'
    hdr = pyfits.getheader(workDir + 'mag10maylgs_kp.fits')
    itime = hdr['ITIME']
    coadds = hdr['COADDS']
    nsamp = hdr['MULTISAM']
    gain = float(hdr['GAIN'])

    readnoise = (16.0 / math.sqrt(nsamp)) * math.sqrt(coadds)  # in DN
    readnoise *= gain  # convert to e-

    print '    MCDS with %d Fowler samples and %d coadds' % (nsamp, coadds)
    print '    noise = %.1f DN (%.1f e-)' % (readnoise / gain, readnoise)
    print ''

    # Figure out the dark current in a 2.8 sec x 10 coadd image.
    darkImg = pyfits.getdata(workDir + 'dark_2.8s_10ca.fits')
    darkImgFlat = darkImg.flatten()
    darkCurrent = istats.mean(darkImg, lsigma=5, hsigma=5, iter=10) * gain
    darkNoise = istats.std(darkImg, lsigma=5, hsigma=5, iter=10) * gain
    print 'DARK:'
    print '    Dark Current = %.1f DN (%.1f e-)' % \
        (darkCurrent/gain, darkCurrent)
    print '    Dark Noise = %.1f DN (%.1f e-)' % \
        (darkNoise/gain, darkNoise)
    print '    !! Dark noise does not match expected readnoise.'
    print ''

    py.clf()
    bins = np.arange(-100, 100, 1)
    fit = stats.norm.pdf(bins, loc=darkCurrent / gain, scale=darkNoise / gain)
    loCut = (darkCurrent - 3.0 * darkNoise) / gain
    hiCut = (darkCurrent + 3.0 * darkNoise) / gain
    idx = np.where((darkImgFlat > loCut) & (darkImgFlat < hiCut))[0]
    fit *= len(idx)
    py.hist(darkImgFlat, bins=bins, histtype='step', label='Observed')
    py.plot(bins, fit, 'k-', linewidth=2, label='Gaussian with Mean/STD')
    py.xlabel('Counts from Dark Image (DN)')
    py.ylabel('Number of Pixels')
    py.legend()
    py.title('Mean Current = %.1f DN, Stddev = %.1f DN' % \
                 (darkCurrent/gain, darkNoise/gain))

    # Reset so that readnoise = darkNoise
    #     print 'SETTING READ NOISE = DARK NOISE'
    #     readnoise = darkNoise

    # Sky background flux
    skyImg = pyfits.getdata(workDir + 'mag10maylgs_sky_kp.fits')
    skyCount = 10.0
    skyLevel = istats.mean(skyImg, lsigma=5, hsigma=5, iter=5) * gain
    skyStd = istats.std(skyImg, lsigma=5, hsigma=5, iter=5) * gain
    skyNoise = math.sqrt((skyLevel + readnoise**2) / skyCount)
    print 'SKY:'
    print '    Sky Level = %.1f DN (%.1f e-)' % (skyLevel / gain, skyLevel)
    print '    Sky Stddev = %.1f DN (%.1f e-)' % (skyStd / gain, skyStd)
    print '    Assuming %d sky exposures' % skyCount
    print '    Sky Noise = %.1f DN (%.1f e-)' % (skyNoise / gain, skyNoise)
    print ''

    # GC background flux
    backLevelDC = 90.0 * gain  # Subtracted DC offset
    backNoiseDC = math.sqrt(backLevelDC)
    backImg = pyfits.getdata(workDir + 'mag10maylgs_kp_back.fits')
    backLevel = istats.mean(backImg, lsigma=5, hsigma=5, iter=5) * gain
    backNoise = math.sqrt(backLevel)
    backStd = istats.std(backImg, lsigma=5, hsigma=5, iter=5) * gain
    print 'GC BACKGROUND:'
    print '    GC Background subtracted off is 90 DN (360 e-)'
    print '    Background Level = %.1f DN (%.1f e-)' % \
        (backLevel/gain, backLevel)
    print '    Background Stddev = %.1f DN (%.1f e-)' % \
        (backStd/gain, backStd)
    print '    Background Photon Noise = %.1f DN (%.1f e-)' % \
        (backNoise/gain, backNoise)
    print '    Actual Photon Noise = %.1f DN (%.1f e-)' % \
        (backNoiseDC/gain, backNoiseDC)
    print ''

    # Final Noise Estimate
    skyFact = 1.0 + (1.0 / skyCount)
    gcCount = 150
    gcNoise = skyFact * (skyLevel + readnoise**2)
    gcNoise += backLevelDC
    gcNoise /= gcCount
    gcNoise = math.sqrt(gcNoise)
    print 'FINAL GC NOISE (excluding photon noise from stars):'
    print '    %.1f DN (%.1f e-)' % (gcNoise / gain, gcNoise)
    print ''

    gcNoise = math.sqrt((skyNoise**2 + readnoise**2) / gcCount)
    print 'GC NOISE added to sky/DC/star photon noise (from sky subtraction and readnoise):'
    print '    %.1f DN (%.1f e-)' % (gcNoise / gain, gcNoise)
    print ''

    return skyLevel, backLevelDC, gcNoise, gain
Esempio n. 16
0
def plot_vector_diff(undersample):
    """
    Make a vector plot of the differences between the input positions
    and the output positions after running starfinder.
    """
    # Dimensions of new grid
    npsf_side = get_npsf_side(undersample)
    suffix = '%d_%d' % (npsf_side, npsf_side)

    s = starset.StarSet('grid_' + suffix + '/align_'+suffix)

    cnt = s.getArray('velCnt')
    mag = s.getArray('mag')
    idx = np.where((cnt == 2) & (mag < 15))[0]
    print 'Using {0} stars out of {1}'.format(len(idx), len(cnt))

    newStars = [s.stars[ii] for ii in idx]
    s.stars = newStars
    
    x0 = s.getArrayFromEpoch(0, 'xorig')
    y0 = s.getArrayFromEpoch(0, 'yorig')
    x1 = s.getArrayFromEpoch(1, 'xorig')
    y1 = s.getArrayFromEpoch(1, 'yorig')

    dx = x1 - x0
    dy = y1 - y0
    dr = np.hypot(dx, dy)
    
    # Boundaries
    t = atpy.Table('svpar_' + suffix + '.txt', type='ascii')
    lx = t.col1
    ux = t.col2
    ly = t.col3
    uy = t.col4
    xedges = np.unique( np.append(lx, ux) )
    yedges = np.unique( np.append(ly, uy) )

    py.clf()
    q = py.quiver(x0, y0, dx, dy, scale=0.2)
    py.xlim(0, 1200)
    py.ylim(0, 1200)
    py.quiverkey(q, 0.5, 0.97, 0.02, '0.02 pixel (0.2 mas)', color='red', coordinates='axes')

    for xx in xedges:
        py.axvline(xx, linestyle='--')
    for yy in yedges:
        py.axhline(yy, linestyle='--')

    py.savefig('plots/vec_diff_'+suffix+'.png')

    xmean = statsIter.mean(dx, hsigma=5, lsigma=5, iter=5)
    ymean = statsIter.mean(dy, hsigma=5, lsigma=5, iter=5)
    rmean = statsIter.mean(dr, hsigma=5, lsigma=5, iter=5)
    xstd = statsIter.std(dx, hsigma=5, lsigma=5, iter=5)
    ystd = statsIter.std(dy, hsigma=5, lsigma=5, iter=5)
    rstd = statsIter.std(dr, hsigma=5, lsigma=5, iter=5)
    print('Mean Delta-X: {0:9.5f} +/- {1:9.5f} pixels'.format(xmean, xstd))
    print('Mean Delta-Y: {0:9.5f} +/- {1:9.5f} pixels'.format(ymean, ystd))
    print('Mean Delta-R: {0:9.5f} +/- {1:9.5f} pixels'.format(rmean, rstd))
    f_name = 'diff_stats_' + suffix + '.txt'
    f_stat = open(f_name, 'w')
    hdr = '#{0:>5s} {1:>10s} {2:>10s} {3:>10s} {4:>10s} {5:>10s} {6:>10s}\n'
    fmt = '{0:6d} {1:10.5f} {2:10.5f} {3:10.5f} {4:10.5f} {5:10.5f} {6:10.5f}\n'
    f_stat.write(hdr.format('Npsf', 'Xmean', 'Ymean', 'Rmean', 'Xstd', 'Ystd', 'Rstd'))
    f_stat.write(fmt.format(npsf_side, xmean, ymean, rmean, xstd, ystd, rstd))
    f_stat.close()

    return f_name