def get_isky(i, pattern):
    """Return index number of image to be employed as sky"""
    if pattern == 'AB':
        if (i + 1) % 2 == 0:
            return i - 1
        else:
            return i + 1
    elif pattern == 'ABBA':
        n_previous_sequences = (i + 1) // 4
        if (i + 1) % 4 == 0:
            n_previous_sequences -= 1
        ieff = i - n_previous_sequences * 4
        if ieff == 0:
            iout = i + 1
        elif ieff == 1:
            iout = i - 1
        elif ieff == 2:
            iout = i + 1
        elif ieff == 3:
            iout = i - 1
        else:
            raise ValueError('Unexpected ieff value={}'.format(ieff))
    else:
        raise ValueError('Unexpected pattern: {}'.format(pattern))
    return iout
