!-------------------------------------- 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 ALINTV2 (PVLEV,DPVI,PVIG,DPPS,KNIDIM,KNI,
     S                    KNPROF,KNO,PPO,DPVO)
#if defined (DOC)
*
***s/r ALINTV2  - Adjoint of linear interpolation and
*                 constant value extrapolation.
*
*
*Author  : J. Halle, CMDA/AES, Oct 1999
*          (based on lintvad)
*
*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  : C. Charette - ARMA/SMC - Sept 2004
*                Conversion to hybrid vertical coordinate
*
*Arguments
*     i   PVLEV(KNIDIM,KNPROF)     : Vertical levels, pressure (source)
*     o   DPVI(KNIDIM,KNPROF)      : Adjoint of vector to be interpolated (source)
*     i   PVIG(KNIDIM,KNPROF)      : Vector to be interpolated (source)
*     o   DPPS(KNPROF)             : Adjoint of surface pressure (destination)
*     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)
*     i   DPVO(KNO,KNPROF)         : Adjoint of interpolated profiles (destination)
*
*    -------------------
**    Purpose: Performs the adjoint calculations of 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),DPVO(KNO,KNPROF)
      REAL*8 DPVI(KNIDIM,KNPROF)
      REAL*8 PVIG(KNIDIM,KNPROF)
      REAL*8 DPPS(KNPROF)
C
      REAL*8     ZPI  (0:KNI+1,KNPROF)
      REAL*8     ZPO  (KNO    ,KNPROF)
      REAL*8     ZDPVI(0:KNI+1,KNPROF)
      REAL*8     ZPVIG(0:KNI+1,KNPROF)
      REAL*8     ZVLEV(0:KNI+1)
      REAL*8     tmpv1
      INTEGER  IL   (KNO    ,KNPROF)
C
      REAL*8 ZW1, ZW2, ZDADPS,ZPRESBPB,ZPRESBPT
      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
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
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
C
      DO JI=1,KNI
         DO JN = 1, KNPROF
           DO JO=1,KNO
               ZRT = ZPO(JO,JN)
               ZP = ZPI(JI,JN)
               XI = SIGN(1.D0,IORDER*(ZRT-ZP))
               IL(JO,JN) = IL(JO,JN) + MAX(0.0D0,XI)
            ENDDO
         ENDDO
      ENDDO
C
C*    .  3.2  Fill extra levels, for constant value extrapolation
C     .       ---------------------------------------------------
C
 320     CONTINUE
C
      DO JN = 1, KNPROF
         DO JK = 1, KNI
            ZVLEV (JK) = VHYBINC (JK)
            ZPVIG(JK,JN) = PVIG(JK,JN)
         ENDDO
      ENDDO
      ZVLEV (0    ) = VHYBINC (1  )
      ZVLEV (KNI+1) = VHYBINC (KNI)
      DO JN = 1, KNPROF
         ZPVIG(0    ,JN) = PVIG(1  ,JN)
         ZPVIG(KNI+1,JN) = PVIG(KNI,JN)
      ENDDO
C
C
C**    Initialize adjoint of profile
C
      DO JN = 1, KNPROF
         DO JK = 0, KNI+1
            ZDPVI(JK,JN) = 0.0
         ENDDO
      ENDDO
C
C
C*    .  3.3  Adjoint of interpolation/extrapolation
C     .       --------------------------------------
C
 330     CONTINUE
C
cpik
      tmpv1=rptopinc/rprefinc
      DO JN = 1, KNPROF
         DO JO=1,KNO
*vdir nodep
            IK = IL(JO,JN)
            ZP = ZPO(JO,JN)
            ZP1 = ZPI(IK  ,JN)
            ZP2 = ZPI(IK+1,JN)
            ZW1 = MAX(dble(IK-KNI+1),MIN( dble(IK),
     S            LOG(ZP/ZP2)/LOG(ZP1/ZP2)))

            ZW2 = 1. - ZW1
C
            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
C
            ZDPVI(IK  ,JN) = ZDPVI(IK  ,JN) + ZW1*DPVO(JO,JN)
            ZDPVI(IK+1,JN) = ZDPVI(IK+1,JN) + ZW2*DPVO(JO,JN)
            DPPS(JN) = DPPS(JN) + (ZPVIG(IK+1,JN)-ZPVIG(IK,JN))*
     &                            ZDADPS*DPVO(JO,JN)
         ENDDO
      ENDDO
C
      DO JN = 1, KNPROF
         DO JK = 1, KNI
            DPVI(JK,JN) = ZDPVI(JK,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