def _check_array_range(istart, iend, npts):
    """
    Takes two indices, checks that they are in the correct order, ie. start
    is smaller than end, checks that start>=0 and end<=npts, and checks 
    that start!=end.

    """
    istart = int(istart if istart<iend else iend)
    iend   = int(istart if istart>iend else iend)

    istart = istart if istart>0   else 0
    iend   = iend   if iend<=npts else npts
    if istart == iend:    
        # ensure that istart and iend are not the same
        if istart > 0:
            istart = istart-1
        else:
            iend = iend+1

    return istart, iend