{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Simultaneously fit multiple lightcurves \n",
    "\n",
    "Here we will fit multiple lightcurves simultaneously using the global fitter in exotic. We will set up bounds for each lightcurve that specify what parameters to fit along with 'global' bounds that are shared between all lightcurves. \n",
    "\n",
    "![](https://s3.amazonaws.com/aasie/images/0004-6256/164/5/null/ajac8deef3_hr.jpg)\n",
    "\n",
    "This notebook will show you how to reproduce something similar to Figure 4 in a study on HD 80606 b, https://ui.adsabs.harvard.edu/abs/2022AJ....164..178P/abstract \n",
    "\n",
    "Please cite that paper if you use this code"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Find lightcurve data in the Exoplanet Watch Database\n",
    "\n",
    "The command `get` will fetch priors, light curves and an ephemeris (O-C) for each target."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded 278 targets\n",
      "['CoRoT-11 b', 'CoRoT-14 b', 'CoRoT-18 b', 'CoRoT-1 b', 'CoRoT-27 b', 'CoRoT-29 b', 'CoRoT-2 b', 'CoRoT-7 b', 'CoRoT-8 b', 'CoRoTID 223977153 b', 'EPIC 206042996 c', 'Gaia-1 b', 'Gaia-2 b', 'GJ 1214 b', 'GJ 3470 b', 'GJ 436 b', 'GPX-1 b', 'HAT-P-11 b', 'HAT-P-12 b', 'HAT-P-13 b', 'HAT-P-16 b', 'HAT-P-17 b', 'HAT-P-18 b', 'HAT-P-19 b', 'HAT-P-1 b', 'HAT-P-20 b', 'HAT-P-21 b', 'HAT-P-22 b', 'HAT-P-23 b', 'HAT-P-25 b', 'HAT-P-26 b', 'HAT-P-27 b', 'HAT-P-28 b', 'HAT-P-29 b', 'HAT-P-30 b', 'HAT-P-32 b', 'HAT-P-33 b', 'HAT-P-36 b', 'HAT-P-37 b', 'HAT-P-39 b', 'HAT-P-3 b', 'HAT-P-41 b', 'HAT-P-44 b', 'HAT-P-45 b', 'HAT-P-49 b', 'HAT-P-4 b', 'HAT-P-51 b', 'HAT-P-52 b', 'HAT-P-53 b', 'HAT-P-54 b', 'HAT-P-55 b', 'HAT-P-56 b', 'HAT-P-57 b', 'HAT-P-59 b', 'HAT-P-5 b', 'HAT-P-68 b', 'HAT-P-6 b', 'HAT-P-70 b', 'HAT-P-7 b', 'HAT-P-8 b', 'HAT-P-9 b', 'HATS-18 b', 'HATS-24 b', 'HATS-33 b', 'HD 149026 b', 'HD 189733 b', 'HD 209458 b', 'HD 80606 b', 'K2-117 b', 'K2-123 b', 'K2-127 b', 'K2-137 b', 'K2-140 b', 'K2-141 c', 'K2-146 b', 'K2-153 b', 'K2-183 b', 'K2-216 b', 'K2-229 b', 'K2-22 b', 'K2-242 b', 'K2-250 b', 'K2-25 b', 'K2-260 b', 'K2-266 b', 'K2-270 c', 'K2-284 b', 'K2-285 b', 'K2-295 b', 'K2-29 b', 'K2-308 b', 'K2-30 b', 'K2-31 b', 'K2-321 b', 'K2-34 b', 'K2-371 b', 'KELT-16 b', 'KELT-17 b', 'KELT-18 b', 'KELT-19 A b', 'KELT-1 b', 'KELT-20 b', 'KELT-23 A b', 'KELT-3 b', 'KELT-4 A b', 'KELT-7 b', 'Kepler-1086 b', 'Kepler-1087 b', 'Kepler-1124 b', 'Kepler-1320 b', 'Kepler-1321 c', 'Kepler-1350 b', 'Kepler-1351 b', 'Kepler-1520 b', 'Kepler-1650 b', 'Kepler-1651 b', 'Kepler-167 e', 'Kepler-17 b', 'Kepler-210 b', 'Kepler-231 b', 'Kepler-254 b', 'Kepler-283 b', 'Kepler-296 c', 'Kepler-412 b', 'Kepler-41 b', 'Kepler-423 b', 'Kepler-424 b', 'Kepler-425 b', 'Kepler-426 b', 'Kepler-428 b', 'Kepler-42 c', 'Kepler-43 b', 'Kepler-446 b', 'Kepler-446 c', 'Kepler-447 b', 'Kepler-44 b', 'Kepler-45 b', 'Kepler-471 b', 'Kepler-472 b', 'Kepler-488 b', 'Kepler-491 b', 'Kepler-493 b', 'Kepler-495 b', 'Kepler-546 b', 'Kepler-548 b', 'Kepler-553 b', 'Kepler-673 b', 'Kepler-685 b', 'Kepler-686 b', 'Kepler-693 b', 'Kepler-695 b', 'Kepler-697 b', 'Kepler-6 b', 'Kepler-702 b', 'Kepler-718 b', 'Kepler-720 b', 'Kepler-723 b', 'Kepler-729 b', 'Kepler-74 b', 'Kepler-76 b', 'Kepler-77 b', 'Kepler-785 b', 'Kepler-81 c', 'Kepler-8 b', 'Kepler-913 b', 'Kepler-94 b', 'Kepler-957 b', 'KOI-13 b', 'KOI-217 b', 'KPS-1 b', 'LHS 1140 c', 'NGTS-10 b', 'NGTS-5 b', 'Qatar-10 b', 'Qatar-1 b', 'Qatar-2 b', 'Qatar-4 b', 'Qatar-6 b', 'Qatar-7 b', 'Qatar-8 b', 'Qatar-9 b', 'TOI-1130 b', 'TOI-1259 A b', 'TOI-1333 b', 'TOI-1431 b', 'TOI-1442 b', 'TOI-1516 b', 'TOI-1518 b', 'TOI-1634 b', 'TOI-1728 b', 'TOI-2046 b', 'TOI-2109 b', 'TOI-3693 b', 'TOI-3714 b', 'TOI-4479 b', 'TOI-519 b', 'TOI-532 b', 'TRAPPIST-1 b', 'TRAPPIST-1 c', 'TrES-1 b', 'TrES-2 b', 'TrES-3 b', 'TrES-4 b', 'TrES-5 b', 'WASP-103 b', 'WASP-104 b', 'WASP-10 b', 'WASP-113 b', 'WASP-114 b', 'WASP-118 b', 'WASP-11 b', 'WASP-127 b', 'WASP-12 b', 'WASP-135 b', 'WASP-138 b', 'WASP-13 b', 'WASP-140 b', 'WASP-148 b', 'WASP-14 b', 'WASP-153 b', 'WASP-156 b', 'WASP-157 b', 'WASP-163 b', 'WASP-177 b', 'WASP-180 A b', 'WASP-181 b', 'WASP-183 b', 'WASP-18 b', 'WASP-19 b', 'WASP-1 b', 'WASP-21 b', 'WASP-24 b', 'WASP-25 b', 'WASP-26 b', 'WASP-28 b', 'WASP-2 b', 'WASP-32 b', 'WASP-33 b', 'WASP-34 b', 'WASP-35 b', 'WASP-36 b', 'WASP-39 b', 'WASP-3 b', 'WASP-41 b', 'WASP-43 b', 'WASP-48 b', 'WASP-49 b', 'WASP-50 b', 'WASP-52 b', 'WASP-53 b', 'WASP-56 b', 'WASP-57 b', 'WASP-58 b', 'WASP-59 b', 'WASP-5 b', 'WASP-60 b', 'WASP-64 b', 'WASP-65 b', 'WASP-67 b', 'WASP-6 b', 'WASP-74 b', 'WASP-75 b', 'WASP-77 A b', 'WASP-80 b', 'WASP-82 b', 'WASP-85 A b', 'WASP-89 b', 'WASP-92 b', 'WASP-93 b', 'WASP-98 b', 'Wendelstein-2 b', 'WTS-2 b', 'XO-1 b', 'XO-2 N b', 'XO-3 b', 'XO-5 b', 'XO-6 b', 'XO-7 b']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "dict_keys(['raw_result', 'host', 'name', 'priors', 'timestamp', 'identifier', 'reduction_count', 'observation_count', 'ephemeris', 'observations'])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from exotic.api.ew import ExoplanetWatch\n",
    "from IPython.display import display, Image\n",
    "\n",
    "# This will load the results JSON from the link above\n",
    "EW = ExoplanetWatch()\n",
    "print(EW.target_list)\n",
    "\n",
    "# names are case and space sensitive\n",
    "result = EW.get('WASP-1 b')\n",
    "\n",
    "# list the result properties\n",
    "result.__dict__.keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Priors\n",
    "A list of stellar and planetary parameters with references. These are used to calculate a light curve model with non-linear limb darkening."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'R*': {'units': 'Sol',\n",
       "  'value': '1.52',\n",
       "  'reference': 'Kokori%20et%20al.%202022',\n",
       "  'uncertainty': '0.05'},\n",
       " 'RA': {'units': 'decimal',\n",
       "  'value': '5.1669536',\n",
       "  'reference': None,\n",
       "  'uncertainty': None},\n",
       " 'Rp': {'units': 'R_Earth',\n",
       "  'value': '17.7',\n",
       "  'reference': 'ExoFOP-TESS%20TOI',\n",
       "  'uncertainty': '0.9'},\n",
       " 'T*': {'units': 'K',\n",
       "  'value': '6116.0',\n",
       "  'reference': 'ExoFOP-TESS%20TOI',\n",
       "  'uncertainty': '106.58'},\n",
       " 'Tc': {'units': 'BJD-TDB',\n",
       "  'value': '2456543.33866',\n",
       "  'reference': 'Ivshina%20&amp;%20Winn%202022',\n",
       "  'uncertainty': '0.00028'},\n",
       " 'DEC': {'units': 'decimal',\n",
       "  'value': '31.9899732',\n",
       "  'reference': None,\n",
       "  'uncertainty': None},\n",
       " 'Tcs': [{'units': 'BJD, BJD-TDB, HJD',\n",
       "   'value': '2453912.5147133335',\n",
       "   'reference': 'Turner%20et%20al.%202016,%20Maciejewski%20et%20al.%202014,%20Collier%20Cameron%20et%20al.%202007',\n",
       "   'uncertainty': '0.00054'},\n",
       "  {'units': 'HJD-UTC',\n",
       "   'value': '2454461.86099',\n",
       "   'reference': 'Simpson%20et%20al.%202011',\n",
       "   'uncertainty': '0.00024'},\n",
       "  {'units': 'BJD-TDB',\n",
       "   'value': '2455215.32701',\n",
       "   'reference': 'Kokori%20et%20al.%202022',\n",
       "   'uncertainty': '0.00015'},\n",
       "  {'units': 'BJD-TDB',\n",
       "   'value': '2455530.3197',\n",
       "   'reference': 'Bonomo%20et%20al.%202017',\n",
       "   'uncertainty': '0.00016'},\n",
       "  {'units': 'BJD-TDB',\n",
       "   'value': '2456543.33866',\n",
       "   'reference': 'Ivshina%20&amp;%20Winn%202022',\n",
       "   'uncertainty': '0.00028'},\n",
       "  {'units': 'BJD',\n",
       "   'value': '2459854.549103',\n",
       "   'reference': 'ExoFOP-TESS%20TOI',\n",
       "   'uncertainty': '0.000382'}],\n",
       " 'ecc': {'units': None,\n",
       "  'value': '0.013',\n",
       "  'reference': 'Bonomo%20et%20al.%202017',\n",
       "  'uncertainty': '0.026'},\n",
       " 'inc': {'units': 'deg',\n",
       "  'value': '90.0',\n",
       "  'reference': 'Bonomo%20et%20al.%202017',\n",
       "  'uncertainty': '0.0'},\n",
       " 'FE/H': {'units': None,\n",
       "  'value': '0.26',\n",
       "  'reference': 'Bonomo%20et%20al.%202017',\n",
       "  'uncertainty': '0.08'},\n",
       " 'LOGG': {'units': 'Log10(cgs)',\n",
       "  'value': '4.11',\n",
       "  'reference': 'ExoFOP-TESS%20TOI',\n",
       "  'uncertainty': '0.08'},\n",
       " 'a/R*': {'units': None,\n",
       "  'value': '5.687',\n",
       "  'reference': 'Maciejewski%20et%20al.%202014',\n",
       "  'uncertainty': '0.034'},\n",
       " 'Rp/R*': {'units': None,\n",
       "  'value': '0.1115',\n",
       "  'reference': 'ExoFOP-TESS%20TOI',\n",
       "  'uncertainty': '0'},\n",
       " 'omega': {'units': 'deg',\n",
       "  'value': '91.1',\n",
       "  'reference': 'Knutson%20et%20al.%202014',\n",
       "  'uncertainty': '170.0'},\n",
       " 'Period': {'units': 'day',\n",
       "  'value': '2.51994639',\n",
       "  'reference': 'Ivshina%20&amp;%20Winn%202022',\n",
       "  'uncertainty': '3.9e-07'}}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.priors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['Tc', 'Tcs', 'files', 'notes', 'Period', 'Tc_err', 'epochs', 'Tcs_err', 'datasets', 'nea_tmids', 'residuals', 'timesaved', 'Period_err', 'nea_epochs', 'nea_tmids_err', 'nea_references', 'ephemeris_url'])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.ephemeris.keys()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Observations\n",
    "A list of observations for each target. These are used to fit the light curve model to the data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(result.observations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['raw_observation', 'name', 'files', 'filter', 'obscode', 'identifier', 'secondary_obscodes', 'errors', 'parameters', 'lightcurve_url', 'posterior_url'])\n"
     ]
    }
   ],
   "source": [
    "# list the properties\n",
    "print(result.observations[0].__dict__.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'u0': 1.8275705061936063,\n",
       " 'u1': -3.2844787398267963,\n",
       " 'u2': 3.938674827919378,\n",
       " 'u3': -1.4817665942861882,\n",
       " 'ecc': 0.013,\n",
       " 'inc': 90.0,\n",
       " 'omega': 91.1,\n",
       " 'duration': 0.1551,\n",
       " 'tmid': 2455895.714,\n",
       " 'a1': 1.1573,\n",
       " 'a2': -0.134,\n",
       " 'ars': 5.708,\n",
       " 'rprs': 0.109,\n",
       " 'per': 2.51994639}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# print the best-fit parameters\n",
    "result.observations[0].parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# show the light curve\n",
    "#for i in range(len(result.observations)):\n",
    "#    display(Image(url=result.observations[i].lightcurve_url))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Download the light curve data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "time, flux, fluxerr, airmass, airmasscorr = result.observations[0].get_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'Rel. Flux')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 12372 characters]",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "plt.plot(time, flux/airmasscorr, 'ko')\n",
    "plt.xlabel(\"Time [BJD]\")\n",
    "plt.ylabel(\"Rel. Flux\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    Downloading __databases__.pickle_new...\n",
      "    Done!\n",
      "Checking exotethys database...\n",
      "Checking ephemerides database...\n",
      "Checking photometry database...\n",
      "Checking catalogues database...\n",
      "    Downloading ecc_planets.yaml...\n",
      "    Done!\n",
      "    Downloading ecc_stars.yaml...\n",
      "    Done!\n",
      "\tUpdating:  ecc_planets.yaml\n",
      "    Downloading ecc_planets.yaml...\n",
      "    Done!\n",
      "\tUpdating:  ecc_stars.yaml\n",
      "    Downloading ecc_stars.yaml...\n",
      "    Done!\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'Rel. Flux')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 15380 characters]",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# let's over plot the transit model too\n",
    "from exotic.api.elca import transit\n",
    "\n",
    "plt.plot(time, flux/airmasscorr, 'ko')\n",
    "plt.plot(time, transit(time, result.observations[0].parameters), 'r-')\n",
    "plt.xlabel(\"Time [BJD]\")\n",
    "plt.ylabel(\"Rel. Flux\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fit a single lightcurve using EXOTIC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2956aceef1a14b89b5263627837ab56c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-5e+01   [-45.6000..-45.6000]*| it/evals=5400/10772 eff=52.0632% N=400   00 0 \n",
      "[ultranest] Likelihood function evaluations: 10810\n",
      "[ultranest]   logZ = -54.5 +- 0.09669\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 2033.4, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.06 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.10, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.13 bs:0.10 tail:0.01 total:0.10 required:<0.50\n",
      "[ultranest] done iterating.\n"
     ]
    }
   ],
   "source": [
    "from exotic.api.elca import lc_fitter\n",
    "\n",
    "# only define bounds for the \"free\" parameters during fitting\n",
    "mybounds = {\n",
    "    # [lower, upper]\n",
    "    'rprs':[0,0.2],\n",
    "    'tmid':[ result.ephemeris['Tc']-0.02,\n",
    "             result.ephemeris['Tc']+0.02],\n",
    "    'a2':[-0.5,0.5] # airmass curvature\n",
    "}\n",
    "\n",
    "myfit = lc_fitter(time, flux, fluxerr, airmass,\n",
    "                  result.observations[0].parameters, # priors\n",
    "                  mybounds, mode='ns' )  # ns = nested sampling, slow but robust + posteriors\n",
    "                                         # lm = levenberg mardquart, fast but no uncertainties"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 43476 characters]",
      "text/plain": [
       "<Figure size 648x432 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "myfit.plot_bestfit()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 184644 characters]",
      "text/plain": [
       "<Figure size 547.2x547.2 with 9 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# show the posterior distributions\n",
    "myfit.plot_triangle()\n",
    "plt.show()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Fit multiple light curves\n",
    "\n",
    "After each light curve is fit, the mid-transit times are used along with values from the NASA Exoplanet Archive to derive a phase-folded fit with every dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NANF 1 : 2012-12-12T02:10:49.860 (-1421.0450) - 2012-12-12T07:53:12.775 (-1420.9507)\n",
      "GELD 2 : 2012-12-17T03:01:54.006 (-1419.0468) - 2012-12-17T06:34:38.636 (-1418.9882)\n",
      "FMAA 3 : 2013-10-23T01:58:05.470 (-1296.0459) - 2013-10-23T07:45:02.729 (-1295.9503)\n",
      "FMAA 4 : 2013-10-28T02:44:57.399 (-1294.0488) - 2013-10-28T08:41:52.907 (-1293.9505)\n",
      "NKED 5 : 2013-12-20T01:16:22.881 (-1273.0411) - 2013-12-20T06:40:19.617 (-1272.9518)\n",
      "FGIC 6 : 2014-09-23T06:20:53.656 (-1163.0342) - 2014-09-23T11:26:54.743 (-1162.9499)\n",
      "FTAC 7 : 2014-11-10T02:44:22.797 (-1144.0459) - 2014-11-10T08:23:14.119 (-1143.9525)\n",
      "LGEC 8 : 2016-08-11T06:35:40.478 (-890.0086) - 2016-08-11T10:02:41.070 (-889.9515)\n",
      "LPAC 9 : 2017-09-05T08:26:12.807 (-735.2129) - 2017-09-05T09:19:20.967 (-735.1983)\n",
      "FMAA 10 : 2017-10-16T02:12:15.185 (-719.0458) - 2017-10-16T07:57:14.374 (-718.9508)\n",
      "NKED 11 : 2017-10-21T03:07:19.347 (-717.0465) - 2017-10-21T08:54:09.355 (-716.9509)\n",
      "MWAA 12 : 2017-12-08T00:38:39.840 (-698.0394) - 2017-12-08T04:57:12.960 (-697.9682)\n",
      "GDAI 13 : 2017-12-13T01:06:55.161 (-696.0475) - 2017-12-13T07:11:13.227 (-695.9471)\n",
      "NANF 14 : 2018-10-29T01:58:19.915 (-569.0465) - 2018-10-29T07:44:54.579 (-568.9510)\n"
     ]
    }
   ],
   "source": [
    "from astropy.time import Time\n",
    "from copy import deepcopy\n",
    "\n",
    "# prep data for global fitter\n",
    "input_data = []\n",
    "\n",
    "# free parameters for each light curve\n",
    "local_bounds = []\n",
    "\n",
    "# copy default priors\n",
    "prior = deepcopy(result.observations[0].parameters)\n",
    "\n",
    "# aavso files on EW\n",
    "for n,obs in enumerate(result.observations):\n",
    "\n",
    "    try:\n",
    "        time, flux, fluxerr, airmass, airmasscorr = obs.get_data()\n",
    "    except:\n",
    "        time, flux, fluxerr, airmass = obs.get_data()\n",
    "\n",
    "    # ignore noisy data\n",
    "    if np.std(flux) > 0.03:\n",
    "        continue\n",
    "    else:\n",
    "        prior = deepcopy(result.observations[n].parameters)\n",
    "\n",
    "        # add data to list\n",
    "        input_data.append({\n",
    "            'time':time,\n",
    "            'flux':flux,\n",
    "            'ferr':fluxerr,\n",
    "            'airmass':airmass,\n",
    "            'priors':prior,\n",
    "            'name':f\"{obs.obscode['id']}\"\n",
    "        })\n",
    "\n",
    "        # set parameters to ephemeris\n",
    "        input_data[-1]['priors']['per'] = result.ephemeris['Period']\n",
    "        input_data[-1]['priors']['tmid'] = result.ephemeris['Tc']\n",
    "\n",
    "    # add parameter for individual airmass detrending\n",
    "    local_bounds.append({'a2':(-0.75,0.75)}) \n",
    "    # bounds will get overwritten by setting \"individual_fit\" flag in global fitter\n",
    "\n",
    "    # print some timing info\n",
    "    mint = Time(input_data[-1]['time'].min(),format='jd').isot\n",
    "    maxt = Time(input_data[-1]['time'].max(),format='jd').isot\n",
    "    maxphase = (input_data[-1]['time'].max() - prior['tmid'])/prior['per']\n",
    "    minphase = (input_data[-1]['time'].min() - prior['tmid'])/prior['per']\n",
    "    print(f\"{input_data[-1]['name']} {len(input_data)} : {mint} ({minphase:.4f}) - {maxt} ({maxphase:.4f})\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PHOENIX models are only computed for solar metallicity stars. Setting stellar_metallicity = 0.\n",
      "['wasp1b_2459872_19_AAVSO_TESS.txt', 'wasp1b_2459854_55_AAVSO_TESS.txt', 'wasp1b_2459869_67_AAVSO_TESS.txt', 'wasp1b_2459867_15_AAVSO_TESS.txt', 'wasp1b_2459879_75_AAVSO_TESS.txt', 'wasp1b_2459857_07_AAVSO_TESS.txt', 'wasp1b_2459862_11_AAVSO_TESS.txt', 'wasp1b_2459859_59_AAVSO_TESS.txt', 'wasp1b_2459877_23_AAVSO_TESS.txt']\n",
      "TESS 15 : 2022-10-19T09:49:57.068 (6.8891) - 2022-10-19T23:13:56.410 (7.1107)\n",
      "TESS 16 : 2022-10-01T18:28:56.315 (-0.1108) - 2022-10-02T07:52:57.070 (0.1107)\n",
      "TESS 17 : 2022-10-16T21:21:19.315 (5.8892) - 2022-10-17T10:44:58.928 (6.1106)\n",
      "TESS 18 : 2022-10-14T08:52:40.365 (4.8892) - 2022-10-14T17:53:00.225 (5.0381)\n",
      "TESS 19 : 2022-10-26T23:16:04.119 (9.8891) - 2022-10-27T12:40:02.857 (10.1107)\n",
      "TESS 20 : 2022-10-04T06:57:39.173 (0.8892) - 2022-10-04T20:21:39.635 (1.1107)\n",
      "TESS 21 : 2022-10-09T07:55:01.507 (2.8891) - 2022-10-09T21:19:01.529 (3.1107)\n",
      "TESS 22 : 2022-10-06T19:26:20.839 (1.8891) - 2022-10-07T08:50:21.069 (2.1107)\n",
      "TESS 23 : 2022-10-24T10:47:29.350 (8.8892) - 2022-10-25T00:11:08.264 (9.1106)\n"
     ]
    }
   ],
   "source": [
    "import glob\n",
    "from pylightcurve import exotethys\n",
    "\n",
    "# generate limb darkening coefficients for TESS\n",
    "get_prior = lambda key: float(result.priors[key]['value'])\n",
    "u0,u1,u2,u3 = exotethys(get_prior('LOGG'), get_prior('T*'), get_prior('FE/H'), 'TESS', method='claret', stellar_model='phoenix')\n",
    "\n",
    "# get files from local directory\n",
    "tess_lightcurves = glob.glob('wasp1b*.txt')\n",
    "print(tess_lightcurves)\n",
    "\n",
    "# add TESS data\n",
    "for n,lc in enumerate(tess_lightcurves):\n",
    "\n",
    "        # read in data\n",
    "        time, flux, fluxerr, airmass, airmasscorr = np.loadtxt(lc, unpack=True, delimiter=',')\n",
    "        prior = deepcopy(result.observations[0].parameters)\n",
    "        prior['u0'] = u0\n",
    "        prior['u1'] = u1\n",
    "        prior['u2'] = u2\n",
    "        prior['u3'] = u3\n",
    "        prior['tmid'] = result.ephemeris['Tc']\n",
    "        prior['per'] = result.ephemeris['Period']\n",
    "        prior['a2'] = 0.0\n",
    "        \n",
    "        # add data to list\n",
    "        input_data.append({\n",
    "            'time':time,\n",
    "            'flux':flux,\n",
    "            'ferr':fluxerr,\n",
    "            'airmass':np.zeros(time.shape),\n",
    "            'priors':prior,\n",
    "            'name':f\"TESS\"\n",
    "        })\n",
    "    \n",
    "        # no airmass detrending\n",
    "        local_bounds.append({})\n",
    "\n",
    "        # print some timing info\n",
    "        mint = Time(input_data[-1]['time'].min(),format='jd').isot\n",
    "        maxt = Time(input_data[-1]['time'].max(),format='jd').isot\n",
    "        maxphase = (input_data[-1]['time'].max() - prior['tmid'])/prior['per']\n",
    "        minphase = (input_data[-1]['time'].min() - prior['tmid'])/prior['per']\n",
    "        print(f\"{input_data[-1]['name']} {len(input_data)} : {mint} ({minphase:.4f}) - {maxt} ({maxphase:.4f})\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting individual light curve 1/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "49ad0bc2c8a04d80928d04e34b19b7b2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-4e+01   [-35.7117..-35.7116]*| it/evals=5280/10217 eff=53.7843% N=400        \n",
      "[ultranest] Likelihood function evaluations: 10235\n",
      "[ultranest]   logZ = -44.25 +- 0.09348\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 2126.2, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.05 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.09, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.13 bs:0.09 tail:0.01 total:0.09 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "NANF & 2456273.59085486 & 2456273.82862008 & 0.0124956530723525 & 113\n",
      "Fitting individual light curve 2/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "fcdfc40522224f97be4762f8dcdcd9c5",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-4e+01   [-43.6381..-43.6381]*| it/evals=5080/9891 eff=53.5244% N=400 0  0  0 \n",
      "[ultranest] Likelihood function evaluations: 9940\n",
      "[ultranest]   logZ = -51.75 +- 0.07591\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1982.4, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.05 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.08, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.13 bs:0.08 tail:0.01 total:0.08 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "GELD & 2456278.62631951 & 2456278.77405829 & 0.006217346300001895 & 70\n",
      "Fitting individual light curve 3/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5fe969f6f92340cdaadaa843ea63201e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-5e+01   [-54.6896..-54.6896]*| it/evals=5130/8842 eff=60.7676% N=400    00  \n",
      "[ultranest] Likelihood function evaluations: 8869\n",
      "[ultranest]   logZ = -62.91 +- 0.1144\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1889.4, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.45+-0.07 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.11, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.13 bs:0.11 tail:0.01 total:0.11 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "FMAA & 2456588.58200776 & 2456588.82294825 & 0.0043307665958584735 & 115\n",
      "Fitting individual light curve 4/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "362c5f91b03b474694fdc660c06cb4dc",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-6e+01   [-57.5775..-57.5773]*| it/evals=4770/9086 eff=54.9160% N=400 0    \n",
      "[ultranest] Likelihood function evaluations: 9124\n",
      "[ultranest]   logZ = -64.92 +- 0.07818\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1795.8, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.07 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.08, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.12 bs:0.08 tail:0.01 total:0.08 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "FMAA & 2456593.61455323 & 2456593.86241791 & 0.004562492322106042 & 118\n",
      "Fitting individual light curve 5/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "18108e628d4e40b099adb246e7ef7323",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-3e+01   [-26.2282..-26.2281]*| it/evals=4480/8189 eff=57.5170% N=400    0   \n",
      "[ultranest] Likelihood function evaluations: 8214\n",
      "[ultranest]   logZ = -32.8 +- 0.06353\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1925.7, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.06 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.06, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.11 bs:0.06 tail:0.01 total:0.06 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "NKED & 2456646.5530426 & 2456646.77800483 & 0.00882119052120192 & 107\n",
      "Fitting individual light curve 6/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "dadd3cac1c95457fae30c77963071c04",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-3e+01   [-29.8142..-29.8142]*| it/evals=4720/8860 eff=55.7920% N=400 0    \n",
      "[ultranest] Likelihood function evaluations: 8891\n",
      "[ultranest]   logZ = -37.04 +- 0.08242\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1824.7, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.05 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.08, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.12 bs:0.08 tail:0.01 total:0.08 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "FGIC & 2456923.76450991 & 2456923.97702249 & 0.0066185406959523635 & 110\n",
      "Fitting individual light curve 7/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9a7cca1e2d5a4049839e149280cb00e1",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-5e+01   [-50.7308..-50.7308]*| it/evals=4720/10354 eff=47.4181% N=400    0 \n",
      "[ultranest] Likelihood function evaluations: 10397\n",
      "[ultranest]   logZ = -57.9 +- 0.07356\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 2068.2, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.05 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.07, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.12 bs:0.07 tail:0.01 total:0.07 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "FTAC & 2456971.61415274 & 2456971.84946897 & 0.008035683063128095 & 113\n",
      "Fitting individual light curve 8/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "91f8ce67ff6244bfa6628c3bd0c74f1f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-2e+01   [-18.4683..-18.4680]*| it/evals=3854/8313 eff=48.7047% N=400 0  \n",
      "[ultranest] Likelihood function evaluations: 8343\n",
      "[ultranest]   logZ = -23.49 +- 0.06398\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1728.6, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.06 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.06, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.10 bs:0.06 tail:0.01 total:0.06 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "LGEC & 2457611.77477405 & 2457611.9185309 & 0.013559586653548115 & 59\n",
      "Fitting individual light curve 9/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9269acf2711b4788919b5fb0a9714361",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-2e+01   [-24.4574..-24.4574]*| it/evals=1710/2838 eff=70.1395% N=400 \n",
      "[ultranest] Likelihood function evaluations: 2849\n",
      "[ultranest]   logZ = -25.28 +- 0.02375\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1143.5, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.08 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.04, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.03 bs:0.02 tail:0.03 total:0.04 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "LPAC & 2458001.85153712 & 2458001.88843712 & 0.013589240522215556 & 50\n",
      "Fitting individual light curve 10/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9a3c98d9dff049fd8f34a8e19f1dfa89",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-7e+01   [-66.7785..-66.7785]*| it/evals=5120/9067 eff=59.0747% N=400    0  0 \n",
      "[ultranest] Likelihood function evaluations: 9069\n",
      "[ultranest]   logZ = -74.96 +- 0.09854\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1897.3, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.47+-0.08 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.10, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.13 bs:0.10 tail:0.01 total:0.10 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "FMAA & 2458042.59184242 & 2458042.83141636 & 0.006627302054959578 & 112\n",
      "Fitting individual light curve 11/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "45881412f12c4126a096785d040e0100",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-1e+02  .03 [-128.1083..-128.1082]*| it/evals=4960/9400 eff=55.1111% N=400  \n",
      "[ultranest] Likelihood function evaluations: 9471\n",
      "[ultranest]   logZ = -136 +- 0.08806\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1972.5, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.05 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.09, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.13 bs:0.09 tail:0.01 total:0.09 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "NKED & 2458047.63008503 & 2458047.87094161 & 0.009020444082726063 & 115\n",
      "Fitting individual light curve 12/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "14be6efaa0ed40f588702278f4cc7234",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-3e+02  .81 [-342.9871..-342.9870]*| it/evals=6906/14692 eff=48.3207% N=400        0  \n",
      "[ultranest] Likelihood function evaluations: 14715\n",
      "[ultranest]   logZ = -355.6 +- 0.1164\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1856.1, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.45+-0.07 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.12, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.17 bs:0.12 tail:0.01 total:0.12 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "MWAA & 2458095.52685 & 2458095.7064 & 0.004108531537542863 & 400\n",
      "Fitting individual light curve 13/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "071eb40b9e6645cfa17828406f320c52",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-4e+01   [-37.8656..-37.8647]*| it/evals=4880/10710 eff=47.3327% N=400   0  0 \n",
      "[ultranest] Likelihood function evaluations: 10765\n",
      "[ultranest]   logZ = -45.46 +- 0.08792\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1859.8, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.45+-0.08 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.09, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.12 bs:0.09 tail:0.01 total:0.09 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "GDAI & 2458100.54647177 & 2458100.79945865 & 0.007863187077689748 & 121\n",
      "Fitting individual light curve 14/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "86e03f3e74684ac6bf86af5a6fde0273",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-6e+01   [-58.3546..-58.3545]*| it/evals=4591/8597 eff=56.0083% N=400   0 \n",
      "[ultranest] Likelihood function evaluations: 8609\n",
      "[ultranest]   logZ = -65.21 +- 0.09387\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1892.7, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.08 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.09, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.12 bs:0.09 tail:0.01 total:0.09 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "NANF & 2458420.58217494 & 2458420.82285392 & 0.01011593108840008 & 117\n",
      "Fitting individual light curve 15/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5e09015cde8e46ecac2c53ade926b804",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-1e+03  152.24 [-1152.2546..-1152.2545]*| it/evals=4541/6332 eff=76.5509% N=400 \n",
      "[ultranest] Likelihood function evaluations: 6332\n",
      "[ultranest]   logZ = -1159 +- 0.09348\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1658.1, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.05 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.09, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.12 bs:0.09 tail:0.01 total:0.09 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "TESS & 2459871.90968829 & 2459872.46801401 & 0.004216161231437193 & 2354\n",
      "Fitting individual light curve 16/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "55a3a97b80434b3e8b12916ba87a6bfc",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-1e+03  134.16 [-1134.1840..-1134.1839]*| it/evals=4480/6460 eff=73.9274% N=400 \n",
      "[ultranest] Likelihood function evaluations: 6480\n",
      "[ultranest]   logZ = -1141 +- 0.08338\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1651.0, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.08 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.08, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.12 bs:0.08 tail:0.01 total:0.08 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "TESS & 2459854.27009624 & 2459854.82843831 & 0.004208519049932949 & 2326\n",
      "Fitting individual light curve 17/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f401dac3657b423c86c69b262d6b76ce",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-1e+03  053.90 [-1053.9514..-1053.9512]*| it/evals=4498/6698 eff=71.4195% N=400 \n",
      "[ultranest] Likelihood function evaluations: 6715\n",
      "[ultranest]   logZ = -1061 +- 0.08619\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1518.4, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.45+-0.09 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.09, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.12 bs:0.09 tail:0.01 total:0.09 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "TESS & 2459869.38980689 & 2459869.94790426 & 0.004072345004751603 & 2308\n",
      "Fitting individual light curve 18/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "3b52bcc93fdf4818a40eeaab1988174e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-9e+02  .98 [-937.0415..-937.0413]*| it/evals=4470/6774 eff=70.1286% N=400   \n",
      "[ultranest] Likelihood function evaluations: 6793\n",
      "[ultranest]   logZ = -943.6 +- 0.08504\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1685.0, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.45+-0.06 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.09, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.12 bs:0.09 tail:0.01 total:0.09 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "TESS & 2459866.86991163 & 2459867.24514149 & 0.004673444944899993 & 1556\n",
      "Fitting individual light curve 19/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c15b2ffeb73a4434a3b11c61f598b649",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-1e+03  176.46 [-1176.4836..-1176.4836]*| it/evals=4410/6628 eff=70.8092% N=400 \n",
      "[ultranest] Likelihood function evaluations: 6638\n",
      "[ultranest]   logZ = -1183 +- 0.08069\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1613.5, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.45+-0.08 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.08, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.12 bs:0.08 tail:0.01 total:0.08 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "TESS & 2459879.46949212 & 2459880.02781085 & 0.004274972647489715 & 2336\n",
      "Fitting individual light curve 20/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7b3b14313c244d56b68628789b9c1a67",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-1e+03  152.35 [-1152.3924..-1152.3924]*| it/evals=4360/6417 eff=72.4614% N=400 \n",
      "[ultranest] Likelihood function evaluations: 6424\n",
      "[ultranest]   logZ = -1159 +- 0.07411\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1565.7, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.08 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.07, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.12 bs:0.07 tail:0.01 total:0.07 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "TESS & 2459856.79003672 & 2459857.3483754 & 0.004219543783676614 & 2350\n",
      "Fitting individual light curve 21/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "8ae769d21df848c7b6975d63688e8cbf",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-1e+03  124.43 [-1124.4665..-1124.4665]*| it/evals=4520/7366 eff=64.8866% N=400 \n",
      "[ultranest] Likelihood function evaluations: 7367\n",
      "[ultranest]   logZ = -1131 +- 0.09153\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1556.9, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.45+-0.08 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.09, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.12 bs:0.09 tail:0.01 total:0.09 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "TESS & 2459861.82987855 & 2459862.38821214 & 0.004173376049754895 & 2342\n",
      "Fitting individual light curve 22/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "89d41df6d2cd4c11a473b28fa1a08929",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-1e+03  359.20 [-1359.2271..-1359.2270]*| it/evals=4500/6418 eff=74.7757% N=400 \n",
      "[ultranest] Likelihood function evaluations: 6429\n",
      "[ultranest]   logZ = -1366 +- 0.079\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1640.1, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.06 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.08, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.12 bs:0.08 tail:0.01 total:0.08 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "TESS & 2459859.30996341 & 2459859.86829941 & 0.0045832301744472784 & 2350\n",
      "Fitting individual light curve 23/23\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b16e59cbf4d449e89a0d780ccec82ece",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-1e+03  070.61 [-1070.6346..-1070.6346]*| it/evals=4462/6380 eff=74.6154% N=400 \n",
      "[ultranest] Likelihood function evaluations: 6383\n",
      "[ultranest]   logZ = -1077 +- 0.09989\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1611.9, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.07 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.10, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.12 bs:0.10 tail:0.01 total:0.10 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "TESS & 2459876.94964526 & 2459877.50773454 & 0.004080358730437356 & 2334\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "260cdc23b69748468d6f87a7a98551df",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-1e+04  -11189.26 [-11191.0108..-11191.0106]*| it/evals=16797/492677 eff=3.4121% N=400 \n",
      "[ultranest] Likelihood function evaluations: 492677\n",
      "[ultranest]   logZ = -1.123e+04 +- 0.1872\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 4058.4, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.07 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.19, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.28 bs:0.19 tail:0.01 total:0.19 required:<0.50\n",
      "[ultranest] done iterating.\n"
     ]
    }
   ],
   "source": [
    "from exotic.api.elca import glc_fitter\n",
    "\n",
    "# bounds shared by all light curves\n",
    "global_bounds = {\n",
    "    'rprs':(prior['rprs']*0.75, prior['rprs']*1.33),\n",
    "    'inc':(prior['inc']-3, min(90,prior['inc']+3)),\n",
    "    'per':( result.ephemeris['Period']-0.001, \n",
    "            result.ephemeris['Period']+0.001),\n",
    "    'tmid':( result.ephemeris['Tc']-0.02,\n",
    "             result.ephemeris['Tc']+0.02),\n",
    "}\n",
    "\n",
    "global_fit = glc_fitter(input_data, global_bounds, local_bounds, individual_fit=True, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 8800332 characters]",
      "text/plain": [
       "<Figure size 2815.2x2815.2 with 324 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "global_fit.plot_triangle()\n",
    "plt.savefig(\"posteriors.png\",facecolor='white')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 915088 characters]",
      "text/plain": [
       "<Figure size 1080x864 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "global_fit.plot_bestfit(alpha=0.025, bin_dt = 15./60/24, title=\"WASP-1 b Global Fit\",phase_limits='median')\n",
    "plt.savefig(\"bestfit.png\",facecolor='white')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 1303532 characters]",
      "text/plain": [
       "<Figure size 2520x2160 with 24 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "global_fit.plot_bestfits()\n",
    "plt.savefig(\"bestfits.png\",facecolor='white')\n",
    "#myfit.plot_stack(dy=0.03)\n",
    "#plt.savefig(\"stackfit.png\",facecolor='white')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['u0', 'u1', 'u2', 'u3', 'ecc', 'inc', 'omega', 'duration', 'tmid', 'a1', 'a2', 'ars', 'rprs', 'per', 'local_a2_0', 'local_a2_1', 'local_a2_2', 'local_a2_3', 'local_a2_4', 'local_a2_5', 'local_a2_6', 'local_a2_7', 'local_a2_8', 'local_a2_9', 'local_a2_10', 'local_a2_11', 'local_a2_12', 'local_a2_13'])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# best-fit parameters from global fit\n",
    "global_fit.parameters.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['Tc', 'Tcs', 'files', 'notes', 'Period', 'Tc_err', 'epochs', 'Tcs_err', 'datasets', 'nea_tmids', 'residuals', 'timesaved', 'Period_err', 'nea_epochs', 'nea_tmids_err', 'nea_references', 'ephemeris_url'])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# compare global ephemeris to values from EW\n",
    "result.ephemeris.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['2453912.5147133335',\n",
       " '2454461.86099',\n",
       " '2455215.32701',\n",
       " '2455530.3197',\n",
       " '2456543.33866',\n",
       " '2459854.549103']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.ephemeris['nea_tmids']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/kpearson/anaconda3/lib/python3.8/site-packages/exotic/api/nested_linear_fitter.py:49: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.\n",
      "  import pandas.util.testing as tm\n"
     ]
    }
   ],
   "source": [
    "from exotic.api.nested_linear_fitter import linear_fitter\n",
    "\n",
    "# min and max values to search between for fitting the ephemeris\n",
    "bounds = {\n",
    "    'm':[ # orbital period\n",
    "        global_fit.parameters['per']-5*global_fit.errors['per'], \n",
    "        global_fit.parameters['per']+5*global_fit.errors['per']\n",
    "    ], \n",
    "    'b':[ # mid-transit time\n",
    "        global_fit.parameters['tmid']-5*global_fit.errors['tmid'],\n",
    "        global_fit.parameters['tmid']+5*global_fit.errors['tmid']\n",
    "    ] \n",
    "}\n",
    "\n",
    "# used to plot red overlay in O-C figure\n",
    "prior = {\n",
    "    'm':[global_fit.parameters['per'], global_fit.errors['per']],   # value from global\n",
    "    'b':[global_fit.parameters['tmid'], global_fit.errors['tmid']]  # value from WLS\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Sampling 420 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "10a3806bf42e4671b22fdc714d61ed2a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-3e+01  \n",
      "[ultranest] Likelihood function evaluations: 5420\n",
      "[ultranest]   logZ = -38.53 +- 0.05934\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1634.6, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.07 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.06, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.10 bs:0.06 tail:0.01 total:0.06 required:<0.50\n",
      "[ultranest] done iterating.\n"
     ]
    }
   ],
   "source": [
    "# combine the good data from Exoplanet Watch and NASA Exoplanet Archive\n",
    "Tcs = np.array(result.ephemeris['Tcs'] + result.ephemeris['nea_tmids'], dtype=float)\n",
    "Tcs_err = np.array(result.ephemeris['Tcs_err'] + result.ephemeris['nea_tmids_err'], dtype=float)\n",
    "\n",
    "# fit the data and compare ephemeris to global light curve fit\n",
    "lf = linear_fitter( Tcs, Tcs_err, bounds, prior=prior )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "image saved to: posterior.png\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 57012 characters]",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lf.plot_triangle()\n",
    "plt.subplots_adjust(top=0.9,hspace=0.2,wspace=0.2)\n",
    "plt.savefig(\"posterior.png\",facecolor='white')\n",
    "plt.close()\n",
    "print(\"image saved to: posterior.png\")\n",
    "Image(\"posterior.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/kpearson/anaconda3/lib/python3.8/site-packages/exotic/api/nested_linear_fitter.py:173: UserWarning: FixedFormatter should only be used together with FixedLocator\n",
      "  ax2.set_xticklabels(dt)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 48472 characters]",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fig,ax = lf.plot_oc()\n",
    "plt.tight_layout()\n",
    "plt.savefig(\"oc.png\",facecolor='white')\n",
    "plt.close()\n",
    "Image(\"oc.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Sampling 420 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "dba6a1905a344c52a2d7dbe5d050d596",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-3e+01  \n",
      "[ultranest] Likelihood function evaluations: 5427\n",
      "[ultranest]   logZ = -38.54 +- 0.07881\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1609.2, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.07 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.08, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.10 bs:0.08 tail:0.01 total:0.08 required:<0.50\n",
      "[ultranest] done iterating.\n"
     ]
    }
   ],
   "source": [
    "# combine the good data from Exoplanet Watch and NASA Exoplanet Archive\n",
    "Tcs = np.array(result.ephemeris['Tcs'] + result.ephemeris['nea_tmids'], dtype=float)\n",
    "Tcs_err = np.array(result.ephemeris['Tcs_err'] + result.ephemeris['nea_tmids_err'], dtype=float)\n",
    "\n",
    "# fit the data and compare ephemeris to global light curve fit\n",
    "lf = linear_fitter( Tcs, Tcs_err, bounds, prior=prior )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Sampling 420 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "36c670a8e22b40b399877892212405ef",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value=''), GridspecLayout(children=(HTML(value=\"<div style='background-color:#6E6BF4;'>&nb…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ultranest] Explored until L=-6e+01  \n",
      "[ultranest] Likelihood function evaluations: 5796\n",
      "[ultranest]   logZ = -67.82 +- 0.09724\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1679.8, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.06 nat, need <0.50 nat)\n",
      "[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.10, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.10 bs:0.10 tail:0.01 total:0.10 required:<0.50\n",
      "[ultranest] done iterating.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/kpearson/anaconda3/lib/python3.8/site-packages/exotic/api/nested_linear_fitter.py:173: UserWarning: FixedFormatter should only be used together with FixedLocator\n",
      "  ax2.set_xticklabels(dt)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 47232 characters]",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# look at data from individual light curves in global fit\n",
    "tmids = [lcfit['individual']['tmid'] for lcfit in global_fit.lc_data]\n",
    "tmids_err = [lcfit['individual_err']['tmid'] for lcfit in global_fit.lc_data]\n",
    "res_stdev = [lcfit['res_stdev'] for lcfit in global_fit.lc_data]\n",
    "rprs2 = [lcfit['individual']['rprs']**2 for lcfit in global_fit.lc_data]\n",
    "rprs = [lcfit['individual']['rprs'] for lcfit in global_fit.lc_data]\n",
    "rprs_err = [lcfit['individual_err']['rprs'] for lcfit in global_fit.lc_data]\n",
    "name = [lcfit['name'] for lcfit in global_fit.lc_data]\n",
    "\n",
    "# filter bad data\n",
    "mask = (res_stdev < np.mean(rprs2)) & (np.mean(rprs) > 5 * np.mean(rprs_err))\n",
    "# filter based on quality? \n",
    "\n",
    "tmids = np.array(tmids)[mask]\n",
    "tmids_err = np.array(tmids_err)[mask]\n",
    "\n",
    "# combine with nea data\n",
    "tmids = np.array(list(tmids) + result.ephemeris['nea_tmids'], dtype=float)\n",
    "tmids_err = np.array(list(tmids_err) + result.ephemeris['nea_tmids_err'], dtype=float)\n",
    "\n",
    "# fit the data and compare ephemeris to global light curve fit\n",
    "lf = linear_fitter( tmids, tmids_err, bounds, prior=prior )\n",
    "\n",
    "fig,ax = lf.plot_oc()\n",
    "plt.tight_layout()\n",
    "plt.savefig(\"oc_global.png\",facecolor='white')\n",
    "plt.close()\n",
    "Image(\"oc_global.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 206640 characters]",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fig,ax = lf.plot_periodogram()\n",
    "plt.tight_layout()\n",
    "plt.savefig(\"plot_periodogram.png\",facecolor='white')\n",
    "plt.close()\n",
    "Image(\"plot_periodogram.png\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "vscode": {
   "interpreter": {
    "hash": "9bea94c061466e98e3c4227ef91da81db5f711c134da0c4ba8a34c93e3b51d83"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
