!+ Subroutine to find the mean scaling factor to fit an ESFT fit.
!
! Method:
!	Newton-Raphson iteration is used to find the best mean
!	scaling factor for an ESFT fit.
!
! 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 FIND_SCALING_MEAN(IERR
     &   , N_TERM, A, K
     &   , LENGTH, AMOUNT, TRANS
     &   , SCALING
     &   )
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/dimensions_spec.ucf'
      INCLUDE 'incl/dimensions_pp.ucf'
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/prec_machine.ucf'
      INCLUDE 'incl/precision.pcf'
      INCLUDE 'incl/iter_scale.acf'
      INCLUDE 'incl/tol_scale.tcf'
      INCLUDE 'incl/error.pcf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(IN)
     &     IERR
!             ERROR FLAG
     &   , N_TERM
!             NUMBER OF ESFT TERMS
     &   , LENGTH
!             LENGTH OF SET TO FIT
      REAL*8	!, INTENT(IN)
     &     A(NPD_ESFT_TERM)
!             ESFT WEIGHTS
     &   , K(NPD_ESFT_TERM)
!             ESFT EXPONENTS
     &   , AMOUNT(NPD_AMOUNT)
!             AMOUNT OF ABSORBER
     &   , TRANS(NPD_AMOUNT)
!             TRANSMISSION
      REAL*8	!, INTENT(OUT)
     &     SCALING
!             SCALING FACTOR
!
!     LOCAL VARIABLES
      INTEGER
     &     I
!             LOOP VARIABLE
     &   , J
!             LOOP VARIABLE
     &   , ITERATION
!             ITERATION
      REAL*8
     &     FIRST_MOMENT
!             FIRST MOMENT OF ESFT TERMS
     &   , SECOND_MOMENT
!             SECOND MOMENT OF ESFT TERMS
     &   , TRANS_EST
!             ESTIMATED TRANSMISSION
     &   , D_OBJECTIVE
!             DERIVATIVE OF OBJECTIVE FUNCTION
     &   , D2_OBJECTIVE
!             2ND DERIVATIVE OF OBJECTIVE FUNCTION
     &   , DEL_SCALING
!             CHANGE IN SCALING
!
!
!     START WITH A SCALING FACTOR OF 1 AND USE NEWTON-RAPHSON ITERATION
!     TO FIND THE OPTIMAL VALUE.
!
      ITERATION=0
      SCALING=1.0D+00
1     IF (ITERATION.LE.NP_MAX_ITERATION_SCALE-1) THEN
         ITERATION=ITERATION+1
         D_OBJECTIVE=0.0D+00
         D2_OBJECTIVE=0.0D+00
         DO J=1, LENGTH
            TRANS_EST=0.0D+00
            FIRST_MOMENT=0.0D+00
            SECOND_MOMENT=0.0D+00
            DO I=1, N_TERM
               TRANS_EST=TRANS_EST+A(I)
     &            *EXP(-K(I)*AMOUNT(J)*SCALING)
               FIRST_MOMENT=FIRST_MOMENT+A(I)*K(I)
     &            *EXP(-K(I)*AMOUNT(J)*SCALING)
               SECOND_MOMENT=SECOND_MOMENT+A(I)*K(I)*K(I)
     &            *EXP(-K(I)*AMOUNT(J)*SCALING)
            ENDDO
            D_OBJECTIVE=D_OBJECTIVE-2.0D+00*AMOUNT(J)*FIRST_MOMENT
     &         *(TRANS_EST-TRANS(J))
            D2_OBJECTIVE=D2_OBJECTIVE+2.0D+00*AMOUNT(J)**2
     &         *(FIRST_MOMENT**2+(TRANS_EST-TRANS(J))*SECOND_MOMENT)
         ENDDO
         IF (ABS(D_OBJECTIVE).LT.TOL_OBJECTIVE) RETURN
         DEL_SCALING=-D_OBJECTIVE/D2_OBJECTIVE
         IF ( ((SCALING+DEL_SCALING).LT.0.0D+00).OR.
     &         (D2_OBJECTIVE.LE.0.0D+00) ) THEN
            SCALING=5.0D-01*SCALING
         ELSE
            SCALING=SCALING+DEL_SCALING
         ENDIF
         GOTO 1
      ELSE
         WRITE(IU_ERR, '(/A)')
     &      '*** ERROR: TOO MANY ITERATIONS ARE REQUIRED '
     &      //'TO FIND THE SCALING.'
         IERR=I_ABORT_CALCULATION
         RETURN
      ENDIF
!
!
      RETURN
      END
