'Insert an HDU into the `HDUList` at the given ``index``.
Parameters
index : int
Index before which to insert the new HDU.
hdu : HDU object
The HDU object to insert'

def insert(self, index, hdu):

    if (not isinstance(hdu, _BaseHDU)):
        raise ValueError('{}    is    not    an    HDU.'.format(hdu))
    num_hdus = len(self)
    if ((index == 0) or (num_hdus == 0)):
        if (num_hdus != 0):
            if isinstance(self[0], GroupsHDU):
                raise ValueError("The    current    Primary    HDU    is    a    GroupsHDU.        It    can't    be    made    into    an    extension    HDU,    so    another    HDU    cannot    be    inserted    before    it.")
            hdu1 = ImageHDU(self[0].data, self[0].header)
            super(HDUList, self).insert(1, hdu1)
            super(HDUList, self).__delitem__(0)
        if (not isinstance(hdu, (PrimaryHDU, _NonstandardHDU))):
            if isinstance(hdu, ImageHDU):
                hdu = PrimaryHDU(hdu.data, hdu.header)
            else:
                phdu = PrimaryHDU()
                super(HDUList, self).insert(0, phdu)
                index = 1
    else:
        if isinstance(hdu, GroupsHDU):
            raise ValueError('A    GroupsHDU    must    be    inserted    as    a    Primary    HDU.')
        if isinstance(hdu, PrimaryHDU):
            hdu = ImageHDU(hdu.data, hdu.header)
    super(HDUList, self).insert(index, hdu)
    hdu._new = True
    self._resize = True
    self._truncate = False
    self.update_extend()
