#Import the normal stuff
import numpy as np
#add this for grid interpolation
import scipy.interpolate as interp
import matplotlib.pyplot as plt
from astropy.io import ascii
#add this to make plots easier
from astroML.plotting import setup_text_plots
setup_text_plots(usetex=True,fontsize=18)
#This example will run through plotting in color
#and plotting with errorbars
#First lets read in some APOGEE data which I have grabbed from Sloan
#These stars all have S/N ratios greater than 180 and are spectral standards
dat = ascii.read('APOGEE_parameters.csv',delimiter=',')
import glob
#you will have to tar xfvs the file attached to this example
#tar xfvs BaSTI_04062015_214230_9.tar.gz
#Now grab the file list using glob
files = glob.glob('*nor_sloan')
#Run through the list and read the files
for j,file in enumerate(files):
fil = ascii.read(file,header_start=-2)
t = open(file,'r')
m = t.readlines()
#Grab the metallicity from the file
m = m[4][20:27]
feh = float(m)
#[390:1190] only keeps the red giant branch which we are currently interested in
dumteff = np.array(fil['logTe'])[390:1190]
dumage = np.array(fil['log(age)'])[390:1190]
dumlum = np.array(fil['log(L/Lo)'])[390:1190]
#this is short for dumb mass because it is a dummy variable
dummass = np.array(fil['(M/Mo)'])[390:1190]
dumfeh = np.zeros(fil['logTe'][390:1190].size)+feh
if j == 0:
allteff = dumteff
allage = dumage
alllum = dumlum
allmass = dummass
allfeh = dumfeh
else:
#Stack everything into one array
allteff = np.vstack((allteff,dumteff))
allage = np.vstack((allage,dumage))
alllum = np.vstack((alllum,dumlum))
allmass = np.vstack((allmass,dummass))
allfeh = np.vstack((allfeh,dumfeh))
fig,ax = plt.subplots()
#Now let us make a plot show the fruits of all that stacking
ax.plot(allteff.T,alllum.T)
fig.gca().invert_xaxis()
ax.set_xlabel('log(Teff)')
ax.set_ylabel('log(L/L$_\odot$)')
#Now lets interpolate the age to a star given its [Fe/H],luminosity, and temperature
#.ravel() makes 1-d for stacking
#create an array of xpoints for which to do the interpolation
pteff = np.array(np.log10(dat['teff']))
#convert teff and log(g) to luminosity assuming solar mass which I already assumed when I made the grid
loggsun = 4.44 #cgs units
tempsun = 5777. #K
plum = np.array((loggsun-dat['logg'])+4.*np.log10(dat['teff']/tempsun))
pmet = np.array(dat['fe_h']*.9) #convert [Fe/H] to metallicity
xyread = np.vstack((pteff,plum,pmet))
closegrid = interp.griddata( (allteff.ravel(),allfeh.ravel(),alllum.ravel()), allage.ravel(), (pteff,pmet,plum),method='linear')
fig,ax = plt.subplots(figsize=(12,12))
#Now let us make a plot show the fruits of all that stacking and interpolating
#Use the same vmax and vmin (i.e. color data range) for both plots so the colors will be consistent in the plot
plot = ax.scatter(allteff.T,alllum.T,c=allage.T,cmap=plt.cm.jet,edgecolor='none',vmax=allage.max(),vmin=allage.min())
cbar = plt.colorbar(plot)
cbar.set_label('log(Age) (years)')
ax.scatter(pteff,plum,c=closegrid,cmap=plt.cm.jet,marker='s',vmax=allage.max(),vmin=allage.min())
fig.gca().invert_xaxis()
ax.set_xlabel('log(Teff)')
ax.set_ylabel('log(L/L$_\odot$)')
#Interpolation Works!!! (i.e. the colors match)