@staticmethod
def _VisualizationDriver_write_data(cubefile, grid, molecule, flag, index, spin
    ):
    """
    Writes cubic grid data to cube file.

    :param cubefile:
        Name of the cube file.
    :param grid:
        The cubic grid.
    :param molecule:
        The molecule.
    :param flag:
        The flag for molecular orbitals ('mo') or density ('density').
    :param index:
        Index (0-based) of the molecular orbital or density matrix.
    :param spin:
        Spin of the molecular orbital or density.
    """
    f_cube = open(cubefile, 'w')
    x = molecule.x_to_numpy()
    y = molecule.y_to_numpy()
    z = molecule.z_to_numpy()
    natoms = molecule.number_of_atoms()
    elem_ids = molecule.elem_ids_to_numpy()
    x0, y0, z0 = grid.x_origin(), grid.y_origin(), grid.z_origin()
    dx, dy, dz = grid.x_step_size(), grid.y_step_size(), grid.z_step_size()
    nx, ny, nz = grid.x_num_points(), grid.y_num_points(), grid.z_num_points()
    print('VeloxChem Cube File', file=f_cube)
    if flag == 'mo':
        print('MO {:d} ({:s})'.format(index + 1, spin), file=f_cube)
        print('{:5d}{:12.6f}{:12.6f}{:12.6f}{:5d}'.format(-natoms, x0, y0,
            z0, 1), file=f_cube)
    elif flag == 'density':
        print('Electron density ({:s})'.format(spin), file=f_cube)
        print('{:5d}{:12.6f}{:12.6f}{:12.6f}{:5d}'.format(natoms, x0, y0,
            z0, 1), file=f_cube)
    print('{:5d}{:12.6f}{:12.6f}{:12.6f}'.format(nx, dx, 0, 0), file=f_cube)
    print('{:5d}{:12.6f}{:12.6f}{:12.6f}'.format(ny, 0, dy, 0), file=f_cube)
    print('{:5d}{:12.6f}{:12.6f}{:12.6f}'.format(nz, 0, 0, dz), file=f_cube)
    for a in range(natoms):
        print('{:5d}{:12.6f}{:12.6f}{:12.6f}{:12.6f}'.format(elem_ids[a],
            float(elem_ids[a]), x[a], y[a], z[a]), file=f_cube)
    if flag == 'mo':
        print('{:5d}{:5d}'.format(1, index + 1), file=f_cube)
    data = grid.values_to_numpy()
    for ix in range(nx):
        for iy in range(ny):
            for iz in range(nz):
                print(' {:12.5E}'.format(data[ix, iy, iz]), end='', file=f_cube
                    )
                if iz % 6 == 5:
                    print('', file=f_cube)
            print('', file=f_cube)
    f_cube.close()
