diff --git a/tardis/io/util.py b/tardis/io/util.py index b0bd6f0835d..e15cbe85a5a 100644 --- a/tardis/io/util.py +++ b/tardis/io/util.py @@ -311,63 +311,6 @@ def to_hdf(self, file_path, path='', name=None, collection=None): super(PlasmaWriterMixin, self).to_hdf(file_path, path, name) -#Deprecated -def to_hdf(path_or_buf, path, elements, complevel=9, complib='blosc'): - """ - A function to uniformly store TARDIS data - to an HDF file. - - Scalars will be stored in a Series under path/scalars - 1D arrays will be stored under path/property_name as distinct Series - 2D arrays will be stored under path/property_name as distinct DataFrames - - Units will be stored as their CGS value - - Parameters - ---------- - path_or_buf: - Path or buffer to the HDF store - path: str - Path inside the HDF store to store the `elements` - elements: dict - A dict of property names and their values to be - stored. - - Returns - ------- - - """ - scalars = {} - for key, value in elements.iteritems(): - if hasattr(value, 'cgs'): - value = value.cgs.value - if np.isscalar(value): - scalars[key] = value - elif hasattr(value, 'shape'): - if value.ndim == 1: - # This try,except block is only for model.plasma.levels - try: - pd.Series(value).to_hdf(path_or_buf, - os.path.join(path, key)) - except NotImplementedError: - pd.DataFrame(value).to_hdf(path_or_buf, - os.path.join(path, key)) - else: - pd.DataFrame(value).to_hdf(path_or_buf, os.path.join(path, key)) - else: - data = pd.DataFrame([value]) - data.to_hdf(path_or_buf, os.path.join(path, key)) - - if scalars: - scalars_series = pd.Series(scalars) - - # Unfortunately, with to_hdf we cannot append, so merge beforehand - scalars_path = os.path.join(path, 'scalars') - with pd.HDFStore(path_or_buf, complevel=complevel, complib=complib) as store: - if scalars_path in store: - scalars_series = store[scalars_path].append(scalars_series) - scalars_series.to_hdf(path_or_buf, os.path.join(path, 'scalars')) - ''' Code for Custom Logger Classes (ColoredFormatter and ColorLogger) and its helper function (formatter_message) is used from this thread diff --git a/tardis/simulation/base.py b/tardis/simulation/base.py index ef301038306..011e29ae9a9 100644 --- a/tardis/simulation/base.py +++ b/tardis/simulation/base.py @@ -8,12 +8,12 @@ from tardis.montecarlo import MontecarloRunner from tardis.model import Radial1DModel from tardis.plasma.standard_plasmas import assemble_plasma - +from tardis.io.util import HDFWriterMixin # Adding logging support logger = logging.getLogger(__name__) -class Simulation(object): +class Simulation(HDFWriterMixin): """A composite object containing all the required information for a simulation. @@ -36,6 +36,8 @@ class Simulation(object): .. note:: TARDIS must be built with OpenMP support in order for `nthreads` to have effect. """ + hdf_properties = ['model', 'plasma', 'runner'] + hdf_name = 'simulation' def __init__(self, iterations, model, plasma, runner, no_of_packets, no_of_virtual_packets, luminosity_nu_start, luminosity_nu_end, last_no_of_packets, @@ -320,34 +322,6 @@ def remove_callback(self, id): except KeyError: return False - def to_hdf(self, path_or_buf, path='simulation', plasma_properties=None, - suffix_count=True): - """ - Store the simulation to an HDF structure. - - Parameters - ---------- - path_or_buf - Path or buffer to the HDF store - path : str - Path inside the HDF store to store the simulation - plasma_properties - `None` or a `PlasmaPropertyCollection` which will - be passed as the collection argument to the - plasma.to_hdf method. - suffix_count : bool - If True, the path inside the HDF will be suffixed with the - number of the iteration being stored. - Returns - ------- - None - """ - if suffix_count: - path += str(self.iterations_executed) - self.runner.to_hdf(path_or_buf, path) - self.model.to_hdf(path_or_buf, path) - self.plasma.to_hdf(path_or_buf, path, plasma_properties) - @classmethod def from_config(cls, config, **kwargs): """