Index: /branches/2017/dev_METO_2017/NEMOGCM/CONFIG/SHARED/namelist_ref
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/CONFIG/SHARED/namelist_ref	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/CONFIG/SHARED/namelist_ref	(revision 8987)
@@ -53,4 +53,6 @@
    ln_clobber  = .true.    !  clobber (overwrite) an existing file
    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_METO_2017/NEMOGCM/NEMO/OPA_SRC/DIA/diahsb.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/DIA/diahsb.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/DIA/diahsb.F90	(revision 8987)
@@ -255,19 +255,17 @@
            IF(lwp) WRITE(numout,*) ' dia_hsb_rst at it= ', kt,' date= ', ndastp
            IF(lwp) WRITE(numout,*) '~~~~~~~'
-           CALL iom_get( numror, 'frc_v', frc_v )
-           CALL iom_get( numror, 'frc_t', frc_t )
-           CALL iom_get( numror, 'frc_s', frc_s )
+           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 )
+           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, 'frc_wn_t', frc_wn_t )
-              CALL iom_get( numror, 'frc_wn_s', frc_wn_s )
-           ENDIF
-           CALL iom_get( numror, jpdom_autoglo, 'surf_ini', surf_ini ) ! ice sheet coupling
-           CALL iom_get( numror, jpdom_autoglo, 'ssh_ini', ssh_ini(:,:) )
-           CALL iom_get( numror, jpdom_autoglo, 'e3t_ini', e3t_ini(:,:,:) )
-           CALL iom_get( numror, jpdom_autoglo, 'hc_loc_ini', hc_loc_ini(:,:,:) )
-           CALL iom_get( numror, jpdom_autoglo, 'sc_loc_ini', sc_loc_ini(:,:,:) )
-           IF( ln_linssh ) THEN
-              CALL iom_get( numror, jpdom_autoglo, 'ssh_hc_loc_ini', ssh_hc_loc_ini(:,:) )
-              CALL iom_get( numror, jpdom_autoglo, 'ssh_sc_loc_ini', ssh_sc_loc_ini(:,:) )
+              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 )
+              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
        ELSE
@@ -308,22 +306,21 @@
         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)
+        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, 'frc_wn_t', frc_wn_t )
-           CALL iom_rstput( kt, nitrst, numrow, 'frc_wn_s', frc_wn_s )
-        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(:,:,:) )
-        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 )
+           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
         !
+        IF( lwxios ) CALL iom_swap(      cxios_context          )
      ENDIF
      !
@@ -367,4 +364,21 @@
       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')
+        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')
+           CALL iom_set_rstw_var_active('frc_wn_t')
+           CALL iom_set_rstw_var_active('frc_wn_s')
+        ENDIF
+      ENDIF
       ! ------------------- !
       ! 1 - Allocate memory !
Index: /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/DOM/daymod.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/DOM/daymod.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/DOM/daymod.F90	(revision 8987)
@@ -143,4 +143,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
 
@@ -318,5 +326,5 @@
          IF( iom_varid( numror, 'kt', ldstop = .FALSE. ) > 0 ) THEN
             ! Get Calendar informations
-            CALL iom_get( numror, 'kt', zkt )   ! 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 : '
@@ -337,8 +345,8 @@
             IF ( nrstdt == 2 ) THEN
                ! read the parameters corresponding to nit000 - 1 (last time step of previous run)
-               CALL iom_get( numror, 'ndastp', zndastp )
+               CALL iom_get( numror, 'ndastp', zndastp, ldxios = lrxios )
                ndastp = NINT( zndastp )
-               CALL iom_get( numror, 'adatrj', adatrj  )
-	       CALL iom_get( numror, 'ntime', ktime )
+               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
@@ -399,9 +407,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
-         !                                                                     ! the begining of the run [s]
-	 CALL iom_rstput( kt, nitrst, numrow, 'ntime'  , REAL( nn_time0, wp) ) ! time
+         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), ldxios = lwxios ) ! time
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
Index: /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90	(revision 8987)
@@ -61,5 +61,5 @@
 CONTAINS
 
-   SUBROUTINE dom_init
+   SUBROUTINE dom_init(cdstr)
       !!----------------------------------------------------------------------
       !!                  ***  ROUTINE dom_init  ***
@@ -79,4 +79,5 @@
       INTEGER ::   iconf = 0    ! local integers
       CHARACTER (len=64) ::   cform = "(A12, 3(A13, I7))" 
+      CHARACTER (len=*), INTENT(IN) :: cdstr                  ! model: NEMO or SAS. Determines core restart variables
       INTEGER , DIMENSION(jpi,jpj) ::   ik_top , ik_bot       ! top and bottom ocean level
       REAL(wp), DIMENSION(jpi,jpj) ::   z1_hu_0, z1_hv_0
@@ -128,4 +129,18 @@
       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(cdstr)
+      ENDIF
+!reset namelist for SAS
+      IF(cdstr == 'SAS') THEN
+         IF(lrxios) THEN
+               IF(lwp) write(numout,*) 'Disable reading restart file using XIOS for SAS'
+               lrxios = .FALSE.
+         ENDIF
+      ENDIF
+      !
       CALL dom_clo( cn_cfg, nn_cfg )   ! Closed seas and lake
       CALL dom_hgr                     ! Horizontal mesh
@@ -285,5 +300,5 @@
          &             nn_it000, nn_itend , nn_date0    , nn_time0     , nn_leapy  , nn_istate ,     &
          &             nn_stock, nn_write , ln_mskland  , ln_clobber   , nn_chunksz, nn_euler  ,     &
-         &             ln_cfmeta, ln_iscpl
+         &             ln_cfmeta, ln_iscpl, 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
@@ -293,4 +308,6 @@
       !!----------------------------------------------------------------------
       !
+      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)
@@ -333,4 +350,11 @@
          WRITE(numout,*) '      NetCDF chunksize (bytes)        nn_chunksz = ', nn_chunksz
          WRITE(numout,*) '      IS coupling at the restart step ln_iscpl   = ', ln_iscpl
+         IF( TRIM(Agrif_CFixed()) == '0' ) THEN
+            WRITE(numout,*) '      READ restart for a single file using XIOS ln_xios_read =', ln_xios_read
+            WRITE(numout,*) '      Write restart using XIOS        nn_wxios   = ', nn_wxios
+         ELSE
+            WRITE(numout,*) "      AGRIF: nn_wxios will be ingored. See setting for parent"
+            WRITE(numout,*) "      AGRIF: ln_xios_read will be ingored. See setting for parent"
+         ENDIF
       ENDIF
 
@@ -413,4 +437,11 @@
       rdt       = rn_rdt
 
+      IF( TRIM(Agrif_CFixed()) == '0' ) THEN
+         lrxios = ln_xios_read.AND.ln_rstart
+!set output file type for XIOS based on NEMO namelist 
+         IF (nn_wxios > 0) lwxios = .TRUE. 
+         nxioso = nn_wxios
+      ENDIF
+
 #if defined key_netcdf4
       !                             ! NetCDF 4 case   ("key_netcdf4" defined)
Index: /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/DOM/domvvl.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/DOM/domvvl.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/DOM/domvvl.F90	(revision 8987)
@@ -242,4 +242,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')
       !
@@ -799,5 +817,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    )
+            CALL iom_get( numror, jpdom_autoglo, 'sshn'   , sshn, ldxios = lrxios    )
             !
             id1 = iom_varid( numror, 'e3t_b', ldstop = .FALSE. )
@@ -810,6 +828,6 @@
             !                             ! --------- !
             IF( MIN( id1, id2 ) > 0 ) THEN       ! all required arrays exist
-               CALL iom_get( numror, jpdom_autoglo, 'e3t_b', e3t_b(:,:,:) )
-               CALL iom_get( numror, jpdom_autoglo, 'e3t_n', e3t_n(:,:,:) )
+               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'
@@ -825,5 +843,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(:,:,:) )
+               CALL iom_get( numror, jpdom_autoglo, 'e3t_b', e3t_b(:,:,:), ldxios = lrxios )
                e3t_n(:,:,:) = e3t_b(:,:,:)
                neuler = 0
@@ -832,5 +850,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(:,:,:) )
+               CALL iom_get( numror, jpdom_autoglo, 'e3t_n', e3t_n(:,:,:), ldxios = lrxios )
                e3t_b(:,:,:) = e3t_n(:,:,:)
                neuler = 0
@@ -857,6 +875,6 @@
                !                          ! ----------------------- !
                IF( MIN( id3, id4 ) > 0 ) THEN  ! all required arrays exist
-                  CALL iom_get( numror, jpdom_autoglo, 'tilde_e3t_b', tilde_e3t_b(:,:,:) )
-                  CALL iom_get( numror, jpdom_autoglo, 'tilde_e3t_n', tilde_e3t_n(:,:,:) )
+                  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
@@ -867,5 +885,5 @@
                   !                       ! ------------ !
                   IF( id5 > 0 ) THEN  ! required array exists
-                     CALL iom_get( numror, jpdom_autoglo, 'hdiv_lf', hdiv_lf(:,:,:) )
+                     CALL iom_get( numror, jpdom_autoglo, 'hdiv_lf', hdiv_lf(:,:,:), ldxios = lrxios )
                   ELSE                ! array is missing
                      hdiv_lf(:,:,:) = 0.0_wp
@@ -938,21 +956,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_METO_2017/NEMOGCM/NEMO/OPA_SRC/DOM/iscplrst.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/DOM/iscplrst.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/DOM/iscplrst.F90	(revision 8987)
@@ -64,12 +64,12 @@
 
       !! get restart variable
-      CALL iom_get( numror, jpdom_autoglo, 'tmask'  , ztmask_b   ) ! need to extrapolate T/S
-      CALL iom_get( numror, jpdom_autoglo, 'umask'  , zumask_b   ) ! need to correct barotropic velocity
-      CALL iom_get( numror, jpdom_autoglo, 'vmask'  , zvmask_b   ) ! need to correct barotropic velocity
-      CALL iom_get( numror, jpdom_autoglo, 'smask'  , zsmask_b   ) ! need to correct barotropic velocity
-      CALL iom_get( numror, jpdom_autoglo, 'e3t_n'  , ze3t_b(:,:,:) )  ! need to compute temperature correction
-      CALL iom_get( numror, jpdom_autoglo, 'e3u_n'  , ze3u_b(:,:,:) )  ! need to correct barotropic velocity
-      CALL iom_get( numror, jpdom_autoglo, 'e3v_n'  , ze3v_b(:,:,:) )  ! need to correct barotropic velocity
-      CALL iom_get( numror, jpdom_autoglo, 'gdepw_n', zdepw_b(:,:,:) ) ! 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_METO_2017/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90	(revision 8987)
@@ -59,5 +59,4 @@
    USE asminc          ! Assimilation increment
 #endif
-
 
    IMPLICIT NONE
@@ -1310,41 +1309,43 @@
       !
       IF( TRIM(cdrw) == 'READ' ) THEN
-         CALL iom_get( numror, jpdom_autoglo, 'ub2_b'  , ub2_b  (:,:) )   
-         CALL iom_get( numror, jpdom_autoglo, 'vb2_b'  , vb2_b  (:,:) ) 
+         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(:,:) )   
-            CALL iom_get( numror, jpdom_autoglo, 'ubb_e'    ,   ubb_e(:,:) )   
-            CALL iom_get( numror, jpdom_autoglo, 'vbb_e'    ,   vbb_e(:,:) )
-            CALL iom_get( numror, jpdom_autoglo, 'sshb_e'   ,  sshb_e(:,:) ) 
-            CALL iom_get( numror, jpdom_autoglo, 'ub_e'     ,    ub_e(:,:) )   
-            CALL iom_get( numror, jpdom_autoglo, 'vb_e'     ,    vb_e(:,:) )
+            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(:,:) )   
-            CALL iom_get( numror, jpdom_autoglo, 'vb2_i_b'  , vb2_i_b(:,:) )
+            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
       !
@@ -1434,4 +1435,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_METO_2017/NEMOGCM/NEMO/OPA_SRC/IOM/in_out_manager.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/IOM/in_out_manager.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/IOM/in_out_manager.F90	(revision 8987)
@@ -44,4 +44,6 @@
    LOGICAL       ::   ln_clobber       !: clobber (overwrite) an existing file
    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
@@ -150,4 +152,6 @@
    LOGICAL       ::   lsp_area = .TRUE.     !: to make a control print over a specific area
    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_METO_2017/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90	(revision 8987)
@@ -44,4 +44,6 @@
    USE ioipsl, ONLY :  ju2ymds    ! for calendar
    USE crs             ! Grid coarsening
+   USE lib_fortran 
+   USE diurnal_bulk, ONLY : ln_diurnal_only, ln_diurnal
 
    IMPLICIT NONE
@@ -63,4 +65,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 iom_set_rst_context, iom_set_rstw_active, iom_set_rstr_active
+   PUBLIC iom_set_rstw_var_active, iom_set_rst_vars
 # endif
 
@@ -89,5 +93,5 @@
 CONTAINS
 
-   SUBROUTINE iom_init( cdname ) 
+   SUBROUTINE iom_init( cdname, fname ) 
       !!----------------------------------------------------------------------
       !!                     ***  ROUTINE   ***
@@ -97,10 +101,12 @@
       !!----------------------------------------------------------------------
       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
       !
       REAL(wp), ALLOCATABLE, DIMENSION(:,:) :: zt_bnds, zw_bnds
@@ -113,5 +119,5 @@
       CALL xios_context_initialize(TRIM(clname), mpi_comm_opa)
       CALL iom_swap( cdname )
-
+      llrst_context =  (TRIM(cdname) == TRIM(crxios_context) .OR. TRIM(cdname) == TRIM(cwxios_context))
 
       ! Calendar type is now defined in xml file 
@@ -126,11 +132,11 @@
 
       ! horizontal grid definition
-      CALL set_scalar
+      IF(.NOT.llrst_context) CALL set_scalar
 
       IF( TRIM(cdname) == TRIM(cxios_context) ) THEN  
-         CALL set_grid( "T", glamt, gphit ) 
-         CALL set_grid( "U", glamu, gphiu )
-         CALL set_grid( "V", glamv, gphiv )
-         CALL set_grid( "W", glamt, gphit )
+         CALL set_grid( "T", glamt, gphit, .FALSE. ) 
+         CALL set_grid( "U", glamu, gphiu, .FALSE. )
+         CALL set_grid( "V", glamv, gphiv, .FALSE. )
+         CALL set_grid( "W", glamt, gphit, .FALSE. )
          CALL set_grid_znl( gphit )
          !
@@ -150,13 +156,13 @@
          CALL dom_grid_crs   ! Save the parent grid information  & Switch to coarse grid domain
          !
-         CALL set_grid( "T", glamt_crs, gphit_crs ) 
-         CALL set_grid( "U", glamu_crs, gphiu_crs ) 
-         CALL set_grid( "V", glamv_crs, gphiv_crs ) 
-         CALL set_grid( "W", glamt_crs, gphit_crs ) 
+         CALL set_grid( "T", glamt_crs, gphit_crs, .FALSE. ) 
+         CALL set_grid( "U", glamu_crs, gphiu_crs, .FALSE. ) 
+         CALL set_grid( "V", glamv_crs, gphiv_crs, .FALSE. ) 
+         CALL set_grid( "W", glamt_crs, gphit_crs, .FALSE. ) 
          CALL set_grid_znl( gphit_crs )
           !
          CALL dom_grid_glo   ! Return to parent grid domain
          !
-         IF( ln_cfmeta ) THEN   ! Add additional grid metadata
+         IF( ln_cfmeta .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))
@@ -171,35 +177,50 @@
 
       ! vertical grid definition
-      CALL iom_set_axis_attr( "deptht", gdept_1d )
-      CALL iom_set_axis_attr( "depthu", gdept_1d )
-      CALL iom_set_axis_attr( "depthv", gdept_1d )
-      CALL iom_set_axis_attr( "depthw", gdepw_1d )
-
-      ! Add vertical grid bounds
-      jkmin = MIN(2,jpk)  ! in case jpk=1 (i.e. sas2D)
-      zt_bnds(2,:        ) = gdept_1d(:)
-      zt_bnds(1,jkmin:jpk) = gdept_1d(1:jpkm1)
-      zt_bnds(1,1        ) = gdept_1d(1) - e3w_1d(1)
-      zw_bnds(1,:        ) = gdepw_1d(:)
-      zw_bnds(2,1:jpkm1  ) = gdepw_1d(jkmin:jpk)
-      zw_bnds(2,jpk:     ) = gdepw_1d(jpk) + e3t_1d(jpk)
-      CALL iom_set_axis_attr( "deptht", bounds=zt_bnds )
-      CALL iom_set_axis_attr( "depthu", bounds=zt_bnds )
-      CALL iom_set_axis_attr( "depthv", bounds=zt_bnds )
-      CALL iom_set_axis_attr( "depthw", bounds=zw_bnds )
+      IF(.NOT.llrst_context) THEN
+          CALL iom_set_axis_attr( "deptht", paxis = gdept_1d )
+          CALL iom_set_axis_attr( "depthu", paxis = gdept_1d )
+          CALL iom_set_axis_attr( "depthv", paxis = gdept_1d )
+          CALL iom_set_axis_attr( "depthw", paxis = gdepw_1d )
+
+          ! Add vertical grid bounds
+          jkmin = MIN(2,jpk)  ! in case jpk=1 (i.e. sas2D)
+          zt_bnds(2,:        ) = gdept_1d(:)
+          zt_bnds(1,jkmin:jpk) = gdept_1d(1:jpkm1)
+          zt_bnds(1,1        ) = gdept_1d(1) - e3w_1d(1)
+          zw_bnds(1,:        ) = gdepw_1d(:)
+          zw_bnds(2,1:jpkm1  ) = gdepw_1d(jkmin:jpk)
+          zw_bnds(2,jpk:     ) = gdepw_1d(jpk) + e3t_1d(jpk)
+          CALL iom_set_axis_attr( "deptht", bounds=zt_bnds )
+          CALL iom_set_axis_attr( "depthu", bounds=zt_bnds )
+          CALL iom_set_axis_attr( "depthv", bounds=zt_bnds )
+          CALL iom_set_axis_attr( "depthw", bounds=zw_bnds )
 
 
 # if defined key_floats
-      CALL iom_set_axis_attr( "nfloat", (/ (REAL(ji,wp), ji=1,nfloat) /) )
+          CALL iom_set_axis_attr( "nfloat", (/ (REAL(ji,wp), ji=1,nfloat) /) )
 # endif
 #if defined key_lim3 || defined key_lim2
-      CALL iom_set_axis_attr( "ncatice", (/ (REAL(ji,wp), ji=1,jpl) /) )
+          CALL iom_set_axis_attr( "ncatice", (/ (REAL(ji,wp), ji=1,jpl) /) )
 #endif
-      CALL iom_set_axis_attr( "icbcla", class_num )
-      CALL iom_set_axis_attr( "iax_20C", (/ REAL(20,wp) /) )
-      CALL iom_set_axis_attr( "iax_28C", (/ REAL(28,wp) /) )
-      
+          CALL iom_set_axis_attr( "icbcla", class_num )
+          CALL iom_set_axis_attr( "iax_20C", (/ REAL(20,wp) /) )
+          CALL iom_set_axis_attr( "iax_28C", (/ REAL(28,wp) /) )
+      ENDIF   
       ! automatic definitions of some of the xml attributs
-      CALL set_xmlatt
+      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 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
+      ENDIF
 
       ! end file definition
@@ -213,7 +234,324 @@
 
 #endif
-      
+
    END SUBROUTINE iom_init
 
+   SUBROUTINE iom_set_rstw_var_active(field)
+      !!---------------------------------------------------------------------
+      !!                   ***  SUBROUTINE  iom_set_rstw_var_active  ***
+      !!
+      !! ** Purpose :  enable variable in restart file when writing with XIOS 
+      !!---------------------------------------------------------------------
+   CHARACTER(len = *), INTENT(IN) :: field
+   INTEGER :: i
+   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()
+      !!---------------------------------------------------------------------
+      !!                   ***  SUBROUTINE  iom_set_rstr_active  ***
+      !!
+      !! ** Purpose :  define file name in XIOS context for reading restart file,
+      !!               enable variables present in restart file for reading with XIOS 
+      !!---------------------------------------------------------------------
+
+!sets enabled = .TRUE. for each field in restart file
+   CHARACTER(len=256) :: rst_file
+   TYPE(xios_field) :: field_hdl
+   TYPE(xios_file) :: file_hdl
+   TYPE(xios_filegroup) :: filegroup_hdl
+   INTEGER :: i
+   CHARACTER(lc)  ::   clpath
+
+        clpath = TRIM(cn_ocerst_indir)
+        IF( clpath(LEN_TRIM(clpath):) /= '/' ) clpath = TRIM(clpath) // '/'
+        IF( TRIM(Agrif_CFixed()) == '0' ) THEN
+           rst_file = TRIM(clpath)//TRIM(cn_ocerst_in)
+        ELSE
+           rst_file = TRIM(clpath)//'1_'//TRIM(cn_ocerst_in)
+        ENDIF
+!set name of the restart file and enable available fields
+        if(lwp) WRITE(numout,*) 'Setting restart filename (for XIOS) to: ',rst_file
+        CALL xios_get_handle("file_definition", filegroup_hdl )
+        CALL xios_add_child(filegroup_hdl, file_hdl, 'rrestart')
+        CALL xios_set_file_attr( "rrestart", name=trim(rst_file), type="one_file", &
+             par_access="collective", enabled=.TRUE., mode="read",                 &
+             output_freq=xios_timestep)
+!define variables for restart context
+        DO i = 1, max_rst_fields
+         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_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_rfields(i)%vname), &
+                        domain_ref="grid_N", operation = "instant") 
+                CASE ("grid_vector")
+                    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_rfields(i)%vname), &
+                        scalar_ref = "grid_scalar", operation = "instant")
+                END SELECT
+                IF(lwp) WRITE(numout,*) 'XIOS read: ', TRIM(rst_rfields(i)%vname), ' enabled in ', TRIM(rst_file)
+           ENDIF
+         ENDIF
+        END DO
+   END SUBROUTINE iom_set_rstr_active
+
+   SUBROUTINE iom_set_rstw_core(cdmdl)
+      !!---------------------------------------------------------------------
+      !!                   ***  SUBROUTINE  iom_set_rstw_core  ***
+      !!
+      !! ** Purpose :  set variables which are always in restart file 
+      !!---------------------------------------------------------------------
+   CHARACTER (len=*), INTENT (IN) :: cdmdl ! model OPA or SAS
+   
+   IF(cdmdl == "OPA") THEN
+!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')
+   ENDIF
+   END SUBROUTINE iom_set_rstw_core
+
+   SUBROUTINE iom_set_rst_vars(fields)
+      !!---------------------------------------------------------------------
+      !!                   ***  SUBROUTINE  iom_set_rstr_active  ***
+      !!
+      !! ** Purpose :  Fill array fields with the information about all 
+      !!               possible variables and corresponding grids definition 
+      !!               for reading/writing restart with XIOS
+      !!---------------------------------------------------------------------
+   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="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)
+      !!---------------------------------------------------------------------
+      !!                   ***  SUBROUTINE  iom_set_rstr_active  ***
+      !!
+      !! ** Purpose :  define file name in XIOS context for writing restart
+      !!               enable variables present in restart file for writing
+      !!---------------------------------------------------------------------
+!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( ) 
+     !!---------------------------------------------------------------------
+      !!                   ***  SUBROUTINE  iom_set_rstr_active  ***
+      !!
+      !! ** Purpose : Define domain, axis and grid for restart (read/write) 
+      !!              context 
+      !!               
+      !!---------------------------------------------------------------------
+#if defined key_iomput
+   TYPE(xios_domaingroup)            :: domaingroup_hdl 
+   TYPE(xios_domain)                 :: domain_hdl 
+   TYPE(xios_axisgroup)              :: axisgroup_hdl 
+   TYPE(xios_axis)                   :: axis_hdl 
+   TYPE(xios_scalar)                 :: scalar_hdl 
+   TYPE(xios_scalargroup)            :: scalargroup_hdl 
+
+     CALL xios_get_handle("domain_definition",domaingroup_hdl) 
+     CALL xios_add_child(domaingroup_hdl, domain_hdl, "grid_N") 
+     CALL set_grid("N", glamt, gphit, .TRUE.) 
+ 
+     CALL xios_get_handle("axis_definition",axisgroup_hdl) 
+     CALL xios_add_child(axisgroup_hdl, axis_hdl, "deptht") 
+!AGRIF fails to compile when unit= is in call to xios_set_axis_attr
+!    CALL xios_set_axis_attr( "deptht", long_name="Vertical levels",  unit="m", positive="down") 
+     CALL xios_set_axis_attr( "deptht", long_name="Vertical levels in meters", positive="down")
+     CALL iom_set_axis_attr( "deptht", paxis = gdept_1d ) 
+
+     CALL xios_get_handle("scalar_definition", scalargroup_hdl) 
+     CALL xios_add_child(scalargroup_hdl, scalar_hdl, "grid_scalar") 
+#endif
+   END SUBROUTINE iom_set_rst_context
 
    SUBROUTINE iom_swap( cdname )
@@ -226,5 +564,4 @@
 #if defined key_iomput
       TYPE(xios_context) :: nemo_hdl
-
       IF( TRIM(Agrif_CFixed()) == '0' ) THEN
         CALL xios_get_handle(TRIM(cdname),nemo_hdl)
@@ -347,4 +684,6 @@
             icnt = icnt + 1
          END DO
+      ELSE
+         lxios_sini = .TRUE.
       ENDIF
       IF( llwrt ) THEN
@@ -530,9 +869,10 @@
    !!                   INTERFACE iom_get
    !!----------------------------------------------------------------------
-   SUBROUTINE iom_g0d( kiomid, cdvar, pvar, ktime )
+   SUBROUTINE iom_g0d( kiomid, cdvar, pvar, ktime, ldxios )
       INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
       CHARACTER(len=*), INTENT(in   )                 ::   cdvar     ! Name of the variable
       REAL(wp)        , INTENT(  out)                 ::   pvar      ! read field
       INTEGER         , INTENT(in   ),     OPTIONAL   ::   ktime     ! record number
+      LOGICAL         , INTENT(in   ),     OPTIONAL   ::   ldxios    ! use xios to read restart
       !
       INTEGER                                         ::   idvar     ! variable id
@@ -542,30 +882,41 @@
       CHARACTER(LEN=100)                              ::   clname    ! file name
       CHARACTER(LEN=1)                                ::   cldmspc   !
-      !
-      itime = 1
-      IF( PRESENT(ktime) ) itime = ktime
-      !
-      clname = iom_file(kiomid)%name
-      clinfo = '          iom_g0d, file: '//trim(clname)//', var: '//trim(cdvar)
-      !
-      IF( kiomid > 0 ) THEN
-         idvar = iom_varid( kiomid, cdvar )
-         IF( iom_file(kiomid)%nfid > 0 .AND. idvar > 0 ) THEN
-            idmspc = iom_file ( kiomid )%ndims( idvar )
-            IF( iom_file(kiomid)%luld(idvar) )  idmspc = idmspc - 1
-            WRITE(cldmspc , fmt='(i1)') idmspc
-            IF( idmspc > 0 )  CALL ctl_stop( TRIM(clinfo), 'When reading to a 0D array, we do not accept data', &
-                                 &                         'with 1 or more spatial dimensions: '//cldmspc//' were found.' , &
-                                 &                         'Use ncwa -a to suppress the unnecessary dimensions' )
-            SELECT CASE (iom_file(kiomid)%iolib)
-            CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, pvar, itime )
-            CASE DEFAULT
-               CALL ctl_stop( 'iom_g0d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
-            END SELECT
-         ENDIF
+      LOGICAL                                         ::   llxios
+      !
+      llxios = .FALSE.
+      IF( PRESENT(ldxios) ) llxios = ldxios
+
+      IF(.NOT.llxios) THEN  ! read data using default library
+         itime = 1
+         IF( PRESENT(ktime) ) itime = ktime
+         !
+         clname = iom_file(kiomid)%name
+         clinfo = '          iom_g0d, file: '//trim(clname)//', var: '//trim(cdvar)
+         !
+         IF( kiomid > 0 ) THEN
+            idvar = iom_varid( kiomid, cdvar )
+            IF( iom_file(kiomid)%nfid > 0 .AND. idvar > 0 ) THEN
+               idmspc = iom_file ( kiomid )%ndims( idvar )
+               IF( iom_file(kiomid)%luld(idvar) )  idmspc = idmspc - 1
+               WRITE(cldmspc , fmt='(i1)') idmspc
+               IF( idmspc > 0 )  CALL ctl_stop( TRIM(clinfo), 'When reading to a 0D array, we do not accept data', &
+                                    &                         'with 1 or more spatial dimensions: '//cldmspc//' were found.' , &
+                                    &                         'Use ncwa -a to suppress the unnecessary dimensions' )
+               SELECT CASE (iom_file(kiomid)%iolib)
+               CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, pvar, itime )
+               CASE DEFAULT
+                  CALL ctl_stop( 'iom_g0d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
+               END SELECT
+            ENDIF
+         ENDIF
+      ELSE
+         IF(lwp) WRITE(numout,*) 'XIOS RST READ (0D): ', trim(cdvar)
+         CALL iom_swap( TRIM(crxios_context) )
+         CALL xios_recv_field( trim(cdvar), pvar)
+         CALL iom_swap( TRIM(cxios_context) )
       ENDIF
    END SUBROUTINE iom_g0d
 
-   SUBROUTINE iom_g1d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount )
+   SUBROUTINE iom_g1d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, ldxios )
       INTEGER         , INTENT(in   )                         ::   kiomid    ! Identifier of the file
       INTEGER         , INTENT(in   )                         ::   kdom      ! Type of domain to be read
@@ -575,12 +926,14 @@
       INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kstart    ! start axis position of the reading 
       INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kcount    ! number of points in each axis
+      LOGICAL         , INTENT(in   ),               OPTIONAL ::   ldxios    ! read data using XIOS
       !
       IF( kiomid > 0 ) THEN
          IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r1d=pvar,   &
-              &                                                     ktime=ktime, kstart=kstart, kcount=kcount )
+              &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
+              &                                                     ldxios=ldxios )
       ENDIF
    END SUBROUTINE iom_g1d
 
-   SUBROUTINE iom_g2d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr )
+   SUBROUTINE iom_g2d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr, ldxios)
       INTEGER         , INTENT(in   )                           ::   kiomid    ! Identifier of the file
       INTEGER         , INTENT(in   )                           ::   kdom      ! Type of domain to be read
@@ -594,13 +947,14 @@
                                                                                ! called open_ocean_jstart to set the start
                                                                                ! value for the 2nd dimension (netcdf only)
+      LOGICAL         , INTENT(in   ),                 OPTIONAL ::   ldxios    ! read data using XIOS
       !
       IF( kiomid > 0 ) THEN
          IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r2d=pvar,   &
               &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
-              &                                                     lrowattr=lrowattr )
+              &                                                     lrowattr=lrowattr,  ldxios=ldxios)
       ENDIF
    END SUBROUTINE iom_g2d
 
-   SUBROUTINE iom_g3d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr )
+   SUBROUTINE iom_g3d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr, ldxios )
       INTEGER         , INTENT(in   )                             ::   kiomid    ! Identifier of the file
       INTEGER         , INTENT(in   )                             ::   kdom      ! Type of domain to be read
@@ -614,9 +968,10 @@
                                                                                  ! called open_ocean_jstart to set the start
                                                                                  ! value for the 2nd dimension (netcdf only)
+      LOGICAL         , INTENT(in   ),                   OPTIONAL ::   ldxios    ! read data using XIOS
       !
       IF( kiomid > 0 ) THEN
          IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r3d=pvar,   &
               &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
-              &                                                     lrowattr=lrowattr )
+              &                                                     lrowattr=lrowattr, ldxios=ldxios )
       ENDIF
    END SUBROUTINE iom_g3d
@@ -626,5 +981,5 @@
          &                  pv_r1d, pv_r2d, pv_r3d,   &
          &                  ktime , kstart, kcount,   &
-         &                  lrowattr                )
+         &                  lrowattr, ldxios        )
       !!-----------------------------------------------------------------------
       !!                  ***  ROUTINE  iom_get_123d  ***
@@ -647,5 +1002,7 @@
                                                                            ! called open_ocean_jstart to set the start
                                                                            ! value for the 2nd dimension (netcdf only)
-      !
+      LOGICAL                    , INTENT(in   ), OPTIONAL ::   ldxios     ! use XIOS to read restart
+      !
+      LOGICAL                        ::   llxios       ! local definition for XIOS read
       LOGICAL                        ::   llnoov      ! local definition to read overlap
       LOGICAL                        ::   luse_jattr  ! local definition to read open_ocean_jstart file attribute
@@ -674,15 +1031,21 @@
       !---------------------------------------------------------------------
       !
-      clname = iom_file(kiomid)%name   !   esier to read
-      clinfo = '          iom_get_123d, file: '//trim(clname)//', var: '//trim(cdvar)
-      ! local definition of the domain ?
+      REAL(wp)                       :: gma, gmi
+      llxios = .FALSE.
+      if(PRESENT(ldxios)) llxios = ldxios
+      idvar = iom_varid( kiomid, cdvar ) 
       idom = kdom
-      ! do we read the overlap 
-      ! ugly patch SM+JMM+RB to overwrite global definition in some cases
-      llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
-      ! check kcount and kstart optionals parameters...
-      IF( PRESENT(kcount) .AND. (.NOT. PRESENT(kstart)) ) CALL ctl_stop(trim(clinfo), 'kcount present needs kstart present')
-      IF( PRESENT(kstart) .AND. (.NOT. PRESENT(kcount)) ) CALL ctl_stop(trim(clinfo), 'kstart present needs kcount present')
-      IF( PRESENT(kstart) .AND. idom /= jpdom_unknown .AND.  idom /= jpdom_autoglo_xy  ) &
+
+      IF(.NOT.llxios) THEN
+          clname = iom_file(kiomid)%name   !   esier to read
+          clinfo = '          iom_get_123d, file: '//trim(clname)//', var: '//trim(cdvar)
+          ! local definition of the domain ?
+          ! do we read the overlap 
+          ! ugly patch SM+JMM+RB to overwrite global definition in some cases
+          llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
+          ! check kcount and kstart optionals parameters...
+          IF( PRESENT(kcount) .AND. (.NOT. PRESENT(kstart)) ) CALL ctl_stop(trim(clinfo), 'kcount present needs kstart present')
+          IF( PRESENT(kstart) .AND. (.NOT. PRESENT(kcount)) ) CALL ctl_stop(trim(clinfo), 'kstart present needs kcount present')
+          IF( PRESENT(kstart) .AND. idom /= jpdom_unknown .AND.  idom /= jpdom_autoglo_xy  ) &
      &           CALL ctl_stop(trim(clinfo), 'kstart present needs kdom = jpdom_unknown or kdom = jpdom_autoglo_xy')
 
@@ -701,76 +1064,75 @@
       ENDIF
 
-      ! Search for the variable in the data base (eventually actualize data)
-      istop = nstop
-      idvar = iom_varid( kiomid, cdvar )
-      !
-      IF( idvar > 0 ) THEN
-         ! to write iom_file(kiomid)%dimsz in a shorter way !
-         idimsz(:) = iom_file(kiomid)%dimsz(:, idvar) 
-         inbdim = iom_file(kiomid)%ndims(idvar)            ! number of dimensions in the file
-         idmspc = inbdim                                   ! number of spatial dimensions in the file
-         IF( iom_file(kiomid)%luld(idvar) )   idmspc = inbdim - 1
-         IF( idmspc > 3 )   CALL ctl_stop(trim(clinfo), 'the file has more than 3 spatial dimensions this case is not coded...') 
-         !
-         ! update idom definition...
-         ! Identify the domain in case of jpdom_auto(glo/dta) definition
-         IF( idom == jpdom_autoglo_xy ) THEN
-            ll_depth_spec = .TRUE.
-            idom = jpdom_autoglo
-         ELSE
-            ll_depth_spec = .FALSE.
-         ENDIF
-         IF( idom == jpdom_autoglo .OR. idom == jpdom_autodta ) THEN            
-            IF( idom == jpdom_autoglo ) THEN   ;   idom = jpdom_global 
-            ELSE                               ;   idom = jpdom_data
-            ENDIF
-            ind1 = INDEX( clname, '_', back = .TRUE. ) + 1
-            ind2 = INDEX( clname, '.', back = .TRUE. ) - 1
-            IF( ind2 > ind1 ) THEN   ;   IF( VERIFY( clname(ind1:ind2), '0123456789' ) == 0 )   idom = jpdom_local   ;   ENDIF
-         ENDIF
-         ! Identify the domain in case of jpdom_local definition
-         IF( idom == jpdom_local ) THEN
-            IF(     idimsz(1) == jpi               .AND. idimsz(2) == jpj               ) THEN   ;   idom = jpdom_local_full
-            ELSEIF( idimsz(1) == nlci              .AND. idimsz(2) == nlcj              ) THEN   ;   idom = jpdom_local_noextra
-            ELSEIF( idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN   ;   idom = jpdom_local_noovlap
-            ELSE   ;   CALL ctl_stop( trim(clinfo), 'impossible to identify the local domain' )
-            ENDIF
-         ENDIF
-         !
-         ! check the consistency between input array and data rank in the file
-         !
-         ! initializations
-         itime = 1
-         IF( PRESENT(ktime) ) itime = ktime
-
-         irankpv = 1 * COUNT( (/PRESENT(pv_r1d)/) ) + 2 * COUNT( (/PRESENT(pv_r2d)/) ) + 3 * COUNT( (/PRESENT(pv_r3d)/) )
-         WRITE(clrankpv, fmt='(i1)') irankpv
-         WRITE(cldmspc , fmt='(i1)') idmspc
-         !
-         IF(     idmspc <  irankpv ) THEN 
-            CALL ctl_stop( TRIM(clinfo), 'The file has only '//cldmspc//' spatial dimension',   &
+          ! Search for the variable in the data base (eventually actualize data)
+          istop = nstop
+          !
+          IF( idvar > 0 ) THEN
+             ! to write iom_file(kiomid)%dimsz in a shorter way !
+             idimsz(:) = iom_file(kiomid)%dimsz(:, idvar) 
+             inbdim = iom_file(kiomid)%ndims(idvar)            ! number of dimensions in the file
+             idmspc = inbdim                                   ! number of spatial dimensions in the file
+             IF( iom_file(kiomid)%luld(idvar) )   idmspc = inbdim - 1
+             IF( idmspc > 3 )   CALL ctl_stop(trim(clinfo), 'the file has more than 3 spatial dimensions this case is not coded...') 
+             !
+             ! update idom definition...
+             ! Identify the domain in case of jpdom_auto(glo/dta) definition
+             IF( idom == jpdom_autoglo_xy ) THEN
+                ll_depth_spec = .TRUE.
+                idom = jpdom_autoglo
+             ELSE
+                ll_depth_spec = .FALSE.
+             ENDIF
+             IF( idom == jpdom_autoglo .OR. idom == jpdom_autodta ) THEN            
+                IF( idom == jpdom_autoglo ) THEN   ;   idom = jpdom_global 
+                ELSE                               ;   idom = jpdom_data
+                ENDIF
+                ind1 = INDEX( clname, '_', back = .TRUE. ) + 1
+                ind2 = INDEX( clname, '.', back = .TRUE. ) - 1
+                IF( ind2 > ind1 ) THEN   ;   IF( VERIFY( clname(ind1:ind2), '0123456789' ) == 0 )   idom = jpdom_local   ;   ENDIF
+             ENDIF
+             ! Identify the domain in case of jpdom_local definition
+             IF( idom == jpdom_local ) THEN
+                IF(     idimsz(1) == jpi               .AND. idimsz(2) == jpj               ) THEN   ;   idom = jpdom_local_full
+                ELSEIF( idimsz(1) == nlci              .AND. idimsz(2) == nlcj              ) THEN   ;   idom = jpdom_local_noextra
+                ELSEIF( idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN   ;   idom = jpdom_local_noovlap
+                ELSE   ;   CALL ctl_stop( trim(clinfo), 'impossible to identify the local domain' )
+                ENDIF
+             ENDIF
+             !
+             ! check the consistency between input array and data rank in the file
+             !
+             ! initializations
+             itime = 1
+             IF( PRESENT(ktime) ) itime = ktime
+
+             irankpv = 1 * COUNT( (/PRESENT(pv_r1d)/) ) + 2 * COUNT( (/PRESENT(pv_r2d)/) ) + 3 * COUNT( (/PRESENT(pv_r3d)/) )
+             WRITE(clrankpv, fmt='(i1)') irankpv
+             WRITE(cldmspc , fmt='(i1)') idmspc
+             !
+             IF(     idmspc <  irankpv ) THEN 
+                CALL ctl_stop( TRIM(clinfo), 'The file has only '//cldmspc//' spatial dimension',   &
                &                         'it is impossible to read a '//clrankpv//'D array from this file...' )
-         ELSEIF( idmspc == irankpv ) THEN
-            IF( PRESENT(pv_r1d) .AND. idom /= jpdom_unknown )   &
+             ELSEIF( idmspc == irankpv ) THEN
+                IF( PRESENT(pv_r1d) .AND. idom /= jpdom_unknown )   &
                &   CALL ctl_stop( TRIM(clinfo), 'case not coded...You must use jpdom_unknown' )
-         ELSEIF( idmspc >  irankpv ) THEN
-               IF( PRESENT(pv_r2d) .AND. itime == 1 .AND. idimsz(3) == 1 .AND. idmspc == 3 ) THEN
-                  CALL ctl_warn( trim(clinfo), '2D array but 3 spatial dimensions for the data...'              ,   &
+             ELSEIF( idmspc >  irankpv ) THEN
+                   IF( PRESENT(pv_r2d) .AND. itime == 1 .AND. idimsz(3) == 1 .AND. idmspc == 3 ) THEN
+                      CALL ctl_warn( trim(clinfo), '2D array but 3 spatial dimensions for the data...'              ,   &
                         &         'As the size of the z dimension is 1 and as we try to read the first record, ',   &
                         &         'we accept this case, even if there is a possible mix-up between z and time dimension' )   
-                  idmspc = idmspc - 1
-               ELSE
-                  CALL ctl_stop( TRIM(clinfo), 'To keep iom lisibility, when reading a '//clrankpv//'D array,'         ,   &
+                      idmspc = idmspc - 1
+                   ELSE
+                      CALL ctl_stop( TRIM(clinfo), 'To keep iom lisibility, when reading a '//clrankpv//'D array,'         ,   &
                      &                         'we do not accept data with '//cldmspc//' spatial dimensions',   &
                      &                         'Use ncwa -a to suppress the unnecessary dimensions' )
-               ENDIF
-         ENDIF
-
-         !
-         ! definition of istart and icnt
-         !
-         icnt  (:) = 1
-         istart(:) = 1
-         istart(idmspc+1) = itime
+                   ENDIF
+             ENDIF
+
+             !
+             ! definition of istart and icnt
+             !
+             icnt  (:) = 1
+             istart(:) = 1
+             istart(idmspc+1) = itime
 
          IF( PRESENT(kstart) .AND. .NOT. ll_depth_spec ) THEN 
@@ -793,5 +1155,5 @@
 ! JMM + SM: ugly patch before getting the new version of lib_mpp)
 !                  IF( idom /= jpdom_local_noovlap )   istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
-                  IF( llnoov .AND. idom /= jpdom_local_noovlap ) istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
+                      IF( llnoov .AND. idom /= jpdom_local_noovlap ) istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
                   ! we do not read the overlap and the extra-halos -> from nldi to nlei and from nldj to nlej 
 ! JMM + SM: ugly patch before getting the new version of lib_mpp)
@@ -810,64 +1172,64 @@
          ENDIF
 
-         ! check that istart and icnt can be used with this file
-         !-
-         DO jl = 1, jpmax_dims
-            itmp = istart(jl)+icnt(jl)-1
-            IF( itmp > idimsz(jl) .AND. idimsz(jl) /= 0 ) THEN
-               WRITE( ctmp1, FMT="('(istart(', i1, ') + icnt(', i1, ') - 1) = ', i5)" ) jl, jl, itmp
-               WRITE( ctmp2, FMT="(' is larger than idimsz(', i1,') = ', i5)"         ) jl, idimsz(jl)
-               CALL ctl_stop( trim(clinfo), 'start and count too big regarding to the size of the data, ', ctmp1, ctmp2 )     
-            ENDIF
-         END DO
-
-         ! check that icnt matches the input array
-         !-     
-         IF( idom == jpdom_unknown ) THEN
-            IF( irankpv == 1 )        ishape(1:1) = SHAPE(pv_r1d)
-            IF( irankpv == 2 )        ishape(1:2) = SHAPE(pv_r2d)
-            IF( irankpv == 3 )        ishape(1:3) = SHAPE(pv_r3d)
-            ctmp1 = 'd'
-         ELSE
-            IF( irankpv == 2 ) THEN
+             ! check that istart and icnt can be used with this file
+             !-
+             DO jl = 1, jpmax_dims
+                itmp = istart(jl)+icnt(jl)-1
+                IF( itmp > idimsz(jl) .AND. idimsz(jl) /= 0 ) THEN
+                   WRITE( ctmp1, FMT="('(istart(', i1, ') + icnt(', i1, ') - 1) = ', i5)" ) jl, jl, itmp
+                   WRITE( ctmp2, FMT="(' is larger than idimsz(', i1,') = ', i5)"         ) jl, idimsz(jl)
+                   CALL ctl_stop( trim(clinfo), 'start and count too big regarding to the size of the data, ', ctmp1, ctmp2 )     
+                ENDIF
+             END DO
+
+             ! check that icnt matches the input array
+             !-     
+             IF( idom == jpdom_unknown ) THEN
+                IF( irankpv == 1 )        ishape(1:1) = SHAPE(pv_r1d)
+                IF( irankpv == 2 )        ishape(1:2) = SHAPE(pv_r2d)
+                IF( irankpv == 3 )        ishape(1:3) = SHAPE(pv_r3d)
+                ctmp1 = 'd'
+             ELSE
+                IF( irankpv == 2 ) THEN
 ! JMM + SM: ugly patch before getting the new version of lib_mpp)
 !               ishape(1:2) = SHAPE(pv_r2d(nldi:nlei,nldj:nlej  ))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej)'
-               IF( llnoov ) THEN ; ishape(1:2)=SHAPE(pv_r2d(nldi:nlei,nldj:nlej  )) ; ctmp1='d(nldi:nlei,nldj:nlej)'
-               ELSE              ; ishape(1:2)=SHAPE(pv_r2d(1   :nlci,1   :nlcj  )) ; ctmp1='d(1:nlci,1:nlcj)'
-               ENDIF
-            ENDIF
-            IF( irankpv == 3 ) THEN 
+                   IF( llnoov ) THEN ; ishape(1:2)=SHAPE(pv_r2d(nldi:nlei,nldj:nlej  )) ; ctmp1='d(nldi:nlei,nldj:nlej)'
+                   ELSE              ; ishape(1:2)=SHAPE(pv_r2d(1   :nlci,1   :nlcj  )) ; ctmp1='d(1:nlci,1:nlcj)'
+                   ENDIF
+                ENDIF
+                IF( irankpv == 3 ) THEN 
 ! JMM + SM: ugly patch before getting the new version of lib_mpp)
 !               ishape(1:3) = SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej,:)'
-               IF( llnoov ) THEN ; ishape(1:3)=SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:)) ; ctmp1='d(nldi:nlei,nldj:nlej,:)'
-               ELSE              ; ishape(1:3)=SHAPE(pv_r3d(1   :nlci,1   :nlcj,:)) ; ctmp1='d(1:nlci,1:nlcj,:)'
-               ENDIF
-            ENDIF
-         ENDIF
+                   IF( llnoov ) THEN ; ishape(1:3)=SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:)) ; ctmp1='d(nldi:nlei,nldj:nlej,:)'
+                   ELSE              ; ishape(1:3)=SHAPE(pv_r3d(1   :nlci,1   :nlcj,:)) ; ctmp1='d(1:nlci,1:nlcj,:)'
+                   ENDIF
+                ENDIF
+             ENDIF
          
-         DO jl = 1, irankpv
-            WRITE( ctmp2, FMT="(', ', i1,'): ', i5,' /= icnt(', i1,'):', i5)" ) jl, ishape(jl), jl, icnt(jl)
-            IF( ishape(jl) /= icnt(jl) )   CALL ctl_stop( TRIM(clinfo), 'size(pv_r'//clrankpv//TRIM(ctmp1)//TRIM(ctmp2) )
-         END DO
-
-      ENDIF
-
-      ! read the data
-      !-     
-      IF( idvar > 0 .AND. istop == nstop ) THEN   ! no additional errors until this point...
-         !
-         ! find the right index of the array to be read
+             DO jl = 1, irankpv
+                WRITE( ctmp2, FMT="(', ', i1,'): ', i5,' /= icnt(', i1,'):', i5)" ) jl, ishape(jl), jl, icnt(jl)
+                IF( ishape(jl) /= icnt(jl) )   CALL ctl_stop( TRIM(clinfo), 'size(pv_r'//clrankpv//TRIM(ctmp1)//TRIM(ctmp2) )
+             END DO
+
+          ENDIF
+
+          ! read the data
+          !-     
+          IF( idvar > 0 .AND. istop == nstop ) THEN   ! no additional errors until this point...
+             !
+             ! find the right index of the array to be read
 ! JMM + SM: ugly patch before getting the new version of lib_mpp)
 !         IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
 !         ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
 !         ENDIF
-         IF( llnoov ) THEN
-            IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
-            ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
-            ENDIF
-         ELSE
-            IF( idom /= jpdom_unknown ) THEN   ;   ix1 = 1      ;   ix2 = nlci      ;   iy1 = 1      ;   iy2 = nlcj
-            ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
-            ENDIF
-         ENDIF
+             IF( llnoov ) THEN
+                IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
+                ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
+                ENDIF
+             ELSE
+                IF( idom /= jpdom_unknown ) THEN   ;   ix1 = 1      ;   ix2 = nlci      ;   iy1 = 1      ;   iy2 = nlcj
+                ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
+                ENDIF
+             ENDIF
       
          SELECT CASE (iom_file(kiomid)%iolib)
@@ -878,42 +1240,73 @@
          END SELECT
 
-         IF( istop == nstop ) THEN   ! no additional errors until this point...
-            IF(lwp) WRITE(numout,"(10x,' read ',a,' (rec: ',i6,') in ',a,' ok')") TRIM(cdvar), itime, TRIM(iom_file(kiomid)%name)
+             IF( istop == nstop ) THEN   ! no additional errors until this point...
+                IF(lwp) WRITE(numout,"(10x,' read ',a,' (rec: ',i6,') in ',a,' ok')") TRIM(cdvar), itime, TRIM(iom_file(kiomid)%name)
           
-            !--- overlap areas and extra hallows (mpp)
-            IF(     PRESENT(pv_r2d) .AND. idom /= jpdom_unknown ) THEN
-               CALL lbc_lnk( pv_r2d,'Z',-999.,'no0' )
-            ELSEIF( PRESENT(pv_r3d) .AND. idom /= jpdom_unknown ) THEN
-               ! this if could be simplified with the new lbc_lnk that works with any size of the 3rd dimension
-               IF( icnt(3) == jpk ) THEN
-                  CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
-               ELSE   ! put some arbitrary value (a call to lbc_lnk will be done later...)
-                  DO jj = nlcj+1, jpj   ;   pv_r3d(1:nlci, jj, :) = pv_r3d(1:nlci, nlej, :)   ;   END DO
-                  DO ji = nlci+1, jpi   ;   pv_r3d(ji    , : , :) = pv_r3d(nlei  , :   , :)   ;   END DO
-               ENDIF
-            ENDIF
-            
-            ! C1D case : always call lbc_lnk to replicate the central value over the whole 3X3 domain
-            IF( lk_c1d .AND. PRESENT(pv_r2d) )   CALL lbc_lnk( pv_r2d,'Z',1. )
-            IF( lk_c1d .AND. PRESENT(pv_r3d) )   CALL lbc_lnk( pv_r3d,'Z',1. )
-    
-            !--- Apply scale_factor and offset
-            zscf = iom_file(kiomid)%scf(idvar)      ! scale factor
-            zofs = iom_file(kiomid)%ofs(idvar)      ! offset
-            IF(     PRESENT(pv_r1d) ) THEN
-               IF( zscf /= 1. )   pv_r1d(:) = pv_r1d(:) * zscf 
-               IF( zofs /= 0. )   pv_r1d(:) = pv_r1d(:) + zofs
-            ELSEIF( PRESENT(pv_r2d) ) THEN
-               IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
-               IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
-            ELSEIF( PRESENT(pv_r3d) ) THEN
-               IF( zscf /= 1.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) * zscf
-               IF( zofs /= 0.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) + zofs
-            ENDIF
-            !
-         ENDIF
-         !
-      ENDIF
-      !
+                !--- overlap areas and extra hallows (mpp)
+                IF(     PRESENT(pv_r2d) .AND. idom /= jpdom_unknown ) THEN
+                   CALL lbc_lnk( pv_r2d,'Z',-999.,'no0' )
+                ELSEIF( PRESENT(pv_r3d) .AND. idom /= jpdom_unknown ) THEN
+                   ! this if could be simplified with the new lbc_lnk that works with any size of the 3rd dimension
+                   IF( icnt(3) == jpk ) THEN
+                      CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
+                   ELSE   ! put some arbitrary value (a call to lbc_lnk will be done later...)
+                      DO jj = nlcj+1, jpj   ;   pv_r3d(1:nlci, jj, :) = pv_r3d(1:nlci, nlej, :)   ;   END DO
+                      DO ji = nlci+1, jpi   ;   pv_r3d(ji    , : , :) = pv_r3d(nlei  , :   , :)   ;   END DO
+                   ENDIF
+                ENDIF
+                !
+             ELSE
+                ! return if istop == nstop is false
+                RETURN
+             ENDIF
+          ELSE
+             ! return if statment idvar > 0 .AND. istop == nstop is false
+             RETURN
+          ENDIF
+          !
+       ELSE        ! read using XIOS. Only if KEY_IOMPUT is defined
+#if defined key_iomput
+!would be good to be able to check which context is active and swap only if current is not restart
+          CALL iom_swap( TRIM(crxios_context) ) 
+          IF( PRESENT(pv_r3d) ) THEN
+             if(lwp) write(numout,*) 'XIOS RST READ (3D): ',trim(cdvar)
+             CALL xios_recv_field( trim(cdvar), pv_r3d)
+             IF(idom /= jpdom_unknown ) then
+                 CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
+             ENDIF
+          ELSEIF( PRESENT(pv_r2d) ) THEN
+             if(lwp) write(numout,*) 'XIOS RST READ (2D): ', trim(cdvar)
+             CALL xios_recv_field( trim(cdvar), pv_r2d)
+             IF(idom /= jpdom_unknown ) THEN
+                 CALL lbc_lnk(pv_r2d,'Z',-999.,'no0')
+             ENDIF
+          ELSEIF( PRESENT(pv_r1d) ) THEN
+             if(lwp) write(numout,*) 'XIOS RST READ (1D): ', trim(cdvar)
+             CALL xios_recv_field( trim(cdvar), pv_r1d)
+          ENDIF
+          CALL iom_swap( TRIM(cxios_context) )
+#else
+          istop = istop + 1 
+          clinfo = 'Can not use XIOS in iom_get_123d, file: '//trim(clname)//', var:'//trim(cdvar)
+#endif
+       ENDIF
+!some final adjustments
+       ! C1D case : always call lbc_lnk to replicate the central value over the whole 3X3 domain
+       IF( lk_c1d .AND. PRESENT(pv_r2d) )   CALL lbc_lnk( pv_r2d,'Z',1. )
+       IF( lk_c1d .AND. PRESENT(pv_r3d) )   CALL lbc_lnk( pv_r3d,'Z',1. )
+
+       !--- Apply scale_factor and offset
+       zscf = iom_file(kiomid)%scf(idvar)      ! scale factor
+       zofs = iom_file(kiomid)%ofs(idvar)      ! offset
+       IF(     PRESENT(pv_r1d) ) THEN
+          IF( zscf /= 1. )   pv_r1d(:) = pv_r1d(:) * zscf 
+          IF( zofs /= 0. )   pv_r1d(:) = pv_r1d(:) + zofs
+       ELSEIF( PRESENT(pv_r2d) ) THEN
+          IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
+          IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
+       ELSEIF( PRESENT(pv_r3d) ) THEN
+          IF( zscf /= 1.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) * zscf
+          IF( zofs /= 0.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) + zofs
+       ENDIF
    END SUBROUTINE iom_get_123d
 
@@ -1115,5 +1508,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
@@ -1122,18 +1515,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
@@ -1142,18 +1549,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
@@ -1162,18 +1583,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
@@ -1182,13 +1617,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
@@ -1262,5 +1711,5 @@
             &    lonvalue_1D=lonvalue, latvalue_1D=latvalue, mask_1D=mask, nvertex=nvertex, bounds_lon_1D=bounds_lon,      &
             &    bounds_lat_1D=bounds_lat, area=area, type='curvilinear')
-     ENDIF
+      ENDIF
       IF ( xios_is_valid_domaingroup(cdid) ) THEN
          CALL xios_set_domaingroup_attr( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
@@ -1368,6 +1817,10 @@
    SUBROUTINE iom_context_finalize( cdname )
       CHARACTER(LEN=*), INTENT(in) :: cdname
-      !
-      IF( xios_is_valid_context(cdname) ) THEN
+      CHARACTER(LEN=120)           :: clname
+      !
+      clname = cdname
+      IF( TRIM(Agrif_CFixed()) .NE. '0' ) clname = TRIM(Agrif_CFixed())//"_"//clname 
+
+      IF( xios_is_valid_context(clname) ) THEN
          CALL iom_swap( cdname )   ! swap to cdname context
          CALL xios_context_finalize() ! finalize the context
@@ -1378,5 +1831,5 @@
 
 
-   SUBROUTINE set_grid( cdgrd, plon, plat )
+   SUBROUTINE set_grid( cdgrd, plon, plat, ldxios )
       !!----------------------------------------------------------------------
       !!                     ***  ROUTINE set_grid  ***
@@ -1391,4 +1844,5 @@
       REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zmask
       INTEGER  :: ni,nj
+      LOGICAL, INTENT(IN) :: ldxios
       
       ni=nlei-nldi+1 ; nj=nlej-nldj+1
@@ -1396,8 +1850,8 @@
       CALL iom_set_domain_attr("grid_"//cdgrd, ni_glo=jpiglo, nj_glo=jpjglo, ibegin=nimpp+nldi-2, jbegin=njmpp+nldj-2, ni=ni, nj=nj)
       CALL iom_set_domain_attr("grid_"//cdgrd, data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
-      CALL iom_set_domain_attr("grid_"//cdgrd, lonvalue = RESHAPE(plon(nldi:nlei, nldj:nlej),(/ ni*nj /)),   &
+      if(.NOT.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 /)))  
 
-      IF ( ln_mskland ) THEN
+      IF ( ln_mskland.AND.(.NOT.ldxios) ) THEN
          ! mask land points, keep values on coast line -> specific mask for U, V and W points
          SELECT CASE ( cdgrd )
@@ -1439,5 +1893,5 @@
       ! Offset of coordinate representing bottom-left corner
       SELECT CASE ( TRIM(cdgrd) )
-         CASE ('T', 'W')
+         CASE ('T', 'W', 'N')
             icnr = -1 ; jcnr = -1
          CASE ('U')
Index: /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/IOM/iom_def.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/IOM/iom_def.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/IOM/iom_def.F90	(revision 8987)
@@ -44,6 +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            ::   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
@@ -66,5 +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 !: maximum number of restart variables defined in iom_set_rst_vars
+   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(RST_FIELD), PUBLIC :: rst_wfields(max_rst_fields), rst_rfields(max_rst_fields)
 
    !!=====================================================================
Index: /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/IOM/iom_nf90.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/IOM/iom_nf90.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/IOM/iom_nf90.F90	(revision 8987)
@@ -126,6 +126,6 @@
             CALL iom_nf90_check(NF90_DEF_DIM( if90id, 'x', kdompar(1,1)  , idmy ), clinfo)
             CALL iom_nf90_check(NF90_DEF_DIM( if90id, 'y', kdompar(2,1)  , idmy ), clinfo)
-            CALL iom_nf90_check(NF90_DEF_DIM( if90id, 'z', jpk           , idmy ), clinfo)
-            CALL iom_nf90_check(NF90_DEF_DIM( if90id, 't', NF90_UNLIMITED, idmy ), clinfo)
+            CALL iom_nf90_check(NF90_DEF_DIM( if90id, 'nav_lev', jpk           , idmy ), clinfo)
+            CALL iom_nf90_check(NF90_DEF_DIM( if90id, 'time_counter', NF90_UNLIMITED, idmy ), clinfo)
             ! global attributes
             CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_number_total'   , jpnij              ), clinfo)
Index: /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/IOM/restart.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/IOM/restart.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/IOM/restart.F90	(revision 8987)
@@ -28,5 +28,5 @@
    USE iom             ! I/O module
    USE diurnal_bulk
-   
+
    IMPLICIT NONE
    PRIVATE
@@ -61,4 +61,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 +95,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 +138,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 +182,4 @@
                nitrst = nstocklist( nrst_lst )
             ENDIF
-            lrst_oce = .FALSE.
       ENDIF
       !
@@ -193,9 +210,27 @@
             WRITE(numout,*) '~~~~~~~~'
          ENDIF
-
+         lxios_sini = .FALSE.
          clpath = TRIM(cn_ocerst_indir)
          IF( clpath(LEN_TRIM(clpath):) /= '/' ) clpath = TRIM(clpath) // '/'
          CALL iom_open( TRIM(clpath)//cn_ocerst_in, numror, kiolib = jlibalt )
-      ENDIF
+! are we using XIOS to read the data? Part above will have to modified once XIOS
+! can handle checking if variable is in the restart file (there will be no need to open
+! restart)
+         IF(.NOT.lxios_set) lrxios = lrxios.AND.lxios_sini
+         IF( lrxios) THEN
+             crxios_context = 'nemo_rst'
+             IF( .NOT.lxios_set ) THEN
+                 IF(lwp) WRITE(numout,*) 'Enable restart reading by XIOS'
+                 CALL iom_init( crxios_context )
+                 lxios_set = .TRUE.
+             ENDIF
+         ENDIF
+         IF( TRIM(Agrif_CFixed()) /= '0' .AND. lrxios) THEN
+             CALL iom_init( crxios_context )
+             IF(lwp) WRITE(numout,*) 'Enable restart reading by XIOS for AGRIF'
+             lxios_set = .TRUE.
+         ENDIF 
+      ENDIF
+
    END SUBROUTINE rst_read_open
 
@@ -211,4 +246,5 @@
       REAL(wp) ::   zrdt
       INTEGER  ::   jk
+      REAL(wp), DIMENSION(jpi, jpj, jpk) :: w3d
       !!----------------------------------------------------------------------
 
@@ -217,35 +253,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 )
+         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  ) 
+      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) ) 
+         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      )   ! before fields
-         CALL iom_get( numror, jpdom_autoglo, 'vb'     , vb      )
-         CALL iom_get( numror, jpdom_autoglo, 'tb'     , tsb(:,:,:,jp_tem) )
-         CALL iom_get( numror, jpdom_autoglo, 'sb'     , tsb(:,:,:,jp_sal) )
-         CALL iom_get( numror, jpdom_autoglo, 'sshb'   , sshb    )
+         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      )   ! now    fields
-      CALL iom_get( numror, jpdom_autoglo, 'vn'     , vn      )
-      CALL iom_get( numror, jpdom_autoglo, 'tn'     , tsn(:,:,:,jp_tem) )
-      CALL iom_get( numror, jpdom_autoglo, 'sn'     , tsn(:,:,:,jp_sal) )
-      CALL iom_get( numror, jpdom_autoglo, 'sshn'   , sshn    )
+      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    )   ! 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_METO_2017/NEMOGCM/NEMO/OPA_SRC/SBC/sbcapr.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/SBC/sbcapr.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/SBC/sbcapr.F90	(revision 8987)
@@ -65,4 +65,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            ::  lrxios   ! read restart using XIOS?
       !!
       NAMELIST/namsbc_apr/ cn_dir, sn_apr, ln_ref_apr, rn_pref, ln_apr_obc
@@ -109,4 +110,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
 
@@ -152,5 +156,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 )   ! 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
@@ -165,5 +169,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_METO_2017/NEMOGCM/NEMO/OPA_SRC/SBC/sbcisf.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/SBC/sbcisf.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/SBC/sbcisf.F90	(revision 8987)
@@ -218,7 +218,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(:,:) )   ! before salt content isf_tsc trend
-               CALL iom_get( numror, jpdom_autoglo, 'isf_sc_b', risf_tsc_b(:,:,jp_sal) )   ! before salt content isf_tsc trend
-               CALL iom_get( numror, jpdom_autoglo, 'isf_hc_b', risf_tsc_b(:,:,jp_tem) )   ! 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(:,:)
@@ -232,7 +232,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
          !
@@ -393,4 +395,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_METO_2017/NEMOGCM/NEMO/OPA_SRC/SBC/sbcmod.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/SBC/sbcmod.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/SBC/sbcmod.F90	(revision 8987)
@@ -56,5 +56,4 @@
    USE lib_mpp        ! MPP library
    USE timing         ! Timing
-
    USE diurnal_bulk, ONLY:   ln_diurnal_only   ! diurnal SST diagnostic
 
@@ -356,4 +355,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
 
@@ -462,13 +471,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 )   ! before i-stress  (U-point)
-            CALL iom_get( numror, jpdom_autoglo, 'vtau_b', vtau_b )   ! before j-stress  (V-point)
-            CALL iom_get( numror, jpdom_autoglo, 'qns_b' , qns_b  )   ! 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  ) ! before     solar heat flux (T-point)
-            CALL iom_get( numror, jpdom_autoglo, 'emp_b', emp_b  )    ! 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 )  ! 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(:,:)
@@ -490,11 +499,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_METO_2017/NEMOGCM/NEMO/OPA_SRC/SBC/sbcrnf.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/SBC/sbcrnf.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/SBC/sbcrnf.F90	(revision 8987)
@@ -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'
-            CALL iom_get( numror, jpdom_autoglo, 'rnf_b', rnf_b )     ! before runoff
-            CALL iom_get( numror, jpdom_autoglo, 'rnf_hc_b', rnf_tsc_b(:,:,jp_tem) )   ! before heat content of runoff
-            CALL iom_get( numror, jpdom_autoglo, 'rnf_sc_b', rnf_tsc_b(:,:,jp_sal) )   ! 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_METO_2017/NEMOGCM/NEMO/OPA_SRC/SBC/sbcssm.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/SBC/sbcssm.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/SBC/sbcssm.F90	(revision 8987)
@@ -155,13 +155,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
          !
@@ -206,14 +208,14 @@
          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 )    ! sbc frequency of previous run
-            CALL iom_get( numror, jpdom_autoglo, 'ssu_m'  , ssu_m  )    ! sea surface mean velocity    (U-point)
-            CALL iom_get( numror, jpdom_autoglo, 'ssv_m'  , ssv_m  )    !   "         "    velocity    (V-point)
-            CALL iom_get( numror, jpdom_autoglo, 'sst_m'  , sst_m  )    !   "         "    temperature (T-point)
-            CALL iom_get( numror, jpdom_autoglo, 'sss_m'  , sss_m  )    !   "         "    salinity    (T-point)
-            CALL iom_get( numror, jpdom_autoglo, 'ssh_m'  , ssh_m  )    !   "         "    height      (T-point)
-            CALL iom_get( numror, jpdom_autoglo, 'e3t_m'  , e3t_m  )    ! 1st level thickness          (T-point)
+            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)
             ! 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  )
+               CALL iom_get( numror, jpdom_autoglo, 'frq_m'  , frq_m, ldxios = lrxios  )
             ELSE
                frq_m(:,:) = 1._wp   ! default definition
@@ -251,4 +253,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_METO_2017/NEMOGCM/NEMO/OPA_SRC/TRA/traqsr.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/TRA/traqsr.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/TRA/traqsr.F90	(revision 8987)
@@ -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 )   ! 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  )
+         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_METO_2017/NEMOGCM/NEMO/OPA_SRC/TRA/trasbc.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/TRA/trasbc.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/TRA/trasbc.F90	(revision 8987)
@@ -112,6 +112,6 @@
             zfact = 0.5_wp
             sbc_tsc(:,:,:) = 0._wp
-            CALL iom_get( numror, jpdom_autoglo, 'sbc_hc_b', sbc_tsc_b(:,:,jp_tem) )   ! before heat content sbc trend
-            CALL iom_get( numror, jpdom_autoglo, 'sbc_sc_b', sbc_tsc_b(:,:,jp_sal) )   ! 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
@@ -161,6 +161,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_METO_2017/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfgls.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfgls.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfgls.F90	(revision 8987)
@@ -1140,4 +1140,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')
       !
@@ -1175,10 +1184,10 @@
             !
             IF( MIN( id1, id2, id3, id4, id5, id6 ) > 0 ) THEN        ! all required arrays exist
-               CALL iom_get( numror, jpdom_autoglo, 'en'    , en     )
-               CALL iom_get( numror, jpdom_autoglo, 'avt'   , avt    )
-               CALL iom_get( numror, jpdom_autoglo, 'avm'   , avm    )
-               CALL iom_get( numror, jpdom_autoglo, 'avmu'  , avmu   )
-               CALL iom_get( numror, jpdom_autoglo, 'avmv'  , avmv   )
-               CALL iom_get( numror, jpdom_autoglo, 'mxln'  , mxln   )
+               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'
@@ -1200,10 +1209,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_METO_2017/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfini.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfini.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfini.F90	(revision 8987)
@@ -159,6 +159,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 )
-            CALL iom_get( numror, jpdom_unknown, 'avtb', avtb )
+            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_METO_2017/NEMOGCM/NEMO/OPA_SRC/ZDF/zdftke.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/ZDF/zdftke.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/ZDF/zdftke.F90	(revision 8987)
@@ -814,4 +814,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
 
@@ -845,11 +854,11 @@
            !
            IF( id1 > 0 ) THEN                       ! 'en' exists
-              CALL iom_get( numror, jpdom_autoglo, 'en', en )
+              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   )
-                 CALL iom_get( numror, jpdom_autoglo, 'avm'  , avm   )
-                 CALL iom_get( numror, jpdom_autoglo, 'avmu' , avmu  )
-                 CALL iom_get( numror, jpdom_autoglo, 'avmv' , avmv  )
-                 CALL iom_get( numror, jpdom_autoglo, 'dissl', dissl )
+                 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)
@@ -880,10 +889,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_METO_2017/NEMOGCM/NEMO/OPA_SRC/nemogcm.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/nemogcm.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/nemogcm.F90	(revision 8987)
@@ -422,5 +422,5 @@
       IF( lk_c1d        )   CALL     c1d_init   ! 1D column configuration
                             CALL     wad_init   ! Wetting and drying options
-                            CALL     dom_init   ! Domain
+                            CALL     dom_init("OPA")   ! Domain
       IF( ln_crs        )   CALL     crs_init   ! coarsened grid: domain initialization 
       IF( ln_nnogather )    CALL nemo_northcomms! northfold neighbour lists (must be done after the masks are defined)
Index: /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/step.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/step.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/OPA_SRC/step.F90	(revision 8987)
@@ -345,4 +345,5 @@
       IF( kstp == nitend .OR. indic < 0 ) THEN 
                       CALL iom_context_finalize(      cxios_context          ) ! needed for XIOS+AGRIF
+                      IF(lrxios) CALL iom_context_finalize(      crxios_context          )
          IF( ln_crs ) CALL iom_context_finalize( trim(cxios_context)//"_crs" ) ! 
       ENDIF
Index: /branches/2017/dev_METO_2017/NEMOGCM/NEMO/SAO_SRC/nemogcm.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/SAO_SRC/nemogcm.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/SAO_SRC/nemogcm.F90	(revision 8987)
@@ -271,5 +271,5 @@
                             CALL     phy_cst    ! Physical constants
                             CALL     eos_init   ! Equation of state
-                            CALL     dom_init   ! Domain
+                            CALL     dom_init('SAO')   ! Domain
 
       IF( ln_nnogather )    CALL nemo_northcomms   ! Initialise the northfold neighbour lists (must be done after the masks are defined)
Index: /branches/2017/dev_METO_2017/NEMOGCM/NEMO/SAS_SRC/daymod.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/SAS_SRC/daymod.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/SAS_SRC/daymod.F90	(revision 8987)
@@ -30,5 +30,5 @@
    USE in_out_manager ! I/O manager
    USE prtctl         ! Print control
-   USE iom            !
+   USE iom            ! I/O manager
    USE timing         ! Timing
    USE restart        ! restart
@@ -142,4 +142,10 @@
       ! call day to set the calendar parameters at the begining of the current simulaton. needed by iom_init
       CALL day( nit000 )
+      IF( lwxios ) THEN
+        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
@@ -318,5 +324,5 @@
          IF( iom_varid( numror, 'kt', ldstop = .FALSE. ) > 0 ) THEN
             ! Get Calendar informations
-            CALL iom_get( numror, 'kt', zkt )   ! 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 : '
@@ -337,8 +343,8 @@
             IF ( nrstdt == 2 ) THEN
                ! read the parameters corresponding to nit000 - 1 (last time step of previous run)
-               CALL iom_get( numror, 'ndastp', zndastp )
+               CALL iom_get( numror, 'ndastp', zndastp, ldxios = lrxios )
                ndastp = NINT( zndastp )
-               CALL iom_get( numror, 'adatrj', adatrj  )
-	       CALL iom_get( numror, 'ntime', ktime )
+               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
@@ -399,9 +405,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_METO_2017/NEMOGCM/NEMO/SAS_SRC/nemogcm.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/SAS_SRC/nemogcm.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/SAS_SRC/nemogcm.F90	(revision 8987)
@@ -359,6 +359,5 @@
                             CALL phy_cst    ! Physical constants
                             CALL eos_init   ! Equation of state
-                            CALL dom_init   ! Domain
-
+                            CALL dom_init('SAS')   ! Domain
      IF( ln_nnogather )     CALL nemo_northcomms   ! Initialise the northfold neighbour lists (must be done after the masks are defined)
 
Index: /branches/2017/dev_METO_2017/NEMOGCM/NEMO/SAS_SRC/step.F90
===================================================================
--- /branches/2017/dev_METO_2017/NEMOGCM/NEMO/SAS_SRC/step.F90	(revision 8986)
+++ /branches/2017/dev_METO_2017/NEMOGCM/NEMO/SAS_SRC/step.F90	(revision 8987)
@@ -122,5 +122,13 @@
 
 #if defined key_iomput
-      IF( kstp == nitend .OR. indic < 0 ) THEN 
+      IF( kstp == nitrst ) THEN
+         IF(.NOT.lwxios) THEN
+            CALL iom_close( numrow )     
+         ELSE
+            CALL iom_context_finalize( cwxios_context )
+         ENDIF
+         lrst_oce = .FALSE.
+      ENDIF
+      IF( kstp == nitend .OR. indic < 0 ) THEN
                              CALL iom_context_finalize( cxios_context ) ! needed for XIOS+AGRIF
       ENDIF
