def pack_t21(fn):
    """
    Pack a TAPE21 results file of an FDE job.

    This will reduce the size of a FDE TAPE21 file by
    only keeping the ActiceFragment section and related
    essential information. All information about frozen
    fragments will be deleted. This is useful for manual
    freeze-and-thaw calculations with many subsystems,
    where this packing can reduce the size of the stored
    files significantly.

    @param fn: The filename of the TAPE21 to be packed.
    @type  fn: str
    """
    from scm.plams import KFReader
    import os
    import subprocess
    fn_orig = fn + '.orig'
    os.rename(fn, fn_orig)
    kf_orig = KFReader(fn_orig)
    keepsections = ['General', 'Properties', 'Num Int Params',
        'LinearScaling', 'Geometry']
    atomtypeindices = kf_orig.read('ActiveFrag', 'atomtypeIndices')
    if not kf_orig._sections:
        kf_orig._create_index()
    for section in kf_orig._sections:
        for i in atomtypeindices:
            if section.startswith('Atyp%3i' % i):
                keepsections.append(section)
            elif section.startswith('Atyp%4i' % i):
                keepsections.append(section)
            elif section.startswith('Atyp%5i' % i):
                keepsections.append(section)
        if section.startswith('ActiveFrag'):
            keepsections.append(section)
    subprocess.Popen(['cpkf', fn_orig, fn] + keepsections, stdout=
        subprocess.PIPE, stderr=subprocess.PIPE).wait()
    os.remove(fn_orig)
