def plotit(ax, x, y, z, label):
     plots.plotc(ax, x, y - x, z, xt=label, yt='inferred-labelled')
     lims = ax.get_xlim()
     ax.plot(lims, [0., 0.], c="#666666", zorder=-1, linestyle=":")
     mean, rms = np.nanmean(y - x), np.nanstd(y - x)
     title = "{}: ({:.2f}, {:.2f})".format(label, mean, rms)
     ax.set_title(title)
Exemple #2
0
def m67(allstar, out='./'):
    """ M67 abundances
    """
    gd = apselect.select(allstar[1].data, badval='STAR_BAD')
    m67 = np.array(
        apselect.clustmember(allstar[1].data[gd],
                             'M67',
                             raw=True,
                             pm=True,
                             dist=True))
    m67 = gd[m67]
    els = allstar[3].data['ELEM_SYMBOL'][0]
    grid = []
    for iel, el in enumerate(els):
        fig, ax = plots.multi(1, 2, figsize=(6, 4), hspace=0.001)
        plots.plotc(ax[0],
                    allstar[1].data['LOGG'][m67],
                    allstar[1].data['X_M'][m67, iel],
                    allstar[1].data['TEFF'][m67],
                    yerr=allstar[1].data['X_M_ERR'][m67, iel],
                    xr=[6, 0],
                    yr=[-0.75, 0.75],
                    xt='log g',
                    yt='[' + el + '/M]')
        plots.plotc(ax[1],
                    allstar[1].data['LOGG'][m67],
                    allstar[1].data['X_H'][m67, iel],
                    allstar[1].data['TEFF'][m67],
                    yerr=allstar[1].data['X_H_ERR'][m67, iel],
                    xr=[6, 0],
                    yr=[-0.75, 0.75],
                    xt='log g',
                    yt='[' + el + '/H]')
        ax[0].text(0.1,
                   0.9,
                   'rms: {:8.3f}'.format(allstar[1].data['X_M'][m67,
                                                                iel].std()),
                   transform=ax[0].transAxes)
        ax[1].text(0.1,
                   0.9,
                   'rms: {:8.3f}'.format(allstar[1].data['X_H'][m67,
                                                                iel].std()),
                   transform=ax[1].transAxes)
        outfile = out + 'm67_{:s}.png'.format(el.strip())
        fig.savefig(outfile)
        plt.close(fig)
        grid.append([os.path.basename(outfile)])
    html.htmltab(grid, file=out + 'm67.html', ytitle=els)
Exemple #3
0
def plotparams(a, title=None, hard=None):
    """ Plot parameters vs Teff
    """
    fig, ax = plots.multi(1, 8, hspace=0.001)

    paramnames, tagnames, flagnames = params()

    for i in range(8):
        plots.plotc(ax[i],
                    a['FPARAM'][:, 0],
                    a['FPARAM'][:, i],
                    a['FPARAM'][:, 3],
                    yt=tagnames[i],
                    xt='Teff')
    if title is not None: fig.suptitle(title)
    if hard is not None: fig.savefig(hard)
Exemple #4
0
def dr13dr12() :
    '''
    compare dr13 dr12 Teff
    '''

    dr12load=apload.ApLoad(dr='dr12')
    dr12=dr12load.allStar()[1].data
    dr13load=apload.ApLoad(dr='dr13')
    dr13=dr13load.allStar()[1].data
    i1,i2 = match.match(dr12['APOGEE_ID'],dr13['APOGEE_ID'])
    dr12=dr12[i1]
    dr13=dr13[i2]

    fig,ax=plots.multi(1,2,hspace=0.001,wspace=0.001)
    plots.plotc(ax[0],dr13['M_H'],dr13['TEFF']-dr12['TEFF'],dr13['TEFF'],xr=[-2.5,0.75],yr=[-300,300],zr=[3500,5000])

    plots.plotc(ax[1],dr13['TEFF'],dr13['TEFF']-dr12['TEFF'],dr13['M_H'],xr=[6500,3000],yr=[-300,300],zr=[-2,0.5])
Exemple #5
0
def datamap(frameid=22600042,
            waveid=22600042,
            psfid=22600030,
            lamp='UNe',
            fibers=np.arange(50, 300, 50)):

    lines = ascii.read(os.environ['APOGEEREDUCE_DIR'] + '/lib/linelists/' +
                       lamp + '.vac.apogee')
    pdb.set_trace()
    bright = np.where((lines['FLUX'] > 500) & (lines['USEWAVE'] == 1))[0]
    wbright = lines['WAVE'][bright]
    data = load.ap2D(frameid)
    psf = load.apEPSF(psfid)
    wave = load.apWave(waveid)

    fig, ax = plots.multi(1, 3)
    lsfmap = np.zeros([300, 2048, 3])
    for ichip, chip in enumerate(['a', 'b', 'c']):
        x = []
        y = []
        z = []
        for w in wbright:
            for fiber in fibers:
                col = np.abs(wave[chip][2].data[fiber, :] - w).argmin()
                if (col > 50) and (col < 2000):
                    row = psf[chip][fiber].data['CENT'][0, col]
                    g = image.gfit(data[chip][1].data,
                                   col,
                                   row,
                                   sub=False,
                                   pafixed=True,
                                   size=3)
                    print(chip, w, fiber, col, row,
                          g[0].x_stddev.value * 2.354,
                          g[0].y_stddev.value * 2.354)
                    lsfmap[fiber, col, ichip] = g[0].x_stddev.value * 2.354
                    x.append(col)
                    y.append(row)
                    z.append(g[0].x_stddev.value * 2.354)
                    pdb.set_trace()
        x = np.array(x)
        y = np.array(y)
        z = np.array(z)
        plots.plotc(ax[ichip], x, y, z, zr=[2, 4], size=50)
        pdb.set_trace()
Exemple #6
0
def kurucz_marcs():

    dr13load = apload.ApLoad(dr='dr13')
    dr13 = dr13load.allStar()[1].data
    gd = np.where(dr13['SNR'] > 150)[0]
    dr13 = dr13[gd]

    dr13load.aspcap = 'l30g'
    dr13_marcs = dr13load.allStar()[1].data
    gd = np.where(dr13_marcs['SNR'] > 150)[0]
    dr13_marcs = dr13_marcs[gd]

    fig, ax = plots.multi(2, 1, wspace=0.001)
    axim = plots.plotc(ax[0],
                       dr13['FPARAM'][:, 0],
                       dr13['FPARAM'][:, 1],
                       dr13['FPARAM'][:, 3],
                       xr=[4200, 3000],
                       yr=[5, -1],
                       zr=[-2, 0.5],
                       xt=r'T$_{\rm eff}$',
                       yt='log g',
                       rasterized=True)
    plots.plotc(ax[1],
                dr13_marcs['FPARAM'][:, 0],
                dr13_marcs['FPARAM'][:, 1],
                dr13_marcs['FPARAM'][:, 3],
                xr=[4200, 3000],
                yr=[5, -1],
                zr=[-2, 0.5],
                xt=r'T$_{\rm eff}$',
                rasterized=True)
    for iax in range(2):
        for item in (ax[iax].get_xticklabels() + ax[iax].get_yticklabels()):
            item.set_fontsize(10)
        ax[iax].xaxis.label.set_size(10)
        ax[iax].yaxis.label.set_size(10)

    cbaxes = fig.add_axes([0.91, 0.1, 0.01, 0.8])
    cb = plt.colorbar(axim, cax=cbaxes)
    cb.set_label('[M/H]')
    cbaxes.tick_params(axis='both', labelsize=10)
    cbaxes.yaxis.label.set_size(10)

    fig.savefig('kurucz_marcs.pdf')
Exemple #7
0
def dr14_vmacro(teff,
                logg,
                mh,
                meanfib,
                v,
                vr,
                fit1d,
                fit2d,
                xr=[5500, 3500],
                yr=[5, 0],
                vt='vmicro'):
    ax = fig.add_subplot(3, 2, 5)
    plots.plotc(ax,
                logg,
                10.**v,
                mh,
                xr=[0, 5],
                yr=vr,
                zr=[-2.5, 0.5],
                size=10,
                colorbar=True,
                xt='log g',
                yt=vt,
                zt='[M/H]')
Exemple #8
0
def fit_vmicro(data,
               teffrange=[3550, 5500],
               mhrange=[-2.5, 1],
               loggrange=[-0.5, 4.75],
               vrange=[0, 4],
               vmrange=[0, 6],
               maxerr=0.1,
               degree=1,
               reject=0,
               apokasc='APOKASC_cat_v3.6.0.fits',
               nopersist=False,
               out='vmicro',
               func=None):
    """ 
    Fit microturbulence relation  with 1D f(log g) and 2D f(Teff, logg) fits, plots

    Args:
        data : calib structure

    Keyword args :
        degree : degree of fit (default=1)
        mhrange : range of [M/H] (default=[-1,1])
        loggrange : range of log g (default=[-1,3.8])
        maxerr : maximum uncertainty in vmicro
        vrange  : scaling range for vmacro plot, vrange[1] sets maximum good vmacro

    Returns:
        fit1d, fit2d : 1D and 2D polynomial fits
    """
    vmicro = data['FPARAM'][:, 2]
    vmacro = data['FPARAM'][:, 7]
    # fix locked vmacro by hand (bad!)
    #j=np.where(np.isclose(vmacro,1.))[0]
    #vmacro[j] = 0.6
    teff = data['FPARAM'][:, 0]
    logg = data['FPARAM'][:, 1]
    mh = data['FPARAM'][:, 3]
    try:
        meanfib = data['MEANFIB']
    except:
        meanfib = 0. * vmicro
    try:
        ninst = data['NINST'][:, 1] - data['NINST'][:, 2]
    except:
        ninst = data['NVISITS'] * 0

    apokasc = fits.open(os.environ['APOGEE_DIR'] + '/data/apokasc/' +
                        apokasc)[1].data
    i1, i2 = match.match(data['APOGEE_ID'], apokasc['2MASS_ID'])
    rgb = np.where(apokasc['CONS_EVSTATES'][i2] == 'RGB')[0]
    rc = np.where(apokasc['CONS_EVSTATES'][i2] == 'RC')[0]
    type = mh * 0.
    type[i1[rgb]] = 1
    type[i1[rc]] = -1

    print('mhrange: ', mhrange)
    print('teffrange: ', teffrange)
    print('loggrange: ', loggrange)
    print('vmacrorange: ', vmrange)
    print('vmicrorange: ', vrange)
    print('maxerr: ', maxerr)
    print('nopersist: ', nopersist)
    print('reject: ', reject)
    gd = np.where((mh > mhrange[0]) & (mh < mhrange[1]) & (logg > loggrange[0])
                  & (logg < loggrange[1]) & (teff > teffrange[0])
                  & (teff < teffrange[1]) & (10.**vmacro > vmrange[0])
                  & (10.**vmacro < vmrange[1])
                  & (np.sqrt(data['FPARAM_COV'][:, 2, 2]) < maxerr)
                  & (10.**vmicro > vrange[0]) & (10.**vmicro < vrange[1]))[0]
    if nopersist:
        j = np.where((data['STARFLAG'][gd] & bitmask.persist()) == 0)[0]
        gd = gd[j]

    # remove non-1st generation GC stars
    gcstars = ascii.read(os.environ['APOGEE_DIR'] +
                         '/data/calib/gc_szabolcs.dat')
    bd = np.where(gcstars['pop'] != 1)[0]
    gd = [x for x in gd if data[x]['APOGEE_ID'] not in gcstars['id'][bd]]

    # 1D plots a f(log g)
    #fig,ax = plots.multi(2,3,figsize=(15,15))
    #fit1d = fit.fit1d(logg[gd], vmicro[gd],degree=degree,reject=reject,plot=ax[0,0],ydata=mh[gd],log=True,xt='log g',yt='vmicro ([M/H])',yr=[-2.5,0.5],colorbar=True,zt='[M/H]',xr=[0,4.5])
    ## plot ALL points (even outside of fit range)
    #plots.plotc(ax[0,0],logg,10.**vmicro,mh,zr=[-2.5,0.5],xr=[-1,5],size=1)
    #
    #    junk = fit.fit1d(logg[gd], vmicro[gd],degree=degree,reject=reject,plot=ax[0,1],ydata=teff[gd],log=True,xt='log g',yt='vmicro',yr=[3500,5500],pfit=fit1d,colorbar=True,zt='Teff',xr=[0,4.5])
    #    plots.plotc(ax[0,1],logg,10.**vmicro,teff,zr=[3500,5500],xr=[-1,5],size=1)
    #    junk = fit.fit1d(logg[gd], vmicro[gd],degree=degree,reject=reject,plot=ax[1,0],ydata=meanfib[gd],log=True,xt='log g',yt='vmicro',yr=[0,300],pfit=fit1d,colorbar=True,zt='mean fiber',xr=[0,4.5])
    #    plots.plotc(ax[1,0],logg,10.**vmicro,meanfib,zr=[0,300],xr=[-1,5],size=1)
    #    junk = fit.fit1d(logg[gd], vmicro[gd],degree=degree,reject=reject,plot=ax[1,1],ydata=10.**vmacro[gd],log=True,xt='log g',yt='vmicro',yr=[0,15],pfit=fit1d,colorbar=True,zt='vmacro',xr=[0,4.5])
    #    plots.plotc(ax[1,1],logg,10.**vmicro,10.**vmacro,zr=[0,15],xr=[-1,5],size=1)
    #
    #    junk = fit.fit1d(logg[gd], vmicro[gd],degree=degree,reject=reject,plot=ax[2,0],ydata=ninst[gd],log=True,xt='log g',yt='vmicro',yr=[-1,1],pfit=fit1d,colorbar=True,zt='ninst1-ninst2',xr=[0,4.5])
    #    plots.plotc(ax[2,0],logg,10.**vmicro,ninst,zr=[-1,1],xr=[-1,5],size=1)
    #    #plots.plotc(ax[3,1],logg[gd1],10.**vmicro[gd1],mh[gd1],zr=[-2.5,0.5],xr=[-1,5])
    #    # 2d plot
    #    #junk = fit.fit1d(logg, vmicro,degree=degree,reject=reject,plot=ax[2,0],plot2d=True,ydata=teff,log=True,yt='Teff',xt='log g',xr=[5,-0.5],yr=[6000,3000],pfit=fit1d,zr=[0,4])
    #    junk = fit.fit1d(logg[gd], vmicro[gd],degree=degree,reject=reject,plot=ax[2,1],ydata=type[gd],log=True,xt='log g',yt='vmicro',yr=[-1,1],pfit=fit1d,colorbar=True,zt='RGB/RC',xr=[0,4.5])
    #    plots.plotc(ax[2,0],logg,10.**vmicro,ninst,zr=[-1,1],xr=[-1,5],size=1)
    #
    #
    #    # plot with DR13 relation
    #    #dr13fit=models.Polynomial1D(degree=3)
    #    #dr13fit.parameters=[0.226,-0.0228,0.0297,-0.013]
    #    #junk = fit.fit1d(logg[gd], vmicro[gd],degree=degree,reject=reject,plot=ax[2,1],ydata=mh[gd],pfit=dr13fit,log=True,xt='log g',yt='vmicro',colorbar=True,zt='[M/H]',yr=[-2.5,0.5])
    #    #plots.plotc(ax[2,0],logg,10.**vmicro,mh,zr=[-2.5,0.5],xr=[-1,5],size=1)
    ##
    #    fig.tight_layout()
    #    fig.savefig(out+'.png')
    #
    #    fig,ax=plots.multi(1,3,figsize=(6,10))
    #    plots.plotc(ax[0],teff[gd],10.**vmicro[gd]-10.**fit1d(logg[gd]),logg[gd],xt='Teff',yt=r'$\Delta vmicro$',xr=[3500,6000],yr=[-1,1],zr=[0,5],zt='log g',size=5)
    #    plots.plotc(ax[0],teff,10.**vmicro-10.**fit1d(logg),logg,xt='Teff',yt=r'$\Delta vmicro$',xr=[3500,6000],yr=[-1,1],zr=[0,5],zt='log g',colorbar=True,size=1)
    #    plots.plotc(ax[1],mh[gd],10.**vmicro[gd]-10.**fit1d(logg[gd]),teff[gd],xt='[M/H]',yt=r'$\Delta vmicro$',xr=[-2.5,0.5],yr=[-1,1],zr=[3500,5500],zt='Teff',size=5)
    #    plots.plotc(ax[1],mh,10.**vmicro-10.**fit1d(logg),teff,xt='[M/H]',yt=r'$\Delta vmicro$',xr=[-2.5,0.5],yr=[-1,1],zr=[3500,5500],zt='Teff',colorbar=True,size=1)
    #    plots.plotc(ax[2],meanfib[gd],10.**vmicro[gd]-10.**fit1d(logg[gd]),logg[gd],xt='mean fiber',yt=r'$\Delta vmicro$',xr=[0,300],yr=[-1,1],zr=[0,5],zt='log g',size=5)
    #    plots.plotc(ax[2],meanfib,10.**vmicro-10.**fit1d(logg),logg,xt='mean fiber',yt=r'$\Delta vmicro$',xr=[0,300],yr=[-1,1],zr=[0,5],zt='log g',colorbar=True,size=1)
    #    fig.tight_layout()
    #    fig.savefig(out+'_res.png')
    #
    #fig,ax=plots.multi(1,2)
    #xr=[
    #junk = fit.fit1d(logg[gd], vmicro[gd],degree=degree,plot=ax[0,0],plot2d=True,yr=[0,5],xr=[0,5],xt='Teff',yt='log g')
    #y, x = np.mgrid[yr[1]:yr[0]:200j, xr[1]:xr[0]:200j]

    # 2D plots a f(teff, logg)
    fig, ax = plots.multi(1, 5, figsize=(6, 12))
    #fit2d = fit.fit2d(logg[gd], mh[gd], vmicro[gd],degree=degree,plot=ax[0],yr=[-2.5,1],xr=[0,5],xt='logg',yt='[M/H]',log=True)
    if func == vm1t:
        print('gd:', len(gd))
        x1 = logg
        xr = [-0.5, 5.5]
        xt = 'logg'
        x2 = mh
        yr = [-2, 0.5]
        yt = '[M/H]'
        #x3=logg
        #x3_0=1.0
        #yr=[-0.5,5.5]
        #yt='log g'
        des = func(x1[gd], x2[gd])
        soln, inv = fit.linear(vmicro[gd], des)
        y, x = np.mgrid[yr[0]:yr[1]:200j, xr[0]:xr[1]:200j]
        ax[0].imshow(10.**func(x, y, soln=soln),
                     extent=[xr[0], xr[1], yr[0], yr[1]],
                     aspect='auto',
                     vmin=0.,
                     vmax=3.,
                     origin='lower')
        plots.plotc(ax[0],
                    x1,
                    x2,
                    10.**vmicro,
                    xr=xr,
                    yr=yr,
                    zr=[0., 3.],
                    xt=xt,
                    yt=yt,
                    zt='vmicro',
                    size=5,
                    linewidth=0)
        plots.plotc(ax[0],
                    x1[gd],
                    x2[gd],
                    10.**vmicro[gd],
                    xr=xr,
                    yr=yr,
                    zr=[0., 3.],
                    xt=xt,
                    yt=yt,
                    zt='vmicro',
                    colorbar=True,
                    size=15,
                    linewidth=0.2)
        cs = ax[0].contour(x, y, 10**func(x, y, soln=soln), colors='k')
        ax[0].clabel(cs)

    else:
        des = func(logg[gd], mh[gd])
        soln, inv = fit.linear(vmicro[gd], des)
        y, x = np.mgrid[-2.5:1.:200j, 0:5.:200j]
        ax[0].imshow(10.**func(x, y, soln=soln),
                     extent=[0, 5, -2.5, 1.],
                     aspect='auto',
                     vmin=0.,
                     vmax=3.,
                     origin='lower')
        plots.plotc(ax[0],
                    logg[gd],
                    mh[gd],
                    10.**vmicro[gd],
                    xr=[-0.5, 5],
                    yr=[-2.5, 1],
                    zr=[0., 3.],
                    xt='log g',
                    yt='[M/H]',
                    zt='vmicro',
                    colorbar=True,
                    size=15,
                    linewidth=1)
        cs = ax[0].contour(x, y, 10**func(x, y, soln=soln), colors='k')
        ax[0].clabel(cs, color='k')
        # create independent variable grid for model and display
    plots.plotc(ax[1],
                logg[gd],
                10.**vmicro[gd],
                mh[gd],
                xt='logg',
                yt=r'$vmicro$',
                xr=[-0.5, 5.],
                yr=[0, 4],
                zr=[-2., 0.5],
                zt='[M/H]',
                size=5)
    plots.plotc(ax[1],
                logg,
                10.**vmicro,
                mh,
                xt='log g',
                yt=r'$vmicro$',
                xr=[-0.5, 5.],
                yr=[0, 4],
                zr=[-2., 0.5],
                zt='[M/H]',
                colorbar=True,
                size=1)
    plots.plotc(ax[2],
                logg[gd],
                10.**vmicro[gd],
                teff[gd],
                xt='logg',
                yt=r'$vmicro$',
                xr=[-0.5, 5.],
                yr=[0, 4],
                zr=[3000, 5500],
                zt='Teff',
                size=5)
    plots.plotc(ax[2],
                logg,
                10.**vmicro,
                teff,
                xt='log g',
                yt=r'$vmicro$',
                xr=[-0.5, 5.],
                yr=[0, 4],
                zr=[3000, 5500],
                zt='Teff',
                colorbar=True,
                size=1)
    plots.plotc(ax[3],
                logg[gd],
                10.**vmicro[gd] - 10.**func(x1[gd], x2[gd], soln=soln),
                teff[gd],
                xt='logg',
                yt=r'$\Delta vmicro$',
                xr=[-0.5, 5.],
                yr=[-1, 1],
                zr=[3000, 5500],
                zt='Teff',
                size=5)
    plots.plotc(ax[3],
                logg,
                10.**vmicro - 10.**func(x1, x2, soln=soln),
                teff,
                xt='log g',
                yt=r'$\Delta vmicro$',
                xr=[-0.5, 5.],
                yr=[-1, 1],
                zr=[3000, 5500],
                zt='Teff',
                colorbar=True,
                size=1)
    plots.plotc(ax[4],
                logg[gd],
                10.**vmicro[gd] - 10.**func(x1[gd], x2[gd], soln=soln),
                mh[gd],
                xt='logg',
                yt=r'$\Delta vmicro$',
                xr=[-0.5, 5.],
                yr=[-1, 1],
                zr=[-2.0, 0.5],
                zt='[M/H]',
                size=5)
    plots.plotc(ax[4],
                logg,
                10.**vmicro - 10.**func(x1, x2, soln=soln),
                mh,
                xt='log g',
                yt=r'$\Delta vmicro$',
                xr=[-0.5, 5.],
                yr=[-1, 1],
                zr=[-2.0, 0.5],
                zt='[M/H]',
                colorbar=True,
                size=1)

    fig.tight_layout()
    fig.savefig(out + '_res.png')

    #summary plots
    #plot(teff, logg, mh, meanfib, vmicro, vrange, fit1d, fit2d, vt='vmicro')

    print('{', end="")
    for i in range(len(soln)):
        print('{:12.8f}'.format(soln[i]), end="")
    print('}')

    #    return fit1d
    return
Exemple #9
0
def dr_compare():
    # load the DRs, select stars with SN>150
    dr12load = apload.ApLoad(dr='dr12')
    dr12 = dr12load.allStar()[1].data
    gd = np.where(dr12['SNR'] > 150)[0]
    dr12 = dr12[gd]

    dr13load = apload.ApLoad(dr='dr13')
    dr13 = dr13load.allStar()[1].data
    gd = np.where(dr13['SNR'] > 150)[0]
    dr13 = dr13[gd]

    dr14load = apload.ApLoad(dr='dr14')
    dr14 = dr14load.allStar()[1].data
    gd = np.where(dr14['SNR'] > 150)[0]
    dr14 = dr14[gd]
    c = apload.allStar()[3].data

    # match them
    m1a, m2a = match.match(dr12['APOGEE_ID'], dr13['APOGEE_ID'])
    m1b, m2b = match.match(dr12['APOGEE_ID'], dr14['APOGEE_ID'])
    m1c, m2c = match.match(dr13['APOGEE_ID'], dr14['APOGEE_ID'])

    # parameter figures
    figu, axu = plots.multi(3, 7, hspace=0.001, wspace=0.001)
    figc, axc = plots.multi(3, 7, hspace=0.001, wspace=0.001)

    tit = [
        r'T$_{\rm eff}$', 'log g', r'V$_{\rm micro}$', '[M/H]', '[C/M]',
        '[N/M]', r'[$\alpha$/M]'
    ]
    for iparam in range(7):

        print(iparam)
        for iy, param in enumerate(['FPARAM', 'PARAM']):
            if iy == 0:
                ax = axu
            else:
                ax = axc
            yt = r'$\Delta$' + tit[iparam]
            if iparam == 6: xt = r'T$_{\rm eff}$'
            else: xt = None
            if iparam == 0:
                ax[iparam, 0].text(0.5,
                                   1.0,
                                   'DR13-DR12',
                                   transform=ax[iparam, 0].transAxes,
                                   ha='center',
                                   va='bottom')
                ax[iparam, 1].text(0.5,
                                   1.0,
                                   'DR14-DR12',
                                   transform=ax[iparam, 1].transAxes,
                                   ha='center',
                                   va='bottom')
                ax[iparam, 2].text(0.5,
                                   1.0,
                                   'DR14-DR13',
                                   transform=ax[iparam, 2].transAxes,
                                   ha='center',
                                   va='bottom')

            if iparam == 0:
                yr = [-300, 300]
            elif iparam == 1:
                yr = [-0.5, 0.5]
            else:
                yr = [-0.3, 0.3]

            xr = [3500, 6000]

            axim = plots.plotc(ax[iparam, 0],
                               dr12['TEFF'][m1a],
                               dr13[param][m2a, iparam] -
                               dr12[param][m1a, iparam],
                               dr12[param][m1a, 3],
                               size=1,
                               xr=xr,
                               yr=yr,
                               zr=[-1, 0.5],
                               yt=yt,
                               xt=xt,
                               rasterized=True)
            plots.plotl(ax[iparam, 0], xr, [0., 0.], ls=':')
            plots.plotc(ax[iparam, 1],
                        dr12['TEFF'][m1b],
                        dr14[param][m2b, iparam] - dr12[param][m1b, iparam],
                        dr12[param][m1b, 3],
                        size=1,
                        xr=xr,
                        yr=yr,
                        zr=[-1, 0.5],
                        xt=xt,
                        rasterized=True)
            plots.plotl(ax[iparam, 1], xr, [0., 0.], ls=':')
            plots.plotc(ax[iparam, 2],
                        dr13['TEFF'][m1c],
                        dr14[param][m2c, iparam] - dr13[param][m1c, iparam],
                        dr13[param][m1c, 3],
                        size=1,
                        xr=xr,
                        yr=yr,
                        zr=[-1, 0.5],
                        xt=xt,
                        rasterized=True)
            plots.plotl(ax[iparam, 2], xr, [0., 0.], ls=':')
            for iax in range(3):
                ax[iparam, iax].tick_params(axis='both', labelsize=8)

    # add colorbar
    for fig in [figu, figc]:
        cbaxes = fig.add_axes([0.91, 0.1, 0.01, 0.8])
        cb = plt.colorbar(axim, cax=cbaxes)
        cb.set_label('[M/H]')
        cbaxes.tick_params(axis='both', labelsize=8)

    figu.savefig('drcomp_uncal.pdf')
    figc.savefig('drcomp_cal.pdf')
    plots.close()

    # abundance figure
    fig, ax = plots.multi(3, 14, hspace=0.001, wspace=0.001, figsize=(8, 32))

    for ielem, elem in enumerate([
            'C', 'N', 'O', 'Na', 'Mg', 'Al', 'Si', 'S', 'K', 'Ca', 'Ti', 'V',
            'Mn', 'Ni'
    ]):
        print(elem)
        yt = r'$\Delta$' + elem
        if ielem == 13: xt = r'T$_{\rm eff}$'
        else: xt = None
        if ielem == 0:
            ax[ielem, 0].text(0.5,
                              1.0,
                              'DR13-DR12',
                              transform=ax[ielem, 0].transAxes,
                              ha='center',
                              va='bottom')
            ax[ielem, 1].text(0.5,
                              1.0,
                              'DR14-DR12',
                              transform=ax[ielem, 1].transAxes,
                              ha='center',
                              va='bottom')
            ax[ielem, 2].text(0.5,
                              1.0,
                              'DR14-DR13',
                              transform=ax[ielem, 2].transAxes,
                              ha='center',
                              va='bottom')

        yr = [-0.5, 0.5]

        dr12elem = dr12[elem.upper() + '_H'][m1a] - dr12['FE_H'][m1a]
        dr13elem = dr13[elem.upper() + '_FE'][m2a]
        gd = np.where((dr12elem > -99) & (dr13elem > -99))[0]
        plots.plotc(ax[ielem, 0],
                    dr12['TEFF'][m1a[gd]],
                    dr13elem[gd] - dr12elem[gd],
                    dr12['PARAM'][m1a[gd], 3],
                    size=1,
                    xr=[3500, 6000],
                    yr=yr,
                    zr=[-1, 0.5],
                    yt=yt,
                    xt=xt,
                    nytick=5,
                    rasterized=True)
        plots.plotl(ax[ielem, 0], xr, [0., 0.], ls=':')
        ax[ielem, 0].tick_params(axis='both', labelsize=8)

        dr12elem = dr12[elem.upper() + '_H'][m1b] - dr12['FE_H'][m1b]
        dr14elem = dr14[elem.upper() + '_FE'][m2b]
        gd = np.where((dr12elem > -99) & (dr14elem > -99))[0]
        plots.plotc(ax[ielem, 1],
                    dr12['TEFF'][m1b[gd]],
                    dr14elem[gd] - dr12elem[gd],
                    dr12['PARAM'][m1b[gd], 3],
                    size=1,
                    xr=[3500, 6000],
                    yr=yr,
                    zr=[-1, 0.5],
                    xt=xt,
                    nytick=5,
                    rasterized=True)
        plots.plotl(ax[ielem, 1], xr, [0., 0.], ls=':')
        ax[ielem, 1].tick_params(axis='both', labelsize=8)

        dr13elem = dr13[elem.upper() + '_FE'][m1c]
        dr14elem = dr14[elem.upper() + '_FE'][m2c]
        gd = np.where((dr13elem > -99) & (dr14elem > -99))[0]
        plots.plotc(ax[ielem, 2],
                    dr13['TEFF'][m1c[gd]],
                    dr14elem[gd] - dr13elem[gd],
                    dr13['PARAM'][m1c[gd], 3],
                    size=1,
                    xr=[3500, 6000],
                    yr=yr,
                    zr=[-1, 0.5],
                    xt=xt,
                    nytick=5,
                    rasterized=True)
        plots.plotl(ax[ielem, 2], xr, [0., 0.], ls=':')
        ax[ielem, 2].tick_params(axis='both', labelsize=8)

    cbaxes = fig.add_axes([0.91, 0.1, 0.01, 0.8])
    cb = plt.colorbar(axim, cax=cbaxes)
    cb.set_label('[M/H]')
    cbaxes.tick_params(axis='both', labelsize=8)

    for item in (cbaxes.get_xticklabels() + cbaxes.get_yticklabels()):
        item.set_fontsize(8)
    fig.savefig('drcomp_elem.pdf')
Exemple #10
0
def repeat(data,out='./',elem=True,logg=[-1,6], log=True, fact=1.0) :
    """ Comparison of repeat observations of objects
    """

    gd=apselect.select(data[1].data,badval='STAR_BAD',raw=True,logg=logg)
    a=data[1].data[gd]
    stars = set(a['APOGEE_ID'])

    snbins=np.arange(50,300,50)
    tebins=np.arange(3500,7500,250)
    tebins=np.arange(3500,6000,250)
    mhbins=np.arange(-2.25,1.00,0.5)
    dte = tebins[1]-tebins[0]
    dmh = mhbins[1]-mhbins[0]
    dsn = snbins[1]-snbins[0]

    # output data files
    params=data[3].data['PARAM_SYMBOL'][0]
    els=data[3].data['ELEM_SYMBOL'][0]
    of=[]
    for el in els: 
        of.append(open(out+el+'.txt','w'))

    # loop over stars looking for duplications
    rmsderiv=[]
    rmsparam=[]
    rmselem=[]
    quad= True
    fmt='{:<20s}'+6*' {:9.2f}'+3*' {:10.3f}'+'\n'
    for star in stars :
        jj = np.where(a['APOGEE_ID'] == star)[0]
        n = len(jj)
        if n > 1 :
            isort = np.argsort(a['SNR'][jj])
            j=jj[isort]
            i=0
            while i < n-1 :
                if a['SNR'][j[i+1]]/a['SNR'][j[i]] < 1.2 :
                   rmsderiv.append([1.,
                                   a['FPARAM'][j[i:i+2],0].mean()-4500.,
                                   np.min([250.,a['SNR'][j[i:i+2]].mean()])-100.,  # cap S/N at 250
                                   a['FPARAM'][j[i:i+2],3].mean(),
                                   (a['FPARAM'][j[i:i+2],0].mean()-4500.)**2 ])
                   rmsparam.append(np.abs(a['FPARAM'][j[i],:]-a['FPARAM'][j[i+1],:])*np.sqrt(np.pi)/2.)
                   try: rmselem.append(np.abs(a['FELEM'][j[i],0,:]-a['FELEM'][j[i+1],0,:])*np.sqrt(np.pi)/2.)
                   except: rmselem.append(np.abs(a['FELEM'][j[i],:]-a['FELEM'][j[i+1],:])*np.sqrt(np.pi)/2.)
                   for iel in range(len(els)) :
                       of[iel].write(fmt.format(star,a['FPARAM'][j[i],0],a['FPARAM'][j[i+1],0],
                                                a['SNR'][j[i]],a['SNR'][j[i+1]],
                                                a['FPARAM'][j[i],3],a['FPARAM'][j[i+1],3],
                                                a['FELEM'][j[i],0,iel],a['FELEM'][j[i+1],0,iel],
                                                np.abs(a['FELEM'][j[i],0,iel]-a['FELEM'][j[i+1],0,iel])))
                   i+=2
                else :
                   i+=1
    for iel in range(len(els)) : of[iel].close()
    rmsderiv=np.array(rmsderiv)
    rmsparam=np.array(rmsparam)
    rmselem=np.array(rmselem)

    # default grids for showing 2D fits
    y, x = np.mgrid[tebins[0]:tebins[-1]:200j,mhbins[0]:mhbins[-1]:200j]

    # parameters
    grid=[]
    ytit=[]
    outtype=np.dtype([('PARAM',params.dtype),('ERRFIT','5f4')])
    outparam=np.empty(len(params),dtype=outtype)

    for i,param in enumerate(params) :
        print(param)
        outparam[i]['PARAM']=param
        if param == 'TEFF' : 
            gd=np.where((rmsparam[:,i] < 500.) & (rmsparam[:,i] > 0.) )[0]
            zr=[0,100] 
        else : 
            gd=np.where((rmsparam[:,i] < 1.) & (rmsparam[:,i] > 0.) )[0]
            zr=[0,0.2]
        if len(gd)<5 : continue

        if log : soln,inv = fit.linear(np.log(rmsparam[gd,i]),rmsderiv[gd,:].transpose())
        else : soln,inv = fit.linear(rmsparam[gd,i],rmsderiv[gd,:].transpose())
        outparam[i]['ERRFIT']=soln

        # plots of 2D fits in bins of S/N
        fig,ax=plots.multi(len(snbins),1,wspace=0.001,figsize=(3*len(snbins),4))
        for iplt in range(len(snbins)) :
            sn = snbins[iplt]+dsn/2.
            ax[iplt].imshow(elemerr(soln,y-4500.,sn-100.,x, quad=quad, log=log, fact=fact),extent=[mhbins[0],mhbins[-1],tebins[0],tebins[-1]], 
                              aspect='auto',vmin=zr[0],vmax=zr[1], origin='lower',cmap='rainbow')
            ax[iplt].text(0.98,0.98,param+' S/N={:4.0f}'.format(sn),va='top',ha='right',transform=ax[iplt].transAxes)

        # plots of rms
        snfig,snax=plots.multi(len(tebins)-1,len(mhbins)-1,wspace=0.001,hspace=0.001,figsize=(3*len(tebins),2*len(mhbins)),xtickrot=60)
        xx=np.arange(0,250)
        for ix in range(len(tebins)-1) :
            if ix == 0 : yt=r'$\sigma$'
            else : yt=''
            for iy in range(len(mhbins)-1) :
                gdplt = np.where((rmsderiv[:,1]+4500.>tebins[ix]) & (rmsderiv[:,1]+4500.<tebins[ix+1]) &
                                  (rmsderiv[:,3]>mhbins[iy]) & (rmsderiv[:,3]<mhbins[iy+1]) )[0]
                if len(gdplt) > 1 :
                    plots.plotc(snax[iy,ix],rmsderiv[gdplt,2]+100,rmsparam[gdplt,i],rmsderiv[gdplt,3],size=5,zr=[-2,0.5],
                                yr=zr,xr=[snbins[0],snbins[-1]],xt='S/N',yt=yt)
                snax[iy,ix].set_ylim(zr)
                snax[iy,ix].plot(xx,elemerr(soln,tebins[ix]+dte/2.-4500,xx-100,mhbins[iy]+dmh/2., quad=quad, log=log, fact=fact))
                snax[iy,ix].text(0.98,0.98,'{:8.0f}{:6.2f}'.format(tebins[ix]+dte/2.,mhbins[iy]+dmh/2.),ha='right',va='top',transform=snax[iy,ix].transAxes,fontsize='x-small')
                for iz in range(len(snbins)-1) :
                    gd= np.where((rmsderiv[gdplt,2]+100.>snbins[iz]) & (rmsderiv[gdplt,2]+100.<snbins[iz+1]) ) [0]
                    if len(gd) > 0 :
                        snax[iy,ix].text(0.98,0.88-iz*0.08,'{:8.2f}'.format(
                                    rmsparam[gdplt[gd],i].mean()),transform=snax[iy,ix].transAxes,fontsize='x-small',ha='right',va='top')
        fig.savefig(out+param+'.png')
        plt.close(fig)
        snfig.savefig(out+param+'_sn.png')
        plt.close(snfig)
        grid.append([os.path.basename(out+param+'.png'),os.path.basename(out+param+'_sn.png')])
        ytit.append(param)
    html.htmltab(grid,file=out+'repeat_param.html',ytitle=ytit)
   
    # elements 
    grid=[]
    ytit=[]
    outtype=np.dtype([('ELEM',els.dtype),('ERRFIT','5f4')])
    outelem=np.empty(len(els),dtype=outtype)
    allfig,allax=plots.multi(5,5,hspace=0.001,wspace=0.001,xtickrot=60)
    for i,el in enumerate(els) :
        print(el)
        outelem[i]['ELEM']=el
        gd=np.where((rmselem[:,i] < 1.) & (rmselem[:,i] > 0.) )[0]
        zr=[0,0.19]
        if len(gd)<5 : continue

        if log :soln,inv = fit.linear(np.log(rmselem[gd,i]),rmsderiv[gd,:].transpose())
        else :soln,inv = fit.linear(rmselem[gd,i],rmsderiv[gd,:].transpose())
        outelem[i]['ERRFIT']=soln
        fig,ax=plots.multi(len(snbins),1,wspace=0.001,figsize=(3*len(snbins),4))
        for iplt in range(len(snbins)) :
            sn = snbins[iplt]+dsn/2.
            ax[iplt].imshow(elemerr(soln,y-4500.,sn-100.,x, quad=quad, log=log, fact=fact),
                              extent=[mhbins[0],mhbins[-1],tebins[0],tebins[-1]], 
                              aspect='auto',vmin=zr[0],vmax=zr[1], origin='lower',cmap='rainbow')
            ax[iplt].text(0.98,0.98,el+' S/N={:4.0f}'.format(sn),va='top',ha='right',transform=ax[iplt].transAxes)
            if i<25 and sn == 125 :
                allax[i//5,i%5].text(0.05,0.95,el,va='top',ha='left',transform=allax[i//5,i%5].transAxes,color='w')
                allax[i//5,i%5].set_xlabel('[M/H]')
                if i%5 == 0 : allax[i//5,i%5].set_ylabel(r'$T_{eff}$')
                cm= allax[i//5,i%5].imshow(elemerr(soln,y-4500.,sn-100.,x, quad=quad, log=log, fact=fact),
                                       extent=[mhbins[0],mhbins[-1],tebins[0],tebins[-1]], 
                                       aspect='auto',vmin=zr[0],vmax=0.1, origin='lower',cmap='rainbow')

        snfig,snax=plots.multi(len(tebins)-1,len(mhbins)-1,wspace=0.001,hspace=0.001,figsize=(2.2*len(tebins),3*len(mhbins)),xtickrot=60)
        fig2,ax2=plots.multi(len(tebins)-1,len(mhbins)-1,wspace=0.001,hspace=0.001,figsize=(2*len(tebins),3*len(mhbins)),xtickrot=60)
        xx=np.arange(0,250)
        for ix in range(len(tebins)-1) :
            for iy in range(len(mhbins)-1) :
                if ix == 0 : snax[iy,ix].set_ylabel(r'$\sigma$('+el+')')
                gdplt = np.where((rmsderiv[:,1]+4500.>tebins[ix]) & (rmsderiv[:,1]+4500.<tebins[ix+1]) &
                                  (rmsderiv[:,3]>mhbins[iy]) & (rmsderiv[:,3]<mhbins[iy+1]) )[0]
                if len(gdplt) > 1 :
                    #plots.plotc(snax[iy,ix],rmsderiv[gdplt,2]+100,rmselem[gdplt,i],rmsderiv[gdplt,3],size=5,zr=[-2,0.5],
                    #            yr=zr,xr=[snbins[0],snbins[-1]],xt='S/N')
                    plots.plotp(snax[iy,ix],rmsderiv[gdplt,2]+100,rmselem[gdplt,i],color='k',size=5,zr=[-2,0.5],
                                yr=zr,xr=[snbins[0],snbins[-1]],xt='S/N')
                snax[iy,ix].set_xlim(snbins[0]+1,snbins[-1]-1)
                snax[iy,ix].set_xlabel('S/N')
                snax[iy,ix].set_ylim(zr)
                snax[iy,ix].plot(xx,elemerr(soln,tebins[ix]+dte/2.-4500,xx-100,mhbins[iy]+dmh/2., quad=quad, log=log, fact=fact))
                snax[iy,ix].text(0.02,0.95,r'$T_{eff}:$'+'{:6.0f}'.format(tebins[ix]+dte/2.),
                                 ha='left',va='top',transform=snax[iy,ix].transAxes,fontsize='large')
                snax[iy,ix].text(0.02,0.80,'[M/H]: {:6.2f}'.format(mhbins[iy]+dmh/2.),
                                 ha='left',va='top',transform=snax[iy,ix].transAxes,fontsize='large')
                for iz in range(len(snbins)-1) :
                    gd= np.where((rmsderiv[gdplt,2]+100.>snbins[iz]) & (rmsderiv[gdplt,2]+100.<snbins[iz+1]) ) [0]
                    #snax[iy,ix].text(0.98,0.88-iz*0.08,'{:8.3f}'.format(rmselem[gdplt[gd],i].mean()),
                    #                 transform=snax[iy,ix].transAxes,fontsize='x-small',ha='right',va='top')
                    ax2[iy,ix].text(0.98,0.98,'{:8.0f}{:6.2f}'.format(tebins[ix]+dte/2.,mhbins[iy]+dmh/2.),
                                    ha='right',va='top',transform=ax2[iy,ix].transAxes,fontsize='x-small')
                    ax2[iy,ix].set_xlim(0,0.16)
                    bins=np.arange(0,0.161,0.02)
                    ax2[iy,ix].set_ylim(0,10)
                    if len(gd) > 0 :
                        if iz == 1 and len(gd) > 3 :
                            ax2[iy,ix].hist(np.clip(rmselem[gdplt[gd],i],bins[0],bins[-1]-0.001),bins=bins,histtype='step',color='k')
                            ylim=ax2[iy,ix].get_ylim()
                            m=rmselem[gdplt[gd],i].mean()
                            ax2[iy,ix].plot([m,m],[ylim[0],ylim[1]],color='r')
                            m=np.median(rmselem[gdplt[gd],i])
                            ax2[iy,ix].plot([m,m],[ylim[0],ylim[1]],color='g')
                            m=elemerr(soln,tebins[ix]+dte/2.-4500,snbins[iz]+dsn/2.-100,mhbins[iy]+dmh/2.,
                                      quad=quad,log=log,fact=fact)
                            ax2[iy,ix].plot([m,m],[ylim[0],ylim[1]],color='b')
        fig.savefig(out+el+'.png')
        plt.close(fig)
        snfig.savefig(out+el+'_sn.png')
        plt.close(snfig)
        fig2.savefig(out+el+'_hist2.png')
        plt.close(fig2)
        fig,ax=plots.multi(1,1)
        ax.hist(rmselem[:,i]-elemerr(soln,rmsderiv[:,1],rmsderiv[:,2],rmsderiv[:,3],quad=quad,log=log,fact=fact),bins=np.arange(-0.4,0.4,0.005))
        ylim=ax.get_ylim()
        ax.plot([0.,0.],[ylim[0],ylim[1]])
        ax.set_xlabel('diff*sqrt(pi)/2.-fit')
        fig.savefig(out+el+'_hist.png')
        plt.close(fig)
        grid.append([os.path.basename(out+el+'.png'),os.path.basename(out+el+'_sn.png'),os.path.basename(out+el+'_hist.png'),
                     os.path.basename(out+el+'_hist2.png')])
        ytit.append('<A HREF='+os.path.basename(out)+el+'.txt>'+el+'</A>')

    cb=allfig.colorbar(cm, ax=list(allax[:,-1]))
    cb.ax.set_ylabel('$\sigma$')

    allfig.savefig(out+'allel.png')
    html.htmltab(grid,file=out+'repeat_elem.html',ytitle=ytit)
    hdulist=fits.HDUList()
    hdulist.append(fits.BinTableHDU(outparam))
    hdulist.append(fits.BinTableHDU(outelem))
    hdulist.append(fits.ImageHDU(rmsderiv))
    hdulist.append(fits.ImageHDU(rmselem))
    hdulist.writeto(out+'errfit.fits',overwrite=True)

    return outparam,outelem
Exemple #11
0
def sample(name='test',
           gridclass=None,
           eps=0.01,
           tefflim=[3000, 8000],
           dtlo=100.,
           logglim=[-0.5, 5.5],
           mhlim=[-2.5, 0.75],
           nmlim=[-0.5, 2.],
           cmlim=[-1.5, 1.],
           emlim=[-0.5, 1.],
           vmicrolim=[0.3, 4.8],
           amlim=[-0.5, 1.],
           vrotlim=[1.5, 96.],
           rot=True,
           nsamp=1,
           niso=None,
           elems='all',
           fact=1.0,
           offgrid=False,
           extracool=1.):
    """ Generate a test sample of parameters and abundances from isochrones
    """

    # set range around isochrones
    isorange = [-1, 0, 1]
    # set output limits
    dthi = 250
    dthot = 250
    dtvhot = 250
    dlogg = 0.5
    dmh = 0.25
    dam = 0.25
    if gridclass == 'GKg':
        tefflim = [3500, 6000]
        logglim = [0, 4.5]
        dtlo = 250.
        rot = False
    elif gridclass == 'Mg':
        tefflim = [3000, 4000]
        logglim = [-0.5, 3.0]
        dtlo = 100.
        rot = False
    elif gridclass == 'GKd':
        tefflim = [3500, 6000]
        logglim = [2.5, 5.5]
        cmlim = [-0.5, 0.5]
        nmlim = [-0.5, 1.5]
        dtlo = 250.
        rot = True
    elif gridclass == 'Md':
        tefflim = [3000, 4000]
        logglim = [2.5, 5.5]
        cmlim = [-0.5, 0.5]
        nmlim = [-0.5, 1.5]
        dtlo = 100.
        rot = True
    elif gridclass == 'Fd':
        tefflim = [5500, 8000]
        logglim = [2.5, 5.5]
        cmlim = [-0.5, 0.5]
        nmlim = [-0.5, 1.5]
        dtlo = 250.
        rot = True
    elif gridclass == 'coarse':
        tefflim = [3500, 8000]
        logglim = [0.5, 5.]
        cmlim = [-0.5, 0.5]
        nmlim = [-0.5, 1.5]
        dtlo = 500.
        dthi = 500.
        dlogg = 1.0
        dmh = 0.5
        dam = 0.5
        rot = True
    elif gridclass == 'rv':
        tefflim = [3000, 20000]
        logglim = [0.5, 5.]
        cmlim = [0., 0.]
        nmlim = [0., 0.]
        emlim = [0., 0.]
        dtlo = 200.
        dthi = 250.
        dthot = 500.
        dtvhot = 1000.
        dlogg = 1.0
        dmh = 0.5
        dam = 0.5
        rot = False
        isorange = [0]
    if gridclass is not None: name = name + '_' + gridclass
    grid = []

    # loop through isochrone data and take grid points nearest and +/- 1 (isorange)
    # accumulate unique set of these
    files = glob.glob(os.environ['ISOCHRONE_DIR'] + '/z*.dat')
    if niso is None: niso = len(files)
    for file in files[0:niso]:
        a = isochrones.read(file, agerange=[7, 20])
        print(file)
        for i in range(len(a)):
            if a['teff'][i] < 4000: dt = dtlo
            elif a['teff'][i] > 8000: dt = dtvhot
            elif a['teff'][i] > 5500: dt = dthot
            else: dt = dthi
            for j in isorange:
                teff = (int(round(a['teff'][i] / dt)) + j) * int(dt)
                logg = (int(round(a['logg'][i] / dlogg)) + j) * dlogg
                mh = (int(round(a['feh'][i] / dmh)) + j) * dmh
                # clip to stay within requested grid limits
                teff = clip(teff, tefflim)
                logg = clip(logg, logglim)
                mh = clip(mh, mhlim)
                grid.append(tuple([teff, logg, mh]))
        grid = list(set(grid))
        print(len(grid))

    # output file
    f = open(name, 'w')
    fipf = open(name + '.ipf', 'w')
    pars = [
        'Teff', 'logg', '[M/H]', '[alpha/M]', '[C/M]', '[N/M]', 'vmicro',
        'vrot'
    ]
    f.write("# ")
    for par in pars:
        f.write(' {:s}'.format(par))
    allteff = []
    alllogg = []
    allmh = []
    allvmic = []
    allvrot = []
    allam = []
    allcm = []
    allnm = []
    els = np.array([
        'O', 'Na', 'Mg', 'Al', 'Si', 'P', 'S', 'K', 'Ca', 'Ti', 'V', 'Cr',
        'Mn', 'Co', 'Fe', 'Ni', 'Cu', 'Ge', 'Rb', 'Ce', 'Nd'
    ])

    els_alpha = np.where((els == 'O') | (els == 'Mg') | (els == 'Si')
                         | (els == 'S') | (els == 'Ca') | (els == 'Ti'))[0]
    for el in els:
        f.write('{:>7s}'.format(el))
    f.write('\n')
    nel = len(els)
    for i, x in enumerate(grid):
        if a['teff'][i] <= 4000: nf = extracool
        else: nf = 1
        for j in range(nsamp * nf):
            if offgrid:
                if a['teff'][i] < 4000: dt = dtlo
                elif a['teff'][i] > 8000: dt = dtvhot
                elif a['teff'][i] > 5500: dt = dthot
                teff = x[0] + np.random.uniform(-dt / 2., dt / 2.)
                logg = x[1] + np.random.uniform(-dlogg / 2., dlogg / 2.)
                mh = x[2] + np.random.uniform(-dmh / 2., dmh / 2.)
                teff = clip(teff, tefflim)
                logg = clip(logg, logglim)
                mh = clip(mh, mhlim)
            else:
                teff = x[0]
                logg = x[1]
                mh = x[2]
            vmicro = 10.**(0.226 - 0.0228 * logg + 0.0297 * logg**2 -
                           0.0113 * logg**3) + np.random.normal(0., 0.3)
            vmicro = clip(vmicro, vmicrolim)
            if (gridclass == 'rv'):
                vmicro = 0.226 - 0.0228 * logg + 0.0297 * logg**2 - 0.0113 * logg**3
                vmicro = 10.**(int(round(vmicro / 0.30103)) * 0.30103 -
                               0.522878)

            vrot = 0.
            if (logg < 3.5) & (teff < 6000):
                # for giants, use vmacro relation + small rotation
                if rot: vrot = np.max([0., np.random.normal(1.5, 0.5 * fact)])
                # carbon and nitrogen with significant range
                cm = np.random.normal(-0.20, .5 * fact)
                if not offgrid: cm = (int(round(cm / 0.25))) * 0.25
                nm = np.random.normal(0.3, 0.7 * fact)
                # no need to pin [N/M] to grid since it is varied in synthesis!
                #nm = (int(round(nm/0.5)))*0.5
            else:
                # for dwarfs, use significant rotation
                if rot: vrot = abs(np.random.normal(0., 30 * fact))
                # carbon and nitrogen with small range
                cm = np.random.normal(0., 0.25 * fact)
                if not offgrid: cm = (int(round(cm / 0.25))) * 0.25
                nm = np.random.normal(0., 0.25 * fact)
            cm = clip(cm, cmlim)
            nm = clip(nm, nmlim)
            # for RV grid, enhance N for giants
            if (gridclass == 'rv') & (logg < 3) & (teff < 6000): nm = 0.25
            # draw a random alpha/M
            am = np.random.uniform(-0.25, 0.5)
            if not offgrid: am = (round(am / dam)) * dam
            am = clip(am, amlim)
            allteff.append(teff)
            alllogg.append(logg)
            allmh.append(mh)
            allvmic.append(vmicro)
            allvrot.append(vrot)
            allam.append(am)
            allcm.append(cm)
            allnm.append(nm)

            out = '{:8.2f}{:8.2f}{:8.2f}{:8.2f}{:8.2f}{:8.2f}{:8.2f}{:8.2f}'.format(
                teff, logg, mh, am, cm, nm, vmicro, vrot)
            # individual elemental abundances
            el = np.random.normal(0., 0.2 * fact, size=nel)
            for ie, e in enumerate(el):
                if elems == 'all' or els[ie] in elems: el[ie] = clip(e, emlim)
                else: el[ie] = 0.
            el[els_alpha] += am
            for e in el:
                # add element abundances
                out = out + '{:7.2f}'.format(e)  # other elements
            print(out)
            f.write(out + '\n')
            if gridclass == 'rv':
                # add some alpha-enhanced and carbon-enhanced models
                out = None
                if (mh < -0.5) & (teff < 5000):
                    out = '{:8.2f}{:8.2f}{:8.2f}{:8.2f}{:8.2f}{:8.2f}{:8.2f}{:8.2f}'.format(
                        teff, logg, mh, am + 0.25, cm, nm, vmicro, vrot)
                elif (mh > -0.5) & (teff < 3500):
                    out = '{:8.2f}{:8.2f}{:8.2f}{:8.2f}{:8.2f}{:8.2f}{:8.2f}{:8.2f}'.format(
                        teff, logg, mh, am + 0.25, cm + 0.5, nm, vmicro, vrot)
                if out is not None:
                    for e in el:
                        out = out + '{:7.2f}'.format(e)  # other elements
                    f.write(out + '\n')

            # clip to adjust slightly off grid edges for FERRE input file
            teff = clip(teff, tefflim, eps=eps)
            logg = clip(logg, logglim, eps=eps)
            mh = clip(mh, mhlim, eps=eps)
            cm = clip(cm, cmlim, eps=eps)
            if not offgrid: nm = (round(nm / 0.5)) * 0.5
            nm = clip(nm, nmlim, eps=eps)
            am = clip(am, amlim, eps=eps)
            vmicro = round(
                (np.log10(vmicro) - math.log10(vmicrolim[0])) /
                math.log10(2.)) * math.log10(2.) + math.log10(vmicrolim[0])
            vmicro = clip(vmicro, np.log10(np.array(vmicrolim)), eps=eps)
            vrot = round(
                (np.log10(vrot) - math.log10(vrotlim[0])) /
                math.log10(2.)) * math.log10(2.) + math.log10(vrotlim[0])
            vrot = clip(vrot, np.log10(np.array(vrotlim)), eps=eps)

            if rot:
                ipf = '{:s}{:d} {:7.2f} {:7.2f} {:7.2f} {:7.2f} {:7.2f} {:7.2f} {:7.2f} {:8.2f}'.format(
                    os.path.basename(name), i + 1, vmicro, cm, nm, am, vrot,
                    mh, logg, teff)
            else:
                ipf = '{:s}{:d} {:7.2f} {:7.2f} {:7.2f} {:7.2f} {:7.2f} {:7.2f} {:8.2f}'.format(
                    os.path.basename(name), i + 1, vmicro, cm, nm, am, mh,
                    logg, teff)
            fipf.write(ipf + '\n')

    f.close()
    fipf.close()

    # plots of sample
    allteff = np.array(allteff)
    alllogg = np.array(alllogg)
    allmh = np.array(allmh)
    allvmic = np.array(allvmic)
    allvrot = np.array(allvrot)
    allam = np.array(allam)
    allcm = np.array(allcm)
    allnm = np.array(allnm)
    t = [x[0] for x in grid]
    g = [x[1] for x in grid]
    m = [x[2] for x in grid]
    fig, ax = plots.multi(2, 3, hspace=0.001, wspace=0.4)
    plots.plotc(ax[0, 0],
                allteff + np.random.uniform(-30., 30., size=len(allteff)),
                alllogg + np.random.uniform(-0.1, 0.1, size=len(alllogg)),
                allmh,
                xr=[8000, 2500],
                yr=[6., -1],
                zr=mhlim,
                zt='[M/H]',
                colorbar=True,
                yt='log g')
    plots.plotc(ax[0, 1],
                allteff + np.random.uniform(-30., 30., size=len(allteff)),
                alllogg + np.random.uniform(-0.1, 0.1, size=len(alllogg)),
                allam,
                xr=[8000, 2500],
                yr=[6., -1],
                zr=amlim,
                zt='[alpha/M]',
                colorbar=True,
                yt='log g')
    plots.plotc(ax[1, 0],
                allteff + np.random.uniform(-30., 30., size=len(allteff)),
                alllogg + np.random.uniform(-0.1, 0.1, size=len(alllogg)),
                allvmic,
                xr=[8000, 2500],
                yr=[6., -1],
                zr=vmicrolim,
                zt='vmicro',
                colorbar=True,
                yt='log g')
    plots.plotc(ax[1, 1],
                allteff + np.random.uniform(-30., 30., size=len(allteff)),
                alllogg + np.random.uniform(-0.1, 0.1, size=len(alllogg)),
                allvrot,
                xr=[8000, 2500],
                yr=[6., -1],
                zr=[0, 30],
                zt='vrot',
                colorbar=True,
                yt='log g')
    plots.plotc(ax[2, 0],
                allteff + np.random.uniform(-30., 30., size=len(allteff)),
                alllogg + np.random.uniform(-0.1, 0.1, size=len(alllogg)),
                allcm,
                xr=[8000, 2500],
                yr=[6., -1],
                zr=cmlim,
                zt='[C/M]',
                colorbar=True,
                yt='log g',
                xt='Teff')
    plots.plotc(ax[2, 1],
                allteff + np.random.uniform(-30., 30., size=len(allteff)),
                alllogg + np.random.uniform(-0.1, 0.1, size=len(alllogg)),
                allnm,
                xr=[8000, 2500],
                yr=[6., -1],
                zr=nmlim,
                zt='[N/M]',
                colorbar=True,
                xt='Teff',
                yt='log g')
    fig.savefig(name + '.png')
    plt.close()
    fig, ax = plots.multi(2, 2, hspace=0.4, wspace=0.4)
    plots.plotc(ax[0, 0],
                allmh + np.random.uniform(-0.1, 0.1, size=len(allmh)),
                allam + np.random.uniform(-0.1, 0.1, size=len(allam)),
                allteff,
                xr=[-2.5, 1],
                yr=[-0.75, 1.],
                zr=[2500, 8000],
                zt='Teff',
                colorbar=True,
                xt='[M/H]',
                yt='[alpha/M]')
    plots.plotc(ax[0, 1],
                alllogg + np.random.uniform(-0.1, 0.1, size=len(alllogg)),
                allcm + np.random.uniform(-0.1, 0.1, size=len(allam)),
                allteff,
                xr=[6, -1],
                yr=[-1.5, 2.],
                zr=[2500, 8000],
                zt='Teff',
                colorbar=True,
                xt='log g',
                yt='[C/M]')
    plots.plotc(ax[1, 1],
                alllogg + np.random.uniform(-0.1, 0.1, size=len(alllogg)),
                allnm + np.random.uniform(-0.1, 0.1, size=len(allam)),
                allteff,
                xr=[6, -1],
                yr=[-1.5, 2.],
                zr=[2500, 8000],
                zt='Teff',
                colorbar=True,
                xt='log g',
                yt='[N/M]')
    fig.tight_layout()
    fig.savefig(name + '_2.png')
    plt.close()
Exemple #12
0
def calib(allstar, out='./'):
    """ Plot calibration relations
    """

    grid = []
    yt = []
    # Teff f([M/H], Teff)
    fig, ax = plots.multi(1, 1)
    plots.plotc(ax,
                allstar[1].data['FPARAM'][:, 3],
                allstar[1].data['PARAM'][:, 0] -
                allstar[1].data['FPARAM'][:, 0],
                allstar[1].data['FPARAM'][:, 0],
                xt='[M/H]',
                yt=r'$\Delta$ Teff',
                zr=[3000, 8000],
                colorbar=True,
                zt='Teff')
    outfile1 = out + 'calib_Teff_all.png'
    fig.savefig(outfile1)
    ax.set_xlim(-3, 1)
    ax.set_ylim(-500, 500)
    outfile2 = out + 'calib_Teff.png'
    fig.savefig(outfile2)
    plt.close(fig)
    grid.append([os.path.basename(outfile2), os.path.basename(outfile1)])
    yt.append('Teff')

    # logg f(logg,[M/H])
    fig, ax = plots.multi(1, 1)
    plots.plotc(ax,
                allstar[1].data['FPARAM'][:, 1],
                allstar[1].data['PARAM'][:, 1] -
                allstar[1].data['FPARAM'][:, 1],
                allstar[1].data['FPARAM'][:, 3],
                xt='log g',
                yt=r'$\Delta$logg',
                zr=[-2, 0.5],
                colorbar=True,
                zt='[M/H]')
    outfile1 = out + 'calib_logg_all.png'
    fig.savefig(outfile1)
    ax.set_xlim(-1, 6)
    ax.set_ylim(-1, 1)
    outfile2 = out + 'calib_logg.png'
    fig.savefig(outfile2)
    plt.close(fig)
    grid.append([os.path.basename(outfile2), os.path.basename(outfile1)])
    yt.append('log g')

    els = allstar[3].data['ELEM_SYMBOL'][0]
    for iel, el in enumerate(els):
        print(el)
        fig, ax = plots.multi(1, 1)
        if allstar[3].data['ELEMTOH'][0][iel] == 0:
            abun = allstar[1].data['FELEM'][:, iel]
        else:
            abun = allstar[1].data['FELEM'][:,
                                            iel] - allstar[1].data['FPARAM'][:,
                                                                             3]
        plots.plotc(ax,
                    allstar[1].data['FPARAM'][:, 0],
                    allstar[1].data['X_M'][:, iel] - abun,
                    allstar[1].data['FPARAM'][:, 1],
                    xt='Teff',
                    yt=r'$\Delta$[' + el + '/M]',
                    zr=[0, 5.],
                    colorbar=True,
                    zt='log g')
        outfile1 = out + 'calib_{:s}_all.png'.format(el.strip())
        fig.savefig(outfile1)
        ax.set_xlim(3000, 8000)
        ax.set_ylim(-0.5, 0.5)
        outfile2 = out + 'calib_{:s}.png'.format(el.strip())
        fig.savefig(outfile2)
        plt.close(fig)
        grid.append([os.path.basename(outfile2), os.path.basename(outfile1)])
        yt.append(el.strip())

    html.htmltab(grid, file=out + 'calib.html', ytitle=yt)
Exemple #13
0
def plot(teff,
         logg,
         mh,
         meanfib,
         v,
         vr,
         fit1d,
         fit2d,
         xr=[5500, 3500],
         yr=[5, 0],
         vt='vmicro'):
    '''
    auxiliary plotting routine for vmicro, vmacro plots
    '''
    fig = plt.figure(figsize=(14, 8))
    y, x = np.mgrid[yr[1]:yr[0]:200j, xr[1]:xr[0]:200j]
    ax = fig.add_subplot(3, 2, 1)
    plots.plotc(ax,
                teff,
                logg,
                10.**v,
                xr=xr,
                yr=yr,
                zr=vr,
                xt='Teff',
                yt='log g',
                zt=vt,
                colorbar=True,
                size=15)
    ax.imshow(10.**fit2d(x, y),
              extent=[xr[1], xr[0], yr[1], yr[0]],
              aspect='auto',
              vmin=0,
              vmax=vr[1],
              origin='lower')

    ax = fig.add_subplot(3, 2, 2)
    plots.plotc(ax,
                teff,
                logg,
                10.**v,
                xr=xr,
                yr=yr,
                zr=vr,
                xt='Teff',
                yt='log g',
                zt=vt,
                colorbar=True,
                size=15)
    ax.imshow(10.**fit1d(y),
              extent=[xr[1], xr[0], yr[1], yr[0]],
              aspect='auto',
              vmin=0,
              vmax=vr[1],
              origin='lower')

    ax = fig.add_subplot(3, 2, 3)
    plots.plotc(ax,
                logg,
                10.**fit2d(teff, logg),
                mh,
                xr=[0, 5],
                yr=vr,
                zr=[-2.5, 0.5],
                size=10,
                colorbar=True,
                xt='log g',
                yt=vt,
                zt='[M/H]')

    ax = fig.add_subplot(3, 2, 4)
    plots.plotc(ax,
                logg,
                10.**fit1d(logg),
                mh,
                xr=[0, 5],
                yr=vr,
                zr=[-2.5, 0.5],
                size=10,
                colorbar=True,
                xt='log g',
                yt=vt,
                zt='[M/H]')
    x = np.arange(0, 5, 0.01)
    plots.plotl(ax, x,
                10.**(0.226 - 0.0228 * x + 0.0297 * x**2 - 0.0113 * x**3))

    ax = fig.add_subplot(3, 2, 5)
    plots.plotc(ax,
                logg,
                10.**v,
                mh,
                xr=[0, 5],
                yr=vr,
                zr=[-2.5, 0.5],
                size=10,
                colorbar=True,
                xt='log g',
                yt=vt,
                zt='[M/H]')

    ax = fig.add_subplot(3, 2, 6)
    plots.plotc(ax,
                logg,
                10.**v,
                meanfib,
                xr=[0, 5],
                yr=vr,
                zr=[0, 300],
                size=10,
                colorbar=True,
                xt='log g',
                yt=vt,
                zt='<fiber>')

    plt.show()
Exemple #14
0
def plotparamdiffs(data, bdata, title=None, cal=False, out=None, elem=True):
    """ Plot parameter differences between two different runs
    """

    a = data[1].data
    b = bdata[1].data
    paramnames, tagnames, flagnames = aspcap.params()
    i1, i2 = match.match(a['APOGEE_ID'], b['APOGEE_ID'])
    print('number of matching stars: ', len(i1))

    # parameters first
    if cal: param = 'PARAM'
    else: param = 'FPARAM'
    grid = []
    yt = []
    for i in range(8):
        if i == 0:
            yr = [-200, 200]
        elif i == 1:
            xr = [-0.5, 5]
            yr = [-0.5, 0.5]
        else:
            xr = [-2.5, 1.0]
            yr = [-0.5, 0.5]
        row = []
        yt.append(paramnames[i])
        for j in range(3):
            fig, ax = plots.multi(1, 2, hspace=0.001)
            if j == 0:
                plots.plotc(ax[0],
                            a['FPARAM'][i1, 0],
                            b['FPARAM'][i2, i] - a['FPARAM'][i1, i],
                            a['FPARAM'][i1, 3],
                            yt=r'$\Delta$' + tagnames[i],
                            xt='Teff',
                            yr=yr,
                            xr=[3000, 8000],
                            zr=[-2, 0.5])
                plots.plotc(ax[1],
                            a['PARAM'][i1, 0],
                            b['PARAM'][i2, i] - a['PARAM'][i1, i],
                            a['PARAM'][i1, 3],
                            yt=r'$\Delta$' + tagnames[i],
                            xt='Teff',
                            yr=yr,
                            xr=[3000, 8000],
                            zr=[-2, 0.5])
            elif j == 1:
                plots.plotc(ax[0],
                            a['FPARAM'][i1, 1],
                            b['FPARAM'][i2, i] - a['FPARAM'][i1, i],
                            a['FPARAM'][i1, 3],
                            yt=r'$\Delta$' + tagnames[i],
                            xt='log g',
                            yr=yr,
                            xr=[-1, 6],
                            zr=[-2, 0.5])
                plots.plotc(ax[1],
                            a['PARAM'][i1, 1],
                            b['PARAM'][i2, i] - a['PARAM'][i1, i],
                            a['PARAM'][i1, 3],
                            yt=r'$\Delta$' + tagnames[i],
                            xt='log g',
                            yr=yr,
                            xr=[-1, 6],
                            zr=[-2, 0.5])
            elif j == 2:
                plots.plotc(ax[0],
                            a['FPARAM'][i1, 3],
                            b['FPARAM'][i2, i] - a['FPARAM'][i1, i],
                            a['FPARAM'][i1, 3],
                            yt=r'$\Delta$' + tagnames[i],
                            xt='[M/H]',
                            yr=yr,
                            xr=[-2.5, 1.0],
                            zr=[-2, 0.5])
                plots.plotc(ax[1],
                            a['PARAM'][i1, 3],
                            b['PARAM'][i2, i] - a['PARAM'][i1, i],
                            a['PARAM'][i1, 3],
                            yt=r'$\Delta$' + tagnames[i],
                            xt='[M/H]',
                            yr=yr,
                            xr=[-2.5, 1.0],
                            zr=[-2, 0.5])
            ax[0].text(0.1, 0.9, 'Uncalibrated', transform=ax[0].transAxes)
            ax[1].text(0.1, 0.9, 'Calibrated', transform=ax[1].transAxes)
            if out is not None:
                outfile = out + 'paramdiffs_{:1d}_{:1d}.png'.format(i, j)
                fig.savefig(outfile)
                row.append(os.path.basename(outfile))
            else:
                pdb.set_trace()
            plt.close(fig)
        grid.append(row)
    ptab = html.table(grid, ytitle=yt)
    html.htmltab(grid, file=out + 'paramdiffs.html', ytitle=yt)

    # now elements
    if elem:
        grid = []
        yt = []
        elemnames = data[3].data['ELEM_SYMBOL'][0]
        belemnames = bdata[3].data['ELEM_SYMBOL'][0]
        if cal: elem = 'ELEM'
        else: elem = 'FELEM'
        for i, el in enumerate(elemnames):
            ii = np.where(belemnames == el)[0]
            if len(ii) > 0:
                yr = [-0.5, 0.5]
                row = []
                yt.append(el)
                if len(a[elem].shape) == 3: abun = a[elem][i1, 0, i]
                else: abun = a[elem][i1, i]
                if len(b[elem].shape) == 3: abun_b = b[elem][i2, 0, ii]
                else: abun_b = b[elem][i2, i]
                for j in range(3):
                    fig, ax = plots.multi(1, 2)
                    if j == 0:
                        plots.plotc(ax[0],
                                    a['FPARAM'][i1, 0],
                                    abun_b - abun,
                                    a['FPARAM'][i1, 3],
                                    yt=r'$\Delta$' + el,
                                    xt='Teff',
                                    yr=yr,
                                    xr=[3000, 8000],
                                    zr=[-2, 0.5])
                        plots.plotc(ax[1],
                                    a['PARAM'][i1, 0],
                                    abun_b - abun,
                                    a['PARAM'][i1, 3],
                                    yt=r'$\Delta$' + el,
                                    xt='Teff',
                                    yr=yr,
                                    xr=[3000, 8000],
                                    zr=[-2, 0.5])
                    elif j == 1:
                        plots.plotc(ax[0],
                                    a['FPARAM'][i1, 1],
                                    abun_b - abun,
                                    a['FPARAM'][i1, 3],
                                    yt=r'$\Delta$' + el,
                                    xt='log g',
                                    yr=yr,
                                    xr=[-1, 6],
                                    zr=[-2, 0.5])
                        plots.plotc(ax[1],
                                    a['PARAM'][i1, 1],
                                    abun_b - abun,
                                    a['PARAM'][i1, 3],
                                    yt=r'$\Delta$' + el,
                                    xt='log g',
                                    yr=yr,
                                    xr=[-1, 6],
                                    zr=[-2, 0.5])
                    elif j == 2:
                        plots.plotc(ax[0],
                                    a['FPARAM'][i1, 3],
                                    abun_b - abun,
                                    a['FPARAM'][i1, 3],
                                    yt=r'$\Delta$' + el,
                                    xt='[M/H]',
                                    yr=yr,
                                    xr=[-2.5, 1.0],
                                    zr=[-2, 0.5])
                        plots.plotc(ax[1],
                                    a['PARAM'][i1, 3],
                                    abun_b - abun,
                                    a['PARAM'][i1, 3],
                                    yt=r'$\Delta$' + el,
                                    xt='[M/H]',
                                    yr=yr,
                                    xr=[-2.5, 1.0],
                                    zr=[-2, 0.5])
                    ax[0].text(0.1,
                               0.9,
                               'Uncalibrated',
                               transform=ax[0].transAxes)
                    ax[1].text(0.1,
                               0.9,
                               'Calibrated',
                               transform=ax[1].transAxes)
                    if out is not None:
                        outfile = out + el + '_diff_{:1d}.png'.format(j)
                        fig.savefig(outfile)
                        row.append(os.path.basename(outfile))
                    else:
                        pdb.set_trace()
                    plt.close(fig)
                grid.append(row)
        etab = html.table(grid, ytitle=yt)
        html.htmltab(grid, file=out + 'elemdiffs.html', ytitle=yt)

    # HR diagrams
    grid = []
    row = []
    aspcap.hr(a[i1], hard=out + 'hr_match1.png', grid=True, size=1)
    row.append(os.path.basename(out + 'hr_match1.png'))
    aspcap.hr(b[i2], hard=out + 'hr_match2.png', grid=True, size=1)
    row.append(os.path.basename(out + 'hr_match2.png'))
    grid.append(row)
    row = []
    aspcap.hr(a[i1],
              hard=out + 'hrcal_match1.png',
              param='PARAM',
              grid=True,
              size=1)
    row.append(os.path.basename(out + 'hrcal_match1.png'))
    aspcap.hr(b[i2],
              hard=out + 'hrcal_match2.png',
              param='PARAM',
              grid=True,
              size=1)
    row.append(os.path.basename(out + 'hrcal_match2.png'))
    grid.append(row)
    hrtab = html.table(grid)

    fp = html.head(out + 'diffs.html')
    fp.write('<h2> HR diagrams, raw(top), calibrated(bottom)')
    fp.write(hrtab)
    fp.write('<h2> Parameter differences as f(Teff, logg, [M/H]')
    fp.write(ptab)
    fp.write('<h2> Abundance differences as f(Teff, logg, [M/H]')
    if elem: fp.write(etab)
    html.tail(fp)
    return
Exemple #15
0
def comp(file,
         true=None,
         truespec=None,
         hard=False,
         plot=False,
         minchi2=0.,
         testid=None,
         rot=False):
    """ Compare input parameters with output results
    """
    if true is None: true = file + '.ipf'
    if rot:
        names = [
            'id', 'vmicro', 'cm', 'nm', 'am', 'vrot', 'mh', 'logg', 'teff'
        ]
        names_spm = [
            'id', 'vmicro', 'cm', 'nm', 'am', 'vrot', 'mh', 'logg', 'teff',
            'evmicro', 'ecm', 'enm', 'eam', 'evrot', 'emh', 'elogg', 'eteff',
            'a', 'b', 'chi2'
        ]
    else:
        names = ['id', 'vmicro', 'cm', 'nm', 'am', 'mh', 'logg', 'teff']
        names_spm = [
            'id', 'vmicro', 'cm', 'nm', 'am', 'mh', 'logg', 'teff', 'evmicro',
            'ecm', 'enm', 'eam', 'emh', 'elogg', 'eteff', 'a', 'b', 'chi2'
        ]
    true = ascii.read(true, names=names)
    ##spec=np.loadtxt('test.dat')

    obs = ascii.read(file + '.spm')
    for i in range(len(names_spm)):
        obs.rename_column('col{:d}'.format(i + 1), names_spm[i])
    i1, i2 = match.match(true['id'], obs['id'])

    # write out file with differences
    f = open(file + '.out', 'w')
    for i in range(len(i1)):
        f.write(('{:<15s}' + '{:7.2f}' * 14 + '\n').format(
            true[i1[i]]['id'], true[i1[i]]['teff'], true[i1[i]]['logg'],
            true[i1[i]]['mh'], true[i1[i]]['am'], true[i1[i]]['cm'],
            true[i1[i]]['nm'], true[i1[i]]['vmicro'],
            obs[i2[i]]['teff'] - true[i1[i]]['teff'],
            obs[i2[i]]['logg'] - true[i1[i]]['logg'],
            obs[i2[i]]['mh'] - true[i1[i]]['mh'],
            obs[i2[i]]['am'] - true[i1[i]]['am'],
            obs[i2[i]]['cm'] - true[i1[i]]['cm'],
            obs[i2[i]]['nm'] - true[i1[i]]['nm'],
            obs[i2[i]]['vmicro'] - true[i1[i]]['vmicro']))
    f.close()

    # histogram of differences, in linear and log histograms
    fig, ax = plots.multi(8,
                          2,
                          wspace=0.001,
                          hspace=0.001,
                          figsize=(12, 4),
                          xtickrot=60)
    for iy, log in enumerate([False, True]):
        ax[iy, 0].hist(dclip(obs[i2]['teff'] - true[i1]['teff'],
                             lim=[-200, 200]),
                       bins=np.arange(-200, 201, 10),
                       histtype='step',
                       log=log)
        ax[iy, 0].set_xlabel('$\Delta$Teff')
        ax[iy, 1].hist(dclip(obs[i2]['logg'] - true[i1]['logg']),
                       bins=np.arange(-0.5, 0.51, 0.01),
                       histtype='step',
                       log=log)
        ax[iy, 1].set_xlabel('$\Delta$logg')
        ax[iy, 2].hist(dclip(obs[i2]['mh'] - true[i1]['mh']),
                       bins=np.arange(-0.5, 0.51, 0.01),
                       histtype='step',
                       log=log)
        ax[iy, 2].set_xlabel('$\Delta$[M/H]')
        ax[iy, 3].hist(dclip(obs[i2]['am'] - true[i1]['am']),
                       bins=np.arange(-0.5, 0.51, 0.01),
                       histtype='step',
                       log=log)
        ax[iy, 3].set_xlabel('$\Delta$[alpha/M]')
        ax[iy, 4].hist(dclip(obs[i2]['cm'] - true[i1]['cm']),
                       bins=np.arange(-0.5, 0.51, 0.01),
                       histtype='step',
                       log=log)
        ax[iy, 4].set_xlabel('$\Delta$[C/M]')
        ax[iy, 5].hist(dclip(obs[i2]['nm'] - true[i1]['nm']),
                       bins=np.arange(-0.5, 0.51, 0.01),
                       histtype='step',
                       log=log)
        ax[iy, 5].set_xlabel('$\Delta$[N/M]')
        ax[iy, 6].hist(dclip(obs[i2]['vmicro'] - true[i1]['vmicro']),
                       bins=np.arange(-0.5, 0.51, 0.01),
                       histtype='step',
                       log=log)
        ax[iy, 6].set_xlabel('$\Delta$vmicro')
        ax[iy, 7].hist(dclip(10.**obs['chi2'], lim=[0, 50]),
                       bins=np.arange(0, 51, 0.1),
                       log=log)
        ax[iy, 7].set_xlabel('chi2')
    fig.suptitle(file)
    if hard:
        fig.savefig(file + '.png')
        plt.close()

    # plots of differences vs Teff, color-coded by various quantities
    fig, ax = plots.multi(6,
                          7,
                          hspace=0.001,
                          wspace=0.001,
                          figsize=(16, 8),
                          xtickrot=60)
    x = true['teff'][i1] + np.random.uniform(-45., 45., size=len(i1))
    for ix in range(6):
        yt = ''
        if ix == 0:
            z = true['logg'][i1]
            tit = 'color: logg'
            zr = [0, 5]
        elif ix == 1:
            z = true['mh'][i1]
            tit = 'color: [M/H]'
            zr = [-1.5, 0.5]
        elif ix == 2:
            z = true['mh'][i1] + true['am'][i1]
            tit = 'color: [alpha/H]'
            zr = [-1.5, 1.5]
        elif ix == 3:
            z = true['mh'][i1] + true['cm'][i1]
            tit = 'color: [C/H]'
            zr = [-1.5, 1.5]
        elif ix == 4:
            z = true['mh'][i1] + true['nm'][i1]
            tit = 'color: [N/H]'
            zr = [-1.5, 1.5]
        elif ix == 5:
            z = obs['chi2'][i2]
            tit = 'color: chi2'
            zr = [0, 10.]
        ax[0, ix].set_title(tit)
        if ix == 0:
            plots.plotc(ax[0, ix],
                        x,
                        obs['teff'][i2] - true['teff'][i1],
                        z,
                        xt='Teff',
                        yt=r'$\Delta$Teff',
                        yr=[-1000, 1000],
                        zr=zr)
            plots.plotc(ax[1, ix],
                        x,
                        obs['logg'][i2] - true['logg'][i1],
                        z,
                        xt='Teff',
                        yt=r'$\Delta$logg',
                        yr=[-2.0, 2.0],
                        zr=zr)
            plots.plotc(ax[2, ix],
                        x,
                        obs['mh'][i2] - true['mh'][i1],
                        z,
                        xt='Teff',
                        yt=r'$\Delta$[M/H]',
                        yr=[-0.5, 0.5],
                        zr=zr)
            plots.plotc(ax[3, ix],
                        x,
                        obs['am'][i2] - true['am'][i1],
                        z,
                        xt='Teff',
                        yt=r'$\Delta$[a/M]',
                        yr=[-0.5, 0.5],
                        zr=zr)
            plots.plotc(ax[4, ix],
                        x,
                        obs['cm'][i2] - true['cm'][i1],
                        z,
                        xt='Teff',
                        yt=r'$\Delta$[C/M]',
                        yr=[-1.5, 1.5],
                        zr=zr)
            plots.plotc(ax[5, ix],
                        x,
                        obs['nm'][i2] - true['nm'][i1],
                        z,
                        xt='Teff',
                        yt=r'$\Delta$[N/M]',
                        yr=[-1.5, 1.5],
                        zr=zr)
            plots.plotc(ax[6, ix],
                        x,
                        10.**obs['vmicro'][i2] - 10.**true['vmicro'][i1],
                        z,
                        xt='Teff',
                        yt=r'$\Delta$vmicro',
                        yr=[-1.0, 1.0],
                        zr=zr)
        else:
            plots.plotc(ax[0, ix],
                        x,
                        obs['teff'][i2] - true['teff'][i1],
                        z,
                        xt='Teff',
                        yr=[-1000, 1000],
                        zr=zr)
            plots.plotc(ax[1, ix],
                        x,
                        obs['logg'][i2] - true['logg'][i1],
                        z,
                        xt='Teff',
                        yr=[-2, 2],
                        zr=zr)
            plots.plotc(ax[2, ix],
                        x,
                        obs['mh'][i2] - true['mh'][i1],
                        z,
                        xt='Teff',
                        yr=[-0.5, 0.5],
                        zr=zr)
            plots.plotc(ax[3, ix],
                        x,
                        obs['am'][i2] - true['am'][i1],
                        z,
                        xt='Teff',
                        yr=[-0.5, 0.5],
                        zr=zr)
            plots.plotc(ax[4, ix],
                        x,
                        obs['cm'][i2] - true['cm'][i1],
                        z,
                        xt='Teff',
                        yr=[-1.5, 1.5],
                        zr=zr)
            plots.plotc(ax[5, ix],
                        x,
                        obs['nm'][i2] - true['nm'][i1],
                        z,
                        xt='Teff',
                        yr=[-1.5, 1.5],
                        zr=zr)
            plots.plotc(ax[6, ix],
                        x,
                        10.**obs['vmicro'][i2] - 10.**true['vmicro'][i1],
                        z,
                        xt='Teff',
                        yr=[-1.0, 1.0],
                        zr=zr)
    fig.suptitle(file)
    plt.show()
    if hard:
        fig.savefig(file + '_2.png')
        plt.close()
    plt.show()

    if plot:
        pdb.set_trace()
        obsspec = np.loadtxt(file + '.mdl')
        if truespec is None: truespec = file + '.frd'
        truespec = np.loadtxt(truespec)
        if testid is None: testid = range(1, len(i1) + 1)
        for tid in testid:
            i = np.where(
                np.core.defchararray.find(obs['id'][i2], 'test' +
                                          str(tid)) >= 0)[0][0]
            if 10.**obs['chi2'][i2[i]] > minchi2:
                plt.clf()
                plt.plot(truespec[i1[i], :], color='b')
                plt.plot(obsspec[i2[i], :], color='r')
                plt.plot(obsspec[i2[i], :] / truespec[i1[i], :] + 0.1,
                         color='g')
                plt.draw()
                print(true['id'][i1[i]])
                print(
                    '{:8.1f}{:7.2f}{:7.2f}{:7.2f}{:7.2f}{:7.2f}{:7.2f}'.format(
                        true['teff'][i1[i]], true['logg'][i1[i]],
                        true['mh'][i1[i]], true['am'][i1[i]],
                        true['cm'][i1[i]], true['nm'][i1[i]],
                        true['vmicro'][i1[i]]))
                print(
                    '{:8.1f}{:7.2f}{:7.2f}{:7.2f}{:7.2f}{:7.2f}{:7.2f}{:10.2f}'
                    .format(obs['teff'][i2[i]], obs['logg'][i2[i]],
                            obs['mh'][i2[i]], obs['am'][i2[i]],
                            obs['cm'][i2[i]], obs['nm'][i2[i]],
                            obs['vmicro'][i2[i]], obs['chi2'][i2[i]]))
                pdb.set_trace()
Exemple #16
0
def plotelem_errs(hdulist, title=None, out=None, calib=False):
    """ Plot uncertainties
    """
    a = hdulist[1].data
    els = hdulist[3].data['ELEM_SYMBOL'][0]
    etoh = hdulist[3].data['ELEMTOH'][0]
    grid = []
    yt = []
    dwarfs = np.where(a['FPARAM'][:, 1] > 3.8)[0]
    giants = np.where(a['FPARAM'][:, 1] < 3.8)[0]

    if calib: param = 'PARAM'
    else: param = 'FPARAM'
    for iel, el in enumerate(els):
        gfig, gax = plots.multi(1, 2, hspace=0.001)
        plots.plotc(gax[0],
                    a[param][giants, 0],
                    a['X_H_ERR'][giants, iel],
                    a[param][giants, 3],
                    xr=[3000, 7000],
                    zr=[-2.5, 1.0],
                    yr=[0.0, 0.5],
                    xt='Teff',
                    colorbar=True,
                    zt='[M/H]',
                    yt=el,
                    size=1)
        try:
            plots.plotc(gax[1],
                        a[param][giants, 0],
                        a['FELEM_ERR'][giants, 0, iel],
                        a[param][giants, 3],
                        xr=[3000, 7000],
                        zr=[-2.5, 1.0],
                        yr=[0.0, 0.5],
                        xt='Teff',
                        colorbar=True,
                        zt='[M/H]',
                        yt=el,
                        size=1)
        except:
            plots.plotc(gax[1],
                        a[param][giants, 0],
                        a['FELEM_ERR'][giants, iel],
                        a[param][giants, 3],
                        xr=[3000, 7000],
                        zr=[-2.5, 1.0],
                        yr=[0.0, 0.5],
                        xt='Teff',
                        colorbar=True,
                        zt='[M/H]',
                        yt=el,
                        size=1)
        dfig, dax = plots.multi(1, 2, hspace=0.001)
        plots.plotc(dax[0],
                    a[param][dwarfs, 0],
                    a['X_H_ERR'][dwarfs, iel],
                    a[param][dwarfs, 3],
                    xr=[3000, 7000],
                    zr=[-2.5, 1.0],
                    yr=[0.0, 0.5],
                    xt='Teff',
                    colorbar=True,
                    zt='[M/H]',
                    yt=el,
                    size=1)
        try:
            plots.plotc(dax[1],
                        a[param][dwarfs, 0],
                        a['FELEM_ERR'][dwarfs, 0, iel],
                        a[param][dwarfs, 3],
                        xr=[3000, 7000],
                        zr=[-2.5, 1.0],
                        yr=[0.0, 0.5],
                        xt='Teff',
                        colorbar=True,
                        zt='[M/H]',
                        yt=el,
                        size=1)
        except:
            plots.plotc(dax[1],
                        a[param][dwarfs, 0],
                        a['FELEM_ERR'][dwarfs, iel],
                        a[param][dwarfs, 3],
                        xr=[3000, 7000],
                        zr=[-2.5, 1.0],
                        yr=[0.0, 0.5],
                        xt='Teff',
                        colorbar=True,
                        zt='[M/H]',
                        yt=el,
                        size=1)
        if out is not None:
            goutfile = out + 'giant_' + el + '_err.png'
            gfig.savefig(goutfile)
            plt.close(gfig)
            doutfile = out + 'dwarf_' + el + '_err.png'
            dfig.savefig(doutfile)
            plt.close(dfig)
            grid.append(
                [os.path.basename(goutfile),
                 os.path.basename(doutfile)])
            yt.append(el)
    if out is not None:
        html.htmltab(grid,
                     file=out + 'elem_errs.html',
                     ytitle=yt,
                     xtitle=['giants', 'dwarfs'])
Exemple #17
0
def plotcn(hdulist, title=None, out=None):
    """ Compare parameter level C/M and N/M with element level
    """
    a = hdulist[1].data
    grid = []
    yt = []
    for iel, el in enumerate(['C', 'N']):
        row = []
        xt = []
        yt.append(el)
        icol = 0
        for te, logg in zip(te_ranges, logg_ranges):
            gd = np.where((a['FPARAM'][:, 0] >= te[0])
                          & (a['FPARAM'][:, 0] <= te[1])
                          & (a['FPARAM'][:, 1] >= logg[0])
                          & (a['FPARAM'][:, 1] <= logg[1]))[0]
            if el == 'C':
                try:
                    abun = a['FELEM'][gd, 0, 0]
                except:
                    abun = a['FELEM'][gd, 0]
                pabun = a['FPARAM'][gd, 4]
            elif el == 'N':
                try:
                    abun = a['FELEM'][gd, 0, 2]
                except:
                    abun = a['FELEM'][gd, 2]
                pabun = a['FPARAM'][gd, 5]

            fig, ax = plots.multi(1, 2, hspace=0.001)
            plots.plotc(ax[0],
                        a['FPARAM'][gd, 3],
                        abun - pabun,
                        a['FPARAM'][gd, 0],
                        yr=[-0.5, 0.5],
                        zr=te,
                        xt='[M/H]',
                        colorbar=True,
                        zt='Teff',
                        yt=r'$\Delta$[' + el + '/M] (el-param)')
            ax[0].text(0.1,
                       0.9,
                       'uncalibrated params',
                       transform=ax[0].transAxes)
            plots.plotc(ax[1],
                        a['FPARAM'][gd, 3],
                        abun - pabun,
                        a['SNR'][gd],
                        yr=[-0.5, 0.5],
                        zr=[50, 200],
                        xt='[M/H]',
                        colorbar=True,
                        zt='S/N',
                        yt=r'[$\Delta$' + el + '/M] (el-param)')
            if out is not None:
                outfile = out + 'param_elem_' + el + '_{:1d}.png'.format(icol)
                fig.savefig(outfile)
                row.append(os.path.basename(outfile))
            else:
                pdb.set_trace()
            plt.close(fig)
            icol += 1
            xt.append(
                '{:6.0f}&lt;Teff&lt;{:6.0f} {:6.1f}&lt;logg&lt;{:6.1f}'.format(
                    te[0], te[1], logg[0], logg[1]))
        grid.append(row)
    html.htmltab(grid, file=out + 'cn.html', ytitle=yt, xtitle=xt)
Exemple #18
0
def plotelems(hdulist,
              title=None,
              out=None,
              calib=False,
              main=True,
              named=False):
    """ Make [X/M] vs [M/H] plots for all elements as f(Teff, logg)
    """
    a = hdulist[1].data
    if main and ('EXTRATARG' in a.columns.names):
        gd = np.where(a['EXTRATARG'] == 0)[0]
        a = a[gd]
        comment = ', main sample only'
    else:
        comment = ', full sample'

    if calib: param = 'PARAM'
    else: param = 'FPARAM'
    try:
        vhelio = a['VHELIO_AVG']
    except:
        vhelio = a['VHELIO']

    els = hdulist[3].data['ELEM_SYMBOL'][0]
    etoh = hdulist[3].data['ELEMTOH'][0]
    grid = []
    yt = []

    # for plots vs logg of solar neighborhood solar stars
    solar = np.where(
        (a['gaia_parallax_error'] / np.abs(a['gaia_parallax']) < 0.1))[0]
    distance = 1000. / a['gaia_parallax'][solar]
    x, y, z, r = lbd2xyz(a['GLON'][solar], a['GLAT'][solar], distance / 1000.)
    gd = np.where((abs(z) < 0.5) & (r > 8) & (r < 9))[0]
    solar = solar[gd]
    gd = np.where((a[param][solar, 3] >= -0.1)
                  & (a[param][solar, 3] <= 0.1))[0]
    solar = solar[gd]
    gels = list(els)
    for el in ['Fe', 'Ge', 'Rb', 'Nd', 'Yb', 'Ce']:
        gels.remove(el)
    igel = 0
    if len(gels) % 2 == 0:
        gfig, gax = plots.multi(2,
                                len(gels) // 2,
                                wspace=0.5,
                                hspace=0.001,
                                figsize=(8, 16))
    else:
        gfig, gax = plots.multi(2,
                                len(gels) // 2 + 1,
                                hspace=0.001,
                                wspace=0.001,
                                figsize=(8, 16))

    for iel, el in enumerate(els):
        if named:
            if el == 'Fe': tag = 'FE_H'
            else: tag = (el + '_FE').upper()
            abun = a[tag]
            ytit = '[' + el + '/Fe]'
        elif calib:
            abun = a['X_M'][:, iel]
            ytit = '[' + el + '/M] (cal)'
        else:
            try:
                if etoh[iel] == 1:
                    abun = a['FELEM'][:, 0, iel] - a['FPARAM'][:, 3]
                else:
                    abun = a['FELEM'][:, 0, iel]
            except:
                if etoh[iel] == 1:
                    abun = a['FELEM'][:, iel] - a['FPARAM'][:, 3]
                else:
                    abun = a['FELEM'][:, iel]
            ytit = '[' + el + '/M] (uncal)'
        row = []
        xt = []
        yt.append(el)
        icol = 0
        for te, logg in zip(te_ranges, logg_ranges):
            gd = np.where((a[param][:, 0] >= te[0]) & (a[param][:, 0] <= te[1])
                          & (a[param][:, 1] >= logg[0])
                          & (a[param][:, 1] <= logg[1]))[0]
            print(el, te, logg, len(gd))

            fig, ax = plots.multi(1, 3, hspace=0.001, figsize=(8, 8))
            plots.plotc(ax[0],
                        a[param][gd, 3],
                        abun[gd],
                        a[param][gd, 0],
                        xr=[-2.5, 1.0],
                        yr=[-0.5, 1],
                        zr=te,
                        xt='[M/H]',
                        colorbar=True,
                        zt='Teff',
                        yt=ytit)
            ax[0].text(0.1,
                       0.9,
                       'uncalibrated params' + comment,
                       transform=ax[0].transAxes)
            plots.plotc(ax[1],
                        a[param][gd, 3],
                        abun[gd],
                        a['SNR'][gd],
                        xr=[-2.5, 1.0],
                        yr=[-0.5, 1],
                        zr=[50, 200],
                        xt='[M/H]',
                        colorbar=True,
                        zt='S/N',
                        yt=ytit)
            plots.plotc(ax[2],
                        a[param][gd, 3],
                        abun[gd],
                        vhelio[gd],
                        xr=[-2.5, 1.0],
                        yr=[-0.5, 1],
                        zr=[-200, 200],
                        xt='[M/H]',
                        colorbar=True,
                        zt='vhelio',
                        yt=ytit)
            if out is not None:
                outfile = out + el + '_{:1d}.png'.format(icol)
                fig.savefig(outfile)
                row.append(os.path.basename(outfile))
            else:
                pdb.set_trace()
            plt.close(fig)
            icol += 1
            xt.append(
                '{:6.0f}&lt;Teff&lt;{:6.0f} {:6.1f}&lt;logg&lt;{:6.1f}'.format(
                    te[0], te[1], logg[0], logg[1]))
        #plot as f(Teff) for giants
        gd = np.where((a[param][:, 1] >= -1) & (a[param][:, 1] <= 3.8))[0]
        fig, ax = plots.multi(1, 3, hspace=0.001, figsize=(8, 8))
        plots.plotc(ax[0],
                    a[param][gd, 0],
                    abun[gd],
                    a[param][gd, 3],
                    xr=[3000, 5000],
                    yr=[-0.5, 1],
                    zr=[-2, 0.5],
                    xt='Teff',
                    colorbar=True,
                    zt='[M/H]',
                    yt=ytit)
        plots.plotc(ax[1],
                    a[param][gd, 0],
                    abun[gd],
                    a['SNR'][gd],
                    xr=[3000, 5000],
                    yr=[-0.5, 1],
                    zr=[50, 200],
                    xt='Teff',
                    colorbar=True,
                    zt='S/N',
                    yt=ytit)
        plots.plotc(ax[2],
                    a[param][gd, 0],
                    abun[gd],
                    vhelio[gd],
                    xr=[3000, 5000],
                    yr=[-0.5, 1],
                    zr=[-200, 200],
                    xt='Teff',
                    colorbar=True,
                    zt='vhelio',
                    yt=ytit)
        xt.append('{:6.1f}&lt;logg&lt;{:6.1f}'.format(-0.5, 3.8))
        outfile = out + el + '_teff.png'
        fig.savefig(outfile)
        plt.close(fig)
        row.append(os.path.basename(outfile))
        grid.append(row)

        if el in gels:
            print(el, igel)
            plots.plotc(gax[igel // 2, igel % 2],
                        a[param][solar, 1],
                        abun[solar],
                        a[param][solar, 3],
                        xr=[5.9, -0.9],
                        yr=[-0.39, 0.39],
                        zr=[-0.1, 0.1],
                        size=1,
                        xt='log g',
                        yt=ytit,
                        zt='[M/H]')
            igel += 1

    gfig.savefig(out + 'elem_solar_logg.png')
    if out is not None:
        html.htmltab(grid, file=out + 'elem_chem.html', ytitle=yt, xtitle=xt)
Exemple #19
0
def fit_vmacro(data,
               teffrange=[3550, 5500],
               mhrange=[-2.5, 1.],
               loggrange=[-1, 3.8],
               vrange=[1, 15],
               degree=1,
               maxerr=0.1,
               apokasc='APOKASC_cat_v3.6.0.fits',
               nopersist=False,
               reject=0,
               out='vmacro'):
    """ 
    Fit macroturbulence relation  with 1D f(log g) and 2D f(Teff, logg) fits, plots

    Args:
        data : calib structure

    Keyword args :
        degree : degree of fit (default=1)
        mhrange : range of [M/H] (default=[1.,1])
        loggrange : range of log g (default=[1.,3.8])
        vrange  : scaling range for vmacro plot, vrange[1] sets maximum good vmacro

    Returns:
        fit1d, fit2d : 1D and 2D polynomial fits
    """

    teff = data['FPARAM'][:, 0]
    logg = data['FPARAM'][:, 1]
    vmicro = data['FPARAM'][:, 2]
    mh = data['FPARAM'][:, 3]
    vmacro = data['FPARAM'][:, 7]
    verr = np.sqrt(data['FPARAM_COV'][:, 7, 7])
    try:
        meanfib = data['MEANFIB']
    except:
        meanfib = 0. * vmicro
    print('mhrange: ', mhrange)
    print('loggrange: ', loggrange)
    print('vrange: ', vrange)
    print('maxerr: ', maxerr)
    print('nopersist: ', nopersist)
    print('reject: ', reject)
    gd = np.where((mh > mhrange[0]) & (mh < mhrange[1]) & (logg > loggrange[0])
                  & (logg < loggrange[1]) & (10.**vmacro < vrange[1])
                  & (teff > teffrange[0]) & (teff < teffrange[1])
                  & (verr < maxerr))[0]
    if nopersist:
        j = np.where((data['STARFLAG'][gd] & bitmask.persist()) == 0)[0]
        gd = gd[j]

    print('len(gd)', len(gd))

    # remove non-1st generation GC stars
    gcstars = ascii.read(os.environ['IDLWRAP_DIR'] + '/data/gc_szabolcs.dat')
    bd = np.where(gcstars['pop'] != 1)[0]
    gd = [x for x in gd if data[x]['APOGEE_ID'] not in gcstars['id'][bd]]

    apokasc = fits.open(os.environ['IDLWRAP_DIR'] + '/data/' + apokasc)[1].data
    i1, i2 = match.match(data['APOGEE_ID'], apokasc['2MASS_ID'])
    rgb = np.where(apokasc['CONS_EVSTATES'][i2] == 'RGB')[0]
    rc = np.where(apokasc['CONS_EVSTATES'][i2] == 'RC')[0]
    type = mh * 0.
    type[i1[rgb]] = 1
    type[i1[rc]] = -1

    #fig,ax=plots.multi(2,2,figsize=(12,8))
    #fit1d = fit.fit1d(logg[gd], vmacro[gd],degree=degree,plot=ax[0,0],xt='log g', yt='vmacro',ydata=mh[gd],colorbar=True,zt='[M/H]',reject=reject,log=True)
    #fit1d = fit.fit1d(logg[gd], vmacro[gd],degree=degree,plot=ax[0,1],xt='log g', yt='vmacro',ydata=teff[gd],colorbar=True,zt='Teff',reject=reject,log=True)
    #fit1d = fit.fit1d(logg[gd], vmacro[gd],degree=degree,plot=ax[1,0],xt='log g', yt='vmacro',ydata=meanfib[gd],colorbar=True,zt='mean fib',reject=reject,log=True)
    #fit1d = fit.fit1d(logg[gd], vmacro[gd],degree=degree,plot=ax[1,1],xt='log g', yt='vmacro',ydata=type[gd],colorbar=True,zt='RGB/RC',reject=reject,log=True)
    #fig.tight_layout()
    #fig.savefig(out+'.png')

    #fig,ax=plots.multi(1,2)
    fig, ax = plots.multi(2, 3, figsize=(12, 8))
    print('2D logg, mh')
    fit2d = fit.fit2d(logg[gd],
                      mh[gd],
                      vmacro[gd],
                      degree=degree,
                      plot=ax[0, 0],
                      xt='log g',
                      yt='[M/H]',
                      zt='log(vmacro)',
                      reject=reject,
                      zr=[0, 15],
                      log=True)
    plots.plotc(ax[0, 1],
                teff[gd],
                10.**vmacro[gd] - 10.**fit2d(logg[gd], mh[gd]),
                mh[gd],
                xt='Teff',
                yt=r'$\Delta vmacro$',
                zt='[M/H]',
                xr=[5500, 3500],
                yr=[-10, 20],
                colorbar=True,
                yerr=verr[gd] * vmacro[gd] * math.log(10))
    parprint(
        [fit2d.parameters[0], 0., fit2d.parameters[1], fit2d.parameters[2]])

    #print('2D teff, logg')
    fit2d_b = fit.fit2d(teff[gd],
                        logg[gd],
                        vmacro[gd],
                        degree=degree,
                        plot=ax[1, 0],
                        xt='Teff',
                        yt='log g',
                        zt='log(vmacro)',
                        reject=reject,
                        xr=[6000, 3000],
                        yr=[5, -1],
                        zr=[0, 15],
                        log=True)
    plots.plotc(ax[1, 1],
                teff[gd],
                10.**vmacro[gd] - 10.**fit2d_b(teff[gd], logg[gd]),
                mh[gd],
                xt='Teff',
                yt=r'$\Delta vmacro$',
                zt='[M/H]',
                xr=[5500, 3500],
                yr=[-10, 20],
                colorbar=True,
                yerr=verr[gd] * vmacro[gd] * math.log(10))
    parprint([
        fit2d_b.parameters[0], fit2d_b.parameters[1], fit2d_b.parameters[2], 0.
    ])
    #print('2D teff, [M/H]')
    #fit2d_c = fit.fit2d(teff[gd], mh[gd], vmacro[gd],degree=degree,plot=ax[0,1],xt='Teff', yt='[M/H]',zt='log(vmacro)',reject=reject,zr=[0,15],log=True)
    #parprint([fit2d_c.parameters[0],fit2d_c.parameters[1],0.,fit2d_c.parameters[2]])

    fit1d = fit.fit1d(mh[gd],
                      vmacro[gd],
                      degree=degree,
                      plot=ax[2, 0],
                      xt='[M/H]',
                      yt='vmacro',
                      colorbar=True,
                      reject=reject,
                      log=True,
                      ydata=logg[gd],
                      yr=[0, 5],
                      zt='log g')
    parprint([fit1d.parameters[0], 0., 0., fit1d.parameters[1]])
    plots.plotc(ax[2, 1],
                teff[gd],
                10.**vmacro[gd] - 10.**fit1d(mh[gd]),
                mh[gd],
                xt='Teff',
                yt=r'$\Delta vmacro$',
                zt='[M/H]',
                xr=[5500, 3500],
                yr=[-10, 20],
                colorbar=True,
                yerr=verr[gd] * vmacro[gd] * math.log(10),
                zr=[-2, 0.5])

    #plots.plotc(ax[1,1],teff[gd],logg[gd],10.**vmacro[gd],xr=[6000,3000],yr=[5,-1],xt='Teff',yt='log g', zr=[0,15])

    # DR13 fit
    #dr13fit=models.Polynomial2D(degree=1)
    #dr13fit.parameters=[0.741,-0.0998,-0.225]
    #junk = fit.fit2d(logg[gd], mh[gd], vmacro[gd],degree=degree,plot=ax[1],xt='log g', yt='[M/H]',zt='log(vmacro)',pfit=dr13fit)
    fig.tight_layout()
    fig.savefig(out + '_2d.png')

    # single plot with 1D fit
    fig, ax = plots.multi(1, 1, figsize=(12, 4))
    fit1d = fit.fit1d(mh[gd],
                      vmacro[gd],
                      degree=degree,
                      plot=ax,
                      xt='[M/H]',
                      yt='vmacro',
                      colorbar=True,
                      reject=reject,
                      log=True,
                      ydata=logg[gd],
                      yr=[0, 5],
                      zt='log g')
    fig.tight_layout()
    fig.savefig(out + '_1d.pdf')

    #pdb.set_trace()
    #plot(teff, logg, mh, meanfib, vmacro, vrange, fit1d, fit2d, vt='vmacro')
    return
Exemple #20
0
def group(lsf, wave=None, hard=None, groups=None):
    """ Plot the FWHM from gaussian fits to the LSF of the 3 chips at column 1024
    """
    fibers = np.arange(1, 301)
    fwhm, r = modelmap(lsf,
                       waveframe=wave,
                       cols=[512, 1024, 1536],
                       fibers=fibers,
                       smooth=0)
    plt.close()
    plt.close()
    fig, ax = plots.multi(2, 2, figsize=(8, 6))
    plots.plotc(ax[0, 0],
                fwhm[:, 1, 0],
                fwhm[:, 1, 1],
                fibers,
                xr=[2.3, 3.8],
                yr=[1.8, 3.0],
                xt='red fwhm, col 1024',
                yt='green fwhm, col 1024')
    ax[0, 0].set_title('Color coded by fiber')
    plots.plotc(ax[0, 1],
                fwhm[:, 1, 1],
                fwhm[:, 1, 2],
                fibers,
                xr=[2.0, 3.5],
                yr=[1.8, 3.0],
                xt='green fwhm, col 1024',
                yt='blue fwhm, col 1024')
    ax[0, 1].set_title('Color coded by fiber')
    ax[0, 0].grid()
    ax[0, 1].grid()
    ax[1, 0].grid()
    ax[1, 1].grid()
    rfig, rax = plots.multi(2, 2, figsize=(8, 6))
    plots.plotc(rax[0, 0],
                r[:, 1, 0],
                r[:, 1, 1],
                fibers,
                xr=[18000, 28000],
                yr=[18000, 28000],
                xt='red R, col 1024',
                yt='green R, col 1024')
    rax[0, 0].set_title('Color coded by fiber')
    plots.plotc(rax[0, 1],
                r[:, 1, 1],
                r[:, 1, 2],
                fibers,
                xr=[18000, 28000],
                yr=[18000, 28000],
                xt='green R, col 1024',
                yt='blue R, col 1024')
    rax[0, 1].set_title('Color coded by fiber')
    rax[0, 0].grid()
    rax[0, 1].grid()
    rax[1, 0].grid()
    rax[1, 1].grid()

    # do a Kmeans analysis to split into LSF groups and plot
    X = np.squeeze(fwhm[:, 1, :])
    km = KMeans(n_clusters=4)
    labels = km.fit_predict(X)
    for j in range(3):
        plots.plotc(ax[1, 0],
                    fwhm[:, j, 0],
                    fwhm[:, j, 1],
                    labels,
                    xr=[2.3, 3.8],
                    yr=[1.8, 3.0],
                    xt='red fwhm, col 1024',
                    yt='green fwhm, col 1024')
        plots.plotc(ax[1, 1],
                    fwhm[:, j, 1],
                    fwhm[:, j, 2],
                    labels,
                    xr=[2.0, 3.5],
                    yr=[1.8, 3.0],
                    xt='green fwhm, col 1024',
                    yt='blue fwhm, col 1024')
        plots.plotc(rax[1, 0],
                    r[:, j, 0],
                    r[:, j, 1],
                    labels,
                    xr=[18000, 28000],
                    yr=[18000, 28000],
                    xt='red R, col 1024',
                    yt='green R, col 1024')
        plots.plotc(rax[1, 1],
                    r[:, j, 1],
                    r[:, j, 2],
                    labels,
                    xr=[18000, 28000],
                    yr=[18000, 28000],
                    xt='green R, col 1024',
                    yt='blue R, col 1024')
    ax[1, 0].set_title('Color coded by group')
    ax[1, 1].set_title('Color coded by group')
    rax[1, 0].set_title('Color coded by group')
    rax[1, 1].set_title('Color coded by group')
    gfig, gax = plots.multi(1, 1)
    plots.plotp(gax, fibers, labels)
    if groups is not None:
        # show default groups if input
        for start in groups:
            gax.plot([start - 0.5, start - 0.5], [-1, 5], color='k')

    if hard is not None:
        fig.tight_layout()
        fig.savefig(hard + '.png')
        rfig.tight_layout()
        rfig.savefig(hard + '_r.png')
        gfig.savefig(hard + '_group.png')
        plt.close()
        plt.close()
        plt.close()

    return fwhm
Exemple #21
0
def litplot(mass=1, feh=0, out='litvmacro'):
    '''
    Plots literature vmacro relations
    '''
    #    parser = argparse.ArgumentParser()
    #    parser.add_argument("--mass",default=1.)
    #    parser.add_argument("--feh",default=0.)
    #    args=parser.parse_args()
    #    print('Using mass: ', args.mass)
    #    mass=float(args.mass)
    #    feh=float(args.feh)
    #    print('Using feh: ', feh)
    #    mass = 0.9

    # setup plots
    mpl.rcParams['xtick.labelsize'] = 8
    mpl.rcParams['ytick.labelsize'] = 8

    p, ax = plots.multi(2, 2)
    lw = 0
    ax[0, 0].set_title('Massarotti vmacro')
    ax[1, 0].set_title('Shetrone vmacro')
    ax[0, 1].set_title('Bergemann vmacro')
    ax[1, 1].set_title('Bergemann vmacro new')

    # L = 4*pi*R^2*sigma*Teff^4
    # R^2 = L / (4 pi sigma Teff^4)
    # log R^2 = log L - log (4*pi*sigma) - 4 logte
    # logg = log G + log M - log L + log(4*pi*sigma) + 4 log te
    sigma = const.sigma_sb.cgs.value
    G = const.G.cgs.value
    lsun = const.L_sun.cgs.value
    msun = const.M_sun.cgs.value
    m = mass * msun
    # get isochrone data
    for iso in [0., -1., -2.]:
        if iso >= 0:
            c = 'p'
        else:
            c = 'm'
        cfeh = 'z' + c + '{:02d}'.format(int(round(abs(iso) * 10)))
        print(cfeh)
        a = ascii.read(os.getenv('ISOCHRONE_DIR') + '/' + cfeh + '.dat')
        age = a['col2']
        j = np.where((age == 9.6) | (age == 9.0) | (age == 10.0))
        logl = a['col5'][j]
        iso_te = 10.**a['col6'][j]
        iso_logg = a['col7'][j]
        iso_feh = iso_logg * 0. + iso
        iso_logl = 4 * np.log10(iso_te) - iso_logg - np.log10(lsun) + np.log10(
            4 * np.pi * sigma * G) + np.log10(m)

        vm = vmacro_massarotti(iso_te, iso_logl, iso_feh)
        plots.plotc(ax[0, 0],
                    iso_te,
                    iso_logg,
                    vm,
                    xr=[6000, 3000],
                    yr=[5, -1.],
                    zr=[0, 15],
                    linewidth=lw,
                    size=15)
        vm = vmacro_shetrone(iso_te, iso_logl, iso_feh)
        plots.plotc(ax[1, 0],
                    iso_te,
                    iso_logg,
                    vm,
                    xr=[6000, 3000],
                    yr=[5, -1.],
                    zr=[0, 15],
                    linewidth=lw,
                    size=15)
        vm = vmacro_bergemann(iso_te, iso_logg, iso_feh)
        plots.plotc(ax[0, 1],
                    iso_te,
                    iso_logg,
                    vm,
                    xr=[6000, 3000],
                    yr=[5, -1.],
                    zr=[0, 15],
                    linewidth=lw,
                    size=15)
        vm = vmacro_bergemann_new(iso_te, iso_logg, iso_feh)
        plots.plotc(ax[1, 1],
                    iso_te,
                    iso_logg,
                    vm,
                    xr=[6000, 3000],
                    yr=[5, -1.],
                    zr=[0, 15],
                    linewidth=lw,
                    size=15)

    # setup grids for Teff and logg
    o = np.ones([61, 31])
    teff = o * (np.arange(31) * 100.) + 3000
    logg = np.transpose(np.transpose(o) * (np.arange(61) * .1)) - 1.
    feh = o * 0. + feh
    logl = 4 * np.log10(teff) - logg - np.log10(lsun) + np.log10(
        4 * np.pi * sigma * G) + np.log10(m)

    a = vmacro_massarotti(teff, logl, feh)
    #axim=ax[0,0].imshow(np.fliplr(a),vmin=0,vmax=10,extent=[7500,2500.,5.,-1.],aspect='auto',origin='upper')
    #p.colorbar(axim)

    a = vmacro_shetrone(teff, logl, feh)
    #axim=ax[1,0].imshow(np.fliplr(a),vmin=0,vmax=10,extent=[7500,2500.,5.,-1.],aspect='auto',origin='upper')
    #p.colorbar(axim)

    a = vmacro_bergemann(teff, logg, feh)
    #axim=ax[0,1].imshow(np.fliplr(a),vmin=0,vmax=10,extent=[7500.,2500.,5.,-1.],aspect='auto',origin='upper')
    #p.colorbar(axim)

    a = vmacro_bergemann_new(teff, logg, feh)
    #axim=ax[1,1].imshow(np.fliplr(a),vmin=0,vmax=10,extent=[7500.,2500.,5.,-1.],aspect='auto',origin='upper')
    #p.colorbar(axim)

    p.tight_layout()
    p.savefig(out + '_' + str(mass) + '.png')
Exemple #22
0
def docal(infile,clobber=False,hr=True,teff=True,logg=True,vmicro=True,vmacro=True,elemcal=True,out=None,stp=False,cal='dr14',calib=False) :
    '''
    Derives all calibration relations and creates plots of them, as requested
    '''

    # output subdirectory
    try: os.mkdir('cal')
    except: pass
    print(os.getcwd())

    c=fits.open(infile)

    figs=[]
    ytitle=[]
    # HR diagram
    if hr :
        reload(apselect)
        fig,ax=plots.multi(1,1)
        if calib : param='PARAM'
        else : param='FPARAM'
        plots.plotc(ax,c[param][:,0],c[param][:,1],c[param][:,3],xr=[6000,3000],yr=[5,-1],zr=[-2,0.5])
        plt.savefig(out+'hr.png')                                                                                                 
        figs.append(['hr.png','hr.png'])
        ytitle.append('HR')

    allcal={}
    # Teff vs photometric
    if teff :
        allcal['teffcal'] = teffcomp.ghb(c[1].data,ebvmax=0.02,glatmin=10,out=out+'tecal',yr=[-750,750],trange=[4500,7000],loggrange=[-1,6],calib=calib)
        allcal['giant_teffcal'] = teffcomp.ghb(c[1].data,ebvmax=0.02,glatmin=10,out=out+'giant_tecal',yr=[-750,750],loggrange=[-1,3.8],calib=calib)
        allcal['dwarf_teffcal'] = teffcomp.ghb(c[1].data,ebvmax=0.02,glatmin=10,trange=[4500,7000],out=out+'dwarf_tecal',yr=[-750,750],loggrange=[3.8,6],calib=calib)
        if out is not None :
            struct.wrfits(struct.dict2struct(allcal['teffcal']),out+'all_tecal.fits')
            struct.wrfits(struct.dict2struct(allcal['giant_teffcal']),out+'giant_tecal.fits')
            struct.wrfits(struct.dict2struct(allcal['dwarf_teffcal']),out+'dwarf_tecal.fits')
        if stp : pdb.set_trace()
    figs.append(['tecal.png','tecal_b.png'])
    ytitle.append('Teff all together')
    figs.append(['giant_tecal.png','dwarf_tecal.png'])
    ytitle.append('Teff, giants and dwarfs')
    figs.append(['giant_tecal_b.png','dwarf_tecal_b.png'])
    ytitle.append('Teff, giants and dwarfs')

    # log g vs asteroseismic
    if logg :
        allcal['rgbrcsep' ] = loggcomp.rcrgb(c[1].data,out=out+'rcrgbsep')
        allcal['giant_loggcal'] = loggcomp.apokasc(c[1].data,plotcal=False,out=out+'rcrgb_loggcal',calib=calib)
        allcal['dwarf_loggcal'] = loggcomp.dwarf(c[1].data,out=out+'logg',calib=calib)
        if out is not None :
            struct.wrfits(struct.dict2struct(dict(allcal['rgbrcsep'].items()+allcal['giant_loggcal'].items())),
                            out+'giant_loggcal.fits')
            struct.wrfits(struct.dict2struct(allcal['dwarf_loggcal']),out+'dwarf_loggcal.fits')
        if stp : pdb.set_trace()
    figs.append(['rcrgbsep.png','rcrgb_loggcal.png'])
    ytitle.append('log g, RGB/RC')
    figs.append(['rcrgb_loggcal_b.png','logg_dwarfs.png'])
    ytitle.append('log g, RGB/RC and dwarfs')
    figs.append(['logg_all.png','logg_all.png'])
    ytitle.append('log g ')

    # vmicro calibration
    if vmicro :
        print("vmicro fit, cubic in log g, linear in [M/H]")
        print("sample limited to FERRE vmicro error <0.01 ")
        vfit.fit_vmicro(c[1].data,degree=3,reject=0.15,mhrange=[-2,1],loggrange=[-0.3,4.9],vmrange=[0,7],
                        teffrange=[3550,6500],vrange=[0.55,4],maxerr=0.01,func=vfit.vm3_1,out=out+'vmicro3_1')

        print("full sample (maxerr=0.1)")
        vfit.fit_vmicro(c[1].data,degree=3,reject=0.15,mhrange=[-2,1],loggrange=[-0.3,4.9],vmrange=[0,7],
                        teffrange=[3500,6500],vrange=[0.55,4],maxerr=0.1,func=vfit.vm3_1,out=out+'vmicro3_1all')

        #dwarfs only
        print("dwarfs only, fit as f(Teff)")
        dw=np.where(c[1].data['FPARAM'][:,1] > 4)[0]
        vfit.fit_vmicro(c[1].data,reject=0.15,mhrange=[-2,1],loggrange=[4,5],vmrange=[0,7],teffrange=[3500,8000],
                        vrange=[0.55,4],maxerr=0.1,func=vfit.vm1t,out=out+'vmicro1t')
        fig,ax=plots.multi(1,1)
        plots.plotc(ax,c[1].data['FPARAM'][dw,0],10**c[1].data['FPARAM'][dw,2],c[1].data['FPARAM'][dw,3],xr=[3500,8000],xt='Teff',
           yr=[0,4],yt='vmicro',zr=[-2,0.5],zt='[M/H]',colorbar=True)

    # vmacro
    if vmacro :
        vfit.fit_vmacro(c[1].data,mhrange=[-2.5,1],reject=0.3,maxerr=0.1,out=out+'vmacro_2d')

    # elemental abundances
    if elemcal :
        elems=np.append(c[3].data['ELEM_SYMBOL'][0],['M','alpha'])
        allcal['giant_abuncal']=elem.cal(c,c[3].data['ELEM_SYMBOL'][0],c[3].data['ELEMTOH'][0],elems,hard=out+'giants_',cal=cal,errpar=True,calib=calib)
        allcal['dwarf_abuncal']=elem.cal(c,c[3].data['ELEM_SYMBOL'][0],c[3].data['ELEMTOH'][0],elems,hard=out+'dwarfs_',dwarfs=True,cal=cal,calib=calib)
        if out is not None :
            struct.wrfits(allcal['giant_abuncal'],out+'giant_abuncal.fits')
            struct.wrfits(allcal['dwarf_abuncal'],out+'dwarf_abuncal.fits')
        if stp : pdb.set_trace()
    figs.append(['giants_all.png','dwarfs_all.png'])
    ytitle.append('clusters')
    figs.append(['giants_allsolar.png','dwarfs_allsolar.png'])
    ytitle.append('solar circle')
    figs.append(['giants_M.png','dwarfs_M.png'])
    ytitle.append('cluster [M/H]')
    figs.append(['giants_clust_key.png','dwarfs_clust_key.png'])
    ytitle.append('cluster ID')

    html.htmltab(figs,xtitle=['giants','dwarfs'],ytitle=ytitle,file=out+infile.replace('.fits','.html'))
    return allcal
Exemple #23
0
def errfit(te, snr, mh, val, snbins=np.arange(50,250,50), tebins=np.arange(3500,6000,250), mhbins=np.arange(-2.25,0.75,0.5),verbose=False,
           out=None,title='', zr=[0,0.1], snplot=True, meanerr=None,quad=False,mkhtml=True ) :
    '''
    Fits for empirical uncertainty as function of Teff, S/N, and [M/H]
    '''
    if out is not None :
        fig,ax=plots.multi(len(snbins),2,wspace=0.001,figsize=(3*len(snbins),5))

    # bin sizes and initialize data arrays
    dte = tebins[1]-tebins[0]
    dmh = mhbins[1]-mhbins[0]
    dsn = snbins[1]-snbins[0]
    rmsdata=[]
    rmsderiv=[]
    nbin=[]
    # accumulate the data: rms in bins of Teff, S/N, and [M/H]
    snmin=snbins[-1]
    snmax=snbins[0]
    temin=tebins[-1]
    temax=tebins[0]
    mhmin=mhbins[-1]
    mhmax=mhbins[0]
    npts=np.zeros([len(tebins),len(mhbins),len(snbins)])
    for imhbin,mhbin in enumerate(mhbins) :
        for itebin,tebin in enumerate(tebins) :
            for isnbin,snbin in enumerate(snbins) :
                ibin = np.where(( te > tebin) & (te <= tebin+dte) &
                                ( mh > mhbin ) & (mh <= mhbin+dmh) &
                                ( snr > snbin) & (snr <= snbin+dsn) & (val > -9990.) )[0]
                if len(ibin) > 3 :
                    npts[itebin,imhbin,isnbin] = len(ibin)
                    if meanerr is not None :
                        err = np.sqrt(np.clip(val[ibin].std()**2 - np.median(meanerr[ibin])**2,0.001,10000000.))
                    else :
                        err = val[ibin].std()
                    rmsdata.append(err)
                    if quad :
                        rmsderiv.append([1.,tebin+dte/2.-4500.,snbin+dsn/2.-100.,mhbin+dmh/2.,(tebin+dte/2.-4500.)**2])
                    else :
                        rmsderiv.append([1.,tebin+dte/2.-4500.,snbin+dsn/2.-100.,mhbin+dmh/2.])
                    if verbose :
                        print(tebin+dte/2.,snbin+dsn/2.,mhbin+dmh/2.,err,len(ibin))
                    snmin=np.array([snmin,snbin]).min()
                    snmax=np.array([snmax,snbin]).max()
                    temin=np.array([temin,tebin]).min()
                    temax=np.array([temax,tebin]).max()
                    mhmin=np.array([mhmin,mhbin]).min()
                    mhmax=np.array([mhmax,mhbin]).max()
                    if out is not None :
                        iplt=np.where(snbins == snbin)[0][0]
                        plots.plotc(ax[0,iplt],np.array([mhbin+dmh/2.]),np.array([tebin+dte/2.]),np.array([err]),
                                    xr=[mhbins[0],mhbins[-1]],yr=[tebins[0],tebins[-1]],zr=zr,size=30,linewidth=1)

    # do the fit in log(rms) so that empirical uncertainty is positive-definite
    rmsdata=np.log(np.array(rmsdata))
    rmsderiv=np.array(rmsderiv)
    if len(rmsdata) > 5 :
        soln,inv = fit.linear(rmsdata,rmsderiv.transpose())

    figs=[]
    if out is not None :
        y, x = np.mgrid[tebins[0]:tebins[-1]:200j,mhbins[0]:mhbins[-1]:200j]
        for iplt in range(len(snbins)) :
            sn = snbins[iplt]+dsn/2.
            try :
                ax[0,iplt].imshow(elemerr(soln,y-4500.,sn-100.,x, quad=quad),extent=[mhbins[0],mhbins[-1],tebins[0],tebins[-1]], 
                                  aspect='auto',vmin=zr[0],vmax=zr[1], origin='lower',cmap='rainbow')
                ax[1,iplt].imshow(npts[:,:,iplt],extent=[mhbins[0],mhbins[-1],tebins[0],tebins[-1]], 
                                  aspect='auto',vmin=0,vmax=50, origin='lower',cmap='rainbow')
            except: pass

            ax[0,iplt].text(0.98,0.98,title+' S/N={:4.0f}'.format(sn),va='top',ha='right',transform=ax[0,iplt].transAxes)

        fig.savefig(out+'_err.png')
        plt.close()
        figs.append([os.path.basename(out+'_err.png')])

        if snplot :
            fig,ax=plots.multi(len(tebins),len(mhbins),wspace=0.001,hspace=0.001,figsize=(2*len(tebins),2*len(mhbins)))
            x=np.arange(0,250)
            for ix in range(len(tebins)) :
              if ix == 0 : yt=r'$\sigma$'
              else : yt=''
              for iy in range(len(mhbins)) :
                try :
                    gdplt=np.where((np.isclose(rmsderiv[:,1]+4500,tebins[ix]+dte/2.)) & (np.isclose(rmsderiv[:,3],mhbins[iy]+dmh/2.)))[0]
                    plots.plotc(ax[iy,ix],rmsderiv[gdplt,2]+100,np.exp(rmsdata[gdplt]),rmsderiv[gdplt,3],size=30,zr=[-2,0.5],
                                yr=zr,xr=[snbins[0],snbins[-1]],xt='S/N',yt=yt)
                    ax[iy,ix].plot(x,elemerr(soln,tebins[ix]+dte/2.-4500,x-100,mhbins[iy]+dmh/2., quad=quad))
                except: pass
                ax[iy,ix].text(0.98,0.98,'{:8.0f} {:8.2f}'.format(tebins[ix]+dte/2.,mhbins[iy]+dmh/2.),ha='right',va='top',transform=ax[iy,ix].transAxes)
            fig.savefig(out+'_err_sn.png')
            plt.close()
            figs.append([os.path.basename(out+'_err_sn.png')])

        if mkhtml : html.htmltab(figs,file=out+'_err.html',header=title+' empirical uncertainties')

    if verbose : 
        print(soln)
        print(snmin,snmax,temin,temax,mhmin,mhmax)
        pdb.set_trace()

    try : return soln
    except : return 0.
def train(planfile,
          skip=1,
          threads=8,
          xh=None,
          model_name=None,
          censor=None,
          sim=False,
          gb=None,
          mh=None):
    '''
    Define training set and train Cannon
    '''

    p = yanny.yanny(planfile, np=True)
    apred = p['apred_vers'].strip("'")
    apstar = getval(p, 'apstar_vers', 'stars').strip("'")
    aspcap_vers = getval(p, 'aspcap_vers', 'aspcap').strip("'")
    results = getval(p, 'results_vers', 'results').strip("'")
    cannon = getval(p, 'cannon_vers', 'cannon_aspcap')
    if model_name is None:
        model_name = getval(p, 'model_name', 'apogee-dr14-giants')
    model_order = int(getval(p, 'model_order', '2'))
    model_scale_factor = float(getval(p, 'model_scale_factor', '1.0'))
    model_regularization = float(getval(p, 'model_regularization', '0.0'))
    threads = int(getval(p, 'ncpus', threads))
    if xh is None: xh = int(getval(p, 'xh', False))
    if censor is None: censor = int(getval(p, 'censor', False))
    logg = getrange(getval(p, 'logg', '-1 3.9'))
    teff = getrange(getval(p, 'teff', '3500 5500'))
    if gb is None: gb = getval(p, 'gb', 0)
    if mh is None: mh = getrange(getval(p, 'mh', '-3. 1.'))
    alpha = getrange(getval(p, 'alpha', '-0.5 1.'))

    # label names
    elems = aspcap.elems()[0]
    #model_labels = ['TEFF','LOGG','M_H']
    model_labels = ['TEFF', 'LOGG', 'M_H', 'ALPHA_M', 'FE_H']
    input_labels = ['TEFF', 'LOGG', 'M_H', 'ALPHA_M', 'FE_H']
    for el in elems:
        d = elem.dr14cal(el)
        if el is not 'Fe' and d['elemfit'] >= 0:
            if xh:
                model_labels.append(el.upper() + '_H')
            else:
                model_labels.append(el.upper() + '_FE')
            input_labels.append(el.upper() + '_FE')

    apl = apload.ApLoad(apred=apred,
                        apstar=apstar,
                        aspcap=aspcap_vers,
                        results=results)

    if sim:
        allstar = fits.open('allStar.fits')[1].data
        gd = apselect.select(allstar,
                             logg=logg,
                             teff=teff,
                             mh=mh,
                             alpha=alpha,
                             sn=[100, 10000])
        model_labels = ['TEFF', 'LOGG', 'M_H']
        input_labels = ['TEFF', 'LOGG', 'M_H']
        model_labels = sim
        input_labels = sim
        if gb:
            gd2 = np.where(
                np.abs((allstar['TEFF'][gd] - 3500) * 4 / 2000. -
                       allstar['LOGG'][gd]) < float(gb))[0]
            gd = gd[gd2]
    else:
        allstar = apl.allStar()[1].data
        gd = apselect.select(
            allstar,
            badval=['STAR_BAD'],
            sn=[100, 10000],
            logg=logg,
            teff=teff,
            mh=mh,
            alpha=alpha,
            badstar=['PERSIST_HIGH', 'PERSIST_MED', 'PERSIST_LOW'],
            gb=gb)

        gcstars = ascii.read(os.environ['IDLWRAP_DIR'] +
                             '/data/gc_szabolcs.dat')
        bd = np.where(gcstars['pop'] != 1)[0]
        jc = [
            x for x in gd if allstar[x]['APOGEE_ID'] not in gcstars['id'][bd]
        ]
        gd = jc

        # down select stars using HR+[M/H] sampling
        i1, i2 = cal.hrsample(allstar, allstar[gd], raw=False)
        # make sure all labels are good
        gd = []
        for i in i1:
            good = True
            for label in input_labels:
                # special handling for NA in DR14
                if label == 'NA_FE' and allstar[label][i] < -5 and allstar[
                        'FE_H'][i] < -1:
                    allstar[label][i] = 0.
                if allstar[label][i] < -5:
                    good = False
                    print('reject', allstar['APOGEE_ID'][i], label,
                          allstar[label][i])
                    break
            if good: gd.append(i)

    print('selected ', len(gd), ' training set stars')
    root = os.environ['APOGEE_ASPCAP'] + '/' + apred + '/' + cannon + '/'
    training_set = os.path.join(root,
                                "{}-training-set.fits".format(model_name))
    if not os.path.exists(os.path.dirname(training_set)):
        os.makedirs(os.path.dirname(training_set))
    struct.wrfits(np.array(allstar[gd]), training_set)

    # The label names to use in the model.

    model_filename = os.path.join(root, "{}.model".format(model_name))
    initial_filename = os.path.join(root, "{}.initial".format(model_name))

    clobber_model = True
    labelled_set = Table.read(training_set)[0:-1:skip]
    N_labelled = len(labelled_set)
    if xh:
        for el in elems:
            d = elem.dr14cal(el)
            if el is not 'Fe' and d['elemfit'] >= 0:
                labelled_set[el.upper() +
                             '_H'] = labelled_set[el.upper() +
                                                  '_FE'] + labelled_set['FE_H']

    # TODO: something's wrong with our dispersion that we extracted.
    #with open(os.path.join(CANNON_DATA_DIR, "dispersion.pkl"), "rb") as fp:
    #    dispersion = pickle.load(fp)
    #P = dispersion.size
    dispersion = None
    P = 8575  # MAGIC

    # These defaults (flux = 1, ivar = 0) will mean that even if we don't find a
    # spectrum for a single star in the training set, then that star will just have
    # no influence on the training (since ivar = 0 implies infinite error on flux).

    normalized_flux = np.ones((N_labelled, P), dtype=float)
    normalized_ivar = np.zeros((N_labelled, P), dtype=float)

    # Enable logging.
    logger = logging.getLogger("apogee.dr14.tc")
    logger.setLevel(logging.INFO)

    handler = logging.StreamHandler()
    handler.setFormatter(
        logging.Formatter("%(asctime)s [%(levelname)-8s] %(message)s"))
    logger.addHandler(handler)

    sdss_path = path.Path()
    ngd = 0
    for i, row in enumerate(labelled_set):

        logger.info("Reading labelled set spectra ({}/{})".format(
            i + 1, N_labelled))
        if row['TELESCOPE'] == 'apo1m':
            filename = sdss_path.full('cannonStar-1m',
                                      apred=apred,
                                      apstar=apstar,
                                      aspcap=aspcap_vers,
                                      results=results,
                                      cannon=cannon,
                                      field=row['FIELD'],
                                      reduction=row['REDUCTION_ID'],
                                      telescope=row['TELESCOPE'])
        else:
            filename = sdss_path.full('cannonStar',
                                      apred=apred,
                                      apstar=apstar,
                                      aspcap=aspcap_vers,
                                      results=results,
                                      cannon=cannon,
                                      field=row['FIELD'],
                                      obj=row['APOGEE_ID'],
                                      telescope=row['TELESCOPE'])

        if not os.path.exists(filename):
            logger.warn("Could not find filename for labelled set star {}: {}"\
                .format(row["APOGEE_ID"], filename))
            continue

        with open(filename, "rb") as fp:
            #flux, ivar = pickle.load(fp)
            metadata, data = pickle.load(fp)
            flux, ivar = data

        if (np.isfinite(flux).all()) & (np.isfinite(ivar).all()):
            normalized_flux[i, :] = flux
            normalized_ivar[i, :] = ivar
        else:
            print('non-finite values in', row['APOGEE_ID'])
            normalized_flux[i, :] = 0.
            normalized_ivar[i, :] = 0.
            #pdb.set_trace()

    # TODO: Cache the normalized_flux and normalized_ivar into a single file so that
    #       it is faster to read in next time?
    assert  np.isfinite(normalized_flux).all(), \
            "Non-finite values in normalized_flux!"
    assert  np.isfinite(normalized_ivar).all(), \
            "Non-finite values in normalized_ivar!"

    # Exclude labelled set stars where there is no spectrum, only because it
    # will get annoying later on when we are doing 1-to-1 and cross-validation
    keep = np.any(normalized_ivar > 0, axis=1)
    if not np.all(keep):
        logger.info(
            "Excluding {} labelled set stars where there was no information in "
            "the spectrum".format(np.sum(~keep)))
        labelled_set = labelled_set[keep]
        normalized_flux = normalized_flux[keep]
        normalized_ivar = normalized_ivar[keep]

    # Construct and train a model. #
    model = tc.L1RegularizedCannonModel(labelled_set,
                                        normalized_flux,
                                        normalized_ivar,
                                        dispersion,
                                        threads=threads)

    model.vectorizer = tc.vectorizer.NormalizedPolynomialVectorizer(
        labelled_set,
        tc.vectorizer.polynomial.terminator(model_labels, model_order),
        scale_factor=model_scale_factor)

    if censor:
        for label in model_labels:
            for el in elems:
                d = elem.dr14cal(el)
                if xh:
                    lab = el.upper() + '_H'
                else:
                    lab = el.upper() + '_FE'
                if lab == label:
                    model.censors[label] = getcensor(
                        el,
                        maskdir=os.environ['SPECLIB_DIR'] +
                        '/lib/filters_26042016/',
                        length=P)
                    print(
                        label,
                        getcensor(el,
                                  maskdir=os.environ['SPECLIB_DIR'] +
                                  '/lib/filters_26042016/'))

    model.s2 = 0
    model.regularization = model_regularization

    model.train()
    model._set_s2_by_hogg_heuristic()

    model.save(model_filename,
               include_training_data=False,
               overwrite=clobber_model)
    model.save(model_filename + '.full',
               include_training_data=True,
               overwrite=clobber_model)

    # Make some 1-to-1 plots just to show sensible behaviour.
    #X = model.labels_array()
    X = model.labels_array
    Y = model.fit(model.normalized_flux, model.normalized_ivar)
    out = Table(
        np.transpose([
            np.mean(model.labels_array, axis=0),
            np.nanmean(Y - X, axis=0),
            np.nanstd(Y - X, axis=0),
            np.array(model_labels)
        ]))
    out.write(initial_filename, overwrite=True, format='ascii')
    #np.savetxt(initial_filename, [np.mean(model.labels_array, axis=0).reshape(-1, 1), np.nanmean(Y-X,axis=0), np.nanstd(Y-X,axis=0), model_labels] )

    try:
        os.makedirs(os.path.join(root, 'plots'))
    except:
        pass
    it = model_labels.index('TEFF')
    ig = model_labels.index('LOGG')
    iz = model_labels.index('M_H')

    def plotit(ax, x, y, z, label):
        plots.plotc(ax, x, y - x, z, xt=label, yt='inferred-labelled')
        lims = ax.get_xlim()
        ax.plot(lims, [0., 0.], c="#666666", zorder=-1, linestyle=":")
        mean, rms = np.nanmean(y - x), np.nanstd(y - x)
        title = "{}: ({:.2f}, {:.2f})".format(label, mean, rms)
        ax.set_title(title)

    fig, ax = plots.multi(2, 3)
    plotit(ax[0, 0], X[:, it], Y[:, it], X[:, iz], 'TEFF')
    plotit(ax[0, 1], X[:, ig], Y[:, ig], X[:, it], 'LOGG')
    plotit(ax[1, 0], X[:, iz], Y[:, iz], X[:, it], 'M_H')
    plots.plotc(ax[1, 1],
                X[:, it],
                X[:, ig],
                X[:, iz],
                xr=[6000, 3500],
                yr=[5, -0.5],
                xt='TEFF',
                yt='LOGG')
    gd = np.where(model.normalized_ivar.flatten() > 1)[0]
    diff = np.abs(model.normalized_flux - model.predict(Y))
    ax[2, 0].hist(diff.flatten()[gd],
                  cumulative=True,
                  normed=True,
                  bins=10.**np.arange(-8, 0, 0.05),
                  histtype='step')
    ax[2, 0].set_xlabel('|Model-true|')
    ax[2, 0].set_ylim(0., 1.)
    ax[2, 0].set_xscale('log')

    figure_path = os.path.join(root, "plots/{}-1to1.png".format(model_name))
    fig.tight_layout()
    fig.savefig(figure_path, dpi=300)
    plt.close()

    for i, label_name in enumerate(model_labels):

        x = X[:, i]
        y = Y[:, i]

        fig, ax = plt.subplots()
        if label_name == 'TEFF':
            ax.scatter(x, y, c=X[:, iz], alpha=0.5)
        else:
            ax.scatter(x, y, c=X[:, it], alpha=0.5)

        lims = np.array([ax.get_xlim(), ax.get_ylim()])
        lims = (lims.min(), lims.max())
        ax.plot(lims, lims, c="#666666", zorder=-1, linestyle=":")
        ax.set_xlim(lims)
        ax.set_ylim(lims)

        ax.set_xlabel("Labelled")
        ax.set_ylabel("Inferred")

        mean, rms = np.nanmean(y - x), np.nanstd(y - x)
        title = "{}: ({:.2f}, {:.2f})".format(label_name, mean, rms)
        ax.set_title(title)
        logger.info("Mean and RMS for {}".format(title))

        figure_path = os.path.join(
            root, "plots/{}-{}-1to1.png".format(model_name, label_name))
        fig.tight_layout()
        fig.savefig(figure_path, dpi=300)
        plt.close()

        logger.info("Created 1-to-1 figure for {} at {}".format(
            label_name, figure_path))
Exemple #25
0
def irfm(allstar,trange=[4000,5000],mhrange=[-2.5,0.75],out='dteff') :
    '''
    Compares allstar ASPCPAP Teff with various photometric Teff from JAJ compilation (SAGA, CL, TH, SFD)
    Does fits 

    Args:
        allstar   : allStar structure

    '''

    # select stars
    gd=apselect.select(allstar,badval=['STAR_BAD'],teff=trange,mh=mhrange,raw=True)
    allstar=allstar[gd]

    # get IRFM data
    irfm=fits.open(os.environ['APOGEE_DIR']+'/data/calib/irfm_temp.fits')[1].data

    # get the subsamples and match. Note that we have to do this separately for each subsample because some
    #   stars appear in more than one subsample
    saga=np.where(irfm['SOURCE'] == 'SAGA')[0]
    saga1,saga2=match.match(np.chararray.strip(allstar['APOGEE_ID']),np.chararray.strip(irfm['2MASS ID'][saga]))
    cl=np.where(irfm['SOURCE'] == 'CL')[0]
    cl1,cl2=match.match(np.chararray.strip(allstar['APOGEE_ID']),np.chararray.strip(irfm['2MASS ID'][cl]))
    th=np.where(irfm['SOURCE'] == 'TH')[0]
    th1,th2=match.match(np.chararray.strip(allstar['APOGEE_ID']),np.chararray.strip(irfm['2MASS ID'][th]))
    sfd=np.where(irfm['SOURCE'] == 'SFD')[0]
    sfd1,sfd2=match.match(np.chararray.strip(allstar['APOGEE_ID']),np.chararray.strip(irfm['2MASS ID'][sfd]))

    # plot diff color-coded by gravity as f([M/H])
    fig,ax=plots.multi(2,2,hspace=0.001,wspace=0.001)
    xr=[-3.0,1.0]
    yr=[-400,300]
    zr=[3500,6000]
    bins=np.arange(-2.5,0.75,0.25)

    # SAGA
    plots.plotc(ax[0,0],allstar['FPARAM'][saga1,3],allstar['FPARAM'][saga1,0]-irfm['IRFM TEFF'][saga[saga2]],allstar['FPARAM'][saga1,0],zr=zr,xr=xr,yr=yr,xt='[M/H]',yt='ASPCAP-photometric Teff')
    mean=bindata(allstar['FPARAM'][saga1,3],allstar['FPARAM'][saga1,0]-irfm['IRFM TEFF'][saga[saga2]],bins)
    plots.plotp(ax[0,0],bins,mean,marker='o',size=40)
    ax[0,0].text(0.1,0.9,'SAGA',transform=ax[0,0].transAxes)

    # CL
    plots.plotc(ax[0,1],allstar['FPARAM'][cl1,3],allstar['FPARAM'][cl1,0]-irfm['IRFM TEFF'][cl[cl2]],allstar['FPARAM'][cl1,0],zr=zr,xr=xr,yr=yr,xt='[M/H]')
    mean=bindata(allstar['FPARAM'][cl1,3],(allstar['FPARAM'][cl1,0]-irfm['IRFM TEFF'][cl[cl2]]),bins)
    plots.plotp(ax[0,1],bins,mean,marker='o',size=40)
    ax[0,1].text(0.1,0.9,'CL',transform=ax[0,1].transAxes)

    # TH
    plots.plotc(ax[1,0],allstar['FPARAM'][th1,3],allstar['FPARAM'][th1,0]-irfm['IRFM TEFF'][th[th2]],allstar['FPARAM'][th1,0],zr=zr,xr=xr,yr=yr,xt='[M/H]',yt='ASPCAP-photometric Teff')
    mean=bindata(allstar['FPARAM'][th1,3],(allstar['FPARAM'][th1,0]-irfm['IRFM TEFF'][th[th2]]),bins)
    plots.plotp(ax[1,0],bins,mean,marker='o',size=40)
    ax[1,0].text(0.1,0.9,'TH',transform=ax[1,0].transAxes)

    # SFD
    plots.plotc(ax[1,1],allstar['FPARAM'][sfd1,3],allstar['FPARAM'][sfd1,0]-irfm['IRFM TEFF'][sfd[sfd2]],allstar['FPARAM'][sfd1,0],zr=zr,xr=xr,yr=yr,xt='[M/H]')
    mean=bindata(allstar['FPARAM'][sfd1,3],(allstar['FPARAM'][sfd1,0]-irfm['IRFM TEFF'][sfd[sfd2]]),bins)
    plots.plotp(ax[1,1],bins,mean,marker='o',size=40)
    ax[1,1].text(0.1,0.9,'SFD',transform=ax[1,1].transAxes)

    fig.savefig(out+'_mh.png')

    # plot diff color-coded by gravity as f([M/H])
    fig,ax=plots.multi(2,2,hspace=0.001,wspace=0.001)
    zr=[-2.0,0.5]
    yr=[-400,300]
    xr=[6000,3500]
    bins=np.arange(3500,5500,250)

    # SAGA
    plots.plotc(ax[0,0],allstar['FPARAM'][saga1,0],allstar['FPARAM'][saga1,0]-irfm['IRFM TEFF'][saga[saga2]],allstar['FPARAM'][saga1,3],zr=zr,xr=xr,yr=yr,xt='Teff',yt='ASPCAP-photometric Teff')
    mean=bindata(allstar['FPARAM'][saga1,0],(allstar['FPARAM'][saga1,0]-irfm['IRFM TEFF'][saga[saga2]]),bins)
    plots.plotp(ax[0,0],bins,mean,marker='o',size=40)
    ax[0,0].text(0.1,0.9,'SAGA',transform=ax[0,0].transAxes)

    # CL
    plots.plotc(ax[0,1],allstar['FPARAM'][cl1,0],allstar['FPARAM'][cl1,0]-irfm['IRFM TEFF'][cl[cl2]],allstar['FPARAM'][cl1,3],zr=zr,xr=xr,yr=yr,xt='Teff')
    mean=bindata(allstar['FPARAM'][cl1,0],(allstar['FPARAM'][cl1,0]-irfm['IRFM TEFF'][cl[cl2]]),bins)
    plots.plotp(ax[0,1],bins,mean,marker='o',size=40)
    ax[0,1].text(0.1,0.9,'CL',transform=ax[0,1].transAxes)

    # TH
    plots.plotc(ax[1,0],allstar['FPARAM'][th1,0],allstar['FPARAM'][th1,0]-irfm['IRFM TEFF'][th[th2]],allstar['FPARAM'][th1,3],zr=zr,xr=xr,yr=yr,xt='Teff',yt='ASPCAP-photometric Teff')
    mean=bindata(allstar['FPARAM'][th1,0],(allstar['FPARAM'][th1,0]-irfm['IRFM TEFF'][th[th2]]),bins)
    plots.plotp(ax[1,0],bins,mean,marker='o',size=40)
    ax[1,0].text(0.1,0.9,'TH',transform=ax[1,0].transAxes)

    # SFD
    plots.plotc(ax[1,1],allstar['FPARAM'][sfd1,0],allstar['FPARAM'][sfd1,0]-irfm['IRFM TEFF'][sfd[sfd2]],allstar['FPARAM'][sfd1,3],zr=zr,xr=xr,yr=yr,xt='Teff')
    mean=bindata(allstar['FPARAM'][sfd1,0],(allstar['FPARAM'][sfd1,0]-irfm['IRFM TEFF'][sfd[sfd2]]),bins)
    plots.plotp(ax[1,1],bins,mean,marker='o',size=40)
    ax[1,1].text(0.1,0.9,'SFD',transform=ax[1,1].transAxes)

    fig.savefig(out+'_teff.png')

    # do 2D fits with Teff and [M/H], and 1D fits with each

    fig,ax=plots.multi(2,2,hspace=0.5,wspace=0.001)
    ax[0,1].xaxis.set_visible(False)
    ax[0,1].yaxis.set_visible(False)
    pfit = fit.fit2d(ax[0,0],allstar['FPARAM'][sfd1,3],allstar['FPARAM'][sfd1,0],allstar['FPARAM'][sfd1,0]-irfm['IRFM TEFF'][sfd[sfd2]],plot=True,zr=[-500,200],xt='[M/H]',yt=['Teff'],zt='$\Delta Teff$')
    pfit = fit.fit1d(ax[1,0],allstar['FPARAM'][sfd1,3],allstar['FPARAM'][sfd1,0]-irfm['IRFM TEFF'][sfd[sfd2]],ydata=allstar['FPARAM'][sfd1,0],plot=True,zr=[-500,200],xt='[M/H]',yt='$\Delta Teff$',xr=[-2.7,0.9],yr=[3500,5000])
    pfit = fit.fit1d(ax[1,1],allstar['FPARAM'][sfd1,0],allstar['FPARAM'][sfd1,0]-irfm['IRFM TEFF'][sfd[sfd2]],ydata=allstar['FPARAM'][sfd1,3],plot=True,zr=[-500,200],xt='Teff',xr=[3900,5100],yr=[-2.5,0.5])

    pdb.set_trace()

    return pfit
Exemple #26
0
def flags(hdulist, out='./', alpha=0.005):
    """ Tabulate number of objects with different flag bits set, and make HR diagrams showing these
    """
    f = html.head(out + 'flags.html')
    mask = bitmask.AspcapBitMask()
    xt = []
    for i in range(32):
        if mask.name[i] != '': xt.append(mask.name[i])
    data = []
    row = []
    yt = ['ASPCAPFLAG']
    for i in range(32):
        j = np.where(hdulist[1].data['ASPCAPFLAG'] & 2**i)[0]
        if mask.name[i] == '' and len(j) > 0:
            print('Unnamed bits are set!', i, len(j))
            pdb.set_trace()
        elif mask.name[i] != '':
            print(mask.name[i])
            if len(j) > 0:
                if np.core.defchararray.find(mask.name[i], 'COLORTE') >= 0:
                    jk0 = hdulist[1].data['J'] - hdulist[1].data[
                        'K'] - 1.5 * hdulist[1].data['AK_TARG']
                    fig, ax = plots.multi(1, 2, hspace=0.001)
                    plots.plotc(ax[0],
                                hdulist[1].data['FPARAM'][:, 0],
                                hdulist[1].data['FPARAM'][:, 1],
                                hdulist[1].data['FPARAM'][:, 3],
                                alpha=alpha,
                                zr=[-2, 0.5])
                    plots.plotc(ax[1],
                                hdulist[1].data['FPARAM'][:, 0],
                                jk0,
                                hdulist[1].data['FPARAM'][:, 3],
                                zr=[-2, 0.5],
                                alpha=alpha)
                    plots.plotc(ax[0],
                                hdulist[1].data['FPARAM'][j, 0],
                                hdulist[1].data['FPARAM'][j, 1],
                                hdulist[1].data['FPARAM'][j, 3],
                                xr=[10000, 3000],
                                yr=[6, -1],
                                zr=[-2, 0.5],
                                xt='Teff (raw)',
                                yt='logg (raw)')
                    plots.plotc(ax[1],
                                hdulist[1].data['FPARAM'][j, 0],
                                jk0[j],
                                hdulist[1].data['FPARAM'][j, 3],
                                xr=[10000, 3000],
                                yr=[-2, 4],
                                zr=[-2, 0.5],
                                xt='Teff (raw)',
                                yt='J-K')
                else:
                    fig, ax = plots.multi(1, 1)
                    plots.plotc(ax,
                                hdulist[1].data['FPARAM'][:, 0],
                                hdulist[1].data['FPARAM'][:, 1],
                                hdulist[1].data['FPARAM'][:, 3],
                                alpha=alpha,
                                zr=[-2, 0.5])
                    plots.plotc(ax,
                                hdulist[1].data['FPARAM'][j, 0],
                                hdulist[1].data['FPARAM'][j, 1],
                                hdulist[1].data['FPARAM'][j, 3],
                                xr=[10000, 3000],
                                yr=[6, -1],
                                zr=[-2, 0.5],
                                xt='Teff (raw)',
                                yt='logg (raw)')
                outfile = out + 'flag_aspcapflag_{:d}.png'.format(i)
                fig.savefig(outfile)
                plt.close()
                row.append('<a href={:s}> {:d} </a>'.format(
                    os.path.basename(outfile), len(j)))
            else:
                row.append('{:d}'.format(len(j)))
    data.append(row)
    f.write(html.table(data, xtitle=xt, ytitle=yt, plots=False, formstr=':s'))

    mask = bitmask.ParamBitMask()
    xt = []
    for i in range(32):
        if mask.name[i] != '': xt.append(mask.name[i])
    data = []
    yt = []
    for iparam, param in enumerate(hdulist[3].data['PARAM_SYMBOL'][0]):
        yt.append(param)
        row = []
        for i in range(32):
            print(param, mask.name[i])
            j = np.where(hdulist[1].data['PARAMFLAG'][:, iparam] & 2**i)[0]
            if mask.name[i] == '' and len(j) > 0:
                print('Unnamed bits are set!', i, len(j))
                pdb.set_trace()
            elif mask.name[i] != '':
                if len(j) > 0:
                    fig, ax = plots.multi(1, 1)
                    plots.plotc(ax,
                                hdulist[1].data['FPARAM'][:, 0],
                                hdulist[1].data['FPARAM'][:, 1],
                                hdulist[1].data['FPARAM'][:, 3],
                                alpha=alpha,
                                zr=[-2, 0.5])
                    plots.plotc(ax,
                                hdulist[1].data['FPARAM'][j, 0],
                                hdulist[1].data['FPARAM'][j, 1],
                                hdulist[1].data['FPARAM'][j, 3],
                                xr=[10000, 3000],
                                yr=[6, -1],
                                zr=[-2, 0.5],
                                xt='Teff (raw)',
                                yt='logg (raw)')
                    outfile = out + 'flag_param_{:d}_{:d}.png'.format(
                        iparam, i)
                    fig.savefig(outfile)
                    plt.close()
                    row.append('<a href={:s}> {:d} </a>'.format(
                        os.path.basename(outfile), len(j)))
                else:
                    row.append('{:d}'.format(len(j)))
        data.append(row)
    f.write(html.table(data, xtitle=xt, ytitle=yt, plots=False, formstr=':s'))

    xt = []
    for i in range(32):
        if mask.name[i] != '': xt.append(mask.name[i])
    data = []
    yt = []
    for ielem, el in enumerate(hdulist[3].data['ELEM_SYMBOL'][0]):
        if hdulist[3].data['ELEMTOH'][0][ielem] == 1: fzr = [-2, 0.5]
        else: fzr = [-1, 1]
        yt.append(el)
        row = []
        for i in range(32):
            j = np.where(hdulist[1].data['ELEMFLAG'][:, ielem] & 2**i)[0]
            if mask.name[i] == '' and len(j) > 0:
                print('Unnamed bits are set!', i, len(j))
                pdb.set_trace()
            elif mask.name[i] != '':
                print(el, mask.name[i])
                if len(j) > 0:
                    fig, ax = plots.multi(1, 2, hspace=0.001)
                    plots.plotc(ax[0],
                                hdulist[1].data['FPARAM'][:, 0],
                                hdulist[1].data['FPARAM'][:, 1],
                                hdulist[1].data['FPARAM'][:, 3],
                                alpha=alpha,
                                zr=[-2, 0.5])
                    plots.plotc(ax[1],
                                hdulist[1].data['FPARAM'][:, 0],
                                hdulist[1].data['FPARAM'][:, 1],
                                hdulist[1].data['FELEM'][:, ielem],
                                alpha=alpha,
                                zr=fzr)
                    plots.plotc(ax[0],
                                hdulist[1].data['FPARAM'][j, 0],
                                hdulist[1].data['FPARAM'][j, 1],
                                hdulist[1].data['FPARAM'][j, 3],
                                xr=[10000, 3000],
                                yr=[6, -1],
                                zr=[-2, 0.5],
                                xt='Teff (raw)',
                                yt='logg (raw)',
                                colorbar=True,
                                zt='[M/H]')
                    plots.plotc(ax[1],
                                hdulist[1].data['FPARAM'][j, 0],
                                hdulist[1].data['FPARAM'][j, 1],
                                hdulist[1].data['FELEM'][j, ielem],
                                xr=[10000, 3000],
                                yr=[6, -1],
                                zr=fzr,
                                xt='Teff (raw)',
                                yt='logg (raw)',
                                colorbar=True,
                                zt='FELEM')
                    outfile = out + 'flag_{:s}_{:d}.png'.format(el, i)
                    fig.savefig(outfile)
                    plt.close()
                    row.append('<a href={:s}> {:d} </a>'.format(
                        os.path.basename(outfile), len(j)))
                else:
                    row.append('{:d}'.format(len(j)))
        data.append(row)
    f.write(html.table(data, xtitle=xt, ytitle=yt, plots=False, formstr=':s'))

    html.tail(f)
Exemple #27
0
def throughplot(instrument='apogee-s',outfile=None,inter=False) :
    '''
    Routine to make zeropoint/throughput plots from apogeeSci summary files
    with information including FWHM, GDRMS, CART
    '''

    # instrument specific
    if instrument == 'apogee-s' :
        gain=3.
        carts=[20,25]
        fiber_rad=0.65
        telescope='lco25m'
    else :
        gain=1.9
        carts=[0,10]
        fiber_rad=1.
        telescope='apo25m'

    # read summary data made by mkmonitor
    a=fits.open(instrument+'Sci.fits')[1].data
    gd = np.where(a['NREADS'] >= 47)[0]
    a=a[gd]

    # use weather information if we can
    clouds=np.zeros(len(a)).astype(int)
    nmiss=0
    nhave=0
    try :
        c=ascii.read(os.environ['APOGEEREDUCEPLAN_DIR']+'/data/'+telescope+'/clouds.txt')
        try:
            for i,p in enumerate(a['PLATE']) :
                j=np.where((c['plate'] == p) & (c['MJD'] == a['MJD'][i]) )[0]
                if len(j)>0 : 
                    if len(j)>1 : 
                        print('double cloud match',p,a['MJD'][i])
                        pdb.set_trace()
                    clouds[i] = c['clouds_level'][j[0]]
                    nhave+=1
                else : 
                    nmiss+=1
                    print('no clouds match found for',a['MJD'][i],p)
        except :
            print('error!',i,p,j)
            pdb.set_trace()

        gd=np.where(clouds <= 1)[0]
        a=a[gd]
    except :
        print('cant open clouds file')


    # seeing correction factor
    sigma = a['FWHM']/2.354
    sigma = a['SEEING']/2.354
    ee = 1. - np.exp(-(fiber_rad**2)/(2*sigma**2))
    corr = a['ZERONORM']-2.5*np.log10(ee)

    gd = np.where(np.isfinite(corr))[0]
    a=a[gd]
    ee=ee[gd]
    corr=corr[gd]

    # run number for LCO
    run = ((a['MJD']-57850)/29.+0.5).astype(int)

    # rough throughput calculation
    h=6.63e-27
    c=3.e10
    lam=1.6e-4
    dlam=0.3
    dt=10.6
    area=math.pi*(125.**2-50.**2)
    fvega=11.38e-11
    through=10**(0.4*a['ZERONORM'])*h*c/lam/dlam/dt*gain/area/fvega/ee
   
    # straight DHA
    dha=a['HA']-a['DESIGN_HA'][:,0]
    #dha=np.abs(dha)
    # "normalized" DHA
    j=np.where(a['HA']<a['DESIGN_HA'][:,0])[0]
    dha[j]/=(a['DESIGN_HA'][j,0]-a['DESIGN_HA'][j,1])
    j=np.where(a['HA']>=a['DESIGN_HA'][:,0])[0]
    dha[j]/=(a['DESIGN_HA'][j,2]-a['DESIGN_HA'][j,0])

    #plots with MJD
    files=[]

    out='monitor/'+instrument+'/'+instrument

    # point size by FWHM
    psize=a['FWHM']/1.*40
    j=np.where(psize == 0.)[0]
    psize[j] = 10

    # histograms by run
    fig,ax=plots.multi(2,3,figsize=(8,12))
    file=out+'zero_hist.png'
    runs=list(set(run))
    runs.append(999)
    for r in runs :
        gd = np.where(run == r)[0]
        if r == 999 :
             gd = np.where(run < 999)[0]
        if r >= 8 : lw=2
        else : lw=1
        print(r,len(gd))
        try:
            n,b,p=plt.hist(a['GDRMS'][gd],histtype='step',bins=np.arange(0,1,0.05),label='{:3d}'.format(r),linewidth=lw,normed=False)
            if r == 999 : n/=2
            ax[0,0].plot(b[0:-1]+(b[1]-b[0])/2.,n,linewidth=lw,label='{:2d}'.format(r))
            ax[0,0].set_xlabel('GDRMS')
        except : pass
        try:
            n,b,p=plt.hist(a['ZERONORM'][gd],histtype='step',bins=np.arange(12,15.5,0.1),linewidth=lw,normed=False,label='{:2d}'.format(r))
            if r == 999 : n/=2
            ax[0,1].plot(b[0:-1]+(b[1]-b[0])/2.,n,linewidth=lw,label='{:2d}'.format(r))
            ax[0,1].set_xlabel('ZERONORM')
            n,b,p=plt.hist(corr[gd],histtype='step',bins=np.arange(12,16,0.1),linewidth=lw,normed=False,label='{:3d}'.format(r))
            if r == 999 : n/=2
            ax[1,0].plot(b[0:-1]+(b[1]-b[0])/2.,n,linewidth=lw,label='{:3d}'.format(r))
            ax[1,0].set_xlabel('ZERONORM (adjusted)')
            n,b,p=plt.hist(a['ZERORMS'][gd],histtype='step',bins=np.arange(0,1,0.05),linewidth=lw,normed=False,label='{:3d}'.format(r))
            if r == 999 : n/=2
            ax[1,1].plot(b[0:-1]+(b[1]-b[0])/2.,n,linewidth=lw,label='{:3d}'.format(r))
            ax[1,1].set_xlabel('ZERORMS')
            n,b,p=plt.hist(through[gd],histtype='step',bins=np.arange(0,0.34,0.02),linewidth=lw,normed=False,label='{:3d}'.format(r))
            if r == 999 : n/=2
            ax[2,0].plot(b[0:-1]+(b[1]-b[0])/2.,n,linewidth=lw,label='{:3d}'.format(r))
            ax[2,0].set_xlabel('THROUGHPUT (adjusted)')
        except : pass
    
    if instrument == 'apogee-s' :
        ax[0,0].legend(fontsize=6,loc=1,title='Run')
        ax[0,1].legend(fontsize=6,loc=2,title='Run')
        ax[1,0].legend(fontsize=6,loc=2,title='Run')
        ax[1,1].legend(fontsize=6,loc=1,title='Run')
    ax[2,1].remove()
    fig.tight_layout()
    fig.savefig(file)
    files.append([os.path.basename(file)])

    ctype = [a['FWHM'],a['SEEING'],a['GDRMS'],dha,a['CART']]
    name = ['zero_fwhm','zero_seeing','zero_gdrms','zero_dha','zero_cart']
    zr=[[0.5,2.],[0.5,2.],[0,0.8],[-2,2],carts]
    zt=['FWHM','SEEING','GDRMS','DHA','CART']
    for j,c in enumerate(ctype) :
      fig,ax=plots.multi(1,4,hspace=0.001,sharex=True,figsize=(24,6))
      file=out+name[j]+'.png'
      plots.plotc(ax[0],a['MJD'],a['ZERONORM'],c,yr=[12,15.5],zr=zr[j],size=psize,colorbar=True,xt='MJD',yt='ZERONORM',zt=zt[j])
      plots.plotc(ax[1],a['MJD'],corr,c,yr=[12,15.5],zr=zr[j],size=psize,colorbar=True,xt='MJD',yt='ZERONORM (adjusted)',zt=zt[j])
      plots.plotc(ax[2],a['MJD'],a['ZERORMS'],c,yr=[0,1],zr=zr[j],size=psize,colorbar=True,xt='MJD',yt='ZERORMS',zt=zt[j])
      plots.plotc(ax[3],a['MJD'],through,c,yr=[0,0.3],zr=zr[j],size=psize,colorbar=True,xt='MJD',yt='throughput',zt=zt[j])
      fig.savefig(file)
      files.append([os.path.basename(file)])

    fig,ax=plots.multi(1,1)
    plots.plotc(ax,a['SEEING'],a['ZERONORM'],a['GDRMS'],xr=[0.,3.0],yr=[13,15.5],zr=[0.2,1.2],xt='Seeing',yt='ZERONORM',zt='GDRMS',colorbar=True,size=1)
    #plots.plotc(ax[1],a['SEEING'],corr,a['GDRMS'],xr=[0.,3.0],yr=[13,15.5],zr=[0.2,1.2],xt='Seeing',yt='seeing-corrected ZERONORM',zt='GDRMS',colorbar=True,size=1)
    file=out+'_seeing.png'
    fig.savefig(file)
    files.append([os.path.basename(file)])

    out='monitor/'+instrument+'/'+instrument
    html.htmltab(files,file=out+'zero.html')
    if inter :
        pdb.set_trace()
Exemple #28
0
def ghb(allstar,glatmin=30.,ebvmax=0.03,trange=[3750,5500],loggrange=[-1,6],mhrange=[-2.5,0.75],alpha=False,out='teffcomp',yr=[-500,500],
        calib=False,dr13=False,grid=None,cmap='rainbow') :
    """
    Compares allstar ASPCPAP Teff with photometric Teff from GHB for sample of stars with GLAT>glatmin and SFD_EBV<ebvmax,
    does fits

    Args:
        allstar   : allStar structure

    Keyword args:
        glatmin (float) : minimum GLAT for sample (default=30)
        ebvmax (float)  : maximum SFD_EBV for sample (default=0.03)
        dwarf (bool)    : use dwarfs and dwarf GHB  (default = False)
    """

    # select data to use
    if 'TARGFLAGS' in allstar.columns.names : badtarg = ['EMBEDDED','EXTENDED']
    else : badtarg = None
    gd=apselect.select(allstar,badval=['STAR_BAD'],badtarg=badtarg,teff=trange,mh=mhrange,logg=loggrange,raw=True)
    allstar=allstar[gd]
    #if dr13 :
    #  j=np.where((abs(allstar['GLAT'])>glatmin)&(allstar['SFD_EBV']<ebvmax))[0]
    #else :
    j=np.where((abs(allstar['GLAT'])>glatmin)&(allstar['SFD_EBV']>-0.01)&(allstar['SFD_EBV']<ebvmax)&(abs(allstar['J'])<90)&(abs(allstar['K'])<90))[0]

    # remove second gen GC stars
    #if not dr13 :
    gcstars = ascii.read(os.environ['APOGEE_DIR']+'/data/calib/gc_szabolcs.dat')
    bd=np.where(gcstars['pop'] != 1)[0]
    j = [x for x in j if allstar[x]['APOGEE_ID'] not in gcstars['id'][bd]]

    allstar=allstar[j]

    ghb,dtdjk=cte_ghb(allstar['J']-allstar['K'],allstar['FPARAM'][:,3],dwarf=False)
    ghb_dwarf,dtdjk_dwarf=cte_ghb(allstar['J']-allstar['K'],allstar['FPARAM'][:,3],dwarf=True)
    # use dwarf relation for dwarfs
    dw=np.where(allstar['FPARAM'][:,1] > 3.8)[0]
    ghb[dw]=ghb_dwarf[dw]
    dtdjk[dw]=dtdjk_dwarf[dw]
    gd=np.where(abs(allstar['FPARAM'][:,0]-ghb) < 500)[0]
    ghb=ghb[gd]
    dtdjk=dtdjk[gd]
    allstar=allstar[gd]
    print('Teff calibration, number of stars: ', len(allstar))

    if calib : 
        param='PARAM'
        teff=allstar[param][:,0]
        logg=allstar[param][:,1]
        mh=allstar[param][:,3]
        am=allstar[param][:,6]
    elif grid is None :
        param='FPARAM'
        teff=allstar[param][:,0]
        logg=allstar[param][:,1]
        mh=allstar[param][:,3]
        am=allstar[param][:,6]
    else :
        param='FPARAM_CLASS'
        teff=allstar[param][:,grid,0]
        logg=allstar[param][:,grid,1]
        mh=allstar[param][:,grid,3]
        am=allstar[param][:,grid,6]
        out=out+'_grid{:1d}'.format(grid)

    # plot Teff difference against metallicity, color-code by temperature
    fig,ax=plots.multi(1,1,hspace=0.001,wspace=0.001,figsize=(12,6))
    xr=[-3.0,1.0]
    zr=trange
    if dr13: zr=[3500,5500]
    binsize=0.25
    bins=np.arange(-2.5,0.75,binsize)
    # diff color-coded by gravity as f([M/H])

    if alpha :
        plots.plotc(ax,mh,teff-ghb,am,zr=[-0.1,0.4],xr=xr,yr=yr,xt='[M/H]',yt='ASPCAP-photometric Teff',colorbar=True,zt=r'[$\alpha$/M]',rasterized=True,cmap=cmap)
    else :
        plots.plotc(ax,mh,teff-ghb,teff,xr=xr,yr=yr,xt='[M/H]',yt='ASPCAP-photometric Teff',colorbar=True,zt='$T_{eff}$',rasterized=True,zr=trange,cmap=cmap)
    mean=bindata(mh,teff-ghb,bins,median=False)
    if not dr13: plots.plotp(ax,bins+binsize/2.,mean,marker='o',size=40)
    mean=bindata(mh,teff-ghb,bins,median=True)
    if not dr13: plots.plotp(ax,bins+binsize/2.,mean,marker='o',size=40,color='b')
    ax.text(0.1,0.9,'E(B-V)<{:6.2f}'.format(ebvmax),transform=ax.transAxes)
    gd=np.where(np.isfinite(mean))[0]
    tefit = fit.fit1d(bins[gd]+binsize/2.,mean[gd],degree=2,reject=0)
    # 1D quadratic fit as a function of metallicity
    allfit = fit.fit1d(mh,teff-ghb,ydata=teff,degree=2,reject=0)
    fig2,ax2=plots.multi(1,1)
    tefit2 = fit.fit2d(mh,teff,teff-ghb,reject=0,plot=ax2,zr=[-500,200],xt='[M/H]',yt=['Teff'],zt='$\Delta Teff$')
    #pfit = fit.fit2d(allstar[param][:,3],allstar[param][:,0],allstar[param][:,0]-ghb,plot=ax[0,0],zr=[-500,200],xt='[M/H]',yt=['Teff'],zt='$\Delta Teff$')
    #ejk=np.clip(np.sqrt(allstar['J_ERR']**2+allstar['K_ERR']**2),0.,0.02)
    #errpar = err.errfit(teff,allstar['SNR'],mh,teff-tefit(mh)-ghb,title='Teff',out=out+'_phot',zr=[0,250],meanerr=abs(dtdjk)*ejk)
    errpar = err.errfit(teff,allstar['SNR'],mh,teff-tefit(mh)-ghb,title='Teff',out=out,zr=[0,150])

    x=np.linspace(-3,1,200)
    rms = (teff-tefit(mh)-ghb).std()
    if dr13: 
      plots.plotl(ax,x,-36.17+95.97*x-15.09*x**2,color='k')
      print(allfit)
    else :
      plots.plotl(ax,x,tefit(x),color='k')
      ax.text(0.98,0.9,'rms: {:6.1f}'.format(rms),transform=ax.transAxes,ha='right')

      cmap = matplotlib.cm.get_cmap(cmap)
      for t in np.arange(trange[0],trange[1],500.) :
          rgba=cmap((t-trange[0])/(trange[1]-trange[0]))
          y=x*0.+t
          plots.plotl(ax,x,tefit2(x,y),color=rgba)

    plots._data_x = mh
    plots._data_y = teff-ghb
    plots._data = allstar
    plots.event(fig)

    # separate fits for low/hi alpha/M if requested
    if alpha :
        gdlo=apselect.select(allstar,badval=['STAR_BAD'],teff=trange,mh=mhrange,logg=[0,3.8],alpha=[-0.1,0.1],raw=True)
        mean=bindata(mh[gdlo],teff[gdlo]-ghb[gdlo],bins)
        plots.plotp(ax,bins,mean,marker='o',size=40,color='g')
        tmpfit = fit.fit1d(mh[gdlo],teff[gdlo]-ghb[gdlo],ydata=teff[gdlo],degree=2)
        plots.plotl(ax,x,tmpfit(x))
        print('low alpha: ', len(gdlo))

        gdhi=apselect.select(allstar,badval=['STAR_BAD'],teff=trange,mh=mhrange,logg=[0,3.8],alpha=[0.1,0.5],raw=True)
        mean=bindata(mh[gdhi],teff[gdhi]-ghb[gdhi],bins)
        plots.plotp(ax,bins,mean,marker='o',size=40,color='b')
        tmpfit = fit.fit1d(mh[gdhi],teff[gdhi]-ghb[gdhi],ydata=teff[gdhi],degree=2)
        plots.plotl(ax,x,tmpfit(x))
        print('hi alpha: ', len(gdhi))

    fig.tight_layout()
    fig.savefig(out+'.png')
    plt.close()
    plt.rc('font',size=14)
    plt.rc('axes',titlesize=14)
    plt.rc('axes',labelsize=14)
    fig.savefig(out+'.pdf')
    plt.close()

    # auxiliary plots with different color-codings
    try:
        meanfib=allstar['MEANFIB']
    except:
        meanfib=teff*0.
    fig,ax=plots.multi(2,2,hspace=0.001,wspace=0.001)
    plots.plotc(ax[0,0],mh,teff-ghb,logg,zr=[0,5],xr=xr,yr=yr,xt='[M/H]',yt='ASPCAP-photometric Teff',colorbar=True,zt='log g')
    plots.plotc(ax[0,1],mh,teff-ghb,meanfib,zr=[0,300],xr=xr,yr=yr,xt='[M/H]',yt='ASPCAP-photometric Teff',colorbar=True,zt='mean fiber')
    pfit = fit.fit1d(mh,teff-ghb,ydata=teff,plot=ax[1,0],zr=[-500,200],xt='[M/H]',yt='$\Delta Teff$',xr=[-2.7,0.9],yr=[3500,5000],colorbar=True,zt='Teff')
    pfit = fit.fit1d(teff,teff-ghb,ydata=mh,plot=ax[1,1],zr=[-500,200],xt='Teff',xr=trange,yr=[-2.5,0.5],colorbar=True,zt='[M/H]')
    fig.tight_layout()
    fig.savefig(out+'_b.png')
    plt.close()
   
    # do some test 2D and 1D fits and plots 
    #fig,ax=plots.multi(2,2,hspace=0.5,wspace=0.001)
    #ax[0,1].xaxis.set_visible(False)
    #ax[0,1].yaxis.set_visible(False)
    #pfit = fit.fit2d(allstar[param][:,3],allstar[param][:,0],allstar[param][:,0]-ghb,plot=ax[0,0],zr=[-500,200],xt='[M/H]',yt=['Teff'],zt='$\Delta Teff$')
    #pfit = fit.fit1d(allstar[param][:,3],allstar[param][:,0]-ghb,ydata=allstar[param][:,0],plot=ax[1,0],zr=[-500,200],xt='[M/H]',yt='$\Delta Teff$',xr=[-2.7,0.9],yr=[3500,5000])
    #pfit = fit.fit1d(allstar[param][:,0],allstar[param][:,0]-ghb,ydata=allstar[param][:,3],plot=ax[1,1],zr=[-500,200],xt='Teff',xr=[3900,5100],yr=[-2.5,0.5])
    plt.draw()
    return {'caltemin': 3000., 'caltemax': 10000., 'temin' : trange[0], 'temax': trange[1], 
            'mhmin': mhrange[0], 'mhmax' : mhrange[1],
            'par': tefit.parameters, 'rms' :rms, 'par2d': tefit2.parameters, 'errpar' : errpar}
Exemple #29
0
def compstars(d1,d2,out=None) :
    '''
    Creates plots to compare 2 different version
    '''
    v1=fits.open(d1+'/'+d1+'/allCal-'+d1+'.fits')[1].data
    v2=fits.open(d2+'/'+d2+'/allCal-'+d2+'.fits')[1].data
    i1,i2=match.match(v1['APOGEE_ID'],v2['APOGEE_ID'])

    fig,ax=plots.multi(1,7,hspace=0.001,figsize=(8,20))
    plots.plotc(ax[0],v1['FPARAM'][i1,0],v2['FPARAM'][i2,0]-v1['FPARAM'][i1,0],v1['FPARAM'][i1,3],zr=[-2,0.5],colorbar=True,zt='[M/H]',yt=r'$\Delta Teff$',yr=[-1000,1000],xt='Teff')
    plots.plotc(ax[1],v1['FPARAM'][i1,0],v2['FPARAM'][i2,1]-v1['FPARAM'][i1,1],v1['FPARAM'][i1,3],zr=[-2,0.5],colorbar=True,zt='[M/H]',yt=r'$\Delta log g$',yr=[-1,1],xt='Teff')
    plots.plotc(ax[2],v1['FPARAM'][i1,0],10.**v2['FPARAM'][i2,2]-10.**v1['FPARAM'][i1,2],v1['FPARAM'][i1,3],zr=[-2,0.5],colorbar=True,zt='[M/H]',yt=r'$\Delta vmicro$',yr=[-1,1],xt='Teff')
    plots.plotc(ax[3],v1['FPARAM'][i1,0],v2['FPARAM'][i2,3]-v1['FPARAM'][i1,3],v1['FPARAM'][i1,3],zr=[-2,0.5],colorbar=True,zt='[M/H]',yt=r'$\Delta [M/H]$',yr=[-0.75,0.75],xt='Teff')
    plots.plotc(ax[4],v1['FPARAM'][i1,0],v2['FPARAM'][i2,4]-v1['FPARAM'][i1,4],v1['FPARAM'][i1,3],zr=[-2,0.5],colorbar=True,zt='[M/H]',yt=r'$\Delta [C/M]$',yr=[-0.75,0.75],xt='Teff')
    plots.plotc(ax[5],v1['FPARAM'][i1,0],v2['FPARAM'][i2,5]-v1['FPARAM'][i1,5],v1['FPARAM'][i1,3],zr=[-2,0.5],colorbar=True,zt='[M/H]',yt=r'$\Delta [N/M]$',yr=[-0.75,0.75],xt='Teff')
    plots.plotc(ax[6],v1['FPARAM'][i1,0],v2['FPARAM'][i2,6]-v1['FPARAM'][i1,6],v1['FPARAM'][i1,3],zr=[-2,0.5],colorbar=True,zt='[M/H]',yt=r'$\Delta \alpha/M]$',yr=[-0.75,0.75],xt='Teff')
    if out is not None:
        plt.savefig(out+'.png')

    # plots as a function of delta logvmicro
    fig,ax=plots.multi(1,7,hspace=0.001,figsize=(8,20))
    plots.plotc(ax[0],v1['FPARAM'][i1,2]-v2['FPARAM'][i2,2],v2['FPARAM'][i2,0]-v1['FPARAM'][i1,0],v1['FPARAM'][i1,3],zr=[-2,0.5],colorbar=True,zt='[M/H]',yt=r'$\Delta Teff$',yr=[-1000,1000],xt=r'$\Delta log vmicro$' )
    plots.plotc(ax[1],v1['FPARAM'][i1,2]-v2['FPARAM'][i2,2],v2['FPARAM'][i2,1]-v1['FPARAM'][i1,1],v1['FPARAM'][i1,3],zr=[-2,0.5],colorbar=True,zt='[M/H]',yt=r'$\Delta log g$',yr=[-1,1],xt=r'$\Delta log vmicro$' )
    plots.plotc(ax[2],v1['FPARAM'][i1,2]-v2['FPARAM'][i2,2],10.**v2['FPARAM'][i2,2]-10.**v1['FPARAM'][i1,2],v1['FPARAM'][i1,3],zr=[-2,0.5],colorbar=True,zt='[M/H]',yt=r'$\Delta vmicro$',yr=[-1,1],xt=r'$\Delta log vmicro$' )
    plots.plotc(ax[3],v1['FPARAM'][i1,2]-v2['FPARAM'][i2,2],v2['FPARAM'][i2,3]-v1['FPARAM'][i1,3],v1['FPARAM'][i1,3],zr=[-2,0.5],colorbar=True,zt='[M/H]',yt=r'$\Delta [M/H]$',yr=[-0.75,0.75],xt=r'$\Delta log vmicro$' )
    plots.plotc(ax[4],v1['FPARAM'][i1,2]-v2['FPARAM'][i2,2],v2['FPARAM'][i2,4]-v1['FPARAM'][i1,4],v1['FPARAM'][i1,3],zr=[-2,0.5],colorbar=True,zt='[M/H]',yt=r'$\Delta [C/M]$',yr=[-0.75,0.75],xt=r'$\Delta log vmicro$' )
    plots.plotc(ax[5],v1['FPARAM'][i1,2]-v2['FPARAM'][i2,2],v2['FPARAM'][i2,5]-v1['FPARAM'][i1,5],v1['FPARAM'][i1,3],zr=[-2,0.5],colorbar=True,zt='[M/H]',yt=r'$\Delta [N/M]$',yr=[-0.75,0.75],xt=r'$\Delta log vmicro$' )
    plots.plotc(ax[6],v1['FPARAM'][i1,2]-v2['FPARAM'][i2,2],v2['FPARAM'][i2,6]-v1['FPARAM'][i1,6],v1['FPARAM'][i1,3],zr=[-2,0.5],colorbar=True,zt='[M/H]',yt=r'$\Delta \alpha/M]$',yr=[-0.75,0.75],xt=r'$\Delta log vmicro$' )
    if out is not None:
        plt.savefig(out+'_dvmicro.png')
def compare(planfile,
            model_name=None,
            outfile=None,
            xh=False,
            output_suffix=''):
    '''
    Make some plots with results
    '''
    p = yanny.yanny(planfile, np=True)
    apred = p['apred_vers'].strip("'")
    apstar = p['apstar_vers'].strip("'")
    aspcap_vers = p['aspcap_vers'].strip("'")
    results = p['results_vers'].strip("'")
    if model_name is None:
        model_name = getval(p, 'model_name', 'apogee-dr14-giants')
    apl = apload.ApLoad(apred=apred,
                        apstar=apstar,
                        aspcap=aspcap_vers,
                        results=results)
    a = apl.allStar()[1].data

    if outfile is None:
        outfile = 'allStarCannon-' + results + '.fits'
    c = fits.open(outfile)[1].data

    elems = aspcap.elems()[0]
    elems = ['Ca', 'Ni']
    model_labels = ['TEFF', 'LOGG', 'M_H', 'ALPHA_M', 'FE_H']
    figs = []
    ytit = []
    for el in elems:
        d = elem.dr14cal(el)
        if el is not 'Fe' and d['elemfit'] >= 0:
            print(el)
            tag = el.upper() + '_FE'
            if xh:
                ctag = el.upper() + '_H'
            else:
                ctag = el.upper() + '_FE'
            model_labels.append(ctag)
            f = []
            xtit = []
            for tmin in range(3500, 5500, 500):
                fig, ax = plots.multi(4,
                                      2,
                                      hspace=0.001,
                                      wspace=0.001,
                                      figsize=(10, 4.5))
                for i, snmin in enumerate(range(50, 250, 50)):
                    #j = apselect.select(a,badval='STAR_BAD',sn=[snmin,100000],teff=[tmin,tmin+500],logg=[-1,3.9],mh=[-3.,1],alpha=[-0.5,1.],raw=True)
                    j = np.where(
                        (np.chararray.find(a['ASPCAPFLAGS'], 'STAR_BAD') < 0)
                        & (c['TEFF'] > tmin) & (c['TEFF'] < tmin + 500)
                        & (a['SNR'] > snmin) & (a['SNR'] < snmin + 50))[0]
                    if i == 0:
                        yt = '[' + el + '/Fe]'
                    else:
                        yt = None
                    ax1 = plots.plotc(ax[0, i],
                                      a['FE_H'][j],
                                      a[tag][j],
                                      a['ASPCAP_CHI2'][j],
                                      xr=[-2.5, 1],
                                      yr=[-0.5, 0.75],
                                      zr=[0, 10],
                                      xt='[Fe/H]',
                                      yt=yt,
                                      nxtick=6,
                                      rasterized=True)
                    ax[0, i].text(0.1,
                                  0.9,
                                  'ASPCAP:',
                                  transform=ax[0, i].transAxes)
                    ax[0, i].text(0.2,
                                  0.8,
                                  'S/N > {:d}'.format(snmin),
                                  transform=ax[0, i].transAxes)
                    if xh:
                        ax2 = plots.plotc(ax[1, i],
                                          c['FE_H'][j],
                                          c[ctag][j] - c['FE_H'][j],
                                          c['r_chi_sq'][j],
                                          xr=[-2.5, 1],
                                          yr=[-0.5, 0.75],
                                          zr=[0, 5],
                                          xt='[Fe/H]',
                                          yt=yt,
                                          nxtick=6,
                                          rasterized=True)
                    else:
                        ax2 = plots.plotc(ax[1, i],
                                          c['FE_H'][j],
                                          c[ctag][j],
                                          c['r_chi_sq'][j],
                                          xr=[-2.5, 1],
                                          yr=[-0.5, 0.75],
                                          zr=[0, 5],
                                          xt='[Fe/H]',
                                          yt=yt,
                                          nxtick=6,
                                          rasterized=True)
                    ax[1, i].text(0.1,
                                  0.9,
                                  'Cannon:',
                                  transform=ax[1, i].transAxes)
                    ax[1,
                       i].text(0.2,
                               0.8,
                               '{:d} < S/N < {:d}'.format(snmin, snmin + 50),
                               transform=ax[1, i].transAxes)
                file = '{:s}_{:04d}{:s}'.format(el, tmin + 250, output_suffix)
                cbaxes = fig.add_axes([0.91, 0.55, 0.01, 0.3])
                cb = plt.colorbar(ax1, cax=cbaxes)
                cb.set_label('CHI2')
                cbaxes.tick_params(axis='both', labelsize=8)
                cbaxes = fig.add_axes([0.91, 0.15, 0.01, 0.3])
                cb = plt.colorbar(ax2, cax=cbaxes)
                cb.set_label('CHI2')
                cbaxes.tick_params(axis='both', labelsize=8)

                #fig.savefig('newplots/'+file+'.jpg',dpi=300)
                fig.savefig('newplots/' + file + '.pdf')
                plt.close()
                f.append(file)
                xtit.append('{:04d} < Teff < {:04d}'.format(tmin, tmin + 500))
            figs.append(f)
            ytit.append(el)
    html.htmltab(figs,
                 file='plots/cannon' + output_suffix + '.html',
                 ytitle=ytit,
                 xtitle=xtit)
    figs = []
    ytit = []
    for label in model_labels:
        print(label)
        fig, ax = plots.multi(1, 1, figsize=(12, 8))
        try:
            gd = np.where((a[label] > -999) & (a[label] < 999)
                          & (c[label] > -999))[0]
            plots.plotc(ax,
                        a[label][gd],
                        c[label][gd],
                        a['M_H'][gd],
                        zr=[-2, 0.5],
                        colorbar=False,
                        xt=label)
        except:
            alabel = label.replace('_H', '_FE')
            print(alabel)
            gd = np.where((a[alabel] > -999) & (a[alabel] < 999)
                          & (c[label] > -999))[0]
            plots.plotc(ax,
                        a[alabel][gd] + a['FE_H'][gd],
                        c[label][gd],
                        a['M_H'][gd],
                        zr=[-2, 0.5],
                        colorbar=False,
                        xt=label)
        file = 'fullcomp_' + label + output_suffix + '.jpg'
        fig.tight_layout()
        fig.savefig('newplots/' + file, dpi=300)
        plt.close()
        f = [file, model_name + '-' + label + '-1to1.png']
        figs.append(f)
        ytit.append(label)
    html.htmltab(figs,
                 file='newplots/compare' + output_suffix + '.html',
                 ytitle=ytit)