      SUBROUTINE SINK_LOCATOR1(SURF,DEM,COLS,ROWS,FLAG_SINK)


      ! THIS SUBROUTINE SEPARTES THE SINK CELLS FROM REST OF THE DEM
      ! CELLS. IT IS IMPORTANT TO NOTE THAT THE SINK CELLS OVER HERE ARE
      ! THOSE SINK CELLS THAT ARE NOT COVERED BY A LAKE.

      ! 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

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

      ! FLAG_SINK: GIVES LOCATION OF NON-LAKE SINK CELLS. GETS MODIFIED
      ! IN THIS SUBROUTINE


      IMPLICIT NONE

      INTEGER :: ROWS,COLS,I,J,II(9),JJ(9),ICLOW,X,A,B,
     &FLAG_SINK(COLS,ROWS) 
      DOUBLE PRECISION :: VALS(9),LAKE_DEPTH(COLS,ROWS),
     &LOW,SURF(COLS,ROWS),DEM(COLS,ROWS)

      LAKE_DEPTH=SURF-DEM

      DO 100 I=2,COLS-1
      DO 110 J=2,ROWS-1

      ! CONSIDER ALL CELLS WITH POSITIVE BASE ELEVATION. FOR ALL
      ! NON-POSITIVE BASE ELEVATIONS, THE FLAG_SINK MATRIX IS MARKED AS
      ! ZERO

      IF (DEM(I,J).GT.0) THEN

              ! 3-BY-3 NEIGHBOURHOOD OF THE CELL

              JJ=(/J-1,J-1,J-1,J,J,J,J+1,J+1,J+1/)
              II=(/I-1,I,I+1,I-1,I,I+1,I-1,I,I+1/)

               DO 300 X=1,9
                  A=II(X)
                  B=JJ(X)
                  VALS(X)=DEM(A,B)
300            CONTINUE

                  ICLOW=0
                  LOW=999999

                  ! ESTIMATING LOCATION OF LOWEST LYING CELL IN THE
                  ! 3-BY-3 NEIGHBOURHOOD

                  DO 800 X=1,9
                  IF (VALS(X).LT.LOW) THEN
                          ICLOW=X
                          LOW=VALS(X)
                  END IF
800               CONTINUE

                  ! IF THE LOWEST LYING CELL IS THE SOURCE CELL AND IT
                  ! IS FREE OF ANY LAKE, MARK THE CORRESPONDING LOCATION
                  ! IN FLAG_SINK MATRIX AS 1

                  IF (VALS(5).EQ.LOW) THEN
                          IF (LAKE_DEPTH(II(5),JJ(5)).EQ.0) THEN
                                  FLAG_SINK(I,J)=1
                          ELSE
                                  FLAG_SINK(I,J)=0
                          END IF
                  ELSE
                          FLAG_SINK(I,J)=0
                  END IF
      ELSE

              FLAG_SINK(I,J)=0
      END IF

110   CONTINUE
100   CONTINUE

      RETURN

      END SUBROUTINE SINK_LOCATOR1
