def main(): progname = 'find_pbdot_corr.py' args = get_opt(progname) ra = 360.0 * (args.ra[0] + args.ra[1]/60. + args.ra[2]/3600.)/24.0 if(args.dec[0] < 0): sgn = -1. else: sgn = 1. dec = sgn * (np.abs(args.dec[0]) + args.dec[1]/60. + args.dec[2]/3600.) print 'RA = ', ra, ' deg = ', np.radians(ra), ' rad' print 'Dec = ', dec, ' deg = ', np.radians(dec), ' rad' # print 'l = ', l, ' deg' # print 'b = ', b, ' deg' if(args.disterr != None): derr = ' +/- '+str(args.disterr) else: derr = '' print 'dist = ', args.dist, derr, ' kpc' if(args.pmerr != None): pmerr = ' +/- '+str(args.pmerr) else: pmerr = '' print 'PM = ', args.pm, pmerr, ' mas/yr' print 'Pbdot = ', args.pbdot if(args.pbdoterr != None): print 'Pbdot error = ', args.pbdoterr # Run calcuation of pbdot correction through each iteration and build # up array of values for pbdot correction # for i_iter in np.arange(args.niter): pbdot_corr, pdbot_corr_err = pc.get_pbdot_corr(args.ra, args.dec, args.pm, args.dist, args.pb, pmerr=args.pmerr, derr=args.disterr, v0=args.v0, R0=args.R0, v0err=args.v0err, R0err=args.R0err, verbose=True) # Create histogram of pbdot correction, and fit a gsaussian to it. # Extract median (mean?) and sigma as our final value and error. print '' print 'Original Pbdot = ', args.pbdot # Check whether all error bars required are given for distance and proper # motion. If they are, go ahead with the random number generation thing. # If not, just do a quick straight calculation of the pbdot correction. if((args.disterr != None) & (args.pmerr != None) & (args.pbdoterr != None)): pbdot_corr_err = '' pbdot_final_err = '' print 'Correction to Pbdot = ', pbdot_corr, ' +/- ', pbdot_corr_err # Now calculate final Pbdot error ###print 'FINAL corrected Pbdot = ', pbdot_final, ' +/- ', pbdot_final_err return
def main(): progname = "find_pbdot_corr_mc.py" args = get_opt(progname) ra_deg = 360.0 * (args.ra[0] + args.ra[1] / 60.0 + args.ra[2] / 3600.0) / 24.0 if args.raerr != None: raerr = (args.raerr / 3600.0) * 360.0 / 24.0 # Convert to degrees raerr_str = " +/- " + str(raerr) else: raerr_str = "" if args.dec[0] < 0: sgn = -1.0 else: sgn = 1.0 dec_deg = sgn * (np.abs(args.dec[0]) + args.dec[1] / 60.0 + args.dec[2] / 3600.0) if args.decerr != None: decerr = args.decerr / 3600.0 # Convert to degrees decerr_str = " +/- " + str(decerr) else: decerr_str = "" print "RA = ", ra_deg, raerr_str, " deg " print "Dec = ", dec_deg, decerr_str, " deg " # Convert nominal values of ra and dec to l and b here just for show c = coord.ICRSCoordinates(ra=ra_deg, dec=dec_deg, unit=(u.degree, u.degree)) l = c.galactic.l.degrees b = c.galactic.b.degrees print "l = ", l, " deg" print "b = ", b, " deg" if args.disterr != None: derr = " +/- " + str(args.disterr) else: derr = "" print "dist = ", args.dist, derr, " kpc" if args.pmerr != None: pmerr = " +/- " + str(args.pmerr) else: pmerr = "" print "PM = ", args.pm, pmerr, " mas/yr" print "Pbdot = ", args.pbdot if args.pbdoterr != None: print "Pbdot error = ", args.pbdoterr # convert pb to seconds args.pb = u.day.to(u.s, args.pb) # Check whether all error bars required are given for distance and proper # motion. If they are, go ahead with the random number generation thing. # If not, just do a quick straight calculation of the pbdot correction. if ( (args.disterr != None) & (args.pmerr != None) & (args.pbdoterr != None) & (args.raerr != None) & (args.decerr != None) ): # For each of the varying parameters (dist, pm, v0, R0), generate # args.niter gaussian deviates, with mean = value and sigma = error dist = np.random.normal(args.dist, args.disterr, args.niter) pm = np.random.normal(args.pm, args.pmerr, args.niter) v0 = np.random.normal(args.v0, args.v0err, args.niter) R0 = np.random.normal(args.R0, args.R0err, args.niter) ra = np.random.normal(ra_deg, raerr, args.niter) dec = np.random.normal(dec_deg, decerr, args.niter) # Plot distributions for each parameter: fig_param = plt.figure() # distance ax_param = fig_param.add_subplot(321) hist, bin_val = np.histogram(dist, bins=32) bin_size = bin_val[1] - bin_val[0] bin_val = bin_val[0 : len(bin_val) - 1] + bin_size / 2.0 ax_param.plot(bin_val, hist, linestyle="steps-mid", linewidth=1.4, color="blue") A, mu, sig = utils.fitgauss(bin_val, hist) # ax_param.plot(bin_val, utils.gaussian(bin_val, A, mu, sig), color='red') ax_param.set_xlabel("Distance (kpc)") # proper motion ax_param = fig_param.add_subplot(322) hist, bin_val = np.histogram(pm, bins=32) bin_size = bin_val[1] - bin_val[0] bin_val = bin_val[0 : len(bin_val) - 1] + bin_size / 2.0 ax_param.plot(bin_val, hist, linestyle="steps-mid", linewidth=1.4, color="blue") ax_param.set_xlabel("Proper motion (mas/yr)") # v0 ax_param = fig_param.add_subplot(323) hist, bin_val = np.histogram(v0, bins=32) bin_size = bin_val[1] - bin_val[0] bin_val = bin_val[0 : len(bin_val) - 1] + bin_size / 2.0 ax_param.plot(bin_val, hist, linestyle="steps-mid", linewidth=1.4, color="blue") ax_param.set_xlabel("Solar velocity (km/s)") # R0 ax_param = fig_param.add_subplot(324) hist, bin_val = np.histogram(R0, bins=32) bin_size = bin_val[1] - bin_val[0] bin_val = bin_val[0 : len(bin_val) - 1] + bin_size / 2.0 ax_param.plot(bin_val, hist, linestyle="steps-mid", linewidth=1.4, color="blue") ax_param.set_xlabel("Galactocentric distance (kpc)") # RA ax_param = fig_param.add_subplot(325) hist, bin_val = np.histogram(ra, bins=32) bin_size = bin_val[1] - bin_val[0] bin_val = bin_val[0 : len(bin_val) - 1] + bin_size / 2.0 ax_param.plot(bin_val, hist, linestyle="steps-mid", linewidth=1.4, color="blue") ax_param.set_xlabel("Right ascension (deg)") # Dec ax_param = fig_param.add_subplot(326) hist, bin_val = np.histogram(dec, bins=32) bin_size = bin_val[1] - bin_val[0] bin_val = bin_val[0 : len(bin_val) - 1] + bin_size / 2.0 ax_param.plot(bin_val, hist, linestyle="steps-mid", linewidth=1.4, color="blue") ax_param.set_xlabel("Declination (deg)") plt.show() # Run calcuation of pbdot correction through each iteration and build # up array of values for pbdot correction # for i_iter in np.arange(args.niter): sys.stdout.write("\n {0:<7s} {1:<7s} \n".format("Iter", "% done")) pbdot_corr_array = [] for i_iter in np.arange(args.niter): pbdot_corr_array.append( pc.get_pbdot_corr( ra[i_iter], dec[i_iter], pm[i_iter], dist[i_iter], args.pb, v0=v0[i_iter], R0=R0[i_iter] ) ) if np.fmod(i_iter, 16) == 0: display_status(i_iter, args.niter) pbdot_corr_array = np.array(pbdot_corr_array) # Create histogram of pbdot correction, and fit a gsaussian to it. # Extract median (mean?) and sigma as our final value and error. if args.corrlim == None: corrlim = ( np.amin(pbdot_corr_array) - 0.1 * np.abs(np.amin(pbdot_corr_array)), np.amax(pbdot_corr_array) + 0.1 * np.abs(np.amax(pbdot_corr_array)), ) else: corrlim = (args.corrlim[0] * 10.0 ** (-12), args.corrlim[1] * 10 ** (-12)) pbdot_pdf, bin_val = np.histogram(pbdot_corr_array, range=corrlim, bins=192, density=True) bin_size = bin_val[1] - bin_val[0] pbdot_x = bin_val[0 : len(bin_val) - 1] + 0.5 * bin_size # fig_pbdot = plt.figure() # ax_pbdot = fig_pbdot.add_axes([0.12, 0.1, 0.8, 0.85]) # ax_pbdot.plot(pbdot_x, pbdot_pdf, linestyle='steps-mid', linewidth=1.4, # color='blue') pbdot_corr_med, pbdot_corr_min, pbdot_corr_max = get_pdf_prob( pbdot_x, pbdot_pdf, prob_intervals, norm=True ) # , \ plot_pdf( pbdot_x, pbdot_pdf, ### weights=alpha_weights, \ xlabel="$\\dot{P}_{b, corr}}$", ylabel="Probability density", prob_lines=np.append(pbdot_corr_min, pbdot_corr_max), prob_linestyle=["dashed", "dashdot", "dotted", "dashed", "dashdot", "dotted"], ) plt.axvline(pbdot_corr_med) pbdot_corr_err_high = np.abs(pbdot_corr_max - pbdot_corr_med) pbdot_corr_err_low = np.abs(pbdot_corr_min - pbdot_corr_med) # A, pbdot_corr, pbdot_corr_err = utils.fitgauss(pbdot_x, pbdot_pdf) # ax_pbdot.plot(pbdot_x, utils.gaussian(pbdot_x, A, pbdot_corr, pbdot_corr_err), # linestyle='solid', linewidth=1.4, color='red') plt.savefig("pbdot_new.png") print "" print "Original Pbdot = ", args.pbdot print "" # print 'Corrected Pbdot = ', pbdot_corr, ' +/- ', pbdot_corr_err print "Pbdot correction = ", pbdot_corr_med, " +", pbdot_corr_err_high[0], " -", pbdot_corr_err_low[0] print "Pbdot correction peak = ", pbdot_x[np.argmax(pbdot_pdf)] pbdot_new = args.pbdot - pbdot_corr_med # pbdot_corr_err_mean = np.mean(np.array([pbdot_corr_err_low[0],pbdot_corr_err_high[0]])) # pbdot_new_err = np.sqrt(args.pbdoterr**2.0 + pbdot_corr_err_mean**2.0) pbdot_new_err_low = np.sqrt(args.pbdoterr ** 2.0 + pbdot_corr_err_low ** 2.0) pbdot_new_err_high = np.sqrt(args.pbdoterr ** 2.0 + pbdot_corr_err_high ** 2.0) # print 'Pbdot new (median and mean error) = ', pbdot_new, ' +/- ', pbdot_new_err # print 'Pbdot new range (68%) = ', pbdot_corr_min[0], ' , ', pbdot_corr_max[0] pbdot_new_mid = np.mean(np.array([pbdot_new - pbdot_new_err_low[0], pbdot_new + pbdot_new_err_high[0]])) print "Pbdot new (using midpoint between ranges) = ", pbdot_new_mid, " - ", pbdot_new_mid - ( pbdot_new - pbdot_new_err_low[0] ), " + ", (pbdot_new + pbdot_new_err_high[0]) - pbdot_new_mid print "Pbdot new (median corr and asymmetric error) = ", pbdot_new, " - ", pbdot_new_err_low[ 0 ], " + ", pbdot_new_err_high[0] else: pbdot_corr = pc.get_pbdot_corr( args.ra, args.dec, args.pm, args.dist, args.pb, v0=args.v0, R0=args.R0, verbose=True ) pbdot_new = args.pbdot - pbdot_corr print "\nWill not calculate final uncertainties...\n" print "Original Pbdot = ", args.pbdot print "Corrected Pbdot = ", pbdot_new return