Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/DIA/diahsb.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/DIA/diahsb.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/DIA/diahsb.F90	(revision 12950)
@@ -262,4 +262,5 @@
             IF(lwp) WRITE(numout,*) '   dia_hsb_rst : read hsb restart at it= ', kt,' date= ', ndastp
             IF(lwp) WRITE(numout,*)
+            IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
             CALL iom_get( numror, 'frc_v', frc_v, ldxios = lrxios )
             CALL iom_get( numror, 'frc_t', frc_t, ldxios = lrxios )
@@ -279,4 +280,5 @@
                CALL iom_get( numror, jpdom_autoglo, 'ssh_sc_loc_ini', ssh_sc_loc_ini, ldxios = lrxios )
             ENDIF
+            IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
          ELSE
             IF(lwp) WRITE(numout,*)
@@ -380,21 +382,4 @@
       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: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/DOM/daymod.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/DOM/daymod.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/DOM/daymod.F90	(revision 12950)
@@ -148,12 +148,4 @@
       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
 
@@ -323,5 +315,5 @@
 
       IF( TRIM(cdrw) == 'READ' ) THEN
-
+         IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
          IF( iom_varid( numror, 'kt', ldstop = .FALSE. ) > 0 ) THEN
             ! Get Calendar informations
@@ -391,4 +383,5 @@
             adatrj = ( REAL( nit000-1, wp ) * rn_Dt ) / rday
          ENDIF
+         IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
          IF( ABS(adatrj  - REAL(NINT(adatrj),wp)) < 0.1 / rday )   adatrj = REAL(NINT(adatrj),wp)   ! avoid truncation error
          !
@@ -409,5 +402,12 @@
          ENDIF
          ! calendar control
-         IF( lwxios ) CALL iom_swap(      cwxios_context          )
+         IF( lwxios ) THEN
+            CALL iom_swap(      cwxios_context          )
+!           IF( kt == nitrst ) THEN
+!               CALL iom_init_closedef(cwxios_context)
+!               CALL iom_setkt( kt - nit000 + 1,      cwxios_context          )
+!               CALL iom_swap(      cwxios_context          )
+!           ENDIF
+         ENDIF
          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
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/DOM/domain.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/DOM/domain.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/DOM/domain.F90	(revision 12950)
@@ -123,9 +123,4 @@
       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
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/DOM/domvvl.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/DOM/domvvl.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/DOM/domvvl.F90	(revision 12950)
@@ -270,22 +270,4 @@
       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
-      !
    END SUBROUTINE dom_vvl_zgr
 
@@ -793,4 +775,5 @@
          IF( ln_rstart ) THEN                   !* Read the restart file
             CALL rst_read_open                  !  open the restart file if necessary
+            IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
             CALL iom_get( numror, jpdom_autoglo, 'sshn'   , ssh(:,:,Kmm), ldxios = lrxios    )
             !
@@ -869,4 +852,5 @@
                ENDIF
             ENDIF
+            IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
             !
          ELSE                                   !* Initialize at "rest"
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/DYN/dynspg_ts.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/DYN/dynspg_ts.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/DYN/dynspg_ts.F90	(revision 12950)
@@ -890,4 +890,5 @@
       IF( TRIM(cdrw) == 'READ' ) THEN        ! Read/initialise 
          !                                   ! ---------------
+         IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
          IF( ln_rstart .AND. ln_bt_fw .AND. (.NOT.l_1st_euler) ) THEN    !* Read the restart file
             CALL iom_get( numror, jpdom_autoglo, 'ub2_b'  , ub2_b  (:,:), ldxios = lrxios )   
@@ -922,4 +923,5 @@
 #endif
          ENDIF
+         IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
          !
       ELSEIF( TRIM(cdrw) == 'WRITE' ) THEN   ! Create restart file
@@ -1039,28 +1041,4 @@
       !                             ! read restart when needed
       CALL ts_rst( nit000, 'READ' )
-      !
-      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')
-         CALL iom_set_rstw_var_active('un_bf')
-         CALL iom_set_rstw_var_active('vn_bf')
-         !
-         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: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/iom.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/iom.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/iom.F90	(revision 12950)
@@ -46,4 +46,6 @@
    USE lib_fortran 
    USE diu_bulk, ONLY : ln_diurnal_only, ln_diurnal
+   USE iom_nf90
+   USE netcdf
 
    IMPLICIT NONE
@@ -65,7 +67,7 @@
    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_sdate
-   PRIVATE iom_set_rst_context, iom_set_rstw_active, iom_set_rstr_active
+   PRIVATE iom_set_rst_context, iom_set_rstr_active
 # endif
-   PUBLIC iom_set_rstw_var_active, iom_set_rstw_core, iom_set_rst_vars
+   PRIVATE iom_set_rstw_active
 
    INTERFACE iom_get
@@ -261,13 +263,8 @@
 !set names of the fields in restart file IF using XIOS to read data
           CALL iom_set_rst_context(.TRUE.)
-          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(.FALSE.)
-          CALL iom_set_rst_vars(rst_wfields)
-!set which fields are to be written to a restart file
-          CALL iom_set_rstw_active(fname)
+          CALL iom_set_rstw_file(fname)
       ELSE
           CALL set_xmlatt
@@ -291,5 +288,5 @@
    END SUBROUTINE iom_init
 
-   SUBROUTINE iom_init_closedef
+   SUBROUTINE iom_init_closedef(cdname)
       !!----------------------------------------------------------------------
       !!            ***  SUBROUTINE iom_init_closedef  ***
@@ -299,8 +296,15 @@
       !!
       !!----------------------------------------------------------------------
-
+   CHARACTER(len=*), OPTIONAL, INTENT(IN) :: cdname
 #if defined key_iomput
+      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(.FALSE.)
+      ENDIF
+
       CALL xios_close_context_definition()
-      CALL xios_update_calendar( 0 )
+
+      IF(.NOT. (TRIM(cdname) == TRIM(cwxios_context)))  CALL xios_update_calendar( 0 )
+
 #else
       IF( .FALSE. )   WRITE(numout,*) 'iom_init_closedef: should not see this'   ! useless statement to avoid compilation warnings
@@ -308,37 +312,4 @@
 
    END SUBROUTINE iom_init_closedef
-
-   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
-   CHARACTER(LEN=256) :: clinfo    ! info character
-
-#if defined key_iomput
-   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
-      WRITE(ctmp1,*) 'iom_set_rstw_var_active: variable ', field ,' is available for writing but not defined' 
-      CALL ctl_stop( 'iom_set_rstw_var_active:', ctmp1 )
-   ENDIF
-#else
-        clinfo = 'iom_set_rstw_var_active: key_iomput is needed to use XIOS restart read/write functionality'
-        CALL ctl_stop('STOP', TRIM(clinfo))
-#endif
-
-   END SUBROUTINE iom_set_rstw_var_active
 
    SUBROUTINE iom_set_rstr_active()
@@ -352,11 +323,33 @@
 !sets enabled = .TRUE. for each field in restart file
    CHARACTER(len=256) :: rst_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
+   INTEGER                                    :: ndims, nvars, natts, unlimitedDimId, dimlen, xtype,mdims
+   TYPE(xios_field)                           :: field_hdl
+   TYPE(xios_file)                            :: file_hdl
+   TYPE(xios_filegroup)                       :: filegroup_hdl
+   INTEGER                                    :: dimids(4), jv,i, idim
+   CHARACTER(LEN=lc)                          :: clpath
+   CHARACTER(LEN=256)                         :: clinfo               ! info character
+   INTEGER, ALLOCATABLE                       :: indimlens(:)
+   CHARACTER(LEN=nf90_max_name), ALLOCATABLE  :: indimnames(:)
+   CHARACTER(LEN=nf90_max_name)               :: dimname, varname
+   LOGICAL                                    :: lmeta
+
+!failed to build with AGRIF
+!      meta(1:NMETA) = ["nav_lat",        &
+!            "nav_lon", "nav_lev", "time_instant",                  &
+!            "time_instant_bounds", "time_counter",                 &
+!            "time_counter_bounds", "x", "y", "numcat"]
+
+        meta(1) = "nav_lat"
+        meta(2) = "nav_lon"
+        meta(3) = "nav_lev"
+        meta(4) = "time_instant"
+        meta(5) = "time_instant_bounds"
+        meta(6) = "time_counter"
+        meta(7) = "time_counter_bounds"
+        meta(8) = "x"
+        meta(9) = "y"
+        meta(10) = "numcat"
 
         clpath = TRIM(cn_ocerst_indir)
@@ -367,6 +360,8 @@
            rst_file = TRIM(clpath)//TRIM(Agrif_CFixed())//'_'//TRIM(cn_ocerst_in)
         ENDIF
+
+        clinfo = '          iom_set_rstr_active, file: '//TRIM(rst_file)
+
 !set name of the restart file and enable available fields
-        if(lwp) WRITE(numout,*) 'Setting restart filename (for XIOS) to: ', TRIM(rst_file)
         CALL xios_get_handle("file_definition", filegroup_hdl )
         CALL xios_add_child(filegroup_hdl, file_hdl, 'rrestart')
@@ -374,188 +369,62 @@
              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="nav_lev", 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="nav_lev", 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
+
+        CALL iom_nf90_check( nf90_inquire( iom_file(numror)%nfid, ndims, nvars, natts ), clinfo )
+        ALLOCATE(indimlens(ndims), indimnames(ndims))
+        CALL iom_nf90_check( nf90_inquire( iom_file(numror)%nfid, unlimitedDimId = unlimitedDimId ), clinfo )
+
+        DO idim = 1, ndims
+           CALL iom_nf90_check( nf90_inquire_dimension( iom_file(numror)%nfid, idim, dimname, dimlen ), clinfo )
+           indimlens(idim) = dimlen
+           indimnames(idim) = dimname
+        ENDDO
+
+        DO jv =1, nvars
+            lmeta = .FALSE.
+            CALL iom_nf90_check( nf90_inquire_variable( iom_file(numror)%nfid, jv, varname, xtype, ndims, dimids, natts ), clinfo )
+            DO i = 1, NMETA
+               IF(TRIM(varname) == TRIM(meta(i))) THEN
+                  lmeta = .TRUE.
+               ENDIF
+            ENDDO
+            IF(.NOT.lmeta) THEN
+               CALL xios_add_child(file_hdl, field_hdl, TRIM(varname))
+               mdims = ndims
+
+               IF(ANY(dimids(1:ndims) == unlimitedDimId)) THEN
+                mdims = mdims - 1
+               ENDIF
+
+               IF(mdims == 3) THEN
+                      CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(varname),   &
+                                   domain_ref="grid_N", axis_ref=TRIM(iom_axis(indimlens(ndims))), &
+                                   prec = 8, operation = "instant")
+               ELSEIF(mdims == 2) THEN
+                      CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(varname), &
+                                          domain_ref="grid_N", prec = 8, operation = "instant") 
+               ELSEIF(mdims == 1) THEN
+                      CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(varname), &
+                                          axis_ref=TRIM(iom_axis(indimlens(ndims))), prec = 8, operation = "instant")
+               ELSEIF(mdims == 0) THEN
+                      CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(varname), &
+                                                scalar_ref = "grid_scalar", prec = 8, operation = "instant")
+               ELSE
+                   WRITE(ctmp1,*) 'iom_set_rstr_active: variable ', TRIM(varname) ,' incorrect number of dimensions' 
+                   CALL ctl_stop( 'iom_set_rstr_active:', ctmp1 )
+               ENDIF
+            ENDIF
+        ENDDO
+        DEALLOCATE(indimlens, indimnames)
 #endif
    END SUBROUTINE iom_set_rstr_active
 
-   SUBROUTINE iom_set_rstw_core(cdmdl)
+   SUBROUTINE iom_set_rstw_file(cdrst_file)
       !!---------------------------------------------------------------------
-      !!                   ***  SUBROUTINE  iom_set_rstw_core  ***
-      !!
-      !! ** Purpose :  set variables which are always in restart file 
+      !!                   ***  SUBROUTINE iom_set_rstw_file   ***
+      !!
+      !! ** Purpose :  define file name in XIOS context for writing restart
       !!---------------------------------------------------------------------
-   CHARACTER (len=*), INTENT (IN) :: cdmdl ! model OPA or SAS
-   CHARACTER(LEN=256)             :: clinfo    ! info character
-#if defined key_iomput
-   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')
-      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
-#else
-        clinfo = 'iom_set_rstw_core: key_iomput is needed to use XIOS restart read/write functionality'
-        CALL ctl_stop('STOP', TRIM(clinfo))
-#endif
-   END SUBROUTINE iom_set_rstw_core
-
-   SUBROUTINE iom_set_rst_vars(fields)
-      !!---------------------------------------------------------------------
-      !!                   ***  SUBROUTINE iom_set_rst_vars   ***
-      !!
-      !! ** 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_k";            fields(i)%grid="grid_N_3D"
-        i = i + 1; fields(i)%vname="avm_k";            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="hmxl_n";         fields(i)%grid="grid_N_3D"
-        i = i + 1; fields(i)%vname="un_bf";          fields(i)%grid="grid_N"
-        i = i + 1; fields(i)%vname="vn_bf";          fields(i)%grid="grid_N"
-        i = i + 1; fields(i)%vname="hbl";            fields(i)%grid="grid_N"
-        i = i + 1; fields(i)%vname="hbli";           fields(i)%grid="grid_N"
-        i = i + 1; fields(i)%vname="wn";             fields(i)%grid="grid_N_3D"
-        i = i + 1; fields(i)%vname="fwfisf_cav_b";   fields(i)%grid="grid_N"
-        i = i + 1; fields(i)%vname="isf_hc_cav_b";   fields(i)%grid="grid_N"
-        i = i + 1; fields(i)%vname="isf_sc_cav_b";   fields(i)%grid="grid_N"
-
-        IF( i-1 > max_rst_fields) THEN
-           WRITE(ctmp1,*) 'E R R O R : iom_set_rst_vars SIZE of RST_FIELD array is too small'
-           CALL ctl_stop( 'iom_set_rst_vars:', ctmp1 )
-        ENDIF
-   END SUBROUTINE iom_set_rst_vars
-
-
-   SUBROUTINE iom_set_rstw_active(cdrst_file)
-      !!---------------------------------------------------------------------
-      !!                   ***  SUBROUTINE iom_set_rstw_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
@@ -577,26 +446,82 @@
         ENDIF 
         CALL xios_set_file_attr( "wrestart", name=trim(cdrst_file))
+#endif
+   END SUBROUTINE iom_set_rstw_file
+
+
+   SUBROUTINE iom_set_rstw_active(sdfield, rd0, rd1, rd2, rd3)
+      !!---------------------------------------------------------------------
+      !!                   ***  SUBROUTINE iom_set_rstw_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 = *), INTENT(IN)                     :: sdfield
+   REAL(wp), OPTIONAL, INTENT(IN)                     :: rd0
+   REAL(wp), OPTIONAL, INTENT(IN), DIMENSION(:)       :: rd1
+   REAL(wp), OPTIONAL, INTENT(IN), DIMENSION(:, :)    :: rd2
+   REAL(wp), OPTIONAL, INTENT(IN), DIMENSION(:, :, :) :: rd3  
+#if defined key_iomput
+   TYPE(xios_field) :: field_hdl
+   TYPE(xios_file) :: file_hdl
+   INTEGER :: i
+   CHARACTER(lc)  ::   clpath
+   CHARACTER(len=1024) :: fname
+   CHARACTER(len=lc)   :: axis_ref
+
+        CALL xios_get_handle("wrestart", file_hdl)
+!       CALL xios_get_file_attr("wrestart", name = fname )
+!       IF(lwp) write(numout, *) TRIM(fname), ' File to write'
+!       IF(lwp) call flush(numout)
 !define fields 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="nav_lev", 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="nav_lev", 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
+        CALL xios_add_child(file_hdl, field_hdl, TRIM(sdfield))
+
+        IF(PRESENT(rd3)) THEN
+               CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(sdfield),   &
+                            domain_ref="grid_N", axis_ref=TRIM(iom_axis(size(rd3, 3))), &
+                            prec = 8, operation = "instant")
+        ENDIF
+
+        IF(PRESENT(rd2)) THEN
+               CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(sdfield), &
+                                   domain_ref="grid_N", prec = 8, operation = "instant") 
+        ENDIF
+
+        IF(PRESENT(rd1)) THEN
+               CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(sdfield), &
+                                   axis_ref=TRIM(iom_axis(size(rd1, 1))), prec = 8, operation = "instant")
+        ENDIF
+
+        IF(PRESENT(rd0)) THEN
+               CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(sdfield), &
+                                         scalar_ref = "grid_scalar", prec = 8, operation = "instant")
+        ENDIF
 #endif
    END SUBROUTINE iom_set_rstw_active
+
+   FUNCTION iom_axis(idlev) result(axis_ref)
+     !!---------------------------------------------------------------------
+      !!                   ***  FUNCTION  iom_axis  ***
+      !!
+      !! ** Purpose : Used for grid definition when XIOS is used to read/write
+      !!              restart. Returns axis corresponding to the number of levels
+      !!              given as an input variable. Axes are defined in routine 
+      !!              iom_set_rst_context
+      !!---------------------------------------------------------------------
+    INTEGER, INTENT(IN) :: idlev
+    CHARACTER(len=lc)   :: axis_ref
+    CHARACTER(len=12)   :: str
+       IF(idlev == jpk) THEN
+         axis_ref="nav_lev"
+#if defined key_si3
+       ELSEIF(idlev == jpl) THEN
+         axis_ref="numcat"
+#endif         
+       ELSE
+         write(str, *) idlev
+         CALL ctl_stop( 'iom_axis', 'Definition for axis with '//TRIM(ADJUSTL(str))//' levels missing')
+       ENDIF
+   END FUNCTION iom_axis
 
    SUBROUTINE iom_set_rst_context(ld_rstr) 
@@ -609,6 +534,5 @@
       !!---------------------------------------------------------------------
    LOGICAL, INTENT(IN)               :: ld_rstr
-!ld_rstr is true for restart context. There is no need to define grid for 
-!restart read, because it's read from file
+   INTEGER :: ji
 #if defined key_iomput
    TYPE(xios_domaingroup)            :: domaingroup_hdl 
@@ -629,4 +553,8 @@
      CALL xios_set_axis_attr( "nav_lev", long_name="Vertical levels in meters", positive="down")
      CALL iom_set_axis_attr( "nav_lev", paxis = gdept_1d ) 
+#if defined key_si3
+     CALL xios_add_child(axisgroup_hdl, axis_hdl, "numcat")
+     CALL iom_set_axis_attr( "numcat", (/ (REAL(ji,wp), ji=1,jpl) /) )
+#endif
 
      CALL xios_get_handle("scalar_definition", scalargroup_hdl) 
@@ -974,7 +902,5 @@
 #if defined key_iomput
          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) )
 #else
          WRITE(ctmp1,*) 'Can not use XIOS in iom_g0d, file: '//trim(clname)//', var:'//trim(cdvar)
@@ -1327,5 +1253,4 @@
 #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
             pv_r3d(:, :, :) = 0.
@@ -1347,5 +1272,4 @@
             CALL xios_recv_field( trim(cdvar), pv_r1d)
          ENDIF
-         CALL iom_swap( TRIM(cxios_context) )
 #else
          istop = istop + 1 
@@ -1561,4 +1485,7 @@
           IF(lwp) write(numout,*) 'RESTART: write (XIOS 0D) ',trim(cdvar)
           CALL xios_send_field(trim(cdvar), pvar)
+      ELSE
+          IF(lwp) write(numout,*) 'RESTART: define (XIOS 0D) ',trim(cdvar)
+          CALL iom_set_rstw_active( trim(cdvar), rd0 = pvar ) 
       ENDIF
 #endif
@@ -1591,4 +1518,7 @@
          IF(lwp) write(numout,*) 'RESTART: write (XIOS 1D) ',trim(cdvar)
          CALL xios_send_field(trim(cdvar), pvar)
+      ELSE
+         IF(lwp) write(numout,*) 'RESTART: define (XIOS 1D) ',trim(cdvar)
+         CALL iom_set_rstw_active( trim(cdvar), rd1 = pvar ) 
       ENDIF
 #endif
@@ -1621,4 +1551,7 @@
          IF(lwp) write(numout,*) 'RESTART: write (XIOS 2D) ',trim(cdvar)
          CALL xios_send_field(trim(cdvar), pvar)
+      ELSE
+         IF(lwp) write(numout,*) 'RESTART: define (XIOS 2D) ',trim(cdvar)
+         CALL iom_set_rstw_active( trim(cdvar), rd2 = pvar ) 
       ENDIF
 #endif
@@ -1651,4 +1584,7 @@
          IF(lwp) write(numout,*) 'RESTART: write (XIOS 3D) ',trim(cdvar)
          CALL xios_send_field(trim(cdvar), pvar)
+      ELSE
+         IF(lwp) write(numout,*) 'RESTART: define (XIOS 3D) ',trim(cdvar)
+         CALL iom_set_rstw_active( trim(cdvar), rd3 = pvar )
       ENDIF
 #endif
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/iom_def.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/iom_def.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/iom_def.F90	(revision 12950)
@@ -9,4 +9,5 @@
    !!----------------------------------------------------------------------
    USE par_kind
+   USE netcdf
 
    IMPLICIT NONE
@@ -67,13 +68,8 @@
    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 = 105 !: 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
+!metadata in restart file for restart read with XIOS
+   INTEGER, PUBLIC, PARAMETER :: NMETA = 10
+   CHARACTER(LEN=nf90_max_name), PUBLIC :: meta(NMETA)
 !$AGRIF_END_DO_NOT_TREAT
-   !
-   TYPE(RST_FIELD), PUBLIC, SAVE :: rst_wfields(max_rst_fields), rst_rfields(max_rst_fields)
    !
    !! * Substitutions
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/iom_nf90.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/iom_nf90.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/iom_nf90.F90	(revision 12950)
@@ -31,4 +31,5 @@
    PUBLIC iom_nf90_open  , iom_nf90_close, iom_nf90_varid, iom_nf90_get, iom_nf90_rstput
    PUBLIC iom_nf90_chkatt, iom_nf90_getatt, iom_nf90_putatt
+   PUBLIC iom_nf90_check
 
    INTERFACE iom_nf90_get
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/restart.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/restart.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/restart.F90	(revision 12950)
@@ -116,6 +116,5 @@
                   clpname = TRIM(Agrif_CFixed())//"_"//clname   
                ENDIF
-               CALL iom_init( cwxios_context, TRIM(clpath)//TRIM(clpname), .false. )
-               CALL xios_update_calendar(nitrst)
+               CALL iom_init( cwxios_context, TRIM(clpath)//TRIM(clpname), .false., ld_closedef = .FALSE. )
                CALL iom_swap(      cxios_context          )
 #else
@@ -214,10 +213,10 @@
              IF( .NOT.lxios_set ) THEN
                  IF(lwp) WRITE(numout,*) 'Enable restart reading by XIOS'
-                 CALL iom_init( crxios_context, ld_tmppatch = .false. )
+                 CALL iom_init( crxios_context, ld_tmppatch = .TRUE. )
                  lxios_set = .TRUE.
              ENDIF
          ENDIF
          IF( TRIM(Agrif_CFixed()) /= '0' .AND. lrxios) THEN
-             CALL iom_init( crxios_context, ld_tmppatch = .false. )
+             CALL iom_init( crxios_context, ld_tmppatch = .TRUE. )
              IF(lwp) WRITE(numout,*) 'Enable restart reading by XIOS for AGRIF'
              lxios_set = .TRUE.
@@ -245,4 +244,5 @@
 
       ! Check dynamics and tracer time-step consistency and force Euler restart if changed
+      IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
       IF( iom_varid( numror, 'rdt', ldstop = .FALSE. ) > 0 )   THEN
          CALL iom_get( numror, 'rdt', zrdt, ldxios = lrxios )
@@ -268,5 +268,5 @@
          RETURN 
       ENDIF  
-      
+
       IF( iom_varid( numror, 'ub', ldstop = .FALSE. ) > 0 ) THEN
          CALL iom_get( numror, jpdom_autoglo, 'ub'     , uu(:,:,:       ,Kbb), ldxios = lrxios )   ! before fields
@@ -289,4 +289,5 @@
          CALL eos( ts(:,:,:,:,Kmm), rhd, rhop, gdept(:,:,:,Kmm) )   
       ENDIF
+      IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
       !
       IF( l_1st_euler ) THEN                                  ! Euler restart 
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ISF/isfcav.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ISF/isfcav.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ISF/isfcav.F90	(revision 12950)
@@ -187,10 +187,4 @@
       !================
       !
-      IF( lwxios ) THEN
-         CALL iom_set_rstw_var_active('fwfisf_cav_b')
-         CALL iom_set_rstw_var_active('isf_hc_cav_b')
-         CALL iom_set_rstw_var_active('isf_sc_cav_b')
-      ENDIF
-      !
       !================
       ! 3: read restart
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ISF/isfcpl.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ISF/isfcpl.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ISF/isfcpl.F90	(revision 12950)
@@ -114,13 +114,4 @@
       e3t(:,:,:,Kbb)   = e3t(:,:,:,Kmm)
  
-      ! prepare writing restart
-      IF( lwxios ) THEN
-         CALL iom_set_rstw_var_active('ssmask')
-         CALL iom_set_rstw_var_active('tmask')
-         CALL iom_set_rstw_var_active('e3t_n')
-         CALL iom_set_rstw_var_active('e3u_n')
-         CALL iom_set_rstw_var_active('e3v_n')
-      END IF
-      !
    END SUBROUTINE isfcpl_init
    ! 
@@ -166,5 +157,7 @@
       !!----------------------------------------------------------------------
       !
+      IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
       CALL iom_get( numror, jpdom_autoglo, 'ssmask'  , zssmask_b, ldxios = lrxios   ) ! need to extrapolate T/S
+      IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
 
       ! compute new ssh if we open a full water column 
@@ -245,7 +238,9 @@
       !!----------------------------------------------------------------------
       ! 
+      IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
       CALL iom_get( numror, jpdom_autoglo, 'tmask'  , ztmask_b, ldxios = lrxios   ) ! need to extrapolate T/S
       !CALL iom_get( numror, jpdom_autoglo, 'wmask'  , zwmask_b, ldxios = lrxios   ) ! need to extrapolate T/S
       !CALL iom_get( numror, jpdom_autoglo, 'gdepw_n', zdepw_b(:,:,:), ldxios = lrxios ) ! need to interpol vertical profile (vvl)
+      IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
       !
       ! 
@@ -391,7 +386,9 @@
       !!----------------------------------------------------------------------
       !
+      IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
       CALL iom_get( numror, jpdom_autoglo, 'tmask'  , ztmask_b, ldxios = lrxios )
       CALL iom_get( numror, jpdom_autoglo, 'e3u_n'  , ze3u_b  , ldxios = lrxios )
       CALL iom_get( numror, jpdom_autoglo, 'e3v_n'  , ze3v_b  , ldxios = lrxios )
+      IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
       !
       ! 1.0: compute horizontal volume flux divergence difference before-after coupling
@@ -495,8 +492,10 @@
 
       ! get restart variable
+      IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
       CALL iom_get( numror, jpdom_autoglo, 'tmask'  , ztmask_b(:,:,:), ldxios = lrxios   ) ! need to extrapolate T/S
       CALL iom_get( numror, jpdom_autoglo, 'e3t_n'  , ze3t_b(:,:,:)  , ldxios = lrxios )
       CALL iom_get( numror, jpdom_autoglo, 'tn'     , zt_b(:,:,:)    , ldxios = lrxios )
       CALL iom_get( numror, jpdom_autoglo, 'sn'     , zs_b(:,:,:)    , ldxios = lrxios )
+      IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
 
       ! compute run length
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ISF/isfrst.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ISF/isfrst.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ISF/isfrst.F90	(revision 12950)
@@ -53,7 +53,9 @@
       IF( iom_varid( numror, cfwf_b, ldstop = .FALSE. ) > 0 ) THEN
          IF(lwp) WRITE(numout,*) '          nit000-1 isf tracer content forcing fields read in the restart file'
+         IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
          CALL iom_get( numror, jpdom_autoglo, cfwf_b, pfwf_b(:,:)        , ldxios = lrxios )   ! before ice shelf melt
          CALL iom_get( numror, jpdom_autoglo, chc_b , ptsc_b (:,:,jp_tem), ldxios = lrxios )   ! before ice shelf heat flux
          CALL iom_get( numror, jpdom_autoglo, csc_b , ptsc_b (:,:,jp_sal), ldxios = lrxios )   ! before ice shelf heat flux
+         IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
       ELSE
          pfwf_b(:,:)   = pfwf(:,:)
@@ -61,10 +63,4 @@
       ENDIF
       !
-      IF( lwxios ) THEN
-         CALL iom_set_rstw_var_active(TRIM(chc_b ))
-         CALL iom_set_rstw_var_active(TRIM(csc_b ))
-         CALL iom_set_rstw_var_active(TRIM(cfwf_b))
-      ENDIF
-
    END SUBROUTINE isfrst_read
    ! 
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/SBC/sbcapr.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/SBC/sbcapr.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/SBC/sbcapr.F90	(revision 12950)
@@ -108,7 +108,4 @@
             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,4 +149,5 @@
          !                                      ! ---------------------------------------- !
          !                                            !* Restart: read in restart file
+         IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
          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'
@@ -160,4 +158,5 @@
             ssh_ibb(:,:) = ssh_ib(:,:)
          ENDIF
+         IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
       ENDIF
       !                                         ! ---------------------------------------- !
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/SBC/sbcmod.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/SBC/sbcmod.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/SBC/sbcmod.F90	(revision 12950)
@@ -366,14 +366,4 @@
       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
 
@@ -514,4 +504,5 @@
       IF( kt == nit000 ) THEN                          !   set the forcing field at nit000 - 1    !
          !                                             ! ---------------------------------------- !
+         IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
          IF( ln_rstart .AND.    &                               !* Restart: read in restart file
             & iom_varid( numror, 'utau_b', ldstop = .FALSE. ) > 0 ) THEN
@@ -537,4 +528,5 @@
             sfx_b (:,:) = sfx (:,:)
          ENDIF
+         IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
       ENDIF
       !                                                ! ---------------------------------------- !
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/SBC/sbcrnf.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/SBC/sbcrnf.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/SBC/sbcrnf.F90	(revision 12950)
@@ -156,4 +156,5 @@
       IF( kt == nit000 ) THEN                          !   set the forcing field at nit000 - 1    !
          !                                             ! ---------------------------------------- !
+         IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
          IF( ln_rstart .AND.    &                               !* Restart: read in restart file
             & iom_varid( numror, 'rnf_b', ldstop = .FALSE. ) > 0 ) THEN
@@ -167,4 +168,5 @@
             rnf_tsc_b(:,:,:) = rnf_tsc(:,:,:)
          ENDIF
+         IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
       ENDIF
       !                                                ! ---------------------------------------- !
@@ -479,10 +481,4 @@
       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: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/SBC/sbcssm.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/SBC/sbcssm.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/SBC/sbcssm.F90	(revision 12950)
@@ -205,4 +205,5 @@
          IF(lwp) WRITE(numout,*) '~~~~~~~~~~~~ '
          !
+         IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
          IF( ln_rstart .AND. iom_varid( numror, 'nn_fsbc', ldstop = .FALSE. ) > 0 ) THEN
             l_ssm_mean = .TRUE.
@@ -235,4 +236,5 @@
             ENDIF
          ENDIF
+         IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
       ENDIF
       !
@@ -254,15 +256,4 @@
       IF( .NOT. ln_traqsr )   fraqsr_1lev(:,:) = 1._wp   ! default definition: qsr 100% in the fisrt level 
       !
-      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: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/TRA/traqsr.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/TRA/traqsr.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/TRA/traqsr.F90	(revision 12950)
@@ -135,4 +135,5 @@
       !                         !-----------------------------------!
       IF( kt == nit000 ) THEN          !==  1st time step  ==!
+         IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
          IF( ln_rstart .AND. iom_varid( numror, 'qsr_hc_b', ldstop = .FALSE. ) > 0  .AND. .NOT.l_1st_euler ) THEN    ! read in restart
             IF(lwp) WRITE(numout,*) '          nit000-1 qsr tracer content forcing field read in the restart file'
@@ -143,4 +144,5 @@
             qsr_hc_b(:,:,:) = 0._wp
          ENDIF
+         IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
       ELSE                             !==  Swap of qsr heat content  ==!
          z1_2 = 0.5_wp
@@ -401,4 +403,5 @@
       !
       ! 1st ocean level attenuation coefficient (used in sbcssm)
+      IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
       IF( iom_varid( numror, 'fraqsr_1lev', ldstop = .FALSE. ) > 0 ) THEN
          CALL iom_get( numror, jpdom_autoglo, 'fraqsr_1lev'  , fraqsr_1lev, ldxios = lrxios  )
@@ -406,9 +409,5 @@
          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( lrxios ) CALL iom_swap( TRIM(cxios_context) )
       !
    END SUBROUTINE tra_qsr_init
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/TRA/trasbc.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/TRA/trasbc.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/TRA/trasbc.F90	(revision 12950)
@@ -106,4 +106,5 @@
       !                             !==  Set before sbc tracer content fields  ==!
       IF( kt == nit000 ) THEN             !* 1st time-step
+         IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
          IF( ln_rstart .AND.    &               ! Restart: read in restart file
               & iom_varid( numror, 'sbc_hc_b', ldstop = .FALSE. ) > 0 ) THEN
@@ -118,4 +119,5 @@
             sbc_tsc_b(:,:,:) = 0._wp
          ENDIF
+         IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
       ELSE                                !* other time-steps: swap of forcing fields
          zfact = 0.5_wp
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ZDF/zdfgls.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ZDF/zdfgls.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ZDF/zdfgls.F90	(revision 12950)
@@ -1025,11 +1025,4 @@
       CALL gls_rst( nit000, 'READ' )      ! (en, avt_k, avm_k, hmxl_n)
       !
-      IF( lwxios ) THEN
-         CALL iom_set_rstw_var_active('en')
-         CALL iom_set_rstw_var_active('avt_k')
-         CALL iom_set_rstw_var_active('avm_k')
-         CALL iom_set_rstw_var_active('hmxl_n')
-      ENDIF
-      !
    END SUBROUTINE zdf_gls_init
 
@@ -1059,4 +1052,5 @@
          !                                   ! ---------------
          IF( ln_rstart ) THEN                   !* Read the restart file
+            IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
             id1 = iom_varid( numror, 'en'    , ldstop = .FALSE. )
             id2 = iom_varid( numror, 'avt_k' , ldstop = .FALSE. )
@@ -1076,4 +1070,5 @@
                ! avt_k, avm_k already set to the background value in zdf_phy_init
             ENDIF
+            IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
          ELSE                                   !* Start from rest
             IF(lwp) WRITE(numout,*)
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ZDF/zdfosm.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ZDF/zdfosm.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ZDF/zdfosm.F90	(revision 12950)
@@ -1424,9 +1424,4 @@
      ghamv(:,:,:) = 0.
      !
-     IF( lwxios ) THEN
-        CALL iom_set_rstw_var_active('wn')
-        CALL iom_set_rstw_var_active('hbl')
-        CALL iom_set_rstw_var_active('hbli')
-     ENDIF
    END SUBROUTINE zdf_osm_init
 
@@ -1459,4 +1454,5 @@
      !!-----------------------------------------------------------------------------
      IF( TRIM(cdrw) == 'READ'.AND. ln_rstart) THEN
+        IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
         id1 = iom_varid( numror, 'wn'   , ldstop = .FALSE. )
         IF( id1 > 0 ) THEN                       ! 'wn' exists; read
@@ -1473,8 +1469,10 @@
            CALL iom_get( numror, jpdom_autoglo, 'hbli', hbli, ldxios = lrxios  )
            WRITE(numout,*) ' ===>>>> :  hbl & hbli read from restart file'
+           IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
            RETURN
         ELSE                      ! 'hbl' & 'hbli' not in restart file, recalculate
            WRITE(numout,*) ' ===>>>> : previous run without osmosis scheme, hbl computed from stratification'
         END IF
+        IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
      END IF
 
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ZDF/zdfric.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ZDF/zdfric.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ZDF/zdfric.F90	(revision 12950)
@@ -103,8 +103,4 @@
       CALL ric_rst( nit000, 'READ' )  !* read or initialize all required files
       !
-      IF( lwxios ) THEN
-         CALL iom_set_rstw_var_active('avt_k')
-         CALL iom_set_rstw_var_active('avm_k')
-      ENDIF
    END SUBROUTINE zdf_ric_init
 
@@ -210,4 +206,5 @@
          !           !* Read the restart file
          IF( ln_rstart ) THEN
+            IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
             id1 = iom_varid( numror, 'avt_k', ldstop = .FALSE. )
             id2 = iom_varid( numror, 'avm_k', ldstop = .FALSE. )
@@ -217,4 +214,5 @@
                CALL iom_get( numror, jpdom_autoglo, 'avm_k', avm_k, ldxios = lrxios )
             ENDIF
+            IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
          ENDIF
          !           !* otherwise Kz already set to the background value in zdf_phy_init
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ZDF/zdftke.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ZDF/zdftke.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/ZDF/zdftke.F90	(revision 12950)
@@ -634,10 +634,4 @@
       CALL tke_rst( nit000, 'READ' )      ! (en, avt_k, avm_k, dissl) 
       !
-      IF( lwxios ) THEN
-         CALL iom_set_rstw_var_active('en')
-         CALL iom_set_rstw_var_active('avt_k')
-         CALL iom_set_rstw_var_active('avm_k')
-         CALL iom_set_rstw_var_active('dissl')
-      ENDIF
    END SUBROUTINE zdf_tke_init
 
@@ -665,4 +659,5 @@
          !                                   ! ---------------
          IF( ln_rstart ) THEN                   !* Read the restart file
+            IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
             id1 = iom_varid( numror, 'en'   , ldstop = .FALSE. )
             id2 = iom_varid( numror, 'avt_k', ldstop = .FALSE. )
@@ -682,4 +677,5 @@
                ! avt_k, avm_k already set to the background value in zdf_phy_init
             ENDIF
+            IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
          ELSE                                   !* Start from rest
             IF(lwp) WRITE(numout,*)
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/step.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/step.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/step.F90	(revision 12950)
@@ -122,4 +122,9 @@
                              CALL iom_init_closedef
          IF( ln_crs      )   CALL iom_init( TRIM(cxios_context)//"_crs" )  ! for coarse grid
+      ELSEIF((kstp == nitrst) .AND. lwxios) THEN
+         CALL iom_swap(      cwxios_context          )
+         CALL iom_init_closedef(cwxios_context)
+         CALL iom_setkt( kstp - nit000 + 1,      cwxios_context          )
+         CALL iom_swap(      cwxios_context          )
       ENDIF
       IF( kstp /= nit000 )   CALL day( kstp )         ! Calendar (day was already called at nit000 in day_init)
Index: /NEMO/branches/2020/dev_12905_xios_restart/tests/CANAL/MY_SRC/domvvl.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/tests/CANAL/MY_SRC/domvvl.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/tests/CANAL/MY_SRC/domvvl.F90	(revision 12950)
@@ -270,22 +270,4 @@
       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
-      !
    END SUBROUTINE dom_vvl_zgr
 
@@ -793,4 +775,5 @@
          IF( ln_rstart ) THEN                   !* Read the restart file
             CALL rst_read_open                  !  open the restart file if necessary
+            IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
             CALL iom_get( numror, jpdom_autoglo, 'sshn'   , ssh(:,:,Kmm), ldxios = lrxios    )
             !
@@ -869,4 +852,5 @@
                ENDIF
             ENDIF
+            IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
             !
          ELSE                                   !* Initialize at "rest"
Index: /NEMO/branches/2020/dev_12905_xios_restart/tests/VORTEX/MY_SRC/domvvl.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/tests/VORTEX/MY_SRC/domvvl.F90	(revision 12949)
+++ /NEMO/branches/2020/dev_12905_xios_restart/tests/VORTEX/MY_SRC/domvvl.F90	(revision 12950)
@@ -270,22 +270,4 @@
       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
-      !
    END SUBROUTINE dom_vvl_zgr
 
@@ -793,4 +775,5 @@
          IF( ln_rstart ) THEN                   !* Read the restart file
             CALL rst_read_open                  !  open the restart file if necessary
+            IF( lrxios ) CALL iom_swap( TRIM(crxios_context) )
             CALL iom_get( numror, jpdom_autoglo, 'sshn'   , ssh(:,:,Kmm), ldxios = lrxios    )
             !
@@ -869,4 +852,5 @@
                ENDIF
             ENDIF
+            IF( lrxios ) CALL iom_swap( TRIM(cxios_context) )
             !
          ELSE                                   !* Initialize at "rest"
