!-------------------------------------- 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 LINTV2 (PVLEV,PVI,KNIDIM,KNI, 2
     S                   KNPROF,KNO,PPO,PVO)
#if defined (DOC)
*
***s/r LINTV2  - Linear interpolation and constant value extrapolation.
*                Input pressure levels can vary for each profile.
*
*
*Author  : J. Halle *CMDA/AES  Oct 1999
*          (based on lintv)
*
*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* june 2002
*                remove unnecessary comdecks.
*
*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
*
*Arguments
*     i   PVLEV(KNIDIM,KNPROF)    : Vertical levels, pressure (source)
*     i   PVI(KNIDIM,KNPROF)      : Vector to be interpolated (source)
*     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 vertical interpolation in log of pressure
*              and constant value extrapolation of one-dimensional vectors.
#endif
      IMPLICIT NONE
*implicits
*
      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)
C
      REAL*8     ZPI (0:KNI+1,KNPROF)
      REAL*8     ZPO (KNO    ,KNPROF)
      REAL*8     ZPVI(0:KNI+1,KNPROF)
      INTEGER  IL  (KNO    ,KNPROF)
C
      REAL*8 ZW1, ZW2
      REAL*8 ZP, XI, ZRT, ZP1, ZP2
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
Cpik
      ZPI(0,:)=2000.
      ZPI(KNI+1,:)=2000.

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
cpik
            DO JN = 1, KNPROF
         DO JK = 1, KNI
               ZPI(JK,JN) = PVLEV(JK,JN)
            ENDDO
         ENDDO
C
C**   2.2 Destination levels
C     .   ------------------
C
cpik
         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
cpik
      IL(:,:)=0
C
      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
            ZPVI(JK,JN) = PVI(JK,JN)
         ENDDO
      ENDDO
      DO JN = 1, KNPROF
         ZPVI(0    ,JN) = PVI(1  ,JN)
         ZPVI(KNI+1,JN) = PVI(KNI,JN)
      ENDDO
C
C
C*    .  3.3  Interpolation/extrapolation
C     .       ---------------------------
C
 330     CONTINUE
Cpik
      DO JN = 1, KNPROF
         DO JO=1,KNO
            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
            PVO(JO,JN) = ZW1*ZPVI(IK,JN) +  ZW2*ZPVI(IK+1,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