def compareVelocity(rootDir='./', align = 'align/align_d_rms1000t', poly='polyfit_d/fit', points='points_d/'): """ Compare velocities from the 2D and 3D polynomial fits. rootDir -- An align root directory such as '06_14_02/' (def='./') align -- Align root name (def = 'align/align_all1000_t') poly -- Polyfit root name (def = 'polyfit/fit') points -- Points directory (def = 'points/') """ s = starset.StarSet(rootDir + align) s.loadPolyfit(rootDir + poly, accel=0, arcsec=1) s.loadPolyfit(rootDir + poly, accel=1, arcsec=1) names = s.getArray('name') x = s.getArray('x') y = s.getArray('y') r = hypot(x, y) vx_vel = s.getArray('fitXv.v') vy_vel = s.getArray('fitYv.v') vxe_vel = s.getArray('fitXv.verr') vye_vel = s.getArray('fitYv.verr') vx_acc = s.getArray('fitXa.v') vy_acc = s.getArray('fitYa.v') vxe_acc = s.getArray('fitXa.verr') vye_acc = s.getArray('fitYa.verr') # Calculate the residuals diffx = vx_vel - vx_acc diffxErr = np.sqrt(vxe_vel**2 + vxe_acc**2) diffy = vy_vel - vy_acc diffyErr = np.sqrt(vye_vel**2 + vye_acc**2) diff = py.hypot(diffx, diffy) diffErr = np.sqrt((diffx*diffxErr)**2 + (diffy*diffyErr)**2) / diff yngNames = young.youngStarNames() idx = (np.where((r > 0.8) & ((diff/diffErr) > 2.0)))[0] print '** Stars with large velocity discrepencies: **' print '%15s %5s %5s %5s %3s' % ('Name', 'Sigma', 'X', 'Y', 'YNG?') for i in idx: try: foo = yngNames.index(names[i]) yngString = 'yng' except ValueError, e: yngString = '' print '%15s %5.1f %5.2f %5.2f %3s' % \ (names[i], diff[i]/diffErr[i], x[i], y[i], yngString)
def plotCompareYoung(align1, poly1, align2, poly2): """ Compare the young stars accelerations for two different runs of align/polyfit. """ s1 = starset.StarSet(align1) s1.loadPolyfit(poly1, accel=0, arcsec=1) s1.loadPolyfit(poly1, accel=1, arcsec=1) s2 = starset.StarSet(align2) s2.loadPolyfit(poly2, accel=0, arcsec=1) s2.loadPolyfit(poly2, accel=1, arcsec=1) names1 = s1.getArray('name') names2 = s2.getArray('name') yngNames = young.youngStarNames() # We need to filter starlists to just the young'uns idx1 = [] for ii in range(len(s1.stars)): if (s1.stars[ii].name in yngNames and s1.stars[ii].r2d > 0.8 and s1.stars[ii].velCnt >= 31): idx1.append(ii) stars1 = [s1.stars[i] for i in idx1] s1.stars = stars1 names1 = s1.getArray('name') idx2 = [] for ii in range(len(s2.stars)): if (s2.stars[ii].name in yngNames and s2.stars[ii].r2d > 0.8 and s2.stars[ii].velCnt >= 29): idx2.append(ii) stars2 = [s2.stars[i] for i in idx2] s2.stars = stars2 names = s2.getArray('name') print names # Make arrays of the accelerations x0_1 = s1.getArray('fitXa.p') y0_1 = s1.getArray('fitYa.p') vx_1 = s1.getArray('fitXa.v') vy_1 = s1.getArray('fitYa.v') ax_1 = s1.getArray('fitXa.a') * 1000.0 ay_1 = s1.getArray('fitYa.a') * 1000.0 axe_1 = s1.getArray('fitXa.aerr') * 1000.0 aye_1 = s1.getArray('fitYa.aerr') * 1000.0 chi2x_1 = s1.getArray('fitXa.chi2red') chi2y_1 = s1.getArray('fitYa.chi2red') x0_2 = s2.getArray('fitXa.p') y0_2 = s2.getArray('fitYa.p') vx_2 = s2.getArray('fitXa.v') vy_2 = s2.getArray('fitYa.v') ax_2 = s2.getArray('fitXa.a') * 1000.0 ay_2 = s2.getArray('fitYa.a') * 1000.0 axe_2 = s2.getArray('fitXa.aerr') * 1000.0 aye_2 = s2.getArray('fitYa.aerr') * 1000.0 chi2x_2 = s2.getArray('fitXa.chi2red') chi2y_2 = s2.getArray('fitYa.chi2red') py.figure(1, figsize=(4,4)) py.clf() # py.errorbar(ax_1, ax_2, xerr=axe_1, yerr=axe_2, fmt='k^') # py.plot([-1, 1], [-1, 1], 'k--') # py.axis('equal') # Determine how different the values are from each other # diffX = ax_1 - ax_2 # diffY = ay_1 - ay_2 # diffXe = np.sqrt(axe_1**2 + axe_2**2) # diffYe = np.sqrt(aye_1**2 + aye_2**2) # sigX = diffX / diffXe # sigY = diffY / diffYe py.clf() py.subplot(2, 1, 1) n1x, bins1x, patches1x = py.hist(chi2x_1, bins=np.arange(0,6,0.2)) py.setp(patches1x, 'facecolor', 'r', 'alpha', 0.50) n1y, bins1y, patches1y = py.hist(chi2y_1, bins=np.arange(0,6,0.2)) py.setp(patches1y, 'facecolor', 'b', 'alpha', 0.50) ysubplot(2, 1, 2) n2x, bins2x, patches2x = py.hist(chi2x_2, bins=np.arange(0,6,0.2)) py.setp(patches2x, 'facecolor', 'r', 'alpha', 0.50) n2y, bins2y, patches2y = py.hist(chi2y_2, bins=np.arange(0,6,0.2)) py.setp(patches2y, 'facecolor', 'b', 'alpha', 0.50) py.savefig('poly_compare_chi2.png') # Compare the number of points for each star cnt1 = s1.getArray('velCnt') cnt2 = s2.getArray('velCnt')
def plotLimits(rootDir='./', align='align/align_d_rms1000_t', poly='polyfit_d_points/fit', points='points_d/', youngOnly=False, sigma=3): """ Find the 4 sigma radial acceleration limits for each star. """ # Load GC constants cc = objects.Constants() # Load up positional information from align. s = starset.StarSet(rootDir + align) s.loadPolyfit(rootDir + poly, arcsec=1) s.loadPolyfit(rootDir + poly, arcsec=1, accel=1) names = s.getArray('name') # In mas/yr^2 x = s.getArray('x') y = s.getArray('y') vx = s.getArray('fitXa.v') vy = s.getArray('fitYa.v') ax = s.getArray('fitXa.a') * 1000.0 ay = s.getArray('fitYa.a') * 1000.0 axe = s.getArray('fitXa.aerr') * 1000.0 aye = s.getArray('fitYa.aerr') * 1000.0 r2d = s.getArray('r2d') cnt = s.getArray('velCnt') if (youngOnly == True): yngNames = young.youngStarNames() idx = [] for ii in range(len(names)): if (r2d[ii] > 0.8 and names[ii] in yngNames and cnt[ii] >= 24): idx.append(ii) names = [names[i] for i in idx] x = x[idx] y = y[idx] vx = vx[idx] vy = vy[idx] ax = ax[idx] ay = ay[idx] axe = axe[idx] aye = aye[idx] r2d = r2d[idx] print 'Found %d young stars' % len(names) # Lets do radial/tangential r = np.sqrt(x**2 + y**2) if ('Radial' in poly): at = ax ar = ay ate = axe are = aye else: ar = ((ax*x) + (ay*y)) / r at = ((ax*y) - (ay*x)) / r are = np.sqrt((axe*x)**2 + (aye*y)**2) / r ate = np.sqrt((axe*y)**2 + (aye*x)**2) / r # Total acceleration atot = py.hypot(ax, ay) atoterr = np.sqrt((ax*axe)**2 + (ay*aye)**2) / atot accLim = ar - (sigma * are) # Calculate the acceleration limit set by the projected radius # Convert into cm r2d = r * cc.dist * cc.cm_in_au # acc1 in cm/s^2 a2d = -cc.G * cc.mass * cc.msun / r2d**2 # acc1 in km/s/yr a2d *= cc.sec_in_yr / 1.0e5 a2d *= 1000.0 / cc.asy_to_kms accLimR2d = a2d _f = open(rootDir + 'tables/accel_limits.txt', 'w') _f.write('### Radial Acceleration Limits for Young Stars ###\n') _f.write('%13s %7s - (%d * %5s) = %9s %21s Constrained?\n' % \ ('Name', 'a_rad', sigma, 'aerr', 'a_obs_lim', 'a_proj_lim')) fmt = '%13s %7.3f - (%d * %5.3f) = %7.3f ' fmt += 'vs %10.3f (mas/yr^2) %s' fmt2 = fmt + '\n' for i in range(len(ar)): constrained = '' if (accLim[i] > accLimR2d[i]): constrained = '**' print fmt % (names[i], ar[i], sigma, are[i], accLim[i], accLimR2d[i], constrained) _f.write(fmt2 % (names[i], ar[i], sigma, are[i], accLim[i], accLimR2d[i], constrained)) _f.close()
def histAccel(rootDir='./', align = 'align/align_d_rms_1000_abs_t', poly='polyfit_d/fit', points='points_d/', youngOnly=False): """ Make a histogram of the accelerations in the radial/tangential, X/Y, and inline/perp. direction of motion. Also, the large outliers (> 3 sigma) are also printed out. Inputs: rootDir = The root directory of an astrometry analysis (e.g. '07_05_18/' or './' if you are in the directory). align = The align root file name (including the directory relative to rootDir). Make sure that polyfit was run on this align output. poly = The polyfit root file name (including the directory relative to rootDir). This should be run on the same align as above. points = The points directory. youngOnly = Only plot the known young stars. This does not include the central arcsecond sources. Output: plots/polyfit_hist_accel.eps (and png) -- Contains the histograms of the accelerations. plots/polyfit_hist_accel_nepochs.eps (ang png) -- Contains a plot of number of stars vs. acceleration significance. """ s = starset.StarSet(rootDir + align) s.loadPolyfit(rootDir + poly, accel=0, arcsec=1) s.loadPolyfit(rootDir + poly, accel=1, arcsec=1) names = s.getArray('name') # In mas/yr^2 x0 = s.getArray('fitXa.p') y0 = s.getArray('fitYa.p') x0e = s.getArray('fitXa.perr') y0e = s.getArray('fitYa.perr') vx = s.getArray('fitXa.v') vy = s.getArray('fitYa.v') ax = s.getArray('fitXa.a') ay = s.getArray('fitYa.a') axe = s.getArray('fitXa.aerr') aye = s.getArray('fitYa.aerr') r2d = s.getArray('r2d') cnt = s.getArray('velCnt') if (youngOnly == True): yngNames = young.youngStarNames() idx = [] for ii in range(len(names)): if (r2d[ii] > 0.8 and names[ii] in yngNames):# and #cnt[ii] >= 24): idx.append(ii) names = [names[i] for i in idx] x0 = x0[idx] y0 = y0[idx] x0e = x0e[idx] y0e = y0e[idx] vx = vx[idx] vy = vy[idx] ax = ax[idx] ay = ay[idx] axe = axe[idx] aye = aye[idx] r2d = r2d[idx] cnt = cnt[idx] print 'Found %d young stars' % len(names) pntcnt = np.zeros(len(names)) for ii in range(len(names)): pntFileName = '%s%s.points' % (rootDir+points, names[ii]) pntFile = open(pntFileName) data = pntFile.readlines() pntcnt[ii] = len(data) # Lets also do radial/tangential r = np.sqrt(x0**2 + y0**2) ar = ((ax*x0) + (ay*y0)) / r at = ((ax*y0) - (ay*x0)) / r are = (axe*x0/r)**2 + (aye*y0/r)**2 are += (y0*x0e*at/r**2)**2 + (x0*y0e*at/r**2)**2 are = np.sqrt(are) ate = (axe*y0/r)**2 + (aye*x0/r)**2 ate += (y0*x0e*ar/r**2)**2 + (x0*y0e*ar/r**2)**2 ate = np.sqrt(ate) # Lets also do parallael/perpendicular to velocity v = np.sqrt(vx**2 + vy**2) am = ((ax*vx) + (ay*vy)) / v an = ((ax*vy) - (ay*vx)) / v ame = np.sqrt((axe*vx)**2 + (aye*vy)**2) / v ane = np.sqrt((axe*vy)**2 + (aye*vx)**2) / v # Total acceleration atot = py.hypot(ax, ay) atoterr = np.sqrt((ax*axe)**2 + (ay*aye)**2) / atot def plotHist(val, pos, label): py.subplot(3, 2, pos) py.hist(val, bins=range(-8, 8, 1)) py.axis([-8, 8, 0, (len(val)/3) + 2]) #py.plot(r2d, val, 'k^') #for ii in range(len(r2d)): # py.text(r2d[ii], val[ii], names[ii]) #py.axis([0.5, 4.0, -7, 7]) py.title(label) print 'Significant Values for %s' % (label) idx = (np.where(abs(val) > 3.0))[0] for i in idx: print '%15s %5.2f sigma %2d epochs' % \ (names[i], val[i], pntcnt[i]) py.figure(3, figsize=(7.5,10)) py.clf() plotHist(atot / atoterr, 1, 'Total') py.clf() # XY plotHist(ax / axe, 1, 'X') plotHist(ay / aye, 2, 'Y') # Radial/Tangential plotHist(ar / are, 3, 'Radial') plotHist(at / ate, 4, 'Tangential') # In line of Motion and out plotHist(an / ane, 5, 'Perpendic. to v') plotHist(am / ame, 6, 'Parallel to v') py.savefig('plots/polyfit_hist_accel.eps') py.savefig('plots/polyfit_hist_accel.png') # Analyze the non-central arcsec sources for Nepochs threshold idx = (np.where(r2d > 0.8))[0] py.figure(1) py.clf() py.subplot(2, 1, 1) py.plot(ar[idx] / are[idx], pntcnt[idx], 'k.') py.axis([-10, 10, 0, 30]) py.xlabel('Radial Acc. Sig. (sigma)') py.ylabel('Number of Epochs Detected') py.subplot(2, 1, 2) py.plot(at[idx] / ate[idx], pntcnt[idx], 'k.') py.axis([-10, 10, 0, 30]) py.xlabel('Tangent. Acc. Sig. (sigma)') py.ylabel('Number of Epochs Detected') py.savefig('plots/polyfit_hist_accel_nepochs.eps') py.savefig('plots/polyfit_hist_accel_nepochs.png')