Write the absorption or loss spectra to a file.

    Note that this function expects to receive an iterable series of spectra.

    Args:
        abs_data (tuple): Series (either :obj:`list` or :obj:`tuple`) of
            optical absorption or loss spectra. Each spectrum should be
            formatted as a :obj:`tuple` of :obj:`list` of :obj:`float`. If the
            data has been averaged, each spectrum should be::

                ([energies], [alpha])

            Else, if the data has not been averaged, each spectrum should be::

                ([energies], [alpha_xx, alpha_yy, alpha_zz]).

        prefix (:obj:`str`, optional): Prefix for file names.
        directory (:obj:`str`, optional): The directory in which to save files.

def write_files(abs_data, basename='absorption', prefix=None, directory=None):
    """Write the absorption or loss spectra to a file.

    Note that this function expects to receive an iterable series of spectra.

    Args:
        abs_data (tuple): Series (either :obj:`list` or :obj:`tuple`) of
            optical absorption or loss spectra. Each spectrum should be
            formatted as a :obj:`tuple` of :obj:`list` of :obj:`float`. If the
            data has been averaged, each spectrum should be::

                ([energies], [alpha])

            Else, if the data has not been averaged, each spectrum should be::

                ([energies], [alpha_xx, alpha_yy, alpha_zz]).

        prefix (:obj:`str`, optional): Prefix for file names.
        directory (:obj:`str`, optional): The directory in which to save files.
    """

    for i, absorption in enumerate(abs_data):
        num_txt = '_{}'.format(i + 1) if len(abs_data) > 1 else ''
        prefix_txt = '{}_'.format(prefix) if prefix else ''
        filename = prefix_txt + basename + num_txt + '.dat'

        if directory:
            filename = os.path.join(directory, filename)

        header = 'energy(eV)'
        if len(absorption[1].shape) == 2:
            header += ' alpha_xx alpha_yy alpha_zz'
            data = np.concatenate((absorption[0][:, None], absorption[1]),
                                  axis=1)
        else:
            header += ' alpha'
            data = np.stack((absorption[0], absorption[1]), axis=1)

        np.savetxt(filename, data, header=header)