!+ 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 RESIDUAL_TRANS(N_SET, LENGTH_SET
     &   , U_SET, TRANS_SET, WEIGHT_POINT
     &   , I_TYPE_RESIDUAL, SCALING
     &   , 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'
      INCLUDE 'incl/type_residual.pcf'
      INCLUDE 'incl/offset_residual_trans.acf'
!
!     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
     &   , I_TYPE_RESIDUAL
!             TYPE OF RESIDUAL USED
      REAL*8	!, INTENT(OUT)
     &     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
     &   , SCALING(NPD_AMOUNT, NPD_SET_SCALE)
!             SCALINGS REQUIRED
     &   , 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)
!             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
     &     SCALE_FACTOR(NPD_SET_SCALE)
!             SCALING FACTOR FOR EACH SET
      REAL*8
     &     FUNC_SCALE
!             SCALING FUNCTION
!
!     FUNCTIONS CALLED:
      EXTERNAL
     &     FUNC_SCALE
!
!
!
      RESIDUAL_TRANS=0.0D+00
!
      IF (I_TYPE_RESIDUAL.EQ.IP_SCALE_TRANS_RESIDUAL) THEN
         DO I=1, N_SET
            SCALE_FACTOR(I)
     &         =FUNC_SCALE(P_ARRAY(I), T_ARRAY(I)
     &         , I_SCALE_FUNCTION, SCALE_VECTOR)
            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)
     &               *SCALE_FACTOR(I))
               ENDDO
               RESIDUAL_SET=RESIDUAL_SET
     &            +WEIGHT_POINT(I, J)*(TRANS_SET(J, I)-TRANS_FIT)**2
            ENDDO
            RESIDUAL_TRANS=RESIDUAL_TRANS+RESIDUAL_SET
         ENDDO
      ELSE IF (I_TYPE_RESIDUAL.EQ.IP_SCALE_FULL_SCL_RES) THEN
         DO I=1, N_SET
            SCALE_FACTOR(I)
     &         =FUNC_SCALE(P_ARRAY(I), T_ARRAY(I)
     &         , I_SCALE_FUNCTION, SCALE_VECTOR)
            RESIDUAL_SET=0.0D+00
            DO J=1, LENGTH_SET(I)
               RESIDUAL_SET=RESIDUAL_SET
     &            +WEIGHT_POINT(I, J)*(LOG((OFFSET_RESIDUAL
     &            +SCALE_FACTOR(I))
     &            /(OFFSET_RESIDUAL+SCALING(J, I))))**2
            ENDDO
            RESIDUAL_TRANS=RESIDUAL_TRANS+RESIDUAL_SET
         ENDDO
      ELSE IF (I_TYPE_RESIDUAL.EQ.IP_SCALE_MEAN_SCL_RES) THEN
         DO I=1, N_SET
            SCALE_FACTOR(I)
     &         =FUNC_SCALE(P_ARRAY(I), T_ARRAY(I)
     &         , I_SCALE_FUNCTION, SCALE_VECTOR)
            RESIDUAL_TRANS=RESIDUAL_TRANS
     &         +(((OFFSET_RESIDUAL
     &         +SCALE_FACTOR(I))
     &         /(OFFSET_RESIDUAL+SCALING(1, I)))-1.0D+00)**2
     &         +(SCALE_FACTOR(I)-SCALING(1, I))**2
         ENDDO
      ENDIF
!
!
      RETURN
      END
