def merge_geom_data(castep_dat, geom_dat):
    """Merge data from a .geom file into data from a .castep file."""
    iterations_idx = -1
    for idx, iter_num in enumerate(geom_dat['iter_num']):
        iterations_idx += 1
        while iter_num != castep_dat['geom']['iterations'][iterations_idx][
            'iter_num']:
            iterations_idx += 1
        castep_dat['geom']['iterations'][iterations_idx].update({'cell':
            geom_dat['cells'][idx], 'energy': geom_dat['energies'][idx],
            'free_energy': geom_dat['free_energies'][idx], 'atoms':
            geom_dat['atoms'][idx], 'forces': geom_dat['forces'][idx]})
        if geom_dat['cell_stresses'].size:
            castep_dat['geom']['iterations'][iter_num].update({
                'cell_stress': geom_dat['cell_stresses'][idx]})
        castep_dat['geom']['species'] = geom_dat['species'][geom_dat[
            'species_idx']]
    return castep_dat
