!-------------------------------------- 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 --------------------------------------
***s/p v4d_zonewnd - Preparation (kind=1) or Closing (kind=2) for polar correction
*                    in case of u-v wind fields, based on EZ_DEFZONES
*                    (Y.Chartier EZSCINT 2001)
*
#include "model_macros_f.h"
*

      subroutine v4d_zonewnd (px,py,npts,fldu,fldv,wx_8,cox_8,six_8,siy_8,i1,i2,j1,j2, 2,3
     %                        nk,jmin,jmax,ni,nimax,gni,grtypi,degree,var,
     %                        l_north,l_south,kind)
*
      use v4dzone
*
      implicit none
*
      integer npts,i1,i2,j1,j2,nk,jmin,jmax,ni,nimax,gni,degree,kind
*
      real,   pointer, dimension(:)   :: px,py
*
      real fldu(i1:i2,j1:j2,nk),fldv(i1:i2,j1:j2,nk)
*
      character*2 var
*
      character*1 grtypi
*
      real*8 wx_8(*),cox_8(*),six_8(*),siy_8(*)
*
      logical l_north,l_south
*
*author Tanguay M.
*
*revision
* v3_00 - Tanguay M.        - initial MPI version
* v3_11 - Tanguay M.        - select processors at north or south 
*
*object
*     see id section
*
*arguments
* Name         I/O        Description
*----------------------------------------------------------------
* px            I         Position x in INPUT grid
* py            I         Position y in INPUT grid
* npts          I         Number of positions
* fldu-v        I         Wind fields on INPUT grid
* wx            I         Weights on INPUT grid x axe
* cox,six,siy   I         Cosinus or Sinus of INPUT grid axes
* i1-i2         I         Dimension x in INPUT grid
* j1-j2         I         Dimension y in INPUT grid
* nk            I         Dimension z in INPUT grid
* jmin          I         Lower  limit j
* jmax          I         Higher limit j
* ni            I         Period if grid='G', Heart if grid = 'Z'
* nimax         I         ni maximal over all processors
* gni           I         Global dimension of a latitude circle
* grtypi        I         Type of INPUT grid
* degree        I         Degree of interpolation
* var           I         Name of fldsc
* l_north       I         TRUE if processor near north pole
* l_south       I         TRUE if processor near south pole
* kind          I         kind=1=Preparation, kind=2=Closing
*----------------------------------------------------------------
*
*implicits
#include "ptopo.cdk"
*
      integer i,j,k,ier,ig,status
*
      real linel(2,nk,ni),reverse(2,nk,gni),lineg(2,gni,nk)
*
      real*8 vx_8,vy_8,coef1_8,coef2_8
*
*     --------------------------------
*     Preparation for polar correction
*     --------------------------------
      if(kind.eq.1) then
*
*        Accumulate px,py positions in zones near north and south poles
*        --------------------------------------------------------------
         if(npts.gt.0) then
            call v4d_pxpypole (px,py,npts,i1,i2,nk,jmin,jmax,degree,l_north,l_south,kind)
         else
            V4dz_nzon(1) = 0
            V4dz_nzon(2) = 0
         endif
*
*        Calculations in zone near north pole
*        ------------------------------------
*
*           Gather global last latitude if GRTYPI='Z'
*           -----------------------------------------
            if(l_north.and.grtypi.eq.'Z') then
*
               do k = 1,nk
               do i = 1,ni
                  linel(1,k,i) = fldu(i,jmax,k)
                  linel(2,k,i) = fldv(i,jmax,k)
               enddo
               enddo
*
               call rpn_comm_Allgather (linel,2*ni*nk,"MPI_REAL",reverse,
     $                                  2*nimax*nk,"MPI_REAL","EW",ier)
*
               do k = 1,nk
               do i = 1,gni
                  lineg(1,i,k) = reverse(1,k,i)
                  lineg(2,i,k) = reverse(2,k,i)
               enddo
               enddo
*
*           Store global last latitude if GRTYPI='G'
*           ----------------------------------------
            elseif(grtypi.eq.'G') then
*
               do k = 1,nk
               do i = 1,gni
                  lineg(1,i,k) = fldu(i,jmax,k) 
                  lineg(2,i,k) = fldv(i,jmax,k) 
               enddo
               enddo
*
            endif
*
*           Estimate value and fill latitude circle at north pole
*           based on ADV_POLW
*           -----------------------------------------------------
            if(V4dz_nzon(1).ne.0) then
*
               allocate ( V4dz_linepolun(i1:i2,nk), STAT=status )
               allocate ( V4dz_linepolvn(i1:i2,nk), STAT=status )
*
               j       = jmax 
               coef1_8 =  1.0
               coef2_8 = -1.0
*
               do k = 1,nk
*
                  vx_8 = 0.0
                  vy_8 = 0.0
*
                  do i=1,gni
                     vx_8 = vx_8 + wx_8 (i) * (six_8(i) * dble(lineg(1,i,k)) + 
     %                             cox_8(i) *  siy_8(j) * dble(lineg(2,i,k)))
                     vy_8 = vy_8 + wx_8 (i) * (cox_8(i) * dble(lineg(1,i,k)) - 
     %                             six_8(i) *  siy_8(j) * dble(lineg(2,i,k)))
                  enddo
*
                  do i=1,gni
                     lineg(1,i,k) = vx_8 * six_8(i) + vy_8 * cox_8(i)
                     lineg(2,i,k) = coef1_8 * vx_8 * cox_8(i) + coef2_8 * vy_8 * six_8(i)
                  enddo
                  do i=i1,i2
                     ig = i+Ptopo_gindx(1,Ptopo_myproc+1)-1
                     if(ig.ge.1.and.ig.le.gni) then
                        V4dz_linepolun(i,k) = lineg(1,ig,k) 
                        V4dz_linepolvn(i,k) = lineg(2,ig,k) 
                     elseif(ig.lt.  1) then
                        V4dz_linepolun(i,k) = lineg(1,ig+gni,k)
                        V4dz_linepolvn(i,k) = lineg(2,ig+gni,k)
                     elseif(ig.gt.gni) then
                        V4dz_linepolun(i,k) = lineg(1,ig-gni,k)
                        V4dz_linepolvn(i,k) = lineg(2,ig-gni,k)
                     endif
                  enddo
*
               enddo
*
            endif
*
*        Calculations in zone near south pole
*        ------------------------------------
*
*           Gather global first latitude if GRTYPI='Z'
*           -----------------------------------------
            if(l_south.and.grtypi.eq.'Z') then
*
               do k = 1,nk
               do i = 1,ni
                  linel(1,k,i) = fldu(i,jmin,k)
                  linel(2,k,i) = fldv(i,jmin,k)
               enddo
               enddo
*
               call rpn_comm_Allgather (linel,2*ni*nk,"MPI_REAL",reverse,
     $                                  2*nimax*nk,"MPI_REAL","EW",ier)
*
               do k = 1,nk
               do i = 1,gni
                  lineg(1,i,k) = reverse(1,k,i)
                  lineg(2,i,k) = reverse(2,k,i)
               enddo
               enddo
*
*           Store global first latitude if GRTYPI='G'
*           -----------------------------------------
            elseif(grtypi.eq.'G') then
*
               do k = 1,nk
               do i = 1,gni
                  lineg(1,i,k) = fldu(i,jmin,k)
                  lineg(2,i,k) = fldv(i,jmin,k)
               enddo
               enddo
*
            endif
*
*           Estimate value and fill latitude circle at south pole
*           based on ADV_POLW
*           -----------------------------------------------------
            if(V4dz_nzon(2).ne.0) then
*
               allocate ( V4dz_linepolus(i1:i2,nk), STAT=status )
               allocate ( V4dz_linepolvs(i1:i2,nk), STAT=status )
*
               j       = jmin 
               coef1_8 = -1.0
               coef2_8 =  1.0
*
               do k = 1,nk
*
                  vx_8 = 0.0
                  vy_8 = 0.0
*
                  do i=1,gni
                     vx_8 = vx_8 + wx_8 (i) * (six_8(i) * dble(lineg(1,i,k)) +
     %                             cox_8(i) *  siy_8(j) * dble(lineg(2,i,k)))
                     vy_8 = vy_8 + wx_8 (i) * (cox_8(i) * dble(lineg(1,i,k)) -
     %                             six_8(i) *  siy_8(j) * dble(lineg(2,i,k)))
                  enddo
*
                  do i=1,gni
                     lineg(1,i,k) = vx_8 * six_8(i) + vy_8 * cox_8(i) 
                     lineg(2,i,k) = coef1_8 * vx_8 * cox_8(i) + coef2_8 * vy_8 * six_8(i)
                  enddo
                  do i=i1,i2
                     ig = i+Ptopo_gindx(1,Ptopo_myproc+1)-1
                     if(ig.ge.1.and.ig.le.gni) then
                        V4dz_linepolus(i,k) = lineg(1,ig,k)
                        V4dz_linepolvs(i,k) = lineg(2,ig,k)
                     elseif(ig.lt.  1) then
                        V4dz_linepolus(i,k) = lineg(1,ig+gni,k)
                        V4dz_linepolvs(i,k) = lineg(2,ig+gni,k)
                     elseif(ig.gt.gni) then
                        V4dz_linepolus(i,k) = lineg(1,ig-gni,k)
                        V4dz_linepolvs(i,k) = lineg(2,ig-gni,k)
                     endif
                  enddo
*
               enddo
*
            endif
*
*     --------------------------------------
*     Deallocations of ZONES internal arrays
*     --------------------------------------
      elseif(kind.eq.2) then
*
         if(V4dz_nzon(1).gt.0) then
            deallocate( V4dz_linepolun, STAT=status )
            deallocate( V4dz_linepolvn, STAT=status )
         endif
         if(V4dz_nzon(2).gt.0) then
            deallocate( V4dz_linepolus, STAT=status )
            deallocate( V4dz_linepolvs, STAT=status )
         endif
*
*        Additional deallocations
*        ------------------------
         call v4d_pxpypole (px,py,npts,i1,i2,nk,jmin,jmax,degree,l_north,l_south,kind)
*
      endif
*
      return
      end