Index: /NEMO/branches/2020/dev_12905_xios_restart/src/ICE/icerst.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/ICE/icerst.F90	(revision 12960)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/ICE/icerst.F90	(revision 12961)
@@ -91,5 +91,6 @@
                   clpname = TRIM(Agrif_CFixed())//"_"//clname
                ENDIF
-               CALL iom_init( cwixios_context, TRIM(clpath)//TRIM(clpname), .false., ld_closedef = .FALSE. )
+               CALL iom_init( cwixios_context, TRIM(clpath)//TRIM(clpname), ld_tmppatch = .false.,&
+                                                                            ld_closedef = .FALSE. )
                CALL iom_swap(      cxios_context          )
 #else
@@ -223,5 +224,6 @@
              clpname = TRIM(Agrif_CFixed())//"_"//cn_icerst_in   
           ENDIF
-          CALL iom_init( crixios_context, fname = TRIM(cn_icerst_indir)//'/'//TRIM(clpname), ld_tmppatch = .TRUE. )
+          CALL iom_init( crixios_context, fname = TRIM(cn_icerst_indir)//'/'//TRIM(clpname), &
+                                          idfp = iom_file(numrir)%nfid, ld_tmppatch = .TRUE. )
       ENDIF
 
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/ICE/icestp.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/ICE/icestp.F90	(revision 12960)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/ICE/icestp.F90	(revision 12961)
@@ -280,5 +280,8 @@
       END WHERE
 
-      IF( ln_rstart )   CALL iom_close( numrir )  ! close input ice restart file
+      IF( ln_rstart )  THEN
+          CALL iom_close( numrir )  ! close input ice restart file
+          IF(lrxios) CALL iom_context_finalize(      crixios_context         )
+      ENDIF
       !
    END SUBROUTINE ice_init
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/in_out_manager.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/in_out_manager.F90	(revision 12960)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/in_out_manager.F90	(revision 12961)
@@ -177,9 +177,15 @@
    LOGICAL       ::   lwp      = .FALSE.    !: boolean : true on the 1st processor only .OR. sn_cfctl%l_oceout=T
    LOGICAL       ::   lsp_area = .TRUE.     !: to make a control print over a specific area
-   CHARACTER(lc) ::   cxios_context         !: context name used in xios
-   CHARACTER(lc) ::   crxios_context        !: context name used in xios to read restart
-   CHARACTER(lc) ::   cwxios_context        !: context name used in xios to write restart file
-   CHARACTER(lc) ::   crixios_context       !: context name used in xios to read SI3 restart
-   CHARACTER(lc) ::   cwixios_context       !: context name used in xios to write SI3 restart file
+   CHARACTER(LEN=lc) ::   cxios_context         !: context name used in xios
+   CHARACTER(LEN=lc) ::   crxios_context        !: context name used in xios to read OCE restart
+   CHARACTER(LEN=lc) ::   cwxios_context        !: context name used in xios to write OCE restart file
+   CHARACTER(LEN=lc) ::   crixios_context       !: context name used in xios to read SI3 restart
+   CHARACTER(LEN=lc) ::   cwixios_context       !: context name used in xios to write SI3 restart file
+   CHARACTER(LEN=lc) ::   crtxios_context       !: context name used in xios to read TOP restart
+   CHARACTER(LEN=lc) ::   cwtxios_context       !: context name used in xios to write TOP restart file
+   CHARACTER(LEN=lc) ::   crsxios_context       !: context name used in xios to read SEDIMENT restart
+   CHARACTER(LEN=lc) ::   cwsxios_context       !: context name used in xios to write SEDIMENT restart file
+
+
 
 
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 12960)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/iom.F90	(revision 12961)
@@ -96,5 +96,5 @@
 CONTAINS
 
-   SUBROUTINE iom_init( cdname, fname, ld_tmppatch, ld_closedef ) 
+   SUBROUTINE iom_init( cdname, fname, idfp, ld_tmppatch, ld_closedef ) 
       !!----------------------------------------------------------------------
       !!                     ***  ROUTINE   ***
@@ -105,4 +105,5 @@
       CHARACTER(len=*),           INTENT(in)  :: cdname
       CHARACTER(len=*), OPTIONAL, INTENT(in)  :: fname
+      INTEGER         , OPTIONAL, INTENT(in)  :: idfp         ! pointer to netcdf file for restart reading with XIOS
       LOGICAL         , OPTIONAL, INTENT(in)  :: ld_tmppatch
       LOGICAL         , OPTIONAL, INTENT(in)  :: ld_closedef
@@ -115,4 +116,5 @@
       INTEGER           :: ji
       LOGICAL           :: llrst_context              ! is context related to restart
+      LOGICAL           :: llrstr, llrstw 
       INTEGER           :: inum
       !
@@ -149,6 +151,14 @@
       CALL xios_context_initialize(TRIM(clname), mpi_comm_oce)
       CALL iom_swap( cdname )
-      llrst_context = (TRIM(cdname) == TRIM(crxios_context)) .OR. (TRIM(cdname) == TRIM(cwxios_context))
-      llrst_context = llrst_context .OR. (TRIM(cdname) == TRIM(crixios_context)) .OR. (TRIM(cdname) == TRIM(cwixios_context))
+
+      llrstr = (TRIM(cdname) == TRIM(crxios_context)) .OR. (TRIM(cdname) == TRIM(crixios_context))
+      llrstr = llrstr .OR. (TRIM(cdname) == TRIM(crtxios_context))
+      llrstr = llrstr .OR. (TRIM(cdname) == TRIM(crsxios_context))
+
+      llrstw = (TRIM(cdname) == TRIM(cwxios_context)) .OR. (TRIM(cdname) == TRIM(cwixios_context))
+      llrstw = llrstw .OR. (TRIM(cdname) == TRIM(cwtxios_context))
+      llrstw = llrstw .OR. (TRIM(cdname) == TRIM(cwsxios_context))
+
+      llrst_context = llrstr .OR. llrstw
 
       ! Calendar type is now defined in xml file 
@@ -266,16 +276,13 @@
       IF(lwp) write(numout, *) 'TEST IOM_INIT: ', TRIM(cdname), TRIM(cdname) == TRIM(crixios_context)
       IF(lwp) CALL FLUSH(numout)
-      IF( TRIM(cdname) == TRIM(crxios_context) .OR. TRIM(cdname) == TRIM(crixios_context)) THEN
-          IF(TRIM(cdname) == TRIM(crxios_context) ) THEN
-             inum = numror
-          ELSEIF(TRIM(cdname) == TRIM(crixios_context)) THEN
-             inum = numrir 
+      IF(llrstr) THEN
+          IF(PRESENT(idfp)) THEN
+             CALL iom_set_rst_context(.TRUE.)
+!set which fields will be read from restart file
+             CALL iom_set_rstr_active(fname, idfp)
           ELSE
-             CALL ctl_stop( 'iom_init:', 'restart read with XIOS: Unknown restart context' )
+             CALL ctl_stop( 'iom_init:', 'restart read with XIOS: missing pointer to NETCDF file' )
           ENDIF
-          CALL iom_set_rst_context(.TRUE.)
-!set which fields will be read from restart file
-          CALL iom_set_rstr_active(fname, inum)
-      ELSE IF( (TRIM(cdname) == TRIM(cwxios_context)) .OR. (TRIM(cdname) == TRIM(cwixios_context))) THEN
+      ELSE IF(llrstw) THEN
           CALL iom_set_rstw_file(fname)
       ELSE
@@ -314,8 +321,9 @@
       IF(lwp) write(numout, *) 'XIOS CLOSE definitions for: ', TRIM(cdname)
       llrstw = .FALSE.
-     IF(PRESENT(cdname)) THEN
-         IF((TRIM(cdname) == TRIM(cwxios_context)) .OR. (TRIM(cdname) == TRIM(cwixios_context))) THEN
-           llrstw = .TRUE.
-         ENDIF
+      IF(PRESENT(cdname)) THEN
+         llrstw = (TRIM(cdname) == TRIM(cwxios_context))
+         llrstw = llrstw .OR. (TRIM(cdname) == TRIM(cwixios_context))
+         llrstw = llrstw .OR. (TRIM(cdname) == TRIM(cwtxios_context))
+         llrstw = llrstw .OR. (TRIM(cdname) == TRIM(cwsxios_context))
       ENDIF
 
@@ -323,10 +331,9 @@
 !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()
-
-      IF(.NOT. llrstw)  CALL xios_update_calendar( 0 )
-
+          CALL xios_close_context_definition()
+      ELSE
+          CALL xios_close_context_definition()
+          CALL xios_update_calendar( 0 )
+      ENDIF
 #else
       IF( .FALSE. )   WRITE(numout,*) 'iom_init_closedef: should not see this'   ! useless statement to avoid compilation warnings
@@ -386,10 +393,10 @@
              output_freq=xios_timestep)
 
-        CALL iom_nf90_check( nf90_inquire( iom_file(idnum)%nfid, ndims, nvars, natts ), clinfo )
+        CALL iom_nf90_check( nf90_inquire(idnum, ndims, nvars, natts ), clinfo )
         ALLOCATE(indimlens(ndims), indimnames(ndims))
-        CALL iom_nf90_check( nf90_inquire( iom_file(idnum)%nfid, unlimitedDimId = unlimitedDimId ), clinfo )
+        CALL iom_nf90_check( nf90_inquire(idnum, unlimitedDimId = unlimitedDimId ), clinfo )
 
         DO idim = 1, ndims
-           CALL iom_nf90_check( nf90_inquire_dimension( iom_file(idnum)%nfid, idim, dimname, dimlen ), clinfo )
+           CALL iom_nf90_check( nf90_inquire_dimension(idnum, idim, dimname, dimlen ), clinfo )
            indimlens(idim) = dimlen
            indimnames(idim) = dimname
@@ -398,5 +405,5 @@
         DO jv =1, nvars
             lmeta = .FALSE.
-            CALL iom_nf90_check( nf90_inquire_variable( iom_file(idnum)%nfid, jv, varname, xtype, ndims, dimids, natts ), clinfo )
+            CALL iom_nf90_check( nf90_inquire_variable(idnum, jv, varname, xtype, ndims, dimids, natts ), clinfo )
             DO i = 1, NMETA
                IF(TRIM(varname) == TRIM(meta(i))) THEN
@@ -510,4 +517,6 @@
 
         IF(PRESENT(rd3)) THEN
+               IF(lwp) write(numout, *) TRIM(sdfield), ' 3D ', size(rd3,3)
+               IF(lwp) CALL FLUSH(numout)
                CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(sdfield),   &
                             domain_ref="grid_N", axis_ref=TRIM(iom_axis(size(rd3, 3))), &
@@ -516,4 +525,6 @@
 
         IF(PRESENT(rd2)) THEN
+               IF(lwp) write(numout, *) TRIM(sdfield), ' 2D'
+               IF(lwp) CALL FLUSH(numout)
                CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(sdfield), &
                                    domain_ref="grid_N", prec = 8, operation = "instant") 
@@ -521,4 +532,6 @@
 
         IF(PRESENT(rd1)) THEN
+               IF(lwp) write(numout, *) TRIM(sdfield), ' 1D ', size(rd1,1)
+               IF(lwp) CALL FLUSH(numout)
                CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(sdfield), &
                                    axis_ref=TRIM(iom_axis(size(rd1, 1))), prec = 8, operation = "instant")
@@ -526,4 +539,6 @@
 
         IF(PRESENT(rd0)) THEN
+               IF(lwp) write(numout, *) TRIM(sdfield), ' 0D'
+               IF(lwp) CALL FLUSH(numout)
                CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(sdfield), &
                                          scalar_ref = "grid_scalar", prec = 8, operation = "instant")
@@ -603,5 +618,5 @@
 #if defined key_iomput
       TYPE(xios_context) :: nemo_hdl
-
+      IF(lwp) write(numout, *) 'SWAP TO: ', TRIM(cdname),' AGRIF: ', TRIM(Agrif_CFixed())
       IF( TRIM(Agrif_CFixed()) == '0' ) THEN
         CALL xios_get_handle(TRIM(cdname),nemo_hdl)
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 12960)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/iom_def.F90	(revision 12961)
@@ -45,5 +45,4 @@
    LOGICAL, PUBLIC            ::   lrxios          !: read single file restart using XIOS
    LOGICAL, PUBLIC            ::   lxios_sini = .FALSE. ! is restart in a single file
-   LOGICAL, PUBLIC            ::   lxios_set  = .FALSE. 
 
 
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 12960)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/restart.F90	(revision 12961)
@@ -116,5 +116,6 @@
                   clpname = TRIM(Agrif_CFixed())//"_"//clname   
                ENDIF
-               CALL iom_init( cwxios_context, TRIM(clpath)//TRIM(clpname), .false., ld_closedef = .FALSE. )
+               CALL iom_init( cwxios_context, TRIM(clpath)//TRIM(clpname), ld_tmppatch = .false.,&
+                                                                           ld_closedef = .FALSE. )
                CALL iom_swap(      cxios_context          )
 #else
@@ -190,7 +191,7 @@
       !!                the file has already been opened
       !!----------------------------------------------------------------------
-      LOGICAL         ::   llok
-      CHARACTER(lc)   ::   clpath   ! full path to ocean output restart file
-      CHARACTER(lc+2) ::   clpname  ! file name including agrif prefix
+      LOGICAL             ::   llok
+      CHARACTER(len=lc)   ::   clpath   ! full path to ocean output restart file
+      CHARACTER(len=lc+2) ::   clpname  ! file name including agrif prefix
       !!----------------------------------------------------------------------
       !
@@ -209,18 +210,16 @@
 ! can handle checking if variable is in the restart file (there will be no need to open
 ! restart)
-         IF(.NOT.lxios_set) lrxios = lrxios.AND.lxios_sini
+         lrxios = lrxios.AND.lxios_sini
 
          IF( lrxios) THEN
              crxios_context = 'oce_rst'
-             IF( .NOT.lxios_set ) THEN
-                 IF(lwp) WRITE(numout,*) 'Enable restart reading by XIOS'
-                 IF( TRIM(Agrif_CFixed()) == '0' ) THEN
-                    clpname = cn_ocerst_in
-                 ELSE
-                    clpname = TRIM(Agrif_CFixed())//"_"//cn_ocerst_in   
-                 ENDIF
-                 CALL iom_init( crxios_context, fname = TRIM(clpath)//TRIM(clpname), ld_tmppatch = .TRUE. )
-                 lxios_set = .TRUE.
+             IF(lwp) WRITE(numout,*) 'Enable restart reading by XIOS'
+             IF( TRIM(Agrif_CFixed()) == '0' ) THEN
+                clpname = cn_ocerst_in
+             ELSE
+                clpname = TRIM(Agrif_CFixed())//"_"//cn_ocerst_in   
              ENDIF
+             CALL iom_init( crxios_context, fname = TRIM(clpath)//TRIM(clpname), &
+                              idfp = iom_file(numror)%nfid, ld_tmppatch = .TRUE. )
          ENDIF
 
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/step.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/step.F90	(revision 12960)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/OCE/step.F90	(revision 12961)
@@ -127,4 +127,9 @@
          CALL iom_init_closedef(cwxios_context)
          CALL iom_setkt( kstp - nit000 + 1,      cwxios_context          )
+#if defined key_top
+         CALL iom_swap(      cwtxios_context          )
+         CALL iom_init_closedef(cwtxios_context)
+         CALL iom_setkt( kstp - nit000 + 1,      cwtxios_context          )
+#endif
       ENDIF
 #if defined key_si3
@@ -346,4 +351,5 @@
       IF( kstp == nit000 ) THEN                          ! 1st time step only
                                         CALL iom_close( numror )   ! close input  ocean restart file
+         IF( lrxios )                   CALL iom_context_finalize(      crxios_context         )
          IF(lwm)                        CALL FLUSH    ( numond )   ! flush output namelist oce
          IF(lwm .AND. numoni /= -1 )    CALL FLUSH    ( numoni )   ! flush output namelist ice (if exist)
@@ -362,10 +368,4 @@
       IF( kstp == nitend .OR. indic < 0 ) THEN 
                       CALL iom_context_finalize(      cxios_context          ) ! needed for XIOS+AGRIF
-         IF( lrxios ) THEN
-                      CALL iom_context_finalize(      crxios_context         )
-#if defined key_si3
-                      CALL iom_context_finalize(      crixios_context         )
-#endif
-         ENDIF
          IF( ln_crs ) CALL iom_context_finalize( trim(cxios_context)//"_crs" ) ! 
       ENDIF
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/C14/trcini_c14.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/C14/trcini_c14.F90	(revision 12960)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/C14/trcini_c14.F90	(revision 12961)
@@ -68,9 +68,11 @@
         IF(lwp) WRITE(numout,*) ' ~~~~~~~~~~~~~~'
         ! 
-        CALL iom_get( numrtr, 'co2sbc', co2sbc ) 
-        CALL iom_get( numrtr, jpdom_autoglo, 'c14sbc', c14sbc ) 
-        CALL iom_get( numrtr, jpdom_autoglo, 'exch_co2', exch_co2 ) 
-        CALL iom_get( numrtr, jpdom_autoglo, 'exch_c14', exch_c14 ) 
-        CALL iom_get( numrtr, jpdom_autoglo, 'qtr_c14', qtr_c14 )
+        IF(lrxios) CALL iom_swap(crtxios_context)
+        CALL iom_get( numrtr, 'co2sbc', co2sbc, ldxios = lrxios ) 
+        CALL iom_get( numrtr, jpdom_autoglo, 'c14sbc', c14sbc, ldxios = lrxios ) 
+        CALL iom_get( numrtr, jpdom_autoglo, 'exch_co2', exch_co2, ldxios = lrxios ) 
+        CALL iom_get( numrtr, jpdom_autoglo, 'exch_c14', exch_c14, ldxios = lrxios ) 
+        CALL iom_get( numrtr, jpdom_autoglo, 'qtr_c14', qtr_c14, ldxios = lrxios )
+        IF(lrxios) CALL iom_swap(cxios_context)
         !
       END IF
@@ -85,5 +87,7 @@
       ELSE
         !
-        CALL iom_get( numrtr, jpdom_autoglo, 'qint_c14', qint_c14 ) 
+        IF(lrxios) CALL iom_swap(crtxios_context)
+        CALL iom_get( numrtr, jpdom_autoglo, 'qint_c14', qint_c14, ldxios = lrxios ) 
+        IF(lrxios) CALL iom_swap(cxios_context)
         !
       ENDIF
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/C14/trcsms_c14.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/C14/trcsms_c14.F90	(revision 12960)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/C14/trcsms_c14.F90	(revision 12961)
@@ -143,10 +143,12 @@
          IF(lwp) WRITE(numout,*) ' ~~~~~~~~~~~~~~'
          !
-         CALL iom_rstput( kt, nitrst, numrtw, 'co2sbc', co2sbc )       ! These five need      &
-         CALL iom_rstput( kt, nitrst, numrtw, 'c14sbc', c14sbc )     ! &    to be written   &
-         CALL iom_rstput( kt, nitrst, numrtw, 'exch_co2', exch_co2 ) ! &    for temporal    &
-         CALL iom_rstput( kt, nitrst, numrtw, 'exch_c14', exch_c14 ) ! &    averages        &
-         CALL iom_rstput( kt, nitrst, numrtw, 'qtr_c14', qtr_c14 )   ! &    to be coherent.
-         CALL iom_rstput( kt, nitrst, numrtw, 'qint_c14', qint_c14 ) ! Cumulative
+         IF( lwxios ) CALL iom_swap(      cwtxios_context         )
+         CALL iom_rstput( kt, nitrst, numrtw, 'co2sbc', co2sbc,     ldxios = lwxios ) ! These five need      &
+         CALL iom_rstput( kt, nitrst, numrtw, 'c14sbc', c14sbc,     ldxios = lwxios ) ! &    to be written   &
+         CALL iom_rstput( kt, nitrst, numrtw, 'exch_co2', exch_co2, ldxios = lwxios ) ! &    for temporal    &
+         CALL iom_rstput( kt, nitrst, numrtw, 'exch_c14', exch_c14, ldxios = lwxios ) ! &    averages        &
+         CALL iom_rstput( kt, nitrst, numrtw, 'qtr_c14', qtr_c14,   ldxios = lwxios ) ! &    to be coherent.
+         CALL iom_rstput( kt, nitrst, numrtw, 'qint_c14', qint_c14, ldxios = lwxios ) ! Cumulative
+         IF( lwxios ) CALL iom_swap(      cxios_context         )
          !
       ENDIF
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/CFC/trcsms_cfc.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/CFC/trcsms_cfc.F90	(revision 12960)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/CFC/trcsms_cfc.F90	(revision 12961)
@@ -178,8 +178,10 @@
          IF(lwp) WRITE(numout,*) '~~~~'
          jl = 0
+         IF( lwxios ) CALL iom_swap(      cwtxios_context         )
          DO jn = jp_cfc0, jp_cfc1
              jl = jl + 1
-            CALL iom_rstput( kt, nitrst, numrtw, 'qint_'//ctrcnm(jn), qint_cfc(:,:,jl) )
+            CALL iom_rstput( kt, nitrst, numrtw, 'qint_'//ctrcnm(jn), qint_cfc(:,:,jl), ldxios = lwxios )
          END DO
+         IF( lwxios ) CALL iom_swap(      cxios_context         )
       ENDIF                                            
       !
@@ -295,8 +297,10 @@
          !
          jl = 0
+         IF(lrxios) CALL iom_swap(crtxios_context)
          DO jn = jp_cfc0, jp_cfc1
             jl = jl + 1
-            CALL iom_get( numrtr, jpdom_autoglo, 'qint_'//ctrcnm(jn), qint_cfc(:,:,jl) ) 
+            CALL iom_get( numrtr, jpdom_autoglo, 'qint_'//ctrcnm(jn), qint_cfc(:,:,jl), ldxios = lrxios ) 
          END DO
+         IF(lrxios) CALL iom_swap(cxios_context)
       ENDIF
       IF(lwp) WRITE(numout,*)
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/PISCES/P2Z/p2zexp.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/PISCES/P2Z/p2zexp.F90	(revision 12960)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/PISCES/P2Z/p2zexp.F90	(revision 12961)
@@ -133,6 +133,8 @@
             &                    'at it= ', kt,' date= ', ndastp
          IF(lwp) WRITE(numout,*) '~~~~'
-         CALL iom_rstput( kt, nitrst, numrtw, 'SEDB'//ctrcnm(jpdet), sedpocb(:,:) )
-         CALL iom_rstput( kt, nitrst, numrtw, 'SEDN'//ctrcnm(jpdet), sedpocn(:,:) )
+         IF( lwxios ) CALL iom_swap(      cwtxios_context         )
+         CALL iom_rstput( kt, nitrst, numrtw, 'SEDB'//ctrcnm(jpdet), sedpocb(:,:), ldxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrtw, 'SEDN'//ctrcnm(jpdet), sedpocn(:,:), ldxios = lwxios )
+         IF( lwxios ) CALL iom_swap(      cxios_context         )
       ENDIF
       !
@@ -213,6 +215,8 @@
       !
       IF( ln_rsttr ) THEN
-         CALL iom_get( numrtr, jpdom_autoglo, 'SEDB'//ctrcnm(jpdet), sedpocb(:,:) )
-         CALL iom_get( numrtr, jpdom_autoglo, 'SEDN'//ctrcnm(jpdet), sedpocn(:,:) )
+         IF(lrxios) CALL iom_swap(crtxios_context)
+         CALL iom_get( numrtr, jpdom_autoglo, 'SEDB'//ctrcnm(jpdet), sedpocb(:,:), ldxios = lrxios )
+         CALL iom_get( numrtr, jpdom_autoglo, 'SEDN'//ctrcnm(jpdet), sedpocn(:,:), ldxios = lrxios )
+         IF(lrxios) CALL iom_swap(cxios_context)
       ELSE
          sedpocb(:,:) = 0._wp
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/PISCES/P4Z/p4zsms.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/PISCES/P4Z/p4zsms.F90	(revision 12960)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/PISCES/P4Z/p4zsms.F90	(revision 12961)
@@ -339,13 +339,14 @@
          IF(lwp) WRITE(numout,*) ' ~~~~~~~~~~~~~~'
          ! 
+         IF(lrxios) CALL iom_swap(crtxios_context)
          IF( iom_varid( numrtr, 'PH', ldstop = .FALSE. ) > 0 ) THEN
-            CALL iom_get( numrtr, jpdom_autoglo, 'PH' , hi(:,:,:)  )
+            CALL iom_get( numrtr, jpdom_autoglo, 'PH' , hi(:,:,:), ldxios = lrxios  )
          ELSE
             CALL p4z_che( Kbb, Kmm )                  ! initialize the chemical constants
             CALL ahini_for_at( hi, Kbb )
          ENDIF
-         CALL iom_get( numrtr, jpdom_autoglo, 'Silicalim', xksi(:,:) )
+         CALL iom_get( numrtr, jpdom_autoglo, 'Silicalim', xksi(:,:), ldxios = lrxios )
          IF( iom_varid( numrtr, 'Silicamax', ldstop = .FALSE. ) > 0 ) THEN
-            CALL iom_get( numrtr, jpdom_autoglo, 'Silicamax' , xksimax(:,:)  )
+            CALL iom_get( numrtr, jpdom_autoglo, 'Silicamax' , xksimax(:,:), ldxios = lrxios  )
          ELSE
             xksimax(:,:) = xksi(:,:)
@@ -353,5 +354,5 @@
          !
          IF( iom_varid( numrtr, 'tcflxcum', ldstop = .FALSE. ) > 0 ) THEN  ! cumulative total flux of carbon
-            CALL iom_get( numrtr, 'tcflxcum' , t_oce_co2_flx_cum  )
+            CALL iom_get( numrtr, 'tcflxcum' , t_oce_co2_flx_cum, ldxios = lrxios  )
          ELSE
             t_oce_co2_flx_cum = 0._wp
@@ -360,7 +361,7 @@
          IF( ln_p5z ) THEN
             IF( iom_varid( numrtr, 'sized', ldstop = .FALSE. ) > 0 ) THEN
-               CALL iom_get( numrtr, jpdom_autoglo, 'sizep' , sizep(:,:,:)  )
-               CALL iom_get( numrtr, jpdom_autoglo, 'sizen' , sizen(:,:,:)  )
-               CALL iom_get( numrtr, jpdom_autoglo, 'sized' , sized(:,:,:)  )
+               CALL iom_get( numrtr, jpdom_autoglo, 'sizep' , sizep(:,:,:), ldxios = lrxios  )
+               CALL iom_get( numrtr, jpdom_autoglo, 'sizen' , sizen(:,:,:), ldxios = lrxios  )
+               CALL iom_get( numrtr, jpdom_autoglo, 'sized' , sized(:,:,:), ldxios = lrxios  )
             ELSE
                sizep(:,:,:) = 1.
@@ -369,4 +370,5 @@
             ENDIF
         ENDIF
+        IF(lrxios) CALL iom_swap(cxios_context)
         !
       ELSEIF( TRIM(cdrw) == 'WRITE' ) THEN
@@ -376,13 +378,15 @@
             IF(lwp) WRITE(numout,*) '~~~~~~~'
          ENDIF
-         CALL iom_rstput( kt, nitrst, numrtw, 'PH', hi(:,:,:) )
-         CALL iom_rstput( kt, nitrst, numrtw, 'Silicalim', xksi(:,:) )
-         CALL iom_rstput( kt, nitrst, numrtw, 'Silicamax', xksimax(:,:) )
-         CALL iom_rstput( kt, nitrst, numrtw, 'tcflxcum', t_oce_co2_flx_cum )
+         IF( lwxios ) CALL iom_swap(      cwtxios_context         )
+         CALL iom_rstput( kt, nitrst, numrtw, 'PH', hi(:,:,:),               ldxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrtw, 'Silicalim', xksi(:,:),        ldxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrtw, 'Silicamax', xksimax(:,:),     ldxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrtw, 'tcflxcum', t_oce_co2_flx_cum, ldxios = lwxios )
          IF( ln_p5z ) THEN
-            CALL iom_rstput( kt, nitrst, numrtw, 'sizep', sizep(:,:,:) )
-            CALL iom_rstput( kt, nitrst, numrtw, 'sizen', sizen(:,:,:) )
-            CALL iom_rstput( kt, nitrst, numrtw, 'sized', sized(:,:,:) )
-         ENDIF
+            CALL iom_rstput( kt, nitrst, numrtw, 'sizep', sizep(:,:,:), ldxios = lwxios )
+            CALL iom_rstput( kt, nitrst, numrtw, 'sizen', sizen(:,:,:), ldxios = lwxios )
+            CALL iom_rstput( kt, nitrst, numrtw, 'sized', sized(:,:,:), ldxios = lwxios )
+         ENDIF
+         IF( lwxios ) CALL iom_swap(      cxios_context         )
       ENDIF
       !
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/PISCES/SED/sedrst.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/PISCES/SED/sedrst.F90	(revision 12960)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/PISCES/SED/sedrst.F90	(revision 12961)
@@ -42,4 +42,5 @@
       CHARACTER(LEN=50)   ::   clname   ! trc output restart file name
       CHARACTER(LEN=256)  ::   clpath   ! full path to ocean output restart file
+      CHARACTER(LEN=52)   ::   clpname   ! trc output restart file name including AGRIF
       !!----------------------------------------------------------------------
       !
@@ -80,5 +81,23 @@
          IF(lwp) WRITE(numsed,*) &
              '             open sed restart.output NetCDF file: ',TRIM(clpath)//clname
-         CALL iom_open( TRIM(clpath)//TRIM(clname), numrsw, ldwrt = .TRUE., kdlev = jpksed, cdcomp = 'SED' )
+         IF(.NOT.lwxios) THEN
+            CALL iom_open( TRIM(clpath)//TRIM(clname), numrsw, ldwrt = .TRUE., kdlev = jpksed, cdcomp = 'SED' )
+         ELSE
+#if defined key_iomput
+            cwsxios_context = "rstws_"//TRIM(ADJUSTL(clkt))
+            IF( TRIM(Agrif_CFixed()) == '0' ) THEN
+               clpname = clname
+            ELSE
+               clpname = TRIM(Agrif_CFixed())//"_"//clname
+            ENDIF
+            CALL iom_init( cwsxios_context, TRIM(clpath)//TRIM(clpname), ld_tmppatch = .false.,&
+                                                                         ld_closedef = .FALSE. )
+            CALL iom_swap(      cxios_context          )
+#else
+               clinfo = 'Can not use XIOS in trc_rst_opn'
+               CALL ctl_stop(TRIM(clinfo))
+#endif
+            ENDIF
+
          lrst_sed = .TRUE.
       ENDIF
@@ -120,8 +139,9 @@
       zdta2 = 0.
 
+      IF(lrxios) CALL iom_swap(crsxios_context)
       DO jn = 1, jptrased
          cltra = TRIM(sedtrcd(jn))
          IF( iom_varid( numrsr, TRIM(cltra) , ldstop = .FALSE. ) > 0 ) THEN
-            CALL iom_get( numrsr, jpdom_autoglo, TRIM(cltra), zdta(:,:,:,jn) )
+            CALL iom_get( numrsr, jpdom_autoglo, TRIM(cltra), zdta(:,:,:,jn), ldxios = lrxios )
          ELSE
             zdta(:,:,:,jn) = 0.0
@@ -142,5 +162,5 @@
          cltra = TRIM(seddia3d(jn))
          IF( iom_varid( numrsr, TRIM(cltra) , ldstop = .FALSE. ) > 0 ) THEN
-            CALL iom_get( numrsr, jpdom_autoglo, TRIM(cltra), zdta1(:,:,:,jn) )
+            CALL iom_get( numrsr, jpdom_autoglo, TRIM(cltra), zdta1(:,:,:,jn), ldxios = lrxios )
          ELSE
             zdta1(:,:,:,jn) = 0.0
@@ -169,5 +189,5 @@
       cltra = "dbioturb"
       IF( iom_varid( numrsr, TRIM(cltra) , ldstop = .FALSE. ) > 0 ) THEN
-         CALL iom_get( numrsr, jpdom_autoglo, TRIM(cltra), zdta2(:,:,:) )
+         CALL iom_get( numrsr, jpdom_autoglo, TRIM(cltra), zdta2(:,:,:), ldxios = lrxios )
       ELSE
          zdta2(:,:,:) = 0.0
@@ -179,5 +199,5 @@
       cltra = "irrig"
       IF( iom_varid( numrsr, TRIM(cltra) , ldstop = .FALSE. ) > 0 ) THEN
-         CALL iom_get( numrsr, jpdom_autoglo, TRIM(cltra), zdta2(:,:,:) )
+         CALL iom_get( numrsr, jpdom_autoglo, TRIM(cltra), zdta2(:,:,:), ldxios = lrxios )
       ELSE
          zdta2(:,:,:) = 0.0
@@ -189,5 +209,5 @@
       cltra = "sedligand"
       IF( iom_varid( numrsr, TRIM(cltra) , ldstop = .FALSE. ) > 0 ) THEN
-         CALL iom_get( numrsr, jpdom_autoglo, TRIM(cltra), zdta2(:,:,:) )
+         CALL iom_get( numrsr, jpdom_autoglo, TRIM(cltra), zdta2(:,:,:), ldxios = lrxios )
       ELSE
          zdta2(:,:,:) = 0.0
@@ -196,5 +216,5 @@
       CALL pack_arr( jpoce, sedligand(1:jpoce,1:jpksed), &
          &             zdta2(1:jpi,1:jpj,1:jpksed), iarroce(1:jpoce) )
-
+      IF(lrxios) CALL iom_swap(cxios_context)
       IF( ln_timing )  CALL timing_stop('sed_rst_read')
      
@@ -240,7 +260,7 @@
       !! 1. WRITE in nutwrs
       !! ------------------
-
-      zinfo(1) = REAL( kt)
-      CALL iom_rstput( kt, nitrst, numrsw, 'kt', zinfo  )
+      IF( lwxios ) CALL iom_swap(      cwsxios_context         )
+!     zinfo(1) = REAL( kt)
+      CALL iom_rstput( kt, nitrst, numrsw, 'kt', REAL( kt    , wp), ldxios = lwxios )
 
       ! Back to 2D geometry
@@ -272,10 +292,10 @@
       DO jn = 1, jptrased
          cltra = TRIM(sedtrcd(jn))
-         CALL iom_rstput( kt, nitrst, numrsw, TRIM(cltra), trcsedi(:,:,:,jn) )
+         CALL iom_rstput( kt, nitrst, numrsw, TRIM(cltra), trcsedi(:,:,:,jn), ldxios = lwxios )
       ENDDO
 
       DO jn = 1, 2
          cltra = TRIM(seddia3d(jn))
-         CALL iom_rstput( kt, nitrst, numrsw, TRIM(cltra), flxsedi3d(:,:,:,jn) )
+         CALL iom_rstput( kt, nitrst, numrsw, TRIM(cltra), flxsedi3d(:,:,:,jn), ldxios = lwxios )
       ENDDO
 
@@ -284,5 +304,5 @@
 
       cltra = "dbioturb"
-      CALL iom_rstput( kt, nitrst, numrsw, TRIM(cltra), zdta2(:,:,:) )
+      CALL iom_rstput( kt, nitrst, numrsw, TRIM(cltra), zdta2(:,:,:), ldxios = lwxios )
 
       CALL unpack_arr( jpoce, zdta2(1:jpi,1:jpj,1:jpksed)  , iarroce(1:jpoce), &
@@ -290,5 +310,5 @@
 
       cltra = "irrig"
-      CALL iom_rstput( kt, nitrst, numrsw, TRIM(cltra), zdta2(:,:,:) )
+      CALL iom_rstput( kt, nitrst, numrsw, TRIM(cltra), zdta2(:,:,:), ldxios = lwxios )
 
       CALL unpack_arr( jpoce, zdta2(1:jpi,1:jpj,1:jpksed)  , iarroce(1:jpoce), &
@@ -296,8 +316,13 @@
 
       cltra = "sedligand"
-      CALL iom_rstput( kt, nitrst, numrsw, TRIM(cltra), zdta2(:,:,:) )
+      CALL iom_rstput( kt, nitrst, numrsw, TRIM(cltra), zdta2(:,:,:), ldxios = lwxios )
+      IF( lwxios ) CALL iom_swap(      cxios_context         )
 
       IF( kt == nitrst ) THEN
-          CALL iom_close( numrsw )     ! close the restart file (only at last time step)
+          IF(.NOT.lwxios) THEN
+             CALL iom_close( numrsw )     ! close the restart file (only at last time step)
+          ELSE
+             CALL iom_context_finalize( cwsxios_context ) 
+          ENDIF
           IF( l_offline .AND. ln_rst_list ) THEN
              nrst_lst = nrst_lst + 1
@@ -342,4 +367,5 @@
       REAL(wp) ::  zkt, zrdttrc1
       REAL(wp) ::  zndastp
+      CHARACTER(len = 82) :: clpname
 
       ! Time domain : restart
@@ -353,7 +379,24 @@
 
          IF( ln_rst_sed ) THEN
+            lxios_sini = .FALSE.
             CALL iom_open( TRIM(cn_sedrst_indir)//'/'//cn_sedrst_in, numrsr )
-            CALL iom_get ( numrsr, 'kt', zkt )   ! last time-step of previous run
-
+
+      IF( lrxios .AND. .NOT. lxios_sini) THEN
+          CALL ctl_stop('OCE and SED restart must be in a single file when XIOS is used to read restart')
+      ENDIF
+      IF( lrxios) THEN
+          crsxios_context = 'sed_rst'
+          IF(lwp) WRITE(numout,*) 'Enable restart reading by XIOS for SED'
+          IF( TRIM(Agrif_CFixed()) == '0' ) THEN
+             clpname = cn_sedrst_in
+          ELSE
+             clpname = TRIM(Agrif_CFixed())//"_"//cn_sedrst_in   
+          ENDIF
+          CALL iom_init( crsxios_context, fname = TRIM(cn_sedrst_indir)//'/'//TRIM(clpname), &
+                                          idfp = iom_file(numrsr)%nfid, ld_tmppatch = .TRUE. )
+      ENDIF
+            IF(lrxios) CALL iom_swap(crsxios_context)
+            CALL iom_get ( numrsr, 'kt', zkt, ldxios = lrxios )   ! last time-step of previous run
+            IF(lrxios) CALL iom_swap(cxios_context)
             IF(lwp) THEN
                WRITE(numsed,*) ' *** Info read in restart : '
@@ -376,7 +419,9 @@
             !                                          ! set the date in offline mode
             IF( ln_rst_sed .AND. nn_rstsed == 2 ) THEN
-               CALL iom_get( numrsr, 'ndastp', zndastp )
+               IF(lrxios) CALL iom_swap(crsxios_context)
+               CALL iom_get( numrsr, 'ndastp', zndastp, ldxios = lrxios )
                ndastp = NINT( zndastp )
-               CALL iom_get( numrsr, 'adatrj', adatrj  )
+               CALL iom_get( numrsr, 'adatrj', adatrj, ldxios = lrxios  )
+               IF(lrxios) CALL iom_swap(crxios_context)
              ELSE
                ndastp = ndate0 - 1     ! ndate0 read in the namelist in dom_nam
@@ -402,9 +447,12 @@
             IF(lwp) WRITE(numsed,*) 'trc_wri : write the TOP restart file (NetCDF) at it= ', kt, ' date= ', ndastp
             IF(lwp) WRITE(numsed,*) '~~~~~~~'
-         ENDIF
-         CALL iom_rstput( kt, nitrst, numrsw, 'kt'     , REAL( kt    , wp) )   ! time-step
-         CALL iom_rstput( kt, nitrst, numrsw, 'ndastp' , REAL( ndastp, wp) )   ! date
-         CALL iom_rstput( kt, nitrst, numrsw, 'adatrj' , adatrj            )   ! number of elapsed days since
-         !                                                                     ! the begining of the run [s]
+            IF( lwxios ) CALL iom_init_closedef(cwsxios_context)
+         ENDIF
+         IF( lwxios ) CALL iom_swap(      cwsxios_context         )
+         CALL iom_rstput( kt, nitrst, numrsw, 'kt'     , REAL( kt    , wp), ldxios = lwxios )   ! time-step
+         CALL iom_rstput( kt, nitrst, numrsw, 'ndastp' , REAL( ndastp, wp), ldxios = lwxios )   ! date
+         CALL iom_rstput( kt, nitrst, numrsw, 'adatrj' , adatrj,            ldxios = lwxios )   ! number of elapsed days since
+         !                                                                                      ! the begining of the run [s]
+         IF( lwxios ) CALL iom_swap(      cxios_context         )
       ENDIF
 
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/PISCES/SED/sedstp.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/PISCES/SED/sedstp.F90	(revision 12960)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/PISCES/SED/sedstp.F90	(revision 12961)
@@ -86,4 +86,5 @@
       IF( kt == nitsed000 ) THEN
           CALL iom_close( numrsr )       ! close input tracer restart file
+          IF(lrxios) CALL iom_context_finalize(      crsxios_context  )
 !          IF(lwm) CALL FLUSH( numont )   ! flush namelist output
       ENDIF
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/TRP/trcsbc.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/TRP/trcsbc.F90	(revision 12960)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/TRP/trcsbc.F90	(revision 12961)
@@ -86,7 +86,9 @@
             IF(lwp) WRITE(numout,*) '          nittrc000-1 surface tracer content forcing fields read in the restart file'
             zfact = 0.5_wp
+            IF(lrxios) CALL iom_swap(crtxios_context)
             DO jn = 1, jptra
-               CALL iom_get( numrtr, jpdom_autoglo, 'sbc_'//TRIM(ctrcnm(jn))//'_b', sbc_trc_b(:,:,jn) )   ! before tracer content sbc
+               CALL iom_get( numrtr, jpdom_autoglo, 'sbc_'//TRIM(ctrcnm(jn))//'_b', sbc_trc_b(:,:,jn), ldxios = lrxios )   ! before tracer content sbc
             END DO
+            IF(lrxios) CALL iom_swap(cxios_context)
          ELSE                                         ! No restart or restart not found: Euler forward time stepping
            zfact = 1._wp
@@ -180,7 +182,9 @@
             &                    'at it= ', kt,' date= ', ndastp
          IF(lwp) WRITE(numout,*) '~~~~'
-         DO jn = 1, jptra
-            CALL iom_rstput( kt, nitrst, numrtw, 'sbc_'//TRIM(ctrcnm(jn))//'_b', sbc_trc(:,:,jn) )
-         END DO
+         IF( lwxios ) CALL iom_swap(      cwtxios_context         )
+         DO jn = 1, jptra
+            CALL iom_rstput( kt, nitrst, numrtw, 'sbc_'//TRIM(ctrcnm(jn))//'_b', sbc_trc(:,:,jn), ldxios = lwxios )
+         END DO
+         IF( lwxios ) CALL iom_swap(      cxios_context         )
       ENDIF
       !
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/trcrst.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/trcrst.F90	(revision 12960)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/trcrst.F90	(revision 12961)
@@ -51,4 +51,5 @@
       CHARACTER(LEN=50)   ::   clname   ! trc output restart file name
       CHARACTER(LEN=256)  ::   clpath   ! full path to ocean output restart file
+      CHARACTER(LEN=50)   ::   clpname  ! trc output restart file name including AGRIF
       !!----------------------------------------------------------------------
       !
@@ -90,5 +91,22 @@
          IF(lwp) WRITE(numout,*) &
              '             open trc restart.output NetCDF file: ',TRIM(clpath)//clname
-         CALL iom_open( TRIM(clpath)//TRIM(clname), numrtw, ldwrt = .TRUE. )
+         IF(.NOT.lwxios) THEN
+            CALL iom_open( TRIM(clpath)//TRIM(clname), numrtw, ldwrt = .TRUE. )
+         ELSE
+#if defined key_iomput
+            cwtxios_context = "rstwt_"//TRIM(ADJUSTL(clkt))
+            IF( TRIM(Agrif_CFixed()) == '0' ) THEN
+               clpname = clname
+            ELSE
+               clpname = TRIM(Agrif_CFixed())//"_"//clname
+            ENDIF
+            CALL iom_init( cwtxios_context, TRIM(clpath)//TRIM(clpname), ld_tmppatch = .false.,&
+                                                                         ld_closedef = .FALSE. )
+            CALL iom_swap(      cxios_context          )
+#else
+               clinfo = 'Can not use XIOS in trc_rst_opn'
+               CALL ctl_stop(TRIM(clinfo))
+#endif
+            ENDIF
          lrst_trc = .TRUE.
       ENDIF
@@ -112,14 +130,15 @@
 
       ! READ prognostic variables and computes diagnostic variable
+      IF(lrxios) CALL iom_swap(crtxios_context)
       DO jn = 1, jptra
-         CALL iom_get( numrtr, jpdom_autoglo, 'TRN'//ctrcnm(jn), tr(:,:,:,jn,Kmm) )
+         CALL iom_get( numrtr, jpdom_autoglo, 'TRN'//ctrcnm(jn), tr(:,:,:,jn,Kmm), ldxios = lrxios )
       END DO
 
       DO jn = 1, jptra
-         CALL iom_get( numrtr, jpdom_autoglo, 'TRB'//ctrcnm(jn), tr(:,:,:,jn,Kbb) )
+         CALL iom_get( numrtr, jpdom_autoglo, 'TRB'//ctrcnm(jn), tr(:,:,:,jn,Kbb), ldxios = lrxios )
       END DO
       !
       CALL iom_delay_rst( 'READ', 'TOP', numrtr )   ! read only TOP delayed global communication variables
-      
+      IF(lrxios) CALL iom_swap(cxios_context) 
    END SUBROUTINE trc_rst_read
 
@@ -136,14 +155,16 @@
       !!----------------------------------------------------------------------
       !
-      CALL iom_rstput( kt, nitrst, numrtw, 'rdttrc1', rn_Dt )   ! passive tracer time step (= ocean time step)
+      IF( lwxios ) CALL iom_swap(      cwtxios_context         )
+      CALL iom_rstput( kt, nitrst, numrtw, 'rdttrc1', rn_Dt, ldxios = lwxios )   ! passive tracer time step (= ocean time step)
       ! prognostic variables 
       ! -------------------- 
       DO jn = 1, jptra
-         CALL iom_rstput( kt, nitrst, numrtw, 'TRN'//ctrcnm(jn), tr(:,:,:,jn,Kmm) )
+         CALL iom_rstput( kt, nitrst, numrtw, 'TRN'//ctrcnm(jn), tr(:,:,:,jn,Kmm), ldxios = lwxios )
       END DO
 
       DO jn = 1, jptra
-         CALL iom_rstput( kt, nitrst, numrtw, 'TRB'//ctrcnm(jn), tr(:,:,:,jn,Kbb) )
-      END DO
+         CALL iom_rstput( kt, nitrst, numrtw, 'TRB'//ctrcnm(jn), tr(:,:,:,jn,Kbb), ldxios = lwxios )
+      END DO
+      IF( lwxios ) CALL iom_swap(      cxios_context         )
       !
       CALL iom_delay_rst( 'WRITE', 'TOP', numrtw )   ! save only TOP delayed global communication variables
@@ -151,5 +172,9 @@
       IF( kt == nitrst ) THEN
           CALL trc_rst_stat( Kmm, Krhs )             ! statistics
-          CALL iom_close( numrtw )     ! close the restart file (only at last time step)
+          IF(lwxios) THEN
+             CALL iom_context_finalize(      cwtxios_context          )
+          ELSE
+             CALL iom_close( numrtw )     ! close the restart file (only at last time step)
+          ENDIF
 #if ! defined key_trdmxl_trc
           lrst_trc = .FALSE.
@@ -195,4 +220,5 @@
       REAL(wp) ::  zrdttrc1, zkt, zndastp, zdayfrac, ksecs, ktime
       INTEGER  ::   ihour, iminute
+      CHARACTER(len=82) :: clpname
 
       ! Time domain : restart
@@ -206,6 +232,24 @@
 
          IF( ln_rsttr ) THEN
+            lxios_sini = .FALSE.
             CALL iom_open( TRIM(cn_trcrst_indir)//'/'//cn_trcrst_in, numrtr )
-            CALL iom_get ( numrtr, 'kt', zkt )   ! last time-step of previous run
+            IF( lrxios .AND. .NOT. lxios_sini) THEN
+               CALL ctl_stop('OCE and TOP restart must be in a single file when XIOS is used to read restart')
+            ENDIF
+            IF( lrxios) THEN
+                crtxios_context = 'top_rst'
+                IF(lwp) WRITE(numout,*) 'Enable restart reading by XIOS for TOP'
+                IF( TRIM(Agrif_CFixed()) == '0' ) THEN
+                   clpname = cn_trcrst_in
+                ELSE
+                   clpname = TRIM(Agrif_CFixed())//"_"//cn_trcrst_in   
+                ENDIF
+                CALL iom_init( crtxios_context, fname = TRIM(cn_trcrst_indir)//'/'//TRIM(clpname), &
+                                          idfp = iom_file(numrtr)%nfid, ld_tmppatch = .TRUE. )
+            ENDIF
+
+            IF(lrxios) CALL iom_swap(crtxios_context)
+            CALL iom_get ( numrtr, 'kt', zkt, ldxios = lrxios )   ! last time-step of previous run
+            IF(lrxios) CALL iom_swap(cxios_context)
 
             IF(lwp) THEN
@@ -229,8 +273,10 @@
             !                                          ! set the date in offline mode
             IF( ln_rsttr .AND. nn_rsttr == 2 ) THEN
-               CALL iom_get( numrtr, 'ndastp', zndastp )
+               IF(lrxios) CALL iom_swap(crtxios_context)
+               CALL iom_get( numrtr, 'ndastp', zndastp, ldxios = lrxios )
                ndastp = NINT( zndastp )
-               CALL iom_get( numrtr, 'adatrj', adatrj  )
-               CALL iom_get( numrtr, 'ntime' , ktime   )
+               CALL iom_get( numrtr, 'adatrj', adatrj, ldxios = lrxios  )
+               CALL iom_get( numrtr, 'ntime' , ktime, ldxios = lrxios   )
+               IF(lrxios) CALL iom_swap(cxios_context)
                nn_time0=INT(ktime)
                ! calculate start time in hours and minutes
@@ -291,9 +337,11 @@
             IF(lwp) WRITE(numout,*) '~~~~~~~'
          ENDIF
-         CALL iom_rstput( kt, nitrst, numrtw, 'kt'     , REAL( kt    , wp) )   ! time-step
-         CALL iom_rstput( kt, nitrst, numrtw, 'ndastp' , REAL( ndastp, wp) )   ! date
-         CALL iom_rstput( kt, nitrst, numrtw, 'adatrj' , adatrj            )   ! number of elapsed days since
+         IF( lwxios ) CALL iom_swap(      cwtxios_context         )
+         CALL iom_rstput( kt, nitrst, numrtw, 'kt'     , REAL( kt    , wp)  , ldxios = lwxios )   ! time-step
+         CALL iom_rstput( kt, nitrst, numrtw, 'ndastp' , REAL( ndastp, wp)  , ldxios = lwxios )   ! date
+         CALL iom_rstput( kt, nitrst, numrtw, 'adatrj' , adatrj             , ldxios = lwxios )   ! number of elapsed days since
          !                                                                     ! the begining of the run [s]
-         CALL iom_rstput( kt, nitrst, numrtw, 'ntime'  , REAL( nn_time0, wp)) ! time
+         CALL iom_rstput( kt, nitrst, numrtw, 'ntime'  , REAL( nn_time0, wp), ldxios = lwxios ) ! time
+         IF( lwxios ) CALL iom_swap(      cxios_context         )
       ENDIF
 
Index: /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/trcstp.F90
===================================================================
--- /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/trcstp.F90	(revision 12960)
+++ /NEMO/branches/2020/dev_12905_xios_restart/src/TOP/trcstp.F90	(revision 12961)
@@ -109,4 +109,5 @@
       IF( kt == nittrc000 ) THEN
          CALL iom_close( numrtr )                         ! close input tracer restart file
+         IF(lrxios) CALL iom_context_finalize(      crtxios_context          )
          IF(lwm) CALL FLUSH( numont )                     ! flush namelist output
       ENDIF
@@ -195,18 +196,18 @@
            &                              .AND. iom_varid( numrtr, 'ktdcy'    , ldstop = .FALSE. ) > 0  &
            &                              .AND. iom_varid( numrtr, 'nrdcy'    , ldstop = .FALSE. ) > 0  ) THEN
-
-            CALL iom_get( numrtr, 'ktdcy', zkt )  
+            IF(lrxios) CALL iom_swap(crtxios_context)
+            CALL iom_get( numrtr, 'ktdcy', zkt, ldxios = lrxios )  
             rsecfst = INT( zkt ) * rn_Dt
             IF(lwp) WRITE(numout,*) 'trc_qsr_mean:   qsr_mean read in the restart file at time-step rsecfst =', rsecfst, ' s '
-            CALL iom_get( numrtr, jpdom_autoglo, 'qsr_mean', qsr_mean )   !  A mean of qsr
-            CALL iom_get( numrtr, 'nrdcy', zrec )   !  Number of record per days
+            CALL iom_get( numrtr, jpdom_autoglo, 'qsr_mean', qsr_mean, ldxios = lrxios )   !  A mean of qsr
+            CALL iom_get( numrtr, 'nrdcy', zrec, ldxios = lrxios )   !  Number of record per days
             IF( INT( zrec ) == nb_rec_per_day ) THEN
                DO jn = 1, nb_rec_per_day 
                   IF( jn <= 9 )  THEN
                     WRITE(cl1,'(i1)') jn
-                    CALL iom_get( numrtr, jpdom_autoglo, 'qsr_arr_'//cl1, qsr_arr(:,:,jn) )   !  A mean of qsr
+                    CALL iom_get( numrtr, jpdom_autoglo, 'qsr_arr_'//cl1, qsr_arr(:,:,jn), ldxios = lrxios )   !  A mean of qsr
                   ELSE
                     WRITE(cl2,'(i2.2)') jn
-                    CALL iom_get( numrtr, jpdom_autoglo, 'qsr_arr_'//cl2, qsr_arr(:,:,jn) )   !  A mean of qsr
+                    CALL iom_get( numrtr, jpdom_autoglo, 'qsr_arr_'//cl2, qsr_arr(:,:,jn), ldxios = lrxios )   !  A mean of qsr
                   ENDIF
               END DO
@@ -216,4 +217,5 @@
                ENDDO
             ENDIF
+            IF(lrxios) CALL iom_swap(cxios_context)
          ELSE                                         !* no restart: set from nit000 values
             IF(lwp) WRITE(numout,*) 'trc_qsr_mean:   qsr_mean set to nit000 values'
@@ -249,16 +251,18 @@
          zkt  = REAL( ktdcy, wp )
          zrec = REAL( nb_rec_per_day, wp )
-         CALL iom_rstput( kt, nitrst, numrtw, 'ktdcy', zkt  )
-         CALL iom_rstput( kt, nitrst, numrtw, 'nrdcy', zrec )
+         IF( lwxios ) CALL iom_swap(      cwtxios_context         )
+         CALL iom_rstput( kt, nitrst, numrtw, 'ktdcy', zkt, ldxios = lwxios  )
+         CALL iom_rstput( kt, nitrst, numrtw, 'nrdcy', zrec, ldxios = lwxios )
           DO jn = 1, nb_rec_per_day 
              IF( jn <= 9 )  THEN
                WRITE(cl1,'(i1)') jn
-               CALL iom_rstput( kt, nitrst, numrtw, 'qsr_arr_'//cl1, qsr_arr(:,:,jn) )
+               CALL iom_rstput( kt, nitrst, numrtw, 'qsr_arr_'//cl1, qsr_arr(:,:,jn), ldxios = lwxios )
              ELSE
                WRITE(cl2,'(i2.2)') jn
-               CALL iom_rstput( kt, nitrst, numrtw, 'qsr_arr_'//cl2, qsr_arr(:,:,jn) )
+               CALL iom_rstput( kt, nitrst, numrtw, 'qsr_arr_'//cl2, qsr_arr(:,:,jn), ldxios = lwxios )
              ENDIF
          END DO
-         CALL iom_rstput( kt, nitrst, numrtw, 'qsr_mean', qsr_mean(:,:) )
+         CALL iom_rstput( kt, nitrst, numrtw, 'qsr_mean', qsr_mean(:,:), ldxios = lwxios )
+         IF( lwxios ) CALL iom_swap(      cxios_context         )
       ENDIF
       !
