#if defined key_agrif
!*************************************************
!
       subroutine prepare_grids_connection
!
!*************************************************
!
!     objet    : prepare la connection dans les zones de recouvrement entre grilles fines
!
!     appelant : main
! on remplit data_to_exchange avec les valeurs des grilles filles en appliquant
! le poids adequat a chaque fois
! ajout de ces valeurs ponderees dans connected_values

USE parameters
use comvars2d
use comvarp2d
use comvarp3d
use agrif_types
use agrifcommonid
use comcolig2d, ONLY : igdu, igdv
use comsiggen, ONLY : hzex, hzey
use comvars3d, ONLY : dsigu
#ifdef key_MPI_2D
USE toolmpi, ONLY : NPROCS,MPI_COMM_MARS, IPROC
     include 'mpif.h'
#endif
implicit none

       integer :: i,j,k
       integer :: glob_i, glob_j
       integer :: nb
       integer :: i1,i2,j1,j2
       integer :: loc_i, loc_j
       real,dimension(:,:,:,:),allocatable :: data_to_exch,data_to_recv
       integer :: ierr_mpi
       integer :: nb_proc
#ifdef key_MPI_2D
       integer,dimension(MPI_STATUS_SIZE) :: status
       integer :: req(0:NPROCS-1)
#else
       integer, parameter :: IPROC = 0
#endif
       integer :: glob_imin, glob_jmin, glob_imax, glob_jmax

       IF (nb_connection_areas == 0) RETURN

       if (agrif_root()) then
         if (Agrif_MaxLevel() > 1) THEN
          STOP 'CONNECTION NOT IMPLEMENTED FOR MORE THAN ONE ZOOM LEVEL'
         endif
         if (allocated(imin_connect)) then
          ! Reset Arrays to zero
           do nb=1,nb_connection_areas
             connected_values(nb)%temp = 0. ! connected_values(nb)%temp(kmax+1,:,:) contient la ssh (?)
             connected_values(nb)%sal  = 0.
             connected_values(nb)%uz  = 0.  ! connected_values(nb)%uz(kmax+1,:,:) contient la vitesse barotrope u
             connected_values(nb)%vz  = 0.  ! connected_values(nb)%vz(kmax+1,:,:) contient la vitesse barotrope v
! pour memoire : et echange des turb ???
           enddo
         endif
         return
       endif

       do nb=1,nb_connection_areas
         IF (has_connection_areas(nb,IPROC)) THEN ! le proc courant (IPROC) a un bout de la zone de connection nb
                         ! i_connect(nb,1:4,IPROC) contient les indices imin,imax,jmin,jmax
                         ! du bout de la zone de connection nb sur le proc IPROC
                         ! les indices sont en numerotation globale par rapport a une grille HR qui couvrirait l ensemble du domaine grossier
       i1=i_connect(nb,1,IPROC)
       i2=i_connect(nb,2,IPROC)
       j1=i_connect(nb,3,IPROC)
       j2=i_connect(nb,4,IPROC)
       allocate(data_to_exch(4,1:kmax+2,i1:i2,j1:j2))
       data_to_exch = 0.
       do j=j1,j2
       do i=i1,i2
        loc_i = i-(Agrif_ix()-1)*agrif_irhox()
        loc_j = j-(Agrif_iy()-1)*agrif_irhoy()

        if (update_at_Tpoints(loc_i,loc_j) /= 3.) then ! update_at_Tpoints=3 en terre
! TEMP ET SAL
        do k=1,kmax
         data_to_exch(1,k,i,j) = temp(k,loc_i,loc_j)
         data_to_exch(2,k,i,j) = sal(k,loc_i,loc_j)
        enddo
! SSH
        k=kmax+1
        data_to_exch(1,k,i,j) = ssh(loc_i,loc_j)

! SCALING
        k=kmax+2
        data_to_exch(1:2,k,i,j) = 1.
       
        do k=1,kmax+2
        data_to_exch(1,k,i,j) = data_to_exch(1,k,i,j) * update_at_Tpoints(loc_i,loc_j)
        data_to_exch(2,k,i,j) = data_to_exch(2,k,i,j) * update_at_Tpoints(loc_i,loc_j)
        connected_values(nb)%temp(k,i,j) = connected_values(nb)%temp(k,i,j) + data_to_exch(1,k,i,j)
        connected_values(nb)%sal(k,i,j) = connected_values(nb)%sal(k,i,j) + data_to_exch(2,k,i,j)
        enddo
        endif
        enddo
        enddo

       glob_imin = (Agrif_ix()-1)*agrif_irhox()+liminp1
       glob_jmin = (Agrif_iy()-1)*agrif_irhoy()+ljmin
       glob_imax = (Agrif_ix()-1)*agrif_irhox()+limax
       glob_jmax = (Agrif_iy()-1)*agrif_irhoy()+ljmax
       i1=max(glob_imin,imin_connect(nb))
       i2=min(glob_imax,imax_connect(nb))
       j1=max(glob_jmin,jmin_connect(nb))
       j2=min(glob_jmax,jmax_connect(nb))

       do j=j1,j2
       do i=i1,i2
        loc_i = i-(Agrif_ix()-1)*agrif_irhox()
        loc_j = j-(Agrif_iy()-1)*agrif_irhoy()

        if (update_at_Upoints(loc_i,loc_j) /= 3.) then
! UZ
         if ((igdu(loc_i,loc_j)/=4).AND.(hex(loc_i,loc_j)/=0.)) then
          do k=1,kmax
           data_to_exch(3,k,i,j) = uz(k,loc_i,loc_j)*hzex(k,loc_i,loc_j)*dsigu(k)
          enddo
          data_to_exch(3,kmax+1,i,j) = u(loc_i,loc_j)
          data_to_exch(3,kmax+2,i,j) = 1.
         else
          do k=1,kmax
           data_to_exch(3,k,i,j) = 0.
          enddo
          data_to_exch(3,kmax+1,i,j) = 0.
          data_to_exch(3,kmax+2,i,j) = 0.
         endif
         if ((i==2465).AND.(j==1945)) then
 print *,'GRILE ',agrif_fixed(),'PROC ',IPROC,data_to_exch(3,kmax+2,i,j),data_to_exch(3,kmax,i,j),&
update_at_Upoints(loc_i,loc_j),uz(kmax,loc_i,loc_j),loc_i,loc_j
         endif
! U

         do k=1,kmax+2
          data_to_exch(3,k,i,j) = data_to_exch(3,k,i,j) * update_at_Upoints(loc_i,loc_j)
          connected_values(nb)%uz(k,i,j)   = connected_values(nb)%uz(k,i,j)   + data_to_exch(3,k,i,j)
         enddo

        endif
        enddo
        enddo

       glob_imin = (Agrif_ix()-1)*agrif_irhox()+limin
       glob_jmin = (Agrif_iy()-1)*agrif_irhoy()+ljminp1
       glob_imax = (Agrif_ix()-1)*agrif_irhox()+limax
       glob_jmax = (Agrif_iy()-1)*agrif_irhoy()+ljmax
       i1=max(glob_imin,imin_connect(nb))
       i2=min(glob_imax,imax_connect(nb))
       j1=max(glob_jmin,jmin_connect(nb))
       j2=min(glob_jmax,jmax_connect(nb))

       do j=j1,j2
       do i=i1,i2

        loc_i = i-(Agrif_ix()-1)*agrif_irhox()
        loc_j = j-(Agrif_iy()-1)*agrif_irhoy()
        if (update_at_Vpoints(loc_i,loc_j) /= 3.) then
!VZ
         if ((igdv(loc_i,loc_j)/=4).and.(hey(loc_i,loc_j)/=0.)) then
          do k=1,kmax
           data_to_exch(4,k,i,j) = vz(k,loc_i,loc_j)*hzey(k,loc_i,loc_j)*dsigu(k)
          enddo
          data_to_exch(4,kmax+1,i,j) = v(loc_i,loc_j)
          data_to_exch(4,kmax+2,i,j) = 1.
         else
          do k=1,kmax
           data_to_exch(4,k,i,j) = 0.
          enddo
          data_to_exch(4,kmax+1,i,j) = 0.
          data_to_exch(4,kmax+2,i,j) = 0.
         endif
! V
         do k=1,kmax+2
          data_to_exch(4,k,i,j) = data_to_exch(4,k,i,j) * update_at_Vpoints(loc_i,loc_j)
          connected_values(nb)%vz(k,i,j)   = connected_values(nb)%vz(k,i,j)   + data_to_exch(4,k,i,j)
         enddo
        endif
       enddo
       enddo

#ifdef key_MPI_2D
       do nb_proc=0,NPROCS-1
          if (thearea_inthegrid(nb,nb_proc).AND.(nb_proc /= IPROC)) then
           CALL MPI_ISEND(data_to_exch,size(data_to_exch),MPI_REAL,nb_proc,1,MPI_COMM_MARS,req(nb_proc),ierr_mpi)
          endif
       enddo
#endif
       endif

#ifdef key_MPI_2D
       if (thearea_inthegrid(nb,IPROC)) then
         do nb_proc=0,NPROCS-1
          if (has_connection_areas(nb,nb_proc).AND.(nb_proc /= IPROC)) then
           allocate(data_to_recv(4,1:kmax+2,i_connect(nb,1,nb_proc):i_connect(nb,2,nb_proc), &
                                 i_connect(nb,3,nb_proc):i_connect(nb,4,nb_proc)))
           CALL MPI_RECV(data_to_recv,size(data_to_recv),MPI_REAL,nb_proc,1,MPI_COMM_MARS,status,ierr_mpi)

           do j=i_connect(nb,3,nb_proc),i_connect(nb,4,nb_proc)
           do i=i_connect(nb,1,nb_proc),i_connect(nb,2,nb_proc)
             do k=1,kmax+2
               connected_values(nb)%temp(k,i,j) = connected_values(nb)%temp(k,i,j) + data_to_recv(1,k,i,j)
               connected_values(nb)%sal(k,i,j)  = connected_values(nb)%sal(k,i,j)  + data_to_recv(2,k,i,j)
               connected_values(nb)%uz(k,i,j)   = connected_values(nb)%uz(k,i,j)   + data_to_recv(3,k,i,j)
               connected_values(nb)%vz(k,i,j)   = connected_values(nb)%vz(k,i,j)   + data_to_recv(4,k,i,j)
             enddo
           enddo
           enddo
           deallocate(data_to_recv)
          endif
         enddo
       endif
#endif

         IF (has_connection_areas(nb,IPROC)) THEN
#ifdef key_MPI_2D
       do nb_proc=0,NPROCS-1
          if (thearea_inthegrid(nb,nb_proc).AND.(nb_proc /= IPROC)) then
           CALL MPI_WAIT(req(nb_proc),status,ierr_mpi)
          endif
       enddo
#endif
            deallocate(data_to_exch)
       endif

       enddo

       end subroutine prepare_grids_connection

!*************************************************
!
       subroutine make_grids_connection
!
!*************************************************
!
!     objet    : fait la connection dans les zones de recouvrement entre grilles fines
!
!     appelant : main

USE parameters
use comvars2d
use comvarp2d
use comvarp3d
use agrif_types
use agrifcommonid
use comcolig2d, ONLY : igdu, igdv
use comsiggen, ONLY : hzex, hzey
use comvars3d, ONLY : dsigu
#ifdef key_MPI_2D
USE toolmpi,   ONLY : IPROC
#endif
implicit none

       integer :: i,j,k
       integer :: nb
       integer :: i1,i2,j1,j2
       integer :: ierr_mpi
       integer :: loc_i, loc_j
       integer :: glob_imin, glob_imax, glob_jmin, glob_jmax
       real :: cff1
#if ! defined key_MPI_2D
       integer, parameter :: IPROC = 0
#endif
       if (agrif_root()) return
       IF (nb_connection_areas == 0) RETURN

       do nb=1,nb_connection_areas
         if (has_connection_areas(nb,IPROC)) then
           glob_imin = (Agrif_ix()-1)*agrif_irhox()+liminm1
           glob_jmin = (Agrif_iy()-1)*agrif_irhoy()+ljminm1
           glob_imax = (Agrif_ix()-1)*agrif_irhox()+limaxp2
           glob_jmax = (Agrif_iy()-1)*agrif_irhoy()+ljmaxp2

         
           i1=max(glob_imin,imin_connect(nb))
           i2=min(glob_imax,imax_connect(nb))
           j1=max(glob_jmin,jmin_connect(nb))
           j2=min(glob_jmax,jmax_connect(nb))

           do j=j1,j2
           do i=i1,i2
             loc_i = i-(Agrif_ix()-1)*agrif_irhox()
             loc_j = j-(Agrif_iy()-1)*agrif_irhoy()
             if (update_at_Tpoints(loc_i,loc_j) /= 3.) then
               do k=1,kmax
                 temp(k,loc_i,loc_j) = connected_values(nb)%temp(k,i,j)
                 sal(k,loc_i,loc_j) = connected_values(nb)%sal(k,i,j)
               enddo
             endif
           enddo
           enddo

           glob_imin = (Agrif_ix()-1)*agrif_irhox()+liminm2
           glob_jmin = (Agrif_iy()-1)*agrif_irhoy()+ljminm2
           glob_imax = (Agrif_ix()-1)*agrif_irhox()+limaxp2
           glob_jmax = (Agrif_iy()-1)*agrif_irhoy()+ljmaxp2

         
           i1=max(glob_imin,imin_connect(nb))
           i2=min(glob_imax,imax_connect(nb))
           j1=max(glob_jmin,jmin_connect(nb))
           j2=min(glob_jmax,jmax_connect(nb))

           do j=j1,j2
           do i=i1,i2
             loc_i = i-(Agrif_ix()-1)*agrif_irhox()
             loc_j = j-(Agrif_iy()-1)*agrif_irhoy()
             if (update_at_Tpoints(loc_i,loc_j) /= 3.) then
               !ssh(loc_i,loc_j) = connected_values(nb)%temp(kmax+1,i,j)
             endif
           enddo
           enddo

! on ne peut pas faire la connection sur tout les tableaux de vitesse (liminm2:limaxp2,ljminm2:ljmaxp2)
! parce que les igdu, igdv, hzex, hzey ne sont pas definis sur les memes bornes
! (ce qui est zarbi)
! ca force a refaire un echange 2D derriere (call exchange_uv)

           glob_imin = (Agrif_ix()-1)*agrif_irhox()+limin
           glob_jmin = (Agrif_iy()-1)*agrif_irhoy()+ljmin
           glob_imax = (Agrif_ix()-1)*agrif_irhox()+limax
           glob_jmax = (Agrif_iy()-1)*agrif_irhoy()+ljmax

         
           i1=max(glob_imin,imin_connect(nb))
           i2=min(glob_imax,imax_connect(nb))
           j1=max(glob_jmin,jmin_connect(nb))
           j2=min(glob_jmax,jmax_connect(nb))

           do j=j1,j2
           do i=i1,i2
             loc_i = i-(Agrif_ix()-1)*agrif_irhox()
             loc_j = j-(Agrif_iy()-1)*agrif_irhoy()
             if (update_at_Upoints(loc_i,loc_j) /= 3.) then
               cff1 = connected_values(nb)%uz(kmax+2,i,j)
               if ((cff1 /=0.).and.(igdu(loc_i,loc_j) /= 4).and.(hex(loc_i,loc_j)/=0.)) then
                 do k=1,kmax
                 !uz(k,loc_i,loc_j) = connected_values(nb)%uz(k,i,j)/(hzex(k,loc_i,loc_j)*dsigu(k))
                 enddo
                 !uz(:,loc_i,loc_j) = uz(:,loc_i,loc_j)/cff1
                 !u(loc_i,loc_j)=SUM(uz(:,loc_i,loc_j)*hzex(:,loc_i,loc_j)*dsigu(:))/hex(loc_i,loc_j)
               else ! ce else est probablement inutile (car dans ce cas uz et u doivent deja etre a 0)
                if (cff1 == 0.) then ! je ne sais pas quoi faire si cff1/=0. ...
                 ! uz(:,loc_i,loc_j) = 0.
                 ! u(loc_i,loc_j) = 0.
                endif
               endif
             endif
           enddo
           enddo

           do j=j1,j2
           do i=i1,i2
             loc_i = i-(Agrif_ix()-1)*agrif_irhox()
             loc_j = j-(Agrif_iy()-1)*agrif_irhoy()
             if (update_at_Vpoints(loc_i,loc_j) /= 3.) then
               cff1 = connected_values(nb)%vz(kmax+2,i,j)
               if ((cff1 /=0.).and.(igdv(loc_i,loc_j) /= 4).and.(hey(loc_i,loc_j)/=0.)) then
                 do k=1,kmax
                 !vz(k,loc_i,loc_j) = connected_values(nb)%vz(k,i,j)/(hzey(k,loc_i,loc_j)*dsigu(k))
                 enddo
                 !vz(:,loc_i,loc_j) = vz(:,loc_i,loc_j)/cff1
                 !v(loc_i,loc_j)=SUM(vz(:,loc_i,loc_j)*hzey(:,loc_i,loc_j)*dsigu(:))/hey(loc_i,loc_j)
               else ! ce else est probablement inutile
                if (cff1 == 0.) then
                  !vz(:,loc_i,loc_j) = 0.
                  !v(loc_i,loc_j)=0.
                endif
               endif
             endif
           enddo
           enddo
         endif
       enddo

#ifdef key_MPI_2D
       call exchange_uv()
#endif
       return
       end subroutine make_grids_connection
#else
       subroutine prepare_grids_connection_empty
       return
       end
#endif
