Example #1
0
    def __init__(self):
        """ CLASS CONSTRUCTOR

        Arguments:
            None
        
        Returns:
            Nothing
        """
        # Initialize dust map if desired and share with class
        self.dustmapJ = mwdust.Combined15(filter='2MASS J')
        self.dustmapH = mwdust.Combined15(filter='2MASS H')
        self.dustmapKs = mwdust.Combined15(filter='2MASS Ks')

        return
Example #2
0
def compute_AK_mwdust(ls, bs, dist, edist):
    '''Using the EB-V map from 2014MNRAS.443.2907S and the extinction vector
    RK = 0.31 from Schlafly and Finkbeiner 2011 (ApJ 737, 103)'''
    dustmap = mwdust.Combined15(filter='2MASS Ks')
    dist_kpc, edist_kpc = dist * 1e-3, edist * 1e-3
    AK, eAK = np.zeros(ls.size), np.zeros(ls.size)
    for i in range(ls.size):
        v = dustmap(ls[i], bs[i],
                    np.array([dist_kpc[i], dist_kpc[i] + edist_kpc[i]]))
        AK[i], eAK[i] = v[0], abs(np.diff(v))
    return AK, eAK
Example #3
0
def query_dustmodel_coords_allsky(ra,dec):
    reddenMap = mwdust.Combined15()
    sightLines = SkyCoord(ra*units.deg,dec*units.deg,frame='galactic')
    distanceSamples = np.array([0.06309573,0.07943284,0.1,0.12589255,0.15848933,0.19952627,0.25118864,0.31622776,0.3981072,0.50118726,0.6309574,0.7943282 ,1.,1.2589258,1.5848933,1.9952621,2.511887,3.1622777,3.981073,5.011873,6.3095727,7.943284,10.,12.589258,15.848933,19.952621,25.11887,31.622776,39.81073,50.11873,63.095726])*1000. # In pc, from bayestar2017 map distance samples
    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
def compute_AK_mwdust(ls, bs, dist, edist, eAK_frac=.3):
    '''Using the EB-V map from 2014MNRAS.443.2907S and the extinction vector
    RK = 0.31 from Schlafly and Finkbeiner 2011 (ApJ 737, 103)'''
    dustmap = mwdust.Combined15(filter='2MASS Ks')
    dist_kpc, edist_kpc = np.ascontiguousarray(dist)*1e-3, \
                          np.ascontiguousarray(edist)*1e-3
    ls, bs = np.ascontiguousarray(ls), np.ascontiguousarray(bs)
    AK, eAK = np.zeros(ls.size), np.zeros(ls.size)
    for i in range(ls.size):
        v = dustmap(ls[i], bs[i],
                    np.array([dist_kpc[i], dist_kpc[i] + edist_kpc[i]]))
        AK[i], eAK[i] = v[0], np.sqrt(
            abs(np.diff(v))**2 + (eAK_frac * v[0])**2)
    return unp.uarray(AK, eAK)
Example #5
0
    def run(self):
        self.print_constraints()
        bcmodel = '/Users/petigura/code/isoclassify/direct/bcgrid.h5'
        dustmodel = mwdust.Combined15()

        x = direct.classify_direct.obsdata()
        self.addspec(x)
        self.addjhk(x)
        self.addgriz(x)
        self.addplx(x)
        self.paras = direct.classify_direct.stparas(input=x,
                                                    bcmodel=bcmodel,
                                                    dustmodel=dustmodel,
                                                    useav=0,
                                                    plot=1)
Example #6
0
# from APASS DR9
vmag_s = star['Vmag'][sindx]
bmag_s = star['Bmag'][sindx]
feh_s = star['feh'][sindx]
rv_s = star['rv'][sindx]
rverr_s = star['rv_err'][sindx]
glon_s = glon[sindx]
glat_s = glat[sindx]

# absolute R mag
mvmag_s = np.interp(teff_s, teffmv, mvmag)
# extinction
# using mwdust
# need to do
# > export DUST_DIR=/Users/dkawata/work/pops/mwdust/DUST_DATA
combined = mwdust.Combined15(filter='CTIO V')
avmag = np.zeros_like(glon_s)
mod0_s = vmag_s - mvmag_s + avmag
dist0_s = np.power(10.0, (mod0_s + 5.0) / 5.0) * 0.001
dist_s = np.power(10.0, (mod0_s + 5.0) / 5.0) * 0.001
for i in range(0):
    # distance modulus
    mod_s = vmag_s - mvmag_s - avmag
    dist_s = np.power(10.0, (mod_s + 5.0) / 5.0) * 0.001
    # calculate extinction
    for j in range(len(glon_s)):
        avmag[j] = combined(glon_s[j], glat_s[j], dist_s[j])
    print ' mwdust iteration ', i, ' finished'

# photometry V and V-I
# dwarf http://www.pas.rochester.edu/~emamajek/EEM_dwarf_UBVIJHK_colors_Teff.txt
Example #7
0
#bv_cyl = cut_flow(bv, vol_cut_indx)
gaia_cyl = cut_flow(gaia, vol_cut_indx)

print("Vol-reduced count: ", len(gaia_cyl))
#-------------------------------------------------volumn cut---------------------------------------------------------------

#-------------------------------------------------extinction---------------------------------------------------------------

#bv_corr = np.empty_like(bv_cyl)
jk_corr = np.zeros_like(jk_cyl)
mj_corr = np.zeros_like(mj_cyl)

if (dereddening) and (not load_dereddened_data):
    print("loading dust map...")
    #dust_combine = mwdust.Combined15()  #b-v filter
    dust_combine_J = mwdust.Combined15("2MASS J")
    dust_combine_K = mwdust.Combined15("2MASS Ks")
    print("Preparing dereddening...")
    for i in tqdm.tqdm(range(len(jk_cyl))):
        ldeg = (gaia_cyl['l'])[i]
        bdeg = (gaia_cyl['b'])[i]
        plx = (gaia_cyl['parallax'])[i]
        ej = dust_combine_J(ldeg, bdeg, np.divide(1000., plx))
        ek = dust_combine_K(ldeg, bdeg, np.divide(1000., plx))
        ejk = ej - ek
        #ebv = dust_combine(ldeg,bdeg,np.divide(1000.,plx))
        #bv_corr[i] = bv_cyl[i] - ebv
        jk_corr[i] = jk_cyl[i] - ejk
        mj_corr[i] = mj_cyl[i] - ej
    dereddened_ouput_file = "temp_ext.txt"
    np.savetxt(dereddened_ouput_file,
Example #8
0
def procedure(rank, size):
 
	steps = 2643.0 - 635.0
	chunky = int(steps/size)
        rest = steps - chunky*size
        mini = chunky*rank
        maxi = chunky*(rank + 1)
        if rank >= (size - 1 - rest):
                maxi += 2 + rank - size + rest
		mini += rank - size + 1 + rest
	mini += 635
	maxi += 635
	mini = int(mini)
	maxi = int(maxi)

        if color == "iband":
            filter_ = "SDSS i"
        elif color == "red":
            filter_ = "SDSS r"
        elif color == "green":
            filter_ = "SDSS g"
        else:
            print("Defined filter not available. Choose red, green or iband")
            return

	imap = mwdust.Combined15(filter = filter_, sf10 = True)


	print("Creating Grid...")
	
	foos = np.arange(mini, maxi, 1)
	full = tools.create_grid(foos)
	print("Kernel %s doing %s up to %s" % (rank, mini, maxi))
	print("Calculating borders of the cells")
	minras, maxras, mindecs, maxdecs = tools.get_centers(full[:,0], full[:,1], borders = True, maskdetect = True)

	print("Using %s objects in Kernel %s" % (full.shape[0], rank))


	print("Converting coordinates to galactic system")
	idy = np.where(np.abs(minras + 99.0) < 0.0001))[0]
	idy2 = np.where(np.abs(minras + 999.0) < 0.0001))[0]
	idy = np.append(idy, idy2)
	idy = np.unique(idy)
	for it in idy:
		minras[it] = 0.0
		mindecs[it] = 0.0
		maxras[it] = 0.0
		maxdecs[it] = 0.0
	c_maxs = SkyCoord(ra = maxras*u.degree, dec = maxdecs*u.degree, frame = 'icrs')
	c_mins = SkyCoord(ra = minras*u.degree, dec = mindecs*u.degree, frame = 'icrs')
	c_maxs = c_maxs.galactic
	c_mins = c_mins.galactic
	ramins = c_mins.l.degree
	ramaxs = c_maxs.l.degree
	decmaxs = c_mins.b.degree
	decmins = c_maxs.b.degree
	print("Calculating extinction...")
	dustyness = np.zeros(0)
	for idx in frogress.bar(range(full.shape[0])):
		if idx in idy:
			dustyness = np.append(dustyness, -99.0)
			continue
		dustynesscolor = np.zeros(0)
		for b in np.linspace(decmins[idx], decmaxs[idx], 5):
                	for l in np.linspace(ramins[idx], ramaxs[idx], 5):
				if color == "red":
                                    foo = rmap(l, b, depth)
				elif color == "iband":
                                    foo = imap(l, b, depth)
                                elif color == "green":
				    foo = gmap(l, b, depth)
                        	dustynesscolor = np.append(dustynesscolor, foo)
        	dustynesscolor = np.average(dustynesscolor)
		dustyness = np.append(dustyness, dustynesscolor)
        np.savetxt(str(outdir)+str(color)+"parts/dust_catalog_"+str(color)+"_"+str(rank)+".csv", np.hstack((full, dustyness.reshape(dustyness.size, 1))))
def stparas(input,
            dnumodel=0,
            bcmodel=0,
            dustmodel=0,
            dnucor=0,
            useav=0,
            plot=0):

    # IAU XXIX Resolution, Mamajek et al. (2015)
    r_sun = 6.957e10
    gconst = 6.67408e-8
    gm = 1.3271244e26
    m_sun = gm / gconst
    rho_sun = m_sun / (4. / 3. * np.pi * r_sun**3)
    g_sun = gconst * m_sun / r_sun**2.

    # solar constants
    numaxsun = 3090.
    dnusun = 135.1
    teffsun = 5777.
    Msun = 4.74  # NB this is fixed to MESA BCs!

    # assumed uncertainty in bolometric corrections
    err_bc = 0.02

    # assumed uncertainty in extinction
    err_ext = 0.02

    # load model if they're not passed on
    if (dnumodel == 0):
        dnumodel = asfgrid.Seism()
    if (bcmodel == 0):
        bcmodel = h5py.File('bcgrid.h5', 'r')
    if (dustmodel == 0.):
        dustmodel = mwdust.Combined15()

    # object containing output values
    out = resdata()

    ## extinction coefficients
    extfactors = extinction()

    ########################################################################################
    # case 1: input is parallax + colors
    ########################################################################################
    if ((input.plx > 0.)):

        # only K-band for now
        teffgrid = np.array(bcmodel['teffgrid'])
        avgrid = np.array(bcmodel['avgrid'])
        interp = RegularGridInterpolator((np.array(bcmodel['teffgrid']),\
                np.array(bcmodel['logggrid']),np.array(bcmodel['fehgrid']),\
                np.array(bcmodel['avgrid'])),np.array(bcmodel['bc_k']))

        ### Monte Carlo starts here

        # number of samples
        nsample = 1e5

        # length scale for exp decreasing vol density prior in pc
        L = 1350.

        # get a rough maximum distance
        tempdis = 1. / input.plx
        tempdise = input.plxe / input.plx**2
        maxds = tempdis + 5. * tempdise

        ds = np.arange(1., 10000, 1.)
        lh = np.exp((-1. / (2. * input.plxe**2)) * (input.plx - 1. / ds)**2)
        prior = (ds**2 / (2. * L**3.)) * np.exp(-ds / L)
        dis = lh * prior
        dis2 = dis / np.sum(dis)
        norm = dis2 / np.max(dis2)
        um = np.where((ds > tempdis) & (norm < 0.001))[0]
        if (len(um) > 0):
            maxds = np.min(ds[um])
        else:
            maxds = 10000.

        print 'using max distance:', maxds
        ds = np.linspace(1., maxds, 10000)
        lh = (1./(np.sqrt(2.*np.pi)*input.plxe))*\
             np.exp( (-1./(2.*input.plxe**2))*(input.plx-1./ds)**2)
        prior = (ds**2 / (2. * L**3.)) * np.exp(-ds / L)
        prior = np.zeros(len(lh)) + 1.
        dis = lh * prior
        dis2 = dis / np.sum(dis)

        # sample distances following the discrete distance posterior
        np.random.seed(seed=10)
        dsamp = np.random.choice(ds, p=dis2, size=nsample)

        equ = ephem.Equatorial(input.ra * np.pi / 180.,
                               input.dec * np.pi / 180.,
                               epoch=ephem.J2000)
        gal = ephem.Galactic(equ)
        lon_deg = gal.lon * 180. / np.pi
        lat_deg = gal.lat * 180. / np.pi

        avs = 3.1 * dustmodel(lon_deg, lat_deg, dsamp / 1000.)
        ext = avs * extfactors.ak
        ext = 0.  # already in BC

        if (input.teff == -99.):
            teff = casagrande(jkmag, 0.0)
            teffe = 100.
        else:
            teff = input.teff
            teffe = input.teffe

        np.random.seed(seed=11)
        teffsamp = teff + np.random.randn(nsample) * teffe

        map = input.kmag
        mape = input.kmage
        np.random.seed(seed=12)
        map_samp = map + np.random.randn(nsample) * mape
        absmag = -5. * np.log10(dsamp) - ext + map_samp + 5.

        if (input.teff < np.min(teffgrid)):
            return out
        if (input.teff > np.max(teffgrid)):
            return out
    #if (out.av > np.max(avgrid)):
    #    return out
    #if (out.av < np.min(avgrid)):
    #    return out

        if ((input.teff > -99.) & (input.logg > -99.) & (input.feh > -99.)):
            #bc = interp(np.array([input.teff,input.logg,input.feh,0.]))[0]
            arr = np.zeros((len(avs), 4))
            arr[:, 0] = np.zeros(len(avs)) + input.teff
            arr[:, 1] = np.zeros(len(avs)) + input.logg
            arr[:, 2] = np.zeros(len(avs)) + input.feh
            arr[:, 3] = np.zeros(len(avs)) + avs
            um = np.where(arr[:, 3] < 0.)[0]
            arr[um, 3] = 0.
            #pdb.set_trace()
            bc = interp(arr)
            #pdb.set_trace()

            #pdb.set_trace()

        Mvbol = absmag + bc
        lum = 10**((Mvbol - Msun) / (-2.5))

        t = teffsamp / teffsun
        rad = (lum * t**(-4.))**0.5

        #pdb.set_trace()

        out.teff = input.teff
        out.teffe = input.teffe
        '''
        out.lum=np.median(lum)
        out.lumep=np.percentile(lum,84.1)-out.lum
        out.lumem=out.lum-np.percentile(lum,15.9)

        out.rad=np.median(rad)
        out.radep=np.percentile(rad,84.1)-out.rad
        out.radem=out.rad-np.percentile(rad,15.9)

        out.dis=np.median(dsamp)
        out.disep=np.percentile(dsamp,84.1)-out.dis
        out.disem=out.dis-np.percentile(dsamp,15.9)
        '''

        out.avs = np.median(avs)
        out.avsep = np.percentile(avs, 84.1) - out.avs
        out.avsem = out.avs - np.percentile(avs, 15.9)

        #pdb.set_trace()

        out.rad, out.radep, out.radem, radbn = getstat(rad)
        out.lum, out.lumep, out.lumem, lumbn = getstat(lum)
        out.dis, out.disep, out.disem, disbn = getstat(dsamp)
        #out.avs,out.avsep,out.avsem=getstat(avs)
        #pdb.set_trace()
        out.teff = input.teff
        out.teffep = input.teffe
        out.teffem = input.teffe
        out.logg = input.logg
        out.loggep = input.logge
        out.loggem = input.logge
        out.feh = input.feh
        out.fehep = input.fehe
        out.fehem = input.fehe

        if (plot == 1):
            plt.ion()
            plt.clf()
            plt.subplot(3, 2, 1)
            plt.hist(teffsamp, bins=100)
            plt.title('Teff')

            plt.subplot(3, 2, 2)
            plt.hist(lum, bins=lumbn)
            plt.title('Lum')

            plt.subplot(3, 2, 3)
            plt.hist(rad, bins=radbn)
            plt.title('Rad')

            plt.subplot(3, 2, 4)
            plt.hist(absmag, bins=100)
            plt.title('absmag')

            plt.subplot(3, 2, 5)
            plt.hist(dsamp, bins=disbn)
            plt.title('distance')

            plt.subplot(3, 2, 6)
            plt.hist(avs, bins=100)
            plt.title('Av')

    #pdb.set_trace()

        print '   '
        print 'teff(K):', out.teff, '+/-', out.teffe
        print 'dis(pc):', out.dis, '+', out.disep, '-', out.disem
        print 'av(mag):', out.avs, '+', out.avsep, '-', out.avsem
        print 'rad(rsun):', out.rad, '+', out.radep, '-', out.radem
        print 'lum(lsun):', out.lum, '+', out.lumep, '-', out.lumem

        print '-----'
    #raw_input(':')
    #pdb.set_trace()

    ########################################################################################
    # case 1: input is spectroscopy + seismology
    ########################################################################################
    if ((input.dnu > -99.) & (input.teff > -99.)):

        # seismic logg, density, M and R from scaling relations; this is iterated,
        # since Dnu scaling relation correction depends on M
        dmass = 1.
        fdnu = 1.
        dnuo = input.dnu
        oldmass = 1.0
        nit = 0.

        while (nit < 5):

            numaxn = input.numax / numaxsun
            numaxne = input.numaxe / numaxsun
            dnun = (dnuo / fdnu) / dnusun
            dnune = input.dnue / dnusun
            teffn = input.teff / teffsun
            teffne = input.teffe / teffsun

            out.rad = (numaxn) * (dnun)**(-2.) * np.sqrt(teffn)
            out.rade = np.sqrt( (input.numaxe/input.numax)**2. + \
                4.*(input.dnue/input.dnu)**2. + \
                0.25*(input.teffe/input.teff)**2.)*out.rad

            out.mass = out.rad**3. * (dnun)**2.
            out.masse = np.sqrt( 9.*(out.rade/out.rad)**2. + \
                4.*(input.dnue/input.dnu)**2. )*out.mass

            out.rho = rho_sun * (dnun**2.)
            out.rhoe = np.sqrt(4. * (input.dnue / input.dnu)**2.) * out.rho

            g = g_sun * numaxn * teffn**0.5
            ge = np.sqrt ( (input.numaxe/input.numax)**2. + \
                (0.5*input.teffe/input.teff)**2. ) * g

            out.logg = np.log10(g)
            out.logge = ge / (g * np.log(10.))

            # Dnu scaling relation correction from Sharma et al. 2016
            if (dnucor == 1):
                if (input.clump == 1):
                    evstate = 2
                else:
                    evstate = 1
                #pdb.set_trace()
                dnu, numax, fdnu = dnumodel.get_dnu_numax(evstate,
                                                          input.feh,
                                                          input.teff,
                                                          out.mass,
                                                          out.mass,
                                                          out.logg,
                                                          isfeh=True)
                #print out.mass,fdnu

            dmass = abs((oldmass - out.mass) / out.mass)
            oldmass = out.mass
            nit = nit + 1

        print fdnu

        #pdb.set_trace()
        out.lum = out.rad**2. * teffn**4.
        out.lume = np.sqrt((2. * out.rade / out.rad)**2. +
                           (4. * input.teffe / input.teff)**2.) * out.lum

        print '   '
        print 'teff(K):', input.teff, '+/-', input.teffe
        print 'feh(dex):', input.feh, '+/-', input.fehe
        print 'logg(dex):', out.logg, '+/-', out.logge
        print 'rho(cgs):', out.rho, '+/-', out.rhoe
        print 'rad(rsun):', out.rad, '+/-', out.rade
        print 'mass(msun):', out.mass, '+/-', out.masse
        print 'lum(lsun):', out.lum, '+/-', out.lume
        print '-----'

        out.teff = input.teff
        out.teffep = input.teffe
        out.teffem = input.teffe
        out.feh = input.feh
        out.fehep = input.fehe
        out.fehem = input.fehe
        out.loggep = out.logge
        out.loggem = out.logge
        out.radep = out.rade
        out.radem = out.rade
        out.rhoep = out.rhoe
        out.rhoem = out.rhoe
        out.massep = out.masse
        out.massem = out.masse
        out.lumep = out.lume
        out.lumem = out.lume

        ddis = 1.
        ext = 0.0
        err_ = 0.01
        olddis = 100.0

        # pick an apparent magnitude from input
        map = -99.
        if (input.vmag > -99.):
            map = input.vmag
            mape = input.vmage
            str = 'bc_v'
            avtoext = extfactors.av

        if (input.vtmag > -99.):
            map = input.vtmag
            mape = input.vtmage
            str = 'bc_vt'
            avtoext = extfactors.avt

        if (input.jmag > -99.):
            map = input.jmag
            mape = input.jmage
            str = 'bc_j'
            avtoext = extfactors.aj

        if (input.kmag > -99.):
            map = input.kmag
            mape = input.kmage
            str = 'bc_k'
            avtoext = extfactors.ak

        if (input.gamag > -99.):
            map = input.gamag
            mape = input.gamage
            str = 'bc_ga'
            avtoext = extfactors.aga

        # if apparent mag is given, calculate distance
        if (map > -99.):
            print 'using ' + str
            print 'using coords: ', input.ra, input.dec

            equ = ephem.Equatorial(input.ra * np.pi / 180.,
                                   input.dec * np.pi / 180.,
                                   epoch=ephem.J2000)
            gal = ephem.Galactic(equ)
            lon_deg = gal.lon * 180. / np.pi
            lat_deg = gal.lat * 180. / np.pi

            # iterated since BC depends on extinction
            nit = 0
            while (nit < 5):

                # bolometric correction interpolated from MESA
                interp = RegularGridInterpolator((np.array(bcmodel['teffgrid']),\
                    np.array(bcmodel['logggrid']),np.array(bcmodel['fehgrid']),\
                np.array(bcmodel['avgrid'])),np.array(bcmodel[str]))

                #pdb.set_trace()
                bc = interp(
                    np.array([input.teff, out.logg, input.feh, out.avs]))[0]
                #bc = interp(np.array([input.teff,out.logg,input.feh,0.]))[0]

                Mvbol = -2.5 * (np.log10(out.lum)) + Msun
                Mvbole = np.sqrt(
                    (-2.5 / (out.lum * np.log(10.)))**2 * out.lume**2)

                Mabs = Mvbol - bc
                Mabse = np.sqrt(Mvbole**2 + err_bc**2)

                ext = 0.  # ext already applied in BC
                logplx = (Mabs - 5. - map + ext) / 5.
                logplxe = np.sqrt((Mabse / 5.)**2. + (mape / 5.)**2. +
                                  (err_ext / 5.)**2.)

                out.plx = 10.**logplx
                out.plxe = np.log(10) * 10.**logplx * logplxe

                out.dis = 1. / out.plx
                out.dise = out.plxe / out.plx**2.

                ddis = abs((olddis - out.dis) / out.dis)
                #print olddis,out.dis,ddis,ext
                olddis = out.dis

                out.avs = 3.1 * dustmodel(lon_deg, lat_deg, out.dis / 1000.)[0]

                if (useav != 0.):
                    out.avs = useav

                if (out.avs < 0.):
                    out.avs = 0.0

                #pdb.set_trace()
                ext = out.avs * avtoext
                nit = nit + 1

            #pdb.set_trace()
            print 'Av(mag):', out.avs
            print 'plx(mas):', out.plx * 1e3, '+/-', out.plxe * 1e3
            print 'dis(pc):', out.dis, '+/-', out.dise

            out.disep = out.dise
            out.disem = out.dise

    return out
Example #10
0
def reddening(data,
              file_loc=os.getcwd() + os.sep + 'TESS_telecon3' + os.sep +
              'reddening' + os.sep,
              v=False):
    if v: print data.shape, 'before reddening'

    # check if the reddening file exists
    if (os.path.isfile(file_loc + 'reddening.txt') == True):
        if v: print 'reddening file located.'
        reds = pd.read_csv(file_loc + 'reddening.txt')

        # if it does exist, add extinction (Av) to the dataframe
        if len(reds) == len(data):
            reds = reds.round({'GLon': 4, 'GLat': 4})
            data = data.round({'GLon': 4, 'GLat': 4})
            data = pd.merge(data,
                            reds[list(['GLon', 'GLat', 'E(B-V)', 'Av'])],
                            how='inner')

            # define the extinction coefficient in I band.
            # from (12) eqn 1 and table 3 a(x) + b(x)/Rv value for Imag
            data['Ai'] = data['Av'] * 0.479
            if v: print data.shape, 'after reddening'

        else:
            if v:
                print 'the reddening file is a different length to the data! recalculate reddening.'
            # calculate reddening values using the dataframe inputs
            # mwdust info at (9). E(B-V) TO Av info at (11)
            import mwdust
            combined15 = mwdust.Combined15(filter='2MASS H')
            a = timeit.default_timer()
            if v: print 'calculating extinction coefficients...'
            GLon, GLat, Dist = np.split(
                data[['GLon', 'GLat', 'Dist']].as_matrix(), 3, 1)
            comb15 = np.full(len(GLon), -5.)
            Av = np.full(len(GLon), -5.)

            # calculate extinction using galactic longitude, latitude and distance (in pc)
            for i in range(len(GLon)):
                comb15[i] = combined15(float(GLon[i]), float(GLat[i]),
                                       float(Dist[i]))
                if (i % 1000. == 0): print i

            Av = comb15 * 3.2  # convert from reddening E(B-V) to extinction Av
            reds = np.c_[GLon, GLat, Dist, comb15, Av]
            h = 'GLon,GLat,Dist,E(B-V),Av'
            np.savetxt(file_loc + 'reddening.txt',
                       reds,
                       comments='',
                       header=h,
                       delimiter=',')
            b = timeit.default_timer()
            if v: print b - a, 'seconds'

    else:
        if v: print 'calculating reddening.'
        # calculate reddening values using the dataframe inputs
        # mwdust info at (9). E(B-V) TO Av info at (11)
        import mwdust
        combined15 = mwdust.Combined15(filter='2MASS H')
        a = timeit.default_timer()
        if v: print 'calculating extinction coefficients...'
        GLon, GLat, Dist = np.split(data[['GLon', 'GLat', 'Dist']].as_matrix(),
                                    3, 1)
        comb15 = np.full(len(GLon), -5.)
        Av = np.full(len(GLon), -5.)

        # calculate extinction using galactic longitude, latitude and distance (in pc)
        for i in range(len(GLon)):
            comb15[i] = combined15(float(GLon[i]), float(GLat[i]),
                                   float(Dist[i]))
            if (i % 1000. == 0):
                print i  # only print the code's progress every 100 stars

        Av = comb15 * 3.2  # convert from reddening E(B-V) to extinction Av
        reds = np.c_[GLon, GLat, Dist, comb15, Av]
        h = 'GLon,GLat,Dist,E(B-V),Av'
        np.savetxt(file_loc + 'reddening.txt',
                   reds,
                   comments='',
                   header=h,
                   delimiter=',')
        b = timeit.default_timer()
        if v: print b - a, 'seconds'
        sys.exit()

    return data