{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Fitting photometry with model spectra"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import species\n",
    "from IPython.display import Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Initiating species v0.1.0... [DONE]\n",
      "Creating species_config.ini... [DONE]\n",
      "Creating species_database.hdf5... [DONE]\n",
      "Creating data folder... [DONE]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<species.core.setup.SpeciesInit at 0x12a338320>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "species.SpeciesInit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "database = species.Database()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading DRIFT-PHOENIX model spectra (151 MB)... [DONE]\n",
      "Unpacking DRIFT-PHOENIX model spectra... [DONE]\n",
      "Adding DRIFT-PHOENIX model spectra... [DONE]                    \n"
     ]
    }
   ],
   "source": [
    "database.add_model(model='drift-phoenix',\n",
    "                   wavel_range=None,\n",
    "                   spec_res=None\n",
    "                   teff_range=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding filter: LCO/VisAO.Ys... [DONE]\n",
      "Downloading Vega spectrum (270 kB)... [DONE]\n",
      "Adding Vega spectrum...[DONE]\n",
      "Adding filter: Paranal/NACO.J... [DONE]\n",
      "Adding filter: Gemini/NICI.ED286... [DONE]\n",
      "Adding filter: Paranal/NACO.H... [DONE]\n",
      "Adding filter: Paranal/NACO.Ks... [DONE]\n",
      "Adding filter: Paranal/NACO.NB374... [DONE]\n",
      "Adding filter: Paranal/NACO.Lp... [DONE]\n",
      "Adding filter: Paranal/NACO.NB405... [DONE]\n",
      "Adding filter: Paranal/NACO.Mp... [DONE]\n",
      "Adding object: beta Pic b... [DONE]\n"
     ]
    }
   ],
   "source": [
    "database.add_companion(name='beta Pic b')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Getting object: beta Pic b... [DONE]\n"
     ]
    }
   ],
   "source": [
    "objectbox = database.get_object(object_name='beta Pic b',\n",
    "                                filters=None,\n",
    "                                inc_phot=True,\n",
    "                                inc_spec=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Getting object: beta Pic b... [DONE]\n"
     ]
    }
   ],
   "source": [
    "fit = species.FitModel(object_name='beta Pic b',\n",
    "                       filters=None,\n",
    "                       model='drift-phoenix',\n",
    "                       bounds=None,\n",
    "                       inc_phot=True,\n",
    "                       inc_spec=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running MCMC...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1000/1000 [14:59<00:00,  1.11it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mean acceptance fraction: 0.479\n",
      "Integrated autocorrelation time = [3171.30662571]\n"
     ]
    }
   ],
   "source": [
    "fit.run_mcmc(nwalkers=200,\n",
    "             nsteps=1000,\n",
    "             guess={'teff': 1800, 'logg': None, 'feh': None, 'radius': 1.3},\n",
    "             tag='betapic',\n",
    "             prior=('mass', 13., 3.))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Plotting walkers: walkers.png... [DONE]\n"
     ]
    }
   ],
   "source": [
    "species.plot_walkers(tag='betapic',\n",
    "                     nsteps=None,\n",
    "                     offset=(-0.24, -0.09),\n",
    "                     output='walkers.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 197780 characters]\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image('walkers.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Plotting posteriors: posteriors.png... [DONE]\n"
     ]
    }
   ],
   "source": [
    "species.plot_posteriors(tag='betapic',\n",
    "                        burnin=500,\n",
    "                        offset=(-0.25, -0.25),\n",
    "                        limits=[(1500., 1920.), (3.4, 4.7), (-0.6, 0.3), (1.1, 1.8)],\n",
    "                        output='posteriors.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 217068 characters]\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image('posteriors.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Getting MCMC spectra: 100%|██████████| 30/30 [05:42<00:00, 11.42s/it]\n"
     ]
    }
   ],
   "source": [
    "samples = database.get_mcmc_spectra(tag='betapic',\n",
    "                                    burnin=500,\n",
    "                                    random=30,\n",
    "                                    wavel_range=(0.7, 6.5),\n",
    "                                    spec_res=50.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "median = database.get_median_sample(tag='betapic',\n",
    "                                    burnin=500)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "read_model = species.ReadModel(model='drift-phoenix',\n",
    "                               wavel_range=(0.7, 6.5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "modelbox = read_model.get_model(model_param=median,\n",
    "                                spec_res=50.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "modelbox = species.add_luminosity(modelbox)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calculating synthetic photometry... [DONE]\n",
      "Calculating residuals... [DONE]\n"
     ]
    }
   ],
   "source": [
    "residuals = species.get_residuals(datatype='model',\n",
    "                                  spectrum='drift-phoenix',\n",
    "                                  parameters=median,\n",
    "                                  filters=None,\n",
    "                                  objectbox=objectbox,\n",
    "                                  inc_phot=True,\n",
    "                                  inc_spec=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calculating synthetic photometry... [DONE]\n"
     ]
    }
   ],
   "source": [
    "synphot = species.multi_photometry(datatype='model',\n",
    "                                   spectrum='drift-phoenix',\n",
    "                                   filters=objectbox.filters,\n",
    "                                   parameters=median)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Plotting spectrum: spectrum.png... [DONE]\n"
     ]
    }
   ],
   "source": [
    "species.plot_spectrum(boxes=[samples, modelbox, objectbox, synphot],\n",
    "                      filters=objectbox.filters,\n",
    "                      residuals=residuals,\n",
    "                      colors=('gray', 'tomato', ('black', None), 'black'),\n",
    "                      xlim=(0.7, 6.0),\n",
    "                      ylim=(-1.2e-15, 1.3e-14),\n",
    "                      scale=('linear', 'linear'),\n",
    "                      offset=(-0.25, -0.06),\n",
    "                      output='spectrum.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 61460 characters]\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image('spectrum.png')"
   ]
  }
 ],
 "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.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
