!-------------------------------------- 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 lphi2ttgd(ptt,pp,ppsg) 1,3
#if defined (DOC)
*
***s/r lphi2ttgd  - Go from mass variable P to TT using hydrostatic operator.
*
*Author  : L. Fillion *ARMA/EC 6 may 2008.
*     .
*Revision: L. Fillion *ARMA/EC 7 Nov 2008. - Upgrade.
**
*Arguments
*     in-
*   pp     : 3D Mass variable P Incr. appearing on r.h.s. of TL-eq.
*   ppsg   : Basic-state Surface-pressure
     out-
*   ptt    : 3D TT field computed from TL-Hydrostatic equation
#endif
C
      IMPLICIT NONE
#include "pardim.cdk"
#include "comcst.cdk"
#include "comdim.cdk"
#include "comlun.cdk"
#include "comgem.cdk"
#include "comode.cdk"
*
      real*8 pp(ni,nflev,nj), ptt(ni,nflev,nj)
      real*8 ppsg(ni,nj)
*
      logical llprint
      integer jk, JLA, index,ILEV,ILEN, JLAT, ILON, JLON, IERR
      integer ji,jj
      real*8  zalpha,zcon
      real*8 zpress(nflev*ni),zhybm,zprof(nflev)
      real*8 zp(nflev)
      real*8 ztt(nflev)
      real*8 ztfac(nflev)
      real*8 ztt1d(nflev), zphi1d(nflev)
*------------------------------------------------------------------
**
      WRITE(NULOUT,FMT='(/,4X,"Starting LPHI2TTGD",//)')
      llprint = .false.
!
      zalpha=-1.d0
      zcon = -1./rd
      write(nulout,*) 'lphi2ttgd: vhybinc=',vhybinc
      write(nulout,*) 'lphi2ttgd: rptopinc=',rptopinc
      write(nulout,*) 'lphi2ttgd: rprefinc=',rprefinc
      write(nulout,*) 'lphi2ttgd: rcoefinc=',rcoefinc
!
      do jj=1,nj
         call calcpres(zpress,vhybinc,nflev,ppsg(1,jj),rptopinc
     &                ,rprefinc,rcoefinc,ni)
         do ji = 1, ni
           llprint = .false.
           do jk = 1,nflev
             index = jk+(ji-1)*nflev
             zprof(jk) = zpress(index)
             if(ji.eq.1.and.jj.eq.1) then
               write(nulout,*) 'inmi_solphi: jk,zprof(jk)=',jk,zprof(jk)
               llprint = .true.
             endif
             zphi1d(jk) = pp(ji,jk,jj)
           enddo
           call matapatx(zprof,zalpha,nflev,llprint)
           call vpat(ztt1d,zphi1d,zcon,nflev,1,1,vmd,vme,vmf)
           do jk=1,nflev
             ptt(ji,jk,jj) = ztt1d(jk)
           enddo
         enddo
       enddo
c
      return
      end