  SUBROUTINE tool_interp2d_sflx(i1,j1,xdp,ydp,tabin,tabout)

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE tool_interp2d_sflx  ***
!&E
!&E ** Purpose : spatial interpolation of meteorological data
!&E
!&E ** Description :
!&E
!&E ** Called by : paramet
!&E
!&E ** External calls :
!&E
!&E ** Used ij-arrays : tabin, i1, j1, xdp, ydp
!&E
!&E ** Modified variables : tabout
!&E
!&E ** Reference :
!&E
!&E ** History :
!&E       !  2004-08-18
!&E
!&E---------------------------------------------------------------------





























































!! * Modules used
   USE parameters
!USE parameters ,only:rsh,riosh,imin,imax,jmin,jmax
   USE comvars2d,ONLY : jb,jh,ig,id,h0,hx,hy
   USE cometdyn, ONLY : imetmin,imetmax,jmetmin,jmetmax
   IMPLICIT NONE

!! * Arguments
   INTEGER         ,DIMENSION(limin:limax,ljmin:ljmax)        ,INTENT(in)  :: i1,j1
   REAL(kind=rsh)  ,DIMENSION(limin:limax,ljmin:ljmax)        ,INTENT(in)  :: xdp,ydp
   REAL(kind=riosh),DIMENSION(imetmin:imetmax,jmetmin:jmetmax),INTENT(in)  :: tabin
   REAL(kind=rsh)  ,DIMENSION(limin:limax,ljmin:ljmax)        ,INTENT(out) :: tabout

!! * Local declarations
   INTEGER        :: i,j,ii1,ii2,jj1,jj2
   REAL(kind=rsh) :: var1,var2

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

  
!$OMP do schedule(runtime),private(i,j,ii1,ii2,jj1,jj2,var1,var2)
   DO j = ljminp1,ljmax
     DO i=MAX0(limin,ig(j)),MIN0(limax,id(j))
      IF (h0(i,j)>-valmanq .and. hx(i,j)>-valmanq .and. hy(i,j)>-valmanq) THEN
       ii1=MAX(imetmin,MAX(i1(i,j),imetmin))
       ii2=MAX(imetmin,MIN(i1(i,j)+1,imetmax))
       jj1=MAX(jmetmin,MAX(j1(i,j),jmetmin))
       jj2=MAX(jmetmin,MIN(j1(i,j)+1,jmetmax))
!print*,"i,j",i,j,h0(i,j)
       if (ii1 ==1 .or. ii2==1) then
        print*,"ii1,ii2,jj1,jj2",ii1,ii2,jj1,jj2
        print*,"bornes meteo:",imetmin,imetmax,jmetmin,jmetmax
        stop
       endif
       var1=xdp(i,j)*tabin(ii2,jj1)+(1.0_rsh-xdp(i,j))*tabin(ii1,jj1)
       var2=xdp(i,j)*tabin(ii2,jj2)+(1.0_rsh-xdp(i,j))*tabin(ii1,jj2)
       tabout(i,j)=(ydp(i,j)*var2+(1.0_rsh-ydp(i,j))*var1)
      ENDIF
     END DO
   END DO
!$OMP end do

! fill borders

!$OMP do private(i,j,ii1,ii2,jj1,jj2,var1,var2)
   DO i = liminp1,limax

! bottom
     j=jb(i)
     IF ((j.LE.ljmax).AND.(j.GE.ljmin))THEN
!IF (h0(i,j)/=-valmanq) THEN
      IF (h0(i,j)>-valmanq .and. hx(i,j)>-valmanq .and. hy(i,j)>-valmanq) THEN
       ii1=MAX(imetmin,MAX(i1(i,j),imetmin))
       ii2=MAX(imetmin,MIN(i1(i,j)+1,imetmax))
       jj1=MAX(jmetmin,MAX(j1(i,j),jmetmin))
       jj2=MAX(jmetmin,MIN(j1(i,j)+1,jmetmax))
       var1=xdp(i,j)*tabin(ii2,jj1)+(1.0_rsh-xdp(i,j))*tabin(ii1,jj1)
       var2=xdp(i,j)*tabin(ii2,jj2)+(1.0_rsh-xdp(i,j))*tabin(ii1,jj2)
       tabout(i,j)=(ydp(i,j)*var2+(1.0_rsh-ydp(i,j))*var1)
      ENDIF
     ENDIF

! top
     j=jh(i)
     IF ((j.LE.ljmax).AND.(j.GE.ljmin))THEN
!IF (h0(i,j)/=-valmanq) THEN
      IF (h0(i,j)>-valmanq .and. hx(i,j)>-valmanq .and. hy(i,j)>-valmanq) THEN
       ii1=MAX(imetmin,MAX(i1(i,j),imetmin))
       ii2=MAX(imetmin,MIN(i1(i,j)+1,imetmax))
       jj1=MAX(jmetmin,MAX(j1(i,j),jmetmin))
       jj2=MAX(jmetmin,MIN(j1(i,j)+1,jmetmax))
       var1=xdp(i,j)*tabin(ii2,jj1)+(1.0_rsh-xdp(i,j))*tabin(ii1,jj1)
       var2=xdp(i,j)*tabin(ii2,jj2)+(1.0_rsh-xdp(i,j))*tabin(ii1,jj2)
       tabout(i,j)=(ydp(i,j)*var2+(1.0_rsh-ydp(i,j))*var1)
      ENDIF
     ENDIF
   END DO
!$OMP end do

  END SUBROUTINE tool_interp2d_sflx
