def write_matches(savefilename): apokasc= match_apokasc_saga() #Perform RC selection logg= apokasc['KASC_RG_LOGG_SCALE_2'] teff= apokasc['TEFF'] z= 0.017*10.**apokasc['METALS'] jk= apokasc['J0']-apokasc['K0'] indx= (logg >= 1.8)\ *(logg <= 0.0018*(teff+382.5*apokasc['METALS']-4607)+2.5)\ *(jk < 0.8)\ *(jk >= 0.5)\ *(z <= 0.06)\ *(z <= rcmodel.jkzcut(jk,upper=True))\ *(z >= rcmodel.jkzcut(jk)) print "Found %i RC stars in APOKASC" % numpy.sum(indx) rcdists= numpy.zeros(len(apokasc))-1 rcd= rcdist('../data/rcmodel_mode_jkz_ks_parsec_newlogg.sav') rcdists[indx]= rcd(jk[indx],z[indx],apokasc['K0'][indx]) pindx= (rcdists > 0.)*(apokasc['DIST_SEISMO'] > 0.) apokasc= apokasc[pindx] rcdists= rcdists[pindx] print "Found %i RC stars in APOKASC with seismic distances" % numpy.sum(pindx) savefile= open(savefilename,'w') savefile.write('#ID,RCDIST(pc),SEISMODIST(SCALE,MO,pc),(SEISMODIST-RCDIST)/RCDIST\n') csvwriter = csv.writer(savefile, delimiter=',') for ii in range(len(apokasc)): csvwriter.writerow([apokasc['KEPLER ID'][ii], rcdists[ii]*1000., apokasc['DIST_SEISMO'][ii]*1000., (apokasc['DIST_SEISMO'][ii]-rcdists[ii])/rcdists[ii]]) savefile.close()
def compare_seismic_distances(plotfilename): apokasc= match_apokasc_saga() #Perform RC selection logg= apokasc['KASC_RG_LOGG_SCALE_2'] teff= apokasc['TEFF'] z= 0.017*10.**apokasc['METALS'] jk= apokasc['J0']-apokasc['K0'] indx= (logg >= 1.8)\ *(logg <= 0.0018*(teff+382.5*apokasc['METALS']-4607)+2.5)\ *(jk < 0.8)\ *(jk >= 0.5)\ *(z <= 0.06)\ *(z <= rcmodel.jkzcut(jk,upper=True))\ *(z >= rcmodel.jkzcut(jk))#\ #*(apokasc['SEISMO EVOL'] == 'CLUMP') print "Found %i RC stars in APOKASC" % numpy.sum(indx) rcdists= numpy.zeros(len(apokasc))-1 rcd= rcdist('../data/rcmodel_mode_jkz_ks_parsec_newlogg.sav') rcdists[indx]= rcd(jk[indx],z[indx],apokasc['K0'][indx]) pindx= (rcdists > 0.)*(apokasc['DIST_SEISMO'] > 0.) print "Found %i RC stars in APOKASC with seismic distances" % numpy.sum(pindx) #Setup plot bovy_plot.bovy_print(fig_height=7.) dx= 0.6 left, bottom, width, height= 0.1, 0.9-dx, 0.8, dx axTop= pyplot.axes([left,bottom,width,height]) fig= pyplot.gcf() fig.sca(axTop) bovy_plot.bovy_plot([0.,20.],[0.,20.],'k-',lw=2.,color='0.4', overplot=True,zorder=0) bovy_plot.bovy_plot(rcdists[pindx],apokasc['DIST_SEISMO'][pindx], 'k.',overplot=True,zorder=10) if False: pyplot.errorbar(rcdists[pindx], apokasc['DIST_SEISMO'][pindx], xerr=0.05*rcdists[pindx], yerr=apokasc['E_DIST_SEISMO'][pindx], marker=',',color='k', linestyle='none') thisax= pyplot.gca() thisax.set_ylim(0.,5.) pyplot.xlim(0.,5.) bovy_plot._add_ticks() nullfmt = NullFormatter() # no labels axTop.xaxis.set_major_formatter(nullfmt) bovy_plot._add_ticks() pyplot.ylabel(r'$\mathrm{seismic\ distance}\,(\mathrm{kpc})$') #Second plot left, bottom, width, height= 0.1, 0.1, 0.8, 0.8-dx thisax= pyplot.axes([left,bottom,width,height]) fig.sca(thisax) bovy_plot.bovy_plot([0.,20.],[0.,0.],'k-',lw=2.,color='0.4', overplot=True,zorder=0) bovy_plot.bovy_plot(rcdists[pindx], (apokasc['DIST_SEISMO'][pindx]-rcdists[pindx])/rcdists[pindx], 'k.',overplot=True,zorder=10) thisax= pyplot.gca() thisax.set_ylim(-0.2,0.2) pyplot.xlim(0.,5.) bovy_plot._add_ticks() nullfmt = NullFormatter() # no labels bovy_plot._add_ticks() pyplot.ylabel(r'$\mathrm{relative\ differene}$') pyplot.xlabel(r'$\mathrm{RC\ distance}\,(\mathrm{kpc})$') medoffset= numpy.median((apokasc['DIST_SEISMO'][pindx]-rcdists[pindx])/rcdists[pindx]) medsig= 1.4826*numpy.median(numpy.fabs((apokasc['DIST_SEISMO'][pindx]-rcdists[pindx])/rcdists[pindx]-medoffset)) bovy_plot.bovy_text(2.75,-0.125,r'$\mathrm{diff} = %.3f\pm%.3f$' % \ (medoffset,medsig),size=14.) bovy_plot.bovy_end_print(plotfilename)
def _calc_one(z, options, nages, lages, dlages): print z if options.allapogee or options.redapogee: rc = rcmodel.rcmodel( Z=z, loggmax=3.5, band=options.band, basti=options.basti, imfmodel=options.imfmodel, parsec=options.parsec, eta=options.eta, ) else: rc = rcmodel.rcmodel( Z=z, loggmin=1.8, loggmax="custom", band=options.band, basti=options.basti, imfmodel=options.imfmodel, parsec=options.parsec, eta=options.eta, ) out = numpy.zeros(nages) for jj in range(nages): jk = rc._jks aindx = (rc._lages <= lages[jj] + dlages) * (rc._lages > lages[jj] - dlages) if options.allapogee: aindx *= jk > 0.5 elif options.redapogee: aindx *= jk > 0.8 else: rcd = rcmodel.rcdist("../../rcdist-apogee/data/rcmodel_mode_jkz_ks_parsec_newlogg.sav") predH = numpy.array([rcd(j, z) for j in jk]) predH = numpy.reshape(predH, len(jk)) aindx *= ( (jk < 0.8) * (jk > 0.5) * (z <= rcmodel.jkzcut(jk, upper=True)) * (z >= rcmodel.jkzcut(jk)) * (z <= 0.06) * (rc._sample[:, 1] > (predH - 0.4)) * (rc._sample[:, 1] < (predH + 0.4)) * (rc._sample[:, 1] > -3.0) * (rc._loggs[:, 0] <= 3.5) ) if options.type == "omega": try: out[jj] = numpy.mean(rc._massweights[aindx]) except ValueError: out[jj] = numpy.nan elif options.type == "numfrac": try: out[jj] = numpy.mean(rc._weights[aindx]) except ValueError: out[jj] = numpy.nan elif options.type == "mass": try: out[jj] = numpy.sum(rc._masses[aindx] * rc._weights[aindx]) / numpy.sum(rc._weights[aindx]) except ValueError: out[jj] = numpy.nan return out
def make_rcsample(parser): options,args= parser.parse_args() savefilename= options.savefilename if savefilename is None: #Create savefilename if not given savefilename= os.path.join(appath._APOGEE_DATA, 'rcsample_'+appath._APOGEE_REDUX+'.fits') print "Saving to %s ..." % savefilename #Read the base-sample data= apread.allStar(adddist=_ADDHAYDENDIST,rmdups=options.rmdups) #Remove a bunch of fields that we do not want to keep data= esutil.numpy_util.remove_fields(data, ['TARGET_ID', 'FILE', 'AK_WISE', 'SFD_EBV', 'SYNTHVHELIO_AVG', 'SYNTHVSCATTER', 'SYNTHVERR', 'SYNTHVERR_MED', 'RV_TEFF', 'RV_LOGG', 'RV_FEH', 'RV_CCFWHM', 'RV_AUTOFWHM', 'SYNTHSCATTER', 'CHI2_THRESHOLD', 'APSTAR_VERSION', 'ASPCAP_VERSION', 'RESULTS_VERSION', 'REDUCTION_ID', 'SRC_H', 'PM_SRC']) if int(appath._APOGEE_REDUX[1:]) < 500: data= esutil.numpy_util.remove_fields(data, ['ELEM']) #Select red-clump stars jk= data['J0']-data['K0'] z= isodist.FEH2Z(data['METALS'],zsolar=0.017) if int(appath._APOGEE_REDUX[1:]) > 600: from apogee.tools import paramIndx if False: #Use my custom logg calibration that's correct for the RC logg= (1.-0.042)*data['FPARAM'][:,paramIndx('logg')]-0.213 lowloggindx= data['FPARAM'][:,paramIndx('logg')] < 1. logg[lowloggindx]= data['FPARAM'][lowloggindx,paramIndx('logg')]-0.255 hiloggindx= data['FPARAM'][:,paramIndx('logg')] > 3.8 logg[hiloggindx]= data['FPARAM'][hiloggindx,paramIndx('logg')]-0.3726 else: #Use my custom logg calibration that's correct on average logg= (1.+0.03)*data['FPARAM'][:,paramIndx('logg')]-0.37 lowloggindx= data['FPARAM'][:,paramIndx('logg')] < 1. logg[lowloggindx]= data['FPARAM'][lowloggindx,paramIndx('logg')]-0.34 hiloggindx= data['FPARAM'][:,paramIndx('logg')] > 3.8 logg[hiloggindx]= data['FPARAM'][hiloggindx,paramIndx('logg')]-0.256 else: logg= data['LOGG'] indx= (jk < 0.8)*(jk >= 0.5)\ *(z <= 0.06)\ *(z <= rcmodel.jkzcut(jk,upper=True))\ *(z >= rcmodel.jkzcut(jk))\ *(logg >= rcmodel.loggteffcut(data['TEFF'],z,upper=False))\ *(logg <= rcmodel.loggteffcut(data['TEFF'],z,upper=True)) data= data[indx] #Add more aggressive flag cut data= esutil.numpy_util.add_fields(data,[('ADDL_LOGG_CUT',numpy.int32)]) data['ADDL_LOGG_CUT']= ((data['TEFF']-4800.)/1000.+2.75) > data['LOGG'] if options.loggcut: data= data[data['ADDL_LOGG_CUT'] == 1] print "Making catalog of %i objects ..." % len(data) #Add distances data= esutil.numpy_util.add_fields(data,[('RC_DIST', float), ('RC_DM', float), ('RC_GALR', float), ('RC_GALPHI', float), ('RC_GALZ', float)]) rcd= rcmodel.rcdist('../../rcdist-apogee/data/rcmodel_mode_jkz_ks_parsec_newlogg.sav') jk= data['J0']-data['K0'] z= isodist.FEH2Z(data['METALS'],zsolar=0.017) data['RC_DIST']= rcd(jk,z,appmag=data['K0'])*options.distfac data['RC_DM']= 5.*numpy.log10(data['RC_DIST'])+10. XYZ= bovy_coords.lbd_to_XYZ(data['GLON'], data['GLAT'], data['RC_DIST'], degree=True) R,phi,Z= bovy_coords.XYZ_to_galcencyl(XYZ[:,0], XYZ[:,1], XYZ[:,2], Xsun=8.,Zsun=0.025) data['RC_GALR']= R data['RC_GALPHI']= phi data['RC_GALZ']= Z #Save fitsio.write(savefilename,data,clobber=True) if not options.nostat: #Determine statistical sample and add flag apo= apogee.select.apogeeSelect() statIndx= apo.determine_statistical(data) mainIndx= apread.mainIndx(data) data= esutil.numpy_util.add_fields(data,[('STAT',numpy.int32), ('INVSF',float)]) data['STAT']= 0 data['STAT'][statIndx*mainIndx]= 1 for ii in range(len(data)): if (statIndx*mainIndx)[ii]: data['INVSF'][ii]= 1./apo(data['LOCATION_ID'][ii], data['H'][ii]) else: data['INVSF'][ii]= -1. if options.nopm: fitsio.write(savefilename,data,clobber=True) return None #Get proper motions from astroquery.vizier import Vizier import astroquery from astropy import units as u import astropy.coordinates as coord pmfile= savefilename.split('.')[0]+'_pms.fits' if os.path.exists(pmfile): pmdata= fitsio.read(pmfile,1) else: pmdata= numpy.recarray(len(data), formats=['f8','f8','f8','f8','f8','f8','i4'], names=['RA','DEC','PMRA','PMDEC', 'PMRA_ERR','PMDEC_ERR','PMMATCH']) rad= u.Quantity(4./3600.,u.degree) v= Vizier(columns=['RAJ2000','DEJ2000','pmRA','pmDE','e_pmRA','e_pmDE']) for ii in range(len(data)): #if ii > 100: break sys.stdout.write('\r'+"Getting pm data for point %i / %i" % (ii+1,len(data))) sys.stdout.flush() pmdata.RA[ii]= data['RA'][ii] pmdata.DEC[ii]= data['DEC'][ii] co= coord.ICRS(ra=data['RA'][ii], dec=data['DEC'][ii], unit=(u.degree, u.degree)) trying= True while trying: try: tab= v.query_region(co,rad,catalog='I/322') #UCAC-4 catalog except astroquery.exceptions.TimeoutError: pass else: trying= False if len(tab) == 0: pmdata.PMMATCH[ii]= 0 print "Didn't find a match for %i ..." % ii continue else: pmdata.PMMATCH[ii]= len(tab) if len(tab[0]['pmRA']) > 1: print "Found more than 1 match for %i ..." % ii try: pmdata.PMRA[ii]= float(tab[0]['pmRA']) except TypeError: jj= 1 while len(tab[0]['pmRA']) > 1 and jj < 4: trad= u.Quantity((4.-jj)/3600.,u.degree) trying= True while trying: try: tab= v.query_region(co,trad,catalog='I/322') #UCAC-4 catalog except astroquery.exceptions.TimeoutError: pass else: trying= False jj+= 1 if len(tab) == 0: pmdata.PMMATCH[ii]= 0 print "Didn't find a unambiguous match for %i ..." % ii continue pmdata.PMRA[ii]= float(tab[0]['pmRA']) pmdata.PMDEC[ii]= float(tab[0]['pmDE']) pmdata.PMRA_ERR[ii]= float(tab[0]['e_pmRA']) pmdata.PMDEC_ERR[ii]= float(tab[0]['e_pmDE']) if numpy.isnan(float(tab[0]['pmRA'])): pmdata.PMMATCH[ii]= 0 sys.stdout.write('\r'+_ERASESTR+'\r') sys.stdout.flush() fitsio.write(pmfile,pmdata,clobber=True) #To make sure we're using the same format below pmdata= fitsio.read(pmfile,1) #Match proper motions try: #These already exist currently, but may not always exist data= esutil.numpy_util.remove_fields(data,['PMRA','PMDEC']) except ValueError: pass data= esutil.numpy_util.add_fields(data,[('PMRA', numpy.float), ('PMDEC', numpy.float), ('PMRA_ERR', numpy.float), ('PMDEC_ERR', numpy.float), ('PMMATCH',numpy.int32)]) data['PMMATCH']= 0 h=esutil.htm.HTM() m1,m2,d12 = h.match(pmdata['RA'],pmdata['DEC'], data['RA'],data['DEC'], 2./3600.,maxmatch=1) data['PMRA'][m2]= pmdata['PMRA'][m1] data['PMDEC'][m2]= pmdata['PMDEC'][m1] data['PMRA_ERR'][m2]= pmdata['PMRA_ERR'][m1] data['PMDEC_ERR'][m2]= pmdata['PMDEC_ERR'][m1] data['PMMATCH'][m2]= pmdata['PMMATCH'][m1].astype(numpy.int32) pmindx= data['PMMATCH'] == 1 data['PMRA'][True-pmindx]= -9999.99 data['PMDEC'][True-pmindx]= -9999.99 data['PMRA_ERR'][True-pmindx]= -9999.99 data['PMDEC_ERR'][True-pmindx]= -9999.99 #Calculate Galactocentric velocities data= esutil.numpy_util.add_fields(data,[('GALVR', numpy.float), ('GALVT', numpy.float), ('GALVZ', numpy.float)]) lb= bovy_coords.radec_to_lb(data['RA'],data['DEC'],degree=True) XYZ= bovy_coords.lbd_to_XYZ(lb[:,0],lb[:,1],data['RC_DIST'],degree=True) pmllpmbb= bovy_coords.pmrapmdec_to_pmllpmbb(data['PMRA'],data['PMDEC'], data['RA'],data['DEC'], degree=True) vxvyvz= bovy_coords.vrpmllpmbb_to_vxvyvz(data['VHELIO_AVG'], pmllpmbb[:,0], pmllpmbb[:,1], lb[:,0],lb[:,1],data['RC_DIST'], degree=True) vR, vT, vZ= bovy_coords.vxvyvz_to_galcencyl(vxvyvz[:,0], vxvyvz[:,1], vxvyvz[:,2], 8.-XYZ[:,0], XYZ[:,1], XYZ[:,2]+0.025, vsun=[-11.1,30.24*8.,7.25])#Assumes proper motion of Sgr A* and R0=8 kpc, zo= 25 pc data['GALVR']= vR data['GALVT']= vT data['GALVZ']= vZ data['GALVR'][True-pmindx]= -9999.99 data['GALVT'][True-pmindx]= -9999.99 data['GALVZ'][True-pmindx]= -9999.99 #Get proper motions pmfile= savefilename.split('.')[0]+'_pms_ppmxl.fits' if os.path.exists(pmfile): pmdata= fitsio.read(pmfile,1) else: pmdata= numpy.recarray(len(data), formats=['f8','f8','f8','f8','f8','f8','i4'], names=['RA','DEC','PMRA','PMDEC', 'PMRA_ERR','PMDEC_ERR','PMMATCH']) rad= u.Quantity(4./3600.,u.degree) v= Vizier(columns=['RAJ2000','DEJ2000','pmRA','pmDE','e_pmRA','e_pmDE']) for ii in range(len(data)): #if ii > 100: break sys.stdout.write('\r'+"Getting pm data for point %i / %i" % (ii+1,len(data))) sys.stdout.flush() pmdata.RA[ii]= data['RA'][ii] pmdata.DEC[ii]= data['DEC'][ii] co= coord.ICRS(ra=data['RA'][ii], dec=data['DEC'][ii], unit=(u.degree, u.degree)) trying= True while trying: try: tab= v.query_region(co,rad,catalog='I/317') #PPMXL catalog except astroquery.exceptions.TimeoutError: pass else: trying= False if len(tab) == 0: pmdata.PMMATCH[ii]= 0 print "Didn't find a match for %i ..." % ii continue else: pmdata.PMMATCH[ii]= len(tab) if len(tab[0]['pmRA']) > 1: pass #print "Found more than 1 match for %i ..." % ii try: pmdata.PMRA[ii]= float(tab[0]['pmRA']) except TypeError: #Find nearest cosdists= numpy.zeros(len(tab[0]['pmRA'])) for jj in range(len(tab[0]['pmRA'])): cosdists[jj]= cos_sphere_dist(tab[0]['RAJ2000'][jj], tab[0]['DEJ2000'][jj], data['RA'][ii], data['DEC'][ii]) closest= numpy.argmax(cosdists) pmdata.PMRA[ii]= float(tab[0]['pmRA'][closest]) pmdata.PMDEC[ii]= float(tab[0]['pmDE'][closest]) pmdata.PMRA_ERR[ii]= float(tab[0]['e_pmRA'][closest]) pmdata.PMDEC_ERR[ii]= float(tab[0]['e_pmDE'][closest]) if numpy.isnan(float(tab[0]['pmRA'][closest])): pmdata.PMMATCH[ii]= 0 else: pmdata.PMDEC[ii]= float(tab[0]['pmDE']) pmdata.PMRA_ERR[ii]= float(tab[0]['e_pmRA']) pmdata.PMDEC_ERR[ii]= float(tab[0]['e_pmDE']) if numpy.isnan(float(tab[0]['pmRA'])): pmdata.PMMATCH[ii]= 0 sys.stdout.write('\r'+_ERASESTR+'\r') sys.stdout.flush() fitsio.write(pmfile,pmdata,clobber=True) #To make sure we're using the same format below pmdata= fitsio.read(pmfile,1) #Match proper motions to ppmxl data= esutil.numpy_util.add_fields(data,[('PMRA_PPMXL', numpy.float), ('PMDEC_PPMXL', numpy.float), ('PMRA_ERR_PPMXL', numpy.float), ('PMDEC_ERR_PPMXL', numpy.float), ('PMMATCH_PPMXL',numpy.int32)]) data['PMMATCH_PPMXL']= 0 h=esutil.htm.HTM() m1,m2,d12 = h.match(pmdata['RA'],pmdata['DEC'], data['RA'],data['DEC'], 2./3600.,maxmatch=1) data['PMRA_PPMXL'][m2]= pmdata['PMRA'][m1] data['PMDEC_PPMXL'][m2]= pmdata['PMDEC'][m1] data['PMRA_ERR_PPMXL'][m2]= pmdata['PMRA_ERR'][m1] data['PMDEC_ERR_PPMXL'][m2]= pmdata['PMDEC_ERR'][m1] data['PMMATCH_PPMXL'][m2]= pmdata['PMMATCH'][m1].astype(numpy.int32) pmindx= data['PMMATCH_PPMXL'] == 1 data['PMRA_PPMXL'][True-pmindx]= -9999.99 data['PMDEC_PPMXL'][True-pmindx]= -9999.99 data['PMRA_ERR_PPMXL'][True-pmindx]= -9999.99 data['PMDEC_ERR_PPMXL'][True-pmindx]= -9999.99 #Calculate Galactocentric velocities data= esutil.numpy_util.add_fields(data,[('GALVR_PPMXL', numpy.float), ('GALVT_PPMXL', numpy.float), ('GALVZ_PPMXL', numpy.float)]) lb= bovy_coords.radec_to_lb(data['RA'],data['DEC'],degree=True) XYZ= bovy_coords.lbd_to_XYZ(lb[:,0],lb[:,1],data['RC_DIST'],degree=True) pmllpmbb= bovy_coords.pmrapmdec_to_pmllpmbb(data['PMRA_PPMXL'], data['PMDEC_PPMXL'], data['RA'],data['DEC'], degree=True) vxvyvz= bovy_coords.vrpmllpmbb_to_vxvyvz(data['VHELIO_AVG'], pmllpmbb[:,0], pmllpmbb[:,1], lb[:,0],lb[:,1],data['RC_DIST'], degree=True) vR, vT, vZ= bovy_coords.vxvyvz_to_galcencyl(vxvyvz[:,0], vxvyvz[:,1], vxvyvz[:,2], 8.-XYZ[:,0], XYZ[:,1], XYZ[:,2]+0.025, vsun=[-11.1,30.24*8.,7.25])#Assumes proper motion of Sgr A* and R0=8 kpc, zo= 25 pc data['GALVR_PPMXL']= vR data['GALVT_PPMXL']= vT data['GALVZ_PPMXL']= vZ data['GALVR_PPMXL'][True-pmindx]= -9999.99 data['GALVT_PPMXL'][True-pmindx]= -9999.99 data['GALVZ_PPMXL'][True-pmindx]= -9999.99 #Save fitsio.write(savefilename,data,clobber=True) return None