!-------------------------------------- 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 v4d_bc_2_nest_ad - ADJ of v4d_bc_2_nest_tl 
*
#include "model_macros_f.h"
*

      subroutine v4d_bc_2_nest_ad (the_time) 1,2
*
      implicit none
*
      integer the_time
*
*author M.Tanguay
*
*revision
* v3_31 - Tanguay M.        - initial version
* v3_31 - Tanguay M.        - Control BC
* v3_31 - Tanguay M.        - Add OPENMP directives 
*
*object
*     see id section
*
*implicits
#include "glb_ld.cdk"
#include "lun.cdk"
#include "schm.cdk"
#include "nest.cdk"
#include "tr3d.cdk"
#include "ind.cdk"
#include "v4d_bc.cdk"
#include "v4dg.cdk"
*
*     __________________________________________________________________
*
      integer  vmmlod,vmmget,vmmuld
      external vmmlod,vmmget,vmmuld
*
      integer pnlod, pnlkey1(20), pnerr, err
*
      integer key2(Tr3d_ntr),key2_
      real trf
      pointer (patrf, trf(LDIST_SHAPE,*))
*
      integer i,j,k
*
      real*8, parameter :: ZERO_8 = 0.0
*
#include "v4d_key_bc.cdk"
*     __________________________________________________________________
*
      if (.not.Schm_hydro_L) call gem_stop ('V4D_BC_2_NEST_AD',-1)
*
      if (Lun_out.gt.0) write(Lun_out,1003) 
*
      pnlkey1(1)= VMM_KEY(nest_u)
      pnlkey1(2)= VMM_KEY(nest_v)
      pnlkey1(3)= VMM_KEY(nest_s)
      pnlkey1(4)= VMM_KEY(nest_tp)
      pnlod = 4
*
*     - - - - - - - - - - - - - - -
      pnerr = vmmlod(pnlkey1,pnlod)
*     - - - - - - - - - - - - - - -
      pnerr = VMM_GET_VAR(nest_u)
      pnerr = VMM_GET_VAR(nest_v)
      pnerr = VMM_GET_VAR(nest_s)
      pnerr = VMM_GET_VAR(nest_tp)
*
*     Equivalencing IND to NEST
*     -------------------------
      Ind_u_   = nest_u_
      Ind_v_   = nest_v_
      Ind_s_   = nest_s_
      Ind_tp_  = nest_tp_
*
      ntime = the_time
*
#include "v4d_lod_bc.cdk"
#include "v4d_get_bc.cdk"
*
*     ADJ of
*     Set independent NEST 
*     --------------------
*
      key2_ = VMM_KEY (nest_tr)
      do n_tr=1,Tr3d_ntr
         key2(n_tr) = key2_ + n_tr
      end do
*
      if (Tr3d_ntr.gt.0) then
          err = vmmlod(key2,Tr3d_ntr)
          do n_tr=1,Tr3d_ntr
*
          err    = vmmget(key2     (n_tr),patrf,  trf)
          bc_err = vmmget(key_bc_tr(n_tr),pabc_tr,f_bc_tr)
*
!$omp parallel do
          do k= 1, G_nk
          do j= 1, l_nj
          do i= 1, l_ni
             f_bc_tr(i,j,k) = trf(i,j,k) + f_bc_tr(i,j,k) 
             trf    (i,j,k) = 0. 
          end do
          end do
          end do
!$omp end parallel do
*
          end do
          err = vmmuld(key2,Tr3d_ntr)
      endif
*
!$omp parallel do
      do j= 1, l_nj
      do i= 1, l_ni
         f_bc_s(i,j) = nest_s(i,j) + f_bc_s(i,j)
         nest_s(i,j) = 0.0
      end do
      end do
!$omp end parallel do
*
!$omp parallel do
      do k= 1, G_nk
      do j= 1, l_nj
      do i= 1, l_ni
         f_bc_tp(i,j,k) = nest_tp(i,j,k) + f_bc_tp(i,j,k)
         nest_tp(i,j,k) = 0.0
      end do
      end do
      end do
!$omp end parallel do
*
      if (.not.(V4dg_4dvar_L.or.V4dg_sgvc_L)) call v4d_uv2img_ad ( )
*
!$omp parallel do
      do k= 1, G_nk
      do j= 1, l_njv
      do i= 1, l_ni
         f_bc_v(i,j,k) = nest_v(i,j,k) + f_bc_v(i,j,k)
         nest_v(i,j,k) = 0.0
      end do
      end do
      end do
!$omp end parallel do
*
!$omp parallel do
      do k= 1, G_nk
      do j= 1, l_nj
      do i= 1, l_niu
         f_bc_u(i,j,k) = nest_u(i,j,k) + f_bc_u(i,j,k)
         nest_u(i,j,k) = 0.0
      end do
      end do
      end do
!$omp end parallel do
*
#include "v4d_unlod_bc.cdk"
*
      pnerr = vmmuld(pnlkey1,pnlod)
*
1003  format(//,'ADJ of PREPROCESSING DATA: (S/R V4D_BC_2_NEST_AD)',
     %        /,'=================================================',//)
*
*     __________________________________________________________________
      return
      end