Index: branches/2014/dev_r4650_UKMO11_restart_functionality/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_flt.F90
===================================================================
--- branches/2014/dev_r4650_UKMO11_restart_functionality/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_flt.F90	(revision 4763)
+++ branches/2014/dev_r4650_UKMO11_restart_functionality/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_flt.F90	(revision 5208)
@@ -13,4 +13,5 @@
    !!             -   !  2006-08  (J.Chanut, A.Sellar) Calls to BDY routines. 
    !!            3.2  !  2009-03  (G. Madec, M. Leclair, R. Benshila) introduce sshwzv module
+   !!            3.7  !  2014-04  (F. Roquet, G. Madec)  add some trends diag
    !!----------------------------------------------------------------------
 #if defined key_dynspg_flt   ||   defined key_esopa  
@@ -36,4 +37,7 @@
    USE bdyvol          ! ocean open boundary condition (bdy_vol routine)
    USE cla             ! cross land advection
+   USE trd_oce         ! trends: ocean variables
+   USE trddyn          ! trend manager: dynamics
+   !
    USE in_out_manager  ! I/O manager
    USE lib_mpp         ! distributed memory computing library
@@ -43,8 +47,8 @@
    USE iom
    USE lib_fortran
+   USE timing          ! Timing
 #if defined key_agrif
    USE agrif_opa_interp
 #endif
-   USE timing          ! Timing
 
    IMPLICIT NONE
@@ -99,15 +103,16 @@
       !! ** Action : - Update (ua,va) with the surf. pressure gradient trend
       !!
-      !! References : Roullet and Madec 1999, JGR.
+      !! References : Roullet and Madec, JGR, 2000.
       !!---------------------------------------------------------------------
       INTEGER, INTENT(in   ) ::   kt       ! ocean time-step index
       INTEGER, INTENT(  out) ::   kindic   ! solver convergence flag (<0 if not converge)
-      !!                                   
+      !
       INTEGER  ::   ji, jj, jk   ! dummy loop indices
       REAL(wp) ::   z2dt, z2dtg, zgcb, zbtd, ztdgu, ztdgv   ! local scalars
+      REAL(wp), POINTER, DIMENSION(:,:,:) ::  ztrdu, ztrdv
+      REAL(wp), POINTER, DIMENSION(:,:)   ::  zpw
       !!----------------------------------------------------------------------
       !
       IF( nn_timing == 1 )  CALL timing_start('dyn_spg_flt')
-      !
       !
       IF( kt == nit000 ) THEN
@@ -179,4 +184,17 @@
          END DO
          !
+         IF( l_trddyn )   THEN                      ! temporary save of spg trends
+            CALL wrk_alloc( jpi, jpj, jpk, ztrdu, ztrdv )
+            DO jk = 1, jpkm1              ! unweighted time stepping 
+               DO jj = 2, jpjm1
+                  DO ji = fs_2, fs_jpim1   ! vector opt.
+                     ztrdu(ji,jj,jk) = spgu(ji,jj) * umask(ji,jj,jk)
+                     ztrdv(ji,jj,jk) = spgv(ji,jj) * vmask(ji,jj,jk)
+                  END DO
+               END DO
+            END DO
+            CALL trd_dyn( ztrdu, ztrdv, jpdyn_spgexp, kt )
+         ENDIF
+         !
       ENDIF
 
@@ -194,31 +212,18 @@
       DO jj = 2, jpjm1
          DO ji = fs_2, fs_jpim1   ! vector opt.
-            spgu(ji,jj) = 0._wp
-            spgv(ji,jj) = 0._wp
-         END DO
-      END DO
-
-      ! vertical sum
-!CDIR NOLOOPCHG
-      IF( lk_vopt_loop ) THEN          ! vector opt., forced unroll
-         DO jk = 1, jpkm1
-            DO ji = 1, jpij
-               spgu(ji,1) = spgu(ji,1) + fse3u_a(ji,1,jk) * ua(ji,1,jk)
-               spgv(ji,1) = spgv(ji,1) + fse3v_a(ji,1,jk) * va(ji,1,jk)
-            END DO
-         END DO
-      ELSE                        ! No  vector opt.
-         DO jk = 1, jpkm1
-            DO jj = 2, jpjm1
-               DO ji = 2, jpim1
-                  spgu(ji,jj) = spgu(ji,jj) + fse3u_a(ji,jj,jk) * ua(ji,jj,jk)
-                  spgv(ji,jj) = spgv(ji,jj) + fse3v_a(ji,jj,jk) * va(ji,jj,jk)
-               END DO
-            END DO
-         END DO
-      ENDIF
-
-      ! transport: multiplied by the horizontal scale factor
-      DO jj = 2, jpjm1
+            spgu(ji,jj) = fse3u_a(ji,jj,1) * ua(ji,jj,1)
+            spgv(ji,jj) = fse3v_a(ji,jj,1) * va(ji,jj,1)
+         END DO
+      END DO
+      DO jk = 2, jpkm1                     ! vertical sum
+         DO jj = 2, jpjm1
+            DO ji = fs_2, fs_jpim1   ! vector opt.
+               spgu(ji,jj) = spgu(ji,jj) + fse3u_a(ji,jj,jk) * ua(ji,jj,jk)
+               spgv(ji,jj) = spgv(ji,jj) + fse3v_a(ji,jj,jk) * va(ji,jj,jk)
+            END DO
+         END DO
+      END DO
+
+      DO jj = 2, jpjm1                     ! transport: multiplied by the horizontal scale factor
          DO ji = fs_2, fs_jpim1   ! vector opt.
             spgu(ji,jj) = spgu(ji,jj) * e2u(ji,jj)
@@ -322,4 +327,26 @@
       ENDIF
 #endif      
+
+      IF( l_trddyn )   THEN                     
+         ztrdu(:,:,:) = ua(:,:,:)                 ! save the after velocity before the filtered SPG
+         ztrdv(:,:,:) = va(:,:,:)
+         !
+         CALL wrk_alloc( jpi, jpj, zpw )
+         !
+         zpw(:,:) = - z2dt * gcx(:,:)
+         CALL iom_put( "ssh_flt" , zpw )          ! output equivalent ssh modification due to implicit filter
+         !
+         !                                        ! save surface pressure flux: -pw at z=0
+         zpw(:,:) = - rau0 * grav * sshn(:,:) * wn(:,:,1) * tmask(:,:,1)
+         CALL iom_put( "pw0_exp" , zpw )
+         zpw(:,:) = wn(:,:,1)
+         CALL iom_put( "w0" , zpw )
+         zpw(:,:) =  rau0 * z2dtg * gcx(:,:) * wn(:,:,1) * tmask(:,:,1)
+         CALL iom_put( "pw0_flt" , zpw )
+         !
+         CALL wrk_dealloc( jpi, jpj, zpw ) 
+         !                                   
+      ENDIF
+      
       ! Add the trends multiplied by z2dt to the after velocity
       ! -------------------------------------------------------
@@ -336,10 +363,15 @@
       END DO
 
-      ! write filtered free surface arrays in restart file
-      ! --------------------------------------------------
-      IF( lrst_oce ) CALL flt_rst( kt, 'WRITE' )
-      !
-      !
-      IF( nn_timing == 1 )  CALL timing_stop('dyn_spg_flt')
+      IF( l_trddyn )   THEN                      ! save the explicit SPG trends for further diagnostics
+         ztrdu(:,:,:) = ( ua(:,:,:) - ztrdu(:,:,:) ) / z2dt
+         ztrdv(:,:,:) = ( va(:,:,:) - ztrdv(:,:,:) ) / z2dt
+         CALL trd_dyn( ztrdu, ztrdv, jpdyn_spgflt, kt )
+         !
+         CALL wrk_dealloc( jpi, jpj, jpk, ztrdu, ztrdv ) 
+      ENDIF
+
+      IF( lrst_oce )   CALL flt_rst( kt, 'WRITE' )      ! write filtered free surface arrays in restart file
+      !
+      IF( nn_timing == 1 )   CALL timing_stop('dyn_spg_flt')
       !
    END SUBROUTINE dyn_spg_flt
@@ -352,6 +384,6 @@
       !! ** Purpose : Read or write filtered free surface arrays in restart file
       !!----------------------------------------------------------------------
-      INTEGER         , INTENT(in) ::   kt         ! ocean time-step
-      CHARACTER(len=*), INTENT(in) ::   cdrw       ! "READ"/"WRITE" flag
+      INTEGER         , INTENT(in) ::   kt     ! ocean time-step
+      CHARACTER(len=*), INTENT(in) ::   cdrw   ! "READ"/"WRITE" flag
       !!----------------------------------------------------------------------
       !
