{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Atmospheric models"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The DRIFT-PHOENIX model spectra are added to the database. The grid is then interpolated and a spectrum for a given set of parameter values and spectral resolution is computed. The spectrum is then plotted together with several filter profiles."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Getting started"
   ]
  },
  {
   "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.1... [DONE]\n",
      "Creating species_config.ini... [DONE]\n",
      "Database: /Users/tomasstolker/applications/species/docs/tutorials/species_database.hdf5\n",
      "Data folder: /Users/tomasstolker/applications/species/docs/tutorials/data\n",
      "Working folder: /Users/tomasstolker/applications/species/docs/tutorials\n",
      "Creating species_database.hdf5... [DONE]\n",
      "Creating data folder... [DONE]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<species.core.setup.SpeciesInit at 0x13095f710>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "species.SpeciesInit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adding model spectra to the database"
   ]
  },
  {
   "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": "markdown",
   "metadata": {},
   "source": [
    "## Interpolating the model grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "read_model = species.ReadModel(model='drift-phoenix',\n",
    "                               wavel_range=(1., 5.))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_param = {'teff':1510., 'logg':4.1, 'feh':0.1}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "modelbox = read_model.get_model(model_param=model_param,\n",
    "                                spec_res=200.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "filters = ['MKO/NSFCam.J', 'MKO/NSFCam.H', 'MKO/NSFCam.K', 'MKO/NSFCam.Lp', 'MKO/NSFCam.Mp']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding filter: MKO/NSFCam.J... [DONE]\n",
      "Adding filter: MKO/NSFCam.H... [DONE]\n",
      "Adding filter: MKO/NSFCam.K... [DONE]\n",
      "Adding filter: MKO/NSFCam.Lp... [DONE]\n",
      "Adding filter: MKO/NSFCam.Mp... [DONE]\n",
      "Plotting spectrum: model_spectrum.png... [DONE]\n"
     ]
    }
   ],
   "source": [
    "species.plot_spectrum(boxes=[modelbox, ],\n",
    "                      filters=filters,\n",
    "                      offset=(-0.08, -0.06),\n",
    "                      xlim=(1., 5.),\n",
    "                      ylim=(0., 1.1e5),\n",
    "                      legend='upper right',\n",
    "                      output='model_spectrum.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 47328 characters]\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image('model_spectrum.png') "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Extracting a spectrum at a grid point"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is also possible to extract a spectrum without interpolating at one of the grid points. Let's first check what parameter values are available for the DRIFT-PHOENIX models."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'teff': array([1000., 1100., 1200., 1300., 1400., 1500., 1600., 1700., 1800.,\n",
       "        1900., 2000., 2100., 2200., 2300., 2400., 2500., 2600., 2700.,\n",
       "        2800., 2900., 3000.], dtype=float32),\n",
       " 'logg': array([3. , 3.5, 4. , 4.5, 5. , 5.5], dtype=float32),\n",
       " 'feh': array([-0.6, -0.3, -0. ,  0.3], dtype=float32)}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "read_model.get_points()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `get_data` function is used for extracting an spectrum from the discrete parameter grid."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_param = {'teff':1500., 'logg':4., 'feh':0., 'radius':1., 'distance':20.}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "read_model = species.ReadModel(model='drift-phoenix',\n",
    "                               wavel_range=(1., 5.))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "modelbox = read_model.get_data(model_param)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Plotting spectrum: model_spectrum.png... [DONE]\n"
     ]
    }
   ],
   "source": [
    "species.plot_spectrum(boxes=[modelbox, ],\n",
    "                      filters=filters,\n",
    "                      offset=(-0.08, -0.06),\n",
    "                      xlim=(1., 5.),\n",
    "                      ylim=(0., 1.35e-15),\n",
    "                      legend='upper right',\n",
    "                      output='model_spectrum.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 58644 characters]\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image('model_spectrum.png')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Calculating synthetic photometry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding filter: Paranal/NACO.Mp... [DONE]\n"
     ]
    }
   ],
   "source": [
    "read_model = species.ReadModel(model='drift-phoenix',\n",
    "                               filter_name='Paranal/NACO.Mp')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Flux density [W m-2 micron-1] = 3.43e-16\n"
     ]
    }
   ],
   "source": [
    "flux = read_model.get_flux(model_param)\n",
    "print(f'Flux density [W m-2 micron-1] = {flux:.2e}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading Vega spectrum (270 kB)... [DONE]\n",
      "Adding Vega spectrum... [DONE]\n",
      "Apparent magnitude [mag] = 12.00\n",
      "Absolute magnitude [mag] = 10.50\n"
     ]
    }
   ],
   "source": [
    "app_mag, abs_mag = read_model.get_magnitude(model_param)\n",
    "print(f'Apparent magnitude [mag] = {app_mag:.2f}')\n",
    "print(f'Absolute magnitude [mag] = {abs_mag:.2f}')"
   ]
  }
 ],
 "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
}
