Index: NEMO/branches/2019/dev_r10721_KERNEL-02_Storkey_Coward_IMMERSE_first_steps_rewrite_time_filterswap/src/OCE/DYN/dynatf.F90
===================================================================
--- NEMO/branches/2019/dev_r10721_KERNEL-02_Storkey_Coward_IMMERSE_first_steps_rewrite_time_filterswap/src/OCE/DYN/dynnxt.F90	(revision 11021)
+++ NEMO/branches/2019/dev_r10721_KERNEL-02_Storkey_Coward_IMMERSE_first_steps_rewrite_time_filterswap/src/OCE/DYN/dynatf.F90	(revision 11050)
@@ -1,6 +1,6 @@
-MODULE dynnxt
+MODULE dynatf
    !!=========================================================================
-   !!                       ***  MODULE  dynnxt  ***
-   !! Ocean dynamics: time stepping
+   !!                       ***  MODULE  dynatf  ***
+   !! Ocean dynamics: time filtering
    !!=========================================================================
    !! History :  OPA  !  1987-02  (P. Andrich, D. L Hostis)  Original code
@@ -20,9 +20,10 @@
    !!            3.6  !  2014-04  (G. Madec) add the diagnostic of the time filter trends
    !!            3.7  !  2015-11  (J. Chanut) Free surface simplification
+   !!            4.1  !  2019-05  (D. Storkey) Rename dynnxt -> dynatf. Now just does time filtering.
    !!-------------------------------------------------------------------------
   
-   !!-------------------------------------------------------------------------
-   !!   dyn_nxt       : obtain the next (after) horizontal velocity
-   !!-------------------------------------------------------------------------
+   !!----------------------------------------------------------------------------------------------
+   !!   dyn_atf       : apply Asselin time filtering to "now" velocities and vertical scale factors
+   !!----------------------------------------------------------------------------------------------
    USE oce            ! ocean dynamics and tracers
    USE dom_oce        ! ocean space and time domain
@@ -50,10 +51,10 @@
 #if defined key_agrif
    USE agrif_oce_interp
-#endif   
+#endif
 
    IMPLICIT NONE
    PRIVATE
 
-   PUBLIC    dyn_nxt   ! routine called by step.F90
+   PUBLIC    dyn_atf   ! routine called by step.F90
 
    !!----------------------------------------------------------------------
@@ -64,8 +65,10 @@
 CONTAINS
 
-   SUBROUTINE dyn_nxt ( kt, Kbb, Kmm, Kaa )
+   SUBROUTINE dyn_atf ( kt, Kbb, Kmm, Kaa, puu, pvv, pe3t, pe3u, pe3v )
       !!----------------------------------------------------------------------
-      !!                  ***  ROUTINE dyn_nxt  ***
+      !!                  ***  ROUTINE dyn_atf  ***
       !!                   
+      !!  !!!!!!!!!!!!!!!!!!  REWRITE HEADER COMMENTS !!!!!!!!!!!!!!!!!
+      !!
       !! ** Purpose :   Finalize after horizontal velocity. Apply the boundary 
       !!             condition on the after velocity, achieve the time stepping 
@@ -83,25 +86,25 @@
       !!              * Apply the time filter applied and swap of the dynamics
       !!             arrays to start the next time step:
-      !!                (ub,vb) = (un,vn) + atfp [ (ub,vb) + (ua,va) - 2 (un,vn) ]
-      !!                (un,vn) = (ua,va).
+      !!                (puu(:,:,:,Kbb),pvv(:,:,:,Kbb)) = (puu(:,:,:,Kmm),pvv(:,:,:,Kmm)) + atfp [ (puu(:,:,:,Kbb),pvv(:,:,:,Kbb)) + (puu(:,:,:,Kaa),pvv(:,:,:,Kaa)) - 2 (puu(:,:,:,Kmm),pvv(:,:,:,Kmm)) ]
+      !!                (puu(:,:,:,Kmm),pvv(:,:,:,Kmm)) = (puu(:,:,:,Kaa),pvv(:,:,:,Kaa)).
       !!             Note that with flux form advection and non linear free surface,
       !!             the time filter is applied on thickness weighted velocity.
-      !!             As a result, dyn_nxt MUST be called after tra_nxt.
-      !!
-      !! ** Action :   ub,vb   filtered before horizontal velocity of next time-step
-      !!               un,vn   now horizontal velocity of next time-step
+      !!             As a result, dyn_atf MUST be called after tra_nxt.
+      !!
+      !! ** Action :   uu(Kmm),vv(Kmm)   filtered now horizontal velocity 
       !!----------------------------------------------------------------------
-      INTEGER, INTENT( in ) ::   kt             ! ocean time-step index
-      INTEGER, INTENT( in ) ::   Kbb, Kmm, Kaa  ! time level indices
+      INTEGER                             , INTENT(in   ) :: kt               ! ocean time-step index
+      INTEGER                             , INTENT(in   ) :: Kbb, Kmm, Kaa    ! before and after time level indices
+      REAL(wp), DIMENSION(jpi,jpj,jpk,jpt), INTENT(inout) :: puu, pvv         ! velocities to be time filtered
+      REAL(wp), DIMENSION(jpi,jpj,jpk,jpt), INTENT(inout) :: pe3t, pe3u, pe3v ! scale factors to be time filtered
       !
       INTEGER  ::   ji, jj, jk   ! dummy loop indices
-      INTEGER  ::   ikt          ! local integers
-      REAL(wp) ::   zue3a, zue3n, zue3b, zuf, zcoef    ! local scalars
-      REAL(wp) ::   zve3a, zve3n, zve3b, zvf, z1_2dt   !   -      -
+      REAL(wp) ::   zue3a, zue3n, zue3b, zcoef    ! local scalars
+      REAL(wp) ::   zve3a, zve3n, zve3b, z1_2dt   !   -      -
       REAL(wp), ALLOCATABLE, DIMENSION(:,:)   ::   zue, zve
-      REAL(wp), ALLOCATABLE, DIMENSION(:,:,:) ::   ze3u_f, ze3v_f, zua, zva 
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:,:) ::   ze3t_f, ze3u_f, ze3v_f, zua, zva 
       !!----------------------------------------------------------------------
       !
-      IF( ln_timing    )   CALL timing_start('dyn_nxt')
+      IF( ln_timing    )   CALL timing_start('dyn_atf')
       IF( ln_dynspg_ts )   ALLOCATE( zue(jpi,jpj)     , zve(jpi,jpj)     )
       IF( l_trddyn     )   ALLOCATE( zua(jpi,jpj,jpk) , zva(jpi,jpj,jpk) )
@@ -109,5 +112,5 @@
       IF( kt == nit000 ) THEN
          IF(lwp) WRITE(numout,*)
-         IF(lwp) WRITE(numout,*) 'dyn_nxt : time stepping'
+         IF(lwp) WRITE(numout,*) 'dyn_atf : Asselin time filtering'
          IF(lwp) WRITE(numout,*) '~~~~~~~'
       ENDIF
@@ -116,13 +119,13 @@
          ! Ensure below that barotropic velocities match time splitting estimate
          ! Compute actual transport and replace it with ts estimate at "after" time step
-         zue(:,:) = e3u_a(:,:,1) * ua(:,:,1) * umask(:,:,1)
-         zve(:,:) = e3v_a(:,:,1) * va(:,:,1) * vmask(:,:,1)
+         zue(:,:) = pe3u(:,:,1,Kaa) * puu(:,:,1,Kaa) * umask(:,:,1)
+         zve(:,:) = pe3v(:,:,1,Kaa) * pvv(:,:,1,Kaa) * vmask(:,:,1)
          DO jk = 2, jpkm1
-            zue(:,:) = zue(:,:) + e3u_a(:,:,jk) * ua(:,:,jk) * umask(:,:,jk)
-            zve(:,:) = zve(:,:) + e3v_a(:,:,jk) * va(:,:,jk) * vmask(:,:,jk)
+            zue(:,:) = zue(:,:) + pe3u(:,:,jk,Kaa) * puu(:,:,jk,Kaa) * umask(:,:,jk)
+            zve(:,:) = zve(:,:) + pe3v(:,:,jk,Kaa) * pvv(:,:,jk,Kaa) * vmask(:,:,jk)
          END DO
          DO jk = 1, jpkm1
-            ua(:,:,jk) = ( ua(:,:,jk) - zue(:,:) * r1_hu_a(:,:) + ua_b(:,:) ) * umask(:,:,jk)
-            va(:,:,jk) = ( va(:,:,jk) - zve(:,:) * r1_hv_a(:,:) + va_b(:,:) ) * vmask(:,:,jk)
+            puu(:,:,jk,Kaa) = ( puu(:,:,jk,Kaa) - zue(:,:) * r1_hu(:,:,Kaa) + uu_b(:,:,Kaa) ) * umask(:,:,jk)
+            pvv(:,:,jk,Kaa) = ( pvv(:,:,jk,Kaa) - zve(:,:) * r1_hv(:,:,Kaa) + vv_b(:,:,Kaa) ) * vmask(:,:,jk)
          END DO
          !
@@ -133,6 +136,6 @@
             ! so that asselin contribution is removed at the same time 
             DO jk = 1, jpkm1
-               un(:,:,jk) = ( un(:,:,jk) - un_adv(:,:)*r1_hu_n(:,:) + un_b(:,:) )*umask(:,:,jk)
-               vn(:,:,jk) = ( vn(:,:,jk) - vn_adv(:,:)*r1_hv_n(:,:) + vn_b(:,:) )*vmask(:,:,jk)
+               puu(:,:,jk,Kmm) = ( puu(:,:,jk,Kmm) - un_adv(:,:)*r1_hu(:,:,Kmm) + uu_b(:,:,Kmm) )*umask(:,:,jk)
+               pvv(:,:,jk,Kmm) = ( pvv(:,:,jk,Kmm) - vn_adv(:,:)*r1_hv(:,:,Kmm) + vv_b(:,:,Kmm) )*vmask(:,:,jk)
             END DO  
          ENDIF
@@ -145,13 +148,9 @@
 # endif
       !
-      CALL lbc_lnk_multi( 'dynnxt', ua, 'U', -1., va, 'V', -1. )     !* local domain boundaries
+      CALL lbc_lnk_multi( 'dynnxt', puu(:,:,:,Kaa), 'U', -1., pvv(:,:,:,Kaa), 'V', -1. )     !* local domain boundaries
       !
       !                                !* BDY open boundaries
-      !! IMMERSE development : Following the general pattern for the new code we want to pass in the 
-      !!                       velocities to bdy_dyn as arguments so here we use "uu" and "vv" even 
-      !!                       though we haven't converted the velocity names in the rest of dynnxt.F90
-      !!                       because it will be completely rewritten. DS.
-      IF( ln_bdy .AND. ln_dynspg_exp )   CALL bdy_dyn( kt, Kbb, uu, vv, Kaa )
-      IF( ln_bdy .AND. ln_dynspg_ts  )   CALL bdy_dyn( kt, Kbb, uu, vv, Kaa, dyn3d_only=.true. )
+      IF( ln_bdy .AND. ln_dynspg_exp )   CALL bdy_dyn( kt, Kbb, puu, pvv, Kaa )
+      IF( ln_bdy .AND. ln_dynspg_ts  )   CALL bdy_dyn( kt, Kbb, puu, pvv, Kaa, dyn3d_only=.true. )
 
 !!$   Do we need a call to bdy_vol here??
@@ -162,15 +161,15 @@
          !
          !                                  ! Kinetic energy and Conversion
-         IF( ln_KE_trd  )   CALL trd_dyn( ua, va, jpdyn_ken, kt, Kmm )
+         IF( ln_KE_trd  )   CALL trd_dyn( puu(:,:,:,Kaa), pvv(:,:,:,Kaa), jpdyn_ken, kt, Kmm )
          !
          IF( ln_dyn_trd ) THEN              ! 3D output: total momentum trends
-            zua(:,:,:) = ( ua(:,:,:) - ub(:,:,:) ) * z1_2dt
-            zva(:,:,:) = ( va(:,:,:) - vb(:,:,:) ) * z1_2dt
+            zua(:,:,:) = ( puu(:,:,:,Kaa) - puu(:,:,:,Kbb) ) * z1_2dt
+            zva(:,:,:) = ( pvv(:,:,:,Kaa) - pvv(:,:,:,Kbb) ) * z1_2dt
             CALL iom_put( "utrd_tot", zua )        ! total momentum trends, except the asselin time filter
             CALL iom_put( "vtrd_tot", zva )
          ENDIF
          !
-         zua(:,:,:) = un(:,:,:)             ! save the now velocity before the asselin filter
-         zva(:,:,:) = vn(:,:,:)             ! (caution: there will be a shift by 1 timestep in the
+         zua(:,:,:) = puu(:,:,:,Kmm)             ! save the now velocity before the asselin filter
+         zva(:,:,:) = pvv(:,:,:,Kmm)             ! (caution: there will be a shift by 1 timestep in the
          !                                  !  computation of the asselin filter trends)
       ENDIF
@@ -178,25 +177,6 @@
       ! Time filter and swap of dynamics arrays
       ! ------------------------------------------
-      IF( neuler == 0 .AND. kt == nit000 ) THEN        !* Euler at first time-step: only swap
-         DO jk = 1, jpkm1
-            un(:,:,jk) = ua(:,:,jk)                         ! un <-- ua
-            vn(:,:,jk) = va(:,:,jk)
-         END DO
-         IF( .NOT.ln_linssh ) THEN                          ! e3._b <-- e3._n
-!!gm BUG ????    I don't understand why it is not : e3._n <-- e3._a  
-            DO jk = 1, jpkm1
-!               e3t_b(:,:,jk) = e3t_n(:,:,jk)
-!               e3u_b(:,:,jk) = e3u_n(:,:,jk)
-!               e3v_b(:,:,jk) = e3v_n(:,:,jk)
-               !
-               e3t_n(:,:,jk) = e3t_a(:,:,jk)
-               e3u_n(:,:,jk) = e3u_a(:,:,jk)
-               e3v_n(:,:,jk) = e3v_a(:,:,jk)
-            END DO
-!!gm BUG end
-         ENDIF
-                                                            ! 
          
-      ELSE                                             !* Leap-Frog : Asselin filter and swap
+      IF( .NOT.( neuler == 0 .AND. kt == nit000 ) ) THEN    !* Leap-Frog : Asselin time filter 
          !                                ! =============!
          IF( ln_linssh ) THEN             ! Fixed volume !
@@ -205,11 +185,6 @@
                DO jj = 1, jpj
                   DO ji = 1, jpi    
-                     zuf = un(ji,jj,jk) + atfp * ( ub(ji,jj,jk) - 2._wp * un(ji,jj,jk) + ua(ji,jj,jk) )
-                     zvf = vn(ji,jj,jk) + atfp * ( vb(ji,jj,jk) - 2._wp * vn(ji,jj,jk) + va(ji,jj,jk) )
-                     !
-                     ub(ji,jj,jk) = zuf                      ! ub <-- filtered velocity
-                     vb(ji,jj,jk) = zvf
-                     un(ji,jj,jk) = ua(ji,jj,jk)             ! un <-- ua
-                     vn(ji,jj,jk) = va(ji,jj,jk)
+                     puu(ji,jj,jk,Kmm) = puu(ji,jj,jk,Kmm) + atfp * ( puu(ji,jj,jk,Kbb) - 2._wp * puu(ji,jj,jk,Kmm) + puu(ji,jj,jk,Kaa) )
+                     pvv(ji,jj,jk,Kmm) = pvv(ji,jj,jk,Kmm) + atfp * ( pvv(ji,jj,jk,Kbb) - 2._wp * pvv(ji,jj,jk,Kmm) + pvv(ji,jj,jk,Kaa) )
                   END DO
                END DO
@@ -218,9 +193,9 @@
          ELSE                             ! Variable volume !
             !                             ! ================!
-            ! Before scale factor at t-points
-            ! (used as a now filtered scale factor until the swap)
+            ! Time-filtered scale factor at t-points
             ! ----------------------------------------------------
+            ALLOCATE( ze3t_f(jpi,jpj,jpk) )
             DO jk = 1, jpkm1
-               e3t_b(:,:,jk) = e3t_n(:,:,jk) + atfp * ( e3t_b(:,:,jk) - 2._wp * e3t_n(:,:,jk) + e3t_a(:,:,jk) )
+               ze3t_f(:,:,jk) = pe3t(:,:,jk,Kmm) + atfp * ( pe3t(:,:,jk,Kbb) - 2._wp * pe3t(:,:,jk,Kmm) + pe3t(:,:,jk,Kaa) )
             END DO
             ! Add volume filter correction: compatibility with tracer advection scheme
@@ -228,14 +203,14 @@
             zcoef = atfp * rdt * r1_rau0
 
-            e3t_b(:,:,1) = e3t_b(:,:,1) - zcoef * ( emp_b(:,:) - emp(:,:) ) * tmask(:,:,1)
+            ze3t_f(:,:,1) = ze3t_f(:,:,1) - zcoef * ( emp_b(:,:) - emp(:,:) ) * tmask(:,:,1)
 
             IF ( ln_rnf ) THEN
                IF( ln_rnf_depth ) THEN
-                  DO jk = 1, jpkm1 ! Deal with Rivers separetely, as can be through depth too
+                  DO jk = 1, jpkm1 ! Deal with Rivers separately, as can be through depth too
                      DO jj = 1, jpj
                         DO ji = 1, jpi
                            IF( jk <=  nk_rnf(ji,jj)  ) THEN
-                               e3t_b(ji,jj,jk) =   e3t_b(ji,jj,jk) - zcoef *  ( - rnf_b(ji,jj) + rnf(ji,jj) ) &
-                                      &          * ( e3t_n(ji,jj,jk) / h_rnf(ji,jj) ) * tmask(ji,jj,jk)
+                               ze3t_f(ji,jj,jk) =   ze3t_f(ji,jj,jk) - zcoef *  ( - rnf_b(ji,jj) + rnf(ji,jj) ) &
+                                      &          * ( pe3t(ji,jj,jk,Kmm) / h_rnf(ji,jj) ) * tmask(ji,jj,jk)
                            ENDIF
                         ENDDO
@@ -243,5 +218,5 @@
                   ENDDO
                ELSE
-                  e3t_b(:,:,1) = e3t_b(:,:,1) - zcoef *  ( -rnf_b(:,:) + rnf(:,:))*tmask(:,:,1)
+                  ze3t_f(:,:,1) = ze3t_f(:,:,1) - zcoef *  ( -rnf_b(:,:) + rnf(:,:))*tmask(:,:,1)
                ENDIF
             END IF
@@ -252,9 +227,9 @@
                      DO ji = 1, jpi
                         IF( misfkt(ji,jj) <=jk .and. jk < misfkb(ji,jj)  ) THEN
-                           e3t_b(ji,jj,jk) = e3t_b(ji,jj,jk) - zcoef * ( fwfisf_b(ji,jj) - fwfisf(ji,jj) ) &
-                                &          * ( e3t_n(ji,jj,jk) * r1_hisf_tbl(ji,jj) ) * tmask(ji,jj,jk)
+                           ze3t_f(ji,jj,jk) = ze3t_f(ji,jj,jk) - zcoef * ( fwfisf_b(ji,jj) - fwfisf(ji,jj) ) &
+                                &          * ( pe3t(ji,jj,jk,Kmm) * r1_hisf_tbl(ji,jj) ) * tmask(ji,jj,jk)
                         ELSEIF ( jk==misfkb(ji,jj) ) THEN
-                           e3t_b(ji,jj,jk) = e3t_b(ji,jj,jk) - zcoef * ( fwfisf_b(ji,jj) - fwfisf(ji,jj) ) &
-                                &          * ( e3t_n(ji,jj,jk) * r1_hisf_tbl(ji,jj) ) * ralpha(ji,jj) * tmask(ji,jj,jk)
+                           ze3t_f(ji,jj,jk) = ze3t_f(ji,jj,jk) - zcoef * ( fwfisf_b(ji,jj) - fwfisf(ji,jj) ) &
+                                &          * ( pe3t(ji,jj,jk,Kmm) * r1_hisf_tbl(ji,jj) ) * ralpha(ji,jj) * tmask(ji,jj,jk)
                         ENDIF
                      END DO
@@ -263,18 +238,15 @@
             END IF
             !
+            pe3t(:,:,1:jpkm1,Kmm) = ze3t_f(:,:,1:jpkm1)        ! filtered scale factor at T-points
+            !
             IF( ln_dynadv_vec ) THEN      ! Asselin filter applied on velocity
                ! Before filtered scale factor at (u/v)-points
-               CALL dom_vvl_interpol( e3t_b(:,:,:), e3u_b(:,:,:), 'U' )
-               CALL dom_vvl_interpol( e3t_b(:,:,:), e3v_b(:,:,:), 'V' )
+               CALL dom_vvl_interpol( pe3t(:,:,:,Kmm), pe3u(:,:,:,Kmm), 'U' )
+               CALL dom_vvl_interpol( pe3t(:,:,:,Kmm), pe3v(:,:,:,Kmm), 'V' )
                DO jk = 1, jpkm1
                   DO jj = 1, jpj
                      DO ji = 1, jpi
-                        zuf = un(ji,jj,jk) + atfp * ( ub(ji,jj,jk) - 2._wp * un(ji,jj,jk) + ua(ji,jj,jk) )
-                        zvf = vn(ji,jj,jk) + atfp * ( vb(ji,jj,jk) - 2._wp * vn(ji,jj,jk) + va(ji,jj,jk) )
-                        !
-                        ub(ji,jj,jk) = zuf                      ! ub <-- filtered velocity
-                        vb(ji,jj,jk) = zvf
-                        un(ji,jj,jk) = ua(ji,jj,jk)             ! un <-- ua
-                        vn(ji,jj,jk) = va(ji,jj,jk)
+                        puu(ji,jj,jk,Kmm) = puu(ji,jj,jk,Kmm) + atfp * ( puu(ji,jj,jk,Kbb) - 2._wp * puu(ji,jj,jk,Kmm) + puu(ji,jj,jk,Kaa) )
+                        pvv(ji,jj,jk,Kmm) = pvv(ji,jj,jk,Kmm) + atfp * ( pvv(ji,jj,jk,Kbb) - 2._wp * pvv(ji,jj,jk,Kmm) + pvv(ji,jj,jk,Kaa) )
                      END DO
                   END DO
@@ -284,51 +256,47 @@
                !
                ALLOCATE( ze3u_f(jpi,jpj,jpk) , ze3v_f(jpi,jpj,jpk) )
-               ! Before filtered scale factor at (u/v)-points stored in ze3u_f, ze3v_f
-               CALL dom_vvl_interpol( e3t_b(:,:,:), ze3u_f, 'U' )
-               CALL dom_vvl_interpol( e3t_b(:,:,:), ze3v_f, 'V' )
+               ! Now filtered scale factor at (u/v)-points stored in ze3u_f, ze3v_f
+               CALL dom_vvl_interpol( pe3t(:,:,:,Kmm), ze3u_f, 'U' )
+               CALL dom_vvl_interpol( pe3t(:,:,:,Kmm), ze3v_f, 'V' )
                DO jk = 1, jpkm1
                   DO jj = 1, jpj
                      DO ji = 1, jpi                  
-                        zue3a = e3u_a(ji,jj,jk) * ua(ji,jj,jk)
-                        zve3a = e3v_a(ji,jj,jk) * va(ji,jj,jk)
-                        zue3n = e3u_n(ji,jj,jk) * un(ji,jj,jk)
-                        zve3n = e3v_n(ji,jj,jk) * vn(ji,jj,jk)
-                        zue3b = e3u_b(ji,jj,jk) * ub(ji,jj,jk)
-                        zve3b = e3v_b(ji,jj,jk) * vb(ji,jj,jk)
+                        zue3a = pe3u(ji,jj,jk,Kaa) * puu(ji,jj,jk,Kaa)
+                        zve3a = pe3v(ji,jj,jk,Kaa) * pvv(ji,jj,jk,Kaa)
+                        zue3n = pe3u(ji,jj,jk,Kmm) * puu(ji,jj,jk,Kmm)
+                        zve3n = pe3v(ji,jj,jk,Kmm) * pvv(ji,jj,jk,Kmm)
+                        zue3b = pe3u(ji,jj,jk,Kbb) * puu(ji,jj,jk,Kbb)
+                        zve3b = pe3v(ji,jj,jk,Kbb) * pvv(ji,jj,jk,Kbb)
                         !
-                        zuf = ( zue3n + atfp * ( zue3b - 2._wp * zue3n  + zue3a ) ) / ze3u_f(ji,jj,jk)
-                        zvf = ( zve3n + atfp * ( zve3b - 2._wp * zve3n  + zve3a ) ) / ze3v_f(ji,jj,jk)
-                        !
-                        ub(ji,jj,jk) = zuf                     ! ub <-- filtered velocity
-                        vb(ji,jj,jk) = zvf
-                        un(ji,jj,jk) = ua(ji,jj,jk)            ! un <-- ua
-                        vn(ji,jj,jk) = va(ji,jj,jk)
+                        puu(ji,jj,jk,Kmm) = ( zue3n + atfp * ( zue3b - 2._wp * zue3n  + zue3a ) ) / ze3u_f(ji,jj,jk)
+                        pvv(ji,jj,jk,Kmm) = ( zve3n + atfp * ( zve3b - 2._wp * zve3n  + zve3a ) ) / ze3v_f(ji,jj,jk)
                      END DO
                   END DO
                END DO
-               e3u_b(:,:,1:jpkm1) = ze3u_f(:,:,1:jpkm1)        ! e3u_b <-- filtered scale factor
-               e3v_b(:,:,1:jpkm1) = ze3v_f(:,:,1:jpkm1)
+               pe3u(:,:,1:jpkm1,Kmm) = ze3u_f(:,:,1:jpkm1)  
+               pe3v(:,:,1:jpkm1,Kmm) = ze3v_f(:,:,1:jpkm1)
                !
                DEALLOCATE( ze3u_f , ze3v_f )
             ENDIF
             !
+            DEALLOCATE( ze3t_f )
          ENDIF
          !
          IF( ln_dynspg_ts .AND. ln_bt_fw ) THEN
-            ! Revert "before" velocities to time split estimate
+            ! Revert filtered "now" velocities to time split estimate
             ! Doing it here also means that asselin filter contribution is removed  
-            zue(:,:) = e3u_b(:,:,1) * ub(:,:,1) * umask(:,:,1)
-            zve(:,:) = e3v_b(:,:,1) * vb(:,:,1) * vmask(:,:,1)    
+            zue(:,:) = pe3u(:,:,1,Kmm) * puu(:,:,1,Kmm) * umask(:,:,1)
+            zve(:,:) = pe3v(:,:,1,Kmm) * pvv(:,:,1,Kmm) * vmask(:,:,1)    
             DO jk = 2, jpkm1
-               zue(:,:) = zue(:,:) + e3u_b(:,:,jk) * ub(:,:,jk) * umask(:,:,jk)
-               zve(:,:) = zve(:,:) + e3v_b(:,:,jk) * vb(:,:,jk) * vmask(:,:,jk)    
+               zue(:,:) = zue(:,:) + pe3u(:,:,jk,Kmm) * puu(:,:,jk,Kmm) * umask(:,:,jk)
+               zve(:,:) = zve(:,:) + pe3v(:,:,jk,Kmm) * pvv(:,:,jk,Kmm) * vmask(:,:,jk)    
             END DO
             DO jk = 1, jpkm1
-               ub(:,:,jk) = ub(:,:,jk) - (zue(:,:) * r1_hu_n(:,:) - un_b(:,:)) * umask(:,:,jk)
-               vb(:,:,jk) = vb(:,:,jk) - (zve(:,:) * r1_hv_n(:,:) - vn_b(:,:)) * vmask(:,:,jk)
+               puu(:,:,jk,Kmm) = puu(:,:,jk,Kmm) - (zue(:,:) * r1_hu(:,:,Kmm) - uu_b(:,:,Kmm)) * umask(:,:,jk)
+               pvv(:,:,jk,Kmm) = pvv(:,:,jk,Kmm) - (zve(:,:) * r1_hv(:,:,Kmm) - vv_b(:,:,Kmm)) * vmask(:,:,jk)
             END DO
          ENDIF
          !
-      ENDIF ! neuler =/0
+      ENDIF ! neuler /= 0
       !
       ! Set "now" and "before" barotropic velocities for next time step:
@@ -336,50 +304,49 @@
       ! integration
       !
-      !
       IF(.NOT.ln_linssh ) THEN
-         hu_b(:,:) = e3u_b(:,:,1) * umask(:,:,1)
-         hv_b(:,:) = e3v_b(:,:,1) * vmask(:,:,1)
+         hu(:,:,Kmm) = pe3u(:,:,1,Kmm ) * umask(:,:,1)
+         hv(:,:,Kmm) = pe3v(:,:,1,Kmm ) * vmask(:,:,1)
          DO jk = 2, jpkm1
-            hu_b(:,:) = hu_b(:,:) + e3u_b(:,:,jk) * umask(:,:,jk)
-            hv_b(:,:) = hv_b(:,:) + e3v_b(:,:,jk) * vmask(:,:,jk)
+            hu(:,:,Kmm) = hu(:,:,Kmm) + pe3u(:,:,jk,Kmm ) * umask(:,:,jk)
+            hv(:,:,Kmm) = hv(:,:,Kmm) + pe3v(:,:,jk,Kmm ) * vmask(:,:,jk)
          END DO
-         r1_hu_b(:,:) = ssumask(:,:) / ( hu_b(:,:) + 1._wp - ssumask(:,:) )
-         r1_hv_b(:,:) = ssvmask(:,:) / ( hv_b(:,:) + 1._wp - ssvmask(:,:) )
-      ENDIF
-      !
-      un_b(:,:) = e3u_a(:,:,1) * un(:,:,1) * umask(:,:,1)
-      ub_b(:,:) = e3u_b(:,:,1) * ub(:,:,1) * umask(:,:,1)
-      vn_b(:,:) = e3v_a(:,:,1) * vn(:,:,1) * vmask(:,:,1)
-      vb_b(:,:) = e3v_b(:,:,1) * vb(:,:,1) * vmask(:,:,1)
+         r1_hu(:,:,Kmm) = ssumask(:,:) / ( hu(:,:,Kmm) + 1._wp - ssumask(:,:) )
+         r1_hv(:,:,Kmm) = ssvmask(:,:) / ( hv(:,:,Kmm) + 1._wp - ssvmask(:,:) )
+      ENDIF
+      !
+      uu_b(:,:,Kaa) = pe3u(:,:,1,Kaa) * puu(:,:,1,Kaa) * umask(:,:,1)
+      uu_b(:,:,Kmm) = pe3u(:,:,1,Kmm) * puu(:,:,1,Kmm) * umask(:,:,1)
+      vv_b(:,:,Kaa) = pe3v(:,:,1,Kaa) * pvv(:,:,1,Kaa) * vmask(:,:,1)
+      vv_b(:,:,Kmm) = pe3v(:,:,1,Kmm) * pvv(:,:,1,Kmm) * vmask(:,:,1)
       DO jk = 2, jpkm1
-         un_b(:,:) = un_b(:,:) + e3u_a(:,:,jk) * un(:,:,jk) * umask(:,:,jk)
-         ub_b(:,:) = ub_b(:,:) + e3u_b(:,:,jk) * ub(:,:,jk) * umask(:,:,jk)
-         vn_b(:,:) = vn_b(:,:) + e3v_a(:,:,jk) * vn(:,:,jk) * vmask(:,:,jk)
-         vb_b(:,:) = vb_b(:,:) + e3v_b(:,:,jk) * vb(:,:,jk) * vmask(:,:,jk)
+         uu_b(:,:,Kaa) = uu_b(:,:,Kaa) + pe3u(:,:,jk,Kaa) * puu(:,:,jk,Kaa) * umask(:,:,jk)
+         uu_b(:,:,Kmm) = uu_b(:,:,Kmm) + pe3u(:,:,jk,Kmm) * puu(:,:,jk,Kmm) * umask(:,:,jk)
+         vv_b(:,:,Kaa) = vv_b(:,:,Kaa) + pe3v(:,:,jk,Kaa) * pvv(:,:,jk,Kaa) * vmask(:,:,jk)
+         vv_b(:,:,Kmm) = vv_b(:,:,Kmm) + pe3v(:,:,jk,Kmm) * pvv(:,:,jk,Kmm) * vmask(:,:,jk)
       END DO
-      un_b(:,:) = un_b(:,:) * r1_hu_a(:,:)
-      vn_b(:,:) = vn_b(:,:) * r1_hv_a(:,:)
-      ub_b(:,:) = ub_b(:,:) * r1_hu_b(:,:)
-      vb_b(:,:) = vb_b(:,:) * r1_hv_b(:,:)
+      uu_b(:,:,Kaa) = uu_b(:,:,Kaa) * r1_hu(:,:,Kaa)
+      vv_b(:,:,Kaa) = vv_b(:,:,Kaa) * r1_hv(:,:,Kaa)
+      uu_b(:,:,Kmm) = uu_b(:,:,Kmm) * r1_hu(:,:,Kmm)
+      vv_b(:,:,Kmm) = vv_b(:,:,Kmm) * r1_hv(:,:,Kmm)
       !
       IF( .NOT.ln_dynspg_ts ) THEN        ! output the barotropic currents
-         CALL iom_put(  "ubar", un_b(:,:) )
-         CALL iom_put(  "vbar", vn_b(:,:) )
+         CALL iom_put(  "ubar", uu_b(:,:,Kmm) )
+         CALL iom_put(  "vbar", vv_b(:,:,Kmm) )
       ENDIF
       IF( l_trddyn ) THEN                ! 3D output: asselin filter trends on momentum
-         zua(:,:,:) = ( ub(:,:,:) - zua(:,:,:) ) * z1_2dt
-         zva(:,:,:) = ( vb(:,:,:) - zva(:,:,:) ) * z1_2dt
+         zua(:,:,:) = ( puu(:,:,:,Kmm) - zua(:,:,:) ) * z1_2dt
+         zva(:,:,:) = ( pvv(:,:,:,Kmm) - zva(:,:,:) ) * z1_2dt
          CALL trd_dyn( zua, zva, jpdyn_atf, kt, Kmm )
       ENDIF
       !
-      IF(ln_ctl)   CALL prt_ctl( tab3d_1=un, clinfo1=' nxt  - Un: ', mask1=umask,   &
-         &                       tab3d_2=vn, clinfo2=' Vn: '       , mask2=vmask )
+      IF(ln_ctl)   CALL prt_ctl( tab3d_1=puu(:,:,:,Kaa), clinfo1=' nxt  - puu(:,:,:,Kaa): ', mask1=umask,   &
+         &                       tab3d_2=pvv(:,:,:,Kaa), clinfo2=' pvv(:,:,:,Kaa): '       , mask2=vmask )
       ! 
       IF( ln_dynspg_ts )   DEALLOCATE( zue, zve )
       IF( l_trddyn     )   DEALLOCATE( zua, zva )
-      IF( ln_timing    )   CALL timing_stop('dyn_nxt')
-      !
-   END SUBROUTINE dyn_nxt
+      IF( ln_timing    )   CALL timing_stop('dyn_atf')
+      !
+   END SUBROUTINE dyn_atf
 
    !!=========================================================================
-END MODULE dynnxt
+END MODULE dynatf
