{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Test transit noise estimate\n",
    "\n",
    " Generate white noise in a time series with gaps then measure the noise in a sliding window using both the \"scaled\" and \"minerr\" methods of the transit_noise function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Populating the interactive namespace from numpy and matplotlib\n"
     ]
    }
   ],
   "source": [
    "%pylab inline\n",
    "from pycheops.instrument import transit_noise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "d = 60/86400 # 1-minute cadence\n",
    "\n",
    "time = concatenate((arange(0.0,0.45,d),\n",
    "                    arange(0.5,0.9,d),\n",
    "                    arange(1.0,1.4,d),\n",
    "                    arange(1.5,2.0,d)))\n",
    "sigma_true = (400 + 400*(time/max(time)))*1e-6\n",
    "\n",
    "flux = np.random.normal(1,sigma_true)\n",
    "sigma = sigma_true/2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28852 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x11c1f5908>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "W = 2 # hours\n",
    "T = linspace(0.1, 1.9, 100)\n",
    "Nscaled = empty_like(T)\n",
    "Nminerr = empty_like(T)\n",
    "\n",
    "for i,_t in enumerate(T):\n",
    "    _n,_f = transit_noise(time, flux, sigma, T_0=_t, width=W)\n",
    "    Nscaled[i] = _n\n",
    "    _n = transit_noise(time, flux, sigma, T_0=_t, width=W, method='minerr')\n",
    "    Nminerr[i] = _n\n",
    "\n",
    "fig,ax=subplots(2,1,figsize=(6,4),sharex=True)\n",
    "ax[0].set_xlim(min(time),max(time))\n",
    "ax[0].plot(time, flux,'b.',ms=1)\n",
    "ax[0].set_ylabel(\"Flux \")\n",
    "ax[0].set_ylim(0.997,1.004)\n",
    "ax[0].errorbar(median(T),1.003,xerr=W/24/2,\n",
    "           capsize=5,color='b',ecolor='b')\n",
    "ax[1].plot(T,Nscaled,'b')\n",
    "ax[1].plot(T,Nminerr,'g')\n",
    "ax[1].set_ylabel(\"Transit noise [ppm] \")\n",
    "ax[1].set_xlabel(\"Time\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&copy; Pierre Maxted, Keele University, 2019"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
