Index: /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_oce.F90
===================================================================
--- /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_oce.F90	(revision 2676)
+++ /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_oce.F90	(revision 2677)
@@ -14,11 +14,13 @@
    
    IMPLICIT NONE
-   PUBLIC 
+   PRIVATE 
+
+   PUBLIC agrif_oce_alloc ! routine called by nemo_init in nemogcm.F90
 
    !                                              !!* Namelist namagrif: AGRIF parameters
    LOGICAL , PUBLIC ::   ln_spc_dyn    = .FALSE.   !:
    INTEGER , PUBLIC ::   nn_cln_update = 3         !: update frequency 
-   REAL(wp), PUBLIC ::   rn_sponge_tra = rdt       !: sponge coeff. for tracers
-   REAL(wp), PUBLIC ::   rn_sponge_dyn = rdt       !: sponge coeff. for dynamics
+   REAL(wp), PUBLIC ::   rn_sponge_tra = 2800.       !: sponge coeff. for tracers
+   REAL(wp), PUBLIC ::   rn_sponge_dyn = 2800.       !: sponge coeff. for dynamics
 
    !                                              !!! OLD namelist names
@@ -29,5 +31,25 @@
    LOGICAL , PUBLIC :: spongedoneT = .FALSE.   !: tracer   sponge layer indicator
    LOGICAL , PUBLIC :: spongedoneU = .FALSE.   !: dynamics sponge layer indicator
-   REAL(wp), PUBLIC, DIMENSION(jpi,jpj) ::   spe1ur, spe2vr ,spbtr2, spe1ur2, spe2vr2, spbtr3   !: ???
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE,  DIMENSION(:,:) ::   spe1ur, spe2vr ,spbtr2, spe1ur2, spe2vr2, spbtr3   !: ???
+   
+   INTEGER :: tn_id,sn_id,tb_id,sb_id,ta_id,sa_id
+   INTEGER :: un_id, vn_id, ua_id, va_id
+   INTEGER :: e1u_id, e2v_id, sshn_id, gcb_id
+   INTEGER :: trn_id, trb_id, tra_id
+
+   CONTAINS 
+
+   FUNCTION agrif_oce_alloc()
+     IMPLICIT none
+     INTEGER :: agrif_oce_alloc
+     INTEGER :: ierr
+
+     ALLOCATE(spe1ur (jpi,jpj), spe2vr (jpi,jpj), spbtr2(jpi,jpj), &
+              spe1ur2(jpi,jpj), spe2vr2(jpi,jpj), spbtr3(jpi,jpj),  &
+              Stat = ierr ) 
+
+      agrif_oce_alloc = ierr 
+
+   END FUNCTION agrif_oce_alloc
 
 #endif
Index: /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_opa_interp.F90
===================================================================
--- /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_opa_interp.F90	(revision 2676)
+++ /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_opa_interp.F90	(revision 2677)
@@ -25,4 +25,6 @@
    USE phycst
    USE in_out_manager
+   USE agrif_opa_sponge
+   USE lib_mpp
 
    IMPLICIT NONE
@@ -45,11 +47,20 @@
       !!                  ***  ROUTINE Agrif_Tra  ***
       !!----------------------------------------------------------------------
+      USE wrk_nemo, ONLY: wrk_in_use, wrk_not_released
+      USE wrk_nemo, ONLY: wrk_3d_1, wrk_3d_2
+      !!
       INTEGER  ::   ji, jj, jk   ! dummy loop indices
       REAL(wp) ::   zrhox , alpha1, alpha2, alpha3
       REAL(wp) ::   alpha4, alpha5, alpha6, alpha7
-      REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zta, zsa   ! 3D workspace
+      REAL(wp), POINTER, DIMENSION(:,:,:) :: zta, zsa
       !!----------------------------------------------------------------------
       !
       IF( Agrif_Root() )   RETURN
+
+      zta => wrk_3d_1 ; zsa => wrk_3d_2
+      IF( wrk_in_use(3, 1,2) )THEN
+         CALL ctl_stop('agrif_tra: requested workspace arrays unavailable.')
+         RETURN
+      END IF
 
       Agrif_SpecialValue    = 0.e0
@@ -58,6 +69,6 @@
       zsa(:,:,:) = 0.e0
 
-      CALL Agrif_Bc_variable( zta, tn )
-      CALL Agrif_Bc_variable( zsa, sn )
+      CALL Agrif_Bc_variable( zta, tn_id, procname = interptn )
+      CALL Agrif_Bc_variable( zsa, sn_id, procname = interpsn )
       Agrif_UseSpecialValue = .FALSE.
 
@@ -162,4 +173,8 @@
       ENDIF
       !
+      IF( wrk_not_released(3, 1,2) ) THEN
+         CALL ctl_stop('agrif_tra: failed to release workspace arrays.')
+      ENDIF
+      !
    END SUBROUTINE Agrif_tra
 
@@ -169,4 +184,9 @@
       !!                  ***  ROUTINE Agrif_DYN  ***
       !!----------------------------------------------------------------------  
+      USE wrk_nemo, ONLY: wrk_in_use, wrk_not_released
+      USE wrk_nemo, ONLY: wrk_2d_4, wrk_2d_5
+      USE wrk_nemo, ONLY: wrk_2d_6, wrk_2d_7
+      USE wrk_nemo, ONLY: wrk_3d_1, wrk_3d_2
+      !! 
       INTEGER, INTENT(in) ::   kt
       !!
@@ -175,10 +195,17 @@
       REAL(wp) :: z2dt, znugdt
       REAL(wp) :: zrhox, rhoy
-      REAL(wp), DIMENSION(jpi,jpj) :: zua2d, zva2d
-      REAL(wp), DIMENSION(jpi,jpj) :: spgu1,spgv1
-      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zua, zva
+      REAL(wp), POINTER, DIMENSION(:,:,:) :: zua, zva
+      REAL(wp), POINTER, DIMENSION(:,:) :: spgv1, spgu1, zua2d, zva2d
       !!----------------------------------------------------------------------  
 
       IF( Agrif_Root() )   RETURN
+
+      spgu1  => wrk_2d_4 ; spgv1 => wrk_2d_5
+      zua2d  => wrk_2d_6 ; zva2d => wrk_2d_7
+      zua  => wrk_3d_1 ; zva => wrk_3d_2
+      IF( wrk_in_use(2, 4,5,6,7) .OR. wrk_in_use(3, 1,2) )THEN
+         CALL ctl_stop('agrif_dyn: requested workspace arrays unavailable.')
+         RETURN
+      END IF
 
       zrhox = Agrif_Rhox()
@@ -199,6 +226,6 @@
       zua = 0.
       zva = 0.
-      CALL Agrif_Bc_variable(zua,un,procname=interpu)
-      CALL Agrif_Bc_variable(zva,vn,procname=interpv)
+      CALL Agrif_Bc_variable(zua,un_id,procname=interpu)
+      CALL Agrif_Bc_variable(zva,vn_id,procname=interpv)
       zua2d = 0.
       zva2d = 0.
@@ -206,6 +233,6 @@
       Agrif_SpecialValue=0.
       Agrif_UseSpecialValue = ln_spc_dyn
-      CALL Agrif_Bc_variable(zua2d,e1u,calledweight=1.,procname=interpu2d)
-      CALL Agrif_Bc_variable(zva2d,e2v,calledweight=1.,procname=interpv2d)
+      CALL Agrif_Bc_variable(zua2d,e1u_id,calledweight=1.,procname=interpu2d)
+      CALL Agrif_Bc_variable(zva2d,e2v_id,calledweight=1.,procname=interpv2d)
       Agrif_UseSpecialValue = .FALSE.
 
@@ -492,5 +519,9 @@
 
       ENDIF
-
+      !
+      IF( wrk_not_released(3, 1,2) .OR. wrk_not_released(2, 4,5,6,7)) THEN
+         CALL ctl_stop('agrif_dyn: failed to release workspace arrays.')
+      ENDIF
+      !
    END SUBROUTINE Agrif_dyn
 
Index: /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_opa_sponge.F90
===================================================================
--- /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_opa_sponge.F90	(revision 2676)
+++ /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_opa_sponge.F90	(revision 2677)
@@ -27,15 +27,26 @@
       !!---------------------------------------------
 #include "domzgr_substitute.h90"
-
+      USE wrk_nemo, ONLY: wrk_in_use, wrk_not_released
+      USE wrk_nemo, ONLY: wrk_2d_1
+      USE wrk_nemo, ONLY: wrk_3d_1, wrk_3d_2
+      USE wrk_nemo, ONLY: wrk_3d_3, wrk_3d_4
+      USE wrk_nemo, ONLY: wrk_3d_7, wrk_3d_6
+      USE wrk_nemo, ONLY: wrk_3d_8
+      !!
       INTEGER :: ji,jj,jk
       INTEGER :: spongearea
       REAL(wp) :: timecoeff
       REAL(wp) :: zta, zsa, zabe1, zabe2, zbtr
-      REAL(wp), DIMENSION(jpi,jpj) :: localviscsponge
-      REAL(wp), DIMENSION(jpi,jpj,jpk) :: tbdiff, sbdiff
-      REAL(wp), DIMENSION(jpi,jpj,jpk) :: ztu ,ztv, zsu ,zsv
-      REAL(wp), DIMENSION(jpi,jpj,jpk) ::  ztab
+      REAL(wp), POINTER, DIMENSION(:,:) :: localviscsponge
+      REAL(wp), POINTER, DIMENSION(:,:,:) :: tbdiff, sbdiff
+      REAL(wp), POINTER, DIMENSION(:,:,:) :: ztu, zsu, ztv, zsv
+      REAL(wp), POINTER, DIMENSION(:,:,:) :: ztab
 
 #if defined SPONGE
+      localviscsponge => wrk_2d_1
+      tbdiff => wrk_3d_1 ;sbdiff => wrk_3d_2
+      ztu => wrk_3d_3 ; zsu => wrk_3d_4
+      ztv => wrk_3d_7 ; zsv => wrk_3d_6
+      ztab => wrk_3d_8
 
       timecoeff = REAL(Agrif_NbStepint(),wp)/Agrif_rhot()
@@ -44,5 +55,5 @@
       Agrif_UseSpecialValue = .TRUE.
       ztab = 0.e0
-      CALL Agrif_Bc_Variable(ztab, ta,calledweight=timecoeff,procname=interptn)
+      CALL Agrif_Bc_Variable(ztab, ta_id,calledweight=timecoeff,procname=interptn)
       Agrif_UseSpecialValue = .FALSE.
 
@@ -52,9 +63,8 @@
       Agrif_SpecialValue=0.
       Agrif_UseSpecialValue = .TRUE.
-      CALL Agrif_Bc_Variable(ztab, sa,calledweight=timecoeff,procname=interpsn)
+      CALL Agrif_Bc_Variable(ztab, sa_id,calledweight=timecoeff,procname=interpsn)
       Agrif_UseSpecialValue = .FALSE.
 
       sbdiff(:,:,:) = sb(:,:,:) - ztab(:,:,:)
-
 
       spongearea = 2 + 2 * Agrif_irhox()
@@ -164,13 +174,24 @@
       !!---------------------------------------------
 #include "domzgr_substitute.h90"
-
+      USE wrk_nemo, ONLY: wrk_in_use, wrk_not_released
+      USE wrk_nemo, ONLY: wrk_2d_1
+      USE wrk_nemo, ONLY: wrk_3d_1, wrk_3d_2
+      USE wrk_nemo, ONLY: wrk_3d_3, wrk_3d_4
+      USE wrk_nemo, ONLY: wrk_3d_5
+      !!
       INTEGER :: ji,jj,jk
       INTEGER :: spongearea
       REAL(wp) :: timecoeff
       REAL(wp) :: ze2u, ze1v, zua, zva, zbtr
-      REAL(wp), DIMENSION(jpi,jpj) :: localviscsponge
-      REAL(wp), DIMENSION(jpi,jpj,jpk) :: ztab, ubdiff, vbdiff,rotdiff,hdivdiff
+      REAL(wp), POINTER, DIMENSION(:,:) :: localviscsponge
+      REAL(wp), POINTER, DIMENSION(:,:,:) :: ubdiff, vbdiff
+      REAL(wp), POINTER, DIMENSION(:,:,:) :: rotdiff, hdivdiff
+      REAL(wp), POINTER, DIMENSION(:,:,:) :: ztab
 
 #if defined SPONGE
+      localviscsponge => wrk_2d_1
+      ubdiff => wrk_3d_1 ; vbdiff => wrk_3d_2
+      rotdiff => wrk_3d_3 ; hdivdiff => wrk_3d_4
+      ztab => wrk_3d_5
 
       timecoeff = REAL(Agrif_NbStepint(),wp)/Agrif_rhot()
@@ -179,5 +200,5 @@
       Agrif_UseSpecialValue = ln_spc_dyn
       ztab = 0.e0
-      CALL Agrif_Bc_Variable(ztab, ua,calledweight=timecoeff,procname=interpun)
+      CALL Agrif_Bc_Variable(ztab, ua_id,calledweight=timecoeff,procname=interpun)
       Agrif_UseSpecialValue = .FALSE.
 
@@ -187,5 +208,5 @@
       Agrif_SpecialValue=0.
       Agrif_UseSpecialValue = ln_spc_dyn
-      CALL Agrif_Bc_Variable(ztab, va,calledweight=timecoeff,procname=interpvn)
+      CALL Agrif_Bc_Variable(ztab, va_id,calledweight=timecoeff,procname=interpvn)
       Agrif_UseSpecialValue = .FALSE.
 
@@ -250,5 +271,5 @@
          spongedoneU = .TRUE.
 	 
-	 spbtr3(:,:) = 1./( e1f(:,:) * e2f(:,:))
+	  spbtr3(:,:) = 1./( e1f(:,:) * e2f(:,:))
       ENDIF
       
Index: /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_opa_update.F90
===================================================================
--- /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_opa_update.F90	(revision 2676)
+++ /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_opa_update.F90	(revision 2677)
@@ -7,4 +7,6 @@
    USE dom_oce
    USE agrif_oce
+   USE in_out_manager  ! I/O manager
+   USE lib_mpp
 
    IMPLICIT NONE
@@ -27,22 +29,35 @@
       !!   *** ROUTINE Agrif_Update_Tra ***
       !!---------------------------------------------
+      USE wrk_nemo, ONLY: wrk_in_use, wrk_not_released
+      USE wrk_nemo, ONLY: wrk_3d_1
+      !!
       INTEGER, INTENT(in) :: kt
-
-      REAL :: ztab(jpi,jpj,jpk)
-
+      REAL(wp), POINTER, DIMENSION(:,:,:) :: ztab
+
+       
       IF((Agrif_NbStepint() .NE. (Agrif_irhot()-1)).AND.(kt /= 0)) RETURN
 #if defined TWO_WAY
+      ztab => wrk_3d_1
+      IF( wrk_in_use(3, 1) ) THEN
+         CALL ctl_stop('agrif_update_tra: ERROR: requested workspace arrays unavailable')
+         RETURN
+      END IF
+
       Agrif_UseSpecialValueInUpdate = .TRUE.
       Agrif_SpecialValueFineGrid = 0.
 
       IF (MOD(nbcline,nbclineupdate) == 0) THEN
-         CALL Agrif_Update_Variable(ztab,tn, procname=updateT)
-         CALL Agrif_Update_Variable(ztab,sn, procname=updateS)
-      ELSE
-         CALL Agrif_Update_Variable(ztab,tn,locupdate=(/0,2/), procname=updateT)
-         CALL Agrif_Update_Variable(ztab,sn,locupdate=(/0,2/), procname=updateS)
+         CALL Agrif_Update_Variable(ztab,tn_id, procname=updateT)
+         CALL Agrif_Update_Variable(ztab,sn_id, procname=updateS)
+      ELSE
+         CALL Agrif_Update_Variable(ztab,tn_id,locupdate=(/0,2/), procname=updateT)
+         CALL Agrif_Update_Variable(ztab,sn_id,locupdate=(/0,2/), procname=updateS)
       ENDIF
 
       Agrif_UseSpecialValueInUpdate = .FALSE.
+
+      IF( wrk_not_released(3, 1) ) THEN
+         CALL ctl_stop('Agrif_Update_Tra: ERROR: failed to release workspace arrays')
+      END IF
 #endif
 
@@ -53,22 +68,31 @@
       !!   *** ROUTINE Agrif_Update_Dyn ***
       !!---------------------------------------------
+      USE wrk_nemo, ONLY: wrk_in_use, wrk_not_released
+      USE wrk_nemo, ONLY: wrk_2d_1
+      USE wrk_nemo, ONLY: wrk_3d_1
+      !!
       INTEGER, INTENT(in) :: kt
-
-      REAL(wp), DIMENSION(jpi,jpj) :: ztab2d
-      REAL(wp), DIMENSION(jpi,jpj,jpk) :: ztab
+      REAL(wp), POINTER, DIMENSION(:,:) :: ztab2d
+      REAL(wp), POINTER, DIMENSION(:,:,:) :: ztab
+
 
       IF ((Agrif_NbStepint() .NE. (Agrif_irhot()-1)).AND.(kt /= 0)) Return
 #if defined TWO_WAY
+      ztab => wrk_3d_1 ; ztab2d => wrk_2d_1
+      IF( ( wrk_in_use(2, 1)) .OR.  wrk_in_use(3, 1) )THEN
+         CALL ctl_stop('agrif_update_dyn: ERROR: requested workspace arrays unavailable')
+         RETURN
+      END IF
 
       IF (mod(nbcline,nbclineupdate) == 0) THEN
-         CALL Agrif_Update_Variable(ztab,un,procname = updateU)
-         CALL Agrif_Update_Variable(ztab,vn,procname = updateV)
-      ELSE
-         CALL Agrif_Update_Variable(ztab,un,locupdate=(/0,1/),procname = updateU)
-         CALL Agrif_Update_Variable(ztab,vn,locupdate=(/0,1/),procname = updateV)         
-      ENDIF
-
-      CALL Agrif_Update_Variable(ztab2d,e1u,procname = updateU2d)
-      CALL Agrif_Update_Variable(ztab2d,e2v,procname = updateV2d)  
+         CALL Agrif_Update_Variable(ztab,un_id,procname = updateU)
+         CALL Agrif_Update_Variable(ztab,vn_id,procname = updateV)
+      ELSE
+         CALL Agrif_Update_Variable(ztab,un_id,locupdate=(/0,1/),procname = updateU)
+         CALL Agrif_Update_Variable(ztab,vn_id,locupdate=(/0,1/),procname = updateV)         
+      ENDIF
+
+      CALL Agrif_Update_Variable(ztab2d,e1u_id,procname = updateU2d)
+      CALL Agrif_Update_Variable(ztab2d,e2v_id,procname = updateV2d)  
 
       nbcline = nbcline + 1
@@ -76,7 +100,10 @@
       Agrif_UseSpecialValueInUpdate = ln_spc_dyn
       Agrif_SpecialValueFineGrid = 0.
-      CALL Agrif_Update_Variable(ztab2d,sshn,procname = updateSSH)
+      CALL Agrif_Update_Variable(ztab2d,sshn_id,procname = updateSSH)
       Agrif_UseSpecialValueInUpdate = .FALSE.
 
+      IF( wrk_not_released(2, 1) .OR. wrk_not_released(3, 1) )THEN
+         CALL ctl_stop('agrif_update_dyn: ERROR: failed to release workspace arrays')
+      END IF
 
 !Done in step
@@ -184,4 +211,5 @@
                DO ji=i1,i2
                   tabres(ji,jj,jk) = e2u(ji,jj) * un(ji,jj,jk)
+                  tabres(ji,jj,jk) = tabres(ji,jj,jk) * fse3u(ji,jj,jk)
                END DO
             END DO
Index: /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_top_interp.F90
===================================================================
--- /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_top_interp.F90	(revision 2676)
+++ /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_top_interp.F90	(revision 2677)
@@ -6,5 +6,7 @@
    USE sol_oce
    USE agrif_oce
+   USE agrif_top_sponge
    USE trc
+   USE lib_mpp
 
    IMPLICIT NONE
@@ -13,5 +15,7 @@
    PUBLIC Agrif_trc
 
-   !!----------------------------------------------------------------------
+#  include "domzgr_substitute.h90"  
+#  include "vectopt_loop_substitute.h90"
+  !!----------------------------------------------------------------------
    !! NEMO/NST 3.3 , NEMO Consortium (2010)
    !! $Id$
@@ -25,6 +29,6 @@
       !!   *** ROUTINE Agrif_trc ***
       !!---------------------------------------------
-#  include "domzgr_substitute.h90"  
-#  include "vectopt_loop_substitute.h90"
+      USE wrk_nemo, ONLY: wrk_in_use, wrk_not_released
+      USE wrk_nemo, ONLY: wrk_4d_1
       
       INTEGER :: ji,jj,jk,jn
@@ -32,7 +36,13 @@
       REAL(wp) :: alpha1, alpha2, alpha3, alpha4
       REAL(wp) :: alpha5, alpha6, alpha7
-      REAL(wp), DIMENSION(jpi,jpj,jpk,jptra) :: ztra
-      
+      REAL(wp), POINTER, DIMENSION(:,:,:,:) :: ztra
+           
       IF (Agrif_Root()) RETURN
+
+      IF( wrk_in_use(4, 1) ) THEN
+         CALL ctl_stop('Agrif_trc : requested workspace arrays unavailable')
+         RETURN
+      ENDIF
+      ztra =>  wrk_4d_1(:,:,:,jptra)
 
       Agrif_SpecialValue=0.
@@ -40,5 +50,5 @@
       ztra = 0.e0
 
-      CALL Agrif_Bc_variable(ztra,trn)
+      CALL Agrif_Bc_variable(ztra,trn_id, procname = interptrn )
       Agrif_UseSpecialValue = .FALSE.
 
@@ -131,4 +141,9 @@
       ENDIF
 
+      IF( wrk_not_released(4, 1) ) THEN
+         CALL ctl_stop('Agrif_trc : failed to release workspace arrays.')
+         RETURN
+      ENDIF
+
    END SUBROUTINE Agrif_trc
 
Index: /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_top_sponge.F90
===================================================================
--- /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_top_sponge.F90	(revision 2676)
+++ /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_top_sponge.F90	(revision 2677)
@@ -9,4 +9,5 @@
    USE agrif_oce
    USE trc
+   USE lib_mpp
 
    IMPLICIT NONE
@@ -28,15 +29,25 @@
       !!---------------------------------------------
 #include "domzgr_substitute.h90"
-
+      USE wrk_nemo, ONLY: wrk_in_use, wrk_not_released
+      USE wrk_nemo, ONLY: wrk_2d_1
+      USE wrk_nemo, ONLY: wrk_4d_1, wrk_4d_2, wrk_4d_3, wrk_4d_4
+      !! 
       INTEGER :: ji,jj,jk,jl
       INTEGER :: spongearea
       REAL(wp) :: timecoeff
       REAL(wp) :: ztra, zabe1, zabe2, zbtr
-      REAL(wp), DIMENSION(jpi,jpj) :: localviscsponge
-      REAL(wp), DIMENSION(jpi,jpj,jpk,jptra) :: trbdiff
-      REAL(wp), DIMENSION(jpi,jpj,jpk,jptra) :: ztru ,ztrv
-      REAL(wp), DIMENSION(jpi,jpj,jpk,jptra) ::  ztab
+      REAL(wp), POINTER, DIMENSION(:,:) :: localviscsponge
+      REAL(wp), POINTER, DIMENSION(:,:,:,:) :: trbdiff, ztru, ztrv, ztab
 
 #if defined SPONGE_TOP
+      IF( wrk_in_use(4, 1,2,3,4) .OR. wrk_in_use(2, 1) ) THEN
+         CALL ctl_stop('Agrif_Sponge_trc : requested workspace arrays unavailable')
+         RETURN
+      ENDIF
+      localviscsponge => wrk_2d_1
+      trbdiff(:,:,:,:) => wrk_4d_1(:,:,:,1:jptra)
+      ztru   (:,:,:,:) => wrk_4d_2(:,:,:,1:jptra)
+      ztrv   (:,:,:,:) => wrk_4d_3(:,:,:,1:jptra)
+      ztab   (:,:,:,:) => wrk_4d_4(:,:,:,1:jptra)
 
       timecoeff = REAL(Agrif_NbStepint(),wp)/Agrif_rhot()
@@ -45,5 +56,5 @@
       Agrif_UseSpecialValue = .TRUE.
       ztab = 0.e0
-      CALL Agrif_Bc_Variable(ztab, tra,calledweight=timecoeff,procname=interptrn)
+      CALL Agrif_Bc_Variable(ztab, tra_id,calledweight=timecoeff,procname=interptrn)
       Agrif_UseSpecialValue = .FALSE.
 
@@ -143,4 +154,9 @@
       ENDDO
       ENDDO
+ 
+      IF( wrk_not_released(4, 1,2,3,4) .OR. wrk_not_released(2, 1) ) THEN
+         CALL ctl_stop('Agrif_Sponge_trc : failed to release workspace arrays.')
+         RETURN
+      ENDIF
 
 #endif
Index: /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_top_update.F90
===================================================================
--- /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_top_update.F90	(revision 2676)
+++ /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_top_update.F90	(revision 2677)
@@ -29,22 +29,36 @@
       !!   *** ROUTINE Agrif_Update_Trc ***
       !!---------------------------------------------
+      USE wrk_nemo, ONLY: wrk_in_use, wrk_not_released
+      USE wrk_nemo, ONLY: wrk_4d_1
+      !!
       INTEGER, INTENT(in) :: kt
+      REAL(wp), POINTER, DIMENSION(:,:,:,:) :: ztra
+
   
-      REAL(wp), DIMENSION(jpi,jpj,jpk,jptra) :: ztra
-
       IF ((Agrif_NbStepint() .NE. (Agrif_irhot()-1)).AND.(kt /= 0)) RETURN
 
 #if defined TWO_WAY
+      IF( wrk_in_use(4, 1) ) THEN
+         CALL ctl_stop('Agrif_Update_trc : requested workspace arrays unavailable')
+         RETURN
+      ENDIF
+      ztra =>  wrk_4d_1(:,:,:,jptra)
+
       Agrif_UseSpecialValueInUpdate = .TRUE.
       Agrif_SpecialValueFineGrid = 0.
  
      IF (MOD(nbcline_trc,nbclineupdate) == 0) THEN
-         CALL Agrif_Update_Variable(ztra,trn, procname=updateTRC)
+         CALL Agrif_Update_Variable(ztra,trn_id, procname=updateTRC)
       ELSE
-         CALL Agrif_Update_Variable(ztra,trn,locupdate=(/0,2/), procname=updateTRC)
+         CALL Agrif_Update_Variable(ztra,trn_id,locupdate=(/0,2/), procname=updateTRC)
       ENDIF
 
       Agrif_UseSpecialValueInUpdate = .FALSE.
       nbcline_trc = nbcline_trc + 1
+
+      IF( wrk_not_released(4, 1) ) THEN
+         CALL ctl_stop('Agrif_Update_trc : failed to release workspace arrays.')
+         RETURN
+      ENDIF
 #endif
 
Index: /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_user.F90
===================================================================
--- /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_user.F90	(revision 2676)
+++ /branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/NST_SRC/agrif_user.F90	(revision 2677)
@@ -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,17 @@
       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 +42,4 @@
    END SUBROUTINE Agrif_InitWorkspace
 
-#if ! defined key_offline
 
    SUBROUTINE Agrif_InitValues
@@ -67,7 +53,4 @@
       USE dom_oce
       USE nemogcm
-#if defined key_top
-      USE trc
-#endif
 #if defined key_tradmp   ||   defined key_esopa
       USE tradmp
@@ -76,21 +59,7 @@
       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
+      !!
 
       ! 0. Initializations
@@ -111,153 +80,62 @@
 #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.
@@ -306,31 +184,102 @@
             ENDIF
          ENDIF
-#if defined key_top
-        ! 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(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
@@ -348,59 +297,29 @@
       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
-
+      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_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_Set_bc(trn,(/0,1/))
-      Call Agrif_Set_bc(tra,(/-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
+      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,calledweight=1.)
-      Call Agrif_Bc_variable(tabtrtemp,tra,calledweight=1.,procname=interptrn)
+      Call Agrif_Bc_variable(tabtrtemp,trn_id,calledweight=1.)
+      Call Agrif_Bc_variable(tabtrtemp,tra_id,calledweight=1.,procname=interptrn)
       Agrif_UseSpecialValue = .FALSE.
 
-      ! 7. Some controls
+      ! 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
@@ -444,17 +363,76 @@
             ENDIF
          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
+      DEALLOCATE(tabtrtemp)
+      !
+   END SUBROUTINE Agrif_InitValues_cont_top
+
+
+   SUBROUTINE agrif_declare_var_top
+      !!----------------------------------------------------------------------
+      !!                 *** ROUTINE agrif_declare_var_top ***
+      !!
+      !! ** Purpose :: Declaration of TOP variables to be interpolated
+      !!----------------------------------------------------------------------
+      USE agrif_util
+      USE dom_oce
+      USE trc
+      
+      IMPLICIT NONE
+   
+      ! 1. Declaration of the type of variable which have to be interpolated
+      !---------------------------------------------------------------------
+      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
+      !-----------------------------
+#  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_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( g, sizex )
@@ -479,8 +457,10 @@
       USE agrif_oce 
       USE in_out_manager
+      USE lib_mpp
       !!
       IMPLICIT NONE
       !!
       NAMELIST/namagrif/ nn_cln_update, rn_sponge_tra, rn_sponge_dyn, ln_spc_dyn
+      INTEGER :: ierr
       !!----------------------------------------------------------------------
       !
@@ -505,4 +485,7 @@
       visc_dyn      = rn_sponge_dyn
       !
+      ierr = agrif_oce_alloc()
+      IF( ierr  > 0 )   CALL ctl_warn('agrif sol_oce_alloc: allocation of arrays failed')
+      !
     END SUBROUTINE agrif_nemo_init
 
