# Path: pygchem/utils/tff.py
# def read_fmt_file(filename, field_spec, skip='#', as_dict=False):
#     """
#     Read a formatted text file.
#     
#     Parameters
#     ----------
#     filename : string
#         name or path to the text file
#     field_spec : sequence of tuples (name, start, length, type)
#         format specification for each field (field name, start position,
#         char length, data type).
#     skip : string
#         character(s) used to skip a line while parsing the file.
#     as_dict : bool
#         whether to return a list of dictionaries (records) or a dict of
#         lists (fields).
#     
#     Returns
#     -------
#     if `as_dict` is False :
#         a sequence (tuple) of dictionaries with field names and field
#         values for each row.
#     if `as_dict` is True :
#         a dictionary with the field names as keys and the field values for
#         all rows as values.      
#     """
#     with open(filename, 'r') as textfile:
#         data = (read_fmt_string(line, field_spec)
#                 for line in textfile if not line.startswith(skip))
# 
#         if as_dict:
#             fieldnames = (f[0] for f in field_spec)
#             return dict(((k, list(d[k] for d in data)) for k in fieldnames))
#         else:
#             return list(data)
# 
# def write_fmt_file(data, filename, field_spec):
#     """
#     Write data to a formatted text file.
# 
#     Parameters
#     ----------
#     data : sequence of dicts or dict of sequences
#         the structured data to write to the file.
#     filename : string
#         name or path to the text file.
#     field_spec : sequence of tuples (name, start, length, type)
#         format specification for each field (field name, start position,
#         char length, data type).
# 
#     """
#     # TODO: write function
#     # TODO: check string length
#     raise NotYetImplementedError()


from pygchem.utils.tff import read_fmt_file, write_fmt_file
    ('scale', 61, 10, float),
    ('unit', 72, 40, str)
)


def read_diaginfo(filename, **kwargs):
    """
    Read a 'diaginfo.dat' file and Returns a dictionary.
    """
    return read_fmt_file(filename, DIAGINFO_FMT, **kwargs)


def read_tracerinfo(filename, **kwargs):
    """
    Read a 'tracerinfo.dat' file and Returns a dictionary.
    """
    return read_fmt_file(filename, TRACERINFO_FMT, **kwargs)


def write_diaginfo(diag_metadata, filename):
    """
    Write diagnostics metadata to a 'diaginfo.dat' file.
    """
    return write_fmt_file(diag_metadata, filename, DIAGINFO_FMT)


def write_tracerinfo(tracer_metadata, filename):
    """
    Write diagnostics metadata to a 'diaginfo.dat' file.
    """

# Path: pygchem/utils/tff.py
# def read_fmt_file(filename, field_spec, skip='#', as_dict=False):
#     """
#     Read a formatted text file.
#     
#     Parameters
#     ----------
#     filename : string
#         name or path to the text file
#     field_spec : sequence of tuples (name, start, length, type)
#         format specification for each field (field name, start position,
#         char length, data type).
#     skip : string
#         character(s) used to skip a line while parsing the file.
#     as_dict : bool
#         whether to return a list of dictionaries (records) or a dict of
#         lists (fields).
#     
#     Returns
#     -------
#     if `as_dict` is False :
#         a sequence (tuple) of dictionaries with field names and field
#         values for each row.
#     if `as_dict` is True :
#         a dictionary with the field names as keys and the field values for
#         all rows as values.      
#     """
#     with open(filename, 'r') as textfile:
#         data = (read_fmt_string(line, field_spec)
#                 for line in textfile if not line.startswith(skip))
# 
#         if as_dict:
#             fieldnames = (f[0] for f in field_spec)
#             return dict(((k, list(d[k] for d in data)) for k in fieldnames))
#         else:
#             return list(data)
# 
# def write_fmt_file(data, filename, field_spec):
#     """
#     Write data to a formatted text file.
# 
#     Parameters
#     ----------
#     data : sequence of dicts or dict of sequences
#         the structured data to write to the file.
#     filename : string
#         name or path to the text file.
#     field_spec : sequence of tuples (name, start, length, type)
#         format specification for each field (field name, start position,
#         char length, data type).
# 
#     """
#     # TODO: write function
#     # TODO: check string length
#     raise NotYetImplementedError()


# Path: pygchem/io/diaginfo.py
from pygchem.utils.tff import read_fmt_file, write_fmt_file

    ('scale', 61, 10, float),
    ('unit', 72, 40, str)
)


def read_diaginfo(filename, **kwargs):
    """
    Read a 'diaginfo.dat' file and Returns a dictionary.
    """
    return read_fmt_file(filename, DIAGINFO_FMT, **kwargs)


def read_tracerinfo(filename, **kwargs):
    """
    Read a 'tracerinfo.dat' file and Returns a dictionary.
    """
    return read_fmt_file(filename, TRACERINFO_FMT, **kwargs)


def write_diaginfo(diag_metadata, filename):
    """
    Write diagnostics metadata to a 'diaginfo.dat' file.
    """
    return write_fmt_file(diag_metadata, filename, DIAGINFO_FMT)


def write_tracerinfo(tracer_metadata, filename):
    """
    Write diagnostics metadata to a 'diaginfo.dat' file.
    """

    return write_fmt_file(tracer_metadata, filename, TRACERINFO_FMT)