{
 "cells": [
  {
   "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"
   ]
  },
  {
   "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": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded 300 targets\n",
      "['CoRoT-11 b', 'CoRoT-14 b', 'CoRoT-18 b', 'CoRoT-1 b', 'CoRoT-27 b', 'CoRoT-29 b', 'CoRoT-2 b', 'CoRoT-5 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-50 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-22 b', 'HATS-24 b', 'HATS-2 b', 'HATS-33 b', 'HD 149026 b', 'HD 189733 b', 'HD 209458 b', 'HD 80606 b', 'K2-105 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-246 b', 'K2-247 c', 'K2-250 b', 'K2-25 b', 'K2-260 b', 'K2-266 b', 'K2-270 c', 'K2-271 b', '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-343 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-24 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-257 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-3757 b', 'TOI-3807 b', 'TOI-3884 b', 'TOI-4479 b', 'TOI-519 b', 'TOI-532 b', 'TOI-564 b', 'TOI-620 b', 'TOI-969 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-160 B 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-31 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-84 b', 'WASP-85 A b', 'WASP-89 b', 'WASP-92 b', 'WASP-93 b', 'WASP-96 b', 'WASP-98 b', 'Wendelstein-2 b', 'WTS-2 b', 'XO-1 b', 'XO-2 N b', 'XO-3 b', 'XO-4 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": 14,
     "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",
    "result = EW.get('HAT-P-32 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": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'R*': {'units': 'Sol',\n",
       "  'value': '1.22',\n",
       "  'reference': 'Kokori%20et%20al.%202022',\n",
       "  'uncertainty': '0.02'},\n",
       " 'RA': {'units': 'decimal',\n",
       "  'value': '31.0427614',\n",
       "  'reference': None,\n",
       "  'uncertainty': None},\n",
       " 'Rp': {'units': 'R_Earth',\n",
       "  'value': '22.19',\n",
       "  'reference': 'Wang%20et%20al.%202019',\n",
       "  'uncertainty': '0.5'},\n",
       " 'T*': {'units': 'K',\n",
       "  'value': '6207.0',\n",
       "  'reference': 'Kokori%20et%20al.%202022',\n",
       "  'uncertainty': '88.0'},\n",
       " 'Tc': {'units': 'BJD-TDB',\n",
       "  'value': '2456628.505625',\n",
       "  'reference': 'Ivshina%20&amp;%20Winn%202022',\n",
       "  'uncertainty': '7.1e-05'},\n",
       " 'DEC': {'units': 'decimal',\n",
       "  'value': '46.6878512',\n",
       "  'reference': None,\n",
       "  'uncertainty': None},\n",
       " 'Tcs': [{'units': 'BJD, BJD-TDB',\n",
       "   'value': '2454420.4465775',\n",
       "   'reference': 'Bonomo%20et%20al.%202017,%20Zhao%20et%20al.%202014,%20Albrecht%20et%20al.%202012,%20Hartman%20et%20al.%202011',\n",
       "   'uncertainty': '0.0002405'},\n",
       "  {'units': 'HJD',\n",
       "   'value': '2454942.898449',\n",
       "   'reference': 'Gibson%20et%20al.%202013',\n",
       "   'uncertainty': '7.7e-05'},\n",
       "  {'units': 'BJD-TDB',\n",
       "   'value': '2455867.402743',\n",
       "   'reference': 'Wang%20et%20al.%202019',\n",
       "   'uncertainty': '4.9e-05'},\n",
       "  {'units': 'BJD-UTC',\n",
       "   'value': '2456185.602987',\n",
       "   'reference': 'Nortmann%20et%20al.%202016',\n",
       "   'uncertainty': '0.00011'},\n",
       "  {'units': 'BJD-TDB',\n",
       "   'value': '2456209.25393',\n",
       "   'reference': 'Kokori%20et%20al.%202022',\n",
       "   'uncertainty': '5e-05'},\n",
       "  {'units': 'BJD-TDB',\n",
       "   'value': '2456628.505625',\n",
       "   'reference': 'Ivshina%20&amp;%20Winn%202022',\n",
       "   'uncertainty': '7.1e-05'},\n",
       "  {'units': 'BJD',\n",
       "   'value': '2459883.617765',\n",
       "   'reference': 'ExoFOP-TESS%20TOI',\n",
       "   'uncertainty': '0.00014'}],\n",
       " 'ecc': {'units': None,\n",
       "  'value': '0.159',\n",
       "  'reference': 'Wang%20et%20al.%202019',\n",
       "  'uncertainty': '0.051'},\n",
       " 'inc': {'units': 'deg',\n",
       "  'value': '88.98',\n",
       "  'reference': 'Wang%20et%20al.%202019',\n",
       "  'uncertainty': '0.68'},\n",
       " 'FE/H': {'units': None,\n",
       "  'value': '-0.16',\n",
       "  'reference': 'Wang%20et%20al.%202019',\n",
       "  'uncertainty': '0.08'},\n",
       " 'LOGG': {'units': 'Log10(cgs)',\n",
       "  'value': '4.22',\n",
       "  'reference': 'Wang%20et%20al.%202019',\n",
       "  'uncertainty': '0.04'},\n",
       " 'a/R*': {'units': None,\n",
       "  'value': '5.344',\n",
       "  'reference': 'Wang%20et%20al.%202019',\n",
       "  'uncertainty': '0.04'},\n",
       " 'Rp/R*': {'units': None,\n",
       "  'value': '0.1489',\n",
       "  'reference': 'Wang%20et%20al.%202019',\n",
       "  'uncertainty': '0'},\n",
       " 'omega': {'units': 'deg',\n",
       "  'value': '50.0',\n",
       "  'reference': 'Wang%20et%20al.%202019',\n",
       "  'uncertainty': '27.0'},\n",
       " 'Period': {'units': 'day',\n",
       "  'value': '2.1500079',\n",
       "  'reference': 'Ivshina%20&amp;%20Winn%202022',\n",
       "  'uncertainty': '1.4e-07'}}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.priors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "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": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.ephemeris.keys()"
   ]
  },
  {
   "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": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(result.observations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "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": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'u0': 2.5572951033425317,\n",
       " 'u1': -4.601284911007853,\n",
       " 'u2': 4.718635289186434,\n",
       " 'u3': -1.6746454815211127,\n",
       " 'ecc': 0.159,\n",
       " 'inc': 88.98,\n",
       " 'omega': 50.0,\n",
       " 'duration': 0.1284,\n",
       " 'tmid': 2455942.6549,\n",
       " 'a1': 1.0008,\n",
       " 'a2': -0.0006,\n",
       " 'ars': 5.415,\n",
       " 'rprs': 0.146,\n",
       " 'per': 2.1500079}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# print the best-fit parameters\n",
    "result.observations[0].parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "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": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "time, flux, fluxerr, airmass, airmasscorr = result.observations[0].get_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'Rel. Flux')"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 43268 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "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": 23,
   "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": 23,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 48776 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "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\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fit a single lightcurve using EXOTIC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "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": "7956248c9ee64ac1b412ef130a4db789",
       "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+02  .80 [-170.9108..-170.9101]*| it/evals=6816/14262 eff=49.1704% N=400 0 0   \n",
      "[ultranest] Likelihood function evaluations: 14276\n",
      "[ultranest]   logZ = -183.3 +- 0.1288\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1819.3, 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.13, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.17 bs:0.13 tail:0.01 total:0.13 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": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 127660 characters]\n",
      "text/plain": [
       "<Figure size 900x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "myfit.plot_bestfit()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 304200 characters]\n",
      "text/plain": [
       "<Figure size 760x760 with 9 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# show the posterior distributions\n",
    "myfit.plot_triangle()\n",
    "plt.show()"
   ]
  },
  {
   "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": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ZDAB 1 : 2012-01-16T01:16:55.379 (-1867.0462) - 2012-01-16T05:56:45.035 (-1866.9558)\n",
      "NANF 2 : 2017-12-20T01:40:35.687 (-860.0658) - 2017-12-20T08:43:02.923 (-859.9293)\n",
      "FMAA 3 : 2018-11-29T02:38:32.433 (-700.0477) - 2018-11-29T07:47:25.149 (-699.9479)\n",
      "FMAA 4 : 2019-09-13T05:08:49.888 (-566.0462) - 2019-09-13T10:08:50.284 (-565.9493)\n",
      "FMAA 5 : 2020-09-19T03:49:17.259 (-393.0493) - 2020-09-19T08:57:27.461 (-392.9497)\n",
      "PJEF 6 : 2020-10-12T18:58:11.078 (-382.0581) - 2020-10-13T00:20:06.230 (-381.9541)\n",
      "FMAA 7 : 2020-10-17T02:36:44.351 (-380.0495) - 2020-10-17T07:47:23.487 (-379.9492)\n",
      "FMAA 8 : 2021-01-11T02:37:55.217 (-340.0493) - 2021-01-11T07:46:49.746 (-339.9495)\n",
      "FMAA 9 : 2020-11-16T05:00:27.947 (-366.0496) - 2020-11-16T10:08:53.460 (-365.9500)\n",
      "FMAA 10 : 2020-11-29T02:35:34.807 (-360.0499) - 2020-11-29T07:44:27.458 (-359.9502)\n",
      "FMAA 11 : 2020-12-14T03:49:32.964 (-353.0493) - 2020-12-14T08:58:28.340 (-352.9496)\n",
      "FMAA 12 : 2020-12-27T01:26:04.580 (-347.0492) - 2020-12-27T06:33:23.168 (-346.9499)\n",
      "KADB 13 : 2021-02-08T01:59:21.782 (-327.0385) - 2021-02-08T05:25:15.859 (-326.9720)\n",
      "SNIC 14 : 2021-07-27T22:37:09.894 (-248.0344) - 2021-07-28T02:09:58.944 (-247.9656)\n",
      "ASUB 15 : 2021-08-14T02:19:16.630 (-240.0557) - 2021-08-14T08:02:15.350 (-239.9449)\n",
      "KADB 16 : 2021-10-13T07:27:09.187 (-212.0494) - 2021-10-13T11:43:28.992 (-211.9666)\n",
      "FMAA 17 : 2021-11-08T02:40:15.317 (-200.0491) - 2021-11-08T07:47:51.130 (-199.9497)\n",
      "KADB 18 : 2021-11-08T02:48:28.541 (-200.0464) - 2021-11-08T07:47:59.741 (-199.9497)\n",
      "FMAA 19 : 2021-11-10T06:14:53.868 (-199.0495) - 2021-11-10T08:29:49.847 (-199.0059)\n",
      "KADB 20 : 2021-12-06T01:27:00.202 (-187.0495) - 2021-12-06T06:35:09.024 (-186.9500)\n",
      "FMAA 21 : 2021-12-06T01:27:30.032 (-187.0493) - 2021-12-06T06:35:00.404 (-186.9500)\n",
      "KADB 22 : 2021-12-21T02:36:29.002 (-180.0504) - 2021-12-21T07:48:56.678 (-179.9494)\n",
      "KMUA 23 : 2022-09-22T07:27:47.058 (-52.0498) - 2022-09-22T12:00:42.475 (-51.9616)\n",
      "PJEG 24 : 2022-10-18T04:58:34.447 (-40.0050) - 2022-10-18T08:04:22.346 (-39.9450)\n",
      "OAS 25 : 2022-11-02T03:55:00.000 (-33.0488) - 2022-11-02T08:59:50.331 (-32.9504)\n",
      "OAS 26 : 2022-11-30T02:41:26.502 (-20.0494) - 2022-11-30T07:47:21.338 (-19.9506)\n",
      "FMAA 27 : 2022-12-15T03:52:28.085 (-13.0497) - 2022-12-15T09:01:26.169 (-12.9499)\n",
      "SAJB 28 : 2022-12-23T18:09:06.134 (-9.0521) - 2022-12-23T23:34:56.726 (-8.9469)\n",
      "OAS 29 : 2023-01-12T02:40:47.764 (-0.0497) - 2023-01-12T07:55:41.953 (0.0521)\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",
    "        try:\n",
    "            time, flux, fluxerr, airmass = obs.get_data()\n",
    "        except:\n",
    "            time, flux, fluxerr, airmass, dummy1, dummy2 = 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": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PHOENIX models are only computed for solar metallicity stars. Setting stellar_metallicity = 0.\n",
      "[]\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": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting individual light curve 1/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b14bf2639c5544ae81b692bea1a08268",
       "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+02  .80 [-170.9299..-170.9297]*| it/evals=6760/13663 eff=50.9689% N=400   0  0 \n",
      "[ultranest] Likelihood function evaluations: 13666\n",
      "[ultranest]   logZ = -183.2 +- 0.1191\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1930.9, 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.12, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.16 bs:0.12 tail:0.01 total:0.12 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "ZDAB & 2455942.55341874 & 2455942.74774346 & 0.0065153780876894465 & 410\n",
      "Fitting individual light curve 2/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "385596ecc2674e82831beeb973f1939d",
       "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   [-67.1945..-67.1943]*| it/evals=6030/13621 eff=45.6093% N=400        0  \n",
      "[ultranest] Likelihood function evaluations: 13634\n",
      "[ultranest]   logZ = -77.66 +- 0.1107\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1823.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.11, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.15 bs:0.11 tail:0.01 total:0.11 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "NANF & 2458107.56985749 & 2458107.86322827 & 0.005319972419416692 & 141\n",
      "Fitting individual light curve 3/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1b7b144558b84873ac0d1cc8fa4c292b",
       "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.2365..-50.2364]*| it/evals=5120/9284 eff=57.6317% N=400     \n",
      "[ultranest] Likelihood function evaluations: 9295\n",
      "[ultranest]   logZ = -58.41 +- 0.08339\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1931.3, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.09 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",
      "FMAA & 2458451.6100976 & 2458451.82459663 & 0.006614563414002086 & 103\n",
      "Fitting individual light curve 4/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d8a691c99ad648d49e05e888889775e6",
       "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   [-73.8135..-73.8134]*| it/evals=6080/11394 eff=55.3029% N=400   0 0   \n",
      "[ultranest] Likelihood function evaluations: 11430\n",
      "[ultranest]   logZ = -84.39 +- 0.1023\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1867.7, 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.10, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.15 bs:0.10 tail:0.01 total:0.10 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "FMAA & 2458739.7144663 & 2458739.92280421 & 0.006945359445285461 & 89\n",
      "Fitting individual light curve 5/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e904b58e480840659ab4873bda04c04d",
       "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   [-47.8562..-47.8562]*| it/evals=5800/10841 eff=55.5502% N=400        0 \n",
      "[ultranest] Likelihood function evaluations: 10924\n",
      "[ultranest]   logZ = -57.82 +- 0.09364\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1953.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.09, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.14 bs:0.09 tail:0.01 total:0.09 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "FMAA & 2459111.65922753 & 2459111.8732345 & 0.008291066648781589 & 101\n",
      "Fitting individual light curve 6/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ca2355f56c404d07ba8449c91506cd48",
       "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+02  .54 [-192.8050..-192.8049]*| it/evals=7920/21827 eff=36.9627% N=400         \n",
      "[ultranest] Likelihood function evaluations: 21849\n",
      "[ultranest]   logZ = -207.9 +- 0.144\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 2125.7, 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.14, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.18 bs:0.14 tail:0.01 total:0.14 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "PJEF & 2459135.290406 & 2459135.513961 & 0.0028274747496078866 & 91\n",
      "Fitting individual light curve 7/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5213f8b2ed1543cfb4e37dfe78ee789e",
       "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   [-15.6846..-15.6842]*| it/evals=4440/8562 eff=54.3984% N=400     \n",
      "[ultranest] Likelihood function evaluations: 8568\n",
      "[ultranest]   logZ = -22.14 +- 0.09265\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1772.3, 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.09, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.11 bs:0.09 tail:0.01 total:0.09 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "FMAA & 2459139.60884665 & 2459139.8245774 & 0.009003213623168766 & 104\n",
      "Fitting individual light curve 8/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "36cf869f68ae4c5db8e80fab3d6c0db4",
       "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.6522..-29.6521]*| it/evals=5640/10083 eff=58.2464% N=400     0   \n",
      "[ultranest] Likelihood function evaluations: 10139\n",
      "[ultranest]   logZ = -39.16 +- 0.1139\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1898.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.11, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.14 bs:0.11 tail:0.01 total:0.11 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "FMAA & 2459225.60966686 & 2459225.82418687 & 0.006529301262071957 & 103\n",
      "Fitting individual light curve 9/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b36ae1186e9e4dd0ae83b21dc7095c19",
       "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+01   [-14.6054..-14.6053]*| it/evals=4560/9153 eff=52.0964% N=400   0 \n",
      "[ultranest] Likelihood function evaluations: 9205\n",
      "[ultranest]   logZ = -21.39 +- 0.07722\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1821.9, 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",
      "FMAA & 2459169.70865679 & 2459169.92284097 & 0.0034956293933783287 & 101\n",
      "Fitting individual light curve 10/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6a90b5092e974bb7bca87af9299f481f",
       "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+01   [-11.9181..-11.9180]*| it/evals=3780/7331 eff=54.5376% N=400 \n",
      "[ultranest] Likelihood function evaluations: 7333\n",
      "[ultranest]   logZ = -16.73 +- 0.07204\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1805.8, 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.09 bs:0.07 tail:0.01 total:0.07 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "FMAA & 2459182.60804175 & 2459182.82254002 & 0.0022676965647036003 & 102\n",
      "Fitting individual light curve 11/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "197cd71a8d5f4548b65c4a0bc8dea740",
       "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   [-36.3652..-36.3652]*| it/evals=5780/11370 eff=52.6892% N=400   0  0 \n",
      "[ultranest] Likelihood function evaluations: 11405\n",
      "[ultranest]   logZ = -46.21 +- 0.09786\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1848.6, 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.10, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.15 bs:0.10 tail:0.01 total:0.10 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "FMAA & 2459197.6594093 & 2459197.87393912 & 0.0059518995547027855 & 99\n",
      "Fitting individual light curve 12/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7e526e590ebf4c9b949c3bef1e842190",
       "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   [-40.4334..-40.4332]*| it/evals=5480/10810 eff=52.6417% N=400    \n",
      "[ultranest] Likelihood function evaluations: 10848\n",
      "[ultranest]   logZ = -49.53 +- 0.106\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1976.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.11, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.14 bs:0.11 tail:0.01 total:0.11 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "FMAA & 2459210.55977523 & 2459210.77318482 & 0.008387073442544184 & 103\n",
      "Fitting individual light curve 13/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "fa78c8bc2ccb4033b0ecbbffe4104e37",
       "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+02  .38 [-159.5328..-159.5328]*| it/evals=6480/13714 eff=48.6706% N=400   00  \n",
      "[ultranest] Likelihood function evaluations: 13764\n",
      "[ultranest]   logZ = -171.1 +- 0.1054\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1889.5, 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.16 bs:0.11 tail:0.01 total:0.11 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "KADB & 2459253.582891 & 2459253.725878 & 0.0054401462614219595 & 69\n",
      "Fitting individual light curve 14/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "60066a391ddd4c2ba7dfdef69620b5c7",
       "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   [-52.3210..-52.3207]*| it/evals=5160/10085 eff=53.2783% N=400   0  0 \n",
      "[ultranest] Likelihood function evaluations: 10124\n",
      "[ultranest]   logZ = -60.64 +- 0.1001\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1984.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.13 bs:0.10 tail:0.01 total:0.10 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "SNIC & 2459423.44247562 & 2459423.59026555 & 0.009923152165898492 & 127\n",
      "Fitting individual light curve 15/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4d5ae100dcae403298d0ff00a476bbb0",
       "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+02  .73 [-173.8785..-173.8783]*| it/evals=6520/12848 eff=52.3779% N=400   0  0 \n",
      "[ultranest] Likelihood function evaluations: 12888\n",
      "[ultranest]   logZ = -185.5 +- 0.1123\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1890.9, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.09 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.16 bs:0.11 tail:0.01 total:0.11 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "ASUB & 2459440.59672026 & 2459440.83489988 & 0.009354747348972008 & 438\n",
      "Fitting individual light curve 16/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7122b61d37c94ce19b68e03e9e9ae5e6",
       "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+02  .25 [-389.3417..-389.3414]*| it/evals=6800/12418 eff=56.5818% N=400     \n",
      "[ultranest] Likelihood function evaluations: 12421\n",
      "[ultranest]   logZ = -401.7 +- 0.1332\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1919.1, 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.13, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.16 bs:0.13 tail:0.01 total:0.13 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "KADB & 2459500.810523 & 2459500.98853 & 0.007553728194097562 & 87\n",
      "Fitting individual light curve 17/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c0aa59d4df6f43319efccc8af274cfa2",
       "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   [-23.4644..-23.4642]*| it/evals=4668/8640 eff=56.6505% N=400   0 \n",
      "[ultranest] Likelihood function evaluations: 8642\n",
      "[ultranest]   logZ = -30.48 +- 0.09446\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1863.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",
      "FMAA & 2459526.61128839 & 2459526.82489734 & 0.009870446176612558 & 98\n",
      "Fitting individual light curve 18/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d1490f9afa54405a9eff8c8138561356",
       "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+02  .01 [-241.1879..-241.1876]*| it/evals=6800/15661 eff=44.5580% N=400     \n",
      "[ultranest] Likelihood function evaluations: 15686\n",
      "[ultranest]   logZ = -253.6 +- 0.1331\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 2181.3, 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.13, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.16 bs:0.13 tail:0.01 total:0.13 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "KADB & 2459526.616997 & 2459526.824997 & 0.007728261915994505 & 98\n",
      "Fitting individual light curve 19/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "3004037fafed4ae29fef117b5d68bafe",
       "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   [-30.1910..-30.1909]*| it/evals=4590/8788 eff=54.7210% N=400  \n",
      "[ultranest] Likelihood function evaluations: 8800\n",
      "[ultranest]   logZ = -37.08 +- 0.09623\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1861.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.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",
      "FMAA & 2459528.76034569 & 2459528.85404916 & 0.0067473948182962575 & 45\n",
      "Fitting individual light curve 20/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "8bc1345331344113a329998474916b92",
       "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+02  .99 [-481.0412..-481.0411]*| it/evals=6440/11467 eff=58.1910% N=400  0 \n",
      "[ultranest] Likelihood function evaluations: 11506\n",
      "[ultranest]   logZ = -492.5 +- 0.1327\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1920.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.13, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.16 bs:0.13 tail:0.01 total:0.13 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "KADB & 2459554.560419 & 2459554.77441 & 0.007937641112090594 & 104\n",
      "Fitting individual light curve 21/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "19688e86bbb74981b03e3fe05da76f8b",
       "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   [-39.7636..-39.7635]*| it/evals=5040/11600 eff=45.0000% N=400   \n",
      "[ultranest] Likelihood function evaluations: 11629\n",
      "[ultranest]   logZ = -47.76 +- 0.09822\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1928.0, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.45+-0.11 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 & 2459554.56076426 & 2459554.77431023 & 0.008206043455050358 & 103\n",
      "Fitting individual light curve 22/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5ce517329a4d4205b03efdcabfbcd624",
       "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  .69 [-134.8502..-134.8497]*| it/evals=6680/13829 eff=49.7431% N=400  0 \n",
      "[ultranest] Likelihood function evaluations: 13884\n",
      "[ultranest]   logZ = -147 +- 0.08276\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 2086.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.16 bs:0.08 tail:0.01 total:0.08 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "KADB & 2459569.608669 & 2459569.825656 & 0.009159144205246732 & 84\n",
      "Fitting individual light curve 23/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e0054a557ee146ceb22abbd4a24ff74a",
       "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.5268..-37.5267]*| it/evals=4800/9778 eff=51.1836% N=400   \n",
      "[ultranest] Likelihood function evaluations: 9850\n",
      "[ultranest]   logZ = -44.92 +- 0.07667\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1997.0, 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.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",
      "KMUA & 2459844.81096132 & 2459845.00049161 & 0.009408924141271868 & 74\n",
      "Fitting individual light curve 24/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "dec14719ee484c19be4885b3c09a6899",
       "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   [-19.5735..-19.5735]*| it/evals=4320/8229 eff=55.1795% N=400   \n",
      "[ultranest] Likelihood function evaluations: 8255\n",
      "[ultranest]   logZ = -25.78 +- 0.09084\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1834.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.09, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.11 bs:0.09 tail:0.01 total:0.09 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "PJEG & 2459870.70734314 & 2459870.83636975 & 0.008132500368447749 & 37\n",
      "Fitting individual light curve 25/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2ee45ce0aa5f40b787e7d28376b50a52",
       "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   [-48.4437..-48.4435]*| it/evals=5360/10881 eff=51.1402% N=400    \n",
      "[ultranest] Likelihood function evaluations: 10890\n",
      "[ultranest]   logZ = -57.22 +- 0.08033\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1850.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.08, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.14 bs:0.08 tail:0.01 total:0.08 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "OAS & 2459885.66319444 & 2459885.87488809 & 0.004921688665700539 & 101\n",
      "Fitting individual light curve 26/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "048f9de535f248bc8f70940186eb33b8",
       "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   [-49.7940..-49.7939]*| it/evals=5560/10319 eff=56.0540% N=400     \n",
      "[ultranest] Likelihood function evaluations: 10361\n",
      "[ultranest]   logZ = -59.08 +- 0.1101\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1891.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.11, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.14 bs:0.11 tail:0.01 total:0.11 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "OAS & 2459913.61211229 & 2459913.82455252 & 0.004365884333753524 & 103\n",
      "Fitting individual light curve 27/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "05db61c353324d8497278ea7c74fe74a",
       "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.0887..-54.0884]*| it/evals=5800/12575 eff=47.6386% N=400   0  0 \n",
      "[ultranest] Likelihood function evaluations: 12592\n",
      "[ultranest]   logZ = -63.96 +- 0.09995\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1876.6, need >400)\n",
      "[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.09 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.14 bs:0.10 tail:0.01 total:0.10 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "FMAA & 2459928.66143617 & 2459928.87599733 & 0.007423180354873883 & 103\n",
      "Fitting individual light curve 28/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "121af3b4dbfa48eca3730e557c49432c",
       "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+02  .32 [-158.3950..-158.3949]*| it/evals=7440/13221 eff=58.0298% N=400   0  \n",
      "[ultranest] Likelihood function evaluations: 13249\n",
      "[ultranest]   logZ = -172.4 +- 0.09188\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1911.1, 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.09, need <0.5)\n",
      "[ultranest]   logZ error budget: single: 0.18 bs:0.09 tail:0.01 total:0.09 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "SAJB & 2459937.256321 & 2459937.482601 & 0.0020872489852802084 & 232\n",
      "Fitting individual light curve 29/29\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "72e8496a51da43aebec4e715daecee01",
       "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   [-21.2994..-21.2994]*| it/evals=4320/8117 eff=55.9803% N=400  0   \n",
      "[ultranest] Likelihood function evaluations: 8178\n",
      "[ultranest]   logZ = -27.54 +- 0.07385\n",
      "[ultranest] Effective samples strategy satisfied (ESS = 1822.6, 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.11 bs:0.07 tail:0.01 total:0.07 required:<0.50\n",
      "[ultranest] done iterating.\n",
      "OAS & 2459956.61166393 & 2459956.83034668 & 0.019306470186350202 & 97\n",
      "[ultranest] Sampling 400 live points from prior ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ccda7fd0cbdd40c2a62535b53f23fd9b",
       "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": [
      "Z=-3947.0(0.00%) | Like=-3932.89..-3483.19 [-4070.3814..-3911.4280] | it/evals=3557/32442 eff=11.1011% N=400     \r"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ultranest/integrator.py:1756: UserWarning: Sampling from region seems inefficient (0/40 accepted in iteration 2500). To improve efficiency, modify the transformation so that the current live points are ellipsoidal, or use a stepsampler, or set frac_remain to a lower number (e.g., 0.5) to terminate earlier.\n",
      "  warnings.warn(warning_message)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z=-3446.1(0.00%) | Like=-3425.96..-3355.10 [-3434.3031..-3422.5328] | it/evals=6922/223595 eff=3.1013% N=400 \r"
     ]
    }
   ],
   "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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "global_fit.plot_triangle()\n",
    "plt.savefig(\"posteriors.png\",facecolor='white')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# best-fit parameters from global fit\n",
    "global_fit.parameters.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# compare global ephemeris to values from EW\n",
    "result.ephemeris.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result.ephemeris['nea_tmids']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": "Python 3 (ipykernel)",
   "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.10"
  },
  "vscode": {
   "interpreter": {
    "hash": "9bea94c061466e98e3c4227ef91da81db5f711c134da0c4ba8a34c93e3b51d83"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
