SUBROUTINE BALSTATAD(KFLEV,KSMAX,KASM0,KSPEC2,PVOR,PDIV,PZZP,PFACT1,PFACT2)

!**** *BALSTATAD*  - statistical tridiagonal linear balance equation,adjoint

!     Purpose.
!     --------
!           adjoint of
!           vorticity to equivalent perturbation height (p) and 
!           divergence conversion
!           using statistical balance equation

!**   Interface. CALL BALSTATAD(...)
!     ----------

!     Explicit arguments :
!     --------------------
!      Input:
!        KFLEV number of fields to process
!        KSMAX truncation n of spectral arrays
!        KASM0(0:KSMAX) index of spectral array coeff (n,m) for each m
!        KSPEC2 dimension of spectral arrays=(nsmax+1)*(nsmax+2)
!        PVOR(KFLEV,KSPEC2) input vorticity fields
!        PDIV(KFLEV,KSPEC2) input divergence fields
!        PFACT1(KSPEC2),PFACT2(KSPEC2) balance equation coefficients 
!           Empirically modified

!      Output:
!        PZZP(KFLEV,KSPEC2) linear balance results

!    Method.
!     -------
!       Solve statistical balance equation in spectral space
!       to convert vorticity into geopotential(P) and divergence.
!            (total = balanced + unbalanced )
!         The balance leads from correlation space :
!            (total Vor, unbal Div, unbal T/ps, unbal O3)
!         To model space :
!            (total Vor, total Div, total T/ps, total O3)
!         In 2 steps :

!       (1)     [P]tot = [A].[Vor]tot  (A=tridiag matrix for each m)
!                                      (Pt=total mass)

!       (2)  [T,ps]tot = [T,ps]unbal + [B].[P]tot + [C].[Div]unbal
!            [Div]tot  = [Div]unbal  + [D].[P]tot
!            [O3]tot   = [O3]unbal   + [E].[P]tot

!               (B,C,D,E=full vertical matrices for each n)

!          Adjoint of Step 1.  Step 2 is adjointed in BALVERT2

!     Author.        John Derber, 1996
!     -------

!     Modifications.
!     --------------
!        Original : 96-01-04 John Derber 
!        Modified   96-07-18 F.Bouttier   : doctorize & comment
!        Modified   96-07-18 J.Derber     : multitask and split off balvertad
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK

USE YOMDIM   , ONLY : NFLSUR   ,NUMP
USE YOMLAP   , ONLY : MYMS

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KSMAX 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSPEC2 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KASM0(0:KSMAX) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVOR(NFLSUR,KSPEC2) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDIV(NFLSUR,KSPEC2) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PZZP(NFLSUR,KSPEC2) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFACT1(KSPEC2) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFACT2(KSPEC2) 
INTEGER(KIND=JPIM) :: INM, INM1, INM2, ISPA, JF, JM, JMLOC, JN, JRI
REAL(KIND=JPRB) :: ZHOOK_HANDLE

!     ------------------------------------------------------------------

!      1. Solve balance equation.

IF (LHOOK) CALL DR_HOOK('BALSTATAD',0,ZHOOK_HANDLE)
M1_LOOP : DO JMLOC=1,NUMP
  JM=MYMS(JMLOC)
  RI1_LOOP : DO JRI=0,MIN(1,JM)
    ISPA=KASM0(JM)-JM*2+JRI
!                                  first and last lines of matrix
    INM1=ISPA+JM*2
    INM2=ISPA+KSMAX*2
    IF (JM<KSMAX) THEN
      PVOR(1:KFLEV,INM1+2)=PVOR(1:KFLEV,INM1+2)&
       & +PFACT1(INM1)*PZZP(1:KFLEV,INM1)  
!           PVOR(1:KFLEV,INM2-2)=PVOR(1:KFLEV,INM2-2)
!    &                           +PFACT2(INM2)*PZZP(1:KFLEV,INM2)
    ENDIF
!                                  central lines of tridiagonal matrix
    IF (JM+1<=KSMAX-1) THEN
      DO JN=JM+1,KSMAX-1
        INM=ISPA+JN*2
        DO JF=1,KFLEV
          PVOR(JF,INM-2)=PVOR(JF,INM-2)+PFACT2(INM)*PZZP(JF,INM)
          PVOR(JF,INM+2)=PVOR(JF,INM+2)+PFACT1(INM)*PZZP(JF,INM)
        ENDDO
      ENDDO
    ENDIF

  ENDDO RI1_LOOP
  IF(JM  ==  0)THEN
    PVOR(1:KFLEV,KASM0(0))=0.0_JPRB
    PDIV(1:KFLEV,KASM0(0))=0.0_JPRB
  ENDIF
ENDDO M1_LOOP

!     ----------------------------
IF (LHOOK) CALL DR_HOOK('BALSTATAD',1,ZHOOK_HANDLE)
END SUBROUTINE BALSTATAD
