  SUBROUTINE tide_genpot

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE tide_genpot  ***
!&E
!&E ** Purpose : compute the tide potential generator
!&E
!&E ** Description :
!&E
!&E ** Called by : step
!&E
!&E ** External calls :
!&E
!&E ** Used ij-arrays : omegat,equitide,periode,nu,cos2g,sin2g cosg,sing
!&E                     tide_nbharmcp,coscafi,sin2fi,ig,id,jh,jb
!&E
!&E ** Modified variables : potgene
!&E
!&E ** Reference :
!&E
!&E ** History :
!&E       !  2008-07  (T. Odaka) MPI
!&E       !  2009-04  (V. Garnier) MPI2D
!&E       OMP OMP+MPI not readyyet
!&E
!&E---------------------------------------------------------------------





























































!! * Modules used!
   USE parameters
   USE comvars2d
   USE comstatic, ONLY : omegat
   USE commar
   USE comaree
   USE toolmpi,   ONLY : ex_i_rsh,ex_j_rsh
   IMPLICIT NONE

!! * Local declarations
   INTEGER        :: n,i,j
   REAL(kind=rsh) :: x3,x2,x1,sinomegat,cosomegat

!!----------------------------------------------------------------------
!! * Executable part
   
   potgene(:,ljmin:ljmax)=0.0_rsh
   DO n=1,tide_nbharmcp
     cosomegat = COS(omegat(nu(n)))
     sinomegat = SIN(omegat(nu(n)))
     x1 = 0.69_rsh*grav*equitide(nu(n)) ! 0.69 : Love number for load effects

     DO j=ljminp1,ljmax
       DO i=MAX0(limin,ig(j)),MIN0(limax,id(j))
         x2 = x1*coscafi(i,j)
         x3 = x1*sin2fi(i,j)
         IF(nu(n) <=28) THEN
           potgene(i,j)=potgene(i,j)+x2*(cosomegat*cos2g(i,j)-sinomegat*sin2g(i,j))
         ELSE
           potgene(i,j)=potgene(i,j)+x3*(cosomegat*cosg(i,j)-sinomegat*sing(i,j))
         ENDIF
       ENDDO
     ENDDO

     DO i=liminp1,limax
       IF (ljmin==jmin) THEN
         j=jb(i)
         x2 = x1*coscafi(i,j)
         x3 = x1*sin2fi(i,j)
         IF(nu(n) <= 28) THEN
           potgene(i,j)=potgene(i,j)+x2*(cosomegat*cos2g(i,j)-sinomegat*sin2g(i,j))
         ELSE
           potgene(i,j)=potgene(i,j)+x3*(cosomegat*cosg(i,j)-sinomegat*sing(i,j))
         ENDIF
       ENDIF

       IF (ljmax==jmax) THEN
         j=jh(i)
         x2 = x1*coscafi(i,j)
         x3 = x1*sin2fi(i,j)
         IF(nu(n) <=28) THEN
           potgene(i,j)=potgene(i,j)+x2*(cosomegat*cos2g(i,j)-sinomegat*sin2g(i,j))
         ELSE
           potgene(i,j)=potgene(i,j)+x3*(cosomegat*cosg(i,j)-sinomegat*sing(i,j))
         ENDIF
       ENDIF
     ENDDO ! ends loop on i
   ENDDO ! ends loop on n

   CALL ex_i_rsh(0,1,1,limin,limaxp1,ljmin,ljmaxp1,potgene(limin:limaxp1,ljmin:ljmaxp1))
   CALL ex_j_rsh(0,1,1,limin,limaxp1,ljmin,ljmaxp1,potgene(limin:limaxp1,ljmin:ljmaxp1))

  END SUBROUTINE tide_genpot
