def fix_nans(picklename):
    """Marshall has a few NaNs, replace these with Drimmel"""
    with h5py.File(picklename.replace('.sav', '.h5'), 'r') as combdata:
        pix_info = combdata['/pixel_info'][:]
        best_fit = combdata['/best_fit'][:]
    nanIndx = numpy.isnan(best_fit[:, 0])
    print("Found %i NaNs ..." % numpy.sum(nanIndx))
    theta, phi = healpy.pixelfunc.pix2ang(
        pix_info['nside'].astype('int32'),
        pix_info['healpix_index'].astype('int64'),
        nest=True)
    bb = (numpy.pi / 2. - theta) / _DEGTORAD
    ll = phi / _DEGTORAD
    indices = numpy.arange(len(pix_info['nside']))[nanIndx]
    drimmelmap = mwdust.Drimmel03()
    for ii in range(numpy.sum(nanIndx)):
        best_fit[indices[ii]] = drimmelmap(ll[ii], bb[ii], _GREEN15DISTS)
    # Now save
    nanIndx = numpy.isnan(best_fit[:, 0])
    print("Found %i NaNs ..." % numpy.sum(nanIndx))
    # Save to h5 file
    outfile = h5py.File(picklename.replace('.sav', '.h5'), "w")
    outfile.create_dataset("pixel_info", data=pix_info)
    outfile.create_dataset("best_fit", data=best_fit)
    outfile.close()
    return None
示例#2
0
    def __init__(self, with_extinction_maps=False):

        self.redd_maps = {}
        with open(dir_path + 'extinction/extinction_coeffs_2017.dat') as f:
            self.R_V_grid = np.fromstring(f.readline(),
                                          dtype=np.float64,
                                          sep=' ')
            for l in f.readlines():
                spl = l.split(" ", 1)
                self.redd_maps[spl[0]] = np.fromstring(spl[1],
                                                       dtype=np.float64,
                                                       sep=' ')

        self.interp_maps = {
            n: interp1d(self.R_V_grid, self.redd_maps[n])
            for n in self.redd_maps
        }

        self.R_G = np.zeros((75, len(self.R_V_grid)))
        with open(dir_path + 'extinction/extinction_coeffs_G_2017.dat') as f:
            self.logTeffgrid = np.fromstring(f.readline(),
                                             dtype=np.float64,
                                             sep=' ')
            for n, l in enumerate(f.readlines()):
                self.R_G[n] = np.fromstring(l, dtype=np.float64, sep=' ')

        self.interp_G_maps = interp2d(self.R_V_grid, self.logTeffgrid,
                                      self.R_G)

        if (with_extinction_maps):
            self.sfd = SFDQuery()
            self.bayestar = BayestarQuery(max_samples=10)
            self.marshall = MarshallQuery()
            self.drimmel = mwdust.Drimmel03(filter='Landolt V')
示例#3
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
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
示例#5
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
示例#6
0
def test_avebv(ll, bb, dist):
    drim_av = mwdust.Drimmel03(filter='CTIO V')
    drim_ebv = mwdust.Drimmel03()
    assert numpy.fabs(
        drim_av(ll, bb, dist) / drim_ebv(ll, bb, dist) / 0.86 - 3.1) < 0.02
    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
示例#8
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)
示例#9
0
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import mwdust
#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]))