CMAX: [Version 1.0 (6/22/93)]
CMAX: ----------------------------------------------------
CMAX: Transformation of TERPV3 from terpv3.f
CMAX: ----------------------------------------------------
      SUBROUTINE terpv3(l,ns,xlat,ylon,data,qdata,pfac,lqu,dif)
C  COMPUTES OBSERVATION MINUS BACKGROUND DIFFERENCES IN THREE DIMENSIONS
C  THE DIFFERENCES ARE COMPUTED AT THE LEVELS ABOVE AND BELOW THE
C  REPORT.  REPORTS ARE ASSUMED WITHIN ONE LEVEL OF L. THESE STRADDLING
C  DIFFERENCES ARE THEN INTERPOLATED USING THE PRESSURE FACTOR PREPARED
C  BY ROUTINE PREFAC. LQU(II) = (L-1) IF OBSERVATION IS ABOVE LEVEL L.
C  NOTE: WHEN TERPV3 IS CALLED FOR HIGHEST LEVEL, QDATA(I,J,1) IS THE
C  FIRST GUESS DATA AT THAT LEVEL. OTHERWISE, IT IS DATA FOR THE LEVEL
C  ABOVE THE ONE BEING ANALYZED.
      DIMENSION DATA (NS)
      DIMENSION DIF (NS)
      INTEGER II
      INTEGER L
      DIMENSION LQU (NS)
C     HALF PRECISION    LQU
      INTEGER NS
      DIMENSION PFAC (NS)
      DIMENSION QDATA (IM, JNP, mlev)
      DIMENSION WORK (20000)
C     HALF PRECISION    WORK
      DIMENSION XLAT (NS)
      DIMENSION YLON (NS)
C  ANALYSIS CONTROL PARAMETERS (IM AND JNP ARE NEEDED BY THIS ROUTINE)
      LOGICAL STRAT, SLP, FDCOS, FEXP, SAVEAE, SIGDAT
      COMMON / CONTRL / IM, JNP, NB, NDALT, NHMS, NLAY, NLAYM1, NLAYP1, 
     &NYMD, STRAT, IDIMEN, SLP, FDCOS, FEXP, SAVEAE, SIGDAT, MAXREP
C  VERTICAL INDICES (MLEV IS NEEDED BY THIS ROUTINE)
      COMMON / MLAY / mlev, MLEVM1, MMIX, MMIXM1, LS, LSM1
!hpf$ distribute data(block)
!hpf$ distribute dif(block)
!hpf$ distribute lqu(block)
!hpf$ distribute pfac(block)
!hpf$ distribute qdata(block,block,block)
!hpf$ distribute work(block)
!hpf$ distribute xlat(block)
!hpf$ distribute ylon(block)
      IF (l .EQ. 1) THEN
        labove = 1
      ELSE
        labove = l - 1
      ENDIF
      lcentral = l
      IF (l .EQ. mlev) THEN
        lbelow = mlev
      ELSE
        lbelow = l + 1
      ENDIF
C  HORIZONTAL INTERPOLATION OF BACKGROUND FROM LEVEL BELOW
      CALL terpv(xlat,ylon,dif,ns,qdata(1,1,lbelow))
C  HORIZONTAL INTERPOLATION OF BACKGROUND FROM LEVEL ABOVE
      CALL terpv_v3(xlat,ylon,work,1,20000,ns,qdata(1,1,labove))
      FORALL (ii = 1:ns, lqu(ii) .LT. l) dif(ii) = work(ii)
C  HORIZONTAL INTERPOLATION OF BACKGROUND AT CENTRAL LEVEL
      CALL terpv_v3(xlat,ylon,work,1,20000,ns,qdata(1,1,lcentral))
C  VERTICAL INTERPOLATION OF BACKGROUND AND FORMATION OF DIFFERENCE
      DO ii = 1 , ns
        dif(ii) = data(ii) - (pfac(ii) * work(ii) + (1.0 - pfac(ii)) 
     &      * dif(ii))
      ENDDO
      RETURN
      END
