#if defined key_agrif

   !!======================================================================

  SUBROUTINE agrif_update_fields
!
!*************************************************
!
!     objet    : actualisation des valeurs des variables de la grille
!                parent par les valeur des variables sur la grille
!                fine sur tout ou partie de la zone de recouvrement.
!
!
!     appelant         : stepu (contexte agrif, voir stepuagrif.h)
!                      :
!     appeles          : Agrif_Root, Agrif_NbStepint
!     appeles          : Agrif_Rhox, Agrif_Rhoy, Agrif_ixl, Agrif_iyl
!     appeles          : COUNT, SUM
!                      :
!     canal d ecriture :
!                      :
!     canal de lecture :
!                      :
!     cle cpp          : key_agrif
!                      :
!     variables in     : sal, temp, ssh, uz, u
!                      :
!     variables out    : valmanq
!                      :
!     variables inout  :
!                      :
!     dernieres modifs : 19/09/02
!                      : 2016-01 : (S. Petton) Major reviews
!                      :
!***********************************************************
!
#include "toolcpp.h"
   USE parameters
   USE comvars3d
   USE comvars2d
   USE comvarp2d
   USE comvarp3d
   USE comvarp3d
   USE agrifcommonid
#ifdef key_substance
   USE comsubstance
#endif
   IMPLICIT NONE
!
   external :: updatetemp, updatesal, updateuz,updatevz,updatessh
   external :: updateturb, updatekz, updatenz, updatehzex, updatehzey
   external :: updatesurf, updatefwet, updatewz
#ifdef key_substance
   external updatecv_wat
#endif

!
!************************************************************
!     Executable statements
!************************************************************
!
   PRINT_DBG*, 'enter UPDATE_AGRIF routine'

   ! s il n y a pas de grille grand mere et que l on est sur la grille mere : on sort
   IF ( Agrif_Root() .AND. (.not.agrif_coarse)) RETURN
   ! s il y a une grille grand mere et que l on est dessus on sort aussi
   IF (Agrif_GrandMother()) RETURN

   ! si on est une sur une grille fille quelconque et que l on n est pas rendu au meme instant d integration
   ! que sa grille parent on sort
   IF (Agrif_NbStepint().NE.(Agrif_IRhot()-1)) THEN
#if defined key_agrif_conserv  
   IF (Agrif_NbStepint() == 0) THEN
     c_agrif_update(1:2) = 'IN' ! Initialise (I) les flux et ne fait pas l update
   ELSE
     c_agrif_update(1:2) = 'SN' ! Somme (S) les flux et ne fait pas l update
   ENDIF
   
   ! Update of fxeau, estimate hzex if not coarsening and then estimate uz in any case
   CALL Agrif_Update_Variable(uz_update_id, locupdate2=(/-1,-2/), procname = updateuz)

   ! Update of fyeau, estimate hyex if not coarsening and then estimate vz in any case
   CALL Agrif_Update_Variable(vz_update_id, locupdate3=(/-1,-2/), procname = updatevz)
#endif
     RETURN
   ENDIF

   ! a partir de la on va faire de l update soit parce que l on est sur un grille fille rendue
   ! au bon moment, soit on a une grille grand mere et que l on est sur la grille mere

   Agrif_USESpecialValueInUpdate = .TRUE.
   Agrif_SpecialValueFineGrid=valmanq

   ! Update of TEMP SAL and Substances
   IF (.NOT. Agrif_Root() .AND. icon >= 1) THEN
     CALL Agrif_Update_Variable(sal_id ,procname = updatesal)
     CALL Agrif_Update_Variable(temp_id,procname = updatetemp)
#ifdef key_substance
     CALL Agrif_Update_Variable(cv_wat_id,procname = updatecv_wat)
#endif
   END IF
 
   ! Update of SSH (not for coarsening, it will be updated later on)
   IF (.NOT. Agrif_Root()) THEN
     CALL Agrif_Update_Variable(ssh_id ,procname = updatessh)
   END IF

   ! Update of kz (coarsening only)
   IF (Agrif_Root()) THEN
     CALL Agrif_Update_Variable(kz_id,procname = updatekz)
   END IF

   Agrif_USESpecialValueInUpdate = .FALSE.

   ! Update of hzex and hzey for coarsening only (they will be estimated with updated ssh)
   IF (Agrif_Root()) THEN
     CALL Agrif_Update_Variable(hzex_id, locupdate2=(/-1,-2/) , procname = updatehzex) ! locupdate2 :  dimension 2 du tableau 3D (donc x ou i dans mars)
     CALL Agrif_Update_Variable(hzey_id, locupdate3=(/-1,-2/) , procname = updatehzey) ! locupdate3 :  dimension 3 du tableau 3D (donc y ou j dans mars)
   END IF

#if defined key_agrif_conserv 
   IF (Agrif_IRhot() == 1) THEN ! Pas de raffinement temporel
     c_agrif_update(1:2) = 'IY=' ! Initialise (I) les flux et fait l update
   ELSE
     c_agrif_update(1:2) = 'SY' ! Somme (S) les flux et fait l update
   ENDIF
#endif
   
   ! Update of fxeau, estimate hzex if not coarsening and then estimate uz in any case
   CALL Agrif_Update_Variable(uz_update_id, locupdate2=(/-1,-2/), procname = updateuz)

   ! Update of fyeau, estimate hyex if not coarsening and then estimate vz in any case
   CALL Agrif_Update_Variable(vz_update_id, locupdate3=(/-1,-2/), procname = updatevz)

   ! Update fwet
   IF (fwetmin /= 1.0_rsh) THEN
     CALL Agrif_Update_Variable(fwet_id, procname = updatefwet)
   END IF

#if defined key_MPI_2D
   CALL Agrif_ChildGrid_To_ParentGrid() 
   CALL exchange_uv() ! MPI exchanges of uz,vz,u,v
   CALL Agrif_ParentGrid_To_ChildGrid()
#endif

   IF (Agrif_Root()) THEN
     ! Coarsening : Update SSH and estimate Wz everywhere over grandmother domain 
     CALL Agrif_ChildGrid_To_ParentGrid()
     CALL update_wz_coarse()
     CALL Agrif_ParentGrid_To_ChildGrid()
   ELSE 
     ! TODO : for this part, there must be a better way without using procname
     ! Estimate Wz in parent grid over the child domain with updated fluxes (fxeau & fyeau)
     CALL Agrif_Update_Variable(wz_id, procname = updatewz)
#if defined key_MPI_2D
     CALL Agrif_ChildGrid_To_ParentGrid()
     CALL exchange_wz() ! MPI exchanges of wz
     CALL Agrif_ParentGrid_To_ChildGrid()
#endif
     ! TODO : add "Correct Boz (& Turb ?)" in parent grid over the child domain with updated temperature & salinity
     ! CALL Agrif_ChildGrid_To_ParentGrid()
     ! CALL Correct_density_and_turb()
     ! CALL Agrif_ParentGrid_To_ChildGrid()
   END IF

   PRINT_DBG*, 'exit UPDATE_AGRIF routine'

  END SUBROUTINE agrif_update_fields

   !!======================================================================

  SUBROUTINE update_wz_coarse()
#include "toolcpp.h"
   USE parameters
   USE comvars3d
   USE comvars2d
   USE comvarp2d
   USE comvarp3d
   USE agrifcommonid
   USE obccombine, ONLY : l_obc_west,l_obc_east,l_obc_north,l_obc_south
#if defined key_MPI_2D && defined key_diag_physwz
   USE toolmpi, ONLY : ex_i_rsh,ex_j_rsh
#endif

   INTEGER :: i,j
   INTEGER :: test
   REAL(KIND=rsh),DIMENSION(limin:limax)                     :: tfh,tfb
   REAL(KIND=rsh),DIMENSION(ljmin:ljmax)                     :: tfg,tfd
   REAL(KIND=rlg) :: somme_xe,cff1
   REAL(KIND=rsh) :: cff2

   ! Coarsening
   ! Estimate of the ssh from fluxes
   ! ---------------------------------------------------------------

   IF (Agrif_Grandmother()) THEN

     DO j=ljminp2,ljmaxm1
       DO i=MAX0(limin,ig(j)+1),MIN0(limax,id(j)-1)
!     cff2=(ssh_old(i,j)*fwetp(i,j)-(SUM(dsigu(1:kmax)*fxeau(1:kmax,i  ,j))*dy_u(i  ,j) &
!                                      -SUM(dsigu(1:kmax)*fxeau(1:kmax,i-1,j))*dy_u(i-1,j) &
!                                      +SUM(dsigu(1:kmax)*fyeau(1:kmax,i  ,j))*dx_v(i  ,j) &
!                                      -SUM(dsigu(1:kmax)*fyeau(1:kmax,i,j-1))*dx_v(i,j-1) &
!                                      -SUM(phieau(1:kmax,i,j))*halfdt )/(surf(i,j)))/fwet(i,j)
!if (ssh(i,j)/=0.) THEN
!if (abs(ssh(i,j)-cff2)/abs(ssh(i,j)).GT.1.E-3) THEN
! print *,i,j,cff2,ssh(i,j),abs(ssh(i,j)-cff2)/abs(ssh(i,j)),fwet(i,j)
! endif
! endif

         ssh(i,j)=(ssh_old(i,j)*fwetp(i,j) &
                  -(SUM(dsigu(1:kmax)*fxeau(1:kmax,i  ,j))*dy_u(i  ,j) &
                  -SUM(dsigu(1:kmax)*fxeau(1:kmax,i-1,j))*dy_u(i-1,j) &
                  +SUM(dsigu(1:kmax)*fyeau(1:kmax,i  ,j))*dx_v(i  ,j) &
                  -SUM(dsigu(1:kmax)*fyeau(1:kmax,i,j-1))*dx_v(i,j-1) &
                  -SUM(phieau(1:kmax,i,j))*halfdt )/(surf(i,j)))/fwet(i,j)
       END DO
     END DO

   ! western boundary
     IF_MPI (limin==imin) THEN
       IF ( l_obc_west ) THEN
         WHERE (h0(imin+1,ljminm2:ljmaxp2)/=-valmanq)
         ssh(imin+1,ljminm2:ljmaxp2) = ssh(imin+2,ljminm2:ljmaxp2)
       ELSE WHERE
         ssh(imin+1,ljminm2:ljmaxp2) = valmanq
         END WHERE
       END IF
     ENDIF_MPI

   ! eastern boundary
     IF_MPI (limax==imax) THEN
       IF ( l_obc_east ) THEN
         WHERE (h0(imax,ljminm2:ljmaxp2)/=-valmanq)
         ssh(imax,ljminm2:ljmaxp2) = ssh(imax-1,ljminm2:ljmaxp2)
       ELSE WHERE
         ssh(imax,ljminm2:ljmaxp2) = valmanq
         END WHERE
       END IF
     ENDIF_MPI

   ! southern boundary
     IF_MPI (ljmin==jmin) THEN
       IF ( l_obc_south ) THEN
         WHERE (h0(liminm2:limaxp2,jmin+1)/=-valmanq)
         ssh(liminm2:limaxp2,jmin+1) = ssh(liminm2:limaxp2,jmin+2)
       ELSE WHERE
         ssh(liminm2:limaxp2,jmin+1) = valmanq
         END WHERE
       END IF
     ENDIF_MPI

   ! northern boundary
     IF_MPI (ljmax==jmax) THEN
       IF ( l_obc_north ) THEN
         WHERE (h0(liminm2:limaxp2,jmax)/=-valmanq)
         ssh(liminm2:limaxp2,jmax) = ssh(liminm2:limaxp2,jmax-1)
       ELSE WHERE
         ssh(liminm2:limaxp2,jmax) = valmanq
         END WHERE
       END IF
     ENDIF_MPI

#if defined key_MPI_2D && defined key_diag_physwz
   CALL ex_i_rsh(-2,2,1,liminm2,limaxp2,ljminm2,ljmaxp2,ssh(liminm2:limaxp2,ljminm2:ljmaxp2))
   CALL ex_j_rsh(-2,2,1,liminm2,limaxp2,ljminm2,ljmaxp2,ssh(liminm2:limaxp2,ljminm2:ljmaxp2))
#endif

   END IF !if (Agrif_Grandmother())

#if defined key_siggen || defined key_gencoord
   CALL dyn3d_wz(ssh_old,ssh,uz,vz,u,v)
#else
   CALL dyn3d_wz(ssh_old,ssh,uz,vz)
#endif

   RETURN

  END SUBROUTINE update_wz_coarse

   !!======================================================================

  SUBROUTINE Updatewz(tabres,k1,k2,i1,i2,j1,j2,before)
   USE parameters
   USE comvars3d
   USE comvars2d
   USE comvarp2d
   USE comvarp3d
   IMPLICIT NONE
   INTEGER i1,i2,j1,j2,k1,k2
   REAL tabres(k1:k2,i1:i2,j1:j2)
   LOGICAL before
   INTEGER i,j,k

   IF (before) THEN
   ELSE
     CALL  agrif_dyn3d_wz(ssh, i1, i2, j1, j2)
   END IF

  END SUBROUTINE updatewz

   !!======================================================================

  SUBROUTINE Updatesal(tabres,k1,k2,i1,i2,j1,j2,before)
   USE parameters
   USE comvars3d
   USE comvars2d
   USE comvarp2d
   USE comvarp3d
   IMPLICIT NONE
   INTEGER i1,i2,j1,j2,k1,k2
   REAL tabres(k1:k2,i1:i2,j1:j2)
   LOGICAL before
   INTEGER i,j,k

!$OMP PARALLEL

   IF (before) THEN

     IF (first_agrif_update) THEN
!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j,k)
       DO j=j1,j2
         DO i=i1,i2
           DO k=k1,k2
             IF (h0(i,j) == -valmanq) THEN
               tabres(k,i,j) = Agrif_SpecialValueFineGrid
             ELSE
               tabres(k,i,j) = sal(k,i,j)
             END IF
           END DO
         END DO
       END DO
!$OMP END DO
     ELSE
!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j,k)
       DO j=j1,j2
         DO i=i1,i2
           DO k=k1,k2
             IF (h0(i,j)+ssh(i,j) < h0fond) THEN
               tabres(k,i,j) = Agrif_SpecialValueFineGrid
             ELSE
               tabres(k,i,j) = sal(k,i,j)
             END IF
           END DO
         END DO
       END DO
!$OMP END DO
     END IF

   ELSE

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j,k)
     DO j=j1,j2
       DO i=i1,i2
         DO k=k1,k2
           IF ((tabres(k,i,j).NE.Agrif_SpecialValueFineGrid).AND.(h0(i,j)/=-valmanq)) THEN
             sal(k,i,j) = tabres(k,i,j)
           END IF
           IF (h0(i,j) == -valmanq) THEN
             sal(k,i,j) = valmanq
           END IF
         END DO
       END DO
     END DO
!$OMP END DO

   END IF

!$OMP END PARALLEL

   RETURN

  END SUBROUTINE Updatesal

   !!======================================================================

  SUBROUTINE Updatetemp(tabres,k1,k2,i1,i2,j1,j2,before)
   USE parameters
   USE comvars3d
   USE comvars2d
   USE comvarp2d
   USE comvarp3d
   IMPLICIT NONE
   INTEGER i1,i2,j1,j2,k1,k2
   REAL tabres(k1:k2,i1:i2,j1:j2)
   LOGICAL before
   INTEGER i,j,k
   REAL :: test_temp

!$OMP PARALLEL

   IF (before) THEN

     IF (first_agrif_update) THEN
!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j,k)
       DO j=j1,j2
         DO i=i1,i2
           DO k=k1,k2
             IF (h0(i,j) == -valmanq) THEN
               tabres(k,i,j) = Agrif_SpecialValueFineGrid
             ELSE
               tabres(k,i,j) = temp(k,i,j)
             END IF
           END DO
         END DO
       END DO
!$OMP END DO
     ELSE
!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j,k)
       DO j=j1,j2
         DO i=i1,i2
           DO k=k1,k2
             IF (h0(i,j)+ssh(i,j) < h0fond) THEN
               tabres(k,i,j) = Agrif_SpecialValueFineGrid
             ELSE
               tabres(k,i,j) = temp(k,i,j)
             END IF
           END DO
         END DO
       END DO
!$OMP END DO
     END IF

   ELSE ! /* after = parent grid */

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j,k)
     DO j=j1,j2
       DO i=i1,i2
         DO k=k1,k2
           IF ((tabres(k,i,j).NE.Agrif_SpecialValueFineGrid).AND.(h0(i,j)/=-valmanq)) THEN
             temp(k,i,j) = tabres(k,i,j)
           END IF
           IF (h0(i,j) == -valmanq) THEN
             temp(k,i,j) = valmanq
           END IF
         END DO
       END DO
     END DO
!$OMP END DO

   END IF ! /* test on before */

!$OMP END PARALLEL

   RETURN
  END SUBROUTINE Updatetemp

   !!======================================================================

  SUBROUTINE Updateturb(tabres,l1,l2,k1,k2,i1,i2,j1,j2,before)
   USE parameters
   USE comvars3d
   USE comvars2d
   USE comvarp2d
   USE comvarp3d
   USE comturb
   IMPLICIT NONE
   INTEGER i1,i2,j1,j2,k1,k2,l1,l2
   REAL tabres(l1:l2,k1:k2,i1:i2,j1:j2)
   LOGICAL before

   INTEGER i,j,k,l

!$OMP PARALLEL

   IF (before) THEN

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j,k,l)
     DO j=j1,j2
       DO i=i1,i2
         DO k=k1,k2

           IF (h0(i,j) == -valmanq) THEN
             DO l=l1,l2
               tabres(l,k,i,j) = Agrif_SpecialValueFineGrid
             END DO
           ELSE
             tabres(1,k,i,j) = ect(k,i,j)
             tabres(2,k,i,j) = psi(k,i,j)
             IF (turb_nbeq == 2) THEN
               tabres(3,k,i,j) = eps(k,i,j)
             END IF
           END IF
         END DO

       END DO
     END DO
!$OMP END DO

   ELSE

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j,k)
     DO j=j1,j2
       DO i=i1,i2
         DO k=k1,k2
           IF ((tabres(1,k,i,j).NE.Agrif_SpecialValueFineGrid).AND.(h0(i,j)/=-valmanq)) THEN
             ect(k,i,j) = tabres(1,k,i,j)
             psi(k,i,j) = tabres(2,k,i,j)
             IF (turb_nbeq == 2) THEN
               eps(k,i,j) = tabres(3,k,i,j)
             END IF

           ELSE
             ect(k,i,j) = valmanq
             psi(k,i,j) = valmanq
             IF (turb_nbeq == 2) THEN
               eps(k,i,j) = valmanq
             END IF

           END IF
         END DO
       END DO
     END DO
!$OMP END DO

   END IF

!$OMP END PARALLEL

   RETURN

  END SUBROUTINE Updateturb

   !!======================================================================

  SUBROUTINE Updatekz(tabres,k1,k2,i1,i2,j1,j2,before)
   USE parameters
   USE comvars3d
   USE comvars2d
   USE comvarp2d
   USE comvarp3d
   USE comturb
   IMPLICIT NONE
   INTEGER i1,i2,j1,j2,k1,k2
   REAL tabres(k1:k2,i1:i2,j1:j2)
   LOGICAL before

   INTEGER i,j,k

!$OMP PARALLEL

   IF (before) THEN

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j,k)
     DO j=j1,j2
       DO i=i1,i2
         DO k=k1,k2
           IF (h0(i,j) == -valmanq) THEN
             tabres(k,i,j) = Agrif_SpecialValueFineGrid
           ELSE
             tabres(k,i,j) = kz(k,i,j)
           END IF
         END DO

       END DO
     END DO
!$OMP END DO

   ELSE

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j,k)
     DO j=j1,j2
       DO i=i1,i2
         DO k=k1,k2
           IF ((tabres(k,i,j).NE.Agrif_SpecialValueFineGrid).AND.(h0(i,j)/=-valmanq)) THEN
             kz(k,i,j)  = tabres(k,i,j)
           ELSE
             kz(k,i,j) = valmanq
           END IF
         END DO
       END DO
     END DO
!$OMP END DO

   END IF

!$OMP END PARALLEL

   RETURN
  END SUBROUTINE Updatekz

   !!======================================================================

  SUBROUTINE Updatenz(tabres,k1,k2,i1,i2,j1,j2,before)
   USE parameters
   USE comvars3d
   USE comvars2d
   USE comvarp2d
   USE comvarp3d
   USE comturb
   IMPLICIT NONE
   INTEGER i1,i2,j1,j2,k1,k2
   REAL tabres(k1:k2,i1:i2,j1:j2)
   LOGICAL before

   INTEGER i,j,k

!$OMP PARALLEL

   IF (before) THEN

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j,k)
     DO j=j1,j2
       DO i=i1,i2
         DO k=k1,k2
           IF (h0(i,j) == -valmanq) THEN
             tabres(k,i,j) = Agrif_SpecialValueFineGrid
           ELSE
             tabres(k,i,j) = nz(k,i,j)
           END IF
         END DO

       END DO
     END DO
!$OMP END DO

   ELSE

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j,k)
     DO j=j1,j2
       DO i=i1,i2
         DO k=k1,k2
           IF ((tabres(k,i,j).NE.Agrif_SpecialValueFineGrid).AND.(h0(i,j)/=-valmanq)) THEN
             nz(k,i,j)  = tabres(k,i,j)
           ELSE
             nz(k,i,j) = valmanq
           END IF
         END DO
       END DO
     END DO
!$OMP END DO

   END IF

!$OMP END PARALLEL

   RETURN
  END SUBROUTINE Updatenz

   !!======================================================================

#ifdef key_substance
  SUBROUTINE Updatecv_wat(tabres,l1,l2,k1,k2,i1,i2,j1,j2,before)
   USE parameters
   USE comvars3d
   USE comvars2d
   USE comvarp2d
   USE comvarp3d
   USE comsubstance

   IMPLICIT NONE
   INTEGER i1,i2,j1,j2,k1,k2,l1,l2
   REAL tabres(l1:l2,k1:k2,i1:i2,j1:j2)
   LOGICAL before

   INTEGER i,j,k,l

!$OMP PARALLEL

   IF (before) THEN

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j,k,l)
     DO j=j1,j2
       DO i=i1,i2
         DO k=k1,k2
           DO l=l1,l2
             tabres(l,k,i,j) = cv_wat(l,k,i,j)
           END DO
         END DO
       END DO
     END DO
!$OMP END DO

   ELSE

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j,k,l)
     DO j=j1,j2
       DO i=i1,i2
         DO k=k1,k2
           DO l=l1,l2
             IF ((tabres(l,k,i,j).NE.Agrif_SpecialValueFineGrid).AND.(h0(i,j)/=-valmanq)) THEN
               cv_wat(l,k,i,j) = tabres(l,k,i,j)
             END IF
           END DO
         END DO
       END DO
     END DO
!$OMP END DO

   END IF

!$OMP END PARALLEL

   RETURN

  END SUBROUTINE Updatecv_wat
#endif

   !!======================================================================

  SUBROUTINE Updatessh(tabres,i1,i2,j1,j2,before)

   !&E       !  2014-09  (S. Petton, F. Dumas) Review of update of ssh and xeadv
   USE parameters
   USE comvars3d
   USE comvars2d
   USE comvarp2d
   USE comvarp3d
   USE comfluxdo

   IMPLICIT NONE
   INTEGER i1,i2,j1,j2
   REAL tabres(i1:i2,j1:j2)
   LOGICAL before

   INTEGER i,j

!$OMP PARALLEL

   IF (before) THEN

     IF (first_agrif_update) THEN
!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
       DO j=j1,j2
         DO i=i1,i2
           IF (h0(i,j)== -valmanq ) THEN
             tabres(i,j) = Agrif_SpecialValueFineGrid
           ELSE
             tabres(i,j) = ssh(i,j)
           END IF
         END DO
       END DO
!$OMP END DO 
     ELSE

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
       DO j=j1,j2
         DO i=i1,i2
           IF (h0(i,j)+ssh(i,j) < h0fond) THEN
             tabres(i,j) = Agrif_SpecialValueFineGrid
           ELSE
             tabres(i,j) = ssh(i,j)
           END IF
         END DO
       END DO
!$OMP END DO
     END IF

   ELSE ! /* after = parent grid */

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
     DO j=j1,j2
       DO i=i1,i2
         IF ((tabres(i,j).NE.Agrif_SpecialValueFineGrid).AND.(h0(i,j)/=-valmanq)) THEN
           ! Cas normal
           ssh(i,j) = tabres(i,j)
         ELSE IF ((tabres(i,j).NE.Agrif_SpecialValueFineGrid).AND.(h0(i,j)/=-valmanq).AND.(h0(i,j)+tabres(i,j)<h0fond)) THEN
           ! Il reste de l eau dans la grille fille mais pas assez
           ssh(i,j) = - h0(i,j)
         ELSE IF ((tabres(i,j)==Agrif_SpecialValueFineGrid).AND.(h0(i,j)/=-valmanq)) THEN
           ! Plus du tout d eau dans la grille fille 
           ssh(i,j) = - h0(i,j)
         END IF
       END DO
     END DO
!$OMP END DO

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
     DO j=MAX(j1,ljmin),MIN(j2,ljmax)
       DO i=MAX(i1,limin),MIN(i2,limax)
         IF (h0(i,j)/=-valmanq) THEN
           xeadv(i,j) = ssh(i,j)
         END IF
       END DO
     END DO
!$OMP END DO

   END IF ! /* test on before */

!$OMP END PARALLEL

   RETURN
  END SUBROUTINE Updatessh

   !!======================================================================

  SUBROUTINE Updatesurf(tabres,i1,i2,j1,j2,before)

   USE parameters
   USE comvars3d
   USE comvars2d
   USE comvarp2d
   USE comvarp3d
   USE comfluxdo
   USE agrifcommonid

   IMPLICIT NONE
   INTEGER i1,i2,j1,j2
   REAL tabres(i1:i2,j1:j2)
   LOGICAL before
   REAL :: rhox, rhoy

   INTEGER i,j

!$OMP PARALLEL PRIVATE(rhox,rhoy)

   IF (before) THEN

     rhox = REAL(agrif_irhox())
     rhoy = REAL(agrif_irhoy())
!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
     DO j=j1,j2
       DO i=i1,i2
         tabres(i,j) = surf(i,j)*rhox*rhoy
       END DO
     END DO
!$OMP END DO

   ELSE ! before is false

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
     DO j=j1,j2
       DO i=i1,i2
         IF (tabres(i,j) /= 0.) THEN
           surf(i,j) = tabres(i,j)
         END IF
       END DO
     END DO
!$OMP END DO

   END IF

!$OMP END PARALLEL

   RETURN
  END SUBROUTINE Updatesurf

   !!======================================================================

  SUBROUTINE Updatefwet(tabres,i1,i2,j1,j2,before)

   !&E       !  2014-09  (S. Petton, F. Dumas, limited update of fwet)

   USE parameters
   USE comvars3d
   USE comvars2d
   USE comvarp2d
   USE comvarp3d
   USE comfluxdo
   USE agrifcommonid

   IMPLICIT NONE
   INTEGER i1,i2,j1,j2
   REAL tabres(i1:i2,j1:j2)
   LOGICAL before
   REAL :: rhox, rhoy

   INTEGER i,j

!$OMP PARALLEL PRIVATE(rhox,rhoy)

   IF (before) THEN

     rhox = REAL(agrif_irhox())
     rhoy = REAL(agrif_irhoy())

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
     DO j=j1,j2
       DO i=i1,i2
         IF (h0(i,j)+ssh(i,j) < h0fond) THEN
           tabres(i,j) = 0.
         ELSE
           tabres(i,j) = surf(i,j)*rhox*rhoy
         END IF
       END DO
     END DO
!$OMP END DO

   ELSE ! before is false

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
     DO j=j1,j2
       DO i=i1,i2
         IF (tabres(i,j) /= 0.) THEN
           fwet(i,j) = MAX(tabres(i,j)/surf(i,j),fwetmin)
         END IF
       END DO
     END DO
!$OMP END DO
     IF (first_agrif_update) THEN
!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
       DO j=j1,j2
         DO i=i1,i2
           fwetp(i,j) = fwet(i,j)
         END DO
       END DO
!$OMP END DO
     END IF

   END IF

!$OMP END PARALLEL

   RETURN
  END SUBROUTINE Updatefwet

   !!======================================================================

  SUBROUTINE Updateuz(tabres,k1,k2,i1,i2,j1,j2,before)
   USE parameters
   USE comvars3d
   USE comvars2d
   USE comvarp2d
   USE comvarp3d
   USE comcolig2d, ONLY : igdu
   USE comfluxdo,    ONLY : fxeau,fyeau
   USE agrifcommonid
#if defined key_siggen || defined key_gencoord
   USE comsiggen, ONLY : hzex,dcusds
   USE toolgeom,  ONLY : f_hzu_x
#endif
   IMPLICIT NONE
   INTEGER i1,i2,j1,j2,k1,k2
   REAL tabres(k1:k2,i1:i2,j1:j2)
   LOGICAL before
   INTEGER i,j,k
   REAL umoy
   REAL hex_loc(i1:i2,j1:j2)
   INTEGER igdu_loc(i1:i2,j1:j2)
   REAL hzex_loc(1:kmax,i1:i2,j1:j2)
   INTEGER test
   REAL :: rhoy
   REAL :: cff

!$OMP PARALLEL PRIVATE(rhoy)

   IF (before) THEN

     rhoy = REAL(agrif_irhoy())
!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
     DO j=MAX(j1,ljmin),MIN(j2,ljmax)
       DO i=MAX(i1,liminm1),MIN(i2,limax)
#if defined key_agrif_conserv
         tabres(1:kmax,i,j) = rhoy*fxeau(:,i,j)*dy_u(i,j)
#else
         tabres(1:kmax,i,j) = rhoy*fxeau(:,i,j)*dy_u(i,j)/halfdt
#endif
  ! rhoy car dans agrifdeclarevariables on fait bien une moyenne et non une somme
  ! (uz_update_id,update2=type_update_u_x,update3=type_update_u_y , 
  !  type_update_u_x     = Agrif_Update_Copy  (on copie tous u de la grille)
  !  type_update_u_y     = Agrif_Update_Average 

       END DO
     END DO
!$OMP END DO

   ELSE

#if defined key_agrif_conserv
      IF (c_agrif_update(1:1) == 'I') THEN
        cff = 0.
      ELSE
        cff = 1.
      ENDIF

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
       DO j=MAX(j1,ljmin),MIN(j2,ljmax)
         DO i=MAX(i1,liminm1),MIN(i2,limax)
             fxeau(:,i,j)=cff*fxeau(:,i,j)+tabres(1:kmax,i,j)/dy_u(i,j)
         END DO
       END DO
!$OMP END DO
     
      IF (c_agrif_update(2:2) == 'N') GOTO 123
#else
!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
       DO j=MAX(j1,ljmin),MIN(j2,ljmax)
         DO i=MAX(i1,liminm1),MIN(i2,limax)
             fxeau(:,i,j)=tabres(1:kmax,i,j)*halfdt/dy_u(i,j)
         END DO
       END DO
!$OMP END DO
#endif     
     ! In case of coarsening, no call to agrif_dyn3d_height_mask because
     ! hex/hzex have already been updated

     IF (.NOT. Agrif_GrandMother()) THEN
       test = 1
!$OMP SINGLE
       CALL agrif_dyn3d_height_mask(ssh,igdu_loc,hex_loc, &
#if defined key_siggen || defined key_gencoord
                                    hzex_loc, &
#endif
                                    i1,i2,j1,j2,test)
!$OMP END SINGLE

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
       DO j=MAX(j1,ljmin),MIN(j2,ljmax)
         DO i=MAX(i1,liminm1),MIN(i2,limax)

           igdu(i,j)  = igdu_loc(i,j)
           hex(i,j) = hex_loc(i,j)
           hzex(:,i,j)= hzex_loc(:,i,j)

           IF (igdu(i,j)/=4) THEN
#if defined key_siggen || defined key_gencoord
             uz(:,i,j) = fxeau(:,i,j)/(hzex(:,i,j)*halfdt)
             u(i,j)=SUM(uz(:,i,j)*hzex(:,i,j)*dsigu(:))/hex(i,j)
#else
             uz(:,i,j) = fxeau(:,i,j)/(hex(i,j)*halfdt)
             u(i,j)=SUM(uz(:,i,j)*dsigu(:))
#endif
           ELSE
             uz(:,i,j)=0.
             u(i,j) = 0.
           END IF

         END DO
       END DO
!$OMP END DO
  
     ELSE ! /* test on Agrif_GrandMother() */

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
       DO j=MAX(j1,ljmin),MIN(j2,ljmax)
         DO i=MAX(i1,liminm1),MIN(i2,limax)

           IF (igdu(i,j)/=4) THEN
#if defined key_siggen || defined key_gencoord
             uz(:,i,j) = fxeau(:,i,j)/(hzex(:,i,j)*halfdt)
             u(i,j)=SUM(uz(:,i,j)*hzex(:,i,j)*dsigu(:))/hex(i,j)
#else
             uz(:,i,j) = fxeau(:,i,j)/(hex(i,j)*halfdt)
             u(i,j)=SUM(uz(:,i,j)*dsigu(:))
#endif
           ELSE
             uz(:,i,j)=0.
             u(i,j) = 0.
           END IF

         END DO
       END DO
!$OMP END DO
     
     END IF

   END IF ! /* test on before */

123  CONTINUE

!$OMP END PARALLEL

   RETURN
  END SUBROUTINE Updateuz

   !!======================================================================

  SUBROUTINE Updatehzex(tabres,k1,k2,i1,i2,j1,j2,before)
   USE parameters
   USE comvars3d
   USE comvars2d
   USE comvarp2d
   USE comvarp3d
   USE comcolig2d, ONLY : igdu
   USE comfluxdo,    ONLY : fxeau,fyeau
#if defined key_siggen || defined key_gencoord
   USE comsiggen, ONLY : hzex,dcusds
   USE toolgeom,  ONLY : f_hzu_x
#endif
   IMPLICIT NONE
   INTEGER i1,i2,j1,j2,k1,k2
   REAL tabres(k1:k2,i1:i2,j1:j2)
   LOGICAL before
   INTEGER i,j,k
   REAL umoy
   REAL hex_loc(i1:i2,j1:j2)
   INTEGER igdu_loc(i1:i2,j1:j2)
   REAL hzex_loc(1:kmax,i1:i2,j1:j2)
   INTEGER test

!$OMP PARALLEL

   IF (before) THEN

     test = 1
!cval car agrif_dyn3d_height_mask pas parallelise pour instant
!$OMP SINGLE
     CALL agrif_dyn3d_height_mask(ssh,igdu_loc,hex_loc, &
#if defined key_siggen || defined key_gencoord
                                  hzex_loc, &
#endif
                                  i1,i2,j1,j2,test)
!$OMP END SINGLE

#if defined key_siggen || defined key_gencoord
!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
     DO j=MAX(j1,ljmin),MIN(j2,ljmax)
       DO i=MAX(i1,limin),MIN(i2,limax)
         tabres(1:kmax,i,j) = hzex_loc(:,i,j)
       END DO
     END DO
!$OMP END DO
#endif
   ELSE

#if defined key_siggen || defined key_gencoord
!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
     DO j=MAX(j1,ljmin),MIN(j2,ljmax)
       DO i=MAX(i1,limin),MIN(i2,limax)
         hzex(:,i,j) = tabres(:,i,j)
       END DO
     END DO
!$OMP END DO
#endif
   END IF

!$OMP END PARALLEL

   RETURN
  END SUBROUTINE Updatehzex

   !!======================================================================

  SUBROUTINE Updatevz(tabres,k1,k2,i1,i2,j1,j2,before)
   USE parameters
   USE comvars3d
   USE comvars2d
   USE comvarp2d
   USE comvarp3d
   USE comcolig2d, ONLY : igdv
   USE comfluxdo,    ONLY : fxeau,fyeau
   USE comcolig3d,   ONLY : intz_exp_vstar
#if defined key_siggen || defined key_gencoord
   USE comsiggen, ONLY : hzey,dcusds
   USE toolgeom,  ONLY : f_hzu_y
#endif
   IMPLICIT NONE
   INTEGER i1,i2,j1,j2,k1,k2
   REAL tabres(k1:k2,i1:i2,j1:j2)
   LOGICAL before
   INTEGER i,j,k
   REAL vmoy
   REAL hey_loc(i1:i2,j1:j2)
   INTEGER igdv_loc(i1:i2,j1:j2)
   REAL hzey_loc(1:kmax,i1:i2,j1:j2)
   INTEGER test
   REAL :: rhox
   REAL :: cff

!$OMP PARALLEL PRIVATE(rhox)

   IF (before) THEN

     rhox = REAL(agrif_irhox())
!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
     DO j=MAX(j1,ljminm1),MIN(j2,ljmax)
       DO i=MAX(i1,limin),MIN(i2,limax)
#if defined key_agrif_conserv
         tabres(1:kmax,i,j) = rhox*fyeau(:,i,j)*dx_v(i,j)
#else
         tabres(1:kmax,i,j) = rhox*fyeau(:,i,j)*dx_v(i,j)/halfdt
#endif
       END DO
     END DO
!$OMP END DO

   ELSE

#if defined key_agrif_conserv
      IF (c_agrif_update(1:1) == 'I') THEN
        cff = 0.
      ELSE
        cff = 1.
      ENDIF

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
     DO j=MAX(j1,ljminm1),MIN(j2,ljmax)
       DO i=MAX(i1,limin),MIN(i2,limax)
         IF ( j>=jb(i) .AND. j<=jh(i)-1 ) THEN
             fyeau(:,i,j)=cff*fyeau(:,i,j)+tabres(1:kmax,i,j)/dx_v(i,j)
           END IF
         END DO
       END DO
!$OMP END DO
     
      IF (c_agrif_update(2:2) == 'N') GOTO 125
#else
!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
     DO j=MAX(j1,ljminm1),MIN(j2,ljmax)
       DO i=MAX(i1,limin),MIN(i2,limax)
         IF ( j>=jb(i) .AND. j<=jh(i)-1 ) THEN
             fyeau(:,i,j)=tabres(1:kmax,i,j)*halfdt/dx_v(i,j)
           END IF
         END DO
       END DO
!$OMP END DO
#endif
      
     ! In case of coarsening, no call to agrif_dyn3d_height_mask because
     ! hey/hzey have already been updated

     IF (.NOT. Agrif_GrandMother()) THEN 
       test = 2
!$OMP SINGLE
       CALL agrif_dyn3d_height_mask(ssh,igdv_loc,hey_loc, &
#if defined key_siggen || defined key_gencoord
                                    hzey_loc, &
#endif
                                    i1,i2,j1,j2,test)
!$OMP END SINGLE

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
     DO j=MAX(j1,ljminm1),MIN(j2,ljmax)
       DO i=MAX(i1,limin),MIN(i2,limax)
         IF ( j>=jb(i) .AND. j<=jh(i)-1 ) THEN

             igdv(i,j)=igdv_loc(i,j)
             hey(i,j)=hey_loc(i,j)
             hzey(:,i,j)=hzey_loc(:,i,j)

             IF (igdv(i,j)/=4) THEN
#if defined key_siggen || defined key_gencoord
               vz(:,i,j) = fyeau(:,i,j)/(hzey(:,i,j)*halfdt)
               v(i,j)=SUM(vz(:,i,j)*hzey(:,i,j)*dsigu(:))/hey(i,j)
#else
               vz(:,i,j) = fyeau(:,i,j)/(hey(i,j)*halfdt)
               v(i,j)=SUM(vz(:,i,j)*dsigu(:))
#endif
             ELSE
               vz(:,i,j) = 0.
               v(i,j) = 0.
             END IF

           END IF
         END DO
       END DO
!$OMP END DO

     ELSE ! /* test on Agrif_GrandMother() */

!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
       DO j=MAX(j1,ljminm1),MIN(j2,ljmax)
         DO i=MAX(i1,limin),MIN(i2,limax)
           IF ( j>=jb(i) .AND. j<=jh(i)-1 ) THEN

             IF (igdv(i,j)/=4) THEN
#if defined key_siggen || defined key_gencoord
               vz(:,i,j) = fyeau(:,i,j)/(hzey(:,i,j)*halfdt)
               v(i,j)=SUM(vz(:,i,j)*hzey(:,i,j)*dsigu(:))/hey(i,j)
#else
               vz(:,i,j) = fyeau(:,i,j)/(hey(i,j)*halfdt)
               v(i,j)=SUM(vz(:,i,j)*dsigu(:))
#endif
             ELSE
               vz(:,i,j) = 0.
               v(i,j) = 0.
             END IF

           END IF
         END DO
       END DO
!$OMP END DO

     ENDIF

   END IF ! /* test on before */

125  CONTINUE

!$OMP END PARALLEL

   RETURN
  END SUBROUTINE Updatevz

   !!======================================================================


  SUBROUTINE Updatehzey(tabres,k1,k2,i1,i2,j1,j2,before)
   USE parameters
   USE comvars3d
   USE comvars2d
   USE comvarp2d
   USE comvarp3d
   USE comcolig2d, ONLY : igdv
   USE comfluxdo,    ONLY : fxeau,fyeau
   USE comcolig3d,   ONLY : intz_exp_vstar
#if defined key_siggen || defined key_gencoord
   USE comsiggen, ONLY : hzey,dcusds
   USE toolgeom,  ONLY : f_hzu_y
#endif
   IMPLICIT NONE
   INTEGER i1,i2,j1,j2,k1,k2
   REAL tabres(k1:k2,i1:i2,j1:j2)
   LOGICAL before
   INTEGER i,j,k
   REAL vmoy
   REAL hey_loc(i1:i2,j1:j2)
   INTEGER igdv_loc(i1:i2,j1:j2)
   REAL hzey_loc(1:kmax,i1:i2,j1:j2)
   INTEGER test


!$OMP PARALLEL

   IF (before) THEN
     test = 2
!cval car agrif_dyn3d_height_mask pas parallelise pour instant
!$OMP SINGLE
     CALL agrif_dyn3d_height_mask(ssh,igdv_loc,hey_loc, &
#if defined key_siggen || defined key_gencoord
                                  hzey_loc, &
#endif
                                  i1,i2,j1,j2,test)
!$OMP END SINGLE

#if defined key_siggen || defined key_gencoord
!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
     DO j=MAX(j1,ljmin),MIN(j2,ljmax)
       DO i=MAX(i1,limin),MIN(i2,limax)
         tabres(1:kmax,i,j) = hzey_loc(:,i,j)
       END DO
     END DO
!$OMP END DO
#endif
   ELSE
#if defined key_siggen || defined key_gencoord
!$OMP DO SCHEDULE(RUNTIME) PRIVATE(i,j)
     DO j=MAX(j1,ljmin),MIN(j2,ljmax)
       DO i=MAX(i1,limin),MIN(i2,limax)
         hzey(:,i,j) = tabres(:,i,j)
       END DO
     END DO
!$OMP END DO
#endif
   END IF

!$OMP END PARALLEL

   RETURN
  END SUBROUTINE Updatehzey

   !!======================================================================

#else
  SUBROUTINE agrif_update_empty
   RETURN
  END SUBROUTINE agrif_update_empty
#endif
