Index: /branches/UKMO/dev_r7573_xios_write/NEMOGCM/CONFIG/SHARED/namelist_ref
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/CONFIG/SHARED/namelist_ref	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/CONFIG/SHARED/namelist_ref	(revision 8079)
@@ -50,4 +50,5 @@
    ln_clobber  = .false.   !  clobber (overwrite) an existing file
    nn_chunksz  =       0   !  chunksize (bytes) for NetCDF file (works only with iom_nf90 routines)
+   nn_wxios = 0      !  use XIOS to write restart file 0 - no, 1 - single file output, 2 - multiple file output
 /
 !
Index: /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/DIA/diahsb.F90
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/DIA/diahsb.F90	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/DIA/diahsb.F90	(revision 8079)
@@ -32,4 +32,5 @@
    USE timing          ! preformance summary
    USE wrk_nemo        ! work arrays
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -304,21 +305,22 @@
         IF(lwp) WRITE(numout,*) ' dia_hsb_rst at it= ', kt,' date= ', ndastp
         IF(lwp) WRITE(numout,*) '~~~~~~~'
-
-        CALL iom_rstput( kt, nitrst, numrow, 'frc_v'   , frc_v     )
-        CALL iom_rstput( kt, nitrst, numrow, 'frc_t'   , frc_t     )
-        CALL iom_rstput( kt, nitrst, numrow, 'frc_s'   , frc_s     )
+        IF( lwxios ) CALL iom_swap(      wxios_context          )
+        CALL iom_rstput( kt, nitrst, numrow, 'frc_v'   , frc_v, lxios = lwxios)
+        CALL iom_rstput( kt, nitrst, numrow, 'frc_t'   , frc_t, lxios = lwxios)
+        CALL iom_rstput( kt, nitrst, numrow, 'frc_s'   , frc_s, lxios = lwxios)
         IF( .NOT. lk_vvl ) THEN
-           CALL iom_rstput( kt, nitrst, numrow, 'frc_wn_t', frc_wn_t )
-           CALL iom_rstput( kt, nitrst, numrow, 'frc_wn_s', frc_wn_s )
+           CALL iom_rstput( kt, nitrst, numrow, 'frc_wn_t', frc_wn_t, lxios = lwxios)
+           CALL iom_rstput( kt, nitrst, numrow, 'frc_wn_s', frc_wn_s, lxios = lwxios )
         ENDIF
-        CALL iom_rstput( kt, nitrst, numrow, 'ssh_ini', ssh_ini )
-        CALL iom_rstput( kt, nitrst, numrow, 'e3t_ini', e3t_ini )
-        CALL iom_rstput( kt, nitrst, numrow, 'hc_loc_ini', hc_loc_ini )
-        CALL iom_rstput( kt, nitrst, numrow, 'sc_loc_ini', sc_loc_ini )
+        CALL iom_rstput( kt, nitrst, numrow, 'ssh_ini', ssh_ini, lxios = lwxios)
+        CALL iom_rstput( kt, nitrst, numrow, 'e3t_ini', e3t_ini, lxios = lwxios)
+        CALL iom_rstput( kt, nitrst, numrow, 'hc_loc_ini', hc_loc_ini, lxios = lwxios)
+        CALL iom_rstput( kt, nitrst, numrow, 'sc_loc_ini', sc_loc_ini, lxios = lwxios)
         IF( .NOT. lk_vvl ) THEN
-           CALL iom_rstput( kt, nitrst, numrow, 'ssh_hc_loc_ini', ssh_hc_loc_ini )
-           CALL iom_rstput( kt, nitrst, numrow, 'ssh_sc_loc_ini', ssh_sc_loc_ini )
+           CALL iom_rstput( kt, nitrst, numrow, 'ssh_hc_loc_ini', ssh_hc_loc_ini, lxios = lwxios )
+           CALL iom_rstput( kt, nitrst, numrow, 'ssh_sc_loc_ini', ssh_sc_loc_ini, lxios = lwxios )
         ENDIF
         !
+        IF( lwxios ) CALL iom_swap(      cxios_context          )
      ENDIF
      !
Index: /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/daymod.F90
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/daymod.F90	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/daymod.F90	(revision 8079)
@@ -35,4 +35,5 @@
    USE timing          ! Timing
    USE restart         ! restart
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -359,8 +360,10 @@
          ENDIF
          ! calendar control
-         CALL iom_rstput( kt, nitrst, numrow, 'kt'     , REAL( kt    , wp) )   ! time-step
-         CALL iom_rstput( kt, nitrst, numrow, 'ndastp' , REAL( ndastp, wp) )   ! date
-         CALL iom_rstput( kt, nitrst, numrow, 'adatrj' , adatrj            )   ! number of elapsed days since
+         IF( lwxios ) CALL iom_swap(      wxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'kt'     , REAL( kt    , wp), lxios = lwxios )   ! time-step
+         CALL iom_rstput( kt, nitrst, numrow, 'ndastp' , REAL( ndastp, wp), lxios = lwxios )   ! date
+         CALL iom_rstput( kt, nitrst, numrow, 'adatrj' , adatrj, lxios = lwxios            )   ! number of elapsed days since
          !                                                                     ! the begining of the run [s]
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
Index: /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90	(revision 8079)
@@ -38,4 +38,5 @@
    USE timing          ! Timing
    USE lbclnk          ! ocean lateral boundary condition (or mpp link)
+   USE iom_def, ONLY:lwxios, wxioso ! write restart flag and output type
 
    IMPLICIT NONE
@@ -138,5 +139,6 @@
          &             nn_no   , cn_exp    , cn_ocerst_in, cn_ocerst_out, ln_rstart , ln_rstdate, nn_rstctl,   &
          &             nn_it000, nn_itend  , nn_date0    , nn_leapy     , nn_istate , nn_stock ,   &
-         &             nn_write, ln_dimgnnn, ln_mskland  , ln_cfmeta    , ln_clobber, nn_chunksz, nn_euler
+         &             nn_write, ln_dimgnnn, ln_mskland  , ln_cfmeta    , ln_clobber, nn_chunksz, nn_euler, &
+         &             nn_wxios
       NAMELIST/namdom/ nn_bathy, rn_bathy , rn_e3zps_min, rn_e3zps_rat, nn_msh, rn_hmin,   &
          &             nn_acc   , rn_atfp     , rn_rdt      , rn_rdtmin ,                  &
@@ -152,5 +154,5 @@
       INTEGER  ::   ios                 ! Local integer output status for namelist read
       !!----------------------------------------------------------------------
-
+      nn_wxios = 0
       REWIND( numnam_ref )              ! Namelist namrun in reference namelist : Parameters of the run
       READ  ( numnam_ref, namrun, IOSTAT = ios, ERR = 901)
@@ -193,4 +195,5 @@
          WRITE(numout,*) '      overwrite an existing file      ln_clobber = ', ln_clobber
          WRITE(numout,*) '      NetCDF chunksize (bytes)        nn_chunksz = ', nn_chunksz
+         WRITE(numout,*) '      Write restart using XIOS        nn_wxios   = ', nn_wxios
       ENDIF
 
@@ -305,4 +308,6 @@
       rdtmax    = rn_rdtmin
       rdth      = rn_rdth
+      if (nn_wxios > 0) lwxios = .TRUE. 
+      wxioso = nn_wxios
 
       REWIND( numnam_ref )              ! Namelist namcla in reference namelist : Cross land advection
Index: /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/domvvl.F90
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/domvvl.F90	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/domvvl.F90	(revision 8079)
@@ -134,4 +134,7 @@
       IF(lwp) WRITE(numout,*) '~~~~~~~~~~~~'
 
+      ! Set variables needed in iom for reastart write with XIOS
+      lr_vvl_ztilde = ln_vvl_ztilde
+      lr_vvl_layer   = ln_vvl_layer
       ! choose vertical coordinate (z_star, z_tilde or layer)
       ! ==========================
@@ -909,18 +912,19 @@
          !                                           ! all cases !
          !                                           ! --------- !
-         CALL iom_rstput( kt, nitrst, numrow, 'fse3t_b', fse3t_b(:,:,:) )
-         CALL iom_rstput( kt, nitrst, numrow, 'fse3t_n', fse3t_n(:,:,:) )
+         IF( lwxios ) CALL iom_swap(      wxios_context          ) 
+         CALL iom_rstput( kt, nitrst, numrow, 'fse3t_b', fse3t_b(:,:,:), lxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'fse3t_n', fse3t_n(:,:,:), lxios = lwxios )
          !                                           ! ----------------------- !
          IF( ln_vvl_ztilde .OR. ln_vvl_layer ) THEN  ! z_tilde and layer cases !
             !                                        ! ----------------------- !
-            CALL iom_rstput( kt, nitrst, numrow, 'tilde_e3t_b', tilde_e3t_b(:,:,:) )
-            CALL iom_rstput( kt, nitrst, numrow, 'tilde_e3t_n', tilde_e3t_n(:,:,:) )
+            CALL iom_rstput( kt, nitrst, numrow, 'tilde_e3t_b', tilde_e3t_b(:,:,:), lxios = lwxios)
+            CALL iom_rstput( kt, nitrst, numrow, 'tilde_e3t_n', tilde_e3t_n(:,:,:), lxios = lwxios)
          END IF
          !                                           ! -------------!    
          IF( ln_vvl_ztilde ) THEN                    ! z_tilde case !
             !                                        ! ------------ !
-            CALL iom_rstput( kt, nitrst, numrow, 'hdiv_lf', hdiv_lf(:,:,:) )
+            CALL iom_rstput( kt, nitrst, numrow, 'hdiv_lf', hdiv_lf(:,:,:), lxios = lwxios)
          ENDIF
-
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       IF( nn_timing == 1 )  CALL timing_stop('dom_vvl_rst')
Index: /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_flt.F90
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_flt.F90	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_flt.F90	(revision 8079)
@@ -51,4 +51,5 @@
    USE agrif_opa_interp
 #endif
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -405,6 +406,8 @@
 ! Caution : extra-hallow
 ! gcx and gcxb are defined as: DIMENSION(1-jpr2di:jpi+jpr2di,1-jpr2dj:jpj+jpr2dj)
-         CALL iom_rstput( kt, nitrst, numrow, 'gcx' , gcx (1:jpi,1:jpj) )
-         CALL iom_rstput( kt, nitrst, numrow, 'gcxb', gcxb(1:jpi,1:jpj) )
+         IF( lwxios ) CALL iom_swap(      wxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'gcx' , gcx (1:jpi,1:jpj), lxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'gcxb', gcxb(1:jpi,1:jpj), lxios = lwxios )
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
Index: /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90	(revision 8079)
@@ -48,4 +48,5 @@
    USE asminc          ! Assimilation increment
 #endif
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -1038,22 +1039,24 @@
       !
       ELSEIF( TRIM(cdrw) == 'WRITE' ) THEN
-         CALL iom_rstput( kt, nitrst, numrow, 'ub2_b'   , ub2_b  (:,:) )
-         CALL iom_rstput( kt, nitrst, numrow, 'vb2_b'   , vb2_b  (:,:) )
+         IF( lwxios ) CALL iom_swap(      wxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'ub2_b'   , ub2_b  (:,:), lxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'vb2_b'   , vb2_b  (:,:), lxios = lwxios )
          !
          IF (.NOT.ln_bt_av) THEN
-            CALL iom_rstput( kt, nitrst, numrow, 'sshbb_e'  , sshbb_e(:,:) ) 
-            CALL iom_rstput( kt, nitrst, numrow, 'ubb_e'    ,   ubb_e(:,:) )
-            CALL iom_rstput( kt, nitrst, numrow, 'vbb_e'    ,   vbb_e(:,:) )
-            CALL iom_rstput( kt, nitrst, numrow, 'sshb_e'   ,  sshb_e(:,:) )
-            CALL iom_rstput( kt, nitrst, numrow, 'ub_e'     ,    ub_e(:,:) )
-            CALL iom_rstput( kt, nitrst, numrow, 'vb_e'     ,    vb_e(:,:) )
+            CALL iom_rstput( kt, nitrst, numrow, 'sshbb_e'  , sshbb_e(:,:), lxios = lwxios ) 
+            CALL iom_rstput( kt, nitrst, numrow, 'ubb_e'    ,   ubb_e(:,:), lxios = lwxios )
+            CALL iom_rstput( kt, nitrst, numrow, 'vbb_e'    ,   vbb_e(:,:), lxios = lwxios )
+            CALL iom_rstput( kt, nitrst, numrow, 'sshb_e'   ,  sshb_e(:,:), lxios = lwxios )
+            CALL iom_rstput( kt, nitrst, numrow, 'ub_e'     ,    ub_e(:,:), lxios = lwxios )
+            CALL iom_rstput( kt, nitrst, numrow, 'vb_e'     ,    vb_e(:,:), lxios = lwxios )
          ENDIF
 #if defined key_agrif
          ! Save time integrated fluxes
          IF ( .NOT.Agrif_Root() ) THEN
-            CALL iom_rstput( kt, nitrst, numrow, 'ub2_i_b'  , ub2_i_b(:,:) )
-            CALL iom_rstput( kt, nitrst, numrow, 'vb2_i_b'  , vb2_i_b(:,:) )
-         ENDIF
-#endif
+            CALL iom_rstput( kt, nitrst, numrow, 'ub2_i_b'  , ub2_i_b(:,:), lxios = lwxios )
+            CALL iom_rstput( kt, nitrst, numrow, 'vb2_i_b'  , vb2_i_b(:,:), lxios = lwxios )
+         ENDIF
+#endif
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
Index: /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/in_out_manager.F90
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/in_out_manager.F90	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/in_out_manager.F90	(revision 8079)
@@ -49,4 +49,5 @@
    LOGICAL       ::   ln_clobber       !: clobber (overwrite) an existing file
    INTEGER       ::   nn_chunksz       !: chunksize (bytes) for NetCDF file (works only with iom_nf90 routines)
+   INTEGER       ::   nn_wxios         !: write resart using xios 0 - no, 1 - single, 2 - multiple file output
 #if defined key_netcdf4
    !!----------------------------------------------------------------------
@@ -152,4 +153,5 @@
    LOGICAL       ::   lsp_area = .TRUE.     !: to make a control print over a specific area
    CHARACTER(lc) ::   cxios_context         !: context name used in xios
+   CHARACTER(lc) ::   wxios_context = "nemo_rstw" !: context name used in xios to write restart file
 
    !!----------------------------------------------------------------------
Index: /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90	(revision 8079)
@@ -41,11 +41,16 @@
    USE dianam          ! build name of file
    USE xios
+   USE iom_def, ONLY : max_rst_fields, rst_fields, wxioso
 # endif
    USE ioipsl, ONLY :  ju2ymds    ! for calendar
    USE crs             ! Grid coarsening
+   USE sbc_oce, ONLY : lk_oasis, ln_coupled_iceshelf_fluxes, ln_apr_dyn, ln_rnf, nn_components, jp_iam_sas
+   USE diadct, ONLY : lk_diadct
 
    IMPLICIT NONE
+   !  values needed to set correctlyfiles in reast file when using XIOS for writing
+   LOGICAL, PUBLIC :: lr_vvl_ztilde, lr_vvl_layer, lr_traadv_cen2
+
    PUBLIC   !   must be public to be able to access iom_def through iom
-   
 #if defined key_iomput
    LOGICAL, PUBLIC, PARAMETER ::   lk_iomput = .TRUE.        !: iom_put flag
@@ -53,4 +58,5 @@
    LOGICAL, PUBLIC, PARAMETER ::   lk_iomput = .FALSE.       !: iom_put flag
 #endif
+
    PUBLIC iom_init, iom_swap, iom_open, iom_close, iom_setkt, iom_varid, iom_get, iom_gettime, iom_rstput, iom_put
    PUBLIC iom_getatt, iom_use, iom_context_finalize
@@ -62,4 +68,5 @@
    PRIVATE iom_set_domain_attr, iom_set_axis_attr, iom_set_field_attr, iom_set_file_attr, iom_get_file_attr, iom_set_grid_attr
    PRIVATE set_grid, set_grid_bounds, set_scalar, set_xmlatt, set_mooring, iom_update_file_name, iom_sdate
+   PRIVATE set_rst_vars, set_rstw_active
 # endif
 
@@ -85,5 +92,5 @@
 CONTAINS
 
-   SUBROUTINE iom_init( cdname ) 
+   SUBROUTINE iom_init( cdname, filename, it ) 
       !!----------------------------------------------------------------------
       !!                     ***  ROUTINE   ***
@@ -105,4 +112,7 @@
       !
       REAL(wp), ALLOCATABLE, DIMENSION(:,:) :: z_bnds
+      CHARACTER(len=*), OPTIONAL, INTENT(in)  :: filename 
+      LOGICAL :: lrst_context              ! is context related to restart
+      INTEGER, OPTIONAL :: it              ! timestep when subroutine was called
       !!----------------------------------------------------------------------
 #if ! defined key_xios2
@@ -116,4 +126,5 @@
       CALL xios_context_initialize(TRIM(clname), mpi_comm_opa)
       CALL iom_swap( cdname )
+      lrst_context =  (TRIM(cdname) == TRIM(wxios_context))
 
       ! calendar parameters
@@ -136,4 +147,5 @@
           &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
       END SELECT
+
 #endif
       ! horizontal grid definition
@@ -141,12 +153,13 @@
       CALL set_scalar
 
-      IF( TRIM(cdname) == TRIM(cxios_context) ) THEN  
-         CALL set_grid( "T", glamt, gphit ) 
-         CALL set_grid( "U", glamu, gphiu )
-         CALL set_grid( "V", glamv, gphiv )
-         CALL set_grid( "W", glamt, gphit )
+      IF( TRIM(cdname) == TRIM(cxios_context) .OR. lrst_context ) THEN  
+         CALL set_grid( "T", glamt, gphit, ln_mskland ) 
+         CALL set_grid( "U", glamu, gphiu, ln_mskland )
+         CALL set_grid( "V", glamv, gphiv, ln_mskland )
+         CALL set_grid( "W", glamt, gphit, ln_mskland )
          CALL set_grid_znl( gphit )
+         CALL set_grid("N",glamt, gphit, .FALSE.)        ! not masked values
          !
-         IF( ln_cfmeta ) THEN   ! Add additional grid metadata
+         IF( ln_cfmeta .AND. .NOT.lrst_context) THEN   ! Add additional grid metadata
             CALL iom_set_domain_attr("grid_T", area = e12t(nldi:nlei, nldj:nlej))
             CALL iom_set_domain_attr("grid_U", area = e12u(nldi:nlei, nldj:nlej))
@@ -163,13 +176,13 @@
          CALL dom_grid_crs   ! Save the parent grid information  & Switch to coarse grid domain
          !
-         CALL set_grid( "T", glamt_crs, gphit_crs ) 
-         CALL set_grid( "U", glamu_crs, gphiu_crs ) 
-         CALL set_grid( "V", glamv_crs, gphiv_crs ) 
-         CALL set_grid( "W", glamt_crs, gphit_crs ) 
+         CALL set_grid( "T", glamt_crs, gphit_crs, ln_mskland ) 
+         CALL set_grid( "U", glamu_crs, gphiu_crs, ln_mskland ) 
+         CALL set_grid( "V", glamv_crs, gphiv_crs, ln_mskland ) 
+         CALL set_grid( "W", glamt_crs, gphit_crs, ln_mskland ) 
          CALL set_grid_znl( gphit_crs )
           !
          CALL dom_grid_glo   ! Return to parent grid domain
          !
-         IF( ln_cfmeta ) THEN   ! Add additional grid metadata
+         IF( ln_cfmeta .AND. .NOT.lrst_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))
@@ -184,8 +197,8 @@
 
       ! vertical grid definition
-      CALL iom_set_axis_attr( "deptht", gdept_1d )
-      CALL iom_set_axis_attr( "depthu", gdept_1d )
-      CALL iom_set_axis_attr( "depthv", gdept_1d )
-      CALL iom_set_axis_attr( "depthw", gdepw_1d )
+      CALL iom_set_axis_attr( "deptht", paxis = gdept_1d )
+      CALL iom_set_axis_attr( "depthu", paxis = gdept_1d )
+      CALL iom_set_axis_attr( "depthv", paxis = gdept_1d )
+      CALL iom_set_axis_attr( "depthw", paxis = gdepw_1d )
 
       ! Add vertical grid bounds
@@ -227,5 +240,12 @@
       
       ! automatic definitions of some of the xml attributs
-      CALL set_xmlatt
+      IF( TRIM(cdname) == TRIM(wxios_context)) THEN
+!set names of the fields in restart file IF using XIOS to read/write data
+       CALL set_rst_vars()
+!set which fields are to be read from restart file
+       CALL set_rstw_active(filename, it)
+      ELSE
+       CALL set_xmlatt
+      ENDIF
 
       CALL set_1point
@@ -241,7 +261,363 @@
 
 #endif
-      
+
    END SUBROUTINE iom_init
 
+   
+   SUBROUTINE set_rst_vars()
+!set names for variables in restart file
+
+        rst_fields(:)%vname="NO_NAME";         rst_fields(:)%grid="NO_GRID"
+
+        rst_fields(1)%vname="rdt";             rst_fields(1)% grid="grid_scalar"
+        rst_fields(2)%vname="rdttra1";         rst_fields(2)% grid="grid_scalar"      
+        rst_fields(3)%vname="un";              rst_fields(3)% grid="grid_N_3D"
+        rst_fields(4)%vname="ub";              rst_fields(4)% grid="grid_N_3D"
+        rst_fields(5)%vname="vn";              rst_fields(5)% grid="grid_N_3D"
+        rst_fields(6)%vname="vb";              rst_fields(6)% grid="grid_N_3D"  
+        rst_fields(7)%vname="tn";              rst_fields(7)% grid="grid_N_3D"
+        rst_fields(8)%vname="tb";              rst_fields(8)% grid="grid_N_3D"
+        rst_fields(9)%vname="sn";              rst_fields(9)% grid="grid_N_3D"
+        rst_fields(10)%vname="sb";             rst_fields(10)%grid="grid_N_3D"
+        rst_fields(11)%vname="sshn";           rst_fields(11)%grid="grid_N"
+        rst_fields(12)%vname="sshb";           rst_fields(12)%grid="grid_N"
+        rst_fields(13)%vname="hdivn";          rst_fields(13)%grid="grid_N_3D"
+        rst_fields(14)%vname="hdivb";          rst_fields(14)%grid="grid_N_3D"
+        rst_fields(15)%vname="rhop";           rst_fields(15)%grid="grid_N_3D"
+        rst_fields(16)%vname="rotn";           rst_fields(16)%grid="grid_N_3D"
+        rst_fields(17)%vname="rotb";           rst_fields(17)%grid="grid_N_3D"
+        rst_fields(18)%vname="kt";             rst_fields(18)%grid="grid_scalar"
+        rst_fields(19)%vname="ndastp";         rst_fields(19)%grid="grid_scalar"
+        rst_fields(20)%vname="adatrj";         rst_fields(20)%grid="grid_scalar"
+        rst_fields(21)%vname="utau_b";         rst_fields(21)%grid="grid_N"
+        rst_fields(22)%vname="vtau_b";         rst_fields(22)%grid="grid_N"
+        rst_fields(23)%vname="qns_b";          rst_fields(23)%grid="grid_N"
+        rst_fields(24)%vname="emp_b";          rst_fields(24)%grid="grid_N"
+        rst_fields(25)%vname="sfx_b";          rst_fields(25)%grid="grid_N"
+        rst_fields(26)%vname="en" ;            rst_fields(26)%grid="grid_N_3D" 
+        rst_fields(27)%vname="avt";            rst_fields(27)%grid="grid_N_3D"
+        rst_fields(28)%vname="avm";            rst_fields(28)%grid="grid_N_3D"
+        rst_fields(29)%vname="avmu";           rst_fields(29)%grid="grid_N_3D"
+        rst_fields(30)%vname="avmv";           rst_fields(30)%grid="grid_N_3D"
+        rst_fields(31)%vname="dissl";          rst_fields(31)%grid="grid_N_3D"
+        rst_fields(32)%vname="sbc_hc_b";       rst_fields(32)%grid="grid_N"
+        rst_fields(33)%vname="sbc_sc_b";       rst_fields(33)%grid="grid_N"
+        rst_fields(34)%vname="qsr_hc_b";       rst_fields(34)%grid="grid_N_3D"
+        rst_fields(35)%vname="gcx";            rst_fields(35)%grid="grid_N"
+        rst_fields(36)%vname="gcxb";           rst_fields(36)%grid="grid_N"
+        rst_fields(37)%vname="fraqsr_1lev";    rst_fields(37)%grid="grid_N"
+        rst_fields(38)%vname="greenland_icesheet_mass"
+                                               rst_fields(38)%grid="grid_scalar"
+        rst_fields(39)%vname="greenland_icesheet_timelapsed"
+                                               rst_fields(39)%grid="grid_scalar"
+        rst_fields(40)%vname="greenland_icesheet_mass_roc"
+                                               rst_fields(40)%grid="grid_scalar"
+        rst_fields(41)%vname="antarctica_icesheet_mass"
+                                               rst_fields(41)%grid="grid_scalar"
+        rst_fields(42)%vname="antarctica_icesheet_timelapsed"
+                                               rst_fields(42)%grid="grid_scalar"
+        rst_fields(43)%vname="antarctica_icesheet_mass_roc"
+                                               rst_fields(43)%grid="grid_scalar"
+        rst_fields(44)%vname="rhd";            rst_fields(44)%grid="grid_N_3D"
+        rst_fields(45)%vname="frc_v";          rst_fields(45)%grid="grid_scalar"
+        rst_fields(46)%vname="frc_t";          rst_fields(46)%grid="grid_scalar"
+        rst_fields(47)%vname="frc_s";          rst_fields(47)%grid="grid_scalar"
+        rst_fields(48)%vname="frc_wn_t";       rst_fields(48)%grid="grid_scalar"
+        rst_fields(49)%vname="frc_wn_s";       rst_fields(49)%grid="grid_scalar"
+        rst_fields(50)%vname="ssh_ini";        rst_fields(50)%grid="grid_N"
+        rst_fields(51)%vname="e3t_ini";        rst_fields(51)%grid="grid_N_3D"
+        rst_fields(52)%vname="hc_loc_ini";     rst_fields(52)%grid="grid_N_3D"
+        rst_fields(53)%vname="sc_loc_ini";     rst_fields(53)%grid="grid_N_3D"
+        rst_fields(54)%vname="ssh_hc_loc_ini"; rst_fields(54)%grid="grid_N"
+        rst_fields(55)%vname="ssh_sc_loc_ini"; rst_fields(55)%grid="grid_N"
+        rst_fields(56)%vname="fse3t_b";        rst_fields(56)%grid="grid_N_3D"
+        rst_fields(57)%vname="fse3t_n";        rst_fields(57)%grid="grid_N_3D"
+        rst_fields(58)%vname="tilde_e3t_b";    rst_fields(58)%grid="grid_N"
+        rst_fields(59)%vname="tilde_e3t_n";    rst_fields(59)%grid="grid_N"
+        rst_fields(60)%vname="hdiv_lf";        rst_fields(60)%grid="grid_N"
+        rst_fields(61)%vname="ub2_b";          rst_fields(61)%grid="grid_N"
+        rst_fields(62)%vname="vb2_b";          rst_fields(62)%grid="grid_N"
+        rst_fields(63)%vname="sshbb_e";        rst_fields(63)%grid="grid_N"
+        rst_fields(64)%vname="ubb_e";          rst_fields(64)%grid="grid_N"
+        rst_fields(65)%vname="vbb_e";          rst_fields(65)%grid="grid_N"
+        rst_fields(66)%vname="sshb_e";         rst_fields(66)%grid="grid_N"
+        rst_fields(67)%vname="ub_e";           rst_fields(67)%grid="grid_N"
+        rst_fields(68)%vname="vb_e";           rst_fields(68)%grid="grid_N"
+        rst_fields(69)%vname="fwf_isf_b";      rst_fields(69)%grid="grid_N"
+        rst_fields(70)%vname="isf_sc_b";       rst_fields(70)%grid="grid_N"
+        rst_fields(71)%vname="isf_hc_b";       rst_fields(71)%grid="grid_N"
+        rst_fields(72)%vname="ssh_ibb";        rst_fields(72)%grid="grid_N"
+        rst_fields(73)%vname="rnf_b";          rst_fields(73)%grid="grid_N"
+        rst_fields(74)%vname="rnf_hc_b";       rst_fields(74)%grid="grid_N"
+        rst_fields(75)%vname="rnf_sc_b";       rst_fields(75)%grid="grid_N"
+        rst_fields(76)%vname="nn_fsbc";        rst_fields(76)%grid="grid_scalar"
+        rst_fields(77)%vname="ssu_m";          rst_fields(77)%grid="grid_N"
+        rst_fields(78)%vname="ssv_m";          rst_fields(78)%grid="grid_N"
+        rst_fields(79)%vname="sst_m";          rst_fields(79)%grid="grid_N"
+        rst_fields(80)%vname="sss_m";          rst_fields(80)%grid="grid_N"
+        rst_fields(81)%vname="ssh_m";          rst_fields(81)%grid="grid_N"
+        rst_fields(82)%vname="e3t_m";          rst_fields(82)%grid="grid_N"
+        rst_fields(83)%vname="frq_m";          rst_fields(83)%grid="grid_N"
+        rst_fields(84)%vname="avmb";           rst_fields(84)%grid="Vgrid"
+        rst_fields(85)%vname="avtb";           rst_fields(85)%grid="Vgrid"
+
+   END SUBROUTINE set_rst_vars
+
+
+   SUBROUTINE set_rstw_active(rst_file, it)
+!sets enabled = .TRUE. for each field in restart file
+   CHARACTER(len=*) :: rst_file
+   INTEGER, INTENT(in) :: it ! timestep when iom_init was called
+   TYPE(xios_field) :: field_hdl
+   TYPE(xios_file) :: file_hdl
+   TYPE(xios_filegroup) :: filegroup_hdl
+   INTEGER :: i
+
+!set then name of the restart file (OUTPUT!) and enable available fields
+        if(lwp) WRITE(numout,*) 'Setting (output) restart filename (for XIOS) to: ',TRIM(rst_file)
+        CALL xios_get_handle("file_definition", filegroup_hdl )
+        CALL xios_add_child(filegroup_hdl, file_hdl, 'wrestart')
+        IF(wxioso.eq.1) THEN
+            CALL xios_set_file_attr( "wrestart", type="one_file", name = TRIM(rst_file), &
+               enabled=.TRUE., mode="write", output_freq=xios_timestep)
+            if(lwp) write(numout,*) 'OPEN ', trim(rst_file), ' in one_file mode'
+        ELSE 
+            CALL xios_set_file_attr( "wrestart", type="multiple_file", name = TRIM(rst_file),&
+               enabled=.TRUE., mode="write", output_freq=xios_timestep)
+          if(lwp) write(numout,*) 'OPEN ', trim(rst_file), ' in multiple_file mode'
+        ENDIF
+
+        CALL xios_set_file_attr( "wrestart", name=trim(rst_file))
+        call flush(numout) 
+!      CALL xios_update_calendar(it+1)       ! + one because we open restart file 
+                                              ! 1 timestep before write
+
+!define fields for restart write context
+!in restart.F90
+        DO i= 1, 17
+           CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+           if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+        ENDDO
+!in daymod.F90
+        DO i= 18, 20
+                      CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+           if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+        ENDDO
+!end daymod.F90
+!sbcmod.F90
+        DO i= 21, 25
+           CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+           if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+        ENDDO
+!end sbcmod.F90
+!ALL FIELDS ABOUVE ALWAYS
+!zdftke.F90
+#if defined key_zdftke   ||   defined key_esopa
+        DO i= 26, 31
+           CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+           if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+        ENDDO
+#endif
+!end zdftke.F90
+!traqsr.F90
+       i = 34
+       CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+       CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")        
+       if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+       i = 37
+       CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+       CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")        
+       if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+!END traqsr.F90
+#if defined key_dynspg_flt   ||   defined key_esopa 
+!dynspg_flt.F90
+        DO i= 35, 36
+           CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+           if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+        ENDDO
+!end dynspg_flt.F90
+#endif
+!trasbc.F90 START
+        DO i= 32, 33
+           CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+           if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+        ENDDO
+        DO i= 69, 71
+           CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+           if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+        ENDDO
+!trasbc.F90 END
+        IF( lk_oasis) THEN
+        ! ln_coupled_iceshelf_fluxes uninitialised unless lk_oasis=true
+          IF( ln_coupled_iceshelf_fluxes ) THEN
+             DO i= 38, 43
+                CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+                if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+             ENDDO
+          ENDIF
+        ENDIF
+#if defined key_zdfkpp
+        i = 44
+        CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+             grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+        if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+#endif
+!dia_hsb_rst
+#if defined key_diadct
+        IF( lk_diadct     ) THEN
+             DO i= 45, 47
+                CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+                if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+             ENDDO
+
+             DO i= 50, 53
+                CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+                if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+             ENDDO
+             IF( .NOT. lk_vvl ) THEN
+             DO i= 48, 48
+                CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+                if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+             ENDDO
+             DO i= 54, 55
+                CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+                if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+             ENDDO
+             ENDIF
+        ENDIF
+#endif
+!end dia_hsb_rst
+!domvvl.F90
+        IF( lk_vvl ) THEN
+             DO i= 56, 57
+                CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+                if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+             ENDDO
+           IF( lr_vvl_ztilde .OR. lr_vvl_layer ) THEN  ! z_tilde and layer cases !
+             DO i= 58, 59
+                CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+                if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+             ENDDO
+            END IF
+            IF( lr_vvl_ztilde ) THEN                    ! z_tilde case !
+            i=60
+            CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+            if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+            ENDIF
+         ENDIF
+!end domvvl.F90
+!dynspg_ts.F90
+#if defined key_dynspg_ts   ||   defined key_esopa
+         DO i= 61, 62
+            CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                 grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+            if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+         ENDDO
+         IF (.NOT.ln_bt_av) THEN
+            DO i= 63, 68
+               CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+               CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                    grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+               if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+            ENDDO
+         ENDIF
+#if defined key_agrif
+         ! Save time integrated fluxes
+         IF ( .NOT.Agrif_Root() ) THEN
+            DO i= 84, 85
+               CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+               CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                    grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+               if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+            ENDDO
+         ENDIF
+#endif
+#endif
+!end dynspg_ts.F90
+!sbcapr.F90
+          IF( ln_apr_dyn) THEN
+             i = 72
+             CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+             if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+          ENDIF
+!end sbcapr.F90
+!sbcrnf.F90
+        IF( ln_rnf      ) THEN
+             DO i= 73, 75
+                CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+                if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+             ENDDO
+        ENDIF 
+!end sbcrnf.F90
+!sbcssm.F90
+        IF( nn_components /= jp_iam_sas .AND. nn_fsbc .NE. 1) THEN
+             DO i= 76, 81
+                CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+                if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+             ENDDO
+             i = 83
+             CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+             if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+             IF( lk_vvl )  THEN
+                i = 82
+                CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+                if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+             ENDIF 
+        ENDIF
+!end sbcssm.F90
+       IF( lr_traadv_cen2   ) THEN
+            DO i= 84, 85
+                CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
+                     grid_ref = TRIM(rst_fields(i)%grid ), operation = "instant")
+                if(lwp) write(numout,*) 'id= ',i,' set ', TRIM(rst_fields(i)%vname), ' enabled'
+             ENDDO
+       ENDIF
+
+   END SUBROUTINE set_rstw_active
 
    SUBROUTINE iom_swap( cdname )
@@ -255,4 +631,5 @@
       TYPE(xios_context) :: nemo_hdl
 
+      if(lwp) write(numout,*) 'iom_swap to: ',trim(cdname)
       IF( TRIM(Agrif_CFixed()) == '0' ) THEN
         CALL xios_get_handle(TRIM(cdname),nemo_hdl)
@@ -950,5 +1327,5 @@
             ELSEIF( PRESENT(pv_r2d) ) THEN
 !CDIR COLLAPSE
-               IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
+              IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
 !CDIR COLLAPSE
                IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
@@ -1055,5 +1432,5 @@
    !!                   INTERFACE iom_rstput
    !!----------------------------------------------------------------------
-   SUBROUTINE iom_rp0d( kt, kwrite, kiomid, cdvar, pvar, ktype )
+   SUBROUTINE iom_rp0d( kt, kwrite, kiomid, cdvar, pvar, ktype, lxios )
       INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
       INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
@@ -1062,20 +1439,32 @@
       REAL(wp)        , INTENT(in)                         ::   pvar     ! written field
       INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
+      LOGICAL, OPTIONAL :: lxios   ! xios write flag
+      LOGICAL :: lx                ! local xios write flag
       INTEGER :: ivid   ! variable id
-      IF( kiomid > 0 ) THEN
-         IF( iom_file(kiomid)%nfid > 0 ) THEN
-            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
-            SELECT CASE (iom_file(kiomid)%iolib)
-            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
-            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
-            CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pvar )
-            CASE DEFAULT     
-               CALL ctl_stop( 'iom_rp0d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
-            END SELECT
+
+      lx = .FALSE.
+      IF(PRESENT(lxios)) lx = lxios
+      IF( lx ) THEN
+#ifdef key_iomput
+       if(lwp) write(numout,*) 'RESTART: write (XIOS 0D) ',trim(cdvar)
+       CALL xios_send_field(trim(cdvar), pvar)
+#endif
+      ELSE
+         IF( kiomid > 0 ) THEN
+            IF( iom_file(kiomid)%nfid > 0 ) THEN
+               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
+               SELECT CASE (iom_file(kiomid)%iolib)
+               CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
+               CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
+               CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pvar )
+               CASE DEFAULT     
+                  CALL ctl_stop( 'iom_rp0d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
+               END SELECT
+            ENDIF
          ENDIF
       ENDIF
    END SUBROUTINE iom_rp0d
 
-   SUBROUTINE iom_rp1d( kt, kwrite, kiomid, cdvar, pvar, ktype )
+   SUBROUTINE iom_rp1d( kt, kwrite, kiomid, cdvar, pvar, ktype, lxios )
       INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
       INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
@@ -1084,20 +1473,32 @@
       REAL(wp)        , INTENT(in), DIMENSION(          :) ::   pvar     ! written field
       INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
+      LOGICAL, OPTIONAL :: lxios   ! xios write flag
+      LOGICAL :: lx                ! local xios write flag
       INTEGER :: ivid   ! variable id
-      IF( kiomid > 0 ) THEN
-         IF( iom_file(kiomid)%nfid > 0 ) THEN
-            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
-            SELECT CASE (iom_file(kiomid)%iolib)
-            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
-            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
-            CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r1d = pvar )
-            CASE DEFAULT     
-               CALL ctl_stop( 'iom_rp1d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
-            END SELECT
+
+      lx = .FALSE.
+      IF(PRESENT(lxios)) lx = lxios
+      IF( lx ) THEN
+#ifdef key_iomput
+       if(lwp) write(numout,*) 'RESTART: write (XIOS 1D) ',trim(cdvar)
+       CALL xios_send_field(trim(cdvar), pvar)
+#endif
+      ELSE
+         IF( kiomid > 0 ) THEN
+            IF( iom_file(kiomid)%nfid > 0 ) THEN
+               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
+               SELECT CASE (iom_file(kiomid)%iolib)
+               CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
+               CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
+               CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r1d = pvar )
+               CASE DEFAULT     
+                  CALL ctl_stop( 'iom_rp1d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
+               END SELECT
+            ENDIF
          ENDIF
       ENDIF
    END SUBROUTINE iom_rp1d
 
-   SUBROUTINE iom_rp2d( kt, kwrite, kiomid, cdvar, pvar, ktype )
+   SUBROUTINE iom_rp2d( kt, kwrite, kiomid, cdvar, pvar, ktype, lxios )
       INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
       INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
@@ -1106,20 +1507,32 @@
       REAL(wp)        , INTENT(in), DIMENSION(:,    :    ) ::   pvar     ! written field
       INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
+      LOGICAL, OPTIONAL :: lxios   ! xios write flag
+      LOGICAL :: lx                ! local xios write flag
       INTEGER :: ivid   ! variable id
-      IF( kiomid > 0 ) THEN
-         IF( iom_file(kiomid)%nfid > 0 ) THEN
-            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
-            SELECT CASE (iom_file(kiomid)%iolib)
-            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
-            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
-            CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r2d = pvar ) 
-            CASE DEFAULT     
-               CALL ctl_stop( 'iom_rp2d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
-            END SELECT
+
+      lx = .FALSE.
+      IF(PRESENT(lxios)) lx = lxios
+      IF( lx ) THEN
+#ifdef key_iomput
+       if(lwp) write(numout,*) 'RESTART: write (XIOS 2D) ',trim(cdvar)
+       CALL xios_send_field(trim(cdvar), pvar)
+#endif
+      ELSE
+         IF( kiomid > 0 ) THEN
+            IF( iom_file(kiomid)%nfid > 0 ) THEN
+               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
+               SELECT CASE (iom_file(kiomid)%iolib)
+               CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
+               CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
+               CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r2d = pvar ) 
+               CASE DEFAULT     
+                  CALL ctl_stop( 'iom_rp2d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
+               END SELECT
+            ENDIF
          ENDIF
       ENDIF
    END SUBROUTINE iom_rp2d
 
-   SUBROUTINE iom_rp3d( kt, kwrite, kiomid, cdvar, pvar, ktype )
+   SUBROUTINE iom_rp3d( kt, kwrite, kiomid, cdvar, pvar, ktype, lxios )
       INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
       INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
@@ -1128,15 +1541,27 @@
       REAL(wp)        , INTENT(in),       DIMENSION(:,:,:) ::   pvar     ! written field
       INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
+      LOGICAL, OPTIONAL :: lxios   ! xios write flag
+      LOGICAL :: lx                 ! local xios write flag
       INTEGER :: ivid   ! variable id
-      IF( kiomid > 0 ) THEN
-         IF( iom_file(kiomid)%nfid > 0 ) THEN
-            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
-            SELECT CASE (iom_file(kiomid)%iolib)
-            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
-            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
-            CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r3d = pvar )
-            CASE DEFAULT     
-               CALL ctl_stop( 'iom_rp3d: accepted IO library are only jpioipsl and jprstdimg' )
-            END SELECT
+
+      lx = .FALSE.
+      IF(PRESENT(lxios)) lx = lxios
+      IF( lx ) THEN
+#ifdef key_iomput
+       if(lwp) write(numout,*) 'RESTART: write (XIOS 3D) ',trim(cdvar)
+       CALL xios_send_field(trim(cdvar), pvar)
+#endif
+      ELSE
+         IF( kiomid > 0 ) THEN
+            IF( iom_file(kiomid)%nfid > 0 ) THEN
+               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
+               SELECT CASE (iom_file(kiomid)%iolib)
+               CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
+               CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
+               CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r3d = pvar )
+               CASE DEFAULT     
+                  CALL ctl_stop( 'iom_rp3d: accepted IO library are only jpioipsl and jprstdimg' )
+               END SELECT
+            ENDIF
          ENDIF
       ENDIF
@@ -1367,5 +1792,5 @@
 
 
-   SUBROUTINE set_grid( cdgrd, plon, plat )
+   SUBROUTINE set_grid( cdgrd, plon, plat, lmask )
       !!----------------------------------------------------------------------
       !!                     ***  ROUTINE set_grid  ***
@@ -1380,4 +1805,5 @@
       REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zmask
       INTEGER  :: ni,nj
+      LOGICAL :: lmask
       
       ni=nlei-nldi+1 ; nj=nlej-nldj+1
@@ -1392,5 +1818,5 @@
          &                                     latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /)))  
 
-      IF ( ln_mskland ) THEN
+      IF ( lmask ) THEN
          ! mask land points, keep values on coast line -> specific mask for U, V and W points
          SELECT CASE ( cdgrd )
@@ -1436,5 +1862,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/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom_def.F90
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom_def.F90	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom_def.F90	(revision 8079)
@@ -51,4 +51,9 @@
 !$AGRIF_DO_NOT_TREAT
    INTEGER, PUBLIC            ::   iom_open_init = 0   !: used to initialize iom_file(:)%nfid to 0
+!XIOS read restart   
+   LOGICAL, PUBLIC            ::   lwxios          !: read single file restart using XIOS
+   INTEGER, PUBLIC            ::   wxioso          !: type of restart file when writing using XIOS 1 - single, 2 - multiple
+
+
 
    TYPE, PUBLIC ::   file_descriptor
@@ -70,6 +75,15 @@
    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 = 85  
+
+
+   TYPE, PUBLIC :: RST_FIELD  
+    CHARACTER(len=30) :: vname ! names of variables in restart file
+    CHARACTER(len=30) :: grid
+   END TYPE RST_FIELD
+   TYPE(RST_FIELD), PUBLIC :: rst_fields(max_rst_fields)
+   !  values needed to set correctlyfiles in reast file when using XIOS for writing
 !$AGRIF_END_DO_NOT_TREAT
-
    !!=====================================================================
 END MODULE iom_def
Index: /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/restart.F90
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/restart.F90	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/restart.F90	(revision 8079)
@@ -26,4 +26,5 @@
    USE divcur          ! hor. divergence and curl      (div & cur routines)
    USE sbc_oce         ! for icesheet freshwater input variables
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -65,4 +66,5 @@
       !!----------------------------------------------------------------------
       !
+
       IF( kt == nit000 ) THEN   ! default definitions
          lrst_oce = .FALSE.   
@@ -84,5 +86,7 @@
       ! we open and define the ocean restart file one time step before writing the data (-> at nitrst - 1)
       ! except if we write ocean restart files every time step or if an ocean restart file was writen at nitend - 1
-      IF( kt == nitrst - 1 .OR. nstock == 1 .OR. ( kt == nitend .AND. .NOT. lrst_oce ) ) THEN
+      ! NO, because the data is written 2 times on nitrst - 1 and nitrst
+!     IF( kt == nitrst - 1 .OR. nstock == 1 .OR. ( kt == nitend .AND. .NOT. lrst_oce ) ) THEN
+      IF( kt == nitrst .OR. nstock == 1 ) THEN
          IF( nitrst <= nitend .AND. nitrst > 0 ) THEN 
             IF ( ln_rstdate ) THEN
@@ -103,20 +107,35 @@
             IF(lwp) THEN
                WRITE(numout,*)
-               SELECT CASE ( jprstlib )
-               CASE ( jprstdimg )   ;   WRITE(numout,*)                            &
-                   '             open ocean restart binary file: ',TRIM(clpath)//clname
-               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(lwxios) THEN
+                  WRITE(numout,*)                                                     &
+                      '        XIOS open ocean restart NetCDF file: ',TRIM(clpath)//TRIM(clname)
+               ELSE
+                  SELECT CASE ( jprstlib )
+                  CASE ( jprstdimg )   ;   WRITE(numout,*)                            &
+                      '             open ocean restart binary file: ',TRIM(clpath)//TRIM(clname)
+                  CASE DEFAULT         ;   WRITE(numout,*)                            &
+                      '             open ocean restart NetCDF file: ',TRIM(clpath)//TRIM(clname)
+                  END SELECT
+                  IF ( snc4set%luse )      WRITE(numout,*) '             opened for NetCDF4 chunking and compression'
+                  IF( kt == nitrst - 1 ) THEN   ;   WRITE(numout,*) '             kt = nitrst - 1 = ', kt
+                  ELSE                          ;   WRITE(numout,*) '             kt = '             , kt
+                  ENDIF
                ENDIF
             ENDIF
             !
-            CALL iom_open( TRIM(clpath)//TRIM(clname), numrow, ldwrt = .TRUE., kiolib = jprstlib )
+            IF(.NOT.lwxios) THEN
+               CALL iom_open( TRIM(clpath)//TRIM(clname), numrow, ldwrt = .TRUE., kiolib = jprstlib )
+            ELSE
+               CALL iom_init( wxios_context, TRIM(clpath)//TRIM(clname) )
+               CALL xios_update_calendar(nitrst)
+               CALL iom_swap(      cxios_context          )
+            ENDIF
+!HAVE TO set lrst_oce to FALSE after XIOS restart is written !!!!!!
+!           THIS CALL SHOULDN't be here we open restart file on iter-1, move it
+!           to SUBROUTINE day to set the flag on the step when we save data
             lrst_oce = .TRUE.
          ENDIF
       ENDIF
+
       !
    END SUBROUTINE rst_opn
@@ -134,41 +153,52 @@
       INTEGER, INTENT(in) ::   kt   ! ocean time-step
       !!----------------------------------------------------------------------
-
-                     CALL iom_rstput( kt, nitrst, numrow, 'rdt'    , rdt       )   ! dynamics time step
-                     CALL iom_rstput( kt, nitrst, numrow, 'rdttra1', rdttra(1) )   ! surface tracer time step
-
-                     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, 'rotb'   , rotb      )
-                     CALL iom_rstput( kt, nitrst, numrow, 'hdivb'  , hdivb     )
-                     CALL iom_rstput( kt, nitrst, numrow, 'sshb'   , sshb      )
+                     IF(lwxios) CALL iom_swap(      wxios_context          )
+                     CALL iom_rstput( kt, nitrst, numrow, 'rdt'    , rdt       , lxios = lwxios)   ! dynamics time step
+                     CALL iom_rstput( kt, nitrst, numrow, 'rdttra1', rdttra(1) , lxios = lwxios)   ! surface tracer time step
+
+                     CALL iom_rstput( kt, nitrst, numrow, 'ub'     , ub        , lxios = lwxios)     ! before fields
+                     CALL iom_rstput( kt, nitrst, numrow, 'vb'     , vb        , lxios = lwxios)
+                     CALL iom_rstput( kt, nitrst, numrow, 'tb'     , tsb(:,:,:,jp_tem) , lxios = lwxios)
+                     CALL iom_rstput( kt, nitrst, numrow, 'sb'     , tsb(:,:,:,jp_sal) , lxios = lwxios)
+                     CALL iom_rstput( kt, nitrst, numrow, 'rotb'   , rotb      , lxios = lwxios)
+                     CALL iom_rstput( kt, nitrst, numrow, 'hdivb'  , hdivb     , lxios = lwxios)
+                     CALL iom_rstput( kt, nitrst, numrow, 'sshb'   , sshb      , lxios = lwxios)
                      !
-                     CALL iom_rstput( kt, nitrst, numrow, 'un'     , un        )     ! now fields
-                     CALL iom_rstput( kt, nitrst, numrow, 'vn'     , vn        )
-                     CALL iom_rstput( kt, nitrst, numrow, 'tn'     , tsn(:,:,:,jp_tem) )
-                     CALL iom_rstput( kt, nitrst, numrow, 'sn'     , tsn(:,:,:,jp_sal) )
-                     CALL iom_rstput( kt, nitrst, numrow, 'rotn'   , rotn      )
-                     CALL iom_rstput( kt, nitrst, numrow, 'hdivn'  , hdivn     )
-                     CALL iom_rstput( kt, nitrst, numrow, 'sshn'   , sshn      )
-                     CALL iom_rstput( kt, nitrst, numrow, 'rhop'   , rhop      )
+                     CALL iom_rstput( kt, nitrst, numrow, 'un'     , un        , lxios = lwxios)     ! now fields
+                     CALL iom_rstput( kt, nitrst, numrow, 'vn'     , vn        , lxios = lwxios)
+                     CALL iom_rstput( kt, nitrst, numrow, 'tn'     , tsn(:,:,:,jp_tem) , lxios = lwxios)
+                     CALL iom_rstput( kt, nitrst, numrow, 'sn'     , tsn(:,:,:,jp_sal) , lxios = lwxios)
+                     CALL iom_rstput( kt, nitrst, numrow, 'rotn'   , rotn      , lxios = lwxios)
+                     CALL iom_rstput( kt, nitrst, numrow, 'hdivn'  , hdivn     , lxios = lwxios)
+                     CALL iom_rstput( kt, nitrst, numrow, 'sshn'   , sshn      , lxios = lwxios)
+                     CALL iom_rstput( kt, nitrst, numrow, 'rhop'   , rhop      , lxios = lwxios)
 #if defined key_zdfkpp
-                     CALL iom_rstput( kt, nitrst, numrow, 'rhd'    , rhd       )
+                     CALL iom_rstput( kt, nitrst, numrow, 'rhd'    , rhd       , lxios = lwxios)
 #endif
                      IF( lk_oasis) THEN
                      ! ln_coupled_iceshelf_fluxes uninitialised unless lk_oasis=true
                      IF( ln_coupled_iceshelf_fluxes ) THEN
-                        CALL iom_rstput( kt, nitrst, numrow, 'greenland_icesheet_mass', greenland_icesheet_mass )
-                        CALL iom_rstput( kt, nitrst, numrow, 'greenland_icesheet_timelapsed', greenland_icesheet_timelapsed )
-                        CALL iom_rstput( kt, nitrst, numrow, 'greenland_icesheet_mass_roc', greenland_icesheet_mass_rate_of_change )
-                        CALL iom_rstput( kt, nitrst, numrow, 'antarctica_icesheet_mass', antarctica_icesheet_mass )
-                        CALL iom_rstput( kt, nitrst, numrow, 'antarctica_icesheet_timelapsed', antarctica_icesheet_timelapsed )
-                        CALL iom_rstput( kt, nitrst, numrow, 'antarctica_icesheet_mass_roc', antarctica_icesheet_mass_rate_of_change )
+                        CALL iom_rstput( kt, nitrst, numrow, 'greenland_icesheet_mass', greenland_icesheet_mass ,&
+      &                                   lxios = lwxios)
+                        CALL iom_rstput( kt, nitrst, numrow, 'greenland_icesheet_timelapsed', greenland_icesheet_timelapsed ,&
+      &                                   lxios = lwxios)
+                        CALL iom_rstput( kt, nitrst, numrow, 'greenland_icesheet_mass_roc', greenland_icesheet_mass_rate_of_change ,&
+      &                                   lxios = lwxios)
+                        CALL iom_rstput( kt, nitrst, numrow, 'antarctica_icesheet_mass', antarctica_icesheet_mass , &
+      &                                  lxios = lwxios)
+                        CALL iom_rstput( kt, nitrst, numrow, 'antarctica_icesheet_timelapsed', antarctica_icesheet_timelapsed ,&
+      &                                   lxios = lwxios)
+                        CALL iom_rstput( kt, nitrst, numrow, 'antarctica_icesheet_mass_roc', &
+      &                                  antarctica_icesheet_mass_rate_of_change , lxios = lwxios)
                      ENDIF
                      ENDIF
 
       IF( kt == nitrst ) THEN
-         CALL iom_close( numrow )     ! close the restart file (only at last time step)
+         IF(.NOT.lwxios) THEN
+            CALL iom_close( numrow )     ! close the restart file (only at last time step)
+         ELSE
+            CALL iom_context_finalize(      wxios_context          )
+            CALL iom_swap(      cxios_context          )
+         ENDIF
 !!gm         IF( .NOT. lk_trdmld )   lrst_oce = .FALSE.
 !!gm  not sure what to do here   ===>>>  ask to Sebastian
@@ -178,5 +208,4 @@
                nitrst = nstocklist( nrst_lst )
             ENDIF
-            lrst_oce = .FALSE.
       ENDIF
       !
@@ -318,5 +347,4 @@
       !
    END SUBROUTINE rst_read
-
    !!=====================================================================
 END MODULE restart
Index: /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcapr.F90
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcapr.F90	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcapr.F90	(revision 8079)
@@ -19,4 +19,5 @@
    USE iom             ! IOM library
    USE lib_mpp         ! MPP library
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -156,5 +157,7 @@
          IF(lwp) WRITE(numout,*) 'sbc_apr : ssh_ib written in ocean restart file at it= ', kt,' date= ', ndastp
          IF(lwp) WRITE(numout,*) '~~~~'
-         CALL iom_rstput( kt, nitrst, numrow, 'ssh_ibb' , ssh_ib )
+         IF( lwxios ) CALL iom_swap(      wxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'ssh_ibb' , ssh_ib, lxios = lwxios )
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
Index: /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcmod.F90
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcmod.F90	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcmod.F90	(revision 8079)
@@ -54,4 +54,5 @@
    USE sbcwave          ! Wave module
    USE bdy_par          ! Require lk_bdy
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -443,11 +444,13 @@
             &                    'at it= ', kt,' date= ', ndastp
          IF(lwp) WRITE(numout,*) '~~~~'
-         CALL iom_rstput( kt, nitrst, numrow, 'utau_b' , utau )
-         CALL iom_rstput( kt, nitrst, numrow, 'vtau_b' , vtau )
-         CALL iom_rstput( kt, nitrst, numrow, 'qns_b'  , qns  )
+         IF( lwxios ) CALL iom_swap(      wxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'utau_b' , utau, lxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'vtau_b' , vtau, lxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'qns_b'  , qns, lxios = lwxios  )
          ! The 3D heat content due to qsr forcing is treated in traqsr
          ! CALL iom_rstput( kt, nitrst, numrow, 'qsr_b'  , qsr  )
-         CALL iom_rstput( kt, nitrst, numrow, 'emp_b'  , emp  )
-         CALL iom_rstput( kt, nitrst, numrow, 'sfx_b'  , sfx  )
+         CALL iom_rstput( kt, nitrst, numrow, 'emp_b'  , emp, lxios = lwxios  )
+         CALL iom_rstput( kt, nitrst, numrow, 'sfx_b'  , sfx, lxios = lwxios  )
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
 
Index: /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcrnf.F90
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcrnf.F90	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcrnf.F90	(revision 8079)
@@ -27,4 +27,5 @@
    USE eosbn2
    USE wrk_nemo        ! Memory allocation
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -171,7 +172,9 @@
             &                    'at it= ', kt,' date= ', ndastp
          IF(lwp) WRITE(numout,*) '~~~~'
-         CALL iom_rstput( kt, nitrst, numrow, 'rnf_b' , rnf )
-         CALL iom_rstput( kt, nitrst, numrow, 'rnf_hc_b', rnf_tsc(:,:,jp_tem) )
-         CALL iom_rstput( kt, nitrst, numrow, 'rnf_sc_b', rnf_tsc(:,:,jp_sal) )
+         IF( lwxios ) CALL iom_swap(      wxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'rnf_b' , rnf, lxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'rnf_hc_b', rnf_tsc(:,:,jp_tem), lxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'rnf_sc_b', rnf_tsc(:,:,jp_sal), lxios = lwxios )
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
Index: /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcssm.F90
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcssm.F90	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcssm.F90	(revision 8079)
@@ -21,4 +21,5 @@
    USE prtctl          ! Print control
    USE iom             ! IOM library
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -156,13 +157,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  )
-            IF( lk_vvl )   CALL iom_rstput( kt, nitrst, numrow, 'e3t_m'  , e3t_m  )
-            CALL iom_rstput( kt, nitrst, numrow, 'frq_m'  , frq_m  )
-            !
+            IF( lwxios ) CALL iom_swap(      wxios_context          )
+            CALL iom_rstput( kt, nitrst, numrow, 'nn_fsbc', zf_sbc, lxios = lwxios )    ! sbc frequency
+            CALL iom_rstput( kt, nitrst, numrow, 'ssu_m'  , ssu_m, lxios = lwxios  )    ! sea surface mean fields
+            CALL iom_rstput( kt, nitrst, numrow, 'ssv_m'  , ssv_m, lxios = lwxios  )
+            CALL iom_rstput( kt, nitrst, numrow, 'sst_m'  , sst_m, lxios = lwxios  )
+            CALL iom_rstput( kt, nitrst, numrow, 'sss_m'  , sss_m, lxios = lwxios  )
+            CALL iom_rstput( kt, nitrst, numrow, 'ssh_m'  , ssh_m, lxios = lwxios  )
+            IF( lk_vvl )   CALL iom_rstput( kt, nitrst, numrow, 'e3t_m'  , e3t_m   )
+            CALL iom_rstput( kt, nitrst, numrow, 'frq_m'  , frq_m, lxios = lwxios )
+            !
+            IF( lwxios ) CALL iom_swap(      cxios_context          )
          ENDIF
          !
Index: /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traadv.F90
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traadv.F90	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traadv.F90	(revision 8079)
@@ -46,5 +46,6 @@
 
    !                              !!* Namelist namtra_adv *
-   LOGICAL ::   ln_traadv_cen2     ! 2nd order centered scheme flag
+!make ln_traadv_cen2 public. It's needed when XIOS is used to write restart file
+   LOGICAL, PUBLIC ::   ln_traadv_cen2     ! 2nd order centered scheme flag
    LOGICAL ::   ln_traadv_tvd      ! TVD scheme flag
    LOGICAL ::   ln_traadv_tvd_zts  ! TVD scheme flag with vertical sub time-stepping
@@ -257,4 +258,5 @@
       !
       CALL tra_adv_mle_init          ! initialisation of the Mixed Layer Eddy parametrisation (MLE)
+      lr_traadv_cen2 = ln_traadv_cen2
       !
    END SUBROUTINE tra_adv_init
Index: /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traadv_cen2.F90
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traadv_cen2.F90	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traadv_cen2.F90	(revision 8079)
@@ -34,4 +34,5 @@
    USE timing          ! Timing
    USE phycst
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -286,6 +287,8 @@
       ! avmb, avtb will be read in zdfini in restart case as they are used in zdftke, kpp etc...
       IF( lrst_oce .AND. cdtype == 'TRA' ) THEN
-         CALL iom_rstput( kt, nitrst, numrow, 'avmb', avmb )
-         CALL iom_rstput( kt, nitrst, numrow, 'avtb', avtb )
+         IF( lwxios ) CALL iom_swap(      wxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'avmb', avmb, lxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'avtb', avtb, lxios = lwxios )
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
Index: /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traqsr.F90
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traqsr.F90	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traqsr.F90	(revision 8079)
@@ -33,4 +33,5 @@
    USE wrk_nemo       ! Memory Allocation
    USE timing         ! Timing
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -368,6 +369,8 @@
             &                    'at it= ', kt,' date= ', ndastp
          IF(lwp) WRITE(numout,*) '~~~~'
-         CALL iom_rstput( kt, nitrst, numrow, 'qsr_hc_b'   , qsr_hc      )
-         CALL iom_rstput( kt, nitrst, numrow, 'fraqsr_1lev', fraqsr_1lev )   ! default definition in sbcssm 
+         IF( lwxios ) CALL iom_swap(      wxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'qsr_hc_b', qsr_hc, lxios = lwxios                )
+         CALL iom_rstput( kt, nitrst, numrow, 'fraqsr_1lev', fraqsr_1lev, lxios = lwxios)   ! default definition in sbcssm 
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
          !
       ENDIF
Index: /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/trasbc.F90
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/trasbc.F90	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/trasbc.F90	(revision 8079)
@@ -33,4 +33,5 @@
    USE timing          ! Timing
    USE eosbn2
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -209,6 +210,8 @@
             &                    'at it= ', kt,' date= ', ndastp
          IF(lwp) WRITE(numout,*) '~~~~'
-         CALL iom_rstput( kt, nitrst, numrow, 'sbc_hc_b', sbc_tsc(:,:,jp_tem) )
-         CALL iom_rstput( kt, nitrst, numrow, 'sbc_sc_b', sbc_tsc(:,:,jp_sal) )
+         IF( lwxios ) CALL iom_swap(      wxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'sbc_hc_b', sbc_tsc(:,:,jp_tem), lxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'sbc_sc_b', sbc_tsc(:,:,jp_sal), lxios = lwxios )
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
       !
@@ -253,7 +256,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(      wxios_context          )
+            CALL iom_rstput( kt, nitrst, numrow, 'fwf_isf_b', fwfisf(:,:)         , lxios = lwxios)
+            CALL iom_rstput( kt, nitrst, numrow, 'isf_hc_b' , risf_tsc(:,:,jp_tem), lxios = lwxios)
+            CALL iom_rstput( kt, nitrst, numrow, 'isf_sc_b' , risf_tsc(:,:,jp_sal), lxios = lwxios)
+            IF( lwxios ) CALL iom_swap(      cxios_context          )
          ENDIF
       END IF
Index: /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/ZDF/zdftke.F90
===================================================================
--- /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/ZDF/zdftke.F90	(revision 8078)
+++ /branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/ZDF/zdftke.F90	(revision 8079)
@@ -53,4 +53,5 @@
    USE timing         ! Timing
    USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)  
+   USE iom_def, ONLY : lwxios
 #if defined key_agrif
    USE agrif_opa_interp
@@ -970,10 +971,12 @@
         !                                   ! -------------------
         IF(lwp) WRITE(numout,*) '---- tke-rst ----'
-        CALL iom_rstput( kt, nitrst, numrow, 'en'   , en     )
-        CALL iom_rstput( kt, nitrst, numrow, 'avt'  , avt_k  )
-        CALL iom_rstput( kt, nitrst, numrow, 'avm'  , avm_k  )
-        CALL iom_rstput( kt, nitrst, numrow, 'avmu' , avmu_k )
-        CALL iom_rstput( kt, nitrst, numrow, 'avmv' , avmv_k )
-        CALL iom_rstput( kt, nitrst, numrow, 'dissl', dissl  )
+        IF( lwxios ) CALL iom_swap(      wxios_context          )
+        CALL iom_rstput( kt, nitrst, numrow, 'en'   , en    , lxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'avt'  , avt_k , lxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'avm'  , avm_k , lxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'avmu' , avmu_k, lxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'avmv' , avmv_k, lxios = lwxios )
+        CALL iom_rstput( kt, nitrst, numrow, 'dissl', dissl , lxios = lwxios )
+        IF( lwxios ) CALL iom_swap(      cxios_context          )
         !
      ENDIF
