 MODULE obcogcm

   !!======================================================================
   !!                   ***  MODULE obcogcm   ***
   !! Open boundary data : read and estimate the data for the open boundaries
   !! 
   !! ** History :
   !!       !  2006    (V. Garnier) Original code
   !!       !  2007-07 (M. Sourisseau) Introduction of substances readding
   !!       !  2009-03 (V. Garnier) Separation ogcm / tide obc
   !!       !  2009-09 (S. Michel, HOCER) constant BC on T,S and substances if l_obc_ogcm=false
   !!       !  2010-05 (F. Vandermeirsch) Reading of barotropic velocities
   !!       !  2011-01 (R. Ramel Alyotech) Ionc library in fortran 90/95, netcdf4 and MPI
   !!       !  2016-12 (B. Thouvenin, A. Fiandrino) integration og key_lagoon
   !!       
   !!======================================================================

#include "toolcpp.h"

   !! * Modules used
   USE parameters
   USE ionc4, ONLY : ionc4_openr,ionc4_read_dimt,ionc4_read_dimi,ionc4_read_dimj,    &
                     ionc4_read_sublxyt,ionc4_read_z,ionc4_read,ionc4_read_time,     &
                     ionc4_read_sublzxyt,ionc4_close
   USE obccombine,   ONLY : l_obc_south, l_obc_north, l_obc_east,l_obc_west,         &
                            obc_ogcm_sshs,obc_ogcm_sshn,obc_ogcm_sshw,obc_ogcm_sshe, &
                            obc_ogcm_uzs,obc_ogcm_vzs,obc_ogcm_us,obc_ogcm_vs,       &
                            obc_ogcm_uzn,obc_ogcm_vzn,obc_ogcm_un,obc_ogcm_vn,       &
                            obc_ogcm_uzw,obc_ogcm_vzw,obc_ogcm_uw,obc_ogcm_vw,       &
                            obc_ogcm_uze,obc_ogcm_vze,obc_ogcm_ue,obc_ogcm_ve,       &
                            l_obc_ogcm_rssh,l_obc_ogcm_rt,l_obc_ogcm_rs,             &
                            l_obc_ogcm_rc,l_obc_ogcm_ruv,l_obc_ogcm_ruvz
   USE comvars2d !
   USE comvars3d !
   USE comvarp2d ! pour avoir temp et sal
#if defined key_siggen || defined key_gencoord
   USE comsiggen,    ONLY : hzutx,hzuty,hc_sig_g,Csu_sig,Csw_sig,b_sig,theta_sig
#endif
#if defined key_substance
   USE comsubstance, ONLY : nv_adv,cini_wat,irk_mod,irk_fil,       &
                            name_var,obc_cv_name,obc_cv_wat_e,     &
                            obc_cv_wat_w,obc_cv_wat_n,obc_cv_wat_s,unit_modif_mudbio_N2dw
#ifdef key_lagoon
   USE comsubstance, ONLY : obc_cv_wat_grau
#endif
#endif
   USE_MPI toolmpi,  ONLY : MASTER,BCAST_MPI,MPI_COMM_MARS

   IMPLICIT NONE
   PRIVATE

   ! * Routine accessibility
   PUBLIC obc_ogcm,obc_tsref ! routines called by step2dxy.F90, step2dyx.F90, step3dxy.F90, step3dyx.F90
#ifdef key_substance
   PUBLIC obc_subs,obc_subsref                       
#endif
#ifdef key_lagoon
   PUBLIC obc_ogcm_lagoon
#endif

   ! * Module variables
   LOGICAL       ,PUBLIC :: l_obc_ogcm_relax  ! .true. if relaxation for T and S along open boundaries
   REAL(KIND=rsh),PUBLIC :: obc_coefrel
   REAL(KIND=rlg)        :: obc_t1,obc_t2,          &  ! time of open boundary fields
                            obc_subs_t1,obc_subs_t2
   REAL(KIND=rsh),DIMENSION(imin:imax,obc_width) :: xe1n,xe2n,xe1s,xe2s
   REAL(KIND=rsh),DIMENSION(obc_width,jmin:jmax) :: xe1e,xe2e,xe1w,xe2w
   REAL(KIND=rsh),DIMENSION(imin:imax,jmin  :jmin+obc_width) :: u1_s,u2_s,v1_s,v2_s
   REAL(KIND=rsh),DIMENSION(imin:imax,jmin+1:jmin+obc_width) :: u1_n,u2_n,v1_n,v2_n   
   REAL(KIND=rsh),DIMENSION(imin+1:imin+obc_width,jmin:jmax) :: u1_e,u2_e,v1_e,v2_e        
   REAL(KIND=rsh),DIMENSION(imin  :imin+obc_width,jmin:jmax) :: u1_w,u2_w,v1_w,v2_w 

   REAL(KIND=rsh),DIMENSION(kmax,imin:imax,jmin+1:jmin+obc_width) :: &
          temp1_s,temp2_s,         &   ! temperature at the southern boundary (time 1 & 2)
          sal1_s,sal2_s                ! salinity    
   REAL(KIND=rsh),DIMENSION(kmax,imin:imax,jmin  :jmin+obc_width) :: &
          uz1_s,uz2_s,             &   ! total velocities at the southern boundary
          vz1_s,vz2_s                  ! (time 1 & 2)
   REAL(KIND=rsh),PUBLIC,ALLOCATABLE,DIMENSION(:,:,:) :: &
          obc_ts,obc_ss                ! southern open boundary fields (3D)

   REAL(KIND=rsh),DIMENSION(kmax,imin+1:imin+obc_width,jmin:jmax) :: &
          temp1_w,temp2_w,         &   ! temperature at the western boundary (time 1 & 2)
          sal1_w,sal2_w                ! salinity   
   REAL(KIND=rsh),DIMENSION(kmax,imin  :imin+obc_width,jmin:jmax) :: &
          uz1_w,uz2_w,             &   ! total velocities at the western boundary
          vz1_w,vz2_w                  ! (time 1 & 2)
   REAL(KIND=rsh),PUBLIC,ALLOCATABLE,DIMENSION(:,:,:) :: &
          obc_tw,obc_sw                ! western open boundary fields (3D)

   REAL(KIND=rsh),DIMENSION(kmax,imin:imax,jmin+1:jmin+obc_width) :: &
          temp1_n,temp2_n,         &   ! temperature at the northern boundary (time 1 & 2)
          sal1_n,sal2_n,           &   ! salinity  
          uz1_n,uz2_n,             &   ! total velocities at the northern boundary
          vz1_n,vz2_n                  ! (time 1 & 2)
   REAL(KIND=rsh),PUBLIC,ALLOCATABLE,DIMENSION(:,:,:) :: &
          obc_tn,obc_sn                ! northern open boundary fields (3D)

   REAL(KIND=rsh),DIMENSION(kmax,imin+1:imin+obc_width,jmin:jmax) :: &
          temp1_e,temp2_e,         &   ! temperature at the eastern boundary (time 1 & 2)
          sal1_e,sal2_e,           &   ! salinity   
          uz1_e,uz2_e,             &   ! total velocities at the eastern boundary
          vz1_e,vz2_e                  ! (time 1 & 2)
   REAL(KIND=rsh),PUBLIC,ALLOCATABLE,DIMENSION(:,:,:) :: &
          obc_te,obc_se                ! eastern open boundary fields (3D)

#ifdef key_lagoon
   REAL(kind=rsh),DIMENSION(:),allocatable        :: cl_sal,cl_temp
#endif

#if defined key_substance
   REAL(KIND=rsh),DIMENSION(nb_var,kmax,imin:imax,jmin+1:jmin+obc_width) :: &
          cv_wat1_s,cv_wat2_s            ! tracer at the southern boundary (time 1 & 2)
   REAL(KIND=rsh),DIMENSION(nb_var,kmax,imin:imax,jmin+1:jmin+obc_width) :: &
          cv_wat1_n,cv_wat2_n            ! tracer at the northern boundary (time 1 & 2)
   REAL(KIND=rsh),DIMENSION(nb_var,kmax,imin+1:imin+obc_width,jmin:jmax) :: &
          cv_wat1_e,cv_wat2_e            ! tracer at the eastern boundary (time 1 & 2)
   REAL(KIND=rsh),DIMENSION(nb_var,kmax,imin+1:imin+obc_width,jmin:jmax) :: &
          cv_wat1_w,cv_wat2_w            ! tracer at the western boundary (time 1 & 2)
#endif

 CONTAINS

   !!======================================================================
#ifdef key_lagoon
  SUBROUTINE obc_ogcm_lagoon

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE obc_ogcm_lagoon  ***
   !&E
   !&E ** Purpose : Read open boundary condition provided by an ogcm model or 3d previous rank
   !&E
   ! ATTENTION ces modifs ne sont pas valables :
   ! - si on tourne en 2D car la subroutine tsobc2dapply n a pas ete modifiee
   ! - si on fait de la relaxation cad si dans paraspec l_obc_ogcm_relax=.true.
   ! - si on a un gradient de S et T suivant la verticale
   !&E ** Description : time interpolated ssh,2d and 3d velocities, salinity,
   !&E                  temperature and substances
   !&E
   !&E ** Called by : initxe.F90,step2dxy.F90,step2dyx.F90,step3dxy.F90,step3dyx.F90
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays :
   !&E
   !&E ** Modified variables : obc_ogcm_sshw,obc_ogcm_uw,obc_ogcm_uzw,obc_ogcm_vw,obc_ogcm_vzw
   !&E                         obc_ogcm_sshe,obc_ogcm_ue,obc_ogcm_uze,obc_ogcm_ve,obc_ogcm_vze
   !&E                         obc_ogcm_sshs,obc_ogcm_us,obc_ogcm_uzs,obc_ogcm_vs,obc_ogcm_vzs
   !&E                         obc_ogcm_sshn,obc_ogcm_un,obc_ogcm_uzn,obc_ogcm_vn,obc_ogcm_vzn
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2006       (V. Garnier) Original code
   !&E       !  2007-07-25 (M. Sourisseau) Introduction of substances readding
   !&E       !  2009-03-30 (V. Garnier) Separation ogcm / tide obc
   !&E       !  2009-09-24 (S. Michel, HOCER) check the consistency of bathy and Z coord.
   !&E       !  2011-11  (V. Garnier) Spatial extension of ssh(liminm2:limaxp2,ljminm2:ljmaxp2)
   !&E       !  2012-02  (V. Garnier) Verifications and reading of the correct record done by the MASTER only
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   !! * Arguments

   !! * Local declarations
   INTEGER               :: i,j,k,kk,eof,idimt,ncerror,nmax,n,nn,kmin
   INTEGER               :: ng,i0,j0
   REAL(KIND=rsh)        :: sal1,sal2,temp1,temp2
   REAL(KIND=rlg)        :: tool_datosec,t1,tint1,tint2,dt1
   CHARACTER(len=lchain) :: rec
   !INTEGER,DIMENSION(5)  :: idmt
   !INTEGER,DIMENSION(4)  :: idmi,idmj
   !INTEGER, SAVE         :: itcl
   !REAL(KIND=rsh)        :: alf
   REAL(KIND=rlg)        :: torigin

   !!----------------------------------------------------------------------
   !! * Executable part

   PRINT_DBG*, 'enter OBC_OGCM_lagoon routine'

   !!!!!!!!!!!!!!!!!!!!!!!!!
   !     Initialisation
   !!!!!!!!!!!!!!!!!!!!!!!!!

   ! READ DATA IF OPEN BOUNDARY EXISTS AND IF DATA REQUIRED

   IF ( l_obc_south.OR.l_obc_north.OR.l_obc_west.OR.l_obc_east) THEN 

     !  si la date origine des conditions aux limites est differente de celle du modele
     ! torigin=tool_datosec('01/01/1900 00:00:00')-tool_datosec('01/01/1994 00:00:00')
     torigin=0.0_rlg

     IF ( nbouc == 1 ) THEN
 
       ALLOCATE(cl_sal(nvalmax))
       ALLOCATE(cl_temp(nvalmax))

       PRINT*,"Cond limite T et S lues dans le fichier ", file_obc_ogcm_ts

       OPEN(29,file = file_obc_ogcm_ts, form='formatted')
       i=1
       READ(29,*)
       READ(29,*)

       READ(29,'(a)',end=999,err=999) rec
       kk=index(rec,'/')-2
       READ(rec(kk+19:),*) sal1,temp1

       tint1 = tool_datosec(rec(kk:kk+18))
       t1=tchrono(i)
       DO WHILE(t1 < tint1.and.i<=nvalmax)
         PRINT*,'le debut de CL Sal Temp est manquant'
         cl_sal(i) = sal1
         cl_temp(i)= temp1
         i=i+1
         if(i<=nvalmax) t1=tchrono(i)
       END DO

       IF(i<nvalmax) THEN
         eof=0
         DO WHILE(eof==0)

           READ(29,'(a)',iostat=eof) rec
           IF(eof==0) THEN
             kk=index(rec,'/')-2
             READ(rec(kk+19:),*) sal2,temp2

             IF(sal2.gt.0.) THEN
               tint2 = tool_datosec(rec(kk:kk+18))

               DO WHILE ((tchrono(i)-tint1)*(tchrono(i)-tint2) <= 0.d0)
                 dt1=(tchrono(i)-tint1)/(tint2-tint1)
                 cl_sal(i) = sal1*(1.d0-dt1)+sal2*dt1
                 cl_temp(i) = temp1*(1.d0-dt1)+temp2*dt1
                 i=i+1
                 IF(i > nvalmax) GOTO 999
               END DO

               tint1 = tint2
               sal1 = sal2
               temp1 = temp2
             END IF

           END IF
         END DO
       END IF

999    CONTINUE
       IF(i < nvalmax) THEN
         WRITE(iscreenlog,*) ' '
         WRITE(iscreenlog,*) 'la totalite de la simulation n est pas couverte'
         WRITE(iscreenlog,*) 'par les donnees de CL en Sal temp dans le grau  '
         WRITE(iscreenlog,*) 'contenues dans le fichier : ',trim(file_obc_ogcm_ts)
       END IF

       CLOSE(29)

     END IF ! ends initialization (nbouc = 1)

     !************************************************************
     !     deroulement normal
     !************************************************************

     dt1=(t-tchrono(indchrono))/(tchrono(indchrono+1)-tchrono(indchrono))

     DO ng=1,nbgrau
           obc_sgrau(ng)=dt1*cl_sal(indchrono+1)+(1.d0-dt1)*cl_sal(indchrono)
           obc_tgrau(ng)=dt1*cl_temp(indchrono+1)+(1.d0-dt1)*cl_temp(indchrono)
     END DO


   END IF ! end test on obc reading requirement

   PRINT_DBG*, 'exit OBC_OGCM_lagoon routine'

  END SUBROUTINE obc_ogcm_lagoon
#endif

   !!======================================================================

  SUBROUTINE obc_ogcm

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE obc_ogcm  ***
   !&E
   !&E ** Purpose : Read open boundary condition provided by an ogcm model or 3d previous rank
   !&E
   !&E ** Description : time interpolated ssh,2d and 3d velocities, salinity,
   !&E                  temperature and substances
   !&E
   !&E ** Called by : initxe.F90,step2dxy.F90,step2dyx.F90,step3dxy.F90,step3dyx.F90
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays :
   !&E
   !&E ** Modified variables : obc_ogcm_sshw,obc_ogcm_uw,obc_ogcm_uzw,obc_ogcm_vw,obc_ogcm_vzw
   !&E                         obc_ogcm_sshe,obc_ogcm_ue,obc_ogcm_uze,obc_ogcm_ve,obc_ogcm_vze
   !&E                         obc_ogcm_sshs,obc_ogcm_us,obc_ogcm_uzs,obc_ogcm_vs,obc_ogcm_vzs
   !&E                         obc_ogcm_sshn,obc_ogcm_un,obc_ogcm_uzn,obc_ogcm_vn,obc_ogcm_vzn
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2006       (V. Garnier) Original code
   !&E       !  2007-07-25 (M. Sourisseau) Introduction of substances readding
   !&E       !  2009-03-30 (V. Garnier) Separation ogcm / tide obc
   !&E       !  2009-09-24 (S. Michel, HOCER) check the consistency of bathy and Z coord.
   !&E       !  2011-11  (V. Garnier) Spatial extension of ssh(liminm2:limaxp2,ljminm2:ljmaxp2)
   !&E       !  2012-02  (V. Garnier) Verifications and reading of the correct record done by the MASTER only
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   !! * Arguments

   !! * Local declarations
   INTEGER               :: i,j,k,idimt,ncerror,nmax,n,nn,kmin
   INTEGER,DIMENSION(5)  :: idmt
   INTEGER,DIMENSION(4)  :: idmi,idmj
   INTEGER, SAVE         :: itcl
   REAL(KIND=rsh)        :: alf
   REAL(KIND=rlg)        :: torigin
   REAL(kind=riosh),DIMENSION(imin:imax,obc_width) :: xtmp_ns
   REAL(kind=riosh),DIMENSION(obc_width,jmin:jmax) :: xtmp_ew
   REAL(kind=riosh),DIMENSION(imin:imax,jmin:jmin+obc_width) :: uvtmp_ns
   REAL(kind=riosh),DIMENSION(imin:imin+obc_width,jmin:jmax) :: uvtmp_ew   
   REAL(kind=riosh),DIMENSION(kmax,imin:imax,jmin:jmin+obc_width) :: tmp_ns
   REAL(kind=riosh),DIMENSION(kmax,imin:imin+obc_width,jmin:jmax) :: tmp_ew
   REAL(kind=riosh),DIMENSION(imin:imax,jmin:jmax) :: xyref
   REAL(kind=riosh),DIMENSION(0:kmax+1) :: sigref,sigtmp
   REAL(kind=riosh)      :: valref,valtmp
   CHARACTER(LEN=19)     :: tool_sectodat
!real(kind=rlg)        :: tool_datosec
   CHARACTER(LEN=lchain) :: namvar,namfile

   !!----------------------------------------------------------------------
   !! * Executable part

   PRINT_DBG*, 'enter OBC_OGCM routine'

   !!!!!!!!!!!!!!!!!!!!!!!!!
   !     Initialisation
   !!!!!!!!!!!!!!!!!!!!!!!!!

   ! READ DATA IF OPEN BOUNDARY EXISTS AND IF DATA REQUIRED

   IF ( (l_obc_south.OR.l_obc_north.OR.l_obc_west.OR.l_obc_east) .AND. &
        (l_obc_ogcm_rssh.OR.l_obc_ogcm_rt.OR.l_obc_ogcm_rs.OR.l_obc_ogcm_ruvz) ) THEN

     !  si la date origine des conditions aux limites est differente de celle du modele
     ! torigin=tool_datosec('01/01/1900 00:00:00')-tool_datosec('01/01/1994 00:00:00')
     torigin=0.0_rlg

     IF ( nbouc == 1 ) THEN

       IF_MPI (MASTER) THEN

       idmt(:)=0
       idmi(:)=0
       idmj(:)=0
       IF ( l_obc_south ) THEN
         CALL ionc4_openr(file_obc_ogcm_s,l_out_nc4par=.false.)
         idmt(1)=ionc4_read_dimt(file_obc_ogcm_s)
         CALL ionc4_read_dimi(file_obc_ogcm_s,idmi(1))
         CALL ionc4_read_dimj(file_obc_ogcm_s,idmj(1))
         file_obc_3d=file_obc_ogcm_s
       END IF
       IF ( l_obc_north ) THEN
         CALL ionc4_openr(file_obc_ogcm_n,l_out_nc4par=.false.)
         idmt(2)=ionc4_read_dimt(file_obc_ogcm_n)
         CALL ionc4_read_dimi(file_obc_ogcm_n,idmi(2))
         CALL ionc4_read_dimj(file_obc_ogcm_n,idmj(2))
         file_obc_3d=file_obc_ogcm_n
       END IF
       IF ( l_obc_west ) THEN
         CALL ionc4_openr(file_obc_ogcm_w,l_out_nc4par=.false.)
         idmt(3)=ionc4_read_dimt(file_obc_ogcm_w)
         CALL ionc4_read_dimi(file_obc_ogcm_w,idmi(3))
         CALL ionc4_read_dimj(file_obc_ogcm_w,idmj(3))
         file_obc_3d=file_obc_ogcm_w
       END IF
       IF ( l_obc_east ) THEN
         CALL ionc4_openr(file_obc_ogcm_e,l_out_nc4par=.false.)
         idmt(4)=ionc4_read_dimt(file_obc_ogcm_e)
         CALL ionc4_read_dimi(file_obc_ogcm_e,idmi(4))
         CALL ionc4_read_dimj(file_obc_ogcm_e,idmj(4))
         file_obc_3d=file_obc_ogcm_e
       END IF
       CALL ionc4_openr(file_obc_3d,l_out_nc4par=.false.)

       ! make sure record is long enough
       idmt(5)=MAXVAL(idmt(1:4))
       WHERE(idmt(1:4)==0) idmt(1:4)=idmt(5)
       IF ( l_obc_south .AND. idmt(1) < idmt(5)) &
         WRITE (iwarnlog,*) 'WARNING: In file ',TRIM(file_obc_ogcm_s),&
                            ', the length of the OBC record is too short.'
       IF ( l_obc_north .AND. idmt(2) < idmt(5)) &
         WRITE (iwarnlog,*) 'WARNING: In file ',TRIM(file_obc_ogcm_n),&
                            ', the length of the OBC record is too short.'
       IF ( l_obc_west  .AND. idmt(3) < idmt(5)) &
         WRITE (iwarnlog,*) 'WARNING: In file ',TRIM(file_obc_ogcm_w),&
                            ', the length of the OBC record is too short.'
       IF ( l_obc_east  .AND. idmt(4) < idmt(5)) &
         WRITE (iwarnlog,*) 'WARNING: In file ',TRIM(file_obc_ogcm_e),&
                            ', the length of the OBC record is too short.'

       ! make sure sizes of obc grid are correct
       ncerror=0
       IF( l_obc_south .AND. idmj(1) /= obc_width+1) THEN
         WRITE(ierrorlog,*) 'ERROR: In file ',TRIM(file_obc_ogcm_s)
         WRITE(ierrorlog,*) 'the width of the OBC record is not correct: idmj = ',idmj(1)
         WRITE(ierrorlog,*) 'The width must be equal to: obc_width+1 = ',obc_width+1
         ncerror=ncerror+1
       END IF
       IF( l_obc_south .AND. idmi(1) /= imax+1) THEN
         WRITE(ierrorlog,*) 'ERROR: In file ',TRIM(file_obc_ogcm_s)
         WRITE(ierrorlog,*) 'the width of the OBC record is not correct: idmi = ',idmi(1)
         WRITE(ierrorlog,*) 'The width must be equal to: imax+1 = ',imax+1
         ncerror=ncerror+1
       END IF
       IF ( l_obc_north .AND. idmj(2) /= obc_width+1) THEN
         WRITE(ierrorlog,*) 'ERROR: In file ',TRIM(file_obc_ogcm_n)
         WRITE(ierrorlog,*) 'the width of the OBC record is not correct: idmj = ',idmj(2)
         WRITE(ierrorlog,*) 'The width must be equal to: obc_width+1 = ',obc_width+1
         ncerror=ncerror+1
       END IF
       IF ( l_obc_north .AND. idmi(2) /= imax+1) THEN
         WRITE(ierrorlog,*) 'ERROR: In file ',TRIM(file_obc_ogcm_n)
         WRITE(ierrorlog,*) 'the width of the OBC record is not correct: idmi = ',idmi(2)
         WRITE(ierrorlog,*) 'The width must be equal to: imax+1 = ',imax+1
         ncerror=ncerror+1
       END IF
       IF ( l_obc_west .AND. idmi(3) /= obc_width+1) THEN
         WRITE(ierrorlog,*) 'ERROR: In file ',TRIM(file_obc_ogcm_w)
         WRITE(ierrorlog,*) 'the width of the OBC record is not correct: idmj = ',idmi(3)
         WRITE(ierrorlog,*) 'The width must be equal to: obc_width+1 = ',obc_width+1
         ncerror=ncerror+1
       END IF
       IF ( l_obc_west .AND. idmj(3) /= jmax+1) THEN
         WRITE(ierrorlog,*) 'ERROR: In file ',TRIM(file_obc_ogcm_w)
         WRITE(ierrorlog,*) 'the width of the OBC record is not correct: idmi = ',idmj(3)
         WRITE(ierrorlog,*) 'The width must be equal to: jmax+1 = ',jmax+1
         ncerror=ncerror+1
       END IF
       IF ( l_obc_east .AND. idmi(4) /= obc_width+1) THEN
         WRITE(ierrorlog,*) 'ERROR: In file ',TRIM(file_obc_ogcm_e)
         WRITE(ierrorlog,*) 'the width of the OBC record is not correct: idmj = ',idmi(4)
         WRITE(ierrorlog,*) 'The width must be equal to: obc_width+1 = ',obc_width+1
         ncerror=ncerror+1
       END IF
       IF ( l_obc_east .AND. idmj(4) /= jmax+1) THEN
         WRITE(ierrorlog,*) 'ERROR: In file ',TRIM(file_obc_ogcm_e)
         WRITE(ierrorlog,*) 'the width of the OBC record is not correct: idmi = ',idmj(4)
         WRITE(ierrorlog,*) 'The width must be equal to: jmax+1 = ',jmax+1
         ncerror=ncerror+1
       END IF
       IF (ncerror>0) THEN
         WRITE(ierrorlog,*) 'Simulation stopped'
         STOP
       ENDIF

       ! make sure OBC bathymetry is correct
       nmax=1
#if defined key_siggen || defined key_gencoord
       nmax=2
#endif
       DO n=1,nmax ! loop on the bathymetric variable
         IF (n==1) THEN
           namvar=name_out_h0
           xyref(:,:)=REAL(h0_g(:,:),riosh)
!         ELSEIF (n==2 .AND. l_init_restart) THEN
!           namvar=name_out_hx
!           xyref(:,:)=REAL(hx(:,:),riosh)
!         ELSEIF (n==3.AND. l_init_restart) THEN
!           namvar=name_out_hy
!           xyref(:,:)=REAL(hy(:,:),riosh)
#if defined key_siggen || defined key_gencoord
         ELSEIF (n==2) THEN
           namvar='hc'
           xyref(:,:)=REAL(hc_sig_g(:,:),riosh)
#endif
         ENDIF

         ! Ignore the OB points containing land.
         WHERE (xyref(:,:) <= REAL(-valmanq,riosh)) xyref(:,:)=-rg_valmanq_io

         IF ( l_obc_south ) THEN
           CALL ionc4_read_sublxyt(file_obc_ogcm_s,namvar,xtmp_ns, &
                                   imin,imax,jmin+1,obc_width,1,imin,jmin+1)
           ncerror=0
           DO i=imin+1,imax
             IF (ABS(xtmp_ns(i,jmin+1) - xyref(i,jmin+1)) > PRECISION(0.0_riosh)) THEN
               IF (ncerror==0) THEN
                 WRITE (iwarnlog,*) ' '
                 WRITE (iwarnlog,*) 'WARNING: verification of the bathymetry in the OBC data file:'
                 WRITE (iwarnlog,*) TRIM(file_obc_ogcm_s)
               ENDIF
               WRITE (iwarnlog,*) 'at the location jmin+1 = ',jmin+1,', i = ', i
               WRITE (iwarnlog,*) 'bathy_obc = ',xtmp_ns(i,jmin+1),&
                                  ' /= bathy_simu = ',xyref(i,jmin+1)
               ncerror=ncerror+1
             END IF
           END DO
         END IF

         IF ( l_obc_north ) THEN
           CALL ionc4_read_sublxyt(file_obc_ogcm_n,namvar,xtmp_ns, &
                                   imin,imax,jmin+1,obc_width,1,imin,jmin+1)
           ncerror=0
           DO i=imin+1,imax
             IF (ABS(xtmp_ns(i,obc_width) - xyref(i,jmax)) > PRECISION(0.0_riosh)) THEN
               IF (ncerror==0) THEN
                 WRITE (iwarnlog,*) ' '
                 WRITE (iwarnlog,*) 'WARNING: verification of the bathymetry in the OBC data file:'
                 WRITE (iwarnlog,*) TRIM(file_obc_ogcm_n)
               ENDIF
               WRITE (iwarnlog,*) 'at the location jmax = ',jmax,', i = ', i
               WRITE (iwarnlog,*) 'bathy_obc = ',xtmp_ns(i,obc_width),&
                                  ' /= bathy_simu = ',xyref(i,jmax)
               ncerror=ncerror+1
             END IF
           END DO
         END IF
         IF ( l_obc_west ) THEN
           CALL ionc4_read_sublxyt(file_obc_ogcm_w,namvar,xtmp_ew, &
                                   imin+1,obc_width,jmin,jmax,1,imin+1,jmin)
           ncerror=0
           DO j=jmin+1,jmax
             IF (ABS(xtmp_ew(imin+1,j) - xyref(imin+1,j)) > PRECISION(0.0_riosh)) THEN
               IF (ncerror==0) THEN
                 WRITE (iwarnlog,*) ' '
                 WRITE (iwarnlog,*) 'WARNING: verification of the bathymetry in the OBC data file:'
                 WRITE (iwarnlog,*) TRIM(file_obc_ogcm_w)
               ENDIF
               WRITE (iwarnlog,*) 'at the location imin+1 = ',imin+1,', j = ', j
               WRITE (iwarnlog,*) 'bathy_obc = ',xtmp_ew(imin+1,j),&
                                  ' /= bathy_simu = ',xyref(imin+1,j)
               ncerror=ncerror+1
             END IF
           END DO
         END IF
         IF ( l_obc_east ) THEN
           CALL ionc4_read_sublxyt(file_obc_ogcm_e,namvar,xtmp_ew, &
                                   imin+1,obc_width,jmin,jmax,1,imin+1,jmin)
           ncerror=0
           DO j=jmin+1,jmax
             IF (ABS(xtmp_ew(obc_width,j) - xyref(imax,j)) > PRECISION(0.0_riosh)) THEN
               IF (ncerror==0) THEN
                 WRITE (iwarnlog,*) ' '
                 WRITE (iwarnlog,*) 'WARNING: verification of the bathymetry in the OBC data file:'
                 WRITE (iwarnlog,*) TRIM(file_obc_ogcm_e)
               ENDIF
               WRITE (iwarnlog,*) 'at the location imax = ',imax,', j = ', j
               WRITE (iwarnlog,*) 'bathy_obc = ',xtmp_ew(obc_width,j),&
                                  ' /= bathy_simu = ',xyref(imax,j)
               ncerror=ncerror+1
             END IF
           END DO
         END IF

       ENDDO ! end of loop on the bathymetric variable

       ! make sure the vertical cordinate is correct

       DO nn=1,4 ! loop on the OBC file
         namfile=' '
         IF ((nn==1).AND.(l_obc_north)) namfile=file_obc_ogcm_n
         IF ((nn==2).AND.(l_obc_south)) namfile=file_obc_ogcm_s
         IF ((nn==3).AND.(l_obc_west) ) namfile=file_obc_ogcm_w
         IF ((nn==4).AND.(l_obc_east) ) namfile=file_obc_ogcm_e
         IF (LEN(TRIM(namfile))>1) THEN

         nmax=1
#if defined key_siggen || defined key_gencoord
         nmax=3
#endif
         DO n=1,nmax
           ncerror=0
           IF (n==1) THEN
             namvar='SIG'
             sigref(:)=REAL(sig(:),riosh)
             kmin=1
#if defined key_siggen || defined key_gencoord
           ELSEIF (n==2) THEN
             namvar='Csu_sig'
             sigref(:)=REAL(Csu_sig(:),riosh)
             kmin=1
           ELSEIF (n==3) THEN
             namvar='Csw_sig'
             sigref(:)=REAL(Csw_sig(:),riosh)
             kmin=0
#endif
           ENDIF
           CALL ionc4_read_z(namfile,namvar,sigtmp(kmin:kmax),kmin,kmax)
           DO k=1,kmax
             IF (ABS(sigtmp(k)-sigref(k))>PRECISION(0.0_riosh)) ncerror=ncerror+1
           ENDDO
           IF (ncerror>0) THEN
             WRITE(iwarnlog,*) ' '
             WRITE(iwarnlog,*) 'ERROR: OBC_OGCM.F90 subroutine'
             WRITE(iwarnlog,*) 'The vertical coordinate "',TRIM(namvar),&
                               '" is not consistent with the coordinate'
             WRITE(iwarnlog,*) 'read from the OBC data file: ',TRIM(namfile)
             WRITE(iwarnlog,*) TRIM(namvar),'_simu = ',sigref(:)
             WRITE(iwarnlog,*) TRIM(namvar),'_obc  = ',sigtmp(:)
           ENDIF
         ENDDO

#if defined key_siggen || defined key_gencoord
         DO n=1,2
           IF (n==1) THEN
             namvar='b'
             valref=REAL(b_sig,riosh)
           ELSEIF (n==2) THEN
             namvar='theta'
             valref=REAL(theta_sig,riosh)
           ENDIF
           CALL ionc4_read(namfile,namvar,valtmp)
           IF (valtmp/=valref) THEN
             !ncerror=ncerror+1
             WRITE(iwarnlog,*) ' '
             WRITE(iwarnlog,*) 'ERROR: OBC_OGCM.F90 subroutine'
             WRITE(iwarnlog,*) 'The vertical parameter "',TRIM(namvar),&
                                '" is not consistent with the parameter'
             WRITE(iwarnlog,*) 'read from the OBC data file:',TRIM(namfile)
             WRITE(iwarnlog,*) TRIM(namvar),'_simu = ',valref
             WRITE(iwarnlog,*) TRIM(namvar),'_obc  = ',valtmp
           ENDIF
         ENDDO
#endif
         ENDIF
       ENDDO ! end of loop on the OBC file

       IF (ncerror>0) THEN
         WRITE(iwarnlog,*) 'Simulation should be stopped.'
!         STOP
       ENDIF

       ! Find dates to be read

       idimt=ionc4_read_dimt(file_obc_3d)
       itcl=2
       CALL ionc4_read_time(file_obc_3d,itcl-1,obc_t1)
       CALL ionc4_read_time(file_obc_3d,itcl  ,obc_t2)
       DO WHILE((t-(obc_t1-torigin))*(t-(obc_t2-torigin)) > 0.0_rlg .AND. itcl < idimt )
         itcl=itcl+1
         CALL ionc4_read_time(file_obc_3d,itcl-1,obc_t1)
         CALL ionc4_read_time(file_obc_3d,itcl  ,obc_t2)
       END DO
       IF (itcl==idimt .AND. t>=(obc_t2-torigin)) THEN
         WRITE(ierrorlog,*) 'ERROR : In file ',TRIM(file_obc_3d)
         WRITE(ierrorlog,*) 'the OBC dates do not correspond to the simulation period.'
         WRITE(ierrorlog,*) 'The open boundary conditions are available'
         WRITE(ierrorlog,*) 'from ',tool_sectodat(obc_t1),' to ',tool_sectodat(obc_t2)
         WRITE(ierrorlog,*) 'itcl =',itcl
         WRITE(ierrorlog,*) 'Simulation stopped'
         STOP
       END IF
       WRITE(iscreenlog,*) ' '
       !MD 5/03/18
       WRITE(iscreenlog,*) 'In file ',TRIM(file_obc_3d)
       !END MD 
       WRITE(iscreenlog,*) 'Routine OBC_OGCM.F90: 1st open boundary conditions'
       WRITE(iscreenlog,*) 'available at date: ',tool_sectodat(obc_t1)

       ! close opened file
       CALL ionc4_close(file_obc_ogcm_s)
       CALL ionc4_close(file_obc_ogcm_n)
       CALL ionc4_close(file_obc_ogcm_w)
       CALL ionc4_close(file_obc_ogcm_e)
       CALL ionc4_close(file_obc_3d)

       ENDIF_MPI   ! test on MASTER

!       CALL_MPI MPI_BARRIER(MPI_COMM_MARS,IERR_MPI)

       ! the MASTER sends informations to every cpus
       CALL_MPI BCAST_MPI(itcl)
       CALL_MPI BCAST_MPI(obc_t1)
       CALL_MPI BCAST_MPI(obc_t2)
       ! All the cpus must open the ogcm files
       IF ( l_obc_south ) THEN
         CALL ionc4_openr(file_obc_ogcm_s,l_out_nc4par=.true.)
         file_obc_3d=file_obc_ogcm_s
       END IF
       IF ( l_obc_north ) THEN
         CALL ionc4_openr(file_obc_ogcm_n,l_out_nc4par=.true.)
         file_obc_3d=file_obc_ogcm_n
       END IF
       IF ( l_obc_west ) THEN
         CALL ionc4_openr(file_obc_ogcm_w,l_out_nc4par=.true.)
         file_obc_3d=file_obc_ogcm_w
       END IF
       IF ( l_obc_east ) THEN
         CALL ionc4_openr(file_obc_ogcm_e,l_out_nc4par=.true.)
         file_obc_3d=file_obc_ogcm_e
       END IF
       CALL ionc4_openr(file_obc_3d,l_out_nc4par=.true.)

       ! Read data

       IF ( l_obc_south ) THEN
         IF (l_obc_ogcm_rssh) THEN
           CALL ionc4_read_sublxyt(file_obc_ogcm_s,name_in_xe,xtmp_ns, &
                         imin,imax,jmin+1,obc_width,itcl-1,imin,jmin+1,fillvar=rg_valmanq_io)
           xe1s(:,:)=xtmp_ns(:,:)
           CALL ionc4_read_sublxyt(file_obc_ogcm_s,name_in_xe,xtmp_ns, &
                         imin,imax,jmin+1,obc_width,itcl  ,imin,jmin+1,fillvar=rg_valmanq_io)
           xe2s(:,:)=xtmp_ns(:,:)
         END IF
         IF (l_obc_ogcm_rt) THEN
           CALL ionc4_read_sublzxyt(file_obc_ogcm_s,name_in_temp,tmp_ns, &
                          imin,imax,jmin  ,obc_width,1,kmax,itcl-1,imin,jmin  ,1,fillvar=rg_valmanq_io)
           temp1_s(:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_s,name_in_temp,tmp_ns, &
                          imin,imax,jmin  ,obc_width,1,kmax,itcl  ,imin,jmin  ,1,fillvar=rg_valmanq_io)
           temp2_s(:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)
         END IF
         IF (l_obc_ogcm_rs) THEN
           CALL ionc4_read_sublzxyt(file_obc_ogcm_s,name_in_sal,tmp_ns, &
                          imin,imax,jmin  ,obc_width,1,kmax,itcl-1,imin,jmin  ,1,fillvar=rg_valmanq_io)
           sal1_s(:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_s,name_in_sal,tmp_ns, &
                          imin,imax,jmin  ,obc_width,1,kmax,itcl  ,imin,jmin  ,1,fillvar=rg_valmanq_io)
           sal2_s(:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)
         END IF
         IF (l_obc_ogcm_ruvz) THEN
           CALL ionc4_read_sublzxyt(file_obc_ogcm_s,name_in_uz,tmp_ns, &
                          imin,imax,jmin  ,obc_width,1,kmax,itcl-1,imin,jmin  ,1,fillvar=0.0_riosh)
           uz1_s(:,:,jmin  :jmin+obc_width)=tmp_ns(:,:,jmin  :jmin+obc_width)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_s,name_in_uz,tmp_ns, &
                          imin,imax,jmin  ,obc_width,1,kmax,itcl  ,imin,jmin  ,1,fillvar=0.0_riosh)
           uz2_s(:,:,jmin  :jmin+obc_width)=tmp_ns(:,:,jmin  :jmin+obc_width)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_s,name_in_vz,tmp_ns, &
                          imin,imax,jmin  ,obc_width,1,kmax,itcl-1,imin,jmin  ,1,fillvar=0.0_riosh)
           vz1_s(:,:,jmin  :jmin+obc_width)=tmp_ns(:,:,jmin  :jmin+obc_width)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_s,name_in_vz,tmp_ns, &
                          imin,imax,jmin  ,obc_width,1,kmax,itcl  ,imin,jmin  ,1,fillvar=0.0_riosh)
           vz2_s(:,:,jmin  :jmin+obc_width)=tmp_ns(:,:,jmin  :jmin+obc_width)
         END IF
         IF (l_obc_ogcm_ruv) THEN
           CALL ionc4_read_sublxyt(file_obc_ogcm_s,name_in_u,uvtmp_ns, &
                         imin,imax,jmin  ,obc_width,itcl-1,imin,jmin,fillvar=0.0_riosh)
           u1_s(:,jmin  :jmin+obc_width)=uvtmp_ns(:,jmin  :jmin+obc_width)
           CALL ionc4_read_sublxyt(file_obc_ogcm_s,name_in_u,uvtmp_ns, &
                         imin,imax,jmin  ,obc_width,itcl  ,imin,jmin,fillvar=0.0_riosh)
           u2_s(:,jmin  :jmin+obc_width)=uvtmp_ns(:,jmin  :jmin+obc_width)
           CALL ionc4_read_sublxyt(file_obc_ogcm_s,name_in_v,uvtmp_ns, &
                         imin,imax,jmin  ,obc_width,itcl-1,imin,jmin,fillvar=0.0_riosh)
           v1_s(:,jmin  :jmin+obc_width)=uvtmp_ns(:,jmin  :jmin+obc_width)
           CALL ionc4_read_sublxyt(file_obc_ogcm_s,name_in_v,uvtmp_ns, &
                         imin,imax,jmin  ,obc_width,itcl  ,imin,jmin,fillvar=0.0_riosh)
           v2_s(:,jmin  :jmin+obc_width)=uvtmp_ns(:,jmin  :jmin+obc_width)
         END IF
       END IF

       IF ( l_obc_north ) THEN
         IF (l_obc_ogcm_rssh) THEN
           CALL ionc4_read_sublxyt(file_obc_ogcm_n,name_in_xe,xtmp_ns, &
                         imin,imax,jmin+1,obc_width,itcl-1,imin,jmin+1,fillvar=rg_valmanq_io)
           xe1n(:,:)=xtmp_ns(:,:)
           CALL ionc4_read_sublxyt(file_obc_ogcm_n,name_in_xe,xtmp_ns, &
                         imin,imax,jmin+1,obc_width,itcl  ,imin,jmin+1,fillvar=rg_valmanq_io)
           xe2n(:,:)=xtmp_ns(:,:)
         END IF
         IF (l_obc_ogcm_rt) THEN
           CALL ionc4_read_sublzxyt(file_obc_ogcm_n,name_in_temp,tmp_ns, &
                          imin,imax,jmin  ,obc_width,1,kmax,itcl-1,imin,jmin  ,1,fillvar=rg_valmanq_io)
           temp1_n(:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_n,name_in_temp,tmp_ns, &
                          imin,imax,jmin  ,obc_width,1,kmax,itcl  ,imin,jmin  ,1,fillvar=rg_valmanq_io)
           temp2_n(:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)
         END IF
         IF (l_obc_ogcm_rs) THEN
           CALL ionc4_read_sublzxyt(file_obc_ogcm_n,name_in_sal,tmp_ns, &
                          imin,imax,jmin  ,obc_width,1,kmax,itcl-1,imin,jmin  ,1,fillvar=rg_valmanq_io)
           sal1_n(:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_n,name_in_sal,tmp_ns, &
                          imin,imax,jmin  ,obc_width,1,kmax,itcl  ,imin,jmin  ,1,fillvar=rg_valmanq_io)
           sal2_n(:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)
         END IF
         IF (l_obc_ogcm_ruvz) THEN
           CALL ionc4_read_sublzxyt(file_obc_ogcm_n,name_in_uz,tmp_ns, &
                          imin,imax,jmin  ,obc_width,1,kmax,itcl-1,imin,jmin  ,1,fillvar=0.0_riosh)
           uz1_n(:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_n,name_in_uz,tmp_ns, &
                          imin,imax,jmin  ,obc_width,1,kmax,itcl  ,imin,jmin  ,1,fillvar=0.0_riosh)
           uz2_n(:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_n,name_in_vz,tmp_ns, &
                          imin,imax,jmin  ,obc_width,1,kmax,itcl-1,imin,jmin  ,1,fillvar=0.0_riosh)
           vz1_n(:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_n,name_in_vz,tmp_ns, &
                          imin,imax,jmin  ,obc_width,1,kmax,itcl  ,imin,jmin  ,1,fillvar=0.0_riosh)
           vz2_n(:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)
         END IF
         IF (l_obc_ogcm_ruv) THEN
           CALL ionc4_read_sublxyt(file_obc_ogcm_n,name_in_u,uvtmp_ns, &
                         imin,imax,jmin  ,obc_width,itcl-1,imin,jmin,fillvar=0.0_riosh)
           u1_n(:,jmin+1:jmin+obc_width)=uvtmp_ns(:,jmin+1:jmin+obc_width)
           CALL ionc4_read_sublxyt(file_obc_ogcm_n,name_in_u,uvtmp_ns, &
                         imin,imax,jmin  ,obc_width,itcl  ,imin,jmin,fillvar=0.0_riosh)
           u2_n(:,jmin+1:jmin+obc_width)=uvtmp_ns(:,jmin+1:jmin+obc_width)
           CALL ionc4_read_sublxyt(file_obc_ogcm_n,name_in_v,uvtmp_ns, &
                         imin,imax,jmin  ,obc_width,itcl-1,imin,jmin,fillvar=0.0_riosh)
           v1_n(:,jmin+1:jmin+obc_width)=uvtmp_ns(:,jmin+1:jmin+obc_width)
           CALL ionc4_read_sublxyt(file_obc_ogcm_n,name_in_v,uvtmp_ns, &
                         imin,imax,jmin  ,obc_width,itcl  ,imin,jmin,fillvar=0.0_riosh)
           v2_n(:,jmin+1:jmin+obc_width)=uvtmp_ns(:,jmin+1:jmin+obc_width)
         END IF
       END IF

       IF ( l_obc_west ) THEN
         IF (l_obc_ogcm_rssh) THEN
           CALL ionc4_read_sublxyt(file_obc_ogcm_w,name_in_xe,xtmp_ew, &
                         imin+1,obc_width,jmin,jmax,itcl-1,imin+1,jmin,fillvar=rg_valmanq_io)
           xe1w(31,7:160)=xtmp_ew(31,7:160)
           CALL ionc4_read_sublxyt(file_obc_ogcm_w,name_in_xe,xtmp_ew, &
                         imin+1,obc_width,jmin,jmax,itcl  ,imin+1,jmin,fillvar=rg_valmanq_io)
           xe2w(31,7:160)=xtmp_ew(31,7:160)
         END IF
         IF (l_obc_ogcm_rt) THEN
           CALL ionc4_read_sublzxyt(file_obc_ogcm_w,name_in_temp,tmp_ew,  &
                          imin  ,obc_width,jmin,jmax,1,kmax,itcl-1,imin  ,jmin,1,fillvar=rg_valmanq_io)
           temp1_w(:,31,7:160)=tmp_ew(:,31,7:160)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_w,name_in_temp,tmp_ew,  &
                          imin  ,obc_width,jmin,jmax,1,kmax,itcl  ,imin  ,jmin,1,fillvar=rg_valmanq_io)
           temp2_w(:,31,7:160)=tmp_ew(:,31,7:160)
         END IF
         IF (l_obc_ogcm_rs) THEN
           CALL ionc4_read_sublzxyt(file_obc_ogcm_w,name_in_sal,tmp_ew, &
                          imin  ,obc_width,jmin,jmax,1,kmax,itcl-1,imin  ,jmin,1,fillvar=rg_valmanq_io)
           sal1_w(:,31,7:160)=tmp_ew(:,31,7:160)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_w,name_in_sal,tmp_ew, &
                          imin  ,obc_width,jmin,jmax,1,kmax,itcl  ,imin  ,jmin,1,fillvar=rg_valmanq_io)
           sal2_w(:,31,7:160)=tmp_ew(:,31,7:160)
         END IF
         IF (l_obc_ogcm_ruvz) THEN
           CALL ionc4_read_sublzxyt(file_obc_ogcm_w,name_in_uz,tmp_ew, &
                          imin  ,obc_width,jmin,jmax,1,kmax,itcl-1,imin  ,jmin,1,fillvar=0.0_riosh)
           uz1_w(:,imin  :imin+obc_width,:)=tmp_ew(:,imin  :imin+obc_width,:)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_w,name_in_uz,tmp_ew, &
                          imin  ,obc_width,jmin,jmax,1,kmax,itcl  ,imin  ,jmin,1,fillvar=0.0_riosh)
           uz2_w(:,imin  :imin+obc_width,:)=tmp_ew(:,imin  :imin+obc_width,:)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_w,name_in_vz,tmp_ew, &
                          imin  ,obc_width,jmin,jmax,1,kmax,itcl-1,imin  ,jmin,1,fillvar=0.0_riosh)
           vz1_w(:,imin  :imin+obc_width,:)=tmp_ew(:,imin  :imin+obc_width,:)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_w,name_in_vz,tmp_ew, &
                          imin  ,obc_width,jmin,jmax,1,kmax,itcl  ,imin  ,jmin,1,fillvar=0.0_riosh)
           vz2_w(:,imin  :imin+obc_width,:)=tmp_ew(:,imin  :imin+obc_width,:)
         END IF
         IF (l_obc_ogcm_ruv) THEN           
           CALL ionc4_read_sublxyt(file_obc_ogcm_w,name_in_u,uvtmp_ew, &
                         imin  ,obc_width,jmin,jmax,itcl-1,imin,jmin,fillvar=0.0_riosh)
           u1_w(imin  :imin+obc_width,:)=uvtmp_ew(imin  :imin+obc_width,:)
           CALL ionc4_read_sublxyt(file_obc_ogcm_w,name_in_u,uvtmp_ew, &
                         imin  ,obc_width,jmin,jmax,itcl  ,imin,jmin,fillvar=0.0_riosh)
           u2_w(imin  :imin+obc_width,:)=uvtmp_ew(imin  :imin+obc_width,:)
           CALL ionc4_read_sublxyt(file_obc_ogcm_w,name_in_v,uvtmp_ew, &
                         imin  ,obc_width,jmin,jmax,itcl-1,imin,jmin,fillvar=0.0_riosh)
           v1_w(imin  :imin+obc_width,:)=uvtmp_ew(imin  :imin+obc_width,:)
           CALL ionc4_read_sublxyt(file_obc_ogcm_w,name_in_v,uvtmp_ew, &
                         imin  ,obc_width,jmin,jmax,itcl  ,imin,jmin,fillvar=0.0_riosh)
           v2_w(imin  :imin+obc_width,:)=uvtmp_ew(imin  :imin+obc_width,:)
         END IF
       END IF

       IF ( l_obc_east ) THEN
         IF (l_obc_ogcm_rssh) THEN
           CALL ionc4_read_sublxyt(file_obc_ogcm_e,name_in_xe,xtmp_ew, &
                           imin+1,obc_width,jmin,jmax,itcl-1,imin+1,jmin,fillvar=rg_valmanq_io)
           xe1e(:,:)=xtmp_ew(:,:)
           CALL ionc4_read_sublxyt(file_obc_ogcm_e,name_in_xe,xtmp_ew, &
                           imin+1,obc_width,jmin,jmax,itcl  ,imin+1,jmin,fillvar=rg_valmanq_io)
           xe2e(:,:)=xtmp_ew(:,:)
         END IF
         IF (l_obc_ogcm_rt) THEN
           CALL ionc4_read_sublzxyt(file_obc_ogcm_e,name_in_temp,tmp_ew, &
                           imin  ,obc_width,jmin,jmax,1,kmax,itcl-1,imin  ,jmin,1,fillvar=rg_valmanq_io)
           temp1_e(:,imin+1:imin+obc_width,:)=tmp_ew(:,imin+1:imin+obc_width,:)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_e,name_in_temp,tmp_ew, &
                           imin  ,obc_width,jmin,jmax,1,kmax,itcl  ,imin  ,jmin,1,fillvar=rg_valmanq_io)
           temp2_e(:,imin+1:imin+obc_width,:)=tmp_ew(:,imin+1:imin+obc_width,:)
         END IF
         IF (l_obc_ogcm_rs) THEN
           CALL ionc4_read_sublzxyt(file_obc_ogcm_e,name_in_sal,tmp_ew, &
                           imin  ,obc_width,jmin,jmax,1,kmax,itcl-1,imin  ,jmin,1,fillvar=rg_valmanq_io)
           sal1_e(:,imin+1:imin+obc_width,:)=tmp_ew(:,imin+1:imin+obc_width,:)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_e,name_in_sal,tmp_ew, &
                           imin  ,obc_width,jmin,jmax,1,kmax,itcl  ,imin  ,jmin,1,fillvar=rg_valmanq_io)
           sal2_e(:,imin+1:imin+obc_width,:)=tmp_ew(:,imin+1:imin+obc_width,:)
         END IF
         IF (l_obc_ogcm_ruvz) THEN
           CALL ionc4_read_sublzxyt(file_obc_ogcm_e,name_in_uz,tmp_ew, &
                           imin  ,obc_width,jmin,jmax,1,kmax,itcl-1,imin  ,jmin,1,fillvar=0.0_riosh)
           uz1_e(:,imin+1:imin+obc_width,:)=tmp_ew(:,imin+1:imin+obc_width,:)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_e,name_in_uz,tmp_ew, &
                           imin  ,obc_width,jmin,jmax,1,kmax,itcl  ,imin  ,jmin,1,fillvar=0.0_riosh)
           uz2_e(:,imin+1:imin+obc_width,:)=tmp_ew(:,imin+1:imin+obc_width,:)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_e,name_in_vz,tmp_ew, &
                           imin  ,obc_width,jmin,jmax,1,kmax,itcl-1,imin  ,jmin,1,fillvar=0.0_riosh)
           vz1_e(:,imin+1:imin+obc_width,:)=tmp_ew(:,imin+1:imin+obc_width,:)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_e,name_in_vz,tmp_ew, &
                           imin  ,obc_width,jmin,jmax,1,kmax,itcl  ,imin  ,jmin,1,fillvar=0.0_riosh)
           vz2_e(:,imin+1:imin+obc_width,:)=tmp_ew(:,imin+1:imin+obc_width,:)
         END IF
         IF (l_obc_ogcm_ruv) THEN
           CALL ionc4_read_sublxyt(file_obc_ogcm_e,name_in_u,uvtmp_ew, &
                         imin  ,obc_width,jmin,jmax,itcl-1,imin,jmin,fillvar=0.0_riosh)
           u1_e(imin+1:imin+obc_width,:)=uvtmp_ew(imin+1:imin+obc_width,:)
           CALL ionc4_read_sublxyt(file_obc_ogcm_e,name_in_u,uvtmp_ew, &
                         imin  ,obc_width,jmin,jmax,itcl  ,imin,jmin,fillvar=0.0_riosh)
           u2_e(imin+1:imin+obc_width,:)=uvtmp_ew(imin+1:imin+obc_width,:)
           CALL ionc4_read_sublxyt(file_obc_ogcm_e,name_in_v,uvtmp_ew, &
                         imin  ,obc_width,jmin,jmax,itcl-1,imin,jmin,fillvar=0.0_riosh)
           v1_e(imin+1:imin+obc_width,:)=uvtmp_ew(imin+1:imin+obc_width,:)
           CALL ionc4_read_sublxyt(file_obc_ogcm_e,name_in_v,uvtmp_ew, &
                         imin  ,obc_width,jmin,jmax,itcl  ,imin,jmin,fillvar=0.0_riosh)
           v2_e(imin+1:imin+obc_width,:)=uvtmp_ew(imin+1:imin+obc_width,:)
         END IF
       END IF

     END IF ! ends initialization (nbouc = 1)

     !************************************************************
     !     deroulement normal
     !************************************************************

     IF ( t > obc_t2-torigin ) THEN ! swap of arrays
       obc_t1=obc_t2
       itcl=itcl+1
       CALL ionc4_read_time(file_obc_3d,itcl,obc_t2)
       IF ( l_obc_south ) THEN
         IF (l_obc_ogcm_rssh) THEN
           xe1s(:,jmin+1)=xe2s(:,jmin+1)
           CALL ionc4_read_sublxyt(file_obc_ogcm_s,name_in_xe,xtmp_ns,                &
                          imin,imax,jmin+1,obc_width,itcl,imin,jmin+1,fillvar=rg_valmanq_io)
           xe2s(:,:)=xtmp_ns(:,:)
         END IF
         IF (l_obc_ogcm_rt) THEN
           temp1_s(:,:,jmin+1:jmin+obc_width)=temp2_s(:,:,jmin+1:jmin+obc_width)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_s,name_in_temp,tmp_ns,            &
                           imin,imax,jmin  ,obc_width,1,kmax,itcl,imin,jmin  ,1,fillvar=rg_valmanq_io)
           temp2_s(:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)
         END IF
         IF (l_obc_ogcm_rs) THEN
           sal1_s(:,:,jmin+1:jmin+obc_width)=sal2_s(:,:,jmin+1:jmin+obc_width)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_s,name_in_sal,tmp_ns,              &
                           imin,imax,jmin  ,obc_width,1,kmax,itcl,imin,jmin  ,1,fillvar=rg_valmanq_io)
           sal2_s(:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)
         END IF
         IF (l_obc_ogcm_ruvz) THEN
           uz1_s(:,:,jmin:jmin+obc_width)=uz2_s(:,:,jmin:jmin+obc_width)
           vz1_s(:,:,jmin:jmin+obc_width)=vz2_s(:,:,jmin:jmin+obc_width)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_s,name_in_uz,tmp_ns,               &
                           imin,imax,jmin  ,obc_width,1,kmax,itcl,imin,jmin  ,1,fillvar=0.0_riosh)
           uz2_s(:,:,jmin  :jmin+obc_width)=tmp_ns(:,:,jmin  :jmin+obc_width)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_s,name_in_vz,tmp_ns,               &
                           imin,imax,jmin  ,obc_width,1,kmax,itcl,imin,jmin  ,1,fillvar=0.0_riosh)
           vz2_s(:,:,jmin  :jmin+obc_width)=tmp_ns(:,:,jmin  :jmin+obc_width)
         END IF
         IF (l_obc_ogcm_ruv) THEN 
           u1_s(:,jmin:jmin+obc_width)=u2_s(:,jmin:jmin+obc_width)
           v1_s(:,jmin:jmin+obc_width)=v2_s(:,jmin:jmin+obc_width)
           CALL ionc4_read_sublxyt(file_obc_ogcm_s,name_in_u,uvtmp_ns,               &
                           imin,imax,jmin  ,obc_width,itcl,imin,jmin,fillvar=0.0_riosh)
           u2_s(:,jmin  :jmin+obc_width)=uvtmp_ns(:,jmin  :jmin+obc_width)
           CALL ionc4_read_sublxyt(file_obc_ogcm_s,name_in_v,uvtmp_ns,               &
                           imin,imax,jmin  ,obc_width,itcl,imin,jmin,fillvar=0.0_riosh)
           v2_s(:,jmin  :jmin+obc_width)=uvtmp_ns(:,jmin  :jmin+obc_width)
         END IF
       END IF

       IF ( l_obc_north ) THEN
         IF (l_obc_ogcm_rssh) THEN
           xe1n(:,obc_width)=xe2n(:,obc_width)
           CALL ionc4_read_sublxyt(file_obc_ogcm_n,name_in_xe,xtmp_ns,                &
                          imin,imax,jmin+1,obc_width,itcl,imin,jmin+1,fillvar=rg_valmanq_io)
           xe2n(:,:)=xtmp_ns(:,:)
         END IF
         IF (l_obc_ogcm_rt) THEN
           temp1_n(:,:,jmin+1:jmin+obc_width)=temp2_n(:,:,jmin+1:jmin+obc_width)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_n,name_in_temp,tmp_ns,             &
                           imin,imax,jmin  ,obc_width,1,kmax,itcl,imin,jmin  ,1,fillvar=rg_valmanq_io)
           temp2_n(:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)
         END IF
         IF (l_obc_ogcm_rs) THEN
           sal1_n(:,:,jmin+1:jmin+obc_width)= sal2_n(:,:,jmin+1:jmin+obc_width)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_n,name_in_sal,tmp_ns,               &
                           imin,imax,jmin  ,obc_width,1,kmax,itcl,imin,jmin  ,1,fillvar=rg_valmanq_io)
           sal2_n(:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)
         END IF
         IF (l_obc_ogcm_ruvz) THEN
           uz1_n(:,:,jmin+1:jmin+obc_width)=uz2_n(:,:,jmin+1:jmin+obc_width)
           vz1_n(:,:,jmin+1:jmin+obc_width)=vz2_n(:,:,jmin+1:jmin+obc_width)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_n,name_in_uz,tmp_ns,                &
                           imin,imax,jmin  ,obc_width,1,kmax,itcl,imin,jmin  ,1,fillvar=0.0_riosh)
           uz2_n(:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_n,name_in_vz,tmp_ns,                &
                           imin,imax,jmin  ,obc_width,1,kmax,itcl,imin,jmin  ,1,fillvar=0.0_riosh)
           vz2_n(:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)
         END IF
         IF (l_obc_ogcm_ruv) THEN 
           u1_n(:,jmin+1:jmin+obc_width)=u2_n(:,jmin+1:jmin+obc_width)
           v1_n(:,jmin+1:jmin+obc_width)=v2_n(:,jmin+1:jmin+obc_width)
           CALL ionc4_read_sublxyt(file_obc_ogcm_n,name_in_u,uvtmp_ns, &
                         imin,imax,jmin  ,obc_width,itcl  ,imin,jmin,fillvar=0.0_riosh)
           u2_n(:,jmin+1:jmin+obc_width)=uvtmp_ns(:,jmin+1:jmin+obc_width)
           CALL ionc4_read_sublxyt(file_obc_ogcm_n,name_in_v,uvtmp_ns, &
                         imin,imax,jmin  ,obc_width,itcl  ,imin,jmin,fillvar=0.0_riosh)
           v2_n(:,jmin+1:jmin+obc_width)=uvtmp_ns(:,jmin+1:jmin+obc_width)
         END IF
       END IF

       IF ( l_obc_west ) THEN
         IF (l_obc_ogcm_rssh) THEN
           xe1w(imin+31,7:160)=xe2w(imin+31,7:160)
           CALL ionc4_read_sublxyt(file_obc_ogcm_w,name_in_xe,xtmp_ew,                &
                          imin+1,obc_width,jmin,jmax,itcl,imin+1,jmin,fillvar=rg_valmanq_io)
           xe2w(31,7:160)=xtmp_ew(31,7:160)
         END IF
         IF (l_obc_ogcm_rt) THEN
           temp1_w(:,31,7:160)=temp2_w(:,31,7:160)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_w,name_in_temp,tmp_ew,              &
                           imin  ,obc_width,jmin,jmax,1,kmax,itcl,imin  ,jmin,1,fillvar=rg_valmanq_io)
           temp2_w(:,31,7:160)=tmp_ew(:,31,7:160)
         END IF
         IF (l_obc_ogcm_rs) THEN
           sal1_w(:,31,7:160)=sal2_w(:,31,7:160)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_w,name_in_sal,tmp_ew,               &
                           imin  ,obc_width,jmin,jmax,1,kmax,itcl,imin  ,jmin,1,fillvar=rg_valmanq_io)
           sal2_w(:,31,7:160)=tmp_ew(:,31,7:160)
         END IF
         IF (l_obc_ogcm_ruvz) THEN
           uz1_w(:,imin:imin+obc_width,:)=uz2_w(:,imin:imin+obc_width,:)
           vz1_w(:,imin:imin+obc_width,:)=vz2_w(:,imin:imin+obc_width,:)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_w,name_in_uz,tmp_ew,                &
                           imin  ,obc_width,jmin,jmax,1,kmax,itcl,imin  ,jmin,1,fillvar=0.0_riosh)
           uz2_w(:,imin  :imin+obc_width,:)=tmp_ew(:,imin  :imin+obc_width,:)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_w,name_in_vz,tmp_ew,               &
                           imin  ,obc_width,jmin,jmax,1,kmax,itcl,imin  ,jmin,1,fillvar=0.0_riosh)
           vz2_w(:,imin  :imin+obc_width,:)=tmp_ew(:,imin  :imin+obc_width,:)
         END IF
         IF (l_obc_ogcm_ruv) THEN 
           u1_w(imin:imin+obc_width,:)=u2_w(imin:imin+obc_width,:)
           v1_w(imin:imin+obc_width,:)=v2_w(imin:imin+obc_width,:)
           CALL ionc4_read_sublxyt(file_obc_ogcm_w,name_in_u,uvtmp_ew, &
                         imin  ,obc_width,jmin,jmax,itcl  ,imin,jmin,fillvar=0.0_riosh)
           u2_w(imin:imin+obc_width,:)=uvtmp_ew(imin:imin+obc_width,:)
           CALL ionc4_read_sublxyt(file_obc_ogcm_w,name_in_v,uvtmp_ew, &
                         imin  ,obc_width,jmin,jmax,itcl  ,imin,jmin,fillvar=0.0_riosh)
           v2_w(imin:imin+obc_width,:)=uvtmp_ew(imin:imin+obc_width,:)
         END IF
       END IF

       IF ( l_obc_east ) THEN
         IF (l_obc_ogcm_rssh) THEN
           xe1e(obc_width,:)=xe2e(obc_width,:)
           CALL ionc4_read_sublxyt(file_obc_ogcm_e,name_in_xe,xtmp_ew,                &
                          imin+1,obc_width,jmin,jmax,itcl,imin+1,jmin,fillvar=rg_valmanq_io)
           xe2e(:,:)=xtmp_ew(:,:)
         END IF
         IF (l_obc_ogcm_rt) THEN
           temp1_e(:,imin+1:imin+obc_width,:)=temp2_e(:,imin+1:imin+obc_width,:)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_e,name_in_temp,tmp_ew,              &
                           imin  ,obc_width,jmin,jmax,1,kmax,itcl,imin  ,jmin,1,fillvar=rg_valmanq_io)
           temp2_e(:,imin+1:imin+obc_width,:)=tmp_ew(:,imin+1:imin+obc_width,:)
         END IF
         IF (l_obc_ogcm_rs) THEN
           sal1_e(:,imin+1:imin+obc_width,:)=sal2_e(:,imin+1:imin+obc_width,:)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_e,name_in_sal,tmp_ew,               &
                           imin  ,obc_width,jmin,jmax,1,kmax,itcl,imin  ,jmin,1,fillvar=rg_valmanq_io)
           sal2_e(:,imin+1:imin+obc_width,:)=tmp_ew(:,imin+1:imin+obc_width,:)
         END IF
         IF (l_obc_ogcm_ruvz) THEN
           uz1_e(:,imin+1:imin+obc_width,:)=uz2_e(:,imin+1:imin+obc_width,:)
           vz1_e(:,imin+1:imin+obc_width,:)=vz2_e(:,imin+1:imin+obc_width,:)
           CALL ionc4_read_sublzxyt(file_obc_ogcm_e,name_in_uz,tmp_ew,                &
                           imin  ,obc_width,jmin,jmax,1,kmax,itcl,imin  ,jmin,1,fillvar=0.0_riosh)
            uz2_e(:,imin+1:imin+obc_width,:)=tmp_ew(:,imin+1:imin+obc_width,:)
            CALL ionc4_read_sublzxyt(file_obc_ogcm_e,name_in_vz,tmp_ew,                &
                           imin  ,obc_width,jmin,jmax,1,kmax,itcl,imin  ,jmin,1,fillvar=0.0_riosh)
            vz2_e(:,imin+1:imin+obc_width,:)=tmp_ew(:,imin+1:imin+obc_width,:)
         END IF
         IF (l_obc_ogcm_ruv) THEN 
           u1_e(imin+1:imin+obc_width,:)=u2_e(imin+1:imin+obc_width,:)
           v1_e(imin+1:imin+obc_width,:)=v2_e(imin+1:imin+obc_width,:)
           CALL ionc4_read_sublxyt(file_obc_ogcm_e,name_in_u,uvtmp_ew, &
                         imin  ,obc_width,jmin,jmax,itcl  ,imin,jmin,fillvar=0.0_riosh)
           u2_e(imin+1:imin+obc_width,:)=uvtmp_ew(imin+1:imin+obc_width,:)
           CALL ionc4_read_sublxyt(file_obc_ogcm_e,name_in_v,uvtmp_ew, &
                         imin  ,obc_width,jmin,jmax,itcl  ,imin,jmin,fillvar=0.0_riosh)
           v2_e(imin+1:imin+obc_width,:)=uvtmp_ew(imin+1:imin+obc_width,:)
       END IF
       END IF

     END IF ! end swap and reading data

     ! time interpolation 

     alf=sngl(obc_t2-torigin-t)/sngl(obc_t2-obc_t1)

     IF ( l_obc_south ) THEN
       IF (l_obc_ogcm_rssh) obc_ogcm_sshs(liminm2:limaxp2)= &
                            alf*xe1s(liminm2:limaxp2,jmin+1)+(1.0_rsh-alf)*xe2s(liminm2:limaxp2,jmin+1)
       IF (l_obc_ogcm_ruv) THEN
         obc_ogcm_us(limin:limax,jmin:jmin+obc_width)=                    &
                     alf*u1_s(limin:limax,jmin:jmin+obc_width)            &
                     +(1.0_rsh-alf)*u2_s(limin:limax,jmin:jmin+obc_width)
         obc_ogcm_vs(limin:limax,jmin:jmin+obc_width)=                    &
                     alf*v1_s(limin:limax,jmin:jmin+obc_width)            &
                     +(1.0_rsh-alf)*v2_s(limin:limax,jmin:jmin+obc_width)
       END IF
       IF (l_obc_ogcm_rt) THEN
         obc_ts(:,limin:limax,jmin+1:jmin+obc_width)=                      &
                     alf*temp1_s(:,limin:limax,jmin+1:jmin+obc_width)      &
                     +(1.0_rsh-alf)*temp2_s(:,limin:limax,jmin+1:jmin+obc_width)
       END IF
       IF (l_obc_ogcm_rs) THEN
         obc_ss(:,limin:limax,jmin+1:jmin+obc_width)=                       &
                     alf*sal1_s(:,limin:limax,jmin+1:jmin+obc_width)        &
                     +(1.0_rsh-alf)*sal2_s(:,limin:limax,jmin+1:jmin+obc_width)
       END IF
       IF (l_obc_ogcm_ruvz) THEN
         obc_ogcm_uzs(:,limin:limax,jmin:jmin+obc_width)=               &
                     alf*uz1_s(:,limin:limax,jmin:jmin+obc_width)       &
                     +(1.0_rsh-alf)*uz2_s(:,limin:limax,jmin:jmin+obc_width)
         obc_ogcm_vzs(:,limin:limax,jmin:jmin+obc_width)=               &
                     alf*vz1_s(:,limin:limax,jmin:jmin+obc_width)       &
                     +(1.0_rsh-alf)*vz2_s(:,limin:limax,jmin:jmin+obc_width)

        IF (.NOT.l_obc_ogcm_ruv) THEN
         obc_ogcm_us(limin:limax,jmin:jmin+obc_width)=0.0_rsh
         obc_ogcm_vs(limin:limax,jmin:jmin+obc_width)=0.0_rsh
         DO k=1,kmax
#if defined key_siggen || defined key_gencoord
           IF_MPI ( (jmin==ljmin) .OR. (jmin+obc_width >= ljmin .AND. jmin+obc_width <= ljmax) ) THEN
           obc_ogcm_us(limin:limax,ljmin:MIN0(jmin+obc_width,ljmax))=      &
              obc_ogcm_us(limin:limax,ljmin:MIN0(jmin+obc_width,ljmax)) +  &
              obc_ogcm_uzs(k,limin:limax,ljmin:MIN0(jmin+obc_width,ljmax))* &
              hzutx(k,limin:limax,ljmin:MIN0(jmin+obc_width,ljmax))*dsigu(k)
           obc_ogcm_vs(limin:limax,ljmin:MIN0(jmin+obc_width,ljmax))=      &
              obc_ogcm_vs(limin:limax,ljmin:MIN0(jmin+obc_width,ljmax)) +  &
              obc_ogcm_vzs(k,limin:limax,ljmin:MIN0(jmin+obc_width,ljmax))* &
              hzuty(k,limin:limax,ljmin:MIN0(jmin+obc_width,ljmax))*dsigu(k)
           ENDIF_MPI
#else
           obc_ogcm_us(limin:limax,jmin:jmin+obc_width)=                &
              obc_ogcm_us(limin:limax,jmin:jmin+obc_width) +            &
              obc_ogcm_uzs(k,limin:limax,jmin:jmin+obc_width)*dsigu(k)
           obc_ogcm_vs(limin:limax,jmin:jmin+obc_width)=                &
              obc_ogcm_vs(limin:limax,jmin:jmin+obc_width) +            &
              obc_ogcm_vzs(k,limin:limax,jmin:jmin+obc_width)*dsigu(k)
#endif
         END DO
#if defined key_siggen || defined key_gencoord
         IF_MPI ( (jmin==ljmin) .OR. (jmin+obc_width >= ljmin .AND. jmin+obc_width <= ljmax) ) THEN
         obc_ogcm_us(limin:limax,ljmin:MIN0(jmin+obc_width,ljmax))= &
           obc_ogcm_us(limin:limax,ljmin:MIN0(jmin+obc_width,ljmax))/htx(limin:limax,ljmin:MIN0(jmin+obc_width,ljmax))
         obc_ogcm_vs(limin:limax,ljmin:MIN0(jmin+obc_width,ljmax))= &
           obc_ogcm_vs(limin:limax,ljmin:MIN0(jmin+obc_width,ljmax))/hty(limin:limax,ljmin:MIN0(jmin+obc_width,ljmax))
         ENDIF_MPI
#endif
        END IF
       END IF
     END IF

     IF ( l_obc_north ) THEN
       IF (l_obc_ogcm_rssh) obc_ogcm_sshn(liminm2:limaxp2)=alf*xe1n(liminm2:limaxp2,obc_width)+ &
                            (1.0_rsh-alf)*xe2n(liminm2:limaxp2,obc_width)
       IF (l_obc_ogcm_ruv) THEN
         obc_ogcm_un(limin:limax,jmax-obc_width+1:jmax)=                  &
                   alf*u1_n(limin:limax,jmin+1:jmin+obc_width)            &
                   +(1.0_rsh-alf)*u2_n(limin:limax,jmin+1:jmin+obc_width)
         obc_ogcm_vn(limin:limax,jmax-obc_width+1:jmax)=                  &
                   alf*v1_n(limin:limax,jmin+1:jmin+obc_width)            &
                   +(1.0_rsh-alf)*v2_n(limin:limax,jmin+1:jmin+obc_width)
       END IF
       IF (l_obc_ogcm_rt) THEN
         obc_tn(:,limin:limax,jmax-obc_width+1:jmax)=                        &
                    alf*temp1_n(:,limin:limax,jmin+1:jmin+obc_width)         &
                    +(1.0_rsh-alf)*temp2_n(:,limin:limax,jmin+1:jmin+obc_width)
       END IF
       IF (l_obc_ogcm_rs) THEN
         obc_sn(:,limin:limax,jmax-obc_width+1:jmax)=                        &
                    alf*sal1_n(:,limin:limax,jmin+1:jmin+obc_width)          &
                    +(1.0_rsh-alf)*sal2_n(:,limin:limax,jmin+1:jmin+obc_width)
       END IF
       IF (l_obc_ogcm_ruvz) THEN
         obc_ogcm_uzn(:,limin:limax,jmax-obc_width+1:jmax)=                  &
                   alf*uz1_n(:,limin:limax,jmin+1:jmin+obc_width)            &
                   +(1.0_rsh-alf)*uz2_n(:,limin:limax,jmin+1:jmin+obc_width)
         obc_ogcm_vzn(:,limin:limax,jmax-obc_width+1:jmax)=                  &
                   alf*vz1_n(:,limin:limax,jmin+1:jmin+obc_width)            &
                   +(1.0_rsh-alf)*vz2_n(:,limin:limax,jmin+1:jmin+obc_width)

        IF (.NOT.l_obc_ogcm_ruv) THEN
         obc_ogcm_un(limin:limax,jmax-obc_width+1:jmax)=0.0_rsh
         obc_ogcm_vn(limin:limax,jmax-obc_width+1:jmax)=0.0_rsh
         DO k=1,kmax
#if defined key_siggen || defined key_gencoord
           IF_MPI ( (jmax==ljmax) .OR. (jmax-obc_width+1 >= ljmin .AND. jmax-obc_width+1 <= ljmax) ) THEN
           obc_ogcm_un(limin:limax,MAX0(jmax-obc_width+1,ljmin):ljmax)=         &
              obc_ogcm_un(limin:limax,MAX0(jmax-obc_width+1,ljmin):ljmax) +     &
              obc_ogcm_uzn(k,limin:limax,MAX0(jmax-obc_width+1,ljmin):ljmax)*   &
              hzutx(k,limin:limax,MAX0(jmax-obc_width+1,ljmin):ljmax)*dsigu(k)
           obc_ogcm_vn(limin:limax,MAX0(jmax-obc_width+1,ljmin):ljmax)=         &
              obc_ogcm_vn(limin:limax,MAX0(jmax-obc_width+1,ljmin):ljmax) +     & 
              obc_ogcm_vzn(k,limin:limax,MAX0(jmax-obc_width+1,ljmin):ljmax)*   &
              hzuty(k,limin:limax,MAX0(jmax-obc_width+1,ljmin):ljmax)*dsigu(k)
           ENDIF_MPI
#else
           obc_ogcm_un(limin:limax,jmax-obc_width+1:jmax)=                   &
              obc_ogcm_un(limin:limax,jmax-obc_width+1:jmax) +               &
              obc_ogcm_uzn(k,limin:limax,jmax-obc_width+1:jmax)*dsigu(k)
           obc_ogcm_vn(limin:limax,jmax-obc_width+1:jmax)=                   &
              obc_ogcm_vn(limin:limax,jmax-obc_width+1:jmax) +               & 
              obc_ogcm_vzn(k,limin:limax,jmax-obc_width+1:jmax)*dsigu(k)
#endif
         END DO
#if defined key_siggen || defined key_gencoord
         IF_MPI ( (jmax==ljmax) .OR. (jmax-obc_width+1 >= ljmin .AND. jmax-obc_width+1 <= ljmax) ) THEN
         obc_ogcm_un(limin:limax,MAX0(jmax-obc_width+1,ljmin):ljmax)= &
           obc_ogcm_un(limin:limax,MAX0(jmax-obc_width+1,ljmin):ljmax)/htx(limin:limax,MAX0(jmax-obc_width+1,ljmin):ljmax)
         obc_ogcm_vn(limin:limax,MAX0(jmax-obc_width+1,ljmin):ljmax)= &
           obc_ogcm_vn(limin:limax,MAX0(jmax-obc_width+1,ljmin):ljmax)/hty(limin:limax,MAX0(jmax-obc_width+1,ljmin):ljmax)
         ENDIF_MPI
#endif
        END IF
       END IF
     END IF

     IF ( l_obc_west ) THEN
       IF (l_obc_ogcm_rssh) obc_ogcm_sshw(ljminm2:ljmaxp2)=alf*xe1w(imin+31,ljminm2:ljmaxp2)+(1.0_rsh-alf)* &
                                                           xe2w(imin+31,ljminm2:ljmaxp2)
       IF (l_obc_ogcm_ruv) THEN
         obc_ogcm_uw(imin:imin+obc_width,ljmin:ljmax)=                  &
                   alf*u1_w(imin:imin+obc_width,ljmin:ljmax)            &
                   +(1.0_rsh-alf)*u2_w(imin:imin+obc_width,ljmin:ljmax)
         obc_ogcm_vw(imin:imin+obc_width,ljmin:ljmax)=                  &
                   alf*v1_w(imin:imin+obc_width,ljmin:ljmax)            &
                   +(1.0_rsh-alf)*v2_w(imin:imin+obc_width,ljmin:ljmax)
       END IF
       IF (l_obc_ogcm_rt) THEN
         obc_tw(:,imin+1:imin+obc_width,ljmin:ljmax)=                       &
                  alf*temp1_w(:,imin+1:imin+obc_width,ljmin:ljmax)          &
                  +(1.0_rsh-alf)*temp2_w(:,imin+1:imin+obc_width,ljmin:ljmax)
       END IF
       IF (l_obc_ogcm_rs) THEN
         obc_sw(:,imin+1:imin+obc_width,ljmin:ljmax)=                       &
                    alf*sal1_w(:,imin+1:imin+obc_width,ljmin:ljmax)         &
                    +(1.0_rsh-alf)*sal2_w(:,imin+1:imin+obc_width,ljmin:ljmax)
       END IF
       IF (l_obc_ogcm_ruvz) THEN
         obc_ogcm_uzw(:,imin:imin+obc_width,ljmin:ljmax)=                  &
                   alf*uz1_w(:,imin:imin+obc_width,ljmin:ljmax)            &
                   +(1.0_rsh-alf)*uz2_w(:,imin:imin+obc_width,ljmin:ljmax)
         obc_ogcm_vzw(:,imin:imin+obc_width,ljmin:ljmax)=                  &
                   alf*vz1_w(:,imin:imin+obc_width,ljmin:ljmax)            &
                   +(1.0_rsh-alf)*vz2_w(:,imin:imin+obc_width,ljmin:ljmax)

        IF (.NOT.l_obc_ogcm_ruv) THEN
         obc_ogcm_uw(imin:imin+obc_width,ljmin:ljmax)=0.0_rsh
         obc_ogcm_vw(imin:imin+obc_width,ljmin:ljmax)=0.0_rsh
         DO k=1,kmax
#if defined key_siggen || defined key_gencoord
           IF_MPI ( (imin==limin) .OR. (imin+obc_width >= limin .AND. imin+obc_width <= limax) ) THEN
           obc_ogcm_uw(limin:MIN0(imin+obc_width,limax),ljmin:ljmax)=         &
             obc_ogcm_uw(limin:MIN0(imin+obc_width,limax),ljmin:ljmax) +      &
             obc_ogcm_uzw(k,limin:MIN0(imin+obc_width,limax),ljmin:ljmax)*    &
             hzutx(k,limin:MIN0(imin+obc_width,limax),ljmin:ljmax)*dsigu(k)
           obc_ogcm_vw(limin:MIN0(imin+obc_width,limax),ljmin:ljmax)=         &
             obc_ogcm_vw(limin:MIN0(imin+obc_width,limax),ljmin:ljmax) +      &
             obc_ogcm_vzw(k,limin:MIN0(imin+obc_width,limax),ljmin:ljmax)*    &
             hzuty(k,limin:MIN0(imin+obc_width,limax),ljmin:ljmax)*dsigu(k)
           ENDIF_MPI
#else
           obc_ogcm_uw(imin:imin+obc_width,ljmin:ljmax)=                   &
              obc_ogcm_uw(imin:imin+obc_width,ljmin:ljmax) +               &
              obc_ogcm_uzw(k,imin:imin+obc_width,ljmin:ljmax)*dsigu(k)
           obc_ogcm_vw(imin:imin+obc_width,ljmin:ljmax)=                   &
             obc_ogcm_vw(imin:imin+obc_width,ljmin:ljmax) +                &
             obc_ogcm_vzw(k,imin:imin+obc_width,ljmin:ljmax)*dsigu(k)
#endif
         END DO
#if defined key_siggen || defined key_gencoord
         IF_MPI ( (imin==limin) .OR. (imin+obc_width >= limin .AND. imin+obc_width <= limax) ) THEN
         obc_ogcm_uw(limin:MIN0(imin+obc_width,limax),ljmin:ljmax)= &
           obc_ogcm_uw(limin:MIN0(imin+obc_width,limax),ljmin:ljmax)/htx(limin:MIN0(imin+obc_width,limax),ljmin:ljmax)
         obc_ogcm_vw(limin:MIN0(imin+obc_width,limax),ljmin:ljmax)= &
           obc_ogcm_vw(limin:MIN0(imin+obc_width,limax),ljmin:ljmax)/hty(limin:MIN0(imin+obc_width,limax),ljmin:ljmax)
         ENDIF_MPI
#endif
      END IF
       END IF
     END IF

     IF ( l_obc_east ) THEN
       IF (l_obc_ogcm_rssh) obc_ogcm_sshe(ljminm2:ljmaxp2)=alf*xe1e(obc_width,ljminm2:ljmaxp2)+ &
                            (1.0_rsh-alf)*xe2e(obc_width,ljminm2:ljmaxp2)
       IF (l_obc_ogcm_ruv) THEN
         obc_ogcm_ue(imax-obc_width+1:imax,ljmin:ljmax)=                 &
                   alf*u1_e(imin+1:imin+obc_width,ljmin:ljmax)           &
                   +(1.0_rsh-alf)*u2_e(imin+1:imin+obc_width,ljmin:ljmax)
         obc_ogcm_ve(imax-obc_width+1:imax,ljmin:ljmax)=                 &
                   alf*v1_e(imin+1:imin+obc_width,ljmin:ljmax)           &
                   +(1.0_rsh-alf)*v2_e(imin+1:imin+obc_width,ljmin:ljmax)
       END IF
       IF (l_obc_ogcm_rt) THEN
         obc_te(:,imax-obc_width+1:imax,ljmin:ljmax)=                       &
                    alf*temp1_e(:,imin+1:imin+obc_width,ljmin:ljmax)        &
                    +(1.0_rsh-alf)*temp2_e(:,imin+1:imin+obc_width,ljmin:ljmax)
       END IF
       IF (l_obc_ogcm_rs) THEN
         obc_se(:,imax-obc_width+1:imax,ljmin:ljmax)=                       &
                    alf*sal1_e(:,imin+1:imin+obc_width,ljmin:ljmax)         &
                    +(1.0_rsh-alf)*sal2_e(:,imin+1:imin+obc_width,ljmin:ljmax)
       END IF
       IF (l_obc_ogcm_ruvz) THEN
         obc_ogcm_uze(:,imax-obc_width+1:imax,ljmin:ljmax)=                 &
                   alf*uz1_e(:,imin+1:imin+obc_width,ljmin:ljmax)           &
                   +(1.0_rsh-alf)*uz2_e(:,imin+1:imin+obc_width,ljmin:ljmax)
         obc_ogcm_vze(:,imax-obc_width+1:imax,ljmin:ljmax)=                 &
                   alf*vz1_e(:,imin+1:imin+obc_width,ljmin:ljmax)           &
                   +(1.0_rsh-alf)*vz2_e(:,imin+1:imin+obc_width,ljmin:ljmax)
        IF (.NOT.l_obc_ogcm_ruv) THEN
         obc_ogcm_ue(imax-obc_width+1:imax,ljmin:ljmax)=0.0_rsh
         obc_ogcm_ve(imax-obc_width+1:imax,ljmin:ljmax)=0.0_rsh
         DO k=1,kmax
#if defined key_siggen || defined key_gencoord
           IF_MPI ( (imax==limax) .OR. (imax-obc_width+1 >= limin .AND. imax-obc_width+1 <= limax) ) THEN
           obc_ogcm_ue(MAX0(imax-obc_width+1,limin):limax,ljmin:ljmax)=        &
              obc_ogcm_ue(MAX0(imax-obc_width+1,limin):limax,ljmin:ljmax) +    &
              obc_ogcm_uze(k,MAX0(imax-obc_width+1,limin):limax,ljmin:ljmax)*  &
              hzutx(k,MAX0(imax-obc_width+1,limin):limax,ljmin:ljmax)*dsigu(k)
           obc_ogcm_ve(MAX0(imax-obc_width+1,limin):limax,ljmin:ljmax)=        &
              obc_ogcm_ve(MAX0(imax-obc_width+1,limin):limax,ljmin:ljmax) +    &
              obc_ogcm_vze(k,MAX0(imax-obc_width+1,limin):limax,ljmin:ljmax)*  &
              hzuty(k,MAX0(imax-obc_width+1,limin):limax,ljmin:ljmax)*dsigu(k)
           ENDIF_MPI
#else
           obc_ogcm_ue(imax-obc_width+1:imax,ljmin:ljmax)=                  &
              obc_ogcm_ue(imax-obc_width+1:imax,ljmin:ljmax) +              &
              obc_ogcm_uze(k,imax-obc_width+1:imax,ljmin:ljmax)*dsigu(k)
           obc_ogcm_ve(imax-obc_width+1:imax,ljmin:ljmax)=                  &
              obc_ogcm_ve(imax-obc_width+1:imax,ljmin:ljmax) +              &
              obc_ogcm_vze(k,imax-obc_width+1:imax,ljmin:ljmax)*dsigu(k)
#endif
         END DO
#if defined key_siggen || defined key_gencoord
         IF_MPI ( (imax==limax) .OR. (imax-obc_width+1 >= limin .AND. imax-obc_width+1 <= limax) ) THEN
         obc_ogcm_ue(MAX0(imax-obc_width+1,limin):limax,ljmin:ljmax)= &
           obc_ogcm_ue(MAX0(imax-obc_width+1,limin):limax,ljmin:ljmax)/htx(MAX0(imax-obc_width+1,limin):limax,ljmin:ljmax)
         obc_ogcm_ve(MAX0(imax-obc_width+1,limin):limax,ljmin:ljmax)= &
           obc_ogcm_ve(MAX0(imax-obc_width+1,limin):limax,ljmin:ljmax)/hty(MAX0(imax-obc_width+1,limin):limax,ljmin:ljmax)
         ENDIF_MPI
#endif
      END IF
       END IF
     END IF 

   END IF ! end test on obc reading requirement

   PRINT_DBG*, 'exit OBC_OGCM routine'

  END SUBROUTINE obc_ogcm

   !!======================================================================

  SUBROUTINE obc_tsref

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE obc_tsref  ***
   !&E
   !&E ** Purpose : Set T and S open boundary condition to reference values
   !&E
   !&E ** Description : 
   !&E
   !&E ** Called by : step2dxy.F90,step2dyx.F90,step3dxy.F90,step3dyx.F90
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays :
   !&E
   !&E ** Modified variables : obc_se,obc_sn,obc_ss,obc_sw
   !&E                         obc_te,obc_tn,obc_ts,obc_tw
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2009-09-11 (S. Michel, HOCER) original code
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   !! * Arguments

   !! * Local declarations
#ifdef key_lagoon
   INTEGER              :: ng,i0,j0
#endif

   !!----------------------------------------------------------------------
   !! * Executable part

   PRINT_DBG*, 'enter OBC_TSREF routine'

   !!======================================================================
#ifdef key_lagoon

   DO ng=1,nbgrau
         obc_sgrau(ng)=saliref
         obc_tgrau(ng)=tetaref          
   ENDDO

#else
   IF (l_obc_south) THEN
     ALLOCATE( obc_ts(kmax,limin:limax,jmin  :jmin+obc_width+jshift_obc_south) )
     ALLOCATE( obc_ss(kmax,limin:limax,jmin  :jmin+obc_width+jshift_obc_south) )
     obc_ss(:,limin:limax,jmin+1:jmin+obc_width+jshift_obc_south)=saliref
     obc_ts(:,limin:limax,jmin+1:jmin+obc_width+jshift_obc_south)=tetaref
   END IF
   IF (l_obc_north) THEN
     ALLOCATE( obc_tn(kmax,limin:limax,jmax-obc_width+1+jshift_obc_north:jmax) )
     ALLOCATE( obc_sn(kmax,limin:limax,jmax-obc_width+1+jshift_obc_north:jmax) )
     obc_sn(:,limin:limax,jmax-obc_width+1+jshift_obc_north:jmax)=saliref
     obc_tn(:,limin:limax,jmax-obc_width+1+jshift_obc_north:jmax)=tetaref
   END IF
   IF (l_obc_west) THEN
     ALLOCATE( obc_tw(kmax,imin  :imin+obc_width+ishift_obc_west,ljmin:ljmax) )
     ALLOCATE( obc_sw(kmax,imin  :imin+obc_width+ishift_obc_west,ljmin:ljmax) )
     obc_sw(:,imin+1:imin+obc_width+ishift_obc_west,:)=saliref
     obc_tw(:,imin+1:imin+obc_width+ishift_obc_west,:)=tetaref
   END IF
   IF (l_obc_east) THEN
     ALLOCATE( obc_te(kmax,imax-obc_width+1+ishift_obc_east:imax,ljmin:ljmax) )
     ALLOCATE( obc_se(kmax,imax-obc_width+1+ishift_obc_east:imax,ljmin:ljmax) )
     obc_se(:,imax-obc_width+1+ishift_obc_east:imax,:)=saliref
     obc_te(:,imax-obc_width+1+ishift_obc_east:imax,:)=tetaref
   END IF
#endif

   PRINT_DBG*, 'exit OBC_TSREF routine'

  END SUBROUTINE obc_tsref

   !!======================================================================

#ifdef key_substance

  SUBROUTINE obc_subs

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE obc_subs  ***
   !&E
   !&E ** Purpose : Read open boundary condition for biology provided by climatology 
   !&E
   !&E ** Description : time interpolated substances
   !&E
   !&E ** Called by : step2dxy.F90,step2dyx.F90,step3dxy.F90,step3dyx.F90
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays :
   !&E
   !&E ** Modified variables :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History : 
   !&E       !  2009-09 (S. Michel, HOCER) check the consistency of bathy and Z coord.
   !&E       !  2010-06 (S. Roudesli) correction of indexes for N/E boundaries (time interpolation)
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   !! * Arguments

   !! * Local declarations
   INTEGER               :: i,j,k,nv,idimt,ncerror,nmax,n,nn,kmin
   INTEGER,DIMENSION(5)  :: idmt
   INTEGER,DIMENSION(4)  :: idmi,idmj
   INTEGER, SAVE         :: itcl_subs
   REAL(KIND=rsh)        :: alf
   REAL(KIND=rlg)        :: torigin
   REAL(kind=riosh),DIMENSION(imin:imax,obc_width) :: xtmp_ns
   REAL(kind=riosh),DIMENSION(obc_width,jmin:jmax) :: xtmp_ew
   REAL(kind=riosh),DIMENSION(kmax,imin:imax,jmin:jmin+obc_width) :: tmp_ns
   REAL(kind=riosh),DIMENSION(kmax,imin:imin+obc_width,jmin:jmax) :: tmp_ew
   REAL(kind=riosh),DIMENSION(imin:imax,jmin:jmax) :: xyref
   REAL(kind=riosh),DIMENSION(0:kmax+1)            :: sigref,sigtmp
   REAL(kind=riosh)      :: valref,valtmp
   CHARACTER(LEN=19)     :: tool_sectodat
   CHARACTER(LEN=lchain) :: namvar,namfile

   !!----------------------------------------------------------------------
   !! * Executable part
   
   PRINT_DBG*, 'enter OBC_SUBS routine'

   !!!!!!!!!!!!!!!!!!!!!!!!!
   !     Initialisation
   !!!!!!!!!!!!!!!!!!!!!!!!!

   ! READ DATA IF OPEN BOUNDARY EXISTS AND IF DATA REQUIRED
   IF ( l_obc_south.OR.l_obc_north.OR.l_obc_west.OR.l_obc_east ) THEN

   !  si la date origine des conditions aux limites est differente de celle du modele
   ! torigin=tool_datosec('01/01/1900 00:00:00')-tool_datosec('01/01/1994 00:00:00')
     torigin=0.0_rlg

     IF ( nbouc == 1 ) THEN

       idmt(:)=0
       idmi(:)=0
       idmj(:)=0
       IF ( l_obc_south ) THEN
         CALL ionc4_openr(file_obc_subs_s,l_out_nc4par=.true.)
         idmt(1)=ionc4_read_dimt(file_obc_subs_s)
         CALL ionc4_read_dimi(file_obc_subs_s,idmi(1))
         CALL ionc4_read_dimj(file_obc_subs_s,idmj(1))
         file_obc_3d_subs=file_obc_subs_s
       END IF
       IF ( l_obc_north ) THEN
         CALL ionc4_openr(file_obc_subs_n,l_out_nc4par=.true.)
         idmt(2)=ionc4_read_dimt(file_obc_subs_n)
         CALL ionc4_read_dimi(file_obc_subs_n,idmi(2))
         CALL ionc4_read_dimj(file_obc_subs_n,idmj(2))
         file_obc_3d_subs=file_obc_subs_n
       END IF
       IF ( l_obc_west ) THEN
         CALL ionc4_openr(file_obc_subs_w,l_out_nc4par=.true.)
         idmt(3)=ionc4_read_dimt(file_obc_subs_w)
         CALL ionc4_read_dimi(file_obc_subs_w,idmi(3))
         CALL ionc4_read_dimj(file_obc_subs_w,idmj(3))
         file_obc_3d_subs=file_obc_subs_w
       END IF
       IF ( l_obc_east ) THEN
         CALL ionc4_openr(file_obc_subs_e,l_out_nc4par=.true.)
         idmt(4)=ionc4_read_dimt(file_obc_subs_e)
         CALL ionc4_read_dimi(file_obc_subs_e,idmi(4))
         CALL ionc4_read_dimj(file_obc_subs_e,idmj(4))
         file_obc_3d_subs=file_obc_subs_e
       END IF
       CALL ionc4_openr(file_obc_3d_subs,l_out_nc4par=.true.)

   ! make sure record is long enough
       idmt(5)=MAXVAL(idmt(1:4))
       WHERE(idmt(1:4)==0) idmt(1:4)=idmt(5)
       IF ( l_obc_south .AND. idmt(1) < idmt(5)) &
         WRITE(iwarnlog,*) 'WARNING: In file ',TRIM(file_obc_subs_s),&
           ', the length of the OBC record is too short (substance file).'
       IF ( l_obc_north .AND. idmt(2) < idmt(5)) &
         WRITE(iwarnlog,*) 'WARNING: In file ',TRIM(file_obc_subs_n),&
           ', the length of the OBC record is too short (substance file).'
       IF ( l_obc_west  .AND. idmt(3) < idmt(5)) &
         WRITE(iwarnlog,*) 'WARNING: In file ',TRIM(file_obc_subs_w),&
           ', the length of the OBC record is too short (substance file).'
       IF ( l_obc_east  .AND. idmt(4) < idmt(5)) &
         WRITE(iwarnlog,*) 'WARNING: In file ',TRIM(file_obc_subs_e),&
           ', the length of the OBC record is too short (substance file).'

       ncerror=0

    ! make sure sizes of OBC grid are correct
       IF ( l_obc_south .AND. idmj(1) /= obc_width+1) THEN
         WRITE(ierrorlog,*) 'ERROR: In file ',TRIM(file_obc_subs_s)
         WRITE(ierrorlog,*) 'the width of the OBC record is not correct: idmj = ',idmj(1)
         WRITE(ierrorlog,*) 'The width must be equal to obc_width+1 = ',obc_width+1
         ncerror=ncerror+1
       END IF
       IF ( l_obc_south .AND. idmi(1) /= imax+1) THEN
         WRITE(ierrorlog,*) 'ERROR: In file ',TRIM(file_obc_subs_s)
         WRITE(ierrorlog,*) 'the width of the OBC record is not correct: idmi = ',idmi(1)
         WRITE(ierrorlog,*) 'The width must be equal to imax+1 = ',imax+1
         ncerror=ncerror+1
       END IF
       IF ( l_obc_north .AND. idmj(2) /= obc_width+1) THEN
         WRITE(ierrorlog,*) 'ERROR: In file ',TRIM(file_obc_subs_n)
         WRITE(ierrorlog,*) 'the width of the OBC record is not correct: idmj = ',idmj(2)
         WRITE(ierrorlog,*) 'The width must be equal to obc_width+1 = ',obc_width+1
         ncerror=ncerror+1
       END IF
       IF ( l_obc_north .AND. idmi(2) /= imax+1) THEN
         WRITE(ierrorlog,*) 'ERROR: In file ',TRIM(file_obc_subs_n)
         WRITE(ierrorlog,*) 'the width of the OBC record is not correct: idmi = ',idmi(2)
         WRITE(ierrorlog,*) 'The width must be equal to imax+1 = ',imax+1
         ncerror=ncerror+1
       END IF
       IF ( l_obc_west .AND. idmi(3) /= obc_width+1) THEN
         WRITE(ierrorlog,*) 'ERROR: In file ',TRIM(file_obc_subs_w)
         WRITE(ierrorlog,*) 'the width of the OBC record is not correct: idmi = ',idmi(3)
         WRITE(ierrorlog,*) 'The width must be equal to obc_width+1 = ',obc_width+1
         ncerror=ncerror+1
       END IF
       IF ( l_obc_west .AND. idmj(3) /= jmax+1) THEN
         WRITE(ierrorlog,*) 'ERROR: In file ',TRIM(file_obc_subs_w)
         WRITE(ierrorlog,*) 'the width of the OBC record is not correct: idmj = ',idmj(3)
         WRITE(ierrorlog,*) 'The width must be equal to jmax+1 = ',jmax+1
         ncerror=ncerror+1
       END IF
       IF ( l_obc_east .AND. idmi(4) /= obc_width+1) THEN
         WRITE(ierrorlog,*) 'ERROR: In file ',TRIM(file_obc_subs_e)
         WRITE(ierrorlog,*) 'the width of the OBC record is not correct: idmi = ',idmi(4)
         WRITE(ierrorlog,*) 'The width must be equal to obc_width+1 = ',obc_width+1
         ncerror=ncerror+1
       END IF
       IF ( l_obc_east .AND. idmj(4) /= jmax+1) THEN
         WRITE(ierrorlog,*) 'ERROR: In file ',TRIM(file_obc_subs_e)
         WRITE(ierrorlog,*) 'the width of the OBC record is not correct: idmj = ',idmj(4)
         WRITE(ierrorlog,*) 'The width must be equal to jmax+1 = ',jmax+1
         ncerror=ncerror+1
       END IF
       IF (ncerror>0) THEN
         WRITE(ierrorlog,*) 'Simulation stopped'
         STOP
       ENDIF

       ! make sure OBC bathymetry is correct
       nmax=1
#if defined key_siggen || defined key_gencoord
       nmax=2
#endif
       DO n=1,nmax ! loop on the bathymetric variable
         IF (n==1) THEN
           namvar=name_out_h0
           xyref(:,:)=REAL(h0_g(:,:),riosh)
!         ELSEIF (n==2) THEN
!           namvar=name_out_hx
!           xyref=REAL(hx,riosh)
!         ELSEIF (n==3) THEN
!           namvar=name_out_hy
!           xyref=REAL(hy,riosh)
#if defined key_siggen || defined key_gencoord
         ELSEIF (n==2) THEN
           namvar='hc'
           xyref(:,:)=REAL(hc_sig_g(:,:),riosh)
#endif
         ENDIF

         ! Ignore the OB points containing land.
         WHERE (xyref <= REAL(-valmanq,riosh)) xyref=-rg_valmanq_io

         IF ( l_obc_south ) THEN
           ncerror=0
           CALL ionc4_read_sublxyt(file_obc_subs_s,namvar,xtmp_ns, &
                                   imin,imax,jmin+1,obc_width,1,imin,jmin+1)
           DO i=imin+1,imax
             IF (ABS(xtmp_ns(i,jmin+1)-xyref(i,jmin+1)) > PRECISION(0.0_riosh)) THEN
               IF (ncerror==0) THEN
                 WRITE (iwarnlog,*) ' '
                 WRITE (iwarnlog,*) 'WARNING: verification of the bathymetry ',&
                                    'in the OBC substance file:'
                 WRITE (iwarnlog,*) TRIM(file_obc_subs_s)
               ENDIF
               WRITE (iwarnlog,*) 'at the location jmin+1 = ',jmin+1,', i = ', i
               WRITE (iwarnlog,*) 'bathy_obc = ',xtmp_ns(i,jmin+1),&
                                  ' /= bathy_simu = ',xyref(i,jmin+1)
               ncerror=ncerror+1
             END IF
           END DO
         END IF
         IF ( l_obc_north ) THEN
           ncerror=0
           CALL ionc4_read_sublxyt(file_obc_subs_n,namvar,xtmp_ns, &
                                   imin,imax,jmin+1,obc_width,1,imin,jmin+1)
           DO i=imin+1,imax
             IF (ABS(xtmp_ns(i,obc_width)-xyref(i,jmax))>PRECISION(0.0_riosh)) THEN
               IF (ncerror==0) THEN
                 WRITE (iwarnlog,*) ' '
                 WRITE (iwarnlog,*) 'WARNING: verification of the bathymetry ',&
                                    'in the OBC substance file:'
                 WRITE (iwarnlog,*) TRIM(file_obc_subs_n)
               ENDIF
               WRITE (iwarnlog,*) 'at the location jmax = ',jmax,', i = ', i
               WRITE (iwarnlog,*) 'bathy_obc = ',xtmp_ns(i,obc_width),&
                                  ' /= bathy_simu = ',xyref(i,jmax)
               ncerror=ncerror+1
             END IF
           END DO
         END IF
         IF ( l_obc_west ) THEN
           ncerror=0
           CALL ionc4_read_sublxyt(file_obc_subs_w,namvar,xtmp_ew, &
                                   imin+1,obc_width,jmin,jmax,1,imin+1,jmin)
           DO j=jmin+1,jmax
             IF (ABS(xtmp_ew(imin+1,j)-xyref(imin+1,j)) > PRECISION(0.0_riosh)) THEN
               IF (ncerror==0) THEN
                 WRITE (iwarnlog,*) ' '
                 WRITE (iwarnlog,*) 'WARNING: verification of the bathymetry ',&
                                    'in the OBC substance file:'
                 WRITE (iwarnlog,*) TRIM(file_obc_subs_w)
               ENDIF
               WRITE (iwarnlog,*) 'at the location imin+1 = ',imin+1,', j = ', j
               WRITE (iwarnlog,*) 'bathy_obc = ',xtmp_ew(imin+1,j),&
                                  ' /= bathy_simu = ',xyref(imin+1,j)
               ncerror=ncerror+1
             END IF
           END DO
         END IF
         IF ( l_obc_east ) THEN
           ncerror=0
           CALL ionc4_read_sublxyt(file_obc_subs_e,namvar,xtmp_ew, &
                                   imin+1,obc_width,jmin,jmax,1,imin+1,jmin)
           DO j=jmin+1,jmax
             IF (ABS(xtmp_ew(obc_width,j)-xyref(imax,j)) > PRECISION(0.0_riosh)) THEN
               IF (ncerror==0) THEN
                 WRITE (iwarnlog,*) ' '
                 WRITE (iwarnlog,*) 'WARNING: verification of the bathymetry ',&
                                    'in the OBC substance file:'
                 WRITE (iwarnlog,*) TRIM(file_obc_subs_e)
               ENDIF
               WRITE (iwarnlog,*) 'at the location imax = ',imax,', j = ', j
               WRITE (iwarnlog,*) 'bathy_obc = ',xtmp_ew(obc_width,j),&
                                  ' /= bathy_simu = ',xyref(imax,j)
               ncerror=ncerror+1
             END IF
           END DO
         END IF

       ENDDO ! end of loop on the bathymetric variable

       ! make sure the vertical cordinate is correct

       DO nn=1,4 ! loop on the OBC file
         namfile=' '
         IF ((nn==1).AND.(l_obc_north)) namfile=file_obc_subs_n
         IF ((nn==2).AND.(l_obc_south)) namfile=file_obc_subs_s
         IF ((nn==3).AND.(l_obc_west) ) namfile=file_obc_subs_w
         IF ((nn==4).AND.(l_obc_east) ) namfile=file_obc_subs_e
         IF (LEN(TRIM(namfile))>1) THEN

         nmax=1
#if defined key_siggen || defined key_gencoord
         nmax=3
#endif
         DO n=1,nmax
           ncerror=0
           IF (n==1) THEN
             namvar='SIG'
             sigref(:)=REAL(sig(:),riosh)
             kmin=1
#if defined key_siggen || defined key_gencoord
           ELSEIF (n==2) THEN
             namvar='Csu_sig'
             sigref(:)=REAL(Csu_sig(:),riosh)
             kmin=1
           ELSEIF (n==3) THEN
             namvar='Csw_sig'
             sigref(:)=REAL(Csw_sig(:),riosh)
             kmin=0
             !sigref(kmax+1)=0.0_riosh
#endif
           ENDIF
           CALL ionc4_read_z(namfile,namvar,sigtmp(kmin:kmax),kmin,kmax)
           DO k=1,kmax
             IF (sigtmp(k)/=sigref(k)) ncerror=ncerror+1
           ENDDO
           IF (ncerror>0) THEN
             WRITE(iwarnlog,*) ' '
             WRITE(iwarnlog,*) 'ERROR: OBC_SUBS.F90 subroutine'
             WRITE(iwarnlog,*) 'The vertical coordinate "',TRIM(namvar),&
                               '" is not consistent with the coordinate'
             WRITE(iwarnlog,*) 'read from the OBC substance data file: ',TRIM(namfile)
             WRITE(iwarnlog,*) TRIM(namvar),'_simu = ',sigref(:)
             WRITE(iwarnlog,*) TRIM(namvar),'_obc  = ',sigtmp(:)
           ENDIF
         ENDDO

#if defined key_siggen || defined key_gencoord
         DO n=1,2
           IF (n==1) THEN
             namvar='b'
             valref=REAL(b_sig,riosh)
           ELSEIF (n==2) THEN
             namvar='theta'
             valref=REAL(theta_sig,riosh)
           ENDIF
           CALL ionc4_read(namfile,namvar,valtmp)
           IF (valtmp/=valref) THEN
             !ncerror=ncerror+1
             WRITE(ierrorlog,*) ' '
             WRITE(ierrorlog,*) 'ERROR: OBC_SUBS.F90 subroutine'
             WRITE(ierrorlog,*) 'The vertical parameter "',TRIM(namvar),&
                                '" is not consistent with the parameter'
             WRITE(ierrorlog,*) 'read from the OBC substance data file:',TRIM(namfile)
             WRITE(ierrorlog,*) TRIM(namvar),'_simu = ',valref
             WRITE(ierrorlog,*) TRIM(namvar),'_obc  = ',valtmp
           ENDIF
         ENDDO
#endif

         ENDIF
         namfile=' '
       ENDDO ! end of loop on the OBC file

       IF (ncerror>0) THEN
         WRITE(iwarnlog,*) 'Simulation should be stopped.'
!         STOP
       ENDIF
         
       ! Find dates to be read

       idimt=ionc4_read_dimt(file_obc_3d_subs)
       itcl_subs=2
       CALL ionc4_read_time(file_obc_3d_subs,itcl_subs-1,obc_subs_t1)
       CALL ionc4_read_time(file_obc_3d_subs,itcl_subs  ,obc_subs_t2)
       DO WHILE((t-(obc_subs_t1-torigin))*(t-(obc_subs_t2-torigin)) > 0.0_rlg .AND. itcl_subs < idimt )
         itcl_subs=itcl_subs+1
         CALL ionc4_read_time(file_obc_3d_subs,itcl_subs-1,obc_subs_t1)
         CALL ionc4_read_time(file_obc_3d_subs,itcl_subs  ,obc_subs_t2)
       END DO

       IF (itcl_subs==idimt) THEN
         WRITE(ierrorlog,*) 'ERROR : In file ',TRIM(file_obc_3d_subs)
         WRITE(ierrorlog,*) 'the OBC dates do not correspond to the simulation period.'
         WRITE(ierrorlog,*) 'The open boundary conditions (subtances) are available'
         WRITE(ierrorlog,*) 'from ',tool_sectodat(obc_subs_t1),' to ',tool_sectodat(obc_subs_t2)
         WRITE(ierrorlog,*) 'itcl_subs =',itcl_subs
         WRITE(ierrorlog,*) 'Simulation stopped'
         STOP
       END IF
       IF_MPI (MASTER) THEN
       WRITE(iscreenlog,*) ' '
       WRITE(iscreenlog,*) 'Routine OBC_SUBS.F90: 1st open boundary conditions'
       WRITE(iscreenlog,*) 'available at date: ',tool_sectodat(obc_t1)
       ENDIF_MPI
         
       ! Read data

       IF ( l_obc_south ) THEN
         DO i=1,nv_adv
#if defined key_zerograd
           IF (obc_cv_name(i)/='none' .AND. obc_cv_name(i) /= 'zerograd') THEN
#else
           IF (obc_cv_name(i)/='none') THEN
#endif
             IF_MPI (MASTER) &
             WRITE(iscreenlog,*) 'Southern boundary conditions for substances ',TRIM(file_obc_subs_s)
             CALL ionc4_read_sublzxyt(file_obc_subs_s,obc_cv_name(i),tmp_ns, &
                  imin,imax,jmin  ,obc_width,1,kmax,itcl_subs-1,imin,jmin  ,1,fillvar=rg_valmanq_io)
             cv_wat1_s(i,:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)*unit_modif_mudbio_N2dw(irk_fil(i))
             CALL ionc4_read_sublzxyt(file_obc_subs_s,obc_cv_name(i),tmp_ns, &
                  imin,imax,jmin  ,obc_width,1,kmax,itcl_subs  ,imin,jmin  ,1,fillvar=rg_valmanq_io)
             cv_wat2_s(i,:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)*unit_modif_mudbio_N2dw(irk_fil(i))
           END IF
         ENDDO
       END IF
         
       IF ( l_obc_north ) THEN
         DO i=1,nv_adv
#if defined key_zerograd
           IF (obc_cv_name(i)/='none' .AND. obc_cv_name(i) /= 'zerograd') THEN
#else
           IF (obc_cv_name(i)/='none') THEN
#endif
             IF_MPI (MASTER) &
             WRITE(iscreenlog,*) 'Northern boundary conditions for substances ',TRIM(file_obc_subs_n)
             CALL ionc4_read_sublzxyt(file_obc_subs_n,obc_cv_name(i),tmp_ns, &
                  imin,imax,jmin  ,obc_width,1,kmax,itcl_subs-1,imin,jmin  ,1,fillvar=rg_valmanq_io)
             cv_wat1_n(i,:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)*unit_modif_mudbio_N2dw(irk_fil(i))
             CALL ionc4_read_sublzxyt(file_obc_subs_n,obc_cv_name(i),tmp_ns, &
                  imin,imax,jmin  ,obc_width,1,kmax,itcl_subs  ,imin,jmin  ,1,fillvar=rg_valmanq_io)
             cv_wat2_n(i,:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)*unit_modif_mudbio_N2dw(irk_fil(i))
           END IF
         ENDDO
       END IF
         
       IF ( l_obc_west ) THEN
         DO i=1,nv_adv
#if defined key_zerograd
           IF (obc_cv_name(i)/='none' .AND. obc_cv_name(i) /= 'zerograd') THEN
#else
           IF (obc_cv_name(i)/='none') THEN
#endif
             IF_MPI (MASTER) &
             WRITE(iscreenlog,*) 'Western boundary conditions for substances ',TRIM(file_obc_subs_w)
             CALL ionc4_read_sublzxyt(file_obc_subs_w,obc_cv_name(i),tmp_ew, &
                  imin  ,obc_width,jmin,jmax,1,kmax,itcl_subs-1,imin  ,jmin,1,fillvar=rg_valmanq_io)
             cv_wat1_w(i,:,imin+1:imin+obc_width,:)=tmp_ew(:,imin+1:imin+obc_width,:)*unit_modif_mudbio_N2dw(irk_fil(i))
             CALL ionc4_read_sublzxyt(file_obc_subs_w,obc_cv_name(i),tmp_ew, &
                  imin  ,obc_width,jmin,jmax,1,kmax,itcl_subs  ,imin  ,jmin,1,fillvar=rg_valmanq_io)
             cv_wat2_w(i,:,imin+1:imin+obc_width,:)=tmp_ew(:,imin+1:imin+obc_width,:)*unit_modif_mudbio_N2dw(irk_fil(i))
           END IF
         ENDDO
       END IF
         
       IF ( l_obc_east ) THEN
         DO i=1,nv_adv
#if defined key_zerograd
           IF (obc_cv_name(i)/='none' .AND. obc_cv_name(i) /= 'zerograd') THEN
#else
           IF (obc_cv_name(i)/='none') THEN
#endif
             IF_MPI (MASTER) &
             WRITE(iscreenlog,*) 'Eastern boundary conditions for substances ',TRIM(file_obc_subs_e)
             CALL ionc4_read_sublzxyt(file_obc_subs_e,obc_cv_name(i),tmp_ew, &
                  imin  ,obc_width,jmin,jmax,1,kmax,itcl_subs-1,imin  ,jmin,1,fillvar=rg_valmanq_io)
             cv_wat1_e(i,:,imin+1:imin+obc_width,:)=tmp_ew(:,imin+1:imin+obc_width,:)*unit_modif_mudbio_N2dw(irk_fil(i))
             CALL ionc4_read_sublzxyt(file_obc_subs_e,obc_cv_name(i),tmp_ew, &
                  imin  ,obc_width,jmin,jmax,1,kmax,itcl_subs  ,imin  ,jmin,1,fillvar=rg_valmanq_io)
             cv_wat2_e(i,:,imin+1:imin+obc_width,:)=tmp_ew(:,imin+1:imin+obc_width,:)*unit_modif_mudbio_N2dw(irk_fil(i))
           END IF
         ENDDO
       END IF
         
     END IF ! ends initialization (nbouc = 1)

   !************************************************************
   !     deroulement normal
   !************************************************************

     IF ( t > obc_subs_t2-torigin ) THEN ! swap of arrays
       obc_subs_t1=obc_subs_t2
       itcl_subs=itcl_subs+1
       CALL ionc4_read_time(file_obc_3d_subs,itcl_subs,obc_subs_t2)
       IF ( l_obc_south ) THEN
         DO i=1,nv_adv
#if defined key_zerograd
           IF (obc_cv_name(i)/='none' .AND. obc_cv_name(i) /= 'zerograd') THEN
#else
           IF (obc_cv_name(i)/='none') THEN
#endif
             cv_wat1_s(i,:,:,jmin+1:jmin+obc_width)=cv_wat2_s(i,:,:,jmin+1:jmin+obc_width)
             CALL ionc4_read_sublzxyt(file_obc_subs_s,obc_cv_name(i),tmp_ns, &
                  imin,imax,jmin  ,obc_width,1,kmax,itcl_subs,imin,jmin  ,1,fillvar=rg_valmanq_io)
             cv_wat2_s(i,:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)*unit_modif_mudbio_N2dw(irk_fil(i))
           END IF
         ENDDO
       END IF
         
       IF ( l_obc_north ) THEN
         DO i=1,nv_adv
#if defined key_zerograd
           IF (obc_cv_name(i)/='none' .AND. obc_cv_name(i) /= 'zerograd') THEN
#else
           IF (obc_cv_name(i)/='none') THEN
#endif
             cv_wat1_n(i,:,:,jmin+1:jmin+obc_width)=cv_wat2_n(i,:,:,jmin+1:jmin+obc_width)
             CALL ionc4_read_sublzxyt(file_obc_subs_n,obc_cv_name(i),tmp_ns, &
                  imin,imax,jmin  ,obc_width,1,kmax,itcl_subs,imin,jmin  ,1,fillvar=rg_valmanq_io)
             cv_wat2_n(i,:,:,jmin+1:jmin+obc_width)=tmp_ns(:,:,jmin+1:jmin+obc_width)*unit_modif_mudbio_N2dw(irk_fil(i))
           END IF
         ENDDO
       END IF
         
       IF ( l_obc_west ) THEN
         DO i=1,nv_adv
#if defined key_zerograd
           IF (obc_cv_name(i)/='none' .AND. obc_cv_name(i) /= 'zerograd') THEN
#else
           IF (obc_cv_name(i)/='none') THEN
#endif
             cv_wat1_w(i,:,imin+1:imin+obc_width,:)=cv_wat2_w(i,:,imin+1:imin+obc_width,:)
             CALL ionc4_read_sublzxyt(file_obc_subs_w,obc_cv_name(i),tmp_ew, &
                  imin  ,obc_width,jmin,jmax,1,kmax,itcl_subs,imin  ,jmin,1,fillvar=rg_valmanq_io)
             cv_wat2_w(i,:,imin+1:imin+obc_width,:)=tmp_ew(:,imin+1:imin+obc_width,:)*unit_modif_mudbio_N2dw(irk_fil(i))
           END IF
         ENDDO
       END IF

       IF ( l_obc_east ) THEN
         DO i=1,nv_adv
#if defined key_zerograd
           IF (obc_cv_name(i)/='none' .AND. obc_cv_name(i) /= 'zerograd') THEN
#else
           IF (obc_cv_name(i)/='none') THEN
#endif
             cv_wat1_e(i,:,imin+1:imin+obc_width,:)=cv_wat2_e(i,:,imin+1:imin+obc_width,:)
             CALL ionc4_read_sublzxyt(file_obc_subs_e,obc_cv_name(i),tmp_ew, &
                  imin  ,obc_width,jmin,jmax,1,kmax,itcl_subs,imin  ,jmin,1,fillvar=rg_valmanq_io)
             cv_wat2_e(i,:,imin+1:imin+obc_width,:)=tmp_ew(:,imin+1:imin+obc_width,:)*unit_modif_mudbio_N2dw(irk_fil(i))
           END IF
         ENDDO
       END IF
        
     END IF ! end swap and reading data
      
     ! time interpolation 
 
     alf=sngl(obc_subs_t2-torigin-t)/sngl(obc_subs_t2-obc_subs_t1)

     IF ( l_obc_south ) THEN
       DO nv=1,nv_adv
#if defined key_zerograd
           IF (obc_cv_name(nv)/='none' .AND. obc_cv_name(nv) /= 'zerograd') THEN
#else
         IF (obc_cv_name(nv)/='none') THEN
#endif
           DO j=jmin+1,jmin+obc_width
             IF_MPI (ljmin<=j .AND. j<=ljmax) THEN
               obc_cv_wat_s(nv,:,limin:limax,j)=  &
                alf*cv_wat1_s(nv,:,limin:limax,j) &
                +(1.0_rsh-alf)*cv_wat2_s(nv,:,limin:limax,j)
             ENDIF_MPI
           END DO
         ELSE
          obc_cv_wat_s(nv,:,limin:limax,jmin+1:jmin+obc_width)=cini_wat(nv)
         END IF
       ENDDO
     END IF

     IF ( l_obc_north ) THEN
       DO nv=1,nv_adv
#if defined key_zerograd
           IF (obc_cv_name(nv)/='none' .AND. obc_cv_name(nv) /= 'zerograd') THEN
#else
         IF (obc_cv_name(nv)/='none') THEN
#endif
           DO j=jmax-obc_width+1,jmax
             IF_MPI (ljmin<=j .AND. j<=ljmax) THEN
               obc_cv_wat_n(nv,:,limin:limax,j)=  &
                alf*cv_wat1_n(nv,:,limin:limax,j-jmax+jmin+obc_width) &
                +(1.0_rsh-alf)*cv_wat2_n(nv,:,limin:limax,j-jmax+jmin+obc_width)
             ENDIF_MPI
           END DO
         ELSE
           obc_cv_wat_n(nv,:,limin:limax,jmax-obc_width+1:jmax)=cini_wat(nv)
         END IF
       ENDDO
     END IF

     IF ( l_obc_west ) THEN
       DO nv=1,nv_adv
#if defined key_zerograd
           IF (obc_cv_name(nv)/='none' .AND. obc_cv_name(nv) /= 'zerograd') THEN
#else
         IF (obc_cv_name(nv)/='none') THEN
#endif
           DO i=imin+1,imin+obc_width
             IF_MPI (limin<=i .AND. i<=limax) THEN
               obc_cv_wat_w(nv,:,i,ljmin:ljmax)=  &
                alf*cv_wat1_w(nv,:,i,ljmin:ljmax) &
                +(1.0_rsh-alf)*cv_wat2_w(nv,:,i,ljmin:ljmax)
             ENDIF_MPI
           END DO
         ELSE
          obc_cv_wat_w(nv,:,imin+1:imin+obc_width,ljmin:ljmax)=cini_wat(nv)
         END IF
       ENDDO
     END IF

     IF ( l_obc_east ) THEN
       DO nv=1,nv_adv
#if defined key_zerograd
           IF (obc_cv_name(nv)/='none' .AND. obc_cv_name(nv) /= 'zerograd') THEN
#else
         IF (obc_cv_name(nv)/='none') THEN
#endif
           DO i=imax-obc_width+1,imax
             IF_MPI (limin<=i .AND. i<=limax) THEN
               obc_cv_wat_e(nv,:,i,ljmin:ljmax)=  &
                alf*cv_wat1_e(nv,:,i-imax+imin+obc_width,ljmin:ljmax) &
                +(1.0_rsh-alf)*cv_wat2_e(nv,:,i-imax+imin+obc_width,ljmin:ljmax)
             ENDIF_MPI
           END DO
         ELSE
           obc_cv_wat_e(nv,:,imax-obc_width+1:imax,ljmin:ljmax)=cini_wat(nv)
         END IF
       ENDDO
     END IF

   END IF ! end test on obc reading requirement

   PRINT_DBG*, 'exit OBC_SUBS routine'

  END SUBROUTINE obc_subs

   !!======================================================================

  SUBROUTINE obc_subsref

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE obc_subsref  ***
   !&E
   !&E ** Purpose : Set substances open boundary condition to reference values
   !&E
   !&E ** Description : 
   !&E
   !&E ** Called by : step2dxy.F90,step2dyx.F90,step3dxy.F90,step3dyx.F90
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays :
   !&E
   !&E ** Modified variables : obc_cv_wat_e,obc_cv_wat_n,obc_cv_wat_s,obc_cv_wat_w
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2009-09-11 (S. Michel, HOCER) original code
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   !! * Arguments

   !! * Local declarations
   INTEGER :: nv
#ifdef key_lagoon
   INTEGER              :: ng,i0,j0
#endif

   !!----------------------------------------------------------------------
   !! * Executable part

   PRINT_DBG*, 'enter OBC_SUBSREF routine'

   !!======================================================================

#ifdef key_lagoon
     DO ng=1,nbgrau
           obc_cv_wat_grau(:,ng)=0.0_rsh
     ENDDO
#else
   IF (l_obc_south) THEN
     DO nv=1,nv_adv
       obc_cv_wat_s(nv,:,limin:limax,jmin+1:jmin+obc_width)=cini_wat(nv)
     ENDDO
   END IF
   IF (l_obc_north) THEN
     DO nv=1,nv_adv
       obc_cv_wat_n(nv,:,limin:limax,jmax-obc_width+1:jmax)=cini_wat(nv)
     ENDDO
   END IF
   IF (l_obc_west) THEN
     DO nv=1,nv_adv
       obc_cv_wat_w(nv,:,imin+1:imin+obc_width,ljmin:ljmax)=cini_wat(nv)
     ENDDO
   END IF
   IF (l_obc_east) THEN
     DO nv=1,nv_adv
       obc_cv_wat_e(nv,:,imax-obc_width+1:imax,ljmin:ljmax)=cini_wat(nv)
     ENDDO
   END IF
#endif

   PRINT_DBG*, 'exit OBC_SUBSREF routine'

  END SUBROUTINE obc_subsref

   !!======================================================================

#endif

 END MODULE obcogcm
