%matplotlib inline
%config InlineBackend.figure_format = 'retina'
import numpy as np
import matplotlib.pyplot as plt

import pandas as pd

#! mkdir ../data/Jha2007

"""
Explanation: Jha et al. 2007
Title: Improved Distances to Type Ia Supernovae with Multicolor Light-Curve Shapes: MLCS2k2
Authors: Saurabh Jha, Adam G. Riess, and Robert P. Kirshner
ADS: http://adsabs.harvard.edu/abs/2007ApJ...659..122J
End of explanation
"""


#! wget -P ../data/Jha_2007ApJ...659..122J/ -e html_extension=Off http://iopscience.iop.org/article/10.1086/512054/fulltext/63969.tb1.txt

! head -n 4 ../data/Jha2007/63969.tb1.txt

names = ['SN_Ia','galactic_longitude','galactic_latitude','cz_km_s_sun','cz_km_s_LG',
         'cz_km_s_CMB','morphological_type','SN_offset_N_as','SN_offset_E_as','t_1_days',
         'filters','E_B_V','Refs']

df_tbl1 = pd.read_csv('../data/Jha2007/63969.tb1.txt', names=names, 
                      delim_whitespace=True, na_values='\ldots')

df_tbl1.tail()

import matplotlib.ticker as plticker

fig,ax=plt.subplots(figsize=(8,8))

#Spacing between each line
intervals = 1.0

loc = plticker.MultipleLocator(base=intervals)
ax.xaxis.set_major_locator(loc)
ax.yaxis.set_major_locator(loc)

# Add the grid
ax.grid(which='major', axis='both', linestyle='-')

ax.plot(df_tbl1.SN_offset_E_as/4.0, df_tbl1.SN_offset_N_as/4.0, '.')
ax.plot([0], [0], 'ro')
ax.set_xlim(-25, 25)
ax.set_ylim(-25, 25)
ax.xaxis.set_ticklabels([])
ax.yaxis.set_ticklabels([])
ax.set_title('Host galaxy type Ia supernovae distances')
ax.set_xlabel('Kepler pixels')
ax.set_ylabel('Kepler pixels');

rad_dist = np.sqrt(df_tbl1.SN_offset_E_as**2 + df_tbl1.SN_offset_N_as**2)

import seaborn as sns

sns.distplot(rad_dist/4.0)
plt.xlabel('$N$ Kepler pixels');

np.percentile(rad_dist/4.0, 80)

"""
Explanation: Table 1
Uncomment out the line below or download directly from the Paper's ApJ Website
End of explanation
"""


