!******************** START FILE FLINT_Z.FOR ; GROUP SIGS2 ********************
!==============================================================================
!
!  FLINT_Z
!   INTERPOLATE ON A 2D ARRAY
!
SUBROUTINE FLINT_Z(X,Y,F,N,MTX,XMIN,XMAX,XLR,NX,YMIN,YMAX,YLR,&
     NY,IWARN)
!
!============
! idecl:  explicitize implicit INTEGER declarations:
  use iso_c_binding, only: fp => c_double
  IMPLICIT NONE
  INTEGER n,nx,ny,it,iwarn,ix,iy,ixp1,iyp1
!============
! idecl:  explicitize implicit REAL declarations:
  real(fp) xmin,xmax,xlr,ymin,ymax,ylr,zx,zy,zzx,zlogx,&
       zzy,zlogy,zf00,zf01,zf10,zf11
!============
  real(fp) MTX(NX,NY)
  real(fp),DIMENSION(n) :: X,Y,F
!
! Interpolates the table MTX(NX,NY) to find the value at (x,y).
! If XLR>0, then the X grid is equally spaced on a logarithmic scale:
!
!   X(i) = XMIN * (exp(XLR)) ** ( (i-1)/(NX-1) )
!
!   So X(i) ranges from XMIN to XMIN*exp(XLR), and XLR=log(xmax/xmin).
!
! For XLR<=0, the X grid is equally spaced on a linear scale from
! XMIN to XMAX.
!
! For YLR>0, Y is logarithmically spaced from YMIN to YMIN*exp(YLR)
! and for YLR<=0, Y is linearly spaced from YMIN to YMAX.
!
! Modified 7/26/00 (PCR) - vectorized.
!
  iwarn=0
!
  do it=1,n
!
     ZX=X(it)
     ZY=Y(it)
!
     if(zx .lt. xmin) then
        zzx=0.0_fp
     else
        IF(XLR.GT.0.0_fp) THEN
           ZLOGX=log(ZX/XMIN)
           ZZX=1.0_fp+ZLOGX/XLR * (NX-1)
        ELSE
           ZZX=1.0_fp+(ZX-XMIN)/(XMAX-XMIN) * (NX-1)
        ENDIF
     endif
!
     if(zy .lt. ymin) then
        zzy=0.0_fp
     else
        IF(YLR.GT.0.0_fp) THEN
           ZLOGY=log(ZY/YMIN)
           ZZY=1.0_fp+ZLOGY/YLR * (NY-1)
        ELSE
           ZZY=1.0_fp+(ZY-YMIN)/(YMAX-YMIN) * (NY-1)
        ENDIF
     endif
!
     IX=ZZX
     IY=ZZY
!  CHECK FOR POINT OUT OF BOUNDS OF ARRAY
     IF(IX.GE.1) GO TO 10
!
     IF(ZX.LT.(XMIN - 5.0e-7_fp)) THEN
        IWARN=1
     ENDIF
     IX=1
     ZZX=1.0_fp
!
10   CONTINUE
     IF(IY.GE.1) GO TO 20
!
     IF(ZY.LT.(YMIN - 5.0e-7_fp)) THEN
        IWARN=1
     ENDIF
     IY=1
     ZZY=1.0_fp
!
20   CONTINUE
     IF(IX.LT.NX) GO TO 30
!
     IF(ZX.GT.(XMAX + 5.0e-7_fp)) THEN
        IWARN=1
     ENDIF
     IX=NX-1
     ZZX=NX
!
30   CONTINUE
     IF(IY.LT.NY) GO TO 40
!
     IF(ZY.GT.(YMAX + 5.0e-7_fp)) THEN
        IWARN=1
     ENDIF
     IY=NY-1
     ZZY=NY
!
!---------------------------
!  OK
!
40   CONTINUE
     IXP1=IX+1
     IYP1=IY+1
     ZZX=ZZX-IX
     ZZY=ZZY-IY
     ZF00=(1._fp-ZZX)*(1._fp-ZZY)
     ZF01=(1._fp-ZZX)*ZZY
     ZF10=ZZX*(1._fp-ZZY)
     ZF11=ZZX*ZZY
!
!  INTERPOLATE
!
     F(it)=ZF00*MTX(IX,IY)+ZF01*MTX(IX,IYP1)+&
          ZF10*MTX(IXP1,IY)+ZF11*MTX(IXP1,IYP1)
!
  enddo
!
!
END SUBROUTINE FLINT_Z
!
!******************** END FILE FLINT.FOR ; GROUP SIGS2 ************************
! 26Jul2000 fgtok -s r8_precision.sub "r8con.csh conversion"
