!+ Function to calculate the residuals in the 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.
!
!- ---------------------------------------------------------------------
      FUNCTION M_RESIDUAL_TRANS(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
     &   )
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/dimensions_pp.ucf'
      INCLUDE 'incl/dimensions_spec.ucf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(IN)
     &     N_SET
!             NUMBER OF SETS OF DATA
     &   , 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)
     &     M_RESIDUAL_TRANS
!             RESIDUAL IN TRANSMISSIVITY
      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)
!             SCALING PARAMETERS
     &   , 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
      REAL*8
     &     TRANS_FIT
!             FITTED TRANSMITTANCE
     &   , RESIDUAL_SET
!             RESIDUAL TERM OF ONE SET
      REAL*8
     &     FUNC_SCALE
!             SCALING FUNCTION
!
!
      M_RESIDUAL_TRANS=0.0D+00
      DO I=1, N_SET
         RESIDUAL_SET=0.0D+00
         DO J=1, LENGTH_SET(I)
            TRANS_FIT=0.0D+00
            DO K=1, N_TERM
               TRANS_FIT=TRANS_FIT
     &            +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)))
            ENDDO
            RESIDUAL_SET=RESIDUAL_SET
     &         +WEIGHT_POINT(I, J)*(TRANS_SET(J, I)-TRANS_FIT)**2
         ENDDO
         M_RESIDUAL_TRANS=M_RESIDUAL_TRANS+RESIDUAL_SET
      ENDDO
!
!
      RETURN
      END
