      SUBROUTINE TSTATS(DA,OBUF,J,KLEV)
C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    TSTATS      COMPUTE AND PRINT STATS ON T,U,V,Q
C   PRGMMR: DIMEGO           ORG: W/NMC22    DATE: 86-03-31
C
C ABSTRACT: COMPUTES STATISTICS ON THE COMPARISON OF ORIGINAL T,U,V,Q
C   PROFILES FROM FIRST-GUESS WITH NEW PROFILES OF T,U,V,Q. ENTRY POINT
C   LSTATS PRINTS OUT A TABLE OF STATISTICS FOR THE COMPARISON AT
C   A FIXED SET OF 16 PRESSURES. BOTH INPUT PROFILES ARE IN SIGMA
C   (GUESS & RAFS) AND MUST BE INTERPOLATED TO THE 16 PRESSURES.
C   LINEAR IN LN P USED FOR T,U,V,Q.
C
C PROGRAM HISTORY LOG:
C   86-03-31  DIMEGO
C   88-06-30  DIMEGO      HALF PRECISION / PL1 / FTN200
C
C USAGE:    CALL TSTATS(DA,OBUF,J,KLEV)
C   INPUT ARGUMENT LIST:
C     DA       - MULTI-VARIABLE ARRAY OF GUESS STRIPS AT KLEV LEVELS
C              - FIRST     VARIABLE IS GEOPOTENTIAL AT INTERFACES
C              - SECOND    VARIABLE IS TEMPERATURE AT INTERFACES
C              - THIRD     VARIABLE IS U COMPONENT OF WIND
C              - FOURTH    VARIABLE IS V COMPONENT OF WIND
C              - FIFTH     VARIABLE IS SPECIFIC HUMIDITY
C     OBUF     - MULTI-VARIABLE ARRAY OF GUESS STRIPS AFTER THEY HAVE
C              - BEEN INTERPOLATED TO RAFS SIGMA STRUCTURE
C     J        - INDEX OF THE STRIP BEING PROCESSED
C     KLEV     - NUMBER OF LAYERS IN THE GUESS VERTICAL STRUCTURE
C
C   OUTPUT FILES:
C     UNIT6    - DIAGNOSTIC PRINTOUT OF STATISTICS THROUGH
C              - CALL TO LSTATS    NO ARGUMENTS
C
C REMARKS:   NO CAVEATS, OTHER HELPFUL HINTS NOR INFORMATION
C
C ATTRIBUTES:
C   LANGUAGE: STANDARD FORTRAN
C   MACHINE:
C
C$$$
      IMPLICIT REAL (A-H,O-Z)
        save
      INCLUDE "parmanl"
      DIMENSION DA( IMAX , KMAX ,6),OBUF( IMAX , LMAX ,6)
      DIMENSION ZIN(4),ZOUT(4)
      INTEGER   MAXI(16,4),MAXJ(16,4)
      DIMENSION STATS(8,16,4)
      DIMENSION IPMAND(16)
      DATA STATS/512*0.0/
      DATA MAXI/64*0/,MAXJ/64*0/
      DATA IPMAND/1000,925,850,700,600,500,400,300,
     +             250,200,150,100, 70, 50, 30, 20/
      DO 15 I=1, IMAX
      DO 14 K=1,16
      P = IPMAND(K)
      PLNP = LOG(P)
      DO 1 KK=1,KLEV
      KL = KK
      IF(DA(I,KK,6).LE.P) GO TO 2
    1 CONTINUE
      GO TO 14
    2 CONTINUE
      IF( KL.LE.1 )  GO TO 14
      KM = KL - 1
      ALNPKL = LOG(DA(I,KL,6))
      ALNPKM = LOG(DA(I,KM,6))
      DO 3 L=2,5
      DTDLNP=(DA(I,KL,L)-DA(I,KM,L))/(ALNPKL-ALNPKM)
      ZIN(L-1)=DA(I,KM,L) + DTDLNP * (PLNP-ALNPKM)
    3 CONTINUE
      DO 4 KK=1,LMAX
      KL = KK
      IF(OBUF(I,KK,6).LE.P) GO TO 5
    4 CONTINUE
      GO TO 14
    5 CONTINUE
      IF( KL.LE.1 )  GO TO 14
      KM = KL - 1
      ALNPKL = LOG(OBUF(I,KL,6))
      ALNPKM = LOG(OBUF(I,KM,6))
      DO 6 L=2,5
      DTDLNP=(OBUF(I,KL,L)-OBUF(I,KM,L))/(ALNPKL-ALNPKM)
      ZOUT(L-1) = OBUF(I,KM,L) + DTDLNP * (PLNP-ALNPKM)
    6 CONTINUE
      DO 10 L=1,4
      ZDIF = ZOUT(L) - ZIN(L)
      STATS(8,K,L) = STATS(8,K,L) + 1.0
      STATS(1,K,L) = STATS(1,K,L) + ZIN(L)
      STATS(2,K,L) = STATS(2,K,L) + ZOUT(L)
      STATS(3,K,L) = STATS(3,K,L) + ZDIF
      STATS(4,K,L) = STATS(4,K,L) + ZIN(L)*ZIN(L)
      STATS(5,K,L) = STATS(5,K,L) + ZOUT(L)*ZOUT(L)
      STATS(6,K,L) = STATS(6,K,L) + ZDIF*ZDIF
      IF(ABS(ZDIF).LE.STATS(7,K,L)) GO TO 10
      STATS(7,K,L) = ABS(ZDIF)
      MAXI(K,L) = I
      MAXJ(K,L) = J
   10 CONTINUE
   14 CONTINUE
   15 CONTINUE
      RETURN
         ENTRY LSTATS
      WRITE(6,20)
   20 FORMAT('1       STATISTICAL ANALYSIS BY PRESSURE ',//,
     + '  N        KP',10X,'BIAS',27X,'RMS',15X,'MAXDIF',//)
      DO 25 L=1,4
      DO 25 K=1,16
      COUNT = MAX(1.0 E+0,STATS(8,K,L))
      COUNT = 1.0 / COUNT
      DO 25 I=1,3
      STATS(I,K,L) = STATS(I,K,L) * COUNT
      STATS(I+3,K,L) = SQRT(STATS(I+3,K,L)*COUNT)
   25 CONTINUE
      DO 35 L=1,4
      DO 35 KK=1,16
      K=16-KK+1
      KNT = MAX0(1,INT(STATS(8,K,L)))
      WRITE(6,30)KNT,IPMAND(K),(STATS(I,K,L),I=1,7),MAXI(K,L),MAXJ(K,L)
   30 FORMAT(I7,I6,6F10.3,F10.3,2I7)
   35 CONTINUE
      RETURN
      END
