 MODULE offline
   !!======================================================================
   !!                   ***  MODULE  offline  ***
   !! Offline : estimate trajectories or advection of substances
   !!           without any estimate of the dynamics
   !! History :
   !!        !  01-2011 (R. Ramel Alyotech) Ionc library in fortran 90/95, netcdf4 and MPI
   !!
   !!======================================================================

#include "toolcpp.h"

   !! * Modules used
   USE parameters
   USE ionc4, ONLY : ionc4_openr,ionc4_read_time,ionc4_read_subxyt,ionc4_read_subzxyt,&
                     ionc4_close
   USE_MPI toolmpi, ONLY : ex_j_rlg,ex_i_rlg,ex_i_rsh,ex_j_rsh

   IMPLICIT NONE
   PRIVATE

   !! * Accessibility
#ifdef key_offline
   PUBLIC dyn3dxy_offline             ! routine called by step3dxy
   PUBLIC dyn3dyx_offline             ! routine called by step3dyx
#endif

   !! * Shared or public module variables

                          ! directory of files (containing conservative
                          ! averaged velocities, water depths and sea level)
                          ! path to list_offline.dat file
   CHARACTER(len=lchain), PUBLIC           :: path_offline
                          ! set to .true. to estimate the buoyancy
   LOGICAL, PUBLIC                         :: l_bz_offline


   !! * Private variables

#ifdef key_offline

                          ! file containing all the file names to be read
   CHARACTER(len=lchain)              :: list_offline

                          ! file name (one record/date by file)
   CHARACTER(len=lchain), DIMENSION(:), ALLOCATABLE :: file_offline

                          ! number of record
   INTEGER                            :: ig_rec_off,                          &
                          ! number of file
                                         id_off,                              &
                          ! number of available files
                                         id_offmax

                          ! time of each record for interpolation
   REAL(KIND=rlg)                     :: off_t1,off_t2

                          ! chain describing the date in file name
   REAL(KIND=rlg), DIMENSION(:), ALLOCATABLE        :: time_offline

                          ! arrays of (conservative) data read at record 1 or 2
   REAL(KIND=rsh), DIMENSION(imin:imax,jmin:jmax)  :: hexr2       ! hx+ssh
   REAL(KIND=rsh), DIMENSION(imin:imax,jmin:jmax)  :: heyr2       ! hy+ssh
   REAL(KIND=rsh), DIMENSION(imin:imax,jmin:jmax)  :: xemr2       ! ssh
   REAL(KIND=rsh), DIMENSION(kmax,imin:imax,jmin:jmax)  :: salr1,salr2   ! salinity 
   REAL(KIND=rsh), DIMENSION(kmax,imin:imax,jmin:jmax)  :: tempr1,tempr2 ! temperature
   REAL(KIND=rsh), DIMENSION(kmax,imin:imax,jmin:jmax)  :: uzr2          ! 3d zonal velocity
   REAL(KIND=rsh), DIMENSION(kmax,imin:imax,jmin:jmax)  :: vzr2          ! 3d meridional velocity
   REAL(KIND=rsh), DIMENSION(kmax,imin:imax,jmin:jmax)  :: kzr2          ! turbulent coefficient
!  REAL(KIND=rlg)                     :: dg_ndt_off


 CONTAINS
   !!======================================================================

  SUBROUTINE dyn3dxy_offline(xev,xeuv,uz,vz,boz,pn2)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE dyn3dxy_offline  ***
   !&E
   !&E ** Purpose : Read data in conservative flux form and estimate the
   !&E              buoyancy (if required) and other variables (especially
   !&E              water fluxes and water depthd) to simulate trajectories
   !&E              and substances advection
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by : step3dxy.F90
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays : xev,xeuv,uz,vz,boz,pn2,kz,xeadv
   !&E                     hex,hey,htx,hty,hx,hy,fxeau,fyeau,sal,temp
   !&E
   !&E
   !&E ** Modified variables : hex,hey,htx,hty,fxeau,fyeau,sal,temp
   !&E                         xeuv,uz,vz,boz,kz,xeadv
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2008-12  (M. Jouan, V. Garnier)  Original code
   !&E       !  2011-03  (F. Dumas) remove case 2 and 3 on tidal flats
   !&E       !  2011-11  (V. Garnier) Spatial extension of ssh(liminm2:limaxp2,ljminm2:ljmaxp2)
   !&E
   !&E---------------------------------------------------------------------
   !! * Module used
   USE comvars2d, ONLY : nbouc,halfdt,t,hex,hey,htx,hty,hx,hy,jh,jb,ig,id,h0fond
   USE comfluxdo, ONLY : fxeau,fyeau,xeadv
   USE comvarp2d, ONLY : sal, temp
   USE comvars3d, ONLY : l_stateeq_lin,kz

   !! * Arguments
   REAL(KIND=rsh), DIMENSION(liminm2:limaxp2,ljminm2:ljmaxp2), INTENT( inout ) :: xev
   REAL(KIND=rsh), DIMENSION(liminm2:limaxp2,ljminm2:ljmaxp2), INTENT( inout ) :: xeuv
   REAL(KIND=rsh), DIMENSION(kmax,liminm2:limaxp2,ljminm2:ljmaxp2), INTENT( inout ) :: uz,vz
   REAL(KIND=rsh), DIMENSION(kmax,liminm1:limaxp2,ljminm1:ljmaxp2), INTENT( inout ) :: boz
   REAL(KIND=rsh), DIMENSION(kmax,limin  :limax  ,ljmin  :ljmax  ), INTENT( inout ) :: pn2

   !! * Local declarations
   INTEGER                     :: i,j,k
   REAL(KIND=rsh)              :: alf

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

   ! Initialization
   ! --------------
 
   IF (nbouc==1) CALL offline_readinit()

   ! swap and read of next file
   ! --------------------------

   IF ( t > off_t2 ) CALL offline_readnext

   ! Initialize the time averaged velocities
   ! ---------------------------------------

   uz(:,limin:limax,ljmin:ljmax)=uzr2(:,limin:limax,ljmin:ljmax)
   vz(:,limin:limax,ljmin:ljmax)=vzr2(:,limin:limax,ljmin:ljmax)


   IF (l_bz_offline) THEN

     ! time interpolation of temperature and salinity for the buoyancy estimate 
     alf=sngl(t-off_t1)/sngl(off_t2-off_t1)
     sal(:,liminm1:limaxp2,ljminp1:ljmaxp2)=(1.0_rsh-alf)*salr1(:,liminm1:limaxp2,ljminp1:ljmaxp2) &
                                                     +alf*salr2(:,liminm1:limaxp2,ljminp1:ljmaxp2)
     temp(:,liminm1:limaxp2,ljminp1:ljmaxp2)=(1.0_rsh-alf)*tempr1(:,liminm1:limaxp2,ljminp1:ljmaxp2) &
                                                      +alf*tempr2(:,liminm1:limaxp2,ljminp1:ljmaxp2)

     ! Estimate of the buoyancy
     IF (l_stateeq_lin) CALL ts_stateeq_linear( sal,temp,boz,pn2)

     IF (.NOT.l_stateeq_lin) CALL ts_stateeq_blumberg(sal,temp,boz,pn2)

   END IF

   ! other variables remain constant over the period between two data files
   xeuv(limin:limax,ljmin:ljmax)=xemr2(limin:limax,ljmin:ljmax)
   hex(liminm2:limaxp2,ljmin:ljmax)= hexr2(liminm2:limaxp2,ljmin:ljmax)
   hey(limin:limax,ljminm2:ljmaxp2)= heyr2(limin:limax,ljminm2:ljmaxp2) 
   kz(:,liminm1:limaxp1,ljminm1:ljmaxp1)=kzr2(:,liminm1:limaxp1,ljminm1:ljmaxp1)


   ! Initialization of the sea surface height for the advection of the substances
   ! ----------------------------------------------------------------------------

   IF (nbouc==1) THEN
     xev(limin:limax,ljmin:ljmax)=xeuv(limin:limax,ljmin:ljmax)
     xeadv(limin:limax,ljmin:ljmax)=xeuv(limin:limax,ljmin:ljmax)
   END IF
  
   ! Estimate of htx and hty for trajectories
   ! ----------------------------------------

   DO j=ljmin,ljmaxm1
     DO i=limin,limaxm1
       htx(i,j)=hx(i,j)+0.5_rsh*(xev(i,j)+xev(i+1,j))
       IF ( htx(i,j)<=0.0_rsh .AND. ( hx(i,j)+xev(i,j) > 0.0_rsh .OR. hx(i,j)+xev(i+1,j) > 0.0_rsh) ) &
         htx(i,j)=hx(i,j)+MAX(xev(i,j),xev(i+1,j))
       hty(i,j)=hy(i,j)+0.5_rsh*(xev(i,j)+xev(i,j+1))
       IF ( hty(i,j)<=0.0_rsh .AND. ( hy(i,j)+xev(i,j) > 0.0_rsh .OR. hy(i,j)+xev(i,j+1) > 0.0_rsh) ) &
         hty(i,j)=hy(i,j)+MAX(xev(i,j),xev(i,j+1))
     END DO
   END DO
   IF_MPI (ljmax==jmax) THEN
     DO i=limin,limaxm1
       IF (hx(i,jmax) /= -valmanq-h0fond) THEN
         htx(i,jmax)=hx(i,jmax)+0.5_rsh*(xev(i,jmax)+xev(i+1,jmax))
         IF ( htx(i,jmax)<=0.0_rsh .AND. ( hx(i,jmax)+xev(i,jmax) > 0.0_rsh .OR. hx(i,jmax)+xev(i+1,jmax) > 0.0_rsh) ) &
           htx(i,jmax)= hx(i,jmax)+MAX(xev(i,jmax),xev(i+1,jmax))
       ENDIF
     ENDDO
   ENDIF_MPI
   IF_MPI (limax==imax) THEN
      DO j=ljmin,ljmaxm1
        IF (hy(imax,j) /= -valmanq-h0fond) THEN
          hty(imax,j)=hy(imax,j)+0.5_rsh*(xev(imax,j)+xev(imax,j+1))
          IF ( hty(imax,j)<=0.0_rsh .AND. ( hy(imax,j)+xev(imax,j) > 0.0_rsh .OR. hy(imax,j)+xev(imax,j+1) > 0.0_rsh) ) &
            hty(imax,j)=hy(imax,j)+MAX(xev(imax,j),xev(imax,j+1))
        ENDIF
      ENDDO
   ENDIF_MPI

OMPMPI barrier
OMPMPI master
   CALL_MPI ex_i_rsh(-2,2,1,liminm2,limaxp2,ljminm2,ljmaxp2,htx(liminm2:limaxp2,ljminm2:ljmaxp2))
   CALL_MPI ex_j_rsh(-2,2,1,liminm2,limaxp2,ljminm2,ljmaxp2,htx(liminm2:limaxp2,ljminm2:ljmaxp2))
   CALL_MPI ex_i_rsh(-2,2,1,liminm2,limaxp2,ljminm2,ljmaxp2,hty(liminm2:limaxp2,ljminm2:ljmaxp2))
   CALL_MPI ex_j_rsh(-2,2,1,liminm2,limaxp2,ljminm2,ljmaxp2,hty(liminm2:limaxp2,ljminm2:ljmaxp2))
OMPMPI end master
OMPMPI barrier
OMPMPI flush(htx,hty)

   ! estimate of water fluxes as in dyn3dxy (used by the tracer advection part)
   ! --------------------------------------------------------------------------

!$OMP DO SCHEDULE(runtime)
   DO j=ljmin,ljmax
     IF ((j.ge.jmin+2).and.(j.le.jmax-1)) THEN
       DO i=MAX0(limin,ig(j)),MIN0(limax,id(j)-1)
#if defined key_siggen || defined key_gencoord
         fxeau(:,i,j)=hzex(:,i,j)*uz(:,i,j)*halfdt
#else
         fxeau(:,i,j)=hex(i,j)*uz(:,i,j)*halfdt
#endif
       END DO
     END IF
     DO i=liminp2,limaxm1
#if defined key_siggen || defined key_gencoord
       IF ((j.ge.jb(i)).and.(j.le.jh(i)-1)) fyeau(:,i,j)=hzey(:,i,j)*vz(:,i,j)*halfdt
#else
       IF ((j.ge.jb(i)).and.(j.le.jh(i)-1)) fyeau(:,i,j)=hey(i,j)*vz(:,i,j)*halfdt
#endif
     END DO
   END DO
!$OMP END DO
!$OMP END PARALLEL

   CALL_MPI ex_i_rlg(-1,0,kmax,liminm1,limax,ljmin,ljmax,fxeau(:,liminm1:limax,ljmin:ljmax))
   CALL_MPI ex_j_rlg(-1,0,kmax,limin,limax,ljminm1,ljmax,fyeau(:,limin:limax,ljminm1:ljmax))

  END SUBROUTINE dyn3dxy_offline

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

  SUBROUTINE dyn3dyx_offline(xeu,xeuv,vz,uz,boz,pn2)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE dyn3dyx_offline  ***
   !&E
   !&E ** Purpose : Read data in conservative flux form and estimate the
   !&E              buoyancy (if required) and other variables (especially
   !&E              water fluxes and water depthd) to simulate trajectories
   !&E              and substances advection
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by : step3dyx.F90
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays : xev,xeuv,uz,vz,boz,pn2,kz
   !&E                     hex,hey,htx,hty,hx,hy,fxeau,fyeau,sal,temp
   !&E
   !&E
   !&E ** Modified variables : hex,hey,htx,hty,fxeau,fyeau,sal,temp
   !&E                         xeuv,uz,vz,boz,pn2,kz
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2008-12  (M. Jouan, V. Garnier)  Original code
   !&E       !  2011-03  (F. Dumas) remove case 2 and 3 on tidal flats
   !&E       !  2011-11  (V. Garnier) Spatial extension of ssh(liminm2:limaxp2,ljminm2:ljmaxp2)
   !&E
   !&E---------------------------------------------------------------------
   !! * Module used
   USE comvars2d, ONLY : halfdt,t,hex,hey,htx,hty,hx,hy,jh,jb,ig,id,h0fond
   USE comfluxdo, ONLY : fxeau,fyeau
   USE comvarp2d, ONLY : sal, temp
   USE comvars3d, ONLY : l_stateeq_lin,kz


   !! * Arguments
   REAL(KIND=rsh), DIMENSION(liminm2:limaxp2,ljminm2:ljmaxp2), INTENT( inout ) :: xeuv
   REAL(KIND=rsh), DIMENSION(liminm2:limaxp2,ljminm2:ljmaxp2), INTENT( in ) :: xeu
   REAL(KIND=rsh), DIMENSION(kmax,liminm2:limaxp2,ljminm2:ljmaxp2), INTENT( inout ) :: uz,vz
   REAL(KIND=rsh), DIMENSION(kmax,liminm1:limaxp2,ljminm1:ljmaxp2), INTENT( inout ) :: boz
   REAL(KIND=rsh), DIMENSION(kmax,limin  :limax  ,ljmin  :ljmax  ), INTENT( inout ) :: pn2

   !! * Local declarations
   INTEGER                      :: i,j,k
   REAL(KIND=rsh)               :: alf

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

   ! swap and read of next file
   ! --------------------------

   IF ( t > off_t2 ) CALL offline_readinit()


   ! Initialize the time averaged velocities
   ! ---------------------------------------

   uz(:,limin:limax,ljmin:ljmax)=uzr2(:,limin:limax,ljmin:ljmax)
   vz(:,limin:limax,ljmin:ljmax)=vzr2(:,limin:limax,ljmin:ljmax)


   IF (l_bz_offline) THEN

     ! time interpolation of temperature and salinity for the buoyancy estimate 
     alf=sngl(t-off_t1)/sngl(off_t2-off_t1)
     sal(:,liminm1:limaxp2,ljminp1:ljmaxp2)=(1.0_rsh-alf)*salr1(:,liminm1:limaxp2,ljminp1:ljmaxp2) &
                                                     +alf*salr2(:,liminm1:limaxp2,ljminp1:ljmaxp2)
     temp(:,liminm1:limaxp2,ljminp1:ljmaxp2)=(1.0_rsh-alf)*tempr1(:,liminm1:limaxp2,ljminp1:ljmaxp2) &
                                                      +alf*tempr2(:,liminm1:limaxp2,ljminp1:ljmaxp2)

     ! Estimate of the buoyancy
     IF (l_stateeq_lin) CALL ts_stateeq_linear( sal,temp,boz,pn2)

     IF (.NOT.l_stateeq_lin) CALL ts_stateeq_blumberg(sal,temp,boz,pn2)

   END IF

   ! other variables remain constant over the period between two data files
   xeuv(limin:limax,ljmin:ljmax)=xemr2(limin:limax,ljmin:ljmax)
   hex(liminm2:limaxp2,ljmin:ljmax)= hexr2(liminm2:limaxp2,ljmin:ljmax)
   hey(limin:limax,ljminm2:ljmaxp2)= heyr2(limin:limax,ljminm2:ljmaxp2) 
   kz(:,liminm1:limaxp1,ljminm1:ljmaxp1)=kzr2(:,liminm1:limaxp1,ljminm1:ljmaxp1)


   ! Estimate of htx and hty for trajectories
   ! ----------------------------------------

   DO j=ljmin,ljmaxm1
     DO i=limin,limaxm1
       htx(i,j)=hx(i,j)+0.5_rsh*(xeu(i,j)+xeu(i+1,j))
       IF ( htx(i,j)<=0.0_rsh .AND. ( hx(i,j)+xeu(i,j) > 0.0_rsh .OR. hx(i,j)+xeu(i+1,j) > 0.0_rsh) ) &
         htx(i,j)=hx(i,j)+MAX(xeu(i,j),xeu(i+1,j))
       hty(i,j)=hy(i,j)+0.5_rsh*(xeu(i,j)+xeu(i,j+1))
       IF ( hty(i,j)<=0.0_rsh .AND. ( hy(i,j)+xeu(i,j) > 0.0_rsh .OR. hy(i,j)+xeu(i,j+1) > 0.0_rsh) ) &
         hty(i,j)=hy(i,j)+MAX(xeu(i,j),xeu(i,j+1))
     END DO
   END DO
   IF_MPI (ljmax==jmax) THEN
     DO i=limin,limaxm1
       IF (hx(i,jmax) /= -valmanq-h0fond) THEN
         htx(i,jmax)=hx(i,jmax)+0.5_rsh*(xeu(i,jmax)+xeu(i+1,jmax))
         IF ( htx(i,jmax)<=0.0_rsh .AND. ( hx(i,jmax)+xeu(i,jmax) > 0.0_rsh .OR. hx(i,jmax)+xeu(i+1,jmax) > 0.0_rsh) ) &
           htx(i,jmax)= hx(i,jmax)+MAX(xeu(i,jmax),xeu(i+1,jmax))
       ENDIF
     ENDDO
   ENDIF_MPI
   IF_MPI (limax==imax) THEN
      DO j=ljmin,ljmaxm1
        IF (hy(imax,j) /= -valmanq-h0fond) THEN
          hty(imax,j)=hy(imax,j)+0.5_rsh*(xeu(imax,j)+xeu(imax,j+1))
          IF ( hty(imax,j)<=0.0_rsh .AND. ( hy(imax,j)+xeu(imax,j) > 0.0_rsh .OR. hy(imax,j)+xeu(imax,j+1) > 0.0_rsh) ) &
           hty(imax,j)=hy(imax,j)+MAX(xeu(imax,j),xeu(imax,j+1))
        ENDIF
      ENDDO
   ENDIF_MPI

OMPMPI barrier
OMPMPI master
   CALL_MPI ex_i_rsh(-2,2,1,liminm2,limaxp2,ljminm2,ljmaxp2,htx(liminm2:limaxp2,ljminm2:ljmaxp2))
   CALL_MPI ex_j_rsh(-2,2,1,liminm2,limaxp2,ljminm2,ljmaxp2,htx(liminm2:limaxp2,ljminm2:ljmaxp2))
   CALL_MPI ex_i_rsh(-2,2,1,liminm2,limaxp2,ljminm2,ljmaxp2,hty(liminm2:limaxp2,ljminm2:ljmaxp2))
   CALL_MPI ex_j_rsh(-2,2,1,liminm2,limaxp2,ljminm2,ljmaxp2,hty(liminm2:limaxp2,ljminm2:ljmaxp2))
OMPMPI end master
OMPMPI barrier
OMPMPI flush(htx,hty)

   ! Estimate of water fluxes as in dyn3dyx (used by the tracer advection part)
   ! --------------------------------------------------------------------------

!$OMP DO SCHEDULE(runtime)
   DO j=ljmin,ljmax
     IF ( j>=jmin+2 .and. j<=jmax-1 ) THEN
       DO i=MAX0(limin,ig(j)),MIN0(limax,id(j)-1)
         DO k=1,kmax
#if defined key_siggen || defined key_gencoord
           fxeau(k,i,j)=hzex(k,i,j)*uz(k,i,j)*halfdt
#else
           fxeau(k,i,j)=hex(i,j)*uz(k,i,j)*halfdt
#endif
         END DO
       END DO
     END IF
     DO i=liminp2,limaxm1
       IF ( j>=jb(i) .and. j<=jh(i)-1 ) THEN
         DO k=1,kmax
#if defined key_siggen || defined key_gencoord
            fyeau(k,i,j)=hzey(k,i,j)*vz(k,i,j)*halfdt
#else
            fyeau(k,i,j)=hey(i,j)*vz(k,i,j)*halfdt
#endif
         END DO
       END IF
     END DO
   END DO
!$OMP END DO
!$OMP END PARALLEL

   CALL_MPI ex_i_rlg(-1,0,kmax,liminm1,limax,ljmin,ljmax,fxeau(:,liminm1:limax,ljmin:ljmax))
   CALL_MPI ex_j_rlg(-1,0,kmax,limin,limax,ljminm1,ljmax,fyeau(:,limin:limax,ljminm1:ljmax))


  END SUBROUTINE dyn3dyx_offline

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

  SUBROUTINE offline_readinit

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE offline_readinit  ***
   !&E
   !&E ** Purpose : Read data in conservative flux form
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by : dyn3dxy_offline.F90
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays : hexr2,heyr2,xemr2,uzr2,vzr2,kzr2,tempr1,tempr2,salr1,salr2
   !&E
   !&E ** Modified variables : hexr2,heyr2,xemr2,uzr2,vzr2,kzr2,tempr1,tempr2,salr1,salr2
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2008-12  (M. Jouan, V. Garnier)  Original code
   !&E
   !&E---------------------------------------------------------------------
   !! * Module used
   USE comvars2d,ONLY : name_in_sal,name_in_temp,name_in_mxe,    &
                        name_in_uz,name_in_vz,name_in_mhex,      &
                        name_in_mhey,name_in_mkz,                &
                        t

   !! * Arguments

   !! * Local declarations
   CHARACTER(LEN=lchain)        :: rec
   INTEGER                      :: eof,l,kk
   REAL(kind=rlg)               :: tool_datosec

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

   ! file containing all the file (in conservative flux form) to be read

   list_offline=TRIM(path_offline)//'list_offline.dat'

   ! Allocate file_offline and time_offline variables

   l=0
   OPEN(88,file=list_offline) !,form='unformatted')
   eof=0
   DO WHILE(eof==0)
     read(88,'(a)',iostat=eof,end=998)
     l=l+1
   END DO 
 998 CONTINUE
   CLOSE(88)
   IF (l == 0) THEN
     PRINT*,' '
     PRINT*,'OFF_LINE : no available files in ',TRIM(list_offline)
     PRINT*,'The simulation is stopped'
     PRINT*,' '
     STOP
   ELSE
     PRINT*,' '
     PRINT*,' OFF_LINE : ',l,' files available from ',TRIM(list_offline)
     PRINT*,' '
   END IF
   ALLOCATE(file_offline(l),time_offline(l))

   ! Initialize file_offline and time_offline variables

   OPEN(88,file=list_offline) !,form='unformatted')
   eof=0
   l=1

   DO WHILE(eof==0)
     read(88,'(a)',iostat=eof,end=999)file_offline(l)
     kk=index(file_offline(l),'.nc')-16
     rec=file_offline(l)(kk:kk+16)
     rec=rec(7:8)//'/'//rec(5:6)//'/'//rec(1:4)//'_'//rec(10:11)// &
         ':'//rec(12:13)//':'//rec(14:15)
     file_offline(l)=TRIM(path_offline)//TRIM(file_offline(l))
     time_offline(l)=tool_datosec(rec)
     id_offmax=l
     l=l+1
   ENDDO
 999 CONTINUE

   ! Check records to be read relative to the starting time of the simulation

   l=1
   id_off=1
   IF(t < time_offline(l)) THEN
     PRINT*,' '
     PRINT*,'OFF_LINE : starting time < first available file'
     PRINT*,'The simulation is stopped'
     PRINT*,' '
     STOP
   END IF
   DO WHILE(t > time_offline(l+1))
     l=l+1
   ENDDO
   id_off=l

   ! read data

   PRINT*,' '
   PRINT*,' OFF_LINE : reading of file :'
   PRINT*,TRIM(file_offline(id_off))
   PRINT*,' and file :'
   PRINT*,TRIM(file_offline(id_off+1))
   PRINT*,' '

   CALL ionc4_openr(TRIM(file_offline(id_off)),l_out_nc4par=.true.)
   CALL ionc4_openr(TRIM(file_offline(id_off+1)),l_out_nc4par=.true.)

   ig_rec_off=1
   CALL ionc4_read_time(file_offline(id_off),ig_rec_off,off_t1)
   CALL ionc4_read_time(file_offline(id_off+1),ig_rec_off,off_t2)


   CALL ionc4_read_subxyt(file_offline(id_off+1),name_in_mhex,hexr2, &
                          imin,imax,jmin,jmax,ig_rec_off,imin,jmin,fillvar=0.0_rsh)
   CALL ionc4_read_subxyt(file_offline(id_off+1),name_in_mhey,heyr2, &
                          imin,imax,jmin,jmax,ig_rec_off,imin,jmin,fillvar=0.0_rsh)
   CALL ionc4_read_subxyt(file_offline(id_off+1),name_in_mxe,xemr2, &
                          imin,imax,jmin,jmax,ig_rec_off,imin,jmin,fillvar=valmanq)

   CALL ionc4_read_subzxyt(file_offline(id_off+1),name_in_uz,uzr2, &
                           imin,imax,jmin,jmax,1,kmax,ig_rec_off,imin,jmin,1,fillvar=0.0_rsh)
   CALL ionc4_read_subzxyt(file_offline(id_off+1),name_in_vz,vzr2, &
                           imin,imax,jmin,jmax,1,kmax,ig_rec_off,imin,jmin,1,fillvar=0.0_rsh)

   CALL ionc4_read_subzxyt(file_offline(id_off+1),name_in_mkz,kzr2, &
                           imin,imax,jmin,jmax,1,kmax,ig_rec_off,imin,jmin,1,fillvar=valmanq)

   IF (l_bz_offline) THEN    ! to estimate the buyancy
     CALL ionc4_read_subzxyt(file_offline(id_off),name_in_temp,tempr1, &
                          imin,imax,jmin,jmax,1,kmax,ig_rec_off,imin,jmin,1,fillvar=valmanq)
     CALL ionc4_read_subzxyt(file_offline(id_off+1),name_in_temp,tempr2, &
                          imin,imax,jmin,jmax,1,kmax,ig_rec_off,imin,jmin,1,fillvar=valmanq)
     CALL ionc4_read_subzxyt(file_offline(id_off),name_in_sal,salr1, &
                          imin,imax,jmin,jmax,1,kmax,ig_rec_off,imin,jmin,1,fillvar=valmanq)
     CALL ionc4_read_subzxyt(file_offline(id_off+1),name_in_sal,salr2, &
                          imin,imax,jmin,jmax,1,kmax,ig_rec_off,imin,jmin,1,fillvar=valmanq)
   END IF

   CALL ionc4_close(file_offline(id_off))
   CALL ionc4_close(file_offline(id_off+1))
   
   ! number of intervals of time interpolation
!   dg_ndt_off=REAL((off_t2-off_t1)/(0.5_rlg*dt),rlg)

  END SUBROUTINE offline_readinit

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

  SUBROUTINE offline_readnext

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE offline_readnext  ***
   !&E
   !&E ** Purpose : Read data in conservative flux form (next record)
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by : dyn3dxy_offline.F90, dyn3dyx_offline.F90
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays : hexr2,heyr2,xemr2,uzr2,vzr2,kzr2,tempr2,salr2
   !&E
   !&E ** Modified variables : hexr2,heyr2,xemr2,uzr2,vzr2,kzr2,tempr2,salr2
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2008-12  (M. Jouan, V. Garnier)  Original code
   !&E
   !&E---------------------------------------------------------------------
   !! * Module used
   USE comvars2d,ONLY : name_in_sal,name_in_temp,name_in_mxe,    &
                        name_in_uz,name_in_vz,name_in_mhex,      &
                        name_in_mhey,name_in_mkz

   !! * Arguments

   !! * Local declarations

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

   ! swap arrays and dates

   off_t1=off_t2
   id_off=id_off+1
   IF (l_bz_offline) THEN 
     tempr1(:,liminm1:limaxp2,ljminp1:ljmaxp2)=tempr2(:,liminm1:limaxp2,ljminp1:ljmaxp2)
     salr1(:,liminm1:limaxp2,ljminp1:ljmaxp2)=salr2(:,liminm1:limaxp2,ljminp1:ljmaxp2)
   END IF

   PRINT*,'reading of the next file, (number ',id_off+1,') : ',TRIM(file_offline(id_off+1))
   IF (id_off+1.gt.id_offmax) THEN
     PRINT*,' '
     PRINT*,'OFF_LINE : no more available files in ',TRIM(list_offline)
     PRINT*,'to cover the period of simulation'
     PRINT*,'The simulation is stopped'
     PRINT*,' '
     STOP
   END IF

   ! Read data

   CALL ionc4_openr(TRIM(file_offline(id_off+1)),l_out_nc4par=.true.)
   CALL ionc4_read_time(file_offline(id_off+1),ig_rec_off,off_t2)

   print*,'off_t2',off_t2
      
!   dg_ndt_off=REAL((off_t2-off_t1)/(0.5_rlg*dt),rlg)   ! nb d'intervalles d'interpolation


   CALL ionc4_read_subxyt(file_offline(id_off+1),name_in_mxe,xemr2, &
                          imin,imax,jmin,jmax,ig_rec_off,imin,jmin,fillvar=valmanq)
   CALL ionc4_read_subxyt(file_offline(id_off+1),name_in_mhex,hexr2, &
                          imin,imax,jmin,jmax,ig_rec_off,imin,jmin,fillvar=0.0_rsh)
   CALL ionc4_read_subxyt(file_offline(id_off+1),name_in_mhey,heyr2, &
                          imin,imax,jmin,jmax,ig_rec_off,imin,jmin,fillvar=0.0_rsh)
   CALL ionc4_read_subzxyt(file_offline(id_off+1),name_in_uz,uzr2, &
                           imin,imax,jmin,jmax,1,kmax,ig_rec_off,imin,jmin,1,fillvar=0.0_rsh)
   CALL ionc4_read_subzxyt(file_offline(id_off+1),name_in_vz,vzr2, &
                           imin,imax,jmin,jmax,1,kmax,ig_rec_off,imin,jmin,1,fillvar=0.0_rsh)
   CALL ionc4_read_subzxyt(file_offline(id_off+1),name_in_mkz,kzr2, &
                           imin,imax,jmin,jmax,1,kmax,ig_rec_off,imin,jmin,1,fillvar=valmanq)
   IF (l_bz_offline) THEN
     CALL ionc4_read_subzxyt(file_offline(id_off+1),name_in_temp,tempr2, &
                             imin,imax,jmin,jmax,1,kmax,ig_rec_off,imin,jmin,1,fillvar=valmanq)
     CALL ionc4_read_subzxyt(file_offline(id_off+1),name_in_sal,salr2, &
                             imin,imax,jmin,jmax,1,kmax,ig_rec_off,imin,jmin,1,fillvar=valmanq)
   END IF

  CALL ionc4_close(file_offline(id_off+1))

  END SUBROUTINE offline_readnext

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

#endif

 END MODULE offline
