# Path: liam2/data.py
# def copy_table(input_table, output_node, output_dtype=None,
#                chunksize=10000, condition=None, stop=None, show_progress=False,
#                default_values=None, **kwargs):
#     complete_kwargs = {'title': input_table._v_title}
# #                       'filters': input_table.filters}
#     output_file = output_node._v_file
#     complete_kwargs.update(kwargs)
#     if output_dtype is None:
#         output_dtype = input_table.dtype
#     output_table = output_file.create_table(output_node, input_table.name,
#                                             output_dtype, **complete_kwargs)
#     return append_table(input_table, output_table, chunksize, condition,
#                         stop=stop, show_progress=show_progress,
#                         default_values=default_values)
# 
# def get_fields(array):
#     dtype = array.dtype
#     return [(name, normalize_type(dtype[name].type)) for name in dtype.names]
# 
# Path: liam2/utils.py
# def timed(func, *args, **kwargs):
#     elapsed, res = gettime(func, *args, **kwargs)
#     if config.show_timings:
#         print("done (%s elapsed)." % time2str(elapsed))
#     else:
#         print("done.")
#     return res


import tables
import numpy as np
    import sys
    import platform
from liam2.data import copy_table, get_fields
from liam2.utils import timed

# encoding: utf-8
from __future__ import absolute_import, division, print_function



__version__ = "0.1"


def dropfields(input_path, output_path, todrop):
    input_file = tables.open_file(input_path, mode="r")
    input_root = input_file.root

    output_file = tables.open_file(output_path, mode="w")
    output_globals = output_file.create_group("/", "globals", "Globals")

    print(" * copying globals ...", end=' ')
    copy_table(input_root.globals.periodic, output_globals)
    print("done.")

    output_entities = output_file.create_group("/", "entities", "Entities")
    for table in input_file.iterNodes(input_root.entities):
        table_fields = get_fields(table)
        output_dtype = np.dtype([(fname, ftype) for fname, ftype in table_fields
                                 if fname not in todrop])
        size = (len(table) * table.dtype.itemsize) / 1024.0 / 1024.0
        # noinspection PyProtectedMember
        print(" * copying table %s (%.2f Mb) ..." % (table._v_name, size),
              end=' ')

# Path: liam2/data.py
# def copy_table(input_table, output_node, output_dtype=None,
#                chunksize=10000, condition=None, stop=None, show_progress=False,
#                default_values=None, **kwargs):
#     complete_kwargs = {'title': input_table._v_title}
# #                       'filters': input_table.filters}
#     output_file = output_node._v_file
#     complete_kwargs.update(kwargs)
#     if output_dtype is None:
#         output_dtype = input_table.dtype
#     output_table = output_file.create_table(output_node, input_table.name,
#                                             output_dtype, **complete_kwargs)
#     return append_table(input_table, output_table, chunksize, condition,
#                         stop=stop, show_progress=show_progress,
#                         default_values=default_values)
# 
# def get_fields(array):
#     dtype = array.dtype
#     return [(name, normalize_type(dtype[name].type)) for name in dtype.names]
# 
# Path: liam2/utils.py
# def timed(func, *args, **kwargs):
#     elapsed, res = gettime(func, *args, **kwargs)
#     if config.show_timings:
#         print("done (%s elapsed)." % time2str(elapsed))
#     else:
#         print("done.")
#     return res


# Path: liam2/dropfields_h5.py
import tables
import numpy as np
    import sys
    import platform
from liam2.data import copy_table, get_fields
from liam2.utils import timed

# encoding: utf-8
from __future__ import absolute_import, division, print_function



__version__ = "0.1"


def dropfields(input_path, output_path, todrop):
    input_file = tables.open_file(input_path, mode="r")
    input_root = input_file.root

    output_file = tables.open_file(output_path, mode="w")
    output_globals = output_file.create_group("/", "globals", "Globals")

    print(" * copying globals ...", end=' ')
    copy_table(input_root.globals.periodic, output_globals)
    print("done.")

    output_entities = output_file.create_group("/", "entities", "Entities")
    for table in input_file.iterNodes(input_root.entities):
        table_fields = get_fields(table)
        output_dtype = np.dtype([(fname, ftype) for fname, ftype in table_fields
                                 if fname not in todrop])
        size = (len(table) * table.dtype.itemsize) / 1024.0 / 1024.0
        # noinspection PyProtectedMember
        print(" * copying table %s (%.2f Mb) ..." % (table._v_name, size),
              end=' ')

        copy_table(table, output_entities, output_dtype)
