!+ Subroutine to assess termination conditions for ESFT fits.
!
! Method:
!	Straightforward.
!
! Current Owner of Code: J. M. Edwards
!
! History:
!	Version		Date			Comment
!	1.0		12-04-95		First Version under RCS
!						(J. M. Edwards)
!
! Description of Code:
!   FORTRAN 77  with extensions listed in documentation.
!
!- ---------------------------------------------------------------------
      SUBROUTINE TERMINATION(N_DATA, ITERATION, N_TERM
     &   , WEIGHT, RESIDUAL, R_MINIMUM
     &   , TOL_FIT, L_END, L_CONVERGE
     &   )
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/prec_machine.ucf'
      INCLUDE 'incl/precision_esft.tcf'
      INCLUDE 'incl/precision_svd.tcf'
      INCLUDE 'incl/esft_parameters.acf'
      INCLUDE 'incl/dimensions_spec.ucf'
      INCLUDE 'incl/dimensions_pp.ucf'
      INCLUDE 'incl/def_std_io.icf'
!
!     DUMMY ARGUMENTS
      INTEGER 	!, INTENT(IN)
     &     N_DATA
!             NUMBER OF DATA POINTS
     &   , N_TERM                           
!             NUMBER OF TERMS
     &   , ITERATION                        
!             NUMBER OF ITERATION
      LOGICAL 	!, INTENT(OUT)
     &     L_END                            
!             ENDING FLAG
     &   , L_CONVERGE                       
!             CONVERGENCE FLAG
      REAL*8 	!, INTENT(IN)
     &     RESIDUAL(NPD_AMOUNT+1)       
!             RESIDUALS
     &   , WEIGHT(NPD_AMOUNT+1)         
!             WEIGHTS FOR RESIDUALS
     &   , R_MINIMUM                        
!             MINIMUM OF RESIDUAL POLYNOMIAL
     &   , TOL_FIT                          
!             TOLERANCE FOR FITTING
!
!     LOCAL VARIABLES.
      INTEGER
     &     I                                
!             LOOP VARIABLE
      REAL*8
     &     RMS_ERROR                        
!             RMS ERROR
!
!
!     FIND THE R. M. S. RESIDUAL ERROR.
      RMS_ERROR=0.0D+00
      DO I=1, N_DATA
         RMS_ERROR=RMS_ERROR+WEIGHT(I)*RESIDUAL(I)**2
      ENDDO
      RMS_ERROR=SQRT(RMS_ERROR/N_DATA)
      WRITE(IU_MONITOR, '(A, 1PE12.5, A, I5)')
     &   'R. M. S. ERROR OF RESIDUALS = ', RMS_ERROR
     &   , ':   NUMBER OF TERMS IN FIT = ', N_TERM
!
!     NOW ASSESS THE CONDITIONS FOR CONVERGENCE AND TERMINATION.
      IF (RMS_ERROR.LT.TOL_FIT) THEN
         L_END=.TRUE.
         L_CONVERGE=.TRUE.
         WRITE(IU_STDOUT, '(/A, 1PE16.9)')
     &      'THE DATA HAVE BEEN FITTED TO WITHIN THE TOLERANCE.'
         WRITE(IU_MONITOR, '(/A, 1PE16.9)')
     &      'THE DATA HAVE BEEN FITTED TO WITHIN THE TOLERANCE.'
      ELSE IF (ITERATION.GE.MAX_ESFT_ITERATION) THEN
         L_CONVERGE=.FALSE.
         L_END=.TRUE.
         WRITE(IU_STDOUT, '(/A)')
     &      'THERE HAVE BEEN TOO MANY ITERATIONS WITHOUT CONVERGENCE.'
         WRITE(IU_MONITOR, '(/A)')
     &      'THERE HAVE BEEN TOO MANY ITERATIONS WITHOUT CONVERGENCE.'
      ELSE IF (ABS(R_MINIMUM).LT.1.0D+01*TOL_SVD) THEN
         L_CONVERGE=.FALSE.
         L_END=.TRUE.
         WRITE(IU_STDOUT, '(/A)')
     &      'THE MINIMUM VALUE OF THE RESIDUAL POLYNOMIAL IS JUST LESS'
         WRITE(IU_STDOUT, '(A)')
     &      'THAN ZERO: ITERATION HAS BEEN TERMINATED.'
         WRITE(IU_MONITOR, '(/A)')
     &      'THE MINIMUM VALUE OF THE RESIDUAL POLYNOMIAL IS JUST LESS'
         WRITE(IU_MONITOR, '(A)')
     &      'THAN ZERO: ITERATION HAS BEEN TERMINATED.'
      ELSE
!        DEFAULT CASE: ANOTHER ITERATION.
         L_CONVERGE=.FALSE.
         L_END=.FALSE.
      ENDIF
!
      IF (L_END) THEN
         WRITE(IU_STDOUT, '(A, 1PE16.9, ///)')
     &      'FINAL R. M. S. ERROR OF RESIDUALS = ', RMS_ERROR
         WRITE(IU_MONITOR, '(A, 1PE16.9, ///)')
     &      'FINAL R. M. S. ERROR OF RESIDUALS = ', RMS_ERROR
      ENDIF
!
!
      RETURN
      END
