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 apolco(hdulist, out='./', snmin=150): """ histograms of LCO-APO parameters and abundances """ gd = apselect.select(hdulist[1].data, badval='STAR_BAD', sn=[snmin, 10000]) a = hdulist[1].data[gd] apo = np.where(a['TELESCOPE'] == 'apo25m')[0] lco = np.where(a['TELESCOPE'] == 'lco25m')[0] i1, i2 = match.match(a['APOGEE_ID'][apo], a['APOGEE_ID'][lco]) grid = [] yt = [] for iparam, param in enumerate(hdulist[3].data['PARAM_SYMBOL'][0]): fig, ax = plots.multi(1, 1, figsize=(6, 4.5)) diff = a['FPARAM'][lco[i2], iparam] - a['FPARAM'][apo[i1], iparam] if iparam == 0: ax.hist(diff, bins=np.arange(-100., 100., 1.)) else: ax.hist(diff, bins=np.arange(-0.5, 0.5, 0.01)) ax.set_xlabel('{:s} (LCO-APO)'.format(param)) ax.text(0.1, 0.9, 'S/N> {:d}'.format(snmin), transform=ax.transAxes) ax.text(0.1, 0.8, 'mean: {:8.3f}'.format(diff.mean()), transform=ax.transAxes) ax.text(0.1, 0.7, 'std: {:8.3f}'.format(diff.std()), transform=ax.transAxes) outfile = out + 'apolco_param_{:d}.png'.format(iparam) fig.savefig(outfile) plt.close() grid.append([os.path.basename(outfile)]) yt.append(param) for ielem, el in enumerate(hdulist[3].data['ELEM_SYMBOL'][0]): fig, ax = plots.multi(1, 1, figsize=(6, 4.5)) diff = a['FELEM'][lco[i2], ielem] - a['FELEM'][apo[i1], ielem] ax.hist(diff, bins=np.arange(-0.5, 0.5, 0.01)) ax.set_xlabel('{:s} (LCO-APO)'.format(el)) ax.text(0.1, 0.9, 'S/N> {:d}'.format(snmin), transform=ax.transAxes) ax.text(0.1, 0.8, 'mean: {:8.3f}'.format(diff.mean()), transform=ax.transAxes) ax.text(0.1, 0.7, 'std: {:8.3f}'.format(diff.std()), transform=ax.transAxes) outfile = out + 'apolco_{:s}.png'.format(el) fig.savefig(outfile) plt.close() grid.append([os.path.basename(outfile)]) yt.append(el) html.htmltab(grid, file=out + 'apolco.html', ytitle=yt)
def gauss() : """ simple simulator to assess statistics of drawing repeats from a Gaussian distribution """ # draw pairs from a unit-Gaussian and accumulate differences diff=[] for i in range(1000000) : a=normal(size=2) diff.append(a[0]-a[1]) fig,ax=plots.multi(1,1,figsize=(10,5)) diff=np.array(diff) ax.hist(diff,bins=51) ax.set_xlabel('Difference between pair') ax.text(0.02,0.95,'<diff>: {:7.3f}'.format(diff.mean()), transform=ax.transAxes,size='small') # Vijith says std of differences is sqrt(2), which is verified ax.text(0.02,0.90,'std: {:7.3f}'.format(diff.std()), transform=ax.transAxes,size='small') # biased estimator of standard deviation ax.text(0.02,0.85,'<abs(diff/sqrt(2))>: {:7.3f}'.format(abs(diff/np.sqrt(2.)).mean()), transform=ax.transAxes,size='small') # is unbiased estimator of variance! ax.text(0.02,0.80,'<diff**2/2>: {:7.3f}'.format(((diff**2/2)).mean()), transform=ax.transAxes,size='small') # corrected biased estimator by sqrt(pi/2.) ax.text(0.02,0.75,'<abs(diff/sqrt(2)*sqrt(pi/2)>: {:7.3f}'.format(abs(diff/np.sqrt(2)*np.sqrt(np.pi/2.)).mean()), transform=ax.transAxes,size='small') # but what if we take the log, compare to exp(<log>) ax.text(0.02,0.70,'exp(<log(abs(diff/sqrt(2)*sqrt(pi/2))>): {:7.3f}'.format(np.exp(np.log(abs(diff/np.sqrt(2)*np.sqrt(np.pi/2.))).mean())), transform=ax.transAxes,size='small') ax.set_xlim(-10,8) print(diff.mean(),diff.std(),abs(diff).mean(),(diff**2).mean(),(diff/np.sqrt(2.)).std(),abs(diff*np.sqrt(np.pi/2.)).mean()) print(abs(diff*np.sqrt(np.pi/2.)).mean(),np.exp(np.log(abs(diff*np.sqrt(np.pi/2.))).mean())) """
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)
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])
def sens(model='apogee-dr14-giants.model', teff=4500, logg=3, mh=0., xh=False): ''' Plot sensitivities of spectra to changes in Cannon labels ''' model = tc.load_model(model) # get label names label_names = model.vectorizer.label_names # set default parameters if xh: d0 = mh else: d0 = 0. pars = [ teff, logg, mh, 0., mh, d0, d0, d0, d0, d0, d0, d0, d0, d0, d0, d0, d0, d0, d0, d0, d0, d0, d0, d0 ] #,d0] synth0 = model.predict(pars)[0] fig, ax = plots.multi(1, 1) axalt = ax.twinx() wave = 10.**(4.179 + 6.e-6 * np.arange(8575)) figs = [] for i, name in enumerate(label_names): print(name) delta = np.zeros(len(pars)) if i == 0: delta[i] += 100. else: delta[i] += 0.1 synth = model.predict(pars + delta)[0] ax.cla() axalt.cla() plots.plotl(ax, wave, synth - synth0, yr=[-0.10, 0.05]) ax.text(0.1, 0.9, name, transform=ax.transAxes) if name not in ['TEFF', 'LOGG', 'M_H', 'ALPHA_M']: elem = name.split('_')[0] print(elem, ' ', name) aspcap.elemsens([elem], plot=axalt, teff=teff, logg=logg, feh=mh, smooth=2, ylim=[0.05, -0.10]) file = name + '_sens.jpg' fig.savefig('plots/' + file) figs.append([file]) pdb.set_trace() #pdb.set_trace() html.htmltab(figs, file='plots/sens.html')
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)
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)
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])
def parplot(lsf, hard=None): """ Plot the LSF parameters of an LSF fit """ fig, ax = plots.multi(4, 3) colors = ['r', 'g', 'b'] for ichip, chip in enumerate(['a', 'b', 'c']): ii = 0 for ipar in range(9, 18) + range(24, 26): iy = ii % 3 ix = ii // 3 ax[iy, ix].plot(lsf[chip][0].data[ipar, :], color=colors[ichip]) ii += 1 if hard is not None: fig.savefig(hard + '.png') plt.close()
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])
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()
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')
def correct(field,libfile,plot=True,write=None,width=151) : """ Read raw FERRE files and create correction to normalization based on ratio """ lib=ferre.rdlibhead(libfile+'.hdr') out=ferre.read(field,libfile+'.hdr') nspec=out[1]['obs'].shape[0] norm=copy.copy(out[1]['obs']) if plot : fig,ax=plots.multi(1,3) for i in range(nspec) : p1=0 for ichip in range(3) : npix=lib[1][ichip]['NPIX'] obs=out[1]['obs'][i,p1:p1+npix] # replace bad pixels with median filtered value med=median_filter(obs,[5*width],mode='nearest') bd=np.where(obs < 0.01)[0] obs[bd] = med[bd] # get ratio of observed / model, make sure edge is reasonable number mdl=out[1]['mdl'][i,p1:p1+npix] ratio=obs/mdl ratio[0]=np.median(ratio[0:9]) ratio[-1]=np.median(ratio[-9:0]) corr=median_filter(obs/mdl,[width],mode='nearest') norm[i,p1:p1+npix] = corr if plot : ax[ichip].cla() plots.plotl(ax[ichip],np.arange(npix),obs,yr=[0.95,1.05],color='r') plots.plotl(ax[ichip],np.arange(npix),mdl,yr=[0.95,1.05],color='b') plots.plotl(ax[ichip],np.arange(npix),obs/mdl,yr=[0.95,1.05],color='g') plots.plotl(ax[ichip],np.arange(npix),corr,color='k',linewidth=3,yr=[0.95,1.05]) plt.draw() plt.show() p1+=npix if plot : pdb.set_trace() if write is not None: # make sure we have no zeros bd=np.where(norm.ravel() < 0.01)[0] norm.ravel()[bd]=1. ferre.writespec(write,norm) return norm
def chip(a, row=150, ax=None, pixel=False, visit=False, color=None): """ Routine to plot 3 chips in 3 panels """ if ax is None: fig, ax = plots.multi(1, 3, hspace=0.3) if visit: rows = range(3) else: rows = [row, row, row] chips = ['a', 'b', 'c'] x = np.arange(a['a'][1].header['NAXIS1']) for ichip, chip in enumerate(chips): if pixel: plots.plotl(ax[ichip], x, a[chip][1].data[rows[ichip], :], color=color) else: plots.plotl(ax[ichip], a[chip][4].data[rows[ichip], :], a[chip][1].data[rows[ichip], :], color=color) try: return fig, ax except: return
import matplotlib.pyplot as plt import numpy as np from astropy.io import fits from tools import plots from sdss import yanny from apogee.utils import apload 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'] + '/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)])
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
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()
def comp(planfile='tgGK_180625.par', dir='marcs/giantisotopes/tgGK_180625', grid='GK', fakehole=False, cmrange=None, nmrange=None, vtrange=None, apstar=False, hard=None, out='rbf_'): # Read planfile and set output file name if not os.path.isfile(planfile): print('{:s} does not exist'.format(planfile)) return p = yanny.yanny(planfile, np=True) # input directory if dir is None: indir = os.environ['APOGEE_SPECLIB'] + '/synth/' + p[ 'specdir'] + '/' if p.get('specdir') else './' else: indir = os.environ['APOGEE_SPECLIB'] + '/synth/turbospec/' + dir + '/' if grid is None: if p['name'].find('GK_'): grid = 'GK' elif p['name'].find('M_'): grid = 'M' elif p['name'].find('F_'): grid = 'F' if cmrange is None: cmrange = spectra.vector(p['cm0'], p['dcm'], p['ncm']) if nmrange is None: nmrange = spectra.vector(p['nm0'], p['dnm'], p['nnm']) if vtrange is None: try: vtrange = 10.**spectra.vector(p['vt0'], p['dvt'], p['nvt']) except: vtrange = [float(p['vmicro'])] if grid is None: if p['specdir'].find('GK_') >= 0: grid = 'GK' elif p['specdir'].find('M_') >= 0: grid = 'M' elif p['specdir'].find('F_') >= 0: grid = 'F' holefile = 'MARCS_' + grid + '_holefile.fits' if fakehole: holes = fits.open(out + holefile)[0] else: holes = fits.open(os.environ['APOGEE_SPECLIB'] + '/atmos/marcs/MARCS_v3_2016/' + holefile)[0] if apstar: prefix = '' else: prefix = '' ii = 0 nx = 5 ny = 6 figs = [] for icm, cm in enumerate(cmrange): for inm, nm in enumerate(nmrange): for ivt, vt in enumerate(vtrange): for iam, am in enumerate( spectra.vector(p['am0'], p['dam'], p['nam'])): print(am, cm, nm, vt) file = ('a{:s}c{:s}n{:s}v{:s}.fits').format( atmos.cval(am), atmos.cval(cm), atmos.cval(nm), atmos.cval(vt)) raw = fits.open(indir + prefix + file)[0].data filled = fits.open(out + file)[0].data #holes=fits.open(out+file)[1].data for imh, mh in enumerate( spectra.vector(p['mh0'], p['dmh'], p['nmh'])): for ilogg, logg in enumerate( spectra.vector(p['logg0'], p['dlogg'], p['nlogg'])): for iteff, teff in enumerate( spectra.vector(p['teff0'], p['dteff'], p['nteff'])): hcm = int( round((cm - holes.header['CRVAL5']) / holes.header['CDELT5'])) if hcm < 0: #print('off carbon grid edge!',hcm) hcm = 0 ham = int( round((am - holes.header['CRVAL4']) / holes.header['CDELT4'])) hmh = int( round((mh - holes.header['CRVAL3']) / holes.header['CDELT3'])) hlogg = int( round((logg - holes.header['CRVAL2']) / holes.header['CDELT2'])) hteff = int( round((teff - holes.header['CRVAL1']) / holes.header['CDELT1'])) if ((fakehole and abs(holes.data[hcm, ham, hmh, hlogg, hteff]) > 99.) or (not fakehole and abs(holes.data[hcm, ham, hmh, hlogg, hteff]) > 0)): print( cm, nm, vt, am, mh, logg, teff, holes.data[hcm, ham, hmh, hlogg, hteff]) lab = '{:6.2f}{:6.2f}{:6.2f}{:6.0f}{:7.2f}'.format( am, mh, logg, teff, holes.data[hcm, ham, hmh, hlogg, hteff]) lab1 = '{:6.2f}{:6.2f}{:6.2f}'.format( cm, nm, vt) if hard: if ii % (nx * ny) == 0: fig, ax = plots.multi(nx, ny, hspace=0.001, wspace=0.001, figsize=(14, 8)) iy = ii % ny ix = (ii % (nx * ny)) / ny print(ii, iy, ix) #ax[iy,ix].plot(raw[imh,ilogg,iteff,:]/np.nanmean(raw[imh,ilogg,iteff,:])) ax[iy, ix].plot(filled[imh, ilogg, iteff, :], color='g') ax[iy, ix].plot( raw[imh, ilogg, iteff, :] / np.nanmean(raw[imh, ilogg, iteff, :]) / filled[imh, ilogg, iteff, :] + 0.2) ax[iy, ix].set_ylim([0.7, 1.35]) if not np.isclose( holes.data[hcm, ham, hmh, hlogg, hteff], 0.) and not np.isclose( holes.data[hcm, ham, hmh, hlogg, hteff], -100.): color = 'r' else: color = 'k' ax[iy, ix].text(0.01, 0.97, lab, transform=ax[iy, ix].transAxes, va='top', fontsize='x-small', color=color) ax[iy, ix].text(0.01, 0.9, lab1, transform=ax[iy, ix].transAxes, va='top', fontsize='x-small', color=color) ii += 1 if ii % (nx * ny) == 0: fig.savefig( out + '{:02d}'.format(ii / (nx * ny)) + '.png') figs.append([ out + '{:02d}'.format(ii / (nx * ny)) + '.png' ]) plt.close() else: plt.clf() plt.plot(raw[imh, ilogg, iteff, :] / np.nanmean(raw[imh, ilogg, iteff, :]) - 0.2, color='b') plt.plot(filled[imh, ilogg, iteff, :], color='g') plt.plot(raw[imh, ilogg, iteff, :] / np.nanmean(raw[imh, ilogg, iteff, :]) / filled[imh, ilogg, iteff, :] + 0.2, color='r') plt.ylim([0.7, 1.3]) plt.draw() pdb.set_trace() html.htmltab(figs, file=out + '.html')
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
def modelmap(lsfframe, waveframe=None, cols=np.arange(5, 2000, 20), fibers=np.arange(1, 300, 3), apStar=False, smooth=5, hard=False): ''' Make LSF map from a model a[ps]LSF file ''' lsfmap = np.zeros([len(fibers), len(cols), 3]) w = np.zeros([len(fibers), len(cols), 3]) dw = np.zeros([len(fibers), len(cols), 3]) r = np.zeros([len(fibers), len(cols), 3]) for ichip, chip in enumerate(['a', 'b', 'c']): lsf = lsfframe[chip][1].data if waveframe is not None: wave = waveframe[chip][2].data nx = lsf.shape[0] x = np.arange(nx) col = 1000 fit = fitting.LevMarLSQFitter() g_init = models.Gaussian1D(mean=nx / 2, stddev=2.5 / 2.354, amplitude=0.3) for i, col in enumerate(cols): for j, fiber in enumerate(fibers): y = lsf[:, 300 - fiber, col] g = fit(g_init, x, y) if waveframe is not None: w[j, i, ichip] = wave[300 - fiber, col] dw[j, i, ichip] = wave[300 - fiber, col] - wave[300 - fiber, col - 1] print( 300 - fiber, col, g.stddev * 2.354, g.stddev * 2.354 / (6e-6 * wave[300 - fiber, col] * np.log(10.)) * np.abs(dw[j, i, ichip])) lsfmap[j, i, ichip] = g.stddev * 2.354 r[j, i, ichip] = w[j, i, ichip] / (g.stddev * 2.354 * np.abs(dw[j, i, ichip])) if apStar and waveframe is not None: lsfmap[j, i, ichip] = lsfmap[j, i, ichip] / ( 6e-6 * wave[300 - fiber, col] * np.log(10.)) * np.abs( dw[j, i, ichip]) if smooth > 0: lsfmap[:, :, ichip] = convolve(np.squeeze(lsfmap[:, :, ichip]), Box2DKernel(smooth), boundary='extend') r[:, :, ichip] = convolve(np.squeeze(r[:, :, ichip]), Box2DKernel(smooth), boundary='extend') plt.imshow(lsfmap[:, :, ichip], vmin=1.5, vmax=3.5, interpolation='nearest') plt.draw() plt.show() plt.close() for idata, data in enumerate([lsfmap, r]): if idata == 0: zr = [1.5, 3.5] suffix = '_fwhm' cmap = 'jet_r' else: zr = [18000, 26000] suffix = '_R' cmap = 'jet' fig, ax = plots.multi(3, 1, figsize=(12, 6), wspace=0.1) #im=ax.imshow(np.reshape(r,(len(fibers),3*len(cols)),order='F'),vmin=18000,vmax=25000,interpolation='nearest',cmap='jet_r') for ichip, chip in enumerate(['a', 'b', 'c']): im = ax[ichip].imshow(data[:, :, ichip], vmin=zr[0], vmax=zr[1], interpolation='nearest', cmap=cmap, extent=(cols[0], cols[-1], fibers[-1], fibers[0]), origin='upper', aspect=2048. / 300.) ax[ichip].set_xlabel('Wavelength') xpix = [] xlab = [] wlab = [ 15200, 15500, 15750, 15900, 16150, 16400, 16550, 16750, 16900 ] if waveframe is not None: wave = waveframe[chip][2].data for w in wlab: ipix = abs(wave[150, :] - w).argmin() print(w, ipix) if ipix > 0 and ipix < 2000: xpix.append(ipix) xlab.append(str(w)) ax[ichip].set_xticks(xpix) ax[ichip].set_xticklabels(xlab) if ichip == 0: ax[ichip].set_ylabel('Fiber') else: #ax.set_xticklabels([]) ax[ichip].set_yticklabels([]) cbar_ax = fig.add_axes([0.25, 0.05, 0.5, 0.03]) fig.colorbar(im, cax=cbar_ax, orientation='horizontal') plt.show() if hard: fig.savefig(lsfframe['filename'].strip('.fits') + suffix + '.png') fig.savefig(lsfframe['filename'].strip('.fits') + suffix + '.eps') plt.close() #pdb.set_trace() return lsfmap, r
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
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
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')
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
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 test(planfile,grid='GKg',npiece=12,npca=75,runraw=True,runpca=True,fit=True, fast=False, niso=None, sns=[1000], rot=False) : """ Routine to set up and run a series of tests for a PCA library Includes comparison of raw and PCA spectra for a sample suite, and several FERRE runs to recover parameters for the sample suite Requires existing test.ipf file with sample suite parameters Args : planfile (str) : name of input plan file that includes filename npiece (int) : number of PCA pieces (for directory/file names) npca (int) : number of PCA pieces (for directory/file names) runraw (bool) : create the sample and input FERRE spectra (default=True) runpca (bool) : create the PCA-derived spectra (default=True) fit (bool) : do the raw-PCA FERRE runs and comparison (default=True) fit (bool) : submit the FERRE test runs to queue (default=True) fast (bool) : use the sdss-fast queue (default=False) """ # Read planfile and set output file name if not os.path.isfile(planfile): print('{:s} does not exist'.format(planfile)) return p=yanny.yanny(planfile,np=True) outfile=os.path.basename(p['name']) # create test directory try : os.mkdir('test') except : pass # create test sample for this grid if runraw: sample.sample('test/test',gridclass=grid,niso=niso) # raw and PCA library file root names flib='f_aps'+outfile plib='p_aps'+outfile+('_{:03d}_{:03d}').format(npiece,npca) # make raw directory and setup to produce test spectra prefix=liblink(flib,'test/raw/') try: os.remove('test/raw/test.ipf') except: pass os.symlink(prefix+'test/test_'+grid+'.ipf','test/raw/test.ipf') if runraw : l=ferre.rdlibhead('f_aps'+outfile+'.hdr')[0] ferre.writenml('test/raw/input.nml','test',l,nov=0,ncpus=1,f_access=1) mkslurm.write('ferre.x',outdir='test/raw/',runplans=False,cwd=os.getcwd()+'/test/raw',fast=fast) print('running ferre in raw to create spectra') subprocess.call(['test/raw/ferre.x'],shell=False) # create uncertainty spectra true=np.loadtxt('test/raw/test.mdl') ferre.writespec('test/raw/test.err',true*0.+0.001) else : true=np.loadtxt('test/raw/test.mdl') # add noise for sn in sns : if sn < 1000 : name = 'test/raw/testsn{:d}'.format(sn) if not os.path.isfile(name+'.mdl') : dim=true.shape truen=true.flatten()+np.random.normal(0.,1./sn,true.flatten().shape) truen=np.reshape(truen,(dim[0],dim[1])) ferre.writespec(name+'.mdl',truen) ferre.writespec(name+'.err',truen*0.+1./sn) # list of all the different tests and their input.nml files; latter are set below # first fdir is just for creating PCA root='test/pca_{:d}_{:d}'.format(npiece,npca) fdirs = [root+'/',root+'/algor3/',root+'/algor1/',root+'/algor1_12/',root+'/algor3_12/', root+'/algor3_001/',root+'/algor3_01/',root+'/algor3_1', root+'/algor3_12_01/',root+'/algor3_12_1', root+'/algor3_indi1234567/',root+'/algor3_indi3142567/', root+'/algor3_4d' ] # general setup for all FERRE directories using PCA library l=ferre.rdlibhead('p_aps'+outfile+('_{:03d}_{:03d}.hdr').format(npiece,npca))[0] print("set up...") for fdir in fdirs : # create library links and return relative directory print(fdir) prefix=liblink(plib,fdir) # create input ipf cmds=[] for sn in sns : if sn < 1000 : name = fdir+'/testsn{:d}'.format(sn) else : name = fdir+'/test' # create input ipf try: os.remove(name+'.ipf') except: pass os.symlink(prefix+'test/test_'+grid+'.ipf',name+'.ipf') # create obs file with true spectra try: os.remove(name+'.obs') except: pass os.symlink(prefix+'test/raw/'+os.path.basename(name)+'.mdl',name+'.obs') # create uncertainty file try: os.remove(name+'.err') except: pass os.symlink(prefix+'test/raw/'+os.path.basename(name)+'.err',name+'.err') cmds.extend(['cp '+os.path.basename(name)+'.nml input.nml','ferre.x']) # create the command file to run FERRE mkslurm.write(cmds,name='ferre.x',outdir=fdir+'/',runplans=False,cwd=os.getcwd()+'/'+fdir,time='48:00:00',fast=fast) # test-specific input.nml files ferre.writenml(root+'/input.nml','test',l,nov=0,ncpus=1) for sn in sns : if sn < 1000 : name = 'testsn{:d}'.format(sn) else : name = 'test' ferre.writenml(root+'/algor3/'+name+'.nml',name,l,algor=3,renorm=4,obscont=1,ncpus=32,init=1) ferre.writenml(root+'/algor1_12/'+name+'.nml',name,l,algor=1,renorm=4,obscont=1,ncpus=32,indini=[1,1,1,1,2,2,3],init=1) ferre.writenml(root+'/algor1/'+name+'.nml',name,l,algor=1,renorm=4,obscont=1,ncpus=32,init=1) ferre.writenml(root+'/algor3_12/'+name+'.nml',name,l,algor=3,renorm=4,obscont=1,ncpus=32,indini=[1,1,1,1,2,2,3],init=1) ferre.writenml(root+'/algor3_001/'+name+'.nml',name,l,algor=3,renorm=4,obscont=1,ncpus=32,stopcr=0.001,init=1) ferre.writenml(root+'/algor3_01/'+name+'.nml',name,l,algor=3,renorm=4,obscont=1,ncpus=32,stopcr=0.01,init=1) ferre.writenml(root+'/algor3_12_01/'+name+'.nml',name,l,algor=3,renorm=4,obscont=1,ncpus=32,stopcr=0.01,indini=[1,1,1,1,2,2,3],init=1) ferre.writenml(root+'/algor3_1/'+name+'.nml',name,l,algor=3,renorm=4,obscont=1,ncpus=32,stopcr=0.1,init=1) ferre.writenml(root+'/algor3_12_1/'+name+'.nml',name,l,algor=3,renorm=4,obscont=1,ncpus=32,stopcr=0.1,indini=[1,1,1,1,2,2,3],init=1) ferre.writenml(root+'/algor3_indi1234567/'+name+'.nml',name,l,algor=3,renorm=4,obscont=1,ncpus=32,indi=[1,2,3,4,5,6,7],init=1) ferre.writenml(root+'/algor3_indi3142567/'+name+'.nml',name,l,algor=3,renorm=4,obscont=1,ncpus=32,indi=[3,1,4,2,5,6,7],init=1) ferre.writenml(root+'/algor3_4d/'+name+'.nml',name,l,algor=3,renorm=4,obscont=1,ncpus=32,indv=[4,5,6,7],init=1) # produce PCA version of test spectra if runpca : print('running ferre in {:d}_{_d} to create spectra'.format(npiece,npca)) subprocess.call([root+'/ferre.x'],shell=False) pca=np.loadtxt(root+'/test.mdl') # histogram of ratio of pca to true print("making pca/raw comparison histogram ...") fig,ax=plots.multi(1,2) hist,bins=np.histogram((pca/true).flatten(),bins=np.linspace(0.9,1.1,4001)) plots.plotl(ax[0],np.linspace(0.8005,1.2,4000),hist/hist.sum(),semilogy=True,xt='pca/true') ax[1].hist(np.abs((pca-true).flatten()),bins=np.logspace(-7,3,50),histtype='step',normed=True,cumulative=True,color='k') ax[1].set_xlim(0.,0.01) ax[1].set_ylim(0.,1.0) ax[1].set_xlabel('|pca-true|') ax[1].set_ylabel('Cumulative fraction') fig.tight_layout() fig.savefig(root+'/'+outfile+'_pca.png') plt.close() # use the PCA libraries to fit raw spectra print("running/making plots...") tab=[] yt=[] # first fdir is just for creating PCA for fdir in fdirs[1:] : print(fdir) if fit : subprocess.call(['sbatch','ferre.x'],shell=False,cwd=fdir) xtab=[] xt=[] tmp='' for sn in sns : if sn < 1000 : name = fdir+'/testsn{:d}'.format(sn) else : name = fdir+'/test' try : sample.comp(name,hard=True,true='test/raw/test.ipf',rot=rot) dt=subprocess.check_output("grep ellapsed "+fdir+"/ferre.x.out | tail -1 | awk '{print $3}'",shell=True) except : print('failed comp: ',name) dt='-1.' xtab.extend(['../'+name+'.png','../'+name+'_2.png']) xt.extend(['S/N = '+str(sn),'S/N = '+str(sn)]) tmp=tmp+dt+',' tab.append(xtab) yt.append('<a href=../'+fdir+'>'+fdir+'</a><br>FERRE time: '+tmp+' s') header = '<h3>'+outfile+'</h3>\n' header = header + '<br> Test sample: <br><img src=test_'+grid+'.png width=40%> <img src=test_'+grid+'_2.png width=40%>' header = header + '<br> Histogram of pixel ratios between raw and PCA for test sample:<br>' header = header + '<img src=../'+root+'/'+outfile+'_pca.png width=40%>\n' html.htmltab(tab,file='test/test.html',header=header,ytitle=yt,xtitle=xt)
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')
# list of file name suffixes and microturbulences #suffix='cn' #vplist=['03','06','12','24','48'] suffix = 'c' vplist = ['05', '10', '20', '40', '80'] y = [] ylab = [] # loop through different dw for t1 in ['01', '02', '03', '05', '10']: x = [] xlab = [] for t2 in ['01', '02', '03', '05', '10']: # loop through different dw for comparison fig, ax = plots.multi(1, 5, hspace=0.001) for iplt, vp in enumerate(vplist): a = fits.open('test' + t1 + suffix + '_lsfcombo5/new_ap00cp00np00vp' + vp + '.fits')[0] b = fits.open('test' + t2 + suffix + '_lsfcombo5/new_ap00cp00np00vp' + vp + '.fits')[0] # loop over 3 Teff, 3 logg, 3 [M/H] for i in range(3): for j in range(3): for k in range(3): if k == 0: color = 'r' elif k == 1: color = 'g' elif k == 2: color = 'b' plots.plotl(ax[iplt], wave_fits(a.header),
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