def iter_ima(cube, index=False):
    """An iterator over the images of successive spectral pixels in a Cube

    Each call to the iterator returns the image of the next spectral
    pixel of the cube. The first image to be returned, is the image of
    spectral pixel 0, followed, on the next call, by the image of
    spectral pixel 1 etc.

    The return value of iter_ima() is a python generator that can be
    used in loops, such as in the following example::

      from mpdaf.obj import iter_ima
      for im,channel in iter_ima(mycube, index=True):
          print("Total flux in channel %d = %g" % (channel, im.data.sum()))

    Parameters
    ----------
    cube : `~mpdaf.obj.Cube`
       The cube that contains the images to be returned.
    index : bool
       If False, return just an image at each iteration.
       If True, return both an image and the index of that image along
       the wavelength axis of the cube.

    Yields
    ------
    `mpdaf.obj.Image`

    """
    if index:
        for l in range(cube.shape[0]):
            yield cube[(l), :, :], l
    else:
        for l in range(cube.shape[0]):
            yield cube[(l), :, :]
