Index: branches/2017/dev_r8600_xios_read/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90
===================================================================
--- branches/2017/dev_r8600_xios_read/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90	(revision 8602)
+++ branches/2017/dev_r8600_xios_read/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90	(revision 8612)
@@ -41,7 +41,9 @@
    USE dianam          ! build name of file
    USE xios
+   USE iom_def, ONLY : max_rst_fields, rst_fields
 # endif
    USE ioipsl, ONLY :  ju2ymds    ! for calendar
    USE crs             ! Grid coarsening
+   USE lib_fortran 
 
    IMPLICIT NONE
@@ -63,4 +65,5 @@
    PRIVATE iom_set_domain_attr, iom_set_axis_attr, iom_set_field_attr, iom_set_file_attr, iom_get_file_attr, iom_set_grid_attr
    PRIVATE set_grid, set_grid_bounds, set_scalar, set_xmlatt, set_mooring, iom_update_file_name, iom_sdate
+   PRIVATE set_rst_vars, set_rstr_active, set_rst_context
 # endif
 
@@ -103,4 +106,5 @@
       CHARACTER(len=10) :: clname
       INTEGER           :: ji, jkmin
+      LOGICAL :: lrst_context              ! is context related to restart
       !
       REAL(wp), ALLOCATABLE, DIMENSION(:,:) :: zt_bnds, zw_bnds
@@ -113,5 +117,5 @@
       CALL xios_context_initialize(TRIM(clname), mpi_comm_opa)
       CALL iom_swap( cdname )
-
+      lrst_context =  (TRIM(cdname) == TRIM(rxios_context))
 
       ! Calendar type is now defined in xml file 
@@ -126,11 +130,11 @@
 
       ! horizontal grid definition
-      CALL set_scalar
+      IF(.NOT.lrst_context) CALL set_scalar
 
       IF( TRIM(cdname) == TRIM(cxios_context) ) THEN  
-         CALL set_grid( "T", glamt, gphit ) 
-         CALL set_grid( "U", glamu, gphiu )
-         CALL set_grid( "V", glamv, gphiv )
-         CALL set_grid( "W", glamt, gphit )
+         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 )
          !
@@ -150,13 +154,13 @@
          CALL dom_grid_crs   ! Save the parent grid information  & Switch to coarse grid domain
          !
-         CALL set_grid( "T", glamt_crs, gphit_crs ) 
-         CALL set_grid( "U", glamu_crs, gphiu_crs ) 
-         CALL set_grid( "V", glamv_crs, gphiv_crs ) 
-         CALL set_grid( "W", glamt_crs, gphit_crs ) 
+         CALL set_grid( "T", glamt_crs, gphit_crs, .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(rxios_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))
@@ -171,35 +175,44 @@
 
       ! vertical grid definition
-      CALL iom_set_axis_attr( "deptht", gdept_1d )
-      CALL iom_set_axis_attr( "depthu", gdept_1d )
-      CALL iom_set_axis_attr( "depthv", gdept_1d )
-      CALL iom_set_axis_attr( "depthw", gdepw_1d )
-
-      ! 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 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
-      CALL set_xmlatt
+      IF( lrst_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()
+!set which fields are to be read from restart file
+       CALL set_rstr_active()
+      ELSE
+       CALL set_xmlatt
+      ENDIF
 
       ! end file definition
@@ -213,7 +226,186 @@
 
 #endif
-      
+
    END SUBROUTINE iom_init
 
+   
+   SUBROUTINE set_rst_vars()
+!set names for variables in restart file
+   INTEGER :: i
+        rst_fields(:)%vname="NO_NAME";         rst_fields(:)%grid="NO_GRID"
+        i = 0
+        i = i + 1; rst_fields(i)%vname="rdt";            rst_fields(i)% grid="grid_scalar"
+        i = i + 1; rst_fields(i)%vname="un";             rst_fields(i)% grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="ub";             rst_fields(i)% grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="vn";             rst_fields(i)% grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="vb";             rst_fields(i)% grid="grid_N_3D"  
+        i = i + 1; rst_fields(i)%vname="tn";             rst_fields(i)% grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="tb";             rst_fields(i)% grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="sn";             rst_fields(i)% grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="sb";             rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="sshn";           rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="sshb";           rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="rhop";           rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="kt";             rst_fields(i)%grid="grid_scalar"
+        i = i + 1; rst_fields(i)%vname="ndastp";         rst_fields(i)%grid="grid_scalar"
+        i = i + 1; rst_fields(i)%vname="adatrj";         rst_fields(i)%grid="grid_scalar"
+        i = i + 1; rst_fields(i)%vname="utau_b";         rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="vtau_b";         rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="qns_b";          rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="emp_b";          rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="sfx_b";          rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="en" ;            rst_fields(i)%grid="grid_N_3D" 
+        i = i + 1; rst_fields(i)%vname="avt";            rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="avm";            rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="avmu";           rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="avmv";           rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="dissl";          rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="sbc_hc_b";       rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="sbc_sc_b";       rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="qsr_hc_b";       rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="fraqsr_1lev";    rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="greenland_icesheet_mass"
+                                               rst_fields(i)%grid="grid_scalar"
+        i = i + 1; rst_fields(i)%vname="greenland_icesheet_timelapsed"
+                                               rst_fields(i)%grid="grid_scalar"
+        i = i + 1; rst_fields(i)%vname="greenland_icesheet_mass_roc"
+                                               rst_fields(i)%grid="grid_scalar"
+        i = i + 1; rst_fields(i)%vname="antarctica_icesheet_mass"
+                                               rst_fields(i)%grid="grid_scalar"
+        i = i + 1; rst_fields(i)%vname="antarctica_icesheet_timelapsed"
+                                               rst_fields(i)%grid="grid_scalar"
+        i = i + 1; rst_fields(i)%vname="antarctica_icesheet_mass_roc"
+                                               rst_fields(i)%grid="grid_scalar"
+        i = i + 1; rst_fields(i)%vname="frc_v";          rst_fields(i)%grid="grid_scalar"
+        i = i + 1; rst_fields(i)%vname="frc_t";          rst_fields(i)%grid="grid_scalar"
+        i = i + 1; rst_fields(i)%vname="frc_s";          rst_fields(i)%grid="grid_scalar"
+        i = i + 1; rst_fields(i)%vname="frc_wn_t";       rst_fields(i)%grid="grid_scalar"
+        i = i + 1; rst_fields(i)%vname="frc_wn_s";       rst_fields(i)%grid="grid_scalar"
+        i = i + 1; rst_fields(i)%vname="ssh_ini";        rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="e3t_ini";        rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="hc_loc_ini";     rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="sc_loc_ini";     rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="ssh_hc_loc_ini"; rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="ssh_sc_loc_ini"; rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="tilde_e3t_b";    rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="tilde_e3t_n";    rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="hdiv_lf";        rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="ub2_b";          rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="vb2_b";          rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="sshbb_e";        rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="ubb_e";          rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="vbb_e";          rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="sshb_e";         rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="ub_e";           rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="vb_e";           rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="fwf_isf_b";      rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="isf_sc_b";       rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="isf_hc_b";       rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="ssh_ibb";        rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="rnf_b";          rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="rnf_hc_b";       rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="rnf_sc_b";       rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="nn_fsbc";        rst_fields(i)%grid="grid_scalar"
+        i = i + 1; rst_fields(i)%vname="ssu_m";          rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="ssv_m";          rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="sst_m";          rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="sss_m";          rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="ssh_m";          rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="e3t_m";          rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="frq_m";          rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="avmb";           rst_fields(i)%grid="grid_vector"
+        i = i + 1; rst_fields(i)%vname="avtb";           rst_fields(i)%grid="grid_vector"
+        i = i + 1; rst_fields(i)%vname="ub2_i_b";        rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="vb2_i_b";        rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="ntime";          rst_fields(i)%grid="grid_scalar"
+        i = i + 1; rst_fields(i)%vname="Dsst";           rst_fields(i)%grid="grid_scalar"
+        i = i + 1; rst_fields(i)%vname="tmask";          rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="umask";          rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="vmask";          rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="smask";          rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="gdepw_n";        rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="e3t_n";          rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="e3u_n";          rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="e3v_n";          rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="surf_ini";       rst_fields(i)%grid="grid_N"
+        i = i + 1; rst_fields(i)%vname="e3t_b";          rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="e3t_n";          rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="mxln";           rst_fields(i)%grid="grid_N_3D"
+        i = i + 1; rst_fields(i)%vname="e3t_m";          rst_fields(i)%grid="grid_N_3D"
+   END SUBROUTINE set_rst_vars
+
+
+   SUBROUTINE set_rstr_active()
+!sets enabled = .TRUE. for each field in restart file
+   CHARACTER(len=256) :: rst_file
+   TYPE(xios_field) :: field_hdl
+   TYPE(xios_file) :: file_hdl
+   TYPE(xios_filegroup) :: filegroup_hdl
+   INTEGER :: i
+   CHARACTER(lc)  ::   clpath
+
+        clpath = TRIM(cn_ocerst_indir)
+        IF( clpath(LEN_TRIM(clpath):) /= '/' ) clpath = TRIM(clpath) // '/'
+        IF( TRIM(Agrif_CFixed()) == '0' ) THEN
+           rst_file = TRIM(clpath)//TRIM(cn_ocerst_in)
+        ELSE
+           rst_file = TRIM(clpath)//'1_'//TRIM(cn_ocerst_in)
+        ENDIF
+!set name of the restart file and enable available fields
+        if(lwp) WRITE(numout,*) 'Setting restart filename (for XIOS) to: ',rst_file
+        CALL xios_get_handle("file_definition", filegroup_hdl )
+        CALL xios_add_child(filegroup_hdl, file_hdl, 'rrestart')
+        CALL xios_set_file_attr( "rrestart", name=trim(rst_file), type="one_file", &
+             par_access="collective", enabled=.TRUE., mode="read",                 &
+             output_freq=xios_timestep)
+!defin files for restart context
+        DO i = 1, max_rst_fields
+         IF( TRIM(rst_fields(i)%vname) /= "NO_NAME") THEN
+           IF( iom_varid( numror, TRIM(rst_fields(i)%vname), ldstop = .FALSE. ) > 0 ) THEN
+                CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_fields(i)%vname))
+                SELECT CASE (TRIM(rst_fields(i)%grid))
+                 CASE ("grid_N_3D")
+                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_fields(i)%vname), &
+                        domain_ref="grid_N", axis_ref="deptht", operation = "instant")
+                 CASE ("grid_N")
+                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_fields(i)%vname), &
+                        domain_ref="grid_N", operation = "instant") 
+                CASE ("grid_vector")
+                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_fields(i)%vname), &
+                         axis_ref="deptht", operation = "instant")
+                 CASE ("grid_scalar")
+                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_fields(i)%vname), &
+                        scalar_ref = "grid_scalar", operation = "instant")
+                END SELECT
+                IF(lwp) WRITE(numout,*) 'XIOS read: ', TRIM(rst_fields(i)%vname), ' enabled in ', TRIM(rst_file)
+           ENDIF
+         ENDIF
+        END DO
+   END SUBROUTINE set_rstr_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.) 
+ 
+     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 SUBROUTINE set_rst_context
 
    SUBROUTINE iom_swap( cdname )
@@ -347,4 +539,6 @@
             icnt = icnt + 1
          END DO
+      ELSE
+         lxios_sini = .TRUE.
       ENDIF
       IF( llwrt ) THEN
@@ -530,9 +724,10 @@
    !!                   INTERFACE iom_get
    !!----------------------------------------------------------------------
-   SUBROUTINE iom_g0d( kiomid, cdvar, pvar, ktime )
+   SUBROUTINE iom_g0d( kiomid, cdvar, pvar, ktime, lrxios )
       INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
       CHARACTER(len=*), INTENT(in   )                 ::   cdvar     ! Name of the variable
       REAL(wp)        , INTENT(  out)                 ::   pvar      ! read field
       INTEGER         , INTENT(in   ),     OPTIONAL   ::   ktime     ! record number
+      LOGICAL         , INTENT(in   ),     OPTIONAL   ::   lrxios    ! use xios to read restart
       !
       INTEGER                                         ::   idvar     ! variable id
@@ -542,4 +737,5 @@
       CHARACTER(LEN=100)                              ::   clname    ! file name
       CHARACTER(LEN=1)                                ::   cldmspc   !
+      LOGICAL                                         ::   lxios
       !
       itime = 1
@@ -567,5 +763,5 @@
    END SUBROUTINE iom_g0d
 
-   SUBROUTINE iom_g1d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount )
+   SUBROUTINE iom_g1d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrxios )
       INTEGER         , INTENT(in   )                         ::   kiomid    ! Identifier of the file
       INTEGER         , INTENT(in   )                         ::   kdom      ! Type of domain to be read
@@ -575,12 +771,14 @@
       INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kstart    ! start axis position of the reading 
       INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kcount    ! number of points in each axis
+      LOGICAL         , INTENT(in   ),               OPTIONAL ::   lrxios    ! read data using XIOS
       !
       IF( kiomid > 0 ) THEN
          IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r1d=pvar,   &
-              &                                                     ktime=ktime, kstart=kstart, kcount=kcount )
+              &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
+              &                                                     lrxios=lrxios )
       ENDIF
    END SUBROUTINE iom_g1d
 
-   SUBROUTINE iom_g2d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr )
+   SUBROUTINE iom_g2d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr, lrxios)
       INTEGER         , INTENT(in   )                           ::   kiomid    ! Identifier of the file
       INTEGER         , INTENT(in   )                           ::   kdom      ! Type of domain to be read
@@ -594,13 +792,14 @@
                                                                                ! called open_ocean_jstart to set the start
                                                                                ! value for the 2nd dimension (netcdf only)
+      LOGICAL         , INTENT(in   ),                 OPTIONAL ::   lrxios    ! read data using XIOS
       !
       IF( kiomid > 0 ) THEN
          IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r2d=pvar,   &
               &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
-              &                                                     lrowattr=lrowattr )
+              &                                                     lrowattr=lrowattr,  lrxios=lrxios)
       ENDIF
    END SUBROUTINE iom_g2d
 
-   SUBROUTINE iom_g3d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr )
+   SUBROUTINE iom_g3d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr, lrxios )
       INTEGER         , INTENT(in   )                             ::   kiomid    ! Identifier of the file
       INTEGER         , INTENT(in   )                             ::   kdom      ! Type of domain to be read
@@ -614,9 +813,10 @@
                                                                                  ! called open_ocean_jstart to set the start
                                                                                  ! value for the 2nd dimension (netcdf only)
+      LOGICAL         , INTENT(in   ),                   OPTIONAL ::   lrxios    ! read data using XIOS
       !
       IF( kiomid > 0 ) THEN
          IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r3d=pvar,   &
               &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
-              &                                                     lrowattr=lrowattr )
+              &                                                     lrowattr=lrowattr, lrxios=lrxios )
       ENDIF
    END SUBROUTINE iom_g3d
@@ -626,5 +826,5 @@
          &                  pv_r1d, pv_r2d, pv_r3d,   &
          &                  ktime , kstart, kcount,   &
-         &                  lrowattr                )
+         &                  lrowattr, lrxios        )
       !!-----------------------------------------------------------------------
       !!                  ***  ROUTINE  iom_get_123d  ***
@@ -647,5 +847,7 @@
                                                                            ! called open_ocean_jstart to set the start
                                                                            ! value for the 2nd dimension (netcdf only)
-      !
+      LOGICAL                    , INTENT(in   ), OPTIONAL ::   lrxios     ! use XIOS to read restart
+      !
+      LOGICAL                        ::   lxios       ! local definition for XIOS read
       LOGICAL                        ::   llnoov      ! local definition to read overlap
       LOGICAL                        ::   luse_jattr  ! local definition to read open_ocean_jstart file attribute
@@ -674,15 +876,21 @@
       !---------------------------------------------------------------------
       !
-      clname = iom_file(kiomid)%name   !   esier to read
-      clinfo = '          iom_get_123d, file: '//trim(clname)//', var: '//trim(cdvar)
-      ! local definition of the domain ?
+      REAL(wp)                       :: gma, gmi
+      lxios = .FALSE.
+      if(PRESENT(lrxios)) lxios = lrxios
+      idvar = iom_varid( kiomid, cdvar ) 
       idom = kdom
-      ! do we read the overlap 
-      ! ugly patch SM+JMM+RB to overwrite global definition in some cases
-      llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
-      ! check kcount and kstart optionals parameters...
-      IF( PRESENT(kcount) .AND. (.NOT. PRESENT(kstart)) ) CALL ctl_stop(trim(clinfo), 'kcount present needs kstart present')
-      IF( PRESENT(kstart) .AND. (.NOT. PRESENT(kcount)) ) CALL ctl_stop(trim(clinfo), 'kstart present needs kcount present')
-      IF( PRESENT(kstart) .AND. idom /= jpdom_unknown .AND.  idom /= jpdom_autoglo_xy  ) &
+
+      IF(.NOT.lxios) THEN
+          clname = iom_file(kiomid)%name   !   esier to read
+          clinfo = '          iom_get_123d, file: '//trim(clname)//', var: '//trim(cdvar)
+          ! local definition of the domain ?
+          ! do we read the overlap 
+          ! ugly patch SM+JMM+RB to overwrite global definition in some cases
+          llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
+          ! check kcount and kstart optionals parameters...
+          IF( PRESENT(kcount) .AND. (.NOT. PRESENT(kstart)) ) CALL ctl_stop(trim(clinfo), 'kcount present needs kstart present')
+          IF( PRESENT(kstart) .AND. (.NOT. PRESENT(kcount)) ) CALL ctl_stop(trim(clinfo), 'kstart present needs kcount present')
+          IF( PRESENT(kstart) .AND. idom /= jpdom_unknown .AND.  idom /= jpdom_autoglo_xy  ) &
      &           CALL ctl_stop(trim(clinfo), 'kstart present needs kdom = jpdom_unknown or kdom = jpdom_autoglo_xy')
 
@@ -701,76 +909,75 @@
       ENDIF
 
-      ! Search for the variable in the data base (eventually actualize data)
-      istop = nstop
-      idvar = iom_varid( kiomid, cdvar )
-      !
-      IF( idvar > 0 ) THEN
-         ! to write iom_file(kiomid)%dimsz in a shorter way !
-         idimsz(:) = iom_file(kiomid)%dimsz(:, idvar) 
-         inbdim = iom_file(kiomid)%ndims(idvar)            ! number of dimensions in the file
-         idmspc = inbdim                                   ! number of spatial dimensions in the file
-         IF( iom_file(kiomid)%luld(idvar) )   idmspc = inbdim - 1
-         IF( idmspc > 3 )   CALL ctl_stop(trim(clinfo), 'the file has more than 3 spatial dimensions this case is not coded...') 
-         !
-         ! update idom definition...
-         ! Identify the domain in case of jpdom_auto(glo/dta) definition
-         IF( idom == jpdom_autoglo_xy ) THEN
-            ll_depth_spec = .TRUE.
-            idom = jpdom_autoglo
-         ELSE
-            ll_depth_spec = .FALSE.
-         ENDIF
-         IF( idom == jpdom_autoglo .OR. idom == jpdom_autodta ) THEN            
-            IF( idom == jpdom_autoglo ) THEN   ;   idom = jpdom_global 
-            ELSE                               ;   idom = jpdom_data
-            ENDIF
-            ind1 = INDEX( clname, '_', back = .TRUE. ) + 1
-            ind2 = INDEX( clname, '.', back = .TRUE. ) - 1
-            IF( ind2 > ind1 ) THEN   ;   IF( VERIFY( clname(ind1:ind2), '0123456789' ) == 0 )   idom = jpdom_local   ;   ENDIF
-         ENDIF
-         ! Identify the domain in case of jpdom_local definition
-         IF( idom == jpdom_local ) THEN
-            IF(     idimsz(1) == jpi               .AND. idimsz(2) == jpj               ) THEN   ;   idom = jpdom_local_full
-            ELSEIF( idimsz(1) == nlci              .AND. idimsz(2) == nlcj              ) THEN   ;   idom = jpdom_local_noextra
-            ELSEIF( idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN   ;   idom = jpdom_local_noovlap
-            ELSE   ;   CALL ctl_stop( trim(clinfo), 'impossible to identify the local domain' )
-            ENDIF
-         ENDIF
-         !
-         ! check the consistency between input array and data rank in the file
-         !
-         ! initializations
-         itime = 1
-         IF( PRESENT(ktime) ) itime = ktime
-
-         irankpv = 1 * COUNT( (/PRESENT(pv_r1d)/) ) + 2 * COUNT( (/PRESENT(pv_r2d)/) ) + 3 * COUNT( (/PRESENT(pv_r3d)/) )
-         WRITE(clrankpv, fmt='(i1)') irankpv
-         WRITE(cldmspc , fmt='(i1)') idmspc
-         !
-         IF(     idmspc <  irankpv ) THEN 
-            CALL ctl_stop( TRIM(clinfo), 'The file has only '//cldmspc//' spatial dimension',   &
+          ! Search for the variable in the data base (eventually actualize data)
+          istop = nstop
+          !
+          IF( idvar > 0 ) THEN
+             ! to write iom_file(kiomid)%dimsz in a shorter way !
+             idimsz(:) = iom_file(kiomid)%dimsz(:, idvar) 
+             inbdim = iom_file(kiomid)%ndims(idvar)            ! number of dimensions in the file
+             idmspc = inbdim                                   ! number of spatial dimensions in the file
+             IF( iom_file(kiomid)%luld(idvar) )   idmspc = inbdim - 1
+             IF( idmspc > 3 )   CALL ctl_stop(trim(clinfo), 'the file has more than 3 spatial dimensions this case is not coded...') 
+             !
+             ! update idom definition...
+             ! Identify the domain in case of jpdom_auto(glo/dta) definition
+             IF( idom == jpdom_autoglo_xy ) THEN
+                ll_depth_spec = .TRUE.
+                idom = jpdom_autoglo
+             ELSE
+                ll_depth_spec = .FALSE.
+             ENDIF
+             IF( idom == jpdom_autoglo .OR. idom == jpdom_autodta ) THEN            
+                IF( idom == jpdom_autoglo ) THEN   ;   idom = jpdom_global 
+                ELSE                               ;   idom = jpdom_data
+                ENDIF
+                ind1 = INDEX( clname, '_', back = .TRUE. ) + 1
+                ind2 = INDEX( clname, '.', back = .TRUE. ) - 1
+                IF( ind2 > ind1 ) THEN   ;   IF( VERIFY( clname(ind1:ind2), '0123456789' ) == 0 )   idom = jpdom_local   ;   ENDIF
+             ENDIF
+             ! Identify the domain in case of jpdom_local definition
+             IF( idom == jpdom_local ) THEN
+                IF(     idimsz(1) == jpi               .AND. idimsz(2) == jpj               ) THEN   ;   idom = jpdom_local_full
+                ELSEIF( idimsz(1) == nlci              .AND. idimsz(2) == nlcj              ) THEN   ;   idom = jpdom_local_noextra
+                ELSEIF( idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN   ;   idom = jpdom_local_noovlap
+                ELSE   ;   CALL ctl_stop( trim(clinfo), 'impossible to identify the local domain' )
+                ENDIF
+             ENDIF
+             !
+             ! check the consistency between input array and data rank in the file
+             !
+             ! initializations
+             itime = 1
+             IF( PRESENT(ktime) ) itime = ktime
+
+             irankpv = 1 * COUNT( (/PRESENT(pv_r1d)/) ) + 2 * COUNT( (/PRESENT(pv_r2d)/) ) + 3 * COUNT( (/PRESENT(pv_r3d)/) )
+             WRITE(clrankpv, fmt='(i1)') irankpv
+             WRITE(cldmspc , fmt='(i1)') idmspc
+             !
+             IF(     idmspc <  irankpv ) THEN 
+                CALL ctl_stop( TRIM(clinfo), 'The file has only '//cldmspc//' spatial dimension',   &
                &                         'it is impossible to read a '//clrankpv//'D array from this file...' )
-         ELSEIF( idmspc == irankpv ) THEN
-            IF( PRESENT(pv_r1d) .AND. idom /= jpdom_unknown )   &
+             ELSEIF( idmspc == irankpv ) THEN
+                IF( PRESENT(pv_r1d) .AND. idom /= jpdom_unknown )   &
                &   CALL ctl_stop( TRIM(clinfo), 'case not coded...You must use jpdom_unknown' )
-         ELSEIF( idmspc >  irankpv ) THEN
-               IF( PRESENT(pv_r2d) .AND. itime == 1 .AND. idimsz(3) == 1 .AND. idmspc == 3 ) THEN
-                  CALL ctl_warn( trim(clinfo), '2D array but 3 spatial dimensions for the data...'              ,   &
+             ELSEIF( idmspc >  irankpv ) THEN
+                   IF( PRESENT(pv_r2d) .AND. itime == 1 .AND. idimsz(3) == 1 .AND. idmspc == 3 ) THEN
+                      CALL ctl_warn( trim(clinfo), '2D array but 3 spatial dimensions for the data...'              ,   &
                         &         'As the size of the z dimension is 1 and as we try to read the first record, ',   &
                         &         'we accept this case, even if there is a possible mix-up between z and time dimension' )   
-                  idmspc = idmspc - 1
-               ELSE
-                  CALL ctl_stop( TRIM(clinfo), 'To keep iom lisibility, when reading a '//clrankpv//'D array,'         ,   &
+                      idmspc = idmspc - 1
+                   ELSE
+                      CALL ctl_stop( TRIM(clinfo), 'To keep iom lisibility, when reading a '//clrankpv//'D array,'         ,   &
                      &                         'we do not accept data with '//cldmspc//' spatial dimensions',   &
                      &                         'Use ncwa -a to suppress the unnecessary dimensions' )
-               ENDIF
-         ENDIF
-
-         !
-         ! definition of istart and icnt
-         !
-         icnt  (:) = 1
-         istart(:) = 1
-         istart(idmspc+1) = itime
+                   ENDIF
+             ENDIF
+
+             !
+             ! definition of istart and icnt
+             !
+             icnt  (:) = 1
+             istart(:) = 1
+             istart(idmspc+1) = itime
 
          IF( PRESENT(kstart) .AND. .NOT. ll_depth_spec ) THEN 
@@ -793,5 +1000,5 @@
 ! JMM + SM: ugly patch before getting the new version of lib_mpp)
 !                  IF( idom /= jpdom_local_noovlap )   istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
-                  IF( llnoov .AND. idom /= jpdom_local_noovlap ) istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
+                      IF( llnoov .AND. idom /= jpdom_local_noovlap ) istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
                   ! we do not read the overlap and the extra-halos -> from nldi to nlei and from nldj to nlej 
 ! JMM + SM: ugly patch before getting the new version of lib_mpp)
@@ -810,64 +1017,64 @@
          ENDIF
 
-         ! check that istart and icnt can be used with this file
-         !-
-         DO jl = 1, jpmax_dims
-            itmp = istart(jl)+icnt(jl)-1
-            IF( itmp > idimsz(jl) .AND. idimsz(jl) /= 0 ) THEN
-               WRITE( ctmp1, FMT="('(istart(', i1, ') + icnt(', i1, ') - 1) = ', i5)" ) jl, jl, itmp
-               WRITE( ctmp2, FMT="(' is larger than idimsz(', i1,') = ', i5)"         ) jl, idimsz(jl)
-               CALL ctl_stop( trim(clinfo), 'start and count too big regarding to the size of the data, ', ctmp1, ctmp2 )     
-            ENDIF
-         END DO
-
-         ! check that icnt matches the input array
-         !-     
-         IF( idom == jpdom_unknown ) THEN
-            IF( irankpv == 1 )        ishape(1:1) = SHAPE(pv_r1d)
-            IF( irankpv == 2 )        ishape(1:2) = SHAPE(pv_r2d)
-            IF( irankpv == 3 )        ishape(1:3) = SHAPE(pv_r3d)
-            ctmp1 = 'd'
-         ELSE
-            IF( irankpv == 2 ) THEN
+             ! check that istart and icnt can be used with this file
+             !-
+             DO jl = 1, jpmax_dims
+                itmp = istart(jl)+icnt(jl)-1
+                IF( itmp > idimsz(jl) .AND. idimsz(jl) /= 0 ) THEN
+                   WRITE( ctmp1, FMT="('(istart(', i1, ') + icnt(', i1, ') - 1) = ', i5)" ) jl, jl, itmp
+                   WRITE( ctmp2, FMT="(' is larger than idimsz(', i1,') = ', i5)"         ) jl, idimsz(jl)
+                   CALL ctl_stop( trim(clinfo), 'start and count too big regarding to the size of the data, ', ctmp1, ctmp2 )     
+                ENDIF
+             END DO
+
+             ! check that icnt matches the input array
+             !-     
+             IF( idom == jpdom_unknown ) THEN
+                IF( irankpv == 1 )        ishape(1:1) = SHAPE(pv_r1d)
+                IF( irankpv == 2 )        ishape(1:2) = SHAPE(pv_r2d)
+                IF( irankpv == 3 )        ishape(1:3) = SHAPE(pv_r3d)
+                ctmp1 = 'd'
+             ELSE
+                IF( irankpv == 2 ) THEN
 ! JMM + SM: ugly patch before getting the new version of lib_mpp)
 !               ishape(1:2) = SHAPE(pv_r2d(nldi:nlei,nldj:nlej  ))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej)'
-               IF( llnoov ) THEN ; ishape(1:2)=SHAPE(pv_r2d(nldi:nlei,nldj:nlej  )) ; ctmp1='d(nldi:nlei,nldj:nlej)'
-               ELSE              ; ishape(1:2)=SHAPE(pv_r2d(1   :nlci,1   :nlcj  )) ; ctmp1='d(1:nlci,1:nlcj)'
-               ENDIF
-            ENDIF
-            IF( irankpv == 3 ) THEN 
+                   IF( llnoov ) THEN ; ishape(1:2)=SHAPE(pv_r2d(nldi:nlei,nldj:nlej  )) ; ctmp1='d(nldi:nlei,nldj:nlej)'
+                   ELSE              ; ishape(1:2)=SHAPE(pv_r2d(1   :nlci,1   :nlcj  )) ; ctmp1='d(1:nlci,1:nlcj)'
+                   ENDIF
+                ENDIF
+                IF( irankpv == 3 ) THEN 
 ! JMM + SM: ugly patch before getting the new version of lib_mpp)
 !               ishape(1:3) = SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej,:)'
-               IF( llnoov ) THEN ; ishape(1:3)=SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:)) ; ctmp1='d(nldi:nlei,nldj:nlej,:)'
-               ELSE              ; ishape(1:3)=SHAPE(pv_r3d(1   :nlci,1   :nlcj,:)) ; ctmp1='d(1:nlci,1:nlcj,:)'
-               ENDIF
-            ENDIF
-         ENDIF
+                   IF( llnoov ) THEN ; ishape(1:3)=SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:)) ; ctmp1='d(nldi:nlei,nldj:nlej,:)'
+                   ELSE              ; ishape(1:3)=SHAPE(pv_r3d(1   :nlci,1   :nlcj,:)) ; ctmp1='d(1:nlci,1:nlcj,:)'
+                   ENDIF
+                ENDIF
+             ENDIF
          
-         DO jl = 1, irankpv
-            WRITE( ctmp2, FMT="(', ', i1,'): ', i5,' /= icnt(', i1,'):', i5)" ) jl, ishape(jl), jl, icnt(jl)
-            IF( ishape(jl) /= icnt(jl) )   CALL ctl_stop( TRIM(clinfo), 'size(pv_r'//clrankpv//TRIM(ctmp1)//TRIM(ctmp2) )
-         END DO
-
-      ENDIF
-
-      ! read the data
-      !-     
-      IF( idvar > 0 .AND. istop == nstop ) THEN   ! no additional errors until this point...
-         !
-         ! find the right index of the array to be read
+             DO jl = 1, irankpv
+                WRITE( ctmp2, FMT="(', ', i1,'): ', i5,' /= icnt(', i1,'):', i5)" ) jl, ishape(jl), jl, icnt(jl)
+                IF( ishape(jl) /= icnt(jl) )   CALL ctl_stop( TRIM(clinfo), 'size(pv_r'//clrankpv//TRIM(ctmp1)//TRIM(ctmp2) )
+             END DO
+
+          ENDIF
+
+          ! read the data
+          !-     
+          IF( idvar > 0 .AND. istop == nstop ) THEN   ! no additional errors until this point...
+             !
+             ! find the right index of the array to be read
 ! JMM + SM: ugly patch before getting the new version of lib_mpp)
 !         IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
 !         ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
 !         ENDIF
-         IF( llnoov ) THEN
-            IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
-            ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
-            ENDIF
-         ELSE
-            IF( idom /= jpdom_unknown ) THEN   ;   ix1 = 1      ;   ix2 = nlci      ;   iy1 = 1      ;   iy2 = nlcj
-            ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
-            ENDIF
-         ENDIF
+             IF( llnoov ) THEN
+                IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
+                ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
+                ENDIF
+             ELSE
+                IF( idom /= jpdom_unknown ) THEN   ;   ix1 = 1      ;   ix2 = nlci      ;   iy1 = 1      ;   iy2 = nlcj
+                ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
+                ENDIF
+             ENDIF
       
          SELECT CASE (iom_file(kiomid)%iolib)
@@ -878,42 +1085,73 @@
          END SELECT
 
-         IF( istop == nstop ) THEN   ! no additional errors until this point...
-            IF(lwp) WRITE(numout,"(10x,' read ',a,' (rec: ',i6,') in ',a,' ok')") TRIM(cdvar), itime, TRIM(iom_file(kiomid)%name)
+             IF( istop == nstop ) THEN   ! no additional errors until this point...
+                IF(lwp) WRITE(numout,"(10x,' read ',a,' (rec: ',i6,') in ',a,' ok')") TRIM(cdvar), itime, TRIM(iom_file(kiomid)%name)
           
-            !--- overlap areas and extra hallows (mpp)
-            IF(     PRESENT(pv_r2d) .AND. idom /= jpdom_unknown ) THEN
-               CALL lbc_lnk( pv_r2d,'Z',-999.,'no0' )
-            ELSEIF( PRESENT(pv_r3d) .AND. idom /= jpdom_unknown ) THEN
-               ! this if could be simplified with the new lbc_lnk that works with any size of the 3rd dimension
-               IF( icnt(3) == jpk ) THEN
-                  CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
-               ELSE   ! put some arbitrary value (a call to lbc_lnk will be done later...)
-                  DO jj = nlcj+1, jpj   ;   pv_r3d(1:nlci, jj, :) = pv_r3d(1:nlci, nlej, :)   ;   END DO
-                  DO ji = nlci+1, jpi   ;   pv_r3d(ji    , : , :) = pv_r3d(nlei  , :   , :)   ;   END DO
-               ENDIF
-            ENDIF
-            
-            ! C1D case : always call lbc_lnk to replicate the central value over the whole 3X3 domain
-            IF( lk_c1d .AND. PRESENT(pv_r2d) )   CALL lbc_lnk( pv_r2d,'Z',1. )
-            IF( lk_c1d .AND. PRESENT(pv_r3d) )   CALL lbc_lnk( pv_r3d,'Z',1. )
-    
-            !--- Apply scale_factor and offset
-            zscf = iom_file(kiomid)%scf(idvar)      ! scale factor
-            zofs = iom_file(kiomid)%ofs(idvar)      ! offset
-            IF(     PRESENT(pv_r1d) ) THEN
-               IF( zscf /= 1. )   pv_r1d(:) = pv_r1d(:) * zscf 
-               IF( zofs /= 0. )   pv_r1d(:) = pv_r1d(:) + zofs
-            ELSEIF( PRESENT(pv_r2d) ) THEN
-               IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
-               IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
-            ELSEIF( PRESENT(pv_r3d) ) THEN
-               IF( zscf /= 1.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) * zscf
-               IF( zofs /= 0.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) + zofs
-            ENDIF
-            !
-         ENDIF
-         !
-      ENDIF
-      !
+                !--- overlap areas and extra hallows (mpp)
+                IF(     PRESENT(pv_r2d) .AND. idom /= jpdom_unknown ) THEN
+                   CALL lbc_lnk( pv_r2d,'Z',-999.,'no0' )
+                ELSEIF( PRESENT(pv_r3d) .AND. idom /= jpdom_unknown ) THEN
+                   ! this if could be simplified with the new lbc_lnk that works with any size of the 3rd dimension
+                   IF( icnt(3) == jpk ) THEN
+                      CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
+                   ELSE   ! put some arbitrary value (a call to lbc_lnk will be done later...)
+                      DO jj = nlcj+1, jpj   ;   pv_r3d(1:nlci, jj, :) = pv_r3d(1:nlci, nlej, :)   ;   END DO
+                      DO ji = nlci+1, jpi   ;   pv_r3d(ji    , : , :) = pv_r3d(nlei  , :   , :)   ;   END DO
+                   ENDIF
+                ENDIF
+                !
+             ELSE
+                ! return if istop == nstop is false
+                RETURN
+             ENDIF
+          ELSE
+             ! return if statment idvar > 0 .AND. istop == nstop is false
+             RETURN
+          ENDIF
+          !
+       ELSE        ! read using XIOS. Only if KEY_IOMPUT is defined
+#if defined key_iomput
+!would be good to be able to check which context is active and swap only if current is not restart
+          CALL iom_swap( TRIM(rxios_context) ) 
+          IF( PRESENT(pv_r3d) ) THEN
+             if(lwp) write(numout,*) 'XIOS RST READ (3D): ',trim(cdvar)
+             CALL xios_recv_field( trim(cdvar), pv_r3d)
+             IF(idom /= jpdom_unknown ) then
+                 CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
+             ENDIF
+          ELSEIF( PRESENT(pv_r2d) ) THEN
+             if(lwp) write(numout,*) 'XIOS RST READ (2D): ', trim(cdvar)
+             CALL xios_recv_field( trim(cdvar), pv_r2d)
+             IF(idom /= jpdom_unknown ) THEN
+                 CALL lbc_lnk(pv_r2d,'Z',-999.,'no0')
+             ENDIF
+          ELSEIF( PRESENT(pv_r1d) ) THEN
+             if(lwp) write(numout,*) 'XIOS RST READ (1D): ', trim(cdvar)
+             CALL xios_recv_field( trim(cdvar), pv_r1d)
+          ENDIF
+          CALL iom_swap( TRIM(cxios_context) )
+#else
+          istop = istop + 1 
+          clinfo = 'Can not use XIOS in iom_get_123d, file: '//trim(clname)//', var:'//trim(cdvar)
+#endif
+       ENDIF
+!some final adjustments
+       ! C1D case : always call lbc_lnk to replicate the central value over the whole 3X3 domain
+       IF( lk_c1d .AND. PRESENT(pv_r2d) )   CALL lbc_lnk( pv_r2d,'Z',1. )
+       IF( lk_c1d .AND. PRESENT(pv_r3d) )   CALL lbc_lnk( pv_r3d,'Z',1. )
+
+       !--- Apply scale_factor and offset
+       zscf = iom_file(kiomid)%scf(idvar)      ! scale factor
+       zofs = iom_file(kiomid)%ofs(idvar)      ! offset
+       IF(     PRESENT(pv_r1d) ) THEN
+          IF( zscf /= 1. )   pv_r1d(:) = pv_r1d(:) * zscf 
+          IF( zofs /= 0. )   pv_r1d(:) = pv_r1d(:) + zofs
+       ELSEIF( PRESENT(pv_r2d) ) THEN
+          IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
+          IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
+       ELSEIF( PRESENT(pv_r3d) ) THEN
+          IF( zscf /= 1.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) * zscf
+          IF( zofs /= 0.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) + zofs
+       ENDIF
    END SUBROUTINE iom_get_123d
 
@@ -1262,5 +1500,5 @@
             &    lonvalue_1D=lonvalue, latvalue_1D=latvalue, mask_1D=mask, nvertex=nvertex, bounds_lon_1D=bounds_lon,      &
             &    bounds_lat_1D=bounds_lat, area=area, type='curvilinear')
-     ENDIF
+      ENDIF
       IF ( xios_is_valid_domaingroup(cdid) ) THEN
          CALL xios_set_domaingroup_attr( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
@@ -1378,5 +1616,5 @@
 
 
-   SUBROUTINE set_grid( cdgrd, plon, plat )
+   SUBROUTINE set_grid( cdgrd, plon, plat, lxios )
       !!----------------------------------------------------------------------
       !!                     ***  ROUTINE set_grid  ***
@@ -1391,4 +1629,5 @@
       REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zmask
       INTEGER  :: ni,nj
+      LOGICAL, INTENT(IN) :: lxios
       
       ni=nlei-nldi+1 ; nj=nlej-nldj+1
@@ -1399,5 +1638,5 @@
          &                                     latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /)))  
 
-      IF ( ln_mskland ) THEN
+      IF ( ln_mskland.AND.(.NOT.lxios) ) THEN
          ! mask land points, keep values on coast line -> specific mask for U, V and W points
          SELECT CASE ( cdgrd )
@@ -1439,5 +1678,5 @@
       ! Offset of coordinate representing bottom-left corner
       SELECT CASE ( TRIM(cdgrd) )
-         CASE ('T', 'W')
+         CASE ('T', 'W', 'N')
             icnr = -1 ; jcnr = -1
          CASE ('U')
