from tardis.io.config_reader import Configuration
from tardis.simulation import Simulation

# Must have the tardis_example folder in the working directory.
config_fname = 'tardis_example/tardis_example.yml'
config = Configuration.from_yaml(config_fname)

simulation = Simulation.from_config(config)

"""
Explanation: Example to_hdf calls
Initialize the simulation with the tardis_example.yml configuration file.
End of explanation
"""


simulation.run()

"""
Explanation: Run the simulation.
End of explanation
"""


simulation.to_hdf('/tmp/full_example.hdf')
#simulation.to_hdf(file_path='/tmp/full_example.hdf', path='/', name='simulation')

"""
Explanation: You can now use to_hdf method, to save properties to a HDF file.
Parameters
file_path: Path where the HDF file should be stored. (Required)
path: Path inside the HDF store to store the elements. (Optional)
name: Name of the group inside HDF store, under which properties will be saved.(Optional)
End of explanation
"""


import pandas as pd
data = pd.HDFStore('/tmp/full_example.hdf')
print data

"""
Explanation: Open the stored HDF file with pandas and print its structure.
End of explanation
"""


print data['/simulation/model/homologous_density/density_0']

"""
Explanation: Access model.homologous_density.density_0 under simulation, which is a one-dimensional array
End of explanation
"""


print data['/simulation/model/scalars']['t_inner']

"""
Explanation: 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.
Note: Quantities are always stored as their SI values.
End of explanation
"""


simulation.plasma.to_hdf('/tmp/plasma_output.hdf', path='parent')

import pandas
with pandas.HDFStore('/tmp/plasma_output.hdf') as data:
    print data

"""
Explanation: Breakdown of the various to_hdf methods
Every module in TARDIS has its own to_hdf method responsible to store its own data to an HDF file.
Plasma
The following call will store every plasma property to /tmp/plasma_output.hdf under /parent/plasma
End of explanation
"""


from tardis.plasma.properties.base import Input
simulation.plasma.to_hdf('/tmp/plasma_input_output.hdf', collection=[Input])

import pandas
with pandas.HDFStore('/tmp/plasma_input_output.hdf') as data:
    print data

"""
Explanation: 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:
End of explanation
"""


simulation.model.to_hdf('/tmp/model_output.hdf')

"""
Explanation: Model
The following call will store properties of the Radial1DModel to /tmp/model_output.hdf under /model.
End of explanation
"""


simulation.runner.to_hdf('/tmp/runner_output.hdf')

import pandas
with pandas.HDFStore('/tmp/runner_output.hdf') as data:
    print data

"""
Explanation: MontecarloRunner
The following call will store properties of the MontecarloRunner to /tmp/runner_output.hdf under /runner
End of explanation
"""


