{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Spectral library"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Initiating species v0.1.0... [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 0x103bf8f28>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import species\n",
    "species.SpeciesInit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "database = species.Database()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading IRTF Spectral Library - L dwarfs (850 kB)... [DONE]\n",
      "Downloading IRTF Spectral Library - T dwarfs (100 kB)... [DONE]\n",
      "Unpacking IRTF Spectral Library... [DONE]\n",
      "Adding IRTF Spectral Library... 2MASS J05591915-1404489                         "
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/tomasstolker/.pyenv/versions/3.6.0/envs/general3.6/lib/python3.6/site-packages/astroquery/simbad/core.py:138: UserWarning: Warning: The script line number 3 raised an error (recorded in the `errors` attribute of the result table): Identifier not found in the database : 2MASS J05591915-1404489\n",
      "  (error.line, error.msg))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\r",
      "Adding IRTF Spectral Library... SDSS J125453.90-012247.4                        \r",
      "Adding IRTF Spectral Library... [DONE]                                          \n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/tomasstolker/applications/species/species/data/queries.py:209: UserWarning: No parallax was found for 2MASS J05591915-1404489 so storing a NaN value.\n",
      "  warnings.warn(f'No parallax was found for {target} so storing a NaN value.')\n"
     ]
    }
   ],
   "source": [
    "database.add_spectrum(spec_library='irtf',\n",
    "                      sptypes=['L', 'T'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "read_spectrum = species.ReadSpectrum(spec_library='irtf',\n",
    "                                     filter_name=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "specbox = read_spectrum.get_spectrum(sptypes=['L0', 'L1'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding filter: MKO/NSFCam.H... [DONE]\n"
     ]
    }
   ],
   "source": [
    "read_spectrum = species.ReadSpectrum(spec_library='irtf',\n",
    "                                     filter_name='MKO/NSFCam.H')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "photbox = read_spectrum.get_flux(sptypes=['L0', 'L1'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opening SpectrumBox...\n",
      "\n",
      "spectrum = None\n",
      "wavelength = [array([0.8125008, 0.8125008, 0.8127338, ..., 4.126095 , 4.1269007,\n",
      "       4.127706 ], dtype=float32)\n",
      " array([0.93558633, 0.93558633, 0.93585825, ..., 2.416591  , 2.4171278 ,\n",
      "       2.417665  ], dtype=float32)\n",
      " array([0.8106775 , 0.8106775 , 0.81091094, ..., 4.113586  , 4.1143913 ,\n",
      "       4.1151967 ], dtype=float32)]\n",
      "flux = [array([1.8895840e-14, 1.8895840e-14, 2.1758479e-14, ..., 3.7088523e-15,\n",
      "       4.1113438e-15, 4.4284915e-15], dtype=float32)\n",
      " array([-1.0867816e-15, -1.0867816e-15,  2.5373139e-15, ...,\n",
      "        2.0060095e-15,  2.1450817e-15,  2.2615936e-15], dtype=float32)\n",
      " array([3.8497135e-15, 3.8497135e-15, 2.5441878e-15, ..., 1.3200795e-15,\n",
      "       1.2305833e-15, 1.0569799e-15], dtype=float32)]\n",
      "error = None\n",
      "name = [b'2MASS J07464256+2000321' b'2MASS J02081833+2542533'\n",
      " b'2MASS J14392836+1929149']\n",
      "simbad = [b'LSPM J0746+2000' b'2MASSW J0208183+254253' b'2MASS J14392836+1929149']\n",
      "sptype = [b'L0' b'L1' b'L1']\n",
      "distance = [11.60092807 23.65777607 14.36781609]\n",
      "spec_library = irtf\n"
     ]
    }
   ],
   "source": [
    "specbox.open_box()"
   ]
  },
  {
   "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.Ks... [DONE]\n",
      "Plotting spectrum: spectrum.png... [DONE]\n"
     ]
    }
   ],
   "source": [
    "species.plot_spectrum(boxes=[specbox, photbox],\n",
    "                      filters=['MKO/NSFCam.J', 'MKO/NSFCam.H', 'MKO/NSFCam.Ks'],\n",
    "                      xlim=(0.9, 2.5),\n",
    "                      ylim=(0., 8.5e-14),\n",
    "                      offset=(-0.08, -0.05),\n",
    "                      legend=(0.6, 0.75),\n",
    "                      output='spectrum.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 98004 characters]\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.display import Image\n",
    "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
}
