C   14/08/83 401161419  MEMBER NAME  CHIFT1   (S)           FORTRAN
      SUBROUTINE CHIFT1(X0,SIGX,Y0,SIGY,FITFCN,EPS,XB,YB,CHISQR)
C
C        ROUTINE TO FIND THE 'BEST' POINT (XB,YB) ON THE CURVE
C        Y=FITFCN(X), GIVEN THE INDEPENDENTLY MEASURED VALUES
C        X0 AND Y0, WITH STANDARD DEVIATIONS SIGX AND SIGY.
C
C        THE 'BEST' POINT IS THE POINT WHICH MINIMIZES CISQUARE,
C        GIVEN BY  CHISQ = ((X0-XB)/SIGX)**2 + ((Y0-YB)/SIGY)**2 .
C
C        THE ITERATION STOPS WHEN THE VALUES OF CHISQ FROM TWO CON-
C        SECUTIVE ITERATIONS DIFFER BY LESS THAN CHISQ*EPS,
C        OR WHEN THE NUMBER OF ITERATIONS REACHES ITRMAX (SET IN
C        DATA STATEMENT).
C
C        IF INPUT PARAMETERS ARE HOPELESS, NO FIT IS ATTEMPTED, AND
C        CHISQR IS SET TO 1.0E30.
C
C                                                  J.A.J.SKARD, 14/8/83
C                                                  J.A.J.SKARD,  6/1/84
C
      EXTERNAL FITFCN
C        MAX ITERATIONS
      DATA ITRMAX/10/
C        STEP SIZE IN X (FRACTION OF SIGX)
      DATA FACSTP/1.0/
C        STEP SIZE REDUCTION FACTOR FOR SUCCESSIVE ITERATIONS
      DATA FACRED/0.1/
C        LIMIT FOR PRINTOUT OF DIAGNOSTICS
      DATA IPRMAX/10/,IPR/0/
C        DEFINE CISQUARE AS AN INTERNAL STATEMENT FUNCTION
C        NOTE: LOGARITHMIC X-SCALE IS USED
      CHISQ(X)=(X0-EXP(X))**2*HX+(Y0-FITFCN(EXP(X)))**2*HY
C        GUARD AGAINST SOME OBVIOUS DISASTERS...
      IF(X0.LE.1.E-15)GO TO 10
      IF(SIGX.LT.1.E-15.OR.SIGY.LE.1.E-15)GO TO 10
      IF(ABS(Y0-FITFCN(X0)).LT.SIGY*1.E15.AND.SIGX.GT.1.E-15)GO TO 1
   10 IPR=IPR+1
      IF(IPR-IPRMAX)2,3,4
    2 WRITE(6,997)X0,SIGX,Y0,SIGY
  997 FORMAT(' *** CHIFT1: HOPELESS PARAMETERS. NO FIT ***',/,
     * 13X,'X0,SIGX,Y0,SIGY:',4E12.4)
      GO TO 4
    3 WRITE(6,998)
    4 XB=X0
      YB=Y0
      CHISQR=1.E30
      GO TO 100
C        PARAMETERS ARE NOT UTTERLY HOPELESS - TRY FITTING
    1 HX=1./(SIGX*SIGX)
      HY=1./(SIGY*SIGY)
C        CHANGE TO LOGARITHMIC X-SCALE
      X=ALOG(X0)
      DELX=AMIN1(FACSTP*SIGX/X0,5.)
      CHIM1=CHISQ(X)
      ITER=0
   11 CHI1=CHIM1
      ITER=ITER+1
   41 X=X+DELX
      CHI2=CHISQ(X)
C        CHECK IF WE STARTED OUT IN THE RIGHT DIRECTION
      IF(CHI1-CHI2)51,61,61
C        WE DID NOT. REVERSE DIRECTION.
   51 DELX=-DELX
      X=X+DELX
      SAVE=CHI1
      CHI1=CHI2
      CHI2=SAVE
C        KEEP STEPPING ALONG...
   61 X=X+DELX
      CHI3=CHISQ(X)
C        CHECK NEW CHISQUARE
      IF(CHI3-CHI2)71,81,81
C        STILL DECREASING, ONE MORE STEP..
   71 CHI1=CHI2
      CHI2=CHI3
      GO TO 61
C        FIND MINIMUM OF PARABOLA DEFINED BY LAST THREE POINTS
   81 DENOM=AMAX1(CHI3-2.*CHI2+CHI1,1.0E-10)
      X=X-DELX*((CHI3-CHI2)/DENOM+0.5)
      CHIM2=CHISQ(X)
C        ARE WE NEAR ENOUGH?
      IF(ABS(CHIM2-CHIM1).LT.CHIM1*EPS)GO TO 91
C        NOT YET - CHECK LIMIT OF ITERATIONS
      IF(ITER.LT.ITRMAX)GO TO 85
C        LIMIT OF ITERATIONS REACHED
      IPR=IPR+1
      IF(IPR-IPRMAX)82,83,91
   82 WRITE(6,999)ITRMAX
  999 FORMAT(' *** CHIFT1: LIMIT OF',I4,' ITERATIONS REACHED, FIT STOPPE
     *D ***')
      GO TO 91
   83 WRITE(6,998)
  998 FORMAT(' *** CHIFT1: DIAGNOSTICS TURNED OFF, LIMIT REACHED ***')
      GO TO 91
C        RESET PARAMETERS AND DO IT AGAIN
   85 CHIM1=CHIM2
C        REDUCE STEP SIZE
      DELX=DELX*FACRED
      GO TO 11
C        GOOD ENOUGH - TIDY UP AND QUIT
   91 CHISQR=CHISQ(X)
      XB=EXP(X)
      YB=FITFCN(XB)
  100 RETURN
      END
