Index: trunk/NEMOGCM/NEMO/NST_SRC/agrif_user.F90
===================================================================
--- trunk/NEMOGCM/NEMO/NST_SRC/agrif_user.F90	(revision 2528)
+++ trunk/NEMOGCM/NEMO/NST_SRC/agrif_user.F90	(revision 2715)
@@ -5,4 +5,6 @@
    !! Software governed by the CeCILL licence (NEMOGCM/NEMO_CeCILL.txt)
    !!----------------------------------------------------------------------
+   SUBROUTINE agrif_before_regridding
+   END SUBROUTINE
 
    SUBROUTINE Agrif_InitWorkspace
@@ -13,32 +15,18 @@
       USE dom_oce
       USE Agrif_Util
-      !!
-      IMPLICIT NONE
-      !!
-#if defined key_mpp_dyndist
-      CHARACTER(len=20) :: namelistname
-      INTEGER nummpp
-      NAMELIST/nammpp_dyndist/ jpni, jpnj, jpnij
-#endif
-      !!----------------------------------------------------------------------
-
-#if defined key_mpp_dyndist
-      ! MPP dynamical distribution : read the processor cutting in the namelist
-      IF( Agrif_Nbstepint() == 0 ) THEN
-        nummpp = Agrif_Get_Unit()
-        namelistname='namelist'
-        IF(.NOT. Agrif_Root() )   namelistname=TRIM(Agrif_CFixed())//'_namelist'
-        !
-        OPEN (nummpp,file=namelistname,status='OLD',form='formatted')
-        READ (nummpp,nammpp_dyndist)
-        CLOSE(nummpp)
-      ENDIF
-#endif
-
+      USE nemogcm
+      !
+      IMPLICIT NONE
+      !!----------------------------------------------------------------------
+      !
       IF( .NOT. Agrif_Root() ) THEN
+         jpni = Agrif_Parent(jpni)
+         jpnj = Agrif_Parent(jpnj)
+         jpnij = Agrif_Parent(jpnij)
          jpiglo  = nbcellsx + 2 + 2*nbghostcells
          jpjglo  = nbcellsy + 2 + 2*nbghostcells
          jpi     = ( jpiglo-2*jpreci + (jpni-1+0) ) / jpni + 2*jpreci
          jpj     = ( jpjglo-2*jprecj + (jpnj-1+0) ) / jpnj + 2*jprecj
+         jpk     = jpkdta
          jpim1   = jpi-1
          jpjm1   = jpj-1
@@ -55,5 +43,4 @@
    END SUBROUTINE Agrif_InitWorkspace
 
-#if ! defined key_offline
 
    SUBROUTINE Agrif_InitValues
@@ -67,7 +54,4 @@
       USE dom_oce
       USE nemogcm
-#if defined key_top
-      USE trc
-#endif
 #if defined key_tradmp   ||   defined key_esopa
       USE tradmp
@@ -76,20 +60,5 @@
       USE obc_par
 #endif
-      USE sol_oce
-      USE in_out_manager
-      USE agrif_opa_update
-      USE agrif_opa_interp
-      USE agrif_opa_sponge
-      USE agrif_top_update
-      USE agrif_top_interp
-      USE agrif_top_sponge
-      !!
-      IMPLICIT NONE
-      !!
-      REAL(wp) :: tabtemp(jpi,jpj,jpk)
-#if defined key_top
-      REAL(wp) :: tabtrtemp(jpi,jpj,jpk,jptra)
-#endif 
-      LOGICAL check_namelist
+      IMPLICIT NONE
       !!----------------------------------------------------------------------
 
@@ -111,153 +80,63 @@
 #endif
 
-      Call nemo_init  ! Initializations of each fine grid
-      Call agrif_nemo_init
-
+      CALL nemo_init  ! Initializations of each fine grid
+      CALL agrif_nemo_init
+# if ! defined key_offline
+      CALL Agrif_InitValues_cont
+# endif       
+# if defined key_top
+      CALL Agrif_InitValues_cont_top
+# endif      
+   END SUBROUTINE Agrif_initvalues
+
+# if ! defined key_offline
+
+   SUBROUTINE Agrif_InitValues_cont
+      !!----------------------------------------------------------------------
+      !!                 *** ROUTINE Agrif_InitValues_cont ***
+      !!
+      !! ** Purpose ::   Declaration of variables to be interpolated
+      !!----------------------------------------------------------------------
+      USE Agrif_Util
+      USE oce 
+      USE dom_oce
+      USE nemogcm
+      USE sol_oce
+      USE in_out_manager
+      USE agrif_opa_update
+      USE agrif_opa_interp
+      USE agrif_opa_sponge
+      !
+      IMPLICIT NONE
+      !
+      REAL(wp), DIMENSION(:,:,:), ALLOCATABLE :: tabtemp
+      LOGICAL :: check_namelist
+      !!----------------------------------------------------------------------
+
+      ALLOCATE( tabtemp(jpi,jpj,jpk) )
+      
+      
       ! 1. Declaration of the type of variable which have to be interpolated
       !---------------------------------------------------------------------
-      Call Agrif_Set_type(un,(/1,2,0/),(/2,3,0/))
-      Call Agrif_Set_type(vn,(/2,1,0/),(/3,2,0/))
-
-      Call Agrif_Set_type(ua,(/1,2,0/),(/2,3,0/))
-      Call Agrif_Set_type(va,(/2,1,0/),(/3,2,0/))
-
-      Call Agrif_Set_type(e1u,(/1,2/),(/2,3/))
-      Call Agrif_Set_type(e2v,(/2,1/),(/3,2/))
-
-      Call Agrif_Set_type(tn,(/2,2,0/),(/3,3,0/))
-      Call Agrif_Set_type(sn,(/2,2,0/),(/3,3,0/)) 
-
-      Call Agrif_Set_type(tb,(/2,2,0/),(/3,3,0/))
-      Call Agrif_Set_type(sb,(/2,2,0/),(/3,3,0/)) 
-
-      Call Agrif_Set_type(ta,(/2,2,0/),(/3,3,0/))
-      Call Agrif_Set_type(sa,(/2,2,0/),(/3,3,0/))       
-
-      Call Agrif_Set_type(sshn,(/2,2/),(/3,3/))
-      Call Agrif_Set_type(gcb,(/2,2/),(/3,3/))
-
-#if defined key_top
-      Call Agrif_Set_type(trb,(/2,2,0,0/),(/3,3,0,0/))
-      Call Agrif_Set_type(trn,(/2,2,0,0/),(/3,3,0,0/))
-      Call Agrif_Set_type(tra,(/2,2,0,0/),(/3,3,0,0/))
-#endif
-      
-      ! 2. Space directions for each variables
-      !---------------------------------------
-      Call Agrif_Set_raf(un,(/'x','y','N'/))
-      Call Agrif_Set_raf(vn,(/'x','y','N'/))
-
-      Call Agrif_Set_raf(ua,(/'x','y','N'/))
-      Call Agrif_Set_raf(va,(/'x','y','N'/))
-
-      Call Agrif_Set_raf(e1u,(/'x','y'/))
-      Call Agrif_Set_raf(e2v,(/'x','y'/))
-
-      Call Agrif_Set_raf(tn,(/'x','y','N'/))
-      Call Agrif_Set_raf(sn,(/'x','y','N'/))
-
-      Call Agrif_Set_raf(tb,(/'x','y','N'/))
-      Call Agrif_Set_raf(sb,(/'x','y','N'/))
-
-      Call Agrif_Set_raf(ta,(/'x','y','N'/))
-      Call Agrif_Set_raf(sa,(/'x','y','N'/))      
-
-      Call Agrif_Set_raf(sshn,(/'x','y'/))
-      Call Agrif_Set_raf(gcb,(/'x','y'/))
-
-#if defined key_top
-      Call Agrif_Set_raf(trn,(/'x','y','N','N'/))
-      Call Agrif_Set_raf(trb,(/'x','y','N','N'/))
-      Call Agrif_Set_raf(tra,(/'x','y','N','N'/))
-#endif
-
-      ! 3. Type of interpolation
-      !------------------------- 
-      Call Agrif_Set_bcinterp(tn,interp=AGRIF_linear)
-      Call Agrif_Set_bcinterp(sn,interp=AGRIF_linear)
-
-      Call Agrif_Set_bcinterp(ta,interp=AGRIF_linear)
-      Call Agrif_Set_bcinterp(sa,interp=AGRIF_linear)
-
-      Call Agrif_Set_bcinterp(un,interp1=Agrif_linear,interp2=AGRIF_ppm)
-      Call Agrif_Set_bcinterp(vn,interp1=AGRIF_ppm,interp2=Agrif_linear)
-
-      Call Agrif_Set_bcinterp(ua,interp1=Agrif_linear,interp2=AGRIF_ppm)
-      Call Agrif_Set_bcinterp(va,interp1=AGRIF_ppm,interp2=Agrif_linear)
-
-      Call Agrif_Set_bcinterp(e1u,interp1=Agrif_linear,interp2=AGRIF_ppm)
-      Call Agrif_Set_bcinterp(e2v,interp1=AGRIF_ppm,interp2=Agrif_linear)
-
-#if defined key_top
-      Call Agrif_Set_bcinterp(trn,interp=AGRIF_linear)
-      Call Agrif_Set_bcinterp(tra,interp=AGRIF_linear)
-#endif
-
-      ! 4. Location of interpolation
-      !-----------------------------
-      Call Agrif_Set_bc(un,(/0,1/))
-      Call Agrif_Set_bc(vn,(/0,1/))
-
-      Call Agrif_Set_bc(e1u,(/0,0/))
-      Call Agrif_Set_bc(e2v,(/0,0/))
-
-      Call Agrif_Set_bc(tn,(/0,1/))
-      Call Agrif_Set_bc(sn,(/0,1/))
-
-      Call Agrif_Set_bc(ta,(/-3*Agrif_irhox(),0/))
-      Call Agrif_Set_bc(sa,(/-3*Agrif_irhox(),0/))
-
-      Call Agrif_Set_bc(ua,(/-2*Agrif_irhox(),0/))
-      Call Agrif_Set_bc(va,(/-2*Agrif_irhox(),0/))
-
-#if defined key_top
-      Call Agrif_Set_bc(trn,(/0,1/))
-      Call Agrif_Set_bc(tra,(/-3*Agrif_irhox(),0/))
-#endif
-
-      ! 5. Update type
-      !--------------- 
-      Call Agrif_Set_Updatetype(tn, update = AGRIF_Update_Average)
-      Call Agrif_Set_Updatetype(sn, update = AGRIF_Update_Average)
-
-      Call Agrif_Set_Updatetype(tb, update = AGRIF_Update_Average)
-      Call Agrif_Set_Updatetype(sb, update = AGRIF_Update_Average)
-
-      Call Agrif_Set_Updatetype(sshn, update = AGRIF_Update_Average)
-      Call Agrif_Set_Updatetype(gcb,update = AGRIF_Update_Average)
-
-#if defined key_top
-      Call Agrif_Set_Updatetype(trn, update = AGRIF_Update_Average)
-      Call Agrif_Set_Updatetype(trb, update = AGRIF_Update_Average)
-#endif
-
-      Call Agrif_Set_Updatetype(un,update1 = Agrif_Update_Copy, update2 = Agrif_Update_Average)
-      Call Agrif_Set_Updatetype(vn,update1 = Agrif_Update_Average, update2 = Agrif_Update_Copy)
-
-      Call Agrif_Set_Updatetype(e1u,update1 = Agrif_Update_Copy, update2=Agrif_Update_Average)
-      Call Agrif_Set_Updatetype(e2v,update1 = Agrif_Update_Average, update2=Agrif_Update_Copy)
-
-      ! 6. First interpolations of potentially non zero fields
+      CALL agrif_declare_var
+
+      ! 2. First interpolations of potentially non zero fields
       !-------------------------------------------------------
       Agrif_SpecialValue=0.
       Agrif_UseSpecialValue = .TRUE.
-      Call Agrif_Bc_variable(tabtemp,tn,calledweight=1.)
-      Call Agrif_Bc_variable(tabtemp,sn,calledweight=1.)
-      Call Agrif_Bc_variable(tabtemp,un,calledweight=1.,procname=interpu)
-      Call Agrif_Bc_variable(tabtemp,vn,calledweight=1.,procname=interpv)
-
-      Call Agrif_Bc_variable(tabtemp,ta,calledweight=1.,procname=interptn)
-      Call Agrif_Bc_variable(tabtemp,sa,calledweight=1.,procname=interpsn)
-
-      Call Agrif_Bc_variable(tabtemp,ua,calledweight=1.,procname=interpun)
-      Call Agrif_Bc_variable(tabtemp,va,calledweight=1.,procname=interpvn)
-
-#if defined key_top
-      Call Agrif_Bc_variable(tabtrtemp,trn,calledweight=1.)
-      Call Agrif_Bc_variable(tabtrtemp,tra,calledweight=1.,procname=interptrn)
-#endif
+      Call Agrif_Bc_variable(tabtemp,tn_id,calledweight=1.,procname=interptn)
+    
+      Call Agrif_Bc_variable(tabtemp,sn_id,calledweight=1.,procname=interpsn)
+      Call Agrif_Bc_variable(tabtemp,un_id,calledweight=1.,procname=interpu)
+      Call Agrif_Bc_variable(tabtemp,vn_id,calledweight=1.,procname=interpv)
+
+      Call Agrif_Bc_variable(tabtemp,ta_id,calledweight=1.,procname=interptn)
+      Call Agrif_Bc_variable(tabtemp,sa_id,calledweight=1.,procname=interpsn)
+
+      Call Agrif_Bc_variable(tabtemp,ua_id,calledweight=1.,procname=interpun)
+      Call Agrif_Bc_variable(tabtemp,va_id,calledweight=1.,procname=interpvn)
       Agrif_UseSpecialValue = .FALSE.
 
-      ! 7. Some controls
+      ! 3. Some controls
       !-----------------
       check_namelist = .true.
@@ -265,4 +144,180 @@
       IF( check_namelist ) THEN
      
+         ! Check time steps           
+         IF( NINT(Agrif_Rhot()) * nint(rdt) /= Agrif_Parent(rdt) ) THEN
+            WRITE(*,*) 'incompatible time step between grids'
+            WRITE(*,*) 'parent grid value : ',Agrif_Parent(rdt)
+            WRITE(*,*) 'child  grid value : ',nint(rdt)
+            WRITE(*,*) 'value on parent grid should be : ',rdt*Agrif_Rhot()
+            STOP
+         ENDIF
+         
+         ! Check run length
+         IF( Agrif_IRhot() * (Agrif_Parent(nitend)-Agrif_Parent(nit000)+1) /= (nitend-nit000+1) ) THEN
+            WRITE(*,*) 'incompatible run length between grids'
+            WRITE(*,*) 'parent grid value : ', (Agrif_Parent(nitend)-Agrif_Parent(nit000)+1),' time step'
+            WRITE(*,*) 'child  grid value : ', (nitend-nit000+1),' time step'
+            WRITE(*,*) 'value on child grid should be: ', Agrif_IRhot() * (Agrif_Parent(nitend)-Agrif_Parent(nit000)+1)
+            STOP
+         ENDIF
+         
+         ! Check coordinates
+         IF( ln_zps ) THEN
+            ! check parameters for partial steps 
+            IF( Agrif_Parent(e3zps_min) .ne. e3zps_min ) THEN
+               WRITE(*,*) 'incompatible e3zps_min between grids'
+               WRITE(*,*) 'parent grid :',Agrif_Parent(e3zps_min)
+               WRITE(*,*) 'child grid  :',e3zps_min
+               WRITE(*,*) 'those values should be identical'
+               STOP
+            ENDIF          
+            IF( Agrif_Parent(e3zps_rat) /= e3zps_rat ) THEN
+               WRITE(*,*) 'incompatible e3zps_rat between grids'
+               WRITE(*,*) 'parent grid :',Agrif_Parent(e3zps_rat)
+               WRITE(*,*) 'child grid  :',e3zps_rat
+               WRITE(*,*) 'those values should be identical'                  
+               STOP
+            ENDIF
+         ENDIF
+      ENDIF
+       
+      CALL Agrif_Update_tra(0)
+      CALL Agrif_Update_dyn(0)
+
+      nbcline = 0
+      !
+      DEALLOCATE(tabtemp)
+      !
+   END SUBROUTINE Agrif_InitValues_cont
+
+
+   SUBROUTINE agrif_declare_var
+      !!----------------------------------------------------------------------
+      !!                 *** ROUTINE agrif_declarE_var ***
+      !!
+      !! ** Purpose :: Declaration of variables to be interpolated
+      !!----------------------------------------------------------------------
+      USE agrif_util
+      USE oce
+      IMPLICIT NONE
+      !!----------------------------------------------------------------------
+   
+      ! 1. Declaration of the type of variable which have to be interpolated
+      !---------------------------------------------------------------------
+      CALL agrif_declare_variable((/2,2,0/),(/3,3,0/),(/'x','y','N'/),(/1,1,1/),(/jpi,jpj,jpk/),tn_id)
+      CALL agrif_declare_variable((/2,2,0/),(/3,3,0/),(/'x','y','N'/),(/1,1,1/),(/jpi,jpj,jpk/),sn_id)
+      CALL agrif_declare_variable((/2,2,0/),(/3,3,0/),(/'x','y','N'/),(/1,1,1/),(/jpi,jpj,jpk/),tb_id)
+      CALL agrif_declare_variable((/2,2,0/),(/3,3,0/),(/'x','y','N'/),(/1,1,1/),(/jpi,jpj,jpk/),sb_id)
+      CALL agrif_declare_variable((/2,2,0/),(/3,3,0/),(/'x','y','N'/),(/1,1,1/),(/jpi,jpj,jpk/),ta_id)
+      CALL agrif_declare_variable((/2,2,0/),(/3,3,0/),(/'x','y','N'/),(/1,1,1/),(/jpi,jpj,jpk/),sa_id)
+         
+      CALL agrif_declare_variable((/1,2,0/),(/2,3,0/),(/'x','y','N'/),(/1,1,1/),(/jpi,jpj,jpk/),un_id)
+      CALL agrif_declare_variable((/2,1,0/),(/3,2,0/),(/'x','y','N'/),(/1,1,1/),(/jpi,jpj,jpk/),vn_id)
+      CALL agrif_declare_variable((/1,2,0/),(/2,3,0/),(/'x','y','N'/),(/1,1,1/),(/jpi,jpj,jpk/),ua_id)
+      CALL agrif_declare_variable((/2,1,0/),(/3,2,0/),(/'x','y','N'/),(/1,1,1/),(/jpi,jpj,jpk/),va_id)
+   
+      CALL agrif_declare_variable((/1,2/),(/2,3/),(/'x','y'/),(/1,1/),(/jpi,jpj/),e1u_id)
+      CALL agrif_declare_variable((/2,1/),(/3,2/),(/'x','y'/),(/1,1/),(/jpi,jpj/),e2v_id)
+
+      CALL agrif_declare_variable((/2,2/),(/3,3/),(/'x','y'/),(/1,1/),(/jpi,jpj/),sshn_id)
+      CALL agrif_declare_variable((/2,2/),(/3,3/),(/'x','y'/),(/1,1/),(/jpi,jpj/),gcb_id)
+       
+      ! 2. Type of interpolation
+      !-------------------------
+      CALL Agrif_Set_bcinterp(tn_id,interp=AGRIF_linear)
+      CALL Agrif_Set_bcinterp(sn_id,interp=AGRIF_linear)
+      CALL Agrif_Set_bcinterp(ta_id,interp=AGRIF_linear)
+      CALL Agrif_Set_bcinterp(sa_id,interp=AGRIF_linear)
+   
+      Call Agrif_Set_bcinterp(un_id,interp1=Agrif_linear,interp2=AGRIF_ppm)
+      Call Agrif_Set_bcinterp(vn_id,interp1=AGRIF_ppm,interp2=Agrif_linear)
+
+      Call Agrif_Set_bcinterp(ua_id,interp1=Agrif_linear,interp2=AGRIF_ppm)
+      Call Agrif_Set_bcinterp(va_id,interp1=AGRIF_ppm,interp2=Agrif_linear)
+
+      Call Agrif_Set_bcinterp(e1u_id,interp1=Agrif_linear,interp2=AGRIF_ppm)
+      Call Agrif_Set_bcinterp(e2v_id,interp1=AGRIF_ppm,interp2=Agrif_linear)
+
+      ! 3. Location of interpolation
+      !-----------------------------
+      Call Agrif_Set_bc(un_id,(/0,1/))
+      Call Agrif_Set_bc(vn_id,(/0,1/))
+
+      Call Agrif_Set_bc(e1u_id,(/0,0/))
+      Call Agrif_Set_bc(e2v_id,(/0,0/))
+
+      Call Agrif_Set_bc(tn_id,(/0,1/))
+      Call Agrif_Set_bc(sn_id,(/0,1/))
+
+      Call Agrif_Set_bc(ta_id,(/-3*Agrif_irhox(),0/))
+      Call Agrif_Set_bc(sa_id,(/-3*Agrif_irhox(),0/))
+
+      Call Agrif_Set_bc(ua_id,(/-2*Agrif_irhox(),0/))
+      Call Agrif_Set_bc(va_id,(/-2*Agrif_irhox(),0/))
+
+      ! 5. Update type
+      !--------------- 
+      Call Agrif_Set_Updatetype(tn_id, update = AGRIF_Update_Average)
+      Call Agrif_Set_Updatetype(sn_id, update = AGRIF_Update_Average)
+
+      Call Agrif_Set_Updatetype(tb_id, update = AGRIF_Update_Average)
+      Call Agrif_Set_Updatetype(sb_id, update = AGRIF_Update_Average)
+
+      Call Agrif_Set_Updatetype(sshn_id, update = AGRIF_Update_Average)
+      Call Agrif_Set_Updatetype(gcb_id,update = AGRIF_Update_Average)
+
+      Call Agrif_Set_Updatetype(un_id,update1 = Agrif_Update_Copy, update2 = Agrif_Update_Average)
+      Call Agrif_Set_Updatetype(vn_id,update1 = Agrif_Update_Average, update2 = Agrif_Update_Copy)
+
+      Call Agrif_Set_Updatetype(e1u_id,update1 = Agrif_Update_Copy, update2=Agrif_Update_Average)
+      Call Agrif_Set_Updatetype(e2v_id,update1 = Agrif_Update_Average, update2=Agrif_Update_Copy)
+
+   END SUBROUTINE agrif_declare_var
+# endif
+   
+# if defined key_top
+   SUBROUTINE Agrif_InitValues_cont_top
+      !!----------------------------------------------------------------------
+      !!                 *** ROUTINE Agrif_InitValues_cont_top ***
+      !!
+      !! ** Purpose :: Declaration of variables to be interpolated
+      !!----------------------------------------------------------------------
+      USE Agrif_Util
+      USE oce 
+      USE dom_oce
+      USE nemogcm
+      USE trc
+      USE in_out_manager
+      USE agrif_top_update
+      USE agrif_top_interp
+      USE agrif_top_sponge
+      !
+      IMPLICIT NONE
+      !
+      REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE :: tabtrtemp
+      LOGICAL :: check_namelist
+      !!----------------------------------------------------------------------
+
+      ALLOCATE( tabtrtemp(jpi,jpj,jpk,jptra) )
+      
+      
+      ! 1. Declaration of the type of variable which have to be interpolated
+      !---------------------------------------------------------------------
+      CALL agrif_declare_var_top
+
+      ! 2. First interpolations of potentially non zero fields
+      !-------------------------------------------------------
+      Agrif_SpecialValue=0.
+      Agrif_UseSpecialValue = .TRUE.
+      Call Agrif_Bc_variable(tabtrtemp,trn_id,calledweight=1.)
+      Call Agrif_Bc_variable(tabtrtemp,tra_id,calledweight=1.,procname=interptrn)
+      Agrif_UseSpecialValue = .FALSE.
+
+      ! 3. Some controls
+      !-----------------
+      check_namelist = .true.
+            
+      IF( check_namelist ) THEN
+#  if defined offline     
          ! Check time steps           
          IF( nint(Agrif_Rhot()) * nint(rdt) .ne. Agrif_Parent(rdt) ) THEN
@@ -275,14 +330,9 @@
          
          ! Check run length
-         IF( Agrif_IRhot() * (Agrif_Parent(nitend)- &
-            Agrif_Parent(nit000)+1) .ne. (nitend-nit000+1) ) THEN
+         IF( Agrif_IRhot() * (Agrif_Parent(nitend)-Agrif_Parent(nit000)+1) .ne. (nitend-nit000+1) ) THEN
             WRITE(*,*) 'incompatible run length between grids'
-            WRITE(*,*) 'parent grid value : ',(Agrif_Parent(nitend)- &
-               Agrif_Parent(nit000)+1),' time step'
-            WRITE(*,*) 'child  grid value : ', &
-               (nitend-nit000+1),' time step'
-            WRITE(*,*) 'value on child grid should be : ', &
-               Agrif_IRhot() * (Agrif_Parent(nitend)- &
-               Agrif_Parent(nit000)+1)
+            WRITE(*,*) 'parent grid value : ', (Agrif_Parent(nitend)-Agrif_Parent(nit000)+1),' time step'
+            WRITE(*,*) 'child  grid value : ', (nitend-nit000+1),' time step'
+            WRITE(*,*) 'value on child grid should be : ', Agrif_IRhot() * (Agrif_Parent(nitend)-Agrif_Parent(nit000)+1)
             STOP
          ENDIF
@@ -306,164 +356,85 @@
             ENDIF
          ENDIF
-#if defined key_top
+#  endif         
         ! Check passive tracer cell
         IF( nn_dttrc .ne. 1 ) THEN
            WRITE(*,*) 'nn_dttrc should be equal to 1'
         ENDIF
-#endif
-
       ENDIF
-
-#if defined key_top
+       
       CALL Agrif_Update_trc(0)
-#endif
-      CALL Agrif_Update_tra(0)
-      CALL Agrif_Update_dyn(0)
-
-#if defined key_top
       nbcline_trc = 0
-#endif
-      nbcline = 0
-      !
-   END SUBROUTINE Agrif_InitValues
-
-#else
-
-   SUBROUTINE Agrif_InitValues
-      !!----------------------------------------------------------------------
-      !!                 *** ROUTINE Agrif_InitValues ***
+      !
+      DEALLOCATE(tabtrtemp)
+      !
+   END SUBROUTINE Agrif_InitValues_cont_top
+
+
+   SUBROUTINE agrif_declare_var_top
+      !!----------------------------------------------------------------------
+      !!                 *** ROUTINE agrif_declare_var_top ***
       !!
-      !! ** Purpose :: Declaration of variables to be interpolated
-      !!----------------------------------------------------------------------
-      USE Agrif_Util
-      USE oce 
+      !! ** Purpose :: Declaration of TOP variables to be interpolated
+      !!----------------------------------------------------------------------
+      USE agrif_util
       USE dom_oce
-      USE nemogcm
       USE trc
-      USE in_out_manager
-      USE agrif_top_update
-      USE agrif_top_interp
-      USE agrif_top_sponge
-      !!
-      IMPLICIT NONE
-      !!
-      REAL(wp) :: tabtrtemp(jpi,jpj,jpk,jptra)
-      LOGICAL check_namelist
-      !!----------------------------------------------------------------------
-
-      ! 0. Initializations
-      !-------------------
-#if defined key_orca_r025 || defined key_orca_r05 || defined key_orca_r2 || defined key_orca_r4
-      jp_cfg = -1    ! set special value for jp_cfg on fine grids
-      cp_cfg = "default"
-#endif
-
-      Call nemo_init  ! Initializations of each fine grid
-      Call agrif_nemo_init
-
+      
+      IMPLICIT NONE
+   
       ! 1. Declaration of the type of variable which have to be interpolated
       !---------------------------------------------------------------------
-      Call Agrif_Set_type(trb,(/2,2,0,0/),(/3,3,0,0/))
-      Call Agrif_Set_type(trn,(/2,2,0,0/),(/3,3,0,0/))
-      Call Agrif_Set_type(tra,(/2,2,0,0/),(/3,3,0,0/))
-      
-      ! 2. Space directions for each variables
-      !---------------------------------------
-      Call Agrif_Set_raf(trn,(/'x','y','N','N'/))
-      Call Agrif_Set_raf(trb,(/'x','y','N','N'/))
-      Call Agrif_Set_raf(tra,(/'x','y','N','N'/))
-
-      ! 3. Type of interpolation
-      !------------------------- 
-      Call Agrif_Set_bcinterp(trn,interp=AGRIF_linear)
-      Call Agrif_Set_bcinterp(tra,interp=AGRIF_linear)
-
-      ! 4. Location of interpolation
+      CALL agrif_declare_variable((/2,2,0,0/),(/3,3,0,0/),(/'x','y','N','N'/),  &
+      &                           (/1,1,1,1/),(/jpi,jpj,jpk,jptra/),trn_id)
+      CALL agrif_declare_variable((/2,2,0,0/),(/3,3,0,0/),(/'x','y','N','N'/),  &
+      &                           (/1,1,1,1/),(/jpi,jpj,jpk,jptra/),trb_id)
+      CALL agrif_declare_variable((/2,2,0,0/),(/3,3,0,0,jptra/),(/'x','y','N','N'/),  &
+      &                           (/1,1,1,1/),(/jpi,jpj,jpk/),tra_id)
+            
+#  if defined key_offline
+      CALL agrif_declare_variable((/1,2/),(/2,3/),(/'x','y'/),(/1,1/),(/jpi,jpj/),e1u_id)
+      CALL agrif_declare_variable((/2,1/),(/3,2/),(/'x','y'/),(/1,1/),(/jpi,jpj/),e2v_id)
+#  endif
+       
+      ! 2. Type of interpolation
+      !-------------------------
+      CALL Agrif_Set_bcinterp(trn_id,interp=AGRIF_linear)
+      CALL Agrif_Set_bcinterp(tra_id,interp=AGRIF_linear)
+   
+#  if defined key_offline
+      Call Agrif_Set_bcinterp(e1u_id,interp1=Agrif_linear,interp2=AGRIF_ppm)
+      Call Agrif_Set_bcinterp(e2v_id,interp1=AGRIF_ppm,interp2=Agrif_linear)
+#  endif
+
+      ! 3. Location of interpolation
       !-----------------------------
-      Call Agrif_Set_bc(trn,(/0,1/))
-      Call Agrif_Set_bc(tra,(/-3*Agrif_irhox(),0/))
+#  if defined key_offline
+      Call Agrif_Set_bc(e1u_id,(/0,0/))
+      Call Agrif_Set_bc(e2v_id,(/0,0/))
+#  endif
+      Call Agrif_Set_bc(trn_id,(/0,1/))
+      Call Agrif_Set_bc(tra_id,(/-3*Agrif_irhox(),0/))
 
       ! 5. Update type
       !--------------- 
-      Call Agrif_Set_Updatetype(trn, update = AGRIF_Update_Average)
-      Call Agrif_Set_Updatetype(trb, update = AGRIF_Update_Average)
-
-      ! 6. First interpolations of potentially non zero fields
-      !-------------------------------------------------------
-      Agrif_SpecialValue=0.
-      Agrif_UseSpecialValue = .TRUE.
-      Call Agrif_Bc_variable(tabtrtemp,trn,calledweight=1.)
-      Call Agrif_Bc_variable(tabtrtemp,tra,calledweight=1.,procname=interptrn)
-      Agrif_UseSpecialValue = .FALSE.
-
-      ! 7. Some controls
-      !-----------------
-      check_namelist = .true.
-            
-      IF( check_namelist ) THEN
-     
-         ! Check time steps           
-         IF( nint(Agrif_Rhot()) * nint(rdt) .ne. Agrif_Parent(rdt) ) THEN
-            WRITE(*,*) 'incompatible time step between grids'
-            WRITE(*,*) 'parent grid value : ',Agrif_Parent(rdt)
-            WRITE(*,*) 'child  grid value : ',nint(rdt)
-            WRITE(*,*) 'value on parent grid should be : ',rdt*Agrif_Rhot()
-            STOP
-         ENDIF
-         
-         ! Check run length
-         IF( Agrif_IRhot() * (Agrif_Parent(nitend)- &
-            Agrif_Parent(nit000)+1) .ne. (nitend-nit000+1) ) THEN
-            WRITE(*,*) 'incompatible run length between grids'
-            WRITE(*,*) 'parent grid value : ',(Agrif_Parent(nitend)- &
-               Agrif_Parent(nit000)+1),' time step'
-            WRITE(*,*) 'child  grid value : ', &
-               (nitend-nit000+1),' time step'
-            WRITE(*,*) 'value on child grid should be : ', &
-               Agrif_IRhot() * (Agrif_Parent(nitend)- &
-               Agrif_Parent(nit000)+1)
-            STOP
-         ENDIF
-         
-         ! Check coordinates
-         IF( ln_zps ) THEN
-            ! check parameters for partial steps 
-            IF( Agrif_Parent(e3zps_min) .ne. e3zps_min ) THEN
-               WRITE(*,*) 'incompatible e3zps_min between grids'
-               WRITE(*,*) 'parent grid :',Agrif_Parent(e3zps_min)
-               WRITE(*,*) 'child grid  :',e3zps_min
-               WRITE(*,*) 'those values should be identical'
-               STOP
-            ENDIF          
-            IF( Agrif_Parent(e3zps_rat) .ne. e3zps_rat ) THEN
-               WRITE(*,*) 'incompatible e3zps_rat between grids'
-               WRITE(*,*) 'parent grid :',Agrif_Parent(e3zps_rat)
-               WRITE(*,*) 'child grid  :',e3zps_rat
-               WRITE(*,*) 'those values should be identical'                  
-               STOP
-            ENDIF
-         ENDIF
-        ! Check passive tracer cell
-        IF( nn_dttrc .ne. 1 ) THEN
-           WRITE(*,*) 'nn_dttrc should be equal to 1'
-        ENDIF
-
-      ENDIF
-
-      CALL Agrif_Update_trc(0)
-      nbcline_trc = 0
-      !
-   END SUBROUTINE Agrif_InitValues
-
-#endif
-   
-   SUBROUTINE Agrif_detect( g, sizex )
+      Call Agrif_Set_Updatetype(trn_id, update = AGRIF_Update_Average)
+      Call Agrif_Set_Updatetype(trb_id, update = AGRIF_Update_Average)
+
+#  if defined key_offline
+      Call Agrif_Set_Updatetype(e1u_id,update1 = Agrif_Update_Copy, update2=Agrif_Update_Average)
+      Call Agrif_Set_Updatetype(e2v_id,update1 = Agrif_Update_Average, update2=Agrif_Update_Copy)
+#  endif
+
+   END SUBROUTINE agrif_declare_var_top
+# endif
+   
+   SUBROUTINE Agrif_detect( kg, ksizex )
       !!----------------------------------------------------------------------
       !!   *** ROUTINE Agrif_detect ***
       !!----------------------------------------------------------------------
       USE Agrif_Types
-      !! 
-      INTEGER, DIMENSION(2) :: sizex
-      INTEGER, DIMENSION(sizex(1),sizex(2)) :: g 
+      !
+      INTEGER, DIMENSION(2) :: ksizex
+      INTEGER, DIMENSION(ksizex(1),ksizex(2)) :: kg 
       !!----------------------------------------------------------------------
       !
@@ -479,7 +450,7 @@
       USE agrif_oce 
       USE in_out_manager
-      !!
-      IMPLICIT NONE
-      !!
+      USE lib_mpp
+      IMPLICIT NONE
+      !
       NAMELIST/namagrif/ nn_cln_update, rn_sponge_tra, rn_sponge_dyn, ln_spc_dyn
       !!----------------------------------------------------------------------
@@ -505,4 +476,6 @@
       visc_dyn      = rn_sponge_dyn
       !
+      IF( agrif_oce_alloc()  > 0 )   CALL ctl_warn('agrif sol_oce_alloc: allocation of arrays failed')
+      !
     END SUBROUTINE agrif_nemo_init
 
@@ -514,8 +487,7 @@
       !!----------------------------------------------------------------------
       USE dom_oce
-      !!
-      IMPLICIT NONE
-      !!
-      INTEGER :: indglob,indloc,nprocloc,i
+      IMPLICIT NONE
+      !
+      INTEGER :: indglob, indloc, nprocloc, i
       !!----------------------------------------------------------------------
       !
@@ -534,5 +506,5 @@
    SUBROUTINE Subcalledbyagrif
       !!----------------------------------------------------------------------
-      !!   *** ROUTINE Subcalledbyagrif ***
+      !!                   *** ROUTINE Subcalledbyagrif ***
       !!----------------------------------------------------------------------
       WRITE(*,*) 'Impossible to be here'
