!-------------------------------------- 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/r vzpng_zmean - Calculate add/substract mean zonal wind component
*
#include "model_macros_f.h"
*

      subroutine vspng_zmean( F_pert,F_field,tsum,DIST_DIM, Nk, lmean ) 4
      implicit none
*
      integer DIST_DIM, Nk
      real F_field(DIST_SHAPE,Nk),F_pert(DIST_SHAPE,Nk),tsum(l_nj,Nk)
      logical lmean
*
*author
*     Sylvie Gravel  - April 2006
*
*revision
* v3_30 - Spacek L.      - initial MPI version
*     
*object
*     The subroutine calculates the zonal mean of the zonal
*     of the wind component and adds/substracts it to/from
*     the corresponding component. It is supposed to be used only for
*     non rotated grid.
*     
*implicits
#include "glb_ld.cdk"
*
      integer i,j,k
      integer i0,in,j0,jn,k0,kn,njk,err
      real*8 sum(l_nj,Nk),gsum(l_nj,Nk)
*
*--------------------------------------------------------------------
*
      i0 = 1
      in = l_ni
      j0 = 1
      jn = l_nj
      k0 = 1
      kn = Nk
      njk= l_nj*Nk
*
      if(lmean) then
*
!$omp parallel
!$omp do
         do k=k0,kn
            do j=j0,jn
               sum(j,k) = 0.
               do i=i0,in
                  sum(j,k) = dble(F_field(i,j,k))+sum(j,k)
               end do
            end do
         end do
!$omp enddo
!$omp end parallel
*
         call rpn_comm_ALLREDUCE ( sum, gsum, l_nj*Nk,
     $                "MPI_DOUBLE_PRECISION","MPI_SUM","EW",err )
*
         tsum=gsum/dble(G_ni)
*
!$omp parallel
!$omp do
         do k=k0,kn
            do j=j0,jn
               do i=i0,in
                  F_pert(i,j,k) = F_field(i,j,k) - tsum(j,k)
               end do
            end do
         end do
!$omp enddo
!$omp end parallel
*
      else
!$omp parallel
!$omp do
         do k=k0,kn
            do j=j0,jn
               do i=i0,in
                  F_pert(i,j,k) = F_field(i,j,k) + tsum(j,k)
               end do
            end do
         end do
!$omp enddo
!$omp end parallel
      endif
*
*----------------------------------------------------------------
*
      return
      end subroutine vspng_zmean