!-------------------------------------- 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 --------------------------------------
!

      SUBROUTINE LLINTV2 (PVLEV,PVI,PVIG,PPS,KNIDIM,KNI,
     S                    KNPROF,KNO,PPO,PVO)
#if defined (DOC)
*
***s/r LLINTV2  - Tangent linear of linear interpolation and constant
*                 value extrapolation.
*                 Input pressure levels can vary for each profile.
*
*
*Author  : J. Halle *CMDA/AES  Oct 1999
*
*Revision 001  : J. Halle *CMDA/AES  dec 2000
*                adapt to TOVS level 1b.
*
*Revision 002  : JM Belanger *CMDA/SMC* june 2001
*                32 bits conversion.
*
*Revision 003  : J Halle *CMDA/SMC* april 2002
*                corriger bug sur l'indice de vlev.
*
*Revision 004: J. Halle CMDA/SMC Feb 2003
*              replace dynamic memory allocation with automatic arrays.
*Revision 005: P. Koclas CMDA/SMC Apr 2003
*             -changed loop nesting order for ibm conversion
*Revision 006: C. Charette - ARMA/SMC - Sep. 2004
*                Conversion to hybrid vertical coordinate
*Revision 007: J. Halle - CMDA/SMC - May 2005
*                Bugfix for ps term; use vhybinc instead of vlev.
*
*Arguments
*     i   PVLEV(KNIDIM,KNPROF)    : Vertical levels, pressure (source)
*     i   PVI(KNIDIM,KNPROF)      : Vector to be interpolated (source) (gradients)
*     i   PVIG(KNIDIM,KNPROF)     : Vector to be interpolated (source)
*     i   PPS(KNPROF)             : Surface pressure (source) (gradients)
*     i   KNIDIM                  : Dimension of input levels (source)
*     i   KNI                     : Number of input levels (source)
*     i   KNPROF                  : Number of profiles
*     i   KNO                     : Number of output levels (destination)
*     i   PPO(KNO)                : Vertical levels, pressure (destination)
*     o   PVO(KNO,KNPROF)         : Interpolated profiles (destination)
*
*    -------------------
**    Purpose: Performs the tangent linear of the vertical interpolation
*              in log of pressure and constant value extrapolation of
*              one-dimensional vectors.
#endif
      IMPLICIT NONE
*implicits
#include "pardim.cdk"
#include "comdim.cdk"
#include "comgem.cdk"
*
      INTEGER  JI, JK, JO, JN, IK, IORDER
      INTEGER  KNIDIM, KNI, KNO, KNPROF, ILEN, IERR
C
      REAL*8 PVLEV(KNIDIM,KNPROF)
      REAL*8 PPO(KNO), PVO(KNO,KNPROF)
      REAL*8 PVI(KNIDIM,KNPROF)
      REAL*8 PVIG(KNIDIM,KNPROF)
      REAL*8 PPS(KNPROF)
C
      REAL*8     ZPI  (0:KNI+1,KNPROF)
      REAL*8     ZPO  (KNO    ,KNPROF)
      REAL*8     ZPVI (0:KNI+1,KNPROF)
      REAL*8     ZPVIG(0:KNI+1,KNPROF)
      REAL*8     ZVLEV(0:KNI+1)
      INTEGER  IL   (KNO    ,KNPROF)
C
      REAL*8 ZW1, ZW2, ZDADPS, ZPRESBPB,ZPRESBPT
      REAL*8 ZP, XI, ZRT, ZP1, ZP2
      REAL*8  tmpv1
C
C**   0. Dynamic memory allocation for temporary vectors
C     .  -----------------------------------------------
C
 050  CONTINUE
C
C     ... removed and replaced by automatic arrays, jh feb 2003 ......
C
C**   1. Initialization for vertical extrapolation (extra dummy levels)
C     .  --------------------------------------------------------------
C
 100  CONTINUE
C
      DO JN = 1, KNPROF
         ZPI(0    ,JN) = 2000.0
         ZPI(KNI+1,JN) = 2000.0
      ENDDO
C
C**      1.1 Determine if input pressure levels are in ascending or
C     .      descending order.
C     .     -------------------------------------------------------
C
      IF ( PVLEV(1,1) .LT. PVLEV(KNI,1) ) THEN
          IORDER = 1
      ELSE
          IORDER = -1
      ENDIF
C
C**   2. Compute pressure levels pressure
C     .  ------------------------------------------------
C
200   CONTINUE
C
C**   2.1 Source levels
C     .   -------------
C
         DO JN = 1, KNPROF
            DO JK = 1, KNI
               ZPI(JK,JN) = PVLEV(JK,JN)
            ENDDO
         ENDDO
C
C**   2.2 Destination levels
C     .   ------------------
C
         DO JN = 1, KNPROF
            DO JK = 1, KNO
               ZPO(JK,JN) = PPO(JK)
            ENDDO
         ENDDO
C
C*    3.  Interpolate in log of pressure or extrapolate with constant value
C*    .   for each destination pressure level
C     .   -----------------------------------------------------------------
C
300   CONTINUE
C
C
C*    .  3.1  Find the adjacent level below
C     .       -----------------------------
C
 310     CONTINUE
C
      DO JN = 1, KNPROF
         DO JO=1,KNO
            IL(JO,JN) = 0
         ENDDO
      ENDDO
Cpiok
            DO JI=1,KNI
      DO JN = 1, KNPROF
         DO JO=1,KNO
               ZRT = ZPO(JO,JN)
               ZP = ZPI(JI,JN)
               XI = SIGN(1.0D0,IORDER*(ZRT-ZP))
               IL(JO,JN) = IL(JO,JN) + MAX(0.0D0,XI)
            ENDDO
         ENDDO
      ENDDO
C
C
C*    .  3.2  Fill extra levels, for constant value extrapolation
C     .       ---------------------------------------------------
C
 320     CONTINUE
Cpik
       DO JN = 1, KNPROF
         DO JK = 1, KNI
         ZVLEV (JK) = VHYBINC (JK)
            ZPVI (JK,JN) = PVI (JK,JN)
            ZPVIG(JK,JN) = PVIG(JK,JN)
         ENDDO
      ENDDO
C
      ZVLEV (0    ) = VHYBINC (1  )
      ZVLEV (KNI+1) = VHYBINC (KNI)
      DO JN = 1, KNPROF
         ZPVI (0    ,JN) = PVI (1  ,JN)
         ZPVI (KNI+1,JN) = PVI (KNI,JN)
         ZPVIG(0    ,JN) = PVIG(1  ,JN)
         ZPVIG(KNI+1,JN) = PVIG(KNI,JN)
      ENDDO
C
C
C*    .  3.3  Interpolation/extrapolation
C     .       ---------------------------
C
 330     CONTINUE
Cpik
      tmpv1 = rptopinc/rprefinc
      DO JO=1,KNO
         DO JN = 1, KNPROF
            IK = IL(JO,JN)
            ZP = ZPO(JO,JN)
            ZP1 = ZPI(IK  ,JN)
            ZP2 = ZPI(IK+1,JN)
            ZW1 = LOG(ZP/ZP2)/LOG(ZP1/ZP2)
            ZW2 = 1. - ZW1
            zpresbpt   = ((zvlev(ik) - tmpv1) /(1.0-tmpv1))**rcoefinc
            zpresbpb   = ((zvlev(ik+1) - tmpv1)/(1.0-tmpv1))**rcoefinc
            ZDADPS     = ( (ZPRESBPT/ZP1)*LOG(ZP/ZP2)
     +                     -(ZPRESBPB/ZP2)*LOG(ZP/ZP1) )
     +                     /LOG(ZP2/ZP1)**2

            PVO(JO,JN) = ZW1*ZPVI(IK,JN) +  ZW2*ZPVI(IK+1,JN) +
     &                   (ZPVIG(IK+1,JN)-ZPVIG(IK,JN))*
     &                    ZDADPS*PPS(JN)
         ENDDO
      ENDDO
C
C*    4.  Deallocate memory
C     .   -----------------
C
400   CONTINUE
C
C     ... removed and replaced by automatic arrays, jh feb 2003 ......
C
      RETURN
      END