Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/DIA/diahsb.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/DIA/diahsb.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/DIA/diahsb.F90	(revision 11405)
@@ -260,4 +260,5 @@
             IF(lwp) WRITE(numout,*) '   dia_hsb_rst : read hsb restart at it= ', kt,' date= ', ndastp
             IF(lwp) WRITE(numout,*)
+            IF(lrxios) CALL iom_swap( TRIM(crxios_context) )
             CALL iom_get( numror, 'frc_v', frc_v, ldxios = lrxios )
             CALL iom_get( numror, 'frc_t', frc_t, ldxios = lrxios )
@@ -276,4 +277,5 @@
                CALL iom_get( numror, jpdom_autoglo, 'ssh_sc_loc_ini', ssh_sc_loc_ini, ldxios = lrxios )
             ENDIF
+            IF(lrxios) CALL iom_swap( TRIM(crxios_context) )
          ELSE
             IF(lwp) WRITE(numout,*)
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/DOM/daymod.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/DOM/daymod.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/DOM/daymod.F90	(revision 11405)
@@ -326,4 +326,5 @@
          IF( iom_varid( numror, 'kt', ldstop = .FALSE. ) > 0 ) THEN
             ! Get Calendar informations
+            IF(lrxios) CALL iom_swap( TRIM(crxios_context) )
             CALL iom_get( numror, 'kt', zkt, ldxios = lrxios )   ! last time-step of previous run
             IF(lwp) THEN
@@ -381,4 +382,5 @@
                ! note this is wrong if time step has changed during run
             ENDIF
+            IF(lrxios) CALL iom_swap( TRIM(cxios_context) )
          ELSE
             ! parameters corresponding to nit000 - 1 (as we start the step loop with a call to day)
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/DOM/domvvl.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/DOM/domvvl.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/DOM/domvvl.F90	(revision 11405)
@@ -806,4 +806,5 @@
          IF( ln_rstart ) THEN                   !* Read the restart file
             CALL rst_read_open                  !  open the restart file if necessary
+            IF(lrxios) CALL iom_swap( TRIM(crxios_context) )
             CALL iom_get( numror, jpdom_autoglo, 'sshn'   , sshn, ldxios = lrxios    )
             !
@@ -881,4 +882,5 @@
             ENDIF
             !
+            IF(lrxios) CALL iom_swap( TRIM(cxios_context) )
          ELSE                                   !* Initialize at "rest"
             !
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/DOM/iscplrst.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/DOM/iscplrst.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/DOM/iscplrst.F90	(revision 11405)
@@ -58,4 +58,5 @@
       !
       !                       ! get restart variable
+      IF(lrxios) CALL iom_swap( TRIM(crxios_context) ) 
       CALL iom_get( numror, jpdom_autoglo, 'tmask'  , ztmask_b, ldxios = lrxios   ) ! need to extrapolate T/S
       CALL iom_get( numror, jpdom_autoglo, 'umask'  , zumask_b, ldxios = lrxios   ) ! need to correct barotropic velocity
@@ -66,4 +67,5 @@
       CALL iom_get( numror, jpdom_autoglo, 'e3v_n'  , ze3v_b(:,:,:), ldxios = lrxios )  ! need to correct barotropic velocity
       CALL iom_get( numror, jpdom_autoglo, 'gdepw_n', zdepw_b(:,:,:), ldxios = lrxios ) ! need to interpol vertical profile (vvl)
+      IF(lrxios) CALL iom_swap( TRIM(cxios_context) )
       !
       CALL iscpl_init()       ! read namelist
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/DYN/dynspg_ts.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/DYN/dynspg_ts.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/DYN/dynspg_ts.F90	(revision 11405)
@@ -1402,4 +1402,5 @@
          !                                   ! ---------------
          IF( ln_rstart .AND. ln_bt_fw .AND. (neuler/=0) ) THEN    !* Read the restart file
+            IF(lrxios) CALL iom_swap( TRIM(crxios_context) )
             CALL iom_get( numror, jpdom_autoglo, 'ub2_b'  , ub2_b  (:,:), ldxios = lrxios )   
             CALL iom_get( numror, jpdom_autoglo, 'vb2_b'  , vb2_b  (:,:), ldxios = lrxios ) 
@@ -1421,4 +1422,5 @@
             ENDIF
 #endif
+            IF(lrxios) CALL iom_swap( TRIM(cxios_context) )
          ELSE                                   !* Start from rest
             IF(lwp) WRITE(numout,*)
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/IOM/in_out_manager.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/IOM/in_out_manager.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/IOM/in_out_manager.F90	(revision 11405)
@@ -175,4 +175,6 @@
    CHARACTER(lc) ::   crxios_context         !: context name used in xios to read restart
    CHARACTER(lc) ::   cwxios_context        !: context name used in xios to write restart file
+   CHARACTER(lc) ::   cbwxios_context  = "iom_bklw"       !: context name used in xios to read interpolation weights in sbc_blk
+   LOGICAL, PARAMETER, PUBLIC :: lxios_blkw    = .TRUE.
 
    !!----------------------------------------------------------------------
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/IOM/iom.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/IOM/iom.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/IOM/iom.F90	(revision 11405)
@@ -1072,8 +1072,8 @@
       llxios = .FALSE.
       if(PRESENT(ldxios)) llxios = ldxios
-      idvar = iom_varid( kiomid, cdvar ) 
       idom = kdom
       !
       IF(.NOT.llxios) THEN
+         idvar = iom_varid( kiomid, cdvar )
          clname = iom_file(kiomid)%name   !   esier to read
          clinfo = '          iom_get_123d, file: '//trim(clname)//', var: '//trim(cdvar)
@@ -1290,11 +1290,22 @@
          ENDIF
          !
+         !--- 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
       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(crxios_context) ) 
          IF( PRESENT(pv_r3d) ) THEN
             pv_r3d(:, :, :) = 0.
-            if(lwp) write(numout,*) 'XIOS RST READ (3D): ',trim(cdvar)
+            if(lwp) write(numout,*) 'XIOS READ (3D): ',trim(cdvar)
             CALL xios_recv_field( trim(cdvar), pv_r3d)
             IF(idom /= jpdom_unknown ) then
@@ -1303,5 +1314,5 @@
          ELSEIF( PRESENT(pv_r2d) ) THEN
             pv_r2d(:, :) = 0.
-            if(lwp) write(numout,*) 'XIOS RST READ (2D): ', trim(cdvar)
+            if(lwp) write(numout,*) 'XIOS READ (2D): ', trim(cdvar)
             CALL xios_recv_field( trim(cdvar), pv_r2d)
             IF(idom /= jpdom_unknown ) THEN
@@ -1310,8 +1321,7 @@
          ELSEIF( PRESENT(pv_r1d) ) THEN
             pv_r1d(:) = 0.
-            if(lwp) write(numout,*) 'XIOS RST READ (1D): ', trim(cdvar)
+            if(lwp) write(numout,*) 'XIOS READ (1D): ', trim(cdvar)
             CALL xios_recv_field( trim(cdvar), pv_r1d)
          ENDIF
-         CALL iom_swap( TRIM(cxios_context) )
 #else
          istop = istop + 1 
@@ -1323,18 +1333,4 @@
       IF( lk_c1d .AND. PRESENT(pv_r2d) )   CALL lbc_lnk( 'iom', pv_r2d,'Z',1. )
       IF( lk_c1d .AND. PRESENT(pv_r3d) )   CALL lbc_lnk( 'iom', 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
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/IOM/restart.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/IOM/restart.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/IOM/restart.F90	(revision 11405)
@@ -255,5 +255,7 @@
       ! Check dynamics and tracer time-step consistency and force Euler restart if changed
       IF( iom_varid( numror, 'rdt', ldstop = .FALSE. ) > 0 )   THEN
+         IF(lrxios) CALL iom_swap( TRIM(crxios_context) )
          CALL iom_get( numror, 'rdt', zrdt, ldxios = lrxios )
+         IF(lrxios) CALL iom_swap( TRIM(cxios_context) )
          IF( zrdt /= rdt )   neuler = 0
       ENDIF
@@ -262,4 +264,5 @@
       
       ! Diurnal DSST 
+      IF(lrxios) CALL iom_swap( TRIM(crxios_context) )
       IF( ln_diurnal ) CALL iom_get( numror, jpdom_autoglo, 'Dsst' , x_dsst, ldxios = lrxios ) 
       IF ( ln_diurnal_only ) THEN 
@@ -292,4 +295,5 @@
          CALL eos( tsn, rhd, rhop, gdept_n(:,:,:) )   
       ENDIF
+      IF(lrxios) CALL iom_swap( TRIM(cxios_context) )
       !
       IF( neuler == 0 ) THEN                                  ! Euler restart (neuler=0)
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/fldread.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/fldread.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/fldread.F90	(revision 11405)
@@ -647,5 +647,5 @@
          ENDIF        
       ELSE IF( LEN(TRIM(sdjf%wgtname)) > 0 ) THEN
-         CALL wgt_list( sdjf, iw )
+         CALL wgt_list( sdjf, iw, lxios_blkw )
          IF( sdjf%ln_tint ) THEN   ;   CALL fld_interp( sdjf%num, sdjf%clvar, iw, ipk, sdjf%fdta(:,:,:,2),          & 
             &                                                                          sdjf%nrec_a(1), sdjf%lsmname )
@@ -1345,5 +1345,5 @@
 
 
-   SUBROUTINE wgt_list( sd, kwgt )
+   SUBROUTINE wgt_list( sd, kwgt, ldxios )
       !!---------------------------------------------------------------------
       !!                    ***  ROUTINE wgt_list  ***
@@ -1355,7 +1355,11 @@
       TYPE( FLD ), INTENT(in   ) ::   sd        ! field with name of weights file
       INTEGER    , INTENT(inout) ::   kwgt      ! index of weights
+      LOGICAL    , INTENT(in), OPTIONAL :: ldxios
       !
       INTEGER ::   kw, nestid   ! local integer
       LOGICAL ::   found        ! local logical
+      LOGICAL ::   luxios
+      luxios = .FALSE.
+      if(PRESENT(ldxios)) luxios = ldxios
       !!----------------------------------------------------------------------
       !
@@ -1381,5 +1385,5 @@
       IF( .NOT.found ) THEN
          kwgt = nxt_wgt
-         CALL fld_weight( sd )
+         CALL fld_weight( sd, luxios )
       ENDIF
       !
@@ -1416,5 +1420,5 @@
 
 
-   SUBROUTINE fld_weight( sd )
+   SUBROUTINE fld_weight( sd, ldxios )
       !!---------------------------------------------------------------------
       !!                    ***  ROUTINE fld_weight  ***
@@ -1424,4 +1428,5 @@
       !!----------------------------------------------------------------------
       TYPE( FLD ), INTENT(in) ::   sd   ! field with name of weights file
+      LOGICAL, INTENT(in), OPTIONAL :: ldxios
       !!
       INTEGER ::   jn         ! dummy loop indices
@@ -1435,5 +1440,8 @@
       INTEGER,  DIMENSION(jpi,jpj) ::   data_src
       REAL(wp), DIMENSION(jpi,jpj) ::   data_tmp
+      LOGICAL                      ::   luxios
       !!----------------------------------------------------------------------
+      luxios = .FALSE.
+      IF(PRESENT(ldxios)) luxios = ldxios
       !
       IF( nxt_wgt > tot_wgts ) THEN
@@ -1464,5 +1472,7 @@
       CALL iom_open ( sd%wgtname, inum )   ! interpolation weights
       IF ( inum > 0 ) THEN
-
+         IF(luxios) THEN 
+            CALL iom_swap(cbwxios_context)
+         ENDIF
          !! determine whether we have an east-west cyclic grid
          !! from global attribute called "ew_wrap" in the weights file
@@ -1511,5 +1521,9 @@
             WRITE(aname,'(a3,i2.2)') 'src',jn
             data_tmp(:,:) = 0
-            CALL iom_get ( inum, jpdom_data, aname, data_tmp(:,:) )
+            IF(luxios) THEN
+               CALL iom_get ( inum, jpdom_data, aname//TRIM(sd%wgtname), data_tmp(:,:), ldxios = luxios )
+            ELSE
+               CALL iom_get ( inum, jpdom_data, aname, data_tmp(:,:) )
+            ENDIF
             data_src(:,:) = INT(data_tmp(:,:))
             ref_wgts(nxt_wgt)%data_jpj(:,:,jn) = 1 + (data_src(:,:)-1) / ref_wgts(nxt_wgt)%ddims(1)
@@ -1521,5 +1535,9 @@
             WRITE(aname,'(a3,i2.2)') 'wgt',jn
             ref_wgts(nxt_wgt)%data_wgt(:,:,jn) = 0.0
-            CALL iom_get ( inum, jpdom_data, aname, ref_wgts(nxt_wgt)%data_wgt(:,:,jn) )
+            IF(luxios) THEN
+               CALL iom_get ( inum, jpdom_data, aname//TRIM(sd%wgtname), ref_wgts(nxt_wgt)%data_wgt(:,:,jn), ldxios = luxios )
+            ELSE
+               CALL iom_get ( inum, jpdom_data, aname, ref_wgts(nxt_wgt)%data_wgt(:,:,jn) )
+            ENDIF
          END DO
          CALL iom_close (inum)
@@ -1548,4 +1566,5 @@
          nxt_wgt = nxt_wgt + 1
          !
+         IF(luxios) CALL iom_swap(cxios_context)
       ELSE 
          CALL ctl_stop( '    fld_weight : unable to read the file ' )
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/sbcapr.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/sbcapr.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/sbcapr.F90	(revision 11405)
@@ -156,5 +156,7 @@
          IF( ln_rstart .AND. iom_varid( numror, 'ssh_ibb', ldstop = .FALSE. ) > 0 ) THEN 
             IF(lwp) WRITE(numout,*) 'sbc_apr:   ssh_ibb read in the restart file'
+            IF(lrxios) CALL iom_swap( TRIM(crxios_context) )
             CALL iom_get( numror, jpdom_autoglo, 'ssh_ibb', ssh_ibb, ldxios = lrxios )   ! before inv. barometer ssh
+            IF(lrxios) CALL iom_swap( TRIM(cxios_context) )
             !
          ELSE                                         !* no restart: set from nit000 values
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/sbcblk.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/sbcblk.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/sbcblk.F90	(revision 11405)
@@ -280,4 +280,10 @@
          !
       ENDIF
+
+      IF(lxios_blkw) THEN
+         CALL iom_blk_wgt_init( cbwxios_context )
+         CALL iom_swap( cxios_context )
+      ENDIF
+      
       !
    END SUBROUTINE sbc_blk_init
@@ -681,4 +687,163 @@
       !
    END FUNCTION L_vap
+
+   SUBROUTINE iom_blk_wgt_init( cdname, ld_tmppatch ) 
+#if defined key_iomput
+      use xios
+#endif
+      !!----------------------------------------------------------------------
+      !!                     ***  ROUTINE   ***
+      !!
+      !! ** Purpose :   initialize context for reading weights for surface
+      !!                forcing
+      !!
+      !!----------------------------------------------------------------------
+      CHARACTER(len=*),           INTENT(in)  :: cdname
+      LOGICAL         , OPTIONAL, INTENT(in)  :: ld_tmppatch
+#if defined key_iomput
+      !
+      INTEGER, PARAMETER :: lcname = 26
+      CHARACTER(len=lc) :: clname
+      INTEGER           :: ji, jkmin
+      LOGICAL ::   ll_tmppatch = .TRUE.    !: seb: patch before we remove periodicity
+      INTEGER ::   nldi_save, nlei_save    !:      and close boundaries in output files
+      INTEGER ::   nldj_save, nlej_save    !:
+      LOGICAL ::   ll_global = .FALSE.     !: do we have variable on model grid
+      CHARACTER(len=lc), DIMENSION( jpts) :: cg_name(jpts)
+      CHARACTER(len=1), DIMENSION(lcname) :: cname
+      CHARACTER(len=lc)               :: cfname ! file name without .nc
+      TYPE(xios_duration)               :: dtime    = xios_duration(0, 0, 0, 0, 0, 0), &
+                                           outp_frq = xios_duration(0, 0, 0, 0, 0, 0)
+      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
+      TYPE(xios_file)                   :: file_hdl
+      TYPE(xios_filegroup)              :: filegroup_hdl
+      TYPE(xios_field)                  :: field_hdl
+      INTEGER                           :: jf, ni, nj, ipos, jfld
+      INTEGER, DIMENSION(3)             :: ndims         ! size of each dimension. this is surface 
+                                                         ! forcing, not more than 3: (x, y, t)
+      INTEGER                           :: ndim          ! number if dimensions 
+      INTEGER                           :: inum          ! file id
+      INTEGER                           :: ivid          ! varable id in netcdf file
+      CHARACTER (len=5)                 :: aname         !name of the field in wght file
+      INTEGER                           :: numwgt        !
+      LOGICAL                           :: lfound
+      INTEGER                           :: jg, id, jn
+      
+
+      cname(:)= (/'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', &
+                  'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', &
+                  'u', 'v', 'w', 'x', 'y', 'z'/)
+      !!----------------------------------------------------------------------
+      !
+      ! seb: patch before we remove periodicity and close boundaries in output files
+      IF ( ll_tmppatch ) THEN
+         nldi_save = nldi   ;   nlei_save = nlei
+         nldj_save = nldj   ;   nlej_save = nlej
+         IF( nimpp           ==      1 ) nldi = 1
+         IF( nimpp + jpi - 1 == jpiglo ) nlei = jpi
+         IF( njmpp           ==      1 ) nldj = 1
+         IF( njmpp + jpj - 1 == jpjglo ) nlej = jpj
+      ENDIF
+      !
+      jfld = SIZE(sf)
+
+      IF(jfld > lcname) THEN
+      STOP 'lcname number is too small'
+      ENDIF
+
+      clname = cdname
+      IF( TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(cdname)
+      CALL xios_context_initialize(TRIM(clname), mpi_comm_oce)
+      CALL iom_swap( cdname )
+      ! 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(1948, 01, 01, 00, 00, 00), &
+          &                                    start_date = xios_date(nyear, nmonth, nday,00,00,00) )
+      CASE ( 0)   ; CALL xios_define_calendar( TYPE = "NoLeap"   , time_origin = xios_date(1948, 01, 01, 00, 00, 00), &
+          &                                    start_date = xios_date(nyear, nmonth, nday,00,00,00) )
+      CASE (30)   ; CALL xios_define_calendar( TYPE = "D360"     , time_origin = xios_date(1948, 01, 01, 00, 00, 00), &
+          &                                    start_date = xios_date(nyear, nmonth, nday, 00, 00, 00) )
+      END SELECT
+
+
+      dtime%month = 1
+      CALL xios_set_timestep( dtime )
+
+      CALL xios_get_handle("domain_definition",domaingroup_hdl)
+      CALL xios_add_child(domaingroup_hdl, domain_hdl, "grid_global")
+      ni = nlei-nldi+1
+      nj = nlej-nldj+1
+      CALL xios_set_domain_attr("grid_global", ni_glo=jpiglo, nj_glo=jpjglo, ibegin=nimpp+nldi-2, jbegin=njmpp+nldj-2, ni=ni, nj=nj)
+      CALL xios_set_domain_attr("grid_global", data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
+      CALL xios_set_domain_attr("grid_global", type='curvilinear')
+
+      CALL xios_get_handle("file_definition", filegroup_hdl )
+
+      DO jf = 1, jfld
+!do we have weights file defined already?
+         lfound = .FALSE.
+         DO jg = 1, jf-1
+            IF(TRIM(sf(jf)%wgtname) == TRIM(sf(jg)%wgtname)) THEN
+               IF(lwp) write(numout, *) TRIM(sf(jf)%wgtname),' already defined, skipping '
+               lfound = .TRUE.
+               exit
+            ENDIF
+         ENDDO
+         IF(lfound) cycle
+!add next weights file
+         ipos = index(sf(jf)%wgtname,'.nc')
+         cfname(1:lc) = " "
+         IF(ipos > 0) THEN
+          cfname(1:ipos-1) = sf(jf)%wgtname(1:ipos-1)
+         ELSE
+          cfname(1:lc) = sf(jf)%wgtname(1:lc)
+         ENDIF
+
+         CALL xios_add_child(filegroup_hdl, file_hdl, cname(jf))
+         CALL xios_set_file_attr( cname(jf), name=TRIM(cfname),           &
+                type="one_file", par_access="collective", enabled=.TRUE., &
+                output_freq=xios_timestep, mode="read")
+
+         CALL iom_open ( sf(jf)%wgtname, inum )
+         id = iom_varid(inum, 'src05', ldstop=.FALSE.)
+         IF(id <= 0) THEN
+             numwgt = 4
+         ELSE
+             numwgt = 16
+         ENDIF
+         CALL iom_close ( inum )
+!define variables in weights file. Because all files have the same names src01,
+!dst01, ... add filename to the name to uniquely identify variables 
+         aname = ' '
+         DO jn = 1, numwgt
+            WRITE(aname,'(a3,i2.2)') 'src',jn
+            CALL xios_add_child(file_hdl, field_hdl, TRIM(aname)//TRIM(sf(jf)%wgtname))
+            CALL xios_set_attr (field_hdl, enabled = .TRUE., &
+                          name = TRIM(aname), domain_ref="grid_global", &
+                          operation = "instant")
+            CALL xios_add_child(file_hdl, field_hdl, TRIM(aname)//TRIM(sf(jf)%wgtname))
+            CALL xios_set_attr (field_hdl, enabled = .TRUE., &
+                          name = TRIM(aname), domain_ref="grid_global", &
+                          operation = "instant") 
+         ENDDO 
+      ENDDO   
+
+
+      CALL xios_close_context_definition()
+      CALL xios_update_calendar( 0 )
+
+      IF ( ll_tmppatch ) THEN
+         nldi = nldi_save   ;   nlei = nlei_save
+         nldj = nldj_save   ;   nlej = nlej_save
+      ENDIF
+#endif
+      !
+   END SUBROUTINE iom_blk_wgt_init
+
 
 #if defined key_si3
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/sbcisf.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/sbcisf.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/sbcisf.F90	(revision 11405)
@@ -207,7 +207,9 @@
             &   iom_varid( numror, 'fwf_isf_b', ldstop = .FALSE. ) > 0 ) THEN
             IF(lwp) WRITE(numout,*) '          nit000-1 isf tracer content forcing fields read in the restart file'
+            IF(lrxios) CALL iom_swap( TRIM(crxios_context) )
             CALL iom_get( numror, jpdom_autoglo, 'fwf_isf_b', fwfisf_b(:,:)         , ldxios = lrxios )   ! before salt content isf_tsc trend
             CALL iom_get( numror, jpdom_autoglo, 'isf_sc_b' , risf_tsc_b(:,:,jp_sal), ldxios = lrxios )   ! before salt content isf_tsc trend
             CALL iom_get( numror, jpdom_autoglo, 'isf_hc_b' , risf_tsc_b(:,:,jp_tem), ldxios = lrxios )   ! before salt content isf_tsc trend
+            IF(lrxios) CALL iom_swap( TRIM(cxios_context) )
          ELSE
             fwfisf_b(:,:)    = fwfisf(:,:)
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/sbcmod.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/sbcmod.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/sbcmod.F90	(revision 11405)
@@ -496,4 +496,5 @@
             & iom_varid( numror, 'utau_b', ldstop = .FALSE. ) > 0 ) THEN
             IF(lwp) WRITE(numout,*) '          nit000-1 surface forcing fields red in the restart file'
+            IF(lrxios) CALL iom_swap( TRIM(crxios_context) )
             CALL iom_get( numror, jpdom_autoglo, 'utau_b', utau_b, ldxios = lrxios )   ! before i-stress  (U-point)
             CALL iom_get( numror, jpdom_autoglo, 'vtau_b', vtau_b, ldxios = lrxios )   ! before j-stress  (V-point)
@@ -508,4 +509,5 @@
                sfx_b (:,:) = sfx(:,:)
             ENDIF
+            IF(lrxios) CALL iom_swap( TRIM(cxios_context) )
          ELSE                                                   !* no restart: set from nit000 values
             IF(lwp) WRITE(numout,*) '          nit000-1 surface forcing fields set to nit000'
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/sbcrnf.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/sbcrnf.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/sbcrnf.F90	(revision 11405)
@@ -146,8 +146,10 @@
          IF( ln_rstart .AND.    &                               !* Restart: read in restart file
             & iom_varid( numror, 'rnf_b', ldstop = .FALSE. ) > 0 ) THEN
+            IF(lrxios) CALL iom_swap( TRIM(crxios_context) )
             IF(lwp) WRITE(numout,*) '          nit000-1 runoff forcing fields red in the restart file', lrxios
             CALL iom_get( numror, jpdom_autoglo, 'rnf_b', rnf_b, ldxios = lrxios )     ! before runoff
             CALL iom_get( numror, jpdom_autoglo, 'rnf_hc_b', rnf_tsc_b(:,:,jp_tem), ldxios = lrxios )   ! before heat content of runoff
             CALL iom_get( numror, jpdom_autoglo, 'rnf_sc_b', rnf_tsc_b(:,:,jp_sal), ldxios = lrxios )   ! before salinity content of runoff
+            IF(lrxios) CALL iom_swap( TRIM(cxios_context) )
          ELSE                                                   !* no restart: set from nit000 values
             IF(lwp) WRITE(numout,*) '          nit000-1 runoff forcing fields set to nit000'
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/sbcssm.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/sbcssm.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/SBC/sbcssm.F90	(revision 11405)
@@ -209,4 +209,5 @@
          IF( ln_rstart .AND. iom_varid( numror, 'nn_fsbc', ldstop = .FALSE. ) > 0 ) THEN
             l_ssm_mean = .TRUE.
+            IF(lrxios) CALL iom_swap( TRIM(crxios_context) )
             CALL iom_get( numror               , 'nn_fsbc', zf_sbc, ldxios = lrxios )    ! sbc frequency of previous run
             CALL iom_get( numror, jpdom_autoglo, 'ssu_m'  , ssu_m, ldxios = lrxios  )    ! sea surface mean velocity    (U-point)
@@ -222,4 +223,5 @@
                frq_m(:,:) = 1._wp   ! default definition
             ENDIF
+            IF(lrxios) CALL iom_swap( TRIM(cxios_context) )
             !
             IF( zf_sbc /= REAL( nn_fsbc, wp ) ) THEN      ! nn_fsbc has changed between 2 runs
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/TRA/traqsr.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/TRA/traqsr.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/TRA/traqsr.F90	(revision 11405)
@@ -137,5 +137,7 @@
             IF(lwp) WRITE(numout,*) '          nit000-1 qsr tracer content forcing field read in the restart file'
             z1_2 = 0.5_wp
+            IF(lrxios) CALL iom_swap( TRIM(crxios_context) )
             CALL iom_get( numror, jpdom_autoglo, 'qsr_hc_b', qsr_hc_b, ldxios = lrxios )   ! before heat content trend due to Qsr flux
+            IF(lrxios) CALL iom_swap( TRIM(cxios_context) )
          ELSE                                           ! No restart or restart not found: Euler forward time stepping
             z1_2 = 1._wp
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/TRA/trasbc.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/TRA/trasbc.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/TRA/trasbc.F90	(revision 11405)
@@ -110,6 +110,8 @@
             zfact = 0.5_wp
             sbc_tsc(:,:,:) = 0._wp
+            IF(lrxios) CALL iom_swap( TRIM(crxios_context) )
             CALL iom_get( numror, jpdom_autoglo, 'sbc_hc_b', sbc_tsc_b(:,:,jp_tem), ldxios = lrxios )   ! before heat content sbc trend
             CALL iom_get( numror, jpdom_autoglo, 'sbc_sc_b', sbc_tsc_b(:,:,jp_sal), ldxios = lrxios )   ! before salt content sbc trend
+            IF(lrxios) CALL iom_swap( TRIM(cxios_context) )
          ELSE                                   ! No restart or restart not found: Euler forward time stepping
             zfact = 1._wp
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/ZDF/zdfgls.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/ZDF/zdfgls.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/ZDF/zdfgls.F90	(revision 11405)
@@ -1172,8 +1172,10 @@
             !
             IF( MIN( id1, id2, id3, id4 ) > 0 ) THEN        ! all required arrays exist
+               IF(lrxios) CALL iom_swap( TRIM(crxios_context) )
                CALL iom_get( numror, jpdom_autoglo, 'en'    , en    , ldxios = lrxios )
                CALL iom_get( numror, jpdom_autoglo, 'avt_k' , avt_k , ldxios = lrxios )
                CALL iom_get( numror, jpdom_autoglo, 'avm_k' , avm_k , ldxios = lrxios )
                CALL iom_get( numror, jpdom_autoglo, 'hmxl_n', hmxl_n, ldxios = lrxios )
+               IF(lrxios) CALL iom_swap( TRIM(cxios_context) )
             ELSE                        
                IF(lwp) WRITE(numout,*)
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/ZDF/zdfosm.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/ZDF/zdfosm.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/ZDF/zdfosm.F90	(revision 11405)
@@ -1543,4 +1543,5 @@
      !!-----------------------------------------------------------------------------
      IF( TRIM(cdrw) == 'READ'.AND. ln_rstart) THEN
+        IF(lrxios) CALL iom_swap( TRIM(crxios_context) )
         id1 = iom_varid( numror, 'wn'   , ldstop = .FALSE. )
         IF( id1 > 0 ) THEN                       ! 'wn' exists; read
@@ -1561,4 +1562,5 @@
            WRITE(numout,*) ' ===>>>> : previous run without osmosis scheme, hbl computed from stratification'
         END IF
+        IF(lrxios) CALL iom_swap( TRIM(cxios_context) )
      END IF
 
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/ZDF/zdfric.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/ZDF/zdfric.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/ZDF/zdfric.F90	(revision 11405)
@@ -226,6 +226,8 @@
             !
             IF( MIN( id1, id2 ) > 0 ) THEN         ! restart exists => read it
+               IF(lrxios) CALL iom_swap( TRIM(crxios_context) )
                CALL iom_get( numror, jpdom_autoglo, 'avt_k', avt_k, ldxios = lrxios )
                CALL iom_get( numror, jpdom_autoglo, 'avm_k', avm_k, ldxios = lrxios )
+               IF(lrxios) CALL iom_swap( TRIM(cxios_context) )
             ENDIF
          ENDIF
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/ZDF/zdftke.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/ZDF/zdftke.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/ZDF/zdftke.F90	(revision 11405)
@@ -776,8 +776,10 @@
             !
             IF( MIN( id1, id2, id3, id4 ) > 0 ) THEN      ! fields exist
+               IF(lrxios) CALL iom_swap( TRIM(crxios_context) ) 
                CALL iom_get( numror, jpdom_autoglo, 'en'   , en   , ldxios = lrxios )
                CALL iom_get( numror, jpdom_autoglo, 'avt_k', avt_k, ldxios = lrxios )
                CALL iom_get( numror, jpdom_autoglo, 'avm_k', avm_k, ldxios = lrxios )
                CALL iom_get( numror, jpdom_autoglo, 'dissl', dissl, ldxios = lrxios )
+               IF(lrxios) CALL iom_swap( TRIM(cxios_context) )
             ELSE                                          ! start TKE from rest
                IF(lwp) WRITE(numout,*)
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/step.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/step.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/step.F90	(revision 11405)
@@ -323,4 +323,5 @@
                       CALL iom_context_finalize(      cxios_context          ) ! needed for XIOS+AGRIF
                       IF(lrxios) CALL iom_context_finalize(      crxios_context          )
+                      IF(lxios_blkw) CALL iom_context_finalize(  cbwxios_context         )
          IF( ln_crs ) CALL iom_context_finalize( trim(cxios_context)//"_crs" ) ! 
       ENDIF
Index: /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/SAS/daymod.F90
===================================================================
--- /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/SAS/daymod.F90	(revision 11404)
+++ /NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/SAS/daymod.F90	(revision 11405)
@@ -324,4 +324,5 @@
          IF( iom_varid( numror, 'kt', ldstop = .FALSE. ) > 0 ) THEN
             ! Get Calendar informations
+            IF(lrxios) CALL iom_swap( TRIM(crxios_context) )
             CALL iom_get( numror, 'kt', zkt, ldxios = lrxios )   ! last time-step of previous run
             IF(lwp) THEN
@@ -379,4 +380,5 @@
                ! note this is wrong if time step has changed during run
             ENDIF
+            IF(lrxios) CALL iom_swap( TRIM(cxios_context) )
          ELSE
             ! parameters corresponding to nit000 - 1 (as we start the step loop with a call to day)
