def query_dustmodel_coords(ra,dec,dust): if dust == 'allsky': reddenMap = mwdust.Combined19() ext = extinction('green19') if dust == 'green19': reddenMap = mwdust.Green19() ext = extinction('green19') if dust == 'zero': reddenMap = mwdust.Zero() ext = extinction('cardelli') if dust == 'none': reddenMap = 0 ext = extinction('cardelli') print('Fitting for reddening.') return reddenMap,ext sightLines = SkyCoord(ra*units.deg,dec*units.deg,frame='galactic') distanceSamples = np.loadtxt(f"{PACKAGEDIR}/data/distance-samples-green19.txt",delimiter=',')*1000. reddenContainer = reddenMap(sightLines.l.value,sightLines.b.value,distanceSamples/1000.) del reddenMap # To clear reddenMap from memory dustModelDF = pd.DataFrame({'ra': [ra], 'dec': [dec]}) for index in range(len(reddenContainer)): dustModelDF['av_'+str(round(distanceSamples[index],6))] = reddenContainer[index] return dustModelDF,ext
def calc_effsel(args, options, sample=None): # Work-horse function to compute the effective selection function, # sample is a data sample of stars to consider for the (JK,Z) sampling # Setup selection function selectFile = '../savs/selfunc-nospdata.sav' if os.path.exists(selectFile): with open(selectFile, 'rb') as savefile: apo = pickle.load(savefile) else: # Setup selection function apo = apogee.select.apogeeSelect() # Delete these because they're big and we don't need them del apo._specdata del apo._photdata save_pickles(selectFile, apo) # Get the full data sample for the locations (need all locations where # stars could be observed, so the whole sample, not just the subsample # being analyzed) data = get_rcsample() locations = list(set(list(data['LOCATION_ID']))) # Load the dust map and setup the effective selection function if options.dmap.lower() == 'green15': dmap3d = mwdust.Green15(filter='2MASS H') elif options.dmap.lower() == 'marshall06': dmap3d = mwdust.Marshall06(filter='2MASS H') elif options.dmap.lower() == 'drimmel03': dmap3d = mwdust.Drimmel03(filter='2MASS H') elif options.dmap.lower() == 'sale14': dmap3d = mwdust.Sale14(filter='2MASS H') elif options.dmap.lower() == 'zero': dmap3d = mwdust.Zero(filter='2MASS H') # Sample the M_H distribution if options.samplemh: if sample is None: sample = data MH = sample['H0'] - sample['RC_DM'] MH = numpy.random.permutation(MH)[:1000] # do 1,000 max else: MH = -1.49 apof = apogee.select.apogeeEffectiveSelect(apo, dmap3d=dmap3d, MH=MH) # Distances at which to calculate the effective selection function distmods = numpy.linspace(options.dm_min, options.dm_max, options.ndm) ds = 10.**(distmods / 5 - 2.) # Now compute all selection functions out= multi.parallel_map((lambda x: _calc_effsel_onelocation(\ locations[x],apof,apo,ds)), range(len(locations)), numcores=numpy.amin([len(locations), multiprocessing.cpu_count(),options.multi])) # Save out out = numpy.array(out) save_pickles(args[0], locations, out, distmods, ds) return None
def __init__(self, tgasSel, MJ=1.8, JK=0.25, dmap3d=None, maxd=None): """ NAME: __init__ PURPOSE: Initialize the effective TGAS selection function for a population of stars INPUT: tgasSel - a tgasSelect object with the TGAS selection function MJ= (1.8) absolute magnitude in J or an array of samples of absolute magnitudes in J for the tracer population JK= (0.25) J-Ks color or an array of samples of the J-Ks color dmap3d= if given, a mwdust.Dustmap3D object that returns the J-band extinction in 3D; if not set use no extinction maxd= (None) if given, only consider distances up to this maximum distance (in kpc) OUTPUT: TGAS-effective-selection-function object HISTORY: 2017-01-18 - Started - Bovy (UofT/CCA) """ self._tgasSel = tgasSel self._maxd = maxd # Parse MJ if isinstance(MJ, (int, float)): self._MJ = numpy.array([MJ]) elif isinstance(MJ, list): self._MJ = numpy.array(MJ) else: self._MJ = MJ # Parse JK if isinstance(JK, (int, float)): self._JK = numpy.array([JK]) elif isinstance(JK, list): self._JK = numpy.array(JK) else: self._JK = JK # Parse dust map if dmap3d is None: if not _MWDUSTLOADED: raise ImportError( "mwdust module not installed, required for extinction tools; download and install from http://github.com/jobovy/mwdust" ) dmap3d = mwdust.Zero(filter='2MASS J') self._dmap3d = dmap3d return None
def generate_evfs(mj_tight, jk_tight, spt, zspace, nintt_step=None, filename="default_evfs.txt", save=True): zWidth = np.mean((np.roll(zspace, -1) - zspace)[:-1]) evfs = np.array([]) tesf = gaia_tools.select.gaiaEffectiveSelect(tsf, dmap3d=mwdust.Zero(), MJ=mj_tight, JK=jk_tight, maxd=max_dist) if nintt_step == None: nintt_step = (2501 * ('A' in spt) + 1001 * (True - ('A' in spt))) for i, z_i in enumerate(zspace): zmin = z_i - zWidth / 2. zmax = z_i + zWidth / 2. evfs = np.append( evfs, tesf.volume(lambda x, y, z: cyl_vol_func( x, y, z, xymax=r_cyl_cut, zmin=zmin, zmax=zmax), ndists=nintt_step, xyz=True, relative=True)) if save == True: np.savetxt(filename, np.transpose(np.array([zspace, evfs])), delimiter=',', header="z_Bin_center, rel_effective_vol", fmt='%10.5f') return (zspace, evfs)
def plot_effsel_location(location, plotname): # Setup selection function selectFile = '../savs/selfunc-nospdata.sav' if os.path.exists(selectFile): with open(selectFile, 'rb') as savefile: apo = pickle.load(savefile) else: # Setup selection function apo = apogee.select.apogeeSelect() # Delete these because they're big and we don't need them del apo._specdata del apo._photdata save_pickles(selectFile, apo) effselFile = '../savs/effselfunc-%i.sav' % location if not os.path.exists(effselFile): # Distances at which to calculate the effective selection function distmods = numpy.linspace(7., 15.5, 301) ds = 10.**(distmods / 5 - 2.) # Setup default effective selection function do_samples = True gd = mwdust.Green15(filter='2MASS H', load_samples=do_samples) apof = apogee.select.apogeeEffectiveSelect(apo, dmap3d=gd) sf_default = apof(location, ds) # Also calculate for a sample of MH data = get_rcsample() MH = data['H0'] - data['RC_DM'] MH = numpy.random.permutation(MH)[:1000] sf_jkz = apof(location, ds, MH=MH) # Go through the samples sf_samples = numpy.zeros((20, len(ds))) if do_samples: for ii in range(20): # Swap in a sample for bestfit in the Green et al. (2015) dmap gd.substitute_sample(ii) apof = apogee.select.apogeeEffectiveSelect(apo, dmap3d=gd) sf_samples[ii] = apof(location, ds) zerodust = mwdust.Zero(filter='2MASS H') apof = apogee.select.apogeeEffectiveSelect(apo, dmap3d=zerodust) sf_zero = apof(location, ds) drimmel = mwdust.Drimmel03(filter='2MASS H') apof = apogee.select.apogeeEffectiveSelect(apo, dmap3d=drimmel) sf_drimmel = apof(location, ds) marshall = mwdust.Marshall06(filter='2MASS H') apof = apogee.select.apogeeEffectiveSelect(apo, dmap3d=marshall) try: sf_marshall = apof(location, ds) except IndexError: sf_marshall = -numpy.ones_like(ds) sale = mwdust.Sale14(filter='2MASS H') apof = apogee.select.apogeeEffectiveSelect(apo, dmap3d=sale) try: sf_sale = apof(location, ds) except (TypeError, ValueError): sf_sale = -numpy.ones_like(ds) save_pickles(effselFile, distmods, sf_default, sf_jkz, sf_samples, sf_zero, sf_drimmel, sf_marshall, sf_sale) else: with open(effselFile, 'rb') as savefile: distmods = pickle.load(savefile) sf_default = pickle.load(savefile) sf_jkz = pickle.load(savefile) sf_samples = pickle.load(savefile) sf_zero = pickle.load(savefile) sf_drimmel = pickle.load(savefile) sf_marshall = pickle.load(savefile) sf_sale = pickle.load(savefile) # Now plot bovy_plot.bovy_print(fig_height=3.) rc('text.latex', preamble=r'\usepackage{amsmath}' + '\n' + r'\usepackage{amssymb}' + '\n' + r'\usepackage{yfonts}') if _PLOTDIST: distmods = 10.**(distmods / 5 - 2.) xrange = [0., 12.] xlabel = r'$D\,(\mathrm{kpc})$' ylabel = r'$\textswab{S}(\mathrm{location},D)$' else: xrange = [7., 15.8], xlabel = r'$\mathrm{distance\ modulus}\ \mu$' ylabel = r'$\textswab{S}(\mathrm{location},\mu)$' line_default = bovy_plot.bovy_plot(distmods, sf_default, 'b-', lw=_LW, zorder=12, xrange=xrange, xlabel=xlabel, yrange=[0., 1.2 * numpy.amax(sf_zero)], ylabel=ylabel) pyplot.fill_between(distmods, sf_default-_EXAGGERATE_ERRORS\ *(sf_default-numpy.amin(sf_samples,axis=0)), sf_default+_EXAGGERATE_ERRORS\ *(numpy.amax(sf_samples,axis=0)-sf_default), color='0.65',zorder=0) line_jkz = bovy_plot.bovy_plot(distmods, sf_jkz, 'g-.', lw=2. * _LW, overplot=True, zorder=13) line_zero = bovy_plot.bovy_plot(distmods, sf_zero, 'k--', lw=_LW, overplot=True, zorder=7) line_drimmel = bovy_plot.bovy_plot(distmods, sf_drimmel, '-', color='gold', lw=_LW, overplot=True, zorder=8) line_marshall = bovy_plot.bovy_plot(distmods, sf_marshall, 'r-', lw=_LW, overplot=True, zorder=9) line_sale = bovy_plot.bovy_plot(distmods, sf_sale, 'c-', lw=_LW, overplot=True, zorder=10) if location == 4378: pyplot.legend( (line_default[0], line_jkz[0], line_zero[0]), (r'$\mathrm{Green\ et\ al.\ (2015)}$', r'$\mathrm{Green\ et\ al.} + p(M_H)$', r'$\mathrm{zero\ extinction}$'), loc='lower right', #bbox_to_anchor=(.91,.375), numpoints=8, prop={'size': 14}, frameon=False) elif location == 4312: pyplot.legend( (line_sale[0], line_marshall[0], line_drimmel[0]), (r'$\mathrm{Sale\ et\ al.\ (2014)}$', r'$\mathrm{Marshall\ et\ al.\ (2006)}$', r'$\mathrm{Drimmel\ et\ al.\ (2003)}$'), loc='lower right', #bbox_to_anchor=(.91,.375), numpoints=8, prop={'size': 14}, frameon=False) # Label lcen, bcen = apo.glonGlat(location) if numpy.fabs(bcen) < 0.1: bcen = 0. bovy_plot.bovy_text(r'$(l,b) = (%.1f,%.1f)$' % (lcen, bcen), top_right=True, size=16.) bovy_plot.bovy_end_print(plotname) return None
#import healpy as hp #import time from pylab import * from matplotlib.colors import LogNorm from mpl_toolkits.axes_grid1.inset_locator import inset_axes #mpl.rcParams['pdf.fonttype'] = 42 #mpl.rcParams['ps.fonttype'] = 42 #mpl.rcParams['text.usetex'] = True #marshall = mwdust.Marshall06(sf10=True) drimmel = mwdust.Drimmel03(sf10=True) green = mwdust.Green15(sf10=True) sale = mwdust.Sale14(sf10=True) zero = mwdust.Zero(sf10=True) #sfd = mwdust.SFD(sf10=True) #combined = mwdust.Combined15(sf10=True) #D = np.array([0.25,0.5,1.,2.,3.,4.,5.,6.]) Ndist = 1000 D = np.linspace(0.05,10.,Ndist) L = 205.09 # 54.7 B = -0.93 #0.08 f = open("Green15.dat", "w") for i in range(Ndist): f.write("%.15E %.15E\n"%(D[i],green(L,B,D)[i])) f.close() f = open("Drimmel03.dat", "w") for i in range(Ndist):