def ensure_leading_positive(A):
    """This routine ensures that any parameter matrix is of a form which has a
    leading positive identity matrix.  If this is already the case, the
    parameter matrix will be returned unchanged; if it is not, a negated
    version of the parameter matrix will be returned.

    Parameters
    ----------
    A : ndarray
        Parameter matrix (3 or 4D)

    Returns
    -------
    ndarray
        Parameter matrix where the leading identity is guaranteed to be
        positive


    """
    if A.ndim == 3:
        entry = A[0, 0, 0]
    elif A.ndim == 4:
        entry = A[0, 0, 0, 0]
    else:
        raise ValueError('Parameter matrix must be 3- or 4D')
    if entry > 0:
        return A
    return -A
