!+ Subroutine to calculate gradient of residuals in transmission.
!
! 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 M_RESIDUAL_GRADIENT(N_SET, LENGTH_SET
     &   , U_SET, TRANS_SET, WEIGHT_POINT
     &   , N_TERM, W_ESFT, K_ESFT
     &   , I_SCALE_FUNCTION, SCALE_VECTOR, P_ARRAY, T_ARRAY
     &   , GRADIENT)
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/dimensions_pp.ucf'
      INCLUDE 'incl/dimensions_spec.ucf'
      INCLUDE 'incl/scale_fnc.pcf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(IN)
     &     N_SET
!             NUMBER OF SETS
     &   , N_TERM
!             NUMBER OF TERMS OF FIT
     &   , LENGTH_SET(NPD_SET_SCALE)
!             LENGTHS OF SETS
     &   , I_SCALE_FUNCTION
!             TYPE OF SCALING FUNCTION
      REAL*8	!, INTENT(OUT)
     &     GRADIENT(NPD_SCALE_VARIABLE, NPD_ESFT_TERM)
!             GRADIENT OF RESIDUAL
      REAL*8	!, INTENT(IN)
     &     U_SET(NPD_AMOUNT, NPD_SET_SCALE)
!             AMOUNTS OF ABSORBERS
     &   , TRANS_SET(NPD_AMOUNT, NPD_SET_SCALE)
!             TRANSMISSIVITIES
     &   , WEIGHT_POINT(NPD_SET_SCALE, NPD_AMOUNT)
!             WEIGHTS FOR TRANSMISSIVITIES
     &   , W_ESFT(NPD_ESFT_TERM)
!             ESFT WIEGHTS
     &   , K_ESFT(NPD_ESFT_TERM)
!             ESFT EXPONENTS
     &   , SCALE_VECTOR(NPD_SCALE_VARIABLE, NPD_ESFT_TERM)
!             PARAMETERS FOR SCALING
     &   , P_ARRAY(NPD_SET_SCALE)
!             PRESSURE ARRAY
     &   , T_ARRAY(NPD_SET_SCALE)
!             TEMPERATURE ARRAY
!
!     LOCAL VARIABLES.
      INTEGER
     &     I
!             LOOP VARIABLE
     &   , J
!             LOOP VARIABLE
     &   , K
!             LOOP VARIABLE
     &   , L
!             LOOP VARIABLE
      REAL*8
     &     TRANS_FIT(NPD_AMOUNT, NPD_SET_SCALE)
!             FITTED TRANSMITTANCE
     &   , SUB_TERM
!             SUB-TERM OF TRANSMITTANCE
!
!     FUNCTIONS CALLED:
      REAL*8
     &     FUNC_SCALE
!             SCALING FUNCTION
     &   , FUNC_SCALE_DERIVATIVE
!             DERIVATIVE OF SCALING FUNCTION
      EXTERNAL
     &     FUNC_SCALE, FUNC_SCALE_DERIVATIVE
!
!
      INCLUDE 'incl/scale_fnc.dcf'
!
!
!     INITIALIZE THE GRADIENT FOR SUMMING.
      DO L=1, N_SCALE_VARIABLE(I_SCALE_FUNCTION)
         DO K=1, N_TERM
            GRADIENT(L, K)=0.0D+00
         ENDDO
      ENDDO
!
      DO I=1, N_SET
         DO J=1, LENGTH_SET(I)
            TRANS_FIT(J, I)=0.0D+00
            DO K=1, N_TERM
               SUB_TERM=W_ESFT(K)*EXP(-K_ESFT(K)*U_SET(J, I)
     &            *FUNC_SCALE(P_ARRAY(I), T_ARRAY(I)
     &            , I_SCALE_FUNCTION, SCALE_VECTOR))
               TRANS_FIT(J, I)=TRANS_FIT(J, I)+SUB_TERM
            ENDDO
         ENDDO
      ENDDO
      DO L=1, N_SCALE_VARIABLE(I_SCALE_FUNCTION)
         DO K=1, N_TERM
            DO I=1, N_SET
               DO J=1, LENGTH_SET(I)
                  GRADIENT(L, K)=GRADIENT(L, K)+2.0D+00
     &               *WEIGHT_POINT(I, J)*(TRANS_SET(J, I)
     &               -TRANS_FIT(J, I))*W_ESFT(K)
     &               *EXP(-K_ESFT(K)*U_SET(J, I)
     &               *FUNC_SCALE(P_ARRAY(I), T_ARRAY(I)
     &               , I_SCALE_FUNCTION
     &               , SCALE_VECTOR(1, K)))*K_ESFT(K)*U_SET(J, I)
     &               *FUNC_SCALE_DERIVATIVE(L, P_ARRAY(I), T_ARRAY(I)
     &               , I_SCALE_FUNCTION, SCALE_VECTOR(1, K))
               ENDDO
            ENDDO
         ENDDO
      ENDDO
!
!
      RETURN
      END
