!+ Suibroutine to initialize the matrix in Schmetz's scheme.
!
! 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 S_SET_MATRIX(N_PROFILE, N_LAYER
     &   , TRANS_FREE, REFLECT_FREE, TRANS_CLOUD, REFLECT_CLOUD
     &   , W_CLOUD, N_CLOUD_TOP
     &   , S_DOWN_FREE, S_UP_FREE, S_DOWN_CLOUD, S_UP_CLOUD
     &   , SOURCE_GROUND, ALBEDO_SURFACE_DIFF, ALBEDO_SURFACE_DIR
     &   , FLUX_INC_DOWN, FLUX_DIRECT_GROUND
     &   , A3, B
     &   )
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/dimensions_field.ucf'
      INCLUDE 'incl/spectral_region.pcf'
      INCLUDE 'incl/prec_machine.ucf'
      INCLUDE 'incl/precision.pcf'
!
!     DECLARATION OF DUMMY ARGUMENTS.
      INTEGER 	!, INTENT(IN)
     &     N_PROFILE
!             NUMBER OF PROFILES
     &   , N_LAYER
!             NUMBER OF LAYERS
     &   , N_CLOUD_TOP
!             TOP CLOUDY LAYER
      REAL*8 	!, INTENT(IN)
     &     TRANS_FREE(NPD_PROFILE, NPD_LAYER)
!             FREE TRANSMISSION
     &   , REFLECT_FREE(NPD_PROFILE, NPD_LAYER)
!             FREE REFLECTION
     &   , TRANS_CLOUD(NPD_PROFILE, NPD_LAYER)
!             CLOUDY TRANSMISSION
     &   , REFLECT_CLOUD (NPD_PROFILE, NPD_LAYER)
!             CLOUDY REFLECTION
     &   , W_CLOUD(NPD_PROFILE, NPD_LAYER)
!             CLOUD AMOUNT
     &   , S_UP_FREE(NPD_PROFILE, NPD_LAYER)
!             UPWARD SOURCE FUNCTION
     &   , S_DOWN_FREE(NPD_PROFILE, NPD_LAYER)
!             DOWNWARD SOURCE FUNCTION
     &   , S_UP_CLOUD(NPD_PROFILE, NPD_LAYER)
!             UPWARD SOURCE FUNCTION
     &   , S_DOWN_CLOUD(NPD_PROFILE, NPD_LAYER)
!             DOWNWARD SOURCE FUNCTION
      REAL*8	!, INTENT(IN)
     &     SOURCE_GROUND(NPD_PROFILE)
!             GROUND SOURCE FUNCTION
     &   , ALBEDO_SURFACE_DIFF(NPD_PROFILE)
!             DIFFUSE ALBEDO OF SURFACE
     &   , ALBEDO_SURFACE_DIR(NPD_PROFILE)
!             DIRECT ALBEDO OF SURFACE
     &   , FLUX_INC_DOWN(NPD_PROFILE)
!             INCIDENT DIFUSE FLUX
     &   , FLUX_DIRECT_GROUND(NPD_PROFILE)
!             DIRECT FLUX AT GROUND
      REAL*8	!, INTENT(OUT)
     &     A3(NPD_PROFILE, 3, 2*NPD_LAYER+2)
!             TRIDIAGONAL MATRIX
     &   , B(NPD_PROFILE, 2*NPD_LAYER+2)
!             RIGHT-HAND SIDES
!
!     DECLARATION OF LOCAL VARIABLES.
      INTEGER
     &     I
!             LOOP VARIABLE
     &   , L
!             LOOP VARIABLE
      REAL*8
     &     P
!             WORKING ARRAY
     &   , Q
!             WORKING ARRAY
     &   , D
!             WORKING ARRAY
!
!
!     CONDITION ON INCIDENT FLUX:
      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
!
!     INNER LOOP.
!
!     UPPER CLEAR-SKY REGION:
      DO I=1, N_CLOUD_TOP-1
         DO L=1, N_PROFILE
            A3(L, 1, 2*I)=1.0D+00
            A3(L, 2, 2*I)=-REFLECT_FREE(L, I)
            A3(L, 3, 2*I)=-TRANS_FREE(L, I)
            B(L, 2*I)=S_UP_FREE(L, I)
            A3(L, 1, 2*I+1)=-TRANS_FREE(L, I)
            A3(L, 2, 2*I+1)=-REFLECT_FREE(L, I)
            A3(L, 3, 2*I+1)=1.0D+00
            B(L, 2*I+1)=S_DOWN_FREE(L, I)
         ENDDO
      ENDDO
!
      DO I=N_CLOUD_TOP, N_LAYER
         DO L=1, N_PROFILE
            P=W_CLOUD(L, I)*REFLECT_CLOUD(L, I)
            Q=1.0D+00-W_CLOUD(L, I)*(1.0D+00-TRANS_CLOUD(L, I))
            D=1.0D+00-P*REFLECT_FREE(L, I)
            A3(L, 1, 2*I)=1.0D+00
            A3(L, 2, 2*I)=-(REFLECT_FREE(L, I)*(Q**2-P**2)+P)/D
            A3(L, 3, 2*I)=-Q*TRANS_FREE(L, I)/D
            B(L, 2*I)=S_UP_CLOUD(L, I)+Q*(REFLECT_FREE(L, I)
     &         *S_DOWN_CLOUD(L, I)+S_UP_FREE(L, I))/D
            A3(L, 1, 2*I+1)=-Q*TRANS_FREE(L, I)/D
            A3(L, 2, 2*I+1)=-(P*(TRANS_FREE(L, I)**2
     &         -REFLECT_FREE(L, I)**2)+REFLECT_FREE(L, I))/D
            A3(L, 3, 2*I+1)=1.0D+00
            B(L, 2*I+1)=S_DOWN_FREE(L, I)+TRANS_FREE(L, I)
     &         *(P*S_UP_FREE(L, I)+S_DOWN_CLOUD(L, I))/D
         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)=-MAX(ALBEDO_SURFACE_DIFF(L), TOL_DIV)
         B(L, 2*N_LAYER+2)=ALBEDO_SURFACE_DIR(L)
     &      *FLUX_DIRECT_GROUND(L)+SOURCE_GROUND(L)
      ENDDO
!
!
      RETURN
      END
