Index: /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/src/OCE/LBC/lib_mpp.F90
===================================================================
--- /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/src/OCE/LBC/lib_mpp.F90	(revision 9771)
+++ /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/src/OCE/LBC/lib_mpp.F90	(revision 9772)
@@ -160,7 +160,14 @@
 
    ! Type of send : standard, buffered, immediate
-   CHARACTER(len=1), PUBLIC ::   cn_mpi_send        !: type od mpi send/recieve (S=standard, B=bsend, I=isend)
-   LOGICAL         , PUBLIC ::   l_isend = .FALSE.  !: isend use indicator (T if cn_mpi_send='I')
-   INTEGER         , PUBLIC ::   nn_buffer          !: size of the buffer in case of mpi_bsend
+   CHARACTER(len=1), PUBLIC ::   cn_mpi_send            !: type od mpi send/recieve (S=standard, B=bsend, I=isend)
+   LOGICAL         , PUBLIC ::   l_isend = .FALSE.      !: isend use indicator (T if cn_mpi_send='I')
+   INTEGER         , PUBLIC ::   nn_buffer              !: size of the buffer in case of mpi_bsend
+   INTEGER         , PUBLIC ::   nn_comm_mod = 1         !: frequency of communication calls (1 = every time step)
+   LOGICAL         , PUBLIC ::   ln_comm_only = .FALSE.  !: replace step routine by realistic communications only
+  
+   INTEGER         , PUBLIC ::   ncom_stp = 0                 !: copy of time step # istp
+   INTEGER         , PUBLIC ::   icomm_sequence(500,2) = 0    !: size of communicated arrays (halos)
+   INTEGER         , PUBLIC ::   n_sequence = 0               !: # of communicated arrays
+ 
 
    REAL(wp), DIMENSION(:), ALLOCATABLE, SAVE ::   tampon   ! buffer in case of bsend
@@ -168,4 +175,6 @@
    LOGICAL, PUBLIC ::   ln_nnogather                !: namelist control of northfold comms
    LOGICAL, PUBLIC ::   l_north_nogather = .FALSE.  !: internal control of northfold comms
+
+   LOGICAL ::           l_print_comm_report = .TRUE. 
 
    !!----------------------------------------------------------------------
@@ -193,5 +202,5 @@
       LOGICAL ::   mpi_was_called
       !
-      NAMELIST/nammpp/ cn_mpi_send, nn_buffer, jpni, jpnj, jpnij, ln_nnogather
+      NAMELIST/nammpp/ cn_mpi_send, nn_buffer, jpni, jpnj, jpnij, ln_nnogather, nn_comm_mod, ln_comm_only
       !!----------------------------------------------------------------------
       !
@@ -1602,4 +1611,27 @@
       !
    END SUBROUTINE mpp_lnk_2d_icb
+
+   SUBROUTINE tic_tac (l_tic)
+
+    LOGICAL, INTENT(IN) :: l_tic
+    LOGICAL, SAVE :: l_write_wt = .TRUE.
+    REAL(wp),SAVE  ::   tic_wt=0., tac_wt=0., tic_ct=0., tac_ct=0.
+
+    ! start count
+    IF ( l_tic ) THEN
+       IF ( ncom_stp > nit000 ) tic_wt = MPI_Wtime()
+       IF ( tic_ct > 0.0_wp ) tac_ct = tac_ct + MPI_Wtime() - tic_ct
+    ! stop count and sum up
+    ELSE
+       IF ( ncom_stp > nit000 ) tac_wt = tac_wt + MPI_Wtime() - tic_wt
+       IF ( ( ncom_stp == ( nitend - 1 ) ) .AND. l_write_wt ) then
+          WRITE(6,'(A20,F11.6,A15,I8)') 'Computing time : ',tac_ct,' on MPI rank : ', narea
+          WRITE(6,'(A20,F11.6,A15,I8)') 'Waiting time : ',tac_wt,' on MPI rank : ', narea
+          l_write_wt = .FALSE.
+       END IF
+       IF ( ncom_stp > nit000 ) tic_ct = MPI_Wtime()
+    ENDIF
+   END SUBROUTINE tic_tac
+
    
 #else
Index: /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/src/OCE/LBC/mpp_lnk_generic.h90
===================================================================
--- /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/src/OCE/LBC/mpp_lnk_generic.h90	(revision 9771)
+++ /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/src/OCE/LBC/mpp_lnk_generic.h90	(revision 9772)
@@ -150,21 +150,49 @@
       imigr = nn_hls * jpj * ipk * ipl * ipf
       !
-      SELECT CASE ( nbondi )
-      CASE ( -1 )
-         CALL mppsend( 2, zt3we(1,1,1,1,1,1), imigr, noea, ml_req1 )
-         CALL mpprecv( 1, zt3ew(1,1,1,1,1,2), imigr, noea )
-         IF(l_isend)   CALL mpi_wait(ml_req1, ml_stat, ml_err)
-      CASE ( 0 )
-         CALL mppsend( 1, zt3ew(1,1,1,1,1,1), imigr, nowe, ml_req1 )
-         CALL mppsend( 2, zt3we(1,1,1,1,1,1), imigr, noea, ml_req2 )
-         CALL mpprecv( 1, zt3ew(1,1,1,1,1,2), imigr, noea )
-         CALL mpprecv( 2, zt3we(1,1,1,1,1,2), imigr, nowe )
-         IF(l_isend)   CALL mpi_wait(ml_req1, ml_stat, ml_err)
-         IF(l_isend)   CALL mpi_wait(ml_req2, ml_stat, ml_err)
-      CASE ( 1 )
-         CALL mppsend( 1, zt3ew(1,1,1,1,1,1), imigr, nowe, ml_req1 )
-         CALL mpprecv( 2, zt3we(1,1,1,1,1,2), imigr, nowe )
-         IF(l_isend)   CALL mpi_wait(ml_req1, ml_stat, ml_err )
-      END SELECT
+      IF ( ncom_stp == nit000 ) then
+         n_sequence = n_sequence + 1
+         icomm_sequence(n_sequence,1) = ipk
+         icomm_sequence(n_sequence,2) = ipf
+         ! write(6,'(A,6I4)') 'size comm ', nn_hls, jpi, jpj, ipk, ipl, ipf
+      ELSE IF ( mpprank == 0 .AND. ncom_stp == (nit000+1) .AND. l_print_comm_report ) THEN
+         write(6,*) 'Communication pattern report : '
+         write(6,*) ' '
+         write(6,'(A,I3)') ' Exchanged halos : ', n_sequence
+         jj = 0; jk = 0; jf = 0; jh = 0
+         DO ji = 1, n_sequence
+           IF ( icomm_sequence(ji,1) .gt. 1 ) jk = jk + 1
+           IF ( icomm_sequence(ji,2) .gt. 1 ) jf = jf + 1
+           IF ( icomm_sequence(ji,1) .gt. 1 .AND. icomm_sequence(ji,2) .gt. 1 ) jj = jj + 1
+           jh = MAX (jh, icomm_sequence(ji,1)*icomm_sequence(ji,2))
+         END DO
+         write(6,'(A,I3)') ' 3D Exchanged halos : ', jk
+         write(6,'(A,I3)') ' Multi arrays exchanged halos : ', jf
+         write(6,'(A,I3)') '   from which 3D : ', jj
+         write(6,'(A,I10)') ' array max size : ', jh*jpi*jpj
+         l_print_comm_report = .FALSE.
+      END IF
+      !
+      IF (ncom_stp <= ( nit000 + 1 ) .or. mod(ncom_stp,nn_comm_mod) == 0 ) THEN
+         !
+         CALL tic_tac(.TRUE.)
+         !
+         SELECT CASE ( nbondi )
+         CASE ( -1 )
+            CALL mppsend( 2, zt3we(1,1,1,1,1,1), imigr, noea, ml_req1 )
+            CALL mpprecv( 1, zt3ew(1,1,1,1,1,2), imigr, noea )
+            IF(l_isend)   CALL mpi_wait(ml_req1, ml_stat, ml_err)
+         CASE ( 0 )
+            CALL mppsend( 1, zt3ew(1,1,1,1,1,1), imigr, nowe, ml_req1 )
+            CALL mppsend( 2, zt3we(1,1,1,1,1,1), imigr, noea, ml_req2 )
+            CALL mpprecv( 1, zt3ew(1,1,1,1,1,2), imigr, noea )
+            CALL mpprecv( 2, zt3we(1,1,1,1,1,2), imigr, nowe )
+            IF(l_isend)   CALL mpi_wait(ml_req1, ml_stat, ml_err)
+            IF(l_isend)   CALL mpi_wait(ml_req2, ml_stat, ml_err)
+         CASE ( 1 )
+            CALL mppsend( 1, zt3ew(1,1,1,1,1,1), imigr, nowe, ml_req1 )
+            CALL mpprecv( 2, zt3we(1,1,1,1,1,2), imigr, nowe )
+            IF(l_isend)   CALL mpi_wait(ml_req1, ml_stat, ml_err )
+         END SELECT
+      END IF
       !
       !                           ! Write Dirichlet lateral conditions
@@ -226,21 +254,27 @@
       imigr = nn_hls * jpi * ipk * ipl * ipf
       !
-      SELECT CASE ( nbondj )
-      CASE ( -1 )
-         CALL mppsend( 4, zt3sn(1,1,1,1,1,1), imigr, nono, ml_req1 )
-         CALL mpprecv( 3, zt3ns(1,1,1,1,1,2), imigr, nono )
-         IF(l_isend) CALL mpi_wait(ml_req1, ml_stat, ml_err )
-      CASE ( 0 )
-         CALL mppsend( 3, zt3ns(1,1,1,1,1,1), imigr, noso, ml_req1 )
-         CALL mppsend( 4, zt3sn(1,1,1,1,1,1), imigr, nono, ml_req2 )
-         CALL mpprecv( 3, zt3ns(1,1,1,1,1,2), imigr, nono )
-         CALL mpprecv( 4, zt3sn(1,1,1,1,1,2), imigr, noso )
-         IF(l_isend)   CALL mpi_wait(ml_req1, ml_stat, ml_err )
-         IF(l_isend)   CALL mpi_wait(ml_req2, ml_stat, ml_err )
-      CASE ( 1 )
-         CALL mppsend( 3, zt3ns(1,1,1,1,1,1), imigr, noso, ml_req1 )
-         CALL mpprecv( 4, zt3sn(1,1,1,1,1,2), imigr, noso )
-         IF(l_isend)   CALL mpi_wait(ml_req1, ml_stat, ml_err )
-      END SELECT
+      IF (ncom_stp <= ( nit000 + 1 ) .or. mod(ncom_stp,nn_comm_mod) == 0 ) THEN
+ 
+         SELECT CASE ( nbondj )
+         CASE ( -1 )
+            CALL mppsend( 4, zt3sn(1,1,1,1,1,1), imigr, nono, ml_req1 )
+            CALL mpprecv( 3, zt3ns(1,1,1,1,1,2), imigr, nono )
+            IF(l_isend) CALL mpi_wait(ml_req1, ml_stat, ml_err )
+         CASE ( 0 )
+            CALL mppsend( 3, zt3ns(1,1,1,1,1,1), imigr, noso, ml_req1 )
+            CALL mppsend( 4, zt3sn(1,1,1,1,1,1), imigr, nono, ml_req2 )
+            CALL mpprecv( 3, zt3ns(1,1,1,1,1,2), imigr, nono )
+            CALL mpprecv( 4, zt3sn(1,1,1,1,1,2), imigr, noso )
+            IF(l_isend)   CALL mpi_wait(ml_req1, ml_stat, ml_err )
+            IF(l_isend)   CALL mpi_wait(ml_req2, ml_stat, ml_err )
+         CASE ( 1 )
+            CALL mppsend( 3, zt3ns(1,1,1,1,1,1), imigr, noso, ml_req1 )
+            CALL mpprecv( 4, zt3sn(1,1,1,1,1,2), imigr, noso )
+            IF(l_isend)   CALL mpi_wait(ml_req1, ml_stat, ml_err )
+         END SELECT
+         ! imbalance measurement
+         CALL tic_tac(.FALSE.)
+         !
+      END IF
       !
       !                           ! Write Dirichlet lateral conditions
Index: /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/src/OCE/LBC/mppini.F90
===================================================================
--- /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/src/OCE/LBC/mppini.F90	(revision 9771)
+++ /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/src/OCE/LBC/mppini.F90	(revision 9772)
@@ -151,5 +151,5 @@
       INTEGER, ALLOCATABLE, DIMENSION(:,:) ::   ilei, ildi, iono, ioea         !  -     -
       INTEGER, ALLOCATABLE, DIMENSION(:,:) ::   ilej, ildj, ioso, iowe         !  -     -
-      INTEGER, DIMENSION(jpiglo,jpjglo) ::   imask   ! 2D global domain workspace
+      INTEGER, ALLOCATABLE, DIMENSION(:,:) ::   imask   ! 2D global domain
       !!----------------------------------------------------------------------
 
@@ -196,9 +196,10 @@
 
       !
-      IF ( jpni * jpnj == jpnij ) THEN    ! regular domain lay out over processors
-         imask(:,:) = 1               
-      ELSEIF ( jpni*jpnj > jpnij ) THEN   ! remove land-only processor (i.e. where imask(:,:)=0)
+      ALLOCATE ( imask(jpiglo,jpjglo), stat=ierr )
+      !
+      IF ( jpni*jpnj > jpnij ) THEN   ! remove land-only processor (i.e. where imask(:,:)=0)
+         IF( ierr /= 0 )   CALL ctl_stop( 'STOP', 'mpp_init: unable to allocate global ocean arrays' )
          CALL mpp_init_mask( imask )   
-      ELSE                                ! error
+      ELSEIF ( jpnij > jpni*jpnj ) THEN   ! error
          CALL ctl_stop( 'mpp_init: jpnij > jpni x jpnj. Check namelist setting!' )
       ENDIF
@@ -335,10 +336,14 @@
          !
          ! Check wet points over the entire domain to preserve the MPI communication stencil
-         isurf = 0
-         DO jj = 1, ilj
-            DO  ji = 1, ili
-               IF( imask(ji+iimppt(ii,ij)-1, jj+ijmppt(ii,ij)-1) == 1)   isurf = isurf+1
-            END DO
-         END DO
+        IF ( jpni * jpnj == jpnij ) THEN    ! regular domain lay out over processors
+           isurf = ili * ilj
+         ELSE
+           isurf = 0
+           DO jj = 1, ilj
+              DO  ji = 1, ili
+                 IF( imask(ji+iimppt(ii,ij)-1, jj+ijmppt(ii,ij)-1) == 1)   isurf = isurf+1
+              END DO
+           END DO
+         ENDIF
          !
          IF( isurf /= 0 ) THEN
@@ -349,4 +354,5 @@
          ENDIF
       END DO
+      DEALLOCATE ( imask )
       !
       nfipproc(:,:) = ipproc(:,:)
Index: /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/src/OCE/nemogcm.F90
===================================================================
--- /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/src/OCE/nemogcm.F90	(revision 9771)
+++ /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/src/OCE/nemogcm.F90	(revision 9772)
@@ -99,4 +99,8 @@
    CHARACTER(lc) ::   cform_aaa="( /, 'AAAAAAAA', / ) "     ! flag for output listing
 
+#if defined key_mpp_mpi
+   INCLUDE 'mpif.h'
+#endif
+
    !!----------------------------------------------------------------------
    !! NEMO/OCE 4.0 , NEMO Consortium (2018)
@@ -121,4 +125,7 @@
       !!----------------------------------------------------------------------
       INTEGER ::   istp   ! time step index
+      INTEGER ::   i, ik, id, iend, iseq   ! time step index
+      REAL(wp) ::   tic  
+
       !!----------------------------------------------------------------------
       !
@@ -178,7 +185,49 @@
          !
          DO WHILE( istp <= nitend .AND. nstop == 0 )
-            CALL stp        ( istp ) 
+#if   defined key_mpp_mpi
+            ncom_stp = istp
+#endif
+            IF ( istp == ( nit000 + 1 ) ) tic = MPI_Wtime()
+            IF ( ln_comm_only .AND. istp >= ( nit000  + 1 )) THEN
+               DO i = 1, n_sequence
+                 IF ( icomm_sequence(i,1) == 1 ) THEN
+                    SELECT CASE ( icomm_sequence(i,2) )
+                      CASE (1)
+                      CALL lbc_lnk_multi( un(:,:,1)  , 'U', -1. )
+                      CASE (2)
+                      CALL lbc_lnk_multi( un(:,:,1)  , 'U', -1., vn(:,:,1)  , 'V', -1.)
+                      CASE (3)
+                      CALL lbc_lnk_multi( un(:,:,1)  , 'U', -1., vn(:,:,1)  , 'V', -1., ua(:,:,1), 'U', -1.)
+                      CASE (4)
+                      CALL lbc_lnk_multi( un(:,:,1)  , 'U', -1., vn(:,:,1)  , 'V', -1., ua(:,:,1), 'U', -1., va(:,:,1)  , 'V', -1.)
+                      CASE (5)
+                      CALL lbc_lnk_multi( un(:,:,1)  , 'U', -1., vn(:,:,1)  , 'V', -1., ua(:,:,1), 'U', -1., va(:,:,1)  , 'V', -1., ub(:,:,1)  , 'U', -1.)
+                      CASE (6)
+                      CALL lbc_lnk_multi( un(:,:,1)  , 'U', -1., vn(:,:,1)  , 'V', -1., ua(:,:,1), 'U', -1., va(:,:,1)  , 'V', -1., ub(:,:,1)  , 'U', -1., vb(:,:,1)  , 'V', -1. )
+                    END SELECT
+                 ELSE
+                    SELECT CASE ( icomm_sequence(i,2) )
+                      CASE (1)
+                      CALL lbc_lnk_multi( un  , 'U', -1. )
+                      CASE (2)
+                      CALL lbc_lnk_multi( un  , 'U', -1., vn  , 'V', -1.)
+                      CASE (3)
+                      CALL lbc_lnk_multi( un  , 'U', -1., vn  , 'V', -1., ua, 'U', -1.)
+                      CASE (4)
+                      CALL lbc_lnk_multi( un  , 'U', -1., vn  , 'V', -1., ua, 'U', -1., va  , 'V', -1.)
+                      CASE (5)
+                      CALL lbc_lnk_multi( un  , 'U', -1., vn  , 'V', -1., ua, 'U', -1., va  , 'V', -1., ub  , 'U', -1.)
+                      CASE (6)
+                      CALL lbc_lnk_multi( un  , 'U', -1., vn  , 'V', -1., ua, 'U', -1., va  , 'V', -1., ub  , 'U', -1., vb  , 'V', -1. )
+                    END SELECT
+                 ENDIF
+               ENDDO
+            ELSE
+              CALL stp        ( istp ) 
+            ENDIF
             istp = istp + 1
+            IF ( istp == ( nitend - 1) ) tic = MPI_Wtime() - tic
          END DO
+         WRITE(6,'(A20,F11.6,A15,I8)') 'timeloop duration : ',tic,' on MPI rank : ', narea
          !
       ELSE                                            !==  diurnal SST time-steeping only  ==!
@@ -596,5 +645,6 @@
       !
       ierr =        oce_alloc    ()    ! ocean 
-      ierr = ierr + dia_wri_alloc()
+      !EM useless alloc for BENCH
+      !EM ierr = ierr + dia_wri_alloc()
       ierr = ierr + dom_oce_alloc()    ! ocean domain
       ierr = ierr + zdf_oce_alloc()    ! ocean vertical physics
Index: /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/tests/BENCH/EXPREF/namelist_cfg_orca025_like
===================================================================
--- /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/tests/BENCH/EXPREF/namelist_cfg_orca025_like	(revision 9771)
+++ /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/tests/BENCH/EXPREF/namelist_cfg_orca025_like	(revision 9772)
@@ -26,4 +26,6 @@
    jpni        =   3       !  jpni   number of processors following i (set automatically if < 1)
    jpnj        =   3       !  jpnj   number of processors following j (set automatically if < 1)
+   nn_comm_mod  =   1      !  step routine call frequency
+   ln_comm_only = .false.  !  replace step routine by realistic communications only
 /
 !-----------------------------------------------------------------------
Index: /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/tests/BENCH/EXPREF/namelist_cfg_orca12_like
===================================================================
--- /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/tests/BENCH/EXPREF/namelist_cfg_orca12_like	(revision 9771)
+++ /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/tests/BENCH/EXPREF/namelist_cfg_orca12_like	(revision 9772)
@@ -26,4 +26,6 @@
    jpni        =   3       !  jpni   number of processors following i (set automatically if < 1)
    jpnj        =   3       !  jpnj   number of processors following j (set automatically if < 1)
+   nn_comm_mod  =   1      !  step routine call frequency
+   ln_comm_only = .false.  !  replace step routine by realistic communications only
 /
 !-----------------------------------------------------------------------
Index: /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/tests/BENCH/EXPREF/namelist_cfg_orca1_like
===================================================================
--- /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/tests/BENCH/EXPREF/namelist_cfg_orca1_like	(revision 9771)
+++ /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/tests/BENCH/EXPREF/namelist_cfg_orca1_like	(revision 9772)
@@ -26,4 +26,6 @@
    jpni        =   3       !  jpni   number of processors following i (set automatically if < 1)
    jpnj        =   3       !  jpnj   number of processors following j (set automatically if < 1)
+   nn_comm_mod  =   1      !  step routine call frequency
+   ln_comm_only = .false.  !  replace step routine by realistic communications only
 /
 !-----------------------------------------------------------------------
Index: /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/tests/BENCH/MY_SRC/restart.F90
===================================================================
--- /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/tests/BENCH/MY_SRC/restart.F90	(revision 9772)
+++ /NEMO/branches/2018/dev_r9759_HPC09_ESIWACE/tests/BENCH/MY_SRC/restart.F90	(revision 9772)
@@ -0,0 +1,316 @@
+MODULE restart
+   !!======================================================================
+   !!                     ***  MODULE  restart  ***
+   !! Ocean restart :  write the ocean restart file
+   !!======================================================================
+   !! History :  OPA  !  1999-11  (M. Imbard)  Original code
+   !!   NEMO     1.0  !  2002-08  (G. Madec)  F90: Free form
+   !!            2.0  !  2006-07  (S. Masson)  use IOM for restart
+   !!            3.3  !  2010-04  (M. Leclair, G. Madec)  modified LF-RA
+   !!            - -  !  2010-10  (C. Ethe, G. Madec) TRC-TRA merge (T-S in 4D)
+   !!            3.7  !  2014-01  (G. Madec) suppression of curl and hdiv from the restart
+   !!             -   !  2014-12  (G. Madec) remove KPP scheme
+   !!----------------------------------------------------------------------
+
+   !!----------------------------------------------------------------------
+   !!   rst_opn    : open the ocean restart file
+   !!   rst_write  : write the ocean restart file
+   !!   rst_read   : read the ocean restart file
+   !!----------------------------------------------------------------------
+   USE oce             ! ocean dynamics and tracers 
+   USE dom_oce         ! ocean space and time domain
+   USE sbc_ice         ! only lk_si3 
+   USE phycst          ! physical constants
+   USE eosbn2          ! equation of state            (eos bn2 routine)
+   USE trdmxl_oce      ! ocean active mixed layer tracers trends variables
+   !
+   USE in_out_manager  ! I/O manager
+   USE iom             ! I/O module
+   USE diurnal_bulk
+
+   IMPLICIT NONE
+   PRIVATE
+
+   PUBLIC   rst_opn         ! routine called by step module
+   PUBLIC   rst_write       ! routine called by step module
+   PUBLIC   rst_read        ! routine called by istate module
+   PUBLIC   rst_read_open   ! routine called in rst_read and (possibly) in dom_vvl_init
+
+   !! * Substitutions
+#  include "vectopt_loop_substitute.h90"
+   !!----------------------------------------------------------------------
+   !! NEMO/OCE 4.0 , NEMO Consortium (2018)
+   !! $Id: restart.F90 9654 2018-05-28 09:08:53Z clem $
+   !! Software governed by the CeCILL licence     (./LICENSE)
+   !!----------------------------------------------------------------------
+CONTAINS
+
+   SUBROUTINE rst_opn( kt )
+      !!---------------------------------------------------------------------
+      !!                   ***  ROUTINE rst_opn  ***
+      !!                     
+      !! ** Purpose : + initialization (should be read in the namelist) of nitrst 
+      !!              + open the restart when we are one time step before nitrst
+      !!                   - restart header is defined when kt = nitrst-1
+      !!                   - restart data  are written when kt = nitrst
+      !!              + define lrst_oce to .TRUE. when we need to define or write the restart
+      !!----------------------------------------------------------------------
+      INTEGER, INTENT(in) ::   kt     ! ocean time-step
+      !!
+      CHARACTER(LEN=20)   ::   clkt     ! ocean time-step deine as a character
+      CHARACTER(LEN=50)   ::   clname   ! ocean output restart file name
+      CHARACTER(lc)       ::   clpath   ! full path to ocean output restart file
+      CHARACTER(LEN=52)   ::   clpname   ! ocean output restart file name including prefix for AGRIF
+      CHARACTER(LEN=256)  ::   clinfo    ! info character
+      !!----------------------------------------------------------------------
+      !
+      IF( kt == nit000 ) THEN   ! default definitions
+         lrst_oce = .FALSE.   
+         IF( ln_rst_list ) THEN
+            nrst_lst = 1
+            nitrst = nstocklist( nrst_lst )
+         ELSE
+            nitrst = nitend
+         ENDIF
+      ENDIF
+
+      ! frequency-based restart dumping (nn_stock)
+      IF( .NOT. ln_rst_list .AND. MOD( kt - 1, nstock ) == 0 ) THEN   
+         ! we use kt - 1 and not kt - nit000 to keep the same periodicity from the beginning of the experiment
+         nitrst = kt + nstock - 1                  ! define the next value of nitrst for restart writing
+         IF( nitrst > nitend )   nitrst = nitend   ! make sure we write a restart at the end of the run
+      ENDIF
+      ! to get better performances with NetCDF format:
+      ! we open and define the ocean restart file one time step before writing the data (-> at nitrst - 1)
+      ! except if we write ocean restart files every time step or if an ocean restart file was writen at nitend - 1
+      IF( kt == nitrst - 1 .OR. nstock == 1 .OR. ( kt == nitend .AND. .NOT. lrst_oce ) ) THEN
+         IF( nitrst <= nitend .AND. nitrst > 0 ) THEN 
+            ! beware of the format used to write kt (default is i8.8, that should be large enough...)
+            IF( nitrst > 999999999 ) THEN   ;   WRITE(clkt, *       ) nitrst
+            ELSE                            ;   WRITE(clkt, '(i8.8)') nitrst
+            ENDIF
+            ! create the file
+            clname = TRIM(cexper)//"_"//TRIM(ADJUSTL(clkt))//"_"//TRIM(cn_ocerst_out)
+            clpath = TRIM(cn_ocerst_outdir)
+            IF( clpath(LEN_TRIM(clpath):) /= '/' ) clpath = TRIM(clpath) // '/'
+            IF(lwp) THEN
+               WRITE(numout,*)
+               IF(.NOT.lwxios) THEN
+                  SELECT CASE ( jprstlib )
+                  CASE DEFAULT         ;   WRITE(numout,*)                            &
+                      '             open ocean restart NetCDF file: ',TRIM(clpath)//TRIM(clname)
+                  END SELECT
+                  IF ( snc4set%luse )      WRITE(numout,*) '             opened for NetCDF4 chunking and compression'
+                  IF( kt == nitrst - 1 ) THEN   ;   WRITE(numout,*) '             kt = nitrst - 1 = ', kt
+                  ELSE                          ;   WRITE(numout,*) '             kt = '             , kt
+                  ENDIF
+               ENDIF
+            ENDIF
+            !
+            IF(.NOT.lwxios) THEN
+               !EM no call to restart opening
+               !EM CALL iom_open( TRIM(clpath)//TRIM(clname), numrow, ldwrt = .TRUE., kiolib = jprstlib )
+            ELSE
+#if defined key_iomput
+               cwxios_context = "rstw_"//TRIM(ADJUSTL(clkt))
+               IF( TRIM(Agrif_CFixed()) == '0' ) THEN
+                  clpname = clname
+               ELSE
+                  clpname = TRIM(Agrif_CFixed())//"_"//clname   
+               ENDIF
+               CALL iom_init( cwxios_context, TRIM(clpath)//TRIM(clpname))
+               CALL xios_update_calendar(nitrst)
+               CALL iom_swap(      cxios_context          )
+#else
+               clinfo = 'Can not use XIOS in rst_opn'
+               CALL ctl_stop(TRIM(clinfo))
+#endif
+            ENDIF
+               !EM restart writing
+            !EM lrst_oce = .TRUE.
+         ENDIF
+      ENDIF
+      !
+   END SUBROUTINE rst_opn
+
+
+   SUBROUTINE rst_write( kt )
+      !!---------------------------------------------------------------------
+      !!                   ***  ROUTINE rstwrite  ***
+      !!                     
+      !! ** Purpose :   Write restart fields in the format corresponding to jprstlib
+      !!
+      !! ** Method  :   Write in numrow when kt == nitrst in NetCDF
+      !!              file, save fields which are necessary for restart
+      !!----------------------------------------------------------------------
+      INTEGER, INTENT(in) ::   kt   ! ocean time-step
+      !!----------------------------------------------------------------------
+                     IF(lwxios) CALL iom_swap(      cwxios_context          )
+                     CALL iom_rstput( kt, nitrst, numrow, 'rdt'    , rdt       , ldxios = lwxios)   ! dynamics time step
+
+      IF ( .NOT. ln_diurnal_only ) THEN
+                     CALL iom_rstput( kt, nitrst, numrow, 'ub'     , ub, ldxios = lwxios        )     ! before fields
+                     CALL iom_rstput( kt, nitrst, numrow, 'vb'     , vb, ldxios = lwxios        )
+                     CALL iom_rstput( kt, nitrst, numrow, 'tb'     , tsb(:,:,:,jp_tem), ldxios = lwxios )
+                     CALL iom_rstput( kt, nitrst, numrow, 'sb'     , tsb(:,:,:,jp_sal), ldxios = lwxios )
+                     CALL iom_rstput( kt, nitrst, numrow, 'sshb'   , sshb, ldxios = lwxios      )
+                     !
+                     CALL iom_rstput( kt, nitrst, numrow, 'un'     , un, ldxios = lwxios        )     ! now fields
+                     CALL iom_rstput( kt, nitrst, numrow, 'vn'     , vn, ldxios = lwxios        )
+                     CALL iom_rstput( kt, nitrst, numrow, 'tn'     , tsn(:,:,:,jp_tem), ldxios = lwxios )
+                     CALL iom_rstput( kt, nitrst, numrow, 'sn'     , tsn(:,:,:,jp_sal), ldxios = lwxios )
+                     CALL iom_rstput( kt, nitrst, numrow, 'sshn'   , sshn, ldxios = lwxios      )
+                     CALL iom_rstput( kt, nitrst, numrow, 'rhop'   , rhop, ldxios = lwxios      )
+                  ! extra variable needed for the ice sheet coupling
+                  IF ( ln_iscpl ) THEN 
+                     CALL iom_rstput( kt, nitrst, numrow, 'tmask'  , tmask, ldxios = lwxios ) ! need to extrapolate T/S
+                     CALL iom_rstput( kt, nitrst, numrow, 'umask'  , umask, ldxios = lwxios ) ! need to correct barotropic velocity
+                     CALL iom_rstput( kt, nitrst, numrow, 'vmask'  , vmask, ldxios = lwxios ) ! need to correct barotropic velocity
+                     CALL iom_rstput( kt, nitrst, numrow, 'smask'  , ssmask, ldxios = lwxios) ! need to correct barotropic velocity
+                     CALL iom_rstput( kt, nitrst, numrow, 'e3t_n', e3t_n(:,:,:), ldxios = lwxios )   ! need to compute temperature correction
+                     CALL iom_rstput( kt, nitrst, numrow, 'e3u_n', e3u_n(:,:,:), ldxios = lwxios )   ! need to compute bt conservation
+                     CALL iom_rstput( kt, nitrst, numrow, 'e3v_n', e3v_n(:,:,:), ldxios = lwxios )   ! need to compute bt conservation
+                     CALL iom_rstput( kt, nitrst, numrow, 'gdepw_n', gdepw_n(:,:,:), ldxios = lwxios ) ! need to compute extrapolation if vvl
+                  END IF
+      ENDIF
+      
+      IF (ln_diurnal) CALL iom_rstput( kt, nitrst, numrow, 'Dsst', x_dsst, ldxios = lwxios )  
+      IF(lwxios) CALL iom_swap(      cxios_context          )
+      IF( kt == nitrst ) THEN
+         IF(.NOT.lwxios) THEN
+            CALL iom_close( numrow )     ! close the restart file (only at last time step)
+         ELSE
+            CALL iom_context_finalize(      cwxios_context          )
+         ENDIF
+!!gm         IF( .NOT. lk_trdmld )   lrst_oce = .FALSE.
+!!gm  not sure what to do here   ===>>>  ask to Sebastian
+         lrst_oce = .FALSE.
+            IF( ln_rst_list ) THEN
+               nrst_lst = MIN(nrst_lst + 1, SIZE(nstocklist,1))
+               nitrst = nstocklist( nrst_lst )
+            ENDIF
+      ENDIF
+      !
+   END SUBROUTINE rst_write
+
+
+   SUBROUTINE rst_read_open
+      !!---------------------------------------------------------------------- 
+      !!                   ***  ROUTINE rst_read_open  ***
+      !! 
+      !! ** Purpose :   Open read files for restart (format fixed by jprstlib )
+      !! 
+      !! ** Method  :   Use a non-zero, positive value of numror to assess whether or not
+      !!                the file has already been opened
+      !!----------------------------------------------------------------------
+      INTEGER        ::   jlibalt = jprstlib
+      LOGICAL        ::   llok
+      CHARACTER(lc)  ::   clpath   ! full path to ocean output restart file
+      !!----------------------------------------------------------------------
+      !
+      IF( numror <= 0 ) THEN
+         IF(lwp) THEN                                             ! Contol prints
+            WRITE(numout,*)
+            SELECT CASE ( jprstlib )
+            CASE ( jpnf90    )   ;   WRITE(numout,*) 'rst_read : read oce NetCDF restart file'
+            END SELECT
+            IF ( snc4set%luse )      WRITE(numout,*) 'rst_read : configured with NetCDF4 support'
+            WRITE(numout,*) '~~~~~~~~'
+         ENDIF
+         lxios_sini = .FALSE.
+         clpath = TRIM(cn_ocerst_indir)
+         IF( clpath(LEN_TRIM(clpath):) /= '/' ) clpath = TRIM(clpath) // '/'
+         CALL iom_open( TRIM(clpath)//cn_ocerst_in, numror, kiolib = jlibalt )
+! are we using XIOS to read the data? Part above will have to modified once XIOS
+! can handle checking if variable is in the restart file (there will be no need to open
+! restart)
+         IF(.NOT.lxios_set) lrxios = lrxios.AND.lxios_sini
+         IF( lrxios) THEN
+             crxios_context = 'nemo_rst'
+             IF( .NOT.lxios_set ) THEN
+                 IF(lwp) WRITE(numout,*) 'Enable restart reading by XIOS'
+                 CALL iom_init( crxios_context )
+                 lxios_set = .TRUE.
+             ENDIF
+         ENDIF
+         IF( TRIM(Agrif_CFixed()) /= '0' .AND. lrxios) THEN
+             CALL iom_init( crxios_context )
+             IF(lwp) WRITE(numout,*) 'Enable restart reading by XIOS for AGRIF'
+             lxios_set = .TRUE.
+         ENDIF 
+      ENDIF
+
+   END SUBROUTINE rst_read_open
+
+
+   SUBROUTINE rst_read
+      !!---------------------------------------------------------------------- 
+      !!                   ***  ROUTINE rst_read  ***
+      !! 
+      !! ** Purpose :   Read files for restart (format fixed by jprstlib )
+      !! 
+      !! ** Method  :   Read in restart.nc file fields which are necessary for restart
+      !!----------------------------------------------------------------------
+      REAL(wp) ::   zrdt
+      INTEGER  ::   jk
+      REAL(wp), DIMENSION(jpi, jpj, jpk) :: w3d
+      !!----------------------------------------------------------------------
+
+      CALL rst_read_open           ! open restart for reading (if not already opened)
+
+      ! Check dynamics and tracer time-step consistency and force Euler restart if changed
+      IF( iom_varid( numror, 'rdt', ldstop = .FALSE. ) > 0 )   THEN
+         CALL iom_get( numror, 'rdt', zrdt, ldxios = lrxios )
+         IF( zrdt /= rdt )   neuler = 0
+      ENDIF
+
+      ! Diurnal DSST 
+      IF( ln_diurnal ) CALL iom_get( numror, jpdom_autoglo, 'Dsst' , x_dsst, ldxios = lrxios ) 
+      IF ( ln_diurnal_only ) THEN 
+         IF(lwp) WRITE( numout, * ) &
+         &   "rst_read:- ln_diurnal_only set, setting rhop=rau0" 
+         rhop = rau0
+         CALL iom_get( numror, jpdom_autoglo, 'tn'     , w3d, ldxios = lrxios ) 
+         tsn(:,:,1,jp_tem) = w3d(:,:,1)
+         RETURN 
+      ENDIF  
+      
+      IF( iom_varid( numror, 'ub', ldstop = .FALSE. ) > 0 ) THEN
+         CALL iom_get( numror, jpdom_autoglo, 'ub'     , ub, ldxios = lrxios                )   ! before fields
+         CALL iom_get( numror, jpdom_autoglo, 'vb'     , vb, ldxios = lrxios                )
+         CALL iom_get( numror, jpdom_autoglo, 'tb'     , tsb(:,:,:,jp_tem), ldxios = lrxios )
+         CALL iom_get( numror, jpdom_autoglo, 'sb'     , tsb(:,:,:,jp_sal), ldxios = lrxios )
+         CALL iom_get( numror, jpdom_autoglo, 'sshb'   , sshb, ldxios = lrxios              )
+      ELSE
+         neuler = 0
+      ENDIF
+      !
+      CALL iom_get( numror, jpdom_autoglo, 'un'     , un, ldxios = lrxios )   ! now    fields
+      CALL iom_get( numror, jpdom_autoglo, 'vn'     , vn, ldxios = lrxios )
+      CALL iom_get( numror, jpdom_autoglo, 'tn'     , tsn(:,:,:,jp_tem), ldxios = lrxios )
+      CALL iom_get( numror, jpdom_autoglo, 'sn'     , tsn(:,:,:,jp_sal), ldxios = lrxios )
+      CALL iom_get( numror, jpdom_autoglo, 'sshn'   , sshn, ldxios = lrxios )
+      IF( iom_varid( numror, 'rhop', ldstop = .FALSE. ) > 0 ) THEN
+         CALL iom_get( numror, jpdom_autoglo, 'rhop'   , rhop, ldxios = lrxios )   ! now    potential density
+      ELSE
+         CALL eos( tsn, rhd, rhop, gdept_n(:,:,:) )   
+      ENDIF
+      !
+      IF( neuler == 0 ) THEN                                  ! Euler restart (neuler=0)
+         tsb  (:,:,:,:) = tsn  (:,:,:,:)                             ! all before fields set to now values
+         ub   (:,:,:)   = un   (:,:,:)
+         vb   (:,:,:)   = vn   (:,:,:)
+         sshb (:,:)     = sshn (:,:)
+         !
+         IF( .NOT.ln_linssh ) THEN
+            DO jk = 1, jpk
+               e3t_b(:,:,jk) = e3t_n(:,:,jk)
+            END DO
+         ENDIF
+         !
+      ENDIF
+      !
+   END SUBROUTINE rst_read
+
+   !!=====================================================================
+END MODULE restart
