# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Exoploring the effect that focus changes have on the detection of planets

# #### We take the code for generating the TESS Input Catalog and recalculate contamination with different PSF widths. 
#
# Here is what we do
# * make a small version of the TIC centered on coordinates ra,dec (50,-30). Why these coordaintes? because they weem to be fairly representative of the sky.
# * We cut out a 12 degree radius circle, this kinda approximatates 1 TESS camera
# * we calculate the contamination on the top 4000 priority targets in this field of view
# * we use a PSF width of 1.88 as a base and multiply that by factors of [1.0, 1.1, 1.2, 1.3, 1.5, 2.0, 3.0, 5.0] 

# We're going to open a datafile that I made earlier

# +
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# %matplotlib inline
# -

contamfile = 'contamfile.h5'
df = pd.read_hdf(contamfile)


df.columns

# Currently the table is in the form of flux ratio. We can make that scale with signal to noise by having 1 / (1 + FluxRatio)

idx = np.digitize(df.iloc[:,8], bins=np.arange(0,20,1))

# +
fig, ax = plt.subplots(1,1,figsize=[11,6], )

ax.scatter(df.iloc[:,8], (1+df.iloc[:,0]), s=2)
ax.scatter(df.iloc[:,8], (1+df.iloc[:,4]), s=1)

ax.set_ylim(0.999,1.01)

# +
fig, ax = plt.subplots(1,1,figsize=[11,6], )

ax.scatter(df.iloc[:,8], (1+df.iloc[:,4])/(1+df.iloc[:,0]), s=2)


ax.set_ylim(0.98,1.1)

# +
fig, ax = plt.subplots(1,1,figsize=[11,6], )

a = np.zeros([np.unique(idx).shape[0], 3])
for i,x in enumerate(np.unique(idx)):
    v = np.percentile((1+df.loc[idx==x,'w2.0'])/(1+df.loc[idx==x,'w1.0']), [50,90,99])
    a[i] = v
ax.plot(np.unique(idx),a.T[0])
# ax.scatter(df.iloc[:,8], (1+df.iloc[:,4])/(1+df.iloc[:,0]), s=2)
ax.fill_between(np.unique(idx),a.T[0],a.T[1], alpha=0.2)

plt.xlabel('TessMag')
# plt.ylim()
# -

columns = ['RAdeg', 'DEdeg' ,'Rp', 'Per', 'S', 'K', 'Rstar', 'Teff', 
           'Vmag', 'Imag', 'Jmag', 'Ksmag', 'DM', 'Dil', 'logsigV', 
           'SNR','Mult']  
tess_catalog = pd.read_table('../../bendeksim/data/apj516470t6_mrt.txt', skiprows=32, delim_whitespace=True, 
                              names=columns)

l = tess_catalog.SNR.shape[0]
print(l)


# +
s = tess_catalog.SNR / np.random.choice((1+df.loc[idx==x,'w3.0'])/(1+df.loc[idx==x,'w1.0']), size=1984)

tess_catalog[s < 7.305]
# -

plt.hist(tess_catalog.Rp, bins=np.arange(0,5,0.5))
plt.hist(tess_catalog.Rp[s < 7.305],bins=np.arange(0,5,0.5))

plt.hist(tess_catalog.Rp[s < 7.305],bins=np.arange(0,5,0.5))



from scipy.integrate import dblquad, 

dblquad(multivariate_normal, -0.5, 0.5)


mm = (df['w1.1'] / df['w1.0'])

mm[np.isfinite(mm)].median()

fig, ax = plt.subplots(1,1)
plt.scatter(df['TESSMAG'] ,(df['w2.0']- df['w1.0']) / df['w1.0'],s=1)
# plt.ylim(0,1.E-4)
ax.set_yscale("log", nonposy='clip')

df[df['w1.0'] > 1.E-5]





StarcoderdataJupyterScriptsDedupFiltered