Index: NEMO/branches/2019/dev_r11756_SI3restart_XIOS/src/OCE/IOM/in_out_manager.F90
===================================================================
--- NEMO/branches/2019/dev_r11756_SI3restart_XIOS/src/OCE/IOM/in_out_manager.F90	(revision 11840)
+++ NEMO/branches/2019/dev_r11756_SI3restart_XIOS/src/OCE/IOM/in_out_manager.F90	(revision 11870)
@@ -171,5 +171,4 @@
    CHARACTER(lc) ::   cwxios_context        !: context name used in xios to write NEMO 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
    LOGICAL       ::   lrsi3_nemo            !: is SI3 irestart in nemo format
 
Index: NEMO/branches/2019/dev_r11756_SI3restart_XIOS/src/OCE/IOM/iom.F90
===================================================================
--- NEMO/branches/2019/dev_r11756_SI3restart_XIOS/src/OCE/IOM/iom.F90	(revision 11840)
+++ NEMO/branches/2019/dev_r11756_SI3restart_XIOS/src/OCE/IOM/iom.F90	(revision 11870)
@@ -48,5 +48,5 @@
    USE diurnal_bulk, ONLY : ln_diurnal_only, ln_diurnal
 #if defined key_si3
-   USE ice, ONLY: nlay_i, nlay_s
+   USE ice, ONLY: nlay_i, nlay_s, cn_icerst_outdir, cn_icerst_out
 #endif
 
@@ -72,7 +72,10 @@
    PRIVATE iom_set_rst_context, iom_set_rstw_active, iom_set_rstr_active
 # endif
+   PRIVATE iom_define_restartw, iom_set_rstw_validate
    PUBLIC iom_set_rstw_var_active, iom_set_rstw_core, iom_set_rst_vars
    PRIVATE iom_add
-   INTEGER, PRIVATE :: numrst
+   INTEGER,           PRIVATE    :: numrst
+   CHARACTER(len=12), PRIVATE    :: snr
+
 
    INTERFACE iom_get
@@ -241,14 +244,11 @@
 !set which fields are to be read from restart file
           CALL iom_set_rstr_active(fname)
-      ELSE IF( TRIM(cdname) == TRIM(cwxios_context) ) THEN
+      ELSE IF( TRIM(cdname) == TRIM(cwxios_context)) THEN
+!validate output for issues
+          CALL iom_set_rstw_validate()
 !set names of the fields in restart file IF using XIOS to write data
-          CALL iom_set_rst_context(.FALSE.)
+          CALL iom_set_rst_context(.FALSE.)   ! grid definition
 !set which fields are to be written to a restart file
-          CALL iom_set_rstw_active(rst_wfields, fname)
-      ELSE IF( TRIM(cdname) == TRIM(cwixios_context) ) THEN
-!set names of the fields in restart file IF using XIOS to write data
-          CALL iom_set_rst_context(.FALSE.)
-!set which fields are to be written to a restart file
-          CALL iom_set_rstw_active(rsti_wfields, fname)
+          CALL iom_define_restartw()          ! fields and files definition
       ELSE
           CALL set_xmlatt
@@ -270,4 +270,119 @@
       !
    END SUBROUTINE iom_init
+
+   SUBROUTINE iom_define_restartw()
+      !!---------------------------------------------------------------------
+      !!                   ***  SUBROUTINE  iom_define_restatw  ***
+      !!
+      !! ** Purpose :  Add files in nemo context to write restart (one and si3) 
+      !!---------------------------------------------------------------------
+      INTEGER :: kt
+      CHARACTER(LEN=50)   ::   clname   ! ocean output restart file name
+      CHARACTER(lc)       ::   clpath   ! full path to ocean output restart file
+      CHARACTER(LEN=52)   ::   clpname   ! ocean output restart file name including prefix for AGRIF
+      CHARACTER(LEN=20)   ::   clkt     ! ocean time-step deine as a character
+      INTEGER             ::   iterx, nitrstx, nrst_lstx
+      LOGICAL             ::   lrst_icex, lrst_ocex
+#if defined key_iomput
+
+      IF( ln_rst_list ) THEN
+         nrst_lstx = 1
+         nitrstx = nn_stocklist( nrst_lst )
+      ELSE
+         nitrstx = nitend
+      ENDIF
+
+      IF( .NOT. ln_rst_list .AND. nn_stock == -1 )   RETURN   ! we will never do any restart
+
+      DO kt = nit000, nitend
+         IF( .NOT. ln_rst_list .AND. MOD( kt - 1, nn_stock ) == 0 ) THEN
+            ! we use kt - 1 and not kt - nit000 to keep the same periodicity from the beginning of the experiment
+            nitrstx = kt + nn_stock - 1                  ! define the next value of nitrst for restart writing
+            IF( nitrstx > nitend )   nitrstx = nitend   ! make sure we write a restart at the end of the run
+         ENDIF
+         IF( kt == nitrstx .OR. nn_stock == 1 .OR. kt == nitend ) THEN
+            IF( nitrstx <= nitend .AND. nitrstx > 0 ) THEN
+
+               IF( nitrst > 999999999 ) THEN   ;   WRITE(clkt, *       ) nitrstx
+               ELSE                            ;   WRITE(clkt, '(i8.8)') nitrstx
+               ENDIF
+               ! create the file
+               clname = TRIM(cexper)//"_"//TRIM(ADJUSTL(clkt))//"_"//TRIM(cn_ocerst_out)
+               clpath = TRIM(cn_ocerst_outdir)
+               IF( clpath(LEN_TRIM(clpath):) /= '/' ) clpath = TRIM(clpath) // '/'
+               IF(lwp) THEN
+                  write(numout, *) 'Create restart file at the step: ', kt
+               ENDIF
+               CALL iom_set_rstw_active(rst_wfields, TRIM(clpath)//TRIM(clname), nitrstx)
+            ENDIF
+         ENDIF
+#if defined key_si3
+         IF( kt == nitrstx - 2*nn_fsbc + 1 .OR. nn_stock == nn_fsbc    &
+         &                             .OR. ( kt == nitend - nn_fsbc + 1 .AND. .NOT. lrst_icex ) ) THEN
+            IF( nitrst <= nitend .AND. nitrst > 0 ) THEN
+               ! beware of the format used to write kt (default is i8.8, that should be large enough...)
+               IF( nitrst > 99999999 ) THEN   ;   WRITE(clkt, *       ) nitrstx
+               ELSE                           ;   WRITE(clkt, '(i8.8)') nitrstx
+               ENDIF
+               ! create the file
+               clname = TRIM(cexper)//"_"//TRIM(ADJUSTL(clkt))//"_"//TRIM(cn_icerst_out)
+               clpath = TRIM(cn_icerst_outdir) 
+               IF( clpath(LEN_TRIM(clpath):) /= '/' ) clpath = TRIM(clpath)//'/'
+               IF(lwp) THEN
+                  WRITE(numout,*)
+                  WRITE(numout,*) '             Create ice restart NetCDF file: ',TRIM(clpath)//clname
+                  IF( kt == nitrstx - 2*nn_fsbc + 1 ) THEN
+                     WRITE(numout,*) '             kt = nitrst - 2*nn_fsbc + 1 = ', kt,' date= ', ndastp
+                  ELSE
+                     WRITE(numout,*) '             kt = '                         , kt,' date= ', ndastp
+                  ENDIF
+               ENDIF
+               lrst_icex = .TRUE.
+               CALL iom_set_rstw_active(rsti_wfields, TRIM(clpath)//TRIM(clname), nitrstx)
+            ENDIF
+         ENDIF
+         iterx = kt + nn_fsbc - 1
+         IF( iterx == nitrstx ) THEN
+            lrst_icex = .FALSE.
+            lrst_ocex = .FALSE.
+         ENDIF
+#endif
+         IF( ln_rst_list .AND. kt == nitrst) THEN
+            nrst_lstx = MIN(nrst_lst + 1, SIZE(nn_stocklist,1))
+            nitrstx = nn_stocklist( nrst_lst )
+         ENDIF
+      ENDDO
+#endif
+   END SUBROUTINE iom_define_restartw
+
+   SUBROUTINE iom_set_rstw_validate()
+      !!---------------------------------------------------------------------
+      !!                   ***  SUBROUTINE iom_set_rst_validate   ***
+      !!
+      !! ** Purpose :  validate restart files. This routine is only used if
+      !!               restart is written with XIOS. It will check if the same variable
+      !!               is written into more than 1 file. Currently with XIOS
+      !!               the same variable can't be written into 2 different files. Print
+      !!               warning if it's a case. 
+      !!---------------------------------------------------------------------
+   INTEGER                        :: ik, jk
+   CHARACTER(LEN=256)             :: clinfo      ! info character
+
+#if defined key_si3
+     DO ik =1, max_rst_fields
+        IF(rst_wfields(ik)%active) THEN
+           DO jk = 1, max_rst_fields
+            IF(rsti_wfields(jk)%active .AND. (TRIM(rsti_wfields(jk)%vname) == TRIM(rst_wfields(ik)%vname))) THEN
+                clinfo = 'Varaible '//TRIM(rsti_wfields(jk)%vname)//' will be written in NEMO and SI3 restart file!'
+                CALL ctl_warn( 'From iom_set_rstw_validate: ',                                                &
+                     trim(clinfo), 'This will cause problems with XIOS; use iom interface instead or rename', &
+                     & 'in one of the restart files')
+            ENDIF
+           ENDDO
+        ENDIF
+     ENDDO
+#endif
+
+   END SUBROUTINE iom_set_rstw_validate
 
    SUBROUTINE iom_set_rstw_var_active(drst_wfields, field)
@@ -411,5 +526,5 @@
          CALL iom_set_rstw_var_active(drst_wfields, 'sbc_sc_b')
    ELSE IF(cdmdl == "SI3") THEN
-      CALL iom_set_rstw_var_active(drst_wfields, 'nn_fsbc')
+      CALL iom_set_rstw_var_active(drst_wfields, 'nn_fsbc_ice')
       CALL iom_set_rstw_var_active(drst_wfields, 'kt_ice')
       CALL iom_set_rstw_var_active(drst_wfields, 'v_i')
@@ -524,5 +639,4 @@
         i = iom_add(i); fields(i)%vname="rnf_hc_b";       fields(i)%grid="grid_N"
         i = iom_add(i); fields(i)%vname="rnf_sc_b";       fields(i)%grid="grid_N"
-        i = iom_add(i); fields(i)%vname="nn_fsbc";        fields(i)%grid="grid_scalar"
         i = iom_add(i); fields(i)%vname="ssu_m";          fields(i)%grid="grid_N"
         i = iom_add(i); fields(i)%vname="ssv_m";          fields(i)%grid="grid_N"
@@ -554,4 +668,5 @@
         i = iom_add(i); fields(i)%vname="hbli";           fields(i)%grid="grid_N"
         i = iom_add(i); fields(i)%vname="wn";             fields(i)%grid="grid_N_3D"
+        i = iom_add(i); fields(i)%vname="nn_fsbc";       fields(i)%grid="grid_scalar"
 #ifdef key_si3
 !ICE/icedia.F90
@@ -648,5 +763,5 @@
         i = iom_add(i); fields(i)%vname="stress2_i";     fields(i)%grid="grid_N"
         i = iom_add(i); fields(i)%vname="stress12_i";    fields(i)%grid="grid_N"
-        i = iom_add(i); fields(i)%vname="nn_fsbc";       fields(i)%grid="grid_scalar"
+        i = iom_add(i); fields(i)%vname="nn_fsbc_ice";   fields(i)%grid="grid_scalar"
         i = iom_add(i); fields(i)%vname="kt_ice";        fields(i)%grid="grid_scalar"
         i = iom_add(i); fields(i)%vname="v_i";           fields(i)%grid="grid_Ni_3D"
@@ -685,5 +800,5 @@
 
 
-   SUBROUTINE iom_set_rstw_active(drst_wfields, cdrst_file)
+   SUBROUTINE iom_set_rstw_active(drst_wfields, cdrst_file, kdstep)
       !!---------------------------------------------------------------------
       !!                   ***  SUBROUTINE iom_set_rstw_active   ***
@@ -695,45 +810,53 @@
    CHARACTER(len=*), INTENT(IN) :: cdrst_file
    TYPE(RST_FIELD),  INTENT(IN) :: drst_wfields(max_rst_fields)
+   INTEGER        ,  INTENT(IN) :: kdstep
 #if defined key_iomput
-   TYPE(xios_field) :: field_hdl
-   TYPE(xios_file) :: file_hdl
+   TYPE(xios_field)     :: field_hdl
+   TYPE(xios_file)      :: file_hdl
    TYPE(xios_filegroup) :: filegroup_hdl
-   INTEGER :: i
-   CHARACTER(lc)  ::   clpath
-
+   TYPE(XIOS_DURATION)  :: freq
+   INTEGER              :: i
+   CHARACTER(lc)        :: clpath
+
+   write(snr, *) kdstep
 !set name of the restart file and enable available fields
         IF(lwp) WRITE(numout,*) 'Setting restart filename (for XIOS write) to: ',cdrst_file
         CALL xios_get_handle("file_definition", filegroup_hdl )
-        CALL xios_add_child(filegroup_hdl, file_hdl, 'wrestart')
+        CALL xios_add_child(filegroup_hdl, file_hdl)
         IF(nxioso.eq.1) THEN 
-           CALL xios_set_file_attr( "wrestart", type="one_file", enabled=.TRUE.,& 
+           CALL xios_set_attr( file_hdl, type="one_file", enabled=.TRUE.,& 
+                                    mode="write", output_freq=kdstep*xios_timestep) 
+           if(lwp) write(numout,*) 'Define ', trim(cdrst_file), ' in one_file mode' 
+        ELSE  
+           CALL xios_set_attr( file_hdl, type="multiple_file", enabled=.TRUE.,& 
                                     mode="write", output_freq=xios_timestep) 
-           if(lwp) write(numout,*) 'OPEN ', trim(cdrst_file), ' in one_file mode' 
-        ELSE  
-           CALL xios_set_file_attr( "wrestart", type="multiple_file", enabled=.TRUE.,& 
-                                    mode="write", output_freq=xios_timestep) 
-           if(lwp) write(numout,*) 'OPEN ', trim(cdrst_file), ' in multiple_file mode' 
+           if(lwp) write(numout,*) 'Define ', trim(cdrst_file), ' in multiple_file mode' 
         ENDIF 
-        CALL xios_set_file_attr( "wrestart", name=trim(cdrst_file))
+        CALL xios_set_attr( file_hdl, name=trim(cdrst_file))
 !define fields for restart context
         DO i = 1, max_rst_fields
          IF( drst_wfields(i)%active ) THEN
-                CALL xios_add_child(file_hdl, field_hdl, TRIM(drst_wfields(i)%vname))
+                CALL xios_add_child(file_hdl, field_hdl, TRIM(drst_wfields(i)%vname)//"_"//TRIM(ADJUSTL(snr)))
                 SELECT CASE (TRIM(drst_wfields(i)%grid))
                  CASE ("grid_Ni_3D")
                     CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(drst_wfields(i)%vname), &
-                        domain_ref="grid_N", axis_ref="numcat", prec = 8, operation = "instant")
+                        domain_ref="grid_N", axis_ref="numcat", prec = 8, operation = "once",            &
+                        freq_offset=(kdstep -1) * xios_timestep)
                  CASE ("grid_N_3D")
                     CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(drst_wfields(i)%vname), &
-                        domain_ref="grid_N", axis_ref="nav_lev", prec = 8, operation = "instant")
+                        domain_ref="grid_N", axis_ref="nav_lev", prec = 8, operation = "once",           &
+                        freq_offset=(kdstep -1) * xios_timestep)
                  CASE ("grid_N")
                     CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(drst_wfields(i)%vname), &
-                        domain_ref="grid_N", prec = 8, operation = "instant") 
+                        domain_ref="grid_N", prec = 8, operation = "once",                               &
+                        freq_offset=(kdstep -1) * xios_timestep)
                  CASE ("grid_vector")
                     CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(drst_wfields(i)%vname), &
-                         axis_ref="nav_lev", prec = 8, operation = "instant")
+                         axis_ref="nav_lev", prec = 8, operation = "once",                               &
+                        freq_offset=(kdstep -1) * xios_timestep)
                  CASE ("grid_scalar")
                     CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(drst_wfields(i)%vname), &
-                        scalar_ref = "grid_scalar", prec = 8, operation = "instant")
+                        scalar_ref = "grid_scalar", prec = 8, operation = "once",                        &
+                        freq_offset=(kdstep -1) * xios_timestep)
                 END SELECT
          ENDIF
@@ -1723,5 +1846,6 @@
       IF( kt == kwrite ) THEN
           IF(lwp) write(numout,*) 'RESTART: write (XIOS 0D) ',trim(cdvar)
-          CALL xios_send_field(trim(cdvar), pvar)
+          write(snr, *) nitrst
+          CALL xios_send_field(trim(cdvar)//"_"//TRIM(ADJUSTL(snr)), pvar)
       ENDIF
 #endif
@@ -1753,5 +1877,6 @@
       IF( kt == kwrite ) THEN
          IF(lwp) write(numout,*) 'RESTART: write (XIOS 1D) ',trim(cdvar)
-         CALL xios_send_field(trim(cdvar), pvar)
+         write(snr, *) nitrst
+         CALL xios_send_field(trim(cdvar)//"_"//TRIM(ADJUSTL(snr)), pvar)
       ENDIF
 #endif
@@ -1783,5 +1908,6 @@
       IF( kt == kwrite ) THEN
          IF(lwp) write(numout,*) 'RESTART: write (XIOS 2D) ',trim(cdvar)
-         CALL xios_send_field(trim(cdvar), pvar)
+         write(snr, *) nitrst
+         CALL xios_send_field(trim(cdvar)//"_"//TRIM(ADJUSTL(snr)), pvar)
       ENDIF
 #endif
@@ -1813,5 +1939,6 @@
       IF( kt == kwrite ) THEN
          IF(lwp) write(numout,*) 'RESTART: write (XIOS 3D) ',trim(cdvar)
-         CALL xios_send_field(trim(cdvar), pvar)
+         write(snr, *) nitrst
+         CALL xios_send_field(trim(cdvar)//"_"//TRIM(ADJUSTL(snr)), pvar)
       ENDIF
 #endif
@@ -1842,4 +1969,6 @@
       !!---------------------------------------------------------------------
       !
+      !return for now. When XIOS is used to write restart it also should be used to write this attribute
+      IF(lwxios) return
       !                                      ===================================
       IF( TRIM(cdaction) == 'READ' ) THEN   ! read restart related to mpp_delay !
Index: NEMO/branches/2019/dev_r11756_SI3restart_XIOS/src/OCE/IOM/restart.F90
===================================================================
--- NEMO/branches/2019/dev_r11756_SI3restart_XIOS/src/OCE/IOM/restart.F90	(revision 11840)
+++ NEMO/branches/2019/dev_r11756_SI3restart_XIOS/src/OCE/IOM/restart.F90	(revision 11870)
@@ -108,21 +108,8 @@
             ENDIF
             !
-            IF(.NOT.lwxios) THEN
+            IF(lwxios) THEN
+               CALL iom_setkt( nitrst, cwxios_context )
+            ELSE
                CALL iom_open( TRIM(clpath)//TRIM(clname), numrow, ldwrt = .TRUE. )
-            ELSE
-#if defined key_iomput
-               cwxios_context = "rstw_"//TRIM(ADJUSTL(clkt))
-               IF( TRIM(Agrif_CFixed()) == '0' ) THEN
-                  clpname = clname
-               ELSE
-                  clpname = TRIM(Agrif_CFixed())//"_"//clname   
-               ENDIF
-               CALL iom_init( cwxios_context, TRIM(clpath)//TRIM(clpname), .false. )
-               CALL xios_update_calendar(nitrst)
-               CALL iom_swap(      cxios_context          )
-#else
-               clinfo = 'Can not use XIOS in rst_opn'
-               CALL ctl_stop(TRIM(clinfo))
-#endif
             ENDIF
             lrst_oce = .TRUE.
@@ -179,6 +166,4 @@
          IF(.NOT.lwxios) THEN
             CALL iom_close( numrow )     ! close the restart file (only at last time step)
-         ELSE
-            CALL iom_context_finalize(      cwxios_context          )
          ENDIF
 !!gm         IF( .NOT. lk_trdmld )   lrst_oce = .FALSE.
