!-------------------------------------- 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 EXTRAP ( PROFIN, PROFOUT, JPMOLEV, JPLEV, KNPF ) 1
#if defined (DOC)
*
***s/r EXTRAP  - Extrapolate temperature profile above 20mb
*                on RTTOV levels (up to 0.1 mbs).
*
*
*Author        : J. Halle *CMDA/AES  April 28, 1994
*
*Revision 001  : J. Halle *CMDA/AES  December 07, 1994
*                new extrapolation coefficients
*
*Revision 002  : J. Halle *CMDA/AES  Oct 1999
*                adapt to new levels (RTTOV5)
*
*Revision 003  : J. Halle *CMDA/AES  dec 2000
*                adapt to TOVS level 1b.
*
*Revision 004  : JM Belanger *CMDA/SMC* june 2001
*                32 bits conversion.
*
*Revision 005  : J. Halle *CMDA/SMC  may 2002
*                adapt to RTTOV-7.
*
*Revision 006  : J. Halle *CMDA/SMC  dec 2004
*                generalize list of extrapolated levels by using jpmolev and jplev.
*
*Revision 007  : J. Halle *CMDA/SMC  may 2005
*                compute jpmotop from jpmolev and jplev.
*
*Arguments
*     i   PROFIN (JPMOLEV,KNPF) : Temperature profile (to be extrapolated)
*     o   PROFOUT(JPLEV,KNPF)   : Temperature profile (      extrapolated)
*     i   JPMOLEV               : number of levels (RT model) from NWP
*     i   JPLEV                 : number of pressure levels
*     i   KNPF                  : Number of profiles
*
*    -------------------
**    Purpose: extrapolate temperature profile above 20 mb (RTTOV5
*              levels 1 to 12) using temperatures from 400mbs to 20mbs
*              (RTTOV5 levels 13 to 28).
#endif
      IMPLICIT NONE
*implicits
#include "comtovxt.cdk"
*
      INTEGER JI, JJ, JK, KNPF, ILEV, JPMOLEV, JPLEV, NLVLS_XTRAP, JPMOTOP
C
      REAL*8 PROFIN(JPMOLEV,KNPF), PROFOUT(JPLEV,KNPF)

C
C*    1.  Initialize output temperature profile
C     .   -------------------------------------
C
 100  CONTINUE
C
      JPMOTOP = JPLEV - JPMOLEV + 1
      NLVLS_XTRAP = JPLEV - JPMOLEV
      DO JI= NLVLS_XTRAP+1, JPLEV
         DO JK = 1, KNPF
            PROFOUT(JI,JK) = PROFIN(JI-JPMOTOP+1,JK)
         ENDDO
      ENDDO
C
C*    2.  Extrapolation of temperatures
C     .   -----------------------------
C
 200  CONTINUE
C
      DO JJ= 1,NLVLS_XTRAP
         DO JK = 1, KNPF
            PROFOUT(JJ,JK) = TREFOU(JJ)
         ENDDO
      ENDDO
C
      DO JJ=1,NLVLS_XTRAP
         DO JI=1,JPXTLVIN
            ILEV = MLVXTIN(JI)
            DO JK = 1, KNPF
               PROFOUT(JJ,JK) = PROFOUT(JJ,JK) +
     S         COEFF(JI,JJ)*(PROFIN(ILEV-JPMOTOP+1,JK)-TREFIN(JI))
            ENDDO
         ENDDO
      ENDDO
C
      RETURN
      END