!-------------------------------------- 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 caldiv_2 - Computes horizontal divergence of wind-like fields 
*
#include "model_macros_f.h"
*

      subroutine caldiv_2 ( F_div, F_uu, F_vv, DIST_DIM, Nk ) 15
*
      implicit none
*
      integer  DIST_DIM, Nk
      real     F_div(DIST_SHAPE,Nk), F_uu  (DIST_SHAPE,Nk),
     $         F_vv (DIST_SHAPE,Nk)
*
*authors
*      Methot/Patoine - cmc - nov 95 
*
*revision
* v2_00 - Desgagne M.       - initial MPI version (from caldiv v1_03)
* v3_10 - Corbeil & Desgagne & Lee - AIXport+Opti+OpenMP
*
*object
*     see id section 
*
*arguments
*  Name        I/O                 Description
*----------------------------------------------------------------
* F_div         O        the resulted divergence field on G-grid
* F_uu          I        wind-like field on U-grid
* F_vv          I        wind-like field on V-grid
*----------------------------------------------------------------
*
*implicits
#include "glb_ld.cdk"
#include "geomg.cdk"
*
      integer i, j, k, i0, in, j0, jn
      real*8 inv_hxu_8(XDIST_SHAPE), inv_cy2_8(YDIST_SHAPE),
     $       inv_hsyv_8(YDIST_SHAPE)
**
*     __________________________________________________________________
*
      i0 = 1
      in = l_niu
      j0 = 1
      jn = l_njv
      if ((G_lam).and.(l_west)) i0 = 2
      if (l_south) j0 = 2
*
      do i = i0-1, in
         inv_hxu_8(i) = 1.0d0 / geomg_hxu_8(i)
      end do
*
      do j = 1, l_nj
         inv_cy2_8(j)  = 1.0d0 / geomg_cy2_8(j)
         inv_hsyv_8(j) = 1.0d0 / geomg_hsyv_8(j)
      end do
      inv_hsyv_8(0) = 1.0d0 / geomg_hsyv_8(0)
*
      do k=1,Nk
         do j = j0, jn
         do i = i0, l_niu
            F_div(i,j,k) = ( F_uu(i,j,k) - F_uu(i-1,j,k) ) 
     $                      *( inv_cy2_8(j)*inv_hxu_8(i-1) )
     $                 + (F_vv(i,j,k)-F_vv(i,j-1,k))*inv_hsyv_8(j-1)
         end do
         end do
*
         if (.not.G_lam) then
            if (l_south) then
            do i = i0, in
               F_div(i,1,k) = ( F_uu(i,1,k) - F_uu(i-1,1,k) ) 
     $                         *( inv_cy2_8(1)*inv_hxu_8(i-1) )
     $                      + F_vv(i,1,k)*inv_hsyv_8(0)
            end do
            endif
            if (l_north) then
            do i = i0, in
               F_div(i,l_nj,k) = ( F_uu(i,l_nj,k) - F_uu(i-1,l_nj,k) ) 
     $                             *( inv_cy2_8(l_nj)*inv_hxu_8(i-1) )
     $                         - F_vv(i,l_nj-1,k)*inv_hsyv_8(l_nj-1)
            end do
            endif
         endif
      end do
*
      return
      end