      SUBROUTINE LAKE_DEL1(PP,QQ,MARKY,MARKY1,SURF,DEM,K1,K2,K3,K4,COLS,
     &ROWS)

      ! THIS SUBROUTINE DELINEATES A LAKE

      ! DESCRIPTION OF VARIABLES:

      ! PP,QQ: NUCLEUS OF THE LAKE. GETS MODIFIED IN THIS SUBROUTINE
      
      ! K1,K2,K3,K4: GIVES THE EXTENT OF THE LAKE. GETS MODIFIED IN THIS
      ! SUBROUTINE

      ! COLS,ROWS: GIVES ROWS AND COLUMNS OF THE STUDY AREA. REMAINS
      ! UNMODIFIED IN THIS SUBROUTINE

      ! MARKY: USED TO DELINEATE AND STORE A LAKE. SHOWS 1 FOR LAKE
      ! CELLS AND 0 FOR NON-LAKE CELLS

      ! MARKY1: USED TO STORE LAKES THAT HAVE BEEN ANALYSED IN A GIVEN
      ! TIMESTEP. GETS MODIFIED IN THIS SUBROUTINE

      ! SURF: CONTAINS SURFACE HEIGHT (IN METERS) OF EVERY GRID CELL.
      ! REMAINS UNMODIFIED IN THIS SUBROUTINE

      ! DEM: CONTAINS DEM SURFACE ELEVATION (IN METERS) FOR EVERY GRID
      ! CELL. REMAINS UNMODIFIED IN THIS SUBROUTINE 

      IMPLICIT NONE

      INTEGER :: PP,QQ,COLS,ROWS,MARKY(COLS,ROWS),MARKY1(COLS,ROWS),K1,
     &K2,K3,K4,COUNTY,I,J,II(9),JJ(9),INDVALX,P,COUNTP,N,A,B,I1,J1,
     &INDI(2)

      DOUBLE PRECISION :: SURF(COLS,ROWS),DEM(COLS,ROWS),VALS(9),
     &LAKE_DEPTH(COLS,ROWS),SURFY_LAK,LAKE_DEP_ISOL(COLS,ROWS),LAKE_VOL

      
      K1=0
      K2=0
      K3=0
      K4=0 

      MARKY=MARKY-MARKY
      LAKE_DEPTH=SURF-DEM

      COUNTY=0
      SURFY_LAK=SURF(PP,QQ) ! LAKE SURFACE ELEVATION OF THE LAKE

      ! LOCATING THE NEAREST LARGEST LAKE DEPTH. THIS ID DONE TO GET AS
      ! CLOSE TO THE REAL LAKE NUCLEUS AS POSSIBLE. THE CLOWER WE ARE TO
      ! THE TRUE NUCLEUS, THE BETTER THE PERFORMANCE OF THE SEARCH
      ! INSIDE THE SEARCH BOX.

      DO WHILE ((COUNTY.EQ.0).AND.(PP.GT.1).AND.(QQ.GT.1).AND.
     &(PP.LT.COLS).AND.(QQ.LT.ROWS))

      II=(/PP-1,PP-1,PP-1,PP,PP,PP,PP+1,PP+1,PP+1/)
      JJ=(/QQ-1,QQ,QQ+1,QQ-1,QQ,QQ+1,QQ-1,QQ,QQ+1/)

      DO 100 N=1,9
      A=II(N)
      B=JJ(N)
      VALS(N)=LAKE_DEPTH(A,B)
      IF ((VALS(N).GT.0).AND.(SURF(A,B).EQ.SURFY_LAK)) THEN 
              MARKY(A,B)=1
      END IF
100   CONTINUE
      INDVALX=MAXLOC(VALS,DIM=1)
      PP=II(INDVALX) ! COL NO. OF THE TENTATIVE NUCLEUS
      QQ=JJ(INDVALX) ! ROW NO. OF THE TENTATIVE NUCLEUS

      IF (INDVALX.EQ.5) COUNTY=1

      END DO

      COUNTY=0
      SURFY_LAK=SURF(PP,QQ) ! LAKE SURFACE ELEVATION OF THE LAKE

      ! K1,K2,K3,K4 REPRESENT THE CORNERS OF THE 120 BY 120 SEARCH BOX
      ! THAT IS CENTERED AROUND THE TENTATIVE LAKE NUCLEUS

      P=25
      COUNTP=1
      DO WHILE ((P.GE.0).AND.(COUNTP.EQ.1))
      IF (PP-P.GE.2) THEN
              K1=P
              COUNTP=0
      ELSE
              P=P-1
              COUNTP=1
      END IF
      END DO

      P=25
      COUNTP=1
      DO WHILE ((P.GE.0).AND.(COUNTP.EQ.1))
      IF (PP+P.LE.COLS-1) THEN
              K2=P
              COUNTP=0
      ELSE
              P=P-1
              COUNTP=1
      END IF
      END DO

      P=25
      COUNTP=1
      DO WHILE ((P.GE.0).AND.(COUNTP.EQ.1))
      IF (QQ-P.GE.2) THEN
              K3=P
              COUNTP=0
      ELSE
              P=P-1
              COUNTP=1
      END IF
      END DO

      P=25
      COUNTP=1
      DO WHILE ((P.GE.0).AND.(COUNTP.EQ.1))
      IF (QQ+P.LE.ROWS-1) THEN
              K4=P
              COUNTP=0
      ELSE
              P=P-1
              COUNTP=1
      END IF
      END DO

      ! DELINEATING THE LAKE WITHIN THE SEARCH WINDOW. ALL THE LAKE
      ! PIXELS WILL HAVE POSITIVE LAKE DEPTHS AND THE SAME SURFACE ELEVATION

      DO 110 I1=PP-K1,PP+K2
      DO 120 J1=QQ-K3,QQ+K4
      IF ((SURF(I1,J1).EQ.SURFY_LAK)) THEN
              MARKY(I1,J1)=1
              MARKY1(I1,J1)=1
      END IF
120   CONTINUE
110   CONTINUE

      ! LOCATING THE REAL LAKE NUCLEUS WHICH IS THE DEEPEST POINT IN THE
      ! LAKE

      WHERE (MARKY.EQ.0)
              LAKE_DEPTH=0
      END WHERE

      INDI=MAXLOC(LAKE_DEPTH,MASK=LAKE_DEPTH.GT.0)
      PP=INDI(1)
      QQ=INDI(2)

      RETURN

      END SUBROUTINE LAKE_DEL1


