In [1]:
#This is a new notebook for use with real data. We totally won't try to divide by zero this time.

In [2]:
import batman
import numpy as np
import matplotlib.pyplot as plt
% matplotlib inline

In [3]:
def chisqa (data, model):
    chi = 0
    
    for i in range(len(data)):
        num = (data[i]-model[i])**2
        denom = model[i]
        summ = num/denom
        chi = chi + summ
    chisum = np.sum(chi)
    return chisum

In [4]:
import astropy.io.ascii
table = astropy.io.ascii.read('kepler1b.txt')
time = table['time']
flux = table['flux']
nflux = flux/np.median(flux)
uncertainty = table['uncertainty']
plt.plot(time, nflux)


Out[4]:
[<matplotlib.lines.Line2D at 0x7f39230d8210>]

In [5]:
params = batman.TransitParams()
params.t0 = 0.0                      #time of inferior conjunction
params.per = 2.47061317              #orbital period
params.rp = 0.1281                   #planet radius (in units of stellar radii)
params.a = 7.903                     #semi-major axis (in units of stellar radii)
params.inc = 83.872                  #orbital inclination (in degrees)
params.ecc = 0.0                     #eccentricity
params.w = 0.0                       #longitude of periastron (in degrees)
params.u = [0.1, 0.3]                #limb darkening coefficients
params.limb_dark = "quadratic"       #limb darkening model


plotting = False


t00 = np.arange(-.8, -0.7, 0.001)
chiSq = np.array([])
for i in t00:

    # set the model t0 to one value of t00
    params.t0 = i
    m = batman.TransitModel(params, time)
    modelflux = m.light_curve(params)
    chiSq = np.append(chiSq, chisqa(nflux, modelflux))

    if plotting:
        plt.plot(time, modelflux, color='orange', linewidth=3)
        plt.scatter(time, nflux)
        plt.title("t0 = {}, chi^2 = {}".format(i, chiSq[-1]))
        plt.show()
#plt.scatter(t00, chiSq)
mint00 = t00[np.argmin(chiSq)]
print chiSq
#plt.title("Mininum t00 = {}".format(mint00))


[ 0.06666417  0.06415853  0.06162956  0.05908018  0.05651547  0.05393816
  0.0513529   0.04876179  0.04616929  0.0435791   0.04099612  0.03842453
  0.03587083  0.03334145  0.03084372  0.02838619  0.02597661  0.02362419
  0.02133843  0.01912973  0.01700802  0.01498519  0.01307203  0.01128025
  0.00962059  0.00810445  0.00674209  0.00554467  0.0045219   0.00368379
  0.00303916  0.00259466  0.00235476  0.00232275  0.00249899  0.00288115
  0.00346481  0.00424318  0.00520875  0.00635308  0.00766699  0.00913932
  0.01075909  0.01251529  0.01439721  0.01639272  0.01848994  0.02067621
  0.02294065  0.02527313  0.02766356  0.03010254  0.03258112  0.03509167
  0.03762615  0.04017887  0.0427431   0.0453151   0.04788933  0.050463
  0.05303135  0.05559181  0.05814087  0.06067549  0.06319163  0.06568592
  0.0681543   0.07059209  0.07299567  0.07535964  0.07768126  0.07995166
  0.08216702  0.08432193  0.0864121   0.08843143  0.09037549  0.09223875
  0.09401656  0.09570363  0.09729534  0.09878769  0.10017592  0.10145793
  0.10263051  0.103693    0.10464355  0.1054827   0.10621101  0.10683089
  0.10734415  0.10775497  0.10806898  0.10829186  0.10842908  0.10848763
  0.10847476  0.10839785  0.10826442  0.10808252  0.1078594 ]

In [6]:
rp0 = np.arange(0.1, 1.8, 0.01)
chiSq2 = np.array([])

for j in rp0:
    params.rp = j
    m = batman.TransitModel(params, time)
    modelflux2 = m.light_curve(params)
    chiSq2 = np.append(chiSq2, chisqa(nflux, modelflux2))
print chiSq2


[  7.21114782e-02   8.21374405e-02   9.50341157e-02   1.11199709e-01
   1.30992180e-01   1.54692635e-01   1.82332359e-01   2.14015802e-01
   2.50386369e-01   2.91973640e-01   3.39303841e-01   3.92921559e-01
   4.53395408e-01   5.21321616e-01   5.97325161e-01   6.82061352e-01
   7.76217214e-01   8.80511885e-01   9.95697666e-01   1.12256156e+00
   1.26192578e+00   1.41464840e+00   1.58162461e+00   1.76378790e+00
   1.96211057e+00   2.17760512e+00   2.41132560e+00   2.66436873e+00
   2.93787427e+00   3.23302769e+00   3.55106038e+00   3.89325085e+00
   4.26092637e+00   4.65546517e+00   5.07829677e+00   5.53090435e+00
   6.01482551e+00   6.53165500e+00   7.08304569e+00   7.67071095e+00
   8.29642641e+00   8.96203128e+00   9.66943069e+00   1.04205976e+01
   1.12175769e+01   1.20624853e+01   1.29575159e+01   1.39049400e+01
   1.49071095e+01   1.59664599e+01   1.70855140e+01   1.82668843e+01
   1.95132765e+01   2.08274939e+01   2.22124396e+01   2.36711208e+01
   2.52066539e+01   2.68222677e+01   2.85213076e+01   3.03072402e+01
   3.21836596e+01   3.41542915e+01   3.62229990e+01   3.83937891e+01
   4.06708178e+01   4.30583967e+01   4.55609989e+01   4.81832683e+01
   5.09300263e+01   5.38062797e+01   5.68172289e+01   5.99682775e+01
   6.32650408e+01   6.67133562e+01   7.03192953e+01   7.40891741e+01
   7.80295405e+01   8.21473089e+01   8.64495314e+01   9.09436534e+01
   9.56374634e+01   1.00538904e+02   1.05656412e+02   1.10998840e+02
   1.16575309e+02   1.22395291e+02   1.28469017e+02   1.34806692e+02
   1.41419314e+02   1.48318298e+02   1.55515633e+02   1.63023812e+02
   1.70855968e+02   1.79025863e+02   1.87547932e+02   1.96437328e+02
   2.05709966e+02   2.15382571e+02   2.25472733e+02   2.35998944e+02
   2.46980707e+02   2.58438711e+02   2.70394317e+02   2.82870610e+02
   2.95891677e+02   3.09483042e+02   3.23671701e+02   3.38486241e+02
   3.53956949e+02   3.70115922e+02   3.86997346e+02   4.04637375e+02
   4.23074577e+02   4.42350000e+02   4.62507353e+02   4.83593288e+02
   5.05657695e+02   5.28753870e+02   5.52938830e+02   5.78273764e+02
   6.04824359e+02   6.32661170e+02   6.61860148e+02   6.92503163e+02
   7.24678537e+02   7.58481826e+02   7.94016445e+02   8.31394571e+02
   8.70738080e+02   9.12179625e+02   9.55863865e+02   1.00194886e+03
   1.05060766e+03   1.10203016e+03   1.15642518e+03   1.21402288e+03
   1.27507764e+03   1.33987126e+03   1.40871684e+03   1.48196317e+03
   1.56000009e+03   1.64326464e+03   1.73224846e+03   1.82750663e+03
   1.92966823e+03   2.03944911e+03   2.15766739e+03   2.28526238e+03
   2.42331797e+03   2.57309138e+03   2.73604934e+03   2.91391360e+03
   3.10871855e+03   3.32288583e+03   3.55932039e+03   3.82153756e+03
   4.11383124e+03   4.44150175e+03   4.81116792e+03   5.23120243e+03
   5.71235181e+03   6.26863741e+03   6.91869924e+03   7.68785931e+03
   8.61139105e+03   9.73991058e+03   1.11486791e+04   1.29545712e+04
   1.53492175e+04   1.86696236e+04]

In [15]:
t00 = np.arange(-.8, -.7, 0.001)
chiSq = np.array([])
rp0 = np.arange(0.01, 0.2, 0.01)

chichi = np.zeros([len(t00), len(rp0)])

for i in range(len(t00)):
    thist = t00[i]
    
    # set the model t0 to one value of t00
    params.t0 = thist
    m = batman.TransitModel(params, time)
    modelflux = m.light_curve(params)
    chiSq = np.append(chiSq, chisqa(nflux, modelflux))
    chiSq2 = np.array([])
    for j in range(len(rp0)):
        #print i, j
        thisrp = rp0[j]
        params.rp = thisrp
        m = batman.TransitModel(params, time)
        modelflux2 = m.light_curve(params)
        chichi[i,j] =  chisqa(nflux, modelflux2)

#print chiSq
#print chiSq2
#print chichi

In [8]:
plt.imshow(np.log(chichi), cmap='gray', vmax = -2.999, aspect = 'auto')

plt.colorbar()
chichi.shape


Out[8]:
(101, 19)

In [11]:
row, column = np.unravel_index([np.argmin(chichi)], np.shape(chichi))
tbest = t00[row]
rbest = rp0[column]
print tbest
print rbest


[-0.767]
[ 0.12]
make imshow plot of t0 vs rp and an imshow plot of t0 vs per comment EVERY single line of code

In [13]:
per00 = np.arange(2, 6, 0.1)
chiSq = np.array([])
rp0 = np.arange(0.01, 0.2, 0.001)

chichi2 = np.zeros([len(t00), len(rp0)])

for i in range(len(per00)):
    thisp = per00[i]
    
    # set the model t0 to one value of t00
    params.per = thist
    m = batman.TransitModel(params, time)
    modelflux = m.light_curve(params)
    chiSq = np.append(chiSq, chisqa(nflux, modelflux))
    chiSq2 = np.array([])
    for j in range(len(rp0)):
        #print i, j
        thisrp = rp0[j]
        params.rp = thisrp
        m = batman.TransitModel(params, time)
        modelflux2 = m.light_curve(params)
        chichi2[i,j] =  chisqa(nflux, modelflux2)

In [14]:
plt.imshow(chichi2, cmap='gray', aspect='equal')#, extent=????)
plt.colorbar()
chichi.shape


Out[14]:
(101, 19)