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 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 hrsample(indata,hrdata,maxbin=50,raw=True) : ''' selects stars covering HR diagram as best as possible from input sample ''' i1,i2 = match.match(indata['APOGEE_ID'],hrdata['APOGEE_ID']) gd=[] for teff in np.arange(3000,6000,500) : gdteff=apselect.select(hrdata[i2],badval=['STAR_BAD'],badtarg=['EMBEDDED','EXTENDED'],teff=[teff,teff+500],sn=[100,1000],raw=raw) for logg in np.arange(-0.5,5.5,1) : j=apselect.select(hrdata[i2[gdteff]],logg=[logg,logg+1],raw=True) gdlogg=gdteff[j] for mh in np.arange(-2.5,0.5,0.5) : j=apselect.select(hrdata[i2[gdlogg]],mh=[mh,mh+0.5],raw=True) j=gdlogg[j] js=np.argsort(hrdata[i2[j]]['SNR'])[::-1] x = j[js] if len(j) < maxbin else j[js[0:maxbin]] gd.extend(x) return i1[gd],i2[gd]
def allCal(files=['clust???/aspcapField-*.fits','cal???/aspcapField-*.fits'],nelem=15,out='allCal.fits',allfield=None) : ''' Concatenate aspcapField files, adding ELEM tags if not there ''' # concatenate the structures all=struct.concat(files,verbose=True,fixfield=True) # add elements tags if we don't have them try : test=all['FELEM'][0] except : n=len(all) form='{:<d}f4'.format(nelem) iform='{:<d}f4'.format(nelem) all=struct.add_cols(all,np.zeros(n,dtype=[('FELEM',form),('FELEM_ERR',form), ('ELEM',form),('ELEM_ERR',form),('ELEMFLAG',iform)])) # add in NINST information from allField file if allfield is not None: a=fits.open(allfield)[1].data i1,i2=match.match(np.core.defchararray.add(all['APOGEE_ID'],all['LOCATION_ID'].astype(str)), np.core.defchararray.add(a['APOGEE_ID'],a['LOCATION_ID'].astype(str))) n=len(all) all=struct.add_cols(all,np.zeros(n,dtype=[('NINST','3i4')])) all['NINST'][i1]=a['NINST'][i2] # write out the file if out is not None: print('writing',out) #struct.wrfits(all,out) hdulist=fits.HDUList() hdu=fits.BinTableHDU.from_columns(all) hdulist.append(hdu) filelist=glob.glob(files[0]) hdu=fits.open(filelist[0])[3] hdulist.append(hdu) hdu=fits.open(filelist[0])[3] hdulist.append(hdu) hdulist.writeto(out,overwrite=True)
def calsample(indata=None,file='clust.html',plot=True,clusters=True,apokasc='APOKASC_cat_v4.4.2', cal1m=True,coolstars=True,dir='cal',hrdata=None,optical='cal_stars_20190329.txt',ns=True,special=None,Ce=True,ebvmax=None,snmin=75,mkall=True) : ''' selects a calibration subsample from an input apField structure, including several calibration sub-classes: cluster, APOKASC stars, 1m calibration stars. Creates cluster web pages/plots if requested ''' if indata is None : indata=allField(files=['apo25m/*/a?Field-*.fits','lco25m/*/a?Field-*.fits','apo1m/calibration/a?Field-*.fits'],out=None,verbose=True) j=np.where((indata['COMMISS'] == 0) & (indata['SNR'] > 75) )[0] data=indata[j] try: os.mkdir(dir) except: pass all=[] if clusters : jc=[] clusts=apselect.clustdata() fstars=open(dir+'/allclust.txt','w') f=html.head(file=dir+'/'+file) f.write('<A HREF=allclust.txt> cluster stars list </a>') f.write('<TABLE BORDER=2>\n') f.write('<TR><TD>NAME<TD>RA<TD>DEC<TD>Radius<TD>RV<TD>Delta RV<TD>Position criterion<TD>RV criterion<TD>PM criterion<TD>Parallax criterion<TD> CMD') clust=apselect.clustdata() for ic in range(len(clust.name)) : print(clust[ic].name) j=apselect.clustmember(data,clust[ic].name,plot=plot,hard=dir) print(clust[ic].name,len(j)) # clusters to exclude here if (clust[ic].name not in ['OmegaCen','Pal1','Pal6','Pal5','Terzan12']) and (len(j) >= 5): jc.extend(j) f.write('<TR><TD><A HREF='+clust[ic].name+'.txt>'+clust[ic].name+'</A><TD>{:12.6f}<TD>{:12.6f}<TD>{:8.2f}<TD>{:8.2f}<TD>{:8.2f}\n'.format( clust[ic].ra,clust[ic].dec,clust[ic].rad,clust[ic].rv,clust[ic].drv)) f.write('<TD><A HREF='+clust[ic].name+'_pos.png><IMG SRC='+clust[ic].name+'_pos.png width=300></A>\n') f.write('<TD><A HREF='+clust[ic].name+'_rv.png><IMG SRC='+clust[ic].name+'_rv.png width=300></A>\n') f.write('<TD><A HREF='+clust[ic].name+'_pm.png><IMG SRC='+clust[ic].name+'_pm.png width=300></A>\n') f.write('<TD><A HREF='+clust[ic].name+'_parallax.png><IMG SRC='+clust[ic].name+'_parallax.png width=300></A>\n') f.write('<TD><A HREF='+clust[ic].name+'_cmd.png><IMG SRC='+clust[ic].name+'_cmd.png width=300></A>\n') np.savetxt(dir+'/'+clust[ic].name+'.txt',data[jc]['APOGEE_ID'],fmt='%s') for star in data[jc]['APOGEE_ID'] : fstars.write('{:s} {:s}\n'.format(star,clust[ic].name)) html.tail(f) fstars.close() print('Number of cluster stars: ',len(jc)) mklinks(data,jc,dir+'_clust') all.extend(jc) if apokasc is not None : jc=[] apokasc = fits.open(os.environ['APOGEE_DIR']+'/data/apokasc/'+apokasc+'.fits')[1].data i1,i2=match.match(data['APOGEE_ID'],apokasc['2MASS_ID']) rgb=np.where(apokasc['CONS_EVSTATES'][i2] == 'RGB')[0] print('Number of APOKASC RGB stars (every 4th): ',len(rgb[0:-1:3])) jc.extend(i1[rgb][0:-1:4]) rc=np.where(apokasc['CONS_EVSTATES'][i2] == 'RC')[0] print('Number of APOKASC RC stars (every 2nd): ',len(rc[0:-2:2])) jc.extend(i1[rc][0:-1:2]) rc=np.where(apokasc['CONS_EVSTATES'][i2] == '2CL')[0] print('Number of APOKASC 2CL stars: ',len(rc)) jc.extend(i1[rc]) rc=np.where(apokasc['CONS_EVSTATES'][i2] == 'RC/2CL')[0] print('Number of APOKASC RC/2CL stars: ',len(rc)) jc.extend(i1[rc]) logg = 'APOKASC2_LOGG' # older version used LOGG_SYSD_SCALING lowg=np.where((apokasc[logg][i2] < 2) & (apokasc[logg][i2] > 0.1))[0] print('Number of APOKASC low log g stars: ',len(lowg)) jc.extend(i1[lowg]) highg=np.where((apokasc[logg][i2] > 3.8) & (apokasc[logg][i2] < 5.5))[0] print('Number of APOKASC high log g stars: ',len(highg)) jc.extend(i1[highg]) highg=np.where((apokasc['LOGG_DW'][i2] > 3.8) & (apokasc['LOGG_DW'][i2] < 5.5))[0] print('Number of APOKASC high LOGG_DW stars: ',len(highg)) jc.extend(i1[highg]) lowz=np.where((apokasc['FE_H_ADOP_COR'][i2] < -1.) & (apokasc['FE_H_ADOP_COR'][i2] > -90.))[0] print('Number of APOKASC low [Fe/H] stars: ',len(lowz)) jc.extend(i1[lowz]) mklinks(data,jc,dir+'_apokasc') all.extend(jc) if coolstars : jc=[] j=np.where(data['FIELD'] == 'GALCEN')[0] print('Number of GALCEN stars: ',len(j)) jc.extend(j) stars = ascii.read(os.environ['APOGEE_DIR']+'/data/calib/coolstars.txt',names=['id'],format='fixed_width_no_header') i1,i2=match.match(data['APOGEE_ID'],stars['id']) print('Number of cool stars: ',len(i1)) jc.extend(i1) mklinks(data,jc,dir+'_galcen') all.extend(jc) if cal1m : j=np.where(data['FIELD'] == 'calibration')[0] print('Number of 1m calibration stars: ',len(j)) all.extend(j) j=np.where(data['FIELD'] == 'RCB')[0] print('Number of 1m RCB stars: ',len(j)) all.extend(j) mklinks(data,j,dir+'_cal1m') if optical is not None: #stars = ascii.read(os.environ['APOGEE_DIR']+'/data/calib/validation_stars_DR16.txt',names=['id'],format='fixed_width_no_header') stars = ascii.read(os.environ['APOGEE_DIR']+'/data/calib/'+optical,names=['id'],format='fixed_width_no_header') i1,i2=match.match(data['APOGEE_ID'],stars['id']) print('Number of optical validation stars: ',len(i1)) mklinks(data,i1,dir+'_optical') all.extend(i1) if Ce : stars = np.loadtxt(os.environ['APOGEE_DIR']+'/data/calib/Ce_test_stars.txt',dtype=str)[:,0] i1,i2=match.match(data['APOGEE_ID'],stars) print('Number of Ce test stars: ',len(i1)) mklinks(data,i1,dir+'_Ce') all.extend(i1) if ns : #north-south overlap jn=np.where((data['FIELD'] == 'N2243') | (data['FIELD'] == '000+08') | (data['FIELD'] == '300+75') | (data['FIELD'] == 'M12-N') )[0] js=np.where((data['FIELD'] == 'N2243-S') | (data['FIELD'] == '000+08-S') | (data['FIELD'] == '300+75-S') | (data['FIELD'] == 'M12-S') )[0] i1,i2=match.match(data['APOGEE_ID'][jn], data['APOGEE_ID'][js]) jc=list(jn[i1]) jc.extend(js[i2]) stars = ascii.read(os.environ['APOGEE_DIR']+'/data/calib/apogee_overlap.txt',names=['id'],format='fixed_width_no_header') jn=np.where(data['TELESCOPE'] == 'apo25m')[0] js=np.where(data['TELESCOPE'] == 'lco25m')[0] i1,i2=match.match(data['APOGEE_ID'][jn],stars['id']) jc.extend(jn[i1]) i1,i2=match.match(data['APOGEE_ID'][js],stars['id']) jc.extend(js[i2]) mklinks(data,jc,dir+'_ns') print('Number of N/S overlap stars: ',len(jc)) all.extend(jc) if special is not None: stars = ascii.read(os.environ['APOGEE_DIR']+'/data/calib/'+special,names=['id'],format='fixed_width_no_header') jn=np.where(data['TELESCOPE'] == 'apo25m')[0] js=np.where(data['TELESCOPE'] == 'lco25m')[0] i1,i2=match.match(data['APOGEE_ID'][jn],stars['id']) jc=list(jn[i1]) i1,i2=match.match(data['APOGEE_ID'][js],stars['id']) jc.extend(js[i2]) print('Number of '+special+' stars: ',len(i1)) mklinks(data,jc,dir+'_special') all.extend(jc) if ebvmax is not None: jc=np.where( (data['SFD_EBV']>0) & (data['SFD_EBV'] < ebvmax) )[0] print('Number of low E(B-V) stars: ',len(jc)) mklinks(data,jc,dir+'_ebv') all.extend(jc) if hrdata is not None: i1, i2 = hrsample(data,hrdata) print('Number of HR sample stars: ',len(i1)) mklinks(data,i1,dir+'_hr') all.extend(i1) # create "all" directories with all stars, removing duplicates print('Total number of stars: ',len(list(set(all)))) if mkall: mklinks(data,list(set(all)),dir+'_all') return indata
def add_gaia(data, gaia_1='gaia_2mass_xmatch.fits.gz', gaia_2='gaia_posn_xmatch.fits.gz'): """ Add GAIA data to allStar file, with coordinate match to (cross-matched) GAIA reference file """ tab = Table(data) in_names = ('source_id', 'parallax', 'parallax_error', 'pmra', 'pmra_error', 'pmdec', 'pmdec_error', 'phot_g_mean_mag', 'phot_bp_mean_mag', 'phot_rp_mean_mag', 'a_g_val', 'e_bp_min_rp_val', 'radial_velocity', 'radial_velocity_error', 'r_est', 'r_lo', 'r_hi') dtypes = ('i8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f4', 'f4', 'f4', 'f4', 'f4', 'f8', 'f8', 'f8', 'f8', 'f8') out_names = [] for name in in_names: out_names.append(('gaia_' + name).upper()) newcols = Table(np.zeros([len(tab), len(out_names)]) - 9999., names=out_names, dtype=dtypes) # for source_id, default to 0, not -9999. newcols['GAIA_SOURCE_ID'] = 0 # get rid of targetting proper motions to avoid confusion! #tab.remove_columns(['PMRA','PMDEC','PM_SRC']) # change to rename tab.rename_column('PMRA', 'TARG_PMRA') tab.rename_column('PMDEC', 'TARG_PMDEC') tab.rename_column('PM_SRC', 'TARG_PM_SRC') # add unpopulated columns tab.add_columns(newcols.columns.values()) # read gaia 2MASS matched file, match by 2MASS ID, and populate gaia = fits.open(gaia_1)[1].data print('number in GAIA-2MASS xmatch catalog: ', len(gaia), len(set(gaia['original_ext_source_id']))) while True: # loop for matches since we have repeats and want them all matched j = np.where(tab['GAIA_SOURCE_ID'] == 0)[0] print('Number missing gaia_source_id: ', len(j)) m1, m2 = match.match( np.core.defchararray.replace(tab['APOGEE_ID'][j], '2M', ''), gaia['original_ext_source_id']) print('Number matched by 2MASS: ', len(m1)) if len(m1) == 0: break for inname, outname in zip(in_names, out_names): tab[outname][j[m1]] = gaia[inname][m2] #if len(m1) < 100 : # for i in m1 : print(tab['APOGEE_ID'][j[m1]]) j = np.where(tab['GAIA_SOURCE_ID'] > 0)[0] print('number of unique APOGEE_ID matches: ', len(set(tab['APOGEE_ID'][j]))) j = np.where(tab['GAIA_SOURCE_ID'] == 0)[0] print('missing sources after 2MASS matches: ', len(j)) gaia = fits.open(gaia_2)[1].data h = htm.HTM() # now do a positional match, take the brightest object within 3 arcsec (which is the max from the GAIA crossmatch) maxrad = 3. / 3600. m1, m2, rad = h.match(tab['RA'][j], tab['DEC'][j], gaia['RA'], gaia['DEC'], maxrad, maxmatch=10) for m in set(m1): jj = np.where(m1 == m)[0] ii = np.argsort(gaia['phot_rp_mean_mag'][m2[jj]]) #print(tab['RA'][j[m]],tab['DEC'][j[m]],tab['TARGET_ID'][j[m]]) #print(gaia['RA'][m2[jj]],gaia['DEC'][m2[jj]],gaia['PHOT_RP_MEAN_MAG'][m2[jj]]) #print(ii) for inname, outname in zip(in_names, out_names): tab[outname][j[m]] = gaia[inname][m2[jj[ii[0]]]] j = np.where(tab['GAIA_SOURCE_ID'] == 0)[0] print('missing sources after second match: ', len(j)) # replace NaNs for name in out_names: bd = np.where(np.isnan(tab[name]))[0] tab[name][bd] = -9999. return tab
def read(name, libfile): """ Read all of the FERRE files associated with a FERRE run """ # get library headers and load wavelength array libhead0, libhead = rdlibhead(libfile) wave = [] for ichip in range(len(libhead)): wave.extend(libhead[ichip]['WAVE'][0] + np.arange(libhead[ichip]['NPIX']) * libhead[ichip]['WAVE'][1]) wave = 10.**np.array(wave) nwave = len(wave) nparam = libhead0['N_OF_DIM'] # input ipf and output spm files, match object names ipfobj, ipf = readferredata(name + '.ipf') spmobj, spm = readferredata(name + '.spm') i1, i2 = match.match(ipfobj, spmobj) nobj = len(ipfobj) param = spm[:, 0:nparam] paramerr = spm[:, nparam:2 * nparam] chi2 = 10.**spm[:, 2 * nparam + 2] covar = spm[:, 2 * nparam + 3:2 * nparam + 3 + nparam**2] covar = np.reshape(covar, (nobj, nparam, nparam)) # load param array params, tagnames, flagnames = aspcap.params() ntotparams = len(params) index = np.zeros(ntotparams, dtype=int) a = np.zeros(nobj, dtype=[('APOGEE_ID', 'S100'), ('FPARAM', 'f4', (ntotparams)), ('FPARAM_COV', 'f4', (ntotparams, ntotparams)), ('PARAM_CHI2', 'f4'), ('PARAMFLAG', 'i4', (ntotparams)), ('ASPCAPFLAG', 'i4')]) a['APOGEE_ID'] = ipfobj a['PARAM_CHI2'] = chi2 parammask = bitmask.ParamBitMask() aspcapmask = bitmask.AspcapBitMask() for i in range(nparam): # load FPARAM and FPARAM_COV into the right order of parameters pname = libhead0['LABEL'][i].decode() index = np.where(params == pname)[0][0] val = param[:, i] a['FPARAM'][:, index] = val for j in range(nparam): jindex = np.where(params == libhead0['LABEL'][j].decode())[0][0] a['FPARAM_COV'][:, index, jindex] = covar[:, i, j] # check for grid edge and flag warn = np.where( (val < libhead0['LLIMITS'][i] + libhead0['STEPS'][i] / 2.) | (val > libhead0['LLIMITS'][i] + libhead0['STEPS'][i] * (libhead0['N_P'][i] - 1 - 1. / 2)))[0] a['PARAMFLAG'][warn, index] |= parammask.getval('GRIDEDGE_WARN') a['ASPCAPFLAG'][warn] |= aspcapmask.getval(flagnames[index] + '_WARN') bad = np.where( (val < libhead0['LLIMITS'][i] + libhead0['STEPS'][i] / 8.))[0] if pname != 'N' and pname != 'LOG10VDOP' and pname != 'LGVSINI': a['PARAMFLAG'][bad, index] |= parammask.getval('GRIDEDGE_BAD') a['ASPCAPFLAG'][bad] |= aspcapmask.getval(flagnames[index] + '_BAD') bad = np.where((val > libhead0['LLIMITS'][i] + libhead0['STEPS'][i] * (libhead0['N_P'][i] - 1 - 1 / 8.)))[0] if pname != 'N': a['PARAMFLAG'][bad, index] |= parammask.getval('GRIDEDGE_BAD') a['ASPCAPFLAG'][bad] |= aspcapmask.getval(flagnames[index] + '_BAD') # put spectral data into a structured array form = '{:d}f4'.format(nwave) sform = '{:d}f4'.format(spm.shape[1]) out = np.empty(nobj, dtype=[('obj', 'S24'), ('spm', sform), ('obs', form), ('frd', form), ('err', form), ('mdl', form), ('chi2', form)]) out['obj'] = ipfobj out['spm'][i1, :] = spm[i2, :] out['obs'] = readspec(name + '.obs')[i2, :] out['frd'] = readspec(name + '.frd')[i2, :] out['err'] = readspec(name + '.err')[i2, :] out['mdl'] = readspec(name + '.mdl')[i2, :] out['chi2'] = (out['obs'] - out['mdl'])**2 / out['err']**2 return a, out, wave
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 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 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 irfm(allstar,trange=[4000,5000],mhrange=[-2.5,0.75],out='dteff') : ''' Compares allstar ASPCPAP Teff with various photometric Teff from JAJ compilation (SAGA, CL, TH, SFD) Does fits Args: allstar : allStar structure ''' # select stars gd=apselect.select(allstar,badval=['STAR_BAD'],teff=trange,mh=mhrange,raw=True) allstar=allstar[gd] # get IRFM data irfm=fits.open(os.environ['APOGEE_DIR']+'/data/calib/irfm_temp.fits')[1].data # get the subsamples and match. Note that we have to do this separately for each subsample because some # stars appear in more than one subsample saga=np.where(irfm['SOURCE'] == 'SAGA')[0] saga1,saga2=match.match(np.chararray.strip(allstar['APOGEE_ID']),np.chararray.strip(irfm['2MASS ID'][saga])) cl=np.where(irfm['SOURCE'] == 'CL')[0] cl1,cl2=match.match(np.chararray.strip(allstar['APOGEE_ID']),np.chararray.strip(irfm['2MASS ID'][cl])) th=np.where(irfm['SOURCE'] == 'TH')[0] th1,th2=match.match(np.chararray.strip(allstar['APOGEE_ID']),np.chararray.strip(irfm['2MASS ID'][th])) sfd=np.where(irfm['SOURCE'] == 'SFD')[0] sfd1,sfd2=match.match(np.chararray.strip(allstar['APOGEE_ID']),np.chararray.strip(irfm['2MASS ID'][sfd])) # plot diff color-coded by gravity as f([M/H]) fig,ax=plots.multi(2,2,hspace=0.001,wspace=0.001) xr=[-3.0,1.0] yr=[-400,300] zr=[3500,6000] bins=np.arange(-2.5,0.75,0.25) # SAGA plots.plotc(ax[0,0],allstar['FPARAM'][saga1,3],allstar['FPARAM'][saga1,0]-irfm['IRFM TEFF'][saga[saga2]],allstar['FPARAM'][saga1,0],zr=zr,xr=xr,yr=yr,xt='[M/H]',yt='ASPCAP-photometric Teff') mean=bindata(allstar['FPARAM'][saga1,3],allstar['FPARAM'][saga1,0]-irfm['IRFM TEFF'][saga[saga2]],bins) plots.plotp(ax[0,0],bins,mean,marker='o',size=40) ax[0,0].text(0.1,0.9,'SAGA',transform=ax[0,0].transAxes) # CL plots.plotc(ax[0,1],allstar['FPARAM'][cl1,3],allstar['FPARAM'][cl1,0]-irfm['IRFM TEFF'][cl[cl2]],allstar['FPARAM'][cl1,0],zr=zr,xr=xr,yr=yr,xt='[M/H]') mean=bindata(allstar['FPARAM'][cl1,3],(allstar['FPARAM'][cl1,0]-irfm['IRFM TEFF'][cl[cl2]]),bins) plots.plotp(ax[0,1],bins,mean,marker='o',size=40) ax[0,1].text(0.1,0.9,'CL',transform=ax[0,1].transAxes) # TH plots.plotc(ax[1,0],allstar['FPARAM'][th1,3],allstar['FPARAM'][th1,0]-irfm['IRFM TEFF'][th[th2]],allstar['FPARAM'][th1,0],zr=zr,xr=xr,yr=yr,xt='[M/H]',yt='ASPCAP-photometric Teff') mean=bindata(allstar['FPARAM'][th1,3],(allstar['FPARAM'][th1,0]-irfm['IRFM TEFF'][th[th2]]),bins) plots.plotp(ax[1,0],bins,mean,marker='o',size=40) ax[1,0].text(0.1,0.9,'TH',transform=ax[1,0].transAxes) # SFD plots.plotc(ax[1,1],allstar['FPARAM'][sfd1,3],allstar['FPARAM'][sfd1,0]-irfm['IRFM TEFF'][sfd[sfd2]],allstar['FPARAM'][sfd1,0],zr=zr,xr=xr,yr=yr,xt='[M/H]') mean=bindata(allstar['FPARAM'][sfd1,3],(allstar['FPARAM'][sfd1,0]-irfm['IRFM TEFF'][sfd[sfd2]]),bins) plots.plotp(ax[1,1],bins,mean,marker='o',size=40) ax[1,1].text(0.1,0.9,'SFD',transform=ax[1,1].transAxes) fig.savefig(out+'_mh.png') # plot diff color-coded by gravity as f([M/H]) fig,ax=plots.multi(2,2,hspace=0.001,wspace=0.001) zr=[-2.0,0.5] yr=[-400,300] xr=[6000,3500] bins=np.arange(3500,5500,250) # SAGA plots.plotc(ax[0,0],allstar['FPARAM'][saga1,0],allstar['FPARAM'][saga1,0]-irfm['IRFM TEFF'][saga[saga2]],allstar['FPARAM'][saga1,3],zr=zr,xr=xr,yr=yr,xt='Teff',yt='ASPCAP-photometric Teff') mean=bindata(allstar['FPARAM'][saga1,0],(allstar['FPARAM'][saga1,0]-irfm['IRFM TEFF'][saga[saga2]]),bins) plots.plotp(ax[0,0],bins,mean,marker='o',size=40) ax[0,0].text(0.1,0.9,'SAGA',transform=ax[0,0].transAxes) # CL plots.plotc(ax[0,1],allstar['FPARAM'][cl1,0],allstar['FPARAM'][cl1,0]-irfm['IRFM TEFF'][cl[cl2]],allstar['FPARAM'][cl1,3],zr=zr,xr=xr,yr=yr,xt='Teff') mean=bindata(allstar['FPARAM'][cl1,0],(allstar['FPARAM'][cl1,0]-irfm['IRFM TEFF'][cl[cl2]]),bins) plots.plotp(ax[0,1],bins,mean,marker='o',size=40) ax[0,1].text(0.1,0.9,'CL',transform=ax[0,1].transAxes) # TH plots.plotc(ax[1,0],allstar['FPARAM'][th1,0],allstar['FPARAM'][th1,0]-irfm['IRFM TEFF'][th[th2]],allstar['FPARAM'][th1,3],zr=zr,xr=xr,yr=yr,xt='Teff',yt='ASPCAP-photometric Teff') mean=bindata(allstar['FPARAM'][th1,0],(allstar['FPARAM'][th1,0]-irfm['IRFM TEFF'][th[th2]]),bins) plots.plotp(ax[1,0],bins,mean,marker='o',size=40) ax[1,0].text(0.1,0.9,'TH',transform=ax[1,0].transAxes) # SFD plots.plotc(ax[1,1],allstar['FPARAM'][sfd1,0],allstar['FPARAM'][sfd1,0]-irfm['IRFM TEFF'][sfd[sfd2]],allstar['FPARAM'][sfd1,3],zr=zr,xr=xr,yr=yr,xt='Teff') mean=bindata(allstar['FPARAM'][sfd1,0],(allstar['FPARAM'][sfd1,0]-irfm['IRFM TEFF'][sfd[sfd2]]),bins) plots.plotp(ax[1,1],bins,mean,marker='o',size=40) ax[1,1].text(0.1,0.9,'SFD',transform=ax[1,1].transAxes) fig.savefig(out+'_teff.png') # do 2D fits with Teff and [M/H], and 1D fits with each fig,ax=plots.multi(2,2,hspace=0.5,wspace=0.001) ax[0,1].xaxis.set_visible(False) ax[0,1].yaxis.set_visible(False) pfit = fit.fit2d(ax[0,0],allstar['FPARAM'][sfd1,3],allstar['FPARAM'][sfd1,0],allstar['FPARAM'][sfd1,0]-irfm['IRFM TEFF'][sfd[sfd2]],plot=True,zr=[-500,200],xt='[M/H]',yt=['Teff'],zt='$\Delta Teff$') pfit = fit.fit1d(ax[1,0],allstar['FPARAM'][sfd1,3],allstar['FPARAM'][sfd1,0]-irfm['IRFM TEFF'][sfd[sfd2]],ydata=allstar['FPARAM'][sfd1,0],plot=True,zr=[-500,200],xt='[M/H]',yt='$\Delta Teff$',xr=[-2.7,0.9],yr=[3500,5000]) pfit = fit.fit1d(ax[1,1],allstar['FPARAM'][sfd1,0],allstar['FPARAM'][sfd1,0]-irfm['IRFM TEFF'][sfd[sfd2]],ydata=allstar['FPARAM'][sfd1,3],plot=True,zr=[-500,200],xt='Teff',xr=[3900,5100],yr=[-2.5,0.5]) pdb.set_trace() return pfit
def plotparamdiffs(data, bdata, title=None, cal=False, out=None, elem=True): """ Plot parameter differences between two different runs """ a = data[1].data b = bdata[1].data paramnames, tagnames, flagnames = aspcap.params() i1, i2 = match.match(a['APOGEE_ID'], b['APOGEE_ID']) print('number of matching stars: ', len(i1)) # parameters first if cal: param = 'PARAM' else: param = 'FPARAM' grid = [] yt = [] for i in range(8): if i == 0: yr = [-200, 200] elif i == 1: xr = [-0.5, 5] yr = [-0.5, 0.5] else: xr = [-2.5, 1.0] yr = [-0.5, 0.5] row = [] yt.append(paramnames[i]) for j in range(3): fig, ax = plots.multi(1, 2, hspace=0.001) if j == 0: plots.plotc(ax[0], a['FPARAM'][i1, 0], b['FPARAM'][i2, i] - a['FPARAM'][i1, i], a['FPARAM'][i1, 3], yt=r'$\Delta$' + tagnames[i], xt='Teff', yr=yr, xr=[3000, 8000], zr=[-2, 0.5]) plots.plotc(ax[1], a['PARAM'][i1, 0], b['PARAM'][i2, i] - a['PARAM'][i1, i], a['PARAM'][i1, 3], yt=r'$\Delta$' + tagnames[i], xt='Teff', yr=yr, xr=[3000, 8000], zr=[-2, 0.5]) elif j == 1: plots.plotc(ax[0], a['FPARAM'][i1, 1], b['FPARAM'][i2, i] - a['FPARAM'][i1, i], a['FPARAM'][i1, 3], yt=r'$\Delta$' + tagnames[i], xt='log g', yr=yr, xr=[-1, 6], zr=[-2, 0.5]) plots.plotc(ax[1], a['PARAM'][i1, 1], b['PARAM'][i2, i] - a['PARAM'][i1, i], a['PARAM'][i1, 3], yt=r'$\Delta$' + tagnames[i], xt='log g', yr=yr, xr=[-1, 6], zr=[-2, 0.5]) elif j == 2: plots.plotc(ax[0], a['FPARAM'][i1, 3], b['FPARAM'][i2, i] - a['FPARAM'][i1, i], a['FPARAM'][i1, 3], yt=r'$\Delta$' + tagnames[i], xt='[M/H]', yr=yr, xr=[-2.5, 1.0], zr=[-2, 0.5]) plots.plotc(ax[1], a['PARAM'][i1, 3], b['PARAM'][i2, i] - a['PARAM'][i1, i], a['PARAM'][i1, 3], yt=r'$\Delta$' + tagnames[i], xt='[M/H]', yr=yr, xr=[-2.5, 1.0], zr=[-2, 0.5]) ax[0].text(0.1, 0.9, 'Uncalibrated', transform=ax[0].transAxes) ax[1].text(0.1, 0.9, 'Calibrated', transform=ax[1].transAxes) if out is not None: outfile = out + 'paramdiffs_{:1d}_{:1d}.png'.format(i, j) fig.savefig(outfile) row.append(os.path.basename(outfile)) else: pdb.set_trace() plt.close(fig) grid.append(row) ptab = html.table(grid, ytitle=yt) html.htmltab(grid, file=out + 'paramdiffs.html', ytitle=yt) # now elements if elem: grid = [] yt = [] elemnames = data[3].data['ELEM_SYMBOL'][0] belemnames = bdata[3].data['ELEM_SYMBOL'][0] if cal: elem = 'ELEM' else: elem = 'FELEM' for i, el in enumerate(elemnames): ii = np.where(belemnames == el)[0] if len(ii) > 0: yr = [-0.5, 0.5] row = [] yt.append(el) if len(a[elem].shape) == 3: abun = a[elem][i1, 0, i] else: abun = a[elem][i1, i] if len(b[elem].shape) == 3: abun_b = b[elem][i2, 0, ii] else: abun_b = b[elem][i2, i] for j in range(3): fig, ax = plots.multi(1, 2) if j == 0: plots.plotc(ax[0], a['FPARAM'][i1, 0], abun_b - abun, a['FPARAM'][i1, 3], yt=r'$\Delta$' + el, xt='Teff', yr=yr, xr=[3000, 8000], zr=[-2, 0.5]) plots.plotc(ax[1], a['PARAM'][i1, 0], abun_b - abun, a['PARAM'][i1, 3], yt=r'$\Delta$' + el, xt='Teff', yr=yr, xr=[3000, 8000], zr=[-2, 0.5]) elif j == 1: plots.plotc(ax[0], a['FPARAM'][i1, 1], abun_b - abun, a['FPARAM'][i1, 3], yt=r'$\Delta$' + el, xt='log g', yr=yr, xr=[-1, 6], zr=[-2, 0.5]) plots.plotc(ax[1], a['PARAM'][i1, 1], abun_b - abun, a['PARAM'][i1, 3], yt=r'$\Delta$' + el, xt='log g', yr=yr, xr=[-1, 6], zr=[-2, 0.5]) elif j == 2: plots.plotc(ax[0], a['FPARAM'][i1, 3], abun_b - abun, a['FPARAM'][i1, 3], yt=r'$\Delta$' + el, xt='[M/H]', yr=yr, xr=[-2.5, 1.0], zr=[-2, 0.5]) plots.plotc(ax[1], a['PARAM'][i1, 3], abun_b - abun, a['PARAM'][i1, 3], yt=r'$\Delta$' + el, xt='[M/H]', yr=yr, xr=[-2.5, 1.0], zr=[-2, 0.5]) ax[0].text(0.1, 0.9, 'Uncalibrated', transform=ax[0].transAxes) ax[1].text(0.1, 0.9, 'Calibrated', transform=ax[1].transAxes) if out is not None: outfile = out + el + '_diff_{:1d}.png'.format(j) fig.savefig(outfile) row.append(os.path.basename(outfile)) else: pdb.set_trace() plt.close(fig) grid.append(row) etab = html.table(grid, ytitle=yt) html.htmltab(grid, file=out + 'elemdiffs.html', ytitle=yt) # HR diagrams grid = [] row = [] aspcap.hr(a[i1], hard=out + 'hr_match1.png', grid=True, size=1) row.append(os.path.basename(out + 'hr_match1.png')) aspcap.hr(b[i2], hard=out + 'hr_match2.png', grid=True, size=1) row.append(os.path.basename(out + 'hr_match2.png')) grid.append(row) row = [] aspcap.hr(a[i1], hard=out + 'hrcal_match1.png', param='PARAM', grid=True, size=1) row.append(os.path.basename(out + 'hrcal_match1.png')) aspcap.hr(b[i2], hard=out + 'hrcal_match2.png', param='PARAM', grid=True, size=1) row.append(os.path.basename(out + 'hrcal_match2.png')) grid.append(row) hrtab = html.table(grid) fp = html.head(out + 'diffs.html') fp.write('<h2> HR diagrams, raw(top), calibrated(bottom)') fp.write(hrtab) fp.write('<h2> Parameter differences as f(Teff, logg, [M/H]') fp.write(ptab) fp.write('<h2> Abundance differences as f(Teff, logg, [M/H]') if elem: fp.write(etab) html.tail(fp) return
def comp(file, true=None, truespec=None, hard=False, plot=False, minchi2=0., testid=None, rot=False): """ Compare input parameters with output results """ if true is None: true = file + '.ipf' if rot: names = [ 'id', 'vmicro', 'cm', 'nm', 'am', 'vrot', 'mh', 'logg', 'teff' ] names_spm = [ 'id', 'vmicro', 'cm', 'nm', 'am', 'vrot', 'mh', 'logg', 'teff', 'evmicro', 'ecm', 'enm', 'eam', 'evrot', 'emh', 'elogg', 'eteff', 'a', 'b', 'chi2' ] else: names = ['id', 'vmicro', 'cm', 'nm', 'am', 'mh', 'logg', 'teff'] names_spm = [ 'id', 'vmicro', 'cm', 'nm', 'am', 'mh', 'logg', 'teff', 'evmicro', 'ecm', 'enm', 'eam', 'emh', 'elogg', 'eteff', 'a', 'b', 'chi2' ] true = ascii.read(true, names=names) ##spec=np.loadtxt('test.dat') obs = ascii.read(file + '.spm') for i in range(len(names_spm)): obs.rename_column('col{:d}'.format(i + 1), names_spm[i]) i1, i2 = match.match(true['id'], obs['id']) # write out file with differences f = open(file + '.out', 'w') for i in range(len(i1)): f.write(('{:<15s}' + '{:7.2f}' * 14 + '\n').format( true[i1[i]]['id'], true[i1[i]]['teff'], true[i1[i]]['logg'], true[i1[i]]['mh'], true[i1[i]]['am'], true[i1[i]]['cm'], true[i1[i]]['nm'], true[i1[i]]['vmicro'], obs[i2[i]]['teff'] - true[i1[i]]['teff'], obs[i2[i]]['logg'] - true[i1[i]]['logg'], obs[i2[i]]['mh'] - true[i1[i]]['mh'], obs[i2[i]]['am'] - true[i1[i]]['am'], obs[i2[i]]['cm'] - true[i1[i]]['cm'], obs[i2[i]]['nm'] - true[i1[i]]['nm'], obs[i2[i]]['vmicro'] - true[i1[i]]['vmicro'])) f.close() # histogram of differences, in linear and log histograms fig, ax = plots.multi(8, 2, wspace=0.001, hspace=0.001, figsize=(12, 4), xtickrot=60) for iy, log in enumerate([False, True]): ax[iy, 0].hist(dclip(obs[i2]['teff'] - true[i1]['teff'], lim=[-200, 200]), bins=np.arange(-200, 201, 10), histtype='step', log=log) ax[iy, 0].set_xlabel('$\Delta$Teff') ax[iy, 1].hist(dclip(obs[i2]['logg'] - true[i1]['logg']), bins=np.arange(-0.5, 0.51, 0.01), histtype='step', log=log) ax[iy, 1].set_xlabel('$\Delta$logg') ax[iy, 2].hist(dclip(obs[i2]['mh'] - true[i1]['mh']), bins=np.arange(-0.5, 0.51, 0.01), histtype='step', log=log) ax[iy, 2].set_xlabel('$\Delta$[M/H]') ax[iy, 3].hist(dclip(obs[i2]['am'] - true[i1]['am']), bins=np.arange(-0.5, 0.51, 0.01), histtype='step', log=log) ax[iy, 3].set_xlabel('$\Delta$[alpha/M]') ax[iy, 4].hist(dclip(obs[i2]['cm'] - true[i1]['cm']), bins=np.arange(-0.5, 0.51, 0.01), histtype='step', log=log) ax[iy, 4].set_xlabel('$\Delta$[C/M]') ax[iy, 5].hist(dclip(obs[i2]['nm'] - true[i1]['nm']), bins=np.arange(-0.5, 0.51, 0.01), histtype='step', log=log) ax[iy, 5].set_xlabel('$\Delta$[N/M]') ax[iy, 6].hist(dclip(obs[i2]['vmicro'] - true[i1]['vmicro']), bins=np.arange(-0.5, 0.51, 0.01), histtype='step', log=log) ax[iy, 6].set_xlabel('$\Delta$vmicro') ax[iy, 7].hist(dclip(10.**obs['chi2'], lim=[0, 50]), bins=np.arange(0, 51, 0.1), log=log) ax[iy, 7].set_xlabel('chi2') fig.suptitle(file) if hard: fig.savefig(file + '.png') plt.close() # plots of differences vs Teff, color-coded by various quantities fig, ax = plots.multi(6, 7, hspace=0.001, wspace=0.001, figsize=(16, 8), xtickrot=60) x = true['teff'][i1] + np.random.uniform(-45., 45., size=len(i1)) for ix in range(6): yt = '' if ix == 0: z = true['logg'][i1] tit = 'color: logg' zr = [0, 5] elif ix == 1: z = true['mh'][i1] tit = 'color: [M/H]' zr = [-1.5, 0.5] elif ix == 2: z = true['mh'][i1] + true['am'][i1] tit = 'color: [alpha/H]' zr = [-1.5, 1.5] elif ix == 3: z = true['mh'][i1] + true['cm'][i1] tit = 'color: [C/H]' zr = [-1.5, 1.5] elif ix == 4: z = true['mh'][i1] + true['nm'][i1] tit = 'color: [N/H]' zr = [-1.5, 1.5] elif ix == 5: z = obs['chi2'][i2] tit = 'color: chi2' zr = [0, 10.] ax[0, ix].set_title(tit) if ix == 0: plots.plotc(ax[0, ix], x, obs['teff'][i2] - true['teff'][i1], z, xt='Teff', yt=r'$\Delta$Teff', yr=[-1000, 1000], zr=zr) plots.plotc(ax[1, ix], x, obs['logg'][i2] - true['logg'][i1], z, xt='Teff', yt=r'$\Delta$logg', yr=[-2.0, 2.0], zr=zr) plots.plotc(ax[2, ix], x, obs['mh'][i2] - true['mh'][i1], z, xt='Teff', yt=r'$\Delta$[M/H]', yr=[-0.5, 0.5], zr=zr) plots.plotc(ax[3, ix], x, obs['am'][i2] - true['am'][i1], z, xt='Teff', yt=r'$\Delta$[a/M]', yr=[-0.5, 0.5], zr=zr) plots.plotc(ax[4, ix], x, obs['cm'][i2] - true['cm'][i1], z, xt='Teff', yt=r'$\Delta$[C/M]', yr=[-1.5, 1.5], zr=zr) plots.plotc(ax[5, ix], x, obs['nm'][i2] - true['nm'][i1], z, xt='Teff', yt=r'$\Delta$[N/M]', yr=[-1.5, 1.5], zr=zr) plots.plotc(ax[6, ix], x, 10.**obs['vmicro'][i2] - 10.**true['vmicro'][i1], z, xt='Teff', yt=r'$\Delta$vmicro', yr=[-1.0, 1.0], zr=zr) else: plots.plotc(ax[0, ix], x, obs['teff'][i2] - true['teff'][i1], z, xt='Teff', yr=[-1000, 1000], zr=zr) plots.plotc(ax[1, ix], x, obs['logg'][i2] - true['logg'][i1], z, xt='Teff', yr=[-2, 2], zr=zr) plots.plotc(ax[2, ix], x, obs['mh'][i2] - true['mh'][i1], z, xt='Teff', yr=[-0.5, 0.5], zr=zr) plots.plotc(ax[3, ix], x, obs['am'][i2] - true['am'][i1], z, xt='Teff', yr=[-0.5, 0.5], zr=zr) plots.plotc(ax[4, ix], x, obs['cm'][i2] - true['cm'][i1], z, xt='Teff', yr=[-1.5, 1.5], zr=zr) plots.plotc(ax[5, ix], x, obs['nm'][i2] - true['nm'][i1], z, xt='Teff', yr=[-1.5, 1.5], zr=zr) plots.plotc(ax[6, ix], x, 10.**obs['vmicro'][i2] - 10.**true['vmicro'][i1], z, xt='Teff', yr=[-1.0, 1.0], zr=zr) fig.suptitle(file) plt.show() if hard: fig.savefig(file + '_2.png') plt.close() plt.show() if plot: pdb.set_trace() obsspec = np.loadtxt(file + '.mdl') if truespec is None: truespec = file + '.frd' truespec = np.loadtxt(truespec) if testid is None: testid = range(1, len(i1) + 1) for tid in testid: i = np.where( np.core.defchararray.find(obs['id'][i2], 'test' + str(tid)) >= 0)[0][0] if 10.**obs['chi2'][i2[i]] > minchi2: plt.clf() plt.plot(truespec[i1[i], :], color='b') plt.plot(obsspec[i2[i], :], color='r') plt.plot(obsspec[i2[i], :] / truespec[i1[i], :] + 0.1, color='g') plt.draw() print(true['id'][i1[i]]) print( '{:8.1f}{:7.2f}{:7.2f}{:7.2f}{:7.2f}{:7.2f}{:7.2f}'.format( true['teff'][i1[i]], true['logg'][i1[i]], true['mh'][i1[i]], true['am'][i1[i]], true['cm'][i1[i]], true['nm'][i1[i]], true['vmicro'][i1[i]])) print( '{:8.1f}{:7.2f}{:7.2f}{:7.2f}{:7.2f}{:7.2f}{:7.2f}{:10.2f}' .format(obs['teff'][i2[i]], obs['logg'][i2[i]], obs['mh'][i2[i]], obs['am'][i2[i]], obs['cm'][i2[i]], obs['nm'][i2[i]], obs['vmicro'][i2[i]], obs['chi2'][i2[i]])) pdb.set_trace()
def merge(planfile, fields=None, outfile=None, clobber=True): ''' Match Cannon results to existing allStar file to make new table ''' p = yanny.yanny(planfile, np=True) apred = p['apred_vers'].strip("'") apstar = p['apstar_vers'].strip("'") aspcap_vers = p['aspcap_vers'].strip("'") results = p['results_vers'].strip("'") apl = apload.ApLoad(apred=apred, apstar=apstar, aspcap=aspcap_vers, results=results) a = apl.allStar()[1].data t = Table(a) out = Table() out['APOGEE_ID'] = t['APOGEE_ID'] length = len(out) if fields is None: fields = glob('*/cannonField*.fits') else: fields = glob(fields) c = fits.open(fields[0])[1].data for i, name in enumerate(c.names): print(name) if name != 'APOGEE_ID' and name != 'model_flux' and name != 'fvec' and name != 'flux' and name != 'ivar': out.add_column(Column(name=name, dtype=c.dtype[i], length=length)) print(name, type(out[name][0])) if type(out[name][0]) is np.string_: print('str!') out[name] = '' else: out[name] = -9999. # add X_M tag #out.add_column(Column(name='X_M',dtype='{:d}f4'.format(len(a['X_M'])),length=length)) for field in fields: print('field', field) c = fits.open(field)[1].data j1 = np.where(a['FIELD'] == c['FIELD'][0])[0] i1, i2 = match.match(a['APOGEE_ID'][j1], c['APOGEE_ID']) bd = np.where(c['chi_sq'][i2] <= 0.)[0] print(len(bd)) for name in out.columns: out[name][j1[i1]] = c[name][i2] if type(c[name][0]) is np.string_: bad = '' else: bad = -9999. if name is not 'APOGEE_ID': out[name][j1[i1[bd]]] = bad # for DR14, "fix" NA_H bd = np.where(out['NA_H'] < -1)[0] out['NA_H'][bd] = -9999. out['CANNON_ID'] = t['ASPCAP_ID'] if outfile is None: outfile = 'allStarCannon-' + results + '.fits' prihdr = fits.Header() prihdr['HISTORY'] = 'IDLWRAP_VERSION: ' + subprocess.check_output( 'idlwrap_version').strip('\n') prihdu = fits.PrimaryHDU(header=prihdr) hdu = fits.BinTableHDU.from_columns(np.array(out)) hdulist = fits.HDUList([prihdu, hdu]) hdulist.writeto(outfile, overwrite=clobber) ##out.write(outfile,overwrite=clobber) return out