!-------------------------------------- LICENCE BEGIN ------------------------------------
!Environment Canada - Atmospheric Science and Technology License/Disclaimer,
!                     version 3; Last Modified: May 7, 2008.
!This is free but copyrighted software; you can use/redistribute/modify it under the terms
!of the Environment Canada - Atmospheric Science and Technology License/Disclaimer
!version 3 or (at your option) any later version that should be found at:
!http://collaboration.cmc.ec.gc.ca/science/rpn.comm/license.html
!
!This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
!without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
!See the above mentioned License/Disclaimer for more details.
!You should have received a copy of the License/Disclaimer along with this software;
!if not, you can write to: EC-RPN COMM Group, 2121 TransCanada, suite 500, Dorval (Quebec),
!CANADA, H9P 1J3; or send e-mail to service.rpn@ec.gc.ca
!-------------------------------------- LICENCE END --------------------------------------
!
*DECK 

      SUBROUTINE  MATPBSS ( PBSSA, PBSSB, PBSSC, PGAM,  1
     +                      PRGAS, PE , KN )
#if defined (DOC)
*
***s/r MATPBSS: Sets the elements of the tridiagonal matrix
*               associated with the finite-element projection
*               of the vertical operator (D/DS)((SIGMA/(RGAS*GAMSTAR))*DP/DS).
*               An integration by parts is done and the integrated
*               term which is a function of P is stored into 
*               PBSSA(1) and PBSSC(KN).
*               
*
* Author : M. Beland  RPN/AES - May 1984
*
* Revision: L. Fillion RPN/AES - Sep 93 - Major modifications to suit the
*                                         variational analysis environment.
* Revision: L. Fillion ARMA/AES - Jan 97 - Uses 3D-Var's levels rather than previously 
*                               . specified levels.
C
C Arguments:
C     PBSSA : LOWER DIAGONAL
C     PBSSB : MAIN DIAGONAL
C     PBSSC : UPPER DIAGONAL
C     PGAM  : ADIABATIC EXPONENT
C     PRGAS : DRY-AIR GAS CONSTANT
C     PE    : SIGMA EXPONENT IN GAMSTAR
C     KN    : NUMBER OF SIGMA LEVELS
*
#endif
      IMPLICIT NONE
*implicits
#include "pardim.cdk"
#include "comdim.cdk"
#include "comgem.cdk"
#include "comode.cdk"
      INTEGER KN
      REAL*8    PGAM, PRGAS, PE
      REAL*8    PBSSA(KN), PBSSB(KN), PBSSC(KN)
C
      INTEGER  INM1, JI
      REAL*8     ZCON, ZE2, ZDM, ZDP, ZXM, ZXP
C
      INM1 = KN - 1
      PBSSA(1) = VLEV(1)/(PRGAS*TSTAR)
      PBSSC(KN) = -1./(PRGAS*TSTAR)
      ZCON = 1./(PRGAS*PGAM)
      ZE2 = 2.-PE
      DO 10 JI=2,INM1
        ZXM=1./(VLEV(JI)-VLEV(JI-1))**2
        ZXP=1./(VLEV(JI+1)-VLEV(JI))**2
        IF(PE.NE.2.) THEN
          ZDM=(VLEV(JI)**ZE2-VLEV(JI-1)**ZE2)/ZE2
          ZDP=(VLEV(JI+1)**ZE2-VLEV(JI)**ZE2)/ZE2
        ELSE
          ZDM=LOG(VLEV(JI)/VLEV(JI-1))
          ZDP=LOG(VLEV(JI+1)/VLEV(JI))
        ENDIF
        PBSSA(JI)=ZDM*ZXM*ZCON
        PBSSB(JI)=-(ZDM*ZXM+ZDP*ZXP)*ZCON
        PBSSC(JI)=ZDP*ZXP*ZCON
   10 CONTINUE
C
      IF(PE.NE.2.) THEN
           PBSSB(1) = -ZCON * (VLEV(2)**ZE2-VLEV(1)**ZE2)/
     +                (ZE2*(VLEV(2)-VLEV(1))**2)
           PBSSC(1) = -PBSSB(1)
           PBSSB(KN)= -ZCON*(VLEV(KN)**ZE2-VLEV(INM1)**ZE2)/
     +                (ZE2*(VLEV(KN)-VLEV(INM1))**2)
           PBSSA(KN)= -PBSSB(KN)
      ELSE
           PBSSB(1) = ZCON*LOG(VLEV(2)/VLEV(1))
           PBSSC(1) = -PBSSB(1)
           PBSSB(KN)= -ZCON*LOG(VLEV(KN)/VLEV(INM1))
           PBSSA(KN)= -PBSSB(KN)
      ENDIF
C
C
      RETURN
      END