Exemple #1
0
def calcMass(options,args):
    if options.sample.lower() == 'g':
        if options.select.lower() == 'program':
            raw= read_gdwarfs(_GDWARFFILE,logg=True,ebv=True,sn=True)
        elif options.select.lower() == 'fakebimodal':
            raw= read_gdwarfs(_FAKEBIMODALGDWARFFILE,
                              logg=True,ebv=True,sn=True)
            options.select= 'all'
        else:
            raw= read_gdwarfs(logg=True,ebv=True,sn=True)
    elif options.sample.lower() == 'k':
        if options.select.lower() == 'program':
            raw= read_kdwarfs(_KDWARFFILE,logg=True,ebv=True,sn=True)
        else:
            raw= read_kdwarfs(logg=True,ebv=True,sn=True)
    #Bin the data
    binned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe)
    #Savefile
    if os.path.exists(args[0]):#Load savefile
        savefile= open(args[0],'rb')
        mass= pickle.load(savefile)
        ii= pickle.load(savefile)
        jj= pickle.load(savefile)
        savefile.close()
    else:
        mass= []
        ii, jj= 0, 0
    #parameters
    if os.path.exists(args[1]):#Load initial
        savefile= open(args[1],'rb')
        fits= pickle.load(savefile)
        savefile.close()
    else:
        print "Error: must provide parameters of best fits"
        print "Returning ..."
        return None
    #Sample?
    if options.mcsample:
        if ii < len(binned.fehedges)-1 and jj < len(binned.afeedges)-1:
            print "First do all of the best-fit mass estimates ..."
            print "Returning ..."
            return None
        if os.path.exists(args[2]): #Load savefile
            savefile= open(args[2],'rb')
            masssamples= pickle.load(savefile)
            ii= pickle.load(savefile)
            jj= pickle.load(savefile)
            savefile.close()
        else:
            masssamples= []
            ii, jj= 0, 0
        if os.path.exists(args[3]): #Load savefile
            savefile= open(args[3],'rb')
            denssamples= pickle.load(savefile)
            savefile.close()
        else:
            print "If mcsample you need to provide the file with the density samples ..."
            print "Returning ..."
            return None
    #Set up model etc.
    if options.model.lower() == 'hwr':
        densfunc= _HWRDensity
    elif options.model.lower() == 'twodblexp':
        densfunc= _TwoDblExpDensity
    like_func= _HWRLikeMinus
    pdf_func= _HWRLike
    if options.sample.lower() == 'g':
        colorrange=[0.48,0.55]
    elif options.sample.lower() == 'k':
        colorrange=[0.55,0.75]
    #Load selection function
    plates= numpy.array(list(set(list(raw.plate))),dtype='int') #Only load plates that we use
    print "Using %i plates, %i stars ..." %(len(plates),len(raw))
    sf= segueSelect(plates=plates,type_faint='tanhrcut',
                    sample=options.sample,type_bright='tanhrcut',
                    sn=True,select=options.select)
    platelb= bovy_coords.radec_to_lb(sf.platestr.ra,sf.platestr.dec,
                                     degree=True)
    indx= [not 'faint' in name for name in sf.platestr.programname]
    platebright= numpy.array(indx,dtype='bool')
    indx= ['faint' in name for name in sf.platestr.programname]
    platefaint= numpy.array(indx,dtype='bool')
    if options.sample.lower() == 'g':
        grmin, grmax= 0.48, 0.55
        rmin,rmax= 14.5, 20.2
    #Run through the bins
    while ii < len(binned.fehedges)-1:
        while jj < len(binned.afeedges)-1:
            data= binned(binned.feh(ii),binned.afe(jj))
            if len(data) < options.minndata:
                if options.mcsample: masssamples.append(None)
                else: mass.append(None)
                jj+= 1
                if jj == len(binned.afeedges)-1: 
                    jj= 0
                    ii+= 1
                    break
                continue               
            print binned.feh(ii), binned.afe(jj), len(data)
            fehindx= binned.fehindx(binned.feh(ii))
            afeindx= binned.afeindx(binned.afe(jj))
            #set up feh and color
            feh= binned.feh(ii)
            fehrange= [binned.fehedges[ii],binned.fehedges[ii+1]]
            #FeH
            fehdist= DistSpline(*numpy.histogram(data.feh,bins=5,
                                                 range=fehrange),
                                 xrange=fehrange,dontcuttorange=False)
            #Color
            colordist= DistSpline(*numpy.histogram(data.dered_g\
                                                       -data.dered_r,
                                                   bins=9,range=colorrange),
                                   xrange=colorrange)
            
            #Age marginalization
            afe= binned.afe(jj)
            if options.simpleage:
                agemin, agemax= 0.5, 10.
            else:
                if afe > 0.25: agemin, agemax= 7.,10.
                else: agemin,agemax= 1.,8.
            if options.mcsample:
                #Loop over samples
                thissamples= denssamples[afeindx+fehindx*binned.npixafe()]
                if options.nsamples < len(thissamples):
                    print "Taking random ..."
                    #Random permutation
                    thissamples= numpy.random.permutation(thissamples)[0:options.nsamples]
                thismasssamples= []
                print "WARNING: DISK MASS IN CALCMASS ONLY FOR G COLORS"
                for kk in range(len(thissamples)):
                    thisparams= thissamples[kk]
                    thismasssamples.append(predictDiskMass(densfunc,
                                                           thisparams,sf,
                                                           colordist,fehdist,
                                                           fehrange[0],
                                                           fehrange[1],feh,
                                                           data,0.45,
                                                           0.58,
                                                           agemin,agemax,
                                                           normalize=options.normalize,
                                                           imfmodel=options.imfmodel))
                #Print some stuff
                print numpy.mean(numpy.array(thismasssamples)), numpy.std(numpy.array(thismasssamples))
                masssamples.append(thismasssamples)
            else:
                thisparams= fits[afeindx+fehindx*binned.npixafe()]
                print "WARNING: DISK MASS IN CALCMASS ONLY FOR G COLORS"
                mass.append(predictDiskMass(densfunc,
                                            thisparams,sf,
                                            colordist,fehdist,
                                            fehrange[0],
                                            fehrange[1],feh,
                                            data,0.45,
                                            0.58,
                                            agemin,agemax,
                                            normalize=options.normalize,
                                            imfmodel=options.imfmodel))
                print mass[-1]
            jj+= 1
            if jj == len(binned.afeedges)-1: 
                jj= 0
                ii+= 1
            if options.mcsample: save_pickles(args[2],masssamples,ii,jj)
            else: save_pickles(args[0],mass,ii,jj)
            if jj == 0: #this means we've reset the counter 
                break
    if options.mcsample: save_pickles(args[2],masssamples,ii,jj)
    else: save_pickles(args[0],mass,ii,jj)
    return None
Exemple #2
0
def plotPixelFitVel(options,args):
    if options.sample.lower() == 'g':
        if options.select.lower() == 'program':
            raw= read_gdwarfs(_GDWARFFILE,logg=True,ebv=True,sn=options.snmin,
                              distfac=options.distfac)
        else:
            raw= read_gdwarfs(logg=True,ebv=True,sn=options.snmin,
                              distfac=options.distfac)
    elif options.sample.lower() == 'k':
        if options.select.lower() == 'program':
            raw= read_kdwarfs(_KDWARFFILE,logg=True,ebv=True,sn=options.snmin,
                              distfac=options.distfac)
        else:
            raw= read_kdwarfs(logg=True,ebv=True,sn=options.snmin,
                              distfac=options.distfac)
    if not options.bmin is None:
        #Cut on |b|
        raw= raw[(numpy.fabs(raw.b) > options.bmin)]
    #print len(raw)
    #Bin the data   
    binned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe)
    if options.tighten:
        tightbinned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe,
                                 fehmin=-1.6,fehmax=0.5,afemin=-0.05,
                                 afemax=0.55)
    else:
        tightbinned= binned
    #Savefile
    if os.path.exists(args[0]):#Load savefile
        savefile= open(args[0],'rb')
        fits= pickle.load(savefile)
        savefile.close()
    #Now plot
    #Run through the pixels and gather
    if options.type.lower() == 'afe' or options.type.lower() == 'feh' \
            or options.type.lower() == 'fehafe' \
            or options.type.lower() == 'afefeh':
        plotthis= []
    else:
        plotthis= numpy.zeros((tightbinned.npixfeh(),tightbinned.npixafe()))
    #ndata= 0
    #maxndata= 0
    for ii in range(tightbinned.npixfeh()):
        for jj in range(tightbinned.npixafe()):
            data= binned(tightbinned.feh(ii),tightbinned.afe(jj))
            fehindx= binned.fehindx(tightbinned.feh(ii))#Map onto regular binning
            afeindx= binned.afeindx(tightbinned.afe(jj))
            if afeindx+fehindx*binned.npixafe() >= len(fits):
                if options.type.lower() == 'afe' or options.type.lower() == 'feh' or options.type.lower() == 'fehafe' \
                        or options.type.lower() == 'afefeh':
                    continue
                else:
                    plotthis[ii,jj]= numpy.nan
                    continue
            thisfit= fits[afeindx+fehindx*binned.npixafe()]
            if thisfit is None:
                if options.type.lower() == 'afe' or options.type.lower() == 'feh' or options.type.lower() == 'fehafe' \
                        or options.type.lower() == 'afefeh':
                    continue
                else:
                    plotthis[ii,jj]= numpy.nan
                    continue
            if len(data) < options.minndata:
                if options.type.lower() == 'afe' or options.type.lower() == 'feh' or options.type.lower() == 'fehafe' \
                        or options.type.lower() == 'afefeh':
                    continue
                else:
                    plotthis[ii,jj]= numpy.nan
                    continue
            #if len(data) > maxndata: maxndata= len(data)
            #ndata+= len(data)
            if options.model.lower() == 'hwr':
                if options.type == 'sz':
                    plotthis[ii,jj]= numpy.exp(thisfit[1])
                elif options.type == 'sz2':
                    plotthis[ii,jj]= numpy.exp(2.*thisfit[1])
                elif options.type == 'hs':
                    plotthis[ii,jj]= numpy.exp(thisfit[4])
                elif options.type == 'hsm':
                    plotthis[ii,jj]= numpy.exp(-thisfit[4])
                elif options.type == 'pbad':
                    plotthis[ii,jj]= thisfit[0]
                elif options.type == 'slopes':
                    plotthis[ii,jj]= thisfit[2]
                elif options.type == 'slope':
                    plotthis[ii,jj]= thisfit[2]
                elif options.type == 'chi2dof':
                    plotthis[ii,jj]= thisfit[6]
                elif options.type.lower() == 'afe' \
                        or options.type.lower() == 'feh' \
                        or options.type.lower() == 'fehafe' \
                        or options.type.lower() == 'afefeh':
                    plotthis.append([tightbinned.feh(ii),
                                     tightbinned.afe(jj),
                                     numpy.exp(thisfit[1]),
                                     numpy.exp(thisfit[3]),
                                     len(data)])
    #print ndata
    #print maxndata
    #Set up plot
    #print numpy.nanmin(plotthis), numpy.nanmax(plotthis)
    if options.type == 'sz':
        if options.vr:
            vmin, vmax= 40.,80.
            zlabel= r'$\sigma_R(z = \langle z \rangle)\ [\mathrm{km\ s}^{-1}]$'
        else:
            vmin, vmax= 10.,60.
            zlabel= r'$\sigma_z(z_{1/2})\ [\mathrm{km\ s}^{-1}]$'
    elif options.type == 'sz2':
        if options.vr:
            vmin, vmax= 40.**2.,80.**2.
            zlabel= r'$\sigma_R^2(z= \langle z \rangle)\ [\mathrm{km\ s}^{-1}]$'
        else:
            vmin, vmax= 15.**2.,50.**2.
            zlabel= r'$\sigma_z^2(z= \langle z \rangle)\ [\mathrm{km\ s}^{-1}]$'
    elif options.type == 'hs':
        if options.vr:
            vmin, vmax= 3.,25.
            zlabel= r'$R_\sigma\ [\mathrm{kpc}]$'
        else:
            vmin, vmax= 3.,15.
            zlabel= r'$h_\sigma\ [\mathrm{kpc}]$'
    elif options.type == 'hsm':
        if options.vr:
            vmin, vmax= 0.,0.3
            zlabel= r'$R^{-1}_\sigma\ [\mathrm{kpc}^{-1}]$'
        else:
            vmin, vmax= 0.,0.3
            zlabel= r'$R^{-1}_\sigma\ [\mathrm{kpc}^{-1}]$'
    elif options.type == 'slope':
        vmin, vmax= -5.,5.
        zlabel= r'$\frac{\mathrm{d} \sigma_z}{\mathrm{d} z}(z_{1/2})\ [\mathrm{km\ s}^{-1}\ \mathrm{kpc}^{-1}]$'
    elif options.type == 'pbad':
        vmin, vmax= 0.,0.1
        zlabel= r'$P_{\mathrm{bad}}$'
    elif options.type == 'chi2dof':
        vmin, vmax= 0.5,1.5
        zlabel= r'$\chi^2/\mathrm{dof}$'
    elif options.type == 'afe':
        vmin, vmax= 0.05,.4
        zlabel=r'$[\alpha/\mathrm{Fe}]$'
    elif options.type == 'feh':
        vmin, vmax= -1.5,0.
        zlabel=r'$[\mathrm{Fe/H}]$'
    elif options.type == 'fehafe':
        vmin, vmax= -.7,.7
        zlabel=r'$[\mathrm{Fe/H}]-[\mathrm{Fe/H}]_{1/2}([\alpha/\mathrm{Fe}])$'
    elif options.type == 'afefeh':
        vmin, vmax= -.15,.15
        zlabel=r'$[\alpha/\mathrm{Fe}]-[\alpha/\mathrm{Fe}]_{1/2}([\mathrm{Fe/H}])$'
    if options.tighten:
        xrange=[-1.6,0.5]
        yrange=[-0.05,0.55]
    else:
        xrange=[-2.,0.6]
        yrange=[-0.1,0.6]
    if options.type.lower() == 'afe' or options.type.lower() == 'feh' \
            or options.type.lower() == 'fehafe' \
            or options.type.lower() == 'afefeh':
        print "Update!! Never used until now (afe etc. type fitting is in plotsz2hz"
        return None
        bovy_plot.bovy_print(fig_height=3.87,fig_width=5.)
        #Gather hR and hz
        hz, hr,afe, feh, ndata= [], [], [], [], []
        for ii in range(len(plotthis)):
            hz.append(plotthis[ii][2])
            hr.append(plotthis[ii][3])
            afe.append(plotthis[ii][1])
            feh.append(plotthis[ii][0])
            ndata.append(plotthis[ii][4])
        hz= numpy.array(hz)
        hr= numpy.array(hr)
        afe= numpy.array(afe)
        feh= numpy.array(feh)
        ndata= numpy.array(ndata)
        #Process ndata
        ndata= ndata**.5
        ndata= ndata/numpy.median(ndata)*35.
        #ndata= numpy.log(ndata)/numpy.log(numpy.median(ndata))
        #ndata= (ndata-numpy.amin(ndata))/(numpy.amax(ndata)-numpy.amin(ndata))*25+12.
        if options.type.lower() == 'afe':
            plotc= afe
        elif options.type.lower() == 'feh':
            plotc= feh
        elif options.type.lower() == 'afefeh':
            #Go through the bins to determine whether feh is high or low for this alpha
            plotc= numpy.zeros(len(afe))
            for ii in range(tightbinned.npixfeh()):
                fehbin= ii
                data= tightbinned.data[(tightbinned.data.feh > tightbinned.fehedges[fehbin])\
                                           *(tightbinned.data.feh <= tightbinned.fehedges[fehbin+1])]
                medianafe= numpy.median(data.afe)
                for jj in range(len(afe)):
                    if feh[jj] == tightbinned.feh(ii):
                        plotc[jj]= afe[jj]-medianafe
        else:
            #Go through the bins to determine whether feh is high or low for this alpha
            plotc= numpy.zeros(len(feh))
            for ii in range(tightbinned.npixafe()):
                afebin= ii
                data= tightbinned.data[(tightbinned.data.afe > tightbinned.afeedges[afebin])\
                                           *(tightbinned.data.afe <= tightbinned.afeedges[afebin+1])]
                medianfeh= numpy.median(data.feh)
                for jj in range(len(feh)):
                    if afe[jj] == tightbinned.afe(ii):
                        plotc[jj]= feh[jj]-medianfeh
        yrange= [150,1200]
        xrange= [1.2,5.]
        bovy_plot.bovy_plot(hr,hz,s=ndata,c=plotc,
                            cmap='jet',
                            ylabel=r'$\mathrm{vertical\ scale\ height\ [pc]}$',
                            xlabel=r'$\mathrm{radial\ scale\ length\ [kpc]}$',
                            clabel=zlabel,
                            xrange=xrange,yrange=yrange,
                            vmin=vmin,vmax=vmax,
                            scatter=True,edgecolors='none',
                            colorbar=True)
    elif options.type.lower() == 'slopes':
        bovy_plot.bovy_print()
        bovy_plot.bovy_hist(plotthis.flatten(),
                            range=[-5.,5.],
                            bins=11,
                            histtype='step',
                            color='k',
                            xlabel=r'$\sigma_z(z)\ \mathrm{slope\ [km\ s}^{-1}\ \mathrm{kpc}^{-1}]$')
    else:
        bovy_plot.bovy_print()
        bovy_plot.bovy_dens2d(plotthis.T,origin='lower',cmap='jet',
                              interpolation='nearest',
                              xlabel=r'$[\mathrm{Fe/H}]$',
                              ylabel=r'$[\alpha/\mathrm{Fe}]$',
                              zlabel=zlabel,
                              xrange=xrange,yrange=yrange,
                              vmin=vmin,vmax=vmax,
                              contours=False,
                              colorbar=True,shrink=0.78)
    if options.observed:
        bovy_plot.bovy_text(r'$\mathrm{observed}$',
                            top_right=True,size=18.)
    bovy_plot.bovy_end_print(options.plotfile)
    return None
Exemple #3
0
def plotMass(options,args):
    if options.sample.lower() == 'g':
        if options.select.lower() == 'program':
            raw= read_gdwarfs(_GDWARFFILE,logg=True,ebv=True,sn=True)
        else:
            raw= read_gdwarfs(logg=True,ebv=True,sn=True)
    elif options.sample.lower() == 'k':
        if options.select.lower() == 'program':
            raw= read_kdwarfs(_KDWARFFILE,logg=True,ebv=True,sn=True)
        else:
            raw= read_kdwarfs(logg=True,ebv=True,sn=True)
    #Bin the data   
    binned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe)
    if options.tighten:
        tightbinned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe,
                                 fehmin=-1.6,fehmax=0.5,afemin=-0.05,
                                 afemax=0.55)
    else:
        tightbinned= binned
    #Savefile
    if os.path.exists(args[0]):#Load savefile
        savefile= open(args[0],'rb')
        mass= pickle.load(savefile)
        ii= pickle.load(savefile)
        jj= pickle.load(savefile)
        savefile.close()
    else:
        mass= []
        ii, jj= 0, 0
    #parameters
    if os.path.exists(args[1]):#Load initial
        savefile= open(args[1],'rb')
        fits= pickle.load(savefile)
        savefile.close()
    else:
        print "Error: must provide parameters of best fits"
        print "Returning ..."
        return None
    #Mass uncertainties are in savefile3
    if len(args) > 2 and os.path.exists(args[2]):
        savefile= open(args[2],'rb')
        masssamples= pickle.load(savefile)
        savefile.close()
        masserrors= True
    else:
        masssamples= None
        masserrors= False
    if len(args) > 3 and os.path.exists(args[3]): #Load savefile
        savefile= open(args[3],'rb')
        denssamples= pickle.load(savefile)
        savefile.close()
        denserrors= True
    else:
        denssamples= None
        denserrors= False
    if len(args) > 4 and os.path.exists(args[4]):#Load savefile
        savefile= open(args[4],'rb')
        velfits= pickle.load(savefile)
        savefile.close()
        velfitsLoaded= True
    else:
        velfitsLoaded= False
    if len(args) > 5 and os.path.exists(args[5]):
        savefile= open(args[5],'rb')
        velsamples= pickle.load(savefile)
        savefile.close()
        velerrors= True
    else:
        velsamples= None            
        velerrors= False
    #Now plot
    #Run through the pixels and gather
    if options.type.lower() == 'afe' or options.type.lower() == 'feh' \
            or options.type.lower() == 'fehafe' \
            or options.type.lower() == 'afefeh':
        plotthis= []
    else:
        plotthis= numpy.zeros((tightbinned.npixfeh(),tightbinned.npixafe()))
    if denserrors: errors= []
    for ii in range(tightbinned.npixfeh()):
        for jj in range(tightbinned.npixafe()):
            data= binned(tightbinned.feh(ii),tightbinned.afe(jj))
            fehindx= binned.fehindx(tightbinned.feh(ii))#Map onto regular binning
            afeindx= binned.afeindx(tightbinned.afe(jj))
            if afeindx+fehindx*binned.npixafe() >= len(fits):
                if options.type.lower() == 'afe' or options.type.lower() == 'feh' or options.type.lower() == 'fehafe' \
                        or options.type.lower() == 'afefeh':
                    continue
                else:
                    plotthis[ii,jj]= numpy.nan
                    continue
            thismass= mass[afeindx+fehindx*binned.npixafe()]
            if masserrors:
                thismasssamples= masssamples[afeindx+fehindx*binned.npixafe()]
            else:
                thismasssamples= None
            thisfit= fits[afeindx+fehindx*binned.npixafe()]
            if denserrors:
                thisdenssamples= denssamples[afeindx+fehindx*binned.npixafe()]
            else:
                thisdenssamples= None
            if velfitsLoaded:
                thisvelfit= velfits[afeindx+fehindx*binned.npixafe()]
            if velerrors:
                thisvelsamples= velsamples[afeindx+fehindx*binned.npixafe()]
            if thisfit is None:
                if options.type.lower() == 'afe' or options.type.lower() == 'feh' or options.type.lower() == 'fehafe' \
                        or options.type.lower() == 'afefeh':
                    continue
                else:
                    plotthis[ii,jj]= numpy.nan
                    continue
            if len(data) < options.minndata:
                if options.type.lower() == 'afe' or options.type.lower() == 'feh' or options.type.lower() == 'fehafe' \
                        or options.type.lower() == 'afefeh':
                    continue
                else:
                    plotthis[ii,jj]= numpy.nan
                    continue
            if options.type == 'mass':
                if not options.distzmin is None or not options.distzmax is None:
                    if options.distzmin is None and not options.distzmax is None:
                        thismass*= (1.-numpy.exp(-options.distzmax/numpy.exp(thisfit[0])/1000.))
                    elif not options.distzmin is None and options.distzmax is None:
                        thismass*= numpy.exp(-options.distzmin/numpy.exp(thisfit[0])/1000.)
                    else:
                        thismass*= (numpy.exp(-options.distzmin/numpy.exp(thisfit[0])/1000.)-numpy.exp(-options.distzmax/numpy.exp(thisfit[0])/1000.))
                if options.logmass:
                    plotthis[ii,jj]= numpy.log10(thismass/10**6.)
                else:
                    plotthis[ii,jj]= thismass/10**6.
            elif options.type == 'nstars':
                if options.logmass:
                    plotthis[ii,jj]= numpy.log10(len(data))
                else:
                    plotthis[ii,jj]= len(data)
            elif options.model.lower() == 'hwr':
                if options.type == 'hz':
                    plotthis[ii,jj]= numpy.exp(thisfit[0])*1000.
                elif options.type == 'hr':
                    plotthis[ii,jj]= numpy.exp(thisfit[1])
                elif options.type.lower() == 'afe' \
                        or options.type.lower() == 'feh' \
                        or options.type.lower() == 'fehafe' \
                        or options.type.lower() == 'afefeh':
                    plotthis.append([tightbinned.feh(ii),
                                         tightbinned.afe(jj),
                                     numpy.exp(thisfit[0])*1000.,
                                     numpy.exp(thisfit[1]),
                                     len(data),
                                     thismass/10.**6.,
                                     thismasssamples])
                    if denserrors:
                        theseerrors= []
                        thesesamples= denssamples[afeindx+fehindx*binned.npixafe()]
                        if options.model.lower() == 'hwr':
                            for kk in [0,1]:
                                xs= numpy.array([s[kk] for s in thesesamples])
                                theseerrors.append(0.5*(-numpy.exp(numpy.mean(xs)-numpy.std(xs))+numpy.exp(numpy.mean(xs)+numpy.std(xs))))
                        errors.append(theseerrors)
                    if velfitsLoaded:
                        if options.velmodel.lower() == 'hwr':
                            plotthis[-1].extend([numpy.exp(thisvelfit[1]),
                                                 numpy.exp(thisvelfit[4]),
                                                 thisvelfit[2],
                                                 thisvelfit[3]])
                            if velerrors:
                                theseerrors= []
                                thesesamples= velsamples[afeindx+fehindx*binned.npixafe()]
                                for kk in [1,4]:
                                    xs= numpy.array([s[kk] for s in thesesamples])
                                    theseerrors.append(0.5*(numpy.exp(numpy.mean(xs))-numpy.exp(numpy.mean(xs)-numpy.std(xs))-numpy.exp(numpy.mean(xs))+numpy.exp(numpy.mean(xs)+numpy.std(xs))))
                                xs= numpy.array([s[4] for s in thesesamples])
                                theseerrors.append(0.5*(numpy.exp(-numpy.mean(xs))-numpy.exp(numpy.mean(-xs)-numpy.std(-xs))-numpy.exp(numpy.mean(-xs))+numpy.exp(numpy.mean(-xs)+numpy.std(-xs))))
                                errors[-1].extend(theseerrors)
    #Set up plot
    #print numpy.nanmin(plotthis), numpy.nanmax(plotthis)
    if options.type == 'mass':
        if options.logmass:
            vmin, vmax= numpy.log10(0.01), numpy.log10(2.)
            zlabel=r'$\log_{10} \Sigma(R_0)\ [M_{\odot}\ \mathrm{pc}^{-2}]$'
        else:
            vmin, vmax= 0.,1.
            zlabel=r'$\Sigma(R_0)\ [M_{\odot}\ \mathrm{pc}^{-2}]$'
            if not options.distzmin is None or not options.distzmax is None:
                vmin, vmax= None, None
        title=r'$\mathrm{mass\ weighted}$'
    elif options.type == 'nstars':
        if options.logmass:
            vmin, vmax= 2., 3.
            zlabel=r'$\log_{10} \mathrm{raw\ number\ of\ G}$-$\mathrm{type\ dwarfs}$'
        else:
            vmin, vmax= 100.,1000.
            zlabel=r'$\mathrm{raw\ number\ of\ G}$-$\mathrm{type\ dwarfs}$'
        title= r'$\mathrm{raw\ sample\ counts}$'
    elif options.type == 'afe':
        vmin, vmax= 0.0,.5
        zlabel=r'$[\alpha/\mathrm{Fe}]$'
    elif options.type == 'feh':
        vmin, vmax= -1.5,0.
        zlabel=r'$[\mathrm{Fe/H}]$'
    elif options.type == 'fehafe':
        vmin, vmax= -.7,.7
        zlabel=r'$[\mathrm{Fe/H}]-[\mathrm{Fe/H}]_{1/2}([\alpha/\mathrm{Fe}])$'
    elif options.type == 'afefeh':
        vmin, vmax= -.15,.15
        zlabel=r'$[\alpha/\mathrm{Fe}]-[\alpha/\mathrm{Fe}]_{1/2}([\mathrm{Fe/H}])$'
    if options.tighten:
        xrange=[-1.6,0.5]
        yrange=[-0.05,0.55]
    else:
        xrange=[-2.,0.6]
        yrange=[-0.1,0.6]
    if options.type.lower() == 'afe' or options.type.lower() == 'feh' \
            or options.type.lower() == 'fehafe' \
            or options.type.lower() == 'afefeh':
        bovy_plot.bovy_print(fig_height=3.87,fig_width=5.)
        #Gather hR and hz
        sz_err, sz, hz_err, hr_err, mass_err, mass, hz, hr,afe, feh, ndata= [], [], [], [], [], [], [], [], [], [], []
        for ii in range(len(plotthis)):
            if denserrors:
                hz_err.append(errors[ii][0]*1000.)
                hr_err.append(errors[ii][1])
            mass.append(plotthis[ii][5])
            hz.append(plotthis[ii][2])
            hr.append(plotthis[ii][3])
            afe.append(plotthis[ii][1])
            feh.append(plotthis[ii][0])
            ndata.append(plotthis[ii][4])
            if velfitsLoaded:
                sz.append(plotthis[ii][7])
            if velerrors:
                sz_err.append(errors[ii][2])
            if masserrors:
                mass_err.append(numpy.std(numpy.array(plotthis[ii][6])/10.**6.))
                
                """
                if options.logmass:
                    mass_err.append(numpy.std(numpy.log10(numpy.array(plotthis[ii][6])/10.**6.)))
                else:
                    mass_err.append(numpy.std(numpy.array(plotthis[ii][6])/10.**6.))
                """
        if denserrors:
            hz_err= numpy.array(hz_err)
            hr_err= numpy.array(hr_err)
        if velfitsLoaded:
            sz= numpy.array(sz)
        if velerrors:
            sz_err= numpy.array(sz_err)
        mass= numpy.array(mass)
        if masserrors:
            mass_err= numpy.array(mass_err)
        hz= numpy.array(hz)
        hr= numpy.array(hr)
        afe= numpy.array(afe)
        feh= numpy.array(feh)
        ndata= numpy.array(ndata)
        #Process ndata
        ndata= ndata**.5
        ndata= ndata/numpy.median(ndata)*35.
        #ndata= numpy.log(ndata)/numpy.log(numpy.median(ndata))
        #ndata= (ndata-numpy.amin(ndata))/(numpy.amax(ndata)-numpy.amin(ndata))*25+12.
        if options.type.lower() == 'afe':
            plotc= afe
        elif options.type.lower() == 'feh':
            plotc= feh
        elif options.type.lower() == 'afefeh':
            #Go through the bins to determine whether feh is high or low for this alpha
            plotc= numpy.zeros(len(afe))
            for ii in range(tightbinned.npixfeh()):
                fehbin= ii
                data= tightbinned.data[(tightbinned.data.feh > tightbinned.fehedges[fehbin])\
                                           *(tightbinned.data.feh <= tightbinned.fehedges[fehbin+1])]
                medianafe= numpy.median(data.afe)
                for jj in range(len(afe)):
                    if feh[jj] == tightbinned.feh(ii):
                        plotc[jj]= afe[jj]-medianafe
        else:
            #Go through the bins to determine whether feh is high or low for this alpha
            plotc= numpy.zeros(len(feh))
            for ii in range(tightbinned.npixafe()):
                afebin= ii
                data= tightbinned.data[(tightbinned.data.afe > tightbinned.afeedges[afebin])\
                                           *(tightbinned.data.afe <= tightbinned.afeedges[afebin+1])]
                medianfeh= numpy.median(data.feh)
                for jj in range(len(feh)):
                    if afe[jj] == tightbinned.afe(ii):
                        plotc[jj]= feh[jj]-medianfeh
        xrange= [150,1200]
        if options.cumul:
            #Print total surface mass and uncertainty
            totmass= numpy.sum(mass)
            if options.ploterrors:
                toterr= numpy.sqrt(numpy.sum(mass_err**2.))
            else:
                toterr= 0.
            print "Total surface-mass density: %4.1f +/- %4.2f" %(totmass,toterr)
            ids= numpy.argsort(hz)
            plotc= plotc[ids]
            ndata= ndata[ids]
            mass= mass[ids]
            mass= numpy.cumsum(mass)
            hz.sort()
            ylabel=r'$\mathrm{cumulative}\ \Sigma(R_0)\ [M_{\odot}\ \mathrm{pc}^{-2}]$'
            if options.logmass:
                yrange= [0.01,30.]
            else:
                yrange= [-0.1,30.]
        else:
            if options.logmass:
                yrange= [0.005*0.13/.07,10.*.13/.07]
            else:
                yrange= [-0.1,10.*.13/.07]
            ylabel=r'$\Sigma_{R_0}(h_z)\ [M_{\odot}\ \mathrm{pc}^{-2}]$'
        if not options.vstructure and not options.hzhr:
            if options.hr:
                ploth= hr
                plotherr= hr_err
                xlabel=r'$\mathrm{radial\ scale\ length\ [kpc]}$'
                xrange= [1.2,4.]
                bins= 11
            elif options.sz:
                ploth= sz**2.
                plotherr= 2.*sz_err*sz
                xlabel=r'$\sigma_z^2\ \mathrm{[km}^2\ \mathrm{s}^{-2}]$'
                xrange= [12.**2.,50.**2.]
                ylabel=r'$\Sigma_{R_0}(\sigma^2_z)\ [M_{\odot}\ \mathrm{pc}^{-2}]$'
                bins= 9
            else:
                ploth= hz
                plotherr= hz_err
                xlabel=r'$\mathrm{vertical\ scale\ height}\ h_z\ \mathrm{[pc]}$'
                xrange= [165,1200]
                bins= 12
            bovy_plot.bovy_plot(ploth,mass*.13/0.07,
                                s=ndata,c=plotc,
                                cmap='jet',
                                xlabel=xlabel,
                                ylabel=ylabel,
                                clabel=zlabel,
                                xrange=xrange,yrange=yrange,
                                vmin=vmin,vmax=vmax,
                                scatter=True,edgecolors='none',
                                colorbar=True,zorder=2,
                                semilogy=options.logmass)
            if not options.cumul and masserrors and options.ploterrors:
                colormap = cm.jet
                for ii in range(len(hz)):
                    pyplot.errorbar(ploth[ii],mass[ii]*.13/0.07,
                                    yerr=mass_err[ii]*.13/0.07,
                                    color=colormap(_squeeze(plotc[ii],
                                                            numpy.amax([vmin,
                                                                        numpy.amin(plotc)]),
                                                            numpy.amin([vmax,
                                                                        numpy.amax(plotc)]))),
                                    elinewidth=1.,capsize=3,zorder=0)
            if not options.cumul and denserrors and options.ploterrors:
                colormap = cm.jet
                for ii in range(len(hz)):
                    pyplot.errorbar(ploth[ii],mass[ii]*.13/0.07,xerr=plotherr[ii],
                                    color=colormap(_squeeze(plotc[ii],
                                                            numpy.amax([vmin,
                                                                        numpy.amin(plotc)]),
                                                            numpy.amin([vmax,
                                                                        numpy.amax(plotc)]))),
                                    elinewidth=1.,capsize=3,zorder=0)
            #Add binsize label
            bovy_plot.bovy_text(r'$\mathrm{points\ use}\ \Delta [\mathrm{Fe/H}] = 0.1,$'+'\n'+r'$\Delta [\alpha/\mathrm{Fe}] = 0.05\ \mathrm{bins}$',
                                bottom_left=True)
            #Overplot histogram
            #ax2 = pyplot.twinx()
            pyplot.hist(ploth,range=xrange,weights=mass*0.13/0.07,color='k',histtype='step',
                        bins=bins,lw=3.,zorder=10)
            #Also XD?
            if options.xd:
                #Set up data
                ydata= numpy.zeros((len(hz),1))
                if options.sz:
                    ydata[:,0]= numpy.log(sz)
                else:
                    ydata[:,0]= numpy.log(hz)
                ycovar= numpy.zeros((len(hz),1))
                if options.sz:
                    ycovar[:,0]= sz_err**2./sz**2.
                else:
                    ycovar[:,0]= hz_err**2./hz**2.
                #Set up initial conditions
                xamp= numpy.ones(options.k)/float(options.k)
                xmean= numpy.zeros((options.k,1))
                for kk in range(options.k):
                    xmean[kk,:]= numpy.mean(ydata,axis=0)\
                        +numpy.random.normal()*numpy.std(ydata,axis=0)
                xcovar= numpy.zeros((options.k,1,1))
                for kk in range(options.k):
                    xcovar[kk,:,:]= numpy.cov(ydata.T)
                #Run XD
                print extreme_deconvolution(ydata,ycovar,xamp,xmean,xcovar,
                                      weight=mass)*len(hz)
                print xamp, xmean, xcovar
                #Plot
                xs= numpy.linspace(xrange[0],xrange[1],1001)
                xdys= numpy.zeros(len(xs))
                for kk in range(options.k):
                    xdys+= xamp[kk]/numpy.sqrt(2.*numpy.pi*xcovar[kk,0,0])\
                        *numpy.exp(-0.5*(numpy.log(xs)-xmean[kk,0])**2./xcovar[kk,0,0])
                xdys/= xs
                bovy_plot.bovy_plot(xs,xdys,'-',color='0.5',overplot=True)
#            ax2.set_yscale('log')
#            ax2.set_yticklabels('')        
#            if options.hr:
#                pyplot.ylim(10**-2.,10.**0.)
#            if options.sz:
#                pyplot.ylim(10**-5.,10.**-2.5)
#            else:
#                pyplot.ylim(10**-5.5,10.**-1.5)
#            pyplot.xlim(xrange[0],xrange[1])
            ax= pyplot.gca()
            if options.sz:
                def my_formatter(x, pos):
                    """s^2"""
                    xs= int(round(math.sqrt(x)))
                    return r'$%i^2$' % xs
                major_formatter = FuncFormatter(my_formatter)
                ax.xaxis.set_major_formatter(major_formatter)
            xstep= ax.xaxis.get_majorticklocs()
            xstep= xstep[1]-xstep[0]
            ax.xaxis.set_minor_locator(MultipleLocator(xstep/5.))
        elif options.hzhr:
            #Make density plot in hR and hz
            bovy_plot.scatterplot(hr,hz,'k,',
                                  levels=[1.01],#HACK such that outliers aren't plotted
                                  cmap='gist_yarg',
                                  bins=11,
                                  xrange=[1.,7.],
                                  yrange=[150.,1200.],
                                  ylabel=r'$\mathrm{vertical\ scale\ height\ [pc]}$',
                                  xlabel=r'$\mathrm{radial\ scale\ length\ [kpc]}$',
                                  onedhists=False,
                                  weights=mass)
        else:
            #Make an illustrative plot of the vertical structure
            nzs= 1001
            zs= numpy.linspace(200.,3000.,nzs)
            total= numpy.zeros(nzs)
            for ii in range(len(hz)):
                total+= mass[ii]/2./hz[ii]*numpy.exp(-zs/hz[ii])
            bovy_plot.bovy_plot(zs,total,color='k',ls='-',lw=3.,
                                semilogy=True,
                                xrange=[0.,3200.],
                                yrange=[0.000001,0.02],
                                xlabel=r'$\mathrm{vertical\ height}\ Z$',
                                ylabel=r'$\rho_*(R=R_0,Z)\ [\mathrm{M}_\odot\ \mathrm{pc}^{-3}]$',
                                zorder=10)
            if options.vbinned:
                #Bin
                mhist, edges= numpy.histogram(hz,range=xrange,
                                              weights=mass,bins=10)
                stotal= numpy.zeros(nzs)
                for ii in range(len(mhist)):
                    hz= (edges[ii+1]+edges[ii])/2.
                    if options.vcumul:
                        if ii == 0.:
                            pstotal= numpy.zeros(nzs)+0.0000001
                        else:
                            pstotal= copy.copy(stotal)
                        stotal+= mhist[ii]/2./hz*numpy.exp(-zs/hz)
                        pyplot.fill_between(zs,stotal,pstotal,
                                            color='%.6f' % (0.25+0.5/(len(mhist)-1)*ii))
                    else:
                        bovy_plot.bovy_plot([zs[0],zs[-1]],
                                            1.*numpy.array([mhist[ii]/2./hz*numpy.exp(-zs[0]/hz),
                                                            mhist[ii]/2./hz*numpy.exp(-zs[-1]/hz)]),
                                            color='0.5',ls='-',overplot=True,
                                            zorder=0)
            else:
                colormap = cm.jet
                for ii in range(len(hz)):
                    bovy_plot.bovy_plot([zs[0],zs[-1]],
                                        100.*numpy.array([mass[ii]/2./hz[ii]*numpy.exp(-zs[0]/hz[ii]),
                                                          mass[ii]/2./hz[ii]*numpy.exp(-zs[-1]/hz[ii])]),
                                        ls='-',overplot=True,alpha=0.5,
                                        zorder=0,
                                        color=colormap(_squeeze(plotc[ii],
                                                                numpy.amax([vmin,
                                                                        numpy.amin(plotc)]),
                                                                numpy.amin([vmax,
                                                                            numpy.amax(plotc)]))))
    else:
        bovy_plot.bovy_print()
        bovy_plot.bovy_dens2d(plotthis.T,origin='lower',cmap='gist_yarg',
                              interpolation='nearest',
                              xlabel=r'$[\mathrm{Fe/H}]$',
                              ylabel=r'$[\alpha/\mathrm{Fe}]$',
                              zlabel=zlabel,
                              xrange=xrange,yrange=yrange,
                              vmin=vmin,vmax=vmax,
                              onedhists=True,
                              contours=False)
        bovy_plot.bovy_text(title,top_right=True,fontsize=16)
        if not options.distzmin is None or not options.distzmax is None:
            if options.distzmin is None:
                distlabel= r'$|Z| < %i\ \mathrm{pc}$' % int(options.distzmax)
            elif options.distzmax is None:
                distlabel= r'$|Z| > %i\ \mathrm{pc}$' % int(options.distzmin)
            else:
                distlabel= r'$%i < |Z| < %i\ \mathrm{pc}$' % (int(options.distzmin),int(options.distzmax))
            bovy_plot.bovy_text(distlabel,bottom_left=True,fontsize=16)
    bovy_plot.bovy_end_print(options.plotfile)
    return None
Exemple #4
0
def readData(metal='rich',sample='G',loggmin=4.2,snmin=15.,select='all'):
    """select= 'program', 'all', 'fakebimodal'"""
    if sample.lower() == 'g':
        if select.lower() == 'program':
            raw= read_gdwarfs(_GDWARFFILE,logg=loggmin,ebv=True,sn=snmin)
        elif select.lower() == 'fakebimodal':
            raw= read_gdwarfs(_FAKEBIMODALGDWARFFILE,
                              logg=loggmin,ebv=True,sn=snmin)
        elif select.lower() == 'fakebimodal_allthin':
            raw= read_gdwarfs(_FAKETHINBIMODALGDWARFFILE,
                              logg=loggmin,ebv=True,sn=snmin)
        elif select.lower() == 'fakebimodal_allthick':
            raw= read_gdwarfs(_FAKETHICKBIMODALGDWARFFILE,
                              logg=loggmin,ebv=True,sn=snmin)
        else:
            raw= read_gdwarfs(logg=loggmin,ebv=True,sn=snmin)
    elif sample.lower() == 'k':
        if select.lower() == 'program':
            raw= read_kdwarfs(_KDWARFFILE,logg=loggmin,ebv=True,sn=snmin)
        else:
            raw= read_kdwarfs(logg=loggmin,ebv=True,sn=snmin)
    #Select sample
    if metal == 'rich':
        indx= (raw.feh > _APOORFEHRANGE[0])*(raw.feh < _APOORFEHRANGE[1])\
            *(raw.afe > _APOORAFERANGE[0])*(raw.afe < _APOORAFERANGE[1])
    elif metal == 'richdiag':
        indx= (raw.feh > _APOORFEHRANGE[0])*(raw.feh < _APOORFEHRANGE[1])\
            *(raw.afe > _APOORAFERANGE[0])*(raw.afe < _APOORAFERANGE[1])*\
            (raw.afe > (-0.15/0.25*(raw.feh+0.25)+0.1))
    elif metal == 'richlowerdiag':
        indx= (raw.feh > _APOORFEHRANGE[0])*(raw.feh < _APOORFEHRANGE[1])\
            *(raw.afe > _APOORAFERANGE[0])*(raw.afe < _APOORAFERANGE[1])*\
            (raw.afe <= (-0.15/0.25*(raw.feh+0.25)+0.1))
    elif metal == 'poor':
        indx= (raw.feh > _ARICHFEHRANGE[0])*(raw.feh < _ARICHFEHRANGE[1])\
            *(raw.afe > _ARICHAFERANGE[0])*(raw.afe < _ARICHAFERANGE[1]) 
    elif metal == 'poorpoor' or metal == 'poorrich':
        #Sort on [Fe/H], cut down the middle
        indx= (raw.feh > _ARICHFEHRANGE[0])*(raw.feh < _ARICHFEHRANGE[1])\
            *(raw.afe > _ARICHAFERANGE[0])*(raw.afe < _ARICHAFERANGE[1])
        raw= raw[indx]
        sfeh= sorted(raw.feh)
        cutfeh= sfeh[len(sfeh)/2]
        #Round to nearest 0.1
        cutfeh= round(10*cutfeh)/10.
        print "Cutting sample down the middle at %4.2f" % cutfeh
        if metal == 'poorpoor': indx= (raw.feh < cutfeh)
        else: indx= (raw.feh >= cutfeh)
        """
    elif metal == 'richpoor' or metal == 'richrich':
    #Sort on [Fe/H], cut down the middle
    indx= (raw.feh > _APOORFEHRANGE[0])*(raw.feh < _APOORFEHRANGE[1])\
    *(raw.afe > _APOORAFERANGE[0])*(raw.afe < _APOORAFERANGE[1])
    raw= raw[indx]
    sfeh= sorted(raw.feh)
    cutfeh= sfeh[len(sfeh)/2]
    #Round to nearest 0.05
    cutfeh= round(20*cutfeh)/20.
    print 'Cutting sample down the middle at %4.2f' % cutfeh
    if metal == 'richpoor': indx= (raw.feh < cutfeh)
    else: indx= (raw.feh >= cutfeh)
    """
    elif metal == 'richrich':
        indx= (raw.feh < _APOORFEHRANGE[1])*(raw.feh > _APOORFEHRANGE[0])\
              *(raw.afe > _APOORAFERANGE[0])*(raw.afe < _APOORAFERANGE[1])
    elif metal == 'richpoor':
        indx= (raw.feh < _APOORFEHRANGE[0])*(raw.feh > -0.6)\
              *(raw.afe > _APOORAFERANGE[0])*(raw.afe < _APOORAFERANGE[1])
    elif metal == 'richpoorest':
        indx= (raw.feh < -0.6)*(raw.feh > -1.5)\
              *(raw.afe > _APOORAFERANGE[0])*(raw.afe < _APOORAFERANGE[1])
    elif metal == 'apoorpoor':
        indx= (raw.feh < _APOORFEHRANGE[1])*(raw.feh > _APOORFEHRANGE[0])\
              *(raw.afe > _APOORAFERANGE[0])*(raw.afe < 0.15)
    elif metal == 'apoorrich':
        indx= (raw.feh < _APOORFEHRANGE[1])*(raw.feh > _APOORFEHRANGE[0])\
              *(raw.afe >= 0.15)*(raw.afe < _APOORAFERANGE[1])
    elif metal == 'arichpoor':
        indx= (raw.feh < _ARICHFEHRANGE[1])*(raw.feh > _ARICHFEHRANGE[0])\
              *(raw.afe >= _ARICHAFERANGE[0])*(raw.afe < 0.35)
    elif metal == 'arichrich':
        indx= (raw.feh < _ARICHFEHRANGE[1])*(raw.feh > _ARICHFEHRANGE[0])\
              *(raw.afe >= 0.35)*(raw.afe < _ARICHAFERANGE[1])
    elif metal == 'allrichpoor':
        indx= (raw.feh > _APOORFEHRANGE[0])*(raw.feh < _APOORFEHRANGE[1])\
              *(raw.afe > _APOORAFERANGE[0])*(raw.afe < _APOORAFERANGE[1])
        raw1= raw[indx]
        indx= (raw.feh > _ARICHFEHRANGE[0])*(raw.feh < _ARICHFEHRANGE[1])\
            *(raw.afe > _ARICHAFERANGE[0])*(raw.afe < _ARICHAFERANGE[1])
        raw2= raw[indx]
        lenraw1= len(raw1)
        raw1.resize(len(raw1)+len(raw2))
        for ii in range(len(raw2)):
            raw1[lenraw1+ii]= raw2[ii]
        raw= raw1
        indx= numpy.array([True for ii in range(len(raw))],dtype='bool')
    elif metal == 'all':
        indx= (raw.feh > -1.5)*(raw.feh < 0.5)\
            *(raw.afe > -0.25)*(raw.afe < 0.5)
    else:
        indx= numpy.array([True for ii in range(len(raw))],dtype='bool')
    raw= raw[indx]
    ndata= len(raw.ra)
    XYZ= numpy.zeros((ndata,3))
    vxvyvz= numpy.zeros((ndata,3))
    cov_vxvyvz= numpy.zeros((ndata,3,3))
    XYZ[:,0]= raw.xc
    XYZ[:,1]= raw.yc
    XYZ[:,2]= raw.zc
    vxvyvz[:,0]= raw.vxc
    vxvyvz[:,1]= raw.vyc
    vxvyvz[:,2]= raw.vzc
    cov_vxvyvz[:,0,0]= raw.vxc_err**2.
    cov_vxvyvz[:,1,1]= raw.vyc_err**2.
    cov_vxvyvz[:,2,2]= raw.vzc_err**2.
    cov_vxvyvz[:,0,1]= raw.vxvyc_rho*raw.vxc_err*raw.vyc_err
    cov_vxvyvz[:,0,2]= raw.vxvzc_rho*raw.vxc_err*raw.vzc_err
    cov_vxvyvz[:,1,2]= raw.vyvzc_rho*raw.vyc_err*raw.vzc_err
    #Load for output
    return (XYZ,vxvyvz,cov_vxvyvz,raw)
Exemple #5
0
def pixelFitVel(options,args):
    if options.sample.lower() == 'g':
        if options.select.lower() == 'program':
            raw= read_gdwarfs(_GDWARFFILE,logg=True,ebv=True,sn=options.snmin,
                              distfac=options.distfac)
        else:
            raw= read_gdwarfs(logg=True,ebv=True,sn=options.snmin,
                              distfac=options.distfac)
    elif options.sample.lower() == 'k':
        if options.select.lower() == 'program':
            raw= read_kdwarfs(_KDWARFFILE,logg=True,ebv=True,sn=options.snmin,
                              distfac=options.distfac)
        else:
            raw= read_kdwarfs(logg=True,ebv=True,sn=options.snmin,
                              distfac=options.distfac)
    if not options.bmin is None:
        #Cut on |b|
        raw= raw[(numpy.fabs(raw.b) > options.bmin)]
    #Bin the data
    binned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe)
    #Savefile
    if os.path.exists(args[0]):#Load savefile
        savefile= open(args[0],'rb')
        fits= pickle.load(savefile)
        ii= pickle.load(savefile)
        jj= pickle.load(savefile)
        savefile.close()
    else:
        fits= []
        ii, jj= 0, 0
    #Sample?
    if options.mcsample:
        if ii < len(binned.fehedges)-1 and jj < len(binned.afeedges)-1:
            print "First do all of the fits ..."
            print "Returning ..."
            return None
        if os.path.exists(args[1]): #Load savefile
            savefile= open(args[1],'rb')
            samples= pickle.load(savefile)
            ii= pickle.load(savefile)
            jj= pickle.load(savefile)
            savefile.close()
        else:
            samples= []
            ii, jj= 0, 0
    #Model
    if options.model.lower() == 'hwr':
        like_func= _HWRLikeMinus
        pdf_func= _HWRLike
        step= [0.01,0.3,0.3,0.3,0.3]
        create_method=['full','step_out','step_out',
                       'step_out','step_out']
        isDomainFinite=[[True,True],[True,True],
                        [True,True],[True,True],
                        [False,True]]
        domain=[[0.,1.],[-10.,10.],[-100.,100.],[-100.,100.],
                [0.,4.6051701859880918]]
    elif options.model.lower() == 'hwrrz':
        like_func= _HWRRZLikeMinus
        pdf_func= _HWRRZLike
        step= [0.01,0.3,0.3,0.3,0.3,
               0.3,0.3,0.3,0.3,
               0.3,0.3,0.3]
        create_method=['full',
                       'step_out','step_out',
                       'step_out','step_out',
                       'step_out','step_out',
                       'step_out','step_out',
                       'step_out','step_out']
#                       'step_out']
        isDomainFinite=[[True,True],
                        [True,True],[True,True],[True,True],[False,True],
                        [True,True],[True,True],[True,True],[False,True],
                        [True,True],[True,True]]#,[True,True]]
        domain=[[0.,1.],
                [-10.,10.],[-100.,100.],[-100.,100.],[0.,4.6051701859880918],
                [-10.,10.],[-100.,100.],[-100.,100.],[0.,4.6051701859880918],
                [-2.,2.],[-10.,10.]]#,[-100.,100.]]
    elif options.model.lower() == 'isotherm':
        like_func= _IsothermLikeMinus
        pdf_func= _IsothermLike
        step= [0.01,0.05,0.3]
        create_method=['full','step_out','step_out']
        isDomainFinite=[[True,True],[False,False],
                        [False,True]]
        domain=[[0.,1.],[0.,0.],[0.,4.6051701859880918]]
    #Run through the bins
    while ii < len(binned.fehedges)-1:
        while jj < len(binned.afeedges)-1:
            data= binned(binned.feh(ii),binned.afe(jj))
            if len(data) < options.minndata:
                if options.mcsample: samples.append(None)
                else: fits.append(None)
                jj+= 1
                if jj == len(binned.afeedges)-1: 
                    jj= 0
                    ii+= 1
                    break
                continue               
            print binned.feh(ii), binned.afe(jj), len(data)
            #Create XYZ and R, vxvyvz, cov_vxvyvz
            R= ((8.-data.xc)**2.+data.yc**2.)**0.5
            #Confine to R-range?
            if not options.rmin is None and not options.rmax is None:
                dataindx= (R >= options.rmin)*\
                    (R < options.rmax)
                data= data[dataindx]
                R= R[dataindx]
            XYZ= numpy.zeros((len(data),3))
            XYZ[:,0]= data.xc
            XYZ[:,1]= data.yc
            XYZ[:,2]= data.zc+_ZSUN
            if options.pivotmean:
                d= numpy.fabs((XYZ[:,2]-numpy.mean(numpy.fabs(XYZ[:,2]))))
            else:
                d= numpy.fabs((XYZ[:,2]-numpy.median(numpy.fabs(XYZ[:,2]))))
            vxvyvz= numpy.zeros((len(data),3))
            vxvyvz[:,0]= data.vxc
            vxvyvz[:,1]= data.vyc
            vxvyvz[:,2]= data.vzc
            cov_vxvyvz= numpy.zeros((len(data),3,3))
            cov_vxvyvz[:,0,0]= data.vxc_err**2.
            cov_vxvyvz[:,1,1]= data.vyc_err**2.
            cov_vxvyvz[:,2,2]= data.vzc_err**2.
            cov_vxvyvz[:,0,1]= data.vxvyc_rho*data.vxc_err*data.vyc_err
            cov_vxvyvz[:,0,2]= data.vxvzc_rho*data.vxc_err*data.vzc_err
            cov_vxvyvz[:,1,2]= data.vyvzc_rho*data.vyc_err*data.vzc_err
            if options.vr or options.vrz:
                #Rotate vxvyvz to vRvTvz
                cosphi= (8.-XYZ[:,0])/R
                sinphi= XYZ[:,1]/R
                vR= -vxvyvz[:,0]*cosphi+vxvyvz[:,1]*sinphi
                vT= vxvyvz[:,0]*sinphi+vxvyvz[:,1]*cosphi
                #Subtract mean vR
                vR-= numpy.mean(vR)
                vxvyvz[:,0]= vR               
                vxvyvz[:,1]= vT
                for rr in range(len(XYZ[:,0])):
                    rot= numpy.array([[cosphi[rr],sinphi[rr]],
                                      [-sinphi[rr],cosphi[rr]]])
                    sxy= cov_vxvyvz[rr,0:2,0:2]
                    sRT= numpy.dot(rot,numpy.dot(sxy,rot.T))
                    cov_vxvyvz[rr,0:2,0:2]= sRT
            #Fit this data
            #Initial condition
            if options.model.lower() == 'hwr':
                params= numpy.array([0.02,numpy.log(30.),0.,0.,numpy.log(6.)])
            elif options.model.lower() == 'hwrrz':
                params= numpy.array([0.02,numpy.log(30.),0.,0.,numpy.log(6.),
                                     numpy.log(30.),0.,0.,numpy.log(6.),
                                     0.2,0.])#,0.])
            elif options.model.lower() == 'isotherm':
                params= numpy.array([0.02,numpy.log(30.),numpy.log(6.)])
            if not options.mcsample:
                #Optimize likelihood
                params= optimize.fmin_powell(like_func,params,
                                             args=(XYZ,vxvyvz,cov_vxvyvz,R,d,
                                                   options.vr,options.vrz))
                if options.chi2:
                    #Calculate chi2 and chi2/dof
                    chi2, dof= like_func(params,XYZ,vxvyvz,cov_vxvyvz,R,d,options.vr,options.vrz,
                                    chi2=True)
                    dof-= len(params)
                    params.resize(len(params)+2)
                    params[-2]= chi2
                    params[-1]= chi2/dof
                print numpy.exp(params)
                fits.append(params)
            else:
                #Load best-fit params
                params= fits[jj+ii*binned.npixafe()]
                print numpy.exp(params)
                thesesamples= bovy_mcmc.markovpy(params,
                #thesesamples= bovy_mcmc.slice(params,
                                                 #step,
                                                 0.01,
                                                 pdf_func,
                                                 (XYZ,vxvyvz,cov_vxvyvz,R,d,
                                                  options.vr),#options.vrz),
                                                 #create_method=create_method,
                                                 isDomainFinite=isDomainFinite,
                                                 domain=domain,
                                                 nsamples=options.nsamples)
                #Print some helpful stuff
                printthis= []
                for kk in range(len(params)):
                    xs= numpy.array([s[kk] for s in thesesamples])
                    printthis.append(0.5*(numpy.exp(numpy.mean(xs))-numpy.exp(numpy.mean(xs)-numpy.std(xs))-numpy.exp(numpy.mean(xs))+numpy.exp(numpy.mean(xs)+numpy.std(xs))))
                print printthis
                samples.append(thesesamples)               
            jj+= 1
            if jj == len(binned.afeedges)-1: 
                jj= 0
                ii+= 1
            if options.mcsample: save_pickles(args[1],samples,ii,jj)
            else: save_pickles(args[0],fits,ii,jj)
            if jj == 0: #this means we've reset the counter 
                break
    if options.mcsample: save_pickles(args[1],samples,ii,jj)
    else: save_pickles(args[0],fits,ii,jj)
    return None
Exemple #6
0
def plotVelPDFs(options,args):
    if options.sample.lower() == 'g':
        if options.select.lower() == 'program':
            raw= read_gdwarfs(_GDWARFFILE,logg=True,ebv=True,sn=True)
        else:
            raw= read_gdwarfs(logg=True,ebv=True,sn=True)
    elif options.sample.lower() == 'k':
        if options.select.lower() == 'program':
            raw= read_kdwarfs(_KDWARFFILE,logg=True,ebv=True,sn=True)
        else:
            raw= read_kdwarfs(logg=True,ebv=True,sn=True)
    #Bin the data   
    binned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe)
    if options.tighten:
        tightbinned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe,
                                 fehmin=-1.6,fehmax=0.5,afemin=-0.05,
                                 afemax=0.55)
    else:
        tightbinned= binned
    #Savefile1
    if os.path.exists(args[0]):#Load savefile
        savefile= open(args[0],'rb')
        velfits= pickle.load(savefile)
        savefile.close()
    if os.path.exists(args[1]):#Load savefile
        savefile= open(args[1],'rb')
        densfits= pickle.load(savefile)
        savefile.close()
    #Uncertainties are in savefile3 and 4
    if len(args) > 3 and os.path.exists(args[3]):
        savefile= open(args[3],'rb')
        denssamples= pickle.load(savefile)
        savefile.close()
        denserrors= True
    else:
        denssamples= None
        denserrors= False
    if len(args) > 2 and os.path.exists(args[2]):
        savefile= open(args[2],'rb')
        velsamples= pickle.load(savefile)
        savefile.close()
        velerrors= True
    else:
        velsamples= None            
        velerrors= False
    #Now plot
    #Run through the pixels and gather
    if options.type.lower() == 'afe' or options.type.lower() == 'feh' \
            or options.type.lower() == 'fehafe' \
            or options.type.lower() == 'zfunc' \
            or options.type.lower() == 'afefeh':
        plotthis= []
        errors= []
    else:
        plotthis= numpy.zeros((tightbinned.npixfeh(),tightbinned.npixafe()))
    if options.kde: allsamples= []
    sausageFehAfe= [options.feh,options.afe]#-0.15,0.075]#[[-0.85,0.425],[-0.45,0.275],[-0.15,0.075]]
    if options.subtype.lower() == 'sausage':
        sausageSamples= []
    for ii in range(tightbinned.npixfeh()):
        for jj in range(tightbinned.npixafe()):
            data= binned(tightbinned.feh(ii),tightbinned.afe(jj))
            fehindx= binned.fehindx(tightbinned.feh(ii))#Map onto regular binning
            afeindx= binned.afeindx(tightbinned.afe(jj))
            if not (numpy.fabs(tightbinned.feh(ii)-sausageFehAfe[0])< 0.01 and numpy.fabs(tightbinned.afe(jj) - sausageFehAfe[1]) < 0.01):
                continue
            thisdensfit= densfits[afeindx+fehindx*binned.npixafe()]
            thisvelfit= velfits[afeindx+fehindx*binned.npixafe()]
            if options.velmodel.lower() == 'hwr':
                thisplot=[tightbinned.feh(ii),
                              tightbinned.afe(jj),
                              numpy.exp(thisvelfit[1]),
                              numpy.exp(thisvelfit[4]),
                              len(data),
                              thisvelfit[2],
                              thisvelfit[3]]
                #Als find min and max z for this data bin, and median
                zsorted= sorted(numpy.fabs(data.zc+_ZSUN))
                zmin= zsorted[int(numpy.ceil(0.16*len(zsorted)))]
                zmax= zsorted[int(numpy.floor(0.84*len(zsorted)))]
                thisplot.extend([zmin,zmax,numpy.mean(numpy.fabs(data.zc+_ZSUN))])
                #Errors
                if velerrors:
                    thesesamples= velsamples[afeindx+fehindx*binned.npixafe()]
                break
    #Now plot
    if options.type.lower() == 'slopequad':
        plotx= numpy.array([thesesamples[ii][3] for ii in range(len(thesesamples))])
        ploty= numpy.array([thesesamples[ii][2] for ii in range(len(thesesamples))])
        xrange= [-10.,10.]
        yrange= [-20.,20.]
        xlabel=r'$\frac{\mathrm{d}^2 \sigma_z}{\mathrm{d} z^2}(z_{1/2})\ [\mathrm{km}\ \mathrm{s}^{-1}\ \mathrm{kpc}^{-2}]$'
        ylabel=r'$\frac{\mathrm{d} \sigma_z}{\mathrm{d} z}(z_{1/2})\ [\mathrm{km\ s}^{-1}\ \mathrm{kpc}^{-1}]$'
    elif options.type.lower() == 'slopehsm':
        ploty= numpy.array([thesesamples[ii][2] for ii in range(len(thesesamples))])
        plotx= numpy.exp(-numpy.array([thesesamples[ii][4] for ii in range(len(thesesamples))]))
        xrange= [0.,0.3]
        yrange= [-20.,20.]
        xlabel=r'$h^{-1}_\sigma\ [\mathrm{kpc}^{-1}]$'
        ylabel=r'$\frac{\mathrm{d} \sigma_z}{\mathrm{d} z}(z_{1/2})\ [\mathrm{km\ s}^{-1}\ \mathrm{kpc}^{-1}]$'
    elif options.type.lower() == 'slopesz':
        ploty= numpy.array([thesesamples[ii][2] for ii in range(len(thesesamples))])
        plotx= numpy.exp(numpy.array([thesesamples[ii][1] for ii in range(len(thesesamples))]))
        xrange= [0.,60.]
        yrange= [-20.,20.]
        xlabel=r'$\sigma_z(z_{1/2}) [\mathrm{km\ s}^{-1}$'
        ylabel=r'$\frac{\mathrm{d} \sigma_z}{\mathrm{d} z}(z_{1/2})\ [\mathrm{km\ s}^{-1}\ \mathrm{kpc}^{-1}]$'
    elif options.type.lower() == 'szhsm':
        plotx= numpy.exp(-numpy.array([thesesamples[ii][4] for ii in range(len(thesesamples))]))
        ploty= numpy.exp(numpy.array([thesesamples[ii][1] for ii in range(len(thesesamples))]))
        yrange= [0.,60.]
        xrange= [0.,0.3]
        xlabel=r'$h^{-1}_\sigma\ [\mathrm{kpc}^{-1}]$'
        ylabel=r'$\sigma_z(z_{1/2}) [\mathrm{km\ s}^{-1}$'
    bovy_plot.bovy_print()
    axScatter, axHistx, axHisty= bovy_plot.scatterplot(plotx,ploty,'k,',
                                                       onedhists=True,
                                                       bins=31,
                                                       xlabel=xlabel,ylabel=ylabel,
                                                       xrange=xrange,
                                                       onedhistynormed=True,
                                                       yrange=yrange,
                                                       retAxes=True)
    if options.type.lower() == 'slopequad':
        #Also add `quadratic term = 0' to vertical histogram
        ploty= ploty[(numpy.fabs(plotx) < 0.5)]
        histy, edges, patches= axHisty.hist(ploty,
                                            bins=51,
                                            orientation='horizontal',
                                            weights=numpy.ones(len(ploty))/float(len(ploty))/2.,
                                            histtype='step',
                                            range=sorted(yrange),
                                            color='0.6',
                                            lw=2.)
    #Label
    bovy_plot.bovy_text(r'$[\mathrm{Fe/H}]\ =\ %.2f$' % options.feh
                        +'\n'
                        +r'$[\alpha/\mathrm{Fe}]\ =\ %.3f$' % options.afe,
                        top_right=True,
                        size=18)                     
    bovy_plot.bovy_end_print(options.plotfile)
Exemple #7
0
def testDFNorm(options,args):
    #Read the data
    print "Reading the data ..."
    if options.sample.lower() == 'g':
        if not options.fakedata is None:
            raw= read_gdwarfs(options.fakedata,logg=True,ebv=True,sn=options.snmin,nosolar=True)
        elif options.select.lower() == 'program':
            raw= read_gdwarfs(_GDWARFFILE,logg=True,ebv=True,sn=options.snmin,nosolar=True)
        else:
            raw= read_gdwarfs(logg=True,ebv=True,sn=options.snmin,nosolar=True)
    elif options.sample.lower() == 'k':
        if options.select.lower() == 'program':
            raw= read_kdwarfs(_KDWARFFILE,logg=True,ebv=True,sn=options.snmin,nosolar=True)
        else:
            raw= read_kdwarfs(logg=True,ebv=True,sn=options.snmin,nosolar=True)
    if not options.bmin is None:
        #Cut on |b|
        raw= raw[(numpy.fabs(raw.b) > options.bmin)]
    if not options.fehmin is None:
        raw= raw[(raw.feh >= options.fehmin)]
    if not options.fehmax is None:
        raw= raw[(raw.feh < options.fehmax)]
    if not options.afemin is None:
        raw= raw[(raw.afe >= options.afemin)]
    if not options.afemax is None:
        raw= raw[(raw.afe < options.afemax)]
    if not options.plate is None and not options.loo:
        raw= raw[(raw.plate == options.plate)]
    elif not options.plate is None:
        raw= raw[(raw.plate != options.plate)]
    #Bin the data
    binned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe)
    #Map the bins with ndata > minndata in 1D
    fehs, afes= [], []
    for ii in range(len(binned.fehedges)-1):
        for jj in range(len(binned.afeedges)-1):
            data= binned(binned.feh(ii),binned.afe(jj))
            if len(data) < options.minndata:
                continue
            fehs.append(binned.feh(ii))
            afes.append(binned.afe(jj))
    nabundancebins= len(fehs)
    fehs= numpy.array(fehs)
    afes= numpy.array(afes)
    if not options.singlefeh is None:
        if options.loo:
            pass
        else:
            #Set up single feh
            indx= binned.callIndx(options.singlefeh,options.singleafe)
            if numpy.sum(indx) == 0:
                raise IOError("Bin corresponding to singlefeh and singleafe is empty ...")
            data= copy.copy(binned.data[indx])
            print "Using %i data points ..." % (len(data))
            #Bin again
            binned= pixelAfeFeh(data,dfeh=options.dfeh,dafe=options.dafe)
            fehs, afes= [], []
            for ii in range(len(binned.fehedges)-1):
                for jj in range(len(binned.afeedges)-1):
                    data= binned(binned.feh(ii),binned.afe(jj))
                    if len(data) < options.minndata:
                        continue
                    fehs.append(binned.feh(ii))
                    afes.append(binned.afe(jj))
            nabundancebins= len(fehs)
            fehs= numpy.array(fehs)
            afes= numpy.array(afes)
    #Setup everything for the selection function
    print "Setting up stuff for the normalization integral ..."
    normintstuff= setup_normintstuff(options,raw,binned,fehs,afes)
    if not options.init is None:
        #Load initial parameters from file
        savefile= open(options.init,'rb')
        params= pickle.load(savefile)
        savefile.close()
    else:
        #First initialization
        params= initialize(options,fehs,afes)
    #Now perform tests
    if options.type.lower() == 'hr':
        testDFNormhr(params,fehs,afes,binned,options,normintstuff)
    elif options.type.lower() == 'sr':
        testDFNormsr(params,fehs,afes,binned,options,normintstuff)
    elif options.type.lower() == 'vo':
        testDFNormvo(params,fehs,afes,binned,options,normintstuff)
Exemple #8
0
def generate_fakeDFData(options,args):
    #Check whether the savefile already exists
    if os.path.exists(args[0]):
        savefile= open(args[0],'rb')
        print "Savefile already exists, not re-sampling and overwriting ..."
        return None
    #Read the data
    print "Reading the data ..."
    if options.sample.lower() == 'g':
        if options.select.lower() == 'program':
            raw= read_gdwarfs(_GDWARFFILE,logg=True,ebv=True,sn=options.snmin,nosolar=True,nocoords=True)
        else:
            raw= read_gdwarfs(logg=True,ebv=True,sn=options.snmin,nosolar=True,nocoords=True)
    elif options.sample.lower() == 'k':
        if options.select.lower() == 'program':
            raw= read_kdwarfs(_KDWARFFILE,logg=True,ebv=True,sn=options.snmin,nosolar=True,nocoords=True)
        else:
            raw= read_kdwarfs(logg=True,ebv=True,sn=options.snmin,nosolar=True,
                              nocoords=True)
    if not options.bmin is None:
        #Cut on |b|
        raw= raw[(numpy.fabs(raw.b) > options.bmin)]
    if not options.fehmin is None:
        raw= raw[(raw.feh >= options.fehmin)]
    if not options.fehmax is None:
        raw= raw[(raw.feh < options.fehmax)]
    if not options.afemin is None:
        raw= raw[(raw.afe >= options.afemin)]
    if not options.afemax is None:
        raw= raw[(raw.afe < options.afemax)]
    if not options.plate is None and not options.loo:
        raw= raw[(raw.plate == options.plate)]
    elif not options.plate is None:
        raw= raw[(raw.plate != options.plate)]
    #Bin the data
    binned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe)
    #Map the bins with ndata > minndata in 1D
    fehs, afes= [], []
    for ii in range(len(binned.fehedges)-1):
        for jj in range(len(binned.afeedges)-1):
            data= binned(binned.feh(ii),binned.afe(jj))
            if len(data) < options.minndata:
                continue
            fehs.append(binned.feh(ii))
            afes.append(binned.afe(jj))
    nabundancebins= len(fehs)
    fehs= numpy.array(fehs)
    afes= numpy.array(afes)
    if not options.singlefeh is None:
        if options.loo:
            pass
        else:
            #Set up single feh
            indx= binned.callIndx(options.singlefeh,options.singleafe)
            if numpy.sum(indx) == 0:
                raise IOError("Bin corresponding to singlefeh and singleafe is empty ...")
            data= copy.copy(binned.data[indx])
            print "Using %i data points ..." % (len(data))
            #Bin again
            binned= pixelAfeFeh(data,dfeh=options.dfeh,dafe=options.dafe)
            fehs, afes= [], []
            for ii in range(len(binned.fehedges)-1):
                for jj in range(len(binned.afeedges)-1):
                    data= binned(binned.feh(ii),binned.afe(jj))
                    if len(data) < options.minndata:
                        continue
                    fehs.append(binned.feh(ii))
                    afes.append(binned.afe(jj))
            nabundancebins= len(fehs)
            fehs= numpy.array(fehs)
            afes= numpy.array(afes)
    #Setup the selection function
    #Load selection function
    plates= numpy.array(list(set(list(raw.plate))),dtype='int') #Only load plates that we use
    print "Using %i plates, %i stars ..." %(len(plates),len(raw))
    sf= segueSelect(plates=plates,type_faint='tanhrcut',
                    sample=options.sample,type_bright='tanhrcut',
                    sn=options.snmin,select=options.select,
                    indiv_brightlims=options.indiv_brightlims)
    platelb= bovy_coords.radec_to_lb(sf.platestr.ra,sf.platestr.dec,
                                     degree=True)
    if options.sample.lower() == 'g':
        grmin, grmax= 0.48, 0.55
        rmin,rmax= 14.50001, 20.199999 #so we don't go out of the range
    if options.sample.lower() == 'k':
        grmin, grmax= 0.55, 0.75
        rmin,rmax= 14.50001, 18.999999
    colorrange=[grmin,grmax]
    mapfehs= monoAbundanceMW.fehs()
    mapafes= monoAbundanceMW.afes()
    #Setup params
    if not options.init is None:
        #Load initial parameters from file
        savefile= open(options.init,'rb')
        tparams= pickle.load(savefile)
        savefile.close()
        #Setup the correct form
        params= initialize(options,fehs,afes)
        params[0:6]= get_dfparams(tparams,options.index,options,log=True)
        params[6:11]= tparams[-5:len(tparams)]
    else:
        params= initialize(options,fehs,afes)
    #Setup potential
    if (options.potential.lower() == 'flatlog' or options.potential.lower() == 'flatlogdisk') \
            and not options.flatten is None:
        #Set flattening
        potparams= list(get_potparams(params,options,len(fehs)))
        potparams[0]= options.flatten
        params= set_potparams(potparams,params,options,len(fehs))
    pot= setup_potential(params,options,len(fehs))
    aA= setup_aA(pot,options)
    if not options.multi is None:
        binned= fakeDFData_abundance_singles(binned,options,args,fehs,afes)
    else:
        for ii in range(len(fehs)):
            print "Working on population %i / %i ..." % (ii+1,len(fehs))
            #Setup qdf
            dfparams= get_dfparams(params,ii,options,log=False)
            vo= get_vo(params,options,len(fehs))
            ro= get_ro(params,options)
            if options.dfmodel.lower() == 'qdf':
                #Normalize
                hr= dfparams[0]/ro
                sr= dfparams[1]/vo
                sz= dfparams[2]/vo
                hsr= dfparams[3]/ro
                hsz= dfparams[4]/ro
            print hr, sr, sz, hsr, hsz
            qdf= quasiisothermaldf(hr,sr,sz,hsr,hsz,pot=pot,aA=aA,cutcounter=True)
            #Some more selection stuff
            data= binned(fehs[ii],afes[ii])
            #feh and color
            feh= fehs[ii]
            fehrange= [feh-options.dfeh/2.,feh+options.dfeh/2.]
            #FeH
            fehdist= DistSpline(*numpy.histogram(data.feh,bins=5,
                                                 range=fehrange),
                                 xrange=fehrange,dontcuttorange=False)
            #Color
            colordist= DistSpline(*numpy.histogram(data.dered_g\
                                                       -data.dered_r,
                                                   bins=9,range=colorrange),
                                   xrange=colorrange)
            #Re-sample
            binned= fakeDFData(binned,qdf,ii,params,fehs,afes,options,
                               rmin,rmax,
                               platelb,
                               grmin,grmax,
                               fehrange,
                               colordist,
                               fehdist,feh,sf,
                               mapfehs,mapafes,
                               ro=None,vo=None)
    #Save to new file
    fitsio.write(args[0],binned.data)
    return None
def plotOneDiskVsTwoDisks(options,args):
    if options.sample.lower() == 'g':
        if options.select.lower() == 'program':
            raw= read_gdwarfs(_GDWARFFILE,logg=True,ebv=True,sn=options.sn)
        else:
            raw= read_gdwarfs(logg=True,ebv=True,sn=options.sn)
    elif options.sample.lower() == 'k':
        if options.select.lower() == 'program':
            raw= read_kdwarfs(_KDWARFFILE,logg=True,ebv=True,sn=options.sn)
        else:
            raw= read_kdwarfs(logg=True,ebv=True,sn=options.sn)
    #Bin the data   
    binned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe)
    if options.tighten:
        tightbinned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe,
                                 fehmin=-2.,fehmax=0.3,afemin=0.,afemax=0.45)
    else:
        tightbinned= binned
    #Savefile1
    if os.path.exists(args[0]):#Load savefile
        savefile= open(args[0],'rb')
        onefits= pickle.load(savefile)
        savefile.close()
    if os.path.exists(args[1]):#Load savefile
        savefile= open(args[1],'rb')
        twofits= pickle.load(savefile)
        savefile.close()
    #Uncertainties are in savefile3 and 4
    if len(args) > 3 and os.path.exists(args[3]):
        savefile= open(args[3],'rb')
        twosamples= pickle.load(savefile)
        savefile.close()
        twoerrors= True
    else:
        twosamples= None
        twoerrors= False
    if len(args) > 2 and os.path.exists(args[2]):
        savefile= open(args[2],'rb')
        onesamples= pickle.load(savefile)
        savefile.close()
        oneerrors= True
    else:
        onesamples= None            
        oneerrors= False
    #If --mass is set to a filename, load the masses from that file
    #and use those for the symbol size
    if not options.mass is None and os.path.exists(options.mass):
        savefile= open(options.mass,'rb')
        mass= pickle.load(savefile)
        savefile.close()
        ndata= mass
        masses= True
    else:
        masses= False
    #Run through the pixels and gather
    plotthis= []
    errors= []
    for ii in range(tightbinned.npixfeh()):
        for jj in range(tightbinned.npixafe()):
            data= binned(tightbinned.feh(ii),tightbinned.afe(jj))
            fehindx= binned.fehindx(tightbinned.feh(ii))#Map onto regular binning
            afeindx= binned.afeindx(tightbinned.afe(jj))
            if afeindx+fehindx*binned.npixafe() >= len(onefits):
                continue
            thisonefit= onefits[afeindx+fehindx*binned.npixafe()]
            thistwofit= twofits[afeindx+fehindx*binned.npixafe()]
            if thisonefit is None:
                    continue
            if len(data) < options.minndata:
                    continue
            #print tightbinned.feh(ii), tightbinned.afe(jj), numpy.exp(thisonefit), numpy.exp(thistwofit)
            #Which is the dominant two-exp component?
            if thistwofit[4] > 0.5: twoIndx= 1
            else: twoIndx= 0
            if options.type == 'hz':
                if masses:
                    plotthis.append([numpy.exp(thisonefit[0])*1000.,
                                     numpy.exp(thistwofit[twoIndx])*1000.,
                                     mass[afeindx+fehindx*binned.npixafe()]])
                else:
                    plotthis.append([numpy.exp(thisonefit[0])*1000.,
                                     numpy.exp(thistwofit[twoIndx])*1000.,
                                     len(data)])
                #Discrepant point
                if plotthis[-1][1] < 250. and plotthis[-1][0] > 500.:
                    print "strange point: ", tightbinned.feh(ii),tightbinned.afe(jj)
            elif options.type == 'hr':
                plotthis.append([numpy.exp(thisonefit[1]),numpy.exp(thistwofit[twoIndx+2]),len(data)])
            theseerrors= []
            if oneerrors:
                theseonesamples= onesamples[afeindx+fehindx*binned.npixafe()]
                if options.type == 'hz':
                    xs= numpy.array([s[0] for s in theseonesamples])
                    theseerrors.append(500.*(-numpy.exp(numpy.mean(xs)-numpy.std(xs))+numpy.exp(numpy.mean(xs)+numpy.std(xs))))
                elif options.type == 'hr':
                    xs= numpy.array([s[1] for s in theseonesamples])
                    theseerrors.append(0.5*(-numpy.exp(numpy.mean(xs)-numpy.std(xs))+numpy.exp(numpy.mean(xs)+numpy.std(xs))))
            if twoerrors:
                thesetwosamples= twosamples[afeindx+fehindx*binned.npixafe()]
                if options.type == 'hz':
                    xs= numpy.array([s[twoIndx] for s in thesetwosamples])
                    theseerrors.append(500.*(-numpy.exp(numpy.mean(xs)-numpy.std(xs))+numpy.exp(numpy.mean(xs)+numpy.std(xs))))
                elif options.type == 'hr':
                    xs= numpy.array([s[2+twoIndx] for s in thesetwosamples])
                    theseerrors.append(0.5*(-numpy.exp(numpy.mean(xs)-numpy.std(xs))+numpy.exp(numpy.mean(xs)+numpy.std(xs))))
            errors.append(theseerrors)
    x, y, ndata= [], [], []
    if oneerrors: x_err= []
    if twoerrors: y_err= []
    for ii in range(len(plotthis)):
        x.append(plotthis[ii][0])
        y.append(plotthis[ii][1])
        ndata.append(plotthis[ii][2])
        if oneerrors: x_err.append(errors[ii][0])
        if twoerrors: y_err.append(errors[ii][1])
    x= numpy.array(x)
    y= numpy.array(y)
    if oneerrors: x_err= numpy.array(x_err)
    if twoerrors: y_err= numpy.array(y_err)
    ndata= numpy.array(ndata)
    #Process ndata
    if not masses:
        ndata= ndata**.5
        ndata= ndata/numpy.median(ndata)*35.
        ndata= 20
    else:    
        ndata= _squeeze(ndata,numpy.amin(ndata),numpy.amax(ndata))
        ndata= ndata*200.+10.
    #Now plot
    if options.type == 'hz':
        xrange= [150,1200]
        xlabel=r'$\mathrm{single-exponential\ scale\ height}$'
        ylabel=r'$\mathrm{two-exponentials\ scale\ height}$'
    elif options.type == 'hr':
        xrange= [1.2,5.]
        xlabel=r'$\mathrm{single-exponential\ scale\ length}$'
        ylabel=r'$\mathrm{two-exponentials\ scale\ length}$'
    yrange=xrange
    bovy_plot.bovy_print()
    bovy_plot.bovy_plot(x,y,color='k',
                        s=ndata,
                        ylabel=ylabel,
                        xlabel=xlabel,
                        xrange=xrange,yrange=yrange,
                        scatter=True,edgecolors='none',
                        colorbar=False,zorder=2)
    bovy_plot.bovy_plot([xrange[0],xrange[1]],[xrange[0],xrange[1]],color='0.5',ls='--',overplot=True)
    if oneerrors:
        #Overplot errors
        for ii in range(len(x)):
            #                if (options.type == 'hr' and x[ii] < 5.) or options.type == 'hz':
            pyplot.errorbar(x[ii],y[ii],xerr=x_err[ii],color='k',
                            elinewidth=1.,capsize=3,zorder=0)
    if twoerrors:
        #Overplot errors
        for ii in range(len(x)):
            #                if (options.type == 'hr' and x[ii] < 5.) or options.type == 'hz':
            pyplot.errorbar(x[ii],y[ii],yerr=y_err[ii],color='k',
                            elinewidth=1.,capsize=3,zorder=0)
    bovy_plot.bovy_end_print(options.plotfile)
    return None   
Exemple #10
0
def plotTilt(options,args):
    if options.sample.lower() == 'g':
        if options.select.lower() == 'program':
            raw= read_gdwarfs(_GDWARFFILE,logg=True,ebv=True,sn=True)
        else:
            raw= read_gdwarfs(logg=True,ebv=True,sn=True)
    elif options.sample.lower() == 'k':
        if options.select.lower() == 'program':
            raw= read_kdwarfs(_KDWARFFILE,logg=True,ebv=True,sn=True)
        else:
            raw= read_kdwarfs(logg=True,ebv=True,sn=True)
    #Bin the data   
    binned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe)
    if options.tighten:
        tightbinned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe,
                                 fehmin=-1.6,fehmax=0.5,afemin=-0.05,
                                 afemax=0.55)
    else:
        tightbinned= binned
    #Savefile1
    if os.path.exists(args[0]):#Load savefile
        savefile= open(args[0],'rb')
        velfits= pickle.load(savefile)
        savefile.close()
    #Uncertainties are in savefile2
    if len(args) > 2 and os.path.exists(args[2]):
        savefile= open(args[2],'rb')
        velsamples= pickle.load(savefile)
        savefile.close()
        velerrors= True
    else:
        velsamples= None            
        velerrors= False
    #Now plot
    #Run through the pixels and gather
    if options.type.lower() == 'afe' or options.type.lower() == 'feh' \
            or options.type.lower() == 'fehafe' \
            or options.type.lower() == 'zfunc' \
            or options.type.lower() == 'afefeh':
        plotthis= []
        errors= []
    else:
        plotthis= numpy.zeros((tightbinned.npixfeh(),tightbinned.npixafe()))
    for ii in range(tightbinned.npixfeh()):
        for jj in range(tightbinned.npixafe()):
            data= binned(tightbinned.feh(ii),tightbinned.afe(jj))
            fehindx= binned.fehindx(tightbinned.feh(ii))#Map onto regular binning
            afeindx= binned.afeindx(tightbinned.afe(jj))
            if afeindx+fehindx*binned.npixafe() >= len(velfits) \
                    or afeindx+fehindx*binned.npixafe() >= len(velfits):
                if options.type.lower() == 'afe' or options.type.lower() == 'feh' or options.type.lower() == 'fehafe' \
                        or options.type.lower() == 'zfunc' \
                        or options.type.lower() == 'afefeh':
                    continue
                else:
                    plotthis[ii,jj]= numpy.nan
                    continue
            thisvelfit= velfits[afeindx+fehindx*binned.npixafe()]
            if thisvelfit is None:
                if options.type.lower() == 'afe' or options.type.lower() == 'feh' or options.type.lower() == 'fehafe' \
                        or options.type.lower() == 'zfunc' \
                        or options.type.lower() == 'afefeh':
                    continue
                else:
                    plotthis[ii,jj]= numpy.nan
                    continue
            if len(data) < options.minndata:
                if options.type.lower() == 'afe' or options.type.lower() == 'feh' or options.type.lower() == 'fehafe' \
                        or options.type.lower() == 'zfunc' \
                        or options.type.lower() == 'afefeh':
                    continue
                else:
                    plotthis[ii,jj]= numpy.nan
                    continue
            if options.type == 'tilt':
                plotthis[ii,jj]= numpy.arctan(thisvelfit[9])/_DEGTORAD
            elif options.type == 'tiltslope':
                plotthis[ii,jj]= thisvelfit[10]
            elif options.type == 'srz':
                plotthis[ii,jj]= (numpy.exp(2.*thisvelfit[5])-numpy.exp(2.*thisvelfit[1]))*thisvelfit[9]/(1.-thisvelfit[9]**2.)
            elif options.type.lower() == 'afe' \
                    or options.type.lower() == 'feh' \
                    or options.type.lower() == 'fehafe' \
                    or options.type.lower() == 'zfunc' \
                    or options.type.lower() == 'afefeh':
                thisplot=[tightbinned.feh(ii),
                          tightbinned.afe(jj),
                          len(data)]
                thisplot.extend(thisvelfit)
                #Als find min and max z for this data bin, and median
                if options.subtype.lower() == 'rfunc':
                    zsorted= sorted(numpy.sqrt((8.-data.xc)**2.+data.yc**2.))
                else:
                    zsorted= sorted(numpy.fabs(data.zc+_ZSUN))
                zmin= zsorted[int(numpy.ceil(0.16*len(zsorted)))]
                zmax= zsorted[int(numpy.floor(0.84*len(zsorted)))]
                zmin= zsorted[int(numpy.ceil(0.025*len(zsorted)))]
                zmax= zsorted[int(numpy.floor(0.975*len(zsorted)))]
                if options.pivotmean:
                    thisplot.extend([zmin,zmax,numpy.mean(numpy.fabs(data.zc+_ZSUN))])
                else:
                    thisplot.extend([zmin,zmax,numpy.median(numpy.fabs(data.zc+_ZSUN))])
                plotthis.append(thisplot)
                #Errors
                if velerrors:
                    theseerrors= []
                    thesesamples= velsamples[afeindx+fehindx*binned.npixafe()]
                    for kk in [1,4,5,8]:
                        xs= numpy.array([s[kk] for s in thesesamples])
                        theseerrors.append(0.5*(numpy.exp(numpy.mean(xs))-numpy.exp(numpy.mean(xs)-numpy.std(xs))-numpy.exp(numpy.mean(xs))+numpy.exp(numpy.mean(xs)+numpy.std(xs))))
                    for kk in [0,2,3,6,7,9,10]: #,11]:
                        xs= numpy.array([s[kk] for s in thesesamples])
                        theseerrors.append(numpy.std(xs))
                        errors.append(theseerrors)
    #Set up plot
    #print numpy.nanmin(plotthis), numpy.nanmax(plotthis)
    if options.type == 'tilt':
        print numpy.nanmin(plotthis), numpy.nanmax(plotthis)
        vmin, vmax= -20.,20.
        zlabel= r'$\mathrm{tilt\ at}\ Z = 0\ [\mathrm{degree}]$'
    elif options.type == 'tiltslope':
        print numpy.nanmin(plotthis), numpy.nanmax(plotthis)
        vmin, vmax= -2.,2.
        zlabel= r'$\frac{\mathrm{d}\tan \mathrm{tilt}}{\mathrm{d} (Z/R)}$'
    elif options.type == 'srz':
        vmin, vmax= -100.,100.
        zlabel= r'$\sigma^2_{RZ}\ [\mathrm{km}^2\, \mathrm{s}^{-2}]$'
    elif options.type == 'afe':
        vmin, vmax= 0.0,.5
        zlabel=r'$[\alpha/\mathrm{Fe}]$'
    elif options.type == 'feh':
        vmin, vmax= -1.6,0.4
        zlabel=r'$[\mathrm{Fe/H}]$'
    if options.tighten:
        xrange=[-1.6,0.5]
        yrange=[-0.05,0.55]
    else:
        xrange=[-2.,0.6]
        yrange=[-0.1,0.6]
    if options.type.lower() == 'afe' or options.type.lower() == 'feh' \
            or options.type.lower() == 'fehafe' \
            or options.type.lower() == 'afefeh':
        bovy_plot.bovy_print(fig_height=3.87,fig_width=5.)
        #Gather everything
        zmin, zmax, pivot, tilt, tiltp1, tiltp2, afe, feh, ndata= [], [], [], [], [], [], [], [], []
        tilt_err, tiltp1_err, tiltp2_err= [], [], []
        for ii in range(len(plotthis)):
            if velerrors:
                tilt_err.append(errors[ii][9])
                tiltp1_err.append(errors[ii][10])
#                tiltp2_err.append(errors[ii][11])
            tilt.append(plotthis[ii][11])
            tiltp1.append(plotthis[ii][12])
#            tiltp2.append(plotthis[ii][13])
            afe.append(plotthis[ii][1])
            feh.append(plotthis[ii][0])
            ndata.append(plotthis[ii][4])
            zmin.append(plotthis[ii][13])
            zmax.append(plotthis[ii][14])
            pivot.append(plotthis[ii][15])
        indxarray= numpy.array([True for ii in range(len(plotthis))],dtype='bool')
        tilt= numpy.array(tilt)[indxarray]
        tiltp1= numpy.array(tiltp1)[indxarray]
#        tiltp2= numpy.array(tiltp2)[indxarray]
        pivot= numpy.array(pivot)[indxarray]
        zmin= numpy.array(zmin)[indxarray]
        zmax= numpy.array(zmax)[indxarray]
        if velerrors:
            tilt_err= numpy.array(tilt_err)[indxarray]
            tiltp1_err= numpy.array(tiltp1_err)[indxarray]
#            tiltp2_err= numpy.array(tiltp2_err)[indxarray]
        afe= numpy.array(afe)[indxarray]
        feh= numpy.array(feh)[indxarray]
        ndata= numpy.array(ndata)[indxarray]
        #Process ndata
        ndata= ndata**.5
        ndata= ndata/numpy.median(ndata)*35.
        #ndata= numpy.log(ndata)/numpy.log(numpy.median(ndata))
        #ndata= (ndata-numpy.amin(ndata))/(numpy.amax(ndata)-numpy.amin(ndata))*25+12.
        if options.type.lower() == 'afe':
            plotc= afe
        elif options.type.lower() == 'feh':
            plotc= feh
        if options.subtype.lower() == 'zfunc':
            from selectFigs import _squeeze
            colormap = cm.jet
            #Set up plot
            yrange= [-30.,30.]
            ylabel=r'$\mathrm{tilt}(Z|R=8\,\mathrm{kpc})\ [\mathrm{degree}]$'
            bovy_plot.bovy_plot([-100.,-100.],[100.,100.],'k,',
                                xrange=[0,2700],yrange=yrange,
                                xlabel=r'$|z|\ [\mathrm{pc}]$',
                                ylabel=ylabel)
            #Calculate and plot all zfuncs
            for ii in numpy.random.permutation(len(afe)):
                if velerrors: #Don't plot if errors > 30%
                    if tilt_err[ii]/tilt[ii] > .2: continue
                ds= numpy.linspace(zmin[ii]*1000.,zmax[ii]*1000.,1001)/8000.
                thiszfunc= numpy.arctan(tilt[ii]+tiltp1[ii]*ds)/_DEGTORAD #+tiltp2[ii]*ds**2.
                pyplot.plot(numpy.linspace(zmin[ii]*1000.,1000*zmax[ii],1001),
                            thiszfunc,'-',
                            color=colormap(_squeeze(plotc[ii],vmin,vmax)),
                            lw=ndata[ii]/15.)
                if not options.nofatdots:
                    #Also plot pivot
                    pyplot.plot(1000.*pivot[ii],
                                numpy.arctan(tilt[ii]+tiltp1[ii]*pivot[ii]/8.\
                                    +tiltp2[ii]*(pivot[ii]/8.)**2.)/_DEGTORAD,
                                'o',ms=8.,mec='none',
                                color=colormap(_squeeze(plotc[ii],vmin,vmax)))
            #Add colorbar
            m = cm.ScalarMappable(cmap=cm.jet)
            m.set_array(plotc)
            m.set_clim(vmin=vmin,vmax=vmax)
            cbar= pyplot.colorbar(m,fraction=0.15)
            cbar.set_clim((vmin,vmax))
            cbar.set_label(zlabel)
    else:
        bovy_plot.bovy_print()
        bovy_plot.bovy_dens2d(plotthis.T,origin='lower',cmap='jet',
                              interpolation='nearest',
                              xlabel=r'$[\mathrm{Fe/H}]$',
                              ylabel=r'$[\alpha/\mathrm{Fe}]$',
                              zlabel=zlabel,
                              xrange=xrange,yrange=yrange,
                              vmin=vmin,vmax=vmax,
                              contours=False,
                              colorbar=True,shrink=0.78)
        bovy_plot.bovy_text(r'$\mathrm{median} = %.2f \pm %.2f$' % (numpy.median(plotthis[numpy.isfinite(plotthis)]),
                                                                    1.4826*numpy.median(numpy.fabs(plotthis[numpy.isfinite(plotthis)]-numpy.median(plotthis[numpy.isfinite(plotthis)])))),
                            bottom_left=True,size=14.)
    bovy_plot.bovy_end_print(options.plotfile)
    return None
def plot_distsystematic(options,args):
    if options.sample.lower() == 'g':
        if options.select.lower() == 'program':
            raw= read_gdwarfs(_GDWARFFILE,logg=True,ebv=True,sn=options.snmin)
        else:
            raw= read_gdwarfs(logg=True,ebv=True,sn=options.snmin)
    elif options.sample.lower() == 'k':
        if options.select.lower() == 'program':
            raw= read_kdwarfs(_KDWARFFILE,logg=True,ebv=True,sn=options.snmin)
        else:
            raw= read_kdwarfs(logg=True,ebv=True,sn=options.snmin)
    if not options.bmin is None:
        #Cut on |b|
        raw= raw[(numpy.fabs(raw.b) > options.bmin)]
    #Bin the data
    binned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe)
    if options.tighten:
        tightbinned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe,
                                 fehmin=-1.6,fehmax=0.5,afemin=-0.05,
                                 afemax=0.55)
    else:
        tightbinned= binned
    plotthis= numpy.zeros((tightbinned.npixfeh(),tightbinned.npixafe()))+numpy.nan
    #Run through the bins
    for ii in range(tightbinned.npixfeh()):
        for jj in range(tightbinned.npixafe()):
            data= binned(tightbinned.feh(ii),tightbinned.afe(jj))
            if len(data) < options.minndata:
                jj+= 1
                if jj == len(binned.afeedges)-1: 
                    jj= 0
                    ii+= 1
                    break
                continue               
            #Create XYZ and R, vxvyvz, cov_vxvyvz
            R= ((8.-data.xc)**2.+data.yc**2.)**0.5
            #Confine to R-range?
            if not options.rmin is None and not options.rmax is None:
                dataindx= (R >= options.rmin)*\
                    (R < options.rmax)
                data= data[dataindx]
                R= R[dataindx]
            XYZ= numpy.zeros((len(data),3))
            XYZ[:,0]= data.xc
            XYZ[:,1]= data.yc
            XYZ[:,2]= data.zc+_ZSUN
            vxvyvz= numpy.zeros((len(data),3))
            vxvyvz[:,0]= data.vxc
            vxvyvz[:,1]= data.vyc
            vxvyvz[:,2]= data.vzc
            """
            cov_vxvyvz= numpy.zeros((len(data),3,3))
            cov_vxvyvz[:,0,0]= data.vxc_err**2.
            cov_vxvyvz[:,1,1]= data.vyc_err**2.
            cov_vxvyvz[:,2,2]= data.vzc_err**2.
            cov_vxvyvz[:,0,1]= data.vxvyc_rho*data.vxc_err*data.vyc_err
            cov_vxvyvz[:,0,2]= data.vxvzc_rho*data.vxc_err*data.vzc_err
            cov_vxvyvz[:,1,2]= data.vyvzc_rho*data.vyc_err*data.vzc_err
            """
            cosphi= (8.-XYZ[:,0])/R
            sinphi= XYZ[:,1]/R
            sinbeta= XYZ[:,2]/numpy.sqrt(R*R+XYZ[:,2]*XYZ[:,2])
            cosbeta= R/numpy.sqrt(R*R+XYZ[:,2]*XYZ[:,2])
            ndata= len(data.ra)
            cov_pmradec= numpy.zeros((ndata,2,2))
            cov_pmradec[:,0,0]= data.pmra_err**2.
            cov_pmradec[:,1,1]= data.pmdec_err**2.
            cov_pmllbb= bovy_coords.cov_pmrapmdec_to_pmllpmbb(cov_pmradec,data.ra,
                                                              data.dec,degree=True)
            """
            vR= -vxvyvz[:,0]*cosphi+vxvyvz[:,1]*sinphi
            vT= vxvyvz[:,0]*sinphi+vxvyvz[:,1]*cosphi
            vz= vxvyvz[:,2]
            vxvyvz[:,0]= vR               
            vxvyvz[:,1]= vT
            for rr in range(len(XYZ[:,0])):
                rot= numpy.array([[cosphi[rr],sinphi[rr]],
                                  [-sinphi[rr],cosphi[rr]]])
                sxy= cov_vxvyvz[rr,0:2,0:2]
                sRT= numpy.dot(rot,numpy.dot(sxy,rot.T))
                cov_vxvyvz[rr,0:2,0:2]= sRT
            """
            #calculate x and y
            lb= bovy_coords.radec_to_lb(data.ra,data.dec,degree=True)
            lb*= _DEGTORAD
            tuu= 1.-numpy.cos(lb[:,1])**2.*numpy.cos(lb[:,0])**2.
            tuv= -0.5*numpy.cos(lb[:,1])**2.*numpy.sin(2.*lb[:,0])
            tuw= -0.5*numpy.cos(lb[:,0])*numpy.sin(2.*lb[:,1])
            tvv= 1.-numpy.cos(lb[:,1])**2.*numpy.sin(lb[:,0])**2.
            tvw= -0.5*numpy.sin(2.*lb[:,1])*numpy.sin(lb[:,0])
            tww= numpy.cos(lb[:,1])**2.
            #x= tuu*_VRSUN+tuv*vxvyvz[:,1]+tuw*vxvyvz[:,2]
            #y= -tww*_VZSUN+tuw*vxvyvz[:,0]+tvw*vxvyvz[:,1]
            x= -tuu*numpy.mean(vxvyvz[:,0])+tuv*vxvyvz[:,1]+tuw*vxvyvz[:,2]
            y= -tww*numpy.mean(vxvyvz[:,2])+tuw*vxvyvz[:,0]+tvw*vxvyvz[:,1]
            if options.type.lower() == 'u':
                corcorr=0.
                plotthis[ii,jj]= (numpy.mean(vxvyvz[:,0]*x)-numpy.mean(vxvyvz[:,0])*numpy.mean(x))/(numpy.var(x)+numpy.mean(tuv**2.+tuw**2.)*numpy.var(vxvyvz[:,0]))
            elif options.type.lower() == 'meanu':
                plotthis[ii,jj]= numpy.mean(vxvyvz[:,0])
            elif options.type.lower() == 'meanw':
                plotthis[ii,jj]= numpy.mean(vxvyvz[:,2])
            else:
                corcorr= 0.25*numpy.mean(2.*sinbeta*cosbeta*numpy.sin(2.*lb[:,1])*numpy.cos(lb[:,0])*cosphi)*(numpy.var(vxvyvz[:,0])-numpy.var(vxvyvz[:,2]))\
                    -0.25*numpy.mean(2.*sinbeta*cosbeta*numpy.sin(2.*lb[:,1])*numpy.sin(lb[:,0])*sinphi)*(numpy.var(vxvyvz[:,0])-numpy.var(vxvyvz[:,2]))\
                    +0.25*numpy.mean(numpy.sin(lb[:,1])**2.*(cov_pmllbb[:,1,1]*data.dist**2.*4.74**2.-data.vr_err**2.))
                plotthis[ii,jj]= (numpy.mean(vxvyvz[:,2]*y)-numpy.mean(vxvyvz[:,2])*numpy.mean(y)-corcorr)/(numpy.var(y)+numpy.mean(tvw**2.+tuw**2.)*numpy.var(vxvyvz[:,2]))
            #print ii, jj, plotthis[ii,jj], corcorr, numpy.mean(vxvyvz[:,2]*y)-numpy.mean(vxvyvz[:,2])*numpy.mean(y)-corcorr
            jj+= 1
            if jj == len(binned.afeedges)-1: 
                jj= 0
                ii+= 1
            if jj == 0: #this means we've reset the counter 
                break
    #print plotthis
    #Set up plot
    if options.type.lower() == 'meanu':
        vmin, vmax= -20.,20.
        zlabel=r'$\mathrm{mean}\ U$'
    elif options.type.lower() == 'meanw':
        vmin, vmax= -20.,20.
        zlabel=r'$\mathrm{mean}\ W$'
    else:
        vmin, vmax= -0.2,0.2
        zlabel=r'$\mathrm{fractional\ distance\ overestimate}$'
    if options.tighten:
        xrange=[-1.6,0.5]
        yrange=[-0.05,0.55]
    else:
        xrange=[-2.,0.5]
        yrange=[-0.2,0.6]
    bovy_plot.bovy_print()
    bovy_plot.bovy_dens2d(plotthis.T,origin='lower',cmap='jet',
                          interpolation='nearest',
                          xlabel=r'$[\mathrm{Fe/H}]$',
                          ylabel=r'$[\alpha/\mathrm{Fe}]$',
                          zlabel=zlabel,
                          xrange=xrange,yrange=yrange,
                          vmin=vmin,vmax=vmax,
                          contours=False,
                          colorbar=True,shrink=0.78)
    bovy_plot.bovy_text(r'$\mathrm{median} = %.2f$' % (numpy.median(plotthis[numpy.isfinite(plotthis)])),
                        bottom_left=True,size=14.)
    bovy_plot.bovy_end_print(options.outfilename)
    return None
Exemple #12
0
def collateFits(options,args):
    if options.sample.lower() == 'g':
        if options.select.lower() == 'program':
            raw= read_gdwarfs(_GDWARFFILE,logg=True,ebv=True,sn=True)
        else:
            raw= read_gdwarfs(logg=True,ebv=True,sn=True)
    elif options.sample.lower() == 'k':
        if options.select.lower() == 'program':
            raw= read_kdwarfs(_KDWARFFILE,logg=True,ebv=True,sn=True)
        else:
            raw= read_kdwarfs(logg=True,ebv=True,sn=True)
    #Bin the data
    binned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe)
    #Savefiles
    if os.path.exists(args[0]):#Load density fits
        savefile= open(args[0],'rb')
        densfits= pickle.load(savefile)
        savefile.close()
    else:
        raise IOError("density fits file not included")
    if os.path.exists(args[1]):#Load density fits
        savefile= open(args[1],'rb')
        denssamples= pickle.load(savefile)
        savefile.close()
    else:
        raise IOError("density samples file not included")
    if os.path.exists(args[2]):#Load density fits
        savefile= open(args[2],'rb')
        mass= pickle.load(savefile)
        savefile.close()
    else:
        raise IOError("masses file not included")
    #if os.path.exists(args[3]):#Load density fits
    #    savefile= open(args[3],'rb')
    #    masssamples= pickle.load(savefile)
    #    savefile.close()
    #else:
    #    raise IOError("mass samples file not included")
    if os.path.exists(args[4]):#Load density fits
        savefile= open(args[4],'rb')
        velfits= pickle.load(savefile)
        savefile.close()
    else:
        raise IOError("vertical velocity file not included")
    if os.path.exists(args[5]):#Load density fits
        savefile= open(args[5],'rb')
        velsamples= pickle.load(savefile)
        savefile.close()
    else:
        raise IOError("vertical velocity samples  file not included")
    if os.path.exists(args[6]):#Load density fits
        savefile= open(args[6],'rb')
        velrfits= pickle.load(savefile)
        savefile.close()
    else:
        raise IOError("radial velocity file not included")
    if os.path.exists(args[7]):#Load density fits
        savefile= open(args[7],'rb')
        velrsamples= pickle.load(savefile)
        savefile.close()
    else:
        raise IOError("radial velocity samples  file not included")
    nrecs= len([r for r in densfits if not r is None])
    out= numpy.recarray(nrecs,dtype=[('feh',float),
                                     ('afe',float),
                                     ('hz',float),
                                     ('hr',float),
                                     ('bc',float),
                                     ('mass',float),
                                     ('sz',float),
                                     ('hsz',float),
                                     ('p1',float),
                                     ('p2',float),
                                     ('sr',float),
                                     ('hsr',float),
                                     ('zmin',float),
                                     ('zmax',float),
                                     ('zmedian',float),
                                     ('hz_err',float),
                                     ('hr_err',float),
                                     ('mass_err',float),
                                     ('sz_err',float),
                                     ('hsz_err',float),
                                     ('p1_err',float),
                                     ('p2_err',float),
                                     ('szp1_corr',float),
                                     ('szp2_corr',float),
                                     ('szhsz_corr',float),
                                     ('p1hsz_corr',float),
                                     ('p2hsz_corr',float),
                                     ('p1p2_corr',float),
                                     ('sr_err',float)])
                                     
    nout= 0
    #Start filling it up
    for ii in range(binned.npixfeh()):
        for jj in range(binned.npixafe()):
            data= binned(binned.feh(ii),binned.afe(jj))
            fehindx= binned.fehindx(binned.feh(ii))#Map onto regular binning
            afeindx= binned.afeindx(binned.afe(jj))#Unnecessary here
            if afeindx+fehindx*binned.npixafe() >= len(densfits):
                continue
            thisdensfit= densfits[afeindx+fehindx*binned.npixafe()]
            thisdenssamples= denssamples[afeindx+fehindx*binned.npixafe()]
            thismass= mass[afeindx+fehindx*binned.npixafe()]
            #thismasssamples= masssamples[afeindx+fehindx*binned.npixafe()]
            thisvelfit= velfits[afeindx+fehindx*binned.npixafe()]
            thesevelsamples= velsamples[afeindx+fehindx*binned.npixafe()]
            thisvelrfit= velrfits[afeindx+fehindx*binned.npixafe()]
            thesevelrsamples= velrsamples[afeindx+fehindx*binned.npixafe()]
            if thisdensfit is None:
                continue
            if len(data) < options.minndata:
                continue
            out['feh'][nout]= binned.feh(ii)
            out[nout]['afe']= binned.afe(jj)
            if options.densmodel.lower() == 'hwr' \
                    or options.densmodel.lower() == 'dblexp':
                out[nout]['hz']= numpy.exp(thisdensfit[0])*1000.
                if options.densmodel.lower() == 'dblexp':
                    out[nout]['hr']= numpy.exp(-thisdensfit[1])
                else:
                    out[nout]['hr']= numpy.exp(thisdensfit[1])
                out[nout]['bc']= thisdensfit[2]
                theseerrors= []
                xs= numpy.array([s[0] for s in thisdenssamples])
                theseerrors.append(0.5*(-numpy.exp(numpy.mean(xs)-numpy.std(xs))+numpy.exp(numpy.mean(xs)+numpy.std(xs))))
                out[nout]['hz_err']= theseerrors[0]*1000.
                if options.densmodel.lower() == 'dblexp':
                    xs= numpy.array([-s[1] for s in thisdenssamples])
                else:
                    xs= numpy.array([s[1] for s in thisdenssamples])
                theseerrors.append(0.5*(-numpy.exp(numpy.mean(xs)-numpy.std(xs))+numpy.exp(numpy.mean(xs)+numpy.std(xs))))
                out[nout]['hr_err']= theseerrors[1]
            #mass
            if options.sample.lower() == 'k':
                out[nout]['mass']= numpy.nan
            else:
                out[nout]['mass']= thismass/10.**6.
            #out[nout]['mass_err']= numpy.std(numpy.array(thismasssamples)/10.**6.)
            #Velocities
            if options.velmodel.lower() == 'hwr':
                out[nout]['sz']= numpy.exp(thisvelfit[1])
                out[nout]['hsz']= numpy.exp(thisvelfit[4])
                out[nout]['sr']= numpy.exp(thisvelrfit[1])
                out[nout]['hsr']= numpy.exp(thisvelrfit[4])
                out[nout]['p1']= thisvelfit[2]
                out[nout]['p2']= thisvelfit[3]
                zsorted= sorted(numpy.fabs(data.zc+_ZSUN))
                out[nout]['zmin']= zsorted[int(numpy.ceil(0.16*len(zsorted)))]*1000.
                out[nout]['zmax']= zsorted[int(numpy.floor(0.84*len(zsorted)))]*1000.
                out[nout]['zmedian']= numpy.median(numpy.fabs(data.zc)-_ZSUN)*1000.
                #Errors
                xs= numpy.array([s[1] for s in thesevelsamples])
                out[nout]['sz_err']= 0.5*(-numpy.exp(numpy.mean(xs)-numpy.std(xs))+numpy.exp(numpy.mean(xs)+numpy.std(xs)))
                xs= numpy.array([s[4] for s in thesevelsamples])
                out[nout]['hsz_err']= 0.5*(-numpy.exp(numpy.mean(xs)-numpy.std(xs))+numpy.exp(numpy.mean(xs)+numpy.std(xs)))
                xs= numpy.array([s[2] for s in thesevelsamples])
                out[nout]['p1_err']= numpy.std(xs)
                xs= numpy.array([s[3] for s in thesevelsamples])
                out[nout]['p2_err']= numpy.std(xs)
                xs= numpy.exp(numpy.array([s[1] for s in thesevelsamples]))
                ys= numpy.array([s[2] for s in thesevelsamples])
                out[nout]['szp1_corr']= numpy.corrcoef(xs,ys)[0,1]
                ys= numpy.array([s[3] for s in thesevelsamples])
                out[nout]['szp2_corr']= numpy.corrcoef(xs,ys)[0,1]
                xs= numpy.array([s[2] for s in thesevelsamples])
                out[nout]['p1p2_corr']= numpy.corrcoef(xs,ys)[0,1]
                xs= numpy.exp(numpy.array([s[4] for s in thesevelsamples]))
                ys= numpy.exp(numpy.array([s[1] for s in thesevelsamples]))
                out[nout]['szhsz_corr']= numpy.corrcoef(xs,ys)[0,1]
                ys= numpy.array([s[2] for s in thesevelsamples])
                out[nout]['p1hsz_corr']= numpy.corrcoef(xs,ys)[0,1]
                ys= numpy.array([s[3] for s in thesevelsamples])
                out[nout]['p2hsz_corr']= numpy.corrcoef(xs,ys)[0,1]
                xs= numpy.array([s[1] for s in thesevelrsamples])
                out[nout]['sr_err']= 0.5*(-numpy.exp(numpy.mean(xs)-numpy.std(xs))+numpy.exp(numpy.mean(xs)+numpy.std(xs)))
            nout+= 1
    #Write output
    fitsio.write(options.outfile,out,clobber=True)