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

# + colab={"base_uri": "https://localhost:8080/"} id="A74kXimPnJO3" executionInfo={"status": "ok", "timestamp": 1628926986520, "user_tz": -420, "elapsed": 19654, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00266109630258428955"}} outputId="63f398fd-17c5-41fc-f632-0fc5a04ab785"
# !pip install lightkurve

# + id="jkYwCvnpm970" executionInfo={"status": "ok", "timestamp": 1628927264723, "user_tz": -420, "elapsed": 300, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00266109630258428955"}}
import lightkurve as lk
import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inline

# + colab={"base_uri": "https://localhost:8080/", "height": 1000} id="JKd5jGDGn5di" executionInfo={"status": "ok", "timestamp": 1628929922311, "user_tz": -420, "elapsed": 355, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00266109630258428955"}} outputId="7a089e73-bf42-4d6a-fa85-fafe883a57da"
# Search Kepler data for Quarters 6, 7, and 8.
search_result = lk.search_lightcurve('WASP-33', mission='TESS')
# Download and stitch the data together
lc = search_result.download()
# Plot the resulting light curve
lc

# + colab={"base_uri": "https://localhost:8080/", "height": 386} id="jAWQi0q3o5R9" executionInfo={"status": "ok", "timestamp": 1628929927333, "user_tz": -420, "elapsed": 1405, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00266109630258428955"}} outputId="e92f218a-bd62-455c-92d2-d3471035255f"
lc.plot();

# + colab={"base_uri": "https://localhost:8080/", "height": 390} id="FwVjyyB9uuzg" executionInfo={"status": "ok", "timestamp": 1628929929010, "user_tz": -420, "elapsed": 619, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00266109630258428955"}} outputId="6668635d-5e74-4367-cf0a-50a27dff6963"
pg = lc.to_periodogram(maximum_period=100)
pg.plot(view='period');

# + colab={"base_uri": "https://localhost:8080/", "height": 37} id="XCF_VvMjuztH" executionInfo={"status": "ok", "timestamp": 1628929932655, "user_tz": -420, "elapsed": 350, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00266109630258428955"}} outputId="0df6bd06-9832-4304-fdda-ece07138eb7b"
pg.period_at_max_power

# + colab={"base_uri": "https://localhost:8080/", "height": 389} id="zzqKfgWUupFJ" executionInfo={"status": "ok", "timestamp": 1628929935452, "user_tz": -420, "elapsed": 1173, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00266109630258428955"}} outputId="a253eb45-d977-4be3-c160-fcd7dac3ac7a"
# Create a model light curve for the highest peak in the periodogram
lc_model = pg.model(time=lc.time, frequency=pg.frequency_at_max_power)
# Plot the light curve
ax = lc.plot()
# Plot the model light curve on top
lc_model.plot(ax=ax, lw=3, ls='--', c='red');

# + colab={"base_uri": "https://localhost:8080/", "height": 386} id="DWuMS-txyk9Z" executionInfo={"status": "ok", "timestamp": 1628929993734, "user_tz": -420, "elapsed": 971, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00266109630258428955"}} outputId="e5f6b978-c55b-430a-aab4-ca1c9bf26ad7"
# Fold the light curve at the known planet period
planet_period = 1.21987
lc.fold(period=planet_period).plot();

# + colab={"base_uri": "https://localhost:8080/", "height": 404} id="H3TbFiJUy182" executionInfo={"status": "ok", "timestamp": 1628930072351, "user_tz": -420, "elapsed": 1690, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00266109630258428955"}} outputId="1c7685d9-7128-43ba-c411-444a49eed319"
pg = lc.to_periodogram()
pg.plot(scale='log');

# + colab={"base_uri": "https://localhost:8080/", "height": 386} id="ZeMOwKW4zGqN" executionInfo={"status": "ok", "timestamp": 1628930101619, "user_tz": -420, "elapsed": 10147, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00266109630258428955"}} outputId="49c2727a-7f81-46e5-df91-d6b0b3074ecf"
# Remove the signals associated with the 50 highest peaks
newlc = lc.copy()
for i in range(50):
  pg = newlc.to_periodogram()
  model = pg.model(time=newlc.time, frequency=pg.frequency_at_max_power)
  newlc.flux = newlc.flux / model.flux

# Plot the new light curve on top of the original one
ax = lc.plot(alpha=.5, label='Original');
newlc.plot(ax=ax, label='New');

# + id="Ncxaw_yezUeV" executionInfo={"status": "ok", "timestamp": 1628930137585, "user_tz": -420, "elapsed": 1665, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00266109630258428955"}} outputId="3966fb5e-6a25-4ddf-f65d-5875860f7350" colab={"base_uri": "https://localhost:8080/"}
ax = newlc.fold(period=planet_period).plot(label='Unbinned')
newlc.fold(period=planet_period).bin(0.1).plot(ax=ax, lw=2, label='Binned');

StarcoderdataJupyterScriptsDedupFiltered