FUNCTION BASE_INTERP, Y, X, X0, WEIGHTS=W, MINSIG=MINSIG, ORDER=ORDER
;
    ON_ERROR, 2
;
;  Check the input parameters.
;
    IF N_PARAMS() NE 3 THEN MESSAGE,    $
        'Syntax:  Result = BASE_INTERP(Y, X, X0)'
;
    IF N_ELEMENTS(Y) LE 1 THEN MESSAGE, $
        'Parameters X and Y should be arrays'
;
    IF N_ELEMENTS(X) NE N_ELEMENTS(Y) THEN MESSAGE, $
        'Arrays X and Y should have the same number of elements'
;
    IF N_ELEMENTS(X0) EQ 0 THEN MESSAGE, 'Parameter X0 not defined'
;
;  Get the array of weights.
;
    IF N_ELEMENTS(WEIGHTS) EQ 0 THEN BEGIN
        W = REPLICATE(1, N_ELEMENTS(Y))
    END ELSE IF N_ELEMENTS(WEIGHTS) NE N_ELEMENTS(Y) THEN MESSAGE, $
        'WEIGHTS must have same number of elements as X, Y'
;
;  Get the polynomial order.
;
    IF N_ELEMENTS(ORDER) EQ 0 THEN ORDER = 1
;
;  Make sure that X is in ascending order, and that all the parameters are at
;  least integer.
;
    S = SORT(X)
    XX = X(S)*1
    YY = Y(S)*1
    WW = W(S)*1
    COUNT = N_ELEMENTS(XX)
;
;  Keep reiterating until the number of elements doesn't change.
;
REITERATE:
    IF COUNT LE (ORDER+1) THEN GOTO, FINISH
    PARAM = POLYFITW(XX, YY, WW, ORDER, YFIT, YBAND, SIGMA)
    IF N_ELEMENTS(MINSIG) EQ 1 THEN SIGMA = SIGMA > MINSIG
    IF SIGMA EQ 0 THEN GOTO, FINISH
    WB = WHERE(ABS(YY-YFIT) GE 2*SIGMA, COUNT)
    IF COUNT NE 0 THEN BEGIN
        WG = WHERE(ABS(YY-YFIT) LT 2*SIGMA, COUNT)
        IF COUNT EQ 0 THEN GOTO, FINISH
        XX = XX(WG)
        YY = YY(WG)
        WW = WW(WG)
        GOTO, REITERATE
    ENDIF
;
;  Interpolate the remaining points.
;
FINISH:
    RETURN, INTERPOL(YY,XX,X0)
    END