Ejemplo n.º 1
0
def compCframe(plate,
               frame,
               apred='test',
               ratio=True,
               rows=range(300),
               yr=None,
               hdu=1):
    load = apload.ApLoad(apred=apred)
    mjd = 55562 + int(frame // 10000)
    new = load.apCframe('M67', plate, mjd, frame)
    old = {}
    fig, ax = plots.multi(1, 3, hspace=0.001)
    x = np.arange(2048)
    for ichip, chip in enumerate(chips):
        old[chip] = fits.open(os.environ['APOGEE_REDUX'] +
                              '/r8/apo25m/{:d}/{:d}/apCframe-{:s}-{:d}.fits'.
                              format(plate, mjd, chip, frame))
        for row in rows:
            if ratio:
                plots.plotl(ax[ichip],
                            x,
                            new[chip][hdu].data[row, :] /
                            old[chip][hdu].data[row, :],
                            yr=[0, 1.5])
            else:
                plots.plotl(ax[ichip], x, new[chip][hdu].data[row, :], yr=yr)
                plots.plotl(ax[ichip], x, old[chip][hdu].data[row, :], yr=yr)
                plots.plotl(ax[ichip],
                            x,
                            new[chip][hdu].data[row, :] -
                            old[chip][hdu].data[row, :],
                            yr=yr)
Ejemplo n.º 2
0
def comp(plate=7267, mjd=56654, fiber=150, frame=10920059, field='M67'):
    r11 = apload.ApLoad(apred='r11')

    v = r11.apVisit(plate, mjd, fiber)
    a = r11.ap1D(frame)
    c = r11.apCframe(field, plate, mjd, frame)

    v14 = fits.open(os.environ['APOGEE_REDUX'] +
                    '/r8/apo25m/{:d}/{:d}/apVisit-r8-{:d}-{:d}-{:03d}.fits'.
                    format(plate, mjd, plate, mjd, fiber))
    a14 = {}
    c14 = {}
    for chip in chips:
        a14[chip] = fits.open(
            os.environ['APOGEE_REDUX'] +
            '/r8/red/{:d}/ap1D-{:s}-{:d}.fits'.format(mjd, chip, frame))
        c14[chip] = fits.open(os.environ['APOGEE_REDUX'] +
                              '/r8/apo25m/{:d}/{:d}/apCframe-{:s}-{:08d}.fits'.
                              format(plate, mjd, chip, frame))

    fig, ax = plots.multi(1, 3, hspace=0.01)
    x = np.arange(4096)
    pixmask = bitmask.PixelBitMask()
    for ichip, chip in enumerate(chips):
        y = v[1].data[ichip, :]
        plots.plotl(ax[ichip], x, v[1].data[ichip, :] / v14[1].data[ichip, :])
        bd = np.where(((v[3].data[ichip, :] & pixmask.badval()) > 0) | (
            (v[3].data[ichip, :] & pixmask.getval('SIG_SKYLINE')) > 0))[0]
        y[bd] = np.nan
        plots.plotl(ax[ichip], x, y / v14[1].data[ichip, :])

    fig, ax = plots.multi(3, 3, hspace=0.01)
    x = np.arange(2048)
    for ichip, chip in enumerate(chips):
        plots.plotl(ax[ichip, 0], x, c[chip][1].data[300 - fiber, :])
        plots.plotl(ax[ichip, 0], x, c14[chip][1].data[300 - fiber, :])
        plots.plotl(
            ax[ichip, 1], x,
            c[chip][1].data[300 - fiber, :] / c14[chip][1].data[300 - fiber])
        plots.plotl(
            ax[ichip, 2], x,
            a[chip][1].data[300 - fiber, :] / a14[chip][1].data[300 - fiber])
Ejemplo n.º 3
0
def comp1d(frame, apred='test', rows=range(300)):
    load = apload.ApLoad(apred=apred)
    new = load.ap1D(frame)
    old = {}
    mjd = 55562 + int(frame // 10000)
    fig, ax = plots.multi(1, 3, hspace=0.001)
    x = np.arange(2048)
    for ichip, chip in enumerate(chips):
        old[chip] = fits.open(
            os.environ['APOGEE_REDUX'] +
            '/r8/red/{:d}/ap1D-{:s}-{:d}.fits'.format(mjd, chip, frame))
        for row in rows:
            plots.plotl(ax[ichip],
                        x,
                        new[chip][1].data[row, :] / old[chip][1].data[row, :],
                        yr=[0, 1.5])
Ejemplo n.º 4
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')
Ejemplo n.º 5
0
def clustmember(data,cluster,logg=[-1,3.8],te=[3800,5500],rv=True,pm=True,dist=True,raw=False,firstgen=False,firstpos=True,
                ratag='RA',dectag='DEC',rvtag='VHELIO',idtag='APOGEE_ID',btag='J',rtag='K',
                plot=False,hard=None) :

    clust=clustdata()
    ic = np.where( np.core.defchararray.strip(clust.name) == cluster)[0]
    if len(ic) == 0 :
        print('no cluster found: ',cluster)
        return []
    print('cluster: ', cluster)
    ic=ic[0]

    # adjust ra for wraparound if needed
    ra=copy.copy(data[ratag])
    if clust[ic].ra > 300 :
        j=np.where(data[ratag] < 180)[0]
        ra[j]+=360
    if clust[ic].ra < 60 :
        j=np.where(data[ratag] > 180)[0]
        ra[j]-=360

    # select by location relative to cluster
    jc=np.where((np.abs(ra-clust[ic].ra)*np.cos(clust[ic].dec*np.pi/180.) < clust[ic].rad/60.) & 
                (np.abs(data[dectag]-clust[ic].dec) < clust[ic].rad/60.))[0]
    if len(jc) > 0 :
        j=np.where( ((ra[jc]-clust[ic].ra)*np.cos(clust[ic].dec*np.pi/180.))**2+ 
                     (data[jc][dectag]-clust[ic].dec)**2 < (clust[ic].rad/60.)**2)[0]
        jc=jc[j]
    else :
        jc=[]
    print('{:d} stars after location criterion'.format(len(jc)))
    if len(jc) == 0 : return jc
    if plot :
        jf=np.where((np.abs(ra-clust[ic].ra)*np.cos(clust[ic].dec*np.pi/180.) < 1.5) & 
                (np.abs(data[dectag]-clust[ic].dec) < 1.5))[0]
        fig,ax=plots.multi(1,1)
        fig.suptitle('{:s} Radius: {:4.2f} arcmin  Ngood: {:d}'.format(cluster,clust[ic].rad,len(jc)))
        plots.plotp(ax,ra[jf],data[dectag][jf],color='k',size=20,draw=False,xt='RA',yt='DEC')
        plots.plotp(ax,ra[jc],data[dectag][jc],color='g',size=20,draw=False)
        circle = plt.Circle((clust[ic].ra,clust[ic].dec), clust[ic].rad/60., color='g', fill=False)
        ax.add_artist(circle)

        if hard is not None :
            print(hard+'/'+clust[ic].name+'_pos.png')
            fig.savefig(hard+'/'+clust[ic].name+'_pos.png')
            plt.close()
        else :
            pdb.set_trace()

    # RV criterion
    try :
        vhelio = data[rvtag]
    except :
        vhelio = data['VHELIO_AVG']
    j=np.where(np.abs(vhelio[jc]-clust[ic].rv) < clust[ic].drv)[0]
    if len(j) > 0 :
        if rv: jc=jc[j]
    else :
        jc=[]
    print('{:d} stars after RV criterion'.format(len(jc)))
    if plot :
        ax.cla() 
        try :
            if len(jf) > 0 : ax.hist(vhelio[jf],color='k',bins=np.arange(clust[ic].rv-100,clust[ic].rv+100,1.),histtype='step')
            if len(jc) > 0 : ax.hist(vhelio[jc],color='r',bins=np.arange(clust[ic].rv-100,clust[ic].rv+100,1.),histtype='step')
            if len(j) > 0 : ax.hist(vhelio[jc[j]],color='g',bins=np.arange(clust[ic].rv-100,clust[ic].rv+100,1.),histtype='step',linewidth=3)
        except : pass
        ymax=ax.get_ylim()[1]
        ax.plot([clust[ic].rv,clust[ic].rv],[0,ymax],color='g')
        ax.plot([clust[ic].rv+clust[ic].drv,clust[ic].rv+clust[ic].drv],[0,ymax],color='r',ls=':')
        ax.plot([clust[ic].rv-clust[ic].drv,clust[ic].rv-clust[ic].drv],[0,ymax],color='r',ls=':')
        fig.suptitle('{:s} RV: {:4.2f} +/- {:4.2f} Ngood: {:d}'.format(cluster,clust[ic].rv,clust[ic].drv,len(j)))
        ax.set_xlabel('RV')
        if hard is not None :
            fig.savefig(hard+'/'+clust[ic].name+'_rv.png')
            plt.close()
        else :
            plt.draw()
            pdb.set_trace()
    if len(jc) <= 1 : return jc

    # proper motion criterion
    if dist or pm : gaia = True
    else : gaia = False
    if gaia :
      job=Gaia.launch_job_async("SELECT xm.original_ext_source_id, gaia.ra, gaia.dec,"+
                               "gaia.pmra, gaia.pmra_error, gaia.pmdec, gaia.pmdec_error, gaia.parallax, gaia.parallax_error "+
                               "FROM gaiadr2.gaia_source AS gaia, gaiadr2.tmass_best_neighbour AS xm "+
                               "WHERE gaia.source_id = xm.source_id AND "+
                                  "CONTAINS(POINT('ICRS',gaia.ra,gaia.dec),CIRCLE('ICRS',{:12.6f},{:12.6f},{:12.6f}))=1;".format(
                                                                                  clust[ic].ra,clust[ic].dec,clust[ic].rad/60.))
    
      # convert to velocities (note mas and kpc cancel out factors of 1000) and get median
      gaia=job.get_results()
      h=htm.HTM()
      maxrad=3/3600.
      i1,i2,rad = h.match(data[ratag][jc],data[dectag][jc],gaia['ra'],gaia['dec'],maxrad,maxmatch=1)
      #i1, i2 = match.match(np.core.defchararray.replace(data[idtag][jc],'2M',''),gaia['original_ext_source_id'])
      vra=4.74*gaia['pmra']*clust[ic].dist
      vra_err=4.74*gaia['pmra_error']*clust[ic].dist
      vdec=4.74*gaia['pmdec']*clust[ic].dist
      vdec_err=4.74*gaia['pmdec_error']*clust[ic].dist
      med_vra=np.median(vra[i2])
      med_vdec=np.median(vdec[i2])
      j=np.where((vra[i2]-med_vra)**2+(vdec[i2]-med_vdec)**2 < 2*clust[ic].drv**2+vra_err[i2]**2+vdec_err[i2]**2)[0]
  
      if len(j) > 0 :
        if pm: 
            #jc=jc[i1[j]]
            # allow for the possibility of multiple instances of a given star in input list
            jnew=[]
            for jjj in j :
              iii= np.where(data[idtag][jc]  == data[idtag][jc[i1[jjj]]])[0]
              jnew.extend(iii)
            jc=jc[list(set(jnew))]
      else :
        jc=[]
      print('{:d} stars after PM criterion'.format(len(jc)))
      if plot :
        ax.cla() 
        plots.plotp(ax,vra,vdec,color='k',
                    xr=[med_vra-150,med_vra+150],xt='PMRA (km/sec at cluster dist)',
                    yr=[med_vdec-150,med_vdec+150],yt='PMDEC (km/sec at cluster dist)')
        plots.plotp(ax,vra[i2],vdec[i2],color='r',size=30)
        plots.plotp(ax,vra[i2[j]],vdec[i2[j]],color='g',size=30)
        fig.suptitle('{:s} PM (km/s): {:4.2f} +/- {:4.2f}  {:4.2f} +/ {:4.2f} Ngood: {:d}'.format(
                      cluster,med_vra,clust[ic].drv, med_vdec,clust[ic].drv,len(jc)))
        if hard is not None :
            fig.savefig(hard+'/'+clust[ic].name+'_pm.png')
            plt.close()
        else :
            pdb.set_trace()
      if len(jc) <= 1 : return jc
   
      # parallaxes
      #gaia=job.get_results()
      #i1, i2 = match.match(np.core.defchararray.replace(data[idtag][jc],'2M',''),gaia['original_ext_source_id'])
      i1,i2,rad = h.match(data[ratag][jc],data[dectag][jc],gaia['ra'],gaia['dec'],maxrad,maxmatch=1)
      par=gaia['parallax']
      par_error=gaia['parallax_error']
      gd=np.where(np.isfinite(par[i2]))[0]
      med_par=np.median(par[i2[gd]])
      med_par_error=np.median(par_error[i2[gd]])
      j=np.where(np.isfinite(par[i2]) & (np.abs(par[i2]-med_par) < 3*med_par_error))[0]
      if len(j) > 0 :
        if dist: 
            #jc=jc[i1[j]]
            # allow for the possibility of multiple instances of a given star in input list
            jnew=[]
            for jjj in j :
              iii= np.where(data['APOGEE_ID'][jc]  == data['APOGEE_ID'][jc[i1[jjj]]])[0]
              jnew.extend(iii)
            jc=jc[jnew]
      else :
        jc=[]
      if plot :
        ax.cla() 
        ax.hist(par,color='k',bins=np.arange(par.min(),par.max(),0.01),histtype='step',range=(0,2))
        ax.hist(par[i2],color='r',bins=np.arange(par.min(),par.max(),0.01),histtype='step',range=(0,2))
        ax.hist(par[i2[j]],color='g',bins=np.arange(par.min(),par.max(),0.01),histtype='step',range=(0,2))
        ax.set_xlabel('Parallax')
        fig.suptitle('{:s} Parallax : {:4.2f} +/- {:4.2f} Ngood: {:d}'.format(cluster,med_par, 3*med_par_error,len(j)))
        if hard is not None :
            fig.savefig(hard+'/'+clust[ic].name+'_parallax.png')
            plt.close()
        else :
            plt.draw()
            pdb.set_trace()
      if len(j) > 0 :
        if dist: 
            #jc=jc[i1[j]]
            # allow for the possibility of multiple instances of a given star in input list
            jnew=[]
            for jjj in j :
              iii= np.where(data[idtag][jc]  == data[idtag][jc[i1[jjj]]])[0]
              jnew.extend(iii)
            jc=jc[list(set(jnew))]
      else :
        jc=[]
      print('{:d} stars after parallax criterion'.format(len(jc)))
      if len(jc) <= 1 : return jc

    # parameters criteria
    if raw :
        param='FPARAM'
    else :
        param='PARAM'
    try :
        j = np.where((data[param][jc,1] >= logg[0]) & (data[param][jc,1] <= logg[1]) &
                     (data[param][jc,0] >= te[0]) & (data[param][jc,0] <= te[1]) )[0]
        if len(j) > 0 :
            jc=jc[j]
        else :
            jc=[]
    except: pass
    print('{:d} stars after parameters criterion'.format(len(jc)))
    if len(jc) <= 1 : return jc

    # Remove badstars
    if plot :
        ax.cla()
        plots.plotp(ax,data[btag][jf]-data[rtag][jf],data[rtag][jf],color='k',size=20,xr=[-0.5,1.5],yr=[17,6],
                    facecolors='none',linewidth=1,draw=False,xt=btag+'-'+rtag,yt=rtag)
        plots.plotp(ax,data[btag][jc]-data[rtag][jc],data[rtag][jc],color='g',size=30,xr=[-0.5,1.5],draw=False,yr=[17,6])
    badstars = open(os.environ['APOGEE_DIR']+'/data/calib/badcal.dat')
    bad = []
    for line in badstars :
       bad.append(line.split()[0])
    jc = [x for x in jc if data[x][idtag] not in bad]
    print('{:d} stars after badstars rejection'.format(len(jc)))
    if len(jc) <= 1 : return jc

    # remove non firstgen GC stars if requested
    if firstgen :
        gcstars = ascii.read(os.environ['APOGEE_DIR']+'/data/calib/gc_szabolcs.dat')
        if firstpos :
            gd=np.where(gcstars['pop'] == 1)[0]
            jc = [x for x in jc if data[x][idtag] in gcstars['id'][gd]]
        else :
            bd=np.where(gcstars['pop'] != 1)[0]
            jc = [x for x in jc if data[x][idtag] not in gcstars['id'][bd]]
    print('{:d} stars after firstgen rejection'.format(len(jc)))

    if plot :
        plots.plotp(ax,data[btag][jc]-data[rtag][jc],data[rtag][jc],color='b',size=30,draw=False)
        if hard is not None :
            fig.savefig(hard+'/'+clust[ic].name+'_cmd.png')
            plt.close()
        else :
            plt.draw()
            pdb.set_trace()
        ax.cla()
        plots.plotp(ax,data[param][jf,0],data[param][jf,1],size=30,draw=False,xt='Teff',yt='logg',xr=[7000,3000],yr=[6,-1])
        plots.plotp(ax,data[param][jc,0],data[param][jc,1],color='b',size=30,draw=False)
        if hard is not None :
            fig.savefig(hard+'/'+clust[ic].name+'_kiel.png')
            plt.close()
        else :
            plt.draw()
            pdb.set_trace()

    return jc
Ejemplo n.º 6
0
import numpy as np
from astropy.io import fits
from holtztools import plots
import pdb
import matplotlib.pyplot as plt

fig, ax = plots.multi(3, 1, figsize=(12, 5), wspace=0.001)
plt.show()

darks = [5560001, 12910009, 15640003, 23870002]
colors = ['k', 'r', 'g', 'b']
label = ['07/10/2012', '07/15/2014', '04/14/2015', '07/17/2017']
for ichip, chip in enumerate(['a', 'b', 'c']):
    for idark, dark in enumerate(darks):
        a = fits.open('apDarkRate-{:s}-{:08d}.fits'.format(chip, dark))[0].data
        ax[ichip].hist(a.flatten(),
                       bins=np.arange(-0.5, 10, 0.1),
                       histtype='step',
                       log=True,
                       color=colors[idark])
        ax[ichip].set_xlabel('DN per 10.6s readout')
        if ichip == 0: ax[ichip].set_ylabel('Number of pixels')
        ax[ichip].text(0.8,
                       0.8 - idark * 0.05,
                       label[idark],
                       ha='right',
                       transform=ax[ichip].transAxes,
                       color=colors[idark])
    ax[ichip].text(0.5,
                   0.9,
                   'APOGEE-N chip ' + chip,
Ejemplo n.º 7
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')
Ejemplo n.º 8
0
from ..utils import apload
#from pyvista import tv
import matplotlib.pyplot as plt
from holtztools import plots
import numpy as np
import pdb


def lsfsum(y):
    for col in range(500, 2500, 500):
        print(y[1].data[:, 150, col].sum(), y[1].data[:, 150, col].sum(),
              y[1].data[:, 150, col].sum())


load = apload.ApLoad(apred='r11')
fig, ax = plots.multi(4, 3, hspace=0.001, wspace=0.001)
pfig, pax = plots.multi(9, 3)
for ichip, chip in enumerate(['a', 'b', 'c']):
    #t=tv.TV()
    y1 = load.apLSF(3430016)[chip]
    print('y1: ')
    lsfsum(y1)
    y2 = load.apLSF(7510018)[chip]
    print('y2: ')
    lsfsum(y2)
    y3 = load.apLSF(11130063)[chip]
    print('y3: ')
    lsfsum(y3)
    y4 = load.apLSF(14600018)[chip]
    print('y4: ')
    lsfsum(y4)
Ejemplo n.º 9
0
def visitcomb(allvisit,
              starver,
              load=None,
              apred='r13',
              telescope='apo25m',
              nres=[5, 4.25, 3.5],
              bconly=False,
              plot=False,
              write=True,
              dorvfit=True,
              apstar_vers='stars',
              logger=None):
    """ Combine multiple visits with individual RVs to rest frame sum
    """

    if logger is None:
        logger = dln.basiclogger()

    if load is None: load = apload.ApLoad(apred=apred, telescope=telescope)
    cspeed = 2.99792458e5  # speed of light in km/s

    logger.info('Doing visitcomb for {:s} '.format(allvisit['apogee_id'][0]))

    wnew = norm.apStarWave()
    nwave = len(wnew)
    nvisit = len(allvisit)

    # initialize array for stack of interpolated spectra
    zeros = np.zeros([nvisit, nwave])
    izeros = np.zeros([nvisit, nwave], dtype=int)
    stack = apload.ApSpec(zeros,
                          err=zeros.copy(),
                          bitmask=izeros,
                          cont=zeros.copy(),
                          sky=zeros.copy(),
                          skyerr=zeros.copy(),
                          telluric=zeros.copy(),
                          telerr=zeros.copy())

    apogee_target1, apogee_target2, apogee_target3 = 0, 0, 0
    apogee2_target1, apogee2_target2, apogee2_target3, apogee2_target4 = 0, 0, 0, 0
    starflag, andflag = np.uint64(0), np.uint64(0)
    starmask = bitmask.StarBitMask()

    # Loop over each visit and interpolate to final wavelength grid
    if plot: fig, ax = plots.multi(1, 2, hspace=0.001)
    for i, visit in enumerate(allvisit):

        if bconly: vrel = -visit['bc']
        else: vrel = visit['vrel']

        # Skip if we don't have an RV
        if np.isfinite(vrel) is False: continue

        # Load the visit
        if load.telescope == 'apo1m':
            apvisit = load.apVisit1m(visit['plate'],
                                     visit['mjd'],
                                     visit['apogee_id'],
                                     load=True)
        else:
            apvisit = load.apVisit(int(visit['plate']),
                                   visit['mjd'],
                                   visit['fiberid'],
                                   load=True)
        pixelmask = bitmask.PixelBitMask()

        # Rest-frame wavelengths transformed to this visit spectra
        w = norm.apStarWave() * (1.0 + vrel / cspeed)

        # Loop over the chips
        for chip in range(3):

            # Get the pixel values to interpolate to
            pix = wave.wave2pix(w, apvisit.wave[chip, :])
            gd, = np.where(np.isfinite(pix))

            # Get a smoothed, filtered spectrum to use as replacement for bad values
            cont = gaussian_filter(
                median_filter(apvisit.flux[chip, :], [501], mode='reflect'),
                100)
            errcont = gaussian_filter(
                median_filter(apvisit.flux[chip, :], [501], mode='reflect'),
                100)
            bd, = np.where(apvisit.bitmask[chip, :] & pixelmask.badval())
            if len(bd) > 0:
                apvisit.flux[chip, bd] = cont[bd]
                apvisit.err[chip, bd] = errcont[bd]

            # Load up quantity/error pairs for interpolation
            raw = [[apvisit.flux[chip, :], apvisit.err[chip, :]**2],
                   [apvisit.sky[chip, :], apvisit.skyerr[chip, :]**2],
                   [apvisit.telluric[chip, :], apvisit.telerr[chip, :]**2]]

            # Load up individual mask bits
            for ibit, name in enumerate(pixelmask.name):
                if name is not '' and len(
                        np.where(apvisit.bitmask[chip, :] & 2**ibit)[0]) > 0:
                    raw.append([
                        np.clip(apvisit.bitmask[chip, :] & 2**ibit, None, 1),
                        None
                    ])

            # Do the sinc interpolation
            out = sincint.sincint(pix[gd], nres[chip], raw)

            # From output flux, get continuum to remove, so that all spectra are
            #   on same scale. We'll later multiply in the median continuum
            flux = out[0][0]
            stack.cont[i, gd] = gaussian_filter(
                median_filter(flux, [501], mode='reflect'), 100)

            # Load interpolated spectra into output stack
            stack.flux[i, gd] = out[0][0] / stack.cont[i, gd]
            stack.err[i, gd] = out[0][1] / stack.cont[i, gd]
            stack.sky[i, gd] = out[1][0]
            stack.skyerr[i, gd] = out[1][1]
            stack.telluric[i, gd] = out[2][0]
            stack.telerr[i, gd] = out[2][1]
            # For mask, set bits where interpolated value is above some threshold
            #   defined for each mask bit
            iout = 3
            for ibit, name in enumerate(pixelmask.name):
                if name is not '' and len(
                        np.where(apvisit.bitmask[chip, :] & 2**ibit)[0]) > 0:
                    j = np.where(
                        np.abs(out[iout][0]) > pixelmask.maskcontrib[ibit])[0]
                    stack.bitmask[i, gd[j]] |= 2**ibit
                    iout += 1

        # Increase uncertainties for persistence pixels
        bd, = np.where(
            (stack.bitmask[i, :] & pixelmask.getval('PERSIST_HIGH')) > 0)
        if len(bd) > 0: stack.err[i, bd] *= np.sqrt(5)
        bd, = np.where((
            (stack.bitmask[i, :] & pixelmask.getval('PERSIST_HIGH')) == 0) & (
                (stack.bitmask[i, :] & pixelmask.getval('PERSIST_MED')) > 0))
        if len(bd) > 0: stack.err[i, bd] *= np.sqrt(4)
        bd, = np.where(
            ((stack.bitmask[i, :] & pixelmask.getval('PERSIST_HIGH')) == 0)
            & ((stack.bitmask[i, :] & pixelmask.getval('PERSIST_MED')) == 0)
            & ((stack.bitmask[i, :] & pixelmask.getval('PERSIST_LOW')) > 0))
        if len(bd) > 0: stack.err[i, bd] *= np.sqrt(3)
        bd, = np.where(
            (stack.bitmask[i, :] & pixelmask.getval('SIG_SKYLINE')) > 0)
        if len(bd) > 0: stack.err[i, bd] *= np.sqrt(100)

        if plot:
            ax[0].plot(norm.apStarWave(), stack.flux[i, :])
            ax[1].plot(norm.apStarWave(), stack.flux[i, :] / stack.err[i, :])
            plt.draw()
            pdb.set_trace()

        # Accumulate for header of combined frame. Turn off visit specific RV flags first
        visitflag = visit['starflag'] & ~starmask.getval(
            'RV_REJECT') & ~starmask.getval('RV_SUSPECT')
        starflag |= visitflag
        andflag &= visitflag
        if visit['survey'] == 'apogee':
            apogee_target1 |= visit['apogee_target1']
            apogee_target2 |= visit['apogee_target2']
            apogee_target3 |= visit['apogee_target3']
        elif visit['survey'].find('apogee2') >= 0:
            apogee2_target1 |= visit['apogee_target1']
            apogee2_target2 |= visit['apogee_target2']
            apogee2_target3 |= visit['apogee_target3']
            try:
                apogee2_target4 |= visit['apogee_target4']
            except:
                pass
        # MWM target flags?

    # Create final spectrum
    zeros = np.zeros([nvisit + 2, nwave])
    izeros = np.zeros([nvisit + 2, nwave], dtype=int)
    apstar = apload.ApSpec(zeros,
                           err=zeros.copy(),
                           bitmask=izeros,
                           wave=norm.apStarWave(),
                           sky=zeros.copy(),
                           skyerr=zeros.copy(),
                           telluric=zeros.copy(),
                           telerr=zeros.copy(),
                           cont=zeros.copy(),
                           template=zeros.copy())
    apstar.header['CRVAL1'] = norm.logw0
    apstar.header['CDELT1'] = norm.dlogw
    apstar.header['CRPIX1'] = 1
    apstar.header['CTYPE1'] = (
        'LOG-LINEAR', 'Logarithmic wavelength scale in subsequent HDU')
    apstar.header['DC-FLAG'] = 1

    # Pixel-by-pixel weighted average
    cont = np.median(stack.cont, axis=0)
    apstar.flux[0, :] = np.sum(stack.flux / stack.err**2, axis=0) / np.sum(
        1. / stack.err**2, axis=0) * cont
    apstar.err[0, :] = np.sqrt(1. / np.sum(1. / stack.err**2, axis=0)) * cont
    apstar.bitmask[0, :] = np.bitwise_and.reduce(stack.bitmask, 0)
    apstar.cont[0, :] = cont

    # Individual visits
    apstar.flux[2:, :] = stack.flux * stack.cont
    apstar.err[2:, :] = stack.err * stack.cont
    apstar.bitmask[2:, :] = stack.bitmask
    apstar.sky[2:, :] = stack.sky
    apstar.skyerr[2:, :] = stack.skyerr
    apstar.telluric[2:, :] = stack.telluric
    apstar.telerr[2:, :] = stack.telerr

    # Populate header
    apstar.header['OBJID'] = (allvisit['apogee_id'][0], 'APOGEE object name')
    apstar.header['APRED'] = (apred, 'APOGEE reduction version')
    apstar.header['STARVER'] = (starver, 'apStar version')
    apstar.header['HEALPIX'] = (apload.obj2healpix(allvisit['apogee_id'][0]),
                                'HEALPix location')
    try:
        apstar.header['SNR'] = (np.nanmedian(apstar.flux / apstar.err),
                                'Median S/N per apStar pixel')
    except:
        apstar.header['SNR'] = (0., 'Median S/N per apStar pixel')
    apstar.header['RA'] = (allvisit['ra'].max(), 'right ascension, deg, J2000')
    apstar.header['DEC'] = (allvisit['dec'].max(), 'declination, deg, J2000')
    apstar.header['GLON'] = (allvisit['glon'].max(), 'Galactic longitude')
    apstar.header['GLAT'] = (allvisit['glat'].max(), 'Galactic latitude')
    apstar.header['J'] = (allvisit['j'].max(), '2MASS J magnitude')
    apstar.header['J_ERR'] = (allvisit['j_err'].max(),
                              '2MASS J magnitude uncertainty')
    apstar.header['H'] = (allvisit['h'].max(), '2MASS H magnitude')
    apstar.header['H_ERR'] = (allvisit['h_err'].max(),
                              '2MASS H magnitude uncertainty')
    apstar.header['K'] = (allvisit['k'].max(), '2MASS K magnitude')
    apstar.header['K_ERR'] = (allvisit['k_err'].max(),
                              '2MASS K magnitude uncertainty')
    try:
        apstar.header['SRC_H'] = (allvisit['src_h'][0],
                                  'source of H magnitude')
    except KeyError:
        pass
    keys = [
        'wash_m', 'wash_t2', 'ddo51', 'irac_3_6', 'irac_4_5', 'irac_5_8',
        'wise_4_5', 'targ_4_5'
    ]
    for key in keys:
        try:
            apstar.header[key] = allvisit[key].max()
        except KeyError:
            pass

    apstar.header['AKTARG'] = (allvisit['ak_targ'].max(),
                               'Extinction used for targeting')
    apstar.header['AKMETHOD'] = (allvisit['ak_targ_method'][0],
                                 'Extinction method using for targeting')
    apstar.header['AKWISE'] = (allvisit['ak_wise'].max(),
                               'WISE all-sky extinction')
    apstar.header['SFD_EBV'] = (allvisit['sfd_ebv'].max(), 'SFD E(B-V)')
    apstar.header['APTARG1'] = (apogee_target1,
                                'APOGEE_TARGET1 targeting flag')
    apstar.header['APTARG2'] = (apogee_target2,
                                'APOGEE_TARGET2 targeting flag')
    apstar.header['APTARG3'] = (apogee_target3,
                                'APOGEE_TARGET3 targeting flag')
    apstar.header['AP2TARG1'] = (apogee2_target1,
                                 'APOGEE2_TARGET1 targeting flag')
    apstar.header['AP2TARG2'] = (apogee2_target2,
                                 'APOGEE2_TARGET2 targeting flag')
    apstar.header['AP2TARG3'] = (apogee2_target3,
                                 'APOGEE2_TARGET3 targeting flag')
    apstar.header['AP2TARG4'] = (apogee2_target4,
                                 'APOGEE2_TARGET4 targeting flag')
    apstar.header['NVISITS'] = (len(allvisit),
                                'Number of visit spectra combined flag')
    apstar.header['STARFLAG'] = (starflag,
                                 'bitwise OR of individual visit starflags')
    apstar.header['ANDFLAG'] = (andflag,
                                'bitwise AND of individual visit starflags')

    try:
        apstar.header['N_COMP'] = (allvisit['n_components'].max(),
                                   'Maximum number of components in RV CCFs')
    except:
        pass
    apstar.header['VHBARY'] = (
        (allvisit['vheliobary'] * allvisit['snr']).sum() /
        allvisit['snr'].sum(), 'S/N weighted mean barycentric RV')
    if len(allvisit) > 1:
        apstar.header['vscatter'] = (allvisit['vheliobary'].std(ddof=1),
                                     'standard deviation of visit RVs')
    else:
        apstar.header['VSCATTER'] = (0., 'standard deviation of visit RVs')
    apstar.header['VERR'] = (0., 'unused')
    apstar.header['RV_TEFF'] = (allvisit['rv_teff'].max(),
                                'Effective temperature from RV fit')
    apstar.header['RV_LOGG'] = (allvisit['rv_logg'].max(),
                                'Surface gravity from RV fit')
    apstar.header['RV_FEH'] = (allvisit['rv_feh'].max(),
                               'Metallicity from RV fit')

    if len(allvisit) > 0:
        meanfib = (allvisit['fiberid'] *
                   allvisit['snr']).sum() / allvisit['snr'].sum()
    else:
        meanfib = 999999.
    if len(allvisit) > 1: sigfib = allvisit['fiberid'].std(ddof=1)
    else: sigfib = 0.
    apstar.header['MEANFIB'] = (meanfib, 'S/N weighted mean fiber number')
    apstar.header['SIGFIB'] = (
        sigfib, 'standard deviation (unweighted) of fiber number')
    apstar.header['NRES'] = ('{:5.2f}{:5.2f}{:5.2f}'.format(*nres),
                             'number of pixels/resolution used for sinc')

    # individual visit information in header
    for i0, visit in enumerate(allvisit):
        i = i0 + 1
        apstar.header['SFILE{:d}'.format(i)] = (
            visit['file'], ' Visit #{:d} spectrum file'.format(i))
        apstar.header['DATE{:d}'.format(i)] = (
            visit['dateobs'], 'DATE-OBS of visit {:d}'.format(i))
        apstar.header['JD{:d}'.format(i)] = (
            visit['jd'], 'Julian date of visit {:d}'.format(i))
        # hjd = helio_jd(visitstr[i].jd-2400000.0,visitstr[i].ra,visitstr[i].dec)
        #apstar.header['HJD{:d}'.format(i)] =
        apstar.header['FIBER{:d}'.format(i)] = (visit['fiberid'],
                                                ' Fiber, visit {:d}'.format(i))
        apstar.header['BC{:d}'.format(i)] = (
            visit['bc'],
            ' Barycentric correction (km/s), visit {:d}'.format(i))
        apstar.header['VRAD{:d}'.format(i)] = (
            visit['vrel'], ' Doppler shift (km/s) of visit {:d}'.format(i))
        #apstar.header['VERR%d'.format(i)] =
        apstar.header['VHBARY{:d}'.format(i)] = (
            visit['vheliobary'],
            ' Barycentric velocity (km/s), visit {:d}'.format(i))
        apstar.header['SNRVIS{:d}'.format(i)] = (
            visit['snr'], ' Signal/Noise ratio, visit {:d}'.format(i))
        apstar.header['FLAG{:d}'.format(i)] = (
            visit['starflag'], ' STARFLAG for visit {:d}'.format(i))
        apstar.header.insert('SFILE{:d}'.format(i),
                             ('COMMENT', 'VISIT {:d} INFORMATION'.format(i)))

    # Do a RV fit just to get a template and normalized spectrum, for plotting
    if dorvfit:
        try:
            apstar.setmask(pixelmask.badval())
            spec = doppler.Spec1D(apstar.flux[0, :],
                                  err=apstar.err[0, :],
                                  bitmask=apstar.bitmask[0, :],
                                  mask=apstar.mask[0, :],
                                  wave=apstar.wave,
                                  lsfpars=np.array([0]),
                                  lsfsigma=apstar.wave / 22500 / 2.354,
                                  instrument='APOGEE',
                                  filename=apstar.filename)
            out = doppler.rv.jointfit([spec],
                                      verbose=False,
                                      plot=False,
                                      tweak=False,
                                      maxvel=[-50, 50])
            apstar.cont = out[3][0].flux
            apstar.template = out[2][0].flux
        except ValueError as err:
            logger.error('Exception raised in visitcomb RV for: ',
                         apstar.header['FIELD'], apstar.header['OBJID'])
            logger.error("ValueError: {0}".format(err))
        except RuntimeError as err:
            logger.error('Exception raised in visitcomb RV for: ',
                         apstar.header['FIELD'], apstar.header['OBJID'])
            logger.error("Runtime error: {0}".format(err))
        except:
            logger.error('Exception raised in visitcomb RV fit for: ',
                         apstar.header['FIELD'], apstar.header['OBJID'])

    # Write the spectrum to file
    if write:
        outfilenover = load.filename('Star', obj=apstar.header['OBJID'])
        outdir = os.path.dirname(outfilenover)
        outbase = os.path.splitext(os.path.basename(outfilenover))[0]
        outbase += '-' + starver  # add star version
        outfile = outdir + '/' + outbase + '.fits'
        if apstar_vers != 'stars':
            outfile = outfile.replace('/stars/', '/' + apstar_vers + '/')
        outdir = os.path.dirname(outfile)
        try:
            os.makedirs(os.path.dirname(outfile))
        except:
            pass
        logger.info('Writing apStar file to ' + outfile)
        apstar.write(outfile)
        apstar.filename = outfile
        mwm_root = os.environ['MWM_ROOT']
        apstar.uri = outfile[len(mwm_root) + 1:]
        # Create symlink no file with no version
        if os.path.exists(outfilenover) or os.path.islink(outfilenover):
            os.remove(outfilenover)
        os.symlink(os.path.basename(outfile), outfilenover)  # relative path

        # Plot
        gd, = np.where(
            (apstar.bitmask[0, :]
             & (pixelmask.badval() | pixelmask.getval('SIG_SKYLINE'))) == 0)
        fig, ax = plots.multi(1, 3, hspace=0.001, figsize=(48, 6))
        med = np.nanmedian(apstar.flux[0, :])
        plots.plotl(ax[0],
                    norm.apStarWave(),
                    apstar.flux[0, :],
                    color='k',
                    yr=[0, 2 * med])
        ax[0].plot(norm.apStarWave()[gd], apstar.flux[0, gd], color='g')
        ax[0].set_ylabel('Flux')
        try:
            ax[1].plot(norm.apStarWave()[gd], apstar.cont[gd], color='g')
            ax[1].set_ylabel('Normalized')
            ax[1].plot(norm.apStarWave(), apstar.template, color='r')
        except:
            pass
        plots.plotl(ax[2],
                    norm.apStarWave(),
                    apstar.flux[0, :] / apstar.err[0, :],
                    yt='S/N')
        for i in range(3):
            ax[i].set_xlim(15100, 17000)
        ax[0].set_xlabel('Wavelength')
        fig.savefig(outdir + '/plots/' + outbase + '.png')

    # Plot
    if plot:
        ax[0].plot(norm.apStarWave(), apstar.flux, color='k')
        ax[1].plot(norm.apStarWave(), apstar.flux / apstar.err, color='k')
        plt.draw()
        pdb.set_trace()

    return apstar
Ejemplo n.º 10
0
def dop_plot(outdir, obj, dopout, decomp=None):
    """ RV diagnostic plots
    """
    sumstr, finalstr, bmodel, specmlist = dopout

    matplotlib.use('Agg')
    n = len(bmodel)
    # Plot final spectra and final models
    # full spectrum
    fig, ax = plots.multi(1, n, hspace=0.001, figsize=(8, 2 + n))
    ax = np.atleast_1d(ax)
    # continuum
    figc, axc = plots.multi(1, n, hspace=0.001, figsize=(8, 2 + n))
    axc = np.atleast_1d(axc)
    # windows
    windows = [[15700, 15780], [15850, 16000], [16700, 16930]]
    fig2, ax2 = plots.multi(len(windows),
                            n,
                            hspace=0.001,
                            wspace=0.001,
                            figsize=(12, 2 + n))
    ax2 = np.atleast_2d(ax2)

    # Loop over visits
    for i, (mod, spec) in enumerate(zip(bmodel, specmlist)):
        ax[i].plot(spec.wave, spec.flux, color='k')
        for iorder in range(3):
            gd, = np.where(~spec.mask[:, iorder])
            ax[i].plot(spec.wave[gd, iorder], spec.flux[gd, iorder], color='g')
        ax[i].plot(mod.wave, mod.flux, color='r')
        ax[i].text(0.1,
                   0.1,
                   '{:d}'.format(spec.head['MJD5']),
                   transform=ax[i].transAxes)
        for iwind, wind in enumerate(windows):
            ax2[i, iwind].plot(spec.wave, spec.flux, color='k')
            for iorder in range(3):
                gd, = np.where(~spec.mask[:, iorder])
                ax2[i, iwind].plot(spec.wave[gd, iorder],
                                   spec.flux[gd, iorder],
                                   color='g')
            ax2[i, iwind].plot(mod.wave, mod.flux, color='r')
            ax2[i, iwind].set_xlim(wind[0], wind[1])
            ax2[i, iwind].set_ylim(0.5, 1.3)
            if iwind == 0:
                ax2[i, iwind].text(0.1,
                                   0.1,
                                   '{:d}'.format(spec.head['MJD5']),
                                   transform=ax2[i, 0].transAxes)
        axc[i].plot(spec.wave, spec.flux * spec.cont, color='k')
        axc[i].plot(spec.wave, spec.cont, color='g')
        axc[i].text(0.1,
                    0.1,
                    '{:d}'.format(spec.head['MJD5']),
                    transform=axc[i].transAxes)
    fig.savefig(outdir + '/' + obj + '_spec.png')
    plt.close()
    fig2.savefig(outdir + '/' + obj + '_spec2.png')
    plt.close()
    figc.savefig(outdir + '/' + obj + '_cont.png')
    plt.close()

    # Plot cross correlation functions with final model
    fig, ax = plots.multi(1, n, hspace=0.001, figsize=(6, 2 + n))
    ax = np.atleast_1d(ax)
    vmed = np.median(finalstr['vrel'])
    for i, (final, spec) in enumerate(zip(finalstr, specmlist)):
        ax[i].plot(final['x_ccf'], final['ccf'], color='k')
        ax[i].plot(final['x_ccf'], final['ccferr'], color='r')
        ax[i].plot([final['vrel'], final['vrel']],
                   ax[i].get_ylim(),
                   color='g',
                   label='fit RV')
        ax[i].plot([final['xcorr_vrel'], final['xcorr_vrel']],
                   ax[i].get_ylim(),
                   color='r',
                   label='xcorr RV')
        ax[i].text(0.1,
                   0.9,
                   '{:d}'.format(spec.head['MJD5']),
                   transform=ax[i].transAxes)
        ax[i].set_xlim(vmed - 200, vmed + 200)
        ax[i].legend()
        if decomp is not None:
            try:
                n = decomp[i]['N_components']
            except:
                n = 0
            if n > 0: n = len(decomp[i]['best_fit_parameters']) // 3
            x = final['x_ccf']
            for j in range(n):
                pars = decomp[i]['best_fit_parameters'][j::n]
                ax[i].plot(x, gaussian(*pars)(x))
                if pars[0] > 0: color = 'k'
                else: color = 'r'
                ax[i].text(0.1,
                           0.8 - j * 0.1,
                           '{:8.1f}{:8.1f}{:8.1f}'.format(*pars),
                           transform=ax[i].transAxes,
                           color=color)
    fig.savefig(outdir + '/' + obj + '_ccf.png')
    plt.close()
Ejemplo n.º 11
0
def gauss_decomp(out,
                 phase='one',
                 alpha1=0.5,
                 alpha2=1.5,
                 thresh=[4, 4],
                 plot=None,
                 filt=False):
    """ Do Gaussian decomposition of CCF using gausspy

        Parameters:
        out : list of dictionaries for each frame, giving x_ccf, ccf, and ccferr
        phase : gausspy paramater
        alpha1 : gausspy parameter
        alpha2 : gausspy parameter for second set of gaussians if phase=='two'
        thresh : gausspy parameter
        plot (str) : if not None, do plot and use as root file name for plot
        filt (bool) : if true, apply filtering to remove components judged to be insignificant
    """
    g = gp.GaussianDecomposer()
    g.set('phase', phase)
    g.set('SNR_thresh', thresh)
    g.set('alpha1', alpha1)
    g.set('alpha2', alpha2)
    gout = []
    if plot is not None:
        fig, ax = plots.multi(1, len(out), hspace=0.001, figsize=(6, 2 + n))
    for i, final in enumerate(out):
        gd, = np.where(np.isfinite(final['x_ccf']))
        x = final['x_ccf'][gd]
        y = final['ccf'][gd]
        # high pass filter for better performance
        if filt:
            final['ccf'][gd] -= gaussian_filter(final['ccf'][gd],
                                                50,
                                                mode='nearest')
        try:
            decomp = g.decompose(x, final['ccf'][gd], final['ccferr'][gd])
            n = decomp['N_components']
        except:
            print(
                'Exception in Gaussian decomposition, setting to 0 components')
            n = 0
            decomp = None
        if filt and n > 0:
            # remove components if they are within width of brighter component, or <0.25 peak ,
            #   or more than twice as wide, or if primary component is wide
            for j in range(1, n):
                pars_j = decomp['best_fit_parameters'][j::n]
                for k in range(j):
                    pars_k = decomp['best_fit_parameters'][k::n]
                    if (pars_j[0] > pars_k[0] and pars_k[0] > 0 and
                        (abs(pars_j[2] - pars_k[2]) < abs(pars_j[1]) or
                         pars_k[0] < 0.25 * pars_j[0] or abs(pars_j[1]) > 100
                         or np.abs(pars_k[1]) > 2 * np.abs(pars_j[1]))):
                        decomp['best_fit_parameters'][k] = 0
                        decomp['N_components'] -= 1
                    elif (pars_k[0] > pars_j[0] and pars_j[0] > 0
                          and (abs(pars_j[2] - pars_k[2]) < abs(pars_k[1])
                               or pars_j[0] < 0.25 * pars_k[0]
                               or abs(pars_k[1]) > 100
                               or np.abs(pars_j[1]) > 2 * np.abs(pars_k[1]))):
                        decomp['best_fit_parameters'][j] = 0
                        pars_j = decomp['best_fit_parameters'][j::n]
                        decomp['N_components'] -= 1

        gout.append(decomp)
        if plot is not None:
            plots.plotl(ax[i], final['x_ccf'], final['ccf'])
            ax[i].plot(final['x_ccf'], final['ccferr'], color='r')
            for j in range(n):
                pars = gout[i]['best_fit_parameters'][j::n]
                ax[i].plot(x, gaussian(*pars)(x))
                if pars[0] > 0: color = 'k'
                else: color = 'r'
                ax[i].text(0.1,
                           0.8 - j * 0.1,
                           '{:8.1f}{:8.1f}{:8.1f}'.format(*pars),
                           transform=ax[i].transAxes,
                           color=color)
            fig.savefig(plot + '_ccf.png')
    del g
    return gout
Ejemplo n.º 12
0
import matplotlib.pyplot as plt
import numpy as np
from astropy.io import fits
from holtztools import plots
from ..utils import apload, yanny
import os
import pdb

chips = ['a', 'b', 'c']
colors = ['r', 'g', 'b']

# APOGEE-N
fig, ax = plots.multi(1, 3, hspace=0.001, sharex=True, sharey=True)
t11 = apload.ApLoad(apred='t11')
b = t11.ap1D(3190056)
plug = yanny.yanny(os.environ['MAPPER_DATA_N'] +
                   '/55880/plPlugMapM-5585-55880-01.par')
objType = np.array(plug['PLUGMAPOBJ']['objType'])
fibers = np.array(plug['PLUGMAPOBJ']['fiberId'])
tel = np.where(objType == 'SPECTROPHOTO_STD')[0]
rows = 300 - fibers[tel]
amed = np.median(b['a'][1].data[rows, :], axis=1)
bmed = np.median(b['b'][1].data[rows, :], axis=1)
cmed = np.median(b['c'][1].data[rows, :], axis=1)
anorm = np.median(bmed / amed)
cnorm = np.median(bmed / cmed)
anorm = 1.
cnorm = 1.
npix = 190
design = np.zeros([3 * npix * len(rows), 5 + len(rows)])
y = np.zeros([3 * npix * len(rows)])
Ejemplo n.º 13
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()