Index: branches/2017/dev_r8600_xios_write/NEMOGCM/CONFIG/SHARED/namelist_ref
===================================================================
--- branches/2017/dev_r8600_xios_write/NEMOGCM/CONFIG/SHARED/namelist_ref	(revision 8622)
+++ branches/2017/dev_r8600_xios_write/NEMOGCM/CONFIG/SHARED/namelist_ref	(revision 8630)
@@ -53,4 +53,5 @@
    ln_clobber  = .true.    !  clobber (overwrite) an existing file
    nn_chunksz  =       0   !  chunksize (bytes) for NetCDF file (works only with iom_nf90 routines)
+   nn_wxios = 0      !  use XIOS to write restart file 0 - no, 1 - single file output, 2 - multiple file output
 /
 !
Index: branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DIA/diahsb.F90
===================================================================
--- branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DIA/diahsb.F90	(revision 8622)
+++ branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DIA/diahsb.F90	(revision 8630)
@@ -32,4 +32,5 @@
    USE timing          ! preformance summary
    USE wrk_nemo        ! work arrays
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -308,22 +309,23 @@
         IF(lwp) WRITE(numout,*) ' dia_hsb_rst at it= ', kt,' date= ', ndastp
         IF(lwp) WRITE(numout,*) '~~~~~~~'
-
-        CALL iom_rstput( kt, nitrst, numrow, 'frc_v'   , frc_v     )
-        CALL iom_rstput( kt, nitrst, numrow, 'frc_t'   , frc_t     )
-        CALL iom_rstput( kt, nitrst, numrow, 'frc_s'   , frc_s     )
+        IF( lwxios ) CALL iom_swap(      wxios_context          )
+        CALL iom_rstput( kt, nitrst, numrow, 'frc_v'   , frc_v, lxios = lwxios)
+        CALL iom_rstput( kt, nitrst, numrow, 'frc_t'   , frc_t, lxios = lwxios)
+        CALL iom_rstput( kt, nitrst, numrow, 'frc_s'   , frc_s, lxios = lwxios)
         IF( ln_linssh ) THEN
-           CALL iom_rstput( kt, nitrst, numrow, 'frc_wn_t', frc_wn_t )
-           CALL iom_rstput( kt, nitrst, numrow, 'frc_wn_s', frc_wn_s )
+           CALL iom_rstput( kt, nitrst, numrow, 'frc_wn_t', frc_wn_t, lxios = lwxios)
+           CALL iom_rstput( kt, nitrst, numrow, 'frc_wn_s', frc_wn_s, lxios = lwxios )
         ENDIF
-        CALL iom_rstput( kt, nitrst, numrow, 'surf_ini', surf_ini )      ! ice sheet coupling
-        CALL iom_rstput( kt, nitrst, numrow, 'ssh_ini', ssh_ini(:,:) )
-        CALL iom_rstput( kt, nitrst, numrow, 'e3t_ini', e3t_ini(:,:,:) )
-        CALL iom_rstput( kt, nitrst, numrow, 'hc_loc_ini', hc_loc_ini(:,:,:) )
-        CALL iom_rstput( kt, nitrst, numrow, 'sc_loc_ini', sc_loc_ini(:,:,:) )
+        CALL iom_rstput( kt, nitrst, numrow, 'surf_ini', surf_ini, lxios = lwxios )      ! ice sheet coupling
+        CALL iom_rstput( kt, nitrst, numrow, 'ssh_ini', ssh_ini(:,:), lxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'e3t_ini', e3t_ini(:,:,:), lxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'hc_loc_ini', hc_loc_ini(:,:,:), lxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'sc_loc_ini', sc_loc_ini(:,:,:), lxios = lwxios )
         IF( ln_linssh ) THEN
-           CALL iom_rstput( kt, nitrst, numrow, 'ssh_hc_loc_ini', ssh_hc_loc_ini(:,:) )
-           CALL iom_rstput( kt, nitrst, numrow, 'ssh_sc_loc_ini', ssh_sc_loc_ini(:,:) )
+           CALL iom_rstput( kt, nitrst, numrow, 'ssh_hc_loc_ini', ssh_hc_loc_ini(:,:), lxios = lwxios )
+           CALL iom_rstput( kt, nitrst, numrow, 'ssh_sc_loc_ini', ssh_sc_loc_ini(:,:), lxios = lwxios )
         ENDIF
         !
+        IF( lwxios ) CALL iom_swap(      cxios_context          )
      ENDIF
      !
Index: branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/daymod.F90
===================================================================
--- branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/daymod.F90	(revision 8622)
+++ branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/daymod.F90	(revision 8630)
@@ -33,4 +33,5 @@
    USE timing         ! Timing
    USE restart        ! restart
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -399,9 +400,11 @@
          ENDIF
          ! calendar control
-         CALL iom_rstput( kt, nitrst, numrow, 'kt'     , REAL( kt    , wp) )   ! time-step
-         CALL iom_rstput( kt, nitrst, numrow, 'ndastp' , REAL( ndastp, wp) )   ! date
-         CALL iom_rstput( kt, nitrst, numrow, 'adatrj' , adatrj            )   ! number of elapsed days since
+         IF( lwxios ) CALL iom_swap(      wxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'kt'     , REAL( kt    , wp), lxios = lwxios )   ! time-step
+         CALL iom_rstput( kt, nitrst, numrow, 'ndastp' , REAL( ndastp, wp), lxios = lwxios )   ! date
+         CALL iom_rstput( kt, nitrst, numrow, 'adatrj' , adatrj, lxios = lwxios            )   ! number of elapsed days since
          !                                                                     ! the begining of the run [s]
-	 CALL iom_rstput( kt, nitrst, numrow, 'ntime'  , REAL( nn_time0, wp) ) ! time
+	 CALL iom_rstput( kt, nitrst, numrow, 'ntime'  , REAL( nn_time0, wp), lxios = lwxios ) ! time
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
Index: branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90
===================================================================
--- branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90	(revision 8622)
+++ branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90	(revision 8630)
@@ -47,5 +47,5 @@
    USE wrk_nemo       ! Memory Allocation
    USE timing         ! Timing
-
+   USE iom_def, ONLY:lwxios, wxioso ! write restart flag and output type
    IMPLICIT NONE
    PRIVATE
@@ -285,5 +285,5 @@
          &             nn_it000, nn_itend , nn_date0    , nn_time0     , nn_leapy  , nn_istate ,     &
          &             nn_stock, nn_write , ln_mskland  , ln_clobber   , nn_chunksz, nn_euler  ,     &
-         &             ln_cfmeta, ln_iscpl
+         &             ln_cfmeta, ln_iscpl, nn_wxios
       NAMELIST/namdom/ ln_linssh, nn_closea, nn_msh, rn_isfhmin, rn_rdt, rn_atfp, ln_crs
 #if defined key_netcdf4
@@ -293,4 +293,5 @@
       !!----------------------------------------------------------------------
       !
+      nn_wxios = 0
       REWIND( numnam_ref )              ! Namelist namrun in reference namelist : Parameters of the run
       READ  ( numnam_ref, namrun, IOSTAT = ios, ERR = 901)
@@ -333,4 +334,7 @@
          WRITE(numout,*) '      NetCDF chunksize (bytes)        nn_chunksz = ', nn_chunksz
          WRITE(numout,*) '      IS coupling at the restart step ln_iscpl   = ', ln_iscpl
+         WRITE(numout,*) '      Write restart using XIOS        nn_wxios   = ', nn_wxios
+         IF( TRIM(Agrif_CFixed()) == '0' ) &
+     &      WRITE(numout,*) '      Write restart using XIOS        nn_wxios   = ', nn_wxios
       ENDIF
 
@@ -413,4 +417,16 @@
       rdt       = rn_rdt
 
+      IF( TRIM(Agrif_CFixed()) == '0' ) THEN
+!set output file type for XIOS based on NEMO namelist 
+         if (nn_wxios > 0) lwxios = .TRUE. 
+         wxioso = nn_wxios
+      ELSE
+         IF(lwp) THEN
+            write(numout,*)
+            write(numout,*) "AGRIF: nn_wxios will be ingored. See setting for NEMO" 
+            write(numout,*)
+         ENDIF
+      ENDIF
+
 #if defined key_netcdf4
       !                             ! NetCDF 4 case   ("key_netcdf4" defined)
Index: branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/domvvl.F90
===================================================================
--- branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/domvvl.F90	(revision 8622)
+++ branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/domvvl.F90	(revision 8630)
@@ -931,18 +931,19 @@
          !                                           ! all cases !
          !                                           ! --------- !
-         CALL iom_rstput( kt, nitrst, numrow, 'e3t_b', e3t_b(:,:,:) )
-         CALL iom_rstput( kt, nitrst, numrow, 'e3t_n', e3t_n(:,:,:) )
+         CALL iom_rstput( kt, nitrst, numrow, 'e3t_b', e3t_b(:,:,:), lxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'e3t_n', e3t_n(:,:,:), lxios = lwxios )
          !                                           ! ----------------------- !
          IF( ln_vvl_ztilde .OR. ln_vvl_layer ) THEN  ! z_tilde and layer cases !
             !                                        ! ----------------------- !
-            CALL iom_rstput( kt, nitrst, numrow, 'tilde_e3t_b', tilde_e3t_b(:,:,:) )
-            CALL iom_rstput( kt, nitrst, numrow, 'tilde_e3t_n', tilde_e3t_n(:,:,:) )
+            CALL iom_rstput( kt, nitrst, numrow, 'tilde_e3t_b', tilde_e3t_b(:,:,:), lxios = lwxios)
+            CALL iom_rstput( kt, nitrst, numrow, 'tilde_e3t_n', tilde_e3t_n(:,:,:), lxios = lwxios)
          END IF
          !                                           ! -------------!    
          IF( ln_vvl_ztilde ) THEN                    ! z_tilde case !
             !                                        ! ------------ !
-            CALL iom_rstput( kt, nitrst, numrow, 'hdiv_lf', hdiv_lf(:,:,:) )
+            CALL iom_rstput( kt, nitrst, numrow, 'hdiv_lf', hdiv_lf(:,:,:), lxios = lwxios)
          ENDIF
          !
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
Index: branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90
===================================================================
--- branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90	(revision 8622)
+++ branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90	(revision 8630)
@@ -56,4 +56,5 @@
    USE asminc          ! Assimilation increment
 #endif
+   USE iom_def, ONLY : lwxios
 
 
@@ -1217,22 +1218,24 @@
       !
       ELSEIF( TRIM(cdrw) == 'WRITE' ) THEN
-         CALL iom_rstput( kt, nitrst, numrow, 'ub2_b'   , ub2_b  (:,:) )
-         CALL iom_rstput( kt, nitrst, numrow, 'vb2_b'   , vb2_b  (:,:) )
+         IF( lwxios ) CALL iom_swap(      wxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'ub2_b'   , ub2_b  (:,:), lxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'vb2_b'   , vb2_b  (:,:), lxios = lwxios )
          !
          IF (.NOT.ln_bt_av) THEN
-            CALL iom_rstput( kt, nitrst, numrow, 'sshbb_e'  , sshbb_e(:,:) ) 
-            CALL iom_rstput( kt, nitrst, numrow, 'ubb_e'    ,   ubb_e(:,:) )
-            CALL iom_rstput( kt, nitrst, numrow, 'vbb_e'    ,   vbb_e(:,:) )
-            CALL iom_rstput( kt, nitrst, numrow, 'sshb_e'   ,  sshb_e(:,:) )
-            CALL iom_rstput( kt, nitrst, numrow, 'ub_e'     ,    ub_e(:,:) )
-            CALL iom_rstput( kt, nitrst, numrow, 'vb_e'     ,    vb_e(:,:) )
+            CALL iom_rstput( kt, nitrst, numrow, 'sshbb_e'  , sshbb_e(:,:), lxios = lwxios ) 
+            CALL iom_rstput( kt, nitrst, numrow, 'ubb_e'    ,   ubb_e(:,:), lxios = lwxios )
+            CALL iom_rstput( kt, nitrst, numrow, 'vbb_e'    ,   vbb_e(:,:), lxios = lwxios )
+            CALL iom_rstput( kt, nitrst, numrow, 'sshb_e'   ,  sshb_e(:,:), lxios = lwxios )
+            CALL iom_rstput( kt, nitrst, numrow, 'ub_e'     ,    ub_e(:,:), lxios = lwxios )
+            CALL iom_rstput( kt, nitrst, numrow, 'vb_e'     ,    vb_e(:,:), lxios = lwxios )
          ENDIF
 #if defined key_agrif
          ! Save time integrated fluxes
          IF ( .NOT.Agrif_Root() ) THEN
-            CALL iom_rstput( kt, nitrst, numrow, 'ub2_i_b'  , ub2_i_b(:,:) )
-            CALL iom_rstput( kt, nitrst, numrow, 'vb2_i_b'  , vb2_i_b(:,:) )
+            CALL iom_rstput( kt, nitrst, numrow, 'ub2_i_b'  , ub2_i_b(:,:), lxios = lwxios )
+            CALL iom_rstput( kt, nitrst, numrow, 'vb2_i_b'  , vb2_i_b(:,:), lxios = lwxios )
          ENDIF
 #endif
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
Index: branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/in_out_manager.F90
===================================================================
--- branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/in_out_manager.F90	(revision 8622)
+++ branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/in_out_manager.F90	(revision 8630)
@@ -44,5 +44,5 @@
    LOGICAL       ::   ln_clobber       !: clobber (overwrite) an existing file
    INTEGER       ::   nn_chunksz       !: chunksize (bytes) for NetCDF file (works only with iom_nf90 routines)
-
+   INTEGER       ::   nn_wxios         !: write resart using xios 0 - no, 1 - single, 2 - multiple file output
 #if defined key_netcdf4
    !!----------------------------------------------------------------------
@@ -150,4 +150,5 @@
    LOGICAL       ::   lsp_area = .TRUE.     !: to make a control print over a specific area
    CHARACTER(lc) ::   cxios_context         !: context name used in xios
+   CHARACTER(lc) ::   wxios_context         !: context name used in xios to write restart file
 
    !!----------------------------------------------------------------------
Index: branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90
===================================================================
--- branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90	(revision 8622)
+++ branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90	(revision 8630)
@@ -41,11 +41,16 @@
    USE dianam          ! build name of file
    USE xios
+   USE iom_def, ONLY : wxioso
 # endif
    USE ioipsl, ONLY :  ju2ymds    ! for calendar
    USE crs             ! Grid coarsening
+   USE sbc_oce, ONLY : lk_oasis, ln_coupled_iceshelf_fluxes, ln_apr_dyn, ln_rnf, nn_components, jp_iam_sas
+   USE diadct, ONLY : lk_diadct
 
    IMPLICIT NONE
+   !  values needed to set correctlyfiles in reast file when using XIOS for writing
+   LOGICAL, PUBLIC :: lr_vvl_ztilde, lr_vvl_layer, lr_traadv_cen2
+
    PUBLIC   !   must be public to be able to access iom_def through iom
-   
 #if defined key_iomput
    LOGICAL, PUBLIC, PARAMETER ::   lk_iomput = .TRUE.        !: iom_put flag
@@ -56,5 +61,4 @@
    PUBLIC iom_getatt, iom_putatt, iom_gettime, iom_rstput, iom_put
    PUBLIC iom_use, iom_context_finalize
-
    PRIVATE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
    PRIVATE iom_g0d, iom_g1d, iom_g2d, iom_g3d, iom_get_123d
@@ -63,4 +67,5 @@
    PRIVATE iom_set_domain_attr, iom_set_axis_attr, iom_set_field_attr, iom_set_file_attr, iom_get_file_attr, iom_set_grid_attr
    PRIVATE set_grid, set_grid_bounds, set_scalar, set_xmlatt, set_mooring, iom_update_file_name, iom_sdate
+   PRIVATE set_rstw_active
 # endif
 
@@ -124,13 +129,12 @@
           &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
       END SELECT
-
       ! horizontal grid definition
       CALL set_scalar
 
-      IF( TRIM(cdname) == TRIM(cxios_context) ) THEN  
-         CALL set_grid( "T", glamt, gphit ) 
-         CALL set_grid( "U", glamu, gphiu )
-         CALL set_grid( "V", glamv, gphiv )
-         CALL set_grid( "W", glamt, gphit )
+      IF( TRIM(cdname) == TRIM(cxios_context)) THEN  
+         CALL set_grid( "T", glamt, gphit, .TRUE. ) 
+         CALL set_grid( "U", glamu, gphiu, .TRUE. )
+         CALL set_grid( "V", glamv, gphiv, .TRUE. )
+         CALL set_grid( "W", glamt, gphit, .TRUE. )
          CALL set_grid_znl( gphit )
          !
@@ -150,13 +154,13 @@
          CALL dom_grid_crs   ! Save the parent grid information  & Switch to coarse grid domain
          !
-         CALL set_grid( "T", glamt_crs, gphit_crs ) 
-         CALL set_grid( "U", glamu_crs, gphiu_crs ) 
-         CALL set_grid( "V", glamv_crs, gphiv_crs ) 
-         CALL set_grid( "W", glamt_crs, gphit_crs ) 
+         CALL set_grid( "T", glamt_crs, gphit_crs, ln_mskland ) 
+         CALL set_grid( "U", glamu_crs, gphiu_crs, ln_mskland ) 
+         CALL set_grid( "V", glamv_crs, gphiv_crs, ln_mskland ) 
+         CALL set_grid( "W", glamt_crs, gphit_crs, ln_mskland ) 
          CALL set_grid_znl( gphit_crs )
           !
          CALL dom_grid_glo   ! Return to parent grid domain
          !
-         IF( ln_cfmeta ) THEN   ! Add additional grid metadata
+         IF( ln_cfmeta) THEN   ! Add additional grid metadata
             CALL iom_set_domain_attr("grid_T", area = e1e2t_crs(nldi:nlei, nldj:nlej))
             CALL iom_set_domain_attr("grid_U", area = e1u_crs(nldi:nlei, nldj:nlej) * e2u_crs(nldi:nlei, nldj:nlej))
@@ -171,8 +175,8 @@
 
       ! vertical grid definition
-      CALL iom_set_axis_attr( "deptht", gdept_1d )
-      CALL iom_set_axis_attr( "depthu", gdept_1d )
-      CALL iom_set_axis_attr( "depthv", gdept_1d )
-      CALL iom_set_axis_attr( "depthw", gdepw_1d )
+      CALL iom_set_axis_attr( "deptht", paxis = gdept_1d )
+      CALL iom_set_axis_attr( "depthu", paxis = gdept_1d )
+      CALL iom_set_axis_attr( "depthv", paxis = gdept_1d )
+      CALL iom_set_axis_attr( "depthw", paxis = gdepw_1d )
 
       ! Add vertical grid bounds
@@ -200,5 +204,4 @@
       CALL iom_set_axis_attr( "iax_28C", (/ REAL(28,wp) /) )
       
-      ! automatic definitions of some of the xml attributs
       CALL set_xmlatt
 
@@ -213,7 +216,510 @@
 
 #endif
-      
+
    END SUBROUTINE iom_init
 
+   SUBROUTINE iom_rstw_init( cdname, filename ) 
+      !!----------------------------------------------------------------------
+      !!                     ***  ROUTINE   ***
+      !!
+      !! ** Purpose :   
+      !!
+      !!----------------------------------------------------------------------
+      CHARACTER(len=*), INTENT(in)  :: cdname
+#if defined key_iomput
+#if defined key_xios2
+      TYPE(xios_duration) :: dtime    = xios_duration(0, 0, 0, 0, 0, 0)
+      TYPE(xios_date)     :: start_date
+      TYPE(xios_domaingroup)            :: domaingroup_hdl
+      TYPE(xios_domain)                 :: domain_hdl
+      TYPE(xios_axisgroup)              :: axisgroup_hdl
+      TYPE(xios_axis)                   :: axis_hdl
+      TYPE(xios_scalar)                 :: scalar_hdl
+      TYPE(xios_scalargroup)            :: scalargroup_hdl
+#endif
+      CHARACTER(len=128)   :: clname
+      INTEGER             :: ji
+      !
+      CHARACTER(len=*), OPTIONAL, INTENT(in)  :: filename 
+      !!----------------------------------------------------------------------
+
+      clname = cdname 
+      IF( TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(cdname)
+      CALL xios_context_initialize(TRIM(clname), mpi_comm_opa)
+      CALL iom_swap( cdname )
+      ! calendar parameters
+#if defined key_xios2
+      ! Calendar type is now defined in xml file 
+      SELECT CASE ( nleapy )        ! Choose calendar for IOIPSL
+      CASE ( 1)   ; CALL xios_define_calendar( TYPE = "Gregorian", time_origin = xios_date(1900,01,01,00,00,00), &
+          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
+      CASE ( 0)   ; CALL xios_define_calendar( TYPE = "NoLeap"   , time_origin = xios_date(1900,01,01,00,00,00), &
+          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
+      CASE (30)   ; CALL xios_define_calendar( TYPE = "D360"     , time_origin = xios_date(1900,01,01,00,00,00), &
+          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
+      END SELECT
+
+#endif
+      CALL xios_get_handle("domain_definition",domaingroup_hdl)
+      CALL xios_add_child(domaingroup_hdl, domain_hdl, "grid_N")
+      CALL set_grid("N", glamt, gphit, .FALSE.)
+ 
+      CALL xios_get_handle("axis_definition",axisgroup_hdl)
+      CALL xios_add_child(axisgroup_hdl, axis_hdl, "deptht")
+!if unit="m" is present AGRIF configuration fails to compile
+      CALL xios_set_axis_attr( "deptht", long_name="Vertical levels in meters", positive="down")
+      ! vertical grid definition
+      CALL iom_set_axis_attr( "deptht", paxis = gdept_1d )
+
+      CALL xios_get_handle("scalar_definition",scalargroup_hdl)
+      CALL xios_add_child(scalargroup_hdl, scalar_hdl, "grid_scalar")
+
+      ! automatic definitions of some of the xml attributs
+      IF( TRIM(cdname) == TRIM(wxios_context)) THEN
+!set which fields are to be read from restart file
+       CALL set_rstw_active(filename)
+      ENDIF
+
+      ! end file definition
+      dtime%second = rdt
+      CALL xios_set_timestep(dtime)
+      CALL xios_close_context_definition()
+#endif
+
+   END SUBROUTINE iom_rstw_init
+
+   SUBROUTINE set_rstw_active(rst_file)
+!sets enabled = .TRUE. for each field in restart file
+   CHARACTER(len=*) :: rst_file
+   TYPE(xios_file) :: file_hdl
+   TYPE(xios_filegroup) :: filegroup_hdl
+
+!set then name of the restart file (OUTPUT!) and enable available fields
+        if(lwp) WRITE(numout,*) 'Setting (output) restart filename (for XIOS) to: ',TRIM(rst_file)
+        CALL xios_get_handle("file_definition", filegroup_hdl )
+        CALL xios_add_child(filegroup_hdl, file_hdl, 'wrestart')
+        IF(wxioso.eq.1) THEN
+            CALL xios_set_file_attr( "wrestart", type="one_file", name = TRIM(rst_file), &
+               enabled=.TRUE., mode="write", output_freq=xios_timestep)
+            if(lwp) write(numout,*) 'OPEN ', trim(rst_file), ' in one_file mode'
+        ELSE 
+            CALL xios_set_file_attr( "wrestart", type="multiple_file", name = TRIM(rst_file),&
+               enabled=.TRUE., mode="write", output_freq=xios_timestep)
+          if(lwp) write(numout,*) 'OPEN ', trim(rst_file), ' in multiple_file mode'
+        ENDIF
+
+        CALL xios_set_file_attr( "wrestart", name=trim(rst_file))
+        CALL set_numro_active(file_hdl)
+ 
+    END SUBROUTINE set_rstw_active
+
+    SUBROUTINE set_numro_active(file_hdl)
+    TYPE(xios_field) :: field_hdl
+    TYPE(xios_file) :: file_hdl
+
+        CALL xios_add_child(file_hdl, field_hdl, "rdt")
+        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  scalar_ref = "grid_scalar", operation = "instant")
+
+        CALL xios_add_child(file_hdl, field_hdl, "rdttra1")
+        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  scalar_ref = "grid_scalar", operation = "instant")
+
+        CALL xios_add_child(file_hdl, field_hdl, "un")
+        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+        CALL xios_add_child(file_hdl, field_hdl, "ub")
+        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+        CALL xios_add_child(file_hdl, field_hdl, "vn")
+        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+        CALL xios_add_child(file_hdl, field_hdl, "vb")
+        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+        CALL xios_add_child(file_hdl, field_hdl, "tn")
+        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+        CALL xios_add_child(file_hdl, field_hdl, "tb")
+        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+        CALL xios_add_child(file_hdl, field_hdl, "sn")
+        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+        CALL xios_add_child(file_hdl, field_hdl, "sb")
+        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+        CALL xios_add_child(file_hdl, field_hdl, "sshn")
+        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref = "grid_N", operation = "instant")
+
+        CALL xios_add_child(file_hdl, field_hdl, "sshb")
+        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref = "grid_N", operation = "instant")
+
+        CALL xios_add_child(file_hdl, field_hdl, "hdivn")
+        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+        CALL xios_add_child(file_hdl, field_hdl, "hdivb")
+        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+        CALL xios_add_child(file_hdl, field_hdl, "rhop")
+        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+        CALL xios_add_child(file_hdl, field_hdl, "rotn")
+        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+        CALL xios_add_child(file_hdl, field_hdl, "rotb")
+        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+!in daymod.F90
+           CALL xios_add_child(file_hdl, field_hdl, "kt")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     scalar_ref = "grid_scalar", operation = "instant")
+
+           CALL xios_add_child(file_hdl, field_hdl, "ndastp")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     scalar_ref = "grid_scalar", operation = "instant")
+
+           CALL xios_add_child(file_hdl, field_hdl, "adatrj")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     scalar_ref = "grid_scalar", operation = "instant")
+!end daymod.F90
+!sbcmod.F90
+           CALL xios_add_child(file_hdl, field_hdl, "utau_b")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref = "grid_N", operation = "instant")
+
+           CALL xios_add_child(file_hdl, field_hdl, "vtau_b")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref = "grid_N", operation = "instant")
+
+           CALL xios_add_child(file_hdl, field_hdl, "qns_b")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref = "grid_N", operation = "instant")
+
+           CALL xios_add_child(file_hdl, field_hdl, "emp_b")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref = "grid_N", operation = "instant")
+
+           CALL xios_add_child(file_hdl, field_hdl, "sfx_b")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref = "grid_N", operation = "instant")
+!end sbcmod.F90
+!ALL FIELDS ABOUVE ALWAYS
+!zdftke.F90
+#if defined key_zdftke   ||   defined key_esopa
+           CALL xios_add_child(file_hdl, field_hdl, "en")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+           CALL xios_add_child(file_hdl, field_hdl, "avt")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+           CALL xios_add_child(file_hdl, field_hdl, "avm")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+           CALL xios_add_child(file_hdl, field_hdl, "avmu")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+           CALL xios_add_child(file_hdl, field_hdl, "avmv")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+           CALL xios_add_child(file_hdl, field_hdl, "dissl")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+#endif
+!end zdftke.F90
+!traqsr.F90
+       CALL xios_add_child(file_hdl, field_hdl, "qsr_hc_b")
+       CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref="grid_N", axis_ref="deptht" , operation = "instant")        
+
+       CALL xios_add_child(file_hdl, field_hdl, "fraqsr_1lev")
+       CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref = "grid_N", operation = "instant")        
+!END traqsr.F90
+#if defined key_dynspg_flt   ||   defined key_esopa 
+!dynspg_flt.F90
+           CALL xios_add_child(file_hdl, field_hdl, "gcx")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref = "grid_N", operation = "instant")
+
+           CALL xios_add_child(file_hdl, field_hdl, "gcxb")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref = "grid_N", operation = "instant")
+!end dynspg_flt.F90
+#endif
+!trasbc.F90 START
+           CALL xios_add_child(file_hdl, field_hdl, "sbc_hc_b")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref = "grid_N", operation = "instant")
+
+           CALL xios_add_child(file_hdl, field_hdl, "sbc_sc_b")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref = "grid_N", operation = "instant")
+
+           CALL xios_add_child(file_hdl, field_hdl, "fwf_isf_b")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref = "grid_N", operation = "instant")
+
+           CALL xios_add_child(file_hdl, field_hdl, "isf_sc_b")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref = "grid_N", operation = "instant")
+
+           CALL xios_add_child(file_hdl, field_hdl, "isf_hc_b")
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref = "grid_N", operation = "instant")
+!trasbc.F90 END
+        IF( lk_oasis) THEN
+        ! ln_coupled_iceshelf_fluxes uninitialised unless lk_oasis=true
+          IF( ln_coupled_iceshelf_fluxes ) THEN
+             CALL xios_add_child(file_hdl, field_hdl, "greenland_icesheet_mass")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  scalar_ref = "grid_scalar", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "greenland_icesheet_timelapsed")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  scalar_ref = "grid_scalar", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "greenland_icesheet_mass_roc")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  scalar_ref = "grid_scalar", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "antarctica_icesheet_mass")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  scalar_ref = "grid_scalar", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "antarctica_icesheet_timelapsed")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  scalar_ref = "grid_scalar", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "antarctica_icesheet_mass_roc")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  scalar_ref = "grid_scalar", operation = "instant")
+          ENDIF
+        ENDIF
+#if defined key_zdfkpp
+        CALL xios_add_child(file_hdl, field_hdl, "rhd")
+        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+             domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+#endif
+!dia_hsb_rst
+#if defined key_diadct
+        IF( lk_diadct     ) THEN
+             CALL xios_add_child(file_hdl, field_hdl, "frc_v")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  scalar_ref = "grid_scalar", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "frc_t")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  scalar_ref = "grid_scalar", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "frc_s")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  scalar_ref = "grid_scalar", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "ssh_ini")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref = "grid_N", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "e3t_ini")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "hc_loc_ini")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "sc_loc_ini")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+
+             IF( .NOT. lk_vvl ) THEN
+                CALL xios_add_child(file_hdl, field_hdl, "frc_wn_t")
+                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     scalar_ref = "grid_scalar", operation = "instant")
+
+                CALL xios_add_child(file_hdl, field_hdl, "frc_wn_s")
+                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     scalar_ref = "grid_scalar", operation = "instant")
+
+                CALL xios_add_child(file_hdl, field_hdl, "ssh_hc_loc_ini")
+                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref = "grid_N", operation = "instant")
+
+                CALL xios_add_child(file_hdl, field_hdl, "ssh_sc_loc_ini")
+                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref = "grid_N", operation = "instant")
+
+
+             ENDIF
+        ENDIF
+#endif
+!end dia_hsb_rst
+!domvvl.F90
+        IF( lk_vvl ) THEN
+             CALL xios_add_child(file_hdl, field_hdl, "fse3t_b")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "fse3t_n")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+
+           IF( lr_vvl_ztilde .OR. lr_vvl_layer ) THEN  ! z_tilde and layer cases !
+             CALL xios_add_child(file_hdl, field_hdl, "tilde_e3t_b")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref = "grid_N", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "tilde_e3t_n")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref = "grid_N", operation = "instant")
+            END IF
+            IF( lr_vvl_ztilde ) THEN                    ! z_tilde case !
+            CALL xios_add_child(file_hdl, field_hdl, "hdiv_lf")
+            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref = "grid_N", operation = "instant")
+            ENDIF
+         ENDIF
+!end domvvl.F90
+!dynspg_ts.F90
+#if defined key_dynspg_ts   ||   defined key_esopa
+         CALL xios_add_child(file_hdl, field_hdl, "ub2_b")
+         CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+              domain_ref = "grid_N", operation = "instant")
+
+         CALL xios_add_child(file_hdl, field_hdl, "vb2_b")
+         CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+              domain_ref = "grid_N", operation = "instant")
+
+
+         IF (.NOT.ln_bt_av) THEN
+            CALL xios_add_child(file_hdl, field_hdl, "sshbb_e")
+            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                 domain_ref = "grid_N", operation = "instant")
+
+            CALL xios_add_child(file_hdl, field_hdl, "ubb_e")
+            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                 domain_ref = "grid_N", operation = "instant")
+
+            CALL xios_add_child(file_hdl, field_hdl, "vbb_e")
+            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                 domain_ref = "grid_N", operation = "instant")
+
+            CALL xios_add_child(file_hdl, field_hdl, "sshb_e")
+            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                 domain_ref = "grid_N", operation = "instant")
+
+            CALL xios_add_child(file_hdl, field_hdl, "ub_e")
+            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                 domain_ref = "grid_N", operation = "instant")
+
+            CALL xios_add_child(file_hdl, field_hdl, "vb_e")
+            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                 domain_ref = "grid_N", operation = "instant")
+         ENDIF
+#if defined key_agrif
+         ! Save time integrated fluxes
+         IF ( .NOT.Agrif_Root() ) THEN
+               CALL xios_add_child(file_hdl, field_hdl, "ub2_i_b")
+               CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                    domain_ref = "grid_N", operation = "instant")
+
+               CALL xios_add_child(file_hdl, field_hdl, "vb2_i_b")
+               CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                    domain_ref = "grid_N", operation = "instant")
+         ENDIF
+#endif
+#endif
+!end dynspg_ts.F90
+!sbcapr.F90
+          IF( ln_apr_dyn) THEN
+             CALL xios_add_child(file_hdl, field_hdl, "ssh_ibb")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref = "grid_N", operation = "instant")
+          ENDIF
+!end sbcapr.F90
+!sbcrnf.F90
+        IF( ln_rnf      ) THEN
+             CALL xios_add_child(file_hdl, field_hdl, "rnf_b")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref = "grid_N", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "rnf_hc_b")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref = "grid_N", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "rnf_sc_b")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref = "grid_N", operation = "instant")
+        ENDIF 
+!end sbcrnf.F90
+!sbcssm.F90
+        IF( nn_components /= jp_iam_sas .AND. nn_fsbc .NE. 1) THEN
+             CALL xios_add_child(file_hdl, field_hdl, "nn_fsbc")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  scalar_ref = "grid_scalar", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "ssu_m")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref = "grid_N", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "ssv_m")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref = "grid_N", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "sst_m")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref = "grid_N", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "sss_m")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref = "grid_N", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "ssh_m")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                  domain_ref = "grid_N", operation = "instant")
+
+             CALL xios_add_child(file_hdl, field_hdl, "frq_m")
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref = "grid_N", operation = "instant")
+
+             IF( lk_vvl )  THEN
+                CALL xios_add_child(file_hdl, field_hdl, "e3t_m")
+                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     domain_ref = "grid_N", operation = "instant")
+             ENDIF 
+        ENDIF
+!end sbcssm.F90
+       IF( lr_traadv_cen2   ) THEN
+          CALL xios_add_child(file_hdl, field_hdl, "avmb")
+          CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+               axis_ref = "deptht", operation = "instant")
+
+          CALL xios_add_child(file_hdl, field_hdl, "avtb")
+          CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+               axis_ref = "deptht", operation = "instant")
+       ENDIF
+
+   END SUBROUTINE set_numro_active
 
    SUBROUTINE iom_swap( cdname )
@@ -905,5 +1411,5 @@
                IF( zofs /= 0. )   pv_r1d(:) = pv_r1d(:) + zofs
             ELSEIF( PRESENT(pv_r2d) ) THEN
-               IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
+              IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
                IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
             ELSEIF( PRESENT(pv_r3d) ) THEN
@@ -1115,5 +1621,5 @@
    !!                   INTERFACE iom_rstput
    !!----------------------------------------------------------------------
-   SUBROUTINE iom_rp0d( kt, kwrite, kiomid, cdvar, pvar, ktype )
+   SUBROUTINE iom_rp0d( kt, kwrite, kiomid, cdvar, pvar, ktype, lxios )
       INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
       INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
@@ -1122,18 +1628,33 @@
       REAL(wp)        , INTENT(in)                         ::   pvar     ! written field
       INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
+      LOGICAL, OPTIONAL :: lxios   ! xios write flag
+      LOGICAL :: lx                ! local xios write flag
       INTEGER :: ivid   ! variable id
-      IF( kiomid > 0 ) THEN
-         IF( iom_file(kiomid)%nfid > 0 ) THEN
-            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
-            SELECT CASE (iom_file(kiomid)%iolib)
-            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
-            CASE DEFAULT
-               CALL ctl_stop( 'iom_rp0d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
-            END SELECT
+      lx = .FALSE.
+      IF(PRESENT(lxios)) lx = lxios
+      IF( lx ) THEN
+#ifdef key_iomput
+      IF( kt == kwrite ) THEN
+          IF(lwp) write(numout,*) 'RESTART: write (XIOS 0D) ',trim(cdvar)
+          CALL xios_send_field(trim(cdvar), pvar)
+      ENDIF
+#endif
+      ELSE
+         IF( kiomid > 0 ) THEN
+            IF( iom_file(kiomid)%nfid > 0 ) THEN
+               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
+               SELECT CASE (iom_file(kiomid)%iolib)
+               CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
+               CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
+               CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pvar )
+               CASE DEFAULT     
+                  CALL ctl_stop( 'iom_rp0d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
+               END SELECT
+            ENDIF
          ENDIF
       ENDIF
    END SUBROUTINE iom_rp0d
 
-   SUBROUTINE iom_rp1d( kt, kwrite, kiomid, cdvar, pvar, ktype )
+   SUBROUTINE iom_rp1d( kt, kwrite, kiomid, cdvar, pvar, ktype, lxios )
       INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
       INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
@@ -1142,18 +1663,33 @@
       REAL(wp)        , INTENT(in), DIMENSION(          :) ::   pvar     ! written field
       INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
+      LOGICAL, OPTIONAL :: lxios   ! xios write flag
+      LOGICAL :: lx                ! local xios write flag
       INTEGER :: ivid   ! variable id
-      IF( kiomid > 0 ) THEN
-         IF( iom_file(kiomid)%nfid > 0 ) THEN
-            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
-            SELECT CASE (iom_file(kiomid)%iolib)
-            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
-            CASE DEFAULT
-               CALL ctl_stop( 'iom_rp1d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
-            END SELECT
+      lx = .FALSE.
+      IF(PRESENT(lxios)) lx = lxios
+      IF( lx ) THEN
+#ifdef key_iomput
+      IF( kt == kwrite ) THEN
+         IF(lwp) write(numout,*) 'RESTART: write (XIOS 1D) ',trim(cdvar)
+         CALL xios_send_field(trim(cdvar), pvar)
+      ENDIF
+#endif
+      ELSE
+         IF( kiomid > 0 ) THEN
+            IF( iom_file(kiomid)%nfid > 0 ) THEN
+               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
+               SELECT CASE (iom_file(kiomid)%iolib)
+               CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
+               CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
+               CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r1d = pvar )
+               CASE DEFAULT     
+                  CALL ctl_stop( 'iom_rp1d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
+               END SELECT
+            ENDIF
          ENDIF
       ENDIF
    END SUBROUTINE iom_rp1d
 
-   SUBROUTINE iom_rp2d( kt, kwrite, kiomid, cdvar, pvar, ktype )
+   SUBROUTINE iom_rp2d( kt, kwrite, kiomid, cdvar, pvar, ktype, lxios )
       INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
       INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
@@ -1162,18 +1698,33 @@
       REAL(wp)        , INTENT(in), DIMENSION(:,    :    ) ::   pvar     ! written field
       INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
+      LOGICAL, OPTIONAL :: lxios   ! xios write flag
+      LOGICAL :: lx                ! local xios write flag
       INTEGER :: ivid   ! variable id
-      IF( kiomid > 0 ) THEN
-         IF( iom_file(kiomid)%nfid > 0 ) THEN
-            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
-            SELECT CASE (iom_file(kiomid)%iolib)
-            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
-            CASE DEFAULT
-               CALL ctl_stop( 'iom_rp2d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
-            END SELECT
+      lx = .FALSE.
+      IF(PRESENT(lxios)) lx = lxios
+      IF( lx ) THEN
+#ifdef key_iomput
+      IF( kt == kwrite ) THEN
+         IF(lwp) write(numout,*) 'RESTART: write (XIOS 2D) ',trim(cdvar)
+         CALL xios_send_field(trim(cdvar), pvar)
+      ENDIF
+#endif
+      ELSE
+         IF( kiomid > 0 ) THEN
+            IF( iom_file(kiomid)%nfid > 0 ) THEN
+               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
+               SELECT CASE (iom_file(kiomid)%iolib)
+               CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
+               CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
+               CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r2d = pvar ) 
+               CASE DEFAULT     
+                  CALL ctl_stop( 'iom_rp2d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
+               END SELECT
+            ENDIF
          ENDIF
       ENDIF
    END SUBROUTINE iom_rp2d
 
-   SUBROUTINE iom_rp3d( kt, kwrite, kiomid, cdvar, pvar, ktype )
+   SUBROUTINE iom_rp3d( kt, kwrite, kiomid, cdvar, pvar, ktype, lxios )
       INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
       INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
@@ -1182,13 +1733,28 @@
       REAL(wp)        , INTENT(in),       DIMENSION(:,:,:) ::   pvar     ! written field
       INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
+      LOGICAL, OPTIONAL :: lxios   ! xios write flag
+      LOGICAL :: lx                 ! local xios write flag
       INTEGER :: ivid   ! variable id
-      IF( kiomid > 0 ) THEN
-         IF( iom_file(kiomid)%nfid > 0 ) THEN
-            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
-            SELECT CASE (iom_file(kiomid)%iolib)
-            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
-            CASE DEFAULT
-               CALL ctl_stop( 'iom_rp3d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
-            END SELECT
+      lx = .FALSE.
+      IF(PRESENT(lxios)) lx = lxios
+      IF( lx ) THEN
+#ifdef key_iomput
+      IF( kt == kwrite ) THEN
+         IF(lwp) write(numout,*) 'RESTART: write (XIOS 3D) ',trim(cdvar)
+         CALL xios_send_field(trim(cdvar), pvar)
+      ENDIF
+#endif
+      ELSE
+         IF( kiomid > 0 ) THEN
+            IF( iom_file(kiomid)%nfid > 0 ) THEN
+               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
+               SELECT CASE (iom_file(kiomid)%iolib)
+               CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
+               CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
+               CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r3d = pvar )
+               CASE DEFAULT     
+                  CALL ctl_stop( 'iom_rp3d: accepted IO library are only jpioipsl and jprstdimg' )
+               END SELECT
+            ENDIF
          ENDIF
       ENDIF
@@ -1234,5 +1800,4 @@
    SUBROUTINE iom_p3d( cdname, pfield3d )
       CHARACTER(LEN=*)                , INTENT(in) ::   cdname
-      REAL(wp),       DIMENSION(:,:,:), INTENT(in) ::   pfield3d
 #if defined key_iomput
       CALL xios_send_field(cdname, pfield3d)
@@ -1368,6 +1933,10 @@
    SUBROUTINE iom_context_finalize( cdname )
       CHARACTER(LEN=*), INTENT(in) :: cdname
-      !
-      IF( xios_is_valid_context(cdname) ) THEN
+      CHARACTER(LEN=120)           :: clname
+      !
+      clname = cdname
+      IF( TRIM(Agrif_CFixed()) .NE. '0' ) clname = TRIM(Agrif_CFixed())//"_"//clname 
+
+      IF( xios_is_valid_context(clname) ) THEN
          CALL iom_swap( cdname )   ! swap to cdname context
          CALL xios_context_finalize() ! finalize the context
@@ -1378,5 +1947,5 @@
 
 
-   SUBROUTINE set_grid( cdgrd, plon, plat )
+   SUBROUTINE set_grid( cdgrd, plon, plat, lxios )
       !!----------------------------------------------------------------------
       !!                     ***  ROUTINE set_grid  ***
@@ -1391,4 +1960,5 @@
       REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zmask
       INTEGER  :: ni,nj
+      LOGICAL :: lxios
       
       ni=nlei-nldi+1 ; nj=nlej-nldj+1
@@ -1396,8 +1966,8 @@
       CALL iom_set_domain_attr("grid_"//cdgrd, ni_glo=jpiglo, nj_glo=jpjglo, ibegin=nimpp+nldi-2, jbegin=njmpp+nldj-2, ni=ni, nj=nj)
       CALL iom_set_domain_attr("grid_"//cdgrd, data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
-      CALL iom_set_domain_attr("grid_"//cdgrd, lonvalue = RESHAPE(plon(nldi:nlei, nldj:nlej),(/ ni*nj /)),   &
+      if(.NOT.lxios) CALL iom_set_domain_attr("grid_"//cdgrd, lonvalue = RESHAPE(plon(nldi:nlei, nldj:nlej),(/ ni*nj /)),   &
          &                                     latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /)))  
 
-      IF ( ln_mskland ) THEN
+      IF ( ln_mskland.AND.(.NOT.lxios) ) THEN
          ! mask land points, keep values on coast line -> specific mask for U, V and W points
          SELECT CASE ( cdgrd )
@@ -1439,5 +2009,5 @@
       ! Offset of coordinate representing bottom-left corner
       SELECT CASE ( TRIM(cdgrd) )
-         CASE ('T', 'W')
+         CASE ('T', 'W', 'N')
             icnr = -1 ; jcnr = -1
          CASE ('U')
Index: branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom_def.F90
===================================================================
--- branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom_def.F90	(revision 8622)
+++ branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom_def.F90	(revision 8630)
@@ -46,4 +46,9 @@
 !$AGRIF_DO_NOT_TREAT
    INTEGER, PUBLIC            ::   iom_open_init = 0   !: used to initialize iom_file(:)%nfid to 0
+!XIOS read restart   
+   LOGICAL, PUBLIC            ::   lwxios          !: read single file restart using XIOS
+   INTEGER, PUBLIC            ::   wxioso          !: type of restart file when writing using XIOS 1 - single, 2 - multiple
+
+
 
    TYPE, PUBLIC ::   file_descriptor
@@ -67,5 +72,4 @@
    TYPE(file_descriptor), DIMENSION(jpmax_files), PUBLIC ::   iom_file !: array containing the info for all opened files
 !$AGRIF_END_DO_NOT_TREAT
-
    !!=====================================================================
 END MODULE iom_def
Index: branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/restart.F90
===================================================================
--- branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/restart.F90	(revision 8622)
+++ branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/restart.F90	(revision 8630)
@@ -28,5 +28,6 @@
    USE iom             ! I/O module
    USE diurnal_bulk
-   
+   USE iom_def, ONLY : lwxios
+
    IMPLICIT NONE
    PRIVATE
@@ -61,6 +62,8 @@
       CHARACTER(LEN=50)   ::   clname   ! ocean output restart file name
       CHARACTER(lc)       ::   clpath   ! full path to ocean output restart file
-      !!----------------------------------------------------------------------
-      !
+      CHARACTER(LEN=52)   ::   dlname   ! ocean output restart file name including prefix for AGRIF
+      !!----------------------------------------------------------------------
+      !
+
       IF( kt == nit000 ) THEN   ! default definitions
          lrst_oce = .FALSE.   
@@ -82,4 +85,5 @@
       ! 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
+      ! NO, because the data is written 2 times on nitrst - 1 and nitrst
       IF( kt == nitrst - 1 .OR. nstock == 1 .OR. ( kt == nitend .AND. .NOT. lrst_oce ) ) THEN
          IF( nitrst <= nitend .AND. nitrst > 0 ) THEN 
@@ -94,18 +98,35 @@
             IF(lwp) THEN
                WRITE(numout,*)
-               SELECT CASE ( jprstlib )
-               CASE DEFAULT         ;   WRITE(numout,*)                            &
-                   '             open ocean restart NetCDF file: ',TRIM(clpath)//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
+               IF(.NOT.lwxios) THEN
+                  SELECT CASE ( jprstlib )
+                  CASE ( jprstdimg )   ;   WRITE(numout,*)                            &
+                      '             open ocean restart binary file: ',TRIM(clpath)//TRIM(clname)
+                  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
             !
-            CALL iom_open( TRIM(clpath)//TRIM(clname), numrow, ldwrt = .TRUE., kiolib = jprstlib )
+            IF(.NOT.lwxios) THEN
+               CALL iom_open( TRIM(clpath)//TRIM(clname), numrow, ldwrt = .TRUE., kiolib = jprstlib )
+            ELSE
+               wxios_context = "rstw_"//TRIM(ADJUSTL(clkt))
+               IF( TRIM(Agrif_CFixed()) == '0' ) THEN
+                  dlname = clname
+               ELSE
+                  dlname = TRIM(Agrif_CFixed())//"_"//clname   
+               ENDIF
+               CALL iom_rstw_init( wxios_context, TRIM(clpath)//TRIM(dlname))
+               CALL xios_update_calendar(nitrst)
+               CALL iom_swap(      cxios_context          )
+            ENDIF
             lrst_oce = .TRUE.
          ENDIF
       ENDIF
+
       !
    END SUBROUTINE rst_opn
@@ -123,21 +144,22 @@
       INTEGER, INTENT(in) ::   kt   ! ocean time-step
       !!----------------------------------------------------------------------
-
+                     IF(lwxios) CALL iom_swap(      wxios_context          )
+                     CALL iom_rstput( kt, nitrst, numrow, 'rdt'    , rdt       , lxios = lwxios)   ! dynamics time step
+                     CALL iom_rstput( kt, nitrst, numrow, 'rdttra1', rdttra(1) , lxios = lwxios)   ! surface tracer time step
                      CALL iom_rstput( kt, nitrst, numrow, 'rdt'    , rdt       )   ! dynamics and tracer time step
 
       IF ( .NOT. ln_diurnal_only ) THEN
-                     CALL iom_rstput( kt, nitrst, numrow, 'ub'     , ub        )     ! before fields
-                     CALL iom_rstput( kt, nitrst, numrow, 'vb'     , vb        )
-                     CALL iom_rstput( kt, nitrst, numrow, 'tb'     , tsb(:,:,:,jp_tem) )
-                     CALL iom_rstput( kt, nitrst, numrow, 'sb'     , tsb(:,:,:,jp_sal) )
-                     CALL iom_rstput( kt, nitrst, numrow, 'sshb'   , sshb      )
+                     CALL iom_rstput( kt, nitrst, numrow, 'ub'     , ub, lxios = lwxios)        )     ! before fields
+                     CALL iom_rstput( kt, nitrst, numrow, 'vb'     , vb, lxios = lwxios)        )
+                     CALL iom_rstput( kt, nitrst, numrow, 'tb'     , tsb(:,:,:,jp_tem), lxios = lwxios) )
+                     CALL iom_rstput( kt, nitrst, numrow, 'sb'     , tsb(:,:,:,jp_sal), lxios = lwxios) )
+                     CALL iom_rstput( kt, nitrst, numrow, 'sshb'   , sshb, lxios = lwxios)      )
                      !
-                     CALL iom_rstput( kt, nitrst, numrow, 'un'     , un        )     ! now fields
-                     CALL iom_rstput( kt, nitrst, numrow, 'vn'     , vn        )
-                     CALL iom_rstput( kt, nitrst, numrow, 'tn'     , tsn(:,:,:,jp_tem) )
-                     CALL iom_rstput( kt, nitrst, numrow, 'sn'     , tsn(:,:,:,jp_sal) )
-                     CALL iom_rstput( kt, nitrst, numrow, 'sshn'   , sshn      )
-                     CALL iom_rstput( kt, nitrst, numrow, 'rhop'   , rhop      )
-
+                     CALL iom_rstput( kt, nitrst, numrow, 'un'     , un, lxios = lwxios        )     ! now fields
+                     CALL iom_rstput( kt, nitrst, numrow, 'vn'     , vn, lxios = lwxios        )
+                     CALL iom_rstput( kt, nitrst, numrow, 'tn'     , tsn(:,:,:,jp_tem), lxios = lwxios )
+                     CALL iom_rstput( kt, nitrst, numrow, 'sn'     , tsn(:,:,:,jp_sal), lxios = lwxios )
+                     CALL iom_rstput( kt, nitrst, numrow, 'sshn'   , sshn, lxios = lwxios      )
+                     CALL iom_rstput( kt, nitrst, numrow, 'rhop'   , rhop, lxios = lwxios      )
                   ! extra variable needed for the ice sheet coupling
                   IF ( ln_iscpl ) THEN 
@@ -156,5 +178,9 @@
 
       IF( kt == nitrst ) THEN
-         CALL iom_close( numrow )     ! close the restart file (only at last time step)
+         IF(.NOT.lwxios) THEN
+            CALL iom_close( numrow )     ! close the restart file (only at last time step)
+         ELSE
+            CALL iom_context_finalize(      wxios_context          )
+         ENDIF
 !!gm         IF( .NOT. lk_trdmld )   lrst_oce = .FALSE.
 !!gm  not sure what to do here   ===>>>  ask to Sebastian
@@ -164,5 +190,4 @@
                nitrst = nstocklist( nrst_lst )
             ENDIF
-            lrst_oce = .FALSE.
       ENDIF
       !
@@ -267,5 +292,4 @@
       !
    END SUBROUTINE rst_read
-
    !!=====================================================================
 END MODULE restart
Index: branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/fldread.F90
===================================================================
--- branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/fldread.F90	(revision 8622)
+++ branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/fldread.F90	(revision 8630)
@@ -1343,5 +1343,4 @@
                &                  '   data type: '      ,       sdf(jf)%cltype      ,   &
                &                  '   land/sea mask:'   , TRIM( sdf(jf)%lsmname    )
-            call flush(numout)
          END DO
       ENDIF
Index: branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcapr.F90
===================================================================
--- branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcapr.F90	(revision 8622)
+++ branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcapr.F90	(revision 8630)
@@ -19,4 +19,5 @@
    USE iom             ! IOM library
    USE lib_mpp         ! MPP library
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -165,5 +166,7 @@
          IF(lwp) WRITE(numout,*) 'sbc_apr : ssh_ib written in ocean restart file at it= ', kt,' date= ', ndastp
          IF(lwp) WRITE(numout,*) '~~~~'
-         CALL iom_rstput( kt, nitrst, numrow, 'ssh_ibb' , ssh_ib )
+         IF( lwxios ) CALL iom_swap(      wxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'ssh_ibb' , ssh_ib, lxios = lwxios )
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
Index: branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcmod.F90
===================================================================
--- branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcmod.F90	(revision 8622)
+++ branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcmod.F90	(revision 8630)
@@ -58,4 +58,5 @@
 
    USE diurnal_bulk, ONLY:   ln_diurnal_only   ! diurnal SST diagnostic
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -485,11 +486,13 @@
             &                    'at it= ', kt,' date= ', ndastp
          IF(lwp) WRITE(numout,*) '~~~~'
-         CALL iom_rstput( kt, nitrst, numrow, 'utau_b' , utau )
-         CALL iom_rstput( kt, nitrst, numrow, 'vtau_b' , vtau )
-         CALL iom_rstput( kt, nitrst, numrow, 'qns_b'  , qns  )
+         IF( lwxios ) CALL iom_swap(      wxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'utau_b' , utau, lxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'vtau_b' , vtau, lxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'qns_b'  , qns, lxios = lwxios  )
          ! The 3D heat content due to qsr forcing is treated in traqsr
          ! CALL iom_rstput( kt, nitrst, numrow, 'qsr_b'  , qsr  )
-         CALL iom_rstput( kt, nitrst, numrow, 'emp_b'  , emp  )
-         CALL iom_rstput( kt, nitrst, numrow, 'sfx_b'  , sfx  )
+         CALL iom_rstput( kt, nitrst, numrow, 'emp_b'  , emp, lxios = lwxios  )
+         CALL iom_rstput( kt, nitrst, numrow, 'sfx_b'  , sfx, lxios = lwxios  )
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !                                                ! ---------------------------------------- !
Index: branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcrnf.F90
===================================================================
--- branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcrnf.F90	(revision 8622)
+++ branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcrnf.F90	(revision 8630)
@@ -28,4 +28,5 @@
    USE lib_mpp        ! MPP library
    USE wrk_nemo       ! Memory allocation
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -164,7 +165,9 @@
             &                    'at it= ', kt,' date= ', ndastp
          IF(lwp) WRITE(numout,*) '~~~~'
-         CALL iom_rstput( kt, nitrst, numrow, 'rnf_b' , rnf )
-         CALL iom_rstput( kt, nitrst, numrow, 'rnf_hc_b', rnf_tsc(:,:,jp_tem) )
-         CALL iom_rstput( kt, nitrst, numrow, 'rnf_sc_b', rnf_tsc(:,:,jp_sal) )
+         IF( lwxios ) CALL iom_swap(      wxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'rnf_b' , rnf, lxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'rnf_hc_b', rnf_tsc(:,:,jp_tem), lxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'rnf_sc_b', rnf_tsc(:,:,jp_sal), lxios = lwxios )
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
Index: branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcssm.F90
===================================================================
--- branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcssm.F90	(revision 8622)
+++ branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcssm.F90	(revision 8630)
@@ -22,4 +22,5 @@
    USE prtctl         ! Print control
    USE iom            ! IOM library
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -155,13 +156,15 @@
             IF(lwp) WRITE(numout,*) '~~~~~~~'
             zf_sbc = REAL( nn_fsbc, wp )
-            CALL iom_rstput( kt, nitrst, numrow, 'nn_fsbc', zf_sbc )    ! sbc frequency
-            CALL iom_rstput( kt, nitrst, numrow, 'ssu_m'  , ssu_m  )    ! sea surface mean fields
-            CALL iom_rstput( kt, nitrst, numrow, 'ssv_m'  , ssv_m  )
-            CALL iom_rstput( kt, nitrst, numrow, 'sst_m'  , sst_m  )
-            CALL iom_rstput( kt, nitrst, numrow, 'sss_m'  , sss_m  )
-            CALL iom_rstput( kt, nitrst, numrow, 'ssh_m'  , ssh_m  )
-            CALL iom_rstput( kt, nitrst, numrow, 'e3t_m'  , e3t_m  )
-            CALL iom_rstput( kt, nitrst, numrow, 'frq_m'  , frq_m  )
-            !
+            IF( lwxios ) CALL iom_swap(      wxios_context          )
+            CALL iom_rstput( kt, nitrst, numrow, 'nn_fsbc', zf_sbc, lxios = lwxios )    ! sbc frequency
+            CALL iom_rstput( kt, nitrst, numrow, 'ssu_m'  , ssu_m, lxios = lwxios  )    ! sea surface mean fields
+            CALL iom_rstput( kt, nitrst, numrow, 'ssv_m'  , ssv_m, lxios = lwxios  )
+            CALL iom_rstput( kt, nitrst, numrow, 'sst_m'  , sst_m, lxios = lwxios  )
+            CALL iom_rstput( kt, nitrst, numrow, 'sss_m'  , sss_m, lxios = lwxios  )
+            CALL iom_rstput( kt, nitrst, numrow, 'ssh_m'  , ssh_m, lxios = lwxios  )
+            CALL iom_rstput( kt, nitrst, numrow, 'e3t_m'  , e3t_m, lxios = lwxios  )
+            CALL iom_rstput( kt, nitrst, numrow, 'frq_m'  , frq_m, lxios = lwxios  )
+            !
+            IF( lwxios ) CALL iom_swap(      cxios_context          )
          ENDIF
          !
Index: branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traadv.F90
===================================================================
--- branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traadv.F90	(revision 8622)
+++ branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traadv.F90	(revision 8630)
@@ -45,5 +45,4 @@
    PUBLIC   tra_adv        ! routine called by step module
    PUBLIC   tra_adv_init   ! routine called by opa module
-
    !                            !!* Namelist namtra_adv *
    LOGICAL ::   ln_traadv_cen    ! centered scheme flag
@@ -300,4 +299,5 @@
       !
       CALL tra_adv_mle_init            !== initialisation of the Mixed Layer Eddy parametrisation (MLE)  ==!
+      lr_traadv_cen2 = ln_traadv_cen2
       !
    END SUBROUTINE tra_adv_init
Index: branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traqsr.F90
===================================================================
--- branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traqsr.F90	(revision 8622)
+++ branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traqsr.F90	(revision 8630)
@@ -36,4 +36,5 @@
    USE wrk_nemo       ! Memory Allocation
    USE timing         ! Timing
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -294,6 +295,8 @@
       !
       IF( lrst_oce ) THEN     ! write in the ocean restart file
-         CALL iom_rstput( kt, nitrst, numrow, 'qsr_hc_b'   , qsr_hc      )
-         CALL iom_rstput( kt, nitrst, numrow, 'fraqsr_1lev', fraqsr_1lev ) 
+         IF( lwxios ) CALL iom_swap(      wxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'qsr_hc_b'   , qsr_hc, lxios = lwxios      )
+         CALL iom_rstput( kt, nitrst, numrow, 'fraqsr_1lev', fraqsr_1lev, lxios = lwxios ) 
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
Index: branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/trasbc.F90
===================================================================
--- branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/trasbc.F90	(revision 8622)
+++ branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/trasbc.F90	(revision 8630)
@@ -34,4 +34,5 @@
    USE wrk_nemo       ! Memory Allocation
    USE timing         ! Timing
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -146,6 +147,8 @@
       !                  
       IF( lrst_oce ) THEN           !==  write sbc_tsc in the ocean restart file  ==!
-         CALL iom_rstput( kt, nitrst, numrow, 'sbc_hc_b', sbc_tsc(:,:,jp_tem) )
-         CALL iom_rstput( kt, nitrst, numrow, 'sbc_sc_b', sbc_tsc(:,:,jp_sal) )
+         IF( lwxios ) CALL iom_swap(      wxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'sbc_hc_b', sbc_tsc(:,:,jp_tem), lxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'sbc_sc_b', sbc_tsc(:,:,jp_sal), lxios = lwxios )
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
Index: branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/ZDF/zdftke.F90
===================================================================
--- branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/ZDF/zdftke.F90	(revision 8622)
+++ branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/ZDF/zdftke.F90	(revision 8630)
@@ -53,4 +53,5 @@
    USE timing         ! Timing
    USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)  
+   USE iom_def, ONLY : lwxios
 #if defined key_agrif
    USE agrif_opa_interp
@@ -880,10 +881,12 @@
         !                                   ! -------------------
         IF(lwp) WRITE(numout,*) '---- tke-rst ----'
-        CALL iom_rstput( kt, nitrst, numrow, 'en'   , en     )
-        CALL iom_rstput( kt, nitrst, numrow, 'avt'  , avt_k  )
-        CALL iom_rstput( kt, nitrst, numrow, 'avm'  , avm_k  )
-        CALL iom_rstput( kt, nitrst, numrow, 'avmu' , avmu_k )
-        CALL iom_rstput( kt, nitrst, numrow, 'avmv' , avmv_k )
-        CALL iom_rstput( kt, nitrst, numrow, 'dissl', dissl  )
+        IF( lwxios ) CALL iom_swap(      wxios_context          )
+        CALL iom_rstput( kt, nitrst, numrow, 'en'   , en    , lxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'avt'  , avt_k , lxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'avm'  , avm_k , lxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'avmu' , avmu_k, lxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'avmv' , avmv_k, lxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'dissl', dissl , lxios = lwxios )
+        IF( lwxios ) CALL iom_swap(      cxios_context          )
         !
      ENDIF
