{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. _to_hdf_notebook:\n",
    "\n",
    "********************\n",
    "Example to_hdf calls\n",
    "********************"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Initialize the simulation with the `tardis_example.yml` configuration file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[\u001b[1mtardis.plasma.standard_plasmas\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  Reading Atomic Data from tardis_example/kurucz_cd23_chianti_H_He.h5 (\u001b[1mstandard_plasmas.py\u001b[0m:72)\n",
      "tardis.plasma.standard_plasmas - \u001b[1;37mINFO\u001b[0m - Reading Atomic Data from tardis_example/kurucz_cd23_chianti_H_He.h5\n",
      "tardis.atomic - INFO - Read Atom Data with UUID=5ca3035ca8b311e3bb684437e69d75d7 and MD5=21095dd25faa1683f4c90c911a00c3f8\n",
      "[\u001b[1mtardis.plasma.base  \u001b[0m][\u001b[1;34mDEBUG\u001b[0m  ]  Updating modules in the following order: (\u001b[1mbase.py\u001b[0m:197)\n",
      "tardis.plasma.base - \u001b[1;34mDEBUG\u001b[0m - Updating modules in the following order:\n",
      "[\u001b[1mtardis.montecarlo.base\u001b[0m][\u001b[1;34mDEBUG\u001b[0m  ]  Electron scattering switched on (\u001b[1mbase.py\u001b[0m:393)\n",
      "tardis.montecarlo.base - \u001b[1;34mDEBUG\u001b[0m - Electron scattering switched on\n",
      "[\u001b[1mpy.warnings         \u001b[0m][\u001b[1;33mWARNING\u001b[0m]  /home/vaibhav/anaconda2/lib/python2.7/site-packages/astropy/units/equivalencies.py:74: RuntimeWarning: divide by zero encountered in double_scalars\n",
      "  (si.m, si.Hz, lambda x: _si.c.value / x),\n",
      " (\u001b[1mequivalencies.py\u001b[0m:74)\n",
      "py.warnings - \u001b[1;33mWARNING\u001b[0m - /home/vaibhav/anaconda2/lib/python2.7/site-packages/astropy/units/equivalencies.py:74: RuntimeWarning: divide by zero encountered in double_scalars\n",
      "  (si.m, si.Hz, lambda x: _si.c.value / x),\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from tardis.io.config_reader import Configuration\n",
    "from tardis.simulation import Simulation\n",
    "\n",
    "# Must have the tardis_example folder in the working directory.\n",
    "config_fname = 'tardis_example/tardis_example.yml'\n",
    "config = Configuration.from_yaml(config_fname)\n",
    "\n",
    "simulation = Simulation.from_config(config)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the simulation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[\u001b[1mtardis.simulation.base\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  Starting iteration 1/5 (\u001b[1mbase.py\u001b[0m:196)\n",
      "tardis.simulation.base - \u001b[1;37mINFO\u001b[0m - Starting iteration 1/5\n",
      "[\u001b[1mtardis.simulation.base\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  Luminosity emitted = 8.10360e+42 erg / s Luminosity absorbed = 2.67587e+42 erg / s Luminosity requested = 1.07688e+43 erg / s (\u001b[1mbase.py\u001b[0m:273)\n",
      "tardis.simulation.base - \u001b[1;37mINFO\u001b[0m - Luminosity emitted = 8.10360e+42 erg / s Luminosity absorbed = 2.67587e+42 erg / s Luminosity requested = 1.07688e+43 erg / s\n",
      "[\u001b[1mtardis.simulation.base\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  Plasma stratification:\n",
      "\t             t_rad    next_t_rad         w    next_w\n",
      "\tShell                                               \n",
      "\t0      9967.488442  10074.800724  0.400392  0.452516\n",
      "\t5      9893.293062  10103.998786  0.211205  0.202916\n",
      "\t10     9820.194102  10007.439423  0.142695  0.130205\n",
      "\t15     9748.167438   9820.947309  0.104556  0.096257\n",
      "\n",
      " (\u001b[1mbase.py\u001b[0m:264)\n",
      "tardis.simulation.base - \u001b[1;37mINFO\u001b[0m - Plasma stratification:\n",
      "\t             t_rad    next_t_rad         w    next_w\n",
      "\tShell                                               \n",
      "\t0      9967.488442  10074.800724  0.400392  0.452516\n",
      "\t5      9893.293062  10103.998786  0.211205  0.202916\n",
      "\t10     9820.194102  10007.439423  0.142695  0.130205\n",
      "\t15     9748.167438   9820.947309  0.104556  0.096257\n",
      "\n",
      "\n",
      "[\u001b[1mtardis.simulation.base\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  t_inner 9974.969 K -- next t_inner 10736.934 K (\u001b[1mbase.py\u001b[0m:266)\n",
      "tardis.simulation.base - \u001b[1;37mINFO\u001b[0m - t_inner 9974.969 K -- next t_inner 10736.934 K\n",
      "[\u001b[1mtardis.plasma.base  \u001b[0m][\u001b[1;34mDEBUG\u001b[0m  ]  Updating modules in the following order: (\u001b[1mbase.py\u001b[0m:197)\n",
      "tardis.plasma.base - \u001b[1;34mDEBUG\u001b[0m - Updating modules in the following order:\n",
      "[\u001b[1mtardis.simulation.base\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  Starting iteration 2/5 (\u001b[1mbase.py\u001b[0m:196)\n",
      "tardis.simulation.base - \u001b[1;37mINFO\u001b[0m - Starting iteration 2/5\n",
      "[\u001b[1mtardis.simulation.base\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  Luminosity emitted = 1.08633e+43 erg / s Luminosity absorbed = 3.60272e+42 erg / s Luminosity requested = 1.07688e+43 erg / s (\u001b[1mbase.py\u001b[0m:273)\n",
      "tardis.simulation.base - \u001b[1;37mINFO\u001b[0m - Luminosity emitted = 1.08633e+43 erg / s Luminosity absorbed = 3.60272e+42 erg / s Luminosity requested = 1.07688e+43 erg / s\n",
      "[\u001b[1mtardis.simulation.base\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  Plasma stratification:\n",
      "\t              t_rad    next_t_rad         w    next_w\n",
      "\tShell                                                \n",
      "\t0      10074.800724  10480.642737  0.452516  0.485511\n",
      "\t5      10103.998786  10542.429569  0.202916  0.203942\n",
      "\t10     10007.439423  10413.108276  0.130205  0.127795\n",
      "\t15      9820.947309  10179.669303  0.096257  0.094281\n",
      "\n",
      " (\u001b[1mbase.py\u001b[0m:264)\n",
      "tardis.simulation.base - \u001b[1;37mINFO\u001b[0m - Plasma stratification:\n",
      "\t              t_rad    next_t_rad         w    next_w\n",
      "\tShell                                                \n",
      "\t0      10074.800724  10480.642737  0.452516  0.485511\n",
      "\t5      10103.998786  10542.429569  0.202916  0.203942\n",
      "\t10     10007.439423  10413.108276  0.130205  0.127795\n",
      "\t15      9820.947309  10179.669303  0.096257  0.094281\n",
      "\n",
      "\n",
      "[\u001b[1mtardis.simulation.base\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  t_inner 10736.934 K -- next t_inner 10713.534 K (\u001b[1mbase.py\u001b[0m:266)\n",
      "tardis.simulation.base - \u001b[1;37mINFO\u001b[0m - t_inner 10736.934 K -- next t_inner 10713.534 K\n",
      "[\u001b[1mtardis.plasma.base  \u001b[0m][\u001b[1;34mDEBUG\u001b[0m  ]  Updating modules in the following order: (\u001b[1mbase.py\u001b[0m:197)\n",
      "tardis.plasma.base - \u001b[1;34mDEBUG\u001b[0m - Updating modules in the following order:\n",
      "[\u001b[1mtardis.simulation.base\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  Starting iteration 3/5 (\u001b[1mbase.py\u001b[0m:196)\n",
      "tardis.simulation.base - \u001b[1;37mINFO\u001b[0m - Starting iteration 3/5\n",
      "[\u001b[1mtardis.simulation.base\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  Luminosity emitted = 1.08260e+43 erg / s Luminosity absorbed = 3.52022e+42 erg / s Luminosity requested = 1.07688e+43 erg / s (\u001b[1mbase.py\u001b[0m:273)\n",
      "tardis.simulation.base - \u001b[1;37mINFO\u001b[0m - Luminosity emitted = 1.08260e+43 erg / s Luminosity absorbed = 3.52022e+42 erg / s Luminosity requested = 1.07688e+43 erg / s\n",
      "[\u001b[1mtardis.simulation.base\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  Plasma stratification:\n",
      "\t              t_rad    next_t_rad         w    next_w\n",
      "\tShell                                                \n",
      "\t0      10480.642737  10711.113330  0.485511  0.493541\n",
      "\t5      10542.429569  10819.470987  0.203942  0.199112\n",
      "\t10     10413.108276  10633.892704  0.127795  0.125270\n",
      "\t15     10179.669303  10359.259776  0.094281  0.092971\n",
      "\n",
      " (\u001b[1mbase.py\u001b[0m:264)\n",
      "tardis.simulation.base - \u001b[1;37mINFO\u001b[0m - Plasma stratification:\n",
      "\t              t_rad    next_t_rad         w    next_w\n",
      "\tShell                                                \n",
      "\t0      10480.642737  10711.113330  0.485511  0.493541\n",
      "\t5      10542.429569  10819.470987  0.203942  0.199112\n",
      "\t10     10413.108276  10633.892704  0.127795  0.125270\n",
      "\t15     10179.669303  10359.259776  0.094281  0.092971\n",
      "\n",
      "\n",
      "[\u001b[1mtardis.simulation.base\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  t_inner 10713.534 K -- next t_inner 10699.352 K (\u001b[1mbase.py\u001b[0m:266)\n",
      "tardis.simulation.base - \u001b[1;37mINFO\u001b[0m - t_inner 10713.534 K -- next t_inner 10699.352 K\n",
      "[\u001b[1mtardis.plasma.base  \u001b[0m][\u001b[1;34mDEBUG\u001b[0m  ]  Updating modules in the following order: (\u001b[1mbase.py\u001b[0m:197)\n",
      "tardis.plasma.base - \u001b[1;34mDEBUG\u001b[0m - Updating modules in the following order:\n",
      "[\u001b[1mtardis.simulation.base\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  Starting iteration 4/5 (\u001b[1mbase.py\u001b[0m:196)\n",
      "tardis.simulation.base - \u001b[1;37mINFO\u001b[0m - Starting iteration 4/5\n",
      "[\u001b[1mtardis.simulation.base\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  Luminosity emitted = 1.07886e+43 erg / s Luminosity absorbed = 3.48159e+42 erg / s Luminosity requested = 1.07688e+43 erg / s (\u001b[1mbase.py\u001b[0m:273)\n",
      "tardis.simulation.base - \u001b[1;37mINFO\u001b[0m - Luminosity emitted = 1.07886e+43 erg / s Luminosity absorbed = 3.48159e+42 erg / s Luminosity requested = 1.07688e+43 erg / s\n",
      "[\u001b[1mtardis.simulation.base\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  Plasma stratification:\n",
      "\t              t_rad    next_t_rad         w    next_w\n",
      "\tShell                                                \n",
      "\t0      10711.113330  10866.508115  0.493541  0.489288\n",
      "\t5      10819.470987  11017.147737  0.199112  0.192830\n",
      "\t10     10633.892704  10795.422555  0.125270  0.121775\n",
      "\t15     10359.259776  10499.976778  0.092971  0.090524\n",
      "\n",
      " (\u001b[1mbase.py\u001b[0m:264)\n",
      "tardis.simulation.base - \u001b[1;37mINFO\u001b[0m - Plasma stratification:\n",
      "\t              t_rad    next_t_rad         w    next_w\n",
      "\tShell                                                \n",
      "\t0      10711.113330  10866.508115  0.493541  0.489288\n",
      "\t5      10819.470987  11017.147737  0.199112  0.192830\n",
      "\t10     10633.892704  10795.422555  0.125270  0.121775\n",
      "\t15     10359.259776  10499.976778  0.092971  0.090524\n",
      "\n",
      "\n",
      "[\u001b[1mtardis.simulation.base\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  t_inner 10699.352 K -- next t_inner 10694.430 K (\u001b[1mbase.py\u001b[0m:266)\n",
      "tardis.simulation.base - \u001b[1;37mINFO\u001b[0m - t_inner 10699.352 K -- next t_inner 10694.430 K\n",
      "[\u001b[1mtardis.plasma.base  \u001b[0m][\u001b[1;34mDEBUG\u001b[0m  ]  Updating modules in the following order: (\u001b[1mbase.py\u001b[0m:197)\n",
      "tardis.plasma.base - \u001b[1;34mDEBUG\u001b[0m - Updating modules in the following order:\n",
      "[\u001b[1mtardis.simulation.base\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  Starting iteration 5/5 (\u001b[1mbase.py\u001b[0m:196)\n",
      "tardis.simulation.base - \u001b[1;37mINFO\u001b[0m - Starting iteration 5/5\n",
      "[\u001b[1mtardis.simulation.base\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  Luminosity emitted = 1.07897e+43 erg / s Luminosity absorbed = 3.45406e+42 erg / s Luminosity requested = 1.07688e+43 erg / s (\u001b[1mbase.py\u001b[0m:273)\n",
      "tardis.simulation.base - \u001b[1;37mINFO\u001b[0m - Luminosity emitted = 1.07897e+43 erg / s Luminosity absorbed = 3.45406e+42 erg / s Luminosity requested = 1.07688e+43 erg / s\n",
      "[\u001b[1mtardis.simulation.base\u001b[0m][\u001b[1;37mINFO\u001b[0m   ]  Simulation finished in 5 iterations and took 50.01 s (\u001b[1mbase.py\u001b[0m:223)\n",
      "tardis.simulation.base - \u001b[1;37mINFO\u001b[0m - Simulation finished in 5 iterations and took 50.01 s\n"
     ]
    }
   ],
   "source": [
    "simulation.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can now use `to_hdf` method, to save properties to a HDF file.\n",
    "\n",
    "#### Parameters  \n",
    "\n",
    "`file_path`: Path where the HDF file should be stored. (Required)  \n",
    "`path`: Path inside the HDF store to store the elements. (Optional)  \n",
    "`name`: Name of the group inside HDF store, under which properties will be saved.(Optional)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[\u001b[1mpy.warnings         \u001b[0m][\u001b[1;33mWARNING\u001b[0m]  /home/vaibhav/anaconda2/lib/python2.7/site-packages/pandas/core/generic.py:1299: PerformanceWarning: \n",
      "your performance may suffer as PyTables will pickle object types that it cannot\n",
      "map directly to c-types [inferred_type->mixed,key->block0_values] [items->[0]]\n",
      "\n",
      "  return pytables.to_hdf(path_or_buf, key, self, **kwargs)\n",
      " (\u001b[1mpytables.py\u001b[0m:2675)\n",
      "py.warnings - \u001b[1;33mWARNING\u001b[0m - /home/vaibhav/anaconda2/lib/python2.7/site-packages/pandas/core/generic.py:1299: PerformanceWarning: \n",
      "your performance may suffer as PyTables will pickle object types that it cannot\n",
      "map directly to c-types [inferred_type->mixed,key->block0_values] [items->[0]]\n",
      "\n",
      "  return pytables.to_hdf(path_or_buf, key, self, **kwargs)\n",
      "\n",
      "[\u001b[1mpy.warnings         \u001b[0m][\u001b[1;33mWARNING\u001b[0m]  /home/vaibhav/anaconda2/lib/python2.7/site-packages/pandas/core/generic.py:1299: PerformanceWarning: \n",
      "your performance may suffer as PyTables will pickle object types that it cannot\n",
      "map directly to c-types [inferred_type->mixed,key->values] [items->None]\n",
      "\n",
      "  return pytables.to_hdf(path_or_buf, key, self, **kwargs)\n",
      " (\u001b[1mpytables.py\u001b[0m:2675)\n",
      "py.warnings - \u001b[1;33mWARNING\u001b[0m - /home/vaibhav/anaconda2/lib/python2.7/site-packages/pandas/core/generic.py:1299: PerformanceWarning: \n",
      "your performance may suffer as PyTables will pickle object types that it cannot\n",
      "map directly to c-types [inferred_type->mixed,key->values] [items->None]\n",
      "\n",
      "  return pytables.to_hdf(path_or_buf, key, self, **kwargs)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "simulation.to_hdf('/tmp/full_example.hdf')\n",
    "#simulation.to_hdf(file_path='/tmp/full_example.hdf', path='/', name='simulation')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Open the stored HDF file with pandas and print its structure."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.io.pytables.HDFStore'>\n",
      "File path: /tmp/full_example.hdf\n",
      "/simulation/model/homologous_density/density_0               series       (shape->[21])      \n",
      "/simulation/model/homologous_density/scalars                 series       (shape->[1])       \n",
      "/simulation/model/scalars                                    series       (shape->[1])       \n",
      "/simulation/model/t_radiative                                series       (shape->[20])      \n",
      "/simulation/model/v_inner                                    series       (shape->[20])      \n",
      "/simulation/model/v_outer                                    series       (shape->[20])      \n",
      "/simulation/model/w                                          series       (shape->[20])      \n",
      "/simulation/plasma/abundance                                 frame        (shape->[6,20])    \n",
      "/simulation/plasma/atomic_mass                               series       (shape->[6])       \n",
      "/simulation/plasma/beta_rad                                  series       (shape->[20])      \n",
      "/simulation/plasma/beta_sobolev                              frame        (shape->[29224,20])\n",
      "/simulation/plasma/density                                   series       (shape->[20])      \n",
      "/simulation/plasma/electron_densities                        series       (shape->[20])      \n",
      "/simulation/plasma/excitation_energy                         series       (shape->[4439])    \n",
      "/simulation/plasma/f_lu                                      series       (shape->[29224])   \n",
      "/simulation/plasma/g                                         series       (shape->[4439])    \n",
      "/simulation/plasma/g_electron                                series       (shape->[20])      \n",
      "/simulation/plasma/general_level_boltzmann_factor            frame        (shape->[4439,20]) \n",
      "/simulation/plasma/ion_number_density                        frame        (shape->[94,20])   \n",
      "/simulation/plasma/ionization_data                           frame        (shape->[88,1])    \n",
      "/simulation/plasma/j_blues                                   frame        (shape->[29224,20])\n",
      "/simulation/plasma/level_boltzmann_factor                    frame        (shape->[4439,20]) \n",
      "/simulation/plasma/level_number_density                      frame        (shape->[4439,20]) \n",
      "/simulation/plasma/levels                                    frame        (shape->[1,1])     \n",
      "/simulation/plasma/lines                                     frame        (shape->[29224,12])\n",
      "/simulation/plasma/lines_lower_level_index                   series       (shape->[29224])   \n",
      "/simulation/plasma/lines_upper_level_index                   series       (shape->[29224])   \n",
      "/simulation/plasma/metastability                             series       (shape->[4439])    \n",
      "/simulation/plasma/nu                                        series       (shape->[29224])   \n",
      "/simulation/plasma/number_density                            frame        (shape->[6,20])    \n",
      "/simulation/plasma/partition_function                        frame        (shape->[94,20])   \n",
      "/simulation/plasma/phi                                       frame        (shape->[88,20])   \n",
      "/simulation/plasma/scalars                                   series       (shape->[1])       \n",
      "/simulation/plasma/selected_atoms                            series       (shape->[6])       \n",
      "/simulation/plasma/stimulated_emission_factor                frame        (shape->[29224,20])\n",
      "/simulation/plasma/t_electrons                               series       (shape->[20])      \n",
      "/simulation/plasma/t_rad                                     series       (shape->[20])      \n",
      "/simulation/plasma/tau_sobolevs                              frame        (shape->[29224,20])\n",
      "/simulation/plasma/transition_probabilities                  frame        (shape->[87672,20])\n",
      "/simulation/plasma/w                                         series       (shape->[20])      \n",
      "/simulation/plasma/wavelength_cm                             series       (shape->[29224])   \n",
      "/simulation/runner/j_estimator                               series       (shape->[20])      \n",
      "/simulation/runner/last_interaction_in_nu                    series       (shape->[500000])  \n",
      "/simulation/runner/last_interaction_type                     series       (shape->[500000])  \n",
      "/simulation/runner/last_line_interaction_in_id               series       (shape->[500000])  \n",
      "/simulation/runner/last_line_interaction_out_id              series       (shape->[500000])  \n",
      "/simulation/runner/last_line_interaction_shell_id            series       (shape->[500000])  \n",
      "/simulation/runner/montecarlo_virtual_luminosity             series       (shape->[10000])   \n",
      "/simulation/runner/nu_bar_estimator                          series       (shape->[20])      \n",
      "/simulation/runner/output_energy                             series       (shape->[500000])  \n",
      "/simulation/runner/output_nu                                 series       (shape->[500000])  \n",
      "/simulation/runner/packet_luminosity                         series       (shape->[500000])  \n",
      "/simulation/runner/spectrum/_frequency                       series       (shape->[10001])   \n",
      "/simulation/runner/spectrum/luminosity                       series       (shape->[10000])   \n",
      "/simulation/runner/spectrum_reabsorbed/_frequency            series       (shape->[10001])   \n",
      "/simulation/runner/spectrum_reabsorbed/luminosity            series       (shape->[10000])   \n",
      "/simulation/runner/spectrum_virtual/_frequency               series       (shape->[10001])   \n",
      "/simulation/runner/spectrum_virtual/luminosity               series       (shape->[10000])   \n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "data = pd.HDFStore('/tmp/full_example.hdf')\n",
    "print data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Access `model.homologous_density.density_0` under simulation, which is a one-dimensional array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0     1970.527174\n",
      "1       13.360318\n",
      "2       10.146658\n",
      "3        7.786621\n",
      "4        6.033444\n",
      "5        4.717122\n",
      "6        3.718946\n",
      "7        2.954982\n",
      "8        2.365191\n",
      "9        1.906156\n",
      "10       1.546154\n",
      "11       1.261789\n",
      "12       1.035646\n",
      "13       0.854653\n",
      "14       0.708918\n",
      "15       0.590901\n",
      "16       0.494811\n",
      "17       0.416168\n",
      "18       0.351490\n",
      "19       0.298047\n",
      "20       0.253691\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print data['/simulation/model/homologous_density/density_0']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Scalars are stored in a `scalars` `pandas.Series` for every module. For example to access `model.t_inner` under simulation, one would need to do the following.\n",
    "\n",
    "Note: Quantities are always stored as their SI values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10694.430019\n"
     ]
    }
   ],
   "source": [
    "print data['/simulation/model/scalars']['t_inner']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Breakdown of the various to_hdf methods\n",
    "Every module in TARDIS has its own `to_hdf` method responsible to store its own data to an HDF file."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plasma\n",
    "The following call will store every plasma property to `/tmp/plasma_output.hdf` under `/parent/plasma`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "simulation.plasma.to_hdf('/tmp/plasma_output.hdf', path='parent')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.io.pytables.HDFStore'>\n",
      "File path: /tmp/plasma_output.hdf\n",
      "/parent/plasma/abundance                                 frame        (shape->[6,20])    \n",
      "/parent/plasma/atomic_mass                               series       (shape->[6])       \n",
      "/parent/plasma/beta_rad                                  series       (shape->[20])      \n",
      "/parent/plasma/beta_sobolev                              frame        (shape->[29224,20])\n",
      "/parent/plasma/density                                   series       (shape->[20])      \n",
      "/parent/plasma/electron_densities                        series       (shape->[20])      \n",
      "/parent/plasma/excitation_energy                         series       (shape->[4439])    \n",
      "/parent/plasma/f_lu                                      series       (shape->[29224])   \n",
      "/parent/plasma/g                                         series       (shape->[4439])    \n",
      "/parent/plasma/g_electron                                series       (shape->[20])      \n",
      "/parent/plasma/general_level_boltzmann_factor            frame        (shape->[4439,20]) \n",
      "/parent/plasma/ion_number_density                        frame        (shape->[94,20])   \n",
      "/parent/plasma/ionization_data                           frame        (shape->[88,1])    \n",
      "/parent/plasma/j_blues                                   frame        (shape->[29224,20])\n",
      "/parent/plasma/level_boltzmann_factor                    frame        (shape->[4439,20]) \n",
      "/parent/plasma/level_number_density                      frame        (shape->[4439,20]) \n",
      "/parent/plasma/levels                                    frame        (shape->[1,1])     \n",
      "/parent/plasma/lines                                     frame        (shape->[29224,12])\n",
      "/parent/plasma/lines_lower_level_index                   series       (shape->[29224])   \n",
      "/parent/plasma/lines_upper_level_index                   series       (shape->[29224])   \n",
      "/parent/plasma/metastability                             series       (shape->[4439])    \n",
      "/parent/plasma/nu                                        series       (shape->[29224])   \n",
      "/parent/plasma/number_density                            frame        (shape->[6,20])    \n",
      "/parent/plasma/partition_function                        frame        (shape->[94,20])   \n",
      "/parent/plasma/phi                                       frame        (shape->[88,20])   \n",
      "/parent/plasma/scalars                                   series       (shape->[1])       \n",
      "/parent/plasma/selected_atoms                            series       (shape->[6])       \n",
      "/parent/plasma/stimulated_emission_factor                frame        (shape->[29224,20])\n",
      "/parent/plasma/t_electrons                               series       (shape->[20])      \n",
      "/parent/plasma/t_rad                                     series       (shape->[20])      \n",
      "/parent/plasma/tau_sobolevs                              frame        (shape->[29224,20])\n",
      "/parent/plasma/transition_probabilities                  frame        (shape->[87672,20])\n",
      "/parent/plasma/w                                         series       (shape->[20])      \n",
      "/parent/plasma/wavelength_cm                             series       (shape->[29224])   \n"
     ]
    }
   ],
   "source": [
    "import pandas\n",
    "with pandas.HDFStore('/tmp/plasma_output.hdf') as data:\n",
    "    print data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Plasma's `to_hdf` method can also accept a `collection` parameter which can specify which types of plasma properties will be stored. For example if we wanted to only store Input plasma properties, we would do the following:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tardis.plasma.properties.base import Input\n",
    "simulation.plasma.to_hdf('/tmp/plasma_input_output.hdf', collection=[Input])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.io.pytables.HDFStore'>\n",
      "File path: /tmp/plasma_input_output.hdf\n",
      "/plasma/abundance            frame        (shape->[6,20])\n",
      "/plasma/density              series       (shape->[20])  \n",
      "/plasma/scalars              series       (shape->[1])   \n",
      "/plasma/t_rad                series       (shape->[20])  \n",
      "/plasma/w                    series       (shape->[20])  \n"
     ]
    }
   ],
   "source": [
    "import pandas\n",
    "with pandas.HDFStore('/tmp/plasma_input_output.hdf') as data:\n",
    "    print data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Model\n",
    "The following call will store properties of the `Radial1DModel` to `/tmp/model_output.hdf` under `/model`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "simulation.model.to_hdf('/tmp/model_output.hdf')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### MontecarloRunner\n",
    "The following call will store properties of the `MontecarloRunner` to `/tmp/runner_output.hdf` under `/runner`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "simulation.runner.to_hdf('/tmp/runner_output.hdf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.io.pytables.HDFStore'>\n",
      "File path: /tmp/runner_output.hdf\n",
      "/runner/j_estimator                               series       (shape->[20])    \n",
      "/runner/last_interaction_in_nu                    series       (shape->[500000])\n",
      "/runner/last_interaction_type                     series       (shape->[500000])\n",
      "/runner/last_line_interaction_in_id               series       (shape->[500000])\n",
      "/runner/last_line_interaction_out_id              series       (shape->[500000])\n",
      "/runner/last_line_interaction_shell_id            series       (shape->[500000])\n",
      "/runner/montecarlo_virtual_luminosity             series       (shape->[10000]) \n",
      "/runner/nu_bar_estimator                          series       (shape->[20])    \n",
      "/runner/output_energy                             series       (shape->[500000])\n",
      "/runner/output_nu                                 series       (shape->[500000])\n",
      "/runner/packet_luminosity                         series       (shape->[500000])\n",
      "/runner/spectrum/_frequency                       series       (shape->[10001]) \n",
      "/runner/spectrum/luminosity                       series       (shape->[10000]) \n",
      "/runner/spectrum_reabsorbed/_frequency            series       (shape->[10001]) \n",
      "/runner/spectrum_reabsorbed/luminosity            series       (shape->[10000]) \n",
      "/runner/spectrum_virtual/_frequency               series       (shape->[10001]) \n",
      "/runner/spectrum_virtual/luminosity               series       (shape->[10000]) \n"
     ]
    }
   ],
   "source": [
    "import pandas\n",
    "with pandas.HDFStore('/tmp/runner_output.hdf') as data:\n",
    "    print data"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Raw Cell Format",
  "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.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}