!+ Subroutine to set the matrix equations for the fluxes.
!
! 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 SET_MATRIX_FULL(N_PROFILE, N_LAYER
     &   , TRANS, REFLECT
     &   , S_DOWN, S_UP
     &   , ALBEDO_SURFACE_DIFF, ALBEDO_SURFACE_DIR
     &   , FLUX_DIRECT_GROUND, FLUX_INC_DOWN
     &   , SOURCE_GROUND
     &   , A3, B
     &   )
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/dimensions_field.ucf'
      INCLUDE 'incl/prec_machine.ucf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(IN)
     &     N_PROFILE
!             NUMBER OF PROFILES
     &   , N_LAYER
!             NUMBER OF LAYERS
      REAL*8	!, INTENT(IN)
     &     TRANS(NPD_PROFILE, NPD_LAYER)
!             TRANSMISSION COEFFICIENTS
     &   , REFLECT(NPD_PROFILE, NPD_LAYER)
!             REFLECT COEFFICIENTS
     &   , S_UP(NPD_PROFILE, NPD_LAYER)
!             UPWARD SOURCE FUNCTION
     &   , S_DOWN(NPD_PROFILE, NPD_LAYER)
!             DOWNWARD SOURCE FUNCTION
     &   , ALBEDO_SURFACE_DIFF(NPD_PROFILE)
!             DIFFUSE REFLECTION COEFFICIENTS
     &   , ALBEDO_SURFACE_DIR(NPD_PROFILE)
!             DIRECT REFLECTION COEFFICIENTS
     &   , FLUX_DIRECT_GROUND(NPD_PROFILE)
!             DIRECT FLUX AT SURFACE
     &   , FLUX_INC_DOWN(NPD_PROFILE)
!             INCIDENT DOWNWARD FLUX
     &   , SOURCE_GROUND(NPD_PROFILE)
!             SOURCE FROM GROUND
      REAL*8	!, INTENT(OUT)
     &     A3(NPD_PROFILE, 3, 2*NPD_LAYER+2)
!             TRIDIAGONAL MATRIX
     &   , B(NPD_PROFILE, 2*NPD_LAYER+2)
!             RHS OF EQUATIONS
!
!     LOCAL VARIABLES.
      INTEGER
     &     I
!             LOOP VARIABLE
     &   , L
!             LOOP VARIABLE
      REAL*8
     &     PERTURBATION
!             PERTURBATION TO RESTORE CONDITIONING
!
!
!     CODE THE EQUATIONS INTO THE MATRIX:
!     UPPER SURFACE:
      DO L=1, N_PROFILE
         A3(L, 2, 1)=0.0D+00
         A3(L, 3, 1)=1.0D+00
         B(L, 1)=FLUX_INC_DOWN(L)
      ENDDO
!     THE INTERIOR EQUATIONS:
      DO I=1, N_LAYER
         DO L=1, N_PROFILE
            PERTURBATION=(1.0D+00-TRANS(L, I)-REFLECT(L, I))
     &         *TOL_MACHINE/(SQRT_TOL_MACHINE+REFLECT(L, I))
            A3(L, 1, 2*I)=1.0D+00
            A3(L, 2, 2*I)=-REFLECT(L, I)-PERTURBATION
            A3(L, 3, 2*I)=-TRANS(L, I)
            B(L, 2*I)=S_UP(L, I)
            A3(L, 1, 2*I+1)=-TRANS(L, I)
            A3(L, 2, 2*I+1)=-REFLECT(L, I)-PERTURBATION
            A3(L, 3, 2*I+1)=1.0D+00
            B(L, 2*I+1)=S_DOWN(L, I)
         ENDDO
      ENDDO
!     LOWER BOUNDARY
      DO L=1, N_PROFILE
         A3(L, 1, 2*N_LAYER+2)=1.0D+00
         A3(L, 2, 2*N_LAYER+2)=-ALBEDO_SURFACE_DIFF(L)
     &      -TOL_MACHINE/(ALBEDO_SURFACE_DIFF(L)+SQRT_TOL_MACHINE)
         B(L, 2*N_LAYER+2)
     &      =(ALBEDO_SURFACE_DIR(L)-ALBEDO_SURFACE_DIFF(L))
     &      *FLUX_DIRECT_GROUND(L)+SOURCE_GROUND(L)
      ENDDO
!
!
      RETURN
      END
