!-------------------------------------- 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 initpc(pgdpsi,pgdchi,pgdvort,pgddiv,cduvgrid) 1,4
!
      IMPLICIT NONE
#if defined (DOC)
!
!s/r initpc : Shell controlling computation of pc from zd  
!
!     Author L. Fillion  - ARMA/EC - 4 Feb 2010.
!Revision:
!
#endif
!
#include "taglam4d.cdk"
#include "pardim.cdk"
#include "comdim.cdk"
#include "comgrd_param.cdk"
#include "comlun.cdk"
#include "comct0.cdk"
#include "comcst.cdk"
#include "comgd1.cdk"
#include "comgdpar.cdk"
#include "namgdpar.cdk"
#include "compdg.cdk"
#include "comgem.cdk"
#include "comgrd.cdk"
#include "comcva.cdk"
!
      character*1 cduvgrid
      real*8 pgdpsi(ni,nflev,nj)
      real*8 pgdchi(ni,nflev,nj)
      real*8 pgdvort(ni,nflev,nj)
      real*8 pgddiv(ni,nflev,nj)
!
      character*1 clpart
      logical llbiais,llfilt,ldwindint
      integer ji,jj,jlev,jvar,inij,inip1,injp1,iflag,ivrbl
      integer idum1,idum2,idum3,idum4,itrunc,index
      real*8  zmin,zmax
!
      real*8 zu9(0:ni+1,nflev,0:nj+1)
      real*8 zv9(0:ni+1,nflev,0:nj+1)
      real*8 zwh(ni,nj)
      real*8 zwh2(ni,nj)
!
!!
      write(nulout,*) 'Begin initpc'
!
      llfilt = .false.
      clpart = 'H'
      itrunc = 10  ! used above 500 hPa
      llbiais = .false.
      inip1 = ni+1
      injp1 = nj+1
!
!*1.  Build (PSI, CHI) & (ut1,vt1) derived from Helmhotlz's functions
!     ---------------------------------------------------------------
!
        if(lcva_euclid) then
          write(nulout,*) 'initpc: avant zd2pc_euclid'
          call zd2pc_euclid(pgdpsi,pgdchi,pgdvort,pgddiv)
          write(nulout,*) 'initpc: apres zd2pc_euclid'
!
        else
          do jlev=1,nflev
            do ji=1,ni
              do jj=1,nj
                zwh(ji,jj)=pgdvort(ji,jlev,jj)
              enddo
            enddo
            call invlap(zwh2,zwh,'P',.false.)
            do ji=1,ni
              do jj=1,nj
                pgdpsi(ji,jlev,jj)=zwh2(ji,jj)
              enddo
            enddo
          enddo
!
          do jlev=1,nflev
            do ji=1,ni
              do jj=1,nj
                zwh(ji,jj)=pgddiv(ji,jlev,jj)
              enddo
            enddo
            call invlap(zwh2,zwh,'S',.false.)
            do ji=1,ni
              do jj=1,nj
                pgdchi(ji,jlev,jj)=zwh2(ji,jj)
              enddo
            enddo
          enddo
        endif
!
!       Build final ut1,vt1
!       N.B.: Those are left untouched if cduvgrid = 'S' (see docum at beginning of sub)
!
        if(cduvgrid.eq.'U') then 
          call pc2UVg_c(zu9,zv9,pgdpsi,pgdchi)
          do ji=1,ni
            do jj=1,nj
              do jlev=1,nflev
                ut1(ji,jlev,jj)=zu9(ji,jlev,jj)
                vt1(ji,jlev,jj)=zv9(ji,jlev,jj)
              enddo
            enddo
          enddo
        endif
!
      return
      end