Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/CONFIG/SHARED/namelist_ref
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/CONFIG/SHARED/namelist_ref	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/CONFIG/SHARED/namelist_ref	(revision 8801)
@@ -54,4 +54,5 @@
    nn_chunksz  =       0   !  chunksize (bytes) for NetCDF file (works only with iom_nf90 routines)
    ln_xios_read = .FALSE.  !  use XIOS to read restart file (only for a single file restart)
+   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_read_write_v2/NEMOGCM/NEMO/OPA_SRC/DIA/diahsb.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/DIA/diahsb.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/DIA/diahsb.F90	(revision 8801)
@@ -32,5 +32,6 @@
    USE timing          ! preformance summary
    USE wrk_nemo        ! work arrays
-   USE iom_def, ONLY : lxios_read
+   USE iom_def, ONLY : lwxios
+   USE iom_def, ONLY : lrxios
 
    IMPLICIT NONE
@@ -256,19 +257,19 @@
            IF(lwp) WRITE(numout,*) ' dia_hsb_rst at it= ', kt,' date= ', ndastp
            IF(lwp) WRITE(numout,*) '~~~~~~~'
-           CALL iom_get( numror, 'frc_v', frc_v, ldxios = lxios_read  )
-           CALL iom_get( numror, 'frc_t', frc_t, ldxios = lxios_read )
-           CALL iom_get( numror, 'frc_s', frc_s, ldxios = lxios_read )
+           CALL iom_get( numror, 'frc_v', frc_v, ldxios = lrxios  )
+           CALL iom_get( numror, 'frc_t', frc_t, ldxios = lrxios )
+           CALL iom_get( numror, 'frc_s', frc_s, ldxios = lrxios )
            IF( ln_linssh ) THEN
-              CALL iom_get( numror, 'frc_wn_t', frc_wn_t, ldxios = lxios_read )
-              CALL iom_get( numror, 'frc_wn_s', frc_wn_s, ldxios = lxios_read )
+              CALL iom_get( numror, 'frc_wn_t', frc_wn_t, ldxios = lrxios )
+              CALL iom_get( numror, 'frc_wn_s', frc_wn_s, ldxios = lrxios )
            ENDIF
-           CALL iom_get( numror, jpdom_autoglo, 'surf_ini', surf_ini, ldxios = lxios_read ) ! ice sheet coupling
-           CALL iom_get( numror, jpdom_autoglo, 'ssh_ini', ssh_ini(:,:), ldxios = lxios_read )
-           CALL iom_get( numror, jpdom_autoglo, 'e3t_ini', e3t_ini(:,:,:), ldxios = lxios_read )
-           CALL iom_get( numror, jpdom_autoglo, 'hc_loc_ini', hc_loc_ini(:,:,:), ldxios = lxios_read )
-           CALL iom_get( numror, jpdom_autoglo, 'sc_loc_ini', sc_loc_ini(:,:,:), ldxios = lxios_read )
+           CALL iom_get( numror, jpdom_autoglo, 'surf_ini', surf_ini, ldxios = lrxios ) ! ice sheet coupling
+           CALL iom_get( numror, jpdom_autoglo, 'ssh_ini', ssh_ini(:,:), ldxios = lrxios )
+           CALL iom_get( numror, jpdom_autoglo, 'e3t_ini', e3t_ini(:,:,:), ldxios = lrxios )
+           CALL iom_get( numror, jpdom_autoglo, 'hc_loc_ini', hc_loc_ini(:,:,:), ldxios = lrxios )
+           CALL iom_get( numror, jpdom_autoglo, 'sc_loc_ini', sc_loc_ini(:,:,:), ldxios = lrxios )
            IF( ln_linssh ) THEN
-              CALL iom_get( numror, jpdom_autoglo, 'ssh_hc_loc_ini', ssh_hc_loc_ini(:,:), ldxios = lxios_read )
-              CALL iom_get( numror, jpdom_autoglo, 'ssh_sc_loc_ini', ssh_sc_loc_ini(:,:), ldxios = lxios_read )
+              CALL iom_get( numror, jpdom_autoglo, 'ssh_hc_loc_ini', ssh_hc_loc_ini(:,:), ldxios = lrxios )
+              CALL iom_get( numror, jpdom_autoglo, 'ssh_sc_loc_ini', ssh_sc_loc_ini(:,:), ldxios = lrxios )
            ENDIF
        ELSE
@@ -309,22 +310,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(      cwxios_context          )
+        CALL iom_rstput( kt, nitrst, numrow, 'frc_v'   , frc_v, ldxios = lwxios)
+        CALL iom_rstput( kt, nitrst, numrow, 'frc_t'   , frc_t, ldxios = lwxios)
+        CALL iom_rstput( kt, nitrst, numrow, 'frc_s'   , frc_s, ldxios = 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, ldxios = lwxios)
+           CALL iom_rstput( kt, nitrst, numrow, 'frc_wn_s', frc_wn_s, ldxios = 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, ldxios = lwxios )      ! ice sheet coupling
+        CALL iom_rstput( kt, nitrst, numrow, 'ssh_ini', ssh_ini(:,:), ldxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'e3t_ini', e3t_ini(:,:,:), ldxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'hc_loc_ini', hc_loc_ini(:,:,:), ldxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'sc_loc_ini', sc_loc_ini(:,:,:), ldxios = 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(:,:), ldxios = lwxios )
+           CALL iom_rstput( kt, nitrst, numrow, 'ssh_sc_loc_ini', ssh_sc_loc_ini(:,:), ldxios = lwxios )
         ENDIF
         !
+        IF( lwxios ) CALL iom_swap(      cxios_context          )
      ENDIF
      !
@@ -368,4 +370,23 @@
       IF( .NOT. ln_diahsb )   RETURN
 
+      IF(lwxios) THEN
+! define variables in restart file when writing with XIOS
+        CALL iom_set_rstw_var_active('frc_v')
+        CALL iom_set_rstw_var_active('frc_t')
+        CALL iom_set_rstw_var_active('frc_s')
+        IF( ln_linssh ) THEN
+           CALL iom_set_rstw_var_active('frc_wn_t')
+           CALL iom_set_rstw_var_active('frc_wn_s')
+        ENDIF
+        CALL iom_set_rstw_var_active('surf_ini')
+        CALL iom_set_rstw_var_active('ssh_ini')
+        CALL iom_set_rstw_var_active('e3t_ini')
+        CALL iom_set_rstw_var_active('hc_loc_ini')
+        CALL iom_set_rstw_var_active('sc_loc_ini')
+        IF( ln_linssh ) THEN
+           CALL iom_set_rstw_var_active('ssh_hc_loc_ini')
+           CALL iom_set_rstw_var_active('ssh_sc_loc_ini')
+        ENDIF
+      ENDIF
       ! ------------------- !
       ! 1 - Allocate memory !
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/DOM/daymod.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/DOM/daymod.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/DOM/daymod.F90	(revision 8801)
@@ -33,5 +33,6 @@
    USE timing         ! Timing
    USE restart        ! restart
-   USE iom_def, ONLY : lxios_read
+   USE iom_def, ONLY : lwxios
+   USE iom_def, ONLY : lrxios
 
    IMPLICIT NONE
@@ -144,4 +145,12 @@
       CALL day( nit000 )
       !
+      IF( lwxios ) THEN
+! define variables in restart file when writing with XIOS
+          CALL iom_set_rstw_var_active('kt')
+          CALL iom_set_rstw_var_active('ndastp')
+          CALL iom_set_rstw_var_active('adatrj')
+          CALL iom_set_rstw_var_active('ntime')
+      ENDIF
+
    END SUBROUTINE day_init
 
@@ -319,5 +328,5 @@
          IF( iom_varid( numror, 'kt', ldstop = .FALSE. ) > 0 ) THEN
             ! Get Calendar informations
-            CALL iom_get( numror, 'kt', zkt, ldxios = lxios_read )   ! last time-step of previous run
+            CALL iom_get( numror, 'kt', zkt, ldxios = lrxios )   ! last time-step of previous run
             IF(lwp) THEN
                WRITE(numout,*) ' *** Info read in restart : '
@@ -338,8 +347,8 @@
             IF ( nrstdt == 2 ) THEN
                ! read the parameters corresponding to nit000 - 1 (last time step of previous run)
-               CALL iom_get( numror, 'ndastp', zndastp, ldxios = lxios_read )
+               CALL iom_get( numror, 'ndastp', zndastp, ldxios = lrxios )
                ndastp = NINT( zndastp )
-               CALL iom_get( numror, 'adatrj', adatrj, ldxios = lxios_read )
-	       CALL iom_get( numror, 'ntime', ktime, ldxios = lxios_read )
+               CALL iom_get( numror, 'adatrj', adatrj, ldxios = lrxios )
+	       CALL iom_get( numror, 'ntime', ktime, ldxios = lrxios )
 	       nn_time0=INT(ktime)
                ! calculate start time in hours and minutes
@@ -400,9 +409,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(      cwxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'kt'     , REAL( kt    , wp), ldxios = lwxios )   ! time-step
+         CALL iom_rstput( kt, nitrst, numrow, 'ndastp' , REAL( ndastp, wp), ldxios = lwxios )   ! date
+         CALL iom_rstput( kt, nitrst, numrow, 'adatrj' , adatrj, ldxios = 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), ldxios = lwxios ) ! time
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90	(revision 8801)
@@ -47,5 +47,7 @@
    USE wrk_nemo       ! Memory Allocation
    USE timing         ! Timing
-   USE iom_def, ONLY : lxios_read
+   USE iom_def, ONLY:lwxios, nxioso, rst_wfields ! write restart flag and output type
+   USE iom, ONLY : iom_set_rst_vars, iom_set_rstw_core
+   USE iom_def, ONLY : lrxios
 
    IMPLICIT NONE
@@ -129,4 +131,11 @@
       CALL dom_glo                     ! global domain versus local domain
       CALL dom_nam                     ! read namelist ( namrun, namdom )
+      !
+      IF( lwxios ) THEN
+!define names for restart write and set core output (restart.F90)
+         CALL iom_set_rst_vars(rst_wfields)
+         CALL iom_set_rstw_core()
+      ENDIF
+      !
       CALL dom_clo( cn_cfg, nn_cfg )   ! Closed seas and lake
       CALL dom_hgr                     ! Horizontal mesh
@@ -286,5 +295,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_xios_read
+         &             ln_cfmeta, ln_iscpl, ln_xios_read, nn_wxios
       NAMELIST/namdom/ ln_linssh, nn_closea, nn_msh, rn_isfhmin, rn_rdt, rn_atfp, ln_crs
 #if defined key_netcdf4
@@ -295,4 +304,5 @@
       !
       ln_xios_read = .false.            ! set in case ln_xios_read is not in namelist
+      nn_wxios = 0
       REWIND( numnam_ref )              ! Namelist namrun in reference namelist : Parameters of the run
       READ  ( numnam_ref, namrun, IOSTAT = ios, ERR = 901)
@@ -336,7 +346,8 @@
          WRITE(numout,*) '      IS coupling at the restart step ln_iscpl   = ', ln_iscpl
          WRITE(numout,*) '      READ restart for a single file using XIOS ln_xios_read =', ln_xios_read
-      IF( TRIM(Agrif_CFixed()) /= '0') THEN
-         WRITE(numout,*) '      READ restart for a single file using XIOS WILL not use AGRIF setting.'
-      ENDIF
+         IF( TRIM(Agrif_CFixed()) /= '0') &
+     &       WRITE(numout,*) '      READ restart for a single file using XIOS WILL not use AGRIF setting.'
+         IF( TRIM(Agrif_CFixed()) == '0' ) &
+     &      WRITE(numout,*) '      Write restart using XIOS        nn_wxios   = ', nn_wxios
       ENDIF
 
@@ -354,5 +365,5 @@
       neuler = nn_euler
       IF( TRIM(Agrif_CFixed()) == '0') THEN 
-       lxios_read = ln_xios_read.AND.ln_rstart
+       lrxios = ln_xios_read.AND.ln_rstart
       ENDIF
       IF ( neuler == 1 .AND. .NOT. ln_rstart ) THEN
@@ -422,4 +433,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. 
+         nxioso = 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_read_write_v2/NEMOGCM/NEMO/OPA_SRC/DOM/domvvl.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/DOM/domvvl.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/DOM/domvvl.F90	(revision 8801)
@@ -33,5 +33,5 @@
    USE wrk_nemo        ! Memory allocation
    USE timing          ! Timing
-   USE iom_def, ONLY : lxios_read
+   USE iom_def, ONLY : lrxios, lwxios
 
    IMPLICIT NONE
@@ -243,4 +243,22 @@
       ENDIF
       !
+      IF(lwxios) THEN
+! define variables in restart file when writing with XIOS
+         CALL iom_set_rstw_var_active('e3t_b')
+         CALL iom_set_rstw_var_active('e3t_n')
+         !                                           ! ----------------------- !
+         IF( ln_vvl_ztilde .OR. ln_vvl_layer ) THEN  ! z_tilde and layer cases !
+            !                                        ! ----------------------- !
+            CALL iom_set_rstw_var_active('tilde_e3t_b')
+            CALL iom_set_rstw_var_active('tilde_e3t_n')
+         END IF
+         !                                           ! -------------!    
+         IF( ln_vvl_ztilde ) THEN                    ! z_tilde case !
+            !                                        ! ------------ !
+            CALL iom_set_rstw_var_active('hdiv_lf')
+         ENDIF
+         !
+      ENDIF
+
       IF( nn_timing == 1 )  CALL timing_stop('dom_vvl_init')
       !
@@ -800,5 +818,5 @@
          IF( ln_rstart ) THEN                   !* Read the restart file
             CALL rst_read_open                  !  open the restart file if necessary
-            CALL iom_get( numror, jpdom_autoglo, 'sshn'   , sshn, ldxios = lxios_read    )
+            CALL iom_get( numror, jpdom_autoglo, 'sshn'   , sshn, ldxios = lrxios    )
             !
             id1 = iom_varid( numror, 'e3t_b', ldstop = .FALSE. )
@@ -811,6 +829,6 @@
             !                             ! --------- !
             IF( MIN( id1, id2 ) > 0 ) THEN       ! all required arrays exist
-               CALL iom_get( numror, jpdom_autoglo, 'e3t_b', e3t_b(:,:,:), ldxios = lxios_read )
-               CALL iom_get( numror, jpdom_autoglo, 'e3t_n', e3t_n(:,:,:), ldxios = lxios_read )
+               CALL iom_get( numror, jpdom_autoglo, 'e3t_b', e3t_b(:,:,:), ldxios = lrxios )
+               CALL iom_get( numror, jpdom_autoglo, 'e3t_n', e3t_n(:,:,:), ldxios = lrxios )
                ! needed to restart if land processor not computed 
                IF(lwp) write(numout,*) 'dom_vvl_rst : e3t_b and e3t_n found in restart files'
@@ -826,5 +844,5 @@
                IF(lwp) write(numout,*) 'e3t_n set equal to e3t_b.'
                IF(lwp) write(numout,*) 'neuler is forced to 0'
-               CALL iom_get( numror, jpdom_autoglo, 'e3t_b', e3t_b(:,:,:), ldxios = lxios_read )
+               CALL iom_get( numror, jpdom_autoglo, 'e3t_b', e3t_b(:,:,:), ldxios = lrxios )
                e3t_n(:,:,:) = e3t_b(:,:,:)
                neuler = 0
@@ -833,5 +851,5 @@
                IF(lwp) write(numout,*) 'e3t_b set equal to e3t_n.'
                IF(lwp) write(numout,*) 'neuler is forced to 0'
-               CALL iom_get( numror, jpdom_autoglo, 'e3t_n', e3t_n(:,:,:), ldxios = lxios_read )
+               CALL iom_get( numror, jpdom_autoglo, 'e3t_n', e3t_n(:,:,:), ldxios = lrxios )
                e3t_b(:,:,:) = e3t_n(:,:,:)
                neuler = 0
@@ -858,6 +876,6 @@
                !                          ! ----------------------- !
                IF( MIN( id3, id4 ) > 0 ) THEN  ! all required arrays exist
-                  CALL iom_get( numror, jpdom_autoglo, 'tilde_e3t_b', tilde_e3t_b(:,:,:), ldxios = lxios_read )
-                  CALL iom_get( numror, jpdom_autoglo, 'tilde_e3t_n', tilde_e3t_n(:,:,:), ldxios = lxios_read )
+                  CALL iom_get( numror, jpdom_autoglo, 'tilde_e3t_b', tilde_e3t_b(:,:,:), ldxios = lrxios )
+                  CALL iom_get( numror, jpdom_autoglo, 'tilde_e3t_n', tilde_e3t_n(:,:,:), ldxios = lrxios )
                ELSE                            ! one at least array is missing
                   tilde_e3t_b(:,:,:) = 0.0_wp
@@ -868,5 +886,5 @@
                   !                       ! ------------ !
                   IF( id5 > 0 ) THEN  ! required array exists
-                     CALL iom_get( numror, jpdom_autoglo, 'hdiv_lf', hdiv_lf(:,:,:), ldxios = lxios_read )
+                     CALL iom_get( numror, jpdom_autoglo, 'hdiv_lf', hdiv_lf(:,:,:), ldxios = lrxios )
                   ELSE                ! array is missing
                      hdiv_lf(:,:,:) = 0.0_wp
@@ -929,21 +947,23 @@
          !                                   ! ===================
          IF(lwp) WRITE(numout,*) '---- dom_vvl_rst ----'
+         IF( lwxios ) CALL iom_swap(      cwxios_context          )
          !                                           ! --------- !
          !                                           ! 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(:,:,:), ldxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'e3t_n', e3t_n(:,:,:), ldxios = 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(:,:,:), ldxios = lwxios)
+            CALL iom_rstput( kt, nitrst, numrow, 'tilde_e3t_n', tilde_e3t_n(:,:,:), ldxios = lwxios)
          END IF
          !                                           ! -------------!    
          IF( ln_vvl_ztilde ) THEN                    ! z_tilde case !
             !                                        ! ------------ !
-            CALL iom_rstput( kt, nitrst, numrow, 'hdiv_lf', hdiv_lf(:,:,:) )
-         ENDIF
-         !
+            CALL iom_rstput( kt, nitrst, numrow, 'hdiv_lf', hdiv_lf(:,:,:), ldxios = lwxios)
+         ENDIF
+         !
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/DOM/iscplrst.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/DOM/iscplrst.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/DOM/iscplrst.F90	(revision 8801)
@@ -25,5 +25,5 @@
    USE iscplini        ! ice sheet coupling: initialisation
    USE iscplhsb        ! ice sheet coupling: conservation
-   USE iom_def, ONLY : lxios_read
+   USE iom_def, ONLY : lrxios
 
    IMPLICIT NONE
@@ -65,12 +65,12 @@
 
       !! get restart variable
-      CALL iom_get( numror, jpdom_autoglo, 'tmask'  , ztmask_b, ldxios = lxios_read   ) ! need to extrapolate T/S
-      CALL iom_get( numror, jpdom_autoglo, 'umask'  , zumask_b, ldxios = lxios_read   ) ! need to correct barotropic velocity
-      CALL iom_get( numror, jpdom_autoglo, 'vmask'  , zvmask_b, ldxios = lxios_read   ) ! need to correct barotropic velocity
-      CALL iom_get( numror, jpdom_autoglo, 'smask'  , zsmask_b, ldxios = lxios_read   ) ! need to correct barotropic velocity
-      CALL iom_get( numror, jpdom_autoglo, 'e3t_n'  , ze3t_b(:,:,:), ldxios = lxios_read )  ! need to compute temperature correction
-      CALL iom_get( numror, jpdom_autoglo, 'e3u_n'  , ze3u_b(:,:,:), ldxios = lxios_read )  ! need to correct barotropic velocity
-      CALL iom_get( numror, jpdom_autoglo, 'e3v_n'  , ze3v_b(:,:,:), ldxios = lxios_read )  ! need to correct barotropic velocity
-      CALL iom_get( numror, jpdom_autoglo, 'gdepw_n', zdepw_b(:,:,:), ldxios = lxios_read ) ! need to interpol vertical profile (vvl)
+      CALL iom_get( numror, jpdom_autoglo, 'tmask'  , ztmask_b, ldxios = lrxios   ) ! need to extrapolate T/S
+      CALL iom_get( numror, jpdom_autoglo, 'umask'  , zumask_b, ldxios = lrxios   ) ! need to correct barotropic velocity
+      CALL iom_get( numror, jpdom_autoglo, 'vmask'  , zvmask_b, ldxios = lrxios   ) ! need to correct barotropic velocity
+      CALL iom_get( numror, jpdom_autoglo, 'smask'  , zsmask_b, ldxios = lrxios   ) ! need to correct barotropic velocity
+      CALL iom_get( numror, jpdom_autoglo, 'e3t_n'  , ze3t_b(:,:,:), ldxios = lrxios )  ! need to compute temperature correction
+      CALL iom_get( numror, jpdom_autoglo, 'e3u_n'  , ze3u_b(:,:,:), ldxios = lrxios )  ! need to correct barotropic velocity
+      CALL iom_get( numror, jpdom_autoglo, 'e3v_n'  , ze3v_b(:,:,:), ldxios = lrxios )  ! need to correct barotropic velocity
+      CALL iom_get( numror, jpdom_autoglo, 'gdepw_n', zdepw_b(:,:,:), ldxios = lrxios ) ! need to interpol vertical profile (vvl)
 
       !! read namelist
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90	(revision 8801)
@@ -56,5 +56,5 @@
    USE asminc          ! Assimilation increment
 #endif
-   USE iom_def, ONLY : lxios_read
+   USE iom_def, ONLY : lrxios, lwxios
 
 
@@ -1199,41 +1199,43 @@
       !
       IF( TRIM(cdrw) == 'READ' ) THEN
-         CALL iom_get( numror, jpdom_autoglo, 'ub2_b'  , ub2_b  (:,:), ldxios = lxios_read )   
-         CALL iom_get( numror, jpdom_autoglo, 'vb2_b'  , vb2_b  (:,:), ldxios = lxios_read ) 
+         CALL iom_get( numror, jpdom_autoglo, 'ub2_b'  , ub2_b  (:,:), ldxios = lrxios )   
+         CALL iom_get( numror, jpdom_autoglo, 'vb2_b'  , vb2_b  (:,:), ldxios = lrxios ) 
          IF( .NOT.ln_bt_av ) THEN
-            CALL iom_get( numror, jpdom_autoglo, 'sshbb_e'  , sshbb_e(:,:), ldxios = lxios_read )   
-            CALL iom_get( numror, jpdom_autoglo, 'ubb_e'    ,   ubb_e(:,:), ldxios = lxios_read )   
-            CALL iom_get( numror, jpdom_autoglo, 'vbb_e'    ,   vbb_e(:,:), ldxios = lxios_read )
-            CALL iom_get( numror, jpdom_autoglo, 'sshb_e'   ,  sshb_e(:,:), ldxios = lxios_read ) 
-            CALL iom_get( numror, jpdom_autoglo, 'ub_e'     ,    ub_e(:,:), ldxios = lxios_read )   
-            CALL iom_get( numror, jpdom_autoglo, 'vb_e'     ,    vb_e(:,:), ldxios = lxios_read )
+            CALL iom_get( numror, jpdom_autoglo, 'sshbb_e'  , sshbb_e(:,:), ldxios = lrxios )   
+            CALL iom_get( numror, jpdom_autoglo, 'ubb_e'    ,   ubb_e(:,:), ldxios = lrxios )   
+            CALL iom_get( numror, jpdom_autoglo, 'vbb_e'    ,   vbb_e(:,:), ldxios = lrxios )
+            CALL iom_get( numror, jpdom_autoglo, 'sshb_e'   ,  sshb_e(:,:), ldxios = lrxios ) 
+            CALL iom_get( numror, jpdom_autoglo, 'ub_e'     ,    ub_e(:,:), ldxios = lrxios )   
+            CALL iom_get( numror, jpdom_autoglo, 'vb_e'     ,    vb_e(:,:), ldxios = lrxios )
          ENDIF
 #if defined key_agrif
          ! Read time integrated fluxes
          IF ( .NOT.Agrif_Root() ) THEN
-            CALL iom_get( numror, jpdom_autoglo, 'ub2_i_b'  , ub2_i_b(:,:), ldxios = lxios_read )   
-            CALL iom_get( numror, jpdom_autoglo, 'vb2_i_b'  , vb2_i_b(:,:), ldxios = lxios_read )
+            CALL iom_get( numror, jpdom_autoglo, 'ub2_i_b'  , ub2_i_b(:,:), ldxios = lrxios )   
+            CALL iom_get( numror, jpdom_autoglo, 'vb2_i_b'  , vb2_i_b(:,:), ldxios = lrxios )
          ENDIF
 #endif
       !
       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(      cwxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'ub2_b'   , ub2_b  (:,:), ldxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'vb2_b'   , vb2_b  (:,:), ldxios = 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(:,:), ldxios = lwxios ) 
+            CALL iom_rstput( kt, nitrst, numrow, 'ubb_e'    ,   ubb_e(:,:), ldxios = lwxios )
+            CALL iom_rstput( kt, nitrst, numrow, 'vbb_e'    ,   vbb_e(:,:), ldxios = lwxios )
+            CALL iom_rstput( kt, nitrst, numrow, 'sshb_e'   ,  sshb_e(:,:), ldxios = lwxios )
+            CALL iom_rstput( kt, nitrst, numrow, 'ub_e'     ,    ub_e(:,:), ldxios = lwxios )
+            CALL iom_rstput( kt, nitrst, numrow, 'vb_e'     ,    vb_e(:,:), ldxios = 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(:,:), ldxios = lwxios )
+            CALL iom_rstput( kt, nitrst, numrow, 'vb2_i_b'  , vb2_i_b(:,:), ldxios = lwxios )
          ENDIF
 #endif
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
@@ -1323,4 +1325,26 @@
       CALL wrk_dealloc( jpi,jpj,   zcu )
       !
+      IF( lwxios ) THEN
+! define variables in restart file when writing with XIOS
+         CALL iom_set_rstw_var_active('ub2_b')
+         CALL iom_set_rstw_var_active('vb2_b')
+         !
+         IF (.NOT.ln_bt_av) THEN
+            CALL iom_set_rstw_var_active('sshbb_e')
+            CALL iom_set_rstw_var_active('ubb_e')
+            CALL iom_set_rstw_var_active('vbb_e')
+            CALL iom_set_rstw_var_active('sshb_e')
+            CALL iom_set_rstw_var_active('ub_e')
+            CALL iom_set_rstw_var_active('vb_e')
+         ENDIF
+#if defined key_agrif
+         ! Save time integrated fluxes
+         IF ( .NOT.Agrif_Root() ) THEN
+            CALL iom_set_rstw_var_active('ub2_i_b')
+            CALL iom_set_rstw_var_active('vb2_i_b')
+         ENDIF
+#endif
+      ENDIF
+
    END SUBROUTINE dyn_spg_ts_init
 
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/IOM/in_out_manager.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/IOM/in_out_manager.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/IOM/in_out_manager.F90	(revision 8801)
@@ -45,4 +45,5 @@
    INTEGER       ::   nn_chunksz       !: chunksize (bytes) for NetCDF file (works only with iom_nf90 routines)
    LOGICAL       ::   ln_xios_read     !: use xios to read single file restart
+   INTEGER       ::   nn_wxios         !: write resart using xios 0 - no, 1 - single, 2 - multiple file output
 
 #if defined key_netcdf4
@@ -152,4 +153,5 @@
    CHARACTER(lc) ::   cxios_context         !: context name used in xios
    CHARACTER(lc) ::   crxios_context         !: context name used in xios to read restart
+   CHARACTER(lc) ::   cwxios_context        !: context name used in xios to write restart file
 
    !!----------------------------------------------------------------------
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90	(revision 8801)
@@ -41,9 +41,10 @@
    USE dianam          ! build name of file
    USE xios
-   USE iom_def, ONLY : max_rst_fields, rst_fields
+   USE iom_def, ONLY : max_rst_fields, rst_rfields, rst_wfields, RST_FIELD
 # endif
    USE ioipsl, ONLY :  ju2ymds    ! for calendar
    USE crs             ! Grid coarsening
    USE lib_fortran 
+   USE diurnal_bulk, ONLY : ln_diurnal_only, ln_diurnal
 
    IMPLICIT NONE
@@ -65,5 +66,6 @@
    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_rst_vars, set_rstr_active, set_rst_context
+   PRIVATE iom_set_rst_context, iom_set_rstw_active, iom_set_rstr_active
+   PUBLIC iom_set_rstw_var_active, iom_set_rst_vars
 # endif
 
@@ -92,5 +94,5 @@
 CONTAINS
 
-   SUBROUTINE iom_init( cdname ) 
+   SUBROUTINE iom_init( cdname, fname ) 
       !!----------------------------------------------------------------------
       !!                     ***  ROUTINE   ***
@@ -100,9 +102,10 @@
       !!----------------------------------------------------------------------
       CHARACTER(len=*), INTENT(in)  :: cdname
+      CHARACTER(len=*), OPTIONAL, INTENT(in)  :: fname
 #if defined key_iomput
 
       TYPE(xios_duration) :: dtime    = xios_duration(0, 0, 0, 0, 0, 0)
       TYPE(xios_date)     :: start_date
-      CHARACTER(len=10) :: clname
+      CHARACTER(len=lc) :: clname
       INTEGER           :: ji, jkmin
       LOGICAL :: llrst_context              ! is context related to restart
@@ -117,5 +120,5 @@
       CALL xios_context_initialize(TRIM(clname), mpi_comm_opa)
       CALL iom_swap( cdname )
-      llrst_context =  (TRIM(cdname) == TRIM(crxios_context))
+      llrst_context =  (TRIM(cdname) == TRIM(crxios_context) .OR. TRIM(cdname) == TRIM(cwxios_context))
 
       ! Calendar type is now defined in xml file 
@@ -162,5 +165,5 @@
          CALL dom_grid_glo   ! Return to parent grid domain
          !
-         IF( ln_cfmeta .AND. TRIM(cdname) .NE. TRIM(crxios_context)) THEN   ! Add additional grid metadata
+         IF( ln_cfmeta .AND. .NOT. llrst_context) 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))
@@ -206,10 +209,16 @@
       ENDIF   
       ! automatic definitions of some of the xml attributs
-      IF( llrst_context ) THEN
-!set names of the fields in restart file IF using XIOS to read/write data
-       CALL set_rst_context()
-       CALL set_rst_vars()
+      IF( TRIM(cdname) == TRIM(crxios_context) ) THEN
+!set names of the fields in restart file IF using XIOS to read data
+       CALL iom_set_rst_context()
+       CALL iom_set_rst_vars(rst_rfields)
 !set which fields are to be read from restart file
-       CALL set_rstr_active()
+       CALL iom_set_rstr_active()
+      ELSE IF( TRIM(cdname) == TRIM(cwxios_context) ) THEN
+!set names of the fields in restart file IF using XIOS to write data
+       CALL iom_set_rst_context()
+       CALL iom_set_rst_vars(rst_wfields)
+!set which fields are to be read from restart file
+       CALL iom_set_rstw_active(fname)
       ELSE
        CALL set_xmlatt
@@ -229,112 +238,30 @@
    END SUBROUTINE iom_init
 
-   
-   SUBROUTINE set_rst_vars()
-!set names for variables in restart file
+   SUBROUTINE iom_set_rstw_var_active(field)
+   CHARACTER(len = *), INTENT(IN) :: field
    INTEGER :: i
-        rst_fields(:)%vname="NO_NAME";         rst_fields(:)%grid="NO_GRID"
-        i = 0
-        i = i + 1; rst_fields(i)%vname="rdt";            rst_fields(i)% grid="grid_scalar"
-        i = i + 1; rst_fields(i)%vname="un";             rst_fields(i)% grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="ub";             rst_fields(i)% grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="vn";             rst_fields(i)% grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="vb";             rst_fields(i)% grid="grid_N_3D"  
-        i = i + 1; rst_fields(i)%vname="tn";             rst_fields(i)% grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="tb";             rst_fields(i)% grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="sn";             rst_fields(i)% grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="sb";             rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="sshn";           rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="sshb";           rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="rhop";           rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="kt";             rst_fields(i)%grid="grid_scalar"
-        i = i + 1; rst_fields(i)%vname="ndastp";         rst_fields(i)%grid="grid_scalar"
-        i = i + 1; rst_fields(i)%vname="adatrj";         rst_fields(i)%grid="grid_scalar"
-        i = i + 1; rst_fields(i)%vname="utau_b";         rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="vtau_b";         rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="qns_b";          rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="emp_b";          rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="sfx_b";          rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="en" ;            rst_fields(i)%grid="grid_N_3D" 
-        i = i + 1; rst_fields(i)%vname="avt";            rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="avm";            rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="avmu";           rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="avmv";           rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="dissl";          rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="sbc_hc_b";       rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="sbc_sc_b";       rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="qsr_hc_b";       rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="fraqsr_1lev";    rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="greenland_icesheet_mass"
-                                               rst_fields(i)%grid="grid_scalar"
-        i = i + 1; rst_fields(i)%vname="greenland_icesheet_timelapsed"
-                                               rst_fields(i)%grid="grid_scalar"
-        i = i + 1; rst_fields(i)%vname="greenland_icesheet_mass_roc"
-                                               rst_fields(i)%grid="grid_scalar"
-        i = i + 1; rst_fields(i)%vname="antarctica_icesheet_mass"
-                                               rst_fields(i)%grid="grid_scalar"
-        i = i + 1; rst_fields(i)%vname="antarctica_icesheet_timelapsed"
-                                               rst_fields(i)%grid="grid_scalar"
-        i = i + 1; rst_fields(i)%vname="antarctica_icesheet_mass_roc"
-                                               rst_fields(i)%grid="grid_scalar"
-        i = i + 1; rst_fields(i)%vname="frc_v";          rst_fields(i)%grid="grid_scalar"
-        i = i + 1; rst_fields(i)%vname="frc_t";          rst_fields(i)%grid="grid_scalar"
-        i = i + 1; rst_fields(i)%vname="frc_s";          rst_fields(i)%grid="grid_scalar"
-        i = i + 1; rst_fields(i)%vname="frc_wn_t";       rst_fields(i)%grid="grid_scalar"
-        i = i + 1; rst_fields(i)%vname="frc_wn_s";       rst_fields(i)%grid="grid_scalar"
-        i = i + 1; rst_fields(i)%vname="ssh_ini";        rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="e3t_ini";        rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="hc_loc_ini";     rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="sc_loc_ini";     rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="ssh_hc_loc_ini"; rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="ssh_sc_loc_ini"; rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="tilde_e3t_b";    rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="tilde_e3t_n";    rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="hdiv_lf";        rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="ub2_b";          rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="vb2_b";          rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="sshbb_e";        rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="ubb_e";          rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="vbb_e";          rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="sshb_e";         rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="ub_e";           rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="vb_e";           rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="fwf_isf_b";      rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="isf_sc_b";       rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="isf_hc_b";       rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="ssh_ibb";        rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="rnf_b";          rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="rnf_hc_b";       rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="rnf_sc_b";       rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="nn_fsbc";        rst_fields(i)%grid="grid_scalar"
-        i = i + 1; rst_fields(i)%vname="ssu_m";          rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="ssv_m";          rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="sst_m";          rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="sss_m";          rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="ssh_m";          rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="e3t_m";          rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="frq_m";          rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="avmb";           rst_fields(i)%grid="grid_vector"
-        i = i + 1; rst_fields(i)%vname="avtb";           rst_fields(i)%grid="grid_vector"
-        i = i + 1; rst_fields(i)%vname="ub2_i_b";        rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="vb2_i_b";        rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="ntime";          rst_fields(i)%grid="grid_scalar"
-        i = i + 1; rst_fields(i)%vname="Dsst";           rst_fields(i)%grid="grid_scalar"
-        i = i + 1; rst_fields(i)%vname="tmask";          rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="umask";          rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="vmask";          rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="smask";          rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="gdepw_n";        rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="e3t_n";          rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="e3u_n";          rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="e3v_n";          rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="surf_ini";       rst_fields(i)%grid="grid_N"
-        i = i + 1; rst_fields(i)%vname="e3t_b";          rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="e3t_n";          rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="mxln";           rst_fields(i)%grid="grid_N_3D"
-        i = i + 1; rst_fields(i)%vname="e3t_m";          rst_fields(i)%grid="grid_N_3D"
-   END SUBROUTINE set_rst_vars
-
-
-   SUBROUTINE set_rstr_active()
+   LOGICAL :: llis_set
+
+   llis_set = .FALSE.
+
+   DO i = 1, max_rst_fields
+       IF(TRIM(rst_wfields(i)%vname) == field) THEN 
+          rst_wfields(i)%active = .TRUE.
+          llis_set = .TRUE.
+          EXIT
+       ENDIF
+   ENDDO
+!Warn if variable is not in defined in rst_wfields
+   IF(.NOT.llis_set) THEN
+      IF(lwp) THEN
+         write(numout,cform_err)
+         write(numout,*) 'iom_set_rstw_var_active: variable ', field ,' is available for writing but not defined' 
+      ENDIF
+        nstop = nstop + 1
+   ENDIF
+
+   END SUBROUTINE iom_set_rstw_var_active
+
+   SUBROUTINE iom_set_rstr_active()
 !sets enabled = .TRUE. for each field in restart file
    CHARACTER(len=256) :: rst_file
@@ -361,28 +288,219 @@
 !defin files for restart context
         DO i = 1, max_rst_fields
-         IF( TRIM(rst_fields(i)%vname) /= "NO_NAME") THEN
-           IF( iom_varid( numror, TRIM(rst_fields(i)%vname), ldstop = .FALSE. ) > 0 ) THEN
-                CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
-                SELECT CASE (TRIM(rst_fields(i)%grid))
+         IF( TRIM(rst_rfields(i)%vname) /= "NO_NAME") THEN
+           IF( iom_varid( numror, TRIM(rst_rfields(i)%vname), ldstop = .FALSE. ) > 0 ) THEN
+                CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_rfields(i)%vname))
+                SELECT CASE (TRIM(rst_rfields(i)%grid))
                  CASE ("grid_N_3D")
-                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_fields(i)%vname), &
+                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_rfields(i)%vname), &
                         domain_ref="grid_N", axis_ref="deptht", operation = "instant")
                  CASE ("grid_N")
-                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_fields(i)%vname), &
+                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_rfields(i)%vname), &
                         domain_ref="grid_N", operation = "instant") 
                 CASE ("grid_vector")
-                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_fields(i)%vname), &
+                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_rfields(i)%vname), &
                          axis_ref="deptht", operation = "instant")
                  CASE ("grid_scalar")
-                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_fields(i)%vname), &
+                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_rfields(i)%vname), &
                         scalar_ref = "grid_scalar", operation = "instant")
                 END SELECT
-                IF(lwp) WRITE(numout,*) 'XIOS read: ', TRIM(rst_fields(i)%vname), ' enabled in ', TRIM(rst_file)
+                IF(lwp) WRITE(numout,*) 'XIOS read: ', TRIM(rst_rfields(i)%vname), ' enabled in ', TRIM(rst_file)
            ENDIF
          ENDIF
         END DO
-   END SUBROUTINE set_rstr_active
-
-   SUBROUTINE set_rst_context( ) 
+   END SUBROUTINE iom_set_rstr_active
+
+   SUBROUTINE iom_set_rstw_core()
+!from restart.F90
+   CALL iom_set_rstw_var_active("rdt")
+   IF ( .NOT. ln_diurnal_only ) THEN
+        CALL iom_set_rstw_var_active('ub'  )
+        CALL iom_set_rstw_var_active('vb'  )
+        CALL iom_set_rstw_var_active('tb'  )
+        CALL iom_set_rstw_var_active('sb'  )
+        CALL iom_set_rstw_var_active('sshb')
+        !
+        CALL iom_set_rstw_var_active('un'  )
+        CALL iom_set_rstw_var_active('vn'  )
+        CALL iom_set_rstw_var_active('tn'  )
+        CALL iom_set_rstw_var_active('sn'  )
+        CALL iom_set_rstw_var_active('sshn')
+        CALL iom_set_rstw_var_active('rhop')
+     ! extra variable needed for the ice sheet coupling
+        IF ( ln_iscpl ) THEN
+             CALL iom_set_rstw_var_active('tmask')
+             CALL iom_set_rstw_var_active('umask')
+             CALL iom_set_rstw_var_active('vmask')
+             CALL iom_set_rstw_var_active('smask')
+             CALL iom_set_rstw_var_active('e3t_n')
+             CALL iom_set_rstw_var_active('e3u_n')
+             CALL iom_set_rstw_var_active('e3v_n')
+             CALL iom_set_rstw_var_active('gdepw_n')
+        END IF
+      ENDIF
+      IF(ln_diurnal) CALL iom_set_rstw_var_active('Dsst')
+!from trasbc.F90
+         CALL iom_set_rstw_var_active('sbc_hc_b')
+         CALL iom_set_rstw_var_active('sbc_sc_b')
+   END SUBROUTINE iom_set_rstw_core
+
+   SUBROUTINE iom_set_rst_vars(fields)
+!set names for variables in restart file
+   TYPE(RST_FIELD), INTENT(INOUT) :: fields(max_rst_fields)
+   INTEGER :: i
+        i = 0
+        i = i + 1; fields(i)%vname="rdt";            fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="un";             fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="ub";             fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="vn";             fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="vb";             fields(i)%grid="grid_N_3D"  
+        i = i + 1; fields(i)%vname="tn";             fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="tb";             fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="sn";             fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="sb";             fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="sshn";           fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="sshb";           fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="rhop";           fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="kt";             fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="ndastp";         fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="adatrj";         fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="utau_b";         fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="vtau_b";         fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="qns_b";          fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="emp_b";          fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="sfx_b";          fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="en" ;            fields(i)%grid="grid_N_3D" 
+        i = i + 1; fields(i)%vname="avt";            fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="avm";            fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="avmu";           fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="avmv";           fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="dissl";          fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="sbc_hc_b";       fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="sbc_sc_b";       fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="qsr_hc_b";       fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="fraqsr_1lev";    fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="greenland_icesheet_mass"
+                                               fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="greenland_icesheet_timelapsed"
+                                               fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="greenland_icesheet_mass_roc"
+                                               fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="antarctica_icesheet_mass"
+                                               fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="antarctica_icesheet_timelapsed"
+                                               fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="antarctica_icesheet_mass_roc"
+                                               fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="frc_v";          fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="frc_t";          fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="frc_s";          fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="frc_wn_t";       fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="frc_wn_s";       fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="ssh_ini";        fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="e3t_ini";        fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="hc_loc_ini";     fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="sc_loc_ini";     fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="ssh_hc_loc_ini"; fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="ssh_sc_loc_ini"; fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="tilde_e3t_b";    fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="tilde_e3t_n";    fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="hdiv_lf";        fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="ub2_b";          fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="vb2_b";          fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="sshbb_e";        fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="ubb_e";          fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="vbb_e";          fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="sshb_e";         fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="ub_e";           fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="vb_e";           fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="fwf_isf_b";      fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="isf_sc_b";       fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="isf_hc_b";       fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="ssh_ibb";        fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="rnf_b";          fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="rnf_hc_b";       fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="rnf_sc_b";       fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="nn_fsbc";        fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="ssu_m";          fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="ssv_m";          fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="sst_m";          fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="sss_m";          fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="ssh_m";          fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="e3t_m";          fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="frq_m";          fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="avmb";           fields(i)%grid="grid_vector"
+        i = i + 1; fields(i)%vname="avtb";           fields(i)%grid="grid_vector"
+        i = i + 1; fields(i)%vname="ub2_i_b";        fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="vb2_i_b";        fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="ntime";          fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="Dsst";           fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="tmask";          fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="umask";          fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="vmask";          fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="smask";          fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="gdepw_n";        fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="e3t_n";          fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="e3u_n";          fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="e3v_n";          fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="surf_ini";       fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="e3t_b";          fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="mxln";           fields(i)%grid="grid_N_3D"
+
+        IF( i-1 > max_rst_fields) THEN
+        IF(lwp) write(numout,*) 'E R R O R : iom_set_rst_vars SIZE of RST_FIELD array is too small'
+        nstop = nstop + 1
+        ENDIF
+
+   END SUBROUTINE iom_set_rst_vars
+
+
+   SUBROUTINE iom_set_rstw_active(cdrst_file)
+!sets enabled = .TRUE. for each field in restart file
+   CHARACTER(len=*) :: cdrst_file
+#if defined key_iomput
+   TYPE(xios_field) :: field_hdl
+   TYPE(xios_file) :: file_hdl
+   TYPE(xios_filegroup) :: filegroup_hdl
+   INTEGER :: i
+   CHARACTER(lc)  ::   clpath
+
+!set name of the restart file and enable available fields
+        IF(lwp) WRITE(numout,*) 'Setting restart filename (for XIOS write) to: ',cdrst_file
+        CALL xios_get_handle("file_definition", filegroup_hdl )
+        CALL xios_add_child(filegroup_hdl, file_hdl, 'wrestart')
+        IF(nxioso.eq.1) THEN 
+           CALL xios_set_file_attr( "wrestart", type="one_file", enabled=.TRUE.,& 
+                                    mode="write", output_freq=xios_timestep) 
+           if(lwp) write(numout,*) 'OPEN ', trim(cdrst_file), ' in one_file mode' 
+        ELSE  
+           CALL xios_set_file_attr( "wrestart", type="multiple_file", enabled=.TRUE.,& 
+                                    mode="write", output_freq=xios_timestep) 
+           if(lwp) write(numout,*) 'OPEN ', trim(cdrst_file), ' in multiple_file mode' 
+        ENDIF 
+        CALL xios_set_file_attr( "wrestart", name=trim(cdrst_file))
+!defin files for restart context
+        DO i = 1, max_rst_fields
+         IF( rst_wfields(i)%active ) THEN
+                CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_wfields(i)%vname))
+                SELECT CASE (TRIM(rst_wfields(i)%grid))
+                 CASE ("grid_N_3D")
+                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_wfields(i)%vname), &
+                        domain_ref="grid_N", axis_ref="deptht", prec = 8, operation = "instant")
+                 CASE ("grid_N")
+                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_wfields(i)%vname), &
+                        domain_ref="grid_N", prec = 8, operation = "instant") 
+                 CASE ("grid_vector")
+                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_wfields(i)%vname), &
+                         axis_ref="deptht", prec = 8, operation = "instant")
+                 CASE ("grid_scalar")
+                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_wfields(i)%vname), &
+                        scalar_ref = "grid_scalar", prec = 8, operation = "instant")
+                END SELECT
+         ENDIF
+        END DO
+#endif
+   END SUBROUTINE iom_set_rstw_active
+
+   SUBROUTINE iom_set_rst_context( ) 
 #if defined key_iomput
    TYPE(xios_domaingroup)            :: domaingroup_hdl 
@@ -407,5 +525,5 @@
      CALL xios_add_child(scalargroup_hdl, scalar_hdl, "grid_scalar") 
 #endif
-   END SUBROUTINE set_rst_context
+   END SUBROUTINE iom_set_rst_context
 
    SUBROUTINE iom_swap( cdname )
@@ -418,5 +536,4 @@
 #if defined key_iomput
       TYPE(xios_context) :: nemo_hdl
-
       IF( TRIM(Agrif_CFixed()) == '0' ) THEN
         CALL xios_get_handle(TRIM(cdname),nemo_hdl)
@@ -1363,5 +1480,5 @@
    !!                   INTERFACE iom_rstput
    !!----------------------------------------------------------------------
-   SUBROUTINE iom_rp0d( kt, kwrite, kiomid, cdvar, pvar, ktype )
+   SUBROUTINE iom_rp0d( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
       INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
       INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
@@ -1370,18 +1487,32 @@
       REAL(wp)        , INTENT(in)                         ::   pvar     ! written field
       INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
+      LOGICAL, OPTIONAL :: ldxios   ! xios write flag
+      LOGICAL :: llx                ! 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
+
+      llx = .FALSE.
+      IF(PRESENT(ldxios)) llx = ldxios
+      IF( llx ) 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 (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
+            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, ldxios )
       INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
       INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
@@ -1390,18 +1521,32 @@
       REAL(wp)        , INTENT(in), DIMENSION(          :) ::   pvar     ! written field
       INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
+      LOGICAL, OPTIONAL :: ldxios   ! xios write flag
+      LOGICAL :: llx                ! 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
+
+      llx = .FALSE.
+      IF(PRESENT(ldxios)) llx = ldxios
+      IF( llx ) 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 (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
+            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, ldxios )
       INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
       INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
@@ -1410,18 +1555,32 @@
       REAL(wp)        , INTENT(in), DIMENSION(:,    :    ) ::   pvar     ! written field
       INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
+      LOGICAL, OPTIONAL :: ldxios   ! xios write flag
+      LOGICAL :: llx
       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
+
+      llx = .FALSE.
+      IF(PRESENT(ldxios)) llx = ldxios
+      IF( llx ) 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 (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
+            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, ldxios )
       INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
       INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
@@ -1430,13 +1589,27 @@
       REAL(wp)        , INTENT(in),       DIMENSION(:,:,:) ::   pvar     ! written field
       INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
+      LOGICAL, OPTIONAL :: ldxios   ! xios write flag
+      LOGICAL :: llx                 ! 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
+
+      llx = .FALSE.
+      IF(PRESENT(ldxios)) llx = ldxios
+      IF( llx ) 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 (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
+            ENDIF
          ENDIF
       ENDIF
@@ -1616,6 +1789,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
@@ -1645,5 +1822,5 @@
       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.ldxios) 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 /)))  
 
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/IOM/iom_def.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/IOM/iom_def.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/IOM/iom_def.F90	(revision 8801)
@@ -44,10 +44,16 @@
    INTEGER, PARAMETER, PUBLIC ::   jpmax_digits =  5   !: maximum number of digits for the cpu number in the file name
 
+
 !$AGRIF_DO_NOT_TREAT
    INTEGER, PUBLIC            ::   iom_open_init = 0   !: used to initialize iom_file(:)%nfid to 0
+!XIOS write restart   
+   LOGICAL, PUBLIC            ::   lwxios          !: write single file restart using XIOS
+   INTEGER, PUBLIC            ::   nxioso          !: type of restart file when writing using XIOS 1 - single, 2 - multiple
 !XIOS read restart   
-   LOGICAL, PUBLIC            ::   lxios_read          !: read single file restart using XIOS
+   LOGICAL, PUBLIC            ::   lrxios          !: read single file restart using XIOS
    LOGICAL, PUBLIC            ::   lxios_sini = .FALSE. ! is restart in a single file
    LOGICAL, PUBLIC            ::   lxios_set  = .FALSE. 
+
+
 
    TYPE, PUBLIC ::   file_descriptor
@@ -70,14 +76,13 @@
    END TYPE file_descriptor
    TYPE(file_descriptor), DIMENSION(jpmax_files), PUBLIC ::   iom_file !: array containing the info for all opened files
-
    INTEGER, PARAMETER, PUBLIC                   :: max_rst_fields = 95  
-
+   TYPE, PUBLIC :: RST_FIELD  
+    CHARACTER(len=30) :: vname = "NO_NAME" ! names of variables in restart file
+    CHARACTER(len=30) :: grid = "NO_GRID"
+    LOGICAL           :: active =.FALSE. ! for restart write only: true - write field, false do not write field
+   END TYPE RST_FIELD
 !$AGRIF_END_DO_NOT_TREAT
 
-   TYPE, PUBLIC :: RST_FIELD  
-    CHARACTER(len=30) :: vname ! names of variables in restart file
-    CHARACTER(len=30) :: grid
-   END TYPE RST_FIELD
-   TYPE(RST_FIELD), PUBLIC :: rst_fields(max_rst_fields)
+   TYPE(RST_FIELD), PUBLIC :: rst_wfields(max_rst_fields), rst_rfields(max_rst_fields)
 
    !!=====================================================================
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/IOM/restart.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/IOM/restart.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/IOM/restart.F90	(revision 8801)
@@ -28,5 +28,6 @@
    USE iom             ! I/O module
    USE diurnal_bulk
-   
+   USE iom_def, ONLY : lwxios
+
    IMPLICIT NONE
    PRIVATE
@@ -61,4 +62,5 @@
       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
       !!----------------------------------------------------------------------
       !
@@ -94,15 +96,29 @@
             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 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
+               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          )
+            ENDIF
             lrst_oce = .TRUE.
          ENDIF
@@ -123,38 +139,41 @@
       INTEGER, INTENT(in) ::   kt   ! ocean time-step
       !!----------------------------------------------------------------------
-
-                     CALL iom_rstput( kt, nitrst, numrow, 'rdt'    , rdt       )   ! dynamics and tracer 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        )     ! 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, 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        )     ! 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, 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     ) ! need to extrapolate T/S
-                     CALL iom_rstput( kt, nitrst, numrow, 'umask'  , umask     ) ! need to correct barotropic velocity
-                     CALL iom_rstput( kt, nitrst, numrow, 'vmask'  , vmask     ) ! need to correct barotropic velocity
-                     CALL iom_rstput( kt, nitrst, numrow, 'smask'  , ssmask    ) ! need to correct barotropic velocity
-                     CALL iom_rstput( kt, nitrst, numrow, 'e3t_n', e3t_n(:,:,:) )   ! need to compute temperature correction
-                     CALL iom_rstput( kt, nitrst, numrow, 'e3u_n', e3u_n(:,:,:) )   ! need to compute bt conservation
-                     CALL iom_rstput( kt, nitrst, numrow, 'e3v_n', e3v_n(:,:,:) )   ! need to compute bt conservation
-                     CALL iom_rstput( kt, nitrst, numrow, 'gdepw_n', gdepw_n(:,:,:) ) ! need to compute extrapolation if vvl
+                     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    )  
-
+      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
-         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(      cwxios_context          )
+         ENDIF
 !!gm         IF( .NOT. lk_trdmld )   lrst_oce = .FALSE.
 !!gm  not sure what to do here   ===>>>  ask to Sebastian
@@ -164,5 +183,4 @@
                nitrst = nstocklist( nrst_lst )
             ENDIF
-            lrst_oce = .FALSE.
       ENDIF
       !
@@ -200,6 +218,6 @@
 ! can handle checking if variable is in the restart file (there will be no need to open
 ! restart)
-         IF(.NOT.lxios_set) lxios_read = lxios_read.AND.lxios_sini
-         IF( lxios_read) THEN
+         IF(.NOT.lxios_set) lrxios = lrxios.AND.lxios_sini
+         IF( lrxios) THEN
            crxios_context = 'nemo_rst'
          if(.NOT.lxios_set) then
@@ -209,5 +227,5 @@
          endif
          ENDIF
-         IF( TRIM(Agrif_CFixed()) /= '0' .AND. lxios_read) THEN
+         IF( TRIM(Agrif_CFixed()) /= '0' .AND. lrxios) THEN
             CALL iom_init( crxios_context )
             IF(lwp) WRITE(numout,*) 'Enable restart reading by XIOS for AGRIF'
@@ -231,4 +249,5 @@
       TYPE(xios_duration):: dtime
       integer::ni,nj,nk
+      REAL(wp), DIMENSION(jpi, jpj, jpk) :: w3d 
       !!----------------------------------------------------------------------
 
@@ -237,35 +256,36 @@
       ! 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 = lxios_read )
+         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 = lxios_read ) 
+      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'     , tsn(:,:,1,jp_tem), ldxios = lxios_read ) 
+         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 = lxios_read                )   ! before fields
-         CALL iom_get( numror, jpdom_autoglo, 'vb'     , vb, ldxios = lxios_read                )
-         CALL iom_get( numror, jpdom_autoglo, 'tb'     , tsb(:,:,:,jp_tem), ldxios = lxios_read )
-         CALL iom_get( numror, jpdom_autoglo, 'sb'     , tsb(:,:,:,jp_sal), ldxios = lxios_read )
-         CALL iom_get( numror, jpdom_autoglo, 'sshb'   , sshb, ldxios = lxios_read              )
+         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 = lxios_read )   ! now    fields
-      CALL iom_get( numror, jpdom_autoglo, 'vn'     , vn, ldxios = lxios_read )
-      CALL iom_get( numror, jpdom_autoglo, 'tn'     , tsn(:,:,:,jp_tem), ldxios = lxios_read )
-      CALL iom_get( numror, jpdom_autoglo, 'sn'     , tsn(:,:,:,jp_sal), ldxios = lxios_read )
-      CALL iom_get( numror, jpdom_autoglo, 'sshn'   , sshn, ldxios = lxios_read )
+      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 = lxios_read )   ! now    potential density
+         CALL iom_get( numror, jpdom_autoglo, 'rhop'   , rhop, ldxios = lrxios )   ! now    potential density
       ELSE
          CALL eos( tsn, rhd, rhop, gdept_n(:,:,:) )   
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/SBC/sbcapr.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/SBC/sbcapr.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/SBC/sbcapr.F90	(revision 8801)
@@ -19,4 +19,5 @@
    USE iom             ! IOM library
    USE lib_mpp         ! MPP library
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -65,5 +66,5 @@
       CHARACTER(len=100) ::  cn_dir   ! Root directory for location of ssr files
       TYPE(FLD_N)        ::  sn_apr   ! informations about the fields to be read
-      LOGICAL            ::  lxios_read ! read restart using XIOS?
+      LOGICAL            ::  lrxios ! read restart using XIOS?
       !!
       NAMELIST/namsbc_apr/ cn_dir, sn_apr, ln_ref_apr, rn_pref, ln_apr_obc
@@ -110,4 +111,7 @@
             CALL ctl_warn( 'sbc_apr: use inverse barometer ssh at open boundary ONLY requires ln_apr_dyn=T' )
       !
+      IF( lwxios ) THEN
+         CALL iom_set_rstw_var_active('ssh_ibb')
+      ENDIF
    END SUBROUTINE sbc_apr_init
 
@@ -153,5 +157,5 @@
          IF( ln_rstart .AND. iom_varid( numror, 'ssh_ibb', ldstop = .FALSE. ) > 0 ) THEN 
             IF(lwp) WRITE(numout,*) 'sbc_apr:   ssh_ibb read in the restart file'
-            CALL iom_get( numror, jpdom_autoglo, 'ssh_ibb', ssh_ibb, ldxios = lxios_read )   ! before inv. barometer ssh
+            CALL iom_get( numror, jpdom_autoglo, 'ssh_ibb', ssh_ibb, ldxios = lrxios )   ! before inv. barometer ssh
             !
          ELSE                                         !* no restart: set from nit000 values
@@ -166,5 +170,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(      cwxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'ssh_ibb' , ssh_ib, ldxios = lwxios )
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/SBC/sbcisf.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/SBC/sbcisf.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/SBC/sbcisf.F90	(revision 8801)
@@ -27,5 +27,5 @@
    USE timing          ! Timing
    USE lib_fortran     ! glob_sum
-   USE iom_def, ONLY : lxios_read
+   USE iom_def, ONLY : lrxios, lwxios
 
    IMPLICIT NONE
@@ -219,7 +219,7 @@
                  & iom_varid( numror, 'fwf_isf_b', ldstop = .FALSE. ) > 0 ) THEN
                IF(lwp) WRITE(numout,*) '          nit000-1 isf tracer content forcing fields read in the restart file'
-               CALL iom_get( numror, jpdom_autoglo, 'fwf_isf_b', fwfisf_b(:,:), ldxios = lxios_read )   ! before salt content isf_tsc trend
-               CALL iom_get( numror, jpdom_autoglo, 'isf_sc_b', risf_tsc_b(:,:,jp_sal), ldxios = lxios_read )   ! before salt content isf_tsc trend
-               CALL iom_get( numror, jpdom_autoglo, 'isf_hc_b', risf_tsc_b(:,:,jp_tem), ldxios = lxios_read )   ! before salt content isf_tsc trend
+               CALL iom_get( numror, jpdom_autoglo, 'fwf_isf_b', fwfisf_b(:,:), ldxios = lrxios )   ! before salt content isf_tsc trend
+               CALL iom_get( numror, jpdom_autoglo, 'isf_sc_b', risf_tsc_b(:,:,jp_sal), ldxios = lrxios )   ! before salt content isf_tsc trend
+               CALL iom_get( numror, jpdom_autoglo, 'isf_hc_b', risf_tsc_b(:,:,jp_tem), ldxios = lrxios )   ! before salt content isf_tsc trend
            ELSE
                fwfisf_b(:,:)    = fwfisf(:,:)
@@ -233,7 +233,9 @@
                &                    'at it= ', kt,' date= ', ndastp
             IF(lwp) WRITE(numout,*) '~~~~'
-            CALL iom_rstput( kt, nitrst, numrow, 'fwf_isf_b', fwfisf(:,:) )
-            CALL iom_rstput( kt, nitrst, numrow, 'isf_hc_b' , risf_tsc(:,:,jp_tem) )
-            CALL iom_rstput( kt, nitrst, numrow, 'isf_sc_b' , risf_tsc(:,:,jp_sal) )
+            IF( lwxios ) CALL iom_swap(      cwxios_context          )
+               CALL iom_rstput( kt, nitrst, numrow, 'fwf_isf_b', fwfisf(:,:), ldxios = lwxios )
+               CALL iom_rstput( kt, nitrst, numrow, 'isf_hc_b' , risf_tsc(:,:,jp_tem), ldxios = lwxios )
+               CALL iom_rstput( kt, nitrst, numrow, 'isf_sc_b' , risf_tsc(:,:,jp_sal), ldxios = lwxios )
+            IF( lwxios ) CALL iom_swap(      cxios_context          )
          ENDIF
          !
@@ -394,4 +396,11 @@
       END DO
 
+      IF( lwxios ) THEN
+          CALL iom_set_rstw_var_active('fwf_isf_b')
+          CALL iom_set_rstw_var_active('isf_hc_b')
+          CALL iom_set_rstw_var_active('isf_sc_b')
+      ENDIF
+
+
   END SUBROUTINE sbc_isf_init
 
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/SBC/sbcmod.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/SBC/sbcmod.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/SBC/sbcmod.F90	(revision 8801)
@@ -58,5 +58,5 @@
 
    USE diurnal_bulk, ONLY:   ln_diurnal_only   ! diurnal SST diagnostic
-   USE iom_def, ONLY : lxios_read
+   USE iom_def, ONLY : lrxios, lwxios
 
    IMPLICIT NONE
@@ -342,4 +342,14 @@
       IF( ln_wave     )   CALL sbc_wave_init              ! surface wave initialisation
       !
+      IF( lwxios ) THEN
+         CALL iom_set_rstw_var_active('utau_b')
+         CALL iom_set_rstw_var_active('vtau_b')
+         CALL iom_set_rstw_var_active('qns_b')
+         ! The 3D heat content due to qsr forcing is treated in traqsr
+         ! CALL iom_set_rstw_var_active('qsr_b')
+         CALL iom_set_rstw_var_active('emp_b')
+         CALL iom_set_rstw_var_active('sfx_b')
+      ENDIF
+
    END SUBROUTINE sbc_init
 
@@ -458,13 +468,13 @@
             & iom_varid( numror, 'utau_b', ldstop = .FALSE. ) > 0 ) THEN
             IF(lwp) WRITE(numout,*) '          nit000-1 surface forcing fields red in the restart file'
-            CALL iom_get( numror, jpdom_autoglo, 'utau_b', utau_b, ldxios = lxios_read )   ! before i-stress  (U-point)
-            CALL iom_get( numror, jpdom_autoglo, 'vtau_b', vtau_b, ldxios = lxios_read )   ! before j-stress  (V-point)
-            CALL iom_get( numror, jpdom_autoglo, 'qns_b' , qns_b, ldxios = lxios_read  )   ! before non solar heat flux (T-point)
+            CALL iom_get( numror, jpdom_autoglo, 'utau_b', utau_b, ldxios = lrxios )   ! before i-stress  (U-point)
+            CALL iom_get( numror, jpdom_autoglo, 'vtau_b', vtau_b, ldxios = lrxios )   ! before j-stress  (V-point)
+            CALL iom_get( numror, jpdom_autoglo, 'qns_b' , qns_b, ldxios = lrxios  )   ! before non solar heat flux (T-point)
             ! The 3D heat content due to qsr forcing is treated in traqsr
-            ! CALL iom_get( numror, jpdom_autoglo, 'qsr_b' , qsr_b, ldxios = lxios_read  ) ! before     solar heat flux (T-point)
-            CALL iom_get( numror, jpdom_autoglo, 'emp_b', emp_b, ldxios = lxios_read  )    ! before     freshwater flux (T-point)
+            ! CALL iom_get( numror, jpdom_autoglo, 'qsr_b' , qsr_b, ldxios = lrxios  ) ! before     solar heat flux (T-point)
+            CALL iom_get( numror, jpdom_autoglo, 'emp_b', emp_b, ldxios = lrxios  )    ! before     freshwater flux (T-point)
             ! To ensure restart capability with 3.3x/3.4 restart files    !! to be removed in v3.6
             IF( iom_varid( numror, 'sfx_b', ldstop = .FALSE. ) > 0 ) THEN
-               CALL iom_get( numror, jpdom_autoglo, 'sfx_b', sfx_b, ldxios = lxios_read )  ! before salt flux (T-point)
+               CALL iom_get( numror, jpdom_autoglo, 'sfx_b', sfx_b, ldxios = lrxios )  ! before salt flux (T-point)
             ELSE
                sfx_b (:,:) = sfx(:,:)
@@ -486,11 +496,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(      cwxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'utau_b' , utau, ldxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'vtau_b' , vtau, ldxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'qns_b'  , qns, ldxios = 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, ldxios = lwxios  )
+         CALL iom_rstput( kt, nitrst, numrow, 'sfx_b'  , sfx, ldxios = lwxios  )
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !                                                ! ---------------------------------------- !
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/SBC/sbcrnf.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/SBC/sbcrnf.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/SBC/sbcrnf.F90	(revision 8801)
@@ -28,5 +28,5 @@
    USE lib_mpp        ! MPP library
    USE wrk_nemo       ! Memory allocation
-   USE iom_def, ONLY : lxios_read
+   USE iom_def, ONLY : lrxios, lwxios
 
    IMPLICIT NONE
@@ -148,8 +148,8 @@
          IF( ln_rstart .AND.    &                               !* Restart: read in restart file
             & iom_varid( numror, 'rnf_b', ldstop = .FALSE. ) > 0 ) THEN
-            IF(lwp) WRITE(numout,*) '          nit000-1 runoff forcing fields red in the restart file', lxios_read
-            CALL iom_get( numror, jpdom_autoglo, 'rnf_b', rnf_b, ldxios = lxios_read )     ! before runoff
-            CALL iom_get( numror, jpdom_autoglo, 'rnf_hc_b', rnf_tsc_b(:,:,jp_tem), ldxios = lxios_read )   ! before heat content of runoff
-            CALL iom_get( numror, jpdom_autoglo, 'rnf_sc_b', rnf_tsc_b(:,:,jp_sal), ldxios = lxios_read )   ! before salinity content of runoff
+            IF(lwp) WRITE(numout,*) '          nit000-1 runoff forcing fields red in the restart file', lrxios
+            CALL iom_get( numror, jpdom_autoglo, 'rnf_b', rnf_b, ldxios = lrxios )     ! before runoff
+            CALL iom_get( numror, jpdom_autoglo, 'rnf_hc_b', rnf_tsc_b(:,:,jp_tem), ldxios = lrxios )   ! before heat content of runoff
+            CALL iom_get( numror, jpdom_autoglo, 'rnf_sc_b', rnf_tsc_b(:,:,jp_sal), ldxios = lrxios )   ! before salinity content of runoff
          ELSE                                                   !* no restart: set from nit000 values
             IF(lwp) WRITE(numout,*) '          nit000-1 runoff forcing fields set to nit000'
@@ -165,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(      cwxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'rnf_b' , rnf, ldxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'rnf_hc_b', rnf_tsc(:,:,jp_tem), ldxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'rnf_sc_b', rnf_tsc(:,:,jp_sal), ldxios = lwxios )
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
@@ -473,4 +475,10 @@
       ENDIF
       !
+      IF( lwxios ) THEN
+         CALL iom_set_rstw_var_active('rnf_b')
+         CALL iom_set_rstw_var_active('rnf_hc_b')
+         CALL iom_set_rstw_var_active('rnf_sc_b')
+      ENDIF
+
    END SUBROUTINE sbc_rnf_init
 
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/SBC/sbcssm.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/SBC/sbcssm.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/SBC/sbcssm.F90	(revision 8801)
@@ -22,5 +22,5 @@
    USE prtctl         ! Print control
    USE iom            ! IOM library
-   USE iom_def, ONLY : lxios_read
+   USE iom_def, ONLY : lrxios, lwxios
 
    IMPLICIT NONE
@@ -156,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(      cwxios_context          )
+            CALL iom_rstput( kt, nitrst, numrow, 'nn_fsbc', zf_sbc, ldxios = lwxios )    ! sbc frequency
+            CALL iom_rstput( kt, nitrst, numrow, 'ssu_m'  , ssu_m, ldxios = lwxios  )    ! sea surface mean fields
+            CALL iom_rstput( kt, nitrst, numrow, 'ssv_m'  , ssv_m, ldxios = lwxios  )
+            CALL iom_rstput( kt, nitrst, numrow, 'sst_m'  , sst_m, ldxios = lwxios  )
+            CALL iom_rstput( kt, nitrst, numrow, 'sss_m'  , sss_m, ldxios = lwxios  )
+            CALL iom_rstput( kt, nitrst, numrow, 'ssh_m'  , ssh_m, ldxios = lwxios  )
+            CALL iom_rstput( kt, nitrst, numrow, 'e3t_m'  , e3t_m, ldxios = lwxios  )
+            CALL iom_rstput( kt, nitrst, numrow, 'frq_m'  , frq_m, ldxios = lwxios  )
+            !
+            IF( lwxios ) CALL iom_swap(      cxios_context          )
          ENDIF
          !
@@ -207,15 +209,15 @@
          IF( ln_rstart .AND. iom_varid( numror, 'nn_fsbc', ldstop = .FALSE. ) > 0 ) THEN
             l_ssm_mean = .TRUE.
-            CALL iom_get( numror               , 'nn_fsbc', zf_sbc, ldxios = lxios_read )    ! sbc frequency of previous run
-            CALL iom_get( numror, jpdom_autoglo, 'ssu_m'  , ssu_m, ldxios = lxios_read  )    ! sea surface mean velocity    (U-point)
-            CALL iom_get( numror, jpdom_autoglo, 'ssv_m'  , ssv_m, ldxios = lxios_read  )    !   "         "    velocity    (V-point)
-            CALL iom_get( numror, jpdom_autoglo, 'sst_m'  , sst_m, ldxios = lxios_read  )    !   "         "    temperature (T-point)
-            CALL iom_get( numror, jpdom_autoglo, 'sss_m'  , sss_m, ldxios = lxios_read  )    !   "         "    salinity    (T-point)
-            CALL iom_get( numror, jpdom_autoglo, 'ssh_m'  , ssh_m, ldxios = lxios_read  )    !   "         "    height      (T-point)
-            CALL iom_get( numror, jpdom_autoglo, 'e3t_m'  , e3t_m, ldxios = lxios_read  )    ! 1st level thickness          (T-point)
-            CALL iom_get( numror, jpdom_autoglo, 'e3t_m', e3t_m, ldxios = lxios_read )
+            CALL iom_get( numror               , 'nn_fsbc', zf_sbc, ldxios = lrxios )    ! sbc frequency of previous run
+            CALL iom_get( numror, jpdom_autoglo, 'ssu_m'  , ssu_m, ldxios = lrxios  )    ! sea surface mean velocity    (U-point)
+            CALL iom_get( numror, jpdom_autoglo, 'ssv_m'  , ssv_m, ldxios = lrxios  )    !   "         "    velocity    (V-point)
+            CALL iom_get( numror, jpdom_autoglo, 'sst_m'  , sst_m, ldxios = lrxios  )    !   "         "    temperature (T-point)
+            CALL iom_get( numror, jpdom_autoglo, 'sss_m'  , sss_m, ldxios = lrxios  )    !   "         "    salinity    (T-point)
+            CALL iom_get( numror, jpdom_autoglo, 'ssh_m'  , ssh_m, ldxios = lrxios  )    !   "         "    height      (T-point)
+            CALL iom_get( numror, jpdom_autoglo, 'e3t_m'  , e3t_m, ldxios = lrxios  )    ! 1st level thickness          (T-point)
+            CALL iom_get( numror, jpdom_autoglo, 'e3t_m', e3t_m, ldxios = lrxios )
             ! fraction of solar net radiation absorbed in 1st T level
             IF( iom_varid( numror, 'frq_m', ldstop = .FALSE. ) > 0 ) THEN
-               CALL iom_get( numror, jpdom_autoglo, 'frq_m'  , frq_m, ldxios = lxios_read  )
+               CALL iom_get( numror, jpdom_autoglo, 'frq_m'  , frq_m, ldxios = lrxios  )
             ELSE
                frq_m(:,:) = 1._wp   ! default definition
@@ -253,4 +255,15 @@
       ENDIF
       !
+      IF( lwxios.AND.nn_fsbc > 1 ) THEN
+         CALL iom_set_rstw_var_active('nn_fsbc')
+         CALL iom_set_rstw_var_active('ssu_m')
+         CALL iom_set_rstw_var_active('ssv_m')
+         CALL iom_set_rstw_var_active('sst_m')
+         CALL iom_set_rstw_var_active('sss_m')
+         CALL iom_set_rstw_var_active('ssh_m')
+         CALL iom_set_rstw_var_active('e3t_m')
+         CALL iom_set_rstw_var_active('frq_m')
+      ENDIF
+
    END SUBROUTINE sbc_ssm_init
 
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/TRA/traqsr.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/TRA/traqsr.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/TRA/traqsr.F90	(revision 8801)
@@ -36,5 +36,5 @@
    USE wrk_nemo       ! Memory Allocation
    USE timing         ! Timing
-   USE iom_def, ONLY : lxios_read
+   USE iom_def, ONLY : lrxios, lwxios
    IMPLICIT NONE
    PRIVATE
@@ -139,5 +139,5 @@
             IF(lwp) WRITE(numout,*) '          nit000-1 qsr tracer content forcing field read in the restart file'
             z1_2 = 0.5_wp
-            CALL iom_get( numror, jpdom_autoglo, 'qsr_hc_b', qsr_hc_b, ldxios = lxios_read )   ! before heat content trend due to Qsr flux
+            CALL iom_get( numror, jpdom_autoglo, 'qsr_hc_b', qsr_hc_b, ldxios = lrxios )   ! before heat content trend due to Qsr flux
          ELSE                                           ! No restart or restart not found: Euler forward time stepping
             z1_2 = 1._wp
@@ -294,6 +294,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(      cwxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'qsr_hc_b'   , qsr_hc, ldxios = lwxios      )
+         CALL iom_rstput( kt, nitrst, numrow, 'fraqsr_1lev', fraqsr_1lev, ldxios = lwxios ) 
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
@@ -430,9 +432,13 @@
       ! 1st ocean level attenuation coefficient (used in sbcssm)
       IF( iom_varid( numror, 'fraqsr_1lev', ldstop = .FALSE. ) > 0 ) THEN
-         CALL iom_get( numror, jpdom_autoglo, 'fraqsr_1lev'  , fraqsr_1lev, ldxios = lxios_read  )
+         CALL iom_get( numror, jpdom_autoglo, 'fraqsr_1lev'  , fraqsr_1lev, ldxios = lrxios  )
       ELSE
          fraqsr_1lev(:,:) = 1._wp   ! default : no penetration
       ENDIF
       !
+      IF( lwxios ) THEN
+         CALL iom_set_rstw_var_active('qsr_hc_b')
+         CALL iom_set_rstw_var_active('fraqsr_1lev')
+      ENDIF
       IF( nn_timing == 1 )   CALL timing_stop('tra_qsr_init')
       !
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/TRA/trasbc.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/TRA/trasbc.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/TRA/trasbc.F90	(revision 8801)
@@ -34,5 +34,5 @@
    USE wrk_nemo       ! Memory Allocation
    USE timing         ! Timing
-   USE iom_def, ONLY : lxios_read
+   USE iom_def, ONLY : lrxios, lwxios
 
    IMPLICIT NONE
@@ -109,6 +109,6 @@
             zfact = 0.5_wp
             sbc_tsc(:,:,:) = 0._wp
-            CALL iom_get( numror, jpdom_autoglo, 'sbc_hc_b', sbc_tsc_b(:,:,jp_tem), ldxios = lxios_read )   ! before heat content sbc trend
-            CALL iom_get( numror, jpdom_autoglo, 'sbc_sc_b', sbc_tsc_b(:,:,jp_sal), ldxios = lxios_read )   ! before salt content sbc trend
+            CALL iom_get( numror, jpdom_autoglo, 'sbc_hc_b', sbc_tsc_b(:,:,jp_tem), ldxios = lrxios )   ! before heat content sbc trend
+            CALL iom_get( numror, jpdom_autoglo, 'sbc_sc_b', sbc_tsc_b(:,:,jp_sal), ldxios = lrxios )   ! before salt content sbc trend
          ELSE                                   ! No restart or restart not found: Euler forward time stepping
             zfact = 1._wp
@@ -147,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(      cwxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'sbc_hc_b', sbc_tsc(:,:,jp_tem), ldxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'sbc_sc_b', sbc_tsc(:,:,jp_sal), ldxios = lwxios )
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfgls.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfgls.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfgls.F90	(revision 8801)
@@ -34,5 +34,5 @@
    USE timing         ! Timing
    USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)  
-   USE iom_def, ONLY : lxios_read
+   USE iom_def, ONLY : lrxios, lwxios
 
    IMPLICIT NONE
@@ -1141,4 +1141,13 @@
       CALL gls_rst( nit000, 'READ' )   !* read or initialize all required files
       !
+      IF( lwxios ) THEN
+         CALL iom_set_rstw_var_active('en')
+         CALL iom_set_rstw_var_active('avt')
+         CALL iom_set_rstw_var_active('avm')
+         CALL iom_set_rstw_var_active('avmu')
+         CALL iom_set_rstw_var_active('avmv')
+         CALL iom_set_rstw_var_active('mxln')
+      ENDIF
+
       IF( nn_timing == 1 )  CALL timing_stop('zdf_gls_init')
       !
@@ -1176,10 +1185,10 @@
             !
             IF( MIN( id1, id2, id3, id4, id5, id6 ) > 0 ) THEN        ! all required arrays exist
-               CALL iom_get( numror, jpdom_autoglo, 'en'    , en, ldxios = lxios_read     )
-               CALL iom_get( numror, jpdom_autoglo, 'avt'   , avt, ldxios = lxios_read    )
-               CALL iom_get( numror, jpdom_autoglo, 'avm'   , avm, ldxios = lxios_read    )
-               CALL iom_get( numror, jpdom_autoglo, 'avmu'  , avmu, ldxios = lxios_read   )
-               CALL iom_get( numror, jpdom_autoglo, 'avmv'  , avmv, ldxios = lxios_read   )
-               CALL iom_get( numror, jpdom_autoglo, 'mxln'  , mxln, ldxios = lxios_read   )
+               CALL iom_get( numror, jpdom_autoglo, 'en'    , en, ldxios = lrxios     )
+               CALL iom_get( numror, jpdom_autoglo, 'avt'   , avt, ldxios = lrxios    )
+               CALL iom_get( numror, jpdom_autoglo, 'avm'   , avm, ldxios = lrxios    )
+               CALL iom_get( numror, jpdom_autoglo, 'avmu'  , avmu, ldxios = lrxios   )
+               CALL iom_get( numror, jpdom_autoglo, 'avmv'  , avmv, ldxios = lrxios   )
+               CALL iom_get( numror, jpdom_autoglo, 'mxln'  , mxln, ldxios = lrxios   )
             ELSE                        
                IF(lwp) WRITE(numout,*) ' ===>>>> : previous run without gls scheme, en and mxln computed by iterative loop'
@@ -1201,10 +1210,12 @@
          !                                   ! -------------------
          IF(lwp) WRITE(numout,*) '---- gls-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, 'mxln' , mxln   )
+         IF( lwxios ) CALL iom_swap(      cwxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'en'   , en, ldxios = lwxios      ) 
+         CALL iom_rstput( kt, nitrst, numrow, 'avt'  , avt_k, ldxios = lwxios  )
+         CALL iom_rstput( kt, nitrst, numrow, 'avm'  , avm_k, ldxios = lwxios  )
+         CALL iom_rstput( kt, nitrst, numrow, 'avmu' , avmu_k, ldxios = lwxios ) 
+         CALL iom_rstput( kt, nitrst, numrow, 'avmv' , avmv_k, ldxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'mxln' , mxln, ldxios = lwxios   )
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
          !
       ENDIF
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfini.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfini.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfini.F90	(revision 8801)
@@ -28,5 +28,5 @@
    USE iom             ! IOM library
    USE lib_mpp         ! distribued memory computing
-   USE iom_def, ONLY : lxios_read
+   USE iom_def, ONLY : lrxios
 
    IMPLICIT NONE
@@ -160,6 +160,6 @@
          ! file in traadv_cen2 end read here. 
          IF( iom_varid( numror, 'avmb', ldstop = .FALSE. ) > 0 ) THEN
-            CALL iom_get( numror, jpdom_unknown, 'avmb', avmb, ldxios = lxios_read )
-            CALL iom_get( numror, jpdom_unknown, 'avtb', avtb, ldxios = lxios_read )
+            CALL iom_get( numror, jpdom_unknown, 'avmb', avmb, ldxios = lrxios )
+            CALL iom_get( numror, jpdom_unknown, 'avtb', avtb, ldxios = lrxios )
          ENDIF
       ENDIF
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/ZDF/zdftke.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/ZDF/zdftke.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/ZDF/zdftke.F90	(revision 8801)
@@ -53,9 +53,10 @@
    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
    USE agrif_opa_update
 #endif
-   USE iom_def, ONLY : lxios_read
+   USE iom_def, ONLY : lrxios
 
    IMPLICIT NONE
@@ -815,4 +816,13 @@
       CALL tke_rst( nit000, 'READ' )  !* read or initialize all required files
       !
+      IF( lwxios ) THEN
+        CALL iom_set_rstw_var_active('en')
+        CALL iom_set_rstw_var_active('avt')
+        CALL iom_set_rstw_var_active('avm')
+        CALL iom_set_rstw_var_active('avmu')
+        CALL iom_set_rstw_var_active('avmv')
+        CALL iom_set_rstw_var_active('dissl')
+      ENDIF
+
    END SUBROUTINE zdf_tke_init
 
@@ -846,11 +856,11 @@
            !
            IF( id1 > 0 ) THEN                       ! 'en' exists
-              CALL iom_get( numror, jpdom_autoglo, 'en', en, ldxios = lxios_read )
+              CALL iom_get( numror, jpdom_autoglo, 'en', en, ldxios = lrxios )
               IF( MIN( id2, id3, id4, id5, id6 ) > 0 ) THEN        ! all required arrays exist
-                 CALL iom_get( numror, jpdom_autoglo, 'avt'  , avt, ldxios = lxios_read   )
-                 CALL iom_get( numror, jpdom_autoglo, 'avm'  , avm, ldxios = lxios_read   )
-                 CALL iom_get( numror, jpdom_autoglo, 'avmu' , avmu, ldxios = lxios_read  )
-                 CALL iom_get( numror, jpdom_autoglo, 'avmv' , avmv, ldxios = lxios_read  )
-                 CALL iom_get( numror, jpdom_autoglo, 'dissl', dissl, ldxios = lxios_read )
+                 CALL iom_get( numror, jpdom_autoglo, 'avt'  , avt, ldxios = lrxios   )
+                 CALL iom_get( numror, jpdom_autoglo, 'avm'  , avm, ldxios = lrxios   )
+                 CALL iom_get( numror, jpdom_autoglo, 'avmu' , avmu, ldxios = lrxios  )
+                 CALL iom_get( numror, jpdom_autoglo, 'avmv' , avmv, ldxios = lrxios  )
+                 CALL iom_get( numror, jpdom_autoglo, 'dissl', dissl, ldxios = lrxios )
               ELSE                                                 ! one at least array is missing
                  CALL tke_avn                                          ! compute avt, avm, avmu, avmv and dissl (approximation)
@@ -881,10 +891,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(      cwxios_context          )
+        CALL iom_rstput( kt, nitrst, numrow, 'en'   , en    , ldxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'avt'  , avt_k , ldxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'avm'  , avm_k , ldxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'avmu' , avmu_k, ldxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'avmv' , avmv_k, ldxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'dissl', dissl , ldxios = lwxios )
+        IF( lwxios ) CALL iom_swap(      cxios_context          )
         !
      ENDIF
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/step.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/step.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/step.F90	(revision 8801)
@@ -345,5 +345,5 @@
       IF( kstp == nitend .OR. indic < 0 ) THEN 
                       CALL iom_context_finalize(      cxios_context          ) ! needed for XIOS+AGRIF
-                      IF(lxios_read) CALL iom_context_finalize(      crxios_context          )
+                      IF(lrxios) CALL iom_context_finalize(      crxios_context          )
          IF( ln_crs ) CALL iom_context_finalize( trim(cxios_context)//"_crs" ) ! 
       ENDIF
Index: /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/SAS_SRC/daymod.F90
===================================================================
--- /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/SAS_SRC/daymod.F90	(revision 8800)
+++ /branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/SAS_SRC/daymod.F90	(revision 8801)
@@ -318,5 +318,5 @@
          IF( iom_varid( numror, 'kt', ldstop = .FALSE. ) > 0 ) THEN
             ! Get Calendar informations
-            CALL iom_get( numror, 'kt', zk, ldxios = lxios_read )   ! last time-step of previous run
+            CALL iom_get( numror, 'kt', zkt, ldxios = lxios_read )   ! last time-step of previous run
             IF(lwp) THEN
                WRITE(numout,*) ' *** Info read in restart : '
