def idNewYoung(alignRoot):
    s = starset.StarSet(alignRoot)
    
    young_new_dat = '/u/ghezgroup/data/gc/source_list/young_new.dat'
    yng = youngStarNames(datfile=young_new_dat)

    ourName = s.getArray('name')
    x = s.getArray('x')
    y = s.getArray('y')
    mag = s.getArray('mag')

    # Load up Paumard
    paum = tabs.Paumard2006()

    for i in range(len(paum.name)):
        dx = x - paum.x[i]
        dy = y - paum.y[i]
        dm = mag - paum.mag[i]

        dr = sqrt(dx**2 + dy**2)

        idx = (where((dr < 0.5) & (abs(dm) < 0.5)))[0]

        if (len(idx) > 0):
            print('Possible matches for:')
            print('  %-14s  %4.1f  %7.3f  %7.3f (%s)' % \
                  (paum.name[i], paum.mag[i], paum.x[i], paum.y[i],
                   paum.ourName[i]))
            for k in idx:
                print('  %-14s  %4.1f  %7.3f  %7.3f   %5.2f  %5.2f  %3.1f' % \
                      (ourName[k], mag[k], x[k], y[k], dx[k], dy[k], dm[k]))

        else:
            print('No match for: %s' % (paum.name[i]))
Exemple #2
0
def process_align_output(suffix='_t'):
    """
    Deal with the trim_align output that has all 3 users' starlists
    and all 3 frames and all 4 chips (separate align for each chip).
    Make them into a FITS catalog.
    """
    for ii in range(1, 4+1):
        s = starset.StarSet('align_{0}{1}'.format(ii, suffix))
        print 'Loaded starset ', ii

        n_users = 3
        n_frames = 3
        n_stars = len(s.stars)

        x = np.zeros((n_users, n_frames, n_stars), dtype=float)
        y = np.zeros((n_users, n_frames, n_stars), dtype=float)
        m = np.zeros((n_users, n_frames, n_stars), dtype=float)

        for uu in range(n_users):
            for ff in range(n_frames):
                icol = ff*n_frames + uu

                x[uu, ff, :] = s.getArrayFromEpoch(icol, 'xpix')
                y[uu, ff, :] = s.getArrayFromEpoch(icol, 'ypix')
                m[uu, ff, :] = s.getArrayFromEpoch(icol, 'mag')

        cat = np.array((x, y, m))
        pyfits.writeto('catalog_quad_{0}{1}.fits'.format(ii, suffix), cat,
                       output_verify='silentfix', clobber=True)
def loadYoungStars(root, align='align/align_d_rms_1000_abs_t',
                   fit='polyfit_c/fit', points='points_c/',
                   radiusCut=0.8, relErr=1, verbose=False,
                   withRVonly=False, silent=False, mosaic=False):


    if not os.path.exists(root + align + '.trans'):
        align = 'align/align_d_rms_100_abs_t'
        #align = 'align/align_d_rms1000_t'
        
    # Load up the absolute position of Sgr A* based on S0-2's orbit
    t = objects.Transform()
    t.loadAbsolute()

    # Load up position/velocity information
    s = starset.StarSet(root + align, relErr=relErr, trans=t)
    s.loadPolyfit(root + fit, arcsec=1, silent=silent)
    if mosaic == False:
        s.loadPolyfit(root + fit, arcsec=1, accel=1, silent=silent)

    yng = youngStarNames()
    # Tables in database w/ RV information
    ucla = tabs.UCLAstars()
    bart = tabs.Bartko2009()
    paum = tabs.Paumard2006()

    cc = objects.Constants()

    # Pull out from the set of stars those that are young
    # stars.
    stars = []
    names = [star.name for star in s.stars]

    for name in yng:
        # Find the star in our star lists
        try:
            idx = names.index(name)
            star = s.stars[idx]

            if (star.r2d >= radiusCut):
                stars.append(star)

            # Number of Epochs Detected should be corrected
            # for epochs trimmed out of the *.points files.
            pntsFile = '%s%s%s.points' % (root, points, star.name)
            _pnts = asciidata.open(pntsFile)
            star.velCnt = _pnts.nrows

            pntDate = _pnts[0].tonumpy()
            pntX = _pnts[1].tonumpy()
            pntY = _pnts[2].tonumpy()
            pntXe = _pnts[3].tonumpy()
            pntYe = _pnts[4].tonumpy()

            # Load up data from the points files.
            for ee in range(len(star.years)):
                tt = (where(abs(pntDate - star.years[ee]) < 0.001))[0]

                if (len(tt) == 0):
                    star.e[ee].x_pnt = -1000.0
                    star.e[ee].y_pnt = -1000.0
                    star.e[ee].xe_pnt = -1000.0
                    star.e[ee].ye_pnt = -1000.0
                else:
                    tt = tt[0]
                    star.e[ee].x_pnt = pntX[tt]
                    star.e[ee].y_pnt = pntY[tt]
                    star.e[ee].xe_pnt = pntXe[tt]
                    star.e[ee].ye_pnt = pntYe[tt]

            if mosaic == True: # We only have 3 epochs (as of 2011)
	        star.fitXv.v *= cc.asy_to_kms
	        star.fitYv.v *= cc.asy_to_kms
	        star.fitXv.verr *= cc.asy_to_kms
	        star.fitYv.verr *= cc.asy_to_kms

	        star.vx = star.fitXv.v
	        star.vy = star.fitYv.v
	        star.vxerr = star.fitXv.verr
	        star.vyerr = star.fitYv.verr
            else:
	        star.fitXa.v *= cc.asy_to_kms
	        star.fitYa.v *= cc.asy_to_kms
	        star.fitXa.verr *= cc.asy_to_kms
	        star.fitYa.verr *= cc.asy_to_kms

	        star.vx = star.fitXa.v
	        star.vy = star.fitYa.v
	        star.vxerr = star.fitXa.verr
	        star.vyerr = star.fitYa.verr
                

            star.rv_ref = 'None'

            def other_RV_tables():
                # If not found in UCLA tables, then check Bartko+2009
                idx = np.where(bart.ourName == name)[0]
                if len(idx) > 0:
                    star.vz = bart.vz[idx][0] 
	            star.vzerr = bart.vzerr[idx][0]
	            star.vzt0 = bart.t0_spectra[idx][0] 
                    star.rv_ref = 'Bartko+2009'
                else:
                    # Next check Paumard+2006
                    idx = np.where(paum.ourName == name)[0]
                    if len(idx) > 0:
                        star.vz = paum.vz[idx][0] 
                        star.vzerr = paum.vzerr[idx][0]
                        star.vzt0 = paum.t0_spectra[idx][0]
                        star.altName = paum.name[idx][0]
                        star.rv_ref = 'Paumard+2006'
                    #else:
                    #    print 'Could not find radial velocity for %s' % name

	    # Find the radial velocity for each star
            # First look in OSIRIS data, then Bartko, then Paumard
            idx = np.where(ucla.ourName == name)[0]
            if len(idx) > 0:
	        star.vz = ucla.vz[idx][0]
	        star.vzerr = ucla.vzerr[idx][0]
	        star.vzt0 = ucla.t0_spectra[idx][0]
                star.rv_ref = 'UCLA'

                # A star could be in the stars table but still not have vz
                if star.vz == None: # then check other tables
                    star.rv_ref = None
                    other_RV_tables()
            else:
                other_RV_tables()

            if withRVonly == True:
                if star.rv_ref == None:
                    # remove this star
                    stars.remove(star)

            if (verbose == True):
                print('Matched %15s to %12s' % (name, star.rv_ref))

	    star.jz = (star.x * star.vy) - (star.y * star.vx)
	    star.jz /= (star.r2d * hypot(star.vx, star.vy))	    
	except ValueError as e:
	    # Couldn't find the star in our lists
	    continue

    # Set the starset's star list
    s.stars = stars

    print('Found %d young stars' % len(stars))
    return s
Exemple #4
0
def save_starset_to_pickle(align_root, n_lists=12, n_good=9):
    s = starset.StarSet(align_root)

    x_all = s.getArrayFromAllEpochs('xpix')
    y_all = s.getArrayFromAllEpochs('ypix')
    m_all = s.getArrayFromAllEpochs('mag')
    xe_all = s.getArrayFromAllEpochs('xpixerr_a')
    ye_all = s.getArrayFromAllEpochs('ypixerr_a')

    x_old_tmp = x_all[0:n_lists, :]
    y_old_tmp = y_all[0:n_lists, :]
    m_old_tmp = m_all[0:n_lists, :]
    x_new_tmp = x_all[n_lists:, :]
    y_new_tmp = y_all[n_lists:, :]
    m_new_tmp = m_all[n_lists:, :]

    xe_old_tmp = xe_all[0:n_lists, :]
    ye_old_tmp = ye_all[0:n_lists, :]
    xe_new_tmp = xe_all[n_lists:, :]
    ye_new_tmp = ye_all[n_lists:, :]

    x_old = np.ma.masked_where((x_old_tmp < -900), x_old_tmp)
    y_old = np.ma.masked_where((x_old_tmp < -900), y_old_tmp)
    m_old = np.ma.masked_where((x_old_tmp < -900), m_old_tmp)
    x_new = np.ma.masked_where((x_new_tmp < -900), x_new_tmp)
    y_new = np.ma.masked_where((x_new_tmp < -900), y_new_tmp)
    m_new = np.ma.masked_where((x_new_tmp < -900), m_new_tmp)
    
    xe_old = np.ma.masked_where((x_old_tmp < -900), xe_old_tmp)
    ye_old = np.ma.masked_where((x_old_tmp < -900), ye_old_tmp)
    xe_new = np.ma.masked_where((x_new_tmp < -900), xe_new_tmp)
    ye_new = np.ma.masked_where((x_new_tmp < -900), ye_new_tmp)
    
    cnt_old = n_lists - x_old.mask.sum(axis=0)
    cnt_new = n_lists - x_new.mask.sum(axis=0)

    idx = np.where((cnt_old >= n_good) & (cnt_new >= n_good))[0]
    cnt_old = cnt_old[idx]
    cnt_new = cnt_new[idx]

    x_old = x_old[:, idx]
    y_old = y_old[:, idx]
    m_old = m_old[:, idx]
    x_new = x_new[:, idx]
    y_new = y_new[:, idx]
    m_new = m_new[:, idx]

    xe_old = xe_old[:, idx]
    ye_old = ye_old[:, idx]
    xe_new = xe_new[:, idx]
    ye_new = ye_new[:, idx]

    xm_old = x_old.mean(axis=0)
    ym_old = y_old.mean(axis=0)
    mm_old = m_old.mean(axis=0)
    xm_new = x_new.mean(axis=0)
    ym_new = y_new.mean(axis=0)
    mm_new = m_new.mean(axis=0)

    xs_old = x_old.std(axis=0)
    ys_old = y_old.std(axis=0)
    ms_old = m_old.std(axis=0)
    xs_new = x_new.std(axis=0)
    ys_new = y_new.std(axis=0)
    ms_new = m_new.std(axis=0)

    _out = open(align_root + '.pickle', 'w')
    pickle.dump(x_old, _out)
    pickle.dump(y_old, _out)
    pickle.dump(m_old, _out)
    pickle.dump(x_new, _out)
    pickle.dump(y_new, _out)
    pickle.dump(m_new, _out)
    
    pickle.dump(xe_old, _out)
    pickle.dump(ye_old, _out)
    pickle.dump(xe_new, _out)
    pickle.dump(ye_new, _out)

    pickle.dump(xm_old, _out)
    pickle.dump(ym_old, _out)
    pickle.dump(mm_old, _out)
    pickle.dump(xm_new, _out)
    pickle.dump(ym_new, _out)
    pickle.dump(mm_new, _out)

    pickle.dump(xs_old, _out)
    pickle.dump(ys_old, _out)
    pickle.dump(ms_old, _out)
    pickle.dump(xs_new, _out)
    pickle.dump(ys_new, _out)
    pickle.dump(ms_new, _out)

    pickle.dump(cnt_old, _out)
    pickle.dump(cnt_new, _out)

    _out.close()

    return
Exemple #5
0
def run(args=None):
    """
    align_rms main routine.
    """
    options = parse_options(args)

    # Read in the align output. Determine the number of
    # individual starlists are in the stack.
    s = starset.StarSet(options.root_name)

    N_lists = len(s.years)

    if options.idx_min == None:
        options.idx_min = N_lists

    if options.idx_max == None:
        options.idx_max = N_lists

    # Trim down the starlist to just those that are
    # in the desired number of epochs and are detected
    # in the reference epoch.
    s.stars = trim_stars(s, options)

    # Fetch the data off the starset
    name = s.getArray('name')
    x = s.getArrayFromAllEpochs('xpix')
    y = s.getArrayFromAllEpochs('ypix')
    m = s.getArrayFromAllEpochs('mag')
    f = 10**(m / -2.5)

    flux_dn = s.getArrayFromAllEpochs('fwhm')
    corr = s.getArrayFromAllEpochs('corr')
    nimg = s.getArrayFromAllEpochs('nframes')
    snr = s.getArrayFromAllEpochs('snr')

    # Identify where we have measurements and where are non-detections.
    good = ((x > -1000) & (y > -1000) & (m != 0))

    # Calculate the number of epochs the stars are detected in.
    cnt = good[options.idx_min:options.idx_max, :].sum(axis=0)

    # Mask the bad data.
    x_msk = np.ma.masked_where(good == False, x, copy=True)
    y_msk = np.ma.masked_where(good == False, y, copy=True)
    f_msk = np.ma.masked_where(good == False, f, copy=True)
    m_msk = np.ma.masked_where(good == False, m, copy=True)

    flux_dn = np.ma.masked_where(good == False, flux_dn, copy=True)
    corr_msk = np.ma.masked_where(good == False, corr, copy=True)
    nimg_msk = np.ma.masked_where(good == False, nimg, copy=True)
    snr_msk = np.ma.masked_where(good == False, snr, copy=True)

    # Calculate the average x, y, m, f
    if options.idx_ref != None:
        print 'Using epoch {0} as average pos/flux'.format(options.idx_ref)
        x_avg = x[options.idx_ref, :]
        y_avg = y[options.idx_ref, :]
        m_avg = m[options.idx_ref, :]
        f_avg = f[options.idx_ref, :]

        year = s.dates[options.idx_ref]

        corr_avg = corr[options.idx_ref, :]
        flux_dn_avg = flux_dn[options.idx_ref, :]
        nimg_avg = nimg[options.idx_ref, :]
        snr_orig = snr[options.idx_ref, :]
    else:
        print 'Calculate average pos/flux '
        print 'from epochs {0} - {1}'.format(options.idx_min, options.idx_max)
        x_avg = x_msk[options.idx_min:options.idx_max, :].mean(axis=0)
        y_avg = y_msk[options.idx_min:options.idx_max, :].mean(axis=0)
        f_avg = f_msk[options.idx_min:options.idx_max, :].mean(axis=0)
        m_avg = -2.5 * np.log10(f_avg)

        year = s.years[options.idx_min:options.idx_max].mean()

        corr_avg = corr_msk[options.idx_min:options.idx_max, :].mean(axis=0)
        flux_dn_avg = flux_dn[options.idx_min:options.idx_max, :].mean(axis=0)
        nimg_avg = cnt
        snr_orig = None

    # Calculate the error on x, y, m, f
    x_std = calc_error(x_msk, x_avg, cnt, options)
    y_std = calc_error(y_msk, y_avg, cnt, options)
    f_std = calc_error(f_msk, f_avg, cnt, options)
    m_std = f_std / f_avg

    # Estimate a new signal to noise ratio
    new_snr = f_avg / f_std

    if (options.calc_rel_err == False) and (snr_orig != None):
        new_snr = 1.0 / np.hypot(1.0 / new_snr, 1.0 / snr_orig)

    # Fix up any infinities in the SNR. Set them to 0.
    new_snr[np.isinf(new_snr)] = 0.0
    new_snr[new_snr.mask] = 0.0

    _out = open(options.out_root + '.lis', 'w')

    hdr = '{name:13s}  {mag:>6s}  {year:>8s}  '
    hdr += '{x:>9s}  {y:>9s}  {xe:>9s}  {ye:>9s}  '
    hdr += '{snr:>20s}  {corr:>6s}  {nimg:>8s}  {flux:>20s}\n'

    _out.write(
        hdr.format(name='# name',
                   mag='mag',
                   year='year',
                   x='x',
                   y='y',
                   xe='xe',
                   ye='ye',
                   snr='snr',
                   corr='corr',
                   nimg='nimg',
                   flux='flux'))

    fmt = '{name:13s}  {mag:6.3f}  {year:8.3f}  '
    fmt += '{x:9.3f}  {y:9.3f}  {xe:9.3f}  {ye:9.3f}  '
    fmt += '{snr:20.4f}  {corr:6.2f}  {nimg:8f}  {flux:20.0f}\n'

    for ss in range(len(x_avg)):
        _out.write(
            fmt.format(name=name[ss],
                       mag=m_avg[ss],
                       year=float(year),
                       x=x_avg[ss],
                       y=y_avg[ss],
                       xe=x_std[ss],
                       ye=y_std[ss],
                       snr=new_snr[ss],
                       corr=corr_avg[ss],
                       nimg=nimg_avg[ss],
                       flux=flux_dn_avg[ss]))

    _out.close()

    return s
def plot_gsaoi_compare_nights():
    alignDir = '/u/jlu/data/gsaoi/commission/reduce/ngc1851/'
    alignDir += 'align_G2_2_nodith/'

    alignRootAll = [alignDir + 'align_2_t', alignDir + 'align_4_t']

    py.close(1)
    py.figure(1, figsize=(12, 6))
    py.subplots_adjust(left=0.1)
    py.axis('equal')

    for ii in range(len(alignRootAll)):
        alignRoot = alignRootAll[ii]

        s = starset.StarSet(alignRoot)

        x = s.getArrayFromAllEpochs('x')
        y = s.getArrayFromAllEpochs('y')
        m = s.getArrayFromAllEpochs('mag')

        x = x[1:, :]
        y = y[1:, :]
        m = m[1:, :]

        name = s.getArray('name')
        ndet = s.getArray('velCnt')

        # Set up masks to get rid of invalid values
        xm = np.ma.masked_where(x <= -1e5, x)
        ym = np.ma.masked_where(x <= -1e5, y)
        mm = np.ma.masked_where(x <= -1e5, m)

        # Read in the align.list file and figure out the dates.
        starlistsTab = atpy.Table(alignRoot + '.list',
                                  type='ascii',
                                  delimiter=' ',
                                  data_start=1)
        starlists = starlistsTab.col1

        xavg = xm.mean(axis=0)
        xstd = xm.std(axis=0) * 1e3
        xerr = xstd / math.sqrt(xm.shape[0])

        yavg = ym.mean(axis=0)
        ystd = ym.std(axis=0) * 1e3
        yerr = ystd / math.sqrt(ym.shape[0])

        dates = np.array([ss[1:9] for ss in starlists])
        uniqueDates = np.unique(dates)
        idx_n1 = np.where(dates == uniqueDates[0])[0]
        idx_n2 = np.where(dates == uniqueDates[1])[0]

        x_n1 = xm[idx_n1, :]
        y_n1 = ym[idx_n1, :]
        m_n1 = mm[idx_n1, :]

        x_n2 = xm[idx_n2, :]
        y_n2 = ym[idx_n2, :]
        m_n2 = mm[idx_n2, :]

        xavg_n1 = x_n1.mean(axis=0)
        yavg_n1 = y_n1.mean(axis=0)
        mavg_n1 = m_n1.mean(axis=0)

        xavg_n2 = x_n2.mean(axis=0)
        yavg_n2 = y_n2.mean(axis=0)
        mavg_n2 = m_n2.mean(axis=0)

        xstd_n1 = x_n1.std(axis=0) * 1e3
        ystd_n1 = y_n1.std(axis=0) * 1e3
        mstd_n1 = m_n1.std(axis=0) * 1e3

        xstd_n2 = x_n2.std(axis=0) * 1e3
        ystd_n2 = y_n2.std(axis=0) * 1e3
        mstd_n2 = m_n2.std(axis=0) * 1e3

        # Correct error to report (assuming random errors) is the error on the mean.
        xerr_n1 = xstd_n1 / math.sqrt(x_n1.shape[0])
        yerr_n1 = ystd_n1 / math.sqrt(x_n1.shape[0])
        merr_n1 = mstd_n1 / math.sqrt(x_n1.shape[0])

        xerr_n2 = xstd_n2 / math.sqrt(x_n2.shape[0])
        yerr_n2 = ystd_n2 / math.sqrt(x_n2.shape[0])
        merr_n2 = mstd_n2 / math.sqrt(x_n2.shape[0])

        dx = (xavg_n2 - xavg_n1) * 1.0e3
        dy = (yavg_n2 - yavg_n1) * 1.0e3

        py.subplot(1, 2, ii + 1)
        py.errorbar(dx, dy, xerr=xerr_n1, yerr=yerr_n1, fmt='k.')
        py.plot([0], [0], 'rs', ms=8)
        py.xlabel('X Difference (mas)')
        py.ylabel('Y Difference (mas)')
        py.title('GSAOI Night 2 - Night 1')
        lim = 6
        py.axis([-lim, lim, -lim, lim])

    outFile = '/u/jlu/doc/papers/proceed_2014_spie/gems_ngc1851_comp_nights.png'
    py.savefig(outFile)