Example #1
0
def meanExtinction(l, b, ds, filter='Ks', radius=1.5, mar=None):
    """
    NAME:
       meanExtinction
    PURPOSE:
       calculate the mean extinction as a function of distance over a field
       centered at l and b
    INPUT:
       l,b- Galactic longitude and latitude (degree)
       ds- distances to consider (array)
       filter= ('J', 'H', 'Ks') filter to return the extinction in
       radius= (1.5) radius of the field (degree)
       mar= (None) mwdust.Marshall06 instance to be used for the Marshall map
    OUTPUT:
       mean extinction over the field as a function of d
    HISTORY:
       2013-12-18 - Written - Bovy (IAS)
    """
    if (l <= 100. or l >= 260.) and numpy.fabs(b) <= 10.:
        if mar is None:
            mar = mwdust.Marshall06(filter='2MASS ' + filter)
    #Find all of the (l,b) pairs in the Marshall map that fall within the field
    cosdist=\
        mwdust.util.tools.cos_sphere_dist(\
        numpy.sin(mar._marshalldata['GLON'].data*_DEGTORAD),
        numpy.cos(mar._marshalldata['GLON'].data*_DEGTORAD),
        numpy.sin(mar._marshalldata['GLAT'].data*_DEGTORAD),
        numpy.cos(mar._marshalldata['GLAT'].data*_DEGTORAD),
        numpy.sin(l*_DEGTORAD),
        numpy.cos(l*_DEGTORAD),
        numpy.sin(b*_DEGTORAD),
        numpy.cos(b*_DEGTORAD))
    indx = cosdist >= numpy.cos(radius * _DEGTORAD)
    return None
Example #2
0
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
Example #3
0
def plot_dustinfield(plotname, field, threshold=False):
    savefile = _SAVEFILE_MARSHALL
    savefile = savefile.replace('FIELD', '%i' % field)
    # Grid
    ls = numpy.linspace(-1.75, 1.75, _NL) + field
    bs = numpy.linspace(-1.75, 1.75, _NB)
    if not os.path.exists(savefile):
        # Setup dust map
        dmap = mwdust.Marshall06(filter='2MASS H')
        plotthis = numpy.empty((_NL, _NB))
        for jj in range(_NB):
            print jj
            for ii in range(_NL):
                plotthis[ii, jj] = dmap(ls[ii], bs[jj], 7.)
        save_pickles(savefile, plotthis)
    else:
        with open(savefile, 'rb') as f:
            plotthis = pickle.load(f)
    # Remove outside the field
    tls = numpy.tile(ls, (_NB, 1)).T
    tbs = numpy.tile(bs, (_NL, 1))
    plotthis[(tls - field)**2. + tbs**2. > 1.5**2.] = numpy.nan
    if threshold:
        plotthis[plotthis > 1.4] = numpy.nan
    # Now plot
    bovy_plot.bovy_print()
    bovy_plot.bovy_dens2d(
        plotthis[::-1].T,
        origin='lower',
        cmap=cm.coolwarm,
        interpolation='nearest',
        vmin=0.,
        vmax=2.,
        aspect=1.,
        xrange=[ls[-1] + (ls[1] - ls[0]) / 2., ls[0] - (ls[1] - ls[0]) / 2.],
        yrange=[bs[0] - (bs[1] - bs[0]) / 2., bs[-1] + (bs[1] - bs[0]) / 2.],
        xlabel=r'$l\,\mathrm{(deg)}$',
        ylabel=r'$b\,\mathrm{(deg)}$',
        zlabel=r'$A_H\,(\mathrm{mag})$',
        zorder=0)
    bovy_plot.bovy_text(r'$(l,b) = (%i,0)$' % field,
                        top_left=True,
                        color='k',
                        size=16.)
    bovy_plot.bovy_end_print(plotname)
    return None
Example #4
0
def select_targets(outfile, location):
    numpy.random.seed(location)
    datafilename = '../target_info/SecQuanObj_%03i+00.fits' % location
    data = fitsio.read(datafilename)
    # De-redden
    ak = data['AK']
    aj = ak * 2.5
    j0 = data['J_M'] - aj
    k0 = data['K_M'] - ak
    # Cut on jk0,h
    indx = ((j0 - k0) > 0.8) * (data['H_M'] > 12.) * (data['H_M'] < 13.)
    data = data[indx]
    # setup dust map
    dmap = mwdust.Marshall06(filter='2MASS H')
    # Calculate A_H(7 kpc) according to Marshall et al. (2006)
    ahdmap = numpy.zeros(len(data))
    lb = bovy_coords.radec_to_lb(data['RA'], data['DEC'], degree=True)
    for ii in range(len(data)):
        ahdmap[ii] = dmap(lb[ii, 0], lb[ii, 1], 7.)
    # Cut on AH <= 1.4
    indx = ahdmap <= 1.4
    data = data[indx]
    # Match against already drilled targets
    data = esutil.numpy_util.add_fields(data, [('FLAG_DONT_OBSERVE', int)])
    data['FLAG_DONT_OBSERVE'] = 0
    yannyfiles = glob.glob('../target_info/plateHolesSorted-*.par')
    for yannyfile in yannyfiles:
        drillFile = yanny.yanny(filename=yannyfile, np=True)
        drilled = drillFile['STRUCT1']
        # spherematch
        h = esutil.htm.HTM()
        m1, m2, d12 = h.match(data['RA'],
                              data['DEC'],
                              drilled['target_ra'],
                              drilled['target_dec'],
                              2. / 3600.,
                              maxmatch=1)
        data['FLAG_DONT_OBSERVE'][m1] = 1
    # Write to file
    fitsio.write(outfile,
                 data[numpy.random.permutation(len(data))],
                 clobber=True)
    return None
def combine_dustmap(picklename):
    if os.path.exists(picklename): return None
    ndists = len(_GREEN15DISTS)
    # First fill in with NSIDE = 512 for Marshall
    marshallmap = mwdust.Marshall06()
    nside_mar = 512
    mar_pix = numpy.arange(healpy.pixelfunc.nside2npix(nside_mar))
    mar_val = numpy.zeros((len(mar_pix), ndists)) + healpy.UNSEEN
    theta, phi= \
        healpy.pixelfunc.pix2ang(nside_mar,mar_pix,nest=True)
    bb = (numpy.pi / 2. - theta) / numpy.pi * 180.
    ll = phi / numpy.pi * 180.
    subIndx= (numpy.fabs(bb) < 10.125)\
        *((ll < 100.125)+(ll > 259.875))
    mar_pix = mar_pix[subIndx]
    mar_val = mar_val[subIndx]
    ll = ll[subIndx]
    ll[ll > 180.] -= 360.
    bb = bb[subIndx]
    for pp, dpix in enumerate(mar_pix):
        sys.stdout.write('\r' + "Working on pixel %i, %i remaining ...\r" %
                         (pp + 1, len(mar_pix) - pp))
        sys.stdout.flush()
        if _DRYISHRUN and pp > 100: break
        mar_val[pp] = marshallmap(ll[pp], bb[pp], _GREEN15DISTS)
    sys.stdout.write('\r' + _ERASESTR + '\r')
    sys.stdout.flush()
    # Now load Green15 and remove those pixels that fall within the Marshall map
    with h5py.File(os.path.join(_greendir, 'dust-map-3d.h5'),
                   'r') as greendata:
        pix_info = greendata['/pixel_info'][:]
        best_fit = greendata['/best_fit'][:]
    # Check whether any of these fall within the Marshall map
    theta, phi = healpy.pixelfunc.pix2ang(
        pix_info['nside'].astype('int32'),
        pix_info['healpix_index'].astype('int64'),
        nest=True)
    inMar= ((phi < 100.125*_DEGTORAD)+(phi > 259.875*_DEGTORAD))\
        *(numpy.fabs(numpy.pi/2.-theta) < 10.125*_DEGTORAD)
    best_fit[inMar] = healpy.UNSEEN
    nside_min = numpy.min(pix_info['nside'])
    nside_max = numpy.max(pix_info['nside'])
    # Fill in remaining gaps with Drimmel at NSIDE=256
    pix_drim = []
    pix_drim_nside = []
    val_drim = []
    drimmelmap = mwdust.Drimmel03()
    for nside_drim in 2**numpy.arange(8, int(numpy.log2(nside_max)) + 1, 1):
        tpix = numpy.arange(healpy.pixelfunc.nside2npix(nside_drim))
        rmIndx = numpy.zeros(len(tpix), dtype='bool')
        # Remove pixels that already have values at this or a higher level
        for nside in 2**numpy.arange(8, int(numpy.log2(nside_max)) + 1, 1):
            mult_factor = (nside / nside_drim)**2
            tgpix = pix_info['healpix_index'][pix_info['nside'] == nside]
            for offset in range(mult_factor):
                rmIndx[numpy.in1d(tpix * mult_factor + offset,
                                  tgpix,
                                  assume_unique=True)] = True
        # Remove pixels that already have values at a lower level
        for nside in 2**numpy.arange(int(numpy.log2(nside_min)),
                                     int(numpy.log2(nside_drim)), 1):
            mult_factor = (nside_drim / nside)**2
            # in Green 15
            tgpix = pix_info['healpix_index'][pix_info['nside'] == nside]
            rmIndx[numpy.in1d(tpix // mult_factor, tgpix,
                              assume_unique=False)] = True
            # In the current Drimmel
            tdpix = numpy.array(pix_drim)[numpy.array(pix_drim_nside) == nside]
            rmIndx[numpy.in1d(tpix // mult_factor, tdpix,
                              assume_unique=False)] = True
        # Also remove pixels that lie within the Marshall area
        theta, phi = healpy.pixelfunc.pix2ang(nside_drim, tpix, nest=True)
        inMar= ((phi < 100.125*_DEGTORAD)+(phi > 259.875*_DEGTORAD))\
            *(numpy.fabs(numpy.pi/2.-theta) < 10.125*_DEGTORAD)
        rmIndx[inMar] = True
        tpix = tpix[True - rmIndx]
        pix_drim.extend(tpix)
        pix_drim_nside.extend(nside_drim * numpy.ones(len(tpix)))
        ll = phi[True - rmIndx] / _DEGTORAD
        bb = (numpy.pi / 2. - theta[True - rmIndx]) / _DEGTORAD
        for pp in range(len(tpix)):
            sys.stdout.write(
                '\r' + "Working on level %i, pixel %i, %i remaining ...\r" %
                (nside_drim, pp + 1, len(tpix) - pp))
            sys.stdout.flush()
            val_drim.append(drimmelmap(ll[pp], bb[pp], _GREEN15DISTS))
            if _DRYISHRUN and pp > 1000: break
    sys.stdout.write('\r' + _ERASESTR + '\r')
    sys.stdout.flush()
    # Save
    g15Indx = best_fit[:, 0] != healpy.UNSEEN
    save_pickles(picklename, mar_pix, mar_val, pix_info['nside'][g15Indx],
                 pix_info['healpix_index'][g15Indx], best_fit[g15Indx],
                 pix_drim, pix_drim_nside, val_drim)
    return None
Example #6
0
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
def plot_dustnearplane(plotname, green=False):
    if green: savefile = _SAVEFILE_GREEN
    else: savefile = _SAVEFILE_MARSHALL
    # Grid
    ls = numpy.linspace(15., 70., _NL)
    bs = numpy.linspace(-2., 2., _NB)
    if not os.path.exists(savefile):
        # Setup dust map
        if green:
            dmap = mwdust.Green15(filter='2MASS H')
        else:
            dmap = mwdust.Marshall06(filter='2MASS H')
        plotthis = numpy.empty((_NL, _NB))
        rad = 0.5  # deg
        for jj in range(_NB):
            print jj
            for ii in range(_NL):
                pa, ah = dmap.dust_vals_disk(ls[ii], bs[jj], 7., rad)
                plotthis[ii, jj] = numpy.sum(pa * ah) / numpy.sum(pa)
        save_pickles(savefile, plotthis)
    else:
        with open(savefile, 'rb') as f:
            plotthis = pickle.load(f)
    # Now plot
    bovy_plot.bovy_print(fig_width=8.4, fig_height=4.)
    bovy_plot.bovy_dens2d(
        plotthis[::-1].T,
        origin='lower',
        cmap=cm.coolwarm,
        #                          interpolation='nearest',
        colorbar=True,
        shrink=0.45,
        vmin=0.,
        vmax=2. - 0.75 * green,
        aspect=3.,
        xrange=[ls[-1] + (ls[1] - ls[0]) / 2., ls[0] - (ls[1] - ls[0]) / 2.],
        yrange=[bs[0] - (bs[1] - bs[0]) / 2., bs[-1] + (bs[1] - bs[0]) / 2.],
        xlabel=r'$l\,\mathrm{(deg)}$',
        ylabel=r'$b\,\mathrm{(deg)}$',
        zlabel=r'$A_H\,(\mathrm{mag})$',
        zorder=0)
    bovy_plot.bovy_text(r'$D = 7\,\mathrm{kpc}$',
                        top_left=True,
                        color='w',
                        size=16.)
    # Overplot fields
    glons = [34., 64., 27.]
    glats = [0., 0., 0.]
    colors = ['w', 'w', 'y']
    xs = numpy.linspace(-1.5, 1.5, 201)
    ys = numpy.sqrt(1.5**2. - xs**2.)
    for glon, glat, c in zip(glons, glats, colors):
        bovy_plot.bovy_plot(xs + glon,
                            ys + glat,
                            '-',
                            overplot=True,
                            zorder=1,
                            lw=2.,
                            color=c)
        bovy_plot.bovy_plot(xs + glon,
                            -ys + glat,
                            '-',
                            overplot=True,
                            zorder=1,
                            lw=2.,
                            color=c)
    bovy_plot.bovy_end_print(plotname)
    return None
def plot_ah_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)
    glon, glat = apo.glonGlat(location)
    glon = glon[0]
    glat = glat[0]
    ahFile = '../savs/ah-%i.sav' % location
    if not os.path.exists(ahFile):
        # Distances at which to calculate the extinction
        distmods = numpy.linspace(7., 15.5, 301)
        ds = 10.**(distmods / 5 - 2.)
        # Setup Green et al. (2015) dust map
        gd = mwdust.Green15(filter='2MASS H')
        pa, ah = gd.dust_vals_disk(glon, glat, ds, apo.radius(location))
        meanah_default = numpy.sum(numpy.tile(pa, (len(ds), 1)).T * ah,
                                   axis=0) / numpy.sum(pa)
        stdah_default= numpy.sqrt(numpy.sum(numpy.tile(pa,(len(ds),1)).T\
                                                *ah**2.,axis=0)\
                                      /numpy.sum(pa)-meanah_default**2.)
        # Marshall et al. (2006)
        marshall = mwdust.Marshall06(filter='2MASS H')
        try:
            pa, ah = marshall.dust_vals_disk(glon, glat, ds,
                                             apo.radius(location))
        except IndexError:
            meanah_marshall = -numpy.ones_like(ds)
            stdah_marshall = -numpy.ones_like(ds)
        else:
            meanah_marshall = numpy.sum(numpy.tile(pa, (len(ds), 1)).T * ah,
                                        axis=0) / numpy.sum(pa)
            stdah_marshall= numpy.sqrt(numpy.sum(numpy.tile(pa,(len(ds),1)).T\
                                                     *ah**2.,axis=0)\
                                       /numpy.sum(pa)-meanah_marshall**2.)
        if True:
            # Drimmel et al. (2003)
            drimmel = mwdust.Drimmel03(filter='2MASS H')
            pa, ah = drimmel.dust_vals_disk(glon, glat, ds,
                                            apo.radius(location))
            meanah_drimmel = numpy.sum(numpy.tile(pa, (len(ds), 1)).T * ah,
                                       axis=0) / numpy.sum(pa)
            stdah_drimmel= numpy.sqrt(numpy.sum(numpy.tile(pa,(len(ds),1)).T\
                                                    *ah**2.,axis=0)\
                                          /numpy.sum(pa)-meanah_drimmel**2.)
        else:
            meanah_drimmel = -numpy.ones_like(ds)
            stdah_drimmel = -numpy.ones_like(ds)
        if True:
            # Sale et al. (2014)
            sale = mwdust.Sale14(filter='2MASS H')
            try:
                pa, ah = sale.dust_vals_disk(glon, glat, ds,
                                             apo.radius(location))
                meanah_sale = numpy.sum(numpy.tile(pa, (len(ds), 1)).T * ah,
                                        axis=0) / numpy.sum(pa)
            except (TypeError, ValueError):
                meanah_sale = -numpy.ones_like(ds)
                stdah_sale = -numpy.ones_like(ds)
            else:
                stdah_sale= numpy.sqrt(numpy.sum(numpy.tile(pa,(len(ds),1)).T\
                                                     *ah**2.,axis=0)\
                                           /numpy.sum(pa)-meanah_sale**2.)
        else:
            meanah_sale = -numpy.ones_like(ds)
            stdah_sale = -numpy.ones_like(ds)
        save_pickles(ahFile, distmods, meanah_default, stdah_default,
                     meanah_marshall, stdah_marshall, meanah_drimmel,
                     stdah_drimmel, meanah_sale, stdah_sale)
    else:
        with open(ahFile, 'rb') as savefile:
            distmods = pickle.load(savefile)
            meanah_default = pickle.load(savefile)
            stdah_default = pickle.load(savefile)
            meanah_marshall = pickle.load(savefile)
            stdah_marshall = pickle.load(savefile)
            meanah_drimmel = pickle.load(savefile)
            stdah_drimmel = pickle.load(savefile)
            meanah_sale = pickle.load(savefile)
            stdah_sale = pickle.load(savefile)
    # Now plot
    bovy_plot.bovy_print(fig_height=3.)
    if _PLOTDIST:
        distmods = 10.**(distmods / 5 - 2.)
        xrange = [0., 12.]
        xlabel = r'$D\,(\mathrm{kpc})$'
    else:
        xrange = [7., 15.8],
        xlabel = r'$\mathrm{distance\ modulus}\ \mu$'
    ylabel = r'$A_H$'
    yrange = [
        0., 1.2 * numpy.amax(
            numpy.vstack(
                (meanah_default + stdah_default,
                 meanah_marshall + stdah_marshall,
                 meanah_drimmel + stdah_drimmel, meanah_sale + stdah_sale)))
    ]
    line_default = bovy_plot.bovy_plot(distmods,
                                       meanah_default,
                                       'b-',
                                       lw=_LW,
                                       zorder=12,
                                       xrange=xrange,
                                       xlabel=xlabel,
                                       yrange=yrange,
                                       ylabel=ylabel)
    pyplot.fill_between(distmods,
                        meanah_default - stdah_default,
                        meanah_default + stdah_default,
                        hatch='/',
                        facecolor=(0, 0, 0, 0),
                        color='b',
                        lw=0.25,
                        zorder=4)
    line_marshall = bovy_plot.bovy_plot(distmods,
                                        meanah_marshall,
                                        'r-',
                                        lw=_LW,
                                        overplot=True,
                                        zorder=8)
    pyplot.fill_between(distmods,
                        meanah_marshall - stdah_marshall,
                        meanah_marshall + stdah_marshall,
                        hatch='\\',
                        facecolor=(0, 0, 0, 0),
                        color='r',
                        lw=0.25,
                        zorder=2)
    line_drimmel = bovy_plot.bovy_plot(distmods,
                                       meanah_drimmel,
                                       '-',
                                       lw=_LW,
                                       color='gold',
                                       overplot=True,
                                       zorder=7)
    pyplot.fill_between(distmods,
                        meanah_drimmel - stdah_drimmel,
                        meanah_drimmel + stdah_drimmel,
                        hatch='///',
                        facecolor=(0, 0, 0, 0),
                        color='gold',
                        lw=0.25,
                        zorder=1)
    line_sale = bovy_plot.bovy_plot(distmods,
                                    meanah_sale,
                                    '-',
                                    lw=_LW,
                                    color='c',
                                    overplot=True,
                                    zorder=9)
    pyplot.fill_between(distmods,
                        meanah_sale - stdah_sale,
                        meanah_sale + stdah_sale,
                        hatch='//',
                        facecolor=(0, 0, 0, 0),
                        color='c',
                        lw=0.25,
                        zorder=3)
    if True:
        data = get_rcsample()
        data = data[data['LOCATION_ID'] == location]
        bovy_plot.bovy_plot(data['RC_DIST'],
                            data['AK_TARG'] * 1.55,
                            'ko',
                            zorder=20,
                            overplot=True,
                            ms=2.)
    if location == 4318:
        pyplot.legend(
            (line_default[0], line_sale[0]),
            (r'$\mathrm{Green\ et\ al.\ (2015)}$',
             r'$\mathrm{Sale\ et\ al.\ (2014)}$'),
            loc='lower right',  #bbox_to_anchor=(.91,.375),
            numpoints=8,
            prop={'size': 14},
            frameon=False)
    elif location == 4242:
        pyplot.legend(
            (line_marshall[0], line_drimmel[0]),
            (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,
                             dpi=300,
                             bbox_extra_artists=pyplot.gca().get_children(),
                             bbox_inches='tight')
    return None
Example #9
0
def generate(locations,
             type='exp',
             sample='lowlow',
             extmap='green15',
             nls=101,
             nmock=1000,
             H0=-1.49,
             _dmapg15=None,
             ncpu=1):
    """
    NAME:
       generate
    PURPOSE:
       generate mock data following a given density
    INPUT:
       locations - locations to be included in the sample
       type= ('exp') type of density profile to sample from
       sample= ('lowlow') for selecting mock parameters
       extmap= ('green15') extinction map to use ('marshall06' and others use Green15 to fill in unobserved regions)
       nls= (101) number of longitude bins to use for each field
       nmock= (1000) number of mock data points to generate
       H0= (-1.49) absolute magnitude (can be array w/ sampling spread)
       ncpu= (1) number of cpus to use to compute the probability
    OUTPUT:
       mockdata recarray with tags 'RC_GALR_H', 'RC_GALPHI_H', 'RC_GALZ_H'
    HISTORY:
       2015-04-03 - Written - Bovy (IAS)
    """
    if isinstance(H0, float): H0 = [H0]
    # Setup the density function and its initial parameters
    rdensfunc = fitDens._setup_densfunc(type)
    mockparams = _setup_mockparams_densfunc(type, sample)
    densfunc = lambda x, y, z: rdensfunc(x, y, z, params=mockparams)
    # Setup the extinction map
    global dmap
    global dmapg15
    if _dmapg15 is None: dmapg15 = mwdust.Green15(filter='2MASS H')
    else: dmapg15 = _dmapg15
    if isinstance(extmap, mwdust.DustMap3D.DustMap3D):
        dmap = extmap
    elif extmap.lower() == 'green15':
        dmap = dmapg15
    elif extmap.lower() == 'marshall06':
        dmap = mwdust.Marshall06(filter='2MASS H')
    elif extmap.lower() == 'sale14':
        dmap = mwdust.Sale14(filter='2MASS H')
    elif extmap.lower() == 'drimmel03':
        dmap = mwdust.Drimmel03(filter='2MASS H')
    # Use brute-force rejection sampling to make no approximations
    # First need to estimate the max probability to use in rejection;
    # Loop through all locations and compute sampling probability on grid in
    # (l,b,D)
    # First restore the APOGEE selection function (assumed pre-computed)
    global apo
    selectFile = '../savs/selfunc-nospdata.sav'
    if os.path.exists(selectFile):
        with open(selectFile, 'rb') as savefile:
            apo = pickle.load(savefile)
    # Now compute the necessary coordinate transformations and evaluate the
    # maximum probability
    distmods = numpy.linspace(7., 15.5, 301)
    ds = 10.**(distmods / 5 - 2.)
    nbs = nls
    lnprobs = numpy.empty((len(locations), len(distmods), nbs, nls))
    radii = []
    lcens, bcens = [], []
    lnprobs = multi.parallel_map(lambda x: _calc_lnprob(
        locations[x], nls, nbs, ds, distmods, H0, densfunc),
                                 range(len(locations)),
                                 numcores=numpy.amin([
                                     len(locations),
                                     multiprocessing.cpu_count(), ncpu
                                 ]))
    lnprobs = numpy.array(lnprobs)
    for ll, loc in enumerate(locations):
        lcen, bcen = apo.glonGlat(loc)
        rad = apo.radius(loc)
        radii.append(rad)  # save for later
        lcens.append(lcen[0])
        bcens.append(bcen[0])
    maxp = (numpy.exp(numpy.nanmax(lnprobs)) -
            10.**-8.) * 1.1  # Just to be sure
    # Now generate mock data using rejection sampling
    nout = 0
    arlocations = numpy.array(locations)
    arradii = numpy.array(radii)
    arlcens = numpy.array(lcens)
    arbcens = numpy.array(bcens)
    out = numpy.recarray((nmock, ),
                         dtype=[('RC_DIST_H', 'f8'), ('RC_DM_H', 'f8'),
                                ('RC_GALR_H', 'f8'), ('RC_GALPHI_H', 'f8'),
                                ('RC_GALZ_H', 'f8')])
    while nout < nmock:
        nnew = 2 * (nmock - nout)
        # nnew new locations
        locIndx = numpy.floor(
            numpy.random.uniform(size=nnew) * len(locations)).astype('int')
        newlocations = arlocations[locIndx]
        # Point within these locations
        newds_coord = numpy.random.uniform(size=nnew)
        newds= 10.**((newds_coord*(numpy.amax(distmods)-numpy.amin(distmods))\
            +numpy.amin(distmods))/5.-2.)
        newdls_coord = numpy.random.uniform(size=nnew)
        newdls= newdls_coord*2.*arradii[locIndx]\
            -arradii[locIndx]
        newdbs_coord = numpy.random.uniform(size=nnew)
        newdbs= newdbs_coord*2.*arradii[locIndx]\
            -arradii[locIndx]
        newr2s = newdls**2. + newdbs**2.
        keepIndx = newr2s < arradii[locIndx]**2.
        newlocations = newlocations[keepIndx]
        newds_coord = newds_coord[keepIndx]
        newdls_coord = newdls_coord[keepIndx]
        newdbs_coord = newdbs_coord[keepIndx]
        newds = newds[keepIndx]
        newdls = newdls[keepIndx]
        newdbs = newdbs[keepIndx]
        newls = newdls + arlcens[locIndx][keepIndx]
        newbs = newdbs + arbcens[locIndx][keepIndx]
        # Reject?
        tps = numpy.zeros_like(newds)
        for nloc in list(set(newlocations)):
            lindx = newlocations == nloc
            pindx = arlocations == nloc
            coord = numpy.array([
                newds_coord[lindx] * (len(distmods) - 1.),
                newdbs_coord[lindx] * (nbs - 1.),
                newdls_coord[lindx] * (nls - 1.)
            ])
            tps[lindx]= \
                numpy.exp(ndimage.interpolation.map_coordinates(\
                    lnprobs[pindx][0],
                    coord,cval=-10.,
                    order=1))-10.**-8.
        XYZ = bovy_coords.lbd_to_XYZ(newls, newbs, newds, degree=True)
        Rphiz = bovy_coords.XYZ_to_galcencyl(XYZ[:, 0],
                                             XYZ[:, 1],
                                             XYZ[:, 2],
                                             Xsun=define_rcsample._R0,
                                             Ysun=0.,
                                             Zsun=define_rcsample._Z0)
        testp = numpy.random.uniform(size=len(newds)) * maxp
        keepIndx = tps > testp
        if numpy.sum(keepIndx) > nmock - nout:
            rangeIndx = numpy.zeros(len(keepIndx), dtype='int')
            rangeIndx[keepIndx] = numpy.arange(numpy.sum(keepIndx))
            keepIndx *= (rangeIndx < nmock - nout)
        out['RC_DIST_H'][nout:nout + numpy.sum(keepIndx)] = newds[keepIndx]
        out['RC_DM_H'][nout:nout+numpy.sum(keepIndx)]= newds_coord[keepIndx]*(numpy.amax(distmods)-numpy.amin(distmods))\
            +numpy.amin(distmods)
        out['RC_GALR_H'][nout:nout + numpy.sum(keepIndx)] = Rphiz[0][keepIndx]
        out['RC_GALPHI_H'][nout:nout +
                           numpy.sum(keepIndx)] = Rphiz[1][keepIndx]
        out['RC_GALZ_H'][nout:nout + numpy.sum(keepIndx)] = Rphiz[2][keepIndx]
        nout = nout + numpy.sum(keepIndx)
    return (out, lnprobs)