!**********************************************************
!
       subroutine tide_calcoef(tide_coef)
!
!**********************************************************
!
!    objet    : calcul des coefficients de maree a destination
!               de tide_harmcompo pour la realisation de marees fictives
!               de coefficients donnes.
!
!     appelant         : tide_harmcompo
!                      :
!     appeles          : tool_datosec
!                      :
!     canal d ecriture :
!                      :
!     canal de lecture : 29
!                      :
!     cle cpp          :
!                      :
!     variables in     : t,file_tide_M2coef, nbouc, l_tide_M2statcoef, tchrono
!     variables in     : nvalmax
!                      :
!     variables out    : tide_coef
!                      :
!     variables inout  : coefficient
!                      :
!     dernieres modifs : 13/06/2004
!&E       !  2009-12  (S. Michel, HOCER) check consistency between tide and simulation dates
!                      :
!***********************************************************
!
use comvars2d
   USE comstatic, ONLY : coefficient
use comaree
!
implicit none
!
character(len=lchain) :: rec
integer               :: i,kk,eof
real(kind=rsh)        :: val1,val2,tide_coef
real(kind=rlg)        :: dt1,tint1,tint2,tool_datosec,t11
!
!************************************************************
!     Executable statements
!************************************************************
!
!  initialisation, lecture des donnees du fichier
!------------------------------------------------
!
       if(nbouc == 0 .and. .not.l_tide_M2statcoef) then
           allocate(coefficient(nvalmax))
           open(29,file = file_tide_M2coef,form='formatted')
           i=1
           read(29,*)
           read(29,*)
           read(29,*)
           read(29,*)
           read(29,'(a)',end=999) rec
           kk=index(rec,'/')-2
           read(rec(kk+19:),*) val1
           tint1 = tool_datosec(rec(kk:kk+18))
!
      t11=tchrono(i)
      do while(t11 < tint1.and.i<=nvalmax)
        coefficient(i)=val1
        i=i+1
        if(i<=nvalmax) t11=tchrono(i)
      end do

! If the simulation starts before the first data record,
! the tide coefficients are set to the first available data.
       IF(i > 1) THEN
         WRITE(iwarnlog,*) ' '
         WRITE(iwarnlog,*) 'WARNING: In TIDECALCOEF.F90, the tide coefficients'
         WRITE(iwarnlog,*) 'are not available from the beginning of the simulation.'
         WRITE(iwarnlog,*) 'The first values from the file: ',TRIM(file_tide_M2coef)
         WRITE(iwarnlog,*) 'corresponding to the date : ',rec(kk:kk+18)
         WRITE(iwarnlog,*) 'are repeated for the first ',i,' input time-steps.'
       ENDIF

      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:),*) val2
         tint2 = tool_datosec(rec(kk:kk+18))
         do while((tchrono(i)-tint1)*(tchrono(i)-tint2) <= 0.0_rlg)
          dt1=(tchrono(i)-tint1)/(tint2-tint1)
          coefficient(i)=val1*(1.0_rlg-dt1)+val2*dt1
          i=i+1
          if( i > nvalmax) goto 999 
         end do 
         tint1=tint2
         val1=val2
        end if
       end do
      end if
!
  999 continue     ! fin de lecture du fichier de coefficient
!
      if(i < nvalmax) then
        WRITE(iwarnlog,*) ' '
        WRITE(iwarnlog,*) 'WARNING: In TIDECALCOEF.F90, the tide coefficients'
        WRITE(iwarnlog,*) 'are not available to the end of the simulation.'
        WRITE(iwarnlog,*) 'The last values from the file: ',TRIM(file_tide_M2coef)
        WRITE(iwarnlog,*) 'corresponding to the date : ',rec(kk:kk+18)
        WRITE(iwarnlog,*) 'are repeated for the last ',nvalmax-i+1,' input time-steps.'
        coefficient(i:nvalmax)=val2
      endif
!
     close(29)
    endif
!
!**********************
! deroulement normal
!**********************
!
       tide_coef=1.0_rsh
       i=indchrono 
       if(l_tide_M2harm .and. .not.l_tide_M2statcoef) then
        dt1=(t-tchrono(i))/(tchrono(i+1)-tchrono(i))
        tide_coef=dt1*coefficient(i+1)+(1.0_rlg-dt1)*coefficient(i)
        tide_coef=tide_coef/70.0_rsh
       endif
       if(l_tide_M2harm .and. l_tide_M2statcoef) tide_coef=tide_M2coef/70.0_rsh
!
end subroutine tide_calcoef
