Index: /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DIA/diahsb.F90
===================================================================
--- /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DIA/diahsb.F90	(revision 8643)
+++ /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DIA/diahsb.F90	(revision 8644)
@@ -369,4 +369,23 @@
       IF( .NOT. ln_diahsb )   RETURN
 
+      IF(lwxios) THEN
+! define variables in restart file when writing with XIOS
+        CALL set_rstw_var_active('frc_v')
+        CALL set_rstw_var_active('frc_t')
+        CALL set_rstw_var_active('frc_s')
+        IF( ln_linssh ) THEN
+           CALL set_rstw_var_active('frc_wn_t')
+           CALL set_rstw_var_active('frc_wn_s')
+        ENDIF
+        CALL set_rstw_var_active('surf_ini')
+        CALL set_rstw_var_active('ssh_ini')
+        CALL set_rstw_var_active('e3t_ini')
+        CALL set_rstw_var_active('hc_loc_ini')
+        CALL set_rstw_var_active('sc_loc_ini')
+        IF( ln_linssh ) THEN
+           CALL set_rstw_var_active('ssh_hc_loc_ini')
+           CALL set_rstw_var_active('ssh_sc_loc_ini')
+        ENDIF
+      ENDIF
       ! ------------------- !
       ! 1 - Allocate memory !
Index: /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/daymod.F90
===================================================================
--- /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/daymod.F90	(revision 8643)
+++ /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/daymod.F90	(revision 8644)
@@ -144,4 +144,12 @@
       CALL day( nit000 )
       !
+      IF( lwxios ) THEN
+! define variables in restart file when writing with XIOS
+          CALL set_rstw_var_active('kt')
+          CALL set_rstw_var_active('ndastp')
+          CALL set_rstw_var_active('adatrj')
+          CALL set_rstw_var_active('ntime')
+      ENDIF
+
    END SUBROUTINE day_init
 
@@ -341,28 +349,28 @@
                ndastp = NINT( zndastp )
                CALL iom_get( numror, 'adatrj', adatrj  )
-	       CALL iom_get( numror, 'ntime', ktime )
-	       nn_time0=INT(ktime)
+               CALL iom_get( numror, 'ntime', ktime )
+               nn_time0=INT(ktime)
                ! calculate start time in hours and minutes
-	       zdayfrac=adatrj-INT(adatrj)
-	       ksecs = NINT(zdayfrac*86400)	       ! Nearest second to catch rounding errors in adatrj	       
-	       ihour = INT(ksecs/3600)
-	       iminute = ksecs/60-ihour*60
- 	        
+               zdayfrac=adatrj-INT(adatrj)
+               ksecs = NINT(zdayfrac*86400)       ! Nearest second to catch rounding errors in adatrj	       
+               ihour = INT(ksecs/3600)
+               iminute = ksecs/60-ihour*60
+                 
                ! Add to nn_time0
                nhour   =   nn_time0 / 100
                nminute = ( nn_time0 - nhour * 100 )
-	       nminute=nminute+iminute
-	       
-	       IF( nminute >= 60 ) THEN
-	          nminute=nminute-60
-		  nhour=nhour+1
-	       ENDIF
-	       nhour=nhour+ihour
-	       IF( nhour >= 24 ) THEN
-		  nhour=nhour-24
-	          adatrj=adatrj+1
-	       ENDIF	       
-	       nn_time0 = nhour * 100 + nminute
-	       adatrj = INT(adatrj)                    ! adatrj set to integer as nn_time0 updated	       
+               nminute=nminute+iminute
+               
+               IF( nminute >= 60 ) THEN
+                  nminute=nminute-60
+                  nhour=nhour+1
+               ENDIF
+               nhour=nhour+ihour
+               IF( nhour >= 24 ) THEN
+                  nhour=nhour-24
+                  adatrj=adatrj+1
+               ENDIF      
+               nn_time0 = nhour * 100 + nminute
+               adatrj = INT(adatrj)                    ! adatrj set to integer as nn_time0 updated	       
             ELSE
                ! parameters corresponding to nit000 - 1 (as we start the step loop with a call to day)
@@ -378,5 +386,5 @@
             ndastp = ndate0           ! ndate0 read in the namelist in dom_nam
             nhour   =   nn_time0 / 100
-	    nminute = ( nn_time0 - nhour * 100 )
+            nminute = ( nn_time0 - nhour * 100 )
             IF( nhour*3600+nminute*60-ndt05 .lt. 0 )  ndastp=ndastp-1      ! Start hour is specified in the namelist (default 0)
             adatrj = ( REAL( nit000-1, wp ) * rdt ) / rday
@@ -388,5 +396,5 @@
             WRITE(numout,*) '   date ndastp                                      : ', ndastp
             WRITE(numout,*) '   number of elapsed days since the begining of run : ', adatrj
-	    WRITE(numout,*) '   nn_time0                                         : ',nn_time0
+            WRITE(numout,*) '   nn_time0                                         : ',nn_time0
             WRITE(numout,*)
          ENDIF
@@ -405,5 +413,5 @@
          CALL iom_rstput( kt, nitrst, numrow, 'adatrj' , adatrj, lxios = lwxios            )   ! number of elapsed days since
          !                                                                     ! the begining of the run [s]
-	 CALL iom_rstput( kt, nitrst, numrow, 'ntime'  , REAL( nn_time0, wp), lxios = lwxios ) ! time
+         CALL iom_rstput( kt, nitrst, numrow, 'ntime'  , REAL( nn_time0, wp), lxios = lwxios ) ! time
          IF( lwxios ) CALL iom_swap(      cxios_context          )
       ENDIF
Index: /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90
===================================================================
--- /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90	(revision 8643)
+++ /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90	(revision 8644)
@@ -47,5 +47,6 @@
    USE wrk_nemo       ! Memory Allocation
    USE timing         ! Timing
-   USE iom_def, ONLY:lwxios, wxioso ! write restart flag and output type
+   USE iom_def, ONLY:lwxios, wxioso, rst_wfields ! write restart flag and output type
+   USE iom, ONLY : set_rst_vars, set_rstw_core
    IMPLICIT NONE
    PRIVATE
@@ -201,4 +202,9 @@
       IF( nn_timing == 1 )   CALL timing_stop('dom_init')
       !
+      IF( lwxios ) THEN
+!define names for restart write
+         CALL set_rst_vars(rst_wfields)
+         CALL set_rstw_core()
+      ENDIF
    END SUBROUTINE dom_init
 
@@ -334,5 +340,4 @@
          WRITE(numout,*) '      NetCDF chunksize (bytes)        nn_chunksz = ', nn_chunksz
          WRITE(numout,*) '      IS coupling at the restart step ln_iscpl   = ', ln_iscpl
-         WRITE(numout,*) '      Write restart using XIOS        nn_wxios   = ', nn_wxios
          IF( TRIM(Agrif_CFixed()) == '0' ) &
      &      WRITE(numout,*) '      Write restart using XIOS        nn_wxios   = ', nn_wxios
Index: /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/domvvl.F90
===================================================================
--- /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/domvvl.F90	(revision 8643)
+++ /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DOM/domvvl.F90	(revision 8644)
@@ -242,4 +242,22 @@
       ENDIF
       !
+      IF(lwxios) THEN
+! define variables in restart file when writing with XIOS
+         CALL set_rstw_var_active('e3t_b')
+         CALL set_rstw_var_active('e3t_n')
+         !                                           ! ----------------------- !
+         IF( ln_vvl_ztilde .OR. ln_vvl_layer ) THEN  ! z_tilde and layer cases !
+            !                                        ! ----------------------- !
+            CALL set_rstw_var_active('tilde_e3t_b')
+            CALL set_rstw_var_active('tilde_e3t_n')
+         END IF
+         !                                           ! -------------!    
+         IF( ln_vvl_ztilde ) THEN                    ! z_tilde case !
+            !                                        ! ------------ !
+            CALL set_rstw_var_active('hdiv_lf')
+         ENDIF
+         !
+      ENDIF
+
       IF( nn_timing == 1 )  CALL timing_stop('dom_vvl_init')
       !
Index: /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90
===================================================================
--- /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90	(revision 8643)
+++ /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90	(revision 8644)
@@ -138,17 +138,17 @@
       INTEGER, INTENT(in)  ::   kt   ! ocean time-step index
       !
-      LOGICAL  ::   ll_fw_start			! if true, forward integration 
-      LOGICAL  ::   ll_init			    ! if true, special startup of 2d equations
+      LOGICAL  ::   ll_fw_start                 ! if true, forward integration 
+      LOGICAL  ::   ll_init                     ! if true, special startup of 2d equations
       LOGICAL  ::   ll_tmp1, ll_tmp2            ! local logical variables used in W/D
-      INTEGER  ::   ji, jj, jk, jn   		! dummy loop indices
-      INTEGER  ::   ikbu, ikbv, noffset    	! local integers
-      INTEGER  ::   iktu, iktv             	! local integers
+      INTEGER  ::   ji, jj, jk, jn              ! dummy loop indices
+      INTEGER  ::   ikbu, ikbv, noffset         ! local integers
+      INTEGER  ::   iktu, iktv                  ! local integers
       REAL(wp) ::   zmdi
-      REAL(wp) ::   zraur, z1_2dt_b, z2dt_bf  	! local scalars
+      REAL(wp) ::   zraur, z1_2dt_b, z2dt_bf    ! local scalars
       REAL(wp) ::   zx1, zy1, zx2, zy2          !   -      -
-      REAL(wp) ::   z1_12, z1_8, z1_4, z1_2 	!   -      -
+      REAL(wp) ::   z1_12, z1_8, z1_4, z1_2     !   -      -
       REAL(wp) ::   zu_spg, zv_spg              !   -      -
-      REAL(wp) ::   zhura, zhvra         	!   -      -
-      REAL(wp) ::   za0, za1, za2, za3		!   -      -
+      REAL(wp) ::   zhura, zhvra                !   -      -
+      REAL(wp) ::   za0, za1, za2, za3          !   -      -
       !
       REAL(wp), POINTER, DIMENSION(:,:) :: zsshp2_e
@@ -192,5 +192,5 @@
       ENDIF
       !
-      IF( kt == nit000 ) THEN             	!* initialisation
+      IF( kt == nit000 ) THEN                       !* initialisation
          !
          IF(lwp) WRITE(numout,*)
@@ -1325,4 +1325,26 @@
       CALL wrk_dealloc( jpi,jpj,   zcu )
       !
+      IF( lwxios ) THEN
+! define variables in restart file when writing with XIOS
+         CALL set_rstw_var_active('ub2_b')
+         CALL set_rstw_var_active('vb2_b')
+         !
+         IF (.NOT.ln_bt_av) THEN
+            CALL set_rstw_var_active('sshbb_e')
+            CALL set_rstw_var_active('ubb_e')
+            CALL set_rstw_var_active('vbb_e')
+            CALL set_rstw_var_active('sshb_e')
+            CALL set_rstw_var_active('ub_e')
+            CALL set_rstw_var_active('vb_e')
+         ENDIF
+#if defined key_agrif
+         ! Save time integrated fluxes
+         IF ( .NOT.Agrif_Root() ) THEN
+            CALL set_rstw_var_active('ub2_i_b')
+            CALL set_rstw_var_active('vb2_i_b')
+         ENDIF
+#endif
+      ENDIF
+
    END SUBROUTINE dyn_spg_ts_init
 
Index: /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90
===================================================================
--- /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90	(revision 8643)
+++ /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90	(revision 8644)
@@ -41,16 +41,14 @@
    USE dianam          ! build name of file
    USE xios
-   USE iom_def, ONLY : wxioso
+   USE iom_def, ONLY : max_rst_fields, rst_wfields, RST_FIELD
 # endif
    USE ioipsl, ONLY :  ju2ymds    ! for calendar
    USE crs             ! Grid coarsening
-   USE sbc_oce, ONLY : lk_oasis, ln_coupled_iceshelf_fluxes, ln_apr_dyn, ln_rnf, nn_components, jp_iam_sas
-   USE diadct, ONLY : lk_diadct
+   USE lib_fortran 
+   USE diurnal_bulk, ONLY : ln_diurnal_only
 
    IMPLICIT NONE
-   !  values needed to set correctlyfiles in reast file when using XIOS for writing
-   LOGICAL, PUBLIC :: lr_vvl_ztilde, lr_vvl_layer, lr_traadv_cen2
-
    PUBLIC   !   must be public to be able to access iom_def through iom
+   
 #if defined key_iomput
    LOGICAL, PUBLIC, PARAMETER ::   lk_iomput = .TRUE.        !: iom_put flag
@@ -61,4 +59,5 @@
    PUBLIC iom_getatt, iom_putatt, iom_gettime, iom_rstput, iom_put
    PUBLIC iom_use, iom_context_finalize
+
    PRIVATE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
    PRIVATE iom_g0d, iom_g1d, iom_g2d, iom_g3d, iom_get_123d
@@ -67,5 +66,6 @@
    PRIVATE iom_set_domain_attr, iom_set_axis_attr, iom_set_field_attr, iom_set_file_attr, iom_get_file_attr, iom_set_grid_attr
    PRIVATE set_grid, set_grid_bounds, set_scalar, set_xmlatt, set_mooring, iom_update_file_name, iom_sdate
-   PRIVATE set_rstw_active
+   PRIVATE set_rst_context, set_rstw_active
+   PUBLIC set_rstw_var_active, set_rst_vars
 # endif
 
@@ -94,5 +94,5 @@
 CONTAINS
 
-   SUBROUTINE iom_init( cdname ) 
+   SUBROUTINE iom_init( cdname, fname ) 
       !!----------------------------------------------------------------------
       !!                     ***  ROUTINE   ***
@@ -102,10 +102,12 @@
       !!----------------------------------------------------------------------
       CHARACTER(len=*), INTENT(in)  :: cdname
+      CHARACTER(len=*), OPTIONAL, INTENT(in)  :: fname
 #if defined key_iomput
 
       TYPE(xios_duration) :: dtime    = xios_duration(0, 0, 0, 0, 0, 0)
       TYPE(xios_date)     :: start_date
-      CHARACTER(len=10) :: clname
+      CHARACTER(len=lc) :: clname
       INTEGER           :: ji, jkmin
+      LOGICAL :: lrst_context              ! is context related to restart
       !
       REAL(wp), ALLOCATABLE, DIMENSION(:,:) :: zt_bnds, zw_bnds
@@ -118,5 +120,5 @@
       CALL xios_context_initialize(TRIM(clname), mpi_comm_opa)
       CALL iom_swap( cdname )
-
+      lrst_context =  (TRIM(cdname) == TRIM(wxios_context))
 
       ! Calendar type is now defined in xml file 
@@ -129,12 +131,13 @@
           &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
       END SELECT
+
       ! horizontal grid definition
-      CALL set_scalar
-
-      IF( TRIM(cdname) == TRIM(cxios_context)) THEN  
-         CALL set_grid( "T", glamt, gphit, .TRUE. ) 
-         CALL set_grid( "U", glamu, gphiu, .TRUE. )
-         CALL set_grid( "V", glamv, gphiv, .TRUE. )
-         CALL set_grid( "W", glamt, gphit, .TRUE. )
+      IF(.NOT.lrst_context) CALL set_scalar
+
+      IF( TRIM(cdname) == TRIM(cxios_context) ) THEN  
+         CALL set_grid( "T", glamt, gphit, .FALSE. ) 
+         CALL set_grid( "U", glamu, gphiu, .FALSE. )
+         CALL set_grid( "V", glamv, gphiv, .FALSE. )
+         CALL set_grid( "W", glamt, gphit, .FALSE. )
          CALL set_grid_znl( gphit )
          !
@@ -154,13 +157,13 @@
          CALL dom_grid_crs   ! Save the parent grid information  & Switch to coarse grid domain
          !
-         CALL set_grid( "T", glamt_crs, gphit_crs, ln_mskland ) 
-         CALL set_grid( "U", glamu_crs, gphiu_crs, ln_mskland ) 
-         CALL set_grid( "V", glamv_crs, gphiv_crs, ln_mskland ) 
-         CALL set_grid( "W", glamt_crs, gphit_crs, ln_mskland ) 
+         CALL set_grid( "T", glamt_crs, gphit_crs, .FALSE. ) 
+         CALL set_grid( "U", glamu_crs, gphiu_crs, .FALSE. ) 
+         CALL set_grid( "V", glamv_crs, gphiv_crs, .FALSE. ) 
+         CALL set_grid( "W", glamt_crs, gphit_crs, .FALSE. ) 
          CALL set_grid_znl( gphit_crs )
           !
          CALL dom_grid_glo   ! Return to parent grid domain
          !
-         IF( ln_cfmeta) THEN   ! Add additional grid metadata
+         IF( ln_cfmeta .AND. TRIM(cdname) .NE. TRIM(wxios_context)) THEN   ! Add additional grid metadata
             CALL iom_set_domain_attr("grid_T", area = e1e2t_crs(nldi:nlei, nldj:nlej))
             CALL iom_set_domain_attr("grid_U", area = e1u_crs(nldi:nlei, nldj:nlej) * e2u_crs(nldi:nlei, nldj:nlej))
@@ -175,34 +178,45 @@
 
       ! vertical grid definition
-      CALL iom_set_axis_attr( "deptht", paxis = gdept_1d )
-      CALL iom_set_axis_attr( "depthu", paxis = gdept_1d )
-      CALL iom_set_axis_attr( "depthv", paxis = gdept_1d )
-      CALL iom_set_axis_attr( "depthw", paxis = gdepw_1d )
-
-      ! Add vertical grid bounds
-      jkmin = MIN(2,jpk)  ! in case jpk=1 (i.e. sas2D)
-      zt_bnds(2,:        ) = gdept_1d(:)
-      zt_bnds(1,jkmin:jpk) = gdept_1d(1:jpkm1)
-      zt_bnds(1,1        ) = gdept_1d(1) - e3w_1d(1)
-      zw_bnds(1,:        ) = gdepw_1d(:)
-      zw_bnds(2,1:jpkm1  ) = gdepw_1d(jkmin:jpk)
-      zw_bnds(2,jpk:     ) = gdepw_1d(jpk) + e3t_1d(jpk)
-      CALL iom_set_axis_attr( "deptht", bounds=zt_bnds )
-      CALL iom_set_axis_attr( "depthu", bounds=zt_bnds )
-      CALL iom_set_axis_attr( "depthv", bounds=zt_bnds )
-      CALL iom_set_axis_attr( "depthw", bounds=zw_bnds )
+      IF(.NOT.lrst_context) THEN
+          CALL iom_set_axis_attr( "deptht", paxis = gdept_1d )
+          CALL iom_set_axis_attr( "depthu", paxis = gdept_1d )
+          CALL iom_set_axis_attr( "depthv", paxis = gdept_1d )
+          CALL iom_set_axis_attr( "depthw", paxis = gdepw_1d )
+
+          ! Add vertical grid bounds
+          jkmin = MIN(2,jpk)  ! in case jpk=1 (i.e. sas2D)
+          zt_bnds(2,:        ) = gdept_1d(:)
+          zt_bnds(1,jkmin:jpk) = gdept_1d(1:jpkm1)
+          zt_bnds(1,1        ) = gdept_1d(1) - e3w_1d(1)
+          zw_bnds(1,:        ) = gdepw_1d(:)
+          zw_bnds(2,1:jpkm1  ) = gdepw_1d(jkmin:jpk)
+          zw_bnds(2,jpk:     ) = gdepw_1d(jpk) + e3t_1d(jpk)
+          CALL iom_set_axis_attr( "deptht", bounds=zt_bnds )
+          CALL iom_set_axis_attr( "depthu", bounds=zt_bnds )
+          CALL iom_set_axis_attr( "depthv", bounds=zt_bnds )
+          CALL iom_set_axis_attr( "depthw", bounds=zw_bnds )
 
 
 # if defined key_floats
-      CALL iom_set_axis_attr( "nfloat", (/ (REAL(ji,wp), ji=1,nfloat) /) )
+          CALL iom_set_axis_attr( "nfloat", (/ (REAL(ji,wp), ji=1,nfloat) /) )
 # endif
 #if defined key_lim3 || defined key_lim2
-      CALL iom_set_axis_attr( "ncatice", (/ (REAL(ji,wp), ji=1,jpl) /) )
+          CALL iom_set_axis_attr( "ncatice", (/ (REAL(ji,wp), ji=1,jpl) /) )
 #endif
-      CALL iom_set_axis_attr( "icbcla", class_num )
-      CALL iom_set_axis_attr( "iax_20C", (/ REAL(20,wp) /) )
-      CALL iom_set_axis_attr( "iax_28C", (/ REAL(28,wp) /) )
-      
-      CALL set_xmlatt
+          CALL iom_set_axis_attr( "icbcla", class_num )
+          CALL iom_set_axis_attr( "iax_20C", (/ REAL(20,wp) /) )
+          CALL iom_set_axis_attr( "iax_28C", (/ REAL(28,wp) /) )
+      ENDIF   
+      ! automatic definitions of some of the xml attributs
+      if(lwp) write(numout,*) TRIM(cdname),' ',TRIM(wxios_context),' TEST1'
+      IF( TRIM(cdname) == TRIM(wxios_context) ) THEN
+!set names of the fields in restart file IF using XIOS to read/write data
+       CALL set_rst_context()
+       CALL set_rst_vars(rst_wfields)
+!set which fields are to be read from restart file
+       CALL set_rstw_active(fname)
+      ELSE
+       CALL set_xmlatt
+      ENDIF
 
       ! end file definition
@@ -219,507 +233,235 @@
    END SUBROUTINE iom_init
 
-   SUBROUTINE iom_rstw_init( cdname, filename ) 
-      !!----------------------------------------------------------------------
-      !!                     ***  ROUTINE   ***
-      !!
-      !! ** Purpose :   
-      !!
-      !!----------------------------------------------------------------------
-      CHARACTER(len=*), INTENT(in)  :: cdname
-#if defined key_iomput
-#if defined key_xios2
-      TYPE(xios_duration) :: dtime    = xios_duration(0, 0, 0, 0, 0, 0)
-      TYPE(xios_date)     :: start_date
-      TYPE(xios_domaingroup)            :: domaingroup_hdl
-      TYPE(xios_domain)                 :: domain_hdl
-      TYPE(xios_axisgroup)              :: axisgroup_hdl
-      TYPE(xios_axis)                   :: axis_hdl
-      TYPE(xios_scalar)                 :: scalar_hdl
-      TYPE(xios_scalargroup)            :: scalargroup_hdl
-#endif
-      CHARACTER(len=128)   :: clname
-      INTEGER             :: ji
-      !
-      CHARACTER(len=*), OPTIONAL, INTENT(in)  :: filename 
-      !!----------------------------------------------------------------------
-
-      clname = cdname 
-      IF( TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(cdname)
-      CALL xios_context_initialize(TRIM(clname), mpi_comm_opa)
-      CALL iom_swap( cdname )
-      ! calendar parameters
-#if defined key_xios2
-      ! Calendar type is now defined in xml file 
-      SELECT CASE ( nleapy )        ! Choose calendar for IOIPSL
-      CASE ( 1)   ; CALL xios_define_calendar( TYPE = "Gregorian", time_origin = xios_date(1900,01,01,00,00,00), &
-          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
-      CASE ( 0)   ; CALL xios_define_calendar( TYPE = "NoLeap"   , time_origin = xios_date(1900,01,01,00,00,00), &
-          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
-      CASE (30)   ; CALL xios_define_calendar( TYPE = "D360"     , time_origin = xios_date(1900,01,01,00,00,00), &
-          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
-      END SELECT
-
-#endif
-      CALL xios_get_handle("domain_definition",domaingroup_hdl)
-      CALL xios_add_child(domaingroup_hdl, domain_hdl, "grid_N")
-      CALL set_grid("N", glamt, gphit, .FALSE.)
- 
-      CALL xios_get_handle("axis_definition",axisgroup_hdl)
-      CALL xios_add_child(axisgroup_hdl, axis_hdl, "deptht")
-!if unit="m" is present AGRIF configuration fails to compile
-      CALL xios_set_axis_attr( "deptht", long_name="Vertical levels in meters", positive="down")
-      ! vertical grid definition
-      CALL iom_set_axis_attr( "deptht", paxis = gdept_1d )
-
-      CALL xios_get_handle("scalar_definition",scalargroup_hdl)
-      CALL xios_add_child(scalargroup_hdl, scalar_hdl, "grid_scalar")
-
-      ! automatic definitions of some of the xml attributs
-      IF( TRIM(cdname) == TRIM(wxios_context)) THEN
-!set which fields are to be read from restart file
-       CALL set_rstw_active(filename)
-      ENDIF
-
-      ! end file definition
-      dtime%second = rdt
-      CALL xios_set_timestep(dtime)
-      CALL xios_close_context_definition()
-#endif
-
-   END SUBROUTINE iom_rstw_init
+   SUBROUTINE set_rstw_var_active(field)
+   CHARACTER(len = *), INTENT(IN) :: field
+   INTEGER :: i
+   LOGICAL :: is_set
+
+   is_set = .FALSE.
+
+   DO i = 1, max_rst_fields
+       IF(TRIM(rst_wfields(i)%vname) == field) THEN 
+          rst_wfields(i)%active = .TRUE.
+          is_set = .TRUE.
+          EXIT
+       ENDIF
+   ENDDO
+!Warn if variable is not in defined in rst_wfields
+   IF(.NOT.is_set) THEN
+      IF(lwp) write(numout,*) 'variable ', field ,' is available for writing but not defined' 
+   ENDIF
+
+   END SUBROUTINE set_rstw_var_active
+  
+   SUBROUTINE set_rstw_core()
+!from restart.F90
+   CALL set_rstw_var_active("rdt")
+   IF ( .NOT. ln_diurnal_only ) THEN
+        CALL set_rstw_var_active('ub'  )
+        CALL set_rstw_var_active('vb'  )
+        CALL set_rstw_var_active('tb'  )
+        CALL set_rstw_var_active('sb'  )
+        CALL set_rstw_var_active('sshb')
+        !
+        CALL set_rstw_var_active('un'  )
+        CALL set_rstw_var_active('vn'  )
+        CALL set_rstw_var_active('tn'  )
+        CALL set_rstw_var_active('sn'  )
+        CALL set_rstw_var_active('sshn')
+        CALL set_rstw_var_active('rhop')
+     ! extra variable needed for the ice sheet coupling
+        IF ( ln_iscpl ) THEN
+             CALL set_rstw_var_active('tmask')
+             CALL set_rstw_var_active('umask')
+             CALL set_rstw_var_active('vmask')
+             CALL set_rstw_var_active('smask')
+             CALL set_rstw_var_active('e3t_n')
+             CALL set_rstw_var_active('e3u_n')
+             CALL set_rstw_var_active('e3v_n')
+             CALL set_rstw_var_active('gdepw_n')
+        END IF
+      ELSE
+        CALL set_rstw_var_active('Dsst')
+      ENDIF
+!from trasbc.F90
+         CALL set_rstw_var_active('sbc_hc_b')
+         CALL set_rstw_var_active('sbc_sc_b')
+   END SUBROUTINE set_rstw_core
+
+   SUBROUTINE set_rst_vars(fields)
+!set names for variables in restart file
+   TYPE(RST_FIELD), INTENT(INOUT) :: fields(max_rst_fields)
+   INTEGER :: i
+        i = 0
+        i = i + 1; fields(i)%vname="rdt";            fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="un";             fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="ub";             fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="vn";             fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="vb";             fields(i)%grid="grid_N_3D"  
+        i = i + 1; fields(i)%vname="tn";             fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="tb";             fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="sn";             fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="sb";             fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="sshn";           fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="sshb";           fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="rhop";           fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="kt";             fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="ndastp";         fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="adatrj";         fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="utau_b";         fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="vtau_b";         fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="qns_b";          fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="emp_b";          fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="sfx_b";          fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="en" ;            fields(i)%grid="grid_N_3D" 
+        i = i + 1; fields(i)%vname="avt";            fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="avm";            fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="avmu";           fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="avmv";           fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="dissl";          fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="sbc_hc_b";       fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="sbc_sc_b";       fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="qsr_hc_b";       fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="fraqsr_1lev";    fields(i)%grid="grid_N"
+        i = i + 1; fields(i)%vname="greenland_icesheet_mass"
+                                               fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="greenland_icesheet_timelapsed"
+                                               fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="greenland_icesheet_mass_roc"
+                                               fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="antarctica_icesheet_mass"
+                                               fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="antarctica_icesheet_timelapsed"
+                                               fields(i)%grid="grid_scalar"
+        i = i + 1; fields(i)%vname="antarctica_icesheet_mass_roc"
+                                               fields(i)%grid="grid_scalar"
+        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="e3t_n";          fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="mxln";           fields(i)%grid="grid_N_3D"
+        i = i + 1; fields(i)%vname="e3t_m";          fields(i)%grid="grid_N_3D"
+   END SUBROUTINE set_rst_vars
+
 
    SUBROUTINE set_rstw_active(rst_file)
 !sets enabled = .TRUE. for each field in restart file
    CHARACTER(len=*) :: rst_file
+   TYPE(xios_field) :: field_hdl
    TYPE(xios_file) :: file_hdl
    TYPE(xios_filegroup) :: filegroup_hdl
-
-!set then name of the restart file (OUTPUT!) and enable available fields
-        if(lwp) WRITE(numout,*) 'Setting (output) restart filename (for XIOS) to: ',TRIM(rst_file)
+   INTEGER :: i
+   CHARACTER(lc)  ::   clpath
+
+!set name of the restart file and enable available fields
+        if(lwp) WRITE(numout,*) 'Setting restart filename (for XIOS write) to: ',rst_file
         CALL xios_get_handle("file_definition", filegroup_hdl )
         CALL xios_add_child(filegroup_hdl, file_hdl, 'wrestart')
-        IF(wxioso.eq.1) THEN
-            CALL xios_set_file_attr( "wrestart", type="one_file", name = TRIM(rst_file), &
-               enabled=.TRUE., mode="write", output_freq=xios_timestep)
-            if(lwp) write(numout,*) 'OPEN ', trim(rst_file), ' in one_file mode'
-        ELSE 
-            CALL xios_set_file_attr( "wrestart", type="multiple_file", name = TRIM(rst_file),&
-               enabled=.TRUE., mode="write", output_freq=xios_timestep)
-          if(lwp) write(numout,*) 'OPEN ', trim(rst_file), ' in multiple_file mode'
-        ENDIF
-
+        IF(wxioso.eq.1) THEN 
+           CALL xios_set_file_attr( "wrestart", type="one_file", enabled=.TRUE.,& 
+                                    mode="write", output_freq=xios_timestep) 
+           if(lwp) write(numout,*) 'OPEN ', trim(rst_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(rst_file), ' in multiple_file mode' 
+        ENDIF 
         CALL xios_set_file_attr( "wrestart", name=trim(rst_file))
-        CALL set_numro_active(file_hdl)
+!defin files for restart context
+        DO i = 1, max_rst_fields
+         IF( rst_wfields(i)%active ) THEN
+                CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_wfields(i)%vname))
+                SELECT CASE (TRIM(rst_wfields(i)%grid))
+                 CASE ("grid_N_3D")
+                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_wfields(i)%vname), &
+                        domain_ref="grid_N", axis_ref="deptht", prec = 8, operation = "instant")
+                 CASE ("grid_N")
+                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_wfields(i)%vname), &
+                        domain_ref="grid_N", prec = 8, operation = "instant") 
+                 CASE ("grid_vector")
+                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_wfields(i)%vname), &
+                         axis_ref="deptht", prec = 8, operation = "instant")
+                 CASE ("grid_scalar")
+                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_wfields(i)%vname), &
+                        scalar_ref = "grid_scalar", prec = 8, operation = "instant")
+                END SELECT
+         ENDIF
+        END DO
+   END SUBROUTINE set_rstw_active
+
+   SUBROUTINE set_rst_context( ) 
+#if defined key_iomput
+   TYPE(xios_domaingroup)            :: domaingroup_hdl 
+   TYPE(xios_domain)                 :: domain_hdl 
+   TYPE(xios_axisgroup)              :: axisgroup_hdl 
+   TYPE(xios_axis)                   :: axis_hdl 
+   TYPE(xios_scalar)                 :: scalar_hdl 
+   TYPE(xios_scalargroup)            :: scalargroup_hdl 
+
+     CALL xios_get_handle("domain_definition",domaingroup_hdl) 
+     CALL xios_add_child(domaingroup_hdl, domain_hdl, "grid_N") 
+     CALL set_grid("N", glamt, gphit, .TRUE.) 
  
-    END SUBROUTINE set_rstw_active
-
-    SUBROUTINE set_numro_active(file_hdl)
-    TYPE(xios_field) :: field_hdl
-    TYPE(xios_file) :: file_hdl
-
-        CALL xios_add_child(file_hdl, field_hdl, "rdt")
-        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  scalar_ref = "grid_scalar", operation = "instant")
-
-        CALL xios_add_child(file_hdl, field_hdl, "rdttra1")
-        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  scalar_ref = "grid_scalar", operation = "instant")
-
-        CALL xios_add_child(file_hdl, field_hdl, "un")
-        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-        CALL xios_add_child(file_hdl, field_hdl, "ub")
-        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-        CALL xios_add_child(file_hdl, field_hdl, "vn")
-        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-        CALL xios_add_child(file_hdl, field_hdl, "vb")
-        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-        CALL xios_add_child(file_hdl, field_hdl, "tn")
-        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-        CALL xios_add_child(file_hdl, field_hdl, "tb")
-        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-        CALL xios_add_child(file_hdl, field_hdl, "sn")
-        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-        CALL xios_add_child(file_hdl, field_hdl, "sb")
-        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-        CALL xios_add_child(file_hdl, field_hdl, "sshn")
-        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref = "grid_N", operation = "instant")
-
-        CALL xios_add_child(file_hdl, field_hdl, "sshb")
-        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref = "grid_N", operation = "instant")
-
-        CALL xios_add_child(file_hdl, field_hdl, "hdivn")
-        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-        CALL xios_add_child(file_hdl, field_hdl, "hdivb")
-        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-        CALL xios_add_child(file_hdl, field_hdl, "rhop")
-        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-        CALL xios_add_child(file_hdl, field_hdl, "rotn")
-        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-        CALL xios_add_child(file_hdl, field_hdl, "rotb")
-        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-!in daymod.F90
-           CALL xios_add_child(file_hdl, field_hdl, "kt")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     scalar_ref = "grid_scalar", operation = "instant")
-
-           CALL xios_add_child(file_hdl, field_hdl, "ndastp")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     scalar_ref = "grid_scalar", operation = "instant")
-
-           CALL xios_add_child(file_hdl, field_hdl, "adatrj")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     scalar_ref = "grid_scalar", operation = "instant")
-!end daymod.F90
-!sbcmod.F90
-           CALL xios_add_child(file_hdl, field_hdl, "utau_b")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref = "grid_N", operation = "instant")
-
-           CALL xios_add_child(file_hdl, field_hdl, "vtau_b")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref = "grid_N", operation = "instant")
-
-           CALL xios_add_child(file_hdl, field_hdl, "qns_b")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref = "grid_N", operation = "instant")
-
-           CALL xios_add_child(file_hdl, field_hdl, "emp_b")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref = "grid_N", operation = "instant")
-
-           CALL xios_add_child(file_hdl, field_hdl, "sfx_b")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref = "grid_N", operation = "instant")
-!end sbcmod.F90
-!ALL FIELDS ABOUVE ALWAYS
-!zdftke.F90
-#if defined key_zdftke   ||   defined key_esopa
-           CALL xios_add_child(file_hdl, field_hdl, "en")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-           CALL xios_add_child(file_hdl, field_hdl, "avt")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-           CALL xios_add_child(file_hdl, field_hdl, "avm")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-           CALL xios_add_child(file_hdl, field_hdl, "avmu")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-           CALL xios_add_child(file_hdl, field_hdl, "avmv")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-           CALL xios_add_child(file_hdl, field_hdl, "dissl")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+     CALL xios_get_handle("axis_definition",axisgroup_hdl) 
+     CALL xios_add_child(axisgroup_hdl, axis_hdl, "deptht") 
+!AGRIF fails to compile when unit= is in call to xios_set_axis_attr
+!    CALL xios_set_axis_attr( "deptht", long_name="Vertical levels",  unit="m", positive="down") 
+     CALL xios_set_axis_attr( "deptht", long_name="Vertical levels in meters", positive="down")
+     CALL iom_set_axis_attr( "deptht", paxis = gdept_1d ) 
+
+     CALL xios_get_handle("scalar_definition", scalargroup_hdl) 
+     CALL xios_add_child(scalargroup_hdl, scalar_hdl, "grid_scalar") 
 #endif
-!end zdftke.F90
-!traqsr.F90
-       CALL xios_add_child(file_hdl, field_hdl, "qsr_hc_b")
-       CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref="grid_N", axis_ref="deptht" , operation = "instant")        
-
-       CALL xios_add_child(file_hdl, field_hdl, "fraqsr_1lev")
-       CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref = "grid_N", operation = "instant")        
-!END traqsr.F90
-#if defined key_dynspg_flt   ||   defined key_esopa 
-!dynspg_flt.F90
-           CALL xios_add_child(file_hdl, field_hdl, "gcx")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref = "grid_N", operation = "instant")
-
-           CALL xios_add_child(file_hdl, field_hdl, "gcxb")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref = "grid_N", operation = "instant")
-!end dynspg_flt.F90
-#endif
-!trasbc.F90 START
-           CALL xios_add_child(file_hdl, field_hdl, "sbc_hc_b")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref = "grid_N", operation = "instant")
-
-           CALL xios_add_child(file_hdl, field_hdl, "sbc_sc_b")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref = "grid_N", operation = "instant")
-
-           CALL xios_add_child(file_hdl, field_hdl, "fwf_isf_b")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref = "grid_N", operation = "instant")
-
-           CALL xios_add_child(file_hdl, field_hdl, "isf_sc_b")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref = "grid_N", operation = "instant")
-
-           CALL xios_add_child(file_hdl, field_hdl, "isf_hc_b")
-           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref = "grid_N", operation = "instant")
-!trasbc.F90 END
-        IF( lk_oasis) THEN
-        ! ln_coupled_iceshelf_fluxes uninitialised unless lk_oasis=true
-          IF( ln_coupled_iceshelf_fluxes ) THEN
-             CALL xios_add_child(file_hdl, field_hdl, "greenland_icesheet_mass")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  scalar_ref = "grid_scalar", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "greenland_icesheet_timelapsed")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  scalar_ref = "grid_scalar", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "greenland_icesheet_mass_roc")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  scalar_ref = "grid_scalar", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "antarctica_icesheet_mass")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  scalar_ref = "grid_scalar", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "antarctica_icesheet_timelapsed")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  scalar_ref = "grid_scalar", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "antarctica_icesheet_mass_roc")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  scalar_ref = "grid_scalar", operation = "instant")
-          ENDIF
-        ENDIF
-#if defined key_zdfkpp
-        CALL xios_add_child(file_hdl, field_hdl, "rhd")
-        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-             domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-#endif
-!dia_hsb_rst
-#if defined key_diadct
-        IF( lk_diadct     ) THEN
-             CALL xios_add_child(file_hdl, field_hdl, "frc_v")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  scalar_ref = "grid_scalar", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "frc_t")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  scalar_ref = "grid_scalar", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "frc_s")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  scalar_ref = "grid_scalar", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "ssh_ini")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref = "grid_N", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "e3t_ini")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "hc_loc_ini")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "sc_loc_ini")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-
-             IF( .NOT. lk_vvl ) THEN
-                CALL xios_add_child(file_hdl, field_hdl, "frc_wn_t")
-                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     scalar_ref = "grid_scalar", operation = "instant")
-
-                CALL xios_add_child(file_hdl, field_hdl, "frc_wn_s")
-                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     scalar_ref = "grid_scalar", operation = "instant")
-
-                CALL xios_add_child(file_hdl, field_hdl, "ssh_hc_loc_ini")
-                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref = "grid_N", operation = "instant")
-
-                CALL xios_add_child(file_hdl, field_hdl, "ssh_sc_loc_ini")
-                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref = "grid_N", operation = "instant")
-
-
-             ENDIF
-        ENDIF
-#endif
-!end dia_hsb_rst
-!domvvl.F90
-        IF( lk_vvl ) THEN
-             CALL xios_add_child(file_hdl, field_hdl, "fse3t_b")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "fse3t_n")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
-
-           IF( lr_vvl_ztilde .OR. lr_vvl_layer ) THEN  ! z_tilde and layer cases !
-             CALL xios_add_child(file_hdl, field_hdl, "tilde_e3t_b")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref = "grid_N", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "tilde_e3t_n")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref = "grid_N", operation = "instant")
-            END IF
-            IF( lr_vvl_ztilde ) THEN                    ! z_tilde case !
-            CALL xios_add_child(file_hdl, field_hdl, "hdiv_lf")
-            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref = "grid_N", operation = "instant")
-            ENDIF
-         ENDIF
-!end domvvl.F90
-!dynspg_ts.F90
-#if defined key_dynspg_ts   ||   defined key_esopa
-         CALL xios_add_child(file_hdl, field_hdl, "ub2_b")
-         CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-              domain_ref = "grid_N", operation = "instant")
-
-         CALL xios_add_child(file_hdl, field_hdl, "vb2_b")
-         CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-              domain_ref = "grid_N", operation = "instant")
-
-
-         IF (.NOT.ln_bt_av) THEN
-            CALL xios_add_child(file_hdl, field_hdl, "sshbb_e")
-            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                 domain_ref = "grid_N", operation = "instant")
-
-            CALL xios_add_child(file_hdl, field_hdl, "ubb_e")
-            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                 domain_ref = "grid_N", operation = "instant")
-
-            CALL xios_add_child(file_hdl, field_hdl, "vbb_e")
-            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                 domain_ref = "grid_N", operation = "instant")
-
-            CALL xios_add_child(file_hdl, field_hdl, "sshb_e")
-            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                 domain_ref = "grid_N", operation = "instant")
-
-            CALL xios_add_child(file_hdl, field_hdl, "ub_e")
-            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                 domain_ref = "grid_N", operation = "instant")
-
-            CALL xios_add_child(file_hdl, field_hdl, "vb_e")
-            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                 domain_ref = "grid_N", operation = "instant")
-         ENDIF
-#if defined key_agrif
-         ! Save time integrated fluxes
-         IF ( .NOT.Agrif_Root() ) THEN
-               CALL xios_add_child(file_hdl, field_hdl, "ub2_i_b")
-               CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                    domain_ref = "grid_N", operation = "instant")
-
-               CALL xios_add_child(file_hdl, field_hdl, "vb2_i_b")
-               CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                    domain_ref = "grid_N", operation = "instant")
-         ENDIF
-#endif
-#endif
-!end dynspg_ts.F90
-!sbcapr.F90
-          IF( ln_apr_dyn) THEN
-             CALL xios_add_child(file_hdl, field_hdl, "ssh_ibb")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref = "grid_N", operation = "instant")
-          ENDIF
-!end sbcapr.F90
-!sbcrnf.F90
-        IF( ln_rnf      ) THEN
-             CALL xios_add_child(file_hdl, field_hdl, "rnf_b")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref = "grid_N", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "rnf_hc_b")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref = "grid_N", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "rnf_sc_b")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref = "grid_N", operation = "instant")
-        ENDIF 
-!end sbcrnf.F90
-!sbcssm.F90
-        IF( nn_components /= jp_iam_sas .AND. nn_fsbc .NE. 1) THEN
-             CALL xios_add_child(file_hdl, field_hdl, "nn_fsbc")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  scalar_ref = "grid_scalar", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "ssu_m")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref = "grid_N", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "ssv_m")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref = "grid_N", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "sst_m")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref = "grid_N", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "sss_m")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref = "grid_N", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "ssh_m")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                  domain_ref = "grid_N", operation = "instant")
-
-             CALL xios_add_child(file_hdl, field_hdl, "frq_m")
-             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref = "grid_N", operation = "instant")
-
-             IF( lk_vvl )  THEN
-                CALL xios_add_child(file_hdl, field_hdl, "e3t_m")
-                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-                     domain_ref = "grid_N", operation = "instant")
-             ENDIF 
-        ENDIF
-!end sbcssm.F90
-       IF( lr_traadv_cen2   ) THEN
-          CALL xios_add_child(file_hdl, field_hdl, "avmb")
-          CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-               axis_ref = "deptht", operation = "instant")
-
-          CALL xios_add_child(file_hdl, field_hdl, "avtb")
-          CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
-               axis_ref = "deptht", operation = "instant")
-       ENDIF
-
-   END SUBROUTINE set_numro_active
+   END SUBROUTINE set_rst_context
 
    SUBROUTINE iom_swap( cdname )
@@ -1631,4 +1373,5 @@
       LOGICAL :: lx                ! local xios write flag
       INTEGER :: ivid   ! variable id
+
       lx = .FALSE.
       IF(PRESENT(lxios)) lx = lxios
@@ -1645,9 +1388,7 @@
                ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
                SELECT CASE (iom_file(kiomid)%iolib)
-               CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
                CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
-               CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pvar )
                CASE DEFAULT     
-                  CALL ctl_stop( 'iom_rp0d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
+                  CALL ctl_stop( 'iom_rp0d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
                END SELECT
             ENDIF
@@ -1666,4 +1407,5 @@
       LOGICAL :: lx                ! local xios write flag
       INTEGER :: ivid   ! variable id
+
       lx = .FALSE.
       IF(PRESENT(lxios)) lx = lxios
@@ -1680,9 +1422,7 @@
                ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
                SELECT CASE (iom_file(kiomid)%iolib)
-               CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
                CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
-               CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r1d = pvar )
                CASE DEFAULT     
-                  CALL ctl_stop( 'iom_rp1d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
+                  CALL ctl_stop( 'iom_rp1d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
                END SELECT
             ENDIF
@@ -1699,6 +1439,7 @@
       INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
       LOGICAL, OPTIONAL :: lxios   ! xios write flag
-      LOGICAL :: lx                ! local xios write flag
+      LOGICAL :: lx
       INTEGER :: ivid   ! variable id
+
       lx = .FALSE.
       IF(PRESENT(lxios)) lx = lxios
@@ -1715,9 +1456,7 @@
                ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
                SELECT CASE (iom_file(kiomid)%iolib)
-               CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
                CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
-               CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r2d = pvar ) 
                CASE DEFAULT     
-                  CALL ctl_stop( 'iom_rp2d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
+                  CALL ctl_stop( 'iom_rp2d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
                END SELECT
             ENDIF
@@ -1736,4 +1475,5 @@
       LOGICAL :: lx                 ! local xios write flag
       INTEGER :: ivid   ! variable id
+
       lx = .FALSE.
       IF(PRESENT(lxios)) lx = lxios
@@ -1750,9 +1490,7 @@
                ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
                SELECT CASE (iom_file(kiomid)%iolib)
-               CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
                CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
-               CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r3d = pvar )
                CASE DEFAULT     
-                  CALL ctl_stop( 'iom_rp3d: accepted IO library are only jpioipsl and jprstdimg' )
+                  CALL ctl_stop( 'iom_rp3d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
                END SELECT
             ENDIF
@@ -1800,4 +1538,5 @@
    SUBROUTINE iom_p3d( cdname, pfield3d )
       CHARACTER(LEN=*)                , INTENT(in) ::   cdname
+      REAL(wp),       DIMENSION(:,:,:), INTENT(in) ::   pfield3d
 #if defined key_iomput
       CALL xios_send_field(cdname, pfield3d)
@@ -1960,5 +1699,5 @@
       REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zmask
       INTEGER  :: ni,nj
-      LOGICAL :: lxios
+      LOGICAL, INTENT(IN) :: lxios
       
       ni=nlei-nldi+1 ; nj=nlej-nldj+1
Index: /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom_def.F90
===================================================================
--- /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom_def.F90	(revision 8643)
+++ /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom_def.F90	(revision 8644)
@@ -46,5 +46,5 @@
 !$AGRIF_DO_NOT_TREAT
    INTEGER, PUBLIC            ::   iom_open_init = 0   !: used to initialize iom_file(:)%nfid to 0
-!XIOS read restart   
+!XIOS write restart   
    LOGICAL, PUBLIC            ::   lwxios          !: read single file restart using XIOS
    INTEGER, PUBLIC            ::   wxioso          !: type of restart file when writing using XIOS 1 - single, 2 - multiple
@@ -71,5 +71,15 @@
    END TYPE file_descriptor
    TYPE(file_descriptor), DIMENSION(jpmax_files), PUBLIC ::   iom_file !: array containing the info for all opened files
+
+   INTEGER, PARAMETER, PUBLIC                   :: max_rst_fields = 95  
 !$AGRIF_END_DO_NOT_TREAT
+
+   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
+   TYPE(RST_FIELD), PUBLIC :: rst_wfields(max_rst_fields)
+
    !!=====================================================================
 END MODULE iom_def
Index: /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom_nf90.F90
===================================================================
--- /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom_nf90.F90	(revision 8643)
+++ /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom_nf90.F90	(revision 8644)
@@ -126,6 +126,6 @@
             CALL iom_nf90_check(NF90_DEF_DIM( if90id, 'x', kdompar(1,1)  , idmy ), clinfo)
             CALL iom_nf90_check(NF90_DEF_DIM( if90id, 'y', kdompar(2,1)  , idmy ), clinfo)
-            CALL iom_nf90_check(NF90_DEF_DIM( if90id, 'z', jpk           , idmy ), clinfo)
-            CALL iom_nf90_check(NF90_DEF_DIM( if90id, 't', NF90_UNLIMITED, idmy ), clinfo)
+            CALL iom_nf90_check(NF90_DEF_DIM( if90id, 'nav_lev', jpk           , idmy ), clinfo)
+            CALL iom_nf90_check(NF90_DEF_DIM( if90id, 'time_counter', NF90_UNLIMITED, idmy ), clinfo)
             ! global attributes
             CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_number_total'   , jpnij              ), clinfo)
Index: /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/restart.F90
===================================================================
--- /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/restart.F90	(revision 8643)
+++ /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/restart.F90	(revision 8644)
@@ -100,6 +100,4 @@
                IF(.NOT.lwxios) THEN
                   SELECT CASE ( jprstlib )
-                  CASE ( jprstdimg )   ;   WRITE(numout,*)                            &
-                      '             open ocean restart binary file: ',TRIM(clpath)//TRIM(clname)
                   CASE DEFAULT         ;   WRITE(numout,*)                            &
                       '             open ocean restart NetCDF file: ',TRIM(clpath)//TRIM(clname)
@@ -121,5 +119,5 @@
                   dlname = TRIM(Agrif_CFixed())//"_"//clname   
                ENDIF
-               CALL iom_rstw_init( wxios_context, TRIM(clpath)//TRIM(dlname))
+               CALL iom_init( wxios_context, TRIM(clpath)//TRIM(dlname))
                CALL xios_update_calendar(nitrst)
                CALL iom_swap(      cxios_context          )
@@ -146,13 +144,11 @@
                      IF(lwxios) CALL iom_swap(      wxios_context          )
                      CALL iom_rstput( kt, nitrst, numrow, 'rdt'    , rdt       , lxios = lwxios)   ! dynamics time step
-                     CALL iom_rstput( kt, nitrst, numrow, 'rdttra1', rdttra(1) , lxios = lwxios)   ! surface tracer time step
-                     CALL iom_rstput( kt, nitrst, numrow, 'rdt'    , rdt       )   ! dynamics and tracer time step
 
       IF ( .NOT. ln_diurnal_only ) THEN
-                     CALL iom_rstput( kt, nitrst, numrow, 'ub'     , ub, lxios = lwxios)        )     ! before fields
-                     CALL iom_rstput( kt, nitrst, numrow, 'vb'     , vb, lxios = lwxios)        )
-                     CALL iom_rstput( kt, nitrst, numrow, 'tb'     , tsb(:,:,:,jp_tem), lxios = lwxios) )
-                     CALL iom_rstput( kt, nitrst, numrow, 'sb'     , tsb(:,:,:,jp_sal), lxios = lwxios) )
-                     CALL iom_rstput( kt, nitrst, numrow, 'sshb'   , sshb, lxios = lwxios)      )
+                     CALL iom_rstput( kt, nitrst, numrow, 'ub'     , ub, lxios = lwxios        )     ! before fields
+                     CALL iom_rstput( kt, nitrst, numrow, 'vb'     , vb, lxios = lwxios        )
+                     CALL iom_rstput( kt, nitrst, numrow, 'tb'     , tsb(:,:,:,jp_tem), lxios = lwxios )
+                     CALL iom_rstput( kt, nitrst, numrow, 'sb'     , tsb(:,:,:,jp_sal), lxios = lwxios )
+                     CALL iom_rstput( kt, nitrst, numrow, 'sshb'   , sshb, lxios = lwxios      )
                      !
                      CALL iom_rstput( kt, nitrst, numrow, 'un'     , un, lxios = lwxios        )     ! now fields
@@ -164,17 +160,17 @@
                   ! extra variable needed for the ice sheet coupling
                   IF ( ln_iscpl ) THEN 
-                     CALL iom_rstput( kt, nitrst, numrow, 'tmask'  , tmask     ) ! need to extrapolate T/S
-                     CALL iom_rstput( kt, nitrst, numrow, 'umask'  , umask     ) ! need to correct barotropic velocity
-                     CALL iom_rstput( kt, nitrst, numrow, 'vmask'  , vmask     ) ! need to correct barotropic velocity
-                     CALL iom_rstput( kt, nitrst, numrow, 'smask'  , ssmask    ) ! need to correct barotropic velocity
-                     CALL iom_rstput( kt, nitrst, numrow, 'e3t_n', e3t_n(:,:,:) )   ! need to compute temperature correction
-                     CALL iom_rstput( kt, nitrst, numrow, 'e3u_n', e3u_n(:,:,:) )   ! need to compute bt conservation
-                     CALL iom_rstput( kt, nitrst, numrow, 'e3v_n', e3v_n(:,:,:) )   ! need to compute bt conservation
-                     CALL iom_rstput( kt, nitrst, numrow, 'gdepw_n', gdepw_n(:,:,:) ) ! need to compute extrapolation if vvl
+                     CALL iom_rstput( kt, nitrst, numrow, 'tmask'  , tmask, lxios = lwxios ) ! need to extrapolate T/S
+                     CALL iom_rstput( kt, nitrst, numrow, 'umask'  , umask, lxios = lwxios ) ! need to correct barotropic velocity
+                     CALL iom_rstput( kt, nitrst, numrow, 'vmask'  , vmask, lxios = lwxios ) ! need to correct barotropic velocity
+                     CALL iom_rstput( kt, nitrst, numrow, 'smask'  , ssmask, lxios = lwxios) ! need to correct barotropic velocity
+                     CALL iom_rstput( kt, nitrst, numrow, 'e3t_n', e3t_n(:,:,:), lxios = lwxios )   ! need to compute temperature correction
+                     CALL iom_rstput( kt, nitrst, numrow, 'e3u_n', e3u_n(:,:,:), lxios = lwxios )   ! need to compute bt conservation
+                     CALL iom_rstput( kt, nitrst, numrow, 'e3v_n', e3v_n(:,:,:), lxios = lwxios )   ! need to compute bt conservation
+                     CALL iom_rstput( kt, nitrst, numrow, 'gdepw_n', gdepw_n(:,:,:), lxios = lwxios ) ! need to compute extrapolation if vvl
                   END IF
       ENDIF
       
-      IF (ln_diurnal) CALL iom_rstput( kt, nitrst, numrow, 'Dsst', x_dsst    )  
-
+      IF (ln_diurnal) CALL iom_rstput( kt, nitrst, numrow, 'Dsst', x_dsst, lxios = lwxios )  
+      IF(lwxios) CALL iom_swap(      cxios_context          )
       IF( kt == nitrst ) THEN
          IF(.NOT.lwxios) THEN
Index: /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcapr.F90
===================================================================
--- /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcapr.F90	(revision 8643)
+++ /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcapr.F90	(revision 8644)
@@ -110,4 +110,7 @@
             CALL ctl_warn( 'sbc_apr: use inverse barometer ssh at open boundary ONLY requires ln_apr_dyn=T' )
       !
+      IF( lwxios ) THEN
+         CALL set_rstw_var_active('ssh_ibb')
+      ENDIF
    END SUBROUTINE sbc_apr_init
 
Index: /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcisf.F90
===================================================================
--- /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcisf.F90	(revision 8643)
+++ /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcisf.F90	(revision 8644)
@@ -27,4 +27,5 @@
    USE timing          ! Timing
    USE lib_fortran     ! glob_sum
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -232,7 +233,9 @@
                &                    'at it= ', kt,' date= ', ndastp
             IF(lwp) WRITE(numout,*) '~~~~'
-            CALL iom_rstput( kt, nitrst, numrow, 'fwf_isf_b', fwfisf(:,:) )
-            CALL iom_rstput( kt, nitrst, numrow, 'isf_hc_b' , risf_tsc(:,:,jp_tem) )
-            CALL iom_rstput( kt, nitrst, numrow, 'isf_sc_b' , risf_tsc(:,:,jp_sal) )
+            IF( lwxios ) CALL iom_swap(      wxios_context          )
+               CALL iom_rstput( kt, nitrst, numrow, 'fwf_isf_b', fwfisf(:,:), lxios = lwxios )
+               CALL iom_rstput( kt, nitrst, numrow, 'isf_hc_b' , risf_tsc(:,:,jp_tem), lxios = lwxios )
+               CALL iom_rstput( kt, nitrst, numrow, 'isf_sc_b' , risf_tsc(:,:,jp_sal), lxios = lwxios )
+            IF( lwxios ) CALL iom_swap(      cxios_context          )
          ENDIF
          !
@@ -393,4 +396,11 @@
       END DO
 
+      IF( lwxios ) THEN
+          CALL set_rstw_var_active('fwf_isf_b')
+          CALL set_rstw_var_active('isf_hc_b')
+          CALL set_rstw_var_active('isf_sc_b')
+      ENDIF
+
+
   END SUBROUTINE sbc_isf_init
 
Index: /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcmod.F90
===================================================================
--- /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcmod.F90	(revision 8643)
+++ /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcmod.F90	(revision 8644)
@@ -342,4 +342,14 @@
       IF( ln_wave     )   CALL sbc_wave_init              ! surface wave initialisation
       !
+      IF( lwxios ) THEN
+         CALL set_rstw_var_active('utau_b')
+         CALL set_rstw_var_active('vtau_b')
+         CALL set_rstw_var_active('qns_b')
+         ! The 3D heat content due to qsr forcing is treated in traqsr
+         ! CALL set_rstw_var_active('qsr_b')
+         CALL set_rstw_var_active('emp_b')
+         CALL set_rstw_var_active('sfx_b')
+      ENDIF
+
    END SUBROUTINE sbc_init
 
Index: /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcrnf.F90
===================================================================
--- /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcrnf.F90	(revision 8643)
+++ /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcrnf.F90	(revision 8644)
@@ -475,4 +475,10 @@
       ENDIF
       !
+      IF( lwxios ) THEN
+         CALL set_rstw_var_active('rnf_b')
+         CALL set_rstw_var_active('rnf_hc_b')
+         CALL set_rstw_var_active('rnf_sc_b')
+      ENDIF
+
    END SUBROUTINE sbc_rnf_init
 
Index: /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcssm.F90
===================================================================
--- /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcssm.F90	(revision 8643)
+++ /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/SBC/sbcssm.F90	(revision 8644)
@@ -254,4 +254,15 @@
       ENDIF
       !
+      IF( lwxios.AND.nn_fsbc > 1 ) THEN
+         CALL set_rstw_var_active('nn_fsbc')
+         CALL set_rstw_var_active('ssu_m')
+         CALL set_rstw_var_active('ssv_m')
+         CALL set_rstw_var_active('sst_m')
+         CALL set_rstw_var_active('sss_m')
+         CALL set_rstw_var_active('ssh_m')
+         CALL set_rstw_var_active('e3t_m')
+         CALL set_rstw_var_active('frq_m')
+      ENDIF
+
    END SUBROUTINE sbc_ssm_init
 
Index: /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traadv.F90
===================================================================
--- /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traadv.F90	(revision 8643)
+++ /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traadv.F90	(revision 8644)
@@ -299,5 +299,4 @@
       !
       CALL tra_adv_mle_init            !== initialisation of the Mixed Layer Eddy parametrisation (MLE)  ==!
-      lr_traadv_cen2 = ln_traadv_cen2
       !
    END SUBROUTINE tra_adv_init
Index: /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traqsr.F90
===================================================================
--- /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traqsr.F90	(revision 8643)
+++ /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/TRA/traqsr.F90	(revision 8644)
@@ -438,4 +438,8 @@
       ENDIF
       !
+      IF( lwxios ) THEN
+         CALL set_rstw_var_active('qsr_hc_b')
+         CALL set_rstw_var_active('fraqsr_1lev')
+      ENDIF
       IF( nn_timing == 1 )   CALL timing_stop('tra_qsr_init')
       !
Index: /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfgls.F90
===================================================================
--- /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfgls.F90	(revision 8643)
+++ /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfgls.F90	(revision 8644)
@@ -34,4 +34,5 @@
    USE timing         ! Timing
    USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)  
+   USE iom_def, ONLY : lwxios
 
    IMPLICIT NONE
@@ -1140,4 +1141,13 @@
       CALL gls_rst( nit000, 'READ' )   !* read or initialize all required files
       !
+      IF( lwxios ) THEN
+         CALL set_rstw_var_active('en')
+         CALL set_rstw_var_active('avt')
+         CALL set_rstw_var_active('avm')
+         CALL set_rstw_var_active('avmu')
+         CALL set_rstw_var_active('avmv')
+         CALL set_rstw_var_active('mxln')
+      ENDIF
+
       IF( nn_timing == 1 )  CALL timing_stop('zdf_gls_init')
       !
@@ -1200,10 +1210,12 @@
          !                                   ! -------------------
          IF(lwp) WRITE(numout,*) '---- gls-rst ----'
-         CALL iom_rstput( kt, nitrst, numrow, 'en'   , en     ) 
-         CALL iom_rstput( kt, nitrst, numrow, 'avt'  , avt_k  )
-         CALL iom_rstput( kt, nitrst, numrow, 'avm'  , avm_k  )
-         CALL iom_rstput( kt, nitrst, numrow, 'avmu' , avmu_k ) 
-         CALL iom_rstput( kt, nitrst, numrow, 'avmv' , avmv_k )
-         CALL iom_rstput( kt, nitrst, numrow, 'mxln' , mxln   )
+         IF( lwxios ) CALL iom_swap(      wxios_context          )
+         CALL iom_rstput( kt, nitrst, numrow, 'en'   , en, lxios = lwxios      ) 
+         CALL iom_rstput( kt, nitrst, numrow, 'avt'  , avt_k, lxios = lwxios  )
+         CALL iom_rstput( kt, nitrst, numrow, 'avm'  , avm_k, lxios = lwxios  )
+         CALL iom_rstput( kt, nitrst, numrow, 'avmu' , avmu_k, lxios = lwxios ) 
+         CALL iom_rstput( kt, nitrst, numrow, 'avmv' , avmv_k, lxios = lwxios )
+         CALL iom_rstput( kt, nitrst, numrow, 'mxln' , mxln, lxios = lwxios   )
+         IF( lwxios ) CALL iom_swap(      cxios_context          )
          !
       ENDIF
Index: /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/ZDF/zdftke.F90
===================================================================
--- /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/ZDF/zdftke.F90	(revision 8643)
+++ /branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/ZDF/zdftke.F90	(revision 8644)
@@ -815,4 +815,13 @@
       CALL tke_rst( nit000, 'READ' )  !* read or initialize all required files
       !
+      IF( lwxios ) THEN
+        CALL set_rstw_var_active('en')
+        CALL set_rstw_var_active('avt')
+        CALL set_rstw_var_active('avm')
+        CALL set_rstw_var_active('avmu')
+        CALL set_rstw_var_active('avmv')
+        CALL set_rstw_var_active('dissl')
+      ENDIF
+
    END SUBROUTINE zdf_tke_init
 
