%pylab inline

import seaborn as sns
sns.set_context("notebook", font_scale=1.5)

import warnings
warnings.filterwarnings("ignore")

"""
Explanation: ApJdataFrames 001: Luhman2003
Title: A Census of the Young Cluster IC 348
Authors: Luhman K.L., Stauffer J.R., Muench A.A., Rieke G.H., Lada E.A., Bouvier J., Lada C.J. 
Data is from this paper:
http://iopscience.iop.org/0004-637X/593/2/1093/
End of explanation
"""


from astropy.io import ascii

#!curl -o ../data/Luhman2003/Luhman2003_Table2.txt http://iopscience.iop.org/0004-637X/593/2/1093/fulltext/datafile2.txt

data = ascii.read("http://iopscience.iop.org/0004-637X/593/2/1093/fulltext/datafile2.txt")

data[0:2]

print data.colnames

"""
Explanation: We're skipping Table 1 because it is an observing log.
Table 2- Members of IC348
Since this table is formatted in the CDS format, it is easiest to take advantage of the Astropy functionality, ascii.read, which automatically detects the header formatting.  We will also take advantage of the fact that ascii.read can accept a url as an argument, so we won't have to actually save the data locally.  Let's hope ApJ doesn't change their web link though.
End of explanation
"""


cmap = sns.palettes.cubehelix_palette(start=0.2, rot=0.7,as_cmap=True)
sns.palplot(sns.palettes.cubehelix_palette(start=0.2, rot=0.7))

plt.figure(figsize=(4.5, 9))

plt.scatter(data['I-Z'].data, data['H-K'].data + data['Kmag'].data, c=data['AJ'].data, cmap=cmap)

plt.xlim(0, 1.2)
plt.ylim(18.5, 9.5)
cbar = plt.colorbar()
cbar.set_label('$A_J$')
plt.xlabel("$I-Z$")
plt.ylabel("$H$")

"""
Explanation: I am using seaborn (imported as sns), to set nice-looking figure deafaults.
End of explanation
"""


import pandas as pd

col_names = ["ID","RA","Dec","SpectralType","Ref","ForegroundEvidence","R-I","I","I-Z","J-H","H-Ks","Ks"]
tbl3 = pd.read_csv('http://iopscience.iop.org/0004-637X/593/2/1093/fulltext/57692.tb3.txt', sep='\t', 
                   header=None, na_values='\ldots', names=col_names)

tbl3.head()

plt.figure(figsize=(4.5, 9))

plt.scatter(data['I-Z'].data, data['H-K'].data + data['Kmag'].data, c=data['AJ'].data, cmap=cmap, label='Members')
plt.plot(tbl3['I-Z'], tbl3['H-Ks']+tbl3['Ks'], 'rs', label='Foreground')

plt.xlim(0, 1.2)
plt.ylim(18.5, 9.5)
cbar = plt.colorbar()
cbar.set_label('$A_J$')
plt.xlabel("$I-Z$")
plt.ylabel("$H$")
plt.legend(loc='lower left')

"""
Explanation: Table 3 - Foreground members
This table is not formatted as CDS.  Instead it is just raw $\LaTeX$, with no header columns.  So I will have to assign the column names manually, which I copy and paste and parse by hand.  Then I use pandas (imported as pd) to read the tab-separated file (sep='\t').
End of explanation
"""


col_names = ["ID","RA","Dec","SpectralType","Ref","R-I","I","I-Z","J-H","H-Ks","Ks"]
tbl4 = pd.read_csv('http://iopscience.iop.org/0004-637X/593/2/1093/fulltext/57692.tb4.txt', sep='\t', 
                   header=None, na_values='\ldots', names=col_names)

tbl4.head()

plt.figure(figsize=(4.5, 9))

plt.scatter(data['I-Z'].data, data['H-K'].data + data['Kmag'].data, c=data['AJ'].data, cmap=cmap, label='Members')
plt.plot(tbl3['I-Z'], tbl3['H-Ks']+tbl3['Ks'], 'r.', label='Foreground')
plt.plot(tbl4['I-Z'], tbl4['H-Ks']+tbl3['Ks'], 'bs', label='Background')

plt.xlim(0, 1.2)
plt.ylim(18.5, 9.5)
cbar = plt.colorbar()
cbar.set_label('$A_J$')
plt.xlabel("$I-Z$")
plt.ylabel("$H$")
plt.legend(loc='lower left')

"""
Explanation: Table 4 - Background stars
End of explanation
"""


tbl8 = pd.read_csv("http://iopscience.iop.org/0004-637X/593/2/1093/fulltext/57692.tb8.txt", 
                   names = ["Spectral_Type", "Teff"],
                   sep = '\t')
tbl8

"""
Explanation: Table 8 - Adopted Temperature Scale
End of explanation
"""


import gully_custom
tbl8["SpT"] = gully_custom.specType(tbl8.Spectral_Type)

sns.lmplot("SpT", "Teff", tbl8)

"""
Explanation: Covert string spectral type to numerical type.
End of explanation
"""


tbl2 = data.to_pandas()

"""
Explanation: Save the data tables locally.
End of explanation
"""


for tbl, outname in zip([tbl2, tbl3, tbl4, tbl8], "tbl2, tbl3, tbl4, tbl8".split(", ")):
    tbl.to_csv("../data/Luhman2003/"+outname+".csv", sep='\t', index=False)

"""
Explanation: ! mkdir ../data/Luhman2003
End of explanation
"""


